]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
c862839b4cf27cbe75265baca4a82f27ce947943
[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 #ifdef HAVE_WCHAR_H
48 #include <wchar.h>
49 #endif
50
51 #if __GNUC__ >= 2
52 /* Define BFD64 here, even if our default architecture is 32 bit ELF
53 as this will allow us to read in and parse 64bit and 32bit ELF files.
54 Only do this if we believe that the compiler can support a 64 bit
55 data type. For now we only rely on GCC being able to do this. */
56 #define BFD64
57 #endif
58
59 #include "bfd.h"
60 #include "bucomm.h"
61 #include "elfcomm.h"
62 #include "dwarf.h"
63 #include "ctf-api.h"
64 #include "demangle.h"
65
66 #include "elf/common.h"
67 #include "elf/external.h"
68 #include "elf/internal.h"
69
70
71 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
72 we can obtain the H8 reloc numbers. We need these for the
73 get_reloc_size() function. We include h8.h again after defining
74 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
75
76 #include "elf/h8.h"
77 #undef _ELF_H8_H
78
79 /* Undo the effects of #including reloc-macros.h. */
80
81 #undef START_RELOC_NUMBERS
82 #undef RELOC_NUMBER
83 #undef FAKE_RELOC
84 #undef EMPTY_RELOC
85 #undef END_RELOC_NUMBERS
86 #undef _RELOC_MACROS_H
87
88 /* The following headers use the elf/reloc-macros.h file to
89 automatically generate relocation recognition functions
90 such as elf_mips_reloc_type() */
91
92 #define RELOC_MACROS_GEN_FUNC
93
94 #include "elf/aarch64.h"
95 #include "elf/alpha.h"
96 #include "elf/arc.h"
97 #include "elf/arm.h"
98 #include "elf/avr.h"
99 #include "elf/bfin.h"
100 #include "elf/cr16.h"
101 #include "elf/cris.h"
102 #include "elf/crx.h"
103 #include "elf/csky.h"
104 #include "elf/d10v.h"
105 #include "elf/d30v.h"
106 #include "elf/dlx.h"
107 #include "elf/bpf.h"
108 #include "elf/epiphany.h"
109 #include "elf/fr30.h"
110 #include "elf/frv.h"
111 #include "elf/ft32.h"
112 #include "elf/h8.h"
113 #include "elf/hppa.h"
114 #include "elf/i386.h"
115 #include "elf/i370.h"
116 #include "elf/i860.h"
117 #include "elf/i960.h"
118 #include "elf/ia64.h"
119 #include "elf/ip2k.h"
120 #include "elf/lm32.h"
121 #include "elf/iq2000.h"
122 #include "elf/m32c.h"
123 #include "elf/m32r.h"
124 #include "elf/m68k.h"
125 #include "elf/m68hc11.h"
126 #include "elf/s12z.h"
127 #include "elf/mcore.h"
128 #include "elf/mep.h"
129 #include "elf/metag.h"
130 #include "elf/microblaze.h"
131 #include "elf/mips.h"
132 #include "elf/mmix.h"
133 #include "elf/mn10200.h"
134 #include "elf/mn10300.h"
135 #include "elf/moxie.h"
136 #include "elf/mt.h"
137 #include "elf/msp430.h"
138 #include "elf/nds32.h"
139 #include "elf/nfp.h"
140 #include "elf/nios2.h"
141 #include "elf/or1k.h"
142 #include "elf/pj.h"
143 #include "elf/ppc.h"
144 #include "elf/ppc64.h"
145 #include "elf/pru.h"
146 #include "elf/riscv.h"
147 #include "elf/rl78.h"
148 #include "elf/rx.h"
149 #include "elf/s390.h"
150 #include "elf/score.h"
151 #include "elf/sh.h"
152 #include "elf/sparc.h"
153 #include "elf/spu.h"
154 #include "elf/tic6x.h"
155 #include "elf/tilegx.h"
156 #include "elf/tilepro.h"
157 #include "elf/v850.h"
158 #include "elf/vax.h"
159 #include "elf/visium.h"
160 #include "elf/wasm32.h"
161 #include "elf/x86-64.h"
162 #include "elf/xc16x.h"
163 #include "elf/xgate.h"
164 #include "elf/xstormy16.h"
165 #include "elf/xtensa.h"
166 #include "elf/z80.h"
167
168 #include "getopt.h"
169 #include "libiberty.h"
170 #include "safe-ctype.h"
171 #include "filenames.h"
172
173 #ifndef offsetof
174 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
175 #endif
176
177 typedef struct elf_section_list
178 {
179 Elf_Internal_Shdr * hdr;
180 struct elf_section_list * next;
181 } elf_section_list;
182
183 /* Flag bits indicating particular types of dump. */
184 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
185 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
186 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
187 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
188 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
189 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
190
191 typedef unsigned char dump_type;
192
193 /* A linked list of the section names for which dumps were requested. */
194 struct dump_list_entry
195 {
196 char * name;
197 dump_type type;
198 struct dump_list_entry * next;
199 };
200
201 /* A dynamic array of flags indicating for which sections a dump
202 has been requested via command line switches. */
203 struct dump_data
204 {
205 dump_type * dump_sects;
206 unsigned int num_dump_sects;
207 };
208
209 static struct dump_data cmdline;
210
211 static struct dump_list_entry * dump_sects_byname;
212
213 char * program_name = "readelf";
214
215 static bfd_boolean show_name = FALSE;
216 static bfd_boolean do_dynamic = FALSE;
217 static bfd_boolean do_syms = FALSE;
218 static bfd_boolean do_dyn_syms = FALSE;
219 static bfd_boolean do_lto_syms = FALSE;
220 static bfd_boolean do_reloc = FALSE;
221 static bfd_boolean do_sections = FALSE;
222 static bfd_boolean do_section_groups = FALSE;
223 static bfd_boolean do_section_details = FALSE;
224 static bfd_boolean do_segments = FALSE;
225 static bfd_boolean do_unwind = FALSE;
226 static bfd_boolean do_using_dynamic = FALSE;
227 static bfd_boolean do_header = FALSE;
228 static bfd_boolean do_dump = FALSE;
229 static bfd_boolean do_version = FALSE;
230 static bfd_boolean do_histogram = FALSE;
231 static bfd_boolean do_debugging = FALSE;
232 static bfd_boolean do_ctf = FALSE;
233 static bfd_boolean do_arch = FALSE;
234 static bfd_boolean do_notes = FALSE;
235 static bfd_boolean do_archive_index = FALSE;
236 static bfd_boolean check_all = FALSE;
237 static bfd_boolean is_32bit_elf = FALSE;
238 static bfd_boolean decompress_dumps = FALSE;
239 static bfd_boolean do_not_show_symbol_truncation = FALSE;
240 static bfd_boolean do_demangle = FALSE; /* Pretty print C++ symbol names. */
241 static bfd_boolean process_links = FALSE;
242 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
243
244 static char *dump_ctf_parent_name;
245 static char *dump_ctf_symtab_name;
246 static char *dump_ctf_strtab_name;
247
248 struct group_list
249 {
250 struct group_list * next;
251 unsigned int section_index;
252 };
253
254 struct group
255 {
256 struct group_list * root;
257 unsigned int group_index;
258 };
259
260 typedef struct filedata
261 {
262 const char * file_name;
263 bfd_boolean is_separate;
264 FILE * handle;
265 bfd_size_type file_size;
266 Elf_Internal_Ehdr file_header;
267 Elf_Internal_Shdr * section_headers;
268 Elf_Internal_Phdr * program_headers;
269 char * string_table;
270 unsigned long string_table_length;
271 unsigned long archive_file_offset;
272 unsigned long archive_file_size;
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[PATH_MAX];
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 DEC,
318 DEC_5,
319 UNSIGNED,
320 PREFIX_HEX,
321 FULL_HEX,
322 LONG_HEX
323 }
324 print_mode;
325
326 /* Versioned symbol info. */
327 enum versioned_symbol_info
328 {
329 symbol_undefined,
330 symbol_hidden,
331 symbol_public
332 };
333
334 static const char * get_symbol_version_string
335 (Filedata *, bfd_boolean, const char *, unsigned long, unsigned,
336 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
337
338 #define UNKNOWN -1
339
340 #define SECTION_NAME(X) \
341 (filedata->string_table + (X)->sh_name)
342
343 #define SECTION_NAME_VALID(X) \
344 ((X) != NULL \
345 && filedata->string_table != NULL \
346 && (X)->sh_name < filedata->string_table_length)
347
348 #define SECTION_NAME_PRINT(X) \
349 ((X) == NULL ? _("<none>") \
350 : filedata->string_table == NULL ? _("<no-strings>") \
351 : (X)->sh_name >= filedata->string_table_length ? _("<corrupt>") \
352 : filedata->string_table + (X)->sh_name)
353
354 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
355
356 #define GET_ELF_SYMBOLS(file, section, sym_count) \
357 (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count) \
358 : get_64bit_elf_symbols (file, section, sym_count))
359
360 #define VALID_SYMBOL_NAME(strtab, strtab_size, offset) \
361 (strtab != NULL && offset < strtab_size)
362 #define VALID_DYNAMIC_NAME(filedata, offset) \
363 VALID_SYMBOL_NAME (filedata->dynamic_strings, \
364 filedata->dynamic_strings_length, offset)
365 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
366 already been called and verified that the string exists. */
367 #define GET_DYNAMIC_NAME(filedata, offset) \
368 (filedata->dynamic_strings + offset)
369
370 #define REMOVE_ARCH_BITS(ADDR) \
371 do \
372 { \
373 if (filedata->file_header.e_machine == EM_ARM) \
374 (ADDR) &= ~1; \
375 } \
376 while (0)
377
378 /* Get the correct GNU hash section name. */
379 #define GNU_HASH_SECTION_NAME(filedata) \
380 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
381 \f
382 /* Print a BFD_VMA to an internal buffer, for use in error messages.
383 BFD_FMA_FMT can't be used in translated strings. */
384
385 static const char *
386 bfd_vmatoa (char *fmtch, bfd_vma value)
387 {
388 /* bfd_vmatoa is used more then once in a printf call for output.
389 Cycle through an array of buffers. */
390 static int buf_pos = 0;
391 static struct bfd_vmatoa_buf
392 {
393 char place[64];
394 } buf[4];
395 char *ret;
396 char fmt[32];
397
398 ret = buf[buf_pos++].place;
399 buf_pos %= ARRAY_SIZE (buf);
400
401 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
402 snprintf (ret, sizeof (buf[0].place), fmt, value);
403 return ret;
404 }
405
406 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
407 OFFSET + the offset of the current archive member, if we are examining an
408 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
409 allocate a buffer using malloc and fill that. In either case return the
410 pointer to the start of the retrieved data or NULL if something went wrong.
411 If something does go wrong and REASON is not NULL then emit an error
412 message using REASON as part of the context. */
413
414 static void *
415 get_data (void * var,
416 Filedata * filedata,
417 unsigned long offset,
418 bfd_size_type size,
419 bfd_size_type nmemb,
420 const char * reason)
421 {
422 void * mvar;
423 bfd_size_type amt = size * nmemb;
424
425 if (size == 0 || nmemb == 0)
426 return NULL;
427
428 /* If the size_t type is smaller than the bfd_size_type, eg because
429 you are building a 32-bit tool on a 64-bit host, then make sure
430 that when the sizes are cast to (size_t) no information is lost. */
431 if ((size_t) size != size
432 || (size_t) nmemb != nmemb
433 || (size_t) amt != amt)
434 {
435 if (reason)
436 error (_("Size truncation prevents reading %s"
437 " elements of size %s for %s\n"),
438 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
439 return NULL;
440 }
441
442 /* Check for size overflow. */
443 if (amt / size != nmemb || (size_t) amt + 1 == 0)
444 {
445 if (reason)
446 error (_("Size overflow prevents reading %s"
447 " elements of size %s for %s\n"),
448 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
449 return NULL;
450 }
451
452 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
453 attempting to allocate memory when the read is bound to fail. */
454 if (filedata->archive_file_offset > filedata->file_size
455 || offset > filedata->file_size - filedata->archive_file_offset
456 || amt > filedata->file_size - filedata->archive_file_offset - offset)
457 {
458 if (reason)
459 error (_("Reading %s bytes extends past end of file for %s\n"),
460 bfd_vmatoa ("u", amt), reason);
461 return NULL;
462 }
463
464 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
465 SEEK_SET))
466 {
467 if (reason)
468 error (_("Unable to seek to 0x%lx for %s\n"),
469 filedata->archive_file_offset + offset, reason);
470 return NULL;
471 }
472
473 mvar = var;
474 if (mvar == NULL)
475 {
476 /* + 1 so that we can '\0' terminate invalid string table sections. */
477 mvar = malloc ((size_t) amt + 1);
478
479 if (mvar == NULL)
480 {
481 if (reason)
482 error (_("Out of memory allocating %s bytes for %s\n"),
483 bfd_vmatoa ("u", amt), reason);
484 return NULL;
485 }
486
487 ((char *) mvar)[amt] = '\0';
488 }
489
490 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
491 {
492 if (reason)
493 error (_("Unable to read in %s bytes of %s\n"),
494 bfd_vmatoa ("u", amt), reason);
495 if (mvar != var)
496 free (mvar);
497 return NULL;
498 }
499
500 return mvar;
501 }
502
503 /* Print a VMA value in the MODE specified.
504 Returns the number of characters displayed. */
505
506 static unsigned int
507 print_vma (bfd_vma vma, print_mode mode)
508 {
509 unsigned int nc = 0;
510
511 switch (mode)
512 {
513 case FULL_HEX:
514 nc = printf ("0x");
515 /* Fall through. */
516 case LONG_HEX:
517 #ifdef BFD64
518 if (is_32bit_elf)
519 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
520 #endif
521 printf_vma (vma);
522 return nc + 16;
523
524 case DEC_5:
525 if (vma <= 99999)
526 return printf ("%5" BFD_VMA_FMT "d", vma);
527 /* Fall through. */
528 case PREFIX_HEX:
529 nc = printf ("0x");
530 /* Fall through. */
531 case HEX:
532 return nc + printf ("%" BFD_VMA_FMT "x", vma);
533
534 case DEC:
535 return printf ("%" BFD_VMA_FMT "d", vma);
536
537 case UNSIGNED:
538 return printf ("%" BFD_VMA_FMT "u", vma);
539
540 default:
541 /* FIXME: Report unrecognised mode ? */
542 return 0;
543 }
544 }
545
546 /* Display a symbol on stdout. Handles the display of control characters and
547 multibye characters (assuming the host environment supports them).
548
549 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
550
551 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
552 abs(WIDTH) - 5 characters followed by "[...]".
553
554 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
555 padding as necessary.
556
557 Returns the number of emitted characters. */
558
559 static unsigned int
560 print_symbol (signed int width, const char * symbol)
561 {
562 bfd_boolean extra_padding = FALSE;
563 bfd_boolean do_dots = FALSE;
564 signed int num_printed = 0;
565 #ifdef HAVE_MBSTATE_T
566 mbstate_t state;
567 #endif
568 unsigned int width_remaining;
569 const void * alloced_symbol = NULL;
570
571 if (width < 0)
572 {
573 /* Keep the width positive. This helps the code below. */
574 width = - width;
575 extra_padding = TRUE;
576 }
577 else if (width == 0)
578 return 0;
579
580 if (do_wide)
581 /* Set the remaining width to a very large value.
582 This simplifies the code below. */
583 width_remaining = INT_MAX;
584 else
585 {
586 width_remaining = width;
587 if (! do_not_show_symbol_truncation
588 && (int) strlen (symbol) > width)
589 {
590 width_remaining -= 5;
591 if ((int) width_remaining < 0)
592 width_remaining = 0;
593 do_dots = TRUE;
594 }
595 }
596
597 #ifdef HAVE_MBSTATE_T
598 /* Initialise the multibyte conversion state. */
599 memset (& state, 0, sizeof (state));
600 #endif
601
602 if (do_demangle && *symbol)
603 {
604 const char * res = cplus_demangle (symbol, demangle_flags);
605
606 if (res != NULL)
607 alloced_symbol = symbol = res;
608 }
609
610 while (width_remaining)
611 {
612 size_t n;
613 const char c = *symbol++;
614
615 if (c == 0)
616 break;
617
618 /* Do not print control characters directly as they can affect terminal
619 settings. Such characters usually appear in the names generated
620 by the assembler for local labels. */
621 if (ISCNTRL (c))
622 {
623 if (width_remaining < 2)
624 break;
625
626 printf ("^%c", c + 0x40);
627 width_remaining -= 2;
628 num_printed += 2;
629 }
630 else if (ISPRINT (c))
631 {
632 putchar (c);
633 width_remaining --;
634 num_printed ++;
635 }
636 else
637 {
638 #ifdef HAVE_MBSTATE_T
639 wchar_t w;
640 #endif
641 /* Let printf do the hard work of displaying multibyte characters. */
642 printf ("%.1s", symbol - 1);
643 width_remaining --;
644 num_printed ++;
645
646 #ifdef HAVE_MBSTATE_T
647 /* Try to find out how many bytes made up the character that was
648 just printed. Advance the symbol pointer past the bytes that
649 were displayed. */
650 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
651 #else
652 n = 1;
653 #endif
654 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
655 symbol += (n - 1);
656 }
657 }
658
659 if (do_dots)
660 num_printed += printf ("[...]");
661
662 if (extra_padding && num_printed < width)
663 {
664 /* Fill in the remaining spaces. */
665 printf ("%-*s", width - num_printed, " ");
666 num_printed = width;
667 }
668
669 free ((void *) alloced_symbol);
670 return num_printed;
671 }
672
673 /* Returns a pointer to a static buffer containing a printable version of
674 the given section's name. Like print_symbol, except that it does not try
675 to print multibyte characters, it just interprets them as hex values. */
676
677 static const char *
678 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
679 {
680 #define MAX_PRINT_SEC_NAME_LEN 256
681 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
682 const char * name = SECTION_NAME_PRINT (sec);
683 char * buf = sec_name_buf;
684 char c;
685 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
686
687 while ((c = * name ++) != 0)
688 {
689 if (ISCNTRL (c))
690 {
691 if (remaining < 2)
692 break;
693
694 * buf ++ = '^';
695 * buf ++ = c + 0x40;
696 remaining -= 2;
697 }
698 else if (ISPRINT (c))
699 {
700 * buf ++ = c;
701 remaining -= 1;
702 }
703 else
704 {
705 static char hex[17] = "0123456789ABCDEF";
706
707 if (remaining < 4)
708 break;
709 * buf ++ = '<';
710 * buf ++ = hex[(c & 0xf0) >> 4];
711 * buf ++ = hex[c & 0x0f];
712 * buf ++ = '>';
713 remaining -= 4;
714 }
715
716 if (remaining == 0)
717 break;
718 }
719
720 * buf = 0;
721 return sec_name_buf;
722 }
723
724 static const char *
725 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
726 {
727 if (ndx >= filedata->file_header.e_shnum)
728 return _("<corrupt>");
729
730 return printable_section_name (filedata, filedata->section_headers + ndx);
731 }
732
733 /* Return a pointer to section NAME, or NULL if no such section exists. */
734
735 static Elf_Internal_Shdr *
736 find_section (Filedata * filedata, const char * name)
737 {
738 unsigned int i;
739
740 if (filedata->section_headers == NULL)
741 return NULL;
742
743 for (i = 0; i < filedata->file_header.e_shnum; i++)
744 if (SECTION_NAME_VALID (filedata->section_headers + i)
745 && streq (SECTION_NAME (filedata->section_headers + i), name))
746 return filedata->section_headers + i;
747
748 return NULL;
749 }
750
751 /* Return a pointer to a section containing ADDR, or NULL if no such
752 section exists. */
753
754 static Elf_Internal_Shdr *
755 find_section_by_address (Filedata * filedata, bfd_vma addr)
756 {
757 unsigned int i;
758
759 if (filedata->section_headers == NULL)
760 return NULL;
761
762 for (i = 0; i < filedata->file_header.e_shnum; i++)
763 {
764 Elf_Internal_Shdr *sec = filedata->section_headers + i;
765
766 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
767 return sec;
768 }
769
770 return NULL;
771 }
772
773 static Elf_Internal_Shdr *
774 find_section_by_type (Filedata * filedata, unsigned int type)
775 {
776 unsigned int i;
777
778 if (filedata->section_headers == NULL)
779 return NULL;
780
781 for (i = 0; i < filedata->file_header.e_shnum; i++)
782 {
783 Elf_Internal_Shdr *sec = filedata->section_headers + i;
784
785 if (sec->sh_type == type)
786 return sec;
787 }
788
789 return NULL;
790 }
791
792 /* Return a pointer to section NAME, or NULL if no such section exists,
793 restricted to the list of sections given in SET. */
794
795 static Elf_Internal_Shdr *
796 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
797 {
798 unsigned int i;
799
800 if (filedata->section_headers == NULL)
801 return NULL;
802
803 if (set != NULL)
804 {
805 while ((i = *set++) > 0)
806 {
807 /* See PR 21156 for a reproducer. */
808 if (i >= filedata->file_header.e_shnum)
809 continue; /* FIXME: Should we issue an error message ? */
810
811 if (SECTION_NAME_VALID (filedata->section_headers + i)
812 && streq (SECTION_NAME (filedata->section_headers + i), name))
813 return filedata->section_headers + i;
814 }
815 }
816
817 return find_section (filedata, name);
818 }
819
820 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
821 This OS has so many departures from the ELF standard that we test it at
822 many places. */
823
824 static inline bfd_boolean
825 is_ia64_vms (Filedata * filedata)
826 {
827 return filedata->file_header.e_machine == EM_IA_64
828 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
829 }
830
831 /* Guess the relocation size commonly used by the specific machines. */
832
833 static bfd_boolean
834 guess_is_rela (unsigned int e_machine)
835 {
836 switch (e_machine)
837 {
838 /* Targets that use REL relocations. */
839 case EM_386:
840 case EM_IAMCU:
841 case EM_960:
842 case EM_ARM:
843 case EM_D10V:
844 case EM_CYGNUS_D10V:
845 case EM_DLX:
846 case EM_MIPS:
847 case EM_MIPS_RS3_LE:
848 case EM_CYGNUS_M32R:
849 case EM_SCORE:
850 case EM_XGATE:
851 case EM_NFP:
852 case EM_BPF:
853 return FALSE;
854
855 /* Targets that use RELA relocations. */
856 case EM_68K:
857 case EM_860:
858 case EM_AARCH64:
859 case EM_ADAPTEVA_EPIPHANY:
860 case EM_ALPHA:
861 case EM_ALTERA_NIOS2:
862 case EM_ARC:
863 case EM_ARC_COMPACT:
864 case EM_ARC_COMPACT2:
865 case EM_AVR:
866 case EM_AVR_OLD:
867 case EM_BLACKFIN:
868 case EM_CR16:
869 case EM_CRIS:
870 case EM_CRX:
871 case EM_CSKY:
872 case EM_D30V:
873 case EM_CYGNUS_D30V:
874 case EM_FR30:
875 case EM_FT32:
876 case EM_CYGNUS_FR30:
877 case EM_CYGNUS_FRV:
878 case EM_H8S:
879 case EM_H8_300:
880 case EM_H8_300H:
881 case EM_IA_64:
882 case EM_IP2K:
883 case EM_IP2K_OLD:
884 case EM_IQ2000:
885 case EM_LATTICEMICO32:
886 case EM_M32C_OLD:
887 case EM_M32C:
888 case EM_M32R:
889 case EM_MCORE:
890 case EM_CYGNUS_MEP:
891 case EM_METAG:
892 case EM_MMIX:
893 case EM_MN10200:
894 case EM_CYGNUS_MN10200:
895 case EM_MN10300:
896 case EM_CYGNUS_MN10300:
897 case EM_MOXIE:
898 case EM_MSP430:
899 case EM_MSP430_OLD:
900 case EM_MT:
901 case EM_NDS32:
902 case EM_NIOS32:
903 case EM_OR1K:
904 case EM_PPC64:
905 case EM_PPC:
906 case EM_TI_PRU:
907 case EM_RISCV:
908 case EM_RL78:
909 case EM_RX:
910 case EM_S390:
911 case EM_S390_OLD:
912 case EM_SH:
913 case EM_SPARC:
914 case EM_SPARC32PLUS:
915 case EM_SPARCV9:
916 case EM_SPU:
917 case EM_TI_C6000:
918 case EM_TILEGX:
919 case EM_TILEPRO:
920 case EM_V800:
921 case EM_V850:
922 case EM_CYGNUS_V850:
923 case EM_VAX:
924 case EM_VISIUM:
925 case EM_X86_64:
926 case EM_L1OM:
927 case EM_K1OM:
928 case EM_XSTORMY16:
929 case EM_XTENSA:
930 case EM_XTENSA_OLD:
931 case EM_MICROBLAZE:
932 case EM_MICROBLAZE_OLD:
933 case EM_WEBASSEMBLY:
934 return TRUE;
935
936 case EM_68HC05:
937 case EM_68HC08:
938 case EM_68HC11:
939 case EM_68HC16:
940 case EM_FX66:
941 case EM_ME16:
942 case EM_MMA:
943 case EM_NCPU:
944 case EM_NDR1:
945 case EM_PCP:
946 case EM_ST100:
947 case EM_ST19:
948 case EM_ST7:
949 case EM_ST9PLUS:
950 case EM_STARCORE:
951 case EM_SVX:
952 case EM_TINYJ:
953 default:
954 warn (_("Don't know about relocations on this machine architecture\n"));
955 return FALSE;
956 }
957 }
958
959 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
960 Returns TRUE upon success, FALSE otherwise. If successful then a
961 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
962 and the number of relocs loaded is placed in *NRELASP. It is the caller's
963 responsibility to free the allocated buffer. */
964
965 static bfd_boolean
966 slurp_rela_relocs (Filedata * filedata,
967 unsigned long rel_offset,
968 unsigned long rel_size,
969 Elf_Internal_Rela ** relasp,
970 unsigned long * nrelasp)
971 {
972 Elf_Internal_Rela * relas;
973 size_t nrelas;
974 unsigned int i;
975
976 if (is_32bit_elf)
977 {
978 Elf32_External_Rela * erelas;
979
980 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
981 rel_size, _("32-bit relocation data"));
982 if (!erelas)
983 return FALSE;
984
985 nrelas = rel_size / sizeof (Elf32_External_Rela);
986
987 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
988 sizeof (Elf_Internal_Rela));
989
990 if (relas == NULL)
991 {
992 free (erelas);
993 error (_("out of memory parsing relocs\n"));
994 return FALSE;
995 }
996
997 for (i = 0; i < nrelas; i++)
998 {
999 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1000 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1001 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1002 }
1003
1004 free (erelas);
1005 }
1006 else
1007 {
1008 Elf64_External_Rela * erelas;
1009
1010 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1011 rel_size, _("64-bit relocation data"));
1012 if (!erelas)
1013 return FALSE;
1014
1015 nrelas = rel_size / sizeof (Elf64_External_Rela);
1016
1017 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1018 sizeof (Elf_Internal_Rela));
1019
1020 if (relas == NULL)
1021 {
1022 free (erelas);
1023 error (_("out of memory parsing relocs\n"));
1024 return FALSE;
1025 }
1026
1027 for (i = 0; i < nrelas; i++)
1028 {
1029 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1030 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1031 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1032
1033 /* The #ifdef BFD64 below is to prevent a compile time
1034 warning. We know that if we do not have a 64 bit data
1035 type that we will never execute this code anyway. */
1036 #ifdef BFD64
1037 if (filedata->file_header.e_machine == EM_MIPS
1038 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1039 {
1040 /* In little-endian objects, r_info isn't really a
1041 64-bit little-endian value: it has a 32-bit
1042 little-endian symbol index followed by four
1043 individual byte fields. Reorder INFO
1044 accordingly. */
1045 bfd_vma inf = relas[i].r_info;
1046 inf = (((inf & 0xffffffff) << 32)
1047 | ((inf >> 56) & 0xff)
1048 | ((inf >> 40) & 0xff00)
1049 | ((inf >> 24) & 0xff0000)
1050 | ((inf >> 8) & 0xff000000));
1051 relas[i].r_info = inf;
1052 }
1053 #endif /* BFD64 */
1054 }
1055
1056 free (erelas);
1057 }
1058
1059 *relasp = relas;
1060 *nrelasp = nrelas;
1061 return TRUE;
1062 }
1063
1064 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1065 Returns TRUE upon success, FALSE otherwise. If successful then a
1066 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1067 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1068 responsibility to free the allocated buffer. */
1069
1070 static bfd_boolean
1071 slurp_rel_relocs (Filedata * filedata,
1072 unsigned long rel_offset,
1073 unsigned long rel_size,
1074 Elf_Internal_Rela ** relsp,
1075 unsigned long * nrelsp)
1076 {
1077 Elf_Internal_Rela * rels;
1078 size_t nrels;
1079 unsigned int i;
1080
1081 if (is_32bit_elf)
1082 {
1083 Elf32_External_Rel * erels;
1084
1085 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1086 rel_size, _("32-bit relocation data"));
1087 if (!erels)
1088 return FALSE;
1089
1090 nrels = rel_size / sizeof (Elf32_External_Rel);
1091
1092 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1093
1094 if (rels == NULL)
1095 {
1096 free (erels);
1097 error (_("out of memory parsing relocs\n"));
1098 return FALSE;
1099 }
1100
1101 for (i = 0; i < nrels; i++)
1102 {
1103 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1104 rels[i].r_info = BYTE_GET (erels[i].r_info);
1105 rels[i].r_addend = 0;
1106 }
1107
1108 free (erels);
1109 }
1110 else
1111 {
1112 Elf64_External_Rel * erels;
1113
1114 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1115 rel_size, _("64-bit relocation data"));
1116 if (!erels)
1117 return FALSE;
1118
1119 nrels = rel_size / sizeof (Elf64_External_Rel);
1120
1121 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1122
1123 if (rels == NULL)
1124 {
1125 free (erels);
1126 error (_("out of memory parsing relocs\n"));
1127 return FALSE;
1128 }
1129
1130 for (i = 0; i < nrels; i++)
1131 {
1132 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1133 rels[i].r_info = BYTE_GET (erels[i].r_info);
1134 rels[i].r_addend = 0;
1135
1136 /* The #ifdef BFD64 below is to prevent a compile time
1137 warning. We know that if we do not have a 64 bit data
1138 type that we will never execute this code anyway. */
1139 #ifdef BFD64
1140 if (filedata->file_header.e_machine == EM_MIPS
1141 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1142 {
1143 /* In little-endian objects, r_info isn't really a
1144 64-bit little-endian value: it has a 32-bit
1145 little-endian symbol index followed by four
1146 individual byte fields. Reorder INFO
1147 accordingly. */
1148 bfd_vma inf = rels[i].r_info;
1149 inf = (((inf & 0xffffffff) << 32)
1150 | ((inf >> 56) & 0xff)
1151 | ((inf >> 40) & 0xff00)
1152 | ((inf >> 24) & 0xff0000)
1153 | ((inf >> 8) & 0xff000000));
1154 rels[i].r_info = inf;
1155 }
1156 #endif /* BFD64 */
1157 }
1158
1159 free (erels);
1160 }
1161
1162 *relsp = rels;
1163 *nrelsp = nrels;
1164 return TRUE;
1165 }
1166
1167 /* Returns the reloc type extracted from the reloc info field. */
1168
1169 static unsigned int
1170 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1171 {
1172 if (is_32bit_elf)
1173 return ELF32_R_TYPE (reloc_info);
1174
1175 switch (filedata->file_header.e_machine)
1176 {
1177 case EM_MIPS:
1178 /* Note: We assume that reloc_info has already been adjusted for us. */
1179 return ELF64_MIPS_R_TYPE (reloc_info);
1180
1181 case EM_SPARCV9:
1182 return ELF64_R_TYPE_ID (reloc_info);
1183
1184 default:
1185 return ELF64_R_TYPE (reloc_info);
1186 }
1187 }
1188
1189 /* Return the symbol index extracted from the reloc info field. */
1190
1191 static bfd_vma
1192 get_reloc_symindex (bfd_vma reloc_info)
1193 {
1194 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1195 }
1196
1197 static inline bfd_boolean
1198 uses_msp430x_relocs (Filedata * filedata)
1199 {
1200 return
1201 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1202 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1203 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1204 /* TI compiler uses ELFOSABI_NONE. */
1205 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1206 }
1207
1208 /* Display the contents of the relocation data found at the specified
1209 offset. */
1210
1211 static bfd_boolean
1212 dump_relocations (Filedata * filedata,
1213 unsigned long rel_offset,
1214 unsigned long rel_size,
1215 Elf_Internal_Sym * symtab,
1216 unsigned long nsyms,
1217 char * strtab,
1218 unsigned long strtablen,
1219 int is_rela,
1220 bfd_boolean is_dynsym)
1221 {
1222 unsigned long i;
1223 Elf_Internal_Rela * rels;
1224 bfd_boolean res = TRUE;
1225
1226 if (is_rela == UNKNOWN)
1227 is_rela = guess_is_rela (filedata->file_header.e_machine);
1228
1229 if (is_rela)
1230 {
1231 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1232 return FALSE;
1233 }
1234 else
1235 {
1236 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1237 return FALSE;
1238 }
1239
1240 if (is_32bit_elf)
1241 {
1242 if (is_rela)
1243 {
1244 if (do_wide)
1245 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1246 else
1247 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1248 }
1249 else
1250 {
1251 if (do_wide)
1252 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1253 else
1254 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1255 }
1256 }
1257 else
1258 {
1259 if (is_rela)
1260 {
1261 if (do_wide)
1262 printf (_(" Offset Info Type Symbol's 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 Symbol's Value Symbol's Name\n"));
1270 else
1271 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1272 }
1273 }
1274
1275 for (i = 0; i < rel_size; i++)
1276 {
1277 const char * rtype;
1278 bfd_vma offset;
1279 bfd_vma inf;
1280 bfd_vma symtab_index;
1281 bfd_vma type;
1282
1283 offset = rels[i].r_offset;
1284 inf = rels[i].r_info;
1285
1286 type = get_reloc_type (filedata, inf);
1287 symtab_index = get_reloc_symindex (inf);
1288
1289 if (is_32bit_elf)
1290 {
1291 printf ("%8.8lx %8.8lx ",
1292 (unsigned long) offset & 0xffffffff,
1293 (unsigned long) inf & 0xffffffff);
1294 }
1295 else
1296 {
1297 printf (do_wide
1298 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1299 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
1300 offset, inf);
1301 }
1302
1303 switch (filedata->file_header.e_machine)
1304 {
1305 default:
1306 rtype = NULL;
1307 break;
1308
1309 case EM_AARCH64:
1310 rtype = elf_aarch64_reloc_type (type);
1311 break;
1312
1313 case EM_M32R:
1314 case EM_CYGNUS_M32R:
1315 rtype = elf_m32r_reloc_type (type);
1316 break;
1317
1318 case EM_386:
1319 case EM_IAMCU:
1320 rtype = elf_i386_reloc_type (type);
1321 break;
1322
1323 case EM_68HC11:
1324 case EM_68HC12:
1325 rtype = elf_m68hc11_reloc_type (type);
1326 break;
1327
1328 case EM_S12Z:
1329 rtype = elf_s12z_reloc_type (type);
1330 break;
1331
1332 case EM_68K:
1333 rtype = elf_m68k_reloc_type (type);
1334 break;
1335
1336 case EM_960:
1337 rtype = elf_i960_reloc_type (type);
1338 break;
1339
1340 case EM_AVR:
1341 case EM_AVR_OLD:
1342 rtype = elf_avr_reloc_type (type);
1343 break;
1344
1345 case EM_OLD_SPARCV9:
1346 case EM_SPARC32PLUS:
1347 case EM_SPARCV9:
1348 case EM_SPARC:
1349 rtype = elf_sparc_reloc_type (type);
1350 break;
1351
1352 case EM_SPU:
1353 rtype = elf_spu_reloc_type (type);
1354 break;
1355
1356 case EM_V800:
1357 rtype = v800_reloc_type (type);
1358 break;
1359 case EM_V850:
1360 case EM_CYGNUS_V850:
1361 rtype = v850_reloc_type (type);
1362 break;
1363
1364 case EM_D10V:
1365 case EM_CYGNUS_D10V:
1366 rtype = elf_d10v_reloc_type (type);
1367 break;
1368
1369 case EM_D30V:
1370 case EM_CYGNUS_D30V:
1371 rtype = elf_d30v_reloc_type (type);
1372 break;
1373
1374 case EM_DLX:
1375 rtype = elf_dlx_reloc_type (type);
1376 break;
1377
1378 case EM_SH:
1379 rtype = elf_sh_reloc_type (type);
1380 break;
1381
1382 case EM_MN10300:
1383 case EM_CYGNUS_MN10300:
1384 rtype = elf_mn10300_reloc_type (type);
1385 break;
1386
1387 case EM_MN10200:
1388 case EM_CYGNUS_MN10200:
1389 rtype = elf_mn10200_reloc_type (type);
1390 break;
1391
1392 case EM_FR30:
1393 case EM_CYGNUS_FR30:
1394 rtype = elf_fr30_reloc_type (type);
1395 break;
1396
1397 case EM_CYGNUS_FRV:
1398 rtype = elf_frv_reloc_type (type);
1399 break;
1400
1401 case EM_CSKY:
1402 rtype = elf_csky_reloc_type (type);
1403 break;
1404
1405 case EM_FT32:
1406 rtype = elf_ft32_reloc_type (type);
1407 break;
1408
1409 case EM_MCORE:
1410 rtype = elf_mcore_reloc_type (type);
1411 break;
1412
1413 case EM_MMIX:
1414 rtype = elf_mmix_reloc_type (type);
1415 break;
1416
1417 case EM_MOXIE:
1418 rtype = elf_moxie_reloc_type (type);
1419 break;
1420
1421 case EM_MSP430:
1422 if (uses_msp430x_relocs (filedata))
1423 {
1424 rtype = elf_msp430x_reloc_type (type);
1425 break;
1426 }
1427 /* Fall through. */
1428 case EM_MSP430_OLD:
1429 rtype = elf_msp430_reloc_type (type);
1430 break;
1431
1432 case EM_NDS32:
1433 rtype = elf_nds32_reloc_type (type);
1434 break;
1435
1436 case EM_PPC:
1437 rtype = elf_ppc_reloc_type (type);
1438 break;
1439
1440 case EM_PPC64:
1441 rtype = elf_ppc64_reloc_type (type);
1442 break;
1443
1444 case EM_MIPS:
1445 case EM_MIPS_RS3_LE:
1446 rtype = elf_mips_reloc_type (type);
1447 break;
1448
1449 case EM_RISCV:
1450 rtype = elf_riscv_reloc_type (type);
1451 break;
1452
1453 case EM_ALPHA:
1454 rtype = elf_alpha_reloc_type (type);
1455 break;
1456
1457 case EM_ARM:
1458 rtype = elf_arm_reloc_type (type);
1459 break;
1460
1461 case EM_ARC:
1462 case EM_ARC_COMPACT:
1463 case EM_ARC_COMPACT2:
1464 rtype = elf_arc_reloc_type (type);
1465 break;
1466
1467 case EM_PARISC:
1468 rtype = elf_hppa_reloc_type (type);
1469 break;
1470
1471 case EM_H8_300:
1472 case EM_H8_300H:
1473 case EM_H8S:
1474 rtype = elf_h8_reloc_type (type);
1475 break;
1476
1477 case EM_OR1K:
1478 rtype = elf_or1k_reloc_type (type);
1479 break;
1480
1481 case EM_PJ:
1482 case EM_PJ_OLD:
1483 rtype = elf_pj_reloc_type (type);
1484 break;
1485 case EM_IA_64:
1486 rtype = elf_ia64_reloc_type (type);
1487 break;
1488
1489 case EM_CRIS:
1490 rtype = elf_cris_reloc_type (type);
1491 break;
1492
1493 case EM_860:
1494 rtype = elf_i860_reloc_type (type);
1495 break;
1496
1497 case EM_X86_64:
1498 case EM_L1OM:
1499 case EM_K1OM:
1500 rtype = elf_x86_64_reloc_type (type);
1501 break;
1502
1503 case EM_S370:
1504 rtype = i370_reloc_type (type);
1505 break;
1506
1507 case EM_S390_OLD:
1508 case EM_S390:
1509 rtype = elf_s390_reloc_type (type);
1510 break;
1511
1512 case EM_SCORE:
1513 rtype = elf_score_reloc_type (type);
1514 break;
1515
1516 case EM_XSTORMY16:
1517 rtype = elf_xstormy16_reloc_type (type);
1518 break;
1519
1520 case EM_CRX:
1521 rtype = elf_crx_reloc_type (type);
1522 break;
1523
1524 case EM_VAX:
1525 rtype = elf_vax_reloc_type (type);
1526 break;
1527
1528 case EM_VISIUM:
1529 rtype = elf_visium_reloc_type (type);
1530 break;
1531
1532 case EM_BPF:
1533 rtype = elf_bpf_reloc_type (type);
1534 break;
1535
1536 case EM_ADAPTEVA_EPIPHANY:
1537 rtype = elf_epiphany_reloc_type (type);
1538 break;
1539
1540 case EM_IP2K:
1541 case EM_IP2K_OLD:
1542 rtype = elf_ip2k_reloc_type (type);
1543 break;
1544
1545 case EM_IQ2000:
1546 rtype = elf_iq2000_reloc_type (type);
1547 break;
1548
1549 case EM_XTENSA_OLD:
1550 case EM_XTENSA:
1551 rtype = elf_xtensa_reloc_type (type);
1552 break;
1553
1554 case EM_LATTICEMICO32:
1555 rtype = elf_lm32_reloc_type (type);
1556 break;
1557
1558 case EM_M32C_OLD:
1559 case EM_M32C:
1560 rtype = elf_m32c_reloc_type (type);
1561 break;
1562
1563 case EM_MT:
1564 rtype = elf_mt_reloc_type (type);
1565 break;
1566
1567 case EM_BLACKFIN:
1568 rtype = elf_bfin_reloc_type (type);
1569 break;
1570
1571 case EM_CYGNUS_MEP:
1572 rtype = elf_mep_reloc_type (type);
1573 break;
1574
1575 case EM_CR16:
1576 rtype = elf_cr16_reloc_type (type);
1577 break;
1578
1579 case EM_MICROBLAZE:
1580 case EM_MICROBLAZE_OLD:
1581 rtype = elf_microblaze_reloc_type (type);
1582 break;
1583
1584 case EM_RL78:
1585 rtype = elf_rl78_reloc_type (type);
1586 break;
1587
1588 case EM_RX:
1589 rtype = elf_rx_reloc_type (type);
1590 break;
1591
1592 case EM_METAG:
1593 rtype = elf_metag_reloc_type (type);
1594 break;
1595
1596 case EM_XC16X:
1597 case EM_C166:
1598 rtype = elf_xc16x_reloc_type (type);
1599 break;
1600
1601 case EM_TI_C6000:
1602 rtype = elf_tic6x_reloc_type (type);
1603 break;
1604
1605 case EM_TILEGX:
1606 rtype = elf_tilegx_reloc_type (type);
1607 break;
1608
1609 case EM_TILEPRO:
1610 rtype = elf_tilepro_reloc_type (type);
1611 break;
1612
1613 case EM_WEBASSEMBLY:
1614 rtype = elf_wasm32_reloc_type (type);
1615 break;
1616
1617 case EM_XGATE:
1618 rtype = elf_xgate_reloc_type (type);
1619 break;
1620
1621 case EM_ALTERA_NIOS2:
1622 rtype = elf_nios2_reloc_type (type);
1623 break;
1624
1625 case EM_TI_PRU:
1626 rtype = elf_pru_reloc_type (type);
1627 break;
1628
1629 case EM_NFP:
1630 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1631 rtype = elf_nfp3200_reloc_type (type);
1632 else
1633 rtype = elf_nfp_reloc_type (type);
1634 break;
1635
1636 case EM_Z80:
1637 rtype = elf_z80_reloc_type (type);
1638 break;
1639 }
1640
1641 if (rtype == NULL)
1642 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1643 else
1644 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1645
1646 if (filedata->file_header.e_machine == EM_ALPHA
1647 && rtype != NULL
1648 && streq (rtype, "R_ALPHA_LITUSE")
1649 && is_rela)
1650 {
1651 switch (rels[i].r_addend)
1652 {
1653 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1654 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1655 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1656 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1657 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1658 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1659 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1660 default: rtype = NULL;
1661 }
1662
1663 if (rtype)
1664 printf (" (%s)", rtype);
1665 else
1666 {
1667 putchar (' ');
1668 printf (_("<unknown addend: %lx>"),
1669 (unsigned long) rels[i].r_addend);
1670 res = FALSE;
1671 }
1672 }
1673 else if (symtab_index)
1674 {
1675 if (symtab == NULL || symtab_index >= nsyms)
1676 {
1677 error (_(" bad symbol index: %08lx in reloc\n"),
1678 (unsigned long) symtab_index);
1679 res = FALSE;
1680 }
1681 else
1682 {
1683 Elf_Internal_Sym * psym;
1684 const char * version_string;
1685 enum versioned_symbol_info sym_info;
1686 unsigned short vna_other;
1687
1688 psym = symtab + symtab_index;
1689
1690 version_string
1691 = get_symbol_version_string (filedata, is_dynsym,
1692 strtab, strtablen,
1693 symtab_index,
1694 psym,
1695 &sym_info,
1696 &vna_other);
1697
1698 printf (" ");
1699
1700 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1701 {
1702 const char * name;
1703 unsigned int len;
1704 unsigned int width = is_32bit_elf ? 8 : 14;
1705
1706 /* Relocations against GNU_IFUNC symbols do not use the value
1707 of the symbol as the address to relocate against. Instead
1708 they invoke the function named by the symbol and use its
1709 result as the address for relocation.
1710
1711 To indicate this to the user, do not display the value of
1712 the symbol in the "Symbols's Value" field. Instead show
1713 its name followed by () as a hint that the symbol is
1714 invoked. */
1715
1716 if (strtab == NULL
1717 || psym->st_name == 0
1718 || psym->st_name >= strtablen)
1719 name = "??";
1720 else
1721 name = strtab + psym->st_name;
1722
1723 len = print_symbol (width, name);
1724 if (version_string)
1725 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1726 version_string);
1727 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1728 }
1729 else
1730 {
1731 print_vma (psym->st_value, LONG_HEX);
1732
1733 printf (is_32bit_elf ? " " : " ");
1734 }
1735
1736 if (psym->st_name == 0)
1737 {
1738 const char * sec_name = "<null>";
1739 char name_buf[40];
1740
1741 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1742 {
1743 if (psym->st_shndx < filedata->file_header.e_shnum)
1744 sec_name = SECTION_NAME_PRINT (filedata->section_headers
1745 + psym->st_shndx);
1746 else if (psym->st_shndx == SHN_ABS)
1747 sec_name = "ABS";
1748 else if (psym->st_shndx == SHN_COMMON)
1749 sec_name = "COMMON";
1750 else if ((filedata->file_header.e_machine == EM_MIPS
1751 && psym->st_shndx == SHN_MIPS_SCOMMON)
1752 || (filedata->file_header.e_machine == EM_TI_C6000
1753 && psym->st_shndx == SHN_TIC6X_SCOMMON))
1754 sec_name = "SCOMMON";
1755 else if (filedata->file_header.e_machine == EM_MIPS
1756 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1757 sec_name = "SUNDEF";
1758 else if ((filedata->file_header.e_machine == EM_X86_64
1759 || filedata->file_header.e_machine == EM_L1OM
1760 || filedata->file_header.e_machine == EM_K1OM)
1761 && psym->st_shndx == SHN_X86_64_LCOMMON)
1762 sec_name = "LARGE_COMMON";
1763 else if (filedata->file_header.e_machine == EM_IA_64
1764 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1765 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1766 sec_name = "ANSI_COM";
1767 else if (is_ia64_vms (filedata)
1768 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1769 sec_name = "VMS_SYMVEC";
1770 else
1771 {
1772 sprintf (name_buf, "<section 0x%x>",
1773 (unsigned int) psym->st_shndx);
1774 sec_name = name_buf;
1775 }
1776 }
1777 print_symbol (22, sec_name);
1778 }
1779 else if (strtab == NULL)
1780 printf (_("<string table index: %3ld>"), psym->st_name);
1781 else if (psym->st_name >= strtablen)
1782 {
1783 error (_("<corrupt string table index: %3ld>\n"),
1784 psym->st_name);
1785 res = FALSE;
1786 }
1787 else
1788 {
1789 print_symbol (22, strtab + psym->st_name);
1790 if (version_string)
1791 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1792 version_string);
1793 }
1794
1795 if (is_rela)
1796 {
1797 bfd_vma off = rels[i].r_addend;
1798
1799 if ((bfd_signed_vma) off < 0)
1800 printf (" - %" BFD_VMA_FMT "x", - off);
1801 else
1802 printf (" + %" BFD_VMA_FMT "x", off);
1803 }
1804 }
1805 }
1806 else if (is_rela)
1807 {
1808 bfd_vma off = rels[i].r_addend;
1809
1810 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1811 if ((bfd_signed_vma) off < 0)
1812 printf ("-%" BFD_VMA_FMT "x", - off);
1813 else
1814 printf ("%" BFD_VMA_FMT "x", off);
1815 }
1816
1817 if (filedata->file_header.e_machine == EM_SPARCV9
1818 && rtype != NULL
1819 && streq (rtype, "R_SPARC_OLO10"))
1820 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1821
1822 putchar ('\n');
1823
1824 #ifdef BFD64
1825 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
1826 {
1827 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1828 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1829 const char * rtype2 = elf_mips_reloc_type (type2);
1830 const char * rtype3 = elf_mips_reloc_type (type3);
1831
1832 printf (" Type2: ");
1833
1834 if (rtype2 == NULL)
1835 printf (_("unrecognized: %-7lx"),
1836 (unsigned long) type2 & 0xffffffff);
1837 else
1838 printf ("%-17.17s", rtype2);
1839
1840 printf ("\n Type3: ");
1841
1842 if (rtype3 == NULL)
1843 printf (_("unrecognized: %-7lx"),
1844 (unsigned long) type3 & 0xffffffff);
1845 else
1846 printf ("%-17.17s", rtype3);
1847
1848 putchar ('\n');
1849 }
1850 #endif /* BFD64 */
1851 }
1852
1853 free (rels);
1854
1855 return res;
1856 }
1857
1858 static const char *
1859 get_aarch64_dynamic_type (unsigned long type)
1860 {
1861 switch (type)
1862 {
1863 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1864 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
1865 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
1866 default:
1867 return NULL;
1868 }
1869 }
1870
1871 static const char *
1872 get_mips_dynamic_type (unsigned long type)
1873 {
1874 switch (type)
1875 {
1876 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1877 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1878 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1879 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1880 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1881 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1882 case DT_MIPS_MSYM: return "MIPS_MSYM";
1883 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1884 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1885 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1886 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1887 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1888 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1889 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1890 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1891 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1892 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1893 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1894 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1895 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1896 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1897 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1898 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1899 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1900 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1901 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1902 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1903 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1904 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1905 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1906 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1907 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1908 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1909 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1910 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1911 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1912 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1913 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1914 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1915 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1916 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1917 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1918 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1919 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1920 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1921 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1922 case DT_MIPS_XHASH: return "MIPS_XHASH";
1923 default:
1924 return NULL;
1925 }
1926 }
1927
1928 static const char *
1929 get_sparc64_dynamic_type (unsigned long type)
1930 {
1931 switch (type)
1932 {
1933 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1934 default:
1935 return NULL;
1936 }
1937 }
1938
1939 static const char *
1940 get_ppc_dynamic_type (unsigned long type)
1941 {
1942 switch (type)
1943 {
1944 case DT_PPC_GOT: return "PPC_GOT";
1945 case DT_PPC_OPT: return "PPC_OPT";
1946 default:
1947 return NULL;
1948 }
1949 }
1950
1951 static const char *
1952 get_ppc64_dynamic_type (unsigned long type)
1953 {
1954 switch (type)
1955 {
1956 case DT_PPC64_GLINK: return "PPC64_GLINK";
1957 case DT_PPC64_OPD: return "PPC64_OPD";
1958 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1959 case DT_PPC64_OPT: return "PPC64_OPT";
1960 default:
1961 return NULL;
1962 }
1963 }
1964
1965 static const char *
1966 get_parisc_dynamic_type (unsigned long type)
1967 {
1968 switch (type)
1969 {
1970 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1971 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1972 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1973 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1974 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1975 case DT_HP_PREINIT: return "HP_PREINIT";
1976 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1977 case DT_HP_NEEDED: return "HP_NEEDED";
1978 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1979 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1980 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1981 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1982 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
1983 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
1984 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
1985 case DT_HP_FILTERED: return "HP_FILTERED";
1986 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
1987 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1988 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
1989 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
1990 case DT_PLT: return "PLT";
1991 case DT_PLT_SIZE: return "PLT_SIZE";
1992 case DT_DLT: return "DLT";
1993 case DT_DLT_SIZE: return "DLT_SIZE";
1994 default:
1995 return NULL;
1996 }
1997 }
1998
1999 static const char *
2000 get_ia64_dynamic_type (unsigned long type)
2001 {
2002 switch (type)
2003 {
2004 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2005 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2006 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2007 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2008 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2009 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2010 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2011 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2012 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2013 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2014 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2015 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2016 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2017 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2018 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2019 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2020 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2021 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2022 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2023 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2024 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2025 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2026 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2027 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2028 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2029 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2030 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2031 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2032 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2033 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2034 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2035 default:
2036 return NULL;
2037 }
2038 }
2039
2040 static const char *
2041 get_solaris_section_type (unsigned long type)
2042 {
2043 switch (type)
2044 {
2045 case 0x6fffffee: return "SUNW_ancillary";
2046 case 0x6fffffef: return "SUNW_capchain";
2047 case 0x6ffffff0: return "SUNW_capinfo";
2048 case 0x6ffffff1: return "SUNW_symsort";
2049 case 0x6ffffff2: return "SUNW_tlssort";
2050 case 0x6ffffff3: return "SUNW_LDYNSYM";
2051 case 0x6ffffff4: return "SUNW_dof";
2052 case 0x6ffffff5: return "SUNW_cap";
2053 case 0x6ffffff6: return "SUNW_SIGNATURE";
2054 case 0x6ffffff7: return "SUNW_ANNOTATE";
2055 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2056 case 0x6ffffff9: return "SUNW_DEBUG";
2057 case 0x6ffffffa: return "SUNW_move";
2058 case 0x6ffffffb: return "SUNW_COMDAT";
2059 case 0x6ffffffc: return "SUNW_syminfo";
2060 case 0x6ffffffd: return "SUNW_verdef";
2061 case 0x6ffffffe: return "SUNW_verneed";
2062 case 0x6fffffff: return "SUNW_versym";
2063 case 0x70000000: return "SPARC_GOTDATA";
2064 default: return NULL;
2065 }
2066 }
2067
2068 static const char *
2069 get_alpha_dynamic_type (unsigned long type)
2070 {
2071 switch (type)
2072 {
2073 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2074 default: return NULL;
2075 }
2076 }
2077
2078 static const char *
2079 get_score_dynamic_type (unsigned long type)
2080 {
2081 switch (type)
2082 {
2083 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2084 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2085 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2086 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2087 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2088 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2089 default: return NULL;
2090 }
2091 }
2092
2093 static const char *
2094 get_tic6x_dynamic_type (unsigned long type)
2095 {
2096 switch (type)
2097 {
2098 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2099 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2100 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2101 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2102 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2103 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2104 default: return NULL;
2105 }
2106 }
2107
2108 static const char *
2109 get_nios2_dynamic_type (unsigned long type)
2110 {
2111 switch (type)
2112 {
2113 case DT_NIOS2_GP: return "NIOS2_GP";
2114 default: return NULL;
2115 }
2116 }
2117
2118 static const char *
2119 get_solaris_dynamic_type (unsigned long type)
2120 {
2121 switch (type)
2122 {
2123 case 0x6000000d: return "SUNW_AUXILIARY";
2124 case 0x6000000e: return "SUNW_RTLDINF";
2125 case 0x6000000f: return "SUNW_FILTER";
2126 case 0x60000010: return "SUNW_CAP";
2127 case 0x60000011: return "SUNW_SYMTAB";
2128 case 0x60000012: return "SUNW_SYMSZ";
2129 case 0x60000013: return "SUNW_SORTENT";
2130 case 0x60000014: return "SUNW_SYMSORT";
2131 case 0x60000015: return "SUNW_SYMSORTSZ";
2132 case 0x60000016: return "SUNW_TLSSORT";
2133 case 0x60000017: return "SUNW_TLSSORTSZ";
2134 case 0x60000018: return "SUNW_CAPINFO";
2135 case 0x60000019: return "SUNW_STRPAD";
2136 case 0x6000001a: return "SUNW_CAPCHAIN";
2137 case 0x6000001b: return "SUNW_LDMACH";
2138 case 0x6000001d: return "SUNW_CAPCHAINENT";
2139 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2140 case 0x60000021: return "SUNW_PARENT";
2141 case 0x60000023: return "SUNW_ASLR";
2142 case 0x60000025: return "SUNW_RELAX";
2143 case 0x60000029: return "SUNW_NXHEAP";
2144 case 0x6000002b: return "SUNW_NXSTACK";
2145
2146 case 0x70000001: return "SPARC_REGISTER";
2147 case 0x7ffffffd: return "AUXILIARY";
2148 case 0x7ffffffe: return "USED";
2149 case 0x7fffffff: return "FILTER";
2150
2151 default: return NULL;
2152 }
2153 }
2154
2155 static const char *
2156 get_dynamic_type (Filedata * filedata, unsigned long type)
2157 {
2158 static char buff[64];
2159
2160 switch (type)
2161 {
2162 case DT_NULL: return "NULL";
2163 case DT_NEEDED: return "NEEDED";
2164 case DT_PLTRELSZ: return "PLTRELSZ";
2165 case DT_PLTGOT: return "PLTGOT";
2166 case DT_HASH: return "HASH";
2167 case DT_STRTAB: return "STRTAB";
2168 case DT_SYMTAB: return "SYMTAB";
2169 case DT_RELA: return "RELA";
2170 case DT_RELASZ: return "RELASZ";
2171 case DT_RELAENT: return "RELAENT";
2172 case DT_STRSZ: return "STRSZ";
2173 case DT_SYMENT: return "SYMENT";
2174 case DT_INIT: return "INIT";
2175 case DT_FINI: return "FINI";
2176 case DT_SONAME: return "SONAME";
2177 case DT_RPATH: return "RPATH";
2178 case DT_SYMBOLIC: return "SYMBOLIC";
2179 case DT_REL: return "REL";
2180 case DT_RELSZ: return "RELSZ";
2181 case DT_RELENT: return "RELENT";
2182 case DT_PLTREL: return "PLTREL";
2183 case DT_DEBUG: return "DEBUG";
2184 case DT_TEXTREL: return "TEXTREL";
2185 case DT_JMPREL: return "JMPREL";
2186 case DT_BIND_NOW: return "BIND_NOW";
2187 case DT_INIT_ARRAY: return "INIT_ARRAY";
2188 case DT_FINI_ARRAY: return "FINI_ARRAY";
2189 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2190 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2191 case DT_RUNPATH: return "RUNPATH";
2192 case DT_FLAGS: return "FLAGS";
2193
2194 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2195 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2196 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2197
2198 case DT_CHECKSUM: return "CHECKSUM";
2199 case DT_PLTPADSZ: return "PLTPADSZ";
2200 case DT_MOVEENT: return "MOVEENT";
2201 case DT_MOVESZ: return "MOVESZ";
2202 case DT_FEATURE: return "FEATURE";
2203 case DT_POSFLAG_1: return "POSFLAG_1";
2204 case DT_SYMINSZ: return "SYMINSZ";
2205 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2206
2207 case DT_ADDRRNGLO: return "ADDRRNGLO";
2208 case DT_CONFIG: return "CONFIG";
2209 case DT_DEPAUDIT: return "DEPAUDIT";
2210 case DT_AUDIT: return "AUDIT";
2211 case DT_PLTPAD: return "PLTPAD";
2212 case DT_MOVETAB: return "MOVETAB";
2213 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2214
2215 case DT_VERSYM: return "VERSYM";
2216
2217 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2218 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2219 case DT_RELACOUNT: return "RELACOUNT";
2220 case DT_RELCOUNT: return "RELCOUNT";
2221 case DT_FLAGS_1: return "FLAGS_1";
2222 case DT_VERDEF: return "VERDEF";
2223 case DT_VERDEFNUM: return "VERDEFNUM";
2224 case DT_VERNEED: return "VERNEED";
2225 case DT_VERNEEDNUM: return "VERNEEDNUM";
2226
2227 case DT_AUXILIARY: return "AUXILIARY";
2228 case DT_USED: return "USED";
2229 case DT_FILTER: return "FILTER";
2230
2231 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2232 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2233 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2234 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2235 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2236 case DT_GNU_HASH: return "GNU_HASH";
2237 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2238
2239 default:
2240 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2241 {
2242 const char * result;
2243
2244 switch (filedata->file_header.e_machine)
2245 {
2246 case EM_AARCH64:
2247 result = get_aarch64_dynamic_type (type);
2248 break;
2249 case EM_MIPS:
2250 case EM_MIPS_RS3_LE:
2251 result = get_mips_dynamic_type (type);
2252 break;
2253 case EM_SPARCV9:
2254 result = get_sparc64_dynamic_type (type);
2255 break;
2256 case EM_PPC:
2257 result = get_ppc_dynamic_type (type);
2258 break;
2259 case EM_PPC64:
2260 result = get_ppc64_dynamic_type (type);
2261 break;
2262 case EM_IA_64:
2263 result = get_ia64_dynamic_type (type);
2264 break;
2265 case EM_ALPHA:
2266 result = get_alpha_dynamic_type (type);
2267 break;
2268 case EM_SCORE:
2269 result = get_score_dynamic_type (type);
2270 break;
2271 case EM_TI_C6000:
2272 result = get_tic6x_dynamic_type (type);
2273 break;
2274 case EM_ALTERA_NIOS2:
2275 result = get_nios2_dynamic_type (type);
2276 break;
2277 default:
2278 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2279 result = get_solaris_dynamic_type (type);
2280 else
2281 result = NULL;
2282 break;
2283 }
2284
2285 if (result != NULL)
2286 return result;
2287
2288 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2289 }
2290 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2291 || (filedata->file_header.e_machine == EM_PARISC
2292 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2293 {
2294 const char * result;
2295
2296 switch (filedata->file_header.e_machine)
2297 {
2298 case EM_PARISC:
2299 result = get_parisc_dynamic_type (type);
2300 break;
2301 case EM_IA_64:
2302 result = get_ia64_dynamic_type (type);
2303 break;
2304 default:
2305 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2306 result = get_solaris_dynamic_type (type);
2307 else
2308 result = NULL;
2309 break;
2310 }
2311
2312 if (result != NULL)
2313 return result;
2314
2315 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2316 type);
2317 }
2318 else
2319 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2320
2321 return buff;
2322 }
2323 }
2324
2325 static char *
2326 get_file_type (unsigned e_type)
2327 {
2328 static char buff[64];
2329
2330 switch (e_type)
2331 {
2332 case ET_NONE: return _("NONE (None)");
2333 case ET_REL: return _("REL (Relocatable file)");
2334 case ET_EXEC: return _("EXEC (Executable file)");
2335 case ET_DYN: return _("DYN (Shared object file)");
2336 case ET_CORE: return _("CORE (Core file)");
2337
2338 default:
2339 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2340 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2341 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2342 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2343 else
2344 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2345 return buff;
2346 }
2347 }
2348
2349 static char *
2350 get_machine_name (unsigned e_machine)
2351 {
2352 static char buff[64]; /* XXX */
2353
2354 switch (e_machine)
2355 {
2356 /* Please keep this switch table sorted by increasing EM_ value. */
2357 /* 0 */
2358 case EM_NONE: return _("None");
2359 case EM_M32: return "WE32100";
2360 case EM_SPARC: return "Sparc";
2361 case EM_386: return "Intel 80386";
2362 case EM_68K: return "MC68000";
2363 case EM_88K: return "MC88000";
2364 case EM_IAMCU: return "Intel MCU";
2365 case EM_860: return "Intel 80860";
2366 case EM_MIPS: return "MIPS R3000";
2367 case EM_S370: return "IBM System/370";
2368 /* 10 */
2369 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2370 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2371 case EM_PARISC: return "HPPA";
2372 case EM_VPP550: return "Fujitsu VPP500";
2373 case EM_SPARC32PLUS: return "Sparc v8+" ;
2374 case EM_960: return "Intel 80960";
2375 case EM_PPC: return "PowerPC";
2376 /* 20 */
2377 case EM_PPC64: return "PowerPC64";
2378 case EM_S390_OLD:
2379 case EM_S390: return "IBM S/390";
2380 case EM_SPU: return "SPU";
2381 /* 30 */
2382 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2383 case EM_FR20: return "Fujitsu FR20";
2384 case EM_RH32: return "TRW RH32";
2385 case EM_MCORE: return "MCORE";
2386 /* 40 */
2387 case EM_ARM: return "ARM";
2388 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2389 case EM_SH: return "Renesas / SuperH SH";
2390 case EM_SPARCV9: return "Sparc v9";
2391 case EM_TRICORE: return "Siemens Tricore";
2392 case EM_ARC: return "ARC";
2393 case EM_H8_300: return "Renesas H8/300";
2394 case EM_H8_300H: return "Renesas H8/300H";
2395 case EM_H8S: return "Renesas H8S";
2396 case EM_H8_500: return "Renesas H8/500";
2397 /* 50 */
2398 case EM_IA_64: return "Intel IA-64";
2399 case EM_MIPS_X: return "Stanford MIPS-X";
2400 case EM_COLDFIRE: return "Motorola Coldfire";
2401 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2402 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2403 case EM_PCP: return "Siemens PCP";
2404 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2405 case EM_NDR1: return "Denso NDR1 microprocesspr";
2406 case EM_STARCORE: return "Motorola Star*Core processor";
2407 case EM_ME16: return "Toyota ME16 processor";
2408 /* 60 */
2409 case EM_ST100: return "STMicroelectronics ST100 processor";
2410 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2411 case EM_X86_64: return "Advanced Micro Devices X86-64";
2412 case EM_PDSP: return "Sony DSP processor";
2413 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2414 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2415 case EM_FX66: return "Siemens FX66 microcontroller";
2416 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2417 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2418 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2419 /* 70 */
2420 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2421 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2422 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2423 case EM_SVX: return "Silicon Graphics SVx";
2424 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2425 case EM_VAX: return "Digital VAX";
2426 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2427 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2428 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2429 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2430 /* 80 */
2431 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2432 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2433 case EM_PRISM: return "Vitesse Prism";
2434 case EM_AVR_OLD:
2435 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2436 case EM_CYGNUS_FR30:
2437 case EM_FR30: return "Fujitsu FR30";
2438 case EM_CYGNUS_D10V:
2439 case EM_D10V: return "d10v";
2440 case EM_CYGNUS_D30V:
2441 case EM_D30V: return "d30v";
2442 case EM_CYGNUS_V850:
2443 case EM_V850: return "Renesas V850";
2444 case EM_CYGNUS_M32R:
2445 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2446 case EM_CYGNUS_MN10300:
2447 case EM_MN10300: return "mn10300";
2448 /* 90 */
2449 case EM_CYGNUS_MN10200:
2450 case EM_MN10200: return "mn10200";
2451 case EM_PJ: return "picoJava";
2452 case EM_OR1K: return "OpenRISC 1000";
2453 case EM_ARC_COMPACT: return "ARCompact";
2454 case EM_XTENSA_OLD:
2455 case EM_XTENSA: return "Tensilica Xtensa Processor";
2456 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2457 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2458 case EM_NS32K: return "National Semiconductor 32000 series";
2459 case EM_TPC: return "Tenor Network TPC processor";
2460 case EM_SNP1K: return "Trebia SNP 1000 processor";
2461 /* 100 */
2462 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2463 case EM_IP2K_OLD:
2464 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2465 case EM_MAX: return "MAX Processor";
2466 case EM_CR: return "National Semiconductor CompactRISC";
2467 case EM_F2MC16: return "Fujitsu F2MC16";
2468 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2469 case EM_BLACKFIN: return "Analog Devices Blackfin";
2470 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2471 case EM_SEP: return "Sharp embedded microprocessor";
2472 case EM_ARCA: return "Arca RISC microprocessor";
2473 /* 110 */
2474 case EM_UNICORE: return "Unicore";
2475 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2476 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2477 case EM_ALTERA_NIOS2: return "Altera Nios II";
2478 case EM_CRX: return "National Semiconductor CRX microprocessor";
2479 case EM_XGATE: return "Motorola XGATE embedded processor";
2480 case EM_C166:
2481 case EM_XC16X: return "Infineon Technologies xc16x";
2482 case EM_M16C: return "Renesas M16C series microprocessors";
2483 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2484 case EM_CE: return "Freescale Communication Engine RISC core";
2485 /* 120 */
2486 case EM_M32C: return "Renesas M32c";
2487 /* 130 */
2488 case EM_TSK3000: return "Altium TSK3000 core";
2489 case EM_RS08: return "Freescale RS08 embedded processor";
2490 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2491 case EM_SCORE: return "SUNPLUS S+Core";
2492 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2493 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2494 case EM_LATTICEMICO32: return "Lattice Mico32";
2495 case EM_SE_C17: return "Seiko Epson C17 family";
2496 /* 140 */
2497 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2498 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2499 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2500 case EM_TI_PRU: return "TI PRU I/O processor";
2501 /* 160 */
2502 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2503 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2504 case EM_R32C: return "Renesas R32C series microprocessors";
2505 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2506 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2507 case EM_8051: return "Intel 8051 and variants";
2508 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2509 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2510 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2511 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2512 /* 170 */
2513 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2514 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2515 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2516 case EM_RX: return "Renesas RX";
2517 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2518 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2519 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2520 case EM_CR16:
2521 case EM_MICROBLAZE:
2522 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2523 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2524 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2525 /* 180 */
2526 case EM_L1OM: return "Intel L1OM";
2527 case EM_K1OM: return "Intel K1OM";
2528 case EM_INTEL182: return "Intel (reserved)";
2529 case EM_AARCH64: return "AArch64";
2530 case EM_ARM184: return "ARM (reserved)";
2531 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2532 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2533 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2534 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2535 /* 190 */
2536 case EM_CUDA: return "NVIDIA CUDA architecture";
2537 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2538 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2539 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2540 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2541 case EM_ARC_COMPACT2: return "ARCv2";
2542 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2543 case EM_RL78: return "Renesas RL78";
2544 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2545 case EM_78K0R: return "Renesas 78K0R";
2546 /* 200 */
2547 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2548 case EM_BA1: return "Beyond BA1 CPU architecture";
2549 case EM_BA2: return "Beyond BA2 CPU architecture";
2550 case EM_XCORE: return "XMOS xCORE processor family";
2551 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2552 /* 210 */
2553 case EM_KM32: return "KM211 KM32 32-bit processor";
2554 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2555 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2556 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2557 case EM_KVARC: return "KM211 KVARC processor";
2558 case EM_CDP: return "Paneve CDP architecture family";
2559 case EM_COGE: return "Cognitive Smart Memory Processor";
2560 case EM_COOL: return "Bluechip Systems CoolEngine";
2561 case EM_NORC: return "Nanoradio Optimized RISC";
2562 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2563 /* 220 */
2564 case EM_Z80: return "Zilog Z80";
2565 case EM_VISIUM: return "CDS VISIUMcore processor";
2566 case EM_FT32: return "FTDI Chip FT32";
2567 case EM_MOXIE: return "Moxie";
2568 case EM_AMDGPU: return "AMD GPU";
2569 /* 230 (all reserved) */
2570 /* 240 */
2571 case EM_RISCV: return "RISC-V";
2572 case EM_LANAI: return "Lanai 32-bit processor";
2573 case EM_CEVA: return "CEVA Processor Architecture Family";
2574 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2575 case EM_BPF: return "Linux BPF";
2576 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2577 case EM_IMG1: return "Imagination Technologies";
2578 /* 250 */
2579 case EM_NFP: return "Netronome Flow Processor";
2580 case EM_VE: return "NEC Vector Engine";
2581 case EM_CSKY: return "C-SKY";
2582 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2583 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2584 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2585 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2586 case EM_65816: return "WDC 65816/65C816";
2587 case EM_LOONGARCH: return "Loongson Loongarch";
2588 case EM_KF32: return "ChipON KungFu32";
2589
2590 /* Large numbers... */
2591 case EM_MT: return "Morpho Techologies MT processor";
2592 case EM_ALPHA: return "Alpha";
2593 case EM_WEBASSEMBLY: return "Web Assembly";
2594 case EM_DLX: return "OpenDLX";
2595 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2596 case EM_IQ2000: return "Vitesse IQ2000";
2597 case EM_M32C_OLD:
2598 case EM_NIOS32: return "Altera Nios";
2599 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2600 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2601 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2602 case EM_S12Z: return "Freescale S12Z";
2603
2604 default:
2605 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2606 return buff;
2607 }
2608 }
2609
2610 static void
2611 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2612 {
2613 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2614 other compilers don't specify an architecture type in the e_flags, and
2615 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2616 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2617 architectures.
2618
2619 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2620 but also sets a specific architecture type in the e_flags field.
2621
2622 However, when decoding the flags we don't worry if we see an
2623 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2624 ARCEM architecture type. */
2625
2626 switch (e_flags & EF_ARC_MACH_MSK)
2627 {
2628 /* We only expect these to occur for EM_ARC_COMPACT2. */
2629 case EF_ARC_CPU_ARCV2EM:
2630 strcat (buf, ", ARC EM");
2631 break;
2632 case EF_ARC_CPU_ARCV2HS:
2633 strcat (buf, ", ARC HS");
2634 break;
2635
2636 /* We only expect these to occur for EM_ARC_COMPACT. */
2637 case E_ARC_MACH_ARC600:
2638 strcat (buf, ", ARC600");
2639 break;
2640 case E_ARC_MACH_ARC601:
2641 strcat (buf, ", ARC601");
2642 break;
2643 case E_ARC_MACH_ARC700:
2644 strcat (buf, ", ARC700");
2645 break;
2646
2647 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2648 new ELF with new architecture being read by an old version of
2649 readelf, or (c) An ELF built with non-GNU compiler that does not
2650 set the architecture in the e_flags. */
2651 default:
2652 if (e_machine == EM_ARC_COMPACT)
2653 strcat (buf, ", Unknown ARCompact");
2654 else
2655 strcat (buf, ", Unknown ARC");
2656 break;
2657 }
2658
2659 switch (e_flags & EF_ARC_OSABI_MSK)
2660 {
2661 case E_ARC_OSABI_ORIG:
2662 strcat (buf, ", (ABI:legacy)");
2663 break;
2664 case E_ARC_OSABI_V2:
2665 strcat (buf, ", (ABI:v2)");
2666 break;
2667 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2668 case E_ARC_OSABI_V3:
2669 strcat (buf, ", v3 no-legacy-syscalls ABI");
2670 break;
2671 case E_ARC_OSABI_V4:
2672 strcat (buf, ", v4 ABI");
2673 break;
2674 default:
2675 strcat (buf, ", unrecognised ARC OSABI flag");
2676 break;
2677 }
2678 }
2679
2680 static void
2681 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2682 {
2683 unsigned eabi;
2684 bfd_boolean unknown = FALSE;
2685
2686 eabi = EF_ARM_EABI_VERSION (e_flags);
2687 e_flags &= ~ EF_ARM_EABIMASK;
2688
2689 /* Handle "generic" ARM flags. */
2690 if (e_flags & EF_ARM_RELEXEC)
2691 {
2692 strcat (buf, ", relocatable executable");
2693 e_flags &= ~ EF_ARM_RELEXEC;
2694 }
2695
2696 if (e_flags & EF_ARM_PIC)
2697 {
2698 strcat (buf, ", position independent");
2699 e_flags &= ~ EF_ARM_PIC;
2700 }
2701
2702 /* Now handle EABI specific flags. */
2703 switch (eabi)
2704 {
2705 default:
2706 strcat (buf, ", <unrecognized EABI>");
2707 if (e_flags)
2708 unknown = TRUE;
2709 break;
2710
2711 case EF_ARM_EABI_VER1:
2712 strcat (buf, ", Version1 EABI");
2713 while (e_flags)
2714 {
2715 unsigned flag;
2716
2717 /* Process flags one bit at a time. */
2718 flag = e_flags & - e_flags;
2719 e_flags &= ~ flag;
2720
2721 switch (flag)
2722 {
2723 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2724 strcat (buf, ", sorted symbol tables");
2725 break;
2726
2727 default:
2728 unknown = TRUE;
2729 break;
2730 }
2731 }
2732 break;
2733
2734 case EF_ARM_EABI_VER2:
2735 strcat (buf, ", Version2 EABI");
2736 while (e_flags)
2737 {
2738 unsigned flag;
2739
2740 /* Process flags one bit at a time. */
2741 flag = e_flags & - e_flags;
2742 e_flags &= ~ flag;
2743
2744 switch (flag)
2745 {
2746 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2747 strcat (buf, ", sorted symbol tables");
2748 break;
2749
2750 case EF_ARM_DYNSYMSUSESEGIDX:
2751 strcat (buf, ", dynamic symbols use segment index");
2752 break;
2753
2754 case EF_ARM_MAPSYMSFIRST:
2755 strcat (buf, ", mapping symbols precede others");
2756 break;
2757
2758 default:
2759 unknown = TRUE;
2760 break;
2761 }
2762 }
2763 break;
2764
2765 case EF_ARM_EABI_VER3:
2766 strcat (buf, ", Version3 EABI");
2767 break;
2768
2769 case EF_ARM_EABI_VER4:
2770 strcat (buf, ", Version4 EABI");
2771 while (e_flags)
2772 {
2773 unsigned flag;
2774
2775 /* Process flags one bit at a time. */
2776 flag = e_flags & - e_flags;
2777 e_flags &= ~ flag;
2778
2779 switch (flag)
2780 {
2781 case EF_ARM_BE8:
2782 strcat (buf, ", BE8");
2783 break;
2784
2785 case EF_ARM_LE8:
2786 strcat (buf, ", LE8");
2787 break;
2788
2789 default:
2790 unknown = TRUE;
2791 break;
2792 }
2793 }
2794 break;
2795
2796 case EF_ARM_EABI_VER5:
2797 strcat (buf, ", Version5 EABI");
2798 while (e_flags)
2799 {
2800 unsigned flag;
2801
2802 /* Process flags one bit at a time. */
2803 flag = e_flags & - e_flags;
2804 e_flags &= ~ flag;
2805
2806 switch (flag)
2807 {
2808 case EF_ARM_BE8:
2809 strcat (buf, ", BE8");
2810 break;
2811
2812 case EF_ARM_LE8:
2813 strcat (buf, ", LE8");
2814 break;
2815
2816 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
2817 strcat (buf, ", soft-float ABI");
2818 break;
2819
2820 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
2821 strcat (buf, ", hard-float ABI");
2822 break;
2823
2824 default:
2825 unknown = TRUE;
2826 break;
2827 }
2828 }
2829 break;
2830
2831 case EF_ARM_EABI_UNKNOWN:
2832 strcat (buf, ", GNU EABI");
2833 while (e_flags)
2834 {
2835 unsigned flag;
2836
2837 /* Process flags one bit at a time. */
2838 flag = e_flags & - e_flags;
2839 e_flags &= ~ flag;
2840
2841 switch (flag)
2842 {
2843 case EF_ARM_INTERWORK:
2844 strcat (buf, ", interworking enabled");
2845 break;
2846
2847 case EF_ARM_APCS_26:
2848 strcat (buf, ", uses APCS/26");
2849 break;
2850
2851 case EF_ARM_APCS_FLOAT:
2852 strcat (buf, ", uses APCS/float");
2853 break;
2854
2855 case EF_ARM_PIC:
2856 strcat (buf, ", position independent");
2857 break;
2858
2859 case EF_ARM_ALIGN8:
2860 strcat (buf, ", 8 bit structure alignment");
2861 break;
2862
2863 case EF_ARM_NEW_ABI:
2864 strcat (buf, ", uses new ABI");
2865 break;
2866
2867 case EF_ARM_OLD_ABI:
2868 strcat (buf, ", uses old ABI");
2869 break;
2870
2871 case EF_ARM_SOFT_FLOAT:
2872 strcat (buf, ", software FP");
2873 break;
2874
2875 case EF_ARM_VFP_FLOAT:
2876 strcat (buf, ", VFP");
2877 break;
2878
2879 case EF_ARM_MAVERICK_FLOAT:
2880 strcat (buf, ", Maverick FP");
2881 break;
2882
2883 default:
2884 unknown = TRUE;
2885 break;
2886 }
2887 }
2888 }
2889
2890 if (unknown)
2891 strcat (buf,_(", <unknown>"));
2892 }
2893
2894 static void
2895 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2896 {
2897 --size; /* Leave space for null terminator. */
2898
2899 switch (e_flags & EF_AVR_MACH)
2900 {
2901 case E_AVR_MACH_AVR1:
2902 strncat (buf, ", avr:1", size);
2903 break;
2904 case E_AVR_MACH_AVR2:
2905 strncat (buf, ", avr:2", size);
2906 break;
2907 case E_AVR_MACH_AVR25:
2908 strncat (buf, ", avr:25", size);
2909 break;
2910 case E_AVR_MACH_AVR3:
2911 strncat (buf, ", avr:3", size);
2912 break;
2913 case E_AVR_MACH_AVR31:
2914 strncat (buf, ", avr:31", size);
2915 break;
2916 case E_AVR_MACH_AVR35:
2917 strncat (buf, ", avr:35", size);
2918 break;
2919 case E_AVR_MACH_AVR4:
2920 strncat (buf, ", avr:4", size);
2921 break;
2922 case E_AVR_MACH_AVR5:
2923 strncat (buf, ", avr:5", size);
2924 break;
2925 case E_AVR_MACH_AVR51:
2926 strncat (buf, ", avr:51", size);
2927 break;
2928 case E_AVR_MACH_AVR6:
2929 strncat (buf, ", avr:6", size);
2930 break;
2931 case E_AVR_MACH_AVRTINY:
2932 strncat (buf, ", avr:100", size);
2933 break;
2934 case E_AVR_MACH_XMEGA1:
2935 strncat (buf, ", avr:101", size);
2936 break;
2937 case E_AVR_MACH_XMEGA2:
2938 strncat (buf, ", avr:102", size);
2939 break;
2940 case E_AVR_MACH_XMEGA3:
2941 strncat (buf, ", avr:103", size);
2942 break;
2943 case E_AVR_MACH_XMEGA4:
2944 strncat (buf, ", avr:104", size);
2945 break;
2946 case E_AVR_MACH_XMEGA5:
2947 strncat (buf, ", avr:105", size);
2948 break;
2949 case E_AVR_MACH_XMEGA6:
2950 strncat (buf, ", avr:106", size);
2951 break;
2952 case E_AVR_MACH_XMEGA7:
2953 strncat (buf, ", avr:107", size);
2954 break;
2955 default:
2956 strncat (buf, ", avr:<unknown>", size);
2957 break;
2958 }
2959
2960 size -= strlen (buf);
2961 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2962 strncat (buf, ", link-relax", size);
2963 }
2964
2965 static void
2966 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2967 {
2968 unsigned abi;
2969 unsigned arch;
2970 unsigned config;
2971 unsigned version;
2972 bfd_boolean has_fpu = FALSE;
2973 unsigned int r = 0;
2974
2975 static const char *ABI_STRINGS[] =
2976 {
2977 "ABI v0", /* use r5 as return register; only used in N1213HC */
2978 "ABI v1", /* use r0 as return register */
2979 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2980 "ABI v2fp", /* for FPU */
2981 "AABI",
2982 "ABI2 FP+"
2983 };
2984 static const char *VER_STRINGS[] =
2985 {
2986 "Andes ELF V1.3 or older",
2987 "Andes ELF V1.3.1",
2988 "Andes ELF V1.4"
2989 };
2990 static const char *ARCH_STRINGS[] =
2991 {
2992 "",
2993 "Andes Star v1.0",
2994 "Andes Star v2.0",
2995 "Andes Star v3.0",
2996 "Andes Star v3.0m"
2997 };
2998
2999 abi = EF_NDS_ABI & e_flags;
3000 arch = EF_NDS_ARCH & e_flags;
3001 config = EF_NDS_INST & e_flags;
3002 version = EF_NDS32_ELF_VERSION & e_flags;
3003
3004 memset (buf, 0, size);
3005
3006 switch (abi)
3007 {
3008 case E_NDS_ABI_V0:
3009 case E_NDS_ABI_V1:
3010 case E_NDS_ABI_V2:
3011 case E_NDS_ABI_V2FP:
3012 case E_NDS_ABI_AABI:
3013 case E_NDS_ABI_V2FP_PLUS:
3014 /* In case there are holes in the array. */
3015 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3016 break;
3017
3018 default:
3019 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3020 break;
3021 }
3022
3023 switch (version)
3024 {
3025 case E_NDS32_ELF_VER_1_2:
3026 case E_NDS32_ELF_VER_1_3:
3027 case E_NDS32_ELF_VER_1_4:
3028 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3029 break;
3030
3031 default:
3032 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3033 break;
3034 }
3035
3036 if (E_NDS_ABI_V0 == abi)
3037 {
3038 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3039 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3040 if (arch == E_NDS_ARCH_STAR_V1_0)
3041 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3042 return;
3043 }
3044
3045 switch (arch)
3046 {
3047 case E_NDS_ARCH_STAR_V1_0:
3048 case E_NDS_ARCH_STAR_V2_0:
3049 case E_NDS_ARCH_STAR_V3_0:
3050 case E_NDS_ARCH_STAR_V3_M:
3051 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3052 break;
3053
3054 default:
3055 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3056 /* ARCH version determines how the e_flags are interpreted.
3057 If it is unknown, we cannot proceed. */
3058 return;
3059 }
3060
3061 /* Newer ABI; Now handle architecture specific flags. */
3062 if (arch == E_NDS_ARCH_STAR_V1_0)
3063 {
3064 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3065 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3066
3067 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3068 r += snprintf (buf + r, size -r, ", MAC");
3069
3070 if (config & E_NDS32_HAS_DIV_INST)
3071 r += snprintf (buf + r, size -r, ", DIV");
3072
3073 if (config & E_NDS32_HAS_16BIT_INST)
3074 r += snprintf (buf + r, size -r, ", 16b");
3075 }
3076 else
3077 {
3078 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3079 {
3080 if (version <= E_NDS32_ELF_VER_1_3)
3081 r += snprintf (buf + r, size -r, ", [B8]");
3082 else
3083 r += snprintf (buf + r, size -r, ", EX9");
3084 }
3085
3086 if (config & E_NDS32_HAS_MAC_DX_INST)
3087 r += snprintf (buf + r, size -r, ", MAC_DX");
3088
3089 if (config & E_NDS32_HAS_DIV_DX_INST)
3090 r += snprintf (buf + r, size -r, ", DIV_DX");
3091
3092 if (config & E_NDS32_HAS_16BIT_INST)
3093 {
3094 if (version <= E_NDS32_ELF_VER_1_3)
3095 r += snprintf (buf + r, size -r, ", 16b");
3096 else
3097 r += snprintf (buf + r, size -r, ", IFC");
3098 }
3099 }
3100
3101 if (config & E_NDS32_HAS_EXT_INST)
3102 r += snprintf (buf + r, size -r, ", PERF1");
3103
3104 if (config & E_NDS32_HAS_EXT2_INST)
3105 r += snprintf (buf + r, size -r, ", PERF2");
3106
3107 if (config & E_NDS32_HAS_FPU_INST)
3108 {
3109 has_fpu = TRUE;
3110 r += snprintf (buf + r, size -r, ", FPU_SP");
3111 }
3112
3113 if (config & E_NDS32_HAS_FPU_DP_INST)
3114 {
3115 has_fpu = TRUE;
3116 r += snprintf (buf + r, size -r, ", FPU_DP");
3117 }
3118
3119 if (config & E_NDS32_HAS_FPU_MAC_INST)
3120 {
3121 has_fpu = TRUE;
3122 r += snprintf (buf + r, size -r, ", FPU_MAC");
3123 }
3124
3125 if (has_fpu)
3126 {
3127 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3128 {
3129 case E_NDS32_FPU_REG_8SP_4DP:
3130 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3131 break;
3132 case E_NDS32_FPU_REG_16SP_8DP:
3133 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3134 break;
3135 case E_NDS32_FPU_REG_32SP_16DP:
3136 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3137 break;
3138 case E_NDS32_FPU_REG_32SP_32DP:
3139 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3140 break;
3141 }
3142 }
3143
3144 if (config & E_NDS32_HAS_AUDIO_INST)
3145 r += snprintf (buf + r, size -r, ", AUDIO");
3146
3147 if (config & E_NDS32_HAS_STRING_INST)
3148 r += snprintf (buf + r, size -r, ", STR");
3149
3150 if (config & E_NDS32_HAS_REDUCED_REGS)
3151 r += snprintf (buf + r, size -r, ", 16REG");
3152
3153 if (config & E_NDS32_HAS_VIDEO_INST)
3154 {
3155 if (version <= E_NDS32_ELF_VER_1_3)
3156 r += snprintf (buf + r, size -r, ", VIDEO");
3157 else
3158 r += snprintf (buf + r, size -r, ", SATURATION");
3159 }
3160
3161 if (config & E_NDS32_HAS_ENCRIPT_INST)
3162 r += snprintf (buf + r, size -r, ", ENCRP");
3163
3164 if (config & E_NDS32_HAS_L2C_INST)
3165 r += snprintf (buf + r, size -r, ", L2C");
3166 }
3167
3168 static char *
3169 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3170 {
3171 static char buf[1024];
3172
3173 buf[0] = '\0';
3174
3175 if (e_flags)
3176 {
3177 switch (e_machine)
3178 {
3179 default:
3180 break;
3181
3182 case EM_ARC_COMPACT2:
3183 case EM_ARC_COMPACT:
3184 decode_ARC_machine_flags (e_flags, e_machine, buf);
3185 break;
3186
3187 case EM_ARM:
3188 decode_ARM_machine_flags (e_flags, buf);
3189 break;
3190
3191 case EM_AVR:
3192 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3193 break;
3194
3195 case EM_BLACKFIN:
3196 if (e_flags & EF_BFIN_PIC)
3197 strcat (buf, ", PIC");
3198
3199 if (e_flags & EF_BFIN_FDPIC)
3200 strcat (buf, ", FDPIC");
3201
3202 if (e_flags & EF_BFIN_CODE_IN_L1)
3203 strcat (buf, ", code in L1");
3204
3205 if (e_flags & EF_BFIN_DATA_IN_L1)
3206 strcat (buf, ", data in L1");
3207
3208 break;
3209
3210 case EM_CYGNUS_FRV:
3211 switch (e_flags & EF_FRV_CPU_MASK)
3212 {
3213 case EF_FRV_CPU_GENERIC:
3214 break;
3215
3216 default:
3217 strcat (buf, ", fr???");
3218 break;
3219
3220 case EF_FRV_CPU_FR300:
3221 strcat (buf, ", fr300");
3222 break;
3223
3224 case EF_FRV_CPU_FR400:
3225 strcat (buf, ", fr400");
3226 break;
3227 case EF_FRV_CPU_FR405:
3228 strcat (buf, ", fr405");
3229 break;
3230
3231 case EF_FRV_CPU_FR450:
3232 strcat (buf, ", fr450");
3233 break;
3234
3235 case EF_FRV_CPU_FR500:
3236 strcat (buf, ", fr500");
3237 break;
3238 case EF_FRV_CPU_FR550:
3239 strcat (buf, ", fr550");
3240 break;
3241
3242 case EF_FRV_CPU_SIMPLE:
3243 strcat (buf, ", simple");
3244 break;
3245 case EF_FRV_CPU_TOMCAT:
3246 strcat (buf, ", tomcat");
3247 break;
3248 }
3249 break;
3250
3251 case EM_68K:
3252 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3253 strcat (buf, ", m68000");
3254 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3255 strcat (buf, ", cpu32");
3256 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3257 strcat (buf, ", fido_a");
3258 else
3259 {
3260 char const * isa = _("unknown");
3261 char const * mac = _("unknown mac");
3262 char const * additional = NULL;
3263
3264 switch (e_flags & EF_M68K_CF_ISA_MASK)
3265 {
3266 case EF_M68K_CF_ISA_A_NODIV:
3267 isa = "A";
3268 additional = ", nodiv";
3269 break;
3270 case EF_M68K_CF_ISA_A:
3271 isa = "A";
3272 break;
3273 case EF_M68K_CF_ISA_A_PLUS:
3274 isa = "A+";
3275 break;
3276 case EF_M68K_CF_ISA_B_NOUSP:
3277 isa = "B";
3278 additional = ", nousp";
3279 break;
3280 case EF_M68K_CF_ISA_B:
3281 isa = "B";
3282 break;
3283 case EF_M68K_CF_ISA_C:
3284 isa = "C";
3285 break;
3286 case EF_M68K_CF_ISA_C_NODIV:
3287 isa = "C";
3288 additional = ", nodiv";
3289 break;
3290 }
3291 strcat (buf, ", cf, isa ");
3292 strcat (buf, isa);
3293 if (additional)
3294 strcat (buf, additional);
3295 if (e_flags & EF_M68K_CF_FLOAT)
3296 strcat (buf, ", float");
3297 switch (e_flags & EF_M68K_CF_MAC_MASK)
3298 {
3299 case 0:
3300 mac = NULL;
3301 break;
3302 case EF_M68K_CF_MAC:
3303 mac = "mac";
3304 break;
3305 case EF_M68K_CF_EMAC:
3306 mac = "emac";
3307 break;
3308 case EF_M68K_CF_EMAC_B:
3309 mac = "emac_b";
3310 break;
3311 }
3312 if (mac)
3313 {
3314 strcat (buf, ", ");
3315 strcat (buf, mac);
3316 }
3317 }
3318 break;
3319
3320 case EM_CYGNUS_MEP:
3321 switch (e_flags & EF_MEP_CPU_MASK)
3322 {
3323 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3324 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3325 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3326 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3327 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3328 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3329 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3330 }
3331
3332 switch (e_flags & EF_MEP_COP_MASK)
3333 {
3334 case EF_MEP_COP_NONE: break;
3335 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3336 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3337 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3338 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3339 default: strcat (buf, _("<unknown MeP copro type>")); break;
3340 }
3341
3342 if (e_flags & EF_MEP_LIBRARY)
3343 strcat (buf, ", Built for Library");
3344
3345 if (e_flags & EF_MEP_INDEX_MASK)
3346 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3347 e_flags & EF_MEP_INDEX_MASK);
3348
3349 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3350 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3351 e_flags & ~ EF_MEP_ALL_FLAGS);
3352 break;
3353
3354 case EM_PPC:
3355 if (e_flags & EF_PPC_EMB)
3356 strcat (buf, ", emb");
3357
3358 if (e_flags & EF_PPC_RELOCATABLE)
3359 strcat (buf, _(", relocatable"));
3360
3361 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3362 strcat (buf, _(", relocatable-lib"));
3363 break;
3364
3365 case EM_PPC64:
3366 if (e_flags & EF_PPC64_ABI)
3367 {
3368 char abi[] = ", abiv0";
3369
3370 abi[6] += e_flags & EF_PPC64_ABI;
3371 strcat (buf, abi);
3372 }
3373 break;
3374
3375 case EM_V800:
3376 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3377 strcat (buf, ", RH850 ABI");
3378
3379 if (e_flags & EF_V800_850E3)
3380 strcat (buf, ", V3 architecture");
3381
3382 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3383 strcat (buf, ", FPU not used");
3384
3385 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3386 strcat (buf, ", regmode: COMMON");
3387
3388 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3389 strcat (buf, ", r4 not used");
3390
3391 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3392 strcat (buf, ", r30 not used");
3393
3394 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3395 strcat (buf, ", r5 not used");
3396
3397 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3398 strcat (buf, ", r2 not used");
3399
3400 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3401 {
3402 switch (e_flags & - e_flags)
3403 {
3404 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3405 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3406 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3407 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3408 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3409 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3410 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3411 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3412 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3413 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3414 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3415 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3416 default: break;
3417 }
3418 }
3419 break;
3420
3421 case EM_V850:
3422 case EM_CYGNUS_V850:
3423 switch (e_flags & EF_V850_ARCH)
3424 {
3425 case E_V850E3V5_ARCH:
3426 strcat (buf, ", v850e3v5");
3427 break;
3428 case E_V850E2V3_ARCH:
3429 strcat (buf, ", v850e2v3");
3430 break;
3431 case E_V850E2_ARCH:
3432 strcat (buf, ", v850e2");
3433 break;
3434 case E_V850E1_ARCH:
3435 strcat (buf, ", v850e1");
3436 break;
3437 case E_V850E_ARCH:
3438 strcat (buf, ", v850e");
3439 break;
3440 case E_V850_ARCH:
3441 strcat (buf, ", v850");
3442 break;
3443 default:
3444 strcat (buf, _(", unknown v850 architecture variant"));
3445 break;
3446 }
3447 break;
3448
3449 case EM_M32R:
3450 case EM_CYGNUS_M32R:
3451 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3452 strcat (buf, ", m32r");
3453 break;
3454
3455 case EM_MIPS:
3456 case EM_MIPS_RS3_LE:
3457 if (e_flags & EF_MIPS_NOREORDER)
3458 strcat (buf, ", noreorder");
3459
3460 if (e_flags & EF_MIPS_PIC)
3461 strcat (buf, ", pic");
3462
3463 if (e_flags & EF_MIPS_CPIC)
3464 strcat (buf, ", cpic");
3465
3466 if (e_flags & EF_MIPS_UCODE)
3467 strcat (buf, ", ugen_reserved");
3468
3469 if (e_flags & EF_MIPS_ABI2)
3470 strcat (buf, ", abi2");
3471
3472 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3473 strcat (buf, ", odk first");
3474
3475 if (e_flags & EF_MIPS_32BITMODE)
3476 strcat (buf, ", 32bitmode");
3477
3478 if (e_flags & EF_MIPS_NAN2008)
3479 strcat (buf, ", nan2008");
3480
3481 if (e_flags & EF_MIPS_FP64)
3482 strcat (buf, ", fp64");
3483
3484 switch ((e_flags & EF_MIPS_MACH))
3485 {
3486 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3487 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3488 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3489 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3490 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3491 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3492 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3493 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3494 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3495 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3496 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3497 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3498 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3499 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3500 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3501 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3502 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3503 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3504 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3505 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
3506 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
3507 case 0:
3508 /* We simply ignore the field in this case to avoid confusion:
3509 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3510 extension. */
3511 break;
3512 default: strcat (buf, _(", unknown CPU")); break;
3513 }
3514
3515 switch ((e_flags & EF_MIPS_ABI))
3516 {
3517 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3518 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3519 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3520 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3521 case 0:
3522 /* We simply ignore the field in this case to avoid confusion:
3523 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3524 This means it is likely to be an o32 file, but not for
3525 sure. */
3526 break;
3527 default: strcat (buf, _(", unknown ABI")); break;
3528 }
3529
3530 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3531 strcat (buf, ", mdmx");
3532
3533 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3534 strcat (buf, ", mips16");
3535
3536 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3537 strcat (buf, ", micromips");
3538
3539 switch ((e_flags & EF_MIPS_ARCH))
3540 {
3541 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3542 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3543 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3544 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3545 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3546 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3547 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3548 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3549 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3550 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3551 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3552 default: strcat (buf, _(", unknown ISA")); break;
3553 }
3554 break;
3555
3556 case EM_NDS32:
3557 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3558 break;
3559
3560 case EM_NFP:
3561 switch (EF_NFP_MACH (e_flags))
3562 {
3563 case E_NFP_MACH_3200:
3564 strcat (buf, ", NFP-32xx");
3565 break;
3566 case E_NFP_MACH_6000:
3567 strcat (buf, ", NFP-6xxx");
3568 break;
3569 }
3570 break;
3571
3572 case EM_RISCV:
3573 if (e_flags & EF_RISCV_RVC)
3574 strcat (buf, ", RVC");
3575
3576 if (e_flags & EF_RISCV_RVE)
3577 strcat (buf, ", RVE");
3578
3579 switch (e_flags & EF_RISCV_FLOAT_ABI)
3580 {
3581 case EF_RISCV_FLOAT_ABI_SOFT:
3582 strcat (buf, ", soft-float ABI");
3583 break;
3584
3585 case EF_RISCV_FLOAT_ABI_SINGLE:
3586 strcat (buf, ", single-float ABI");
3587 break;
3588
3589 case EF_RISCV_FLOAT_ABI_DOUBLE:
3590 strcat (buf, ", double-float ABI");
3591 break;
3592
3593 case EF_RISCV_FLOAT_ABI_QUAD:
3594 strcat (buf, ", quad-float ABI");
3595 break;
3596 }
3597 break;
3598
3599 case EM_SH:
3600 switch ((e_flags & EF_SH_MACH_MASK))
3601 {
3602 case EF_SH1: strcat (buf, ", sh1"); break;
3603 case EF_SH2: strcat (buf, ", sh2"); break;
3604 case EF_SH3: strcat (buf, ", sh3"); break;
3605 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3606 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3607 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3608 case EF_SH3E: strcat (buf, ", sh3e"); break;
3609 case EF_SH4: strcat (buf, ", sh4"); break;
3610 case EF_SH5: strcat (buf, ", sh5"); break;
3611 case EF_SH2E: strcat (buf, ", sh2e"); break;
3612 case EF_SH4A: strcat (buf, ", sh4a"); break;
3613 case EF_SH2A: strcat (buf, ", sh2a"); break;
3614 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3615 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3616 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3617 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3618 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3619 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3620 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3621 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3622 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3623 default: strcat (buf, _(", unknown ISA")); break;
3624 }
3625
3626 if (e_flags & EF_SH_PIC)
3627 strcat (buf, ", pic");
3628
3629 if (e_flags & EF_SH_FDPIC)
3630 strcat (buf, ", fdpic");
3631 break;
3632
3633 case EM_OR1K:
3634 if (e_flags & EF_OR1K_NODELAY)
3635 strcat (buf, ", no delay");
3636 break;
3637
3638 case EM_SPARCV9:
3639 if (e_flags & EF_SPARC_32PLUS)
3640 strcat (buf, ", v8+");
3641
3642 if (e_flags & EF_SPARC_SUN_US1)
3643 strcat (buf, ", ultrasparcI");
3644
3645 if (e_flags & EF_SPARC_SUN_US3)
3646 strcat (buf, ", ultrasparcIII");
3647
3648 if (e_flags & EF_SPARC_HAL_R1)
3649 strcat (buf, ", halr1");
3650
3651 if (e_flags & EF_SPARC_LEDATA)
3652 strcat (buf, ", ledata");
3653
3654 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3655 strcat (buf, ", tso");
3656
3657 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3658 strcat (buf, ", pso");
3659
3660 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3661 strcat (buf, ", rmo");
3662 break;
3663
3664 case EM_PARISC:
3665 switch (e_flags & EF_PARISC_ARCH)
3666 {
3667 case EFA_PARISC_1_0:
3668 strcpy (buf, ", PA-RISC 1.0");
3669 break;
3670 case EFA_PARISC_1_1:
3671 strcpy (buf, ", PA-RISC 1.1");
3672 break;
3673 case EFA_PARISC_2_0:
3674 strcpy (buf, ", PA-RISC 2.0");
3675 break;
3676 default:
3677 break;
3678 }
3679 if (e_flags & EF_PARISC_TRAPNIL)
3680 strcat (buf, ", trapnil");
3681 if (e_flags & EF_PARISC_EXT)
3682 strcat (buf, ", ext");
3683 if (e_flags & EF_PARISC_LSB)
3684 strcat (buf, ", lsb");
3685 if (e_flags & EF_PARISC_WIDE)
3686 strcat (buf, ", wide");
3687 if (e_flags & EF_PARISC_NO_KABP)
3688 strcat (buf, ", no kabp");
3689 if (e_flags & EF_PARISC_LAZYSWAP)
3690 strcat (buf, ", lazyswap");
3691 break;
3692
3693 case EM_PJ:
3694 case EM_PJ_OLD:
3695 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3696 strcat (buf, ", new calling convention");
3697
3698 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3699 strcat (buf, ", gnu calling convention");
3700 break;
3701
3702 case EM_IA_64:
3703 if ((e_flags & EF_IA_64_ABI64))
3704 strcat (buf, ", 64-bit");
3705 else
3706 strcat (buf, ", 32-bit");
3707 if ((e_flags & EF_IA_64_REDUCEDFP))
3708 strcat (buf, ", reduced fp model");
3709 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3710 strcat (buf, ", no function descriptors, constant gp");
3711 else if ((e_flags & EF_IA_64_CONS_GP))
3712 strcat (buf, ", constant gp");
3713 if ((e_flags & EF_IA_64_ABSOLUTE))
3714 strcat (buf, ", absolute");
3715 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3716 {
3717 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3718 strcat (buf, ", vms_linkages");
3719 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3720 {
3721 case EF_IA_64_VMS_COMCOD_SUCCESS:
3722 break;
3723 case EF_IA_64_VMS_COMCOD_WARNING:
3724 strcat (buf, ", warning");
3725 break;
3726 case EF_IA_64_VMS_COMCOD_ERROR:
3727 strcat (buf, ", error");
3728 break;
3729 case EF_IA_64_VMS_COMCOD_ABORT:
3730 strcat (buf, ", abort");
3731 break;
3732 default:
3733 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3734 e_flags & EF_IA_64_VMS_COMCOD);
3735 strcat (buf, ", <unknown>");
3736 }
3737 }
3738 break;
3739
3740 case EM_VAX:
3741 if ((e_flags & EF_VAX_NONPIC))
3742 strcat (buf, ", non-PIC");
3743 if ((e_flags & EF_VAX_DFLOAT))
3744 strcat (buf, ", D-Float");
3745 if ((e_flags & EF_VAX_GFLOAT))
3746 strcat (buf, ", G-Float");
3747 break;
3748
3749 case EM_VISIUM:
3750 if (e_flags & EF_VISIUM_ARCH_MCM)
3751 strcat (buf, ", mcm");
3752 else if (e_flags & EF_VISIUM_ARCH_MCM24)
3753 strcat (buf, ", mcm24");
3754 if (e_flags & EF_VISIUM_ARCH_GR6)
3755 strcat (buf, ", gr6");
3756 break;
3757
3758 case EM_RL78:
3759 switch (e_flags & E_FLAG_RL78_CPU_MASK)
3760 {
3761 case E_FLAG_RL78_ANY_CPU: break;
3762 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3763 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3764 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3765 }
3766 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3767 strcat (buf, ", 64-bit doubles");
3768 break;
3769
3770 case EM_RX:
3771 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3772 strcat (buf, ", 64-bit doubles");
3773 if (e_flags & E_FLAG_RX_DSP)
3774 strcat (buf, ", dsp");
3775 if (e_flags & E_FLAG_RX_PID)
3776 strcat (buf, ", pid");
3777 if (e_flags & E_FLAG_RX_ABI)
3778 strcat (buf, ", RX ABI");
3779 if (e_flags & E_FLAG_RX_SINSNS_SET)
3780 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3781 ? ", uses String instructions" : ", bans String instructions");
3782 if (e_flags & E_FLAG_RX_V2)
3783 strcat (buf, ", V2");
3784 if (e_flags & E_FLAG_RX_V3)
3785 strcat (buf, ", V3");
3786 break;
3787
3788 case EM_S390:
3789 if (e_flags & EF_S390_HIGH_GPRS)
3790 strcat (buf, ", highgprs");
3791 break;
3792
3793 case EM_TI_C6000:
3794 if ((e_flags & EF_C6000_REL))
3795 strcat (buf, ", relocatable module");
3796 break;
3797
3798 case EM_MSP430:
3799 strcat (buf, _(": architecture variant: "));
3800 switch (e_flags & EF_MSP430_MACH)
3801 {
3802 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3803 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3804 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3805 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3806 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3807 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3808 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3809 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3810 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3811 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3812 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3813 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3814 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3815 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3816 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
3817 default:
3818 strcat (buf, _(": unknown")); break;
3819 }
3820
3821 if (e_flags & ~ EF_MSP430_MACH)
3822 strcat (buf, _(": unknown extra flag bits also present"));
3823 break;
3824
3825 case EM_Z80:
3826 switch (e_flags & EF_Z80_MACH_MSK)
3827 {
3828 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
3829 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
3830 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
3831 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
3832 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
3833 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
3834 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
3835 default:
3836 strcat (buf, _(", unknown")); break;
3837 }
3838 break;
3839 }
3840 }
3841
3842 return buf;
3843 }
3844
3845 static const char *
3846 get_osabi_name (Filedata * filedata, unsigned int osabi)
3847 {
3848 static char buff[32];
3849
3850 switch (osabi)
3851 {
3852 case ELFOSABI_NONE: return "UNIX - System V";
3853 case ELFOSABI_HPUX: return "UNIX - HP-UX";
3854 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
3855 case ELFOSABI_GNU: return "UNIX - GNU";
3856 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
3857 case ELFOSABI_AIX: return "UNIX - AIX";
3858 case ELFOSABI_IRIX: return "UNIX - IRIX";
3859 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
3860 case ELFOSABI_TRU64: return "UNIX - TRU64";
3861 case ELFOSABI_MODESTO: return "Novell - Modesto";
3862 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
3863 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
3864 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
3865 case ELFOSABI_AROS: return "AROS";
3866 case ELFOSABI_FENIXOS: return "FenixOS";
3867 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
3868 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
3869 default:
3870 if (osabi >= 64)
3871 switch (filedata->file_header.e_machine)
3872 {
3873 case EM_ARM:
3874 switch (osabi)
3875 {
3876 case ELFOSABI_ARM: return "ARM";
3877 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
3878 default:
3879 break;
3880 }
3881 break;
3882
3883 case EM_MSP430:
3884 case EM_MSP430_OLD:
3885 case EM_VISIUM:
3886 switch (osabi)
3887 {
3888 case ELFOSABI_STANDALONE: return _("Standalone App");
3889 default:
3890 break;
3891 }
3892 break;
3893
3894 case EM_TI_C6000:
3895 switch (osabi)
3896 {
3897 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
3898 case ELFOSABI_C6000_LINUX: return "Linux C6000";
3899 default:
3900 break;
3901 }
3902 break;
3903
3904 default:
3905 break;
3906 }
3907 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3908 return buff;
3909 }
3910 }
3911
3912 static const char *
3913 get_aarch64_segment_type (unsigned long type)
3914 {
3915 switch (type)
3916 {
3917 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
3918 default: return NULL;
3919 }
3920 }
3921
3922 static const char *
3923 get_arm_segment_type (unsigned long type)
3924 {
3925 switch (type)
3926 {
3927 case PT_ARM_EXIDX: return "EXIDX";
3928 default: return NULL;
3929 }
3930 }
3931
3932 static const char *
3933 get_s390_segment_type (unsigned long type)
3934 {
3935 switch (type)
3936 {
3937 case PT_S390_PGSTE: return "S390_PGSTE";
3938 default: return NULL;
3939 }
3940 }
3941
3942 static const char *
3943 get_mips_segment_type (unsigned long type)
3944 {
3945 switch (type)
3946 {
3947 case PT_MIPS_REGINFO: return "REGINFO";
3948 case PT_MIPS_RTPROC: return "RTPROC";
3949 case PT_MIPS_OPTIONS: return "OPTIONS";
3950 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
3951 default: return NULL;
3952 }
3953 }
3954
3955 static const char *
3956 get_parisc_segment_type (unsigned long type)
3957 {
3958 switch (type)
3959 {
3960 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
3961 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
3962 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
3963 default: return NULL;
3964 }
3965 }
3966
3967 static const char *
3968 get_ia64_segment_type (unsigned long type)
3969 {
3970 switch (type)
3971 {
3972 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
3973 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
3974 default: return NULL;
3975 }
3976 }
3977
3978 static const char *
3979 get_tic6x_segment_type (unsigned long type)
3980 {
3981 switch (type)
3982 {
3983 case PT_C6000_PHATTR: return "C6000_PHATTR";
3984 default: return NULL;
3985 }
3986 }
3987
3988 static const char *
3989 get_hpux_segment_type (unsigned long type, unsigned e_machine)
3990 {
3991 if (e_machine == EM_PARISC)
3992 switch (type)
3993 {
3994 case PT_HP_TLS: return "HP_TLS";
3995 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
3996 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
3997 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
3998 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
3999 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4000 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4001 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4002 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4003 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4004 case PT_HP_PARALLEL: return "HP_PARALLEL";
4005 case PT_HP_FASTBIND: return "HP_FASTBIND";
4006 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4007 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4008 case PT_HP_STACK: return "HP_STACK";
4009 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4010 default: return NULL;
4011 }
4012
4013 if (e_machine == EM_IA_64)
4014 switch (type)
4015 {
4016 case PT_HP_TLS: return "HP_TLS";
4017 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4018 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4019 case PT_IA_64_HP_STACK: return "HP_STACK";
4020 default: return NULL;
4021 }
4022
4023 return NULL;
4024 }
4025
4026 static const char *
4027 get_solaris_segment_type (unsigned long type)
4028 {
4029 switch (type)
4030 {
4031 case 0x6464e550: return "PT_SUNW_UNWIND";
4032 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4033 case 0x6ffffff7: return "PT_LOSUNW";
4034 case 0x6ffffffa: return "PT_SUNWBSS";
4035 case 0x6ffffffb: return "PT_SUNWSTACK";
4036 case 0x6ffffffc: return "PT_SUNWDTRACE";
4037 case 0x6ffffffd: return "PT_SUNWCAP";
4038 case 0x6fffffff: return "PT_HISUNW";
4039 default: return NULL;
4040 }
4041 }
4042
4043 static const char *
4044 get_segment_type (Filedata * filedata, unsigned long p_type)
4045 {
4046 static char buff[32];
4047
4048 switch (p_type)
4049 {
4050 case PT_NULL: return "NULL";
4051 case PT_LOAD: return "LOAD";
4052 case PT_DYNAMIC: return "DYNAMIC";
4053 case PT_INTERP: return "INTERP";
4054 case PT_NOTE: return "NOTE";
4055 case PT_SHLIB: return "SHLIB";
4056 case PT_PHDR: return "PHDR";
4057 case PT_TLS: return "TLS";
4058 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4059 case PT_GNU_STACK: return "GNU_STACK";
4060 case PT_GNU_RELRO: return "GNU_RELRO";
4061 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4062
4063 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4064 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4065 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4066
4067 default:
4068 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4069 {
4070 const char * result;
4071
4072 switch (filedata->file_header.e_machine)
4073 {
4074 case EM_AARCH64:
4075 result = get_aarch64_segment_type (p_type);
4076 break;
4077 case EM_ARM:
4078 result = get_arm_segment_type (p_type);
4079 break;
4080 case EM_MIPS:
4081 case EM_MIPS_RS3_LE:
4082 result = get_mips_segment_type (p_type);
4083 break;
4084 case EM_PARISC:
4085 result = get_parisc_segment_type (p_type);
4086 break;
4087 case EM_IA_64:
4088 result = get_ia64_segment_type (p_type);
4089 break;
4090 case EM_TI_C6000:
4091 result = get_tic6x_segment_type (p_type);
4092 break;
4093 case EM_S390:
4094 case EM_S390_OLD:
4095 result = get_s390_segment_type (p_type);
4096 break;
4097 default:
4098 result = NULL;
4099 break;
4100 }
4101
4102 if (result != NULL)
4103 return result;
4104
4105 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4106 }
4107 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4108 {
4109 const char * result = NULL;
4110
4111 switch (filedata->file_header.e_ident[EI_OSABI])
4112 {
4113 case ELFOSABI_GNU:
4114 case ELFOSABI_FREEBSD:
4115 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4116 {
4117 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4118 result = buff;
4119 }
4120 break;
4121 case ELFOSABI_HPUX:
4122 result = get_hpux_segment_type (p_type,
4123 filedata->file_header.e_machine);
4124 break;
4125 case ELFOSABI_SOLARIS:
4126 result = get_solaris_segment_type (p_type);
4127 break;
4128 default:
4129 break;
4130 }
4131 if (result != NULL)
4132 return result;
4133
4134 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4135 }
4136 else
4137 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4138
4139 return buff;
4140 }
4141 }
4142
4143 static const char *
4144 get_arc_section_type_name (unsigned int sh_type)
4145 {
4146 switch (sh_type)
4147 {
4148 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4149 default:
4150 break;
4151 }
4152 return NULL;
4153 }
4154
4155 static const char *
4156 get_mips_section_type_name (unsigned int sh_type)
4157 {
4158 switch (sh_type)
4159 {
4160 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4161 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4162 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4163 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4164 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4165 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4166 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4167 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4168 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4169 case SHT_MIPS_RELD: return "MIPS_RELD";
4170 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4171 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4172 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4173 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4174 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4175 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4176 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4177 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4178 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4179 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4180 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4181 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4182 case SHT_MIPS_LINE: return "MIPS_LINE";
4183 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4184 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4185 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4186 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4187 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4188 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4189 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4190 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4191 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4192 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4193 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4194 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4195 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4196 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4197 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4198 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4199 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4200 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4201 default:
4202 break;
4203 }
4204 return NULL;
4205 }
4206
4207 static const char *
4208 get_parisc_section_type_name (unsigned int sh_type)
4209 {
4210 switch (sh_type)
4211 {
4212 case SHT_PARISC_EXT: return "PARISC_EXT";
4213 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4214 case SHT_PARISC_DOC: return "PARISC_DOC";
4215 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4216 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4217 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4218 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4219 default: return NULL;
4220 }
4221 }
4222
4223 static const char *
4224 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4225 {
4226 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4227 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4228 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4229
4230 switch (sh_type)
4231 {
4232 case SHT_IA_64_EXT: return "IA_64_EXT";
4233 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4234 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4235 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4236 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4237 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4238 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4239 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4240 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4241 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4242 default:
4243 break;
4244 }
4245 return NULL;
4246 }
4247
4248 static const char *
4249 get_x86_64_section_type_name (unsigned int sh_type)
4250 {
4251 switch (sh_type)
4252 {
4253 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4254 default: return NULL;
4255 }
4256 }
4257
4258 static const char *
4259 get_aarch64_section_type_name (unsigned int sh_type)
4260 {
4261 switch (sh_type)
4262 {
4263 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4264 default: return NULL;
4265 }
4266 }
4267
4268 static const char *
4269 get_arm_section_type_name (unsigned int sh_type)
4270 {
4271 switch (sh_type)
4272 {
4273 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4274 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4275 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4276 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4277 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4278 default: return NULL;
4279 }
4280 }
4281
4282 static const char *
4283 get_tic6x_section_type_name (unsigned int sh_type)
4284 {
4285 switch (sh_type)
4286 {
4287 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4288 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4289 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4290 case SHT_TI_ICODE: return "TI_ICODE";
4291 case SHT_TI_XREF: return "TI_XREF";
4292 case SHT_TI_HANDLER: return "TI_HANDLER";
4293 case SHT_TI_INITINFO: return "TI_INITINFO";
4294 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4295 default: return NULL;
4296 }
4297 }
4298
4299 static const char *
4300 get_msp430_section_type_name (unsigned int sh_type)
4301 {
4302 switch (sh_type)
4303 {
4304 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4305 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4306 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4307 default: return NULL;
4308 }
4309 }
4310
4311 static const char *
4312 get_nfp_section_type_name (unsigned int sh_type)
4313 {
4314 switch (sh_type)
4315 {
4316 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4317 case SHT_NFP_INITREG: return "NFP_INITREG";
4318 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4319 default: return NULL;
4320 }
4321 }
4322
4323 static const char *
4324 get_v850_section_type_name (unsigned int sh_type)
4325 {
4326 switch (sh_type)
4327 {
4328 case SHT_V850_SCOMMON: return "V850 Small Common";
4329 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4330 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4331 case SHT_RENESAS_IOP: return "RENESAS IOP";
4332 case SHT_RENESAS_INFO: return "RENESAS INFO";
4333 default: return NULL;
4334 }
4335 }
4336
4337 static const char *
4338 get_riscv_section_type_name (unsigned int sh_type)
4339 {
4340 switch (sh_type)
4341 {
4342 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4343 default: return NULL;
4344 }
4345 }
4346
4347 static const char *
4348 get_csky_section_type_name (unsigned int sh_type)
4349 {
4350 switch (sh_type)
4351 {
4352 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4353 default: return NULL;
4354 }
4355 }
4356
4357 static const char *
4358 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4359 {
4360 static char buff[32];
4361 const char * result;
4362
4363 switch (sh_type)
4364 {
4365 case SHT_NULL: return "NULL";
4366 case SHT_PROGBITS: return "PROGBITS";
4367 case SHT_SYMTAB: return "SYMTAB";
4368 case SHT_STRTAB: return "STRTAB";
4369 case SHT_RELA: return "RELA";
4370 case SHT_HASH: return "HASH";
4371 case SHT_DYNAMIC: return "DYNAMIC";
4372 case SHT_NOTE: return "NOTE";
4373 case SHT_NOBITS: return "NOBITS";
4374 case SHT_REL: return "REL";
4375 case SHT_SHLIB: return "SHLIB";
4376 case SHT_DYNSYM: return "DYNSYM";
4377 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4378 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4379 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4380 case SHT_GNU_HASH: return "GNU_HASH";
4381 case SHT_GROUP: return "GROUP";
4382 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4383 case SHT_GNU_verdef: return "VERDEF";
4384 case SHT_GNU_verneed: return "VERNEED";
4385 case SHT_GNU_versym: return "VERSYM";
4386 case 0x6ffffff0: return "VERSYM";
4387 case 0x6ffffffc: return "VERDEF";
4388 case 0x7ffffffd: return "AUXILIARY";
4389 case 0x7fffffff: return "FILTER";
4390 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4391
4392 default:
4393 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4394 {
4395 switch (filedata->file_header.e_machine)
4396 {
4397 case EM_ARC:
4398 case EM_ARC_COMPACT:
4399 case EM_ARC_COMPACT2:
4400 result = get_arc_section_type_name (sh_type);
4401 break;
4402 case EM_MIPS:
4403 case EM_MIPS_RS3_LE:
4404 result = get_mips_section_type_name (sh_type);
4405 break;
4406 case EM_PARISC:
4407 result = get_parisc_section_type_name (sh_type);
4408 break;
4409 case EM_IA_64:
4410 result = get_ia64_section_type_name (filedata, sh_type);
4411 break;
4412 case EM_X86_64:
4413 case EM_L1OM:
4414 case EM_K1OM:
4415 result = get_x86_64_section_type_name (sh_type);
4416 break;
4417 case EM_AARCH64:
4418 result = get_aarch64_section_type_name (sh_type);
4419 break;
4420 case EM_ARM:
4421 result = get_arm_section_type_name (sh_type);
4422 break;
4423 case EM_TI_C6000:
4424 result = get_tic6x_section_type_name (sh_type);
4425 break;
4426 case EM_MSP430:
4427 result = get_msp430_section_type_name (sh_type);
4428 break;
4429 case EM_NFP:
4430 result = get_nfp_section_type_name (sh_type);
4431 break;
4432 case EM_V800:
4433 case EM_V850:
4434 case EM_CYGNUS_V850:
4435 result = get_v850_section_type_name (sh_type);
4436 break;
4437 case EM_RISCV:
4438 result = get_riscv_section_type_name (sh_type);
4439 break;
4440 case EM_CSKY:
4441 result = get_csky_section_type_name (sh_type);
4442 break;
4443 default:
4444 result = NULL;
4445 break;
4446 }
4447
4448 if (result != NULL)
4449 return result;
4450
4451 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4452 }
4453 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4454 {
4455 switch (filedata->file_header.e_machine)
4456 {
4457 case EM_IA_64:
4458 result = get_ia64_section_type_name (filedata, sh_type);
4459 break;
4460 default:
4461 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4462 result = get_solaris_section_type (sh_type);
4463 else
4464 {
4465 switch (sh_type)
4466 {
4467 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4468 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4469 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4470 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4471 default:
4472 result = NULL;
4473 break;
4474 }
4475 }
4476 break;
4477 }
4478
4479 if (result != NULL)
4480 return result;
4481
4482 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4483 }
4484 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4485 {
4486 switch (filedata->file_header.e_machine)
4487 {
4488 case EM_V800:
4489 case EM_V850:
4490 case EM_CYGNUS_V850:
4491 result = get_v850_section_type_name (sh_type);
4492 break;
4493 default:
4494 result = NULL;
4495 break;
4496 }
4497
4498 if (result != NULL)
4499 return result;
4500
4501 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4502 }
4503 else
4504 /* This message is probably going to be displayed in a 15
4505 character wide field, so put the hex value first. */
4506 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4507
4508 return buff;
4509 }
4510 }
4511
4512 enum long_option_values
4513 {
4514 OPTION_DEBUG_DUMP = 512,
4515 OPTION_DYN_SYMS,
4516 OPTION_LTO_SYMS,
4517 OPTION_DWARF_DEPTH,
4518 OPTION_DWARF_START,
4519 OPTION_DWARF_CHECK,
4520 OPTION_CTF_DUMP,
4521 OPTION_CTF_PARENT,
4522 OPTION_CTF_SYMBOLS,
4523 OPTION_CTF_STRINGS,
4524 OPTION_WITH_SYMBOL_VERSIONS,
4525 OPTION_RECURSE_LIMIT,
4526 OPTION_NO_RECURSE_LIMIT,
4527 OPTION_NO_DEMANGLING
4528 };
4529
4530 static struct option options[] =
4531 {
4532 /* Note - This table is alpha-sorted on the 'val'
4533 field in order to make adding new options easier. */
4534 {"arch-specific", no_argument, 0, 'A'},
4535 {"all", no_argument, 0, 'a'},
4536 {"demangle", optional_argument, 0, 'C'},
4537 {"archive-index", no_argument, 0, 'c'},
4538 {"use-dynamic", no_argument, 0, 'D'},
4539 {"dynamic", no_argument, 0, 'd'},
4540 {"headers", no_argument, 0, 'e'},
4541 {"section-groups", no_argument, 0, 'g'},
4542 {"help", no_argument, 0, 'H'},
4543 {"file-header", no_argument, 0, 'h'},
4544 {"histogram", no_argument, 0, 'I'},
4545 {"lint", no_argument, 0, 'L'},
4546 {"enable-checks", no_argument, 0, 'L'},
4547 {"program-headers", no_argument, 0, 'l'},
4548 {"segments", no_argument, 0, 'l'},
4549 {"full-section-name",no_argument, 0, 'N'},
4550 {"notes", no_argument, 0, 'n'},
4551 {"process-links", no_argument, 0, 'P'},
4552 {"string-dump", required_argument, 0, 'p'},
4553 {"relocated-dump", required_argument, 0, 'R'},
4554 {"relocs", no_argument, 0, 'r'},
4555 {"section-headers", no_argument, 0, 'S'},
4556 {"sections", no_argument, 0, 'S'},
4557 {"symbols", no_argument, 0, 's'},
4558 {"syms", no_argument, 0, 's'},
4559 {"silent-truncation",no_argument, 0, 'T'},
4560 {"section-details", no_argument, 0, 't'},
4561 {"unwind", no_argument, 0, 'u'},
4562 {"version-info", no_argument, 0, 'V'},
4563 {"version", no_argument, 0, 'v'},
4564 {"wide", no_argument, 0, 'W'},
4565 {"hex-dump", required_argument, 0, 'x'},
4566 {"decompress", no_argument, 0, 'z'},
4567
4568 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4569 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4570 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4571 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4572 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
4573 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
4574 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
4575 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4576 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4577 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
4578 #ifdef ENABLE_LIBCTF
4579 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
4580 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4581 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4582 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
4583 #endif
4584
4585 {0, no_argument, 0, 0}
4586 };
4587
4588 static void
4589 usage (FILE * stream)
4590 {
4591 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4592 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4593 fprintf (stream, _(" Options are:\n\
4594 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
4595 -h --file-header Display the ELF file header\n\
4596 -l --program-headers Display the program headers\n\
4597 --segments An alias for --program-headers\n\
4598 -S --section-headers Display the sections' header\n\
4599 --sections An alias for --section-headers\n\
4600 -g --section-groups Display the section groups\n\
4601 -t --section-details Display the section details\n\
4602 -e --headers Equivalent to: -h -l -S\n\
4603 -s --syms Display the symbol table\n\
4604 --symbols An alias for --syms\n\
4605 --dyn-syms Display the dynamic symbol table\n\
4606 --lto-syms Display LTO symbol tables\n\
4607 -C --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
4608 The STYLE, if specified, can be `auto' (the default),\n\
4609 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
4610 or `gnat'\n\
4611 --no-demangle Do not demangle low-level symbol names. (This is the default)\n\
4612 --recurse-limit Enable a demangling recursion limit. (This is the default)\n\
4613 --no-recurse-limit Disable a demangling recursion limit\n\
4614 -n --notes Display the core notes (if present)\n\
4615 -r --relocs Display the relocations (if present)\n\
4616 -u --unwind Display the unwind info (if present)\n\
4617 -d --dynamic Display the dynamic section (if present)\n\
4618 -V --version-info Display the version sections (if present)\n\
4619 -A --arch-specific Display architecture specific information (if any)\n\
4620 -c --archive-index Display the symbol/file index in an archive\n\
4621 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
4622 -L --lint|--enable-checks Display warning messages for possible problems\n\
4623 -x --hex-dump=<number|name>\n\
4624 Dump the contents of section <number|name> as bytes\n\
4625 -p --string-dump=<number|name>\n\
4626 Dump the contents of section <number|name> as strings\n\
4627 -R --relocated-dump=<number|name>\n\
4628 Dump the contents of section <number|name> as relocated bytes\n\
4629 -z --decompress Decompress section before dumping it\n\
4630 -w[lLiaprmfFsoORtUuTgAc] or\n\
4631 --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
4632 =frames-interp,=str,=str-offsets,=loc,=Ranges,=pubtypes,\n\
4633 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
4634 =addr,=cu_index]\n\
4635 Display the contents of DWARF debug sections\n\
4636 -wk,--debug-dump=links Display the contents of sections that link to separate debuginfo files\n\
4637 -P,--process-links Display the contents of non-debug sections in separate debuginfo files. (Implies -wK)\n"));
4638 #if DEFAULT_FOR_FOLLOW_LINKS
4639 fprintf (stream, _("\
4640 -wK,--debug-dump=follow-links Follow links to separate debug info files (default)\n\
4641 -wN,--debug-dump=no-follow-links Do not follow links to separate debug info files\n\
4642 "));
4643 #else
4644 fprintf (stream, _("\
4645 -wK,--debug-dump=follow-links Follow links to separate debug info files\n\
4646 -wN,--debug-dump=no-follow-links Do not follow links to separate debug info files (default)\n\
4647 "));
4648 #endif
4649 fprintf (stream, _("\
4650 --dwarf-depth=N Do not display DIEs at depth N or greater\n\
4651 --dwarf-start=N Display DIEs starting with N, at the same depth\n\
4652 or deeper\n"));
4653 #ifdef ENABLE_LIBCTF
4654 fprintf (stream, _("\
4655 --ctf=<number|name> Display CTF info from section <number|name>\n\
4656 --ctf-parent=<number|name>\n\
4657 Use section <number|name> as the CTF parent\n\n\
4658 --ctf-symbols=<number|name>\n\
4659 Use section <number|name> as the CTF external symtab\n\n\
4660 --ctf-strings=<number|name>\n\
4661 Use section <number|name> as the CTF external strtab\n\n"));
4662 #endif
4663
4664 #ifdef SUPPORT_DISASSEMBLY
4665 fprintf (stream, _("\
4666 -i --instruction-dump=<number|name>\n\
4667 Disassemble the contents of section <number|name>\n"));
4668 #endif
4669 fprintf (stream, _("\
4670 -I --histogram Display histogram of bucket list lengths\n\
4671 -W --wide Allow output width to exceed 80 characters\n\
4672 -T --silent-truncation If a symbol name is truncated, do not add a suffix [...]\n\
4673 @<file> Read options from <file>\n\
4674 -H --help Display this information\n\
4675 -v --version Display the version number of readelf\n"));
4676
4677 if (REPORT_BUGS_TO[0] && stream == stdout)
4678 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4679
4680 exit (stream == stdout ? 0 : 1);
4681 }
4682
4683 /* Record the fact that the user wants the contents of section number
4684 SECTION to be displayed using the method(s) encoded as flags bits
4685 in TYPE. Note, TYPE can be zero if we are creating the array for
4686 the first time. */
4687
4688 static void
4689 request_dump_bynumber (struct dump_data *dumpdata,
4690 unsigned int section, dump_type type)
4691 {
4692 if (section >= dumpdata->num_dump_sects)
4693 {
4694 dump_type * new_dump_sects;
4695
4696 new_dump_sects = (dump_type *) calloc (section + 1,
4697 sizeof (* new_dump_sects));
4698
4699 if (new_dump_sects == NULL)
4700 error (_("Out of memory allocating dump request table.\n"));
4701 else
4702 {
4703 if (dumpdata->dump_sects)
4704 {
4705 /* Copy current flag settings. */
4706 memcpy (new_dump_sects, dumpdata->dump_sects,
4707 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
4708
4709 free (dumpdata->dump_sects);
4710 }
4711
4712 dumpdata->dump_sects = new_dump_sects;
4713 dumpdata->num_dump_sects = section + 1;
4714 }
4715 }
4716
4717 if (dumpdata->dump_sects)
4718 dumpdata->dump_sects[section] |= type;
4719 }
4720
4721 /* Request a dump by section name. */
4722
4723 static void
4724 request_dump_byname (const char * section, dump_type type)
4725 {
4726 struct dump_list_entry * new_request;
4727
4728 new_request = (struct dump_list_entry *)
4729 malloc (sizeof (struct dump_list_entry));
4730 if (!new_request)
4731 error (_("Out of memory allocating dump request table.\n"));
4732
4733 new_request->name = strdup (section);
4734 if (!new_request->name)
4735 error (_("Out of memory allocating dump request table.\n"));
4736
4737 new_request->type = type;
4738
4739 new_request->next = dump_sects_byname;
4740 dump_sects_byname = new_request;
4741 }
4742
4743 static inline void
4744 request_dump (struct dump_data *dumpdata, dump_type type)
4745 {
4746 int section;
4747 char * cp;
4748
4749 do_dump++;
4750 section = strtoul (optarg, & cp, 0);
4751
4752 if (! *cp && section >= 0)
4753 request_dump_bynumber (dumpdata, section, type);
4754 else
4755 request_dump_byname (optarg, type);
4756 }
4757
4758 static void
4759 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
4760 {
4761 int c;
4762
4763 if (argc < 2)
4764 usage (stderr);
4765
4766 while ((c = getopt_long
4767 (argc, argv, "ACDHILNPR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4768 {
4769 switch (c)
4770 {
4771 case 0:
4772 /* Long options. */
4773 break;
4774 case 'H':
4775 usage (stdout);
4776 break;
4777
4778 case 'a':
4779 do_syms = TRUE;
4780 do_reloc = TRUE;
4781 do_unwind = TRUE;
4782 do_dynamic = TRUE;
4783 do_header = TRUE;
4784 do_sections = TRUE;
4785 do_section_groups = TRUE;
4786 do_segments = TRUE;
4787 do_version = TRUE;
4788 do_histogram = TRUE;
4789 do_arch = TRUE;
4790 do_notes = TRUE;
4791 break;
4792
4793 case 'g':
4794 do_section_groups = TRUE;
4795 break;
4796 case 't':
4797 case 'N':
4798 do_sections = TRUE;
4799 do_section_details = TRUE;
4800 break;
4801 case 'e':
4802 do_header = TRUE;
4803 do_sections = TRUE;
4804 do_segments = TRUE;
4805 break;
4806 case 'A':
4807 do_arch = TRUE;
4808 break;
4809 case 'D':
4810 do_using_dynamic = TRUE;
4811 break;
4812 case 'r':
4813 do_reloc = TRUE;
4814 break;
4815 case 'u':
4816 do_unwind = TRUE;
4817 break;
4818 case 'h':
4819 do_header = TRUE;
4820 break;
4821 case 'l':
4822 do_segments = TRUE;
4823 break;
4824 case 's':
4825 do_syms = TRUE;
4826 break;
4827 case 'S':
4828 do_sections = TRUE;
4829 break;
4830 case 'd':
4831 do_dynamic = TRUE;
4832 break;
4833 case 'I':
4834 do_histogram = TRUE;
4835 break;
4836 case 'n':
4837 do_notes = TRUE;
4838 break;
4839 case 'c':
4840 do_archive_index = TRUE;
4841 break;
4842 case 'L':
4843 do_checks = TRUE;
4844 break;
4845 case 'P':
4846 process_links = TRUE;
4847 do_follow_links = TRUE;
4848 break;
4849 case 'x':
4850 request_dump (dumpdata, HEX_DUMP);
4851 break;
4852 case 'p':
4853 request_dump (dumpdata, STRING_DUMP);
4854 break;
4855 case 'R':
4856 request_dump (dumpdata, RELOC_DUMP);
4857 break;
4858 case 'z':
4859 decompress_dumps = TRUE;
4860 break;
4861 case 'w':
4862 do_dump = TRUE;
4863 if (optarg == NULL)
4864 {
4865 do_debugging = TRUE;
4866 dwarf_select_sections_all ();
4867 }
4868 else
4869 {
4870 do_debugging = FALSE;
4871 dwarf_select_sections_by_letters (optarg);
4872 }
4873 break;
4874 case OPTION_DEBUG_DUMP:
4875 do_dump = TRUE;
4876 if (optarg == NULL)
4877 do_debugging = TRUE;
4878 else
4879 {
4880 do_debugging = FALSE;
4881 dwarf_select_sections_by_names (optarg);
4882 }
4883 break;
4884 case OPTION_DWARF_DEPTH:
4885 {
4886 char *cp;
4887
4888 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4889 }
4890 break;
4891 case OPTION_DWARF_START:
4892 {
4893 char *cp;
4894
4895 dwarf_start_die = strtoul (optarg, & cp, 0);
4896 }
4897 break;
4898 case OPTION_DWARF_CHECK:
4899 dwarf_check = TRUE;
4900 break;
4901 case OPTION_CTF_DUMP:
4902 do_ctf = TRUE;
4903 request_dump (dumpdata, CTF_DUMP);
4904 break;
4905 case OPTION_CTF_SYMBOLS:
4906 free (dump_ctf_symtab_name);
4907 dump_ctf_symtab_name = strdup (optarg);
4908 break;
4909 case OPTION_CTF_STRINGS:
4910 free (dump_ctf_strtab_name);
4911 dump_ctf_strtab_name = strdup (optarg);
4912 break;
4913 case OPTION_CTF_PARENT:
4914 free (dump_ctf_parent_name);
4915 dump_ctf_parent_name = strdup (optarg);
4916 break;
4917 case OPTION_DYN_SYMS:
4918 do_dyn_syms = TRUE;
4919 break;
4920 case OPTION_LTO_SYMS:
4921 do_lto_syms = TRUE;
4922 break;
4923 #ifdef SUPPORT_DISASSEMBLY
4924 case 'i':
4925 request_dump (dumpdata, DISASS_DUMP);
4926 break;
4927 #endif
4928 case 'v':
4929 print_version (program_name);
4930 break;
4931 case 'V':
4932 do_version = TRUE;
4933 break;
4934 case 'W':
4935 do_wide = TRUE;
4936 break;
4937 case 'T':
4938 do_not_show_symbol_truncation = TRUE;
4939 break;
4940 case 'C':
4941 do_demangle = TRUE;
4942 if (optarg != NULL)
4943 {
4944 enum demangling_styles style;
4945
4946 style = cplus_demangle_name_to_style (optarg);
4947 if (style == unknown_demangling)
4948 error (_("unknown demangling style `%s'"), optarg);
4949
4950 cplus_demangle_set_style (style);
4951 }
4952 break;
4953 case OPTION_NO_DEMANGLING:
4954 do_demangle = FALSE;
4955 break;
4956 case OPTION_RECURSE_LIMIT:
4957 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
4958 break;
4959 case OPTION_NO_RECURSE_LIMIT:
4960 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
4961 break;
4962 case OPTION_WITH_SYMBOL_VERSIONS:
4963 /* Ignored for backward compatibility. */
4964 break;
4965
4966 default:
4967 /* xgettext:c-format */
4968 error (_("Invalid option '-%c'\n"), c);
4969 /* Fall through. */
4970 case '?':
4971 usage (stderr);
4972 }
4973 }
4974
4975 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4976 && !do_segments && !do_header && !do_dump && !do_version
4977 && !do_histogram && !do_debugging && !do_arch && !do_notes
4978 && !do_section_groups && !do_archive_index
4979 && !do_dyn_syms && !do_lto_syms)
4980 {
4981 if (do_checks)
4982 {
4983 check_all = TRUE;
4984 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = TRUE;
4985 do_segments = do_header = do_dump = do_version = TRUE;
4986 do_histogram = do_debugging = do_arch = do_notes = TRUE;
4987 do_section_groups = do_archive_index = do_dyn_syms = TRUE;
4988 do_lto_syms = TRUE;
4989 }
4990 else
4991 usage (stderr);
4992 }
4993 }
4994
4995 static const char *
4996 get_elf_class (unsigned int elf_class)
4997 {
4998 static char buff[32];
4999
5000 switch (elf_class)
5001 {
5002 case ELFCLASSNONE: return _("none");
5003 case ELFCLASS32: return "ELF32";
5004 case ELFCLASS64: return "ELF64";
5005 default:
5006 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5007 return buff;
5008 }
5009 }
5010
5011 static const char *
5012 get_data_encoding (unsigned int encoding)
5013 {
5014 static char buff[32];
5015
5016 switch (encoding)
5017 {
5018 case ELFDATANONE: return _("none");
5019 case ELFDATA2LSB: return _("2's complement, little endian");
5020 case ELFDATA2MSB: return _("2's complement, big endian");
5021 default:
5022 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5023 return buff;
5024 }
5025 }
5026
5027 /* Decode the data held in 'filedata->file_header'. */
5028
5029 static bfd_boolean
5030 process_file_header (Filedata * filedata)
5031 {
5032 Elf_Internal_Ehdr * header = & filedata->file_header;
5033
5034 if ( header->e_ident[EI_MAG0] != ELFMAG0
5035 || header->e_ident[EI_MAG1] != ELFMAG1
5036 || header->e_ident[EI_MAG2] != ELFMAG2
5037 || header->e_ident[EI_MAG3] != ELFMAG3)
5038 {
5039 error
5040 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5041 return FALSE;
5042 }
5043
5044 if (! filedata->is_separate)
5045 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5046
5047 if (do_header)
5048 {
5049 unsigned i;
5050
5051 if (filedata->is_separate)
5052 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5053 else
5054 printf (_("ELF Header:\n"));
5055 printf (_(" Magic: "));
5056 for (i = 0; i < EI_NIDENT; i++)
5057 printf ("%2.2x ", header->e_ident[i]);
5058 printf ("\n");
5059 printf (_(" Class: %s\n"),
5060 get_elf_class (header->e_ident[EI_CLASS]));
5061 printf (_(" Data: %s\n"),
5062 get_data_encoding (header->e_ident[EI_DATA]));
5063 printf (_(" Version: %d%s\n"),
5064 header->e_ident[EI_VERSION],
5065 (header->e_ident[EI_VERSION] == EV_CURRENT
5066 ? _(" (current)")
5067 : (header->e_ident[EI_VERSION] != EV_NONE
5068 ? _(" <unknown>")
5069 : "")));
5070 printf (_(" OS/ABI: %s\n"),
5071 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5072 printf (_(" ABI Version: %d\n"),
5073 header->e_ident[EI_ABIVERSION]);
5074 printf (_(" Type: %s\n"),
5075 get_file_type (header->e_type));
5076 printf (_(" Machine: %s\n"),
5077 get_machine_name (header->e_machine));
5078 printf (_(" Version: 0x%lx\n"),
5079 header->e_version);
5080
5081 printf (_(" Entry point address: "));
5082 print_vma (header->e_entry, PREFIX_HEX);
5083 printf (_("\n Start of program headers: "));
5084 print_vma (header->e_phoff, DEC);
5085 printf (_(" (bytes into file)\n Start of section headers: "));
5086 print_vma (header->e_shoff, DEC);
5087 printf (_(" (bytes into file)\n"));
5088
5089 printf (_(" Flags: 0x%lx%s\n"),
5090 header->e_flags,
5091 get_machine_flags (filedata, header->e_flags, header->e_machine));
5092 printf (_(" Size of this header: %u (bytes)\n"),
5093 header->e_ehsize);
5094 printf (_(" Size of program headers: %u (bytes)\n"),
5095 header->e_phentsize);
5096 printf (_(" Number of program headers: %u"),
5097 header->e_phnum);
5098 if (filedata->section_headers != NULL
5099 && header->e_phnum == PN_XNUM
5100 && filedata->section_headers[0].sh_info != 0)
5101 {
5102 header->e_phnum = filedata->section_headers[0].sh_info;
5103 printf (" (%u)", header->e_phnum);
5104 }
5105 putc ('\n', stdout);
5106 printf (_(" Size of section headers: %u (bytes)\n"),
5107 header->e_shentsize);
5108 printf (_(" Number of section headers: %u"),
5109 header->e_shnum);
5110 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5111 {
5112 header->e_shnum = filedata->section_headers[0].sh_size;
5113 printf (" (%u)", header->e_shnum);
5114 }
5115 putc ('\n', stdout);
5116 printf (_(" Section header string table index: %u"),
5117 header->e_shstrndx);
5118 if (filedata->section_headers != NULL
5119 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5120 {
5121 header->e_shstrndx = filedata->section_headers[0].sh_link;
5122 printf (" (%u)", header->e_shstrndx);
5123 }
5124 if (header->e_shstrndx != SHN_UNDEF
5125 && header->e_shstrndx >= header->e_shnum)
5126 {
5127 header->e_shstrndx = SHN_UNDEF;
5128 printf (_(" <corrupt: out of range>"));
5129 }
5130 putc ('\n', stdout);
5131 }
5132
5133 if (filedata->section_headers != NULL)
5134 {
5135 if (header->e_phnum == PN_XNUM
5136 && filedata->section_headers[0].sh_info != 0)
5137 header->e_phnum = filedata->section_headers[0].sh_info;
5138 if (header->e_shnum == SHN_UNDEF)
5139 header->e_shnum = filedata->section_headers[0].sh_size;
5140 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5141 header->e_shstrndx = filedata->section_headers[0].sh_link;
5142 if (header->e_shstrndx >= header->e_shnum)
5143 header->e_shstrndx = SHN_UNDEF;
5144 free (filedata->section_headers);
5145 filedata->section_headers = NULL;
5146 }
5147
5148 return TRUE;
5149 }
5150
5151 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5152 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5153
5154 static bfd_boolean
5155 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5156 {
5157 Elf32_External_Phdr * phdrs;
5158 Elf32_External_Phdr * external;
5159 Elf_Internal_Phdr * internal;
5160 unsigned int i;
5161 unsigned int size = filedata->file_header.e_phentsize;
5162 unsigned int num = filedata->file_header.e_phnum;
5163
5164 /* PR binutils/17531: Cope with unexpected section header sizes. */
5165 if (size == 0 || num == 0)
5166 return FALSE;
5167 if (size < sizeof * phdrs)
5168 {
5169 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5170 return FALSE;
5171 }
5172 if (size > sizeof * phdrs)
5173 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5174
5175 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5176 size, num, _("program headers"));
5177 if (phdrs == NULL)
5178 return FALSE;
5179
5180 for (i = 0, internal = pheaders, external = phdrs;
5181 i < filedata->file_header.e_phnum;
5182 i++, internal++, external++)
5183 {
5184 internal->p_type = BYTE_GET (external->p_type);
5185 internal->p_offset = BYTE_GET (external->p_offset);
5186 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5187 internal->p_paddr = BYTE_GET (external->p_paddr);
5188 internal->p_filesz = BYTE_GET (external->p_filesz);
5189 internal->p_memsz = BYTE_GET (external->p_memsz);
5190 internal->p_flags = BYTE_GET (external->p_flags);
5191 internal->p_align = BYTE_GET (external->p_align);
5192 }
5193
5194 free (phdrs);
5195 return TRUE;
5196 }
5197
5198 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5199 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5200
5201 static bfd_boolean
5202 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5203 {
5204 Elf64_External_Phdr * phdrs;
5205 Elf64_External_Phdr * external;
5206 Elf_Internal_Phdr * internal;
5207 unsigned int i;
5208 unsigned int size = filedata->file_header.e_phentsize;
5209 unsigned int num = filedata->file_header.e_phnum;
5210
5211 /* PR binutils/17531: Cope with unexpected section header sizes. */
5212 if (size == 0 || num == 0)
5213 return FALSE;
5214 if (size < sizeof * phdrs)
5215 {
5216 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5217 return FALSE;
5218 }
5219 if (size > sizeof * phdrs)
5220 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5221
5222 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5223 size, num, _("program headers"));
5224 if (!phdrs)
5225 return FALSE;
5226
5227 for (i = 0, internal = pheaders, external = phdrs;
5228 i < filedata->file_header.e_phnum;
5229 i++, internal++, external++)
5230 {
5231 internal->p_type = BYTE_GET (external->p_type);
5232 internal->p_flags = BYTE_GET (external->p_flags);
5233 internal->p_offset = BYTE_GET (external->p_offset);
5234 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5235 internal->p_paddr = BYTE_GET (external->p_paddr);
5236 internal->p_filesz = BYTE_GET (external->p_filesz);
5237 internal->p_memsz = BYTE_GET (external->p_memsz);
5238 internal->p_align = BYTE_GET (external->p_align);
5239 }
5240
5241 free (phdrs);
5242 return TRUE;
5243 }
5244
5245 /* Returns TRUE if the program headers were read into `program_headers'. */
5246
5247 static bfd_boolean
5248 get_program_headers (Filedata * filedata)
5249 {
5250 Elf_Internal_Phdr * phdrs;
5251
5252 /* Check cache of prior read. */
5253 if (filedata->program_headers != NULL)
5254 return TRUE;
5255
5256 /* Be kind to memory checkers by looking for
5257 e_phnum values which we know must be invalid. */
5258 if (filedata->file_header.e_phnum
5259 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5260 >= filedata->file_size)
5261 {
5262 error (_("Too many program headers - %#x - the file is not that big\n"),
5263 filedata->file_header.e_phnum);
5264 return FALSE;
5265 }
5266
5267 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5268 sizeof (Elf_Internal_Phdr));
5269 if (phdrs == NULL)
5270 {
5271 error (_("Out of memory reading %u program headers\n"),
5272 filedata->file_header.e_phnum);
5273 return FALSE;
5274 }
5275
5276 if (is_32bit_elf
5277 ? get_32bit_program_headers (filedata, phdrs)
5278 : get_64bit_program_headers (filedata, phdrs))
5279 {
5280 filedata->program_headers = phdrs;
5281 return TRUE;
5282 }
5283
5284 free (phdrs);
5285 return FALSE;
5286 }
5287
5288 /* Returns TRUE if the program headers were loaded. */
5289
5290 static bfd_boolean
5291 process_program_headers (Filedata * filedata)
5292 {
5293 Elf_Internal_Phdr * segment;
5294 unsigned int i;
5295 Elf_Internal_Phdr * previous_load = NULL;
5296
5297 filedata->dynamic_addr = 0;
5298 filedata->dynamic_size = 0;
5299
5300 if (filedata->file_header.e_phnum == 0)
5301 {
5302 /* PR binutils/12467. */
5303 if (filedata->file_header.e_phoff != 0)
5304 {
5305 warn (_("possibly corrupt ELF header - it has a non-zero program"
5306 " header offset, but no program headers\n"));
5307 return FALSE;
5308 }
5309 else if (do_segments)
5310 {
5311 if (filedata->is_separate)
5312 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5313 filedata->file_name);
5314 else
5315 printf (_("\nThere are no program headers in this file.\n"));
5316 }
5317 return TRUE;
5318 }
5319
5320 if (do_segments && !do_header)
5321 {
5322 if (filedata->is_separate)
5323 printf ("\nIn linked file '%s' the ELF file type is %s\n",
5324 filedata->file_name,
5325 get_file_type (filedata->file_header.e_type));
5326 else
5327 printf (_("\nElf file type is %s\n"), get_file_type (filedata->file_header.e_type));
5328 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5329 printf (ngettext ("There is %d program header, starting at offset %s\n",
5330 "There are %d program headers, starting at offset %s\n",
5331 filedata->file_header.e_phnum),
5332 filedata->file_header.e_phnum,
5333 bfd_vmatoa ("u", filedata->file_header.e_phoff));
5334 }
5335
5336 if (! get_program_headers (filedata))
5337 return TRUE;
5338
5339 if (do_segments)
5340 {
5341 if (filedata->file_header.e_phnum > 1)
5342 printf (_("\nProgram Headers:\n"));
5343 else
5344 printf (_("\nProgram Headers:\n"));
5345
5346 if (is_32bit_elf)
5347 printf
5348 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5349 else if (do_wide)
5350 printf
5351 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5352 else
5353 {
5354 printf
5355 (_(" Type Offset VirtAddr PhysAddr\n"));
5356 printf
5357 (_(" FileSiz MemSiz Flags Align\n"));
5358 }
5359 }
5360
5361 for (i = 0, segment = filedata->program_headers;
5362 i < filedata->file_header.e_phnum;
5363 i++, segment++)
5364 {
5365 if (do_segments)
5366 {
5367 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
5368
5369 if (is_32bit_elf)
5370 {
5371 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5372 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5373 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5374 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5375 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5376 printf ("%c%c%c ",
5377 (segment->p_flags & PF_R ? 'R' : ' '),
5378 (segment->p_flags & PF_W ? 'W' : ' '),
5379 (segment->p_flags & PF_X ? 'E' : ' '));
5380 printf ("%#lx", (unsigned long) segment->p_align);
5381 }
5382 else if (do_wide)
5383 {
5384 if ((unsigned long) segment->p_offset == segment->p_offset)
5385 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5386 else
5387 {
5388 print_vma (segment->p_offset, FULL_HEX);
5389 putchar (' ');
5390 }
5391
5392 print_vma (segment->p_vaddr, FULL_HEX);
5393 putchar (' ');
5394 print_vma (segment->p_paddr, FULL_HEX);
5395 putchar (' ');
5396
5397 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5398 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5399 else
5400 {
5401 print_vma (segment->p_filesz, FULL_HEX);
5402 putchar (' ');
5403 }
5404
5405 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5406 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5407 else
5408 {
5409 print_vma (segment->p_memsz, FULL_HEX);
5410 }
5411
5412 printf (" %c%c%c ",
5413 (segment->p_flags & PF_R ? 'R' : ' '),
5414 (segment->p_flags & PF_W ? 'W' : ' '),
5415 (segment->p_flags & PF_X ? 'E' : ' '));
5416
5417 if ((unsigned long) segment->p_align == segment->p_align)
5418 printf ("%#lx", (unsigned long) segment->p_align);
5419 else
5420 {
5421 print_vma (segment->p_align, PREFIX_HEX);
5422 }
5423 }
5424 else
5425 {
5426 print_vma (segment->p_offset, FULL_HEX);
5427 putchar (' ');
5428 print_vma (segment->p_vaddr, FULL_HEX);
5429 putchar (' ');
5430 print_vma (segment->p_paddr, FULL_HEX);
5431 printf ("\n ");
5432 print_vma (segment->p_filesz, FULL_HEX);
5433 putchar (' ');
5434 print_vma (segment->p_memsz, FULL_HEX);
5435 printf (" %c%c%c ",
5436 (segment->p_flags & PF_R ? 'R' : ' '),
5437 (segment->p_flags & PF_W ? 'W' : ' '),
5438 (segment->p_flags & PF_X ? 'E' : ' '));
5439 print_vma (segment->p_align, PREFIX_HEX);
5440 }
5441
5442 putc ('\n', stdout);
5443 }
5444
5445 switch (segment->p_type)
5446 {
5447 case PT_LOAD:
5448 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5449 required by the ELF standard, several programs, including the Linux
5450 kernel, make use of non-ordered segments. */
5451 if (previous_load
5452 && previous_load->p_vaddr > segment->p_vaddr)
5453 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5454 #endif
5455 if (segment->p_memsz < segment->p_filesz)
5456 error (_("the segment's file size is larger than its memory size\n"));
5457 previous_load = segment;
5458 break;
5459
5460 case PT_PHDR:
5461 /* PR 20815 - Verify that the program header is loaded into memory. */
5462 if (i > 0 && previous_load != NULL)
5463 error (_("the PHDR segment must occur before any LOAD segment\n"));
5464 if (filedata->file_header.e_machine != EM_PARISC)
5465 {
5466 unsigned int j;
5467
5468 for (j = 1; j < filedata->file_header.e_phnum; j++)
5469 {
5470 Elf_Internal_Phdr *load = filedata->program_headers + j;
5471 if (load->p_type == PT_LOAD
5472 && load->p_offset <= segment->p_offset
5473 && (load->p_offset + load->p_filesz
5474 >= segment->p_offset + segment->p_filesz)
5475 && load->p_vaddr <= segment->p_vaddr
5476 && (load->p_vaddr + load->p_filesz
5477 >= segment->p_vaddr + segment->p_filesz))
5478 break;
5479 }
5480 if (j == filedata->file_header.e_phnum)
5481 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5482 }
5483 break;
5484
5485 case PT_DYNAMIC:
5486 if (filedata->dynamic_addr)
5487 error (_("more than one dynamic segment\n"));
5488
5489 /* By default, assume that the .dynamic section is the first
5490 section in the DYNAMIC segment. */
5491 filedata->dynamic_addr = segment->p_offset;
5492 filedata->dynamic_size = segment->p_filesz;
5493
5494 /* Try to locate the .dynamic section. If there is
5495 a section header table, we can easily locate it. */
5496 if (filedata->section_headers != NULL)
5497 {
5498 Elf_Internal_Shdr * sec;
5499
5500 sec = find_section (filedata, ".dynamic");
5501 if (sec == NULL || sec->sh_size == 0)
5502 {
5503 /* A corresponding .dynamic section is expected, but on
5504 IA-64/OpenVMS it is OK for it to be missing. */
5505 if (!is_ia64_vms (filedata))
5506 error (_("no .dynamic section in the dynamic segment\n"));
5507 break;
5508 }
5509
5510 if (sec->sh_type == SHT_NOBITS)
5511 {
5512 filedata->dynamic_size = 0;
5513 break;
5514 }
5515
5516 filedata->dynamic_addr = sec->sh_offset;
5517 filedata->dynamic_size = sec->sh_size;
5518
5519 /* The PT_DYNAMIC segment, which is used by the run-time
5520 loader, should exactly match the .dynamic section. */
5521 if (do_checks
5522 && (filedata->dynamic_addr != segment->p_offset
5523 || filedata->dynamic_size != segment->p_filesz))
5524 warn (_("\
5525 the .dynamic section is not the same as the dynamic segment\n"));
5526 }
5527
5528 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5529 segment. Check this after matching against the section headers
5530 so we don't warn on debuginfo file (which have NOBITS .dynamic
5531 sections). */
5532 if (filedata->dynamic_addr > filedata->file_size
5533 || (filedata->dynamic_size
5534 > filedata->file_size - filedata->dynamic_addr))
5535 {
5536 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5537 filedata->dynamic_addr = filedata->dynamic_size = 0;
5538 }
5539 break;
5540
5541 case PT_INTERP:
5542 if (fseek (filedata->handle,
5543 filedata->archive_file_offset + (long) segment->p_offset,
5544 SEEK_SET))
5545 error (_("Unable to find program interpreter name\n"));
5546 else
5547 {
5548 char fmt [32];
5549 int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
5550
5551 if (ret >= (int) sizeof (fmt) || ret < 0)
5552 error (_("Internal error: failed to create format string to display program interpreter\n"));
5553
5554 filedata->program_interpreter[0] = 0;
5555 if (fscanf (filedata->handle, fmt,
5556 filedata->program_interpreter) <= 0)
5557 error (_("Unable to read program interpreter name\n"));
5558
5559 if (do_segments)
5560 printf (_(" [Requesting program interpreter: %s]\n"),
5561 filedata->program_interpreter);
5562 }
5563 break;
5564 }
5565 }
5566
5567 if (do_segments
5568 && filedata->section_headers != NULL
5569 && filedata->string_table != NULL)
5570 {
5571 printf (_("\n Section to Segment mapping:\n"));
5572 printf (_(" Segment Sections...\n"));
5573
5574 for (i = 0; i < filedata->file_header.e_phnum; i++)
5575 {
5576 unsigned int j;
5577 Elf_Internal_Shdr * section;
5578
5579 segment = filedata->program_headers + i;
5580 section = filedata->section_headers + 1;
5581
5582 printf (" %2.2d ", i);
5583
5584 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5585 {
5586 if (!ELF_TBSS_SPECIAL (section, segment)
5587 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5588 printf ("%s ", printable_section_name (filedata, section));
5589 }
5590
5591 putc ('\n',stdout);
5592 }
5593 }
5594
5595 return TRUE;
5596 }
5597
5598
5599 /* Find the file offset corresponding to VMA by using the program headers. */
5600
5601 static long
5602 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5603 {
5604 Elf_Internal_Phdr * seg;
5605
5606 if (! get_program_headers (filedata))
5607 {
5608 warn (_("Cannot interpret virtual addresses without program headers.\n"));
5609 return (long) vma;
5610 }
5611
5612 for (seg = filedata->program_headers;
5613 seg < filedata->program_headers + filedata->file_header.e_phnum;
5614 ++seg)
5615 {
5616 if (seg->p_type != PT_LOAD)
5617 continue;
5618
5619 if (vma >= (seg->p_vaddr & -seg->p_align)
5620 && vma + size <= seg->p_vaddr + seg->p_filesz)
5621 return vma - seg->p_vaddr + seg->p_offset;
5622 }
5623
5624 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5625 (unsigned long) vma);
5626 return (long) vma;
5627 }
5628
5629
5630 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5631 If PROBE is true, this is just a probe and we do not generate any error
5632 messages if the load fails. */
5633
5634 static bfd_boolean
5635 get_32bit_section_headers (Filedata * filedata, bfd_boolean probe)
5636 {
5637 Elf32_External_Shdr * shdrs;
5638 Elf_Internal_Shdr * internal;
5639 unsigned int i;
5640 unsigned int size = filedata->file_header.e_shentsize;
5641 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5642
5643 /* PR binutils/17531: Cope with unexpected section header sizes. */
5644 if (size == 0 || num == 0)
5645 return FALSE;
5646 if (size < sizeof * shdrs)
5647 {
5648 if (! probe)
5649 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5650 return FALSE;
5651 }
5652 if (!probe && size > sizeof * shdrs)
5653 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5654
5655 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5656 size, num,
5657 probe ? NULL : _("section headers"));
5658 if (shdrs == NULL)
5659 return FALSE;
5660
5661 free (filedata->section_headers);
5662 filedata->section_headers = (Elf_Internal_Shdr *)
5663 cmalloc (num, sizeof (Elf_Internal_Shdr));
5664 if (filedata->section_headers == NULL)
5665 {
5666 if (!probe)
5667 error (_("Out of memory reading %u section headers\n"), num);
5668 free (shdrs);
5669 return FALSE;
5670 }
5671
5672 for (i = 0, internal = filedata->section_headers;
5673 i < num;
5674 i++, internal++)
5675 {
5676 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5677 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5678 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5679 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5680 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5681 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5682 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5683 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5684 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5685 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5686 if (!probe && internal->sh_link > num)
5687 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5688 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5689 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5690 }
5691
5692 free (shdrs);
5693 return TRUE;
5694 }
5695
5696 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
5697
5698 static bfd_boolean
5699 get_64bit_section_headers (Filedata * filedata, bfd_boolean probe)
5700 {
5701 Elf64_External_Shdr * shdrs;
5702 Elf_Internal_Shdr * internal;
5703 unsigned int i;
5704 unsigned int size = filedata->file_header.e_shentsize;
5705 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5706
5707 /* PR binutils/17531: Cope with unexpected section header sizes. */
5708 if (size == 0 || num == 0)
5709 return FALSE;
5710
5711 if (size < sizeof * shdrs)
5712 {
5713 if (! probe)
5714 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5715 return FALSE;
5716 }
5717
5718 if (! probe && size > sizeof * shdrs)
5719 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5720
5721 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5722 filedata->file_header.e_shoff,
5723 size, num,
5724 probe ? NULL : _("section headers"));
5725 if (shdrs == NULL)
5726 return FALSE;
5727
5728 free (filedata->section_headers);
5729 filedata->section_headers = (Elf_Internal_Shdr *)
5730 cmalloc (num, sizeof (Elf_Internal_Shdr));
5731 if (filedata->section_headers == NULL)
5732 {
5733 if (! probe)
5734 error (_("Out of memory reading %u section headers\n"), num);
5735 free (shdrs);
5736 return FALSE;
5737 }
5738
5739 for (i = 0, internal = filedata->section_headers;
5740 i < num;
5741 i++, internal++)
5742 {
5743 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5744 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5745 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5746 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5747 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5748 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5749 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5750 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5751 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5752 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5753 if (!probe && internal->sh_link > num)
5754 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5755 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5756 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5757 }
5758
5759 free (shdrs);
5760 return TRUE;
5761 }
5762
5763 static Elf_Internal_Sym *
5764 get_32bit_elf_symbols (Filedata * filedata,
5765 Elf_Internal_Shdr * section,
5766 unsigned long * num_syms_return)
5767 {
5768 unsigned long number = 0;
5769 Elf32_External_Sym * esyms = NULL;
5770 Elf_External_Sym_Shndx * shndx = NULL;
5771 Elf_Internal_Sym * isyms = NULL;
5772 Elf_Internal_Sym * psym;
5773 unsigned int j;
5774 elf_section_list * entry;
5775
5776 if (section->sh_size == 0)
5777 {
5778 if (num_syms_return != NULL)
5779 * num_syms_return = 0;
5780 return NULL;
5781 }
5782
5783 /* Run some sanity checks first. */
5784 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5785 {
5786 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5787 printable_section_name (filedata, section),
5788 (unsigned long) section->sh_entsize);
5789 goto exit_point;
5790 }
5791
5792 if (section->sh_size > filedata->file_size)
5793 {
5794 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5795 printable_section_name (filedata, section),
5796 (unsigned long) section->sh_size);
5797 goto exit_point;
5798 }
5799
5800 number = section->sh_size / section->sh_entsize;
5801
5802 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5803 {
5804 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5805 (unsigned long) section->sh_size,
5806 printable_section_name (filedata, section),
5807 (unsigned long) section->sh_entsize);
5808 goto exit_point;
5809 }
5810
5811 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5812 section->sh_size, _("symbols"));
5813 if (esyms == NULL)
5814 goto exit_point;
5815
5816 shndx = NULL;
5817 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
5818 {
5819 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5820 continue;
5821
5822 if (shndx != NULL)
5823 {
5824 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5825 free (shndx);
5826 }
5827
5828 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5829 entry->hdr->sh_offset,
5830 1, entry->hdr->sh_size,
5831 _("symbol table section indices"));
5832 if (shndx == NULL)
5833 goto exit_point;
5834
5835 /* PR17531: file: heap-buffer-overflow */
5836 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5837 {
5838 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5839 printable_section_name (filedata, entry->hdr),
5840 (unsigned long) entry->hdr->sh_size,
5841 (unsigned long) section->sh_size);
5842 goto exit_point;
5843 }
5844 }
5845
5846 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5847
5848 if (isyms == NULL)
5849 {
5850 error (_("Out of memory reading %lu symbols\n"),
5851 (unsigned long) number);
5852 goto exit_point;
5853 }
5854
5855 for (j = 0, psym = isyms; j < number; j++, psym++)
5856 {
5857 psym->st_name = BYTE_GET (esyms[j].st_name);
5858 psym->st_value = BYTE_GET (esyms[j].st_value);
5859 psym->st_size = BYTE_GET (esyms[j].st_size);
5860 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5861 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5862 psym->st_shndx
5863 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5864 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5865 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5866 psym->st_info = BYTE_GET (esyms[j].st_info);
5867 psym->st_other = BYTE_GET (esyms[j].st_other);
5868 }
5869
5870 exit_point:
5871 free (shndx);
5872 free (esyms);
5873
5874 if (num_syms_return != NULL)
5875 * num_syms_return = isyms == NULL ? 0 : number;
5876
5877 return isyms;
5878 }
5879
5880 static Elf_Internal_Sym *
5881 get_64bit_elf_symbols (Filedata * filedata,
5882 Elf_Internal_Shdr * section,
5883 unsigned long * num_syms_return)
5884 {
5885 unsigned long number = 0;
5886 Elf64_External_Sym * esyms = NULL;
5887 Elf_External_Sym_Shndx * shndx = NULL;
5888 Elf_Internal_Sym * isyms = NULL;
5889 Elf_Internal_Sym * psym;
5890 unsigned int j;
5891 elf_section_list * entry;
5892
5893 if (section->sh_size == 0)
5894 {
5895 if (num_syms_return != NULL)
5896 * num_syms_return = 0;
5897 return NULL;
5898 }
5899
5900 /* Run some sanity checks first. */
5901 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5902 {
5903 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5904 printable_section_name (filedata, section),
5905 (unsigned long) section->sh_entsize);
5906 goto exit_point;
5907 }
5908
5909 if (section->sh_size > filedata->file_size)
5910 {
5911 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5912 printable_section_name (filedata, section),
5913 (unsigned long) section->sh_size);
5914 goto exit_point;
5915 }
5916
5917 number = section->sh_size / section->sh_entsize;
5918
5919 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
5920 {
5921 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5922 (unsigned long) section->sh_size,
5923 printable_section_name (filedata, section),
5924 (unsigned long) section->sh_entsize);
5925 goto exit_point;
5926 }
5927
5928 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5929 section->sh_size, _("symbols"));
5930 if (!esyms)
5931 goto exit_point;
5932
5933 shndx = NULL;
5934 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
5935 {
5936 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5937 continue;
5938
5939 if (shndx != NULL)
5940 {
5941 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5942 free (shndx);
5943 }
5944
5945 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5946 entry->hdr->sh_offset,
5947 1, entry->hdr->sh_size,
5948 _("symbol table section indices"));
5949 if (shndx == NULL)
5950 goto exit_point;
5951
5952 /* PR17531: file: heap-buffer-overflow */
5953 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5954 {
5955 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5956 printable_section_name (filedata, entry->hdr),
5957 (unsigned long) entry->hdr->sh_size,
5958 (unsigned long) section->sh_size);
5959 goto exit_point;
5960 }
5961 }
5962
5963 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5964
5965 if (isyms == NULL)
5966 {
5967 error (_("Out of memory reading %lu symbols\n"),
5968 (unsigned long) number);
5969 goto exit_point;
5970 }
5971
5972 for (j = 0, psym = isyms; j < number; j++, psym++)
5973 {
5974 psym->st_name = BYTE_GET (esyms[j].st_name);
5975 psym->st_info = BYTE_GET (esyms[j].st_info);
5976 psym->st_other = BYTE_GET (esyms[j].st_other);
5977 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5978
5979 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5980 psym->st_shndx
5981 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5982 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5983 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5984
5985 psym->st_value = BYTE_GET (esyms[j].st_value);
5986 psym->st_size = BYTE_GET (esyms[j].st_size);
5987 }
5988
5989 exit_point:
5990 free (shndx);
5991 free (esyms);
5992
5993 if (num_syms_return != NULL)
5994 * num_syms_return = isyms == NULL ? 0 : number;
5995
5996 return isyms;
5997 }
5998
5999 static const char *
6000 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6001 {
6002 static char buff[1024];
6003 char * p = buff;
6004 unsigned int field_size = is_32bit_elf ? 8 : 16;
6005 signed int sindex;
6006 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6007 bfd_vma os_flags = 0;
6008 bfd_vma proc_flags = 0;
6009 bfd_vma unknown_flags = 0;
6010 static const struct
6011 {
6012 const char * str;
6013 unsigned int len;
6014 }
6015 flags [] =
6016 {
6017 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6018 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6019 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6020 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6021 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6022 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6023 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6024 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6025 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6026 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6027 /* IA-64 specific. */
6028 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6029 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6030 /* IA-64 OpenVMS specific. */
6031 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6032 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6033 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6034 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6035 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6036 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6037 /* Generic. */
6038 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6039 /* SPARC specific. */
6040 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6041 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6042 /* ARM specific. */
6043 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6044 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6045 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6046 /* GNU specific. */
6047 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6048 /* VLE specific. */
6049 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6050 /* GNU specific. */
6051 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6052 };
6053
6054 if (do_section_details)
6055 {
6056 sprintf (buff, "[%*.*lx]: ",
6057 field_size, field_size, (unsigned long) sh_flags);
6058 p += field_size + 4;
6059 }
6060
6061 while (sh_flags)
6062 {
6063 bfd_vma flag;
6064
6065 flag = sh_flags & - sh_flags;
6066 sh_flags &= ~ flag;
6067
6068 if (do_section_details)
6069 {
6070 switch (flag)
6071 {
6072 case SHF_WRITE: sindex = 0; break;
6073 case SHF_ALLOC: sindex = 1; break;
6074 case SHF_EXECINSTR: sindex = 2; break;
6075 case SHF_MERGE: sindex = 3; break;
6076 case SHF_STRINGS: sindex = 4; break;
6077 case SHF_INFO_LINK: sindex = 5; break;
6078 case SHF_LINK_ORDER: sindex = 6; break;
6079 case SHF_OS_NONCONFORMING: sindex = 7; break;
6080 case SHF_GROUP: sindex = 8; break;
6081 case SHF_TLS: sindex = 9; break;
6082 case SHF_EXCLUDE: sindex = 18; break;
6083 case SHF_COMPRESSED: sindex = 20; break;
6084
6085 default:
6086 sindex = -1;
6087 switch (filedata->file_header.e_machine)
6088 {
6089 case EM_IA_64:
6090 if (flag == SHF_IA_64_SHORT)
6091 sindex = 10;
6092 else if (flag == SHF_IA_64_NORECOV)
6093 sindex = 11;
6094 #ifdef BFD64
6095 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6096 switch (flag)
6097 {
6098 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6099 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6100 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6101 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6102 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6103 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6104 default: break;
6105 }
6106 #endif
6107 break;
6108
6109 case EM_386:
6110 case EM_IAMCU:
6111 case EM_X86_64:
6112 case EM_L1OM:
6113 case EM_K1OM:
6114 case EM_OLD_SPARCV9:
6115 case EM_SPARC32PLUS:
6116 case EM_SPARCV9:
6117 case EM_SPARC:
6118 if (flag == SHF_ORDERED)
6119 sindex = 19;
6120 break;
6121
6122 case EM_ARM:
6123 switch (flag)
6124 {
6125 case SHF_ENTRYSECT: sindex = 21; break;
6126 case SHF_ARM_PURECODE: sindex = 22; break;
6127 case SHF_COMDEF: sindex = 23; break;
6128 default: break;
6129 }
6130 break;
6131 case EM_PPC:
6132 if (flag == SHF_PPC_VLE)
6133 sindex = 25;
6134 break;
6135 default:
6136 break;
6137 }
6138
6139 switch (filedata->file_header.e_ident[EI_OSABI])
6140 {
6141 case ELFOSABI_GNU:
6142 case ELFOSABI_FREEBSD:
6143 if (flag == SHF_GNU_RETAIN)
6144 sindex = 26;
6145 /* Fall through */
6146 case ELFOSABI_NONE:
6147 if (flag == SHF_GNU_MBIND)
6148 /* We should not recognize SHF_GNU_MBIND for
6149 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6150 not set the EI_OSABI header byte. */
6151 sindex = 24;
6152 break;
6153 default:
6154 break;
6155 }
6156 break;
6157 }
6158
6159 if (sindex != -1)
6160 {
6161 if (p != buff + field_size + 4)
6162 {
6163 if (size < (10 + 2))
6164 {
6165 warn (_("Internal error: not enough buffer room for section flag info"));
6166 return _("<unknown>");
6167 }
6168 size -= 2;
6169 *p++ = ',';
6170 *p++ = ' ';
6171 }
6172
6173 size -= flags [sindex].len;
6174 p = stpcpy (p, flags [sindex].str);
6175 }
6176 else if (flag & SHF_MASKOS)
6177 os_flags |= flag;
6178 else if (flag & SHF_MASKPROC)
6179 proc_flags |= flag;
6180 else
6181 unknown_flags |= flag;
6182 }
6183 else
6184 {
6185 switch (flag)
6186 {
6187 case SHF_WRITE: *p = 'W'; break;
6188 case SHF_ALLOC: *p = 'A'; break;
6189 case SHF_EXECINSTR: *p = 'X'; break;
6190 case SHF_MERGE: *p = 'M'; break;
6191 case SHF_STRINGS: *p = 'S'; break;
6192 case SHF_INFO_LINK: *p = 'I'; break;
6193 case SHF_LINK_ORDER: *p = 'L'; break;
6194 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6195 case SHF_GROUP: *p = 'G'; break;
6196 case SHF_TLS: *p = 'T'; break;
6197 case SHF_EXCLUDE: *p = 'E'; break;
6198 case SHF_COMPRESSED: *p = 'C'; break;
6199
6200 default:
6201 if ((filedata->file_header.e_machine == EM_X86_64
6202 || filedata->file_header.e_machine == EM_L1OM
6203 || filedata->file_header.e_machine == EM_K1OM)
6204 && flag == SHF_X86_64_LARGE)
6205 *p = 'l';
6206 else if (filedata->file_header.e_machine == EM_ARM
6207 && flag == SHF_ARM_PURECODE)
6208 *p = 'y';
6209 else if (filedata->file_header.e_machine == EM_PPC
6210 && flag == SHF_PPC_VLE)
6211 *p = 'v';
6212 else if (flag & SHF_MASKOS)
6213 {
6214 switch (filedata->file_header.e_ident[EI_OSABI])
6215 {
6216 case ELFOSABI_GNU:
6217 case ELFOSABI_FREEBSD:
6218 if (flag == SHF_GNU_RETAIN)
6219 {
6220 *p = 'R';
6221 break;
6222 }
6223 /* Fall through */
6224 case ELFOSABI_NONE:
6225 if (flag == SHF_GNU_MBIND)
6226 {
6227 /* We should not recognize SHF_GNU_MBIND for
6228 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6229 not set the EI_OSABI header byte. */
6230 *p = 'D';
6231 break;
6232 }
6233 /* Fall through */
6234 default:
6235 *p = 'o';
6236 sh_flags &= ~SHF_MASKOS;
6237 break;
6238 }
6239 }
6240 else if (flag & SHF_MASKPROC)
6241 {
6242 *p = 'p';
6243 sh_flags &= ~ SHF_MASKPROC;
6244 }
6245 else
6246 *p = 'x';
6247 break;
6248 }
6249 p++;
6250 }
6251 }
6252
6253 if (do_section_details)
6254 {
6255 if (os_flags)
6256 {
6257 size -= 5 + field_size;
6258 if (p != buff + field_size + 4)
6259 {
6260 if (size < (2 + 1))
6261 {
6262 warn (_("Internal error: not enough buffer room for section flag info"));
6263 return _("<unknown>");
6264 }
6265 size -= 2;
6266 *p++ = ',';
6267 *p++ = ' ';
6268 }
6269 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6270 (unsigned long) os_flags);
6271 p += 5 + field_size;
6272 }
6273 if (proc_flags)
6274 {
6275 size -= 7 + field_size;
6276 if (p != buff + field_size + 4)
6277 {
6278 if (size < (2 + 1))
6279 {
6280 warn (_("Internal error: not enough buffer room for section flag info"));
6281 return _("<unknown>");
6282 }
6283 size -= 2;
6284 *p++ = ',';
6285 *p++ = ' ';
6286 }
6287 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6288 (unsigned long) proc_flags);
6289 p += 7 + field_size;
6290 }
6291 if (unknown_flags)
6292 {
6293 size -= 10 + field_size;
6294 if (p != buff + field_size + 4)
6295 {
6296 if (size < (2 + 1))
6297 {
6298 warn (_("Internal error: not enough buffer room for section flag info"));
6299 return _("<unknown>");
6300 }
6301 size -= 2;
6302 *p++ = ',';
6303 *p++ = ' ';
6304 }
6305 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6306 (unsigned long) unknown_flags);
6307 p += 10 + field_size;
6308 }
6309 }
6310
6311 *p = '\0';
6312 return buff;
6313 }
6314
6315 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
6316 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6317 {
6318 if (is_32bit_elf)
6319 {
6320 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6321
6322 if (size < sizeof (* echdr))
6323 {
6324 error (_("Compressed section is too small even for a compression header\n"));
6325 return 0;
6326 }
6327
6328 chdr->ch_type = BYTE_GET (echdr->ch_type);
6329 chdr->ch_size = BYTE_GET (echdr->ch_size);
6330 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6331 return sizeof (*echdr);
6332 }
6333 else
6334 {
6335 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6336
6337 if (size < sizeof (* echdr))
6338 {
6339 error (_("Compressed section is too small even for a compression header\n"));
6340 return 0;
6341 }
6342
6343 chdr->ch_type = BYTE_GET (echdr->ch_type);
6344 chdr->ch_size = BYTE_GET (echdr->ch_size);
6345 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6346 return sizeof (*echdr);
6347 }
6348 }
6349
6350 static bfd_boolean
6351 process_section_headers (Filedata * filedata)
6352 {
6353 Elf_Internal_Shdr * section;
6354 unsigned int i;
6355
6356 free (filedata->section_headers);
6357 filedata->section_headers = NULL;
6358 free (filedata->dynamic_symbols);
6359 filedata->dynamic_symbols = NULL;
6360 filedata->num_dynamic_syms = 0;
6361 free (filedata->dynamic_strings);
6362 filedata->dynamic_strings = NULL;
6363 filedata->dynamic_strings_length = 0;
6364 free (filedata->dynamic_syminfo);
6365 filedata->dynamic_syminfo = NULL;
6366 while (filedata->symtab_shndx_list != NULL)
6367 {
6368 elf_section_list *next = filedata->symtab_shndx_list->next;
6369 free (filedata->symtab_shndx_list);
6370 filedata->symtab_shndx_list = next;
6371 }
6372
6373 if (filedata->file_header.e_shnum == 0)
6374 {
6375 /* PR binutils/12467. */
6376 if (filedata->file_header.e_shoff != 0)
6377 {
6378 warn (_("possibly corrupt ELF file header - it has a non-zero"
6379 " section header offset, but no section headers\n"));
6380 return FALSE;
6381 }
6382 else if (do_sections)
6383 printf (_("\nThere are no sections in this file.\n"));
6384
6385 return TRUE;
6386 }
6387
6388 if (do_sections && !do_header)
6389 {
6390 if (filedata->is_separate && process_links)
6391 printf (_("In linked file '%s': "), filedata->file_name);
6392 if (! filedata->is_separate || process_links)
6393 printf (ngettext ("There is %d section header, "
6394 "starting at offset 0x%lx:\n",
6395 "There are %d section headers, "
6396 "starting at offset 0x%lx:\n",
6397 filedata->file_header.e_shnum),
6398 filedata->file_header.e_shnum,
6399 (unsigned long) filedata->file_header.e_shoff);
6400 }
6401
6402 if (is_32bit_elf)
6403 {
6404 if (! get_32bit_section_headers (filedata, FALSE))
6405 return FALSE;
6406 }
6407 else
6408 {
6409 if (! get_64bit_section_headers (filedata, FALSE))
6410 return FALSE;
6411 }
6412
6413 /* Read in the string table, so that we have names to display. */
6414 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6415 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6416 {
6417 section = filedata->section_headers + filedata->file_header.e_shstrndx;
6418
6419 if (section->sh_size != 0)
6420 {
6421 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6422 1, section->sh_size,
6423 _("string table"));
6424
6425 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6426 }
6427 }
6428
6429 /* Scan the sections for the dynamic symbol table
6430 and dynamic string table and debug sections. */
6431 eh_addr_size = is_32bit_elf ? 4 : 8;
6432 switch (filedata->file_header.e_machine)
6433 {
6434 case EM_MIPS:
6435 case EM_MIPS_RS3_LE:
6436 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6437 FDE addresses. However, the ABI also has a semi-official ILP32
6438 variant for which the normal FDE address size rules apply.
6439
6440 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6441 section, where XX is the size of longs in bits. Unfortunately,
6442 earlier compilers provided no way of distinguishing ILP32 objects
6443 from LP64 objects, so if there's any doubt, we should assume that
6444 the official LP64 form is being used. */
6445 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6446 && find_section (filedata, ".gcc_compiled_long32") == NULL)
6447 eh_addr_size = 8;
6448 break;
6449
6450 case EM_H8_300:
6451 case EM_H8_300H:
6452 switch (filedata->file_header.e_flags & EF_H8_MACH)
6453 {
6454 case E_H8_MACH_H8300:
6455 case E_H8_MACH_H8300HN:
6456 case E_H8_MACH_H8300SN:
6457 case E_H8_MACH_H8300SXN:
6458 eh_addr_size = 2;
6459 break;
6460 case E_H8_MACH_H8300H:
6461 case E_H8_MACH_H8300S:
6462 case E_H8_MACH_H8300SX:
6463 eh_addr_size = 4;
6464 break;
6465 }
6466 break;
6467
6468 case EM_M32C_OLD:
6469 case EM_M32C:
6470 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6471 {
6472 case EF_M32C_CPU_M16C:
6473 eh_addr_size = 2;
6474 break;
6475 }
6476 break;
6477 }
6478
6479 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6480 do \
6481 { \
6482 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6483 if (section->sh_entsize != expected_entsize) \
6484 { \
6485 char buf[40]; \
6486 sprintf_vma (buf, section->sh_entsize); \
6487 /* Note: coded this way so that there is a single string for \
6488 translation. */ \
6489 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6490 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6491 (unsigned) expected_entsize); \
6492 section->sh_entsize = expected_entsize; \
6493 } \
6494 } \
6495 while (0)
6496
6497 #define CHECK_ENTSIZE(section, i, type) \
6498 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
6499 sizeof (Elf64_External_##type))
6500
6501 for (i = 0, section = filedata->section_headers;
6502 i < filedata->file_header.e_shnum;
6503 i++, section++)
6504 {
6505 char * name = SECTION_NAME_PRINT (section);
6506
6507 /* Run some sanity checks on the headers and
6508 possibly fill in some file data as well. */
6509 switch (section->sh_type)
6510 {
6511 case SHT_DYNSYM:
6512 if (filedata->dynamic_symbols != NULL)
6513 {
6514 error (_("File contains multiple dynamic symbol tables\n"));
6515 continue;
6516 }
6517
6518 CHECK_ENTSIZE (section, i, Sym);
6519 filedata->dynamic_symbols
6520 = GET_ELF_SYMBOLS (filedata, section, &filedata->num_dynamic_syms);
6521 filedata->dynamic_symtab_section = section;
6522 break;
6523
6524 case SHT_STRTAB:
6525 if (streq (name, ".dynstr"))
6526 {
6527 if (filedata->dynamic_strings != NULL)
6528 {
6529 error (_("File contains multiple dynamic string tables\n"));
6530 continue;
6531 }
6532
6533 filedata->dynamic_strings
6534 = (char *) get_data (NULL, filedata, section->sh_offset,
6535 1, section->sh_size, _("dynamic strings"));
6536 filedata->dynamic_strings_length
6537 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
6538 filedata->dynamic_strtab_section = section;
6539 }
6540 break;
6541
6542 case SHT_SYMTAB_SHNDX:
6543 {
6544 elf_section_list * entry = xmalloc (sizeof * entry);
6545
6546 entry->hdr = section;
6547 entry->next = filedata->symtab_shndx_list;
6548 filedata->symtab_shndx_list = entry;
6549 }
6550 break;
6551
6552 case SHT_SYMTAB:
6553 CHECK_ENTSIZE (section, i, Sym);
6554 break;
6555
6556 case SHT_GROUP:
6557 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6558 break;
6559
6560 case SHT_REL:
6561 CHECK_ENTSIZE (section, i, Rel);
6562 if (do_checks && section->sh_size == 0)
6563 warn (_("Section '%s': zero-sized relocation section\n"), name);
6564 break;
6565
6566 case SHT_RELA:
6567 CHECK_ENTSIZE (section, i, Rela);
6568 if (do_checks && section->sh_size == 0)
6569 warn (_("Section '%s': zero-sized relocation section\n"), name);
6570 break;
6571
6572 case SHT_NOTE:
6573 case SHT_PROGBITS:
6574 /* Having a zero sized section is not illegal according to the
6575 ELF standard, but it might be an indication that something
6576 is wrong. So issue a warning if we are running in lint mode. */
6577 if (do_checks && section->sh_size == 0)
6578 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6579 break;
6580
6581 default:
6582 break;
6583 }
6584
6585 if ((do_debugging || do_debug_info || do_debug_abbrevs
6586 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6587 || do_debug_aranges || do_debug_frames || do_debug_macinfo
6588 || do_debug_str || do_debug_str_offsets || do_debug_loc
6589 || do_debug_ranges
6590 || do_debug_addr || do_debug_cu_index || do_debug_links)
6591 && (const_strneq (name, ".debug_")
6592 || const_strneq (name, ".zdebug_")))
6593 {
6594 if (name[1] == 'z')
6595 name += sizeof (".zdebug_") - 1;
6596 else
6597 name += sizeof (".debug_") - 1;
6598
6599 if (do_debugging
6600 || (do_debug_info && const_strneq (name, "info"))
6601 || (do_debug_info && const_strneq (name, "types"))
6602 || (do_debug_abbrevs && const_strneq (name, "abbrev"))
6603 || (do_debug_lines && strcmp (name, "line") == 0)
6604 || (do_debug_lines && const_strneq (name, "line."))
6605 || (do_debug_pubnames && const_strneq (name, "pubnames"))
6606 || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
6607 || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
6608 || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
6609 || (do_debug_aranges && const_strneq (name, "aranges"))
6610 || (do_debug_ranges && const_strneq (name, "ranges"))
6611 || (do_debug_ranges && const_strneq (name, "rnglists"))
6612 || (do_debug_frames && const_strneq (name, "frame"))
6613 || (do_debug_macinfo && const_strneq (name, "macinfo"))
6614 || (do_debug_macinfo && const_strneq (name, "macro"))
6615 || (do_debug_str && const_strneq (name, "str"))
6616 || (do_debug_links && const_strneq (name, "sup"))
6617 || (do_debug_str_offsets && const_strneq (name, "str_offsets"))
6618 || (do_debug_loc && const_strneq (name, "loc"))
6619 || (do_debug_loc && const_strneq (name, "loclists"))
6620 || (do_debug_addr && const_strneq (name, "addr"))
6621 || (do_debug_cu_index && const_strneq (name, "cu_index"))
6622 || (do_debug_cu_index && const_strneq (name, "tu_index"))
6623 )
6624 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6625 }
6626 /* Linkonce section to be combined with .debug_info at link time. */
6627 else if ((do_debugging || do_debug_info)
6628 && const_strneq (name, ".gnu.linkonce.wi."))
6629 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6630 else if (do_debug_frames && streq (name, ".eh_frame"))
6631 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6632 else if (do_gdb_index && (streq (name, ".gdb_index")
6633 || streq (name, ".debug_names")))
6634 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6635 /* Trace sections for Itanium VMS. */
6636 else if ((do_debugging || do_trace_info || do_trace_abbrevs
6637 || do_trace_aranges)
6638 && const_strneq (name, ".trace_"))
6639 {
6640 name += sizeof (".trace_") - 1;
6641
6642 if (do_debugging
6643 || (do_trace_info && streq (name, "info"))
6644 || (do_trace_abbrevs && streq (name, "abbrev"))
6645 || (do_trace_aranges && streq (name, "aranges"))
6646 )
6647 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6648 }
6649 else if ((do_debugging || do_debug_links)
6650 && (const_strneq (name, ".gnu_debuglink")
6651 || const_strneq (name, ".gnu_debugaltlink")))
6652 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6653 }
6654
6655 if (! do_sections)
6656 return TRUE;
6657
6658 if (filedata->is_separate && ! process_links)
6659 return TRUE;
6660
6661 if (filedata->is_separate)
6662 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
6663 else if (filedata->file_header.e_shnum > 1)
6664 printf (_("\nSection Headers:\n"));
6665 else
6666 printf (_("\nSection Header:\n"));
6667
6668 if (is_32bit_elf)
6669 {
6670 if (do_section_details)
6671 {
6672 printf (_(" [Nr] Name\n"));
6673 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
6674 }
6675 else
6676 printf
6677 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
6678 }
6679 else if (do_wide)
6680 {
6681 if (do_section_details)
6682 {
6683 printf (_(" [Nr] Name\n"));
6684 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
6685 }
6686 else
6687 printf
6688 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
6689 }
6690 else
6691 {
6692 if (do_section_details)
6693 {
6694 printf (_(" [Nr] Name\n"));
6695 printf (_(" Type Address Offset Link\n"));
6696 printf (_(" Size EntSize Info Align\n"));
6697 }
6698 else
6699 {
6700 printf (_(" [Nr] Name Type Address Offset\n"));
6701 printf (_(" Size EntSize Flags Link Info Align\n"));
6702 }
6703 }
6704
6705 if (do_section_details)
6706 printf (_(" Flags\n"));
6707
6708 for (i = 0, section = filedata->section_headers;
6709 i < filedata->file_header.e_shnum;
6710 i++, section++)
6711 {
6712 /* Run some sanity checks on the section header. */
6713
6714 /* Check the sh_link field. */
6715 switch (section->sh_type)
6716 {
6717 case SHT_REL:
6718 case SHT_RELA:
6719 if (section->sh_link == 0
6720 && (filedata->file_header.e_type == ET_EXEC
6721 || filedata->file_header.e_type == ET_DYN))
6722 /* A dynamic relocation section where all entries use a
6723 zero symbol index need not specify a symtab section. */
6724 break;
6725 /* Fall through. */
6726 case SHT_SYMTAB_SHNDX:
6727 case SHT_GROUP:
6728 case SHT_HASH:
6729 case SHT_GNU_HASH:
6730 case SHT_GNU_versym:
6731 if (section->sh_link == 0
6732 || section->sh_link >= filedata->file_header.e_shnum
6733 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6734 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6735 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6736 i, section->sh_link);
6737 break;
6738
6739 case SHT_DYNAMIC:
6740 case SHT_SYMTAB:
6741 case SHT_DYNSYM:
6742 case SHT_GNU_verneed:
6743 case SHT_GNU_verdef:
6744 case SHT_GNU_LIBLIST:
6745 if (section->sh_link == 0
6746 || section->sh_link >= filedata->file_header.e_shnum
6747 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6748 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6749 i, section->sh_link);
6750 break;
6751
6752 case SHT_INIT_ARRAY:
6753 case SHT_FINI_ARRAY:
6754 case SHT_PREINIT_ARRAY:
6755 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6756 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6757 i, section->sh_link);
6758 break;
6759
6760 default:
6761 /* FIXME: Add support for target specific section types. */
6762 #if 0 /* Currently we do not check other section types as there are too
6763 many special cases. Stab sections for example have a type
6764 of SHT_PROGBITS but an sh_link field that links to the .stabstr
6765 section. */
6766 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6767 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6768 i, section->sh_link);
6769 #endif
6770 break;
6771 }
6772
6773 /* Check the sh_info field. */
6774 switch (section->sh_type)
6775 {
6776 case SHT_REL:
6777 case SHT_RELA:
6778 if (section->sh_info == 0
6779 && (filedata->file_header.e_type == ET_EXEC
6780 || filedata->file_header.e_type == ET_DYN))
6781 /* Dynamic relocations apply to segments, so they do not
6782 need to specify the section they relocate. */
6783 break;
6784 if (section->sh_info == 0
6785 || section->sh_info >= filedata->file_header.e_shnum
6786 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
6787 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
6788 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
6789 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
6790 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
6791 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
6792 /* FIXME: Are other section types valid ? */
6793 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
6794 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
6795 i, section->sh_info);
6796 break;
6797
6798 case SHT_DYNAMIC:
6799 case SHT_HASH:
6800 case SHT_SYMTAB_SHNDX:
6801 case SHT_INIT_ARRAY:
6802 case SHT_FINI_ARRAY:
6803 case SHT_PREINIT_ARRAY:
6804 if (section->sh_info != 0)
6805 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6806 i, section->sh_info);
6807 break;
6808
6809 case SHT_GROUP:
6810 case SHT_SYMTAB:
6811 case SHT_DYNSYM:
6812 /* A symbol index - we assume that it is valid. */
6813 break;
6814
6815 default:
6816 /* FIXME: Add support for target specific section types. */
6817 if (section->sh_type == SHT_NOBITS)
6818 /* NOBITS section headers with non-zero sh_info fields can be
6819 created when a binary is stripped of everything but its debug
6820 information. The stripped sections have their headers
6821 preserved but their types set to SHT_NOBITS. So do not check
6822 this type of section. */
6823 ;
6824 else if (section->sh_flags & SHF_INFO_LINK)
6825 {
6826 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
6827 warn (_("[%2u]: Expected link to another section in info field"), i);
6828 }
6829 else if (section->sh_type < SHT_LOOS
6830 && (section->sh_flags & SHF_GNU_MBIND) == 0
6831 && section->sh_info != 0)
6832 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6833 i, section->sh_info);
6834 break;
6835 }
6836
6837 /* Check the sh_size field. */
6838 if (section->sh_size > filedata->file_size
6839 && section->sh_type != SHT_NOBITS
6840 && section->sh_type != SHT_NULL
6841 && section->sh_type < SHT_LOOS)
6842 warn (_("Size of section %u is larger than the entire file!\n"), i);
6843
6844 printf (" [%2u] ", i);
6845 if (do_section_details)
6846 printf ("%s\n ", printable_section_name (filedata, section));
6847 else
6848 print_symbol (-17, SECTION_NAME_PRINT (section));
6849
6850 printf (do_wide ? " %-15s " : " %-15.15s ",
6851 get_section_type_name (filedata, section->sh_type));
6852
6853 if (is_32bit_elf)
6854 {
6855 const char * link_too_big = NULL;
6856
6857 print_vma (section->sh_addr, LONG_HEX);
6858
6859 printf ( " %6.6lx %6.6lx %2.2lx",
6860 (unsigned long) section->sh_offset,
6861 (unsigned long) section->sh_size,
6862 (unsigned long) section->sh_entsize);
6863
6864 if (do_section_details)
6865 fputs (" ", stdout);
6866 else
6867 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6868
6869 if (section->sh_link >= filedata->file_header.e_shnum)
6870 {
6871 link_too_big = "";
6872 /* The sh_link value is out of range. Normally this indicates
6873 an error but it can have special values in Solaris binaries. */
6874 switch (filedata->file_header.e_machine)
6875 {
6876 case EM_386:
6877 case EM_IAMCU:
6878 case EM_X86_64:
6879 case EM_L1OM:
6880 case EM_K1OM:
6881 case EM_OLD_SPARCV9:
6882 case EM_SPARC32PLUS:
6883 case EM_SPARCV9:
6884 case EM_SPARC:
6885 if (section->sh_link == (SHN_BEFORE & 0xffff))
6886 link_too_big = "BEFORE";
6887 else if (section->sh_link == (SHN_AFTER & 0xffff))
6888 link_too_big = "AFTER";
6889 break;
6890 default:
6891 break;
6892 }
6893 }
6894
6895 if (do_section_details)
6896 {
6897 if (link_too_big != NULL && * link_too_big)
6898 printf ("<%s> ", link_too_big);
6899 else
6900 printf ("%2u ", section->sh_link);
6901 printf ("%3u %2lu\n", section->sh_info,
6902 (unsigned long) section->sh_addralign);
6903 }
6904 else
6905 printf ("%2u %3u %2lu\n",
6906 section->sh_link,
6907 section->sh_info,
6908 (unsigned long) section->sh_addralign);
6909
6910 if (link_too_big && ! * link_too_big)
6911 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
6912 i, section->sh_link);
6913 }
6914 else if (do_wide)
6915 {
6916 print_vma (section->sh_addr, LONG_HEX);
6917
6918 if ((long) section->sh_offset == section->sh_offset)
6919 printf (" %6.6lx", (unsigned long) section->sh_offset);
6920 else
6921 {
6922 putchar (' ');
6923 print_vma (section->sh_offset, LONG_HEX);
6924 }
6925
6926 if ((unsigned long) section->sh_size == section->sh_size)
6927 printf (" %6.6lx", (unsigned long) section->sh_size);
6928 else
6929 {
6930 putchar (' ');
6931 print_vma (section->sh_size, LONG_HEX);
6932 }
6933
6934 if ((unsigned long) section->sh_entsize == section->sh_entsize)
6935 printf (" %2.2lx", (unsigned long) section->sh_entsize);
6936 else
6937 {
6938 putchar (' ');
6939 print_vma (section->sh_entsize, LONG_HEX);
6940 }
6941
6942 if (do_section_details)
6943 fputs (" ", stdout);
6944 else
6945 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6946
6947 printf ("%2u %3u ", section->sh_link, section->sh_info);
6948
6949 if ((unsigned long) section->sh_addralign == section->sh_addralign)
6950 printf ("%2lu\n", (unsigned long) section->sh_addralign);
6951 else
6952 {
6953 print_vma (section->sh_addralign, DEC);
6954 putchar ('\n');
6955 }
6956 }
6957 else if (do_section_details)
6958 {
6959 putchar (' ');
6960 print_vma (section->sh_addr, LONG_HEX);
6961 if ((long) section->sh_offset == section->sh_offset)
6962 printf (" %16.16lx", (unsigned long) section->sh_offset);
6963 else
6964 {
6965 printf (" ");
6966 print_vma (section->sh_offset, LONG_HEX);
6967 }
6968 printf (" %u\n ", section->sh_link);
6969 print_vma (section->sh_size, LONG_HEX);
6970 putchar (' ');
6971 print_vma (section->sh_entsize, LONG_HEX);
6972
6973 printf (" %-16u %lu\n",
6974 section->sh_info,
6975 (unsigned long) section->sh_addralign);
6976 }
6977 else
6978 {
6979 putchar (' ');
6980 print_vma (section->sh_addr, LONG_HEX);
6981 if ((long) section->sh_offset == section->sh_offset)
6982 printf (" %8.8lx", (unsigned long) section->sh_offset);
6983 else
6984 {
6985 printf (" ");
6986 print_vma (section->sh_offset, LONG_HEX);
6987 }
6988 printf ("\n ");
6989 print_vma (section->sh_size, LONG_HEX);
6990 printf (" ");
6991 print_vma (section->sh_entsize, LONG_HEX);
6992
6993 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6994
6995 printf (" %2u %3u %lu\n",
6996 section->sh_link,
6997 section->sh_info,
6998 (unsigned long) section->sh_addralign);
6999 }
7000
7001 if (do_section_details)
7002 {
7003 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7004 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7005 {
7006 /* Minimum section size is 12 bytes for 32-bit compression
7007 header + 12 bytes for compressed data header. */
7008 unsigned char buf[24];
7009
7010 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7011 if (get_data (&buf, filedata, section->sh_offset, 1,
7012 sizeof (buf), _("compression header")))
7013 {
7014 Elf_Internal_Chdr chdr;
7015
7016 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7017 printf (_(" [<corrupt>]\n"));
7018 else
7019 {
7020 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7021 printf (" ZLIB, ");
7022 else
7023 printf (_(" [<unknown>: 0x%x], "),
7024 chdr.ch_type);
7025 print_vma (chdr.ch_size, LONG_HEX);
7026 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7027 }
7028 }
7029 }
7030 }
7031 }
7032
7033 if (!do_section_details)
7034 {
7035 /* The ordering of the letters shown here matches the ordering of the
7036 corresponding SHF_xxx values, and hence the order in which these
7037 letters will be displayed to the user. */
7038 printf (_("Key to Flags:\n\
7039 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7040 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7041 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7042 switch (filedata->file_header.e_ident[EI_OSABI])
7043 {
7044 case ELFOSABI_GNU:
7045 case ELFOSABI_FREEBSD:
7046 printf (_("R (retain), "));
7047 /* Fall through */
7048 case ELFOSABI_NONE:
7049 printf (_("D (mbind), "));
7050 break;
7051 default:
7052 break;
7053 }
7054 if (filedata->file_header.e_machine == EM_X86_64
7055 || filedata->file_header.e_machine == EM_L1OM
7056 || filedata->file_header.e_machine == EM_K1OM)
7057 printf (_("l (large), "));
7058 else if (filedata->file_header.e_machine == EM_ARM)
7059 printf (_("y (purecode), "));
7060 else if (filedata->file_header.e_machine == EM_PPC)
7061 printf (_("v (VLE), "));
7062 printf ("p (processor specific)\n");
7063 }
7064
7065 return TRUE;
7066 }
7067
7068 static bfd_boolean
7069 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7070 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7071 char **strtab, unsigned long *strtablen)
7072 {
7073 *strtab = NULL;
7074 *strtablen = 0;
7075 *symtab = GET_ELF_SYMBOLS (filedata, symsec, nsyms);
7076
7077 if (*symtab == NULL)
7078 return FALSE;
7079
7080 if (symsec->sh_link != 0)
7081 {
7082 Elf_Internal_Shdr *strsec;
7083
7084 if (symsec->sh_link >= filedata->file_header.e_shnum)
7085 {
7086 error (_("Bad sh_link in symbol table section\n"));
7087 free (*symtab);
7088 *symtab = NULL;
7089 *nsyms = 0;
7090 return FALSE;
7091 }
7092
7093 strsec = filedata->section_headers + symsec->sh_link;
7094
7095 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7096 1, strsec->sh_size, _("string table"));
7097 if (*strtab == NULL)
7098 {
7099 free (*symtab);
7100 *symtab = NULL;
7101 *nsyms = 0;
7102 return FALSE;
7103 }
7104 *strtablen = strsec->sh_size;
7105 }
7106 return TRUE;
7107 }
7108
7109 static const char *
7110 get_group_flags (unsigned int flags)
7111 {
7112 static char buff[128];
7113
7114 if (flags == 0)
7115 return "";
7116 else if (flags == GRP_COMDAT)
7117 return "COMDAT ";
7118
7119 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7120 flags,
7121 flags & GRP_MASKOS ? _("<OS specific>") : "",
7122 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7123 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7124 ? _("<unknown>") : ""));
7125
7126 return buff;
7127 }
7128
7129 static bfd_boolean
7130 process_section_groups (Filedata * filedata)
7131 {
7132 Elf_Internal_Shdr * section;
7133 unsigned int i;
7134 struct group * group;
7135 Elf_Internal_Shdr * symtab_sec;
7136 Elf_Internal_Shdr * strtab_sec;
7137 Elf_Internal_Sym * symtab;
7138 unsigned long num_syms;
7139 char * strtab;
7140 size_t strtab_size;
7141
7142 /* Don't process section groups unless needed. */
7143 if (!do_unwind && !do_section_groups)
7144 return TRUE;
7145
7146 if (filedata->file_header.e_shnum == 0)
7147 {
7148 if (do_section_groups)
7149 {
7150 if (filedata->is_separate)
7151 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7152 filedata->file_name);
7153 else
7154 printf (_("\nThere are no section groups in this file.\n"));
7155 }
7156 return TRUE;
7157 }
7158
7159 if (filedata->section_headers == NULL)
7160 {
7161 error (_("Section headers are not available!\n"));
7162 /* PR 13622: This can happen with a corrupt ELF header. */
7163 return FALSE;
7164 }
7165
7166 filedata->section_headers_groups
7167 = (struct group **) calloc (filedata->file_header.e_shnum,
7168 sizeof (struct group *));
7169
7170 if (filedata->section_headers_groups == NULL)
7171 {
7172 error (_("Out of memory reading %u section group headers\n"),
7173 filedata->file_header.e_shnum);
7174 return FALSE;
7175 }
7176
7177 /* Scan the sections for the group section. */
7178 filedata->group_count = 0;
7179 for (i = 0, section = filedata->section_headers;
7180 i < filedata->file_header.e_shnum;
7181 i++, section++)
7182 if (section->sh_type == SHT_GROUP)
7183 filedata->group_count++;
7184
7185 if (filedata->group_count == 0)
7186 {
7187 if (do_section_groups)
7188 {
7189 if (filedata->is_separate)
7190 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7191 filedata->file_name);
7192 else
7193 printf (_("\nThere are no section groups in this file.\n"));
7194 }
7195
7196 return TRUE;
7197 }
7198
7199 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7200 sizeof (struct group));
7201
7202 if (filedata->section_groups == NULL)
7203 {
7204 error (_("Out of memory reading %lu groups\n"),
7205 (unsigned long) filedata->group_count);
7206 return FALSE;
7207 }
7208
7209 symtab_sec = NULL;
7210 strtab_sec = NULL;
7211 symtab = NULL;
7212 num_syms = 0;
7213 strtab = NULL;
7214 strtab_size = 0;
7215
7216 if (filedata->is_separate)
7217 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7218
7219 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7220 i < filedata->file_header.e_shnum;
7221 i++, section++)
7222 {
7223 if (section->sh_type == SHT_GROUP)
7224 {
7225 const char * name = printable_section_name (filedata, section);
7226 const char * group_name;
7227 unsigned char * start;
7228 unsigned char * indices;
7229 unsigned int entry, j, size;
7230 Elf_Internal_Shdr * sec;
7231 Elf_Internal_Sym * sym;
7232
7233 /* Get the symbol table. */
7234 if (section->sh_link >= filedata->file_header.e_shnum
7235 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7236 != SHT_SYMTAB))
7237 {
7238 error (_("Bad sh_link in group section `%s'\n"), name);
7239 continue;
7240 }
7241
7242 if (symtab_sec != sec)
7243 {
7244 symtab_sec = sec;
7245 free (symtab);
7246 symtab = GET_ELF_SYMBOLS (filedata, symtab_sec, & num_syms);
7247 }
7248
7249 if (symtab == NULL)
7250 {
7251 error (_("Corrupt header in group section `%s'\n"), name);
7252 continue;
7253 }
7254
7255 if (section->sh_info >= num_syms)
7256 {
7257 error (_("Bad sh_info in group section `%s'\n"), name);
7258 continue;
7259 }
7260
7261 sym = symtab + section->sh_info;
7262
7263 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7264 {
7265 if (sym->st_shndx == 0
7266 || sym->st_shndx >= filedata->file_header.e_shnum)
7267 {
7268 error (_("Bad sh_info in group section `%s'\n"), name);
7269 continue;
7270 }
7271
7272 group_name = SECTION_NAME_PRINT (filedata->section_headers
7273 + sym->st_shndx);
7274 strtab_sec = NULL;
7275 free (strtab);
7276 strtab = NULL;
7277 strtab_size = 0;
7278 }
7279 else
7280 {
7281 /* Get the string table. */
7282 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7283 {
7284 strtab_sec = NULL;
7285 free (strtab);
7286 strtab = NULL;
7287 strtab_size = 0;
7288 }
7289 else if (strtab_sec
7290 != (sec = filedata->section_headers + symtab_sec->sh_link))
7291 {
7292 strtab_sec = sec;
7293 free (strtab);
7294
7295 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
7296 1, strtab_sec->sh_size,
7297 _("string table"));
7298 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
7299 }
7300 group_name = sym->st_name < strtab_size
7301 ? strtab + sym->st_name : _("<corrupt>");
7302 }
7303
7304 /* PR 17531: file: loop. */
7305 if (section->sh_entsize > section->sh_size)
7306 {
7307 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
7308 printable_section_name (filedata, section),
7309 (unsigned long) section->sh_entsize,
7310 (unsigned long) section->sh_size);
7311 continue;
7312 }
7313
7314 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
7315 1, section->sh_size,
7316 _("section data"));
7317 if (start == NULL)
7318 continue;
7319
7320 indices = start;
7321 size = (section->sh_size / section->sh_entsize) - 1;
7322 entry = byte_get (indices, 4);
7323 indices += 4;
7324
7325 if (do_section_groups)
7326 {
7327 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
7328 get_group_flags (entry), i, name, group_name, size);
7329
7330 printf (_(" [Index] Name\n"));
7331 }
7332
7333 group->group_index = i;
7334
7335 for (j = 0; j < size; j++)
7336 {
7337 struct group_list * g;
7338
7339 entry = byte_get (indices, 4);
7340 indices += 4;
7341
7342 if (entry >= filedata->file_header.e_shnum)
7343 {
7344 static unsigned num_group_errors = 0;
7345
7346 if (num_group_errors ++ < 10)
7347 {
7348 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
7349 entry, i, filedata->file_header.e_shnum - 1);
7350 if (num_group_errors == 10)
7351 warn (_("Further error messages about overlarge group section indices suppressed\n"));
7352 }
7353 continue;
7354 }
7355
7356 if (filedata->section_headers_groups [entry] != NULL)
7357 {
7358 if (entry)
7359 {
7360 static unsigned num_errs = 0;
7361
7362 if (num_errs ++ < 10)
7363 {
7364 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7365 entry, i,
7366 filedata->section_headers_groups [entry]->group_index);
7367 if (num_errs == 10)
7368 warn (_("Further error messages about already contained group sections suppressed\n"));
7369 }
7370 continue;
7371 }
7372 else
7373 {
7374 /* Intel C/C++ compiler may put section 0 in a
7375 section group. We just warn it the first time
7376 and ignore it afterwards. */
7377 static bfd_boolean warned = FALSE;
7378 if (!warned)
7379 {
7380 error (_("section 0 in group section [%5u]\n"),
7381 filedata->section_headers_groups [entry]->group_index);
7382 warned = TRUE;
7383 }
7384 }
7385 }
7386
7387 filedata->section_headers_groups [entry] = group;
7388
7389 if (do_section_groups)
7390 {
7391 sec = filedata->section_headers + entry;
7392 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
7393 }
7394
7395 g = (struct group_list *) xmalloc (sizeof (struct group_list));
7396 g->section_index = entry;
7397 g->next = group->root;
7398 group->root = g;
7399 }
7400
7401 free (start);
7402
7403 group++;
7404 }
7405 }
7406
7407 free (symtab);
7408 free (strtab);
7409 return TRUE;
7410 }
7411
7412 /* Data used to display dynamic fixups. */
7413
7414 struct ia64_vms_dynfixup
7415 {
7416 bfd_vma needed_ident; /* Library ident number. */
7417 bfd_vma needed; /* Index in the dstrtab of the library name. */
7418 bfd_vma fixup_needed; /* Index of the library. */
7419 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7420 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7421 };
7422
7423 /* Data used to display dynamic relocations. */
7424
7425 struct ia64_vms_dynimgrela
7426 {
7427 bfd_vma img_rela_cnt; /* Number of relocations. */
7428 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7429 };
7430
7431 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7432 library). */
7433
7434 static bfd_boolean
7435 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7436 struct ia64_vms_dynfixup * fixup,
7437 const char * strtab,
7438 unsigned int strtab_sz)
7439 {
7440 Elf64_External_VMS_IMAGE_FIXUP * imfs;
7441 long i;
7442 const char * lib_name;
7443
7444 imfs = get_data (NULL, filedata,
7445 filedata->dynamic_addr + fixup->fixup_rela_off,
7446 sizeof (*imfs), fixup->fixup_rela_cnt,
7447 _("dynamic section image fixups"));
7448 if (!imfs)
7449 return FALSE;
7450
7451 if (fixup->needed < strtab_sz)
7452 lib_name = strtab + fixup->needed;
7453 else
7454 {
7455 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7456 (unsigned long) fixup->needed);
7457 lib_name = "???";
7458 }
7459
7460 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7461 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7462 printf
7463 (_("Seg Offset Type SymVec DataType\n"));
7464
7465 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7466 {
7467 unsigned int type;
7468 const char *rtype;
7469
7470 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7471 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7472 type = BYTE_GET (imfs [i].type);
7473 rtype = elf_ia64_reloc_type (type);
7474 if (rtype == NULL)
7475 printf (" 0x%08x ", type);
7476 else
7477 printf (" %-32s ", rtype);
7478 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7479 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7480 }
7481
7482 free (imfs);
7483 return TRUE;
7484 }
7485
7486 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7487
7488 static bfd_boolean
7489 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
7490 {
7491 Elf64_External_VMS_IMAGE_RELA *imrs;
7492 long i;
7493
7494 imrs = get_data (NULL, filedata,
7495 filedata->dynamic_addr + imgrela->img_rela_off,
7496 sizeof (*imrs), imgrela->img_rela_cnt,
7497 _("dynamic section image relocations"));
7498 if (!imrs)
7499 return FALSE;
7500
7501 printf (_("\nImage relocs\n"));
7502 printf
7503 (_("Seg Offset Type Addend Seg Sym Off\n"));
7504
7505 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7506 {
7507 unsigned int type;
7508 const char *rtype;
7509
7510 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7511 printf ("%08" BFD_VMA_FMT "x ",
7512 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7513 type = BYTE_GET (imrs [i].type);
7514 rtype = elf_ia64_reloc_type (type);
7515 if (rtype == NULL)
7516 printf ("0x%08x ", type);
7517 else
7518 printf ("%-31s ", rtype);
7519 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7520 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7521 printf ("%08" BFD_VMA_FMT "x\n",
7522 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7523 }
7524
7525 free (imrs);
7526 return TRUE;
7527 }
7528
7529 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
7530
7531 static bfd_boolean
7532 process_ia64_vms_dynamic_relocs (Filedata * filedata)
7533 {
7534 struct ia64_vms_dynfixup fixup;
7535 struct ia64_vms_dynimgrela imgrela;
7536 Elf_Internal_Dyn *entry;
7537 bfd_vma strtab_off = 0;
7538 bfd_vma strtab_sz = 0;
7539 char *strtab = NULL;
7540 bfd_boolean res = TRUE;
7541
7542 memset (&fixup, 0, sizeof (fixup));
7543 memset (&imgrela, 0, sizeof (imgrela));
7544
7545 /* Note: the order of the entries is specified by the OpenVMS specs. */
7546 for (entry = filedata->dynamic_section;
7547 entry < filedata->dynamic_section + filedata->dynamic_nent;
7548 entry++)
7549 {
7550 switch (entry->d_tag)
7551 {
7552 case DT_IA_64_VMS_STRTAB_OFFSET:
7553 strtab_off = entry->d_un.d_val;
7554 break;
7555 case DT_STRSZ:
7556 strtab_sz = entry->d_un.d_val;
7557 if (strtab == NULL)
7558 strtab = get_data (NULL, filedata,
7559 filedata->dynamic_addr + strtab_off,
7560 1, strtab_sz, _("dynamic string section"));
7561 if (strtab == NULL)
7562 strtab_sz = 0;
7563 break;
7564
7565 case DT_IA_64_VMS_NEEDED_IDENT:
7566 fixup.needed_ident = entry->d_un.d_val;
7567 break;
7568 case DT_NEEDED:
7569 fixup.needed = entry->d_un.d_val;
7570 break;
7571 case DT_IA_64_VMS_FIXUP_NEEDED:
7572 fixup.fixup_needed = entry->d_un.d_val;
7573 break;
7574 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7575 fixup.fixup_rela_cnt = entry->d_un.d_val;
7576 break;
7577 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7578 fixup.fixup_rela_off = entry->d_un.d_val;
7579 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7580 res = FALSE;
7581 break;
7582 case DT_IA_64_VMS_IMG_RELA_CNT:
7583 imgrela.img_rela_cnt = entry->d_un.d_val;
7584 break;
7585 case DT_IA_64_VMS_IMG_RELA_OFF:
7586 imgrela.img_rela_off = entry->d_un.d_val;
7587 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7588 res = FALSE;
7589 break;
7590
7591 default:
7592 break;
7593 }
7594 }
7595
7596 free (strtab);
7597
7598 return res;
7599 }
7600
7601 static struct
7602 {
7603 const char * name;
7604 int reloc;
7605 int size;
7606 int rela;
7607 }
7608 dynamic_relocations [] =
7609 {
7610 { "REL", DT_REL, DT_RELSZ, FALSE },
7611 { "RELA", DT_RELA, DT_RELASZ, TRUE },
7612 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7613 };
7614
7615 /* Process the reloc section. */
7616
7617 static bfd_boolean
7618 process_relocs (Filedata * filedata)
7619 {
7620 unsigned long rel_size;
7621 unsigned long rel_offset;
7622
7623 if (!do_reloc)
7624 return TRUE;
7625
7626 if (do_using_dynamic)
7627 {
7628 int is_rela;
7629 const char * name;
7630 bfd_boolean has_dynamic_reloc;
7631 unsigned int i;
7632
7633 has_dynamic_reloc = FALSE;
7634
7635 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7636 {
7637 is_rela = dynamic_relocations [i].rela;
7638 name = dynamic_relocations [i].name;
7639 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
7640 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
7641
7642 if (rel_size)
7643 has_dynamic_reloc = TRUE;
7644
7645 if (is_rela == UNKNOWN)
7646 {
7647 if (dynamic_relocations [i].reloc == DT_JMPREL)
7648 switch (filedata->dynamic_info[DT_PLTREL])
7649 {
7650 case DT_REL:
7651 is_rela = FALSE;
7652 break;
7653 case DT_RELA:
7654 is_rela = TRUE;
7655 break;
7656 }
7657 }
7658
7659 if (rel_size)
7660 {
7661 if (filedata->is_separate)
7662 printf
7663 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
7664 filedata->file_name, name, rel_offset, rel_size);
7665 else
7666 printf
7667 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7668 name, rel_offset, rel_size);
7669
7670
7671 dump_relocations (filedata,
7672 offset_from_vma (filedata, rel_offset, rel_size),
7673 rel_size,
7674 filedata->dynamic_symbols,
7675 filedata->num_dynamic_syms,
7676 filedata->dynamic_strings,
7677 filedata->dynamic_strings_length,
7678 is_rela, TRUE /* is_dynamic */);
7679 }
7680 }
7681
7682 if (is_ia64_vms (filedata))
7683 if (process_ia64_vms_dynamic_relocs (filedata))
7684 has_dynamic_reloc = TRUE;
7685
7686 if (! has_dynamic_reloc)
7687 {
7688 if (filedata->is_separate)
7689 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
7690 filedata->file_name);
7691 else
7692 printf (_("\nThere are no dynamic relocations in this file.\n"));
7693 }
7694 }
7695 else
7696 {
7697 Elf_Internal_Shdr * section;
7698 unsigned long i;
7699 bfd_boolean found = FALSE;
7700
7701 for (i = 0, section = filedata->section_headers;
7702 i < filedata->file_header.e_shnum;
7703 i++, section++)
7704 {
7705 if ( section->sh_type != SHT_RELA
7706 && section->sh_type != SHT_REL)
7707 continue;
7708
7709 rel_offset = section->sh_offset;
7710 rel_size = section->sh_size;
7711
7712 if (rel_size)
7713 {
7714 int is_rela;
7715 unsigned long num_rela;
7716
7717 if (filedata->is_separate)
7718 printf (_("\nIn linked file '%s' relocation section "),
7719 filedata->file_name);
7720 else
7721 printf (_("\nRelocation section "));
7722
7723 if (filedata->string_table == NULL)
7724 printf ("%d", section->sh_name);
7725 else
7726 printf ("'%s'", printable_section_name (filedata, section));
7727
7728 num_rela = rel_size / section->sh_entsize;
7729 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7730 " at offset 0x%lx contains %lu entries:\n",
7731 num_rela),
7732 rel_offset, num_rela);
7733
7734 is_rela = section->sh_type == SHT_RELA;
7735
7736 if (section->sh_link != 0
7737 && section->sh_link < filedata->file_header.e_shnum)
7738 {
7739 Elf_Internal_Shdr * symsec;
7740 Elf_Internal_Sym * symtab;
7741 unsigned long nsyms;
7742 unsigned long strtablen = 0;
7743 char * strtab = NULL;
7744
7745 symsec = filedata->section_headers + section->sh_link;
7746 if (symsec->sh_type != SHT_SYMTAB
7747 && symsec->sh_type != SHT_DYNSYM)
7748 continue;
7749
7750 if (!get_symtab (filedata, symsec,
7751 &symtab, &nsyms, &strtab, &strtablen))
7752 continue;
7753
7754 dump_relocations (filedata, rel_offset, rel_size,
7755 symtab, nsyms, strtab, strtablen,
7756 is_rela,
7757 symsec->sh_type == SHT_DYNSYM);
7758 free (strtab);
7759 free (symtab);
7760 }
7761 else
7762 dump_relocations (filedata, rel_offset, rel_size,
7763 NULL, 0, NULL, 0, is_rela,
7764 FALSE /* is_dynamic */);
7765
7766 found = TRUE;
7767 }
7768 }
7769
7770 if (! found)
7771 {
7772 /* Users sometimes forget the -D option, so try to be helpful. */
7773 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7774 {
7775 if (filedata->dynamic_info[dynamic_relocations [i].size])
7776 {
7777 if (filedata->is_separate)
7778 printf (_("\nThere are no static relocations in linked file '%s'."),
7779 filedata->file_name);
7780 else
7781 printf (_("\nThere are no static relocations in this file."));
7782 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
7783
7784 break;
7785 }
7786 }
7787 if (i == ARRAY_SIZE (dynamic_relocations))
7788 {
7789 if (filedata->is_separate)
7790 printf (_("\nThere are no relocations in linked file '%s'.\n"),
7791 filedata->file_name);
7792 else
7793 printf (_("\nThere are no relocations in this file.\n"));
7794 }
7795 }
7796 }
7797
7798 return TRUE;
7799 }
7800
7801 /* An absolute address consists of a section and an offset. If the
7802 section is NULL, the offset itself is the address, otherwise, the
7803 address equals to LOAD_ADDRESS(section) + offset. */
7804
7805 struct absaddr
7806 {
7807 unsigned short section;
7808 bfd_vma offset;
7809 };
7810
7811 /* Find the nearest symbol at or below ADDR. Returns the symbol
7812 name, if found, and the offset from the symbol to ADDR. */
7813
7814 static void
7815 find_symbol_for_address (Filedata * filedata,
7816 Elf_Internal_Sym * symtab,
7817 unsigned long nsyms,
7818 const char * strtab,
7819 unsigned long strtab_size,
7820 struct absaddr addr,
7821 const char ** symname,
7822 bfd_vma * offset)
7823 {
7824 bfd_vma dist = 0x100000;
7825 Elf_Internal_Sym * sym;
7826 Elf_Internal_Sym * beg;
7827 Elf_Internal_Sym * end;
7828 Elf_Internal_Sym * best = NULL;
7829
7830 REMOVE_ARCH_BITS (addr.offset);
7831 beg = symtab;
7832 end = symtab + nsyms;
7833
7834 while (beg < end)
7835 {
7836 bfd_vma value;
7837
7838 sym = beg + (end - beg) / 2;
7839
7840 value = sym->st_value;
7841 REMOVE_ARCH_BITS (value);
7842
7843 if (sym->st_name != 0
7844 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
7845 && addr.offset >= value
7846 && addr.offset - value < dist)
7847 {
7848 best = sym;
7849 dist = addr.offset - value;
7850 if (!dist)
7851 break;
7852 }
7853
7854 if (addr.offset < value)
7855 end = sym;
7856 else
7857 beg = sym + 1;
7858 }
7859
7860 if (best)
7861 {
7862 *symname = (best->st_name >= strtab_size
7863 ? _("<corrupt>") : strtab + best->st_name);
7864 *offset = dist;
7865 return;
7866 }
7867
7868 *symname = NULL;
7869 *offset = addr.offset;
7870 }
7871
7872 static /* signed */ int
7873 symcmp (const void *p, const void *q)
7874 {
7875 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
7876 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
7877
7878 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
7879 }
7880
7881 /* Process the unwind section. */
7882
7883 #include "unwind-ia64.h"
7884
7885 struct ia64_unw_table_entry
7886 {
7887 struct absaddr start;
7888 struct absaddr end;
7889 struct absaddr info;
7890 };
7891
7892 struct ia64_unw_aux_info
7893 {
7894 struct ia64_unw_table_entry * table; /* Unwind table. */
7895 unsigned long table_len; /* Length of unwind table. */
7896 unsigned char * info; /* Unwind info. */
7897 unsigned long info_size; /* Size of unwind info. */
7898 bfd_vma info_addr; /* Starting address of unwind info. */
7899 bfd_vma seg_base; /* Starting address of segment. */
7900 Elf_Internal_Sym * symtab; /* The symbol table. */
7901 unsigned long nsyms; /* Number of symbols. */
7902 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
7903 unsigned long nfuns; /* Number of entries in funtab. */
7904 char * strtab; /* The string table. */
7905 unsigned long strtab_size; /* Size of string table. */
7906 };
7907
7908 static bfd_boolean
7909 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
7910 {
7911 struct ia64_unw_table_entry * tp;
7912 unsigned long j, nfuns;
7913 int in_body;
7914 bfd_boolean res = TRUE;
7915
7916 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7917 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7918 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7919 aux->funtab[nfuns++] = aux->symtab[j];
7920 aux->nfuns = nfuns;
7921 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7922
7923 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7924 {
7925 bfd_vma stamp;
7926 bfd_vma offset;
7927 const unsigned char * dp;
7928 const unsigned char * head;
7929 const unsigned char * end;
7930 const char * procname;
7931
7932 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
7933 aux->strtab_size, tp->start, &procname, &offset);
7934
7935 fputs ("\n<", stdout);
7936
7937 if (procname)
7938 {
7939 fputs (procname, stdout);
7940
7941 if (offset)
7942 printf ("+%lx", (unsigned long) offset);
7943 }
7944
7945 fputs (">: [", stdout);
7946 print_vma (tp->start.offset, PREFIX_HEX);
7947 fputc ('-', stdout);
7948 print_vma (tp->end.offset, PREFIX_HEX);
7949 printf ("], info at +0x%lx\n",
7950 (unsigned long) (tp->info.offset - aux->seg_base));
7951
7952 /* PR 17531: file: 86232b32. */
7953 if (aux->info == NULL)
7954 continue;
7955
7956 offset = tp->info.offset;
7957 if (tp->info.section)
7958 {
7959 if (tp->info.section >= filedata->file_header.e_shnum)
7960 {
7961 warn (_("Invalid section %u in table entry %ld\n"),
7962 tp->info.section, (long) (tp - aux->table));
7963 res = FALSE;
7964 continue;
7965 }
7966 offset += filedata->section_headers[tp->info.section].sh_addr;
7967 }
7968 offset -= aux->info_addr;
7969 /* PR 17531: file: 0997b4d1. */
7970 if (offset >= aux->info_size
7971 || aux->info_size - offset < 8)
7972 {
7973 warn (_("Invalid offset %lx in table entry %ld\n"),
7974 (long) tp->info.offset, (long) (tp - aux->table));
7975 res = FALSE;
7976 continue;
7977 }
7978
7979 head = aux->info + offset;
7980 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
7981
7982 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
7983 (unsigned) UNW_VER (stamp),
7984 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
7985 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
7986 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
7987 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
7988
7989 if (UNW_VER (stamp) != 1)
7990 {
7991 printf (_("\tUnknown version.\n"));
7992 continue;
7993 }
7994
7995 in_body = 0;
7996 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
7997 /* PR 17531: file: 16ceda89. */
7998 if (end > aux->info + aux->info_size)
7999 end = aux->info + aux->info_size;
8000 for (dp = head + 8; dp < end;)
8001 dp = unw_decode (dp, in_body, & in_body, end);
8002 }
8003
8004 free (aux->funtab);
8005
8006 return res;
8007 }
8008
8009 static bfd_boolean
8010 slurp_ia64_unwind_table (Filedata * filedata,
8011 struct ia64_unw_aux_info * aux,
8012 Elf_Internal_Shdr * sec)
8013 {
8014 unsigned long size, nrelas, i;
8015 Elf_Internal_Phdr * seg;
8016 struct ia64_unw_table_entry * tep;
8017 Elf_Internal_Shdr * relsec;
8018 Elf_Internal_Rela * rela;
8019 Elf_Internal_Rela * rp;
8020 unsigned char * table;
8021 unsigned char * tp;
8022 Elf_Internal_Sym * sym;
8023 const char * relname;
8024
8025 aux->table_len = 0;
8026
8027 /* First, find the starting address of the segment that includes
8028 this section: */
8029
8030 if (filedata->file_header.e_phnum)
8031 {
8032 if (! get_program_headers (filedata))
8033 return FALSE;
8034
8035 for (seg = filedata->program_headers;
8036 seg < filedata->program_headers + filedata->file_header.e_phnum;
8037 ++seg)
8038 {
8039 if (seg->p_type != PT_LOAD)
8040 continue;
8041
8042 if (sec->sh_addr >= seg->p_vaddr
8043 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8044 {
8045 aux->seg_base = seg->p_vaddr;
8046 break;
8047 }
8048 }
8049 }
8050
8051 /* Second, build the unwind table from the contents of the unwind section: */
8052 size = sec->sh_size;
8053 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8054 _("unwind table"));
8055 if (!table)
8056 return FALSE;
8057
8058 aux->table_len = size / (3 * eh_addr_size);
8059 aux->table = (struct ia64_unw_table_entry *)
8060 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8061 tep = aux->table;
8062
8063 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8064 {
8065 tep->start.section = SHN_UNDEF;
8066 tep->end.section = SHN_UNDEF;
8067 tep->info.section = SHN_UNDEF;
8068 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8069 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8070 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8071 tep->start.offset += aux->seg_base;
8072 tep->end.offset += aux->seg_base;
8073 tep->info.offset += aux->seg_base;
8074 }
8075 free (table);
8076
8077 /* Third, apply any relocations to the unwind table: */
8078 for (relsec = filedata->section_headers;
8079 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8080 ++relsec)
8081 {
8082 if (relsec->sh_type != SHT_RELA
8083 || relsec->sh_info >= filedata->file_header.e_shnum
8084 || filedata->section_headers + relsec->sh_info != sec)
8085 continue;
8086
8087 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8088 & rela, & nrelas))
8089 {
8090 free (aux->table);
8091 aux->table = NULL;
8092 aux->table_len = 0;
8093 return FALSE;
8094 }
8095
8096 for (rp = rela; rp < rela + nrelas; ++rp)
8097 {
8098 unsigned int sym_ndx;
8099 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8100 relname = elf_ia64_reloc_type (r_type);
8101
8102 /* PR 17531: file: 9fa67536. */
8103 if (relname == NULL)
8104 {
8105 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8106 continue;
8107 }
8108
8109 if (! const_strneq (relname, "R_IA64_SEGREL"))
8110 {
8111 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8112 continue;
8113 }
8114
8115 i = rp->r_offset / (3 * eh_addr_size);
8116
8117 /* PR 17531: file: 5bc8d9bf. */
8118 if (i >= aux->table_len)
8119 {
8120 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8121 continue;
8122 }
8123
8124 sym_ndx = get_reloc_symindex (rp->r_info);
8125 if (sym_ndx >= aux->nsyms)
8126 {
8127 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8128 sym_ndx);
8129 continue;
8130 }
8131 sym = aux->symtab + sym_ndx;
8132
8133 switch (rp->r_offset / eh_addr_size % 3)
8134 {
8135 case 0:
8136 aux->table[i].start.section = sym->st_shndx;
8137 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8138 break;
8139 case 1:
8140 aux->table[i].end.section = sym->st_shndx;
8141 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8142 break;
8143 case 2:
8144 aux->table[i].info.section = sym->st_shndx;
8145 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8146 break;
8147 default:
8148 break;
8149 }
8150 }
8151
8152 free (rela);
8153 }
8154
8155 return TRUE;
8156 }
8157
8158 static bfd_boolean
8159 ia64_process_unwind (Filedata * filedata)
8160 {
8161 Elf_Internal_Shdr * sec;
8162 Elf_Internal_Shdr * unwsec = NULL;
8163 unsigned long i, unwcount = 0, unwstart = 0;
8164 struct ia64_unw_aux_info aux;
8165 bfd_boolean res = TRUE;
8166
8167 memset (& aux, 0, sizeof (aux));
8168
8169 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8170 {
8171 if (sec->sh_type == SHT_SYMTAB)
8172 {
8173 if (aux.symtab)
8174 {
8175 error (_("Multiple symbol tables encountered\n"));
8176 free (aux.symtab);
8177 aux.symtab = NULL;
8178 free (aux.strtab);
8179 aux.strtab = NULL;
8180 }
8181 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8182 &aux.strtab, &aux.strtab_size))
8183 return FALSE;
8184 }
8185 else if (sec->sh_type == SHT_IA_64_UNWIND)
8186 unwcount++;
8187 }
8188
8189 if (!unwcount)
8190 printf (_("\nThere are no unwind sections in this file.\n"));
8191
8192 while (unwcount-- > 0)
8193 {
8194 char * suffix;
8195 size_t len, len2;
8196
8197 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8198 i < filedata->file_header.e_shnum; ++i, ++sec)
8199 if (sec->sh_type == SHT_IA_64_UNWIND)
8200 {
8201 unwsec = sec;
8202 break;
8203 }
8204 /* We have already counted the number of SHT_IA64_UNWIND
8205 sections so the loop above should never fail. */
8206 assert (unwsec != NULL);
8207
8208 unwstart = i + 1;
8209 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8210
8211 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8212 {
8213 /* We need to find which section group it is in. */
8214 struct group_list * g;
8215
8216 if (filedata->section_headers_groups == NULL
8217 || filedata->section_headers_groups[i] == NULL)
8218 i = filedata->file_header.e_shnum;
8219 else
8220 {
8221 g = filedata->section_headers_groups[i]->root;
8222
8223 for (; g != NULL; g = g->next)
8224 {
8225 sec = filedata->section_headers + g->section_index;
8226
8227 if (SECTION_NAME_VALID (sec)
8228 && streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
8229 break;
8230 }
8231
8232 if (g == NULL)
8233 i = filedata->file_header.e_shnum;
8234 }
8235 }
8236 else if (SECTION_NAME_VALID (unwsec)
8237 && strneq (SECTION_NAME (unwsec),
8238 ELF_STRING_ia64_unwind_once, len))
8239 {
8240 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8241 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8242 suffix = SECTION_NAME (unwsec) + len;
8243 for (i = 0, sec = filedata->section_headers;
8244 i < filedata->file_header.e_shnum;
8245 ++i, ++sec)
8246 if (SECTION_NAME_VALID (sec)
8247 && strneq (SECTION_NAME (sec),
8248 ELF_STRING_ia64_unwind_info_once, len2)
8249 && streq (SECTION_NAME (sec) + len2, suffix))
8250 break;
8251 }
8252 else
8253 {
8254 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8255 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8256 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8257 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8258 suffix = "";
8259 if (SECTION_NAME_VALID (unwsec)
8260 && strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
8261 suffix = SECTION_NAME (unwsec) + len;
8262 for (i = 0, sec = filedata->section_headers;
8263 i < filedata->file_header.e_shnum;
8264 ++i, ++sec)
8265 if (SECTION_NAME_VALID (sec)
8266 && strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
8267 && streq (SECTION_NAME (sec) + len2, suffix))
8268 break;
8269 }
8270
8271 if (i == filedata->file_header.e_shnum)
8272 {
8273 printf (_("\nCould not find unwind info section for "));
8274
8275 if (filedata->string_table == NULL)
8276 printf ("%d", unwsec->sh_name);
8277 else
8278 printf ("'%s'", printable_section_name (filedata, unwsec));
8279 }
8280 else
8281 {
8282 aux.info_addr = sec->sh_addr;
8283 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
8284 sec->sh_size,
8285 _("unwind info"));
8286 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
8287
8288 printf (_("\nUnwind section "));
8289
8290 if (filedata->string_table == NULL)
8291 printf ("%d", unwsec->sh_name);
8292 else
8293 printf ("'%s'", printable_section_name (filedata, unwsec));
8294
8295 printf (_(" at offset 0x%lx contains %lu entries:\n"),
8296 (unsigned long) unwsec->sh_offset,
8297 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
8298
8299 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
8300 && aux.table_len > 0)
8301 dump_ia64_unwind (filedata, & aux);
8302
8303 free ((char *) aux.table);
8304 free ((char *) aux.info);
8305 aux.table = NULL;
8306 aux.info = NULL;
8307 }
8308 }
8309
8310 free (aux.symtab);
8311 free ((char *) aux.strtab);
8312
8313 return res;
8314 }
8315
8316 struct hppa_unw_table_entry
8317 {
8318 struct absaddr start;
8319 struct absaddr end;
8320 unsigned int Cannot_unwind:1; /* 0 */
8321 unsigned int Millicode:1; /* 1 */
8322 unsigned int Millicode_save_sr0:1; /* 2 */
8323 unsigned int Region_description:2; /* 3..4 */
8324 unsigned int reserved1:1; /* 5 */
8325 unsigned int Entry_SR:1; /* 6 */
8326 unsigned int Entry_FR:4; /* Number saved 7..10 */
8327 unsigned int Entry_GR:5; /* Number saved 11..15 */
8328 unsigned int Args_stored:1; /* 16 */
8329 unsigned int Variable_Frame:1; /* 17 */
8330 unsigned int Separate_Package_Body:1; /* 18 */
8331 unsigned int Frame_Extension_Millicode:1; /* 19 */
8332 unsigned int Stack_Overflow_Check:1; /* 20 */
8333 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8334 unsigned int Ada_Region:1; /* 22 */
8335 unsigned int cxx_info:1; /* 23 */
8336 unsigned int cxx_try_catch:1; /* 24 */
8337 unsigned int sched_entry_seq:1; /* 25 */
8338 unsigned int reserved2:1; /* 26 */
8339 unsigned int Save_SP:1; /* 27 */
8340 unsigned int Save_RP:1; /* 28 */
8341 unsigned int Save_MRP_in_frame:1; /* 29 */
8342 unsigned int extn_ptr_defined:1; /* 30 */
8343 unsigned int Cleanup_defined:1; /* 31 */
8344
8345 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8346 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8347 unsigned int Large_frame:1; /* 2 */
8348 unsigned int Pseudo_SP_Set:1; /* 3 */
8349 unsigned int reserved4:1; /* 4 */
8350 unsigned int Total_frame_size:27; /* 5..31 */
8351 };
8352
8353 struct hppa_unw_aux_info
8354 {
8355 struct hppa_unw_table_entry * table; /* Unwind table. */
8356 unsigned long table_len; /* Length of unwind table. */
8357 bfd_vma seg_base; /* Starting address of segment. */
8358 Elf_Internal_Sym * symtab; /* The symbol table. */
8359 unsigned long nsyms; /* Number of symbols. */
8360 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8361 unsigned long nfuns; /* Number of entries in funtab. */
8362 char * strtab; /* The string table. */
8363 unsigned long strtab_size; /* Size of string table. */
8364 };
8365
8366 static bfd_boolean
8367 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
8368 {
8369 struct hppa_unw_table_entry * tp;
8370 unsigned long j, nfuns;
8371 bfd_boolean res = TRUE;
8372
8373 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8374 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8375 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8376 aux->funtab[nfuns++] = aux->symtab[j];
8377 aux->nfuns = nfuns;
8378 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8379
8380 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8381 {
8382 bfd_vma offset;
8383 const char * procname;
8384
8385 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8386 aux->strtab_size, tp->start, &procname,
8387 &offset);
8388
8389 fputs ("\n<", stdout);
8390
8391 if (procname)
8392 {
8393 fputs (procname, stdout);
8394
8395 if (offset)
8396 printf ("+%lx", (unsigned long) offset);
8397 }
8398
8399 fputs (">: [", stdout);
8400 print_vma (tp->start.offset, PREFIX_HEX);
8401 fputc ('-', stdout);
8402 print_vma (tp->end.offset, PREFIX_HEX);
8403 printf ("]\n\t");
8404
8405 #define PF(_m) if (tp->_m) printf (#_m " ");
8406 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8407 PF(Cannot_unwind);
8408 PF(Millicode);
8409 PF(Millicode_save_sr0);
8410 /* PV(Region_description); */
8411 PF(Entry_SR);
8412 PV(Entry_FR);
8413 PV(Entry_GR);
8414 PF(Args_stored);
8415 PF(Variable_Frame);
8416 PF(Separate_Package_Body);
8417 PF(Frame_Extension_Millicode);
8418 PF(Stack_Overflow_Check);
8419 PF(Two_Instruction_SP_Increment);
8420 PF(Ada_Region);
8421 PF(cxx_info);
8422 PF(cxx_try_catch);
8423 PF(sched_entry_seq);
8424 PF(Save_SP);
8425 PF(Save_RP);
8426 PF(Save_MRP_in_frame);
8427 PF(extn_ptr_defined);
8428 PF(Cleanup_defined);
8429 PF(MPE_XL_interrupt_marker);
8430 PF(HP_UX_interrupt_marker);
8431 PF(Large_frame);
8432 PF(Pseudo_SP_Set);
8433 PV(Total_frame_size);
8434 #undef PF
8435 #undef PV
8436 }
8437
8438 printf ("\n");
8439
8440 free (aux->funtab);
8441
8442 return res;
8443 }
8444
8445 static bfd_boolean
8446 slurp_hppa_unwind_table (Filedata * filedata,
8447 struct hppa_unw_aux_info * aux,
8448 Elf_Internal_Shdr * sec)
8449 {
8450 unsigned long size, unw_ent_size, nentries, nrelas, i;
8451 Elf_Internal_Phdr * seg;
8452 struct hppa_unw_table_entry * tep;
8453 Elf_Internal_Shdr * relsec;
8454 Elf_Internal_Rela * rela;
8455 Elf_Internal_Rela * rp;
8456 unsigned char * table;
8457 unsigned char * tp;
8458 Elf_Internal_Sym * sym;
8459 const char * relname;
8460
8461 /* First, find the starting address of the segment that includes
8462 this section. */
8463 if (filedata->file_header.e_phnum)
8464 {
8465 if (! get_program_headers (filedata))
8466 return FALSE;
8467
8468 for (seg = filedata->program_headers;
8469 seg < filedata->program_headers + filedata->file_header.e_phnum;
8470 ++seg)
8471 {
8472 if (seg->p_type != PT_LOAD)
8473 continue;
8474
8475 if (sec->sh_addr >= seg->p_vaddr
8476 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8477 {
8478 aux->seg_base = seg->p_vaddr;
8479 break;
8480 }
8481 }
8482 }
8483
8484 /* Second, build the unwind table from the contents of the unwind
8485 section. */
8486 size = sec->sh_size;
8487 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8488 _("unwind table"));
8489 if (!table)
8490 return FALSE;
8491
8492 unw_ent_size = 16;
8493 nentries = size / unw_ent_size;
8494 size = unw_ent_size * nentries;
8495
8496 aux->table_len = nentries;
8497 tep = aux->table = (struct hppa_unw_table_entry *)
8498 xcmalloc (nentries, sizeof (aux->table[0]));
8499
8500 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
8501 {
8502 unsigned int tmp1, tmp2;
8503
8504 tep->start.section = SHN_UNDEF;
8505 tep->end.section = SHN_UNDEF;
8506
8507 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8508 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8509 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8510 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8511
8512 tep->start.offset += aux->seg_base;
8513 tep->end.offset += aux->seg_base;
8514
8515 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8516 tep->Millicode = (tmp1 >> 30) & 0x1;
8517 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8518 tep->Region_description = (tmp1 >> 27) & 0x3;
8519 tep->reserved1 = (tmp1 >> 26) & 0x1;
8520 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8521 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8522 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8523 tep->Args_stored = (tmp1 >> 15) & 0x1;
8524 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8525 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8526 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8527 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8528 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8529 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8530 tep->cxx_info = (tmp1 >> 8) & 0x1;
8531 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8532 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8533 tep->reserved2 = (tmp1 >> 5) & 0x1;
8534 tep->Save_SP = (tmp1 >> 4) & 0x1;
8535 tep->Save_RP = (tmp1 >> 3) & 0x1;
8536 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8537 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8538 tep->Cleanup_defined = tmp1 & 0x1;
8539
8540 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8541 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8542 tep->Large_frame = (tmp2 >> 29) & 0x1;
8543 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8544 tep->reserved4 = (tmp2 >> 27) & 0x1;
8545 tep->Total_frame_size = tmp2 & 0x7ffffff;
8546 }
8547 free (table);
8548
8549 /* Third, apply any relocations to the unwind table. */
8550 for (relsec = filedata->section_headers;
8551 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8552 ++relsec)
8553 {
8554 if (relsec->sh_type != SHT_RELA
8555 || relsec->sh_info >= filedata->file_header.e_shnum
8556 || filedata->section_headers + relsec->sh_info != sec)
8557 continue;
8558
8559 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8560 & rela, & nrelas))
8561 return FALSE;
8562
8563 for (rp = rela; rp < rela + nrelas; ++rp)
8564 {
8565 unsigned int sym_ndx;
8566 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8567 relname = elf_hppa_reloc_type (r_type);
8568
8569 if (relname == NULL)
8570 {
8571 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8572 continue;
8573 }
8574
8575 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
8576 if (! const_strneq (relname, "R_PARISC_SEGREL"))
8577 {
8578 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8579 continue;
8580 }
8581
8582 i = rp->r_offset / unw_ent_size;
8583 if (i >= aux->table_len)
8584 {
8585 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8586 continue;
8587 }
8588
8589 sym_ndx = get_reloc_symindex (rp->r_info);
8590 if (sym_ndx >= aux->nsyms)
8591 {
8592 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8593 sym_ndx);
8594 continue;
8595 }
8596 sym = aux->symtab + sym_ndx;
8597
8598 switch ((rp->r_offset % unw_ent_size) / 4)
8599 {
8600 case 0:
8601 aux->table[i].start.section = sym->st_shndx;
8602 aux->table[i].start.offset = sym->st_value + rp->r_addend;
8603 break;
8604 case 1:
8605 aux->table[i].end.section = sym->st_shndx;
8606 aux->table[i].end.offset = sym->st_value + rp->r_addend;
8607 break;
8608 default:
8609 break;
8610 }
8611 }
8612
8613 free (rela);
8614 }
8615
8616 return TRUE;
8617 }
8618
8619 static bfd_boolean
8620 hppa_process_unwind (Filedata * filedata)
8621 {
8622 struct hppa_unw_aux_info aux;
8623 Elf_Internal_Shdr * unwsec = NULL;
8624 Elf_Internal_Shdr * sec;
8625 unsigned long i;
8626 bfd_boolean res = TRUE;
8627
8628 if (filedata->string_table == NULL)
8629 return FALSE;
8630
8631 memset (& aux, 0, sizeof (aux));
8632
8633 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8634 {
8635 if (sec->sh_type == SHT_SYMTAB)
8636 {
8637 if (aux.symtab)
8638 {
8639 error (_("Multiple symbol tables encountered\n"));
8640 free (aux.symtab);
8641 aux.symtab = NULL;
8642 free (aux.strtab);
8643 aux.strtab = NULL;
8644 }
8645 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8646 &aux.strtab, &aux.strtab_size))
8647 return FALSE;
8648 }
8649 else if (SECTION_NAME_VALID (sec)
8650 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
8651 unwsec = sec;
8652 }
8653
8654 if (!unwsec)
8655 printf (_("\nThere are no unwind sections in this file.\n"));
8656
8657 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8658 {
8659 if (SECTION_NAME_VALID (sec)
8660 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
8661 {
8662 unsigned long num_unwind = sec->sh_size / 16;
8663
8664 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8665 "contains %lu entry:\n",
8666 "\nUnwind section '%s' at offset 0x%lx "
8667 "contains %lu entries:\n",
8668 num_unwind),
8669 printable_section_name (filedata, sec),
8670 (unsigned long) sec->sh_offset,
8671 num_unwind);
8672
8673 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8674 res = FALSE;
8675
8676 if (res && aux.table_len > 0)
8677 {
8678 if (! dump_hppa_unwind (filedata, &aux))
8679 res = FALSE;
8680 }
8681
8682 free ((char *) aux.table);
8683 aux.table = NULL;
8684 }
8685 }
8686
8687 free (aux.symtab);
8688 free ((char *) aux.strtab);
8689
8690 return res;
8691 }
8692
8693 struct arm_section
8694 {
8695 unsigned char * data; /* The unwind data. */
8696 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
8697 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
8698 unsigned long nrelas; /* The number of relocations. */
8699 unsigned int rel_type; /* REL or RELA ? */
8700 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
8701 };
8702
8703 struct arm_unw_aux_info
8704 {
8705 Filedata * filedata; /* The file containing the unwind sections. */
8706 Elf_Internal_Sym * symtab; /* The file's symbol table. */
8707 unsigned long nsyms; /* Number of symbols. */
8708 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8709 unsigned long nfuns; /* Number of these symbols. */
8710 char * strtab; /* The file's string table. */
8711 unsigned long strtab_size; /* Size of string table. */
8712 };
8713
8714 static const char *
8715 arm_print_vma_and_name (Filedata * filedata,
8716 struct arm_unw_aux_info * aux,
8717 bfd_vma fn,
8718 struct absaddr addr)
8719 {
8720 const char *procname;
8721 bfd_vma sym_offset;
8722
8723 if (addr.section == SHN_UNDEF)
8724 addr.offset = fn;
8725
8726 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8727 aux->strtab_size, addr, &procname,
8728 &sym_offset);
8729
8730 print_vma (fn, PREFIX_HEX);
8731
8732 if (procname)
8733 {
8734 fputs (" <", stdout);
8735 fputs (procname, stdout);
8736
8737 if (sym_offset)
8738 printf ("+0x%lx", (unsigned long) sym_offset);
8739 fputc ('>', stdout);
8740 }
8741
8742 return procname;
8743 }
8744
8745 static void
8746 arm_free_section (struct arm_section *arm_sec)
8747 {
8748 free (arm_sec->data);
8749 free (arm_sec->rela);
8750 }
8751
8752 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8753 cached section and install SEC instead.
8754 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8755 and return its valued in * WORDP, relocating if necessary.
8756 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
8757 relocation's offset in ADDR.
8758 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
8759 into the string table of the symbol associated with the reloc. If no
8760 reloc was applied store -1 there.
8761 5) Return TRUE upon success, FALSE otherwise. */
8762
8763 static bfd_boolean
8764 get_unwind_section_word (Filedata * filedata,
8765 struct arm_unw_aux_info * aux,
8766 struct arm_section * arm_sec,
8767 Elf_Internal_Shdr * sec,
8768 bfd_vma word_offset,
8769 unsigned int * wordp,
8770 struct absaddr * addr,
8771 bfd_vma * sym_name)
8772 {
8773 Elf_Internal_Rela *rp;
8774 Elf_Internal_Sym *sym;
8775 const char * relname;
8776 unsigned int word;
8777 bfd_boolean wrapped;
8778
8779 if (sec == NULL || arm_sec == NULL)
8780 return FALSE;
8781
8782 addr->section = SHN_UNDEF;
8783 addr->offset = 0;
8784
8785 if (sym_name != NULL)
8786 *sym_name = (bfd_vma) -1;
8787
8788 /* If necessary, update the section cache. */
8789 if (sec != arm_sec->sec)
8790 {
8791 Elf_Internal_Shdr *relsec;
8792
8793 arm_free_section (arm_sec);
8794
8795 arm_sec->sec = sec;
8796 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
8797 sec->sh_size, _("unwind data"));
8798 arm_sec->rela = NULL;
8799 arm_sec->nrelas = 0;
8800
8801 for (relsec = filedata->section_headers;
8802 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8803 ++relsec)
8804 {
8805 if (relsec->sh_info >= filedata->file_header.e_shnum
8806 || filedata->section_headers + relsec->sh_info != sec
8807 /* PR 15745: Check the section type as well. */
8808 || (relsec->sh_type != SHT_REL
8809 && relsec->sh_type != SHT_RELA))
8810 continue;
8811
8812 arm_sec->rel_type = relsec->sh_type;
8813 if (relsec->sh_type == SHT_REL)
8814 {
8815 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
8816 relsec->sh_size,
8817 & arm_sec->rela, & arm_sec->nrelas))
8818 return FALSE;
8819 }
8820 else /* relsec->sh_type == SHT_RELA */
8821 {
8822 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
8823 relsec->sh_size,
8824 & arm_sec->rela, & arm_sec->nrelas))
8825 return FALSE;
8826 }
8827 break;
8828 }
8829
8830 arm_sec->next_rela = arm_sec->rela;
8831 }
8832
8833 /* If there is no unwind data we can do nothing. */
8834 if (arm_sec->data == NULL)
8835 return FALSE;
8836
8837 /* If the offset is invalid then fail. */
8838 if (/* PR 21343 *//* PR 18879 */
8839 sec->sh_size < 4
8840 || word_offset > (sec->sh_size - 4)
8841 || ((bfd_signed_vma) word_offset) < 0)
8842 return FALSE;
8843
8844 /* Get the word at the required offset. */
8845 word = byte_get (arm_sec->data + word_offset, 4);
8846
8847 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
8848 if (arm_sec->rela == NULL)
8849 {
8850 * wordp = word;
8851 return TRUE;
8852 }
8853
8854 /* Look through the relocs to find the one that applies to the provided offset. */
8855 wrapped = FALSE;
8856 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
8857 {
8858 bfd_vma prelval, offset;
8859
8860 if (rp->r_offset > word_offset && !wrapped)
8861 {
8862 rp = arm_sec->rela;
8863 wrapped = TRUE;
8864 }
8865 if (rp->r_offset > word_offset)
8866 break;
8867
8868 if (rp->r_offset & 3)
8869 {
8870 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
8871 (unsigned long) rp->r_offset);
8872 continue;
8873 }
8874
8875 if (rp->r_offset < word_offset)
8876 continue;
8877
8878 /* PR 17531: file: 027-161405-0.004 */
8879 if (aux->symtab == NULL)
8880 continue;
8881
8882 if (arm_sec->rel_type == SHT_REL)
8883 {
8884 offset = word & 0x7fffffff;
8885 if (offset & 0x40000000)
8886 offset |= ~ (bfd_vma) 0x7fffffff;
8887 }
8888 else if (arm_sec->rel_type == SHT_RELA)
8889 offset = rp->r_addend;
8890 else
8891 {
8892 error (_("Unknown section relocation type %d encountered\n"),
8893 arm_sec->rel_type);
8894 break;
8895 }
8896
8897 /* PR 17531 file: 027-1241568-0.004. */
8898 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
8899 {
8900 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
8901 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
8902 break;
8903 }
8904
8905 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
8906 offset += sym->st_value;
8907 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
8908
8909 /* Check that we are processing the expected reloc type. */
8910 if (filedata->file_header.e_machine == EM_ARM)
8911 {
8912 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
8913 if (relname == NULL)
8914 {
8915 warn (_("Skipping unknown ARM relocation type: %d\n"),
8916 (int) ELF32_R_TYPE (rp->r_info));
8917 continue;
8918 }
8919
8920 if (streq (relname, "R_ARM_NONE"))
8921 continue;
8922
8923 if (! streq (relname, "R_ARM_PREL31"))
8924 {
8925 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
8926 continue;
8927 }
8928 }
8929 else if (filedata->file_header.e_machine == EM_TI_C6000)
8930 {
8931 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
8932 if (relname == NULL)
8933 {
8934 warn (_("Skipping unknown C6000 relocation type: %d\n"),
8935 (int) ELF32_R_TYPE (rp->r_info));
8936 continue;
8937 }
8938
8939 if (streq (relname, "R_C6000_NONE"))
8940 continue;
8941
8942 if (! streq (relname, "R_C6000_PREL31"))
8943 {
8944 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
8945 continue;
8946 }
8947
8948 prelval >>= 1;
8949 }
8950 else
8951 {
8952 /* This function currently only supports ARM and TI unwinders. */
8953 warn (_("Only TI and ARM unwinders are currently supported\n"));
8954 break;
8955 }
8956
8957 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
8958 addr->section = sym->st_shndx;
8959 addr->offset = offset;
8960
8961 if (sym_name)
8962 * sym_name = sym->st_name;
8963 break;
8964 }
8965
8966 *wordp = word;
8967 arm_sec->next_rela = rp;
8968
8969 return TRUE;
8970 }
8971
8972 static const char *tic6x_unwind_regnames[16] =
8973 {
8974 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
8975 "A14", "A13", "A12", "A11", "A10",
8976 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
8977 };
8978
8979 static void
8980 decode_tic6x_unwind_regmask (unsigned int mask)
8981 {
8982 int i;
8983
8984 for (i = 12; mask; mask >>= 1, i--)
8985 {
8986 if (mask & 1)
8987 {
8988 fputs (tic6x_unwind_regnames[i], stdout);
8989 if (mask > 1)
8990 fputs (", ", stdout);
8991 }
8992 }
8993 }
8994
8995 #define ADVANCE \
8996 if (remaining == 0 && more_words) \
8997 { \
8998 data_offset += 4; \
8999 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9000 data_offset, & word, & addr, NULL)) \
9001 return FALSE; \
9002 remaining = 4; \
9003 more_words--; \
9004 } \
9005
9006 #define GET_OP(OP) \
9007 ADVANCE; \
9008 if (remaining) \
9009 { \
9010 remaining--; \
9011 (OP) = word >> 24; \
9012 word <<= 8; \
9013 } \
9014 else \
9015 { \
9016 printf (_("[Truncated opcode]\n")); \
9017 return FALSE; \
9018 } \
9019 printf ("0x%02x ", OP)
9020
9021 static bfd_boolean
9022 decode_arm_unwind_bytecode (Filedata * filedata,
9023 struct arm_unw_aux_info * aux,
9024 unsigned int word,
9025 unsigned int remaining,
9026 unsigned int more_words,
9027 bfd_vma data_offset,
9028 Elf_Internal_Shdr * data_sec,
9029 struct arm_section * data_arm_sec)
9030 {
9031 struct absaddr addr;
9032 bfd_boolean res = TRUE;
9033
9034 /* Decode the unwinding instructions. */
9035 while (1)
9036 {
9037 unsigned int op, op2;
9038
9039 ADVANCE;
9040 if (remaining == 0)
9041 break;
9042 remaining--;
9043 op = word >> 24;
9044 word <<= 8;
9045
9046 printf (" 0x%02x ", op);
9047
9048 if ((op & 0xc0) == 0x00)
9049 {
9050 int offset = ((op & 0x3f) << 2) + 4;
9051
9052 printf (" vsp = vsp + %d", offset);
9053 }
9054 else if ((op & 0xc0) == 0x40)
9055 {
9056 int offset = ((op & 0x3f) << 2) + 4;
9057
9058 printf (" vsp = vsp - %d", offset);
9059 }
9060 else if ((op & 0xf0) == 0x80)
9061 {
9062 GET_OP (op2);
9063 if (op == 0x80 && op2 == 0)
9064 printf (_("Refuse to unwind"));
9065 else
9066 {
9067 unsigned int mask = ((op & 0x0f) << 8) | op2;
9068 bfd_boolean first = TRUE;
9069 int i;
9070
9071 printf ("pop {");
9072 for (i = 0; i < 12; i++)
9073 if (mask & (1 << i))
9074 {
9075 if (first)
9076 first = FALSE;
9077 else
9078 printf (", ");
9079 printf ("r%d", 4 + i);
9080 }
9081 printf ("}");
9082 }
9083 }
9084 else if ((op & 0xf0) == 0x90)
9085 {
9086 if (op == 0x9d || op == 0x9f)
9087 printf (_(" [Reserved]"));
9088 else
9089 printf (" vsp = r%d", op & 0x0f);
9090 }
9091 else if ((op & 0xf0) == 0xa0)
9092 {
9093 int end = 4 + (op & 0x07);
9094 bfd_boolean first = TRUE;
9095 int i;
9096
9097 printf (" pop {");
9098 for (i = 4; i <= end; i++)
9099 {
9100 if (first)
9101 first = FALSE;
9102 else
9103 printf (", ");
9104 printf ("r%d", i);
9105 }
9106 if (op & 0x08)
9107 {
9108 if (!first)
9109 printf (", ");
9110 printf ("r14");
9111 }
9112 printf ("}");
9113 }
9114 else if (op == 0xb0)
9115 printf (_(" finish"));
9116 else if (op == 0xb1)
9117 {
9118 GET_OP (op2);
9119 if (op2 == 0 || (op2 & 0xf0) != 0)
9120 printf (_("[Spare]"));
9121 else
9122 {
9123 unsigned int mask = op2 & 0x0f;
9124 bfd_boolean first = TRUE;
9125 int i;
9126
9127 printf ("pop {");
9128 for (i = 0; i < 12; i++)
9129 if (mask & (1 << i))
9130 {
9131 if (first)
9132 first = FALSE;
9133 else
9134 printf (", ");
9135 printf ("r%d", i);
9136 }
9137 printf ("}");
9138 }
9139 }
9140 else if (op == 0xb2)
9141 {
9142 unsigned char buf[9];
9143 unsigned int i, len;
9144 unsigned long offset;
9145
9146 for (i = 0; i < sizeof (buf); i++)
9147 {
9148 GET_OP (buf[i]);
9149 if ((buf[i] & 0x80) == 0)
9150 break;
9151 }
9152 if (i == sizeof (buf))
9153 {
9154 error (_("corrupt change to vsp\n"));
9155 res = FALSE;
9156 }
9157 else
9158 {
9159 offset = read_leb128 (buf, buf + i + 1, FALSE, &len, NULL);
9160 assert (len == i + 1);
9161 offset = offset * 4 + 0x204;
9162 printf ("vsp = vsp + %ld", offset);
9163 }
9164 }
9165 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9166 {
9167 unsigned int first, last;
9168
9169 GET_OP (op2);
9170 first = op2 >> 4;
9171 last = op2 & 0x0f;
9172 if (op == 0xc8)
9173 first = first + 16;
9174 printf ("pop {D%d", first);
9175 if (last)
9176 printf ("-D%d", first + last);
9177 printf ("}");
9178 }
9179 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9180 {
9181 unsigned int count = op & 0x07;
9182
9183 printf ("pop {D8");
9184 if (count)
9185 printf ("-D%d", 8 + count);
9186 printf ("}");
9187 }
9188 else if (op >= 0xc0 && op <= 0xc5)
9189 {
9190 unsigned int count = op & 0x07;
9191
9192 printf (" pop {wR10");
9193 if (count)
9194 printf ("-wR%d", 10 + count);
9195 printf ("}");
9196 }
9197 else if (op == 0xc6)
9198 {
9199 unsigned int first, last;
9200
9201 GET_OP (op2);
9202 first = op2 >> 4;
9203 last = op2 & 0x0f;
9204 printf ("pop {wR%d", first);
9205 if (last)
9206 printf ("-wR%d", first + last);
9207 printf ("}");
9208 }
9209 else if (op == 0xc7)
9210 {
9211 GET_OP (op2);
9212 if (op2 == 0 || (op2 & 0xf0) != 0)
9213 printf (_("[Spare]"));
9214 else
9215 {
9216 unsigned int mask = op2 & 0x0f;
9217 bfd_boolean first = TRUE;
9218 int i;
9219
9220 printf ("pop {");
9221 for (i = 0; i < 4; i++)
9222 if (mask & (1 << i))
9223 {
9224 if (first)
9225 first = FALSE;
9226 else
9227 printf (", ");
9228 printf ("wCGR%d", i);
9229 }
9230 printf ("}");
9231 }
9232 }
9233 else
9234 {
9235 printf (_(" [unsupported opcode]"));
9236 res = FALSE;
9237 }
9238
9239 printf ("\n");
9240 }
9241
9242 return res;
9243 }
9244
9245 static bfd_boolean
9246 decode_tic6x_unwind_bytecode (Filedata * filedata,
9247 struct arm_unw_aux_info * aux,
9248 unsigned int word,
9249 unsigned int remaining,
9250 unsigned int more_words,
9251 bfd_vma data_offset,
9252 Elf_Internal_Shdr * data_sec,
9253 struct arm_section * data_arm_sec)
9254 {
9255 struct absaddr addr;
9256
9257 /* Decode the unwinding instructions. */
9258 while (1)
9259 {
9260 unsigned int op, op2;
9261
9262 ADVANCE;
9263 if (remaining == 0)
9264 break;
9265 remaining--;
9266 op = word >> 24;
9267 word <<= 8;
9268
9269 printf (" 0x%02x ", op);
9270
9271 if ((op & 0xc0) == 0x00)
9272 {
9273 int offset = ((op & 0x3f) << 3) + 8;
9274 printf (" sp = sp + %d", offset);
9275 }
9276 else if ((op & 0xc0) == 0x80)
9277 {
9278 GET_OP (op2);
9279 if (op == 0x80 && op2 == 0)
9280 printf (_("Refuse to unwind"));
9281 else
9282 {
9283 unsigned int mask = ((op & 0x1f) << 8) | op2;
9284 if (op & 0x20)
9285 printf ("pop compact {");
9286 else
9287 printf ("pop {");
9288
9289 decode_tic6x_unwind_regmask (mask);
9290 printf("}");
9291 }
9292 }
9293 else if ((op & 0xf0) == 0xc0)
9294 {
9295 unsigned int reg;
9296 unsigned int nregs;
9297 unsigned int i;
9298 const char *name;
9299 struct
9300 {
9301 unsigned int offset;
9302 unsigned int reg;
9303 } regpos[16];
9304
9305 /* Scan entire instruction first so that GET_OP output is not
9306 interleaved with disassembly. */
9307 nregs = 0;
9308 for (i = 0; nregs < (op & 0xf); i++)
9309 {
9310 GET_OP (op2);
9311 reg = op2 >> 4;
9312 if (reg != 0xf)
9313 {
9314 regpos[nregs].offset = i * 2;
9315 regpos[nregs].reg = reg;
9316 nregs++;
9317 }
9318
9319 reg = op2 & 0xf;
9320 if (reg != 0xf)
9321 {
9322 regpos[nregs].offset = i * 2 + 1;
9323 regpos[nregs].reg = reg;
9324 nregs++;
9325 }
9326 }
9327
9328 printf (_("pop frame {"));
9329 if (nregs == 0)
9330 {
9331 printf (_("*corrupt* - no registers specified"));
9332 }
9333 else
9334 {
9335 reg = nregs - 1;
9336 for (i = i * 2; i > 0; i--)
9337 {
9338 if (regpos[reg].offset == i - 1)
9339 {
9340 name = tic6x_unwind_regnames[regpos[reg].reg];
9341 if (reg > 0)
9342 reg--;
9343 }
9344 else
9345 name = _("[pad]");
9346
9347 fputs (name, stdout);
9348 if (i > 1)
9349 printf (", ");
9350 }
9351 }
9352
9353 printf ("}");
9354 }
9355 else if (op == 0xd0)
9356 printf (" MOV FP, SP");
9357 else if (op == 0xd1)
9358 printf (" __c6xabi_pop_rts");
9359 else if (op == 0xd2)
9360 {
9361 unsigned char buf[9];
9362 unsigned int i, len;
9363 unsigned long offset;
9364
9365 for (i = 0; i < sizeof (buf); i++)
9366 {
9367 GET_OP (buf[i]);
9368 if ((buf[i] & 0x80) == 0)
9369 break;
9370 }
9371 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9372 if (i == sizeof (buf))
9373 {
9374 warn (_("Corrupt stack pointer adjustment detected\n"));
9375 return FALSE;
9376 }
9377
9378 offset = read_leb128 (buf, buf + i + 1, FALSE, &len, NULL);
9379 assert (len == i + 1);
9380 offset = offset * 8 + 0x408;
9381 printf (_("sp = sp + %ld"), offset);
9382 }
9383 else if ((op & 0xf0) == 0xe0)
9384 {
9385 if ((op & 0x0f) == 7)
9386 printf (" RETURN");
9387 else
9388 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9389 }
9390 else
9391 {
9392 printf (_(" [unsupported opcode]"));
9393 }
9394 putchar ('\n');
9395 }
9396
9397 return TRUE;
9398 }
9399
9400 static bfd_vma
9401 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9402 {
9403 bfd_vma offset;
9404
9405 offset = word & 0x7fffffff;
9406 if (offset & 0x40000000)
9407 offset |= ~ (bfd_vma) 0x7fffffff;
9408
9409 if (filedata->file_header.e_machine == EM_TI_C6000)
9410 offset <<= 1;
9411
9412 return offset + where;
9413 }
9414
9415 static bfd_boolean
9416 decode_arm_unwind (Filedata * filedata,
9417 struct arm_unw_aux_info * aux,
9418 unsigned int word,
9419 unsigned int remaining,
9420 bfd_vma data_offset,
9421 Elf_Internal_Shdr * data_sec,
9422 struct arm_section * data_arm_sec)
9423 {
9424 int per_index;
9425 unsigned int more_words = 0;
9426 struct absaddr addr;
9427 bfd_vma sym_name = (bfd_vma) -1;
9428 bfd_boolean res = TRUE;
9429
9430 if (remaining == 0)
9431 {
9432 /* Fetch the first word.
9433 Note - when decoding an object file the address extracted
9434 here will always be 0. So we also pass in the sym_name
9435 parameter so that we can find the symbol associated with
9436 the personality routine. */
9437 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9438 & word, & addr, & sym_name))
9439 return FALSE;
9440
9441 remaining = 4;
9442 }
9443 else
9444 {
9445 addr.section = SHN_UNDEF;
9446 addr.offset = 0;
9447 }
9448
9449 if ((word & 0x80000000) == 0)
9450 {
9451 /* Expand prel31 for personality routine. */
9452 bfd_vma fn;
9453 const char *procname;
9454
9455 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9456 printf (_(" Personality routine: "));
9457 if (fn == 0
9458 && addr.section == SHN_UNDEF && addr.offset == 0
9459 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9460 {
9461 procname = aux->strtab + sym_name;
9462 print_vma (fn, PREFIX_HEX);
9463 if (procname)
9464 {
9465 fputs (" <", stdout);
9466 fputs (procname, stdout);
9467 fputc ('>', stdout);
9468 }
9469 }
9470 else
9471 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
9472 fputc ('\n', stdout);
9473
9474 /* The GCC personality routines use the standard compact
9475 encoding, starting with one byte giving the number of
9476 words. */
9477 if (procname != NULL
9478 && (const_strneq (procname, "__gcc_personality_v0")
9479 || const_strneq (procname, "__gxx_personality_v0")
9480 || const_strneq (procname, "__gcj_personality_v0")
9481 || const_strneq (procname, "__gnu_objc_personality_v0")))
9482 {
9483 remaining = 0;
9484 more_words = 1;
9485 ADVANCE;
9486 if (!remaining)
9487 {
9488 printf (_(" [Truncated data]\n"));
9489 return FALSE;
9490 }
9491 more_words = word >> 24;
9492 word <<= 8;
9493 remaining--;
9494 per_index = -1;
9495 }
9496 else
9497 return TRUE;
9498 }
9499 else
9500 {
9501 /* ARM EHABI Section 6.3:
9502
9503 An exception-handling table entry for the compact model looks like:
9504
9505 31 30-28 27-24 23-0
9506 -- ----- ----- ----
9507 1 0 index Data for personalityRoutine[index] */
9508
9509 if (filedata->file_header.e_machine == EM_ARM
9510 && (word & 0x70000000))
9511 {
9512 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
9513 res = FALSE;
9514 }
9515
9516 per_index = (word >> 24) & 0x7f;
9517 printf (_(" Compact model index: %d\n"), per_index);
9518 if (per_index == 0)
9519 {
9520 more_words = 0;
9521 word <<= 8;
9522 remaining--;
9523 }
9524 else if (per_index < 3)
9525 {
9526 more_words = (word >> 16) & 0xff;
9527 word <<= 16;
9528 remaining -= 2;
9529 }
9530 }
9531
9532 switch (filedata->file_header.e_machine)
9533 {
9534 case EM_ARM:
9535 if (per_index < 3)
9536 {
9537 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
9538 data_offset, data_sec, data_arm_sec))
9539 res = FALSE;
9540 }
9541 else
9542 {
9543 warn (_("Unknown ARM compact model index encountered\n"));
9544 printf (_(" [reserved]\n"));
9545 res = FALSE;
9546 }
9547 break;
9548
9549 case EM_TI_C6000:
9550 if (per_index < 3)
9551 {
9552 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9553 data_offset, data_sec, data_arm_sec))
9554 res = FALSE;
9555 }
9556 else if (per_index < 5)
9557 {
9558 if (((word >> 17) & 0x7f) == 0x7f)
9559 printf (_(" Restore stack from frame pointer\n"));
9560 else
9561 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9562 printf (_(" Registers restored: "));
9563 if (per_index == 4)
9564 printf (" (compact) ");
9565 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9566 putchar ('\n');
9567 printf (_(" Return register: %s\n"),
9568 tic6x_unwind_regnames[word & 0xf]);
9569 }
9570 else
9571 printf (_(" [reserved (%d)]\n"), per_index);
9572 break;
9573
9574 default:
9575 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9576 filedata->file_header.e_machine);
9577 res = FALSE;
9578 }
9579
9580 /* Decode the descriptors. Not implemented. */
9581
9582 return res;
9583 }
9584
9585 static bfd_boolean
9586 dump_arm_unwind (Filedata * filedata,
9587 struct arm_unw_aux_info * aux,
9588 Elf_Internal_Shdr * exidx_sec)
9589 {
9590 struct arm_section exidx_arm_sec, extab_arm_sec;
9591 unsigned int i, exidx_len;
9592 unsigned long j, nfuns;
9593 bfd_boolean res = TRUE;
9594
9595 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9596 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9597 exidx_len = exidx_sec->sh_size / 8;
9598
9599 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9600 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9601 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9602 aux->funtab[nfuns++] = aux->symtab[j];
9603 aux->nfuns = nfuns;
9604 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9605
9606 for (i = 0; i < exidx_len; i++)
9607 {
9608 unsigned int exidx_fn, exidx_entry;
9609 struct absaddr fn_addr, entry_addr;
9610 bfd_vma fn;
9611
9612 fputc ('\n', stdout);
9613
9614 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9615 8 * i, & exidx_fn, & fn_addr, NULL)
9616 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9617 8 * i + 4, & exidx_entry, & entry_addr, NULL))
9618 {
9619 free (aux->funtab);
9620 arm_free_section (& exidx_arm_sec);
9621 arm_free_section (& extab_arm_sec);
9622 return FALSE;
9623 }
9624
9625 /* ARM EHABI, Section 5:
9626 An index table entry consists of 2 words.
9627 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
9628 if (exidx_fn & 0x80000000)
9629 {
9630 warn (_("corrupt index table entry: %x\n"), exidx_fn);
9631 res = FALSE;
9632 }
9633
9634 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9635
9636 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9637 fputs (": ", stdout);
9638
9639 if (exidx_entry == 1)
9640 {
9641 print_vma (exidx_entry, PREFIX_HEX);
9642 fputs (" [cantunwind]\n", stdout);
9643 }
9644 else if (exidx_entry & 0x80000000)
9645 {
9646 print_vma (exidx_entry, PREFIX_HEX);
9647 fputc ('\n', stdout);
9648 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9649 }
9650 else
9651 {
9652 bfd_vma table, table_offset = 0;
9653 Elf_Internal_Shdr *table_sec;
9654
9655 fputs ("@", stdout);
9656 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9657 print_vma (table, PREFIX_HEX);
9658 printf ("\n");
9659
9660 /* Locate the matching .ARM.extab. */
9661 if (entry_addr.section != SHN_UNDEF
9662 && entry_addr.section < filedata->file_header.e_shnum)
9663 {
9664 table_sec = filedata->section_headers + entry_addr.section;
9665 table_offset = entry_addr.offset;
9666 /* PR 18879 */
9667 if (table_offset > table_sec->sh_size
9668 || ((bfd_signed_vma) table_offset) < 0)
9669 {
9670 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9671 (unsigned long) table_offset,
9672 printable_section_name (filedata, table_sec));
9673 res = FALSE;
9674 continue;
9675 }
9676 }
9677 else
9678 {
9679 table_sec = find_section_by_address (filedata, table);
9680 if (table_sec != NULL)
9681 table_offset = table - table_sec->sh_addr;
9682 }
9683
9684 if (table_sec == NULL)
9685 {
9686 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9687 (unsigned long) table);
9688 res = FALSE;
9689 continue;
9690 }
9691
9692 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9693 &extab_arm_sec))
9694 res = FALSE;
9695 }
9696 }
9697
9698 printf ("\n");
9699
9700 free (aux->funtab);
9701 arm_free_section (&exidx_arm_sec);
9702 arm_free_section (&extab_arm_sec);
9703
9704 return res;
9705 }
9706
9707 /* Used for both ARM and C6X unwinding tables. */
9708
9709 static bfd_boolean
9710 arm_process_unwind (Filedata * filedata)
9711 {
9712 struct arm_unw_aux_info aux;
9713 Elf_Internal_Shdr *unwsec = NULL;
9714 Elf_Internal_Shdr *sec;
9715 unsigned long i;
9716 unsigned int sec_type;
9717 bfd_boolean res = TRUE;
9718
9719 switch (filedata->file_header.e_machine)
9720 {
9721 case EM_ARM:
9722 sec_type = SHT_ARM_EXIDX;
9723 break;
9724
9725 case EM_TI_C6000:
9726 sec_type = SHT_C6000_UNWIND;
9727 break;
9728
9729 default:
9730 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9731 filedata->file_header.e_machine);
9732 return FALSE;
9733 }
9734
9735 if (filedata->string_table == NULL)
9736 return FALSE;
9737
9738 memset (& aux, 0, sizeof (aux));
9739 aux.filedata = filedata;
9740
9741 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9742 {
9743 if (sec->sh_type == SHT_SYMTAB)
9744 {
9745 if (aux.symtab)
9746 {
9747 error (_("Multiple symbol tables encountered\n"));
9748 free (aux.symtab);
9749 aux.symtab = NULL;
9750 free (aux.strtab);
9751 aux.strtab = NULL;
9752 }
9753 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9754 &aux.strtab, &aux.strtab_size))
9755 return FALSE;
9756 }
9757 else if (sec->sh_type == sec_type)
9758 unwsec = sec;
9759 }
9760
9761 if (unwsec == NULL)
9762 printf (_("\nThere are no unwind sections in this file.\n"));
9763 else
9764 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9765 {
9766 if (sec->sh_type == sec_type)
9767 {
9768 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
9769 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9770 "contains %lu entry:\n",
9771 "\nUnwind section '%s' at offset 0x%lx "
9772 "contains %lu entries:\n",
9773 num_unwind),
9774 printable_section_name (filedata, sec),
9775 (unsigned long) sec->sh_offset,
9776 num_unwind);
9777
9778 if (! dump_arm_unwind (filedata, &aux, sec))
9779 res = FALSE;
9780 }
9781 }
9782
9783 free (aux.symtab);
9784 free ((char *) aux.strtab);
9785
9786 return res;
9787 }
9788
9789 static bfd_boolean
9790 process_unwind (Filedata * filedata)
9791 {
9792 struct unwind_handler
9793 {
9794 unsigned int machtype;
9795 bfd_boolean (* handler)(Filedata *);
9796 } handlers[] =
9797 {
9798 { EM_ARM, arm_process_unwind },
9799 { EM_IA_64, ia64_process_unwind },
9800 { EM_PARISC, hppa_process_unwind },
9801 { EM_TI_C6000, arm_process_unwind },
9802 { 0, NULL }
9803 };
9804 int i;
9805
9806 if (!do_unwind)
9807 return TRUE;
9808
9809 for (i = 0; handlers[i].handler != NULL; i++)
9810 if (filedata->file_header.e_machine == handlers[i].machtype)
9811 return handlers[i].handler (filedata);
9812
9813 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
9814 get_machine_name (filedata->file_header.e_machine));
9815 return TRUE;
9816 }
9817
9818 static void
9819 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
9820 {
9821 switch (entry->d_tag)
9822 {
9823 case DT_AARCH64_BTI_PLT:
9824 case DT_AARCH64_PAC_PLT:
9825 break;
9826 default:
9827 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9828 break;
9829 }
9830 putchar ('\n');
9831 }
9832
9833 static void
9834 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
9835 {
9836 switch (entry->d_tag)
9837 {
9838 case DT_MIPS_FLAGS:
9839 if (entry->d_un.d_val == 0)
9840 printf (_("NONE"));
9841 else
9842 {
9843 static const char * opts[] =
9844 {
9845 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
9846 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
9847 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
9848 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
9849 "RLD_ORDER_SAFE"
9850 };
9851 unsigned int cnt;
9852 bfd_boolean first = TRUE;
9853
9854 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
9855 if (entry->d_un.d_val & (1 << cnt))
9856 {
9857 printf ("%s%s", first ? "" : " ", opts[cnt]);
9858 first = FALSE;
9859 }
9860 }
9861 break;
9862
9863 case DT_MIPS_IVERSION:
9864 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
9865 printf (_("Interface Version: %s"),
9866 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
9867 else
9868 {
9869 char buf[40];
9870 sprintf_vma (buf, entry->d_un.d_ptr);
9871 /* Note: coded this way so that there is a single string for translation. */
9872 printf (_("<corrupt: %s>"), buf);
9873 }
9874 break;
9875
9876 case DT_MIPS_TIME_STAMP:
9877 {
9878 char timebuf[128];
9879 struct tm * tmp;
9880 time_t atime = entry->d_un.d_val;
9881
9882 tmp = gmtime (&atime);
9883 /* PR 17531: file: 6accc532. */
9884 if (tmp == NULL)
9885 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
9886 else
9887 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
9888 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9889 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9890 printf (_("Time Stamp: %s"), timebuf);
9891 }
9892 break;
9893
9894 case DT_MIPS_RLD_VERSION:
9895 case DT_MIPS_LOCAL_GOTNO:
9896 case DT_MIPS_CONFLICTNO:
9897 case DT_MIPS_LIBLISTNO:
9898 case DT_MIPS_SYMTABNO:
9899 case DT_MIPS_UNREFEXTNO:
9900 case DT_MIPS_HIPAGENO:
9901 case DT_MIPS_DELTA_CLASS_NO:
9902 case DT_MIPS_DELTA_INSTANCE_NO:
9903 case DT_MIPS_DELTA_RELOC_NO:
9904 case DT_MIPS_DELTA_SYM_NO:
9905 case DT_MIPS_DELTA_CLASSSYM_NO:
9906 case DT_MIPS_COMPACT_SIZE:
9907 print_vma (entry->d_un.d_val, DEC);
9908 break;
9909
9910 case DT_MIPS_XHASH:
9911 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
9912 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
9913 /* Falls through. */
9914
9915 default:
9916 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9917 }
9918 putchar ('\n');
9919 }
9920
9921 static void
9922 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
9923 {
9924 switch (entry->d_tag)
9925 {
9926 case DT_HP_DLD_FLAGS:
9927 {
9928 static struct
9929 {
9930 long int bit;
9931 const char * str;
9932 }
9933 flags[] =
9934 {
9935 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
9936 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
9937 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
9938 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
9939 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
9940 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
9941 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
9942 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
9943 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
9944 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
9945 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
9946 { DT_HP_GST, "HP_GST" },
9947 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
9948 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
9949 { DT_HP_NODELETE, "HP_NODELETE" },
9950 { DT_HP_GROUP, "HP_GROUP" },
9951 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
9952 };
9953 bfd_boolean first = TRUE;
9954 size_t cnt;
9955 bfd_vma val = entry->d_un.d_val;
9956
9957 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
9958 if (val & flags[cnt].bit)
9959 {
9960 if (! first)
9961 putchar (' ');
9962 fputs (flags[cnt].str, stdout);
9963 first = FALSE;
9964 val ^= flags[cnt].bit;
9965 }
9966
9967 if (val != 0 || first)
9968 {
9969 if (! first)
9970 putchar (' ');
9971 print_vma (val, HEX);
9972 }
9973 }
9974 break;
9975
9976 default:
9977 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9978 break;
9979 }
9980 putchar ('\n');
9981 }
9982
9983 #ifdef BFD64
9984
9985 /* VMS vs Unix time offset and factor. */
9986
9987 #define VMS_EPOCH_OFFSET 35067168000000000LL
9988 #define VMS_GRANULARITY_FACTOR 10000000
9989 #ifndef INT64_MIN
9990 #define INT64_MIN (-9223372036854775807LL - 1)
9991 #endif
9992
9993 /* Display a VMS time in a human readable format. */
9994
9995 static void
9996 print_vms_time (bfd_int64_t vmstime)
9997 {
9998 struct tm *tm = NULL;
9999 time_t unxtime;
10000
10001 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10002 {
10003 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10004 unxtime = vmstime;
10005 if (unxtime == vmstime)
10006 tm = gmtime (&unxtime);
10007 }
10008 if (tm != NULL)
10009 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10010 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10011 tm->tm_hour, tm->tm_min, tm->tm_sec);
10012 }
10013 #endif /* BFD64 */
10014
10015 static void
10016 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10017 {
10018 switch (entry->d_tag)
10019 {
10020 case DT_IA_64_PLT_RESERVE:
10021 /* First 3 slots reserved. */
10022 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10023 printf (" -- ");
10024 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10025 break;
10026
10027 case DT_IA_64_VMS_LINKTIME:
10028 #ifdef BFD64
10029 print_vms_time (entry->d_un.d_val);
10030 #endif
10031 break;
10032
10033 case DT_IA_64_VMS_LNKFLAGS:
10034 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10035 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10036 printf (" CALL_DEBUG");
10037 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10038 printf (" NOP0BUFS");
10039 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10040 printf (" P0IMAGE");
10041 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10042 printf (" MKTHREADS");
10043 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10044 printf (" UPCALLS");
10045 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10046 printf (" IMGSTA");
10047 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10048 printf (" INITIALIZE");
10049 if (entry->d_un.d_val & VMS_LF_MAIN)
10050 printf (" MAIN");
10051 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10052 printf (" EXE_INIT");
10053 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10054 printf (" TBK_IN_IMG");
10055 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10056 printf (" DBG_IN_IMG");
10057 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10058 printf (" TBK_IN_DSF");
10059 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10060 printf (" DBG_IN_DSF");
10061 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10062 printf (" SIGNATURES");
10063 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10064 printf (" REL_SEG_OFF");
10065 break;
10066
10067 default:
10068 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10069 break;
10070 }
10071 putchar ('\n');
10072 }
10073
10074 static bfd_boolean
10075 get_32bit_dynamic_section (Filedata * filedata)
10076 {
10077 Elf32_External_Dyn * edyn;
10078 Elf32_External_Dyn * ext;
10079 Elf_Internal_Dyn * entry;
10080
10081 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10082 filedata->dynamic_addr, 1,
10083 filedata->dynamic_size,
10084 _("dynamic section"));
10085 if (!edyn)
10086 return FALSE;
10087
10088 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10089 might not have the luxury of section headers. Look for the DT_NULL
10090 terminator to determine the number of entries. */
10091 for (ext = edyn, filedata->dynamic_nent = 0;
10092 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10093 ext++)
10094 {
10095 filedata->dynamic_nent++;
10096 if (BYTE_GET (ext->d_tag) == DT_NULL)
10097 break;
10098 }
10099
10100 filedata->dynamic_section
10101 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10102 if (filedata->dynamic_section == NULL)
10103 {
10104 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10105 (unsigned long) filedata->dynamic_nent);
10106 free (edyn);
10107 return FALSE;
10108 }
10109
10110 for (ext = edyn, entry = filedata->dynamic_section;
10111 entry < filedata->dynamic_section + filedata->dynamic_nent;
10112 ext++, entry++)
10113 {
10114 entry->d_tag = BYTE_GET (ext->d_tag);
10115 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10116 }
10117
10118 free (edyn);
10119
10120 return TRUE;
10121 }
10122
10123 static bfd_boolean
10124 get_64bit_dynamic_section (Filedata * filedata)
10125 {
10126 Elf64_External_Dyn * edyn;
10127 Elf64_External_Dyn * ext;
10128 Elf_Internal_Dyn * entry;
10129
10130 /* Read in the data. */
10131 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10132 filedata->dynamic_addr, 1,
10133 filedata->dynamic_size,
10134 _("dynamic section"));
10135 if (!edyn)
10136 return FALSE;
10137
10138 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10139 might not have the luxury of section headers. Look for the DT_NULL
10140 terminator to determine the number of entries. */
10141 for (ext = edyn, filedata->dynamic_nent = 0;
10142 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10143 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10144 ext++)
10145 {
10146 filedata->dynamic_nent++;
10147 if (BYTE_GET (ext->d_tag) == DT_NULL)
10148 break;
10149 }
10150
10151 filedata->dynamic_section
10152 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10153 if (filedata->dynamic_section == NULL)
10154 {
10155 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10156 (unsigned long) filedata->dynamic_nent);
10157 free (edyn);
10158 return FALSE;
10159 }
10160
10161 /* Convert from external to internal formats. */
10162 for (ext = edyn, entry = filedata->dynamic_section;
10163 entry < filedata->dynamic_section + filedata->dynamic_nent;
10164 ext++, entry++)
10165 {
10166 entry->d_tag = BYTE_GET (ext->d_tag);
10167 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10168 }
10169
10170 free (edyn);
10171
10172 return TRUE;
10173 }
10174
10175 static void
10176 print_dynamic_flags (bfd_vma flags)
10177 {
10178 bfd_boolean first = TRUE;
10179
10180 while (flags)
10181 {
10182 bfd_vma flag;
10183
10184 flag = flags & - flags;
10185 flags &= ~ flag;
10186
10187 if (first)
10188 first = FALSE;
10189 else
10190 putc (' ', stdout);
10191
10192 switch (flag)
10193 {
10194 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10195 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10196 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10197 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10198 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10199 default: fputs (_("unknown"), stdout); break;
10200 }
10201 }
10202 puts ("");
10203 }
10204
10205 static bfd_vma *
10206 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10207 {
10208 unsigned char * e_data;
10209 bfd_vma * i_data;
10210
10211 /* If the size_t type is smaller than the bfd_size_type, eg because
10212 you are building a 32-bit tool on a 64-bit host, then make sure
10213 that when (number) is cast to (size_t) no information is lost. */
10214 if (sizeof (size_t) < sizeof (bfd_size_type)
10215 && (bfd_size_type) ((size_t) number) != number)
10216 {
10217 error (_("Size truncation prevents reading %s elements of size %u\n"),
10218 bfd_vmatoa ("u", number), ent_size);
10219 return NULL;
10220 }
10221
10222 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10223 attempting to allocate memory when the read is bound to fail. */
10224 if (ent_size * number > filedata->file_size)
10225 {
10226 error (_("Invalid number of dynamic entries: %s\n"),
10227 bfd_vmatoa ("u", number));
10228 return NULL;
10229 }
10230
10231 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10232 if (e_data == NULL)
10233 {
10234 error (_("Out of memory reading %s dynamic entries\n"),
10235 bfd_vmatoa ("u", number));
10236 return NULL;
10237 }
10238
10239 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10240 {
10241 error (_("Unable to read in %s bytes of dynamic data\n"),
10242 bfd_vmatoa ("u", number * ent_size));
10243 free (e_data);
10244 return NULL;
10245 }
10246
10247 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10248 if (i_data == NULL)
10249 {
10250 error (_("Out of memory allocating space for %s dynamic entries\n"),
10251 bfd_vmatoa ("u", number));
10252 free (e_data);
10253 return NULL;
10254 }
10255
10256 while (number--)
10257 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10258
10259 free (e_data);
10260
10261 return i_data;
10262 }
10263
10264 static unsigned long
10265 get_num_dynamic_syms (Filedata * filedata)
10266 {
10267 unsigned long num_of_syms = 0;
10268
10269 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10270 return num_of_syms;
10271
10272 if (filedata->dynamic_info[DT_HASH])
10273 {
10274 unsigned char nb[8];
10275 unsigned char nc[8];
10276 unsigned int hash_ent_size = 4;
10277
10278 if ((filedata->file_header.e_machine == EM_ALPHA
10279 || filedata->file_header.e_machine == EM_S390
10280 || filedata->file_header.e_machine == EM_S390_OLD)
10281 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10282 hash_ent_size = 8;
10283
10284 if (fseek (filedata->handle,
10285 (filedata->archive_file_offset
10286 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10287 sizeof nb + sizeof nc)),
10288 SEEK_SET))
10289 {
10290 error (_("Unable to seek to start of dynamic information\n"));
10291 goto no_hash;
10292 }
10293
10294 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10295 {
10296 error (_("Failed to read in number of buckets\n"));
10297 goto no_hash;
10298 }
10299
10300 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10301 {
10302 error (_("Failed to read in number of chains\n"));
10303 goto no_hash;
10304 }
10305
10306 filedata->nbuckets = byte_get (nb, hash_ent_size);
10307 filedata->nchains = byte_get (nc, hash_ent_size);
10308
10309 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10310 {
10311 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10312 hash_ent_size);
10313 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10314 hash_ent_size);
10315
10316 if (filedata->buckets != NULL && filedata->chains != NULL)
10317 num_of_syms = filedata->nchains;
10318 }
10319 no_hash:
10320 if (num_of_syms == 0)
10321 {
10322 free (filedata->buckets);
10323 filedata->buckets = NULL;
10324 free (filedata->chains);
10325 filedata->chains = NULL;
10326 filedata->nbuckets = 0;
10327 }
10328 }
10329
10330 if (filedata->dynamic_info_DT_GNU_HASH)
10331 {
10332 unsigned char nb[16];
10333 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10334 bfd_vma buckets_vma;
10335 unsigned long hn;
10336
10337 if (fseek (filedata->handle,
10338 (filedata->archive_file_offset
10339 + offset_from_vma (filedata,
10340 filedata->dynamic_info_DT_GNU_HASH,
10341 sizeof nb)),
10342 SEEK_SET))
10343 {
10344 error (_("Unable to seek to start of dynamic information\n"));
10345 goto no_gnu_hash;
10346 }
10347
10348 if (fread (nb, 16, 1, filedata->handle) != 1)
10349 {
10350 error (_("Failed to read in number of buckets\n"));
10351 goto no_gnu_hash;
10352 }
10353
10354 filedata->ngnubuckets = byte_get (nb, 4);
10355 filedata->gnusymidx = byte_get (nb + 4, 4);
10356 bitmaskwords = byte_get (nb + 8, 4);
10357 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10358 if (is_32bit_elf)
10359 buckets_vma += bitmaskwords * 4;
10360 else
10361 buckets_vma += bitmaskwords * 8;
10362
10363 if (fseek (filedata->handle,
10364 (filedata->archive_file_offset
10365 + offset_from_vma (filedata, buckets_vma, 4)),
10366 SEEK_SET))
10367 {
10368 error (_("Unable to seek to start of dynamic information\n"));
10369 goto no_gnu_hash;
10370 }
10371
10372 filedata->gnubuckets
10373 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10374
10375 if (filedata->gnubuckets == NULL)
10376 goto no_gnu_hash;
10377
10378 for (i = 0; i < filedata->ngnubuckets; i++)
10379 if (filedata->gnubuckets[i] != 0)
10380 {
10381 if (filedata->gnubuckets[i] < filedata->gnusymidx)
10382 goto no_gnu_hash;
10383
10384 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10385 maxchain = filedata->gnubuckets[i];
10386 }
10387
10388 if (maxchain == 0xffffffff)
10389 goto no_gnu_hash;
10390
10391 maxchain -= filedata->gnusymidx;
10392
10393 if (fseek (filedata->handle,
10394 (filedata->archive_file_offset
10395 + offset_from_vma (filedata,
10396 buckets_vma + 4 * (filedata->ngnubuckets
10397 + maxchain),
10398 4)),
10399 SEEK_SET))
10400 {
10401 error (_("Unable to seek to start of dynamic information\n"));
10402 goto no_gnu_hash;
10403 }
10404
10405 do
10406 {
10407 if (fread (nb, 4, 1, filedata->handle) != 1)
10408 {
10409 error (_("Failed to determine last chain length\n"));
10410 goto no_gnu_hash;
10411 }
10412
10413 if (maxchain + 1 == 0)
10414 goto no_gnu_hash;
10415
10416 ++maxchain;
10417 }
10418 while ((byte_get (nb, 4) & 1) == 0);
10419
10420 if (fseek (filedata->handle,
10421 (filedata->archive_file_offset
10422 + offset_from_vma (filedata, (buckets_vma
10423 + 4 * filedata->ngnubuckets),
10424 4)),
10425 SEEK_SET))
10426 {
10427 error (_("Unable to seek to start of dynamic information\n"));
10428 goto no_gnu_hash;
10429 }
10430
10431 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10432 filedata->ngnuchains = maxchain;
10433
10434 if (filedata->gnuchains == NULL)
10435 goto no_gnu_hash;
10436
10437 if (filedata->dynamic_info_DT_MIPS_XHASH)
10438 {
10439 if (fseek (filedata->handle,
10440 (filedata->archive_file_offset
10441 + offset_from_vma (filedata, (buckets_vma
10442 + 4 * (filedata->ngnubuckets
10443 + maxchain)), 4)),
10444 SEEK_SET))
10445 {
10446 error (_("Unable to seek to start of dynamic information\n"));
10447 goto no_gnu_hash;
10448 }
10449
10450 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
10451 if (filedata->mipsxlat == NULL)
10452 goto no_gnu_hash;
10453 }
10454
10455 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10456 if (filedata->gnubuckets[hn] != 0)
10457 {
10458 bfd_vma si = filedata->gnubuckets[hn];
10459 bfd_vma off = si - filedata->gnusymidx;
10460
10461 do
10462 {
10463 if (filedata->dynamic_info_DT_MIPS_XHASH)
10464 {
10465 if (off < filedata->ngnuchains
10466 && filedata->mipsxlat[off] >= num_of_syms)
10467 num_of_syms = filedata->mipsxlat[off] + 1;
10468 }
10469 else
10470 {
10471 if (si >= num_of_syms)
10472 num_of_syms = si + 1;
10473 }
10474 si++;
10475 }
10476 while (off < filedata->ngnuchains
10477 && (filedata->gnuchains[off++] & 1) == 0);
10478 }
10479
10480 if (num_of_syms == 0)
10481 {
10482 no_gnu_hash:
10483 free (filedata->mipsxlat);
10484 filedata->mipsxlat = NULL;
10485 free (filedata->gnuchains);
10486 filedata->gnuchains = NULL;
10487 free (filedata->gnubuckets);
10488 filedata->gnubuckets = NULL;
10489 filedata->ngnubuckets = 0;
10490 filedata->ngnuchains = 0;
10491 }
10492 }
10493
10494 return num_of_syms;
10495 }
10496
10497 /* Parse and display the contents of the dynamic section. */
10498
10499 static bfd_boolean
10500 process_dynamic_section (Filedata * filedata)
10501 {
10502 Elf_Internal_Dyn * entry;
10503
10504 if (filedata->dynamic_size == 0)
10505 {
10506 if (do_dynamic)
10507 {
10508 if (filedata->is_separate)
10509 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
10510 filedata->file_name);
10511 else
10512 printf (_("\nThere is no dynamic section in this file.\n"));
10513 }
10514
10515 return TRUE;
10516 }
10517
10518 if (is_32bit_elf)
10519 {
10520 if (! get_32bit_dynamic_section (filedata))
10521 return FALSE;
10522 }
10523 else
10524 {
10525 if (! get_64bit_dynamic_section (filedata))
10526 return FALSE;
10527 }
10528
10529 /* Find the appropriate symbol table. */
10530 if (filedata->dynamic_symbols == NULL || do_histogram)
10531 {
10532 unsigned long num_of_syms;
10533
10534 for (entry = filedata->dynamic_section;
10535 entry < filedata->dynamic_section + filedata->dynamic_nent;
10536 ++entry)
10537 if (entry->d_tag == DT_SYMTAB)
10538 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10539 else if (entry->d_tag == DT_SYMENT)
10540 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10541 else if (entry->d_tag == DT_HASH)
10542 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10543 else if (entry->d_tag == DT_GNU_HASH)
10544 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10545 else if ((filedata->file_header.e_machine == EM_MIPS
10546 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10547 && entry->d_tag == DT_MIPS_XHASH)
10548 {
10549 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10550 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10551 }
10552
10553 num_of_syms = get_num_dynamic_syms (filedata);
10554
10555 if (num_of_syms != 0
10556 && filedata->dynamic_symbols == NULL
10557 && filedata->dynamic_info[DT_SYMTAB]
10558 && filedata->dynamic_info[DT_SYMENT])
10559 {
10560 Elf_Internal_Phdr *seg;
10561 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
10562
10563 if (! get_program_headers (filedata))
10564 {
10565 error (_("Cannot interpret virtual addresses "
10566 "without program headers.\n"));
10567 return FALSE;
10568 }
10569
10570 for (seg = filedata->program_headers;
10571 seg < filedata->program_headers + filedata->file_header.e_phnum;
10572 ++seg)
10573 {
10574 if (seg->p_type != PT_LOAD)
10575 continue;
10576
10577 if (seg->p_offset + seg->p_filesz > filedata->file_size)
10578 {
10579 /* See PR 21379 for a reproducer. */
10580 error (_("Invalid PT_LOAD entry\n"));
10581 return FALSE;
10582 }
10583
10584 if (vma >= (seg->p_vaddr & -seg->p_align)
10585 && vma < seg->p_vaddr + seg->p_filesz)
10586 {
10587 /* Since we do not know how big the symbol table is,
10588 we default to reading in up to the end of PT_LOAD
10589 segment and processing that. This is overkill, I
10590 know, but it should work. */
10591 Elf_Internal_Shdr section;
10592 section.sh_offset = (vma - seg->p_vaddr
10593 + seg->p_offset);
10594 section.sh_size = (num_of_syms
10595 * filedata->dynamic_info[DT_SYMENT]);
10596 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
10597
10598 if (do_checks
10599 && filedata->dynamic_symtab_section != NULL
10600 && ((filedata->dynamic_symtab_section->sh_offset
10601 != section.sh_offset)
10602 || (filedata->dynamic_symtab_section->sh_size
10603 != section.sh_size)
10604 || (filedata->dynamic_symtab_section->sh_entsize
10605 != section.sh_entsize)))
10606 warn (_("\
10607 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
10608
10609 section.sh_name = filedata->string_table_length;
10610 filedata->dynamic_symbols
10611 = GET_ELF_SYMBOLS (filedata, &section,
10612 &filedata->num_dynamic_syms);
10613 if (filedata->dynamic_symbols == NULL
10614 || filedata->num_dynamic_syms != num_of_syms)
10615 {
10616 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
10617 return FALSE;
10618 }
10619 break;
10620 }
10621 }
10622 }
10623 }
10624
10625 /* Similarly find a string table. */
10626 if (filedata->dynamic_strings == NULL)
10627 for (entry = filedata->dynamic_section;
10628 entry < filedata->dynamic_section + filedata->dynamic_nent;
10629 ++entry)
10630 {
10631 if (entry->d_tag == DT_STRTAB)
10632 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
10633
10634 if (entry->d_tag == DT_STRSZ)
10635 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
10636
10637 if (filedata->dynamic_info[DT_STRTAB]
10638 && filedata->dynamic_info[DT_STRSZ])
10639 {
10640 unsigned long offset;
10641 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10642
10643 offset = offset_from_vma (filedata,
10644 filedata->dynamic_info[DT_STRTAB],
10645 str_tab_len);
10646 if (do_checks
10647 && filedata->dynamic_strtab_section
10648 && ((filedata->dynamic_strtab_section->sh_offset
10649 != (file_ptr) offset)
10650 || (filedata->dynamic_strtab_section->sh_size
10651 != str_tab_len)))
10652 warn (_("\
10653 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
10654
10655 filedata->dynamic_strings
10656 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
10657 _("dynamic string table"));
10658 if (filedata->dynamic_strings == NULL)
10659 {
10660 error (_("Corrupt DT_STRTAB dynamic entry\n"));
10661 break;
10662 }
10663
10664 filedata->dynamic_strings_length = str_tab_len;
10665 break;
10666 }
10667 }
10668
10669 /* And find the syminfo section if available. */
10670 if (filedata->dynamic_syminfo == NULL)
10671 {
10672 unsigned long syminsz = 0;
10673
10674 for (entry = filedata->dynamic_section;
10675 entry < filedata->dynamic_section + filedata->dynamic_nent;
10676 ++entry)
10677 {
10678 if (entry->d_tag == DT_SYMINENT)
10679 {
10680 /* Note: these braces are necessary to avoid a syntax
10681 error from the SunOS4 C compiler. */
10682 /* PR binutils/17531: A corrupt file can trigger this test.
10683 So do not use an assert, instead generate an error message. */
10684 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
10685 error (_("Bad value (%d) for SYMINENT entry\n"),
10686 (int) entry->d_un.d_val);
10687 }
10688 else if (entry->d_tag == DT_SYMINSZ)
10689 syminsz = entry->d_un.d_val;
10690 else if (entry->d_tag == DT_SYMINFO)
10691 filedata->dynamic_syminfo_offset
10692 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
10693 }
10694
10695 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
10696 {
10697 Elf_External_Syminfo * extsyminfo;
10698 Elf_External_Syminfo * extsym;
10699 Elf_Internal_Syminfo * syminfo;
10700
10701 /* There is a syminfo section. Read the data. */
10702 extsyminfo = (Elf_External_Syminfo *)
10703 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
10704 1, syminsz, _("symbol information"));
10705 if (!extsyminfo)
10706 return FALSE;
10707
10708 if (filedata->dynamic_syminfo != NULL)
10709 {
10710 error (_("Multiple dynamic symbol information sections found\n"));
10711 free (filedata->dynamic_syminfo);
10712 }
10713 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
10714 if (filedata->dynamic_syminfo == NULL)
10715 {
10716 error (_("Out of memory allocating %lu bytes "
10717 "for dynamic symbol info\n"),
10718 (unsigned long) syminsz);
10719 return FALSE;
10720 }
10721
10722 filedata->dynamic_syminfo_nent
10723 = syminsz / sizeof (Elf_External_Syminfo);
10724 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
10725 syminfo < (filedata->dynamic_syminfo
10726 + filedata->dynamic_syminfo_nent);
10727 ++syminfo, ++extsym)
10728 {
10729 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
10730 syminfo->si_flags = BYTE_GET (extsym->si_flags);
10731 }
10732
10733 free (extsyminfo);
10734 }
10735 }
10736
10737 if (do_dynamic && filedata->dynamic_addr)
10738 {
10739 if (filedata->dynamic_nent == 1)
10740 {
10741 if (filedata->is_separate)
10742 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains 1 entry:\n"),
10743 filedata->file_name,
10744 filedata->dynamic_addr);
10745 else
10746 printf (_("\nDynamic section at offset 0x%lx contains 1 entry:\n"),
10747 filedata->dynamic_addr);
10748 }
10749 else
10750 {
10751 if (filedata->is_separate)
10752 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n"),
10753 filedata->file_name,
10754 filedata->dynamic_addr,
10755 (unsigned long) filedata->dynamic_nent);
10756 else
10757 printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
10758 filedata->dynamic_addr,
10759 (unsigned long) filedata->dynamic_nent);
10760 }
10761 }
10762 if (do_dynamic)
10763 printf (_(" Tag Type Name/Value\n"));
10764
10765 for (entry = filedata->dynamic_section;
10766 entry < filedata->dynamic_section + filedata->dynamic_nent;
10767 entry++)
10768 {
10769 if (do_dynamic)
10770 {
10771 const char * dtype;
10772
10773 putchar (' ');
10774 print_vma (entry->d_tag, FULL_HEX);
10775 dtype = get_dynamic_type (filedata, entry->d_tag);
10776 printf (" (%s)%*s", dtype,
10777 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
10778 }
10779
10780 switch (entry->d_tag)
10781 {
10782 case DT_FLAGS:
10783 if (do_dynamic)
10784 print_dynamic_flags (entry->d_un.d_val);
10785 break;
10786
10787 case DT_AUXILIARY:
10788 case DT_FILTER:
10789 case DT_CONFIG:
10790 case DT_DEPAUDIT:
10791 case DT_AUDIT:
10792 if (do_dynamic)
10793 {
10794 switch (entry->d_tag)
10795 {
10796 case DT_AUXILIARY:
10797 printf (_("Auxiliary library"));
10798 break;
10799
10800 case DT_FILTER:
10801 printf (_("Filter library"));
10802 break;
10803
10804 case DT_CONFIG:
10805 printf (_("Configuration file"));
10806 break;
10807
10808 case DT_DEPAUDIT:
10809 printf (_("Dependency audit library"));
10810 break;
10811
10812 case DT_AUDIT:
10813 printf (_("Audit library"));
10814 break;
10815 }
10816
10817 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
10818 printf (": [%s]\n",
10819 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
10820 else
10821 {
10822 printf (": ");
10823 print_vma (entry->d_un.d_val, PREFIX_HEX);
10824 putchar ('\n');
10825 }
10826 }
10827 break;
10828
10829 case DT_FEATURE:
10830 if (do_dynamic)
10831 {
10832 printf (_("Flags:"));
10833
10834 if (entry->d_un.d_val == 0)
10835 printf (_(" None\n"));
10836 else
10837 {
10838 unsigned long int val = entry->d_un.d_val;
10839
10840 if (val & DTF_1_PARINIT)
10841 {
10842 printf (" PARINIT");
10843 val ^= DTF_1_PARINIT;
10844 }
10845 if (val & DTF_1_CONFEXP)
10846 {
10847 printf (" CONFEXP");
10848 val ^= DTF_1_CONFEXP;
10849 }
10850 if (val != 0)
10851 printf (" %lx", val);
10852 puts ("");
10853 }
10854 }
10855 break;
10856
10857 case DT_POSFLAG_1:
10858 if (do_dynamic)
10859 {
10860 printf (_("Flags:"));
10861
10862 if (entry->d_un.d_val == 0)
10863 printf (_(" None\n"));
10864 else
10865 {
10866 unsigned long int val = entry->d_un.d_val;
10867
10868 if (val & DF_P1_LAZYLOAD)
10869 {
10870 printf (" LAZYLOAD");
10871 val ^= DF_P1_LAZYLOAD;
10872 }
10873 if (val & DF_P1_GROUPPERM)
10874 {
10875 printf (" GROUPPERM");
10876 val ^= DF_P1_GROUPPERM;
10877 }
10878 if (val != 0)
10879 printf (" %lx", val);
10880 puts ("");
10881 }
10882 }
10883 break;
10884
10885 case DT_FLAGS_1:
10886 if (do_dynamic)
10887 {
10888 printf (_("Flags:"));
10889 if (entry->d_un.d_val == 0)
10890 printf (_(" None\n"));
10891 else
10892 {
10893 unsigned long int val = entry->d_un.d_val;
10894
10895 if (val & DF_1_NOW)
10896 {
10897 printf (" NOW");
10898 val ^= DF_1_NOW;
10899 }
10900 if (val & DF_1_GLOBAL)
10901 {
10902 printf (" GLOBAL");
10903 val ^= DF_1_GLOBAL;
10904 }
10905 if (val & DF_1_GROUP)
10906 {
10907 printf (" GROUP");
10908 val ^= DF_1_GROUP;
10909 }
10910 if (val & DF_1_NODELETE)
10911 {
10912 printf (" NODELETE");
10913 val ^= DF_1_NODELETE;
10914 }
10915 if (val & DF_1_LOADFLTR)
10916 {
10917 printf (" LOADFLTR");
10918 val ^= DF_1_LOADFLTR;
10919 }
10920 if (val & DF_1_INITFIRST)
10921 {
10922 printf (" INITFIRST");
10923 val ^= DF_1_INITFIRST;
10924 }
10925 if (val & DF_1_NOOPEN)
10926 {
10927 printf (" NOOPEN");
10928 val ^= DF_1_NOOPEN;
10929 }
10930 if (val & DF_1_ORIGIN)
10931 {
10932 printf (" ORIGIN");
10933 val ^= DF_1_ORIGIN;
10934 }
10935 if (val & DF_1_DIRECT)
10936 {
10937 printf (" DIRECT");
10938 val ^= DF_1_DIRECT;
10939 }
10940 if (val & DF_1_TRANS)
10941 {
10942 printf (" TRANS");
10943 val ^= DF_1_TRANS;
10944 }
10945 if (val & DF_1_INTERPOSE)
10946 {
10947 printf (" INTERPOSE");
10948 val ^= DF_1_INTERPOSE;
10949 }
10950 if (val & DF_1_NODEFLIB)
10951 {
10952 printf (" NODEFLIB");
10953 val ^= DF_1_NODEFLIB;
10954 }
10955 if (val & DF_1_NODUMP)
10956 {
10957 printf (" NODUMP");
10958 val ^= DF_1_NODUMP;
10959 }
10960 if (val & DF_1_CONFALT)
10961 {
10962 printf (" CONFALT");
10963 val ^= DF_1_CONFALT;
10964 }
10965 if (val & DF_1_ENDFILTEE)
10966 {
10967 printf (" ENDFILTEE");
10968 val ^= DF_1_ENDFILTEE;
10969 }
10970 if (val & DF_1_DISPRELDNE)
10971 {
10972 printf (" DISPRELDNE");
10973 val ^= DF_1_DISPRELDNE;
10974 }
10975 if (val & DF_1_DISPRELPND)
10976 {
10977 printf (" DISPRELPND");
10978 val ^= DF_1_DISPRELPND;
10979 }
10980 if (val & DF_1_NODIRECT)
10981 {
10982 printf (" NODIRECT");
10983 val ^= DF_1_NODIRECT;
10984 }
10985 if (val & DF_1_IGNMULDEF)
10986 {
10987 printf (" IGNMULDEF");
10988 val ^= DF_1_IGNMULDEF;
10989 }
10990 if (val & DF_1_NOKSYMS)
10991 {
10992 printf (" NOKSYMS");
10993 val ^= DF_1_NOKSYMS;
10994 }
10995 if (val & DF_1_NOHDR)
10996 {
10997 printf (" NOHDR");
10998 val ^= DF_1_NOHDR;
10999 }
11000 if (val & DF_1_EDITED)
11001 {
11002 printf (" EDITED");
11003 val ^= DF_1_EDITED;
11004 }
11005 if (val & DF_1_NORELOC)
11006 {
11007 printf (" NORELOC");
11008 val ^= DF_1_NORELOC;
11009 }
11010 if (val & DF_1_SYMINTPOSE)
11011 {
11012 printf (" SYMINTPOSE");
11013 val ^= DF_1_SYMINTPOSE;
11014 }
11015 if (val & DF_1_GLOBAUDIT)
11016 {
11017 printf (" GLOBAUDIT");
11018 val ^= DF_1_GLOBAUDIT;
11019 }
11020 if (val & DF_1_SINGLETON)
11021 {
11022 printf (" SINGLETON");
11023 val ^= DF_1_SINGLETON;
11024 }
11025 if (val & DF_1_STUB)
11026 {
11027 printf (" STUB");
11028 val ^= DF_1_STUB;
11029 }
11030 if (val & DF_1_PIE)
11031 {
11032 printf (" PIE");
11033 val ^= DF_1_PIE;
11034 }
11035 if (val & DF_1_KMOD)
11036 {
11037 printf (" KMOD");
11038 val ^= DF_1_KMOD;
11039 }
11040 if (val & DF_1_WEAKFILTER)
11041 {
11042 printf (" WEAKFILTER");
11043 val ^= DF_1_WEAKFILTER;
11044 }
11045 if (val & DF_1_NOCOMMON)
11046 {
11047 printf (" NOCOMMON");
11048 val ^= DF_1_NOCOMMON;
11049 }
11050 if (val != 0)
11051 printf (" %lx", val);
11052 puts ("");
11053 }
11054 }
11055 break;
11056
11057 case DT_PLTREL:
11058 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11059 if (do_dynamic)
11060 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11061 break;
11062
11063 case DT_NULL :
11064 case DT_NEEDED :
11065 case DT_PLTGOT :
11066 case DT_HASH :
11067 case DT_STRTAB :
11068 case DT_SYMTAB :
11069 case DT_RELA :
11070 case DT_INIT :
11071 case DT_FINI :
11072 case DT_SONAME :
11073 case DT_RPATH :
11074 case DT_SYMBOLIC:
11075 case DT_REL :
11076 case DT_DEBUG :
11077 case DT_TEXTREL :
11078 case DT_JMPREL :
11079 case DT_RUNPATH :
11080 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11081
11082 if (do_dynamic)
11083 {
11084 char * name;
11085
11086 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11087 name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
11088 else
11089 name = NULL;
11090
11091 if (name)
11092 {
11093 switch (entry->d_tag)
11094 {
11095 case DT_NEEDED:
11096 printf (_("Shared library: [%s]"), name);
11097
11098 if (streq (name, filedata->program_interpreter))
11099 printf (_(" program interpreter"));
11100 break;
11101
11102 case DT_SONAME:
11103 printf (_("Library soname: [%s]"), name);
11104 break;
11105
11106 case DT_RPATH:
11107 printf (_("Library rpath: [%s]"), name);
11108 break;
11109
11110 case DT_RUNPATH:
11111 printf (_("Library runpath: [%s]"), name);
11112 break;
11113
11114 default:
11115 print_vma (entry->d_un.d_val, PREFIX_HEX);
11116 break;
11117 }
11118 }
11119 else
11120 print_vma (entry->d_un.d_val, PREFIX_HEX);
11121
11122 putchar ('\n');
11123 }
11124 break;
11125
11126 case DT_PLTRELSZ:
11127 case DT_RELASZ :
11128 case DT_STRSZ :
11129 case DT_RELSZ :
11130 case DT_RELAENT :
11131 case DT_SYMENT :
11132 case DT_RELENT :
11133 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11134 /* Fall through. */
11135 case DT_PLTPADSZ:
11136 case DT_MOVEENT :
11137 case DT_MOVESZ :
11138 case DT_INIT_ARRAYSZ:
11139 case DT_FINI_ARRAYSZ:
11140 case DT_GNU_CONFLICTSZ:
11141 case DT_GNU_LIBLISTSZ:
11142 if (do_dynamic)
11143 {
11144 print_vma (entry->d_un.d_val, UNSIGNED);
11145 printf (_(" (bytes)\n"));
11146 }
11147 break;
11148
11149 case DT_VERDEFNUM:
11150 case DT_VERNEEDNUM:
11151 case DT_RELACOUNT:
11152 case DT_RELCOUNT:
11153 if (do_dynamic)
11154 {
11155 print_vma (entry->d_un.d_val, UNSIGNED);
11156 putchar ('\n');
11157 }
11158 break;
11159
11160 case DT_SYMINSZ:
11161 case DT_SYMINENT:
11162 case DT_SYMINFO:
11163 case DT_USED:
11164 case DT_INIT_ARRAY:
11165 case DT_FINI_ARRAY:
11166 if (do_dynamic)
11167 {
11168 if (entry->d_tag == DT_USED
11169 && VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11170 {
11171 char * name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
11172
11173 if (*name)
11174 {
11175 printf (_("Not needed object: [%s]\n"), name);
11176 break;
11177 }
11178 }
11179
11180 print_vma (entry->d_un.d_val, PREFIX_HEX);
11181 putchar ('\n');
11182 }
11183 break;
11184
11185 case DT_BIND_NOW:
11186 /* The value of this entry is ignored. */
11187 if (do_dynamic)
11188 putchar ('\n');
11189 break;
11190
11191 case DT_GNU_PRELINKED:
11192 if (do_dynamic)
11193 {
11194 struct tm * tmp;
11195 time_t atime = entry->d_un.d_val;
11196
11197 tmp = gmtime (&atime);
11198 /* PR 17533 file: 041-1244816-0.004. */
11199 if (tmp == NULL)
11200 printf (_("<corrupt time val: %lx"),
11201 (unsigned long) atime);
11202 else
11203 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11204 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11205 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11206
11207 }
11208 break;
11209
11210 case DT_GNU_HASH:
11211 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11212 if (do_dynamic)
11213 {
11214 print_vma (entry->d_un.d_val, PREFIX_HEX);
11215 putchar ('\n');
11216 }
11217 break;
11218
11219 case DT_GNU_FLAGS_1:
11220 if (do_dynamic)
11221 {
11222 printf (_("Flags:"));
11223 if (entry->d_un.d_val == 0)
11224 printf (_(" None\n"));
11225 else
11226 {
11227 unsigned long int val = entry->d_un.d_val;
11228
11229 if (val & DF_GNU_1_UNIQUE)
11230 {
11231 printf (" UNIQUE");
11232 val ^= DF_GNU_1_UNIQUE;
11233 }
11234 if (val != 0)
11235 printf (" %lx", val);
11236 puts ("");
11237 }
11238 }
11239 break;
11240
11241 default:
11242 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11243 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11244 = entry->d_un.d_val;
11245
11246 if (do_dynamic)
11247 {
11248 switch (filedata->file_header.e_machine)
11249 {
11250 case EM_AARCH64:
11251 dynamic_section_aarch64_val (entry);
11252 break;
11253 case EM_MIPS:
11254 case EM_MIPS_RS3_LE:
11255 dynamic_section_mips_val (filedata, entry);
11256 break;
11257 case EM_PARISC:
11258 dynamic_section_parisc_val (entry);
11259 break;
11260 case EM_IA_64:
11261 dynamic_section_ia64_val (entry);
11262 break;
11263 default:
11264 print_vma (entry->d_un.d_val, PREFIX_HEX);
11265 putchar ('\n');
11266 }
11267 }
11268 break;
11269 }
11270 }
11271
11272 return TRUE;
11273 }
11274
11275 static char *
11276 get_ver_flags (unsigned int flags)
11277 {
11278 static char buff[128];
11279
11280 buff[0] = 0;
11281
11282 if (flags == 0)
11283 return _("none");
11284
11285 if (flags & VER_FLG_BASE)
11286 strcat (buff, "BASE");
11287
11288 if (flags & VER_FLG_WEAK)
11289 {
11290 if (flags & VER_FLG_BASE)
11291 strcat (buff, " | ");
11292
11293 strcat (buff, "WEAK");
11294 }
11295
11296 if (flags & VER_FLG_INFO)
11297 {
11298 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
11299 strcat (buff, " | ");
11300
11301 strcat (buff, "INFO");
11302 }
11303
11304 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11305 {
11306 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11307 strcat (buff, " | ");
11308
11309 strcat (buff, _("<unknown>"));
11310 }
11311
11312 return buff;
11313 }
11314
11315 /* Display the contents of the version sections. */
11316
11317 static bfd_boolean
11318 process_version_sections (Filedata * filedata)
11319 {
11320 Elf_Internal_Shdr * section;
11321 unsigned i;
11322 bfd_boolean found = FALSE;
11323
11324 if (! do_version)
11325 return TRUE;
11326
11327 for (i = 0, section = filedata->section_headers;
11328 i < filedata->file_header.e_shnum;
11329 i++, section++)
11330 {
11331 switch (section->sh_type)
11332 {
11333 case SHT_GNU_verdef:
11334 {
11335 Elf_External_Verdef * edefs;
11336 unsigned long idx;
11337 unsigned long cnt;
11338 char * endbuf;
11339
11340 found = TRUE;
11341
11342 if (filedata->is_separate)
11343 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11344 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11345 section->sh_info),
11346 filedata->file_name,
11347 printable_section_name (filedata, section),
11348 section->sh_info);
11349 else
11350 printf (ngettext ("\nVersion definition section '%s' "
11351 "contains %u entry:\n",
11352 "\nVersion definition section '%s' "
11353 "contains %u entries:\n",
11354 section->sh_info),
11355 printable_section_name (filedata, section),
11356 section->sh_info);
11357
11358 printf (_(" Addr: 0x"));
11359 printf_vma (section->sh_addr);
11360 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11361 (unsigned long) section->sh_offset, section->sh_link,
11362 printable_section_name_from_index (filedata, section->sh_link));
11363
11364 edefs = (Elf_External_Verdef *)
11365 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
11366 _("version definition section"));
11367 if (!edefs)
11368 break;
11369 endbuf = (char *) edefs + section->sh_size;
11370
11371 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11372 {
11373 char * vstart;
11374 Elf_External_Verdef * edef;
11375 Elf_Internal_Verdef ent;
11376 Elf_External_Verdaux * eaux;
11377 Elf_Internal_Verdaux aux;
11378 unsigned long isum;
11379 int j;
11380
11381 vstart = ((char *) edefs) + idx;
11382 if (vstart + sizeof (*edef) > endbuf)
11383 break;
11384
11385 edef = (Elf_External_Verdef *) vstart;
11386
11387 ent.vd_version = BYTE_GET (edef->vd_version);
11388 ent.vd_flags = BYTE_GET (edef->vd_flags);
11389 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11390 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11391 ent.vd_hash = BYTE_GET (edef->vd_hash);
11392 ent.vd_aux = BYTE_GET (edef->vd_aux);
11393 ent.vd_next = BYTE_GET (edef->vd_next);
11394
11395 printf (_(" %#06lx: Rev: %d Flags: %s"),
11396 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11397
11398 printf (_(" Index: %d Cnt: %d "),
11399 ent.vd_ndx, ent.vd_cnt);
11400
11401 /* Check for overflow. */
11402 if (ent.vd_aux > (size_t) (endbuf - vstart))
11403 break;
11404
11405 vstart += ent.vd_aux;
11406
11407 if (vstart + sizeof (*eaux) > endbuf)
11408 break;
11409 eaux = (Elf_External_Verdaux *) vstart;
11410
11411 aux.vda_name = BYTE_GET (eaux->vda_name);
11412 aux.vda_next = BYTE_GET (eaux->vda_next);
11413
11414 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11415 printf (_("Name: %s\n"),
11416 GET_DYNAMIC_NAME (filedata, aux.vda_name));
11417 else
11418 printf (_("Name index: %ld\n"), aux.vda_name);
11419
11420 isum = idx + ent.vd_aux;
11421
11422 for (j = 1; j < ent.vd_cnt; j++)
11423 {
11424 if (aux.vda_next < sizeof (*eaux)
11425 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11426 {
11427 warn (_("Invalid vda_next field of %lx\n"),
11428 aux.vda_next);
11429 j = ent.vd_cnt;
11430 break;
11431 }
11432 /* Check for overflow. */
11433 if (aux.vda_next > (size_t) (endbuf - vstart))
11434 break;
11435
11436 isum += aux.vda_next;
11437 vstart += aux.vda_next;
11438
11439 if (vstart + sizeof (*eaux) > endbuf)
11440 break;
11441 eaux = (Elf_External_Verdaux *) vstart;
11442
11443 aux.vda_name = BYTE_GET (eaux->vda_name);
11444 aux.vda_next = BYTE_GET (eaux->vda_next);
11445
11446 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11447 printf (_(" %#06lx: Parent %d: %s\n"),
11448 isum, j,
11449 GET_DYNAMIC_NAME (filedata, aux.vda_name));
11450 else
11451 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
11452 isum, j, aux.vda_name);
11453 }
11454
11455 if (j < ent.vd_cnt)
11456 printf (_(" Version def aux past end of section\n"));
11457
11458 /* PR 17531:
11459 file: id:000001,src:000172+005151,op:splice,rep:2. */
11460 if (ent.vd_next < sizeof (*edef)
11461 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11462 {
11463 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11464 cnt = section->sh_info;
11465 break;
11466 }
11467 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
11468 break;
11469
11470 idx += ent.vd_next;
11471 }
11472
11473 if (cnt < section->sh_info)
11474 printf (_(" Version definition past end of section\n"));
11475
11476 free (edefs);
11477 }
11478 break;
11479
11480 case SHT_GNU_verneed:
11481 {
11482 Elf_External_Verneed * eneed;
11483 unsigned long idx;
11484 unsigned long cnt;
11485 char * endbuf;
11486
11487 found = TRUE;
11488
11489 if (filedata->is_separate)
11490 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
11491 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
11492 section->sh_info),
11493 filedata->file_name,
11494 printable_section_name (filedata, section),
11495 section->sh_info);
11496 else
11497 printf (ngettext ("\nVersion needs section '%s' "
11498 "contains %u entry:\n",
11499 "\nVersion needs section '%s' "
11500 "contains %u entries:\n",
11501 section->sh_info),
11502 printable_section_name (filedata, section),
11503 section->sh_info);
11504
11505 printf (_(" Addr: 0x"));
11506 printf_vma (section->sh_addr);
11507 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11508 (unsigned long) section->sh_offset, section->sh_link,
11509 printable_section_name_from_index (filedata, section->sh_link));
11510
11511 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
11512 section->sh_offset, 1,
11513 section->sh_size,
11514 _("Version Needs section"));
11515 if (!eneed)
11516 break;
11517 endbuf = (char *) eneed + section->sh_size;
11518
11519 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11520 {
11521 Elf_External_Verneed * entry;
11522 Elf_Internal_Verneed ent;
11523 unsigned long isum;
11524 int j;
11525 char * vstart;
11526
11527 vstart = ((char *) eneed) + idx;
11528 if (vstart + sizeof (*entry) > endbuf)
11529 break;
11530
11531 entry = (Elf_External_Verneed *) vstart;
11532
11533 ent.vn_version = BYTE_GET (entry->vn_version);
11534 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
11535 ent.vn_file = BYTE_GET (entry->vn_file);
11536 ent.vn_aux = BYTE_GET (entry->vn_aux);
11537 ent.vn_next = BYTE_GET (entry->vn_next);
11538
11539 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
11540
11541 if (VALID_DYNAMIC_NAME (filedata, ent.vn_file))
11542 printf (_(" File: %s"),
11543 GET_DYNAMIC_NAME (filedata, ent.vn_file));
11544 else
11545 printf (_(" File: %lx"), ent.vn_file);
11546
11547 printf (_(" Cnt: %d\n"), ent.vn_cnt);
11548
11549 /* Check for overflow. */
11550 if (ent.vn_aux > (size_t) (endbuf - vstart))
11551 break;
11552 vstart += ent.vn_aux;
11553
11554 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11555 {
11556 Elf_External_Vernaux * eaux;
11557 Elf_Internal_Vernaux aux;
11558
11559 if (vstart + sizeof (*eaux) > endbuf)
11560 break;
11561 eaux = (Elf_External_Vernaux *) vstart;
11562
11563 aux.vna_hash = BYTE_GET (eaux->vna_hash);
11564 aux.vna_flags = BYTE_GET (eaux->vna_flags);
11565 aux.vna_other = BYTE_GET (eaux->vna_other);
11566 aux.vna_name = BYTE_GET (eaux->vna_name);
11567 aux.vna_next = BYTE_GET (eaux->vna_next);
11568
11569 if (VALID_DYNAMIC_NAME (filedata, aux.vna_name))
11570 printf (_(" %#06lx: Name: %s"),
11571 isum, GET_DYNAMIC_NAME (filedata, aux.vna_name));
11572 else
11573 printf (_(" %#06lx: Name index: %lx"),
11574 isum, aux.vna_name);
11575
11576 printf (_(" Flags: %s Version: %d\n"),
11577 get_ver_flags (aux.vna_flags), aux.vna_other);
11578
11579 if (aux.vna_next < sizeof (*eaux)
11580 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
11581 {
11582 warn (_("Invalid vna_next field of %lx\n"),
11583 aux.vna_next);
11584 j = ent.vn_cnt;
11585 break;
11586 }
11587 /* Check for overflow. */
11588 if (aux.vna_next > (size_t) (endbuf - vstart))
11589 break;
11590 isum += aux.vna_next;
11591 vstart += aux.vna_next;
11592 }
11593
11594 if (j < ent.vn_cnt)
11595 warn (_("Missing Version Needs auxiliary information\n"));
11596
11597 if (ent.vn_next < sizeof (*entry)
11598 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
11599 {
11600 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
11601 cnt = section->sh_info;
11602 break;
11603 }
11604 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
11605 break;
11606 idx += ent.vn_next;
11607 }
11608
11609 if (cnt < section->sh_info)
11610 warn (_("Missing Version Needs information\n"));
11611
11612 free (eneed);
11613 }
11614 break;
11615
11616 case SHT_GNU_versym:
11617 {
11618 Elf_Internal_Shdr * link_section;
11619 size_t total;
11620 unsigned int cnt;
11621 unsigned char * edata;
11622 unsigned short * data;
11623 char * strtab;
11624 Elf_Internal_Sym * symbols;
11625 Elf_Internal_Shdr * string_sec;
11626 unsigned long num_syms;
11627 long off;
11628
11629 if (section->sh_link >= filedata->file_header.e_shnum)
11630 break;
11631
11632 link_section = filedata->section_headers + section->sh_link;
11633 total = section->sh_size / sizeof (Elf_External_Versym);
11634
11635 if (link_section->sh_link >= filedata->file_header.e_shnum)
11636 break;
11637
11638 found = TRUE;
11639
11640 symbols = GET_ELF_SYMBOLS (filedata, link_section, & num_syms);
11641 if (symbols == NULL)
11642 break;
11643
11644 string_sec = filedata->section_headers + link_section->sh_link;
11645
11646 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
11647 string_sec->sh_size,
11648 _("version string table"));
11649 if (!strtab)
11650 {
11651 free (symbols);
11652 break;
11653 }
11654
11655 if (filedata->is_separate)
11656 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
11657 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
11658 total),
11659 filedata->file_name,
11660 printable_section_name (filedata, section),
11661 (unsigned long) total);
11662 else
11663 printf (ngettext ("\nVersion symbols section '%s' "
11664 "contains %lu entry:\n",
11665 "\nVersion symbols section '%s' "
11666 "contains %lu entries:\n",
11667 total),
11668 printable_section_name (filedata, section),
11669 (unsigned long) total);
11670
11671 printf (_(" Addr: 0x"));
11672 printf_vma (section->sh_addr);
11673 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11674 (unsigned long) section->sh_offset, section->sh_link,
11675 printable_section_name (filedata, link_section));
11676
11677 off = offset_from_vma (filedata,
11678 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11679 total * sizeof (short));
11680 edata = (unsigned char *) get_data (NULL, filedata, off,
11681 sizeof (short), total,
11682 _("version symbol data"));
11683 if (!edata)
11684 {
11685 free (strtab);
11686 free (symbols);
11687 break;
11688 }
11689
11690 data = (short unsigned int *) cmalloc (total, sizeof (short));
11691
11692 for (cnt = total; cnt --;)
11693 data[cnt] = byte_get (edata + cnt * sizeof (short),
11694 sizeof (short));
11695
11696 free (edata);
11697
11698 for (cnt = 0; cnt < total; cnt += 4)
11699 {
11700 int j, nn;
11701 char *name;
11702 char *invalid = _("*invalid*");
11703
11704 printf (" %03x:", cnt);
11705
11706 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
11707 switch (data[cnt + j])
11708 {
11709 case 0:
11710 fputs (_(" 0 (*local*) "), stdout);
11711 break;
11712
11713 case 1:
11714 fputs (_(" 1 (*global*) "), stdout);
11715 break;
11716
11717 default:
11718 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
11719 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
11720
11721 /* If this index value is greater than the size of the symbols
11722 array, break to avoid an out-of-bounds read. */
11723 if ((unsigned long)(cnt + j) >= num_syms)
11724 {
11725 warn (_("invalid index into symbol array\n"));
11726 break;
11727 }
11728
11729 name = NULL;
11730 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11731 {
11732 Elf_Internal_Verneed ivn;
11733 unsigned long offset;
11734
11735 offset = offset_from_vma
11736 (filedata,
11737 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11738 sizeof (Elf_External_Verneed));
11739
11740 do
11741 {
11742 Elf_Internal_Vernaux ivna;
11743 Elf_External_Verneed evn;
11744 Elf_External_Vernaux evna;
11745 unsigned long a_off;
11746
11747 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
11748 _("version need")) == NULL)
11749 break;
11750
11751 ivn.vn_aux = BYTE_GET (evn.vn_aux);
11752 ivn.vn_next = BYTE_GET (evn.vn_next);
11753
11754 a_off = offset + ivn.vn_aux;
11755
11756 do
11757 {
11758 if (get_data (&evna, filedata, a_off, sizeof (evna),
11759 1, _("version need aux (2)")) == NULL)
11760 {
11761 ivna.vna_next = 0;
11762 ivna.vna_other = 0;
11763 }
11764 else
11765 {
11766 ivna.vna_next = BYTE_GET (evna.vna_next);
11767 ivna.vna_other = BYTE_GET (evna.vna_other);
11768 }
11769
11770 a_off += ivna.vna_next;
11771 }
11772 while (ivna.vna_other != data[cnt + j]
11773 && ivna.vna_next != 0);
11774
11775 if (ivna.vna_other == data[cnt + j])
11776 {
11777 ivna.vna_name = BYTE_GET (evna.vna_name);
11778
11779 if (ivna.vna_name >= string_sec->sh_size)
11780 name = invalid;
11781 else
11782 name = strtab + ivna.vna_name;
11783 break;
11784 }
11785
11786 offset += ivn.vn_next;
11787 }
11788 while (ivn.vn_next);
11789 }
11790
11791 if (data[cnt + j] != 0x8001
11792 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
11793 {
11794 Elf_Internal_Verdef ivd;
11795 Elf_External_Verdef evd;
11796 unsigned long offset;
11797
11798 offset = offset_from_vma
11799 (filedata,
11800 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
11801 sizeof evd);
11802
11803 do
11804 {
11805 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
11806 _("version def")) == NULL)
11807 {
11808 ivd.vd_next = 0;
11809 /* PR 17531: file: 046-1082287-0.004. */
11810 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
11811 break;
11812 }
11813 else
11814 {
11815 ivd.vd_next = BYTE_GET (evd.vd_next);
11816 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
11817 }
11818
11819 offset += ivd.vd_next;
11820 }
11821 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
11822 && ivd.vd_next != 0);
11823
11824 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
11825 {
11826 Elf_External_Verdaux evda;
11827 Elf_Internal_Verdaux ivda;
11828
11829 ivd.vd_aux = BYTE_GET (evd.vd_aux);
11830
11831 if (get_data (&evda, filedata,
11832 offset - ivd.vd_next + ivd.vd_aux,
11833 sizeof (evda), 1,
11834 _("version def aux")) == NULL)
11835 break;
11836
11837 ivda.vda_name = BYTE_GET (evda.vda_name);
11838
11839 if (ivda.vda_name >= string_sec->sh_size)
11840 name = invalid;
11841 else if (name != NULL && name != invalid)
11842 name = _("*both*");
11843 else
11844 name = strtab + ivda.vda_name;
11845 }
11846 }
11847 if (name != NULL)
11848 nn += printf ("(%s%-*s",
11849 name,
11850 12 - (int) strlen (name),
11851 ")");
11852
11853 if (nn < 18)
11854 printf ("%*c", 18 - nn, ' ');
11855 }
11856
11857 putchar ('\n');
11858 }
11859
11860 free (data);
11861 free (strtab);
11862 free (symbols);
11863 }
11864 break;
11865
11866 default:
11867 break;
11868 }
11869 }
11870
11871 if (! found)
11872 {
11873 if (filedata->is_separate)
11874 printf (_("\nNo version information found in linked file '%s'.\n"),
11875 filedata->file_name);
11876 else
11877 printf (_("\nNo version information found in this file.\n"));
11878 }
11879
11880 return TRUE;
11881 }
11882
11883 static const char *
11884 get_symbol_binding (Filedata * filedata, unsigned int binding)
11885 {
11886 static char buff[64];
11887
11888 switch (binding)
11889 {
11890 case STB_LOCAL: return "LOCAL";
11891 case STB_GLOBAL: return "GLOBAL";
11892 case STB_WEAK: return "WEAK";
11893 default:
11894 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
11895 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
11896 binding);
11897 else if (binding >= STB_LOOS && binding <= STB_HIOS)
11898 {
11899 if (binding == STB_GNU_UNIQUE
11900 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
11901 return "UNIQUE";
11902 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
11903 }
11904 else
11905 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
11906 return buff;
11907 }
11908 }
11909
11910 static const char *
11911 get_symbol_type (Filedata * filedata, unsigned int type)
11912 {
11913 static char buff[64];
11914
11915 switch (type)
11916 {
11917 case STT_NOTYPE: return "NOTYPE";
11918 case STT_OBJECT: return "OBJECT";
11919 case STT_FUNC: return "FUNC";
11920 case STT_SECTION: return "SECTION";
11921 case STT_FILE: return "FILE";
11922 case STT_COMMON: return "COMMON";
11923 case STT_TLS: return "TLS";
11924 case STT_RELC: return "RELC";
11925 case STT_SRELC: return "SRELC";
11926 default:
11927 if (type >= STT_LOPROC && type <= STT_HIPROC)
11928 {
11929 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
11930 return "THUMB_FUNC";
11931
11932 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
11933 return "REGISTER";
11934
11935 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
11936 return "PARISC_MILLI";
11937
11938 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
11939 }
11940 else if (type >= STT_LOOS && type <= STT_HIOS)
11941 {
11942 if (filedata->file_header.e_machine == EM_PARISC)
11943 {
11944 if (type == STT_HP_OPAQUE)
11945 return "HP_OPAQUE";
11946 if (type == STT_HP_STUB)
11947 return "HP_STUB";
11948 }
11949
11950 if (type == STT_GNU_IFUNC
11951 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
11952 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
11953 return "IFUNC";
11954
11955 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
11956 }
11957 else
11958 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
11959 return buff;
11960 }
11961 }
11962
11963 static const char *
11964 get_symbol_visibility (unsigned int visibility)
11965 {
11966 switch (visibility)
11967 {
11968 case STV_DEFAULT: return "DEFAULT";
11969 case STV_INTERNAL: return "INTERNAL";
11970 case STV_HIDDEN: return "HIDDEN";
11971 case STV_PROTECTED: return "PROTECTED";
11972 default:
11973 error (_("Unrecognized visibility value: %u\n"), visibility);
11974 return _("<unknown>");
11975 }
11976 }
11977
11978 static const char *
11979 get_alpha_symbol_other (unsigned int other)
11980 {
11981 switch (other)
11982 {
11983 case STO_ALPHA_NOPV: return "NOPV";
11984 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
11985 default:
11986 error (_("Unrecognized alpha specific other value: %u\n"), other);
11987 return _("<unknown>");
11988 }
11989 }
11990
11991 static const char *
11992 get_solaris_symbol_visibility (unsigned int visibility)
11993 {
11994 switch (visibility)
11995 {
11996 case 4: return "EXPORTED";
11997 case 5: return "SINGLETON";
11998 case 6: return "ELIMINATE";
11999 default: return get_symbol_visibility (visibility);
12000 }
12001 }
12002
12003 static const char *
12004 get_aarch64_symbol_other (unsigned int other)
12005 {
12006 static char buf[32];
12007
12008 if (other & STO_AARCH64_VARIANT_PCS)
12009 {
12010 other &= ~STO_AARCH64_VARIANT_PCS;
12011 if (other == 0)
12012 return "VARIANT_PCS";
12013 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12014 return buf;
12015 }
12016 return NULL;
12017 }
12018
12019 static const char *
12020 get_mips_symbol_other (unsigned int other)
12021 {
12022 switch (other)
12023 {
12024 case STO_OPTIONAL: return "OPTIONAL";
12025 case STO_MIPS_PLT: return "MIPS PLT";
12026 case STO_MIPS_PIC: return "MIPS PIC";
12027 case STO_MICROMIPS: return "MICROMIPS";
12028 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12029 case STO_MIPS16: return "MIPS16";
12030 default: return NULL;
12031 }
12032 }
12033
12034 static const char *
12035 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12036 {
12037 if (is_ia64_vms (filedata))
12038 {
12039 static char res[32];
12040
12041 res[0] = 0;
12042
12043 /* Function types is for images and .STB files only. */
12044 switch (filedata->file_header.e_type)
12045 {
12046 case ET_DYN:
12047 case ET_EXEC:
12048 switch (VMS_ST_FUNC_TYPE (other))
12049 {
12050 case VMS_SFT_CODE_ADDR:
12051 strcat (res, " CA");
12052 break;
12053 case VMS_SFT_SYMV_IDX:
12054 strcat (res, " VEC");
12055 break;
12056 case VMS_SFT_FD:
12057 strcat (res, " FD");
12058 break;
12059 case VMS_SFT_RESERVE:
12060 strcat (res, " RSV");
12061 break;
12062 default:
12063 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12064 VMS_ST_FUNC_TYPE (other));
12065 strcat (res, " <unknown>");
12066 break;
12067 }
12068 break;
12069 default:
12070 break;
12071 }
12072 switch (VMS_ST_LINKAGE (other))
12073 {
12074 case VMS_STL_IGNORE:
12075 strcat (res, " IGN");
12076 break;
12077 case VMS_STL_RESERVE:
12078 strcat (res, " RSV");
12079 break;
12080 case VMS_STL_STD:
12081 strcat (res, " STD");
12082 break;
12083 case VMS_STL_LNK:
12084 strcat (res, " LNK");
12085 break;
12086 default:
12087 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12088 VMS_ST_LINKAGE (other));
12089 strcat (res, " <unknown>");
12090 break;
12091 }
12092
12093 if (res[0] != 0)
12094 return res + 1;
12095 else
12096 return res;
12097 }
12098 return NULL;
12099 }
12100
12101 static const char *
12102 get_ppc64_symbol_other (unsigned int other)
12103 {
12104 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12105 return NULL;
12106
12107 other >>= STO_PPC64_LOCAL_BIT;
12108 if (other <= 6)
12109 {
12110 static char buf[64];
12111 if (other >= 2)
12112 other = ppc64_decode_local_entry (other);
12113 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12114 return buf;
12115 }
12116 return NULL;
12117 }
12118
12119 static const char *
12120 get_symbol_other (Filedata * filedata, unsigned int other)
12121 {
12122 const char * result = NULL;
12123 static char buff [64];
12124
12125 if (other == 0)
12126 return "";
12127
12128 switch (filedata->file_header.e_machine)
12129 {
12130 case EM_ALPHA:
12131 result = get_alpha_symbol_other (other);
12132 break;
12133 case EM_AARCH64:
12134 result = get_aarch64_symbol_other (other);
12135 break;
12136 case EM_MIPS:
12137 result = get_mips_symbol_other (other);
12138 break;
12139 case EM_IA_64:
12140 result = get_ia64_symbol_other (filedata, other);
12141 break;
12142 case EM_PPC64:
12143 result = get_ppc64_symbol_other (other);
12144 break;
12145 default:
12146 result = NULL;
12147 break;
12148 }
12149
12150 if (result)
12151 return result;
12152
12153 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12154 return buff;
12155 }
12156
12157 static const char *
12158 get_symbol_index_type (Filedata * filedata, unsigned int type)
12159 {
12160 static char buff[32];
12161
12162 switch (type)
12163 {
12164 case SHN_UNDEF: return "UND";
12165 case SHN_ABS: return "ABS";
12166 case SHN_COMMON: return "COM";
12167 default:
12168 if (type == SHN_IA_64_ANSI_COMMON
12169 && filedata->file_header.e_machine == EM_IA_64
12170 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12171 return "ANSI_COM";
12172 else if ((filedata->file_header.e_machine == EM_X86_64
12173 || filedata->file_header.e_machine == EM_L1OM
12174 || filedata->file_header.e_machine == EM_K1OM)
12175 && type == SHN_X86_64_LCOMMON)
12176 return "LARGE_COM";
12177 else if ((type == SHN_MIPS_SCOMMON
12178 && filedata->file_header.e_machine == EM_MIPS)
12179 || (type == SHN_TIC6X_SCOMMON
12180 && filedata->file_header.e_machine == EM_TI_C6000))
12181 return "SCOM";
12182 else if (type == SHN_MIPS_SUNDEFINED
12183 && filedata->file_header.e_machine == EM_MIPS)
12184 return "SUND";
12185 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12186 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12187 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12188 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12189 else if (type >= SHN_LORESERVE)
12190 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12191 else if (filedata->file_header.e_shnum != 0
12192 && type >= filedata->file_header.e_shnum)
12193 sprintf (buff, _("bad section index[%3d]"), type);
12194 else
12195 sprintf (buff, "%3d", type);
12196 break;
12197 }
12198
12199 return buff;
12200 }
12201
12202 static const char *
12203 get_symbol_version_string (Filedata * filedata,
12204 bfd_boolean is_dynsym,
12205 const char * strtab,
12206 unsigned long int strtab_size,
12207 unsigned int si,
12208 Elf_Internal_Sym * psym,
12209 enum versioned_symbol_info * sym_info,
12210 unsigned short * vna_other)
12211 {
12212 unsigned char data[2];
12213 unsigned short vers_data;
12214 unsigned long offset;
12215 unsigned short max_vd_ndx;
12216
12217 if (!is_dynsym
12218 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12219 return NULL;
12220
12221 offset = offset_from_vma (filedata,
12222 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12223 sizeof data + si * sizeof (vers_data));
12224
12225 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12226 sizeof (data), 1, _("version data")) == NULL)
12227 return NULL;
12228
12229 vers_data = byte_get (data, 2);
12230
12231 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12232 return NULL;
12233
12234 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12235 max_vd_ndx = 0;
12236
12237 /* Usually we'd only see verdef for defined symbols, and verneed for
12238 undefined symbols. However, symbols defined by the linker in
12239 .dynbss for variables copied from a shared library in order to
12240 avoid text relocations are defined yet have verneed. We could
12241 use a heuristic to detect the special case, for example, check
12242 for verneed first on symbols defined in SHT_NOBITS sections, but
12243 it is simpler and more reliable to just look for both verdef and
12244 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12245
12246 if (psym->st_shndx != SHN_UNDEF
12247 && vers_data != 0x8001
12248 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12249 {
12250 Elf_Internal_Verdef ivd;
12251 Elf_Internal_Verdaux ivda;
12252 Elf_External_Verdaux evda;
12253 unsigned long off;
12254
12255 off = offset_from_vma (filedata,
12256 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12257 sizeof (Elf_External_Verdef));
12258
12259 do
12260 {
12261 Elf_External_Verdef evd;
12262
12263 if (get_data (&evd, filedata, off, sizeof (evd), 1,
12264 _("version def")) == NULL)
12265 {
12266 ivd.vd_ndx = 0;
12267 ivd.vd_aux = 0;
12268 ivd.vd_next = 0;
12269 ivd.vd_flags = 0;
12270 }
12271 else
12272 {
12273 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12274 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12275 ivd.vd_next = BYTE_GET (evd.vd_next);
12276 ivd.vd_flags = BYTE_GET (evd.vd_flags);
12277 }
12278
12279 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12280 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12281
12282 off += ivd.vd_next;
12283 }
12284 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
12285
12286 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12287 {
12288 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
12289 return NULL;
12290
12291 off -= ivd.vd_next;
12292 off += ivd.vd_aux;
12293
12294 if (get_data (&evda, filedata, off, sizeof (evda), 1,
12295 _("version def aux")) != NULL)
12296 {
12297 ivda.vda_name = BYTE_GET (evda.vda_name);
12298
12299 if (psym->st_name != ivda.vda_name)
12300 return (ivda.vda_name < strtab_size
12301 ? strtab + ivda.vda_name : _("<corrupt>"));
12302 }
12303 }
12304 }
12305
12306 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12307 {
12308 Elf_External_Verneed evn;
12309 Elf_Internal_Verneed ivn;
12310 Elf_Internal_Vernaux ivna;
12311
12312 offset = offset_from_vma (filedata,
12313 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12314 sizeof evn);
12315 do
12316 {
12317 unsigned long vna_off;
12318
12319 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12320 _("version need")) == NULL)
12321 {
12322 ivna.vna_next = 0;
12323 ivna.vna_other = 0;
12324 ivna.vna_name = 0;
12325 break;
12326 }
12327
12328 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12329 ivn.vn_next = BYTE_GET (evn.vn_next);
12330
12331 vna_off = offset + ivn.vn_aux;
12332
12333 do
12334 {
12335 Elf_External_Vernaux evna;
12336
12337 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
12338 _("version need aux (3)")) == NULL)
12339 {
12340 ivna.vna_next = 0;
12341 ivna.vna_other = 0;
12342 ivna.vna_name = 0;
12343 }
12344 else
12345 {
12346 ivna.vna_other = BYTE_GET (evna.vna_other);
12347 ivna.vna_next = BYTE_GET (evna.vna_next);
12348 ivna.vna_name = BYTE_GET (evna.vna_name);
12349 }
12350
12351 vna_off += ivna.vna_next;
12352 }
12353 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
12354
12355 if (ivna.vna_other == vers_data)
12356 break;
12357
12358 offset += ivn.vn_next;
12359 }
12360 while (ivn.vn_next != 0);
12361
12362 if (ivna.vna_other == vers_data)
12363 {
12364 *sym_info = symbol_undefined;
12365 *vna_other = ivna.vna_other;
12366 return (ivna.vna_name < strtab_size
12367 ? strtab + ivna.vna_name : _("<corrupt>"));
12368 }
12369 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12370 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12371 return _("<corrupt>");
12372 }
12373 return NULL;
12374 }
12375
12376 static void
12377 print_dynamic_symbol (Filedata *filedata, unsigned long si,
12378 Elf_Internal_Sym *symtab,
12379 Elf_Internal_Shdr *section,
12380 char *strtab, size_t strtab_size)
12381 {
12382 const char *version_string;
12383 enum versioned_symbol_info sym_info;
12384 unsigned short vna_other;
12385 Elf_Internal_Sym *psym = symtab + si;
12386
12387 printf ("%6ld: ", si);
12388 print_vma (psym->st_value, LONG_HEX);
12389 putchar (' ');
12390 print_vma (psym->st_size, DEC_5);
12391 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12392 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12393 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12394 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12395 else
12396 {
12397 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
12398
12399 printf (" %-7s", get_symbol_visibility (vis));
12400 /* Check to see if any other bits in the st_other field are set.
12401 Note - displaying this information disrupts the layout of the
12402 table being generated, but for the moment this case is very rare. */
12403 if (psym->st_other ^ vis)
12404 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
12405 }
12406 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
12407
12408 bfd_boolean is_valid = VALID_SYMBOL_NAME (strtab, strtab_size,
12409 psym->st_name);
12410 const char * sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
12411
12412 version_string
12413 = get_symbol_version_string (filedata,
12414 (section == NULL
12415 || section->sh_type == SHT_DYNSYM),
12416 strtab, strtab_size, si,
12417 psym, &sym_info, &vna_other);
12418
12419 int len_avail = 21;
12420 if (! do_wide && version_string != NULL)
12421 {
12422 char buffer[16];
12423
12424 len_avail -= 1 + strlen (version_string);
12425
12426 if (sym_info == symbol_undefined)
12427 len_avail -= sprintf (buffer," (%d)", vna_other);
12428 else if (sym_info != symbol_hidden)
12429 len_avail -= 1;
12430 }
12431
12432 print_symbol (len_avail, sstr);
12433
12434 if (version_string)
12435 {
12436 if (sym_info == symbol_undefined)
12437 printf ("@%s (%d)", version_string, vna_other);
12438 else
12439 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12440 version_string);
12441 }
12442
12443 putchar ('\n');
12444
12445 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12446 && section != NULL
12447 && si >= section->sh_info
12448 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
12449 && filedata->file_header.e_machine != EM_MIPS
12450 /* Solaris binaries have been found to violate this requirement as
12451 well. Not sure if this is a bug or an ABI requirement. */
12452 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12453 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12454 si, printable_section_name (filedata, section), section->sh_info);
12455 }
12456
12457 static const char *
12458 get_lto_kind (unsigned int kind)
12459 {
12460 switch (kind)
12461 {
12462 case 0: return "DEF";
12463 case 1: return "WEAKDEF";
12464 case 2: return "UNDEF";
12465 case 3: return "WEAKUNDEF";
12466 case 4: return "COMMON";
12467 default:
12468 break;
12469 }
12470
12471 static char buffer[30];
12472 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
12473 sprintf (buffer, "<unknown: %u>", kind);
12474 return buffer;
12475 }
12476
12477 static const char *
12478 get_lto_visibility (unsigned int visibility)
12479 {
12480 switch (visibility)
12481 {
12482 case 0: return "DEFAULT";
12483 case 1: return "PROTECTED";
12484 case 2: return "INTERNAL";
12485 case 3: return "HIDDEN";
12486 default:
12487 break;
12488 }
12489
12490 static char buffer[30];
12491 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
12492 sprintf (buffer, "<unknown: %u>", visibility);
12493 return buffer;
12494 }
12495
12496 static const char *
12497 get_lto_sym_type (unsigned int sym_type)
12498 {
12499 switch (sym_type)
12500 {
12501 case 0: return "UNKNOWN";
12502 case 1: return "FUNCTION";
12503 case 2: return "VARIABLE";
12504 default:
12505 break;
12506 }
12507
12508 static char buffer[30];
12509 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
12510 sprintf (buffer, "<unknown: %u>", sym_type);
12511 return buffer;
12512 }
12513
12514 /* Display an LTO format symbol table.
12515 FIXME: The format of LTO symbol tables is not formalized.
12516 So this code could need changing in the future. */
12517
12518 static bfd_boolean
12519 display_lto_symtab (Filedata * filedata,
12520 Elf_Internal_Shdr * section)
12521 {
12522 if (section->sh_size == 0)
12523 {
12524 if (filedata->is_separate)
12525 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
12526 printable_section_name (filedata, section),
12527 filedata->file_name);
12528 else
12529 printf (_("\nLTO Symbol table '%s' is empty!\n"),
12530 printable_section_name (filedata, section));
12531
12532 return TRUE;
12533 }
12534
12535 if (section->sh_size > filedata->file_size)
12536 {
12537 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
12538 printable_section_name (filedata, section),
12539 (unsigned long) section->sh_size);
12540 return FALSE;
12541 }
12542
12543 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
12544 section->sh_size, 1, _("LTO symbols"));
12545 if (alloced_data == NULL)
12546 return FALSE;
12547
12548 /* Look for extended data for the symbol table. */
12549 Elf_Internal_Shdr * ext;
12550 void * ext_data_orig = NULL;
12551 char * ext_data = NULL;
12552 char * ext_data_end = NULL;
12553 char * ext_name = NULL;
12554
12555 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
12556 SECTION_NAME (section) + sizeof (".gnu.lto_.symtab.") - 1) > 0
12557 && ext_name != NULL /* Paranoia. */
12558 && (ext = find_section (filedata, ext_name)) != NULL)
12559 {
12560 if (ext->sh_size < 3)
12561 error (_("LTO Symbol extension table '%s' is empty!\n"),
12562 printable_section_name (filedata, ext));
12563 else
12564 {
12565 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
12566 ext->sh_size, 1,
12567 _("LTO ext symbol data"));
12568 if (ext_data != NULL)
12569 {
12570 ext_data_end = ext_data + ext->sh_size;
12571 if (* ext_data++ != 1)
12572 error (_("Unexpected version number in symbol extension table\n"));
12573 }
12574 }
12575 }
12576
12577 const unsigned char * data = (const unsigned char *) alloced_data;
12578 const unsigned char * end = data + section->sh_size;
12579
12580 if (filedata->is_separate)
12581 printf (_("\nIn linked file '%s': "), filedata->file_name);
12582 else
12583 printf ("\n");
12584
12585 if (ext_data_orig != NULL)
12586 {
12587 if (do_wide)
12588 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
12589 printable_section_name (filedata, section),
12590 printable_section_name (filedata, ext));
12591 else
12592 {
12593 printf (_("LTO Symbol table '%s'\n"),
12594 printable_section_name (filedata, section));
12595 printf (_(" and extension table '%s' contain:\n"),
12596 printable_section_name (filedata, ext));
12597 }
12598 }
12599 else
12600 printf (_("LTO Symbol table '%s' contains:\n"),
12601 printable_section_name (filedata, section));
12602
12603 /* FIXME: Add a wide version. */
12604 if (ext_data_orig != NULL)
12605 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
12606 else
12607 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
12608
12609 /* FIXME: We do not handle style prefixes. */
12610
12611 while (data < end)
12612 {
12613 const unsigned char * sym_name = data;
12614 data += strnlen ((const char *) sym_name, end - data) + 1;
12615 if (data >= end)
12616 goto fail;
12617
12618 const unsigned char * comdat_key = data;
12619 data += strnlen ((const char *) comdat_key, end - data) + 1;
12620 if (data >= end)
12621 goto fail;
12622
12623 if (data + 2 + 8 + 4 > end)
12624 goto fail;
12625
12626 unsigned int kind = *data++;
12627 unsigned int visibility = *data++;
12628
12629 elf_vma size = byte_get (data, 8);
12630 data += 8;
12631
12632 elf_vma slot = byte_get (data, 4);
12633 data += 4;
12634
12635 if (ext_data != NULL)
12636 {
12637 if (ext_data < (ext_data_end - 1))
12638 {
12639 unsigned int sym_type = * ext_data ++;
12640 unsigned int sec_kind = * ext_data ++;
12641
12642 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
12643 * comdat_key == 0 ? "-" : (char *) comdat_key,
12644 get_lto_kind (kind),
12645 get_lto_visibility (visibility),
12646 (long) size,
12647 (long) slot,
12648 get_lto_sym_type (sym_type),
12649 (long) sec_kind);
12650 print_symbol (6, (const char *) sym_name);
12651 }
12652 else
12653 {
12654 error (_("Ran out of LTO symbol extension data\n"));
12655 ext_data = NULL;
12656 /* FIXME: return FAIL result ? */
12657 }
12658 }
12659 else
12660 {
12661 printf (" %10s %10s %11s %08lx %08lx _",
12662 * comdat_key == 0 ? "-" : (char *) comdat_key,
12663 get_lto_kind (kind),
12664 get_lto_visibility (visibility),
12665 (long) size,
12666 (long) slot);
12667 print_symbol (21, (const char *) sym_name);
12668 }
12669 putchar ('\n');
12670 }
12671
12672 if (ext_data != NULL && ext_data < ext_data_end)
12673 {
12674 error (_("Data remains in the LTO symbol extension table\n"));
12675 goto fail;
12676 }
12677
12678 free (alloced_data);
12679 free (ext_data_orig);
12680 free (ext_name);
12681 return TRUE;
12682
12683 fail:
12684 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
12685 free (alloced_data);
12686 free (ext_data_orig);
12687 free (ext_name);
12688 return FALSE;
12689 }
12690
12691 /* Display LTO symbol tables. */
12692
12693 static bfd_boolean
12694 process_lto_symbol_tables (Filedata * filedata)
12695 {
12696 Elf_Internal_Shdr * section;
12697 unsigned int i;
12698 bfd_boolean res = TRUE;
12699
12700 if (!do_lto_syms)
12701 return TRUE;
12702
12703 if (filedata->section_headers == NULL)
12704 return TRUE;
12705
12706 for (i = 0, section = filedata->section_headers;
12707 i < filedata->file_header.e_shnum;
12708 i++, section++)
12709 if (SECTION_NAME_VALID (section)
12710 && CONST_STRNEQ (SECTION_NAME (section), ".gnu.lto_.symtab."))
12711 res &= display_lto_symtab (filedata, section);
12712
12713 return res;
12714 }
12715
12716 /* Dump the symbol table. */
12717
12718 static bfd_boolean
12719 process_symbol_table (Filedata * filedata)
12720 {
12721 Elf_Internal_Shdr * section;
12722
12723 if (!do_syms && !do_dyn_syms && !do_histogram)
12724 return TRUE;
12725
12726 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
12727 && do_syms
12728 && do_using_dynamic
12729 && filedata->dynamic_strings != NULL
12730 && filedata->dynamic_symbols != NULL)
12731 {
12732 unsigned long si;
12733
12734 if (filedata->is_separate)
12735 {
12736 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
12737 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
12738 filedata->num_dynamic_syms),
12739 filedata->file_name,
12740 filedata->num_dynamic_syms);
12741 }
12742 else
12743 {
12744 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
12745 "\nSymbol table for image contains %lu entries:\n",
12746 filedata->num_dynamic_syms),
12747 filedata->num_dynamic_syms);
12748 }
12749 if (is_32bit_elf)
12750 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12751 else
12752 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12753
12754 for (si = 0; si < filedata->num_dynamic_syms; si++)
12755 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
12756 filedata->dynamic_strings,
12757 filedata->dynamic_strings_length);
12758 }
12759 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
12760 && filedata->section_headers != NULL)
12761 {
12762 unsigned int i;
12763
12764 for (i = 0, section = filedata->section_headers;
12765 i < filedata->file_header.e_shnum;
12766 i++, section++)
12767 {
12768 char * strtab = NULL;
12769 unsigned long int strtab_size = 0;
12770 Elf_Internal_Sym * symtab;
12771 unsigned long si, num_syms;
12772
12773 if ((section->sh_type != SHT_SYMTAB
12774 && section->sh_type != SHT_DYNSYM)
12775 || (!do_syms
12776 && section->sh_type == SHT_SYMTAB))
12777 continue;
12778
12779 if (section->sh_entsize == 0)
12780 {
12781 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
12782 printable_section_name (filedata, section));
12783 continue;
12784 }
12785
12786 num_syms = section->sh_size / section->sh_entsize;
12787
12788 if (filedata->is_separate)
12789 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
12790 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
12791 num_syms),
12792 filedata->file_name,
12793 printable_section_name (filedata, section),
12794 num_syms);
12795 else
12796 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
12797 "\nSymbol table '%s' contains %lu entries:\n",
12798 num_syms),
12799 printable_section_name (filedata, section),
12800 num_syms);
12801
12802 if (is_32bit_elf)
12803 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12804 else
12805 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12806
12807 symtab = GET_ELF_SYMBOLS (filedata, section, & num_syms);
12808 if (symtab == NULL)
12809 continue;
12810
12811 if (section->sh_link == filedata->file_header.e_shstrndx)
12812 {
12813 strtab = filedata->string_table;
12814 strtab_size = filedata->string_table_length;
12815 }
12816 else if (section->sh_link < filedata->file_header.e_shnum)
12817 {
12818 Elf_Internal_Shdr * string_sec;
12819
12820 string_sec = filedata->section_headers + section->sh_link;
12821
12822 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
12823 1, string_sec->sh_size,
12824 _("string table"));
12825 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
12826 }
12827
12828 for (si = 0; si < num_syms; si++)
12829 print_dynamic_symbol (filedata, si, symtab, section,
12830 strtab, strtab_size);
12831
12832 free (symtab);
12833 if (strtab != filedata->string_table)
12834 free (strtab);
12835 }
12836 }
12837 else if (do_syms)
12838 printf
12839 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
12840
12841 if (do_histogram && filedata->buckets != NULL)
12842 {
12843 unsigned long * lengths;
12844 unsigned long * counts;
12845 unsigned long hn;
12846 bfd_vma si;
12847 unsigned long maxlength = 0;
12848 unsigned long nzero_counts = 0;
12849 unsigned long nsyms = 0;
12850 char *visited;
12851
12852 printf (ngettext ("\nHistogram for bucket list length "
12853 "(total of %lu bucket):\n",
12854 "\nHistogram for bucket list length "
12855 "(total of %lu buckets):\n",
12856 (unsigned long) filedata->nbuckets),
12857 (unsigned long) filedata->nbuckets);
12858
12859 lengths = (unsigned long *) calloc (filedata->nbuckets,
12860 sizeof (*lengths));
12861 if (lengths == NULL)
12862 {
12863 error (_("Out of memory allocating space for histogram buckets\n"));
12864 goto err_out;
12865 }
12866 visited = xcmalloc (filedata->nchains, 1);
12867 memset (visited, 0, filedata->nchains);
12868
12869 printf (_(" Length Number %% of total Coverage\n"));
12870 for (hn = 0; hn < filedata->nbuckets; ++hn)
12871 {
12872 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
12873 {
12874 ++nsyms;
12875 if (maxlength < ++lengths[hn])
12876 ++maxlength;
12877 if (si >= filedata->nchains || visited[si])
12878 {
12879 error (_("histogram chain is corrupt\n"));
12880 break;
12881 }
12882 visited[si] = 1;
12883 }
12884 }
12885 free (visited);
12886
12887 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
12888 if (counts == NULL)
12889 {
12890 free (lengths);
12891 error (_("Out of memory allocating space for histogram counts\n"));
12892 goto err_out;
12893 }
12894
12895 for (hn = 0; hn < filedata->nbuckets; ++hn)
12896 ++counts[lengths[hn]];
12897
12898 if (filedata->nbuckets > 0)
12899 {
12900 unsigned long i;
12901 printf (" 0 %-10lu (%5.1f%%)\n",
12902 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
12903 for (i = 1; i <= maxlength; ++i)
12904 {
12905 nzero_counts += counts[i] * i;
12906 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
12907 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
12908 (nzero_counts * 100.0) / nsyms);
12909 }
12910 }
12911
12912 free (counts);
12913 free (lengths);
12914 }
12915
12916 free (filedata->buckets);
12917 filedata->buckets = NULL;
12918 filedata->nbuckets = 0;
12919 free (filedata->chains);
12920 filedata->chains = NULL;
12921
12922 if (do_histogram && filedata->gnubuckets != NULL)
12923 {
12924 unsigned long * lengths;
12925 unsigned long * counts;
12926 unsigned long hn;
12927 unsigned long maxlength = 0;
12928 unsigned long nzero_counts = 0;
12929 unsigned long nsyms = 0;
12930
12931 printf (ngettext ("\nHistogram for `%s' bucket list length "
12932 "(total of %lu bucket):\n",
12933 "\nHistogram for `%s' bucket list length "
12934 "(total of %lu buckets):\n",
12935 (unsigned long) filedata->ngnubuckets),
12936 GNU_HASH_SECTION_NAME (filedata),
12937 (unsigned long) filedata->ngnubuckets);
12938
12939 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
12940 sizeof (*lengths));
12941 if (lengths == NULL)
12942 {
12943 error (_("Out of memory allocating space for gnu histogram buckets\n"));
12944 goto err_out;
12945 }
12946
12947 printf (_(" Length Number %% of total Coverage\n"));
12948
12949 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12950 if (filedata->gnubuckets[hn] != 0)
12951 {
12952 bfd_vma off, length = 1;
12953
12954 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
12955 /* PR 17531 file: 010-77222-0.004. */
12956 off < filedata->ngnuchains
12957 && (filedata->gnuchains[off] & 1) == 0;
12958 ++off)
12959 ++length;
12960 lengths[hn] = length;
12961 if (length > maxlength)
12962 maxlength = length;
12963 nsyms += length;
12964 }
12965
12966 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
12967 if (counts == NULL)
12968 {
12969 free (lengths);
12970 error (_("Out of memory allocating space for gnu histogram counts\n"));
12971 goto err_out;
12972 }
12973
12974 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12975 ++counts[lengths[hn]];
12976
12977 if (filedata->ngnubuckets > 0)
12978 {
12979 unsigned long j;
12980 printf (" 0 %-10lu (%5.1f%%)\n",
12981 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
12982 for (j = 1; j <= maxlength; ++j)
12983 {
12984 nzero_counts += counts[j] * j;
12985 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
12986 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
12987 (nzero_counts * 100.0) / nsyms);
12988 }
12989 }
12990
12991 free (counts);
12992 free (lengths);
12993 }
12994 free (filedata->gnubuckets);
12995 filedata->gnubuckets = NULL;
12996 filedata->ngnubuckets = 0;
12997 free (filedata->gnuchains);
12998 filedata->gnuchains = NULL;
12999 filedata->ngnuchains = 0;
13000 free (filedata->mipsxlat);
13001 filedata->mipsxlat = NULL;
13002 return TRUE;
13003
13004 err_out:
13005 free (filedata->gnubuckets);
13006 filedata->gnubuckets = NULL;
13007 filedata->ngnubuckets = 0;
13008 free (filedata->gnuchains);
13009 filedata->gnuchains = NULL;
13010 filedata->ngnuchains = 0;
13011 free (filedata->mipsxlat);
13012 filedata->mipsxlat = NULL;
13013 free (filedata->buckets);
13014 filedata->buckets = NULL;
13015 filedata->nbuckets = 0;
13016 free (filedata->chains);
13017 filedata->chains = NULL;
13018 return FALSE;
13019 }
13020
13021 static bfd_boolean
13022 process_syminfo (Filedata * filedata)
13023 {
13024 unsigned int i;
13025
13026 if (filedata->dynamic_syminfo == NULL
13027 || !do_dynamic)
13028 /* No syminfo, this is ok. */
13029 return TRUE;
13030
13031 /* There better should be a dynamic symbol section. */
13032 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13033 return FALSE;
13034
13035 if (filedata->is_separate)
13036 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13037 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13038 filedata->dynamic_syminfo_nent),
13039 filedata->file_name,
13040 filedata->dynamic_syminfo_offset,
13041 filedata->dynamic_syminfo_nent);
13042 else
13043 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13044 "contains %d entry:\n",
13045 "\nDynamic info segment at offset 0x%lx "
13046 "contains %d entries:\n",
13047 filedata->dynamic_syminfo_nent),
13048 filedata->dynamic_syminfo_offset,
13049 filedata->dynamic_syminfo_nent);
13050
13051 printf (_(" Num: Name BoundTo Flags\n"));
13052 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13053 {
13054 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13055
13056 printf ("%4d: ", i);
13057 if (i >= filedata->num_dynamic_syms)
13058 printf (_("<corrupt index>"));
13059 else if (VALID_DYNAMIC_NAME (filedata, filedata->dynamic_symbols[i].st_name))
13060 print_symbol (30, GET_DYNAMIC_NAME (filedata,
13061 filedata->dynamic_symbols[i].st_name));
13062 else
13063 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13064 putchar (' ');
13065
13066 switch (filedata->dynamic_syminfo[i].si_boundto)
13067 {
13068 case SYMINFO_BT_SELF:
13069 fputs ("SELF ", stdout);
13070 break;
13071 case SYMINFO_BT_PARENT:
13072 fputs ("PARENT ", stdout);
13073 break;
13074 default:
13075 if (filedata->dynamic_syminfo[i].si_boundto > 0
13076 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13077 && VALID_DYNAMIC_NAME (filedata,
13078 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13079 {
13080 print_symbol (10, GET_DYNAMIC_NAME (filedata,
13081 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13082 putchar (' ' );
13083 }
13084 else
13085 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13086 break;
13087 }
13088
13089 if (flags & SYMINFO_FLG_DIRECT)
13090 printf (" DIRECT");
13091 if (flags & SYMINFO_FLG_PASSTHRU)
13092 printf (" PASSTHRU");
13093 if (flags & SYMINFO_FLG_COPY)
13094 printf (" COPY");
13095 if (flags & SYMINFO_FLG_LAZYLOAD)
13096 printf (" LAZYLOAD");
13097
13098 puts ("");
13099 }
13100
13101 return TRUE;
13102 }
13103
13104 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13105 is contained by the region START .. END. The types of ADDR, START
13106 and END should all be the same. Note both ADDR + NELEM and END
13107 point to just beyond the end of the regions that are being tested. */
13108 #define IN_RANGE(START,END,ADDR,NELEM) \
13109 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13110
13111 /* Check to see if the given reloc needs to be handled in a target specific
13112 manner. If so then process the reloc and return TRUE otherwise return
13113 FALSE.
13114
13115 If called with reloc == NULL, then this is a signal that reloc processing
13116 for the current section has finished, and any saved state should be
13117 discarded. */
13118
13119 static bfd_boolean
13120 target_specific_reloc_handling (Filedata * filedata,
13121 Elf_Internal_Rela * reloc,
13122 unsigned char * start,
13123 unsigned char * end,
13124 Elf_Internal_Sym * symtab,
13125 unsigned long num_syms)
13126 {
13127 unsigned int reloc_type = 0;
13128 unsigned long sym_index = 0;
13129
13130 if (reloc)
13131 {
13132 reloc_type = get_reloc_type (filedata, reloc->r_info);
13133 sym_index = get_reloc_symindex (reloc->r_info);
13134 }
13135
13136 switch (filedata->file_header.e_machine)
13137 {
13138 case EM_MSP430:
13139 case EM_MSP430_OLD:
13140 {
13141 static Elf_Internal_Sym * saved_sym = NULL;
13142
13143 if (reloc == NULL)
13144 {
13145 saved_sym = NULL;
13146 return TRUE;
13147 }
13148
13149 switch (reloc_type)
13150 {
13151 case 10: /* R_MSP430_SYM_DIFF */
13152 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13153 if (uses_msp430x_relocs (filedata))
13154 break;
13155 /* Fall through. */
13156 case 21: /* R_MSP430X_SYM_DIFF */
13157 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13158 /* PR 21139. */
13159 if (sym_index >= num_syms)
13160 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13161 sym_index);
13162 else
13163 saved_sym = symtab + sym_index;
13164 return TRUE;
13165
13166 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13167 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13168 goto handle_sym_diff;
13169
13170 case 5: /* R_MSP430_16_BYTE */
13171 case 9: /* R_MSP430_8 */
13172 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13173 if (uses_msp430x_relocs (filedata))
13174 break;
13175 goto handle_sym_diff;
13176
13177 case 2: /* R_MSP430_ABS16 */
13178 case 15: /* R_MSP430X_ABS16 */
13179 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13180 if (! uses_msp430x_relocs (filedata))
13181 break;
13182 goto handle_sym_diff;
13183
13184 handle_sym_diff:
13185 if (saved_sym != NULL)
13186 {
13187 bfd_vma value;
13188 unsigned int reloc_size = 0;
13189 int leb_ret = 0;
13190 switch (reloc_type)
13191 {
13192 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13193 reloc_size = 4;
13194 break;
13195 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13196 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13197 if (reloc->r_offset < (size_t) (end - start))
13198 read_leb128 (start + reloc->r_offset, end, FALSE,
13199 &reloc_size, &leb_ret);
13200 break;
13201 default:
13202 reloc_size = 2;
13203 break;
13204 }
13205
13206 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
13207 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13208 "ULEB128 value\n"),
13209 (long) reloc->r_offset);
13210 else if (sym_index >= num_syms)
13211 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13212 sym_index);
13213 else
13214 {
13215 value = reloc->r_addend + (symtab[sym_index].st_value
13216 - saved_sym->st_value);
13217
13218 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13219 byte_put (start + reloc->r_offset, value, reloc_size);
13220 else
13221 /* PR 21137 */
13222 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13223 (long) reloc->r_offset);
13224 }
13225
13226 saved_sym = NULL;
13227 return TRUE;
13228 }
13229 break;
13230
13231 default:
13232 if (saved_sym != NULL)
13233 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13234 break;
13235 }
13236 break;
13237 }
13238
13239 case EM_MN10300:
13240 case EM_CYGNUS_MN10300:
13241 {
13242 static Elf_Internal_Sym * saved_sym = NULL;
13243
13244 if (reloc == NULL)
13245 {
13246 saved_sym = NULL;
13247 return TRUE;
13248 }
13249
13250 switch (reloc_type)
13251 {
13252 case 34: /* R_MN10300_ALIGN */
13253 return TRUE;
13254 case 33: /* R_MN10300_SYM_DIFF */
13255 if (sym_index >= num_syms)
13256 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13257 sym_index);
13258 else
13259 saved_sym = symtab + sym_index;
13260 return TRUE;
13261
13262 case 1: /* R_MN10300_32 */
13263 case 2: /* R_MN10300_16 */
13264 if (saved_sym != NULL)
13265 {
13266 int reloc_size = reloc_type == 1 ? 4 : 2;
13267 bfd_vma value;
13268
13269 if (sym_index >= num_syms)
13270 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13271 sym_index);
13272 else
13273 {
13274 value = reloc->r_addend + (symtab[sym_index].st_value
13275 - saved_sym->st_value);
13276
13277 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13278 byte_put (start + reloc->r_offset, value, reloc_size);
13279 else
13280 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13281 (long) reloc->r_offset);
13282 }
13283
13284 saved_sym = NULL;
13285 return TRUE;
13286 }
13287 break;
13288 default:
13289 if (saved_sym != NULL)
13290 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
13291 break;
13292 }
13293 break;
13294 }
13295
13296 case EM_RL78:
13297 {
13298 static bfd_vma saved_sym1 = 0;
13299 static bfd_vma saved_sym2 = 0;
13300 static bfd_vma value;
13301
13302 if (reloc == NULL)
13303 {
13304 saved_sym1 = saved_sym2 = 0;
13305 return TRUE;
13306 }
13307
13308 switch (reloc_type)
13309 {
13310 case 0x80: /* R_RL78_SYM. */
13311 saved_sym1 = saved_sym2;
13312 if (sym_index >= num_syms)
13313 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13314 sym_index);
13315 else
13316 {
13317 saved_sym2 = symtab[sym_index].st_value;
13318 saved_sym2 += reloc->r_addend;
13319 }
13320 return TRUE;
13321
13322 case 0x83: /* R_RL78_OPsub. */
13323 value = saved_sym1 - saved_sym2;
13324 saved_sym2 = saved_sym1 = 0;
13325 return TRUE;
13326 break;
13327
13328 case 0x41: /* R_RL78_ABS32. */
13329 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
13330 byte_put (start + reloc->r_offset, value, 4);
13331 else
13332 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13333 (long) reloc->r_offset);
13334 value = 0;
13335 return TRUE;
13336
13337 case 0x43: /* R_RL78_ABS16. */
13338 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
13339 byte_put (start + reloc->r_offset, value, 2);
13340 else
13341 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13342 (long) reloc->r_offset);
13343 value = 0;
13344 return TRUE;
13345
13346 default:
13347 break;
13348 }
13349 break;
13350 }
13351 }
13352
13353 return FALSE;
13354 }
13355
13356 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13357 DWARF debug sections. This is a target specific test. Note - we do not
13358 go through the whole including-target-headers-multiple-times route, (as
13359 we have already done with <elf/h8.h>) because this would become very
13360 messy and even then this function would have to contain target specific
13361 information (the names of the relocs instead of their numeric values).
13362 FIXME: This is not the correct way to solve this problem. The proper way
13363 is to have target specific reloc sizing and typing functions created by
13364 the reloc-macros.h header, in the same way that it already creates the
13365 reloc naming functions. */
13366
13367 static bfd_boolean
13368 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13369 {
13370 /* Please keep this table alpha-sorted for ease of visual lookup. */
13371 switch (filedata->file_header.e_machine)
13372 {
13373 case EM_386:
13374 case EM_IAMCU:
13375 return reloc_type == 1; /* R_386_32. */
13376 case EM_68K:
13377 return reloc_type == 1; /* R_68K_32. */
13378 case EM_860:
13379 return reloc_type == 1; /* R_860_32. */
13380 case EM_960:
13381 return reloc_type == 2; /* R_960_32. */
13382 case EM_AARCH64:
13383 return (reloc_type == 258
13384 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
13385 case EM_BPF:
13386 return reloc_type == 11; /* R_BPF_DATA_32 */
13387 case EM_ADAPTEVA_EPIPHANY:
13388 return reloc_type == 3;
13389 case EM_ALPHA:
13390 return reloc_type == 1; /* R_ALPHA_REFLONG. */
13391 case EM_ARC:
13392 return reloc_type == 1; /* R_ARC_32. */
13393 case EM_ARC_COMPACT:
13394 case EM_ARC_COMPACT2:
13395 return reloc_type == 4; /* R_ARC_32. */
13396 case EM_ARM:
13397 return reloc_type == 2; /* R_ARM_ABS32 */
13398 case EM_AVR_OLD:
13399 case EM_AVR:
13400 return reloc_type == 1;
13401 case EM_BLACKFIN:
13402 return reloc_type == 0x12; /* R_byte4_data. */
13403 case EM_CRIS:
13404 return reloc_type == 3; /* R_CRIS_32. */
13405 case EM_CR16:
13406 return reloc_type == 3; /* R_CR16_NUM32. */
13407 case EM_CRX:
13408 return reloc_type == 15; /* R_CRX_NUM32. */
13409 case EM_CSKY:
13410 return reloc_type == 1; /* R_CKCORE_ADDR32. */
13411 case EM_CYGNUS_FRV:
13412 return reloc_type == 1;
13413 case EM_CYGNUS_D10V:
13414 case EM_D10V:
13415 return reloc_type == 6; /* R_D10V_32. */
13416 case EM_CYGNUS_D30V:
13417 case EM_D30V:
13418 return reloc_type == 12; /* R_D30V_32_NORMAL. */
13419 case EM_DLX:
13420 return reloc_type == 3; /* R_DLX_RELOC_32. */
13421 case EM_CYGNUS_FR30:
13422 case EM_FR30:
13423 return reloc_type == 3; /* R_FR30_32. */
13424 case EM_FT32:
13425 return reloc_type == 1; /* R_FT32_32. */
13426 case EM_H8S:
13427 case EM_H8_300:
13428 case EM_H8_300H:
13429 return reloc_type == 1; /* R_H8_DIR32. */
13430 case EM_IA_64:
13431 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
13432 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
13433 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
13434 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
13435 case EM_IP2K_OLD:
13436 case EM_IP2K:
13437 return reloc_type == 2; /* R_IP2K_32. */
13438 case EM_IQ2000:
13439 return reloc_type == 2; /* R_IQ2000_32. */
13440 case EM_LATTICEMICO32:
13441 return reloc_type == 3; /* R_LM32_32. */
13442 case EM_M32C_OLD:
13443 case EM_M32C:
13444 return reloc_type == 3; /* R_M32C_32. */
13445 case EM_M32R:
13446 return reloc_type == 34; /* R_M32R_32_RELA. */
13447 case EM_68HC11:
13448 case EM_68HC12:
13449 return reloc_type == 6; /* R_M68HC11_32. */
13450 case EM_S12Z:
13451 return reloc_type == 7 || /* R_S12Z_EXT32 */
13452 reloc_type == 6; /* R_S12Z_CW32. */
13453 case EM_MCORE:
13454 return reloc_type == 1; /* R_MCORE_ADDR32. */
13455 case EM_CYGNUS_MEP:
13456 return reloc_type == 4; /* R_MEP_32. */
13457 case EM_METAG:
13458 return reloc_type == 2; /* R_METAG_ADDR32. */
13459 case EM_MICROBLAZE:
13460 return reloc_type == 1; /* R_MICROBLAZE_32. */
13461 case EM_MIPS:
13462 return reloc_type == 2; /* R_MIPS_32. */
13463 case EM_MMIX:
13464 return reloc_type == 4; /* R_MMIX_32. */
13465 case EM_CYGNUS_MN10200:
13466 case EM_MN10200:
13467 return reloc_type == 1; /* R_MN10200_32. */
13468 case EM_CYGNUS_MN10300:
13469 case EM_MN10300:
13470 return reloc_type == 1; /* R_MN10300_32. */
13471 case EM_MOXIE:
13472 return reloc_type == 1; /* R_MOXIE_32. */
13473 case EM_MSP430_OLD:
13474 case EM_MSP430:
13475 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
13476 case EM_MT:
13477 return reloc_type == 2; /* R_MT_32. */
13478 case EM_NDS32:
13479 return reloc_type == 20; /* R_NDS32_RELA. */
13480 case EM_ALTERA_NIOS2:
13481 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
13482 case EM_NIOS32:
13483 return reloc_type == 1; /* R_NIOS_32. */
13484 case EM_OR1K:
13485 return reloc_type == 1; /* R_OR1K_32. */
13486 case EM_PARISC:
13487 return (reloc_type == 1 /* R_PARISC_DIR32. */
13488 || reloc_type == 2 /* R_PARISC_DIR21L. */
13489 || reloc_type == 41); /* R_PARISC_SECREL32. */
13490 case EM_PJ:
13491 case EM_PJ_OLD:
13492 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
13493 case EM_PPC64:
13494 return reloc_type == 1; /* R_PPC64_ADDR32. */
13495 case EM_PPC:
13496 return reloc_type == 1; /* R_PPC_ADDR32. */
13497 case EM_TI_PRU:
13498 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
13499 case EM_RISCV:
13500 return reloc_type == 1; /* R_RISCV_32. */
13501 case EM_RL78:
13502 return reloc_type == 1; /* R_RL78_DIR32. */
13503 case EM_RX:
13504 return reloc_type == 1; /* R_RX_DIR32. */
13505 case EM_S370:
13506 return reloc_type == 1; /* R_I370_ADDR31. */
13507 case EM_S390_OLD:
13508 case EM_S390:
13509 return reloc_type == 4; /* R_S390_32. */
13510 case EM_SCORE:
13511 return reloc_type == 8; /* R_SCORE_ABS32. */
13512 case EM_SH:
13513 return reloc_type == 1; /* R_SH_DIR32. */
13514 case EM_SPARC32PLUS:
13515 case EM_SPARCV9:
13516 case EM_SPARC:
13517 return reloc_type == 3 /* R_SPARC_32. */
13518 || reloc_type == 23; /* R_SPARC_UA32. */
13519 case EM_SPU:
13520 return reloc_type == 6; /* R_SPU_ADDR32 */
13521 case EM_TI_C6000:
13522 return reloc_type == 1; /* R_C6000_ABS32. */
13523 case EM_TILEGX:
13524 return reloc_type == 2; /* R_TILEGX_32. */
13525 case EM_TILEPRO:
13526 return reloc_type == 1; /* R_TILEPRO_32. */
13527 case EM_CYGNUS_V850:
13528 case EM_V850:
13529 return reloc_type == 6; /* R_V850_ABS32. */
13530 case EM_V800:
13531 return reloc_type == 0x33; /* R_V810_WORD. */
13532 case EM_VAX:
13533 return reloc_type == 1; /* R_VAX_32. */
13534 case EM_VISIUM:
13535 return reloc_type == 3; /* R_VISIUM_32. */
13536 case EM_WEBASSEMBLY:
13537 return reloc_type == 1; /* R_WASM32_32. */
13538 case EM_X86_64:
13539 case EM_L1OM:
13540 case EM_K1OM:
13541 return reloc_type == 10; /* R_X86_64_32. */
13542 case EM_XC16X:
13543 case EM_C166:
13544 return reloc_type == 3; /* R_XC16C_ABS_32. */
13545 case EM_XGATE:
13546 return reloc_type == 4; /* R_XGATE_32. */
13547 case EM_XSTORMY16:
13548 return reloc_type == 1; /* R_XSTROMY16_32. */
13549 case EM_XTENSA_OLD:
13550 case EM_XTENSA:
13551 return reloc_type == 1; /* R_XTENSA_32. */
13552 case EM_Z80:
13553 return reloc_type == 6; /* R_Z80_32. */
13554 default:
13555 {
13556 static unsigned int prev_warn = 0;
13557
13558 /* Avoid repeating the same warning multiple times. */
13559 if (prev_warn != filedata->file_header.e_machine)
13560 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
13561 filedata->file_header.e_machine);
13562 prev_warn = filedata->file_header.e_machine;
13563 return FALSE;
13564 }
13565 }
13566 }
13567
13568 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13569 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
13570
13571 static bfd_boolean
13572 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13573 {
13574 switch (filedata->file_header.e_machine)
13575 /* Please keep this table alpha-sorted for ease of visual lookup. */
13576 {
13577 case EM_386:
13578 case EM_IAMCU:
13579 return reloc_type == 2; /* R_386_PC32. */
13580 case EM_68K:
13581 return reloc_type == 4; /* R_68K_PC32. */
13582 case EM_AARCH64:
13583 return reloc_type == 261; /* R_AARCH64_PREL32 */
13584 case EM_ADAPTEVA_EPIPHANY:
13585 return reloc_type == 6;
13586 case EM_ALPHA:
13587 return reloc_type == 10; /* R_ALPHA_SREL32. */
13588 case EM_ARC_COMPACT:
13589 case EM_ARC_COMPACT2:
13590 return reloc_type == 49; /* R_ARC_32_PCREL. */
13591 case EM_ARM:
13592 return reloc_type == 3; /* R_ARM_REL32 */
13593 case EM_AVR_OLD:
13594 case EM_AVR:
13595 return reloc_type == 36; /* R_AVR_32_PCREL. */
13596 case EM_MICROBLAZE:
13597 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
13598 case EM_OR1K:
13599 return reloc_type == 9; /* R_OR1K_32_PCREL. */
13600 case EM_PARISC:
13601 return reloc_type == 9; /* R_PARISC_PCREL32. */
13602 case EM_PPC:
13603 return reloc_type == 26; /* R_PPC_REL32. */
13604 case EM_PPC64:
13605 return reloc_type == 26; /* R_PPC64_REL32. */
13606 case EM_RISCV:
13607 return reloc_type == 57; /* R_RISCV_32_PCREL. */
13608 case EM_S390_OLD:
13609 case EM_S390:
13610 return reloc_type == 5; /* R_390_PC32. */
13611 case EM_SH:
13612 return reloc_type == 2; /* R_SH_REL32. */
13613 case EM_SPARC32PLUS:
13614 case EM_SPARCV9:
13615 case EM_SPARC:
13616 return reloc_type == 6; /* R_SPARC_DISP32. */
13617 case EM_SPU:
13618 return reloc_type == 13; /* R_SPU_REL32. */
13619 case EM_TILEGX:
13620 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
13621 case EM_TILEPRO:
13622 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
13623 case EM_VISIUM:
13624 return reloc_type == 6; /* R_VISIUM_32_PCREL */
13625 case EM_X86_64:
13626 case EM_L1OM:
13627 case EM_K1OM:
13628 return reloc_type == 2; /* R_X86_64_PC32. */
13629 case EM_VAX:
13630 return reloc_type == 4; /* R_VAX_PCREL32. */
13631 case EM_XTENSA_OLD:
13632 case EM_XTENSA:
13633 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
13634 default:
13635 /* Do not abort or issue an error message here. Not all targets use
13636 pc-relative 32-bit relocs in their DWARF debug information and we
13637 have already tested for target coverage in is_32bit_abs_reloc. A
13638 more helpful warning message will be generated by apply_relocations
13639 anyway, so just return. */
13640 return FALSE;
13641 }
13642 }
13643
13644 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13645 a 64-bit absolute RELA relocation used in DWARF debug sections. */
13646
13647 static bfd_boolean
13648 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13649 {
13650 switch (filedata->file_header.e_machine)
13651 {
13652 case EM_AARCH64:
13653 return reloc_type == 257; /* R_AARCH64_ABS64. */
13654 case EM_ALPHA:
13655 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
13656 case EM_IA_64:
13657 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
13658 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
13659 case EM_PARISC:
13660 return reloc_type == 80; /* R_PARISC_DIR64. */
13661 case EM_PPC64:
13662 return reloc_type == 38; /* R_PPC64_ADDR64. */
13663 case EM_RISCV:
13664 return reloc_type == 2; /* R_RISCV_64. */
13665 case EM_SPARC32PLUS:
13666 case EM_SPARCV9:
13667 case EM_SPARC:
13668 return reloc_type == 32 /* R_SPARC_64. */
13669 || reloc_type == 54; /* R_SPARC_UA64. */
13670 case EM_X86_64:
13671 case EM_L1OM:
13672 case EM_K1OM:
13673 return reloc_type == 1; /* R_X86_64_64. */
13674 case EM_S390_OLD:
13675 case EM_S390:
13676 return reloc_type == 22; /* R_S390_64. */
13677 case EM_TILEGX:
13678 return reloc_type == 1; /* R_TILEGX_64. */
13679 case EM_MIPS:
13680 return reloc_type == 18; /* R_MIPS_64. */
13681 default:
13682 return FALSE;
13683 }
13684 }
13685
13686 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
13687 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
13688
13689 static bfd_boolean
13690 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13691 {
13692 switch (filedata->file_header.e_machine)
13693 {
13694 case EM_AARCH64:
13695 return reloc_type == 260; /* R_AARCH64_PREL64. */
13696 case EM_ALPHA:
13697 return reloc_type == 11; /* R_ALPHA_SREL64. */
13698 case EM_IA_64:
13699 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
13700 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
13701 case EM_PARISC:
13702 return reloc_type == 72; /* R_PARISC_PCREL64. */
13703 case EM_PPC64:
13704 return reloc_type == 44; /* R_PPC64_REL64. */
13705 case EM_SPARC32PLUS:
13706 case EM_SPARCV9:
13707 case EM_SPARC:
13708 return reloc_type == 46; /* R_SPARC_DISP64. */
13709 case EM_X86_64:
13710 case EM_L1OM:
13711 case EM_K1OM:
13712 return reloc_type == 24; /* R_X86_64_PC64. */
13713 case EM_S390_OLD:
13714 case EM_S390:
13715 return reloc_type == 23; /* R_S390_PC64. */
13716 case EM_TILEGX:
13717 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
13718 default:
13719 return FALSE;
13720 }
13721 }
13722
13723 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13724 a 24-bit absolute RELA relocation used in DWARF debug sections. */
13725
13726 static bfd_boolean
13727 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13728 {
13729 switch (filedata->file_header.e_machine)
13730 {
13731 case EM_CYGNUS_MN10200:
13732 case EM_MN10200:
13733 return reloc_type == 4; /* R_MN10200_24. */
13734 case EM_FT32:
13735 return reloc_type == 5; /* R_FT32_20. */
13736 case EM_Z80:
13737 return reloc_type == 5; /* R_Z80_24. */
13738 default:
13739 return FALSE;
13740 }
13741 }
13742
13743 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13744 a 16-bit absolute RELA relocation used in DWARF debug sections. */
13745
13746 static bfd_boolean
13747 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13748 {
13749 /* Please keep this table alpha-sorted for ease of visual lookup. */
13750 switch (filedata->file_header.e_machine)
13751 {
13752 case EM_ARC:
13753 case EM_ARC_COMPACT:
13754 case EM_ARC_COMPACT2:
13755 return reloc_type == 2; /* R_ARC_16. */
13756 case EM_ADAPTEVA_EPIPHANY:
13757 return reloc_type == 5;
13758 case EM_AVR_OLD:
13759 case EM_AVR:
13760 return reloc_type == 4; /* R_AVR_16. */
13761 case EM_CYGNUS_D10V:
13762 case EM_D10V:
13763 return reloc_type == 3; /* R_D10V_16. */
13764 case EM_FT32:
13765 return reloc_type == 2; /* R_FT32_16. */
13766 case EM_H8S:
13767 case EM_H8_300:
13768 case EM_H8_300H:
13769 return reloc_type == R_H8_DIR16;
13770 case EM_IP2K_OLD:
13771 case EM_IP2K:
13772 return reloc_type == 1; /* R_IP2K_16. */
13773 case EM_M32C_OLD:
13774 case EM_M32C:
13775 return reloc_type == 1; /* R_M32C_16 */
13776 case EM_CYGNUS_MN10200:
13777 case EM_MN10200:
13778 return reloc_type == 2; /* R_MN10200_16. */
13779 case EM_CYGNUS_MN10300:
13780 case EM_MN10300:
13781 return reloc_type == 2; /* R_MN10300_16. */
13782 case EM_MSP430:
13783 if (uses_msp430x_relocs (filedata))
13784 return reloc_type == 2; /* R_MSP430_ABS16. */
13785 /* Fall through. */
13786 case EM_MSP430_OLD:
13787 return reloc_type == 5; /* R_MSP430_16_BYTE. */
13788 case EM_NDS32:
13789 return reloc_type == 19; /* R_NDS32_RELA. */
13790 case EM_ALTERA_NIOS2:
13791 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
13792 case EM_NIOS32:
13793 return reloc_type == 9; /* R_NIOS_16. */
13794 case EM_OR1K:
13795 return reloc_type == 2; /* R_OR1K_16. */
13796 case EM_RISCV:
13797 return reloc_type == 55; /* R_RISCV_SET16. */
13798 case EM_TI_PRU:
13799 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
13800 case EM_TI_C6000:
13801 return reloc_type == 2; /* R_C6000_ABS16. */
13802 case EM_VISIUM:
13803 return reloc_type == 2; /* R_VISIUM_16. */
13804 case EM_XC16X:
13805 case EM_C166:
13806 return reloc_type == 2; /* R_XC16C_ABS_16. */
13807 case EM_XGATE:
13808 return reloc_type == 3; /* R_XGATE_16. */
13809 case EM_Z80:
13810 return reloc_type == 4; /* R_Z80_16. */
13811 default:
13812 return FALSE;
13813 }
13814 }
13815
13816 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13817 a 8-bit absolute RELA relocation used in DWARF debug sections. */
13818
13819 static bfd_boolean
13820 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13821 {
13822 switch (filedata->file_header.e_machine)
13823 {
13824 case EM_RISCV:
13825 return reloc_type == 54; /* R_RISCV_SET8. */
13826 case EM_Z80:
13827 return reloc_type == 1; /* R_Z80_8. */
13828 default:
13829 return FALSE;
13830 }
13831 }
13832
13833 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13834 a 6-bit absolute RELA relocation used in DWARF debug sections. */
13835
13836 static bfd_boolean
13837 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13838 {
13839 switch (filedata->file_header.e_machine)
13840 {
13841 case EM_RISCV:
13842 return reloc_type == 53; /* R_RISCV_SET6. */
13843 default:
13844 return FALSE;
13845 }
13846 }
13847
13848 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13849 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
13850
13851 static bfd_boolean
13852 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13853 {
13854 /* Please keep this table alpha-sorted for ease of visual lookup. */
13855 switch (filedata->file_header.e_machine)
13856 {
13857 case EM_RISCV:
13858 return reloc_type == 35; /* R_RISCV_ADD32. */
13859 default:
13860 return FALSE;
13861 }
13862 }
13863
13864 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13865 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
13866
13867 static bfd_boolean
13868 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13869 {
13870 /* Please keep this table alpha-sorted for ease of visual lookup. */
13871 switch (filedata->file_header.e_machine)
13872 {
13873 case EM_RISCV:
13874 return reloc_type == 39; /* R_RISCV_SUB32. */
13875 default:
13876 return FALSE;
13877 }
13878 }
13879
13880 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13881 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
13882
13883 static bfd_boolean
13884 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13885 {
13886 /* Please keep this table alpha-sorted for ease of visual lookup. */
13887 switch (filedata->file_header.e_machine)
13888 {
13889 case EM_RISCV:
13890 return reloc_type == 36; /* R_RISCV_ADD64. */
13891 default:
13892 return FALSE;
13893 }
13894 }
13895
13896 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13897 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
13898
13899 static bfd_boolean
13900 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13901 {
13902 /* Please keep this table alpha-sorted for ease of visual lookup. */
13903 switch (filedata->file_header.e_machine)
13904 {
13905 case EM_RISCV:
13906 return reloc_type == 40; /* R_RISCV_SUB64. */
13907 default:
13908 return FALSE;
13909 }
13910 }
13911
13912 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13913 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
13914
13915 static bfd_boolean
13916 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13917 {
13918 /* Please keep this table alpha-sorted for ease of visual lookup. */
13919 switch (filedata->file_header.e_machine)
13920 {
13921 case EM_RISCV:
13922 return reloc_type == 34; /* R_RISCV_ADD16. */
13923 default:
13924 return FALSE;
13925 }
13926 }
13927
13928 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13929 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
13930
13931 static bfd_boolean
13932 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13933 {
13934 /* Please keep this table alpha-sorted for ease of visual lookup. */
13935 switch (filedata->file_header.e_machine)
13936 {
13937 case EM_RISCV:
13938 return reloc_type == 38; /* R_RISCV_SUB16. */
13939 default:
13940 return FALSE;
13941 }
13942 }
13943
13944 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13945 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
13946
13947 static bfd_boolean
13948 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13949 {
13950 /* Please keep this table alpha-sorted for ease of visual lookup. */
13951 switch (filedata->file_header.e_machine)
13952 {
13953 case EM_RISCV:
13954 return reloc_type == 33; /* R_RISCV_ADD8. */
13955 default:
13956 return FALSE;
13957 }
13958 }
13959
13960 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13961 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
13962
13963 static bfd_boolean
13964 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13965 {
13966 /* Please keep this table alpha-sorted for ease of visual lookup. */
13967 switch (filedata->file_header.e_machine)
13968 {
13969 case EM_RISCV:
13970 return reloc_type == 37; /* R_RISCV_SUB8. */
13971 default:
13972 return FALSE;
13973 }
13974 }
13975
13976 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13977 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
13978
13979 static bfd_boolean
13980 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13981 {
13982 switch (filedata->file_header.e_machine)
13983 {
13984 case EM_RISCV:
13985 return reloc_type == 52; /* R_RISCV_SUB6. */
13986 default:
13987 return FALSE;
13988 }
13989 }
13990
13991 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
13992 relocation entries (possibly formerly used for SHT_GROUP sections). */
13993
13994 static bfd_boolean
13995 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
13996 {
13997 switch (filedata->file_header.e_machine)
13998 {
13999 case EM_386: /* R_386_NONE. */
14000 case EM_68K: /* R_68K_NONE. */
14001 case EM_ADAPTEVA_EPIPHANY:
14002 case EM_ALPHA: /* R_ALPHA_NONE. */
14003 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14004 case EM_ARC: /* R_ARC_NONE. */
14005 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14006 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14007 case EM_ARM: /* R_ARM_NONE. */
14008 case EM_C166: /* R_XC16X_NONE. */
14009 case EM_CRIS: /* R_CRIS_NONE. */
14010 case EM_FT32: /* R_FT32_NONE. */
14011 case EM_IA_64: /* R_IA64_NONE. */
14012 case EM_K1OM: /* R_X86_64_NONE. */
14013 case EM_L1OM: /* R_X86_64_NONE. */
14014 case EM_M32R: /* R_M32R_NONE. */
14015 case EM_MIPS: /* R_MIPS_NONE. */
14016 case EM_MN10300: /* R_MN10300_NONE. */
14017 case EM_MOXIE: /* R_MOXIE_NONE. */
14018 case EM_NIOS32: /* R_NIOS_NONE. */
14019 case EM_OR1K: /* R_OR1K_NONE. */
14020 case EM_PARISC: /* R_PARISC_NONE. */
14021 case EM_PPC64: /* R_PPC64_NONE. */
14022 case EM_PPC: /* R_PPC_NONE. */
14023 case EM_RISCV: /* R_RISCV_NONE. */
14024 case EM_S390: /* R_390_NONE. */
14025 case EM_S390_OLD:
14026 case EM_SH: /* R_SH_NONE. */
14027 case EM_SPARC32PLUS:
14028 case EM_SPARC: /* R_SPARC_NONE. */
14029 case EM_SPARCV9:
14030 case EM_TILEGX: /* R_TILEGX_NONE. */
14031 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14032 case EM_TI_C6000:/* R_C6000_NONE. */
14033 case EM_X86_64: /* R_X86_64_NONE. */
14034 case EM_XC16X:
14035 case EM_Z80: /* R_Z80_NONE. */
14036 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14037 return reloc_type == 0;
14038
14039 case EM_AARCH64:
14040 return reloc_type == 0 || reloc_type == 256;
14041 case EM_AVR_OLD:
14042 case EM_AVR:
14043 return (reloc_type == 0 /* R_AVR_NONE. */
14044 || reloc_type == 30 /* R_AVR_DIFF8. */
14045 || reloc_type == 31 /* R_AVR_DIFF16. */
14046 || reloc_type == 32 /* R_AVR_DIFF32. */);
14047 case EM_METAG:
14048 return reloc_type == 3; /* R_METAG_NONE. */
14049 case EM_NDS32:
14050 return (reloc_type == 0 /* R_XTENSA_NONE. */
14051 || reloc_type == 204 /* R_NDS32_DIFF8. */
14052 || reloc_type == 205 /* R_NDS32_DIFF16. */
14053 || reloc_type == 206 /* R_NDS32_DIFF32. */
14054 || reloc_type == 207 /* R_NDS32_ULEB128. */);
14055 case EM_TI_PRU:
14056 return (reloc_type == 0 /* R_PRU_NONE. */
14057 || reloc_type == 65 /* R_PRU_DIFF8. */
14058 || reloc_type == 66 /* R_PRU_DIFF16. */
14059 || reloc_type == 67 /* R_PRU_DIFF32. */);
14060 case EM_XTENSA_OLD:
14061 case EM_XTENSA:
14062 return (reloc_type == 0 /* R_XTENSA_NONE. */
14063 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14064 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14065 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14066 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14067 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14068 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14069 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14070 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14071 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14072 }
14073 return FALSE;
14074 }
14075
14076 /* Returns TRUE if there is a relocation against
14077 section NAME at OFFSET bytes. */
14078
14079 bfd_boolean
14080 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14081 {
14082 Elf_Internal_Rela * relocs;
14083 Elf_Internal_Rela * rp;
14084
14085 if (dsec == NULL || dsec->reloc_info == NULL)
14086 return FALSE;
14087
14088 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14089
14090 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14091 if (rp->r_offset == offset)
14092 return TRUE;
14093
14094 return FALSE;
14095 }
14096
14097 /* Apply relocations to a section.
14098 Returns TRUE upon success, FALSE otherwise.
14099 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14100 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14101 will be set to the number of relocs loaded.
14102
14103 Note: So far support has been added only for those relocations
14104 which can be found in debug sections. FIXME: Add support for
14105 more relocations ? */
14106
14107 static bfd_boolean
14108 apply_relocations (Filedata * filedata,
14109 const Elf_Internal_Shdr * section,
14110 unsigned char * start,
14111 bfd_size_type size,
14112 void ** relocs_return,
14113 unsigned long * num_relocs_return)
14114 {
14115 Elf_Internal_Shdr * relsec;
14116 unsigned char * end = start + size;
14117
14118 if (relocs_return != NULL)
14119 {
14120 * (Elf_Internal_Rela **) relocs_return = NULL;
14121 * num_relocs_return = 0;
14122 }
14123
14124 if (filedata->file_header.e_type != ET_REL)
14125 /* No relocs to apply. */
14126 return TRUE;
14127
14128 /* Find the reloc section associated with the section. */
14129 for (relsec = filedata->section_headers;
14130 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14131 ++relsec)
14132 {
14133 bfd_boolean is_rela;
14134 unsigned long num_relocs;
14135 Elf_Internal_Rela * relocs;
14136 Elf_Internal_Rela * rp;
14137 Elf_Internal_Shdr * symsec;
14138 Elf_Internal_Sym * symtab;
14139 unsigned long num_syms;
14140 Elf_Internal_Sym * sym;
14141
14142 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14143 || relsec->sh_info >= filedata->file_header.e_shnum
14144 || filedata->section_headers + relsec->sh_info != section
14145 || relsec->sh_size == 0
14146 || relsec->sh_link >= filedata->file_header.e_shnum)
14147 continue;
14148
14149 symsec = filedata->section_headers + relsec->sh_link;
14150 if (symsec->sh_type != SHT_SYMTAB
14151 && symsec->sh_type != SHT_DYNSYM)
14152 return FALSE;
14153
14154 is_rela = relsec->sh_type == SHT_RELA;
14155
14156 if (is_rela)
14157 {
14158 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14159 relsec->sh_size, & relocs, & num_relocs))
14160 return FALSE;
14161 }
14162 else
14163 {
14164 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14165 relsec->sh_size, & relocs, & num_relocs))
14166 return FALSE;
14167 }
14168
14169 /* SH uses RELA but uses in place value instead of the addend field. */
14170 if (filedata->file_header.e_machine == EM_SH)
14171 is_rela = FALSE;
14172
14173 symtab = GET_ELF_SYMBOLS (filedata, symsec, & num_syms);
14174
14175 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14176 {
14177 bfd_vma addend;
14178 unsigned int reloc_type;
14179 unsigned int reloc_size;
14180 bfd_boolean reloc_inplace = FALSE;
14181 bfd_boolean reloc_subtract = FALSE;
14182 unsigned char * rloc;
14183 unsigned long sym_index;
14184
14185 reloc_type = get_reloc_type (filedata, rp->r_info);
14186
14187 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14188 continue;
14189 else if (is_none_reloc (filedata, reloc_type))
14190 continue;
14191 else if (is_32bit_abs_reloc (filedata, reloc_type)
14192 || is_32bit_pcrel_reloc (filedata, reloc_type))
14193 reloc_size = 4;
14194 else if (is_64bit_abs_reloc (filedata, reloc_type)
14195 || is_64bit_pcrel_reloc (filedata, reloc_type))
14196 reloc_size = 8;
14197 else if (is_24bit_abs_reloc (filedata, reloc_type))
14198 reloc_size = 3;
14199 else if (is_16bit_abs_reloc (filedata, reloc_type))
14200 reloc_size = 2;
14201 else if (is_8bit_abs_reloc (filedata, reloc_type)
14202 || is_6bit_abs_reloc (filedata, reloc_type))
14203 reloc_size = 1;
14204 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14205 reloc_type))
14206 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14207 {
14208 reloc_size = 4;
14209 reloc_inplace = TRUE;
14210 }
14211 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14212 reloc_type))
14213 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14214 {
14215 reloc_size = 8;
14216 reloc_inplace = TRUE;
14217 }
14218 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14219 reloc_type))
14220 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14221 {
14222 reloc_size = 2;
14223 reloc_inplace = TRUE;
14224 }
14225 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14226 reloc_type))
14227 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14228 {
14229 reloc_size = 1;
14230 reloc_inplace = TRUE;
14231 }
14232 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14233 reloc_type)))
14234 {
14235 reloc_size = 1;
14236 reloc_inplace = TRUE;
14237 }
14238 else
14239 {
14240 static unsigned int prev_reloc = 0;
14241
14242 if (reloc_type != prev_reloc)
14243 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
14244 reloc_type, printable_section_name (filedata, section));
14245 prev_reloc = reloc_type;
14246 continue;
14247 }
14248
14249 rloc = start + rp->r_offset;
14250 if (!IN_RANGE (start, end, rloc, reloc_size))
14251 {
14252 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14253 (unsigned long) rp->r_offset,
14254 printable_section_name (filedata, section));
14255 continue;
14256 }
14257
14258 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14259 if (sym_index >= num_syms)
14260 {
14261 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
14262 sym_index, printable_section_name (filedata, section));
14263 continue;
14264 }
14265 sym = symtab + sym_index;
14266
14267 /* If the reloc has a symbol associated with it,
14268 make sure that it is of an appropriate type.
14269
14270 Relocations against symbols without type can happen.
14271 Gcc -feliminate-dwarf2-dups may generate symbols
14272 without type for debug info.
14273
14274 Icc generates relocations against function symbols
14275 instead of local labels.
14276
14277 Relocations against object symbols can happen, eg when
14278 referencing a global array. For an example of this see
14279 the _clz.o binary in libgcc.a. */
14280 if (sym != symtab
14281 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
14282 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
14283 {
14284 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
14285 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14286 printable_section_name (filedata, relsec),
14287 (long int)(rp - relocs));
14288 continue;
14289 }
14290
14291 addend = 0;
14292 if (is_rela)
14293 addend += rp->r_addend;
14294 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14295 partial_inplace. */
14296 if (!is_rela
14297 || (filedata->file_header.e_machine == EM_XTENSA
14298 && reloc_type == 1)
14299 || ((filedata->file_header.e_machine == EM_PJ
14300 || filedata->file_header.e_machine == EM_PJ_OLD)
14301 && reloc_type == 1)
14302 || ((filedata->file_header.e_machine == EM_D30V
14303 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
14304 && reloc_type == 12)
14305 || reloc_inplace)
14306 {
14307 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14308 addend += byte_get (rloc, reloc_size) & 0x3f;
14309 else
14310 addend += byte_get (rloc, reloc_size);
14311 }
14312
14313 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14314 || is_64bit_pcrel_reloc (filedata, reloc_type))
14315 {
14316 /* On HPPA, all pc-relative relocations are biased by 8. */
14317 if (filedata->file_header.e_machine == EM_PARISC)
14318 addend -= 8;
14319 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
14320 reloc_size);
14321 }
14322 else if (is_6bit_abs_reloc (filedata, reloc_type)
14323 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14324 {
14325 if (reloc_subtract)
14326 addend -= sym->st_value;
14327 else
14328 addend += sym->st_value;
14329 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14330 byte_put (rloc, addend, reloc_size);
14331 }
14332 else if (reloc_subtract)
14333 byte_put (rloc, addend - sym->st_value, reloc_size);
14334 else
14335 byte_put (rloc, addend + sym->st_value, reloc_size);
14336 }
14337
14338 free (symtab);
14339 /* Let the target specific reloc processing code know that
14340 we have finished with these relocs. */
14341 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
14342
14343 if (relocs_return)
14344 {
14345 * (Elf_Internal_Rela **) relocs_return = relocs;
14346 * num_relocs_return = num_relocs;
14347 }
14348 else
14349 free (relocs);
14350
14351 break;
14352 }
14353
14354 return TRUE;
14355 }
14356
14357 #ifdef SUPPORT_DISASSEMBLY
14358 static bfd_boolean
14359 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
14360 {
14361 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
14362
14363 /* FIXME: XXX -- to be done --- XXX */
14364
14365 return TRUE;
14366 }
14367 #endif
14368
14369 /* Reads in the contents of SECTION from FILE, returning a pointer
14370 to a malloc'ed buffer or NULL if something went wrong. */
14371
14372 static char *
14373 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
14374 {
14375 bfd_size_type num_bytes = section->sh_size;
14376
14377 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14378 {
14379 printf (_("Section '%s' has no data to dump.\n"),
14380 printable_section_name (filedata, section));
14381 return NULL;
14382 }
14383
14384 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
14385 _("section contents"));
14386 }
14387
14388 /* Uncompresses a section that was compressed using zlib, in place. */
14389
14390 static bfd_boolean
14391 uncompress_section_contents (unsigned char ** buffer,
14392 dwarf_size_type uncompressed_size,
14393 dwarf_size_type * size)
14394 {
14395 dwarf_size_type compressed_size = *size;
14396 unsigned char * compressed_buffer = *buffer;
14397 unsigned char * uncompressed_buffer;
14398 z_stream strm;
14399 int rc;
14400
14401 /* It is possible the section consists of several compressed
14402 buffers concatenated together, so we uncompress in a loop. */
14403 /* PR 18313: The state field in the z_stream structure is supposed
14404 to be invisible to the user (ie us), but some compilers will
14405 still complain about it being used without initialisation. So
14406 we first zero the entire z_stream structure and then set the fields
14407 that we need. */
14408 memset (& strm, 0, sizeof strm);
14409 strm.avail_in = compressed_size;
14410 strm.next_in = (Bytef *) compressed_buffer;
14411 strm.avail_out = uncompressed_size;
14412 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
14413
14414 rc = inflateInit (& strm);
14415 while (strm.avail_in > 0)
14416 {
14417 if (rc != Z_OK)
14418 break;
14419 strm.next_out = ((Bytef *) uncompressed_buffer
14420 + (uncompressed_size - strm.avail_out));
14421 rc = inflate (&strm, Z_FINISH);
14422 if (rc != Z_STREAM_END)
14423 break;
14424 rc = inflateReset (& strm);
14425 }
14426 if (inflateEnd (& strm) != Z_OK
14427 || rc != Z_OK
14428 || strm.avail_out != 0)
14429 goto fail;
14430
14431 *buffer = uncompressed_buffer;
14432 *size = uncompressed_size;
14433 return TRUE;
14434
14435 fail:
14436 free (uncompressed_buffer);
14437 /* Indicate decompression failure. */
14438 *buffer = NULL;
14439 return FALSE;
14440 }
14441
14442 static bfd_boolean
14443 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
14444 {
14445 Elf_Internal_Shdr * relsec;
14446 bfd_size_type num_bytes;
14447 unsigned char * data;
14448 unsigned char * end;
14449 unsigned char * real_start;
14450 unsigned char * start;
14451 bfd_boolean some_strings_shown;
14452
14453 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14454 if (start == NULL)
14455 /* PR 21820: Do not fail if the section was empty. */
14456 return (section->sh_size == 0 || section->sh_type == SHT_NOBITS) ? TRUE : FALSE;
14457
14458 num_bytes = section->sh_size;
14459
14460 printf (_("\nString dump of section '%s':\n"), printable_section_name (filedata, section));
14461
14462 if (decompress_dumps)
14463 {
14464 dwarf_size_type new_size = num_bytes;
14465 dwarf_size_type uncompressed_size = 0;
14466
14467 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14468 {
14469 Elf_Internal_Chdr chdr;
14470 unsigned int compression_header_size
14471 = get_compression_header (& chdr, (unsigned char *) start,
14472 num_bytes);
14473 if (compression_header_size == 0)
14474 /* An error message will have already been generated
14475 by get_compression_header. */
14476 goto error_out;
14477
14478 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14479 {
14480 warn (_("section '%s' has unsupported compress type: %d\n"),
14481 printable_section_name (filedata, section), chdr.ch_type);
14482 goto error_out;
14483 }
14484 uncompressed_size = chdr.ch_size;
14485 start += compression_header_size;
14486 new_size -= compression_header_size;
14487 }
14488 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14489 {
14490 /* Read the zlib header. In this case, it should be "ZLIB"
14491 followed by the uncompressed section size, 8 bytes in
14492 big-endian order. */
14493 uncompressed_size = start[4]; uncompressed_size <<= 8;
14494 uncompressed_size += start[5]; uncompressed_size <<= 8;
14495 uncompressed_size += start[6]; uncompressed_size <<= 8;
14496 uncompressed_size += start[7]; uncompressed_size <<= 8;
14497 uncompressed_size += start[8]; uncompressed_size <<= 8;
14498 uncompressed_size += start[9]; uncompressed_size <<= 8;
14499 uncompressed_size += start[10]; uncompressed_size <<= 8;
14500 uncompressed_size += start[11];
14501 start += 12;
14502 new_size -= 12;
14503 }
14504
14505 if (uncompressed_size)
14506 {
14507 if (uncompress_section_contents (& start,
14508 uncompressed_size, & new_size))
14509 num_bytes = new_size;
14510 else
14511 {
14512 error (_("Unable to decompress section %s\n"),
14513 printable_section_name (filedata, section));
14514 goto error_out;
14515 }
14516 }
14517 else
14518 start = real_start;
14519 }
14520
14521 /* If the section being dumped has relocations against it the user might
14522 be expecting these relocations to have been applied. Check for this
14523 case and issue a warning message in order to avoid confusion.
14524 FIXME: Maybe we ought to have an option that dumps a section with
14525 relocs applied ? */
14526 for (relsec = filedata->section_headers;
14527 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14528 ++relsec)
14529 {
14530 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14531 || relsec->sh_info >= filedata->file_header.e_shnum
14532 || filedata->section_headers + relsec->sh_info != section
14533 || relsec->sh_size == 0
14534 || relsec->sh_link >= filedata->file_header.e_shnum)
14535 continue;
14536
14537 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14538 break;
14539 }
14540
14541 data = start;
14542 end = start + num_bytes;
14543 some_strings_shown = FALSE;
14544
14545 #ifdef HAVE_MBSTATE_T
14546 mbstate_t state;
14547 /* Initialise the multibyte conversion state. */
14548 memset (& state, 0, sizeof (state));
14549 #endif
14550
14551 bfd_boolean continuing = FALSE;
14552
14553 while (data < end)
14554 {
14555 while (!ISPRINT (* data))
14556 if (++ data >= end)
14557 break;
14558
14559 if (data < end)
14560 {
14561 size_t maxlen = end - data;
14562
14563 if (continuing)
14564 {
14565 printf (" ");
14566 continuing = FALSE;
14567 }
14568 else
14569 {
14570 printf (" [%6lx] ", (unsigned long) (data - start));
14571 }
14572
14573 if (maxlen > 0)
14574 {
14575 char c = 0;
14576
14577 while (maxlen)
14578 {
14579 c = *data++;
14580
14581 if (c == 0)
14582 break;
14583
14584 /* PR 25543: Treat new-lines as string-ending characters. */
14585 if (c == '\n')
14586 {
14587 printf ("\\n\n");
14588 if (*data != 0)
14589 continuing = TRUE;
14590 break;
14591 }
14592
14593 /* Do not print control characters directly as they can affect terminal
14594 settings. Such characters usually appear in the names generated
14595 by the assembler for local labels. */
14596 if (ISCNTRL (c))
14597 {
14598 printf ("^%c", c + 0x40);
14599 }
14600 else if (ISPRINT (c))
14601 {
14602 putchar (c);
14603 }
14604 else
14605 {
14606 size_t n;
14607 #ifdef HAVE_MBSTATE_T
14608 wchar_t w;
14609 #endif
14610 /* Let printf do the hard work of displaying multibyte characters. */
14611 printf ("%.1s", data - 1);
14612 #ifdef HAVE_MBSTATE_T
14613 /* Try to find out how many bytes made up the character that was
14614 just printed. Advance the symbol pointer past the bytes that
14615 were displayed. */
14616 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
14617 #else
14618 n = 1;
14619 #endif
14620 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
14621 data += (n - 1);
14622 }
14623 }
14624
14625 if (c != '\n')
14626 putchar ('\n');
14627 }
14628 else
14629 {
14630 printf (_("<corrupt>\n"));
14631 data = end;
14632 }
14633 some_strings_shown = TRUE;
14634 }
14635 }
14636
14637 if (! some_strings_shown)
14638 printf (_(" No strings found in this section."));
14639
14640 free (real_start);
14641
14642 putchar ('\n');
14643 return TRUE;
14644
14645 error_out:
14646 free (real_start);
14647 return FALSE;
14648 }
14649
14650 static bfd_boolean
14651 dump_section_as_bytes (Elf_Internal_Shdr * section,
14652 Filedata * filedata,
14653 bfd_boolean relocate)
14654 {
14655 Elf_Internal_Shdr * relsec;
14656 bfd_size_type bytes;
14657 bfd_size_type section_size;
14658 bfd_vma addr;
14659 unsigned char * data;
14660 unsigned char * real_start;
14661 unsigned char * start;
14662
14663 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14664 if (start == NULL)
14665 /* PR 21820: Do not fail if the section was empty. */
14666 return (section->sh_size == 0 || section->sh_type == SHT_NOBITS) ? TRUE : FALSE;
14667
14668 section_size = section->sh_size;
14669
14670 printf (_("\nHex dump of section '%s':\n"), printable_section_name (filedata, section));
14671
14672 if (decompress_dumps)
14673 {
14674 dwarf_size_type new_size = section_size;
14675 dwarf_size_type uncompressed_size = 0;
14676
14677 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14678 {
14679 Elf_Internal_Chdr chdr;
14680 unsigned int compression_header_size
14681 = get_compression_header (& chdr, start, section_size);
14682
14683 if (compression_header_size == 0)
14684 /* An error message will have already been generated
14685 by get_compression_header. */
14686 goto error_out;
14687
14688 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14689 {
14690 warn (_("section '%s' has unsupported compress type: %d\n"),
14691 printable_section_name (filedata, section), chdr.ch_type);
14692 goto error_out;
14693 }
14694 uncompressed_size = chdr.ch_size;
14695 start += compression_header_size;
14696 new_size -= compression_header_size;
14697 }
14698 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14699 {
14700 /* Read the zlib header. In this case, it should be "ZLIB"
14701 followed by the uncompressed section size, 8 bytes in
14702 big-endian order. */
14703 uncompressed_size = start[4]; uncompressed_size <<= 8;
14704 uncompressed_size += start[5]; uncompressed_size <<= 8;
14705 uncompressed_size += start[6]; uncompressed_size <<= 8;
14706 uncompressed_size += start[7]; uncompressed_size <<= 8;
14707 uncompressed_size += start[8]; uncompressed_size <<= 8;
14708 uncompressed_size += start[9]; uncompressed_size <<= 8;
14709 uncompressed_size += start[10]; uncompressed_size <<= 8;
14710 uncompressed_size += start[11];
14711 start += 12;
14712 new_size -= 12;
14713 }
14714
14715 if (uncompressed_size)
14716 {
14717 if (uncompress_section_contents (& start, uncompressed_size,
14718 & new_size))
14719 {
14720 section_size = new_size;
14721 }
14722 else
14723 {
14724 error (_("Unable to decompress section %s\n"),
14725 printable_section_name (filedata, section));
14726 /* FIXME: Print the section anyway ? */
14727 goto error_out;
14728 }
14729 }
14730 else
14731 start = real_start;
14732 }
14733
14734 if (relocate)
14735 {
14736 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
14737 goto error_out;
14738 }
14739 else
14740 {
14741 /* If the section being dumped has relocations against it the user might
14742 be expecting these relocations to have been applied. Check for this
14743 case and issue a warning message in order to avoid confusion.
14744 FIXME: Maybe we ought to have an option that dumps a section with
14745 relocs applied ? */
14746 for (relsec = filedata->section_headers;
14747 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14748 ++relsec)
14749 {
14750 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14751 || relsec->sh_info >= filedata->file_header.e_shnum
14752 || filedata->section_headers + relsec->sh_info != section
14753 || relsec->sh_size == 0
14754 || relsec->sh_link >= filedata->file_header.e_shnum)
14755 continue;
14756
14757 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14758 break;
14759 }
14760 }
14761
14762 addr = section->sh_addr;
14763 bytes = section_size;
14764 data = start;
14765
14766 while (bytes)
14767 {
14768 int j;
14769 int k;
14770 int lbytes;
14771
14772 lbytes = (bytes > 16 ? 16 : bytes);
14773
14774 printf (" 0x%8.8lx ", (unsigned long) addr);
14775
14776 for (j = 0; j < 16; j++)
14777 {
14778 if (j < lbytes)
14779 printf ("%2.2x", data[j]);
14780 else
14781 printf (" ");
14782
14783 if ((j & 3) == 3)
14784 printf (" ");
14785 }
14786
14787 for (j = 0; j < lbytes; j++)
14788 {
14789 k = data[j];
14790 if (k >= ' ' && k < 0x7f)
14791 printf ("%c", k);
14792 else
14793 printf (".");
14794 }
14795
14796 putchar ('\n');
14797
14798 data += lbytes;
14799 addr += lbytes;
14800 bytes -= lbytes;
14801 }
14802
14803 free (real_start);
14804
14805 putchar ('\n');
14806 return TRUE;
14807
14808 error_out:
14809 free (real_start);
14810 return FALSE;
14811 }
14812
14813 #ifdef ENABLE_LIBCTF
14814 static ctf_sect_t *
14815 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
14816 {
14817 buf->cts_name = SECTION_NAME_PRINT (shdr);
14818 buf->cts_size = shdr->sh_size;
14819 buf->cts_entsize = shdr->sh_entsize;
14820
14821 return buf;
14822 }
14823
14824 /* Formatting callback function passed to ctf_dump. Returns either the pointer
14825 it is passed, or a pointer to newly-allocated storage, in which case
14826 dump_ctf() will free it when it no longer needs it. */
14827
14828 static char *
14829 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
14830 char *s, void *arg)
14831 {
14832 const char *blanks = arg;
14833 char *new_s;
14834
14835 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
14836 return s;
14837 return new_s;
14838 }
14839
14840 /* Dump CTF errors/warnings. */
14841 static void
14842 dump_ctf_errs (ctf_dict_t *fp)
14843 {
14844 ctf_next_t *it = NULL;
14845 char *errtext;
14846 int is_warning;
14847 int err;
14848
14849 /* Dump accumulated errors and warnings. */
14850 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
14851 {
14852 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
14853 errtext);
14854 free (errtext);
14855 }
14856 if (err != ECTF_NEXT_END)
14857 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
14858 }
14859
14860 /* Dump one CTF archive member. */
14861
14862 static int
14863 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
14864 {
14865 ctf_dict_t *parent = (ctf_dict_t *) arg;
14866 const char *things[] = {"Header", "Labels", "Data objects",
14867 "Function objects", "Variables", "Types", "Strings",
14868 ""};
14869 const char **thing;
14870 size_t i;
14871 int err = 0;
14872
14873 /* Only print out the name of non-default-named archive members.
14874 The name .ctf appears everywhere, even for things that aren't
14875 really archives, so printing it out is liable to be confusing.
14876
14877 The parent, if there is one, is the default-owned archive member:
14878 avoid importing it into itself. (This does no harm, but looks
14879 confusing.) */
14880
14881 if (strcmp (name, ".ctf") != 0)
14882 {
14883 printf (_("\nCTF archive member: %s:\n"), name);
14884 ctf_import (ctf, parent);
14885 }
14886
14887 for (i = 0, thing = things; *thing[0]; thing++, i++)
14888 {
14889 ctf_dump_state_t *s = NULL;
14890 char *item;
14891
14892 printf ("\n %s:\n", *thing);
14893 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
14894 (void *) " ")) != NULL)
14895 {
14896 printf ("%s\n", item);
14897 free (item);
14898 }
14899
14900 if (ctf_errno (ctf))
14901 {
14902 error (_("Iteration failed: %s, %s\n"), *thing,
14903 ctf_errmsg (ctf_errno (ctf)));
14904 err = 1;
14905 goto out;
14906 }
14907 }
14908
14909 out:
14910 dump_ctf_errs (ctf);
14911 return err;
14912 }
14913
14914 static bfd_boolean
14915 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
14916 {
14917 Elf_Internal_Shdr * parent_sec = NULL;
14918 Elf_Internal_Shdr * symtab_sec = NULL;
14919 Elf_Internal_Shdr * strtab_sec = NULL;
14920 void * data = NULL;
14921 void * symdata = NULL;
14922 void * strdata = NULL;
14923 void * parentdata = NULL;
14924 ctf_sect_t ctfsect, symsect, strsect, parentsect;
14925 ctf_sect_t * symsectp = NULL;
14926 ctf_sect_t * strsectp = NULL;
14927 ctf_archive_t * ctfa = NULL;
14928 ctf_archive_t * parenta = NULL, *lookparent;
14929 ctf_dict_t * parent = NULL;
14930
14931 int err;
14932 bfd_boolean ret = FALSE;
14933
14934 shdr_to_ctf_sect (&ctfsect, section, filedata);
14935 data = get_section_contents (section, filedata);
14936 ctfsect.cts_data = data;
14937
14938 if (!dump_ctf_symtab_name)
14939 dump_ctf_symtab_name = strdup (".dynsym");
14940
14941 if (!dump_ctf_strtab_name)
14942 dump_ctf_strtab_name = strdup (".dynstr");
14943
14944 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
14945 {
14946 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
14947 {
14948 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
14949 goto fail;
14950 }
14951 if ((symdata = (void *) get_data (NULL, filedata,
14952 symtab_sec->sh_offset, 1,
14953 symtab_sec->sh_size,
14954 _("symbols"))) == NULL)
14955 goto fail;
14956 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
14957 symsect.cts_data = symdata;
14958 }
14959 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
14960 {
14961 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
14962 {
14963 error (_("No string table section named %s\n"),
14964 dump_ctf_strtab_name);
14965 goto fail;
14966 }
14967 if ((strdata = (void *) get_data (NULL, filedata,
14968 strtab_sec->sh_offset, 1,
14969 strtab_sec->sh_size,
14970 _("strings"))) == NULL)
14971 goto fail;
14972 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
14973 strsect.cts_data = strdata;
14974 }
14975 if (dump_ctf_parent_name)
14976 {
14977 if ((parent_sec = find_section (filedata, dump_ctf_parent_name)) == NULL)
14978 {
14979 error (_("No CTF parent section named %s\n"), dump_ctf_parent_name);
14980 goto fail;
14981 }
14982 if ((parentdata = (void *) get_data (NULL, filedata,
14983 parent_sec->sh_offset, 1,
14984 parent_sec->sh_size,
14985 _("CTF parent"))) == NULL)
14986 goto fail;
14987 shdr_to_ctf_sect (&parentsect, parent_sec, filedata);
14988 parentsect.cts_data = parentdata;
14989 }
14990
14991 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
14992 libctf papers over the difference, so we can pretend it is always an
14993 archive. Possibly open the parent as well, if one was specified. */
14994
14995 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
14996 {
14997 dump_ctf_errs (NULL);
14998 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
14999 goto fail;
15000 }
15001
15002 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15003 != ELFDATA2MSB);
15004
15005 if (parentdata)
15006 {
15007 if ((parenta = ctf_arc_bufopen (&parentsect, symsectp, strsectp,
15008 &err)) == NULL)
15009 {
15010 dump_ctf_errs (NULL);
15011 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15012 goto fail;
15013 }
15014 lookparent = parenta;
15015 }
15016 else
15017 lookparent = ctfa;
15018
15019 /* Assume that the applicable parent archive member is the default one.
15020 (This is what all known implementations are expected to do, if they
15021 put CTFs and their parents in archives together.) */
15022 if ((parent = ctf_dict_open (lookparent, NULL, &err)) == NULL)
15023 {
15024 dump_ctf_errs (NULL);
15025 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15026 goto fail;
15027 }
15028
15029 ret = TRUE;
15030
15031 printf (_("\nDump of CTF section '%s':\n"),
15032 printable_section_name (filedata, section));
15033
15034 if ((err = ctf_archive_iter (ctfa, dump_ctf_archive_member, parent)) != 0)
15035 {
15036 dump_ctf_errs (NULL);
15037 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15038 ret = FALSE;
15039 }
15040
15041 fail:
15042 ctf_dict_close (parent);
15043 ctf_close (ctfa);
15044 ctf_close (parenta);
15045 free (parentdata);
15046 free (data);
15047 free (symdata);
15048 free (strdata);
15049 return ret;
15050 }
15051 #endif
15052
15053 static bfd_boolean
15054 load_specific_debug_section (enum dwarf_section_display_enum debug,
15055 const Elf_Internal_Shdr * sec,
15056 void * data)
15057 {
15058 struct dwarf_section * section = &debug_displays [debug].section;
15059 char buf [64];
15060 Filedata * filedata = (Filedata *) data;
15061
15062 if (section->start != NULL)
15063 {
15064 /* If it is already loaded, do nothing. */
15065 if (streq (section->filename, filedata->file_name))
15066 return TRUE;
15067 free (section->start);
15068 }
15069
15070 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15071 section->address = sec->sh_addr;
15072 section->filename = filedata->file_name;
15073 section->start = (unsigned char *) get_data (NULL, filedata,
15074 sec->sh_offset, 1,
15075 sec->sh_size, buf);
15076 if (section->start == NULL)
15077 section->size = 0;
15078 else
15079 {
15080 unsigned char *start = section->start;
15081 dwarf_size_type size = sec->sh_size;
15082 dwarf_size_type uncompressed_size = 0;
15083
15084 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15085 {
15086 Elf_Internal_Chdr chdr;
15087 unsigned int compression_header_size;
15088
15089 if (size < (is_32bit_elf
15090 ? sizeof (Elf32_External_Chdr)
15091 : sizeof (Elf64_External_Chdr)))
15092 {
15093 warn (_("compressed section %s is too small to contain a compression header\n"),
15094 section->name);
15095 return FALSE;
15096 }
15097
15098 compression_header_size = get_compression_header (&chdr, start, size);
15099 if (compression_header_size == 0)
15100 /* An error message will have already been generated
15101 by get_compression_header. */
15102 return FALSE;
15103
15104 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15105 {
15106 warn (_("section '%s' has unsupported compress type: %d\n"),
15107 section->name, chdr.ch_type);
15108 return FALSE;
15109 }
15110 uncompressed_size = chdr.ch_size;
15111 start += compression_header_size;
15112 size -= compression_header_size;
15113 }
15114 else if (size > 12 && streq ((char *) start, "ZLIB"))
15115 {
15116 /* Read the zlib header. In this case, it should be "ZLIB"
15117 followed by the uncompressed section size, 8 bytes in
15118 big-endian order. */
15119 uncompressed_size = start[4]; uncompressed_size <<= 8;
15120 uncompressed_size += start[5]; uncompressed_size <<= 8;
15121 uncompressed_size += start[6]; uncompressed_size <<= 8;
15122 uncompressed_size += start[7]; uncompressed_size <<= 8;
15123 uncompressed_size += start[8]; uncompressed_size <<= 8;
15124 uncompressed_size += start[9]; uncompressed_size <<= 8;
15125 uncompressed_size += start[10]; uncompressed_size <<= 8;
15126 uncompressed_size += start[11];
15127 start += 12;
15128 size -= 12;
15129 }
15130
15131 if (uncompressed_size)
15132 {
15133 if (uncompress_section_contents (&start, uncompressed_size,
15134 &size))
15135 {
15136 /* Free the compressed buffer, update the section buffer
15137 and the section size if uncompress is successful. */
15138 free (section->start);
15139 section->start = start;
15140 }
15141 else
15142 {
15143 error (_("Unable to decompress section %s\n"),
15144 printable_section_name (filedata, sec));
15145 return FALSE;
15146 }
15147 }
15148
15149 section->size = size;
15150 }
15151
15152 if (section->start == NULL)
15153 return FALSE;
15154
15155 if (debug_displays [debug].relocate)
15156 {
15157 if (! apply_relocations (filedata, sec, section->start, section->size,
15158 & section->reloc_info, & section->num_relocs))
15159 return FALSE;
15160 }
15161 else
15162 {
15163 section->reloc_info = NULL;
15164 section->num_relocs = 0;
15165 }
15166
15167 return TRUE;
15168 }
15169
15170 #if HAVE_LIBDEBUGINFOD
15171 /* Return a hex string representation of the build-id. */
15172 unsigned char *
15173 get_build_id (void * data)
15174 {
15175 Filedata * filedata = (Filedata *) data;
15176 Elf_Internal_Shdr * shdr;
15177 unsigned long i;
15178
15179 /* Iterate through notes to find note.gnu.build-id.
15180 FIXME: Only the first note in any note section is examined. */
15181 for (i = 0, shdr = filedata->section_headers;
15182 i < filedata->file_header.e_shnum && shdr != NULL;
15183 i++, shdr++)
15184 {
15185 if (shdr->sh_type != SHT_NOTE)
15186 continue;
15187
15188 char * next;
15189 char * end;
15190 size_t data_remaining;
15191 size_t min_notesz;
15192 Elf_External_Note * enote;
15193 Elf_Internal_Note inote;
15194
15195 bfd_vma offset = shdr->sh_offset;
15196 bfd_vma align = shdr->sh_addralign;
15197 bfd_vma length = shdr->sh_size;
15198
15199 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15200 if (enote == NULL)
15201 continue;
15202
15203 if (align < 4)
15204 align = 4;
15205 else if (align != 4 && align != 8)
15206 {
15207 free (enote);
15208 continue;
15209 }
15210
15211 end = (char *) enote + length;
15212 data_remaining = end - (char *) enote;
15213
15214 if (!is_ia64_vms (filedata))
15215 {
15216 min_notesz = offsetof (Elf_External_Note, name);
15217 if (data_remaining < min_notesz)
15218 {
15219 warn (_("\
15220 malformed note encountered in section %s whilst scanning for build-id note\n"),
15221 printable_section_name (filedata, shdr));
15222 free (enote);
15223 continue;
15224 }
15225 data_remaining -= min_notesz;
15226
15227 inote.type = BYTE_GET (enote->type);
15228 inote.namesz = BYTE_GET (enote->namesz);
15229 inote.namedata = enote->name;
15230 inote.descsz = BYTE_GET (enote->descsz);
15231 inote.descdata = ((char *) enote
15232 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15233 inote.descpos = offset + (inote.descdata - (char *) enote);
15234 next = ((char *) enote
15235 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15236 }
15237 else
15238 {
15239 Elf64_External_VMS_Note *vms_enote;
15240
15241 /* PR binutils/15191
15242 Make sure that there is enough data to read. */
15243 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15244 if (data_remaining < min_notesz)
15245 {
15246 warn (_("\
15247 malformed note encountered in section %s whilst scanning for build-id note\n"),
15248 printable_section_name (filedata, shdr));
15249 free (enote);
15250 continue;
15251 }
15252 data_remaining -= min_notesz;
15253
15254 vms_enote = (Elf64_External_VMS_Note *) enote;
15255 inote.type = BYTE_GET (vms_enote->type);
15256 inote.namesz = BYTE_GET (vms_enote->namesz);
15257 inote.namedata = vms_enote->name;
15258 inote.descsz = BYTE_GET (vms_enote->descsz);
15259 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15260 inote.descpos = offset + (inote.descdata - (char *) enote);
15261 next = inote.descdata + align_power (inote.descsz, 3);
15262 }
15263
15264 /* Skip malformed notes. */
15265 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15266 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15267 || (size_t) (next - inote.descdata) < inote.descsz
15268 || ((size_t) (next - inote.descdata)
15269 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15270 {
15271 warn (_("\
15272 malformed note encountered in section %s whilst scanning for build-id note\n"),
15273 printable_section_name (filedata, shdr));
15274 free (enote);
15275 continue;
15276 }
15277
15278 /* Check if this is the build-id note. If so then convert the build-id
15279 bytes to a hex string. */
15280 if (inote.namesz > 0
15281 && const_strneq (inote.namedata, "GNU")
15282 && inote.type == NT_GNU_BUILD_ID)
15283 {
15284 unsigned long j;
15285 char * build_id;
15286
15287 build_id = malloc (inote.descsz * 2 + 1);
15288 if (build_id == NULL)
15289 {
15290 free (enote);
15291 return NULL;
15292 }
15293
15294 for (j = 0; j < inote.descsz; ++j)
15295 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15296 build_id[inote.descsz * 2] = '\0';
15297 free (enote);
15298
15299 return (unsigned char *) build_id;
15300 }
15301 free (enote);
15302 }
15303
15304 return NULL;
15305 }
15306 #endif /* HAVE_LIBDEBUGINFOD */
15307
15308 /* If this is not NULL, load_debug_section will only look for sections
15309 within the list of sections given here. */
15310 static unsigned int * section_subset = NULL;
15311
15312 bfd_boolean
15313 load_debug_section (enum dwarf_section_display_enum debug, void * data)
15314 {
15315 struct dwarf_section * section = &debug_displays [debug].section;
15316 Elf_Internal_Shdr * sec;
15317 Filedata * filedata = (Filedata *) data;
15318
15319 /* Without section headers we cannot find any sections. */
15320 if (filedata->section_headers == NULL)
15321 return FALSE;
15322
15323 if (filedata->string_table == NULL
15324 && filedata->file_header.e_shstrndx != SHN_UNDEF
15325 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
15326 {
15327 Elf_Internal_Shdr * strs;
15328
15329 /* Read in the string table, so that we have section names to scan. */
15330 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15331
15332 if (strs != NULL && strs->sh_size != 0)
15333 {
15334 filedata->string_table
15335 = (char *) get_data (NULL, filedata, strs->sh_offset,
15336 1, strs->sh_size, _("string table"));
15337
15338 filedata->string_table_length
15339 = filedata->string_table != NULL ? strs->sh_size : 0;
15340 }
15341 }
15342
15343 /* Locate the debug section. */
15344 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
15345 if (sec != NULL)
15346 section->name = section->uncompressed_name;
15347 else
15348 {
15349 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
15350 if (sec != NULL)
15351 section->name = section->compressed_name;
15352 }
15353 if (sec == NULL)
15354 return FALSE;
15355
15356 /* If we're loading from a subset of sections, and we've loaded
15357 a section matching this name before, it's likely that it's a
15358 different one. */
15359 if (section_subset != NULL)
15360 free_debug_section (debug);
15361
15362 return load_specific_debug_section (debug, sec, data);
15363 }
15364
15365 void
15366 free_debug_section (enum dwarf_section_display_enum debug)
15367 {
15368 struct dwarf_section * section = &debug_displays [debug].section;
15369
15370 if (section->start == NULL)
15371 return;
15372
15373 free ((char *) section->start);
15374 section->start = NULL;
15375 section->address = 0;
15376 section->size = 0;
15377
15378 free (section->reloc_info);
15379 section->reloc_info = NULL;
15380 section->num_relocs = 0;
15381 }
15382
15383 static bfd_boolean
15384 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
15385 {
15386 char * name = SECTION_NAME_VALID (section) ? SECTION_NAME (section) : "";
15387 const char * print_name = printable_section_name (filedata, section);
15388 bfd_size_type length;
15389 bfd_boolean result = TRUE;
15390 int i;
15391
15392 length = section->sh_size;
15393 if (length == 0)
15394 {
15395 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
15396 return TRUE;
15397 }
15398 if (section->sh_type == SHT_NOBITS)
15399 {
15400 /* There is no point in dumping the contents of a debugging section
15401 which has the NOBITS type - the bits in the file will be random.
15402 This can happen when a file containing a .eh_frame section is
15403 stripped with the --only-keep-debug command line option. */
15404 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
15405 print_name);
15406 return FALSE;
15407 }
15408
15409 if (const_strneq (name, ".gnu.linkonce.wi."))
15410 name = ".debug_info";
15411
15412 /* See if we know how to display the contents of this section. */
15413 for (i = 0; i < max; i++)
15414 {
15415 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
15416 struct dwarf_section_display * display = debug_displays + i;
15417 struct dwarf_section * sec = & display->section;
15418
15419 if (streq (sec->uncompressed_name, name)
15420 || (id == line && const_strneq (name, ".debug_line."))
15421 || streq (sec->compressed_name, name))
15422 {
15423 bfd_boolean secondary = (section != find_section (filedata, name));
15424
15425 if (secondary)
15426 free_debug_section (id);
15427
15428 if (i == line && const_strneq (name, ".debug_line."))
15429 sec->name = name;
15430 else if (streq (sec->uncompressed_name, name))
15431 sec->name = sec->uncompressed_name;
15432 else
15433 sec->name = sec->compressed_name;
15434
15435 if (load_specific_debug_section (id, section, filedata))
15436 {
15437 /* If this debug section is part of a CU/TU set in a .dwp file,
15438 restrict load_debug_section to the sections in that set. */
15439 section_subset = find_cu_tu_set (filedata, shndx);
15440
15441 result &= display->display (sec, filedata);
15442
15443 section_subset = NULL;
15444
15445 if (secondary || (id != info && id != abbrev))
15446 free_debug_section (id);
15447 }
15448 break;
15449 }
15450 }
15451
15452 if (i == max)
15453 {
15454 printf (_("Unrecognized debug section: %s\n"), print_name);
15455 result = FALSE;
15456 }
15457
15458 return result;
15459 }
15460
15461 /* Set DUMP_SECTS for all sections where dumps were requested
15462 based on section name. */
15463
15464 static void
15465 initialise_dumps_byname (Filedata * filedata)
15466 {
15467 struct dump_list_entry * cur;
15468
15469 for (cur = dump_sects_byname; cur; cur = cur->next)
15470 {
15471 unsigned int i;
15472 bfd_boolean any = FALSE;
15473
15474 for (i = 0; i < filedata->file_header.e_shnum; i++)
15475 if (SECTION_NAME_VALID (filedata->section_headers + i)
15476 && streq (SECTION_NAME (filedata->section_headers + i), cur->name))
15477 {
15478 request_dump_bynumber (&filedata->dump, i, cur->type);
15479 any = TRUE;
15480 }
15481
15482 if (!any)
15483 {
15484 if (filedata->is_separate)
15485 warn (_("Section '%s' in linked file '%s' was not dumped because it does not exist\n"),
15486 cur->name, filedata->file_name);
15487 else
15488 warn (_("Section '%s' was not dumped because it does not exist\n"),
15489 cur->name);
15490 }
15491 }
15492 }
15493
15494 static bfd_boolean
15495 process_section_contents (Filedata * filedata)
15496 {
15497 Elf_Internal_Shdr * section;
15498 unsigned int i;
15499 bfd_boolean res = TRUE;
15500
15501 if (! do_dump)
15502 return TRUE;
15503
15504 initialise_dumps_byname (filedata);
15505
15506 for (i = 0, section = filedata->section_headers;
15507 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
15508 i++, section++)
15509 {
15510 dump_type dump = filedata->dump.dump_sects[i];
15511
15512 #ifdef SUPPORT_DISASSEMBLY
15513 if (dump & DISASS_DUMP)
15514 {
15515 if (! disassemble_section (section, filedata))
15516 res = FALSE;
15517 }
15518 #endif
15519 if (dump & HEX_DUMP)
15520 {
15521 if (! dump_section_as_bytes (section, filedata, FALSE))
15522 res = FALSE;
15523 }
15524
15525 if (dump & RELOC_DUMP)
15526 {
15527 if (! dump_section_as_bytes (section, filedata, TRUE))
15528 res = FALSE;
15529 }
15530
15531 if (dump & STRING_DUMP)
15532 {
15533 if (! dump_section_as_strings (section, filedata))
15534 res = FALSE;
15535 }
15536
15537 if (dump & DEBUG_DUMP)
15538 {
15539 if (! display_debug_section (i, section, filedata))
15540 res = FALSE;
15541 }
15542
15543 #ifdef ENABLE_LIBCTF
15544 if (dump & CTF_DUMP)
15545 {
15546 if (! dump_section_as_ctf (section, filedata))
15547 res = FALSE;
15548 }
15549 #endif
15550 }
15551
15552 /* Check to see if the user requested a
15553 dump of a section that does not exist. */
15554 while (i < filedata->dump.num_dump_sects)
15555 {
15556 if (filedata->dump.dump_sects[i])
15557 {
15558 if (filedata->is_separate)
15559 warn (_("Section %d in linked file '%s' was not dumped because it does not exist!\n"),
15560 i, filedata->file_name);
15561 else
15562 warn (_("Section %d was not dumped because it does not exist!\n"), i);
15563 res = FALSE;
15564 }
15565 i++;
15566 }
15567
15568 return res;
15569 }
15570
15571 static void
15572 process_mips_fpe_exception (int mask)
15573 {
15574 if (mask)
15575 {
15576 bfd_boolean first = TRUE;
15577
15578 if (mask & OEX_FPU_INEX)
15579 fputs ("INEX", stdout), first = FALSE;
15580 if (mask & OEX_FPU_UFLO)
15581 printf ("%sUFLO", first ? "" : "|"), first = FALSE;
15582 if (mask & OEX_FPU_OFLO)
15583 printf ("%sOFLO", first ? "" : "|"), first = FALSE;
15584 if (mask & OEX_FPU_DIV0)
15585 printf ("%sDIV0", first ? "" : "|"), first = FALSE;
15586 if (mask & OEX_FPU_INVAL)
15587 printf ("%sINVAL", first ? "" : "|");
15588 }
15589 else
15590 fputs ("0", stdout);
15591 }
15592
15593 /* Display's the value of TAG at location P. If TAG is
15594 greater than 0 it is assumed to be an unknown tag, and
15595 a message is printed to this effect. Otherwise it is
15596 assumed that a message has already been printed.
15597
15598 If the bottom bit of TAG is set it assumed to have a
15599 string value, otherwise it is assumed to have an integer
15600 value.
15601
15602 Returns an updated P pointing to the first unread byte
15603 beyond the end of TAG's value.
15604
15605 Reads at or beyond END will not be made. */
15606
15607 static unsigned char *
15608 display_tag_value (signed int tag,
15609 unsigned char * p,
15610 const unsigned char * const end)
15611 {
15612 unsigned long val;
15613
15614 if (tag > 0)
15615 printf (" Tag_unknown_%d: ", tag);
15616
15617 if (p >= end)
15618 {
15619 warn (_("<corrupt tag>\n"));
15620 }
15621 else if (tag & 1)
15622 {
15623 /* PR 17531 file: 027-19978-0.004. */
15624 size_t maxlen = (end - p) - 1;
15625
15626 putchar ('"');
15627 if (maxlen > 0)
15628 {
15629 print_symbol ((int) maxlen, (const char *) p);
15630 p += strnlen ((char *) p, maxlen) + 1;
15631 }
15632 else
15633 {
15634 printf (_("<corrupt string tag>"));
15635 p = (unsigned char *) end;
15636 }
15637 printf ("\"\n");
15638 }
15639 else
15640 {
15641 READ_ULEB (val, p, end);
15642 printf ("%ld (0x%lx)\n", val, val);
15643 }
15644
15645 assert (p <= end);
15646 return p;
15647 }
15648
15649 /* ARC ABI attributes section. */
15650
15651 static unsigned char *
15652 display_arc_attribute (unsigned char * p,
15653 const unsigned char * const end)
15654 {
15655 unsigned int tag;
15656 unsigned int val;
15657
15658 READ_ULEB (tag, p, end);
15659
15660 switch (tag)
15661 {
15662 case Tag_ARC_PCS_config:
15663 READ_ULEB (val, p, end);
15664 printf (" Tag_ARC_PCS_config: ");
15665 switch (val)
15666 {
15667 case 0:
15668 printf (_("Absent/Non standard\n"));
15669 break;
15670 case 1:
15671 printf (_("Bare metal/mwdt\n"));
15672 break;
15673 case 2:
15674 printf (_("Bare metal/newlib\n"));
15675 break;
15676 case 3:
15677 printf (_("Linux/uclibc\n"));
15678 break;
15679 case 4:
15680 printf (_("Linux/glibc\n"));
15681 break;
15682 default:
15683 printf (_("Unknown\n"));
15684 break;
15685 }
15686 break;
15687
15688 case Tag_ARC_CPU_base:
15689 READ_ULEB (val, p, end);
15690 printf (" Tag_ARC_CPU_base: ");
15691 switch (val)
15692 {
15693 default:
15694 case TAG_CPU_NONE:
15695 printf (_("Absent\n"));
15696 break;
15697 case TAG_CPU_ARC6xx:
15698 printf ("ARC6xx\n");
15699 break;
15700 case TAG_CPU_ARC7xx:
15701 printf ("ARC7xx\n");
15702 break;
15703 case TAG_CPU_ARCEM:
15704 printf ("ARCEM\n");
15705 break;
15706 case TAG_CPU_ARCHS:
15707 printf ("ARCHS\n");
15708 break;
15709 }
15710 break;
15711
15712 case Tag_ARC_CPU_variation:
15713 READ_ULEB (val, p, end);
15714 printf (" Tag_ARC_CPU_variation: ");
15715 switch (val)
15716 {
15717 default:
15718 if (val > 0 && val < 16)
15719 printf ("Core%d\n", val);
15720 else
15721 printf ("Unknown\n");
15722 break;
15723
15724 case 0:
15725 printf (_("Absent\n"));
15726 break;
15727 }
15728 break;
15729
15730 case Tag_ARC_CPU_name:
15731 printf (" Tag_ARC_CPU_name: ");
15732 p = display_tag_value (-1, p, end);
15733 break;
15734
15735 case Tag_ARC_ABI_rf16:
15736 READ_ULEB (val, p, end);
15737 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
15738 break;
15739
15740 case Tag_ARC_ABI_osver:
15741 READ_ULEB (val, p, end);
15742 printf (" Tag_ARC_ABI_osver: v%d\n", val);
15743 break;
15744
15745 case Tag_ARC_ABI_pic:
15746 case Tag_ARC_ABI_sda:
15747 READ_ULEB (val, p, end);
15748 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
15749 : " Tag_ARC_ABI_pic: ");
15750 switch (val)
15751 {
15752 case 0:
15753 printf (_("Absent\n"));
15754 break;
15755 case 1:
15756 printf ("MWDT\n");
15757 break;
15758 case 2:
15759 printf ("GNU\n");
15760 break;
15761 default:
15762 printf (_("Unknown\n"));
15763 break;
15764 }
15765 break;
15766
15767 case Tag_ARC_ABI_tls:
15768 READ_ULEB (val, p, end);
15769 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
15770 break;
15771
15772 case Tag_ARC_ABI_enumsize:
15773 READ_ULEB (val, p, end);
15774 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
15775 _("smallest"));
15776 break;
15777
15778 case Tag_ARC_ABI_exceptions:
15779 READ_ULEB (val, p, end);
15780 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
15781 : _("default"));
15782 break;
15783
15784 case Tag_ARC_ABI_double_size:
15785 READ_ULEB (val, p, end);
15786 printf (" Tag_ARC_ABI_double_size: %d\n", val);
15787 break;
15788
15789 case Tag_ARC_ISA_config:
15790 printf (" Tag_ARC_ISA_config: ");
15791 p = display_tag_value (-1, p, end);
15792 break;
15793
15794 case Tag_ARC_ISA_apex:
15795 printf (" Tag_ARC_ISA_apex: ");
15796 p = display_tag_value (-1, p, end);
15797 break;
15798
15799 case Tag_ARC_ISA_mpy_option:
15800 READ_ULEB (val, p, end);
15801 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
15802 break;
15803
15804 case Tag_ARC_ATR_version:
15805 READ_ULEB (val, p, end);
15806 printf (" Tag_ARC_ATR_version: %d\n", val);
15807 break;
15808
15809 default:
15810 return display_tag_value (tag & 1, p, end);
15811 }
15812
15813 return p;
15814 }
15815
15816 /* ARM EABI attributes section. */
15817 typedef struct
15818 {
15819 unsigned int tag;
15820 const char * name;
15821 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
15822 unsigned int type;
15823 const char *const *table;
15824 } arm_attr_public_tag;
15825
15826 static const char *const arm_attr_tag_CPU_arch[] =
15827 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
15828 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
15829 "v8-M.mainline", "", "", "", "v8.1-M.mainline"};
15830 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
15831 static const char *const arm_attr_tag_THUMB_ISA_use[] =
15832 {"No", "Thumb-1", "Thumb-2", "Yes"};
15833 static const char *const arm_attr_tag_FP_arch[] =
15834 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
15835 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
15836 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
15837 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
15838 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
15839 "NEON for ARMv8.1"};
15840 static const char *const arm_attr_tag_PCS_config[] =
15841 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
15842 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
15843 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
15844 {"V6", "SB", "TLS", "Unused"};
15845 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
15846 {"Absolute", "PC-relative", "SB-relative", "None"};
15847 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
15848 {"Absolute", "PC-relative", "None"};
15849 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
15850 {"None", "direct", "GOT-indirect"};
15851 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
15852 {"None", "??? 1", "2", "??? 3", "4"};
15853 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
15854 static const char *const arm_attr_tag_ABI_FP_denormal[] =
15855 {"Unused", "Needed", "Sign only"};
15856 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
15857 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
15858 static const char *const arm_attr_tag_ABI_FP_number_model[] =
15859 {"Unused", "Finite", "RTABI", "IEEE 754"};
15860 static const char *const arm_attr_tag_ABI_enum_size[] =
15861 {"Unused", "small", "int", "forced to int"};
15862 static const char *const arm_attr_tag_ABI_HardFP_use[] =
15863 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
15864 static const char *const arm_attr_tag_ABI_VFP_args[] =
15865 {"AAPCS", "VFP registers", "custom", "compatible"};
15866 static const char *const arm_attr_tag_ABI_WMMX_args[] =
15867 {"AAPCS", "WMMX registers", "custom"};
15868 static const char *const arm_attr_tag_ABI_optimization_goals[] =
15869 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
15870 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
15871 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
15872 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
15873 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
15874 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
15875 static const char *const arm_attr_tag_FP_HP_extension[] =
15876 {"Not Allowed", "Allowed"};
15877 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
15878 {"None", "IEEE 754", "Alternative Format"};
15879 static const char *const arm_attr_tag_DSP_extension[] =
15880 {"Follow architecture", "Allowed"};
15881 static const char *const arm_attr_tag_MPextension_use[] =
15882 {"Not Allowed", "Allowed"};
15883 static const char *const arm_attr_tag_DIV_use[] =
15884 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
15885 "Allowed in v7-A with integer division extension"};
15886 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
15887 static const char *const arm_attr_tag_Virtualization_use[] =
15888 {"Not Allowed", "TrustZone", "Virtualization Extensions",
15889 "TrustZone and Virtualization Extensions"};
15890 static const char *const arm_attr_tag_MPextension_use_legacy[] =
15891 {"Not Allowed", "Allowed"};
15892
15893 static const char *const arm_attr_tag_MVE_arch[] =
15894 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
15895
15896 #define LOOKUP(id, name) \
15897 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
15898 static arm_attr_public_tag arm_attr_public_tags[] =
15899 {
15900 {4, "CPU_raw_name", 1, NULL},
15901 {5, "CPU_name", 1, NULL},
15902 LOOKUP(6, CPU_arch),
15903 {7, "CPU_arch_profile", 0, NULL},
15904 LOOKUP(8, ARM_ISA_use),
15905 LOOKUP(9, THUMB_ISA_use),
15906 LOOKUP(10, FP_arch),
15907 LOOKUP(11, WMMX_arch),
15908 LOOKUP(12, Advanced_SIMD_arch),
15909 LOOKUP(13, PCS_config),
15910 LOOKUP(14, ABI_PCS_R9_use),
15911 LOOKUP(15, ABI_PCS_RW_data),
15912 LOOKUP(16, ABI_PCS_RO_data),
15913 LOOKUP(17, ABI_PCS_GOT_use),
15914 LOOKUP(18, ABI_PCS_wchar_t),
15915 LOOKUP(19, ABI_FP_rounding),
15916 LOOKUP(20, ABI_FP_denormal),
15917 LOOKUP(21, ABI_FP_exceptions),
15918 LOOKUP(22, ABI_FP_user_exceptions),
15919 LOOKUP(23, ABI_FP_number_model),
15920 {24, "ABI_align_needed", 0, NULL},
15921 {25, "ABI_align_preserved", 0, NULL},
15922 LOOKUP(26, ABI_enum_size),
15923 LOOKUP(27, ABI_HardFP_use),
15924 LOOKUP(28, ABI_VFP_args),
15925 LOOKUP(29, ABI_WMMX_args),
15926 LOOKUP(30, ABI_optimization_goals),
15927 LOOKUP(31, ABI_FP_optimization_goals),
15928 {32, "compatibility", 0, NULL},
15929 LOOKUP(34, CPU_unaligned_access),
15930 LOOKUP(36, FP_HP_extension),
15931 LOOKUP(38, ABI_FP_16bit_format),
15932 LOOKUP(42, MPextension_use),
15933 LOOKUP(44, DIV_use),
15934 LOOKUP(46, DSP_extension),
15935 LOOKUP(48, MVE_arch),
15936 {64, "nodefaults", 0, NULL},
15937 {65, "also_compatible_with", 0, NULL},
15938 LOOKUP(66, T2EE_use),
15939 {67, "conformance", 1, NULL},
15940 LOOKUP(68, Virtualization_use),
15941 LOOKUP(70, MPextension_use_legacy)
15942 };
15943 #undef LOOKUP
15944
15945 static unsigned char *
15946 display_arm_attribute (unsigned char * p,
15947 const unsigned char * const end)
15948 {
15949 unsigned int tag;
15950 unsigned int val;
15951 arm_attr_public_tag * attr;
15952 unsigned i;
15953 unsigned int type;
15954
15955 READ_ULEB (tag, p, end);
15956 attr = NULL;
15957 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
15958 {
15959 if (arm_attr_public_tags[i].tag == tag)
15960 {
15961 attr = &arm_attr_public_tags[i];
15962 break;
15963 }
15964 }
15965
15966 if (attr)
15967 {
15968 printf (" Tag_%s: ", attr->name);
15969 switch (attr->type)
15970 {
15971 case 0:
15972 switch (tag)
15973 {
15974 case 7: /* Tag_CPU_arch_profile. */
15975 READ_ULEB (val, p, end);
15976 switch (val)
15977 {
15978 case 0: printf (_("None\n")); break;
15979 case 'A': printf (_("Application\n")); break;
15980 case 'R': printf (_("Realtime\n")); break;
15981 case 'M': printf (_("Microcontroller\n")); break;
15982 case 'S': printf (_("Application or Realtime\n")); break;
15983 default: printf ("??? (%d)\n", val); break;
15984 }
15985 break;
15986
15987 case 24: /* Tag_align_needed. */
15988 READ_ULEB (val, p, end);
15989 switch (val)
15990 {
15991 case 0: printf (_("None\n")); break;
15992 case 1: printf (_("8-byte\n")); break;
15993 case 2: printf (_("4-byte\n")); break;
15994 case 3: printf ("??? 3\n"); break;
15995 default:
15996 if (val <= 12)
15997 printf (_("8-byte and up to %d-byte extended\n"),
15998 1 << val);
15999 else
16000 printf ("??? (%d)\n", val);
16001 break;
16002 }
16003 break;
16004
16005 case 25: /* Tag_align_preserved. */
16006 READ_ULEB (val, p, end);
16007 switch (val)
16008 {
16009 case 0: printf (_("None\n")); break;
16010 case 1: printf (_("8-byte, except leaf SP\n")); break;
16011 case 2: printf (_("8-byte\n")); break;
16012 case 3: printf ("??? 3\n"); break;
16013 default:
16014 if (val <= 12)
16015 printf (_("8-byte and up to %d-byte extended\n"),
16016 1 << val);
16017 else
16018 printf ("??? (%d)\n", val);
16019 break;
16020 }
16021 break;
16022
16023 case 32: /* Tag_compatibility. */
16024 {
16025 READ_ULEB (val, p, end);
16026 printf (_("flag = %d, vendor = "), val);
16027 if (p < end - 1)
16028 {
16029 size_t maxlen = (end - p) - 1;
16030
16031 print_symbol ((int) maxlen, (const char *) p);
16032 p += strnlen ((char *) p, maxlen) + 1;
16033 }
16034 else
16035 {
16036 printf (_("<corrupt>"));
16037 p = (unsigned char *) end;
16038 }
16039 putchar ('\n');
16040 }
16041 break;
16042
16043 case 64: /* Tag_nodefaults. */
16044 /* PR 17531: file: 001-505008-0.01. */
16045 if (p < end)
16046 p++;
16047 printf (_("True\n"));
16048 break;
16049
16050 case 65: /* Tag_also_compatible_with. */
16051 READ_ULEB (val, p, end);
16052 if (val == 6 /* Tag_CPU_arch. */)
16053 {
16054 READ_ULEB (val, p, end);
16055 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16056 printf ("??? (%d)\n", val);
16057 else
16058 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16059 }
16060 else
16061 printf ("???\n");
16062 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16063 ;
16064 break;
16065
16066 default:
16067 printf (_("<unknown: %d>\n"), tag);
16068 break;
16069 }
16070 return p;
16071
16072 case 1:
16073 return display_tag_value (-1, p, end);
16074 case 2:
16075 return display_tag_value (0, p, end);
16076
16077 default:
16078 assert (attr->type & 0x80);
16079 READ_ULEB (val, p, end);
16080 type = attr->type & 0x7f;
16081 if (val >= type)
16082 printf ("??? (%d)\n", val);
16083 else
16084 printf ("%s\n", attr->table[val]);
16085 return p;
16086 }
16087 }
16088
16089 return display_tag_value (tag, p, end);
16090 }
16091
16092 static unsigned char *
16093 display_gnu_attribute (unsigned char * p,
16094 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16095 const unsigned char * const end)
16096 {
16097 unsigned int tag;
16098 unsigned int val;
16099
16100 READ_ULEB (tag, p, end);
16101
16102 /* Tag_compatibility is the only generic GNU attribute defined at
16103 present. */
16104 if (tag == 32)
16105 {
16106 READ_ULEB (val, p, end);
16107
16108 printf (_("flag = %d, vendor = "), val);
16109 if (p == end)
16110 {
16111 printf (_("<corrupt>\n"));
16112 warn (_("corrupt vendor attribute\n"));
16113 }
16114 else
16115 {
16116 if (p < end - 1)
16117 {
16118 size_t maxlen = (end - p) - 1;
16119
16120 print_symbol ((int) maxlen, (const char *) p);
16121 p += strnlen ((char *) p, maxlen) + 1;
16122 }
16123 else
16124 {
16125 printf (_("<corrupt>"));
16126 p = (unsigned char *) end;
16127 }
16128 putchar ('\n');
16129 }
16130 return p;
16131 }
16132
16133 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16134 return display_proc_gnu_attribute (p, tag, end);
16135
16136 return display_tag_value (tag, p, end);
16137 }
16138
16139 static unsigned char *
16140 display_m68k_gnu_attribute (unsigned char * p,
16141 unsigned int tag,
16142 const unsigned char * const end)
16143 {
16144 unsigned int val;
16145
16146 if (tag == Tag_GNU_M68K_ABI_FP)
16147 {
16148 printf (" Tag_GNU_M68K_ABI_FP: ");
16149 if (p == end)
16150 {
16151 printf (_("<corrupt>\n"));
16152 return p;
16153 }
16154 READ_ULEB (val, p, end);
16155
16156 if (val > 3)
16157 printf ("(%#x), ", val);
16158
16159 switch (val & 3)
16160 {
16161 case 0:
16162 printf (_("unspecified hard/soft float\n"));
16163 break;
16164 case 1:
16165 printf (_("hard float\n"));
16166 break;
16167 case 2:
16168 printf (_("soft float\n"));
16169 break;
16170 }
16171 return p;
16172 }
16173
16174 return display_tag_value (tag & 1, p, end);
16175 }
16176
16177 static unsigned char *
16178 display_power_gnu_attribute (unsigned char * p,
16179 unsigned int tag,
16180 const unsigned char * const end)
16181 {
16182 unsigned int val;
16183
16184 if (tag == Tag_GNU_Power_ABI_FP)
16185 {
16186 printf (" Tag_GNU_Power_ABI_FP: ");
16187 if (p == end)
16188 {
16189 printf (_("<corrupt>\n"));
16190 return p;
16191 }
16192 READ_ULEB (val, p, end);
16193
16194 if (val > 15)
16195 printf ("(%#x), ", val);
16196
16197 switch (val & 3)
16198 {
16199 case 0:
16200 printf (_("unspecified hard/soft float, "));
16201 break;
16202 case 1:
16203 printf (_("hard float, "));
16204 break;
16205 case 2:
16206 printf (_("soft float, "));
16207 break;
16208 case 3:
16209 printf (_("single-precision hard float, "));
16210 break;
16211 }
16212
16213 switch (val & 0xC)
16214 {
16215 case 0:
16216 printf (_("unspecified long double\n"));
16217 break;
16218 case 4:
16219 printf (_("128-bit IBM long double\n"));
16220 break;
16221 case 8:
16222 printf (_("64-bit long double\n"));
16223 break;
16224 case 12:
16225 printf (_("128-bit IEEE long double\n"));
16226 break;
16227 }
16228 return p;
16229 }
16230
16231 if (tag == Tag_GNU_Power_ABI_Vector)
16232 {
16233 printf (" Tag_GNU_Power_ABI_Vector: ");
16234 if (p == end)
16235 {
16236 printf (_("<corrupt>\n"));
16237 return p;
16238 }
16239 READ_ULEB (val, p, end);
16240
16241 if (val > 3)
16242 printf ("(%#x), ", val);
16243
16244 switch (val & 3)
16245 {
16246 case 0:
16247 printf (_("unspecified\n"));
16248 break;
16249 case 1:
16250 printf (_("generic\n"));
16251 break;
16252 case 2:
16253 printf ("AltiVec\n");
16254 break;
16255 case 3:
16256 printf ("SPE\n");
16257 break;
16258 }
16259 return p;
16260 }
16261
16262 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16263 {
16264 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
16265 if (p == end)
16266 {
16267 printf (_("<corrupt>\n"));
16268 return p;
16269 }
16270 READ_ULEB (val, p, end);
16271
16272 if (val > 2)
16273 printf ("(%#x), ", val);
16274
16275 switch (val & 3)
16276 {
16277 case 0:
16278 printf (_("unspecified\n"));
16279 break;
16280 case 1:
16281 printf ("r3/r4\n");
16282 break;
16283 case 2:
16284 printf (_("memory\n"));
16285 break;
16286 case 3:
16287 printf ("???\n");
16288 break;
16289 }
16290 return p;
16291 }
16292
16293 return display_tag_value (tag & 1, p, end);
16294 }
16295
16296 static unsigned char *
16297 display_s390_gnu_attribute (unsigned char * p,
16298 unsigned int tag,
16299 const unsigned char * const end)
16300 {
16301 unsigned int val;
16302
16303 if (tag == Tag_GNU_S390_ABI_Vector)
16304 {
16305 printf (" Tag_GNU_S390_ABI_Vector: ");
16306 READ_ULEB (val, p, end);
16307
16308 switch (val)
16309 {
16310 case 0:
16311 printf (_("any\n"));
16312 break;
16313 case 1:
16314 printf (_("software\n"));
16315 break;
16316 case 2:
16317 printf (_("hardware\n"));
16318 break;
16319 default:
16320 printf ("??? (%d)\n", val);
16321 break;
16322 }
16323 return p;
16324 }
16325
16326 return display_tag_value (tag & 1, p, end);
16327 }
16328
16329 static void
16330 display_sparc_hwcaps (unsigned int mask)
16331 {
16332 if (mask)
16333 {
16334 bfd_boolean first = TRUE;
16335
16336 if (mask & ELF_SPARC_HWCAP_MUL32)
16337 fputs ("mul32", stdout), first = FALSE;
16338 if (mask & ELF_SPARC_HWCAP_DIV32)
16339 printf ("%sdiv32", first ? "" : "|"), first = FALSE;
16340 if (mask & ELF_SPARC_HWCAP_FSMULD)
16341 printf ("%sfsmuld", first ? "" : "|"), first = FALSE;
16342 if (mask & ELF_SPARC_HWCAP_V8PLUS)
16343 printf ("%sv8plus", first ? "" : "|"), first = FALSE;
16344 if (mask & ELF_SPARC_HWCAP_POPC)
16345 printf ("%spopc", first ? "" : "|"), first = FALSE;
16346 if (mask & ELF_SPARC_HWCAP_VIS)
16347 printf ("%svis", first ? "" : "|"), first = FALSE;
16348 if (mask & ELF_SPARC_HWCAP_VIS2)
16349 printf ("%svis2", first ? "" : "|"), first = FALSE;
16350 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
16351 printf ("%sASIBlkInit", first ? "" : "|"), first = FALSE;
16352 if (mask & ELF_SPARC_HWCAP_FMAF)
16353 printf ("%sfmaf", first ? "" : "|"), first = FALSE;
16354 if (mask & ELF_SPARC_HWCAP_VIS3)
16355 printf ("%svis3", first ? "" : "|"), first = FALSE;
16356 if (mask & ELF_SPARC_HWCAP_HPC)
16357 printf ("%shpc", first ? "" : "|"), first = FALSE;
16358 if (mask & ELF_SPARC_HWCAP_RANDOM)
16359 printf ("%srandom", first ? "" : "|"), first = FALSE;
16360 if (mask & ELF_SPARC_HWCAP_TRANS)
16361 printf ("%strans", first ? "" : "|"), first = FALSE;
16362 if (mask & ELF_SPARC_HWCAP_FJFMAU)
16363 printf ("%sfjfmau", first ? "" : "|"), first = FALSE;
16364 if (mask & ELF_SPARC_HWCAP_IMA)
16365 printf ("%sima", first ? "" : "|"), first = FALSE;
16366 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
16367 printf ("%scspare", first ? "" : "|"), first = FALSE;
16368 }
16369 else
16370 fputc ('0', stdout);
16371 fputc ('\n', stdout);
16372 }
16373
16374 static void
16375 display_sparc_hwcaps2 (unsigned int mask)
16376 {
16377 if (mask)
16378 {
16379 bfd_boolean first = TRUE;
16380
16381 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
16382 fputs ("fjathplus", stdout), first = FALSE;
16383 if (mask & ELF_SPARC_HWCAP2_VIS3B)
16384 printf ("%svis3b", first ? "" : "|"), first = FALSE;
16385 if (mask & ELF_SPARC_HWCAP2_ADP)
16386 printf ("%sadp", first ? "" : "|"), first = FALSE;
16387 if (mask & ELF_SPARC_HWCAP2_SPARC5)
16388 printf ("%ssparc5", first ? "" : "|"), first = FALSE;
16389 if (mask & ELF_SPARC_HWCAP2_MWAIT)
16390 printf ("%smwait", first ? "" : "|"), first = FALSE;
16391 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
16392 printf ("%sxmpmul", first ? "" : "|"), first = FALSE;
16393 if (mask & ELF_SPARC_HWCAP2_XMONT)
16394 printf ("%sxmont2", first ? "" : "|"), first = FALSE;
16395 if (mask & ELF_SPARC_HWCAP2_NSEC)
16396 printf ("%snsec", first ? "" : "|"), first = FALSE;
16397 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
16398 printf ("%sfjathhpc", first ? "" : "|"), first = FALSE;
16399 if (mask & ELF_SPARC_HWCAP2_FJDES)
16400 printf ("%sfjdes", first ? "" : "|"), first = FALSE;
16401 if (mask & ELF_SPARC_HWCAP2_FJAES)
16402 printf ("%sfjaes", first ? "" : "|"), first = FALSE;
16403 }
16404 else
16405 fputc ('0', stdout);
16406 fputc ('\n', stdout);
16407 }
16408
16409 static unsigned char *
16410 display_sparc_gnu_attribute (unsigned char * p,
16411 unsigned int tag,
16412 const unsigned char * const end)
16413 {
16414 unsigned int val;
16415
16416 if (tag == Tag_GNU_Sparc_HWCAPS)
16417 {
16418 READ_ULEB (val, p, end);
16419 printf (" Tag_GNU_Sparc_HWCAPS: ");
16420 display_sparc_hwcaps (val);
16421 return p;
16422 }
16423 if (tag == Tag_GNU_Sparc_HWCAPS2)
16424 {
16425 READ_ULEB (val, p, end);
16426 printf (" Tag_GNU_Sparc_HWCAPS2: ");
16427 display_sparc_hwcaps2 (val);
16428 return p;
16429 }
16430
16431 return display_tag_value (tag, p, end);
16432 }
16433
16434 static void
16435 print_mips_fp_abi_value (unsigned int val)
16436 {
16437 switch (val)
16438 {
16439 case Val_GNU_MIPS_ABI_FP_ANY:
16440 printf (_("Hard or soft float\n"));
16441 break;
16442 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16443 printf (_("Hard float (double precision)\n"));
16444 break;
16445 case Val_GNU_MIPS_ABI_FP_SINGLE:
16446 printf (_("Hard float (single precision)\n"));
16447 break;
16448 case Val_GNU_MIPS_ABI_FP_SOFT:
16449 printf (_("Soft float\n"));
16450 break;
16451 case Val_GNU_MIPS_ABI_FP_OLD_64:
16452 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16453 break;
16454 case Val_GNU_MIPS_ABI_FP_XX:
16455 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
16456 break;
16457 case Val_GNU_MIPS_ABI_FP_64:
16458 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
16459 break;
16460 case Val_GNU_MIPS_ABI_FP_64A:
16461 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16462 break;
16463 case Val_GNU_MIPS_ABI_FP_NAN2008:
16464 printf (_("NaN 2008 compatibility\n"));
16465 break;
16466 default:
16467 printf ("??? (%d)\n", val);
16468 break;
16469 }
16470 }
16471
16472 static unsigned char *
16473 display_mips_gnu_attribute (unsigned char * p,
16474 unsigned int tag,
16475 const unsigned char * const end)
16476 {
16477 if (tag == Tag_GNU_MIPS_ABI_FP)
16478 {
16479 unsigned int val;
16480
16481 printf (" Tag_GNU_MIPS_ABI_FP: ");
16482 READ_ULEB (val, p, end);
16483 print_mips_fp_abi_value (val);
16484 return p;
16485 }
16486
16487 if (tag == Tag_GNU_MIPS_ABI_MSA)
16488 {
16489 unsigned int val;
16490
16491 printf (" Tag_GNU_MIPS_ABI_MSA: ");
16492 READ_ULEB (val, p, end);
16493
16494 switch (val)
16495 {
16496 case Val_GNU_MIPS_ABI_MSA_ANY:
16497 printf (_("Any MSA or not\n"));
16498 break;
16499 case Val_GNU_MIPS_ABI_MSA_128:
16500 printf (_("128-bit MSA\n"));
16501 break;
16502 default:
16503 printf ("??? (%d)\n", val);
16504 break;
16505 }
16506 return p;
16507 }
16508
16509 return display_tag_value (tag & 1, p, end);
16510 }
16511
16512 static unsigned char *
16513 display_tic6x_attribute (unsigned char * p,
16514 const unsigned char * const end)
16515 {
16516 unsigned int tag;
16517 unsigned int val;
16518
16519 READ_ULEB (tag, p, end);
16520
16521 switch (tag)
16522 {
16523 case Tag_ISA:
16524 printf (" Tag_ISA: ");
16525 READ_ULEB (val, p, end);
16526
16527 switch (val)
16528 {
16529 case C6XABI_Tag_ISA_none:
16530 printf (_("None\n"));
16531 break;
16532 case C6XABI_Tag_ISA_C62X:
16533 printf ("C62x\n");
16534 break;
16535 case C6XABI_Tag_ISA_C67X:
16536 printf ("C67x\n");
16537 break;
16538 case C6XABI_Tag_ISA_C67XP:
16539 printf ("C67x+\n");
16540 break;
16541 case C6XABI_Tag_ISA_C64X:
16542 printf ("C64x\n");
16543 break;
16544 case C6XABI_Tag_ISA_C64XP:
16545 printf ("C64x+\n");
16546 break;
16547 case C6XABI_Tag_ISA_C674X:
16548 printf ("C674x\n");
16549 break;
16550 default:
16551 printf ("??? (%d)\n", val);
16552 break;
16553 }
16554 return p;
16555
16556 case Tag_ABI_wchar_t:
16557 printf (" Tag_ABI_wchar_t: ");
16558 READ_ULEB (val, p, end);
16559 switch (val)
16560 {
16561 case 0:
16562 printf (_("Not used\n"));
16563 break;
16564 case 1:
16565 printf (_("2 bytes\n"));
16566 break;
16567 case 2:
16568 printf (_("4 bytes\n"));
16569 break;
16570 default:
16571 printf ("??? (%d)\n", val);
16572 break;
16573 }
16574 return p;
16575
16576 case Tag_ABI_stack_align_needed:
16577 printf (" Tag_ABI_stack_align_needed: ");
16578 READ_ULEB (val, p, end);
16579 switch (val)
16580 {
16581 case 0:
16582 printf (_("8-byte\n"));
16583 break;
16584 case 1:
16585 printf (_("16-byte\n"));
16586 break;
16587 default:
16588 printf ("??? (%d)\n", val);
16589 break;
16590 }
16591 return p;
16592
16593 case Tag_ABI_stack_align_preserved:
16594 READ_ULEB (val, p, end);
16595 printf (" Tag_ABI_stack_align_preserved: ");
16596 switch (val)
16597 {
16598 case 0:
16599 printf (_("8-byte\n"));
16600 break;
16601 case 1:
16602 printf (_("16-byte\n"));
16603 break;
16604 default:
16605 printf ("??? (%d)\n", val);
16606 break;
16607 }
16608 return p;
16609
16610 case Tag_ABI_DSBT:
16611 READ_ULEB (val, p, end);
16612 printf (" Tag_ABI_DSBT: ");
16613 switch (val)
16614 {
16615 case 0:
16616 printf (_("DSBT addressing not used\n"));
16617 break;
16618 case 1:
16619 printf (_("DSBT addressing used\n"));
16620 break;
16621 default:
16622 printf ("??? (%d)\n", val);
16623 break;
16624 }
16625 return p;
16626
16627 case Tag_ABI_PID:
16628 READ_ULEB (val, p, end);
16629 printf (" Tag_ABI_PID: ");
16630 switch (val)
16631 {
16632 case 0:
16633 printf (_("Data addressing position-dependent\n"));
16634 break;
16635 case 1:
16636 printf (_("Data addressing position-independent, GOT near DP\n"));
16637 break;
16638 case 2:
16639 printf (_("Data addressing position-independent, GOT far from DP\n"));
16640 break;
16641 default:
16642 printf ("??? (%d)\n", val);
16643 break;
16644 }
16645 return p;
16646
16647 case Tag_ABI_PIC:
16648 READ_ULEB (val, p, end);
16649 printf (" Tag_ABI_PIC: ");
16650 switch (val)
16651 {
16652 case 0:
16653 printf (_("Code addressing position-dependent\n"));
16654 break;
16655 case 1:
16656 printf (_("Code addressing position-independent\n"));
16657 break;
16658 default:
16659 printf ("??? (%d)\n", val);
16660 break;
16661 }
16662 return p;
16663
16664 case Tag_ABI_array_object_alignment:
16665 READ_ULEB (val, p, end);
16666 printf (" Tag_ABI_array_object_alignment: ");
16667 switch (val)
16668 {
16669 case 0:
16670 printf (_("8-byte\n"));
16671 break;
16672 case 1:
16673 printf (_("4-byte\n"));
16674 break;
16675 case 2:
16676 printf (_("16-byte\n"));
16677 break;
16678 default:
16679 printf ("??? (%d)\n", val);
16680 break;
16681 }
16682 return p;
16683
16684 case Tag_ABI_array_object_align_expected:
16685 READ_ULEB (val, p, end);
16686 printf (" Tag_ABI_array_object_align_expected: ");
16687 switch (val)
16688 {
16689 case 0:
16690 printf (_("8-byte\n"));
16691 break;
16692 case 1:
16693 printf (_("4-byte\n"));
16694 break;
16695 case 2:
16696 printf (_("16-byte\n"));
16697 break;
16698 default:
16699 printf ("??? (%d)\n", val);
16700 break;
16701 }
16702 return p;
16703
16704 case Tag_ABI_compatibility:
16705 {
16706 READ_ULEB (val, p, end);
16707 printf (" Tag_ABI_compatibility: ");
16708 printf (_("flag = %d, vendor = "), val);
16709 if (p < end - 1)
16710 {
16711 size_t maxlen = (end - p) - 1;
16712
16713 print_symbol ((int) maxlen, (const char *) p);
16714 p += strnlen ((char *) p, maxlen) + 1;
16715 }
16716 else
16717 {
16718 printf (_("<corrupt>"));
16719 p = (unsigned char *) end;
16720 }
16721 putchar ('\n');
16722 return p;
16723 }
16724
16725 case Tag_ABI_conformance:
16726 {
16727 printf (" Tag_ABI_conformance: \"");
16728 if (p < end - 1)
16729 {
16730 size_t maxlen = (end - p) - 1;
16731
16732 print_symbol ((int) maxlen, (const char *) p);
16733 p += strnlen ((char *) p, maxlen) + 1;
16734 }
16735 else
16736 {
16737 printf (_("<corrupt>"));
16738 p = (unsigned char *) end;
16739 }
16740 printf ("\"\n");
16741 return p;
16742 }
16743 }
16744
16745 return display_tag_value (tag, p, end);
16746 }
16747
16748 static void
16749 display_raw_attribute (unsigned char * p, unsigned char const * const end)
16750 {
16751 unsigned long addr = 0;
16752 size_t bytes = end - p;
16753
16754 assert (end >= p);
16755 while (bytes)
16756 {
16757 int j;
16758 int k;
16759 int lbytes = (bytes > 16 ? 16 : bytes);
16760
16761 printf (" 0x%8.8lx ", addr);
16762
16763 for (j = 0; j < 16; j++)
16764 {
16765 if (j < lbytes)
16766 printf ("%2.2x", p[j]);
16767 else
16768 printf (" ");
16769
16770 if ((j & 3) == 3)
16771 printf (" ");
16772 }
16773
16774 for (j = 0; j < lbytes; j++)
16775 {
16776 k = p[j];
16777 if (k >= ' ' && k < 0x7f)
16778 printf ("%c", k);
16779 else
16780 printf (".");
16781 }
16782
16783 putchar ('\n');
16784
16785 p += lbytes;
16786 bytes -= lbytes;
16787 addr += lbytes;
16788 }
16789
16790 putchar ('\n');
16791 }
16792
16793 static unsigned char *
16794 display_msp430_attribute (unsigned char * p,
16795 const unsigned char * const end)
16796 {
16797 unsigned int val;
16798 unsigned int tag;
16799
16800 READ_ULEB (tag, p, end);
16801
16802 switch (tag)
16803 {
16804 case OFBA_MSPABI_Tag_ISA:
16805 printf (" Tag_ISA: ");
16806 READ_ULEB (val, p, end);
16807 switch (val)
16808 {
16809 case 0: printf (_("None\n")); break;
16810 case 1: printf (_("MSP430\n")); break;
16811 case 2: printf (_("MSP430X\n")); break;
16812 default: printf ("??? (%d)\n", val); break;
16813 }
16814 break;
16815
16816 case OFBA_MSPABI_Tag_Code_Model:
16817 printf (" Tag_Code_Model: ");
16818 READ_ULEB (val, p, end);
16819 switch (val)
16820 {
16821 case 0: printf (_("None\n")); break;
16822 case 1: printf (_("Small\n")); break;
16823 case 2: printf (_("Large\n")); break;
16824 default: printf ("??? (%d)\n", val); break;
16825 }
16826 break;
16827
16828 case OFBA_MSPABI_Tag_Data_Model:
16829 printf (" Tag_Data_Model: ");
16830 READ_ULEB (val, p, end);
16831 switch (val)
16832 {
16833 case 0: printf (_("None\n")); break;
16834 case 1: printf (_("Small\n")); break;
16835 case 2: printf (_("Large\n")); break;
16836 case 3: printf (_("Restricted Large\n")); break;
16837 default: printf ("??? (%d)\n", val); break;
16838 }
16839 break;
16840
16841 default:
16842 printf (_(" <unknown tag %d>: "), tag);
16843
16844 if (tag & 1)
16845 {
16846 putchar ('"');
16847 if (p < end - 1)
16848 {
16849 size_t maxlen = (end - p) - 1;
16850
16851 print_symbol ((int) maxlen, (const char *) p);
16852 p += strnlen ((char *) p, maxlen) + 1;
16853 }
16854 else
16855 {
16856 printf (_("<corrupt>"));
16857 p = (unsigned char *) end;
16858 }
16859 printf ("\"\n");
16860 }
16861 else
16862 {
16863 READ_ULEB (val, p, end);
16864 printf ("%d (0x%x)\n", val, val);
16865 }
16866 break;
16867 }
16868
16869 assert (p <= end);
16870 return p;
16871 }
16872
16873 static unsigned char *
16874 display_msp430_gnu_attribute (unsigned char * p,
16875 unsigned int tag,
16876 const unsigned char * const end)
16877 {
16878 if (tag == Tag_GNU_MSP430_Data_Region)
16879 {
16880 unsigned int val;
16881
16882 printf (" Tag_GNU_MSP430_Data_Region: ");
16883 READ_ULEB (val, p, end);
16884
16885 switch (val)
16886 {
16887 case Val_GNU_MSP430_Data_Region_Any:
16888 printf (_("Any Region\n"));
16889 break;
16890 case Val_GNU_MSP430_Data_Region_Lower:
16891 printf (_("Lower Region Only\n"));
16892 break;
16893 default:
16894 printf ("??? (%u)\n", val);
16895 }
16896 return p;
16897 }
16898 return display_tag_value (tag & 1, p, end);
16899 }
16900
16901 struct riscv_attr_tag_t {
16902 const char *name;
16903 unsigned int tag;
16904 };
16905
16906 static struct riscv_attr_tag_t riscv_attr_tag[] =
16907 {
16908 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
16909 T(arch),
16910 T(priv_spec),
16911 T(priv_spec_minor),
16912 T(priv_spec_revision),
16913 T(unaligned_access),
16914 T(stack_align),
16915 #undef T
16916 };
16917
16918 static unsigned char *
16919 display_riscv_attribute (unsigned char *p,
16920 const unsigned char * const end)
16921 {
16922 unsigned int val;
16923 unsigned int tag;
16924 struct riscv_attr_tag_t *attr = NULL;
16925 unsigned i;
16926
16927 READ_ULEB (tag, p, end);
16928
16929 /* Find the name of attribute. */
16930 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
16931 {
16932 if (riscv_attr_tag[i].tag == tag)
16933 {
16934 attr = &riscv_attr_tag[i];
16935 break;
16936 }
16937 }
16938
16939 if (attr)
16940 printf (" %s: ", attr->name);
16941 else
16942 return display_tag_value (tag, p, end);
16943
16944 switch (tag)
16945 {
16946 case Tag_RISCV_priv_spec:
16947 case Tag_RISCV_priv_spec_minor:
16948 case Tag_RISCV_priv_spec_revision:
16949 READ_ULEB (val, p, end);
16950 printf (_("%u\n"), val);
16951 break;
16952 case Tag_RISCV_unaligned_access:
16953 READ_ULEB (val, p, end);
16954 switch (val)
16955 {
16956 case 0:
16957 printf (_("No unaligned access\n"));
16958 break;
16959 case 1:
16960 printf (_("Unaligned access\n"));
16961 break;
16962 }
16963 break;
16964 case Tag_RISCV_stack_align:
16965 READ_ULEB (val, p, end);
16966 printf (_("%u-bytes\n"), val);
16967 break;
16968 case Tag_RISCV_arch:
16969 p = display_tag_value (-1, p, end);
16970 break;
16971 default:
16972 return display_tag_value (tag, p, end);
16973 }
16974
16975 return p;
16976 }
16977
16978 static unsigned char *
16979 display_csky_attribute (unsigned char * p,
16980 const unsigned char * const end)
16981 {
16982 unsigned int tag;
16983 unsigned int val;
16984 READ_ULEB (tag, p, end);
16985
16986 if (tag >= Tag_CSKY_MAX)
16987 {
16988 return display_tag_value (-1, p, end);
16989 }
16990
16991 switch (tag)
16992 {
16993 case Tag_CSKY_ARCH_NAME:
16994 printf (" Tag_CSKY_ARCH_NAME:\t\t");
16995 return display_tag_value (-1, p, end);
16996 case Tag_CSKY_CPU_NAME:
16997 printf (" Tag_CSKY_CPU_NAME:\t\t");
16998 return display_tag_value (-1, p, end);
16999
17000 case Tag_CSKY_ISA_FLAGS:
17001 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17002 return display_tag_value (0, p, end);
17003 case Tag_CSKY_ISA_EXT_FLAGS:
17004 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17005 return display_tag_value (0, p, end);
17006
17007 case Tag_CSKY_DSP_VERSION:
17008 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17009 READ_ULEB (val, p, end);
17010 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17011 printf ("DSP Extension\n");
17012 else if (val == VAL_CSKY_DSP_VERSION_2)
17013 printf ("DSP 2.0\n");
17014 break;
17015
17016 case Tag_CSKY_VDSP_VERSION:
17017 printf (" Tag_CSKY_VDSP_VERSION:\t");
17018 READ_ULEB (val, p, end);
17019 printf ("VDSP Version %d\n", val);
17020 break;
17021
17022 case Tag_CSKY_FPU_VERSION:
17023 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17024 READ_ULEB (val, p, end);
17025 if (val == VAL_CSKY_FPU_VERSION_1)
17026 printf ("ABIV1 FPU Version 1\n");
17027 else if (val == VAL_CSKY_FPU_VERSION_2)
17028 printf ("FPU Version 2\n");
17029 break;
17030
17031 case Tag_CSKY_FPU_ABI:
17032 printf (" Tag_CSKY_FPU_ABI:\t\t");
17033 READ_ULEB (val, p, end);
17034 if (val == VAL_CSKY_FPU_ABI_HARD)
17035 printf ("Hard\n");
17036 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17037 printf ("SoftFP\n");
17038 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17039 printf ("Soft\n");
17040 break;
17041 case Tag_CSKY_FPU_ROUNDING:
17042 READ_ULEB (val, p, end);
17043 if (val == 1) {
17044 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17045 printf ("Needed\n");
17046 }
17047 break;
17048 case Tag_CSKY_FPU_DENORMAL:
17049 READ_ULEB (val, p, end);
17050 if (val == 1) {
17051 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17052 printf ("Needed\n");
17053 }
17054 break;
17055 case Tag_CSKY_FPU_Exception:
17056 READ_ULEB (val, p, end);
17057 if (val == 1) {
17058 printf (" Tag_CSKY_FPU_Exception:\t");
17059 printf ("Needed\n");
17060 }
17061 break;
17062 case Tag_CSKY_FPU_NUMBER_MODULE:
17063 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17064 return display_tag_value (-1, p, end);
17065 case Tag_CSKY_FPU_HARDFP:
17066 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17067 READ_ULEB (val, p, end);
17068 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17069 printf (" Half");
17070 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17071 printf (" Single");
17072 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17073 printf (" Double");
17074 printf ("\n");
17075 break;
17076 default:
17077 return display_tag_value (tag, p, end);
17078 }
17079 return p;
17080 }
17081
17082 static bfd_boolean
17083 process_attributes (Filedata * filedata,
17084 const char * public_name,
17085 unsigned int proc_type,
17086 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17087 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17088 {
17089 Elf_Internal_Shdr * sect;
17090 unsigned i;
17091 bfd_boolean res = TRUE;
17092
17093 /* Find the section header so that we get the size. */
17094 for (i = 0, sect = filedata->section_headers;
17095 i < filedata->file_header.e_shnum;
17096 i++, sect++)
17097 {
17098 unsigned char * contents;
17099 unsigned char * p;
17100
17101 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17102 continue;
17103
17104 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17105 sect->sh_size, _("attributes"));
17106 if (contents == NULL)
17107 {
17108 res = FALSE;
17109 continue;
17110 }
17111
17112 p = contents;
17113 /* The first character is the version of the attributes.
17114 Currently only version 1, (aka 'A') is recognised here. */
17115 if (*p != 'A')
17116 {
17117 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17118 res = FALSE;
17119 }
17120 else
17121 {
17122 bfd_vma section_len;
17123
17124 section_len = sect->sh_size - 1;
17125 p++;
17126
17127 while (section_len > 0)
17128 {
17129 bfd_vma attr_len;
17130 unsigned int namelen;
17131 bfd_boolean public_section;
17132 bfd_boolean gnu_section;
17133
17134 if (section_len <= 4)
17135 {
17136 error (_("Tag section ends prematurely\n"));
17137 res = FALSE;
17138 break;
17139 }
17140 attr_len = byte_get (p, 4);
17141 p += 4;
17142
17143 if (attr_len > section_len)
17144 {
17145 error (_("Bad attribute length (%u > %u)\n"),
17146 (unsigned) attr_len, (unsigned) section_len);
17147 attr_len = section_len;
17148 res = FALSE;
17149 }
17150 /* PR 17531: file: 001-101425-0.004 */
17151 else if (attr_len < 5)
17152 {
17153 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17154 res = FALSE;
17155 break;
17156 }
17157
17158 section_len -= attr_len;
17159 attr_len -= 4;
17160
17161 namelen = strnlen ((char *) p, attr_len) + 1;
17162 if (namelen == 0 || namelen >= attr_len)
17163 {
17164 error (_("Corrupt attribute section name\n"));
17165 res = FALSE;
17166 break;
17167 }
17168
17169 printf (_("Attribute Section: "));
17170 print_symbol (INT_MAX, (const char *) p);
17171 putchar ('\n');
17172
17173 if (public_name && streq ((char *) p, public_name))
17174 public_section = TRUE;
17175 else
17176 public_section = FALSE;
17177
17178 if (streq ((char *) p, "gnu"))
17179 gnu_section = TRUE;
17180 else
17181 gnu_section = FALSE;
17182
17183 p += namelen;
17184 attr_len -= namelen;
17185
17186 while (attr_len > 0 && p < contents + sect->sh_size)
17187 {
17188 int tag;
17189 unsigned int val;
17190 bfd_vma size;
17191 unsigned char * end;
17192
17193 /* PR binutils/17531: Safe handling of corrupt files. */
17194 if (attr_len < 6)
17195 {
17196 error (_("Unused bytes at end of section\n"));
17197 res = FALSE;
17198 section_len = 0;
17199 break;
17200 }
17201
17202 tag = *(p++);
17203 size = byte_get (p, 4);
17204 if (size > attr_len)
17205 {
17206 error (_("Bad subsection length (%u > %u)\n"),
17207 (unsigned) size, (unsigned) attr_len);
17208 res = FALSE;
17209 size = attr_len;
17210 }
17211 /* PR binutils/17531: Safe handling of corrupt files. */
17212 if (size < 6)
17213 {
17214 error (_("Bad subsection length (%u < 6)\n"),
17215 (unsigned) size);
17216 res = FALSE;
17217 section_len = 0;
17218 break;
17219 }
17220
17221 attr_len -= size;
17222 end = p + size - 1;
17223 assert (end <= contents + sect->sh_size);
17224 p += 4;
17225
17226 switch (tag)
17227 {
17228 case 1:
17229 printf (_("File Attributes\n"));
17230 break;
17231 case 2:
17232 printf (_("Section Attributes:"));
17233 goto do_numlist;
17234 case 3:
17235 printf (_("Symbol Attributes:"));
17236 /* Fall through. */
17237 do_numlist:
17238 for (;;)
17239 {
17240 READ_ULEB (val, p, end);
17241 if (val == 0)
17242 break;
17243 printf (" %d", val);
17244 }
17245 printf ("\n");
17246 break;
17247 default:
17248 printf (_("Unknown tag: %d\n"), tag);
17249 public_section = FALSE;
17250 break;
17251 }
17252
17253 if (public_section && display_pub_attribute != NULL)
17254 {
17255 while (p < end)
17256 p = display_pub_attribute (p, end);
17257 assert (p == end);
17258 }
17259 else if (gnu_section && display_proc_gnu_attribute != NULL)
17260 {
17261 while (p < end)
17262 p = display_gnu_attribute (p,
17263 display_proc_gnu_attribute,
17264 end);
17265 assert (p == end);
17266 }
17267 else if (p < end)
17268 {
17269 printf (_(" Unknown attribute:\n"));
17270 display_raw_attribute (p, end);
17271 p = end;
17272 }
17273 else
17274 attr_len = 0;
17275 }
17276 }
17277 }
17278
17279 free (contents);
17280 }
17281
17282 return res;
17283 }
17284
17285 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17286 Print the Address, Access and Initial fields of an entry at VMA ADDR
17287 and return the VMA of the next entry, or -1 if there was a problem.
17288 Does not read from DATA_END or beyond. */
17289
17290 static bfd_vma
17291 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17292 unsigned char * data_end)
17293 {
17294 printf (" ");
17295 print_vma (addr, LONG_HEX);
17296 printf (" ");
17297 if (addr < pltgot + 0xfff0)
17298 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17299 else
17300 printf ("%10s", "");
17301 printf (" ");
17302 if (data == NULL)
17303 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17304 else
17305 {
17306 bfd_vma entry;
17307 unsigned char * from = data + addr - pltgot;
17308
17309 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17310 {
17311 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17312 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17313 return (bfd_vma) -1;
17314 }
17315 else
17316 {
17317 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17318 print_vma (entry, LONG_HEX);
17319 }
17320 }
17321 return addr + (is_32bit_elf ? 4 : 8);
17322 }
17323
17324 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17325 PLTGOT. Print the Address and Initial fields of an entry at VMA
17326 ADDR and return the VMA of the next entry. */
17327
17328 static bfd_vma
17329 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
17330 {
17331 printf (" ");
17332 print_vma (addr, LONG_HEX);
17333 printf (" ");
17334 if (data == NULL)
17335 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17336 else
17337 {
17338 bfd_vma entry;
17339
17340 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17341 print_vma (entry, LONG_HEX);
17342 }
17343 return addr + (is_32bit_elf ? 4 : 8);
17344 }
17345
17346 static void
17347 print_mips_ases (unsigned int mask)
17348 {
17349 if (mask & AFL_ASE_DSP)
17350 fputs ("\n\tDSP ASE", stdout);
17351 if (mask & AFL_ASE_DSPR2)
17352 fputs ("\n\tDSP R2 ASE", stdout);
17353 if (mask & AFL_ASE_DSPR3)
17354 fputs ("\n\tDSP R3 ASE", stdout);
17355 if (mask & AFL_ASE_EVA)
17356 fputs ("\n\tEnhanced VA Scheme", stdout);
17357 if (mask & AFL_ASE_MCU)
17358 fputs ("\n\tMCU (MicroController) ASE", stdout);
17359 if (mask & AFL_ASE_MDMX)
17360 fputs ("\n\tMDMX ASE", stdout);
17361 if (mask & AFL_ASE_MIPS3D)
17362 fputs ("\n\tMIPS-3D ASE", stdout);
17363 if (mask & AFL_ASE_MT)
17364 fputs ("\n\tMT ASE", stdout);
17365 if (mask & AFL_ASE_SMARTMIPS)
17366 fputs ("\n\tSmartMIPS ASE", stdout);
17367 if (mask & AFL_ASE_VIRT)
17368 fputs ("\n\tVZ ASE", stdout);
17369 if (mask & AFL_ASE_MSA)
17370 fputs ("\n\tMSA ASE", stdout);
17371 if (mask & AFL_ASE_MIPS16)
17372 fputs ("\n\tMIPS16 ASE", stdout);
17373 if (mask & AFL_ASE_MICROMIPS)
17374 fputs ("\n\tMICROMIPS ASE", stdout);
17375 if (mask & AFL_ASE_XPA)
17376 fputs ("\n\tXPA ASE", stdout);
17377 if (mask & AFL_ASE_MIPS16E2)
17378 fputs ("\n\tMIPS16e2 ASE", stdout);
17379 if (mask & AFL_ASE_CRC)
17380 fputs ("\n\tCRC ASE", stdout);
17381 if (mask & AFL_ASE_GINV)
17382 fputs ("\n\tGINV ASE", stdout);
17383 if (mask & AFL_ASE_LOONGSON_MMI)
17384 fputs ("\n\tLoongson MMI ASE", stdout);
17385 if (mask & AFL_ASE_LOONGSON_CAM)
17386 fputs ("\n\tLoongson CAM ASE", stdout);
17387 if (mask & AFL_ASE_LOONGSON_EXT)
17388 fputs ("\n\tLoongson EXT ASE", stdout);
17389 if (mask & AFL_ASE_LOONGSON_EXT2)
17390 fputs ("\n\tLoongson EXT2 ASE", stdout);
17391 if (mask == 0)
17392 fprintf (stdout, "\n\t%s", _("None"));
17393 else if ((mask & ~AFL_ASE_MASK) != 0)
17394 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
17395 }
17396
17397 static void
17398 print_mips_isa_ext (unsigned int isa_ext)
17399 {
17400 switch (isa_ext)
17401 {
17402 case 0:
17403 fputs (_("None"), stdout);
17404 break;
17405 case AFL_EXT_XLR:
17406 fputs ("RMI XLR", stdout);
17407 break;
17408 case AFL_EXT_OCTEON3:
17409 fputs ("Cavium Networks Octeon3", stdout);
17410 break;
17411 case AFL_EXT_OCTEON2:
17412 fputs ("Cavium Networks Octeon2", stdout);
17413 break;
17414 case AFL_EXT_OCTEONP:
17415 fputs ("Cavium Networks OcteonP", stdout);
17416 break;
17417 case AFL_EXT_OCTEON:
17418 fputs ("Cavium Networks Octeon", stdout);
17419 break;
17420 case AFL_EXT_5900:
17421 fputs ("Toshiba R5900", stdout);
17422 break;
17423 case AFL_EXT_4650:
17424 fputs ("MIPS R4650", stdout);
17425 break;
17426 case AFL_EXT_4010:
17427 fputs ("LSI R4010", stdout);
17428 break;
17429 case AFL_EXT_4100:
17430 fputs ("NEC VR4100", stdout);
17431 break;
17432 case AFL_EXT_3900:
17433 fputs ("Toshiba R3900", stdout);
17434 break;
17435 case AFL_EXT_10000:
17436 fputs ("MIPS R10000", stdout);
17437 break;
17438 case AFL_EXT_SB1:
17439 fputs ("Broadcom SB-1", stdout);
17440 break;
17441 case AFL_EXT_4111:
17442 fputs ("NEC VR4111/VR4181", stdout);
17443 break;
17444 case AFL_EXT_4120:
17445 fputs ("NEC VR4120", stdout);
17446 break;
17447 case AFL_EXT_5400:
17448 fputs ("NEC VR5400", stdout);
17449 break;
17450 case AFL_EXT_5500:
17451 fputs ("NEC VR5500", stdout);
17452 break;
17453 case AFL_EXT_LOONGSON_2E:
17454 fputs ("ST Microelectronics Loongson 2E", stdout);
17455 break;
17456 case AFL_EXT_LOONGSON_2F:
17457 fputs ("ST Microelectronics Loongson 2F", stdout);
17458 break;
17459 case AFL_EXT_INTERAPTIV_MR2:
17460 fputs ("Imagination interAptiv MR2", stdout);
17461 break;
17462 default:
17463 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
17464 }
17465 }
17466
17467 static signed int
17468 get_mips_reg_size (int reg_size)
17469 {
17470 return (reg_size == AFL_REG_NONE) ? 0
17471 : (reg_size == AFL_REG_32) ? 32
17472 : (reg_size == AFL_REG_64) ? 64
17473 : (reg_size == AFL_REG_128) ? 128
17474 : -1;
17475 }
17476
17477 static bfd_boolean
17478 process_mips_specific (Filedata * filedata)
17479 {
17480 Elf_Internal_Dyn * entry;
17481 Elf_Internal_Shdr *sect = NULL;
17482 size_t liblist_offset = 0;
17483 size_t liblistno = 0;
17484 size_t conflictsno = 0;
17485 size_t options_offset = 0;
17486 size_t conflicts_offset = 0;
17487 size_t pltrelsz = 0;
17488 size_t pltrel = 0;
17489 bfd_vma pltgot = 0;
17490 bfd_vma mips_pltgot = 0;
17491 bfd_vma jmprel = 0;
17492 bfd_vma local_gotno = 0;
17493 bfd_vma gotsym = 0;
17494 bfd_vma symtabno = 0;
17495 bfd_boolean res = TRUE;
17496
17497 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
17498 display_mips_gnu_attribute))
17499 res = FALSE;
17500
17501 sect = find_section (filedata, ".MIPS.abiflags");
17502
17503 if (sect != NULL)
17504 {
17505 Elf_External_ABIFlags_v0 *abiflags_ext;
17506 Elf_Internal_ABIFlags_v0 abiflags_in;
17507
17508 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
17509 {
17510 error (_("Corrupt MIPS ABI Flags section.\n"));
17511 res = FALSE;
17512 }
17513 else
17514 {
17515 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
17516 sect->sh_size, _("MIPS ABI Flags section"));
17517 if (abiflags_ext)
17518 {
17519 abiflags_in.version = BYTE_GET (abiflags_ext->version);
17520 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
17521 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
17522 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
17523 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
17524 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
17525 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
17526 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
17527 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
17528 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
17529 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
17530
17531 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
17532 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
17533 if (abiflags_in.isa_rev > 1)
17534 printf ("r%d", abiflags_in.isa_rev);
17535 printf ("\nGPR size: %d",
17536 get_mips_reg_size (abiflags_in.gpr_size));
17537 printf ("\nCPR1 size: %d",
17538 get_mips_reg_size (abiflags_in.cpr1_size));
17539 printf ("\nCPR2 size: %d",
17540 get_mips_reg_size (abiflags_in.cpr2_size));
17541 fputs ("\nFP ABI: ", stdout);
17542 print_mips_fp_abi_value (abiflags_in.fp_abi);
17543 fputs ("ISA Extension: ", stdout);
17544 print_mips_isa_ext (abiflags_in.isa_ext);
17545 fputs ("\nASEs:", stdout);
17546 print_mips_ases (abiflags_in.ases);
17547 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
17548 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
17549 fputc ('\n', stdout);
17550 free (abiflags_ext);
17551 }
17552 }
17553 }
17554
17555 /* We have a lot of special sections. Thanks SGI! */
17556 if (filedata->dynamic_section == NULL)
17557 {
17558 /* No dynamic information available. See if there is static GOT. */
17559 sect = find_section (filedata, ".got");
17560 if (sect != NULL)
17561 {
17562 unsigned char *data_end;
17563 unsigned char *data;
17564 bfd_vma ent, end;
17565 int addr_size;
17566
17567 pltgot = sect->sh_addr;
17568
17569 ent = pltgot;
17570 addr_size = (is_32bit_elf ? 4 : 8);
17571 end = pltgot + sect->sh_size;
17572
17573 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
17574 end - pltgot, 1,
17575 _("Global Offset Table data"));
17576 /* PR 12855: Null data is handled gracefully throughout. */
17577 data_end = data + (end - pltgot);
17578
17579 printf (_("\nStatic GOT:\n"));
17580 printf (_(" Canonical gp value: "));
17581 print_vma (ent + 0x7ff0, LONG_HEX);
17582 printf ("\n\n");
17583
17584 /* In a dynamic binary GOT[0] is reserved for the dynamic
17585 loader to store the lazy resolver pointer, however in
17586 a static binary it may well have been omitted and GOT
17587 reduced to a table of addresses.
17588 PR 21344: Check for the entry being fully available
17589 before fetching it. */
17590 if (data
17591 && data + ent - pltgot + addr_size <= data_end
17592 && byte_get (data + ent - pltgot, addr_size) == 0)
17593 {
17594 printf (_(" Reserved entries:\n"));
17595 printf (_(" %*s %10s %*s\n"),
17596 addr_size * 2, _("Address"), _("Access"),
17597 addr_size * 2, _("Value"));
17598 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17599 printf ("\n");
17600 if (ent == (bfd_vma) -1)
17601 goto sgot_print_fail;
17602
17603 /* Check for the MSB of GOT[1] being set, identifying a
17604 GNU object. This entry will be used by some runtime
17605 loaders, to store the module pointer. Otherwise this
17606 is an ordinary local entry.
17607 PR 21344: Check for the entry being fully available
17608 before fetching it. */
17609 if (data
17610 && data + ent - pltgot + addr_size <= data_end
17611 && (byte_get (data + ent - pltgot, addr_size)
17612 >> (addr_size * 8 - 1)) != 0)
17613 {
17614 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17615 printf ("\n");
17616 if (ent == (bfd_vma) -1)
17617 goto sgot_print_fail;
17618 }
17619 printf ("\n");
17620 }
17621
17622 if (data != NULL && ent < end)
17623 {
17624 printf (_(" Local entries:\n"));
17625 printf (" %*s %10s %*s\n",
17626 addr_size * 2, _("Address"), _("Access"),
17627 addr_size * 2, _("Value"));
17628 while (ent < end)
17629 {
17630 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17631 printf ("\n");
17632 if (ent == (bfd_vma) -1)
17633 goto sgot_print_fail;
17634 }
17635 printf ("\n");
17636 }
17637
17638 sgot_print_fail:
17639 free (data);
17640 }
17641 return res;
17642 }
17643
17644 for (entry = filedata->dynamic_section;
17645 /* PR 17531 file: 012-50589-0.004. */
17646 (entry < filedata->dynamic_section + filedata->dynamic_nent
17647 && entry->d_tag != DT_NULL);
17648 ++entry)
17649 switch (entry->d_tag)
17650 {
17651 case DT_MIPS_LIBLIST:
17652 liblist_offset
17653 = offset_from_vma (filedata, entry->d_un.d_val,
17654 liblistno * sizeof (Elf32_External_Lib));
17655 break;
17656 case DT_MIPS_LIBLISTNO:
17657 liblistno = entry->d_un.d_val;
17658 break;
17659 case DT_MIPS_OPTIONS:
17660 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
17661 break;
17662 case DT_MIPS_CONFLICT:
17663 conflicts_offset
17664 = offset_from_vma (filedata, entry->d_un.d_val,
17665 conflictsno * sizeof (Elf32_External_Conflict));
17666 break;
17667 case DT_MIPS_CONFLICTNO:
17668 conflictsno = entry->d_un.d_val;
17669 break;
17670 case DT_PLTGOT:
17671 pltgot = entry->d_un.d_ptr;
17672 break;
17673 case DT_MIPS_LOCAL_GOTNO:
17674 local_gotno = entry->d_un.d_val;
17675 break;
17676 case DT_MIPS_GOTSYM:
17677 gotsym = entry->d_un.d_val;
17678 break;
17679 case DT_MIPS_SYMTABNO:
17680 symtabno = entry->d_un.d_val;
17681 break;
17682 case DT_MIPS_PLTGOT:
17683 mips_pltgot = entry->d_un.d_ptr;
17684 break;
17685 case DT_PLTREL:
17686 pltrel = entry->d_un.d_val;
17687 break;
17688 case DT_PLTRELSZ:
17689 pltrelsz = entry->d_un.d_val;
17690 break;
17691 case DT_JMPREL:
17692 jmprel = entry->d_un.d_ptr;
17693 break;
17694 default:
17695 break;
17696 }
17697
17698 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
17699 {
17700 Elf32_External_Lib * elib;
17701 size_t cnt;
17702
17703 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
17704 sizeof (Elf32_External_Lib),
17705 liblistno,
17706 _("liblist section data"));
17707 if (elib)
17708 {
17709 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
17710 "\nSection '.liblist' contains %lu entries:\n",
17711 (unsigned long) liblistno),
17712 (unsigned long) liblistno);
17713 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
17714 stdout);
17715
17716 for (cnt = 0; cnt < liblistno; ++cnt)
17717 {
17718 Elf32_Lib liblist;
17719 time_t atime;
17720 char timebuf[128];
17721 struct tm * tmp;
17722
17723 liblist.l_name = BYTE_GET (elib[cnt].l_name);
17724 atime = BYTE_GET (elib[cnt].l_time_stamp);
17725 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
17726 liblist.l_version = BYTE_GET (elib[cnt].l_version);
17727 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
17728
17729 tmp = gmtime (&atime);
17730 snprintf (timebuf, sizeof (timebuf),
17731 "%04u-%02u-%02uT%02u:%02u:%02u",
17732 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
17733 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
17734
17735 printf ("%3lu: ", (unsigned long) cnt);
17736 if (VALID_DYNAMIC_NAME (filedata, liblist.l_name))
17737 print_symbol (20, GET_DYNAMIC_NAME (filedata, liblist.l_name));
17738 else
17739 printf (_("<corrupt: %9ld>"), liblist.l_name);
17740 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
17741 liblist.l_version);
17742
17743 if (liblist.l_flags == 0)
17744 puts (_(" NONE"));
17745 else
17746 {
17747 static const struct
17748 {
17749 const char * name;
17750 int bit;
17751 }
17752 l_flags_vals[] =
17753 {
17754 { " EXACT_MATCH", LL_EXACT_MATCH },
17755 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
17756 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
17757 { " EXPORTS", LL_EXPORTS },
17758 { " DELAY_LOAD", LL_DELAY_LOAD },
17759 { " DELTA", LL_DELTA }
17760 };
17761 int flags = liblist.l_flags;
17762 size_t fcnt;
17763
17764 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
17765 if ((flags & l_flags_vals[fcnt].bit) != 0)
17766 {
17767 fputs (l_flags_vals[fcnt].name, stdout);
17768 flags ^= l_flags_vals[fcnt].bit;
17769 }
17770 if (flags != 0)
17771 printf (" %#x", (unsigned int) flags);
17772
17773 puts ("");
17774 }
17775 }
17776
17777 free (elib);
17778 }
17779 else
17780 res = FALSE;
17781 }
17782
17783 if (options_offset != 0)
17784 {
17785 Elf_External_Options * eopt;
17786 size_t offset;
17787 int cnt;
17788 sect = filedata->section_headers;
17789
17790 /* Find the section header so that we get the size. */
17791 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
17792 /* PR 17533 file: 012-277276-0.004. */
17793 if (sect == NULL)
17794 {
17795 error (_("No MIPS_OPTIONS header found\n"));
17796 return FALSE;
17797 }
17798 /* PR 24243 */
17799 if (sect->sh_size < sizeof (* eopt))
17800 {
17801 error (_("The MIPS options section is too small.\n"));
17802 return FALSE;
17803 }
17804
17805 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
17806 sect->sh_size, _("options"));
17807 if (eopt)
17808 {
17809 Elf_Internal_Options option;
17810
17811 offset = cnt = 0;
17812 while (offset <= sect->sh_size - sizeof (* eopt))
17813 {
17814 Elf_External_Options * eoption;
17815 unsigned int optsize;
17816
17817 eoption = (Elf_External_Options *) ((char *) eopt + offset);
17818
17819 optsize = BYTE_GET (eoption->size);
17820
17821 /* PR 17531: file: ffa0fa3b. */
17822 if (optsize < sizeof (* eopt)
17823 || optsize > sect->sh_size - offset)
17824 {
17825 error (_("Invalid size (%u) for MIPS option\n"),
17826 optsize);
17827 free (eopt);
17828 return FALSE;
17829 }
17830 offset += optsize;
17831 ++cnt;
17832 }
17833
17834 printf (ngettext ("\nSection '%s' contains %d entry:\n",
17835 "\nSection '%s' contains %d entries:\n",
17836 cnt),
17837 printable_section_name (filedata, sect), cnt);
17838
17839 offset = 0;
17840 while (cnt-- > 0)
17841 {
17842 size_t len;
17843 Elf_External_Options * eoption;
17844
17845 eoption = (Elf_External_Options *) ((char *) eopt + offset);
17846
17847 option.kind = BYTE_GET (eoption->kind);
17848 option.size = BYTE_GET (eoption->size);
17849 option.section = BYTE_GET (eoption->section);
17850 option.info = BYTE_GET (eoption->info);
17851
17852 switch (option.kind)
17853 {
17854 case ODK_NULL:
17855 /* This shouldn't happen. */
17856 printf (" NULL %" PRId16 " %" PRIx32,
17857 option.section, option.info);
17858 break;
17859
17860 case ODK_REGINFO:
17861 printf (" REGINFO ");
17862 if (filedata->file_header.e_machine == EM_MIPS)
17863 {
17864 Elf32_External_RegInfo * ereg;
17865 Elf32_RegInfo reginfo;
17866
17867 /* 32bit form. */
17868 if (option.size < (sizeof (Elf_External_Options)
17869 + sizeof (Elf32_External_RegInfo)))
17870 {
17871 printf (_("<corrupt>\n"));
17872 error (_("Truncated MIPS REGINFO option\n"));
17873 cnt = 0;
17874 break;
17875 }
17876
17877 ereg = (Elf32_External_RegInfo *) (eoption + 1);
17878
17879 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
17880 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
17881 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
17882 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
17883 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
17884 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
17885
17886 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
17887 reginfo.ri_gprmask, reginfo.ri_gp_value);
17888 printf (" "
17889 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
17890 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
17891 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
17892 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
17893 }
17894 else
17895 {
17896 /* 64 bit form. */
17897 Elf64_External_RegInfo * ereg;
17898 Elf64_Internal_RegInfo reginfo;
17899
17900 if (option.size < (sizeof (Elf_External_Options)
17901 + sizeof (Elf64_External_RegInfo)))
17902 {
17903 printf (_("<corrupt>\n"));
17904 error (_("Truncated MIPS REGINFO option\n"));
17905 cnt = 0;
17906 break;
17907 }
17908
17909 ereg = (Elf64_External_RegInfo *) (eoption + 1);
17910 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
17911 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
17912 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
17913 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
17914 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
17915 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
17916
17917 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
17918 reginfo.ri_gprmask, reginfo.ri_gp_value);
17919 printf (" "
17920 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
17921 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
17922 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
17923 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
17924 }
17925 offset += option.size;
17926 continue;
17927
17928 case ODK_EXCEPTIONS:
17929 fputs (" EXCEPTIONS fpe_min(", stdout);
17930 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
17931 fputs (") fpe_max(", stdout);
17932 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
17933 fputs (")", stdout);
17934
17935 if (option.info & OEX_PAGE0)
17936 fputs (" PAGE0", stdout);
17937 if (option.info & OEX_SMM)
17938 fputs (" SMM", stdout);
17939 if (option.info & OEX_FPDBUG)
17940 fputs (" FPDBUG", stdout);
17941 if (option.info & OEX_DISMISS)
17942 fputs (" DISMISS", stdout);
17943 break;
17944
17945 case ODK_PAD:
17946 fputs (" PAD ", stdout);
17947 if (option.info & OPAD_PREFIX)
17948 fputs (" PREFIX", stdout);
17949 if (option.info & OPAD_POSTFIX)
17950 fputs (" POSTFIX", stdout);
17951 if (option.info & OPAD_SYMBOL)
17952 fputs (" SYMBOL", stdout);
17953 break;
17954
17955 case ODK_HWPATCH:
17956 fputs (" HWPATCH ", stdout);
17957 if (option.info & OHW_R4KEOP)
17958 fputs (" R4KEOP", stdout);
17959 if (option.info & OHW_R8KPFETCH)
17960 fputs (" R8KPFETCH", stdout);
17961 if (option.info & OHW_R5KEOP)
17962 fputs (" R5KEOP", stdout);
17963 if (option.info & OHW_R5KCVTL)
17964 fputs (" R5KCVTL", stdout);
17965 break;
17966
17967 case ODK_FILL:
17968 fputs (" FILL ", stdout);
17969 /* XXX Print content of info word? */
17970 break;
17971
17972 case ODK_TAGS:
17973 fputs (" TAGS ", stdout);
17974 /* XXX Print content of info word? */
17975 break;
17976
17977 case ODK_HWAND:
17978 fputs (" HWAND ", stdout);
17979 if (option.info & OHWA0_R4KEOP_CHECKED)
17980 fputs (" R4KEOP_CHECKED", stdout);
17981 if (option.info & OHWA0_R4KEOP_CLEAN)
17982 fputs (" R4KEOP_CLEAN", stdout);
17983 break;
17984
17985 case ODK_HWOR:
17986 fputs (" HWOR ", stdout);
17987 if (option.info & OHWA0_R4KEOP_CHECKED)
17988 fputs (" R4KEOP_CHECKED", stdout);
17989 if (option.info & OHWA0_R4KEOP_CLEAN)
17990 fputs (" R4KEOP_CLEAN", stdout);
17991 break;
17992
17993 case ODK_GP_GROUP:
17994 printf (" GP_GROUP %#06x self-contained %#06x",
17995 option.info & OGP_GROUP,
17996 (option.info & OGP_SELF) >> 16);
17997 break;
17998
17999 case ODK_IDENT:
18000 printf (" IDENT %#06x self-contained %#06x",
18001 option.info & OGP_GROUP,
18002 (option.info & OGP_SELF) >> 16);
18003 break;
18004
18005 default:
18006 /* This shouldn't happen. */
18007 printf (" %3d ??? %" PRId16 " %" PRIx32,
18008 option.kind, option.section, option.info);
18009 break;
18010 }
18011
18012 len = sizeof (* eopt);
18013 while (len < option.size)
18014 {
18015 unsigned char datum = *((unsigned char *) eoption + len);
18016
18017 if (ISPRINT (datum))
18018 printf ("%c", datum);
18019 else
18020 printf ("\\%03o", datum);
18021 len ++;
18022 }
18023 fputs ("\n", stdout);
18024
18025 offset += option.size;
18026 }
18027 free (eopt);
18028 }
18029 else
18030 res = FALSE;
18031 }
18032
18033 if (conflicts_offset != 0 && conflictsno != 0)
18034 {
18035 Elf32_Conflict * iconf;
18036 size_t cnt;
18037
18038 if (filedata->dynamic_symbols == NULL)
18039 {
18040 error (_("conflict list found without a dynamic symbol table\n"));
18041 return FALSE;
18042 }
18043
18044 /* PR 21345 - print a slightly more helpful error message
18045 if we are sure that the cmalloc will fail. */
18046 if (conflictsno > filedata->file_size / sizeof (* iconf))
18047 {
18048 error (_("Overlarge number of conflicts detected: %lx\n"),
18049 (long) conflictsno);
18050 return FALSE;
18051 }
18052
18053 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18054 if (iconf == NULL)
18055 {
18056 error (_("Out of memory allocating space for dynamic conflicts\n"));
18057 return FALSE;
18058 }
18059
18060 if (is_32bit_elf)
18061 {
18062 Elf32_External_Conflict * econf32;
18063
18064 econf32 = (Elf32_External_Conflict *)
18065 get_data (NULL, filedata, conflicts_offset,
18066 sizeof (*econf32), conflictsno, _("conflict"));
18067 if (!econf32)
18068 {
18069 free (iconf);
18070 return FALSE;
18071 }
18072
18073 for (cnt = 0; cnt < conflictsno; ++cnt)
18074 iconf[cnt] = BYTE_GET (econf32[cnt]);
18075
18076 free (econf32);
18077 }
18078 else
18079 {
18080 Elf64_External_Conflict * econf64;
18081
18082 econf64 = (Elf64_External_Conflict *)
18083 get_data (NULL, filedata, conflicts_offset,
18084 sizeof (*econf64), conflictsno, _("conflict"));
18085 if (!econf64)
18086 {
18087 free (iconf);
18088 return FALSE;
18089 }
18090
18091 for (cnt = 0; cnt < conflictsno; ++cnt)
18092 iconf[cnt] = BYTE_GET (econf64[cnt]);
18093
18094 free (econf64);
18095 }
18096
18097 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18098 "\nSection '.conflict' contains %lu entries:\n",
18099 (unsigned long) conflictsno),
18100 (unsigned long) conflictsno);
18101 puts (_(" Num: Index Value Name"));
18102
18103 for (cnt = 0; cnt < conflictsno; ++cnt)
18104 {
18105 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18106
18107 if (iconf[cnt] >= filedata->num_dynamic_syms)
18108 printf (_("<corrupt symbol index>"));
18109 else
18110 {
18111 Elf_Internal_Sym * psym;
18112
18113 psym = & filedata->dynamic_symbols[iconf[cnt]];
18114 print_vma (psym->st_value, FULL_HEX);
18115 putchar (' ');
18116 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18117 print_symbol (25, GET_DYNAMIC_NAME (filedata, psym->st_name));
18118 else
18119 printf (_("<corrupt: %14ld>"), psym->st_name);
18120 }
18121 putchar ('\n');
18122 }
18123
18124 free (iconf);
18125 }
18126
18127 if (pltgot != 0 && local_gotno != 0)
18128 {
18129 bfd_vma ent, local_end, global_end;
18130 size_t i, offset;
18131 unsigned char * data;
18132 unsigned char * data_end;
18133 int addr_size;
18134
18135 ent = pltgot;
18136 addr_size = (is_32bit_elf ? 4 : 8);
18137 local_end = pltgot + local_gotno * addr_size;
18138
18139 /* PR binutils/17533 file: 012-111227-0.004 */
18140 if (symtabno < gotsym)
18141 {
18142 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18143 (unsigned long) gotsym, (unsigned long) symtabno);
18144 return FALSE;
18145 }
18146
18147 global_end = local_end + (symtabno - gotsym) * addr_size;
18148 /* PR 17531: file: 54c91a34. */
18149 if (global_end < local_end)
18150 {
18151 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18152 return FALSE;
18153 }
18154
18155 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18156 data = (unsigned char *) get_data (NULL, filedata, offset,
18157 global_end - pltgot, 1,
18158 _("Global Offset Table data"));
18159 /* PR 12855: Null data is handled gracefully throughout. */
18160 data_end = data + (global_end - pltgot);
18161
18162 printf (_("\nPrimary GOT:\n"));
18163 printf (_(" Canonical gp value: "));
18164 print_vma (pltgot + 0x7ff0, LONG_HEX);
18165 printf ("\n\n");
18166
18167 printf (_(" Reserved entries:\n"));
18168 printf (_(" %*s %10s %*s Purpose\n"),
18169 addr_size * 2, _("Address"), _("Access"),
18170 addr_size * 2, _("Initial"));
18171 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18172 printf (_(" Lazy resolver\n"));
18173 if (ent == (bfd_vma) -1)
18174 goto got_print_fail;
18175
18176 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18177 This entry will be used by some runtime loaders, to store the
18178 module pointer. Otherwise this is an ordinary local entry.
18179 PR 21344: Check for the entry being fully available before
18180 fetching it. */
18181 if (data
18182 && data + ent - pltgot + addr_size <= data_end
18183 && (byte_get (data + ent - pltgot, addr_size)
18184 >> (addr_size * 8 - 1)) != 0)
18185 {
18186 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18187 printf (_(" Module pointer (GNU extension)\n"));
18188 if (ent == (bfd_vma) -1)
18189 goto got_print_fail;
18190 }
18191 printf ("\n");
18192
18193 if (data != NULL && ent < local_end)
18194 {
18195 printf (_(" Local entries:\n"));
18196 printf (" %*s %10s %*s\n",
18197 addr_size * 2, _("Address"), _("Access"),
18198 addr_size * 2, _("Initial"));
18199 while (ent < local_end)
18200 {
18201 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18202 printf ("\n");
18203 if (ent == (bfd_vma) -1)
18204 goto got_print_fail;
18205 }
18206 printf ("\n");
18207 }
18208
18209 if (data != NULL && gotsym < symtabno)
18210 {
18211 int sym_width;
18212
18213 printf (_(" Global entries:\n"));
18214 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
18215 addr_size * 2, _("Address"),
18216 _("Access"),
18217 addr_size * 2, _("Initial"),
18218 addr_size * 2, _("Sym.Val."),
18219 _("Type"),
18220 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18221 _("Ndx"), _("Name"));
18222
18223 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
18224
18225 for (i = gotsym; i < symtabno; i++)
18226 {
18227 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18228 printf (" ");
18229
18230 if (filedata->dynamic_symbols == NULL)
18231 printf (_("<no dynamic symbols>"));
18232 else if (i < filedata->num_dynamic_syms)
18233 {
18234 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
18235
18236 print_vma (psym->st_value, LONG_HEX);
18237 printf (" %-7s %3s ",
18238 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18239 get_symbol_index_type (filedata, psym->st_shndx));
18240
18241 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18242 print_symbol (sym_width,
18243 GET_DYNAMIC_NAME (filedata, psym->st_name));
18244 else
18245 printf (_("<corrupt: %14ld>"), psym->st_name);
18246 }
18247 else
18248 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18249 (unsigned long) i);
18250
18251 printf ("\n");
18252 if (ent == (bfd_vma) -1)
18253 break;
18254 }
18255 printf ("\n");
18256 }
18257
18258 got_print_fail:
18259 free (data);
18260 }
18261
18262 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18263 {
18264 bfd_vma ent, end;
18265 size_t offset, rel_offset;
18266 unsigned long count, i;
18267 unsigned char * data;
18268 int addr_size, sym_width;
18269 Elf_Internal_Rela * rels;
18270
18271 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
18272 if (pltrel == DT_RELA)
18273 {
18274 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18275 return FALSE;
18276 }
18277 else
18278 {
18279 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18280 return FALSE;
18281 }
18282
18283 ent = mips_pltgot;
18284 addr_size = (is_32bit_elf ? 4 : 8);
18285 end = mips_pltgot + (2 + count) * addr_size;
18286
18287 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18288 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
18289 1, _("Procedure Linkage Table data"));
18290 if (data == NULL)
18291 {
18292 free (rels);
18293 return FALSE;
18294 }
18295
18296 printf ("\nPLT GOT:\n\n");
18297 printf (_(" Reserved entries:\n"));
18298 printf (_(" %*s %*s Purpose\n"),
18299 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
18300 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18301 printf (_(" PLT lazy resolver\n"));
18302 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18303 printf (_(" Module pointer\n"));
18304 printf ("\n");
18305
18306 printf (_(" Entries:\n"));
18307 printf (" %*s %*s %*s %-7s %3s %s\n",
18308 addr_size * 2, _("Address"),
18309 addr_size * 2, _("Initial"),
18310 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
18311 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18312 for (i = 0; i < count; i++)
18313 {
18314 unsigned long idx = get_reloc_symindex (rels[i].r_info);
18315
18316 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18317 printf (" ");
18318
18319 if (idx >= filedata->num_dynamic_syms)
18320 printf (_("<corrupt symbol index: %lu>"), idx);
18321 else
18322 {
18323 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
18324
18325 print_vma (psym->st_value, LONG_HEX);
18326 printf (" %-7s %3s ",
18327 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18328 get_symbol_index_type (filedata, psym->st_shndx));
18329 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18330 print_symbol (sym_width,
18331 GET_DYNAMIC_NAME (filedata, psym->st_name));
18332 else
18333 printf (_("<corrupt: %14ld>"), psym->st_name);
18334 }
18335 printf ("\n");
18336 }
18337 printf ("\n");
18338
18339 free (data);
18340 free (rels);
18341 }
18342
18343 return res;
18344 }
18345
18346 static bfd_boolean
18347 process_nds32_specific (Filedata * filedata)
18348 {
18349 Elf_Internal_Shdr *sect = NULL;
18350
18351 sect = find_section (filedata, ".nds32_e_flags");
18352 if (sect != NULL && sect->sh_size >= 4)
18353 {
18354 unsigned char *buf;
18355 unsigned int flag;
18356
18357 printf ("\nNDS32 elf flags section:\n");
18358 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18359 _("NDS32 elf flags section"));
18360
18361 if (buf == NULL)
18362 return FALSE;
18363
18364 flag = byte_get (buf, 4);
18365 free (buf);
18366 switch (flag & 0x3)
18367 {
18368 case 0:
18369 printf ("(VEC_SIZE):\tNo entry.\n");
18370 break;
18371 case 1:
18372 printf ("(VEC_SIZE):\t4 bytes\n");
18373 break;
18374 case 2:
18375 printf ("(VEC_SIZE):\t16 bytes\n");
18376 break;
18377 case 3:
18378 printf ("(VEC_SIZE):\treserved\n");
18379 break;
18380 }
18381 }
18382
18383 return TRUE;
18384 }
18385
18386 static bfd_boolean
18387 process_gnu_liblist (Filedata * filedata)
18388 {
18389 Elf_Internal_Shdr * section;
18390 Elf_Internal_Shdr * string_sec;
18391 Elf32_External_Lib * elib;
18392 char * strtab;
18393 size_t strtab_size;
18394 size_t cnt;
18395 unsigned long num_liblist;
18396 unsigned i;
18397 bfd_boolean res = TRUE;
18398
18399 if (! do_arch)
18400 return TRUE;
18401
18402 for (i = 0, section = filedata->section_headers;
18403 i < filedata->file_header.e_shnum;
18404 i++, section++)
18405 {
18406 switch (section->sh_type)
18407 {
18408 case SHT_GNU_LIBLIST:
18409 if (section->sh_link >= filedata->file_header.e_shnum)
18410 break;
18411
18412 elib = (Elf32_External_Lib *)
18413 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
18414 _("liblist section data"));
18415
18416 if (elib == NULL)
18417 {
18418 res = FALSE;
18419 break;
18420 }
18421
18422 string_sec = filedata->section_headers + section->sh_link;
18423 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
18424 string_sec->sh_size,
18425 _("liblist string table"));
18426 if (strtab == NULL
18427 || section->sh_entsize != sizeof (Elf32_External_Lib))
18428 {
18429 free (elib);
18430 free (strtab);
18431 res = FALSE;
18432 break;
18433 }
18434 strtab_size = string_sec->sh_size;
18435
18436 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
18437 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
18438 "\nLibrary list section '%s' contains %lu entries:\n",
18439 num_liblist),
18440 printable_section_name (filedata, section),
18441 num_liblist);
18442
18443 puts (_(" Library Time Stamp Checksum Version Flags"));
18444
18445 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
18446 ++cnt)
18447 {
18448 Elf32_Lib liblist;
18449 time_t atime;
18450 char timebuf[128];
18451 struct tm * tmp;
18452
18453 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18454 atime = BYTE_GET (elib[cnt].l_time_stamp);
18455 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18456 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18457 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18458
18459 tmp = gmtime (&atime);
18460 snprintf (timebuf, sizeof (timebuf),
18461 "%04u-%02u-%02uT%02u:%02u:%02u",
18462 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18463 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18464
18465 printf ("%3lu: ", (unsigned long) cnt);
18466 if (do_wide)
18467 printf ("%-20s", liblist.l_name < strtab_size
18468 ? strtab + liblist.l_name : _("<corrupt>"));
18469 else
18470 printf ("%-20.20s", liblist.l_name < strtab_size
18471 ? strtab + liblist.l_name : _("<corrupt>"));
18472 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
18473 liblist.l_version, liblist.l_flags);
18474 }
18475
18476 free (elib);
18477 free (strtab);
18478 }
18479 }
18480
18481 return res;
18482 }
18483
18484 static const char *
18485 get_note_type (Filedata * filedata, unsigned e_type)
18486 {
18487 static char buff[64];
18488
18489 if (filedata->file_header.e_type == ET_CORE)
18490 switch (e_type)
18491 {
18492 case NT_AUXV:
18493 return _("NT_AUXV (auxiliary vector)");
18494 case NT_PRSTATUS:
18495 return _("NT_PRSTATUS (prstatus structure)");
18496 case NT_FPREGSET:
18497 return _("NT_FPREGSET (floating point registers)");
18498 case NT_PRPSINFO:
18499 return _("NT_PRPSINFO (prpsinfo structure)");
18500 case NT_TASKSTRUCT:
18501 return _("NT_TASKSTRUCT (task structure)");
18502 case NT_PRXFPREG:
18503 return _("NT_PRXFPREG (user_xfpregs structure)");
18504 case NT_PPC_VMX:
18505 return _("NT_PPC_VMX (ppc Altivec registers)");
18506 case NT_PPC_VSX:
18507 return _("NT_PPC_VSX (ppc VSX registers)");
18508 case NT_PPC_TAR:
18509 return _("NT_PPC_TAR (ppc TAR register)");
18510 case NT_PPC_PPR:
18511 return _("NT_PPC_PPR (ppc PPR register)");
18512 case NT_PPC_DSCR:
18513 return _("NT_PPC_DSCR (ppc DSCR register)");
18514 case NT_PPC_EBB:
18515 return _("NT_PPC_EBB (ppc EBB registers)");
18516 case NT_PPC_PMU:
18517 return _("NT_PPC_PMU (ppc PMU registers)");
18518 case NT_PPC_TM_CGPR:
18519 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
18520 case NT_PPC_TM_CFPR:
18521 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
18522 case NT_PPC_TM_CVMX:
18523 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
18524 case NT_PPC_TM_CVSX:
18525 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
18526 case NT_PPC_TM_SPR:
18527 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
18528 case NT_PPC_TM_CTAR:
18529 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
18530 case NT_PPC_TM_CPPR:
18531 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
18532 case NT_PPC_TM_CDSCR:
18533 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
18534 case NT_386_TLS:
18535 return _("NT_386_TLS (x86 TLS information)");
18536 case NT_386_IOPERM:
18537 return _("NT_386_IOPERM (x86 I/O permissions)");
18538 case NT_X86_XSTATE:
18539 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
18540 case NT_X86_CET:
18541 return _("NT_X86_CET (x86 CET state)");
18542 case NT_S390_HIGH_GPRS:
18543 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
18544 case NT_S390_TIMER:
18545 return _("NT_S390_TIMER (s390 timer register)");
18546 case NT_S390_TODCMP:
18547 return _("NT_S390_TODCMP (s390 TOD comparator register)");
18548 case NT_S390_TODPREG:
18549 return _("NT_S390_TODPREG (s390 TOD programmable register)");
18550 case NT_S390_CTRS:
18551 return _("NT_S390_CTRS (s390 control registers)");
18552 case NT_S390_PREFIX:
18553 return _("NT_S390_PREFIX (s390 prefix register)");
18554 case NT_S390_LAST_BREAK:
18555 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
18556 case NT_S390_SYSTEM_CALL:
18557 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
18558 case NT_S390_TDB:
18559 return _("NT_S390_TDB (s390 transaction diagnostic block)");
18560 case NT_S390_VXRS_LOW:
18561 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
18562 case NT_S390_VXRS_HIGH:
18563 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
18564 case NT_S390_GS_CB:
18565 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
18566 case NT_S390_GS_BC:
18567 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
18568 case NT_ARM_VFP:
18569 return _("NT_ARM_VFP (arm VFP registers)");
18570 case NT_ARM_TLS:
18571 return _("NT_ARM_TLS (AArch TLS registers)");
18572 case NT_ARM_HW_BREAK:
18573 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
18574 case NT_ARM_HW_WATCH:
18575 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
18576 case NT_ARC_V2:
18577 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
18578 case NT_PSTATUS:
18579 return _("NT_PSTATUS (pstatus structure)");
18580 case NT_FPREGS:
18581 return _("NT_FPREGS (floating point registers)");
18582 case NT_PSINFO:
18583 return _("NT_PSINFO (psinfo structure)");
18584 case NT_LWPSTATUS:
18585 return _("NT_LWPSTATUS (lwpstatus_t structure)");
18586 case NT_LWPSINFO:
18587 return _("NT_LWPSINFO (lwpsinfo_t structure)");
18588 case NT_WIN32PSTATUS:
18589 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
18590 case NT_SIGINFO:
18591 return _("NT_SIGINFO (siginfo_t data)");
18592 case NT_FILE:
18593 return _("NT_FILE (mapped files)");
18594 default:
18595 break;
18596 }
18597 else
18598 switch (e_type)
18599 {
18600 case NT_VERSION:
18601 return _("NT_VERSION (version)");
18602 case NT_ARCH:
18603 return _("NT_ARCH (architecture)");
18604 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18605 return _("OPEN");
18606 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18607 return _("func");
18608 default:
18609 break;
18610 }
18611
18612 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18613 return buff;
18614 }
18615
18616 static bfd_boolean
18617 print_core_note (Elf_Internal_Note *pnote)
18618 {
18619 unsigned int addr_size = is_32bit_elf ? 4 : 8;
18620 bfd_vma count, page_size;
18621 unsigned char *descdata, *filenames, *descend;
18622
18623 if (pnote->type != NT_FILE)
18624 {
18625 if (do_wide)
18626 printf ("\n");
18627 return TRUE;
18628 }
18629
18630 #ifndef BFD64
18631 if (!is_32bit_elf)
18632 {
18633 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
18634 /* Still "successful". */
18635 return TRUE;
18636 }
18637 #endif
18638
18639 if (pnote->descsz < 2 * addr_size)
18640 {
18641 error (_(" Malformed note - too short for header\n"));
18642 return FALSE;
18643 }
18644
18645 descdata = (unsigned char *) pnote->descdata;
18646 descend = descdata + pnote->descsz;
18647
18648 if (descdata[pnote->descsz - 1] != '\0')
18649 {
18650 error (_(" Malformed note - does not end with \\0\n"));
18651 return FALSE;
18652 }
18653
18654 count = byte_get (descdata, addr_size);
18655 descdata += addr_size;
18656
18657 page_size = byte_get (descdata, addr_size);
18658 descdata += addr_size;
18659
18660 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
18661 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
18662 {
18663 error (_(" Malformed note - too short for supplied file count\n"));
18664 return FALSE;
18665 }
18666
18667 printf (_(" Page size: "));
18668 print_vma (page_size, DEC);
18669 printf ("\n");
18670
18671 printf (_(" %*s%*s%*s\n"),
18672 (int) (2 + 2 * addr_size), _("Start"),
18673 (int) (4 + 2 * addr_size), _("End"),
18674 (int) (4 + 2 * addr_size), _("Page Offset"));
18675 filenames = descdata + count * 3 * addr_size;
18676 while (count-- > 0)
18677 {
18678 bfd_vma start, end, file_ofs;
18679
18680 if (filenames == descend)
18681 {
18682 error (_(" Malformed note - filenames end too early\n"));
18683 return FALSE;
18684 }
18685
18686 start = byte_get (descdata, addr_size);
18687 descdata += addr_size;
18688 end = byte_get (descdata, addr_size);
18689 descdata += addr_size;
18690 file_ofs = byte_get (descdata, addr_size);
18691 descdata += addr_size;
18692
18693 printf (" ");
18694 print_vma (start, FULL_HEX);
18695 printf (" ");
18696 print_vma (end, FULL_HEX);
18697 printf (" ");
18698 print_vma (file_ofs, FULL_HEX);
18699 printf ("\n %s\n", filenames);
18700
18701 filenames += 1 + strlen ((char *) filenames);
18702 }
18703
18704 return TRUE;
18705 }
18706
18707 static const char *
18708 get_gnu_elf_note_type (unsigned e_type)
18709 {
18710 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
18711 switch (e_type)
18712 {
18713 case NT_GNU_ABI_TAG:
18714 return _("NT_GNU_ABI_TAG (ABI version tag)");
18715 case NT_GNU_HWCAP:
18716 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
18717 case NT_GNU_BUILD_ID:
18718 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
18719 case NT_GNU_GOLD_VERSION:
18720 return _("NT_GNU_GOLD_VERSION (gold version)");
18721 case NT_GNU_PROPERTY_TYPE_0:
18722 return _("NT_GNU_PROPERTY_TYPE_0");
18723 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18724 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
18725 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18726 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
18727 default:
18728 {
18729 static char buff[64];
18730
18731 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18732 return buff;
18733 }
18734 }
18735 }
18736
18737 static void
18738 decode_x86_compat_isa (unsigned int bitmask)
18739 {
18740 while (bitmask)
18741 {
18742 unsigned int bit = bitmask & (- bitmask);
18743
18744 bitmask &= ~ bit;
18745 switch (bit)
18746 {
18747 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
18748 printf ("i486");
18749 break;
18750 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
18751 printf ("586");
18752 break;
18753 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
18754 printf ("686");
18755 break;
18756 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
18757 printf ("SSE");
18758 break;
18759 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
18760 printf ("SSE2");
18761 break;
18762 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
18763 printf ("SSE3");
18764 break;
18765 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
18766 printf ("SSSE3");
18767 break;
18768 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
18769 printf ("SSE4_1");
18770 break;
18771 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
18772 printf ("SSE4_2");
18773 break;
18774 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
18775 printf ("AVX");
18776 break;
18777 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
18778 printf ("AVX2");
18779 break;
18780 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
18781 printf ("AVX512F");
18782 break;
18783 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
18784 printf ("AVX512CD");
18785 break;
18786 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
18787 printf ("AVX512ER");
18788 break;
18789 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
18790 printf ("AVX512PF");
18791 break;
18792 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
18793 printf ("AVX512VL");
18794 break;
18795 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
18796 printf ("AVX512DQ");
18797 break;
18798 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
18799 printf ("AVX512BW");
18800 break;
18801 default:
18802 printf (_("<unknown: %x>"), bit);
18803 break;
18804 }
18805 if (bitmask)
18806 printf (", ");
18807 }
18808 }
18809
18810 static void
18811 decode_x86_compat_2_isa (unsigned int bitmask)
18812 {
18813 if (!bitmask)
18814 {
18815 printf (_("<None>"));
18816 return;
18817 }
18818
18819 while (bitmask)
18820 {
18821 unsigned int bit = bitmask & (- bitmask);
18822
18823 bitmask &= ~ bit;
18824 switch (bit)
18825 {
18826 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
18827 printf ("CMOV");
18828 break;
18829 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
18830 printf ("SSE");
18831 break;
18832 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
18833 printf ("SSE2");
18834 break;
18835 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
18836 printf ("SSE3");
18837 break;
18838 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
18839 printf ("SSSE3");
18840 break;
18841 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
18842 printf ("SSE4_1");
18843 break;
18844 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
18845 printf ("SSE4_2");
18846 break;
18847 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
18848 printf ("AVX");
18849 break;
18850 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
18851 printf ("AVX2");
18852 break;
18853 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
18854 printf ("FMA");
18855 break;
18856 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
18857 printf ("AVX512F");
18858 break;
18859 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
18860 printf ("AVX512CD");
18861 break;
18862 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
18863 printf ("AVX512ER");
18864 break;
18865 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
18866 printf ("AVX512PF");
18867 break;
18868 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
18869 printf ("AVX512VL");
18870 break;
18871 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
18872 printf ("AVX512DQ");
18873 break;
18874 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
18875 printf ("AVX512BW");
18876 break;
18877 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
18878 printf ("AVX512_4FMAPS");
18879 break;
18880 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
18881 printf ("AVX512_4VNNIW");
18882 break;
18883 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
18884 printf ("AVX512_BITALG");
18885 break;
18886 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
18887 printf ("AVX512_IFMA");
18888 break;
18889 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
18890 printf ("AVX512_VBMI");
18891 break;
18892 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
18893 printf ("AVX512_VBMI2");
18894 break;
18895 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
18896 printf ("AVX512_VNNI");
18897 break;
18898 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
18899 printf ("AVX512_BF16");
18900 break;
18901 default:
18902 printf (_("<unknown: %x>"), bit);
18903 break;
18904 }
18905 if (bitmask)
18906 printf (", ");
18907 }
18908 }
18909
18910 static void
18911 decode_x86_isa (unsigned int bitmask)
18912 {
18913 while (bitmask)
18914 {
18915 unsigned int bit = bitmask & (- bitmask);
18916
18917 bitmask &= ~ bit;
18918 switch (bit)
18919 {
18920 case GNU_PROPERTY_X86_ISA_1_BASELINE:
18921 printf ("x86-64-baseline");
18922 break;
18923 case GNU_PROPERTY_X86_ISA_1_V2:
18924 printf ("x86-64-v2");
18925 break;
18926 case GNU_PROPERTY_X86_ISA_1_V3:
18927 printf ("x86-64-v3");
18928 break;
18929 case GNU_PROPERTY_X86_ISA_1_V4:
18930 printf ("x86-64-v4");
18931 break;
18932 default:
18933 printf (_("<unknown: %x>"), bit);
18934 break;
18935 }
18936 if (bitmask)
18937 printf (", ");
18938 }
18939 }
18940
18941 static void
18942 decode_x86_feature_1 (unsigned int bitmask)
18943 {
18944 if (!bitmask)
18945 {
18946 printf (_("<None>"));
18947 return;
18948 }
18949
18950 while (bitmask)
18951 {
18952 unsigned int bit = bitmask & (- bitmask);
18953
18954 bitmask &= ~ bit;
18955 switch (bit)
18956 {
18957 case GNU_PROPERTY_X86_FEATURE_1_IBT:
18958 printf ("IBT");
18959 break;
18960 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
18961 printf ("SHSTK");
18962 break;
18963 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
18964 printf ("LAM_U48");
18965 break;
18966 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
18967 printf ("LAM_U57");
18968 break;
18969 default:
18970 printf (_("<unknown: %x>"), bit);
18971 break;
18972 }
18973 if (bitmask)
18974 printf (", ");
18975 }
18976 }
18977
18978 static void
18979 decode_x86_feature_2 (unsigned int bitmask)
18980 {
18981 if (!bitmask)
18982 {
18983 printf (_("<None>"));
18984 return;
18985 }
18986
18987 while (bitmask)
18988 {
18989 unsigned int bit = bitmask & (- bitmask);
18990
18991 bitmask &= ~ bit;
18992 switch (bit)
18993 {
18994 case GNU_PROPERTY_X86_FEATURE_2_X86:
18995 printf ("x86");
18996 break;
18997 case GNU_PROPERTY_X86_FEATURE_2_X87:
18998 printf ("x87");
18999 break;
19000 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19001 printf ("MMX");
19002 break;
19003 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19004 printf ("XMM");
19005 break;
19006 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19007 printf ("YMM");
19008 break;
19009 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19010 printf ("ZMM");
19011 break;
19012 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19013 printf ("TMM");
19014 break;
19015 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19016 printf ("MASK");
19017 break;
19018 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19019 printf ("FXSR");
19020 break;
19021 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19022 printf ("XSAVE");
19023 break;
19024 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19025 printf ("XSAVEOPT");
19026 break;
19027 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19028 printf ("XSAVEC");
19029 break;
19030 default:
19031 printf (_("<unknown: %x>"), bit);
19032 break;
19033 }
19034 if (bitmask)
19035 printf (", ");
19036 }
19037 }
19038
19039 static void
19040 decode_aarch64_feature_1_and (unsigned int bitmask)
19041 {
19042 while (bitmask)
19043 {
19044 unsigned int bit = bitmask & (- bitmask);
19045
19046 bitmask &= ~ bit;
19047 switch (bit)
19048 {
19049 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19050 printf ("BTI");
19051 break;
19052
19053 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19054 printf ("PAC");
19055 break;
19056
19057 default:
19058 printf (_("<unknown: %x>"), bit);
19059 break;
19060 }
19061 if (bitmask)
19062 printf (", ");
19063 }
19064 }
19065
19066 static void
19067 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19068 {
19069 unsigned char * ptr = (unsigned char *) pnote->descdata;
19070 unsigned char * ptr_end = ptr + pnote->descsz;
19071 unsigned int size = is_32bit_elf ? 4 : 8;
19072
19073 printf (_(" Properties: "));
19074
19075 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19076 {
19077 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19078 return;
19079 }
19080
19081 while (ptr < ptr_end)
19082 {
19083 unsigned int j;
19084 unsigned int type;
19085 unsigned int datasz;
19086
19087 if ((size_t) (ptr_end - ptr) < 8)
19088 {
19089 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19090 break;
19091 }
19092
19093 type = byte_get (ptr, 4);
19094 datasz = byte_get (ptr + 4, 4);
19095
19096 ptr += 8;
19097
19098 if (datasz > (size_t) (ptr_end - ptr))
19099 {
19100 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19101 type, datasz);
19102 break;
19103 }
19104
19105 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19106 {
19107 if (filedata->file_header.e_machine == EM_X86_64
19108 || filedata->file_header.e_machine == EM_IAMCU
19109 || filedata->file_header.e_machine == EM_386)
19110 {
19111 unsigned int bitmask;
19112
19113 if (datasz == 4)
19114 bitmask = byte_get (ptr, 4);
19115 else
19116 bitmask = 0;
19117
19118 switch (type)
19119 {
19120 case GNU_PROPERTY_X86_ISA_1_USED:
19121 if (datasz != 4)
19122 printf (_("x86 ISA used: <corrupt length: %#x> "),
19123 datasz);
19124 else
19125 {
19126 printf ("x86 ISA used: ");
19127 decode_x86_isa (bitmask);
19128 }
19129 goto next;
19130
19131 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19132 if (datasz != 4)
19133 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19134 datasz);
19135 else
19136 {
19137 printf ("x86 ISA needed: ");
19138 decode_x86_isa (bitmask);
19139 }
19140 goto next;
19141
19142 case GNU_PROPERTY_X86_FEATURE_1_AND:
19143 if (datasz != 4)
19144 printf (_("x86 feature: <corrupt length: %#x> "),
19145 datasz);
19146 else
19147 {
19148 printf ("x86 feature: ");
19149 decode_x86_feature_1 (bitmask);
19150 }
19151 goto next;
19152
19153 case GNU_PROPERTY_X86_FEATURE_2_USED:
19154 if (datasz != 4)
19155 printf (_("x86 feature used: <corrupt length: %#x> "),
19156 datasz);
19157 else
19158 {
19159 printf ("x86 feature used: ");
19160 decode_x86_feature_2 (bitmask);
19161 }
19162 goto next;
19163
19164 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19165 if (datasz != 4)
19166 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19167 else
19168 {
19169 printf ("x86 feature needed: ");
19170 decode_x86_feature_2 (bitmask);
19171 }
19172 goto next;
19173
19174 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19175 if (datasz != 4)
19176 printf (_("x86 ISA used: <corrupt length: %#x> "),
19177 datasz);
19178 else
19179 {
19180 printf ("x86 ISA used: ");
19181 decode_x86_compat_isa (bitmask);
19182 }
19183 goto next;
19184
19185 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19186 if (datasz != 4)
19187 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19188 datasz);
19189 else
19190 {
19191 printf ("x86 ISA needed: ");
19192 decode_x86_compat_isa (bitmask);
19193 }
19194 goto next;
19195
19196 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19197 if (datasz != 4)
19198 printf (_("x86 ISA used: <corrupt length: %#x> "),
19199 datasz);
19200 else
19201 {
19202 printf ("x86 ISA used: ");
19203 decode_x86_compat_2_isa (bitmask);
19204 }
19205 goto next;
19206
19207 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19208 if (datasz != 4)
19209 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19210 datasz);
19211 else
19212 {
19213 printf ("x86 ISA needed: ");
19214 decode_x86_compat_2_isa (bitmask);
19215 }
19216 goto next;
19217
19218 default:
19219 break;
19220 }
19221 }
19222 else if (filedata->file_header.e_machine == EM_AARCH64)
19223 {
19224 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19225 {
19226 printf ("AArch64 feature: ");
19227 if (datasz != 4)
19228 printf (_("<corrupt length: %#x> "), datasz);
19229 else
19230 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19231 goto next;
19232 }
19233 }
19234 }
19235 else
19236 {
19237 switch (type)
19238 {
19239 case GNU_PROPERTY_STACK_SIZE:
19240 printf (_("stack size: "));
19241 if (datasz != size)
19242 printf (_("<corrupt length: %#x> "), datasz);
19243 else
19244 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19245 goto next;
19246
19247 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19248 printf ("no copy on protected ");
19249 if (datasz)
19250 printf (_("<corrupt length: %#x> "), datasz);
19251 goto next;
19252
19253 default:
19254 break;
19255 }
19256 }
19257
19258 if (type < GNU_PROPERTY_LOPROC)
19259 printf (_("<unknown type %#x data: "), type);
19260 else if (type < GNU_PROPERTY_LOUSER)
19261 printf (_("<processor-specific type %#x data: "), type);
19262 else
19263 printf (_("<application-specific type %#x data: "), type);
19264 for (j = 0; j < datasz; ++j)
19265 printf ("%02x ", ptr[j] & 0xff);
19266 printf (">");
19267
19268 next:
19269 ptr += ((datasz + (size - 1)) & ~ (size - 1));
19270 if (ptr == ptr_end)
19271 break;
19272
19273 if (do_wide)
19274 printf (", ");
19275 else
19276 printf ("\n\t");
19277 }
19278
19279 printf ("\n");
19280 }
19281
19282 static bfd_boolean
19283 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
19284 {
19285 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
19286 switch (pnote->type)
19287 {
19288 case NT_GNU_BUILD_ID:
19289 {
19290 unsigned long i;
19291
19292 printf (_(" Build ID: "));
19293 for (i = 0; i < pnote->descsz; ++i)
19294 printf ("%02x", pnote->descdata[i] & 0xff);
19295 printf ("\n");
19296 }
19297 break;
19298
19299 case NT_GNU_ABI_TAG:
19300 {
19301 unsigned long os, major, minor, subminor;
19302 const char *osname;
19303
19304 /* PR 17531: file: 030-599401-0.004. */
19305 if (pnote->descsz < 16)
19306 {
19307 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19308 break;
19309 }
19310
19311 os = byte_get ((unsigned char *) pnote->descdata, 4);
19312 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19313 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
19314 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
19315
19316 switch (os)
19317 {
19318 case GNU_ABI_TAG_LINUX:
19319 osname = "Linux";
19320 break;
19321 case GNU_ABI_TAG_HURD:
19322 osname = "Hurd";
19323 break;
19324 case GNU_ABI_TAG_SOLARIS:
19325 osname = "Solaris";
19326 break;
19327 case GNU_ABI_TAG_FREEBSD:
19328 osname = "FreeBSD";
19329 break;
19330 case GNU_ABI_TAG_NETBSD:
19331 osname = "NetBSD";
19332 break;
19333 case GNU_ABI_TAG_SYLLABLE:
19334 osname = "Syllable";
19335 break;
19336 case GNU_ABI_TAG_NACL:
19337 osname = "NaCl";
19338 break;
19339 default:
19340 osname = "Unknown";
19341 break;
19342 }
19343
19344 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
19345 major, minor, subminor);
19346 }
19347 break;
19348
19349 case NT_GNU_GOLD_VERSION:
19350 {
19351 unsigned long i;
19352
19353 printf (_(" Version: "));
19354 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
19355 printf ("%c", pnote->descdata[i]);
19356 printf ("\n");
19357 }
19358 break;
19359
19360 case NT_GNU_HWCAP:
19361 {
19362 unsigned long num_entries, mask;
19363
19364 /* Hardware capabilities information. Word 0 is the number of entries.
19365 Word 1 is a bitmask of enabled entries. The rest of the descriptor
19366 is a series of entries, where each entry is a single byte followed
19367 by a nul terminated string. The byte gives the bit number to test
19368 if enabled in the bitmask. */
19369 printf (_(" Hardware Capabilities: "));
19370 if (pnote->descsz < 8)
19371 {
19372 error (_("<corrupt GNU_HWCAP>\n"));
19373 return FALSE;
19374 }
19375 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
19376 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19377 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
19378 /* FIXME: Add code to display the entries... */
19379 }
19380 break;
19381
19382 case NT_GNU_PROPERTY_TYPE_0:
19383 print_gnu_property_note (filedata, pnote);
19384 break;
19385
19386 default:
19387 /* Handle unrecognised types. An error message should have already been
19388 created by get_gnu_elf_note_type(), so all that we need to do is to
19389 display the data. */
19390 {
19391 unsigned long i;
19392
19393 printf (_(" Description data: "));
19394 for (i = 0; i < pnote->descsz; ++i)
19395 printf ("%02x ", pnote->descdata[i] & 0xff);
19396 printf ("\n");
19397 }
19398 break;
19399 }
19400
19401 return TRUE;
19402 }
19403
19404 static const char *
19405 get_v850_elf_note_type (enum v850_notes n_type)
19406 {
19407 static char buff[64];
19408
19409 switch (n_type)
19410 {
19411 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
19412 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
19413 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
19414 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
19415 case V850_NOTE_CACHE_INFO: return _("Use of cache");
19416 case V850_NOTE_MMU_INFO: return _("Use of MMU");
19417 default:
19418 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
19419 return buff;
19420 }
19421 }
19422
19423 static bfd_boolean
19424 print_v850_note (Elf_Internal_Note * pnote)
19425 {
19426 unsigned int val;
19427
19428 if (pnote->descsz != 4)
19429 return FALSE;
19430
19431 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
19432
19433 if (val == 0)
19434 {
19435 printf (_("not set\n"));
19436 return TRUE;
19437 }
19438
19439 switch (pnote->type)
19440 {
19441 case V850_NOTE_ALIGNMENT:
19442 switch (val)
19443 {
19444 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return TRUE;
19445 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return TRUE;
19446 }
19447 break;
19448
19449 case V850_NOTE_DATA_SIZE:
19450 switch (val)
19451 {
19452 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return TRUE;
19453 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return TRUE;
19454 }
19455 break;
19456
19457 case V850_NOTE_FPU_INFO:
19458 switch (val)
19459 {
19460 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return TRUE;
19461 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return TRUE;
19462 }
19463 break;
19464
19465 case V850_NOTE_MMU_INFO:
19466 case V850_NOTE_CACHE_INFO:
19467 case V850_NOTE_SIMD_INFO:
19468 if (val == EF_RH850_SIMD)
19469 {
19470 printf (_("yes\n"));
19471 return TRUE;
19472 }
19473 break;
19474
19475 default:
19476 /* An 'unknown note type' message will already have been displayed. */
19477 break;
19478 }
19479
19480 printf (_("unknown value: %x\n"), val);
19481 return FALSE;
19482 }
19483
19484 static bfd_boolean
19485 process_netbsd_elf_note (Elf_Internal_Note * pnote)
19486 {
19487 unsigned int version;
19488
19489 switch (pnote->type)
19490 {
19491 case NT_NETBSD_IDENT:
19492 if (pnote->descsz < 1)
19493 break;
19494 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19495 if ((version / 10000) % 100)
19496 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
19497 version, version / 100000000, (version / 1000000) % 100,
19498 (version / 10000) % 100 > 26 ? "Z" : "",
19499 'A' + (version / 10000) % 26);
19500 else
19501 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
19502 version, version / 100000000, (version / 1000000) % 100,
19503 (version / 100) % 100);
19504 return TRUE;
19505
19506 case NT_NETBSD_MARCH:
19507 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
19508 pnote->descdata);
19509 return TRUE;
19510
19511 #ifdef NT_NETBSD_PAX
19512 case NT_NETBSD_PAX:
19513 if (pnote->descsz < 1)
19514 break;
19515 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19516 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
19517 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
19518 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
19519 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
19520 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
19521 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
19522 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
19523 return TRUE;
19524 #endif
19525 }
19526
19527 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
19528 pnote->descsz, pnote->type);
19529 return FALSE;
19530 }
19531
19532 static const char *
19533 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19534 {
19535 switch (e_type)
19536 {
19537 case NT_FREEBSD_THRMISC:
19538 return _("NT_THRMISC (thrmisc structure)");
19539 case NT_FREEBSD_PROCSTAT_PROC:
19540 return _("NT_PROCSTAT_PROC (proc data)");
19541 case NT_FREEBSD_PROCSTAT_FILES:
19542 return _("NT_PROCSTAT_FILES (files data)");
19543 case NT_FREEBSD_PROCSTAT_VMMAP:
19544 return _("NT_PROCSTAT_VMMAP (vmmap data)");
19545 case NT_FREEBSD_PROCSTAT_GROUPS:
19546 return _("NT_PROCSTAT_GROUPS (groups data)");
19547 case NT_FREEBSD_PROCSTAT_UMASK:
19548 return _("NT_PROCSTAT_UMASK (umask data)");
19549 case NT_FREEBSD_PROCSTAT_RLIMIT:
19550 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
19551 case NT_FREEBSD_PROCSTAT_OSREL:
19552 return _("NT_PROCSTAT_OSREL (osreldate data)");
19553 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
19554 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
19555 case NT_FREEBSD_PROCSTAT_AUXV:
19556 return _("NT_PROCSTAT_AUXV (auxv data)");
19557 case NT_FREEBSD_PTLWPINFO:
19558 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
19559 }
19560 return get_note_type (filedata, e_type);
19561 }
19562
19563 static const char *
19564 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19565 {
19566 static char buff[64];
19567
19568 switch (e_type)
19569 {
19570 case NT_NETBSDCORE_PROCINFO:
19571 /* NetBSD core "procinfo" structure. */
19572 return _("NetBSD procinfo structure");
19573
19574 #ifdef NT_NETBSDCORE_AUXV
19575 case NT_NETBSDCORE_AUXV:
19576 return _("NetBSD ELF auxiliary vector data");
19577 #endif
19578
19579 #ifdef NT_NETBSDCORE_LWPSTATUS
19580 case NT_NETBSDCORE_LWPSTATUS:
19581 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
19582 #endif
19583
19584 default:
19585 /* As of Jan 2020 there are no other machine-independent notes
19586 defined for NetBSD core files. If the note type is less
19587 than the start of the machine-dependent note types, we don't
19588 understand it. */
19589
19590 if (e_type < NT_NETBSDCORE_FIRSTMACH)
19591 {
19592 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19593 return buff;
19594 }
19595 break;
19596 }
19597
19598 switch (filedata->file_header.e_machine)
19599 {
19600 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
19601 and PT_GETFPREGS == mach+2. */
19602
19603 case EM_OLD_ALPHA:
19604 case EM_ALPHA:
19605 case EM_SPARC:
19606 case EM_SPARC32PLUS:
19607 case EM_SPARCV9:
19608 switch (e_type)
19609 {
19610 case NT_NETBSDCORE_FIRSTMACH + 0:
19611 return _("PT_GETREGS (reg structure)");
19612 case NT_NETBSDCORE_FIRSTMACH + 2:
19613 return _("PT_GETFPREGS (fpreg structure)");
19614 default:
19615 break;
19616 }
19617 break;
19618
19619 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
19620 There's also old PT___GETREGS40 == mach + 1 for old reg
19621 structure which lacks GBR. */
19622 case EM_SH:
19623 switch (e_type)
19624 {
19625 case NT_NETBSDCORE_FIRSTMACH + 1:
19626 return _("PT___GETREGS40 (old reg structure)");
19627 case NT_NETBSDCORE_FIRSTMACH + 3:
19628 return _("PT_GETREGS (reg structure)");
19629 case NT_NETBSDCORE_FIRSTMACH + 5:
19630 return _("PT_GETFPREGS (fpreg structure)");
19631 default:
19632 break;
19633 }
19634 break;
19635
19636 /* On all other arch's, PT_GETREGS == mach+1 and
19637 PT_GETFPREGS == mach+3. */
19638 default:
19639 switch (e_type)
19640 {
19641 case NT_NETBSDCORE_FIRSTMACH + 1:
19642 return _("PT_GETREGS (reg structure)");
19643 case NT_NETBSDCORE_FIRSTMACH + 3:
19644 return _("PT_GETFPREGS (fpreg structure)");
19645 default:
19646 break;
19647 }
19648 }
19649
19650 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
19651 e_type - NT_NETBSDCORE_FIRSTMACH);
19652 return buff;
19653 }
19654
19655 static const char *
19656 get_stapsdt_note_type (unsigned e_type)
19657 {
19658 static char buff[64];
19659
19660 switch (e_type)
19661 {
19662 case NT_STAPSDT:
19663 return _("NT_STAPSDT (SystemTap probe descriptors)");
19664
19665 default:
19666 break;
19667 }
19668
19669 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19670 return buff;
19671 }
19672
19673 static bfd_boolean
19674 print_stapsdt_note (Elf_Internal_Note *pnote)
19675 {
19676 size_t len, maxlen;
19677 unsigned long addr_size = is_32bit_elf ? 4 : 8;
19678 char *data = pnote->descdata;
19679 char *data_end = pnote->descdata + pnote->descsz;
19680 bfd_vma pc, base_addr, semaphore;
19681 char *provider, *probe, *arg_fmt;
19682
19683 if (pnote->descsz < (addr_size * 3))
19684 goto stapdt_note_too_small;
19685
19686 pc = byte_get ((unsigned char *) data, addr_size);
19687 data += addr_size;
19688
19689 base_addr = byte_get ((unsigned char *) data, addr_size);
19690 data += addr_size;
19691
19692 semaphore = byte_get ((unsigned char *) data, addr_size);
19693 data += addr_size;
19694
19695 if (data >= data_end)
19696 goto stapdt_note_too_small;
19697 maxlen = data_end - data;
19698 len = strnlen (data, maxlen);
19699 if (len < maxlen)
19700 {
19701 provider = data;
19702 data += len + 1;
19703 }
19704 else
19705 goto stapdt_note_too_small;
19706
19707 if (data >= data_end)
19708 goto stapdt_note_too_small;
19709 maxlen = data_end - data;
19710 len = strnlen (data, maxlen);
19711 if (len < maxlen)
19712 {
19713 probe = data;
19714 data += len + 1;
19715 }
19716 else
19717 goto stapdt_note_too_small;
19718
19719 if (data >= data_end)
19720 goto stapdt_note_too_small;
19721 maxlen = data_end - data;
19722 len = strnlen (data, maxlen);
19723 if (len < maxlen)
19724 {
19725 arg_fmt = data;
19726 data += len + 1;
19727 }
19728 else
19729 goto stapdt_note_too_small;
19730
19731 printf (_(" Provider: %s\n"), provider);
19732 printf (_(" Name: %s\n"), probe);
19733 printf (_(" Location: "));
19734 print_vma (pc, FULL_HEX);
19735 printf (_(", Base: "));
19736 print_vma (base_addr, FULL_HEX);
19737 printf (_(", Semaphore: "));
19738 print_vma (semaphore, FULL_HEX);
19739 printf ("\n");
19740 printf (_(" Arguments: %s\n"), arg_fmt);
19741
19742 return data == data_end;
19743
19744 stapdt_note_too_small:
19745 printf (_(" <corrupt - note is too small>\n"));
19746 error (_("corrupt stapdt note - the data size is too small\n"));
19747 return FALSE;
19748 }
19749
19750 static const char *
19751 get_ia64_vms_note_type (unsigned e_type)
19752 {
19753 static char buff[64];
19754
19755 switch (e_type)
19756 {
19757 case NT_VMS_MHD:
19758 return _("NT_VMS_MHD (module header)");
19759 case NT_VMS_LNM:
19760 return _("NT_VMS_LNM (language name)");
19761 case NT_VMS_SRC:
19762 return _("NT_VMS_SRC (source files)");
19763 case NT_VMS_TITLE:
19764 return "NT_VMS_TITLE";
19765 case NT_VMS_EIDC:
19766 return _("NT_VMS_EIDC (consistency check)");
19767 case NT_VMS_FPMODE:
19768 return _("NT_VMS_FPMODE (FP mode)");
19769 case NT_VMS_LINKTIME:
19770 return "NT_VMS_LINKTIME";
19771 case NT_VMS_IMGNAM:
19772 return _("NT_VMS_IMGNAM (image name)");
19773 case NT_VMS_IMGID:
19774 return _("NT_VMS_IMGID (image id)");
19775 case NT_VMS_LINKID:
19776 return _("NT_VMS_LINKID (link id)");
19777 case NT_VMS_IMGBID:
19778 return _("NT_VMS_IMGBID (build id)");
19779 case NT_VMS_GSTNAM:
19780 return _("NT_VMS_GSTNAM (sym table name)");
19781 case NT_VMS_ORIG_DYN:
19782 return "NT_VMS_ORIG_DYN";
19783 case NT_VMS_PATCHTIME:
19784 return "NT_VMS_PATCHTIME";
19785 default:
19786 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19787 return buff;
19788 }
19789 }
19790
19791 static bfd_boolean
19792 print_ia64_vms_note (Elf_Internal_Note * pnote)
19793 {
19794 int maxlen = pnote->descsz;
19795
19796 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
19797 goto desc_size_fail;
19798
19799 switch (pnote->type)
19800 {
19801 case NT_VMS_MHD:
19802 if (maxlen <= 36)
19803 goto desc_size_fail;
19804
19805 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
19806
19807 printf (_(" Creation date : %.17s\n"), pnote->descdata);
19808 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
19809 if (l + 34 < maxlen)
19810 {
19811 printf (_(" Module name : %s\n"), pnote->descdata + 34);
19812 if (l + 35 < maxlen)
19813 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
19814 else
19815 printf (_(" Module version : <missing>\n"));
19816 }
19817 else
19818 {
19819 printf (_(" Module name : <missing>\n"));
19820 printf (_(" Module version : <missing>\n"));
19821 }
19822 break;
19823
19824 case NT_VMS_LNM:
19825 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
19826 break;
19827
19828 #ifdef BFD64
19829 case NT_VMS_FPMODE:
19830 printf (_(" Floating Point mode: "));
19831 if (maxlen < 8)
19832 goto desc_size_fail;
19833 /* FIXME: Generate an error if descsz > 8 ? */
19834
19835 printf ("0x%016" BFD_VMA_FMT "x\n",
19836 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
19837 break;
19838
19839 case NT_VMS_LINKTIME:
19840 printf (_(" Link time: "));
19841 if (maxlen < 8)
19842 goto desc_size_fail;
19843 /* FIXME: Generate an error if descsz > 8 ? */
19844
19845 print_vms_time
19846 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
19847 printf ("\n");
19848 break;
19849
19850 case NT_VMS_PATCHTIME:
19851 printf (_(" Patch time: "));
19852 if (maxlen < 8)
19853 goto desc_size_fail;
19854 /* FIXME: Generate an error if descsz > 8 ? */
19855
19856 print_vms_time
19857 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
19858 printf ("\n");
19859 break;
19860
19861 case NT_VMS_ORIG_DYN:
19862 if (maxlen < 34)
19863 goto desc_size_fail;
19864
19865 printf (_(" Major id: %u, minor id: %u\n"),
19866 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
19867 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
19868 printf (_(" Last modified : "));
19869 print_vms_time
19870 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
19871 printf (_("\n Link flags : "));
19872 printf ("0x%016" BFD_VMA_FMT "x\n",
19873 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
19874 printf (_(" Header flags: 0x%08x\n"),
19875 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
19876 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
19877 break;
19878 #endif
19879
19880 case NT_VMS_IMGNAM:
19881 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
19882 break;
19883
19884 case NT_VMS_GSTNAM:
19885 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
19886 break;
19887
19888 case NT_VMS_IMGID:
19889 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
19890 break;
19891
19892 case NT_VMS_LINKID:
19893 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
19894 break;
19895
19896 default:
19897 return FALSE;
19898 }
19899
19900 return TRUE;
19901
19902 desc_size_fail:
19903 printf (_(" <corrupt - data size is too small>\n"));
19904 error (_("corrupt IA64 note: data size is too small\n"));
19905 return FALSE;
19906 }
19907
19908 struct build_attr_cache {
19909 Filedata *filedata;
19910 char *strtab;
19911 unsigned long strtablen;
19912 Elf_Internal_Sym *symtab;
19913 unsigned long nsyms;
19914 } ba_cache;
19915
19916 /* Find the symbol associated with a build attribute that is attached
19917 to address OFFSET. If PNAME is non-NULL then store the name of
19918 the symbol (if found) in the provided pointer, Returns NULL if a
19919 symbol could not be found. */
19920
19921 static Elf_Internal_Sym *
19922 get_symbol_for_build_attribute (Filedata * filedata,
19923 unsigned long offset,
19924 bfd_boolean is_open_attr,
19925 const char ** pname)
19926 {
19927 Elf_Internal_Sym *saved_sym = NULL;
19928 Elf_Internal_Sym *sym;
19929
19930 if (filedata->section_headers != NULL
19931 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
19932 {
19933 Elf_Internal_Shdr * symsec;
19934
19935 free (ba_cache.strtab);
19936 ba_cache.strtab = NULL;
19937 free (ba_cache.symtab);
19938 ba_cache.symtab = NULL;
19939
19940 /* Load the symbol and string sections. */
19941 for (symsec = filedata->section_headers;
19942 symsec < filedata->section_headers + filedata->file_header.e_shnum;
19943 symsec ++)
19944 {
19945 if (symsec->sh_type == SHT_SYMTAB
19946 && get_symtab (filedata, symsec,
19947 &ba_cache.symtab, &ba_cache.nsyms,
19948 &ba_cache.strtab, &ba_cache.strtablen))
19949 break;
19950 }
19951 ba_cache.filedata = filedata;
19952 }
19953
19954 if (ba_cache.symtab == NULL)
19955 return NULL;
19956
19957 /* Find a symbol whose value matches offset. */
19958 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
19959 if (sym->st_value == offset)
19960 {
19961 if (sym->st_name >= ba_cache.strtablen)
19962 /* Huh ? This should not happen. */
19963 continue;
19964
19965 if (ba_cache.strtab[sym->st_name] == 0)
19966 continue;
19967
19968 /* The AArch64 and ARM architectures define mapping symbols
19969 (eg $d, $x, $t) which we want to ignore. */
19970 if (ba_cache.strtab[sym->st_name] == '$'
19971 && ba_cache.strtab[sym->st_name + 1] != 0
19972 && ba_cache.strtab[sym->st_name + 2] == 0)
19973 continue;
19974
19975 if (is_open_attr)
19976 {
19977 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
19978 and FILE or OBJECT symbols over NOTYPE symbols. We skip
19979 FUNC symbols entirely. */
19980 switch (ELF_ST_TYPE (sym->st_info))
19981 {
19982 case STT_OBJECT:
19983 case STT_FILE:
19984 saved_sym = sym;
19985 if (sym->st_size)
19986 {
19987 /* If the symbol has a size associated
19988 with it then we can stop searching. */
19989 sym = ba_cache.symtab + ba_cache.nsyms;
19990 }
19991 continue;
19992
19993 case STT_FUNC:
19994 /* Ignore function symbols. */
19995 continue;
19996
19997 default:
19998 break;
19999 }
20000
20001 switch (ELF_ST_BIND (sym->st_info))
20002 {
20003 case STB_GLOBAL:
20004 if (saved_sym == NULL
20005 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20006 saved_sym = sym;
20007 break;
20008
20009 case STB_LOCAL:
20010 if (saved_sym == NULL)
20011 saved_sym = sym;
20012 break;
20013
20014 default:
20015 break;
20016 }
20017 }
20018 else
20019 {
20020 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20021 continue;
20022
20023 saved_sym = sym;
20024 break;
20025 }
20026 }
20027
20028 if (saved_sym && pname)
20029 * pname = ba_cache.strtab + saved_sym->st_name;
20030
20031 return saved_sym;
20032 }
20033
20034 /* Returns true iff addr1 and addr2 are in the same section. */
20035
20036 static bfd_boolean
20037 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20038 {
20039 Elf_Internal_Shdr * a1;
20040 Elf_Internal_Shdr * a2;
20041
20042 a1 = find_section_by_address (filedata, addr1);
20043 a2 = find_section_by_address (filedata, addr2);
20044
20045 return a1 == a2 && a1 != NULL;
20046 }
20047
20048 static bfd_boolean
20049 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20050 Filedata * filedata)
20051 {
20052 static unsigned long global_offset = 0;
20053 static unsigned long global_end = 0;
20054 static unsigned long func_offset = 0;
20055 static unsigned long func_end = 0;
20056
20057 Elf_Internal_Sym * sym;
20058 const char * name;
20059 unsigned long start;
20060 unsigned long end;
20061 bfd_boolean is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20062
20063 switch (pnote->descsz)
20064 {
20065 case 0:
20066 /* A zero-length description means that the range of
20067 the previous note of the same type should be used. */
20068 if (is_open_attr)
20069 {
20070 if (global_end > global_offset)
20071 printf (_(" Applies to region from %#lx to %#lx\n"),
20072 global_offset, global_end);
20073 else
20074 printf (_(" Applies to region from %#lx\n"), global_offset);
20075 }
20076 else
20077 {
20078 if (func_end > func_offset)
20079 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20080 else
20081 printf (_(" Applies to region from %#lx\n"), func_offset);
20082 }
20083 return TRUE;
20084
20085 case 4:
20086 start = byte_get ((unsigned char *) pnote->descdata, 4);
20087 end = 0;
20088 break;
20089
20090 case 8:
20091 start = byte_get ((unsigned char *) pnote->descdata, 4);
20092 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20093 break;
20094
20095 case 16:
20096 start = byte_get ((unsigned char *) pnote->descdata, 8);
20097 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20098 break;
20099
20100 default:
20101 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20102 printf (_(" <invalid descsz>"));
20103 return FALSE;
20104 }
20105
20106 name = NULL;
20107 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
20108 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20109 in order to avoid them being confused with the start address of the
20110 first function in the file... */
20111 if (sym == NULL && is_open_attr)
20112 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20113 & name);
20114
20115 if (end == 0 && sym != NULL && sym->st_size > 0)
20116 end = start + sym->st_size;
20117
20118 if (is_open_attr)
20119 {
20120 /* FIXME: Need to properly allow for section alignment.
20121 16 is just the alignment used on x86_64. */
20122 if (global_end > 0
20123 && start > BFD_ALIGN (global_end, 16)
20124 /* Build notes are not guaranteed to be organised in order of
20125 increasing address, but we should find the all of the notes
20126 for one section in the same place. */
20127 && same_section (filedata, start, global_end))
20128 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20129 global_end + 1, start - 1);
20130
20131 printf (_(" Applies to region from %#lx"), start);
20132 global_offset = start;
20133
20134 if (end)
20135 {
20136 printf (_(" to %#lx"), end);
20137 global_end = end;
20138 }
20139 }
20140 else
20141 {
20142 printf (_(" Applies to region from %#lx"), start);
20143 func_offset = start;
20144
20145 if (end)
20146 {
20147 printf (_(" to %#lx"), end);
20148 func_end = end;
20149 }
20150 }
20151
20152 if (sym && name)
20153 printf (_(" (%s)"), name);
20154
20155 printf ("\n");
20156 return TRUE;
20157 }
20158
20159 static bfd_boolean
20160 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20161 {
20162 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20163 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20164 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
20165 char name_type;
20166 char name_attribute;
20167 const char * expected_types;
20168 const char * name = pnote->namedata;
20169 const char * text;
20170 signed int left;
20171
20172 if (name == NULL || pnote->namesz < 2)
20173 {
20174 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20175 print_symbol (-20, _(" <corrupt name>"));
20176 return FALSE;
20177 }
20178
20179 if (do_wide)
20180 left = 28;
20181 else
20182 left = 20;
20183
20184 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20185 if (name[0] == 'G' && name[1] == 'A')
20186 {
20187 if (pnote->namesz < 4)
20188 {
20189 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20190 print_symbol (-20, _(" <corrupt name>"));
20191 return FALSE;
20192 }
20193
20194 printf ("GA");
20195 name += 2;
20196 left -= 2;
20197 }
20198
20199 switch ((name_type = * name))
20200 {
20201 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20202 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20203 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20204 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20205 printf ("%c", * name);
20206 left --;
20207 break;
20208 default:
20209 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20210 print_symbol (-20, _("<unknown name type>"));
20211 return FALSE;
20212 }
20213
20214 ++ name;
20215 text = NULL;
20216
20217 switch ((name_attribute = * name))
20218 {
20219 case GNU_BUILD_ATTRIBUTE_VERSION:
20220 text = _("<version>");
20221 expected_types = string_expected;
20222 ++ name;
20223 break;
20224 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20225 text = _("<stack prot>");
20226 expected_types = "!+*";
20227 ++ name;
20228 break;
20229 case GNU_BUILD_ATTRIBUTE_RELRO:
20230 text = _("<relro>");
20231 expected_types = bool_expected;
20232 ++ name;
20233 break;
20234 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20235 text = _("<stack size>");
20236 expected_types = number_expected;
20237 ++ name;
20238 break;
20239 case GNU_BUILD_ATTRIBUTE_TOOL:
20240 text = _("<tool>");
20241 expected_types = string_expected;
20242 ++ name;
20243 break;
20244 case GNU_BUILD_ATTRIBUTE_ABI:
20245 text = _("<ABI>");
20246 expected_types = "$*";
20247 ++ name;
20248 break;
20249 case GNU_BUILD_ATTRIBUTE_PIC:
20250 text = _("<PIC>");
20251 expected_types = number_expected;
20252 ++ name;
20253 break;
20254 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20255 text = _("<short enum>");
20256 expected_types = bool_expected;
20257 ++ name;
20258 break;
20259 default:
20260 if (ISPRINT (* name))
20261 {
20262 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20263
20264 if (len > left && ! do_wide)
20265 len = left;
20266 printf ("%.*s:", len, name);
20267 left -= len;
20268 name += len;
20269 }
20270 else
20271 {
20272 static char tmpbuf [128];
20273
20274 error (_("unrecognised byte in name field: %d\n"), * name);
20275 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20276 text = tmpbuf;
20277 name ++;
20278 }
20279 expected_types = "*$!+";
20280 break;
20281 }
20282
20283 if (text)
20284 left -= printf ("%s", text);
20285
20286 if (strchr (expected_types, name_type) == NULL)
20287 warn (_("attribute does not have an expected type (%c)\n"), name_type);
20288
20289 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
20290 {
20291 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
20292 (unsigned long) pnote->namesz,
20293 (long) (name - pnote->namedata));
20294 return FALSE;
20295 }
20296
20297 if (left < 1 && ! do_wide)
20298 return TRUE;
20299
20300 switch (name_type)
20301 {
20302 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20303 {
20304 unsigned int bytes;
20305 unsigned long long val = 0;
20306 unsigned int shift = 0;
20307 char * decoded = NULL;
20308
20309 bytes = pnote->namesz - (name - pnote->namedata);
20310 if (bytes > 0)
20311 /* The -1 is because the name field is always 0 terminated, and we
20312 want to be able to ensure that the shift in the while loop below
20313 will not overflow. */
20314 -- bytes;
20315
20316 if (bytes > sizeof (val))
20317 {
20318 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
20319 bytes);
20320 bytes = sizeof (val);
20321 }
20322 /* We do not bother to warn if bytes == 0 as this can
20323 happen with some early versions of the gcc plugin. */
20324
20325 while (bytes --)
20326 {
20327 unsigned long long byte = *name++ & 0xff;
20328
20329 val |= byte << shift;
20330 shift += 8;
20331 }
20332
20333 switch (name_attribute)
20334 {
20335 case GNU_BUILD_ATTRIBUTE_PIC:
20336 switch (val)
20337 {
20338 case 0: decoded = "static"; break;
20339 case 1: decoded = "pic"; break;
20340 case 2: decoded = "PIC"; break;
20341 case 3: decoded = "pie"; break;
20342 case 4: decoded = "PIE"; break;
20343 default: break;
20344 }
20345 break;
20346 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20347 switch (val)
20348 {
20349 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
20350 case 0: decoded = "off"; break;
20351 case 1: decoded = "on"; break;
20352 case 2: decoded = "all"; break;
20353 case 3: decoded = "strong"; break;
20354 case 4: decoded = "explicit"; break;
20355 default: break;
20356 }
20357 break;
20358 default:
20359 break;
20360 }
20361
20362 if (decoded != NULL)
20363 {
20364 print_symbol (-left, decoded);
20365 left = 0;
20366 }
20367 else if (val == 0)
20368 {
20369 printf ("0x0");
20370 left -= 3;
20371 }
20372 else
20373 {
20374 if (do_wide)
20375 left -= printf ("0x%llx", val);
20376 else
20377 left -= printf ("0x%-.*llx", left, val);
20378 }
20379 }
20380 break;
20381 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20382 left -= print_symbol (- left, name);
20383 break;
20384 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20385 left -= print_symbol (- left, "true");
20386 break;
20387 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20388 left -= print_symbol (- left, "false");
20389 break;
20390 }
20391
20392 if (do_wide && left > 0)
20393 printf ("%-*s", left, " ");
20394
20395 return TRUE;
20396 }
20397
20398 /* Note that by the ELF standard, the name field is already null byte
20399 terminated, and namesz includes the terminating null byte.
20400 I.E. the value of namesz for the name "FSF" is 4.
20401
20402 If the value of namesz is zero, there is no name present. */
20403
20404 static bfd_boolean
20405 process_note (Elf_Internal_Note * pnote,
20406 Filedata * filedata)
20407 {
20408 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
20409 const char * nt;
20410
20411 if (pnote->namesz == 0)
20412 /* If there is no note name, then use the default set of
20413 note type strings. */
20414 nt = get_note_type (filedata, pnote->type);
20415
20416 else if (const_strneq (pnote->namedata, "GNU"))
20417 /* GNU-specific object file notes. */
20418 nt = get_gnu_elf_note_type (pnote->type);
20419
20420 else if (const_strneq (pnote->namedata, "FreeBSD"))
20421 /* FreeBSD-specific core file notes. */
20422 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
20423
20424 else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
20425 /* NetBSD-specific core file notes. */
20426 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
20427
20428 else if (const_strneq (pnote->namedata, "NetBSD"))
20429 /* NetBSD-specific core file notes. */
20430 return process_netbsd_elf_note (pnote);
20431
20432 else if (const_strneq (pnote->namedata, "PaX"))
20433 /* NetBSD-specific core file notes. */
20434 return process_netbsd_elf_note (pnote);
20435
20436 else if (strneq (pnote->namedata, "SPU/", 4))
20437 {
20438 /* SPU-specific core file notes. */
20439 nt = pnote->namedata + 4;
20440 name = "SPU";
20441 }
20442
20443 else if (const_strneq (pnote->namedata, "IPF/VMS"))
20444 /* VMS/ia64-specific file notes. */
20445 nt = get_ia64_vms_note_type (pnote->type);
20446
20447 else if (const_strneq (pnote->namedata, "stapsdt"))
20448 nt = get_stapsdt_note_type (pnote->type);
20449
20450 else
20451 /* Don't recognize this note name; just use the default set of
20452 note type strings. */
20453 nt = get_note_type (filedata, pnote->type);
20454
20455 printf (" ");
20456
20457 if (((const_strneq (pnote->namedata, "GA")
20458 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20459 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20460 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20461 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20462 print_gnu_build_attribute_name (pnote);
20463 else
20464 print_symbol (-20, name);
20465
20466 if (do_wide)
20467 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
20468 else
20469 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
20470
20471 if (const_strneq (pnote->namedata, "IPF/VMS"))
20472 return print_ia64_vms_note (pnote);
20473 else if (const_strneq (pnote->namedata, "GNU"))
20474 return print_gnu_note (filedata, pnote);
20475 else if (const_strneq (pnote->namedata, "stapsdt"))
20476 return print_stapsdt_note (pnote);
20477 else if (const_strneq (pnote->namedata, "CORE"))
20478 return print_core_note (pnote);
20479 else if (((const_strneq (pnote->namedata, "GA")
20480 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20481 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20482 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20483 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20484 return print_gnu_build_attribute_description (pnote, filedata);
20485
20486 if (pnote->descsz)
20487 {
20488 unsigned long i;
20489
20490 printf (_(" description data: "));
20491 for (i = 0; i < pnote->descsz; i++)
20492 printf ("%02x ", pnote->descdata[i] & 0xff);
20493 if (!do_wide)
20494 printf ("\n");
20495 }
20496
20497 if (do_wide)
20498 printf ("\n");
20499
20500 return TRUE;
20501 }
20502
20503 static bfd_boolean
20504 process_notes_at (Filedata * filedata,
20505 Elf_Internal_Shdr * section,
20506 bfd_vma offset,
20507 bfd_vma length,
20508 bfd_vma align)
20509 {
20510 Elf_External_Note * pnotes;
20511 Elf_External_Note * external;
20512 char * end;
20513 bfd_boolean res = TRUE;
20514
20515 if (length <= 0)
20516 return FALSE;
20517
20518 if (section)
20519 {
20520 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
20521 if (pnotes)
20522 {
20523 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
20524 {
20525 free (pnotes);
20526 return FALSE;
20527 }
20528 }
20529 }
20530 else
20531 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20532 _("notes"));
20533
20534 if (pnotes == NULL)
20535 return FALSE;
20536
20537 external = pnotes;
20538
20539 if (filedata->is_separate)
20540 printf (_("In linked file '%s': "), filedata->file_name);
20541 else
20542 printf ("\n");
20543 if (section)
20544 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
20545 else
20546 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
20547 (unsigned long) offset, (unsigned long) length);
20548
20549 /* NB: Some note sections may have alignment value of 0 or 1. gABI
20550 specifies that notes should be aligned to 4 bytes in 32-bit
20551 objects and to 8 bytes in 64-bit objects. As a Linux extension,
20552 we also support 4 byte alignment in 64-bit objects. If section
20553 alignment is less than 4, we treate alignment as 4 bytes. */
20554 if (align < 4)
20555 align = 4;
20556 else if (align != 4 && align != 8)
20557 {
20558 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
20559 (long) align);
20560 free (pnotes);
20561 return FALSE;
20562 }
20563
20564 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
20565
20566 end = (char *) pnotes + length;
20567 while ((char *) external < end)
20568 {
20569 Elf_Internal_Note inote;
20570 size_t min_notesz;
20571 char * next;
20572 char * temp = NULL;
20573 size_t data_remaining = end - (char *) external;
20574
20575 if (!is_ia64_vms (filedata))
20576 {
20577 /* PR binutils/15191
20578 Make sure that there is enough data to read. */
20579 min_notesz = offsetof (Elf_External_Note, name);
20580 if (data_remaining < min_notesz)
20581 {
20582 warn (ngettext ("Corrupt note: only %ld byte remains, "
20583 "not enough for a full note\n",
20584 "Corrupt note: only %ld bytes remain, "
20585 "not enough for a full note\n",
20586 data_remaining),
20587 (long) data_remaining);
20588 break;
20589 }
20590 data_remaining -= min_notesz;
20591
20592 inote.type = BYTE_GET (external->type);
20593 inote.namesz = BYTE_GET (external->namesz);
20594 inote.namedata = external->name;
20595 inote.descsz = BYTE_GET (external->descsz);
20596 inote.descdata = ((char *) external
20597 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
20598 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20599 next = ((char *) external
20600 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
20601 }
20602 else
20603 {
20604 Elf64_External_VMS_Note *vms_external;
20605
20606 /* PR binutils/15191
20607 Make sure that there is enough data to read. */
20608 min_notesz = offsetof (Elf64_External_VMS_Note, name);
20609 if (data_remaining < min_notesz)
20610 {
20611 warn (ngettext ("Corrupt note: only %ld byte remains, "
20612 "not enough for a full note\n",
20613 "Corrupt note: only %ld bytes remain, "
20614 "not enough for a full note\n",
20615 data_remaining),
20616 (long) data_remaining);
20617 break;
20618 }
20619 data_remaining -= min_notesz;
20620
20621 vms_external = (Elf64_External_VMS_Note *) external;
20622 inote.type = BYTE_GET (vms_external->type);
20623 inote.namesz = BYTE_GET (vms_external->namesz);
20624 inote.namedata = vms_external->name;
20625 inote.descsz = BYTE_GET (vms_external->descsz);
20626 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
20627 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20628 next = inote.descdata + align_power (inote.descsz, 3);
20629 }
20630
20631 /* PR 17531: file: 3443835e. */
20632 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
20633 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
20634 || (size_t) (inote.descdata - inote.namedata) > data_remaining
20635 || (size_t) (next - inote.descdata) < inote.descsz
20636 || ((size_t) (next - inote.descdata)
20637 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
20638 {
20639 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
20640 (unsigned long) ((char *) external - (char *) pnotes));
20641 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
20642 inote.type, inote.namesz, inote.descsz, (int) align);
20643 break;
20644 }
20645
20646 external = (Elf_External_Note *) next;
20647
20648 /* Verify that name is null terminated. It appears that at least
20649 one version of Linux (RedHat 6.0) generates corefiles that don't
20650 comply with the ELF spec by failing to include the null byte in
20651 namesz. */
20652 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
20653 {
20654 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
20655 {
20656 temp = (char *) malloc (inote.namesz + 1);
20657 if (temp == NULL)
20658 {
20659 error (_("Out of memory allocating space for inote name\n"));
20660 res = FALSE;
20661 break;
20662 }
20663
20664 memcpy (temp, inote.namedata, inote.namesz);
20665 inote.namedata = temp;
20666 }
20667 inote.namedata[inote.namesz] = 0;
20668 }
20669
20670 if (! process_note (& inote, filedata))
20671 res = FALSE;
20672
20673 free (temp);
20674 temp = NULL;
20675 }
20676
20677 free (pnotes);
20678
20679 return res;
20680 }
20681
20682 static bfd_boolean
20683 process_corefile_note_segments (Filedata * filedata)
20684 {
20685 Elf_Internal_Phdr * segment;
20686 unsigned int i;
20687 bfd_boolean res = TRUE;
20688
20689 if (! get_program_headers (filedata))
20690 return TRUE;
20691
20692 for (i = 0, segment = filedata->program_headers;
20693 i < filedata->file_header.e_phnum;
20694 i++, segment++)
20695 {
20696 if (segment->p_type == PT_NOTE)
20697 if (! process_notes_at (filedata, NULL,
20698 (bfd_vma) segment->p_offset,
20699 (bfd_vma) segment->p_filesz,
20700 (bfd_vma) segment->p_align))
20701 res = FALSE;
20702 }
20703
20704 return res;
20705 }
20706
20707 static bfd_boolean
20708 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
20709 {
20710 Elf_External_Note * pnotes;
20711 Elf_External_Note * external;
20712 char * end;
20713 bfd_boolean res = TRUE;
20714
20715 if (length <= 0)
20716 return FALSE;
20717
20718 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20719 _("v850 notes"));
20720 if (pnotes == NULL)
20721 return FALSE;
20722
20723 external = pnotes;
20724 end = (char*) pnotes + length;
20725
20726 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
20727 (unsigned long) offset, (unsigned long) length);
20728
20729 while ((char *) external + sizeof (Elf_External_Note) < end)
20730 {
20731 Elf_External_Note * next;
20732 Elf_Internal_Note inote;
20733
20734 inote.type = BYTE_GET (external->type);
20735 inote.namesz = BYTE_GET (external->namesz);
20736 inote.namedata = external->name;
20737 inote.descsz = BYTE_GET (external->descsz);
20738 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
20739 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20740
20741 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
20742 {
20743 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
20744 inote.descdata = inote.namedata;
20745 inote.namesz = 0;
20746 }
20747
20748 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
20749
20750 if ( ((char *) next > end)
20751 || ((char *) next < (char *) pnotes))
20752 {
20753 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
20754 (unsigned long) ((char *) external - (char *) pnotes));
20755 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20756 inote.type, inote.namesz, inote.descsz);
20757 break;
20758 }
20759
20760 external = next;
20761
20762 /* Prevent out-of-bounds indexing. */
20763 if ( inote.namedata + inote.namesz > end
20764 || inote.namedata + inote.namesz < inote.namedata)
20765 {
20766 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
20767 (unsigned long) ((char *) external - (char *) pnotes));
20768 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20769 inote.type, inote.namesz, inote.descsz);
20770 break;
20771 }
20772
20773 printf (" %s: ", get_v850_elf_note_type (inote.type));
20774
20775 if (! print_v850_note (& inote))
20776 {
20777 res = FALSE;
20778 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
20779 inote.namesz, inote.descsz);
20780 }
20781 }
20782
20783 free (pnotes);
20784
20785 return res;
20786 }
20787
20788 static bfd_boolean
20789 process_note_sections (Filedata * filedata)
20790 {
20791 Elf_Internal_Shdr * section;
20792 unsigned long i;
20793 unsigned int n = 0;
20794 bfd_boolean res = TRUE;
20795
20796 for (i = 0, section = filedata->section_headers;
20797 i < filedata->file_header.e_shnum && section != NULL;
20798 i++, section++)
20799 {
20800 if (section->sh_type == SHT_NOTE)
20801 {
20802 if (! process_notes_at (filedata, section,
20803 (bfd_vma) section->sh_offset,
20804 (bfd_vma) section->sh_size,
20805 (bfd_vma) section->sh_addralign))
20806 res = FALSE;
20807 n++;
20808 }
20809
20810 if (( filedata->file_header.e_machine == EM_V800
20811 || filedata->file_header.e_machine == EM_V850
20812 || filedata->file_header.e_machine == EM_CYGNUS_V850)
20813 && section->sh_type == SHT_RENESAS_INFO)
20814 {
20815 if (! process_v850_notes (filedata,
20816 (bfd_vma) section->sh_offset,
20817 (bfd_vma) section->sh_size))
20818 res = FALSE;
20819 n++;
20820 }
20821 }
20822
20823 if (n == 0)
20824 /* Try processing NOTE segments instead. */
20825 return process_corefile_note_segments (filedata);
20826
20827 return res;
20828 }
20829
20830 static bfd_boolean
20831 process_notes (Filedata * filedata)
20832 {
20833 /* If we have not been asked to display the notes then do nothing. */
20834 if (! do_notes)
20835 return TRUE;
20836
20837 if (filedata->file_header.e_type != ET_CORE)
20838 return process_note_sections (filedata);
20839
20840 /* No program headers means no NOTE segment. */
20841 if (filedata->file_header.e_phnum > 0)
20842 return process_corefile_note_segments (filedata);
20843
20844 if (filedata->is_separate)
20845 printf (_("No notes found in linked file '%s'.\n"),
20846 filedata->file_name);
20847 else
20848 printf (_("No notes found file.\n"));
20849
20850 return TRUE;
20851 }
20852
20853 static unsigned char *
20854 display_public_gnu_attributes (unsigned char * start,
20855 const unsigned char * const end)
20856 {
20857 printf (_(" Unknown GNU attribute: %s\n"), start);
20858
20859 start += strnlen ((char *) start, end - start);
20860 display_raw_attribute (start, end);
20861
20862 return (unsigned char *) end;
20863 }
20864
20865 static unsigned char *
20866 display_generic_attribute (unsigned char * start,
20867 unsigned int tag,
20868 const unsigned char * const end)
20869 {
20870 if (tag == 0)
20871 return (unsigned char *) end;
20872
20873 return display_tag_value (tag, start, end);
20874 }
20875
20876 static bfd_boolean
20877 process_arch_specific (Filedata * filedata)
20878 {
20879 if (! do_arch)
20880 return TRUE;
20881
20882 switch (filedata->file_header.e_machine)
20883 {
20884 case EM_ARC:
20885 case EM_ARC_COMPACT:
20886 case EM_ARC_COMPACT2:
20887 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
20888 display_arc_attribute,
20889 display_generic_attribute);
20890 case EM_ARM:
20891 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
20892 display_arm_attribute,
20893 display_generic_attribute);
20894
20895 case EM_MIPS:
20896 case EM_MIPS_RS3_LE:
20897 return process_mips_specific (filedata);
20898
20899 case EM_MSP430:
20900 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
20901 display_msp430_attribute,
20902 display_msp430_gnu_attribute);
20903
20904 case EM_RISCV:
20905 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
20906 display_riscv_attribute,
20907 display_generic_attribute);
20908
20909 case EM_NDS32:
20910 return process_nds32_specific (filedata);
20911
20912 case EM_68K:
20913 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20914 display_m68k_gnu_attribute);
20915
20916 case EM_PPC:
20917 case EM_PPC64:
20918 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20919 display_power_gnu_attribute);
20920
20921 case EM_S390:
20922 case EM_S390_OLD:
20923 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20924 display_s390_gnu_attribute);
20925
20926 case EM_SPARC:
20927 case EM_SPARC32PLUS:
20928 case EM_SPARCV9:
20929 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20930 display_sparc_gnu_attribute);
20931
20932 case EM_TI_C6000:
20933 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
20934 display_tic6x_attribute,
20935 display_generic_attribute);
20936
20937 case EM_CSKY:
20938 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
20939 display_csky_attribute, NULL);
20940
20941 default:
20942 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
20943 display_public_gnu_attributes,
20944 display_generic_attribute);
20945 }
20946 }
20947
20948 static bfd_boolean
20949 get_file_header (Filedata * filedata)
20950 {
20951 /* Read in the identity array. */
20952 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
20953 return FALSE;
20954
20955 /* Determine how to read the rest of the header. */
20956 switch (filedata->file_header.e_ident[EI_DATA])
20957 {
20958 default:
20959 case ELFDATANONE:
20960 case ELFDATA2LSB:
20961 byte_get = byte_get_little_endian;
20962 byte_put = byte_put_little_endian;
20963 break;
20964 case ELFDATA2MSB:
20965 byte_get = byte_get_big_endian;
20966 byte_put = byte_put_big_endian;
20967 break;
20968 }
20969
20970 /* For now we only support 32 bit and 64 bit ELF files. */
20971 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
20972
20973 /* Read in the rest of the header. */
20974 if (is_32bit_elf)
20975 {
20976 Elf32_External_Ehdr ehdr32;
20977
20978 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
20979 return FALSE;
20980
20981 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
20982 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
20983 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
20984 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
20985 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
20986 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
20987 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
20988 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
20989 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
20990 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
20991 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
20992 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
20993 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
20994 }
20995 else
20996 {
20997 Elf64_External_Ehdr ehdr64;
20998
20999 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21000 we will not be able to cope with the 64bit data found in
21001 64 ELF files. Detect this now and abort before we start
21002 overwriting things. */
21003 if (sizeof (bfd_vma) < 8)
21004 {
21005 error (_("This instance of readelf has been built without support for a\n\
21006 64 bit data type and so it cannot read 64 bit ELF files.\n"));
21007 return FALSE;
21008 }
21009
21010 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
21011 return FALSE;
21012
21013 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21014 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21015 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21016 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21017 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21018 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21019 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21020 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21021 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21022 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21023 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21024 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21025 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
21026 }
21027
21028 if (filedata->file_header.e_shoff)
21029 {
21030 /* There may be some extensions in the first section header. Don't
21031 bomb if we can't read it. */
21032 if (is_32bit_elf)
21033 get_32bit_section_headers (filedata, TRUE);
21034 else
21035 get_64bit_section_headers (filedata, TRUE);
21036 }
21037
21038 return TRUE;
21039 }
21040
21041 static void
21042 close_file (Filedata * filedata)
21043 {
21044 if (filedata)
21045 {
21046 if (filedata->handle)
21047 fclose (filedata->handle);
21048 free (filedata);
21049 }
21050 }
21051
21052 void
21053 close_debug_file (void * data)
21054 {
21055 close_file ((Filedata *) data);
21056 }
21057
21058 static Filedata *
21059 open_file (const char * pathname, bfd_boolean is_separate)
21060 {
21061 struct stat statbuf;
21062 Filedata * filedata = NULL;
21063
21064 if (stat (pathname, & statbuf) < 0
21065 || ! S_ISREG (statbuf.st_mode))
21066 goto fail;
21067
21068 filedata = calloc (1, sizeof * filedata);
21069 if (filedata == NULL)
21070 goto fail;
21071
21072 filedata->handle = fopen (pathname, "rb");
21073 if (filedata->handle == NULL)
21074 goto fail;
21075
21076 filedata->file_size = (bfd_size_type) statbuf.st_size;
21077 filedata->file_name = pathname;
21078 filedata->is_separate = is_separate;
21079
21080 if (! get_file_header (filedata))
21081 goto fail;
21082
21083 if (filedata->file_header.e_shoff)
21084 {
21085 bfd_boolean res;
21086
21087 /* Read the section headers again, this time for real. */
21088 if (is_32bit_elf)
21089 res = get_32bit_section_headers (filedata, FALSE);
21090 else
21091 res = get_64bit_section_headers (filedata, FALSE);
21092
21093 if (!res)
21094 goto fail;
21095 }
21096
21097 return filedata;
21098
21099 fail:
21100 if (filedata)
21101 {
21102 if (filedata->handle)
21103 fclose (filedata->handle);
21104 free (filedata);
21105 }
21106 return NULL;
21107 }
21108
21109 void *
21110 open_debug_file (const char * pathname)
21111 {
21112 return open_file (pathname, TRUE);
21113 }
21114
21115 /* Process one ELF object file according to the command line options.
21116 This file may actually be stored in an archive. The file is
21117 positioned at the start of the ELF object. Returns TRUE if no
21118 problems were encountered, FALSE otherwise. */
21119
21120 static bfd_boolean
21121 process_object (Filedata * filedata)
21122 {
21123 bfd_boolean have_separate_files;
21124 unsigned int i;
21125 bfd_boolean res;
21126
21127 if (! get_file_header (filedata))
21128 {
21129 error (_("%s: Failed to read file header\n"), filedata->file_name);
21130 return FALSE;
21131 }
21132
21133 /* Initialise per file variables. */
21134 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21135 filedata->version_info[i] = 0;
21136
21137 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21138 filedata->dynamic_info[i] = 0;
21139 filedata->dynamic_info_DT_GNU_HASH = 0;
21140 filedata->dynamic_info_DT_MIPS_XHASH = 0;
21141
21142 /* Process the file. */
21143 if (show_name)
21144 printf (_("\nFile: %s\n"), filedata->file_name);
21145
21146 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21147 Note we do this even if cmdline_dump_sects is empty because we
21148 must make sure that the dump_sets array is zeroed out before each
21149 object file is processed. */
21150 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21151 memset (filedata->dump.dump_sects, 0,
21152 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21153
21154 if (cmdline.num_dump_sects > 0)
21155 {
21156 if (filedata->dump.num_dump_sects == 0)
21157 /* A sneaky way of allocating the dump_sects array. */
21158 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21159
21160 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21161 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21162 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21163 }
21164
21165 if (! process_file_header (filedata))
21166 return FALSE;
21167
21168 if (! process_section_headers (filedata))
21169 {
21170 /* Without loaded section headers we cannot process lots of things. */
21171 do_unwind = do_version = do_dump = do_arch = FALSE;
21172
21173 if (! do_using_dynamic)
21174 do_syms = do_dyn_syms = do_reloc = FALSE;
21175 }
21176
21177 if (! process_section_groups (filedata))
21178 /* Without loaded section groups we cannot process unwind. */
21179 do_unwind = FALSE;
21180
21181 res = process_program_headers (filedata);
21182 if (res)
21183 res = process_dynamic_section (filedata);
21184
21185 if (! process_relocs (filedata))
21186 res = FALSE;
21187
21188 if (! process_unwind (filedata))
21189 res = FALSE;
21190
21191 if (! process_symbol_table (filedata))
21192 res = FALSE;
21193
21194 if (! process_lto_symbol_tables (filedata))
21195 res = FALSE;
21196
21197 if (! process_syminfo (filedata))
21198 res = FALSE;
21199
21200 if (! process_version_sections (filedata))
21201 res = FALSE;
21202
21203 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
21204 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
21205 else
21206 have_separate_files = FALSE;
21207
21208 if (! process_section_contents (filedata))
21209 res = FALSE;
21210
21211 if (have_separate_files)
21212 {
21213 separate_info * d;
21214
21215 for (d = first_separate_info; d != NULL; d = d->next)
21216 {
21217 if (process_links && ! process_file_header (d->handle))
21218 res = FALSE;
21219 else if (! process_section_headers (d->handle))
21220 res = FALSE;
21221 else if (process_links)
21222 {
21223 if (! process_section_contents (d->handle))
21224 res = FALSE;
21225 if (! process_section_groups (d->handle))
21226 res = FALSE;
21227 if (! process_program_headers (d->handle))
21228 res = FALSE;
21229 if (! process_dynamic_section (d->handle))
21230 res = FALSE;
21231 if (! process_relocs (d->handle))
21232 res = FALSE;
21233 if (! process_unwind (d->handle))
21234 res = FALSE;
21235 if (! process_symbol_table (d->handle))
21236 res = FALSE;
21237 if (! process_lto_symbol_tables (d->handle))
21238 res = FALSE;
21239 if (! process_syminfo (d->handle))
21240 res = FALSE;
21241 if (! process_version_sections (d->handle))
21242 res = FALSE;
21243 if (! process_notes (d->handle))
21244 res = FALSE;
21245 }
21246 }
21247
21248 /* The file handles are closed by the call to free_debug_memory() below. */
21249 }
21250
21251 if (! process_notes (filedata))
21252 res = FALSE;
21253
21254 if (! process_gnu_liblist (filedata))
21255 res = FALSE;
21256
21257 if (! process_arch_specific (filedata))
21258 res = FALSE;
21259
21260 free (filedata->program_headers);
21261 filedata->program_headers = NULL;
21262
21263 free (filedata->section_headers);
21264 filedata->section_headers = NULL;
21265
21266 free (filedata->string_table);
21267 filedata->string_table = NULL;
21268 filedata->string_table_length = 0;
21269
21270 free (filedata->dump.dump_sects);
21271 filedata->dump.dump_sects = NULL;
21272 filedata->dump.num_dump_sects = 0;
21273
21274 free (filedata->dynamic_strings);
21275 filedata->dynamic_strings = NULL;
21276 filedata->dynamic_strings_length = 0;
21277
21278 free (filedata->dynamic_symbols);
21279 filedata->dynamic_symbols = NULL;
21280 filedata->num_dynamic_syms = 0;
21281
21282 free (filedata->dynamic_syminfo);
21283 filedata->dynamic_syminfo = NULL;
21284
21285 free (filedata->dynamic_section);
21286 filedata->dynamic_section = NULL;
21287
21288 while (filedata->symtab_shndx_list != NULL)
21289 {
21290 elf_section_list *next = filedata->symtab_shndx_list->next;
21291 free (filedata->symtab_shndx_list);
21292 filedata->symtab_shndx_list = next;
21293 }
21294
21295 free (filedata->section_headers_groups);
21296 filedata->section_headers_groups = NULL;
21297
21298 if (filedata->section_groups)
21299 {
21300 struct group_list * g;
21301 struct group_list * next;
21302
21303 for (i = 0; i < filedata->group_count; i++)
21304 {
21305 for (g = filedata->section_groups [i].root; g != NULL; g = next)
21306 {
21307 next = g->next;
21308 free (g);
21309 }
21310 }
21311
21312 free (filedata->section_groups);
21313 filedata->section_groups = NULL;
21314 }
21315
21316 free_debug_memory ();
21317
21318 return res;
21319 }
21320
21321 /* Process an ELF archive.
21322 On entry the file is positioned just after the ARMAG string.
21323 Returns TRUE upon success, FALSE otherwise. */
21324
21325 static bfd_boolean
21326 process_archive (Filedata * filedata, bfd_boolean is_thin_archive)
21327 {
21328 struct archive_info arch;
21329 struct archive_info nested_arch;
21330 size_t got;
21331 bfd_boolean ret = TRUE;
21332
21333 show_name = TRUE;
21334
21335 /* The ARCH structure is used to hold information about this archive. */
21336 arch.file_name = NULL;
21337 arch.file = NULL;
21338 arch.index_array = NULL;
21339 arch.sym_table = NULL;
21340 arch.longnames = NULL;
21341
21342 /* The NESTED_ARCH structure is used as a single-item cache of information
21343 about a nested archive (when members of a thin archive reside within
21344 another regular archive file). */
21345 nested_arch.file_name = NULL;
21346 nested_arch.file = NULL;
21347 nested_arch.index_array = NULL;
21348 nested_arch.sym_table = NULL;
21349 nested_arch.longnames = NULL;
21350
21351 if (setup_archive (&arch, filedata->file_name, filedata->handle,
21352 filedata->file_size, is_thin_archive,
21353 do_archive_index) != 0)
21354 {
21355 ret = FALSE;
21356 goto out;
21357 }
21358
21359 if (do_archive_index)
21360 {
21361 if (arch.sym_table == NULL)
21362 error (_("%s: unable to dump the index as none was found\n"),
21363 filedata->file_name);
21364 else
21365 {
21366 unsigned long i, l;
21367 unsigned long current_pos;
21368
21369 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
21370 "in the symbol table)\n"),
21371 filedata->file_name, (unsigned long) arch.index_num,
21372 arch.sym_size);
21373
21374 current_pos = ftell (filedata->handle);
21375
21376 for (i = l = 0; i < arch.index_num; i++)
21377 {
21378 if (i == 0
21379 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
21380 {
21381 char * member_name
21382 = get_archive_member_name_at (&arch, arch.index_array[i],
21383 &nested_arch);
21384
21385 if (member_name != NULL)
21386 {
21387 char * qualified_name
21388 = make_qualified_name (&arch, &nested_arch,
21389 member_name);
21390
21391 if (qualified_name != NULL)
21392 {
21393 printf (_("Contents of binary %s at offset "),
21394 qualified_name);
21395 (void) print_vma (arch.index_array[i], PREFIX_HEX);
21396 putchar ('\n');
21397 free (qualified_name);
21398 }
21399 free (member_name);
21400 }
21401 }
21402
21403 if (l >= arch.sym_size)
21404 {
21405 error (_("%s: end of the symbol table reached "
21406 "before the end of the index\n"),
21407 filedata->file_name);
21408 ret = FALSE;
21409 break;
21410 }
21411 /* PR 17531: file: 0b6630b2. */
21412 printf ("\t%.*s\n",
21413 (int) (arch.sym_size - l), arch.sym_table + l);
21414 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
21415 }
21416
21417 if (arch.uses_64bit_indices)
21418 l = (l + 7) & ~ 7;
21419 else
21420 l += l & 1;
21421
21422 if (l < arch.sym_size)
21423 {
21424 error (ngettext ("%s: %ld byte remains in the symbol table, "
21425 "but without corresponding entries in "
21426 "the index table\n",
21427 "%s: %ld bytes remain in the symbol table, "
21428 "but without corresponding entries in "
21429 "the index table\n",
21430 arch.sym_size - l),
21431 filedata->file_name, arch.sym_size - l);
21432 ret = FALSE;
21433 }
21434
21435 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
21436 {
21437 error (_("%s: failed to seek back to start of object files "
21438 "in the archive\n"),
21439 filedata->file_name);
21440 ret = FALSE;
21441 goto out;
21442 }
21443 }
21444
21445 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
21446 && !do_segments && !do_header && !do_dump && !do_version
21447 && !do_histogram && !do_debugging && !do_arch && !do_notes
21448 && !do_section_groups && !do_dyn_syms)
21449 {
21450 ret = TRUE; /* Archive index only. */
21451 goto out;
21452 }
21453 }
21454
21455 while (1)
21456 {
21457 char * name;
21458 size_t namelen;
21459 char * qualified_name;
21460
21461 /* Read the next archive header. */
21462 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
21463 {
21464 error (_("%s: failed to seek to next archive header\n"),
21465 arch.file_name);
21466 ret = FALSE;
21467 break;
21468 }
21469 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
21470 if (got != sizeof arch.arhdr)
21471 {
21472 if (got == 0)
21473 break;
21474 /* PR 24049 - we cannot use filedata->file_name as this will
21475 have already been freed. */
21476 error (_("%s: failed to read archive header\n"), arch.file_name);
21477
21478 ret = FALSE;
21479 break;
21480 }
21481 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
21482 {
21483 error (_("%s: did not find a valid archive header\n"),
21484 arch.file_name);
21485 ret = FALSE;
21486 break;
21487 }
21488
21489 arch.next_arhdr_offset += sizeof arch.arhdr;
21490
21491 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
21492 if (filedata->archive_file_size & 01)
21493 ++filedata->archive_file_size;
21494
21495 name = get_archive_member_name (&arch, &nested_arch);
21496 if (name == NULL)
21497 {
21498 error (_("%s: bad archive file name\n"), arch.file_name);
21499 ret = FALSE;
21500 break;
21501 }
21502 namelen = strlen (name);
21503
21504 qualified_name = make_qualified_name (&arch, &nested_arch, name);
21505 if (qualified_name == NULL)
21506 {
21507 error (_("%s: bad archive file name\n"), arch.file_name);
21508 free (name);
21509 ret = FALSE;
21510 break;
21511 }
21512
21513 if (is_thin_archive && arch.nested_member_origin == 0)
21514 {
21515 /* This is a proxy for an external member of a thin archive. */
21516 Filedata * member_filedata;
21517 char * member_file_name = adjust_relative_path
21518 (filedata->file_name, name, namelen);
21519
21520 free (name);
21521 if (member_file_name == NULL)
21522 {
21523 free (qualified_name);
21524 ret = FALSE;
21525 break;
21526 }
21527
21528 member_filedata = open_file (member_file_name, FALSE);
21529 if (member_filedata == NULL)
21530 {
21531 error (_("Input file '%s' is not readable.\n"), member_file_name);
21532 free (member_file_name);
21533 free (qualified_name);
21534 ret = FALSE;
21535 break;
21536 }
21537
21538 filedata->archive_file_offset = arch.nested_member_origin;
21539 member_filedata->file_name = qualified_name;
21540
21541 if (! process_object (member_filedata))
21542 ret = FALSE;
21543
21544 close_file (member_filedata);
21545 free (member_file_name);
21546 }
21547 else if (is_thin_archive)
21548 {
21549 Filedata thin_filedata;
21550
21551 memset (&thin_filedata, 0, sizeof (thin_filedata));
21552
21553 /* PR 15140: Allow for corrupt thin archives. */
21554 if (nested_arch.file == NULL)
21555 {
21556 error (_("%s: contains corrupt thin archive: %s\n"),
21557 qualified_name, name);
21558 free (qualified_name);
21559 free (name);
21560 ret = FALSE;
21561 break;
21562 }
21563 free (name);
21564
21565 /* This is a proxy for a member of a nested archive. */
21566 filedata->archive_file_offset
21567 = arch.nested_member_origin + sizeof arch.arhdr;
21568
21569 /* The nested archive file will have been opened and setup by
21570 get_archive_member_name. */
21571 if (fseek (nested_arch.file, filedata->archive_file_offset,
21572 SEEK_SET) != 0)
21573 {
21574 error (_("%s: failed to seek to archive member.\n"),
21575 nested_arch.file_name);
21576 free (qualified_name);
21577 ret = FALSE;
21578 break;
21579 }
21580
21581 thin_filedata.handle = nested_arch.file;
21582 thin_filedata.file_name = qualified_name;
21583
21584 if (! process_object (& thin_filedata))
21585 ret = FALSE;
21586 }
21587 else
21588 {
21589 free (name);
21590 filedata->archive_file_offset = arch.next_arhdr_offset;
21591 filedata->file_name = qualified_name;
21592 if (! process_object (filedata))
21593 ret = FALSE;
21594 arch.next_arhdr_offset += filedata->archive_file_size;
21595 /* Stop looping with "negative" archive_file_size. */
21596 if (arch.next_arhdr_offset < filedata->archive_file_size)
21597 arch.next_arhdr_offset = -1ul;
21598 }
21599
21600 free (qualified_name);
21601 }
21602
21603 out:
21604 if (nested_arch.file != NULL)
21605 fclose (nested_arch.file);
21606 release_archive (&nested_arch);
21607 release_archive (&arch);
21608
21609 return ret;
21610 }
21611
21612 static bfd_boolean
21613 process_file (char * file_name)
21614 {
21615 Filedata * filedata = NULL;
21616 struct stat statbuf;
21617 char armag[SARMAG];
21618 bfd_boolean ret = TRUE;
21619
21620 if (stat (file_name, &statbuf) < 0)
21621 {
21622 if (errno == ENOENT)
21623 error (_("'%s': No such file\n"), file_name);
21624 else
21625 error (_("Could not locate '%s'. System error message: %s\n"),
21626 file_name, strerror (errno));
21627 return FALSE;
21628 }
21629
21630 if (! S_ISREG (statbuf.st_mode))
21631 {
21632 error (_("'%s' is not an ordinary file\n"), file_name);
21633 return FALSE;
21634 }
21635
21636 filedata = calloc (1, sizeof * filedata);
21637 if (filedata == NULL)
21638 {
21639 error (_("Out of memory allocating file data structure\n"));
21640 return FALSE;
21641 }
21642
21643 filedata->file_name = file_name;
21644 filedata->handle = fopen (file_name, "rb");
21645 if (filedata->handle == NULL)
21646 {
21647 error (_("Input file '%s' is not readable.\n"), file_name);
21648 free (filedata);
21649 return FALSE;
21650 }
21651
21652 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
21653 {
21654 error (_("%s: Failed to read file's magic number\n"), file_name);
21655 fclose (filedata->handle);
21656 free (filedata);
21657 return FALSE;
21658 }
21659
21660 filedata->file_size = (bfd_size_type) statbuf.st_size;
21661 filedata->is_separate = FALSE;
21662
21663 if (memcmp (armag, ARMAG, SARMAG) == 0)
21664 {
21665 if (! process_archive (filedata, FALSE))
21666 ret = FALSE;
21667 }
21668 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
21669 {
21670 if ( ! process_archive (filedata, TRUE))
21671 ret = FALSE;
21672 }
21673 else
21674 {
21675 if (do_archive_index && !check_all)
21676 error (_("File %s is not an archive so its index cannot be displayed.\n"),
21677 file_name);
21678
21679 rewind (filedata->handle);
21680 filedata->archive_file_size = filedata->archive_file_offset = 0;
21681
21682 if (! process_object (filedata))
21683 ret = FALSE;
21684 }
21685
21686 fclose (filedata->handle);
21687 free (filedata->section_headers);
21688 free (filedata->program_headers);
21689 free (filedata->string_table);
21690 free (filedata->dump.dump_sects);
21691 free (filedata);
21692
21693 free (ba_cache.strtab);
21694 ba_cache.strtab = NULL;
21695 free (ba_cache.symtab);
21696 ba_cache.symtab = NULL;
21697 ba_cache.filedata = NULL;
21698
21699 return ret;
21700 }
21701
21702 #ifdef SUPPORT_DISASSEMBLY
21703 /* Needed by the i386 disassembler. For extra credit, someone could
21704 fix this so that we insert symbolic addresses here, esp for GOT/PLT
21705 symbols. */
21706
21707 void
21708 print_address (unsigned int addr, FILE * outfile)
21709 {
21710 fprintf (outfile,"0x%8.8x", addr);
21711 }
21712
21713 /* Needed by the i386 disassembler. */
21714
21715 void
21716 db_task_printsym (unsigned int addr)
21717 {
21718 print_address (addr, stderr);
21719 }
21720 #endif
21721
21722 int
21723 main (int argc, char ** argv)
21724 {
21725 int err;
21726
21727 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
21728 setlocale (LC_MESSAGES, "");
21729 #endif
21730 #if defined (HAVE_SETLOCALE)
21731 setlocale (LC_CTYPE, "");
21732 #endif
21733 bindtextdomain (PACKAGE, LOCALEDIR);
21734 textdomain (PACKAGE);
21735
21736 expandargv (&argc, &argv);
21737
21738 parse_args (& cmdline, argc, argv);
21739
21740 if (optind < (argc - 1))
21741 /* When displaying information for more than one file,
21742 prefix the information with the file name. */
21743 show_name = TRUE;
21744 else if (optind >= argc)
21745 {
21746 /* Ensure that the warning is always displayed. */
21747 do_checks = TRUE;
21748
21749 warn (_("Nothing to do.\n"));
21750 usage (stderr);
21751 }
21752
21753 err = FALSE;
21754 while (optind < argc)
21755 if (! process_file (argv[optind++]))
21756 err = TRUE;
21757
21758 free (cmdline.dump_sects);
21759
21760 free (dump_ctf_symtab_name);
21761 free (dump_ctf_strtab_name);
21762 free (dump_ctf_parent_name);
21763
21764 return err ? EXIT_FAILURE : EXIT_SUCCESS;
21765 }