]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
Do not build libctf for targets that do not use the ELF file format.
[thirdparty/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2019 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 #ifdef HAVE_LIBCTF
64 #include "ctf-api.h"
65 #endif
66
67 #include "elf/common.h"
68 #include "elf/external.h"
69 #include "elf/internal.h"
70
71
72 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
73 we can obtain the H8 reloc numbers. We need these for the
74 get_reloc_size() function. We include h8.h again after defining
75 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
76
77 #include "elf/h8.h"
78 #undef _ELF_H8_H
79
80 /* Undo the effects of #including reloc-macros.h. */
81
82 #undef START_RELOC_NUMBERS
83 #undef RELOC_NUMBER
84 #undef FAKE_RELOC
85 #undef EMPTY_RELOC
86 #undef END_RELOC_NUMBERS
87 #undef _RELOC_MACROS_H
88
89 /* The following headers use the elf/reloc-macros.h file to
90 automatically generate relocation recognition functions
91 such as elf_mips_reloc_type() */
92
93 #define RELOC_MACROS_GEN_FUNC
94
95 #include "elf/aarch64.h"
96 #include "elf/alpha.h"
97 #include "elf/arc.h"
98 #include "elf/arm.h"
99 #include "elf/avr.h"
100 #include "elf/bfin.h"
101 #include "elf/cr16.h"
102 #include "elf/cris.h"
103 #include "elf/crx.h"
104 #include "elf/csky.h"
105 #include "elf/d10v.h"
106 #include "elf/d30v.h"
107 #include "elf/dlx.h"
108 #include "elf/bpf.h"
109 #include "elf/epiphany.h"
110 #include "elf/fr30.h"
111 #include "elf/frv.h"
112 #include "elf/ft32.h"
113 #include "elf/h8.h"
114 #include "elf/hppa.h"
115 #include "elf/i386.h"
116 #include "elf/i370.h"
117 #include "elf/i860.h"
118 #include "elf/i960.h"
119 #include "elf/ia64.h"
120 #include "elf/ip2k.h"
121 #include "elf/lm32.h"
122 #include "elf/iq2000.h"
123 #include "elf/m32c.h"
124 #include "elf/m32r.h"
125 #include "elf/m68k.h"
126 #include "elf/m68hc11.h"
127 #include "elf/s12z.h"
128 #include "elf/mcore.h"
129 #include "elf/mep.h"
130 #include "elf/metag.h"
131 #include "elf/microblaze.h"
132 #include "elf/mips.h"
133 #include "elf/mmix.h"
134 #include "elf/mn10200.h"
135 #include "elf/mn10300.h"
136 #include "elf/moxie.h"
137 #include "elf/mt.h"
138 #include "elf/msp430.h"
139 #include "elf/nds32.h"
140 #include "elf/nfp.h"
141 #include "elf/nios2.h"
142 #include "elf/or1k.h"
143 #include "elf/pj.h"
144 #include "elf/ppc.h"
145 #include "elf/ppc64.h"
146 #include "elf/pru.h"
147 #include "elf/riscv.h"
148 #include "elf/rl78.h"
149 #include "elf/rx.h"
150 #include "elf/s390.h"
151 #include "elf/score.h"
152 #include "elf/sh.h"
153 #include "elf/sparc.h"
154 #include "elf/spu.h"
155 #include "elf/tic6x.h"
156 #include "elf/tilegx.h"
157 #include "elf/tilepro.h"
158 #include "elf/v850.h"
159 #include "elf/vax.h"
160 #include "elf/visium.h"
161 #include "elf/wasm32.h"
162 #include "elf/x86-64.h"
163 #include "elf/xc16x.h"
164 #include "elf/xgate.h"
165 #include "elf/xstormy16.h"
166 #include "elf/xtensa.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 #ifdef HAVE_LIBCTF
190 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
191 #endif
192
193 typedef unsigned char dump_type;
194
195 /* A linked list of the section names for which dumps were requested. */
196 struct dump_list_entry
197 {
198 char * name;
199 dump_type type;
200 struct dump_list_entry * next;
201 };
202
203 typedef struct filedata
204 {
205 const char * file_name;
206 FILE * handle;
207 bfd_size_type file_size;
208 Elf_Internal_Ehdr file_header;
209 Elf_Internal_Shdr * section_headers;
210 Elf_Internal_Phdr * program_headers;
211 char * string_table;
212 unsigned long string_table_length;
213 /* A dynamic array of flags indicating for which sections a dump of
214 some kind has been requested. It is reset on a per-object file
215 basis and then initialised from the cmdline_dump_sects array,
216 the results of interpreting the -w switch, and the
217 dump_sects_byname list. */
218 dump_type * dump_sects;
219 unsigned int num_dump_sects;
220 } Filedata;
221
222 char * program_name = "readelf";
223
224 static unsigned long archive_file_offset;
225 static unsigned long archive_file_size;
226 static unsigned long dynamic_addr;
227 static bfd_size_type dynamic_size;
228 static size_t dynamic_nent;
229 static char * dynamic_strings;
230 static unsigned long dynamic_strings_length;
231 static unsigned long num_dynamic_syms;
232 static Elf_Internal_Sym * dynamic_symbols;
233 static Elf_Internal_Syminfo * dynamic_syminfo;
234 static unsigned long dynamic_syminfo_offset;
235 static unsigned int dynamic_syminfo_nent;
236 static char program_interpreter[PATH_MAX];
237 static bfd_vma dynamic_info[DT_ENCODING];
238 static bfd_vma dynamic_info_DT_GNU_HASH;
239 static bfd_vma version_info[16];
240 static Elf_Internal_Dyn * dynamic_section;
241 static elf_section_list * symtab_shndx_list;
242 static bfd_boolean show_name = FALSE;
243 static bfd_boolean do_dynamic = FALSE;
244 static bfd_boolean do_syms = FALSE;
245 static bfd_boolean do_dyn_syms = FALSE;
246 static bfd_boolean do_reloc = FALSE;
247 static bfd_boolean do_sections = FALSE;
248 static bfd_boolean do_section_groups = FALSE;
249 static bfd_boolean do_section_details = FALSE;
250 static bfd_boolean do_segments = FALSE;
251 static bfd_boolean do_unwind = FALSE;
252 static bfd_boolean do_using_dynamic = FALSE;
253 static bfd_boolean do_header = FALSE;
254 static bfd_boolean do_dump = FALSE;
255 static bfd_boolean do_version = FALSE;
256 static bfd_boolean do_histogram = FALSE;
257 static bfd_boolean do_debugging = FALSE;
258 #ifdef HAVE_LIBCTF
259 static bfd_boolean do_ctf = FALSE;
260 #endif
261 static bfd_boolean do_arch = FALSE;
262 static bfd_boolean do_notes = FALSE;
263 static bfd_boolean do_archive_index = FALSE;
264 static bfd_boolean is_32bit_elf = FALSE;
265 static bfd_boolean decompress_dumps = FALSE;
266
267 #ifdef HAVE_LIBCTF
268 static char *dump_ctf_parent_name;
269 static char *dump_ctf_symtab_name;
270 static char *dump_ctf_strtab_name;
271 #endif
272
273 struct group_list
274 {
275 struct group_list * next;
276 unsigned int section_index;
277 };
278
279 struct group
280 {
281 struct group_list * root;
282 unsigned int group_index;
283 };
284
285 static size_t group_count;
286 static struct group * section_groups;
287 static struct group ** section_headers_groups;
288
289 /* A dynamic array of flags indicating for which sections a dump
290 has been requested via command line switches. */
291 static Filedata cmdline;
292
293 static struct dump_list_entry * dump_sects_byname;
294
295 /* How to print a vma value. */
296 typedef enum print_mode
297 {
298 HEX,
299 DEC,
300 DEC_5,
301 UNSIGNED,
302 PREFIX_HEX,
303 FULL_HEX,
304 LONG_HEX
305 }
306 print_mode;
307
308 /* Versioned symbol info. */
309 enum versioned_symbol_info
310 {
311 symbol_undefined,
312 symbol_hidden,
313 symbol_public
314 };
315
316 static const char * get_symbol_version_string
317 (Filedata *, bfd_boolean, const char *, unsigned long, unsigned,
318 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
319
320 #define UNKNOWN -1
321
322 #define SECTION_NAME(X) \
323 ((X) == NULL ? _("<none>") \
324 : filedata->string_table == NULL ? _("<no-strings>") \
325 : ((X)->sh_name >= filedata->string_table_length ? _("<corrupt>") \
326 : filedata->string_table + (X)->sh_name))
327
328 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
329
330 #define GET_ELF_SYMBOLS(file, section, sym_count) \
331 (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count) \
332 : get_64bit_elf_symbols (file, section, sym_count))
333
334 #define VALID_DYNAMIC_NAME(offset) ((dynamic_strings != NULL) && (offset < dynamic_strings_length))
335 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
336 already been called and verified that the string exists. */
337 #define GET_DYNAMIC_NAME(offset) (dynamic_strings + offset)
338
339 #define REMOVE_ARCH_BITS(ADDR) \
340 do \
341 { \
342 if (filedata->file_header.e_machine == EM_ARM) \
343 (ADDR) &= ~1; \
344 } \
345 while (0)
346 \f
347 /* Print a BFD_VMA to an internal buffer, for use in error messages.
348 BFD_FMA_FMT can't be used in translated strings. */
349
350 static const char *
351 bfd_vmatoa (char *fmtch, bfd_vma value)
352 {
353 /* bfd_vmatoa is used more then once in a printf call for output.
354 Cycle through an array of buffers. */
355 static int buf_pos = 0;
356 static struct bfd_vmatoa_buf
357 {
358 char place[64];
359 } buf[4];
360 char *ret;
361 char fmt[32];
362
363 ret = buf[buf_pos++].place;
364 buf_pos %= ARRAY_SIZE (buf);
365
366 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
367 snprintf (ret, sizeof (buf[0].place), fmt, value);
368 return ret;
369 }
370
371 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
372 OFFSET + the offset of the current archive member, if we are examining an
373 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
374 allocate a buffer using malloc and fill that. In either case return the
375 pointer to the start of the retrieved data or NULL if something went wrong.
376 If something does go wrong and REASON is not NULL then emit an error
377 message using REASON as part of the context. */
378
379 static void *
380 get_data (void * var,
381 Filedata * filedata,
382 unsigned long offset,
383 bfd_size_type size,
384 bfd_size_type nmemb,
385 const char * reason)
386 {
387 void * mvar;
388 bfd_size_type amt = size * nmemb;
389
390 if (size == 0 || nmemb == 0)
391 return NULL;
392
393 /* If the size_t type is smaller than the bfd_size_type, eg because
394 you are building a 32-bit tool on a 64-bit host, then make sure
395 that when the sizes are cast to (size_t) no information is lost. */
396 if (sizeof (size_t) < sizeof (bfd_size_type)
397 && ( (bfd_size_type) ((size_t) size) != size
398 || (bfd_size_type) ((size_t) nmemb) != nmemb))
399 {
400 if (reason)
401 error (_("Size truncation prevents reading %s"
402 " elements of size %s for %s\n"),
403 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
404 return NULL;
405 }
406
407 /* Check for size overflow. */
408 if (amt < nmemb)
409 {
410 if (reason)
411 error (_("Size overflow prevents reading %s"
412 " elements of size %s for %s\n"),
413 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
414 return NULL;
415 }
416
417 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
418 attempting to allocate memory when the read is bound to fail. */
419 if (archive_file_offset > filedata->file_size
420 || offset > filedata->file_size - archive_file_offset
421 || amt > filedata->file_size - archive_file_offset - offset)
422 {
423 if (reason)
424 error (_("Reading %s bytes extends past end of file for %s\n"),
425 bfd_vmatoa ("u", amt), reason);
426 return NULL;
427 }
428
429 if (fseek (filedata->handle, archive_file_offset + offset, SEEK_SET))
430 {
431 if (reason)
432 error (_("Unable to seek to 0x%lx for %s\n"),
433 archive_file_offset + offset, reason);
434 return NULL;
435 }
436
437 mvar = var;
438 if (mvar == NULL)
439 {
440 /* Check for overflow. */
441 if (nmemb < (~(bfd_size_type) 0 - 1) / size)
442 /* + 1 so that we can '\0' terminate invalid string table sections. */
443 mvar = malloc ((size_t) amt + 1);
444
445 if (mvar == NULL)
446 {
447 if (reason)
448 error (_("Out of memory allocating %s bytes for %s\n"),
449 bfd_vmatoa ("u", amt), reason);
450 return NULL;
451 }
452
453 ((char *) mvar)[amt] = '\0';
454 }
455
456 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
457 {
458 if (reason)
459 error (_("Unable to read in %s bytes of %s\n"),
460 bfd_vmatoa ("u", amt), reason);
461 if (mvar != var)
462 free (mvar);
463 return NULL;
464 }
465
466 return mvar;
467 }
468
469 /* Print a VMA value in the MODE specified.
470 Returns the number of characters displayed. */
471
472 static unsigned int
473 print_vma (bfd_vma vma, print_mode mode)
474 {
475 unsigned int nc = 0;
476
477 switch (mode)
478 {
479 case FULL_HEX:
480 nc = printf ("0x");
481 /* Fall through. */
482 case LONG_HEX:
483 #ifdef BFD64
484 if (is_32bit_elf)
485 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
486 #endif
487 printf_vma (vma);
488 return nc + 16;
489
490 case DEC_5:
491 if (vma <= 99999)
492 return printf ("%5" BFD_VMA_FMT "d", vma);
493 /* Fall through. */
494 case PREFIX_HEX:
495 nc = printf ("0x");
496 /* Fall through. */
497 case HEX:
498 return nc + printf ("%" BFD_VMA_FMT "x", vma);
499
500 case DEC:
501 return printf ("%" BFD_VMA_FMT "d", vma);
502
503 case UNSIGNED:
504 return printf ("%" BFD_VMA_FMT "u", vma);
505
506 default:
507 /* FIXME: Report unrecognised mode ? */
508 return 0;
509 }
510 }
511
512 /* Display a symbol on stdout. Handles the display of control characters and
513 multibye characters (assuming the host environment supports them).
514
515 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
516
517 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
518 padding as necessary.
519
520 Returns the number of emitted characters. */
521
522 static unsigned int
523 print_symbol (signed int width, const char *symbol)
524 {
525 bfd_boolean extra_padding = FALSE;
526 signed int num_printed = 0;
527 #ifdef HAVE_MBSTATE_T
528 mbstate_t state;
529 #endif
530 unsigned int width_remaining;
531
532 if (width < 0)
533 {
534 /* Keep the width positive. This helps the code below. */
535 width = - width;
536 extra_padding = TRUE;
537 }
538 else if (width == 0)
539 return 0;
540
541 if (do_wide)
542 /* Set the remaining width to a very large value.
543 This simplifies the code below. */
544 width_remaining = INT_MAX;
545 else
546 width_remaining = width;
547
548 #ifdef HAVE_MBSTATE_T
549 /* Initialise the multibyte conversion state. */
550 memset (& state, 0, sizeof (state));
551 #endif
552
553 while (width_remaining)
554 {
555 size_t n;
556 const char c = *symbol++;
557
558 if (c == 0)
559 break;
560
561 /* Do not print control characters directly as they can affect terminal
562 settings. Such characters usually appear in the names generated
563 by the assembler for local labels. */
564 if (ISCNTRL (c))
565 {
566 if (width_remaining < 2)
567 break;
568
569 printf ("^%c", c + 0x40);
570 width_remaining -= 2;
571 num_printed += 2;
572 }
573 else if (ISPRINT (c))
574 {
575 putchar (c);
576 width_remaining --;
577 num_printed ++;
578 }
579 else
580 {
581 #ifdef HAVE_MBSTATE_T
582 wchar_t w;
583 #endif
584 /* Let printf do the hard work of displaying multibyte characters. */
585 printf ("%.1s", symbol - 1);
586 width_remaining --;
587 num_printed ++;
588
589 #ifdef HAVE_MBSTATE_T
590 /* Try to find out how many bytes made up the character that was
591 just printed. Advance the symbol pointer past the bytes that
592 were displayed. */
593 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
594 #else
595 n = 1;
596 #endif
597 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
598 symbol += (n - 1);
599 }
600 }
601
602 if (extra_padding && num_printed < width)
603 {
604 /* Fill in the remaining spaces. */
605 printf ("%-*s", width - num_printed, " ");
606 num_printed = width;
607 }
608
609 return num_printed;
610 }
611
612 /* Returns a pointer to a static buffer containing a printable version of
613 the given section's name. Like print_symbol, except that it does not try
614 to print multibyte characters, it just interprets them as hex values. */
615
616 static const char *
617 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
618 {
619 #define MAX_PRINT_SEC_NAME_LEN 128
620 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
621 const char * name = SECTION_NAME (sec);
622 char * buf = sec_name_buf;
623 char c;
624 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
625
626 while ((c = * name ++) != 0)
627 {
628 if (ISCNTRL (c))
629 {
630 if (remaining < 2)
631 break;
632
633 * buf ++ = '^';
634 * buf ++ = c + 0x40;
635 remaining -= 2;
636 }
637 else if (ISPRINT (c))
638 {
639 * buf ++ = c;
640 remaining -= 1;
641 }
642 else
643 {
644 static char hex[17] = "0123456789ABCDEF";
645
646 if (remaining < 4)
647 break;
648 * buf ++ = '<';
649 * buf ++ = hex[(c & 0xf0) >> 4];
650 * buf ++ = hex[c & 0x0f];
651 * buf ++ = '>';
652 remaining -= 4;
653 }
654
655 if (remaining == 0)
656 break;
657 }
658
659 * buf = 0;
660 return sec_name_buf;
661 }
662
663 static const char *
664 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
665 {
666 if (ndx >= filedata->file_header.e_shnum)
667 return _("<corrupt>");
668
669 return printable_section_name (filedata, filedata->section_headers + ndx);
670 }
671
672 /* Return a pointer to section NAME, or NULL if no such section exists. */
673
674 static Elf_Internal_Shdr *
675 find_section (Filedata * filedata, const char * name)
676 {
677 unsigned int i;
678
679 if (filedata->section_headers == NULL)
680 return NULL;
681
682 for (i = 0; i < filedata->file_header.e_shnum; i++)
683 if (streq (SECTION_NAME (filedata->section_headers + i), name))
684 return filedata->section_headers + i;
685
686 return NULL;
687 }
688
689 /* Return a pointer to a section containing ADDR, or NULL if no such
690 section exists. */
691
692 static Elf_Internal_Shdr *
693 find_section_by_address (Filedata * filedata, bfd_vma addr)
694 {
695 unsigned int i;
696
697 if (filedata->section_headers == NULL)
698 return NULL;
699
700 for (i = 0; i < filedata->file_header.e_shnum; i++)
701 {
702 Elf_Internal_Shdr *sec = filedata->section_headers + i;
703
704 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
705 return sec;
706 }
707
708 return NULL;
709 }
710
711 static Elf_Internal_Shdr *
712 find_section_by_type (Filedata * filedata, unsigned int type)
713 {
714 unsigned int i;
715
716 if (filedata->section_headers == NULL)
717 return NULL;
718
719 for (i = 0; i < filedata->file_header.e_shnum; i++)
720 {
721 Elf_Internal_Shdr *sec = filedata->section_headers + i;
722
723 if (sec->sh_type == type)
724 return sec;
725 }
726
727 return NULL;
728 }
729
730 /* Return a pointer to section NAME, or NULL if no such section exists,
731 restricted to the list of sections given in SET. */
732
733 static Elf_Internal_Shdr *
734 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
735 {
736 unsigned int i;
737
738 if (filedata->section_headers == NULL)
739 return NULL;
740
741 if (set != NULL)
742 {
743 while ((i = *set++) > 0)
744 {
745 /* See PR 21156 for a reproducer. */
746 if (i >= filedata->file_header.e_shnum)
747 continue; /* FIXME: Should we issue an error message ? */
748
749 if (streq (SECTION_NAME (filedata->section_headers + i), name))
750 return filedata->section_headers + i;
751 }
752 }
753
754 return find_section (filedata, name);
755 }
756
757 /* Read an unsigned LEB128 encoded value from DATA.
758 Set *LENGTH_RETURN to the number of bytes read. */
759
760 static inline unsigned long
761 read_uleb128 (unsigned char * data,
762 unsigned int * length_return,
763 const unsigned char * const end)
764 {
765 return read_leb128 (data, length_return, FALSE, end);
766 }
767
768 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
769 This OS has so many departures from the ELF standard that we test it at
770 many places. */
771
772 static inline bfd_boolean
773 is_ia64_vms (Filedata * filedata)
774 {
775 return filedata->file_header.e_machine == EM_IA_64
776 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
777 }
778
779 /* Guess the relocation size commonly used by the specific machines. */
780
781 static bfd_boolean
782 guess_is_rela (unsigned int e_machine)
783 {
784 switch (e_machine)
785 {
786 /* Targets that use REL relocations. */
787 case EM_386:
788 case EM_IAMCU:
789 case EM_960:
790 case EM_ARM:
791 case EM_D10V:
792 case EM_CYGNUS_D10V:
793 case EM_DLX:
794 case EM_MIPS:
795 case EM_MIPS_RS3_LE:
796 case EM_CYGNUS_M32R:
797 case EM_SCORE:
798 case EM_XGATE:
799 case EM_NFP:
800 case EM_BPF:
801 return FALSE;
802
803 /* Targets that use RELA relocations. */
804 case EM_68K:
805 case EM_860:
806 case EM_AARCH64:
807 case EM_ADAPTEVA_EPIPHANY:
808 case EM_ALPHA:
809 case EM_ALTERA_NIOS2:
810 case EM_ARC:
811 case EM_ARC_COMPACT:
812 case EM_ARC_COMPACT2:
813 case EM_AVR:
814 case EM_AVR_OLD:
815 case EM_BLACKFIN:
816 case EM_CR16:
817 case EM_CRIS:
818 case EM_CRX:
819 case EM_CSKY:
820 case EM_D30V:
821 case EM_CYGNUS_D30V:
822 case EM_FR30:
823 case EM_FT32:
824 case EM_CYGNUS_FR30:
825 case EM_CYGNUS_FRV:
826 case EM_H8S:
827 case EM_H8_300:
828 case EM_H8_300H:
829 case EM_IA_64:
830 case EM_IP2K:
831 case EM_IP2K_OLD:
832 case EM_IQ2000:
833 case EM_LATTICEMICO32:
834 case EM_M32C_OLD:
835 case EM_M32C:
836 case EM_M32R:
837 case EM_MCORE:
838 case EM_CYGNUS_MEP:
839 case EM_METAG:
840 case EM_MMIX:
841 case EM_MN10200:
842 case EM_CYGNUS_MN10200:
843 case EM_MN10300:
844 case EM_CYGNUS_MN10300:
845 case EM_MOXIE:
846 case EM_MSP430:
847 case EM_MSP430_OLD:
848 case EM_MT:
849 case EM_NDS32:
850 case EM_NIOS32:
851 case EM_OR1K:
852 case EM_PPC64:
853 case EM_PPC:
854 case EM_TI_PRU:
855 case EM_RISCV:
856 case EM_RL78:
857 case EM_RX:
858 case EM_S390:
859 case EM_S390_OLD:
860 case EM_SH:
861 case EM_SPARC:
862 case EM_SPARC32PLUS:
863 case EM_SPARCV9:
864 case EM_SPU:
865 case EM_TI_C6000:
866 case EM_TILEGX:
867 case EM_TILEPRO:
868 case EM_V800:
869 case EM_V850:
870 case EM_CYGNUS_V850:
871 case EM_VAX:
872 case EM_VISIUM:
873 case EM_X86_64:
874 case EM_L1OM:
875 case EM_K1OM:
876 case EM_XSTORMY16:
877 case EM_XTENSA:
878 case EM_XTENSA_OLD:
879 case EM_MICROBLAZE:
880 case EM_MICROBLAZE_OLD:
881 case EM_WEBASSEMBLY:
882 return TRUE;
883
884 case EM_68HC05:
885 case EM_68HC08:
886 case EM_68HC11:
887 case EM_68HC16:
888 case EM_FX66:
889 case EM_ME16:
890 case EM_MMA:
891 case EM_NCPU:
892 case EM_NDR1:
893 case EM_PCP:
894 case EM_ST100:
895 case EM_ST19:
896 case EM_ST7:
897 case EM_ST9PLUS:
898 case EM_STARCORE:
899 case EM_SVX:
900 case EM_TINYJ:
901 default:
902 warn (_("Don't know about relocations on this machine architecture\n"));
903 return FALSE;
904 }
905 }
906
907 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
908 Returns TRUE upon success, FALSE otherwise. If successful then a
909 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
910 and the number of relocs loaded is placed in *NRELASP. It is the caller's
911 responsibility to free the allocated buffer. */
912
913 static bfd_boolean
914 slurp_rela_relocs (Filedata * filedata,
915 unsigned long rel_offset,
916 unsigned long rel_size,
917 Elf_Internal_Rela ** relasp,
918 unsigned long * nrelasp)
919 {
920 Elf_Internal_Rela * relas;
921 size_t nrelas;
922 unsigned int i;
923
924 if (is_32bit_elf)
925 {
926 Elf32_External_Rela * erelas;
927
928 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
929 rel_size, _("32-bit relocation data"));
930 if (!erelas)
931 return FALSE;
932
933 nrelas = rel_size / sizeof (Elf32_External_Rela);
934
935 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
936 sizeof (Elf_Internal_Rela));
937
938 if (relas == NULL)
939 {
940 free (erelas);
941 error (_("out of memory parsing relocs\n"));
942 return FALSE;
943 }
944
945 for (i = 0; i < nrelas; i++)
946 {
947 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
948 relas[i].r_info = BYTE_GET (erelas[i].r_info);
949 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
950 }
951
952 free (erelas);
953 }
954 else
955 {
956 Elf64_External_Rela * erelas;
957
958 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
959 rel_size, _("64-bit relocation data"));
960 if (!erelas)
961 return FALSE;
962
963 nrelas = rel_size / sizeof (Elf64_External_Rela);
964
965 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
966 sizeof (Elf_Internal_Rela));
967
968 if (relas == NULL)
969 {
970 free (erelas);
971 error (_("out of memory parsing relocs\n"));
972 return FALSE;
973 }
974
975 for (i = 0; i < nrelas; i++)
976 {
977 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
978 relas[i].r_info = BYTE_GET (erelas[i].r_info);
979 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
980
981 /* The #ifdef BFD64 below is to prevent a compile time
982 warning. We know that if we do not have a 64 bit data
983 type that we will never execute this code anyway. */
984 #ifdef BFD64
985 if (filedata->file_header.e_machine == EM_MIPS
986 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
987 {
988 /* In little-endian objects, r_info isn't really a
989 64-bit little-endian value: it has a 32-bit
990 little-endian symbol index followed by four
991 individual byte fields. Reorder INFO
992 accordingly. */
993 bfd_vma inf = relas[i].r_info;
994 inf = (((inf & 0xffffffff) << 32)
995 | ((inf >> 56) & 0xff)
996 | ((inf >> 40) & 0xff00)
997 | ((inf >> 24) & 0xff0000)
998 | ((inf >> 8) & 0xff000000));
999 relas[i].r_info = inf;
1000 }
1001 #endif /* BFD64 */
1002 }
1003
1004 free (erelas);
1005 }
1006
1007 *relasp = relas;
1008 *nrelasp = nrelas;
1009 return TRUE;
1010 }
1011
1012 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1013 Returns TRUE upon success, FALSE otherwise. If successful then a
1014 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1015 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1016 responsibility to free the allocated buffer. */
1017
1018 static bfd_boolean
1019 slurp_rel_relocs (Filedata * filedata,
1020 unsigned long rel_offset,
1021 unsigned long rel_size,
1022 Elf_Internal_Rela ** relsp,
1023 unsigned long * nrelsp)
1024 {
1025 Elf_Internal_Rela * rels;
1026 size_t nrels;
1027 unsigned int i;
1028
1029 if (is_32bit_elf)
1030 {
1031 Elf32_External_Rel * erels;
1032
1033 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1034 rel_size, _("32-bit relocation data"));
1035 if (!erels)
1036 return FALSE;
1037
1038 nrels = rel_size / sizeof (Elf32_External_Rel);
1039
1040 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1041
1042 if (rels == NULL)
1043 {
1044 free (erels);
1045 error (_("out of memory parsing relocs\n"));
1046 return FALSE;
1047 }
1048
1049 for (i = 0; i < nrels; i++)
1050 {
1051 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1052 rels[i].r_info = BYTE_GET (erels[i].r_info);
1053 rels[i].r_addend = 0;
1054 }
1055
1056 free (erels);
1057 }
1058 else
1059 {
1060 Elf64_External_Rel * erels;
1061
1062 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1063 rel_size, _("64-bit relocation data"));
1064 if (!erels)
1065 return FALSE;
1066
1067 nrels = rel_size / sizeof (Elf64_External_Rel);
1068
1069 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1070
1071 if (rels == NULL)
1072 {
1073 free (erels);
1074 error (_("out of memory parsing relocs\n"));
1075 return FALSE;
1076 }
1077
1078 for (i = 0; i < nrels; i++)
1079 {
1080 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1081 rels[i].r_info = BYTE_GET (erels[i].r_info);
1082 rels[i].r_addend = 0;
1083
1084 /* The #ifdef BFD64 below is to prevent a compile time
1085 warning. We know that if we do not have a 64 bit data
1086 type that we will never execute this code anyway. */
1087 #ifdef BFD64
1088 if (filedata->file_header.e_machine == EM_MIPS
1089 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1090 {
1091 /* In little-endian objects, r_info isn't really a
1092 64-bit little-endian value: it has a 32-bit
1093 little-endian symbol index followed by four
1094 individual byte fields. Reorder INFO
1095 accordingly. */
1096 bfd_vma inf = rels[i].r_info;
1097 inf = (((inf & 0xffffffff) << 32)
1098 | ((inf >> 56) & 0xff)
1099 | ((inf >> 40) & 0xff00)
1100 | ((inf >> 24) & 0xff0000)
1101 | ((inf >> 8) & 0xff000000));
1102 rels[i].r_info = inf;
1103 }
1104 #endif /* BFD64 */
1105 }
1106
1107 free (erels);
1108 }
1109
1110 *relsp = rels;
1111 *nrelsp = nrels;
1112 return TRUE;
1113 }
1114
1115 /* Returns the reloc type extracted from the reloc info field. */
1116
1117 static unsigned int
1118 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1119 {
1120 if (is_32bit_elf)
1121 return ELF32_R_TYPE (reloc_info);
1122
1123 switch (filedata->file_header.e_machine)
1124 {
1125 case EM_MIPS:
1126 /* Note: We assume that reloc_info has already been adjusted for us. */
1127 return ELF64_MIPS_R_TYPE (reloc_info);
1128
1129 case EM_SPARCV9:
1130 return ELF64_R_TYPE_ID (reloc_info);
1131
1132 default:
1133 return ELF64_R_TYPE (reloc_info);
1134 }
1135 }
1136
1137 /* Return the symbol index extracted from the reloc info field. */
1138
1139 static bfd_vma
1140 get_reloc_symindex (bfd_vma reloc_info)
1141 {
1142 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1143 }
1144
1145 static inline bfd_boolean
1146 uses_msp430x_relocs (Filedata * filedata)
1147 {
1148 return
1149 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1150 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1151 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1152 /* TI compiler uses ELFOSABI_NONE. */
1153 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1154 }
1155
1156 /* Display the contents of the relocation data found at the specified
1157 offset. */
1158
1159 static bfd_boolean
1160 dump_relocations (Filedata * filedata,
1161 unsigned long rel_offset,
1162 unsigned long rel_size,
1163 Elf_Internal_Sym * symtab,
1164 unsigned long nsyms,
1165 char * strtab,
1166 unsigned long strtablen,
1167 int is_rela,
1168 bfd_boolean is_dynsym)
1169 {
1170 unsigned long i;
1171 Elf_Internal_Rela * rels;
1172 bfd_boolean res = TRUE;
1173
1174 if (is_rela == UNKNOWN)
1175 is_rela = guess_is_rela (filedata->file_header.e_machine);
1176
1177 if (is_rela)
1178 {
1179 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1180 return FALSE;
1181 }
1182 else
1183 {
1184 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1185 return FALSE;
1186 }
1187
1188 if (is_32bit_elf)
1189 {
1190 if (is_rela)
1191 {
1192 if (do_wide)
1193 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1194 else
1195 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1196 }
1197 else
1198 {
1199 if (do_wide)
1200 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1201 else
1202 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1203 }
1204 }
1205 else
1206 {
1207 if (is_rela)
1208 {
1209 if (do_wide)
1210 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1211 else
1212 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1213 }
1214 else
1215 {
1216 if (do_wide)
1217 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1218 else
1219 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1220 }
1221 }
1222
1223 for (i = 0; i < rel_size; i++)
1224 {
1225 const char * rtype;
1226 bfd_vma offset;
1227 bfd_vma inf;
1228 bfd_vma symtab_index;
1229 bfd_vma type;
1230
1231 offset = rels[i].r_offset;
1232 inf = rels[i].r_info;
1233
1234 type = get_reloc_type (filedata, inf);
1235 symtab_index = get_reloc_symindex (inf);
1236
1237 if (is_32bit_elf)
1238 {
1239 printf ("%8.8lx %8.8lx ",
1240 (unsigned long) offset & 0xffffffff,
1241 (unsigned long) inf & 0xffffffff);
1242 }
1243 else
1244 {
1245 #if BFD_HOST_64BIT_LONG
1246 printf (do_wide
1247 ? "%16.16lx %16.16lx "
1248 : "%12.12lx %12.12lx ",
1249 offset, inf);
1250 #elif BFD_HOST_64BIT_LONG_LONG
1251 #ifndef __MSVCRT__
1252 printf (do_wide
1253 ? "%16.16llx %16.16llx "
1254 : "%12.12llx %12.12llx ",
1255 offset, inf);
1256 #else
1257 printf (do_wide
1258 ? "%16.16I64x %16.16I64x "
1259 : "%12.12I64x %12.12I64x ",
1260 offset, inf);
1261 #endif
1262 #else
1263 printf (do_wide
1264 ? "%8.8lx%8.8lx %8.8lx%8.8lx "
1265 : "%4.4lx%8.8lx %4.4lx%8.8lx ",
1266 _bfd_int64_high (offset),
1267 _bfd_int64_low (offset),
1268 _bfd_int64_high (inf),
1269 _bfd_int64_low (inf));
1270 #endif
1271 }
1272
1273 switch (filedata->file_header.e_machine)
1274 {
1275 default:
1276 rtype = NULL;
1277 break;
1278
1279 case EM_AARCH64:
1280 rtype = elf_aarch64_reloc_type (type);
1281 break;
1282
1283 case EM_M32R:
1284 case EM_CYGNUS_M32R:
1285 rtype = elf_m32r_reloc_type (type);
1286 break;
1287
1288 case EM_386:
1289 case EM_IAMCU:
1290 rtype = elf_i386_reloc_type (type);
1291 break;
1292
1293 case EM_68HC11:
1294 case EM_68HC12:
1295 rtype = elf_m68hc11_reloc_type (type);
1296 break;
1297
1298 case EM_S12Z:
1299 rtype = elf_s12z_reloc_type (type);
1300 break;
1301
1302 case EM_68K:
1303 rtype = elf_m68k_reloc_type (type);
1304 break;
1305
1306 case EM_960:
1307 rtype = elf_i960_reloc_type (type);
1308 break;
1309
1310 case EM_AVR:
1311 case EM_AVR_OLD:
1312 rtype = elf_avr_reloc_type (type);
1313 break;
1314
1315 case EM_OLD_SPARCV9:
1316 case EM_SPARC32PLUS:
1317 case EM_SPARCV9:
1318 case EM_SPARC:
1319 rtype = elf_sparc_reloc_type (type);
1320 break;
1321
1322 case EM_SPU:
1323 rtype = elf_spu_reloc_type (type);
1324 break;
1325
1326 case EM_V800:
1327 rtype = v800_reloc_type (type);
1328 break;
1329 case EM_V850:
1330 case EM_CYGNUS_V850:
1331 rtype = v850_reloc_type (type);
1332 break;
1333
1334 case EM_D10V:
1335 case EM_CYGNUS_D10V:
1336 rtype = elf_d10v_reloc_type (type);
1337 break;
1338
1339 case EM_D30V:
1340 case EM_CYGNUS_D30V:
1341 rtype = elf_d30v_reloc_type (type);
1342 break;
1343
1344 case EM_DLX:
1345 rtype = elf_dlx_reloc_type (type);
1346 break;
1347
1348 case EM_SH:
1349 rtype = elf_sh_reloc_type (type);
1350 break;
1351
1352 case EM_MN10300:
1353 case EM_CYGNUS_MN10300:
1354 rtype = elf_mn10300_reloc_type (type);
1355 break;
1356
1357 case EM_MN10200:
1358 case EM_CYGNUS_MN10200:
1359 rtype = elf_mn10200_reloc_type (type);
1360 break;
1361
1362 case EM_FR30:
1363 case EM_CYGNUS_FR30:
1364 rtype = elf_fr30_reloc_type (type);
1365 break;
1366
1367 case EM_CYGNUS_FRV:
1368 rtype = elf_frv_reloc_type (type);
1369 break;
1370
1371 case EM_CSKY:
1372 rtype = elf_csky_reloc_type (type);
1373 break;
1374
1375 case EM_FT32:
1376 rtype = elf_ft32_reloc_type (type);
1377 break;
1378
1379 case EM_MCORE:
1380 rtype = elf_mcore_reloc_type (type);
1381 break;
1382
1383 case EM_MMIX:
1384 rtype = elf_mmix_reloc_type (type);
1385 break;
1386
1387 case EM_MOXIE:
1388 rtype = elf_moxie_reloc_type (type);
1389 break;
1390
1391 case EM_MSP430:
1392 if (uses_msp430x_relocs (filedata))
1393 {
1394 rtype = elf_msp430x_reloc_type (type);
1395 break;
1396 }
1397 /* Fall through. */
1398 case EM_MSP430_OLD:
1399 rtype = elf_msp430_reloc_type (type);
1400 break;
1401
1402 case EM_NDS32:
1403 rtype = elf_nds32_reloc_type (type);
1404 break;
1405
1406 case EM_PPC:
1407 rtype = elf_ppc_reloc_type (type);
1408 break;
1409
1410 case EM_PPC64:
1411 rtype = elf_ppc64_reloc_type (type);
1412 break;
1413
1414 case EM_MIPS:
1415 case EM_MIPS_RS3_LE:
1416 rtype = elf_mips_reloc_type (type);
1417 break;
1418
1419 case EM_RISCV:
1420 rtype = elf_riscv_reloc_type (type);
1421 break;
1422
1423 case EM_ALPHA:
1424 rtype = elf_alpha_reloc_type (type);
1425 break;
1426
1427 case EM_ARM:
1428 rtype = elf_arm_reloc_type (type);
1429 break;
1430
1431 case EM_ARC:
1432 case EM_ARC_COMPACT:
1433 case EM_ARC_COMPACT2:
1434 rtype = elf_arc_reloc_type (type);
1435 break;
1436
1437 case EM_PARISC:
1438 rtype = elf_hppa_reloc_type (type);
1439 break;
1440
1441 case EM_H8_300:
1442 case EM_H8_300H:
1443 case EM_H8S:
1444 rtype = elf_h8_reloc_type (type);
1445 break;
1446
1447 case EM_OR1K:
1448 rtype = elf_or1k_reloc_type (type);
1449 break;
1450
1451 case EM_PJ:
1452 case EM_PJ_OLD:
1453 rtype = elf_pj_reloc_type (type);
1454 break;
1455 case EM_IA_64:
1456 rtype = elf_ia64_reloc_type (type);
1457 break;
1458
1459 case EM_CRIS:
1460 rtype = elf_cris_reloc_type (type);
1461 break;
1462
1463 case EM_860:
1464 rtype = elf_i860_reloc_type (type);
1465 break;
1466
1467 case EM_X86_64:
1468 case EM_L1OM:
1469 case EM_K1OM:
1470 rtype = elf_x86_64_reloc_type (type);
1471 break;
1472
1473 case EM_S370:
1474 rtype = i370_reloc_type (type);
1475 break;
1476
1477 case EM_S390_OLD:
1478 case EM_S390:
1479 rtype = elf_s390_reloc_type (type);
1480 break;
1481
1482 case EM_SCORE:
1483 rtype = elf_score_reloc_type (type);
1484 break;
1485
1486 case EM_XSTORMY16:
1487 rtype = elf_xstormy16_reloc_type (type);
1488 break;
1489
1490 case EM_CRX:
1491 rtype = elf_crx_reloc_type (type);
1492 break;
1493
1494 case EM_VAX:
1495 rtype = elf_vax_reloc_type (type);
1496 break;
1497
1498 case EM_VISIUM:
1499 rtype = elf_visium_reloc_type (type);
1500 break;
1501
1502 case EM_BPF:
1503 rtype = elf_bpf_reloc_type (type);
1504 break;
1505
1506 case EM_ADAPTEVA_EPIPHANY:
1507 rtype = elf_epiphany_reloc_type (type);
1508 break;
1509
1510 case EM_IP2K:
1511 case EM_IP2K_OLD:
1512 rtype = elf_ip2k_reloc_type (type);
1513 break;
1514
1515 case EM_IQ2000:
1516 rtype = elf_iq2000_reloc_type (type);
1517 break;
1518
1519 case EM_XTENSA_OLD:
1520 case EM_XTENSA:
1521 rtype = elf_xtensa_reloc_type (type);
1522 break;
1523
1524 case EM_LATTICEMICO32:
1525 rtype = elf_lm32_reloc_type (type);
1526 break;
1527
1528 case EM_M32C_OLD:
1529 case EM_M32C:
1530 rtype = elf_m32c_reloc_type (type);
1531 break;
1532
1533 case EM_MT:
1534 rtype = elf_mt_reloc_type (type);
1535 break;
1536
1537 case EM_BLACKFIN:
1538 rtype = elf_bfin_reloc_type (type);
1539 break;
1540
1541 case EM_CYGNUS_MEP:
1542 rtype = elf_mep_reloc_type (type);
1543 break;
1544
1545 case EM_CR16:
1546 rtype = elf_cr16_reloc_type (type);
1547 break;
1548
1549 case EM_MICROBLAZE:
1550 case EM_MICROBLAZE_OLD:
1551 rtype = elf_microblaze_reloc_type (type);
1552 break;
1553
1554 case EM_RL78:
1555 rtype = elf_rl78_reloc_type (type);
1556 break;
1557
1558 case EM_RX:
1559 rtype = elf_rx_reloc_type (type);
1560 break;
1561
1562 case EM_METAG:
1563 rtype = elf_metag_reloc_type (type);
1564 break;
1565
1566 case EM_XC16X:
1567 case EM_C166:
1568 rtype = elf_xc16x_reloc_type (type);
1569 break;
1570
1571 case EM_TI_C6000:
1572 rtype = elf_tic6x_reloc_type (type);
1573 break;
1574
1575 case EM_TILEGX:
1576 rtype = elf_tilegx_reloc_type (type);
1577 break;
1578
1579 case EM_TILEPRO:
1580 rtype = elf_tilepro_reloc_type (type);
1581 break;
1582
1583 case EM_WEBASSEMBLY:
1584 rtype = elf_wasm32_reloc_type (type);
1585 break;
1586
1587 case EM_XGATE:
1588 rtype = elf_xgate_reloc_type (type);
1589 break;
1590
1591 case EM_ALTERA_NIOS2:
1592 rtype = elf_nios2_reloc_type (type);
1593 break;
1594
1595 case EM_TI_PRU:
1596 rtype = elf_pru_reloc_type (type);
1597 break;
1598
1599 case EM_NFP:
1600 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1601 rtype = elf_nfp3200_reloc_type (type);
1602 else
1603 rtype = elf_nfp_reloc_type (type);
1604 break;
1605 }
1606
1607 if (rtype == NULL)
1608 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1609 else
1610 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1611
1612 if (filedata->file_header.e_machine == EM_ALPHA
1613 && rtype != NULL
1614 && streq (rtype, "R_ALPHA_LITUSE")
1615 && is_rela)
1616 {
1617 switch (rels[i].r_addend)
1618 {
1619 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1620 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1621 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1622 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1623 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1624 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1625 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1626 default: rtype = NULL;
1627 }
1628
1629 if (rtype)
1630 printf (" (%s)", rtype);
1631 else
1632 {
1633 putchar (' ');
1634 printf (_("<unknown addend: %lx>"),
1635 (unsigned long) rels[i].r_addend);
1636 res = FALSE;
1637 }
1638 }
1639 else if (symtab_index)
1640 {
1641 if (symtab == NULL || symtab_index >= nsyms)
1642 {
1643 error (_(" bad symbol index: %08lx in reloc"), (unsigned long) symtab_index);
1644 res = FALSE;
1645 }
1646 else
1647 {
1648 Elf_Internal_Sym * psym;
1649 const char * version_string;
1650 enum versioned_symbol_info sym_info;
1651 unsigned short vna_other;
1652
1653 psym = symtab + symtab_index;
1654
1655 version_string
1656 = get_symbol_version_string (filedata, is_dynsym,
1657 strtab, strtablen,
1658 symtab_index,
1659 psym,
1660 &sym_info,
1661 &vna_other);
1662
1663 printf (" ");
1664
1665 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1666 {
1667 const char * name;
1668 unsigned int len;
1669 unsigned int width = is_32bit_elf ? 8 : 14;
1670
1671 /* Relocations against GNU_IFUNC symbols do not use the value
1672 of the symbol as the address to relocate against. Instead
1673 they invoke the function named by the symbol and use its
1674 result as the address for relocation.
1675
1676 To indicate this to the user, do not display the value of
1677 the symbol in the "Symbols's Value" field. Instead show
1678 its name followed by () as a hint that the symbol is
1679 invoked. */
1680
1681 if (strtab == NULL
1682 || psym->st_name == 0
1683 || psym->st_name >= strtablen)
1684 name = "??";
1685 else
1686 name = strtab + psym->st_name;
1687
1688 len = print_symbol (width, name);
1689 if (version_string)
1690 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1691 version_string);
1692 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1693 }
1694 else
1695 {
1696 print_vma (psym->st_value, LONG_HEX);
1697
1698 printf (is_32bit_elf ? " " : " ");
1699 }
1700
1701 if (psym->st_name == 0)
1702 {
1703 const char * sec_name = "<null>";
1704 char name_buf[40];
1705
1706 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1707 {
1708 if (psym->st_shndx < filedata->file_header.e_shnum)
1709 sec_name = SECTION_NAME (filedata->section_headers + psym->st_shndx);
1710 else if (psym->st_shndx == SHN_ABS)
1711 sec_name = "ABS";
1712 else if (psym->st_shndx == SHN_COMMON)
1713 sec_name = "COMMON";
1714 else if ((filedata->file_header.e_machine == EM_MIPS
1715 && psym->st_shndx == SHN_MIPS_SCOMMON)
1716 || (filedata->file_header.e_machine == EM_TI_C6000
1717 && psym->st_shndx == SHN_TIC6X_SCOMMON))
1718 sec_name = "SCOMMON";
1719 else if (filedata->file_header.e_machine == EM_MIPS
1720 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1721 sec_name = "SUNDEF";
1722 else if ((filedata->file_header.e_machine == EM_X86_64
1723 || filedata->file_header.e_machine == EM_L1OM
1724 || filedata->file_header.e_machine == EM_K1OM)
1725 && psym->st_shndx == SHN_X86_64_LCOMMON)
1726 sec_name = "LARGE_COMMON";
1727 else if (filedata->file_header.e_machine == EM_IA_64
1728 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1729 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1730 sec_name = "ANSI_COM";
1731 else if (is_ia64_vms (filedata)
1732 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1733 sec_name = "VMS_SYMVEC";
1734 else
1735 {
1736 sprintf (name_buf, "<section 0x%x>",
1737 (unsigned int) psym->st_shndx);
1738 sec_name = name_buf;
1739 }
1740 }
1741 print_symbol (22, sec_name);
1742 }
1743 else if (strtab == NULL)
1744 printf (_("<string table index: %3ld>"), psym->st_name);
1745 else if (psym->st_name >= strtablen)
1746 {
1747 error (_("<corrupt string table index: %3ld>"), psym->st_name);
1748 res = FALSE;
1749 }
1750 else
1751 {
1752 print_symbol (22, strtab + psym->st_name);
1753 if (version_string)
1754 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1755 version_string);
1756 }
1757
1758 if (is_rela)
1759 {
1760 bfd_vma off = rels[i].r_addend;
1761
1762 if ((bfd_signed_vma) off < 0)
1763 printf (" - %" BFD_VMA_FMT "x", - off);
1764 else
1765 printf (" + %" BFD_VMA_FMT "x", off);
1766 }
1767 }
1768 }
1769 else if (is_rela)
1770 {
1771 bfd_vma off = rels[i].r_addend;
1772
1773 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1774 if ((bfd_signed_vma) off < 0)
1775 printf ("-%" BFD_VMA_FMT "x", - off);
1776 else
1777 printf ("%" BFD_VMA_FMT "x", off);
1778 }
1779
1780 if (filedata->file_header.e_machine == EM_SPARCV9
1781 && rtype != NULL
1782 && streq (rtype, "R_SPARC_OLO10"))
1783 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1784
1785 putchar ('\n');
1786
1787 #ifdef BFD64
1788 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
1789 {
1790 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1791 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1792 const char * rtype2 = elf_mips_reloc_type (type2);
1793 const char * rtype3 = elf_mips_reloc_type (type3);
1794
1795 printf (" Type2: ");
1796
1797 if (rtype2 == NULL)
1798 printf (_("unrecognized: %-7lx"),
1799 (unsigned long) type2 & 0xffffffff);
1800 else
1801 printf ("%-17.17s", rtype2);
1802
1803 printf ("\n Type3: ");
1804
1805 if (rtype3 == NULL)
1806 printf (_("unrecognized: %-7lx"),
1807 (unsigned long) type3 & 0xffffffff);
1808 else
1809 printf ("%-17.17s", rtype3);
1810
1811 putchar ('\n');
1812 }
1813 #endif /* BFD64 */
1814 }
1815
1816 free (rels);
1817
1818 return res;
1819 }
1820
1821 static const char *
1822 get_aarch64_dynamic_type (unsigned long type)
1823 {
1824 switch (type)
1825 {
1826 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1827 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
1828 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
1829 default:
1830 return NULL;
1831 }
1832 }
1833
1834 static const char *
1835 get_mips_dynamic_type (unsigned long type)
1836 {
1837 switch (type)
1838 {
1839 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1840 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1841 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1842 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1843 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1844 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1845 case DT_MIPS_MSYM: return "MIPS_MSYM";
1846 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1847 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1848 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1849 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1850 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1851 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1852 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1853 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1854 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1855 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1856 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1857 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1858 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1859 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1860 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1861 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1862 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1863 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1864 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1865 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1866 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1867 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1868 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1869 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1870 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1871 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1872 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1873 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1874 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1875 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1876 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1877 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1878 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1879 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1880 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1881 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1882 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1883 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1884 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1885 default:
1886 return NULL;
1887 }
1888 }
1889
1890 static const char *
1891 get_sparc64_dynamic_type (unsigned long type)
1892 {
1893 switch (type)
1894 {
1895 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1896 default:
1897 return NULL;
1898 }
1899 }
1900
1901 static const char *
1902 get_ppc_dynamic_type (unsigned long type)
1903 {
1904 switch (type)
1905 {
1906 case DT_PPC_GOT: return "PPC_GOT";
1907 case DT_PPC_OPT: return "PPC_OPT";
1908 default:
1909 return NULL;
1910 }
1911 }
1912
1913 static const char *
1914 get_ppc64_dynamic_type (unsigned long type)
1915 {
1916 switch (type)
1917 {
1918 case DT_PPC64_GLINK: return "PPC64_GLINK";
1919 case DT_PPC64_OPD: return "PPC64_OPD";
1920 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1921 case DT_PPC64_OPT: return "PPC64_OPT";
1922 default:
1923 return NULL;
1924 }
1925 }
1926
1927 static const char *
1928 get_parisc_dynamic_type (unsigned long type)
1929 {
1930 switch (type)
1931 {
1932 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1933 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1934 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1935 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1936 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1937 case DT_HP_PREINIT: return "HP_PREINIT";
1938 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1939 case DT_HP_NEEDED: return "HP_NEEDED";
1940 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1941 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1942 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1943 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1944 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
1945 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
1946 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
1947 case DT_HP_FILTERED: return "HP_FILTERED";
1948 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
1949 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1950 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
1951 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
1952 case DT_PLT: return "PLT";
1953 case DT_PLT_SIZE: return "PLT_SIZE";
1954 case DT_DLT: return "DLT";
1955 case DT_DLT_SIZE: return "DLT_SIZE";
1956 default:
1957 return NULL;
1958 }
1959 }
1960
1961 static const char *
1962 get_ia64_dynamic_type (unsigned long type)
1963 {
1964 switch (type)
1965 {
1966 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
1967 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
1968 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
1969 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
1970 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1971 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
1972 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
1973 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
1974 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
1975 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
1976 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
1977 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
1978 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
1979 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
1980 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
1981 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
1982 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
1983 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
1984 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
1985 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
1986 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
1987 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
1988 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
1989 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
1990 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
1991 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
1992 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
1993 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
1994 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
1995 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
1996 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
1997 default:
1998 return NULL;
1999 }
2000 }
2001
2002 static const char *
2003 get_solaris_section_type (unsigned long type)
2004 {
2005 switch (type)
2006 {
2007 case 0x6fffffee: return "SUNW_ancillary";
2008 case 0x6fffffef: return "SUNW_capchain";
2009 case 0x6ffffff0: return "SUNW_capinfo";
2010 case 0x6ffffff1: return "SUNW_symsort";
2011 case 0x6ffffff2: return "SUNW_tlssort";
2012 case 0x6ffffff3: return "SUNW_LDYNSYM";
2013 case 0x6ffffff4: return "SUNW_dof";
2014 case 0x6ffffff5: return "SUNW_cap";
2015 case 0x6ffffff6: return "SUNW_SIGNATURE";
2016 case 0x6ffffff7: return "SUNW_ANNOTATE";
2017 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2018 case 0x6ffffff9: return "SUNW_DEBUG";
2019 case 0x6ffffffa: return "SUNW_move";
2020 case 0x6ffffffb: return "SUNW_COMDAT";
2021 case 0x6ffffffc: return "SUNW_syminfo";
2022 case 0x6ffffffd: return "SUNW_verdef";
2023 case 0x6ffffffe: return "SUNW_verneed";
2024 case 0x6fffffff: return "SUNW_versym";
2025 case 0x70000000: return "SPARC_GOTDATA";
2026 default: return NULL;
2027 }
2028 }
2029
2030 static const char *
2031 get_alpha_dynamic_type (unsigned long type)
2032 {
2033 switch (type)
2034 {
2035 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2036 default: return NULL;
2037 }
2038 }
2039
2040 static const char *
2041 get_score_dynamic_type (unsigned long type)
2042 {
2043 switch (type)
2044 {
2045 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2046 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2047 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2048 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2049 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2050 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2051 default: return NULL;
2052 }
2053 }
2054
2055 static const char *
2056 get_tic6x_dynamic_type (unsigned long type)
2057 {
2058 switch (type)
2059 {
2060 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2061 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2062 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2063 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2064 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2065 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2066 default: return NULL;
2067 }
2068 }
2069
2070 static const char *
2071 get_nios2_dynamic_type (unsigned long type)
2072 {
2073 switch (type)
2074 {
2075 case DT_NIOS2_GP: return "NIOS2_GP";
2076 default: return NULL;
2077 }
2078 }
2079
2080 static const char *
2081 get_solaris_dynamic_type (unsigned long type)
2082 {
2083 switch (type)
2084 {
2085 case 0x6000000d: return "SUNW_AUXILIARY";
2086 case 0x6000000e: return "SUNW_RTLDINF";
2087 case 0x6000000f: return "SUNW_FILTER";
2088 case 0x60000010: return "SUNW_CAP";
2089 case 0x60000011: return "SUNW_SYMTAB";
2090 case 0x60000012: return "SUNW_SYMSZ";
2091 case 0x60000013: return "SUNW_SORTENT";
2092 case 0x60000014: return "SUNW_SYMSORT";
2093 case 0x60000015: return "SUNW_SYMSORTSZ";
2094 case 0x60000016: return "SUNW_TLSSORT";
2095 case 0x60000017: return "SUNW_TLSSORTSZ";
2096 case 0x60000018: return "SUNW_CAPINFO";
2097 case 0x60000019: return "SUNW_STRPAD";
2098 case 0x6000001a: return "SUNW_CAPCHAIN";
2099 case 0x6000001b: return "SUNW_LDMACH";
2100 case 0x6000001d: return "SUNW_CAPCHAINENT";
2101 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2102 case 0x60000021: return "SUNW_PARENT";
2103 case 0x60000023: return "SUNW_ASLR";
2104 case 0x60000025: return "SUNW_RELAX";
2105 case 0x60000029: return "SUNW_NXHEAP";
2106 case 0x6000002b: return "SUNW_NXSTACK";
2107
2108 case 0x70000001: return "SPARC_REGISTER";
2109 case 0x7ffffffd: return "AUXILIARY";
2110 case 0x7ffffffe: return "USED";
2111 case 0x7fffffff: return "FILTER";
2112
2113 default: return NULL;
2114 }
2115 }
2116
2117 static const char *
2118 get_dynamic_type (Filedata * filedata, unsigned long type)
2119 {
2120 static char buff[64];
2121
2122 switch (type)
2123 {
2124 case DT_NULL: return "NULL";
2125 case DT_NEEDED: return "NEEDED";
2126 case DT_PLTRELSZ: return "PLTRELSZ";
2127 case DT_PLTGOT: return "PLTGOT";
2128 case DT_HASH: return "HASH";
2129 case DT_STRTAB: return "STRTAB";
2130 case DT_SYMTAB: return "SYMTAB";
2131 case DT_RELA: return "RELA";
2132 case DT_RELASZ: return "RELASZ";
2133 case DT_RELAENT: return "RELAENT";
2134 case DT_STRSZ: return "STRSZ";
2135 case DT_SYMENT: return "SYMENT";
2136 case DT_INIT: return "INIT";
2137 case DT_FINI: return "FINI";
2138 case DT_SONAME: return "SONAME";
2139 case DT_RPATH: return "RPATH";
2140 case DT_SYMBOLIC: return "SYMBOLIC";
2141 case DT_REL: return "REL";
2142 case DT_RELSZ: return "RELSZ";
2143 case DT_RELENT: return "RELENT";
2144 case DT_PLTREL: return "PLTREL";
2145 case DT_DEBUG: return "DEBUG";
2146 case DT_TEXTREL: return "TEXTREL";
2147 case DT_JMPREL: return "JMPREL";
2148 case DT_BIND_NOW: return "BIND_NOW";
2149 case DT_INIT_ARRAY: return "INIT_ARRAY";
2150 case DT_FINI_ARRAY: return "FINI_ARRAY";
2151 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2152 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2153 case DT_RUNPATH: return "RUNPATH";
2154 case DT_FLAGS: return "FLAGS";
2155
2156 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2157 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2158 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2159
2160 case DT_CHECKSUM: return "CHECKSUM";
2161 case DT_PLTPADSZ: return "PLTPADSZ";
2162 case DT_MOVEENT: return "MOVEENT";
2163 case DT_MOVESZ: return "MOVESZ";
2164 case DT_FEATURE: return "FEATURE";
2165 case DT_POSFLAG_1: return "POSFLAG_1";
2166 case DT_SYMINSZ: return "SYMINSZ";
2167 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2168
2169 case DT_ADDRRNGLO: return "ADDRRNGLO";
2170 case DT_CONFIG: return "CONFIG";
2171 case DT_DEPAUDIT: return "DEPAUDIT";
2172 case DT_AUDIT: return "AUDIT";
2173 case DT_PLTPAD: return "PLTPAD";
2174 case DT_MOVETAB: return "MOVETAB";
2175 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2176
2177 case DT_VERSYM: return "VERSYM";
2178
2179 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2180 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2181 case DT_RELACOUNT: return "RELACOUNT";
2182 case DT_RELCOUNT: return "RELCOUNT";
2183 case DT_FLAGS_1: return "FLAGS_1";
2184 case DT_VERDEF: return "VERDEF";
2185 case DT_VERDEFNUM: return "VERDEFNUM";
2186 case DT_VERNEED: return "VERNEED";
2187 case DT_VERNEEDNUM: return "VERNEEDNUM";
2188
2189 case DT_AUXILIARY: return "AUXILIARY";
2190 case DT_USED: return "USED";
2191 case DT_FILTER: return "FILTER";
2192
2193 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2194 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2195 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2196 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2197 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2198 case DT_GNU_HASH: return "GNU_HASH";
2199
2200 default:
2201 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2202 {
2203 const char * result;
2204
2205 switch (filedata->file_header.e_machine)
2206 {
2207 case EM_AARCH64:
2208 result = get_aarch64_dynamic_type (type);
2209 break;
2210 case EM_MIPS:
2211 case EM_MIPS_RS3_LE:
2212 result = get_mips_dynamic_type (type);
2213 break;
2214 case EM_SPARCV9:
2215 result = get_sparc64_dynamic_type (type);
2216 break;
2217 case EM_PPC:
2218 result = get_ppc_dynamic_type (type);
2219 break;
2220 case EM_PPC64:
2221 result = get_ppc64_dynamic_type (type);
2222 break;
2223 case EM_IA_64:
2224 result = get_ia64_dynamic_type (type);
2225 break;
2226 case EM_ALPHA:
2227 result = get_alpha_dynamic_type (type);
2228 break;
2229 case EM_SCORE:
2230 result = get_score_dynamic_type (type);
2231 break;
2232 case EM_TI_C6000:
2233 result = get_tic6x_dynamic_type (type);
2234 break;
2235 case EM_ALTERA_NIOS2:
2236 result = get_nios2_dynamic_type (type);
2237 break;
2238 default:
2239 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2240 result = get_solaris_dynamic_type (type);
2241 else
2242 result = NULL;
2243 break;
2244 }
2245
2246 if (result != NULL)
2247 return result;
2248
2249 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2250 }
2251 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2252 || (filedata->file_header.e_machine == EM_PARISC
2253 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2254 {
2255 const char * result;
2256
2257 switch (filedata->file_header.e_machine)
2258 {
2259 case EM_PARISC:
2260 result = get_parisc_dynamic_type (type);
2261 break;
2262 case EM_IA_64:
2263 result = get_ia64_dynamic_type (type);
2264 break;
2265 default:
2266 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2267 result = get_solaris_dynamic_type (type);
2268 else
2269 result = NULL;
2270 break;
2271 }
2272
2273 if (result != NULL)
2274 return result;
2275
2276 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2277 type);
2278 }
2279 else
2280 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2281
2282 return buff;
2283 }
2284 }
2285
2286 static char *
2287 get_file_type (unsigned e_type)
2288 {
2289 static char buff[32];
2290
2291 switch (e_type)
2292 {
2293 case ET_NONE: return _("NONE (None)");
2294 case ET_REL: return _("REL (Relocatable file)");
2295 case ET_EXEC: return _("EXEC (Executable file)");
2296 case ET_DYN: return _("DYN (Shared object file)");
2297 case ET_CORE: return _("CORE (Core file)");
2298
2299 default:
2300 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2301 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2302 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2303 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2304 else
2305 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2306 return buff;
2307 }
2308 }
2309
2310 static char *
2311 get_machine_name (unsigned e_machine)
2312 {
2313 static char buff[64]; /* XXX */
2314
2315 switch (e_machine)
2316 {
2317 /* Please keep this switch table sorted by increasing EM_ value. */
2318 /* 0 */
2319 case EM_NONE: return _("None");
2320 case EM_M32: return "WE32100";
2321 case EM_SPARC: return "Sparc";
2322 case EM_386: return "Intel 80386";
2323 case EM_68K: return "MC68000";
2324 case EM_88K: return "MC88000";
2325 case EM_IAMCU: return "Intel MCU";
2326 case EM_860: return "Intel 80860";
2327 case EM_MIPS: return "MIPS R3000";
2328 case EM_S370: return "IBM System/370";
2329 /* 10 */
2330 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2331 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2332 case EM_PARISC: return "HPPA";
2333 case EM_VPP550: return "Fujitsu VPP500";
2334 case EM_SPARC32PLUS: return "Sparc v8+" ;
2335 case EM_960: return "Intel 80960";
2336 case EM_PPC: return "PowerPC";
2337 /* 20 */
2338 case EM_PPC64: return "PowerPC64";
2339 case EM_S390_OLD:
2340 case EM_S390: return "IBM S/390";
2341 case EM_SPU: return "SPU";
2342 /* 30 */
2343 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2344 case EM_FR20: return "Fujitsu FR20";
2345 case EM_RH32: return "TRW RH32";
2346 case EM_MCORE: return "MCORE";
2347 /* 40 */
2348 case EM_ARM: return "ARM";
2349 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2350 case EM_SH: return "Renesas / SuperH SH";
2351 case EM_SPARCV9: return "Sparc v9";
2352 case EM_TRICORE: return "Siemens Tricore";
2353 case EM_ARC: return "ARC";
2354 case EM_H8_300: return "Renesas H8/300";
2355 case EM_H8_300H: return "Renesas H8/300H";
2356 case EM_H8S: return "Renesas H8S";
2357 case EM_H8_500: return "Renesas H8/500";
2358 /* 50 */
2359 case EM_IA_64: return "Intel IA-64";
2360 case EM_MIPS_X: return "Stanford MIPS-X";
2361 case EM_COLDFIRE: return "Motorola Coldfire";
2362 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2363 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2364 case EM_PCP: return "Siemens PCP";
2365 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2366 case EM_NDR1: return "Denso NDR1 microprocesspr";
2367 case EM_STARCORE: return "Motorola Star*Core processor";
2368 case EM_ME16: return "Toyota ME16 processor";
2369 /* 60 */
2370 case EM_ST100: return "STMicroelectronics ST100 processor";
2371 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2372 case EM_X86_64: return "Advanced Micro Devices X86-64";
2373 case EM_PDSP: return "Sony DSP processor";
2374 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2375 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2376 case EM_FX66: return "Siemens FX66 microcontroller";
2377 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2378 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2379 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2380 /* 70 */
2381 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2382 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2383 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2384 case EM_SVX: return "Silicon Graphics SVx";
2385 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2386 case EM_VAX: return "Digital VAX";
2387 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2388 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2389 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2390 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2391 /* 80 */
2392 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2393 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2394 case EM_PRISM: return "Vitesse Prism";
2395 case EM_AVR_OLD:
2396 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2397 case EM_CYGNUS_FR30:
2398 case EM_FR30: return "Fujitsu FR30";
2399 case EM_CYGNUS_D10V:
2400 case EM_D10V: return "d10v";
2401 case EM_CYGNUS_D30V:
2402 case EM_D30V: return "d30v";
2403 case EM_CYGNUS_V850:
2404 case EM_V850: return "Renesas V850";
2405 case EM_CYGNUS_M32R:
2406 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2407 case EM_CYGNUS_MN10300:
2408 case EM_MN10300: return "mn10300";
2409 /* 90 */
2410 case EM_CYGNUS_MN10200:
2411 case EM_MN10200: return "mn10200";
2412 case EM_PJ: return "picoJava";
2413 case EM_OR1K: return "OpenRISC 1000";
2414 case EM_ARC_COMPACT: return "ARCompact";
2415 case EM_XTENSA_OLD:
2416 case EM_XTENSA: return "Tensilica Xtensa Processor";
2417 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2418 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2419 case EM_NS32K: return "National Semiconductor 32000 series";
2420 case EM_TPC: return "Tenor Network TPC processor";
2421 case EM_SNP1K: return "Trebia SNP 1000 processor";
2422 /* 100 */
2423 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2424 case EM_IP2K_OLD:
2425 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2426 case EM_MAX: return "MAX Processor";
2427 case EM_CR: return "National Semiconductor CompactRISC";
2428 case EM_F2MC16: return "Fujitsu F2MC16";
2429 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2430 case EM_BLACKFIN: return "Analog Devices Blackfin";
2431 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2432 case EM_SEP: return "Sharp embedded microprocessor";
2433 case EM_ARCA: return "Arca RISC microprocessor";
2434 /* 110 */
2435 case EM_UNICORE: return "Unicore";
2436 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2437 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2438 case EM_ALTERA_NIOS2: return "Altera Nios II";
2439 case EM_CRX: return "National Semiconductor CRX microprocessor";
2440 case EM_XGATE: return "Motorola XGATE embedded processor";
2441 case EM_C166:
2442 case EM_XC16X: return "Infineon Technologies xc16x";
2443 case EM_M16C: return "Renesas M16C series microprocessors";
2444 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2445 case EM_CE: return "Freescale Communication Engine RISC core";
2446 /* 120 */
2447 case EM_M32C: return "Renesas M32c";
2448 /* 130 */
2449 case EM_TSK3000: return "Altium TSK3000 core";
2450 case EM_RS08: return "Freescale RS08 embedded processor";
2451 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2452 case EM_SCORE: return "SUNPLUS S+Core";
2453 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2454 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2455 case EM_LATTICEMICO32: return "Lattice Mico32";
2456 case EM_SE_C17: return "Seiko Epson C17 family";
2457 /* 140 */
2458 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2459 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2460 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2461 case EM_TI_PRU: return "TI PRU I/O processor";
2462 /* 160 */
2463 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2464 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2465 case EM_R32C: return "Renesas R32C series microprocessors";
2466 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2467 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2468 case EM_8051: return "Intel 8051 and variants";
2469 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2470 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2471 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2472 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2473 /* 170 */
2474 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2475 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2476 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2477 case EM_RX: return "Renesas RX";
2478 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2479 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2480 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2481 case EM_CR16:
2482 case EM_MICROBLAZE:
2483 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2484 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2485 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2486 /* 180 */
2487 case EM_L1OM: return "Intel L1OM";
2488 case EM_K1OM: return "Intel K1OM";
2489 case EM_INTEL182: return "Intel (reserved)";
2490 case EM_AARCH64: return "AArch64";
2491 case EM_ARM184: return "ARM (reserved)";
2492 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2493 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2494 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2495 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2496 /* 190 */
2497 case EM_CUDA: return "NVIDIA CUDA architecture";
2498 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2499 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2500 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2501 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2502 case EM_ARC_COMPACT2: return "ARCv2";
2503 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2504 case EM_RL78: return "Renesas RL78";
2505 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2506 case EM_78K0R: return "Renesas 78K0R";
2507 /* 200 */
2508 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2509 case EM_BA1: return "Beyond BA1 CPU architecture";
2510 case EM_BA2: return "Beyond BA2 CPU architecture";
2511 case EM_XCORE: return "XMOS xCORE processor family";
2512 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2513 /* 210 */
2514 case EM_KM32: return "KM211 KM32 32-bit processor";
2515 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2516 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2517 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2518 case EM_KVARC: return "KM211 KVARC processor";
2519 case EM_CDP: return "Paneve CDP architecture family";
2520 case EM_COGE: return "Cognitive Smart Memory Processor";
2521 case EM_COOL: return "Bluechip Systems CoolEngine";
2522 case EM_NORC: return "Nanoradio Optimized RISC";
2523 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2524 /* 220 */
2525 case EM_Z80: return "Zilog Z80";
2526 case EM_VISIUM: return "CDS VISIUMcore processor";
2527 case EM_FT32: return "FTDI Chip FT32";
2528 case EM_MOXIE: return "Moxie";
2529 case EM_AMDGPU: return "AMD GPU";
2530 case EM_RISCV: return "RISC-V";
2531 case EM_LANAI: return "Lanai 32-bit processor";
2532 case EM_BPF: return "Linux BPF";
2533 case EM_NFP: return "Netronome Flow Processor";
2534
2535 /* Large numbers... */
2536 case EM_MT: return "Morpho Techologies MT processor";
2537 case EM_ALPHA: return "Alpha";
2538 case EM_WEBASSEMBLY: return "Web Assembly";
2539 case EM_DLX: return "OpenDLX";
2540 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2541 case EM_IQ2000: return "Vitesse IQ2000";
2542 case EM_M32C_OLD:
2543 case EM_NIOS32: return "Altera Nios";
2544 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2545 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2546 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2547 case EM_S12Z: return "Freescale S12Z";
2548 case EM_CSKY: return "C-SKY";
2549
2550 default:
2551 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2552 return buff;
2553 }
2554 }
2555
2556 static void
2557 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2558 {
2559 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2560 other compilers don't a specific architecture type in the e_flags, and
2561 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2562 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2563 architectures.
2564
2565 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2566 but also sets a specific architecture type in the e_flags field.
2567
2568 However, when decoding the flags we don't worry if we see an
2569 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2570 ARCEM architecture type. */
2571
2572 switch (e_flags & EF_ARC_MACH_MSK)
2573 {
2574 /* We only expect these to occur for EM_ARC_COMPACT2. */
2575 case EF_ARC_CPU_ARCV2EM:
2576 strcat (buf, ", ARC EM");
2577 break;
2578 case EF_ARC_CPU_ARCV2HS:
2579 strcat (buf, ", ARC HS");
2580 break;
2581
2582 /* We only expect these to occur for EM_ARC_COMPACT. */
2583 case E_ARC_MACH_ARC600:
2584 strcat (buf, ", ARC600");
2585 break;
2586 case E_ARC_MACH_ARC601:
2587 strcat (buf, ", ARC601");
2588 break;
2589 case E_ARC_MACH_ARC700:
2590 strcat (buf, ", ARC700");
2591 break;
2592
2593 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2594 new ELF with new architecture being read by an old version of
2595 readelf, or (c) An ELF built with non-GNU compiler that does not
2596 set the architecture in the e_flags. */
2597 default:
2598 if (e_machine == EM_ARC_COMPACT)
2599 strcat (buf, ", Unknown ARCompact");
2600 else
2601 strcat (buf, ", Unknown ARC");
2602 break;
2603 }
2604
2605 switch (e_flags & EF_ARC_OSABI_MSK)
2606 {
2607 case E_ARC_OSABI_ORIG:
2608 strcat (buf, ", (ABI:legacy)");
2609 break;
2610 case E_ARC_OSABI_V2:
2611 strcat (buf, ", (ABI:v2)");
2612 break;
2613 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2614 case E_ARC_OSABI_V3:
2615 strcat (buf, ", v3 no-legacy-syscalls ABI");
2616 break;
2617 case E_ARC_OSABI_V4:
2618 strcat (buf, ", v4 ABI");
2619 break;
2620 default:
2621 strcat (buf, ", unrecognised ARC OSABI flag");
2622 break;
2623 }
2624 }
2625
2626 static void
2627 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2628 {
2629 unsigned eabi;
2630 bfd_boolean unknown = FALSE;
2631
2632 eabi = EF_ARM_EABI_VERSION (e_flags);
2633 e_flags &= ~ EF_ARM_EABIMASK;
2634
2635 /* Handle "generic" ARM flags. */
2636 if (e_flags & EF_ARM_RELEXEC)
2637 {
2638 strcat (buf, ", relocatable executable");
2639 e_flags &= ~ EF_ARM_RELEXEC;
2640 }
2641
2642 if (e_flags & EF_ARM_PIC)
2643 {
2644 strcat (buf, ", position independent");
2645 e_flags &= ~ EF_ARM_PIC;
2646 }
2647
2648 /* Now handle EABI specific flags. */
2649 switch (eabi)
2650 {
2651 default:
2652 strcat (buf, ", <unrecognized EABI>");
2653 if (e_flags)
2654 unknown = TRUE;
2655 break;
2656
2657 case EF_ARM_EABI_VER1:
2658 strcat (buf, ", Version1 EABI");
2659 while (e_flags)
2660 {
2661 unsigned flag;
2662
2663 /* Process flags one bit at a time. */
2664 flag = e_flags & - e_flags;
2665 e_flags &= ~ flag;
2666
2667 switch (flag)
2668 {
2669 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2670 strcat (buf, ", sorted symbol tables");
2671 break;
2672
2673 default:
2674 unknown = TRUE;
2675 break;
2676 }
2677 }
2678 break;
2679
2680 case EF_ARM_EABI_VER2:
2681 strcat (buf, ", Version2 EABI");
2682 while (e_flags)
2683 {
2684 unsigned flag;
2685
2686 /* Process flags one bit at a time. */
2687 flag = e_flags & - e_flags;
2688 e_flags &= ~ flag;
2689
2690 switch (flag)
2691 {
2692 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2693 strcat (buf, ", sorted symbol tables");
2694 break;
2695
2696 case EF_ARM_DYNSYMSUSESEGIDX:
2697 strcat (buf, ", dynamic symbols use segment index");
2698 break;
2699
2700 case EF_ARM_MAPSYMSFIRST:
2701 strcat (buf, ", mapping symbols precede others");
2702 break;
2703
2704 default:
2705 unknown = TRUE;
2706 break;
2707 }
2708 }
2709 break;
2710
2711 case EF_ARM_EABI_VER3:
2712 strcat (buf, ", Version3 EABI");
2713 break;
2714
2715 case EF_ARM_EABI_VER4:
2716 strcat (buf, ", Version4 EABI");
2717 while (e_flags)
2718 {
2719 unsigned flag;
2720
2721 /* Process flags one bit at a time. */
2722 flag = e_flags & - e_flags;
2723 e_flags &= ~ flag;
2724
2725 switch (flag)
2726 {
2727 case EF_ARM_BE8:
2728 strcat (buf, ", BE8");
2729 break;
2730
2731 case EF_ARM_LE8:
2732 strcat (buf, ", LE8");
2733 break;
2734
2735 default:
2736 unknown = TRUE;
2737 break;
2738 }
2739 }
2740 break;
2741
2742 case EF_ARM_EABI_VER5:
2743 strcat (buf, ", Version5 EABI");
2744 while (e_flags)
2745 {
2746 unsigned flag;
2747
2748 /* Process flags one bit at a time. */
2749 flag = e_flags & - e_flags;
2750 e_flags &= ~ flag;
2751
2752 switch (flag)
2753 {
2754 case EF_ARM_BE8:
2755 strcat (buf, ", BE8");
2756 break;
2757
2758 case EF_ARM_LE8:
2759 strcat (buf, ", LE8");
2760 break;
2761
2762 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
2763 strcat (buf, ", soft-float ABI");
2764 break;
2765
2766 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
2767 strcat (buf, ", hard-float ABI");
2768 break;
2769
2770 default:
2771 unknown = TRUE;
2772 break;
2773 }
2774 }
2775 break;
2776
2777 case EF_ARM_EABI_UNKNOWN:
2778 strcat (buf, ", GNU EABI");
2779 while (e_flags)
2780 {
2781 unsigned flag;
2782
2783 /* Process flags one bit at a time. */
2784 flag = e_flags & - e_flags;
2785 e_flags &= ~ flag;
2786
2787 switch (flag)
2788 {
2789 case EF_ARM_INTERWORK:
2790 strcat (buf, ", interworking enabled");
2791 break;
2792
2793 case EF_ARM_APCS_26:
2794 strcat (buf, ", uses APCS/26");
2795 break;
2796
2797 case EF_ARM_APCS_FLOAT:
2798 strcat (buf, ", uses APCS/float");
2799 break;
2800
2801 case EF_ARM_PIC:
2802 strcat (buf, ", position independent");
2803 break;
2804
2805 case EF_ARM_ALIGN8:
2806 strcat (buf, ", 8 bit structure alignment");
2807 break;
2808
2809 case EF_ARM_NEW_ABI:
2810 strcat (buf, ", uses new ABI");
2811 break;
2812
2813 case EF_ARM_OLD_ABI:
2814 strcat (buf, ", uses old ABI");
2815 break;
2816
2817 case EF_ARM_SOFT_FLOAT:
2818 strcat (buf, ", software FP");
2819 break;
2820
2821 case EF_ARM_VFP_FLOAT:
2822 strcat (buf, ", VFP");
2823 break;
2824
2825 case EF_ARM_MAVERICK_FLOAT:
2826 strcat (buf, ", Maverick FP");
2827 break;
2828
2829 default:
2830 unknown = TRUE;
2831 break;
2832 }
2833 }
2834 }
2835
2836 if (unknown)
2837 strcat (buf,_(", <unknown>"));
2838 }
2839
2840 static void
2841 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2842 {
2843 --size; /* Leave space for null terminator. */
2844
2845 switch (e_flags & EF_AVR_MACH)
2846 {
2847 case E_AVR_MACH_AVR1:
2848 strncat (buf, ", avr:1", size);
2849 break;
2850 case E_AVR_MACH_AVR2:
2851 strncat (buf, ", avr:2", size);
2852 break;
2853 case E_AVR_MACH_AVR25:
2854 strncat (buf, ", avr:25", size);
2855 break;
2856 case E_AVR_MACH_AVR3:
2857 strncat (buf, ", avr:3", size);
2858 break;
2859 case E_AVR_MACH_AVR31:
2860 strncat (buf, ", avr:31", size);
2861 break;
2862 case E_AVR_MACH_AVR35:
2863 strncat (buf, ", avr:35", size);
2864 break;
2865 case E_AVR_MACH_AVR4:
2866 strncat (buf, ", avr:4", size);
2867 break;
2868 case E_AVR_MACH_AVR5:
2869 strncat (buf, ", avr:5", size);
2870 break;
2871 case E_AVR_MACH_AVR51:
2872 strncat (buf, ", avr:51", size);
2873 break;
2874 case E_AVR_MACH_AVR6:
2875 strncat (buf, ", avr:6", size);
2876 break;
2877 case E_AVR_MACH_AVRTINY:
2878 strncat (buf, ", avr:100", size);
2879 break;
2880 case E_AVR_MACH_XMEGA1:
2881 strncat (buf, ", avr:101", size);
2882 break;
2883 case E_AVR_MACH_XMEGA2:
2884 strncat (buf, ", avr:102", size);
2885 break;
2886 case E_AVR_MACH_XMEGA3:
2887 strncat (buf, ", avr:103", size);
2888 break;
2889 case E_AVR_MACH_XMEGA4:
2890 strncat (buf, ", avr:104", size);
2891 break;
2892 case E_AVR_MACH_XMEGA5:
2893 strncat (buf, ", avr:105", size);
2894 break;
2895 case E_AVR_MACH_XMEGA6:
2896 strncat (buf, ", avr:106", size);
2897 break;
2898 case E_AVR_MACH_XMEGA7:
2899 strncat (buf, ", avr:107", size);
2900 break;
2901 default:
2902 strncat (buf, ", avr:<unknown>", size);
2903 break;
2904 }
2905
2906 size -= strlen (buf);
2907 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2908 strncat (buf, ", link-relax", size);
2909 }
2910
2911 static void
2912 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2913 {
2914 unsigned abi;
2915 unsigned arch;
2916 unsigned config;
2917 unsigned version;
2918 bfd_boolean has_fpu = FALSE;
2919 unsigned int r = 0;
2920
2921 static const char *ABI_STRINGS[] =
2922 {
2923 "ABI v0", /* use r5 as return register; only used in N1213HC */
2924 "ABI v1", /* use r0 as return register */
2925 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2926 "ABI v2fp", /* for FPU */
2927 "AABI",
2928 "ABI2 FP+"
2929 };
2930 static const char *VER_STRINGS[] =
2931 {
2932 "Andes ELF V1.3 or older",
2933 "Andes ELF V1.3.1",
2934 "Andes ELF V1.4"
2935 };
2936 static const char *ARCH_STRINGS[] =
2937 {
2938 "",
2939 "Andes Star v1.0",
2940 "Andes Star v2.0",
2941 "Andes Star v3.0",
2942 "Andes Star v3.0m"
2943 };
2944
2945 abi = EF_NDS_ABI & e_flags;
2946 arch = EF_NDS_ARCH & e_flags;
2947 config = EF_NDS_INST & e_flags;
2948 version = EF_NDS32_ELF_VERSION & e_flags;
2949
2950 memset (buf, 0, size);
2951
2952 switch (abi)
2953 {
2954 case E_NDS_ABI_V0:
2955 case E_NDS_ABI_V1:
2956 case E_NDS_ABI_V2:
2957 case E_NDS_ABI_V2FP:
2958 case E_NDS_ABI_AABI:
2959 case E_NDS_ABI_V2FP_PLUS:
2960 /* In case there are holes in the array. */
2961 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
2962 break;
2963
2964 default:
2965 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
2966 break;
2967 }
2968
2969 switch (version)
2970 {
2971 case E_NDS32_ELF_VER_1_2:
2972 case E_NDS32_ELF_VER_1_3:
2973 case E_NDS32_ELF_VER_1_4:
2974 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
2975 break;
2976
2977 default:
2978 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
2979 break;
2980 }
2981
2982 if (E_NDS_ABI_V0 == abi)
2983 {
2984 /* OLD ABI; only used in N1213HC, has performance extension 1. */
2985 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
2986 if (arch == E_NDS_ARCH_STAR_V1_0)
2987 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
2988 return;
2989 }
2990
2991 switch (arch)
2992 {
2993 case E_NDS_ARCH_STAR_V1_0:
2994 case E_NDS_ARCH_STAR_V2_0:
2995 case E_NDS_ARCH_STAR_V3_0:
2996 case E_NDS_ARCH_STAR_V3_M:
2997 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
2998 break;
2999
3000 default:
3001 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3002 /* ARCH version determines how the e_flags are interpreted.
3003 If it is unknown, we cannot proceed. */
3004 return;
3005 }
3006
3007 /* Newer ABI; Now handle architecture specific flags. */
3008 if (arch == E_NDS_ARCH_STAR_V1_0)
3009 {
3010 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3011 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3012
3013 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3014 r += snprintf (buf + r, size -r, ", MAC");
3015
3016 if (config & E_NDS32_HAS_DIV_INST)
3017 r += snprintf (buf + r, size -r, ", DIV");
3018
3019 if (config & E_NDS32_HAS_16BIT_INST)
3020 r += snprintf (buf + r, size -r, ", 16b");
3021 }
3022 else
3023 {
3024 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3025 {
3026 if (version <= E_NDS32_ELF_VER_1_3)
3027 r += snprintf (buf + r, size -r, ", [B8]");
3028 else
3029 r += snprintf (buf + r, size -r, ", EX9");
3030 }
3031
3032 if (config & E_NDS32_HAS_MAC_DX_INST)
3033 r += snprintf (buf + r, size -r, ", MAC_DX");
3034
3035 if (config & E_NDS32_HAS_DIV_DX_INST)
3036 r += snprintf (buf + r, size -r, ", DIV_DX");
3037
3038 if (config & E_NDS32_HAS_16BIT_INST)
3039 {
3040 if (version <= E_NDS32_ELF_VER_1_3)
3041 r += snprintf (buf + r, size -r, ", 16b");
3042 else
3043 r += snprintf (buf + r, size -r, ", IFC");
3044 }
3045 }
3046
3047 if (config & E_NDS32_HAS_EXT_INST)
3048 r += snprintf (buf + r, size -r, ", PERF1");
3049
3050 if (config & E_NDS32_HAS_EXT2_INST)
3051 r += snprintf (buf + r, size -r, ", PERF2");
3052
3053 if (config & E_NDS32_HAS_FPU_INST)
3054 {
3055 has_fpu = TRUE;
3056 r += snprintf (buf + r, size -r, ", FPU_SP");
3057 }
3058
3059 if (config & E_NDS32_HAS_FPU_DP_INST)
3060 {
3061 has_fpu = TRUE;
3062 r += snprintf (buf + r, size -r, ", FPU_DP");
3063 }
3064
3065 if (config & E_NDS32_HAS_FPU_MAC_INST)
3066 {
3067 has_fpu = TRUE;
3068 r += snprintf (buf + r, size -r, ", FPU_MAC");
3069 }
3070
3071 if (has_fpu)
3072 {
3073 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3074 {
3075 case E_NDS32_FPU_REG_8SP_4DP:
3076 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3077 break;
3078 case E_NDS32_FPU_REG_16SP_8DP:
3079 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3080 break;
3081 case E_NDS32_FPU_REG_32SP_16DP:
3082 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3083 break;
3084 case E_NDS32_FPU_REG_32SP_32DP:
3085 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3086 break;
3087 }
3088 }
3089
3090 if (config & E_NDS32_HAS_AUDIO_INST)
3091 r += snprintf (buf + r, size -r, ", AUDIO");
3092
3093 if (config & E_NDS32_HAS_STRING_INST)
3094 r += snprintf (buf + r, size -r, ", STR");
3095
3096 if (config & E_NDS32_HAS_REDUCED_REGS)
3097 r += snprintf (buf + r, size -r, ", 16REG");
3098
3099 if (config & E_NDS32_HAS_VIDEO_INST)
3100 {
3101 if (version <= E_NDS32_ELF_VER_1_3)
3102 r += snprintf (buf + r, size -r, ", VIDEO");
3103 else
3104 r += snprintf (buf + r, size -r, ", SATURATION");
3105 }
3106
3107 if (config & E_NDS32_HAS_ENCRIPT_INST)
3108 r += snprintf (buf + r, size -r, ", ENCRP");
3109
3110 if (config & E_NDS32_HAS_L2C_INST)
3111 r += snprintf (buf + r, size -r, ", L2C");
3112 }
3113
3114 static char *
3115 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3116 {
3117 static char buf[1024];
3118
3119 buf[0] = '\0';
3120
3121 if (e_flags)
3122 {
3123 switch (e_machine)
3124 {
3125 default:
3126 break;
3127
3128 case EM_ARC_COMPACT2:
3129 case EM_ARC_COMPACT:
3130 decode_ARC_machine_flags (e_flags, e_machine, buf);
3131 break;
3132
3133 case EM_ARM:
3134 decode_ARM_machine_flags (e_flags, buf);
3135 break;
3136
3137 case EM_AVR:
3138 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3139 break;
3140
3141 case EM_BLACKFIN:
3142 if (e_flags & EF_BFIN_PIC)
3143 strcat (buf, ", PIC");
3144
3145 if (e_flags & EF_BFIN_FDPIC)
3146 strcat (buf, ", FDPIC");
3147
3148 if (e_flags & EF_BFIN_CODE_IN_L1)
3149 strcat (buf, ", code in L1");
3150
3151 if (e_flags & EF_BFIN_DATA_IN_L1)
3152 strcat (buf, ", data in L1");
3153
3154 break;
3155
3156 case EM_CYGNUS_FRV:
3157 switch (e_flags & EF_FRV_CPU_MASK)
3158 {
3159 case EF_FRV_CPU_GENERIC:
3160 break;
3161
3162 default:
3163 strcat (buf, ", fr???");
3164 break;
3165
3166 case EF_FRV_CPU_FR300:
3167 strcat (buf, ", fr300");
3168 break;
3169
3170 case EF_FRV_CPU_FR400:
3171 strcat (buf, ", fr400");
3172 break;
3173 case EF_FRV_CPU_FR405:
3174 strcat (buf, ", fr405");
3175 break;
3176
3177 case EF_FRV_CPU_FR450:
3178 strcat (buf, ", fr450");
3179 break;
3180
3181 case EF_FRV_CPU_FR500:
3182 strcat (buf, ", fr500");
3183 break;
3184 case EF_FRV_CPU_FR550:
3185 strcat (buf, ", fr550");
3186 break;
3187
3188 case EF_FRV_CPU_SIMPLE:
3189 strcat (buf, ", simple");
3190 break;
3191 case EF_FRV_CPU_TOMCAT:
3192 strcat (buf, ", tomcat");
3193 break;
3194 }
3195 break;
3196
3197 case EM_68K:
3198 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3199 strcat (buf, ", m68000");
3200 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3201 strcat (buf, ", cpu32");
3202 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3203 strcat (buf, ", fido_a");
3204 else
3205 {
3206 char const * isa = _("unknown");
3207 char const * mac = _("unknown mac");
3208 char const * additional = NULL;
3209
3210 switch (e_flags & EF_M68K_CF_ISA_MASK)
3211 {
3212 case EF_M68K_CF_ISA_A_NODIV:
3213 isa = "A";
3214 additional = ", nodiv";
3215 break;
3216 case EF_M68K_CF_ISA_A:
3217 isa = "A";
3218 break;
3219 case EF_M68K_CF_ISA_A_PLUS:
3220 isa = "A+";
3221 break;
3222 case EF_M68K_CF_ISA_B_NOUSP:
3223 isa = "B";
3224 additional = ", nousp";
3225 break;
3226 case EF_M68K_CF_ISA_B:
3227 isa = "B";
3228 break;
3229 case EF_M68K_CF_ISA_C:
3230 isa = "C";
3231 break;
3232 case EF_M68K_CF_ISA_C_NODIV:
3233 isa = "C";
3234 additional = ", nodiv";
3235 break;
3236 }
3237 strcat (buf, ", cf, isa ");
3238 strcat (buf, isa);
3239 if (additional)
3240 strcat (buf, additional);
3241 if (e_flags & EF_M68K_CF_FLOAT)
3242 strcat (buf, ", float");
3243 switch (e_flags & EF_M68K_CF_MAC_MASK)
3244 {
3245 case 0:
3246 mac = NULL;
3247 break;
3248 case EF_M68K_CF_MAC:
3249 mac = "mac";
3250 break;
3251 case EF_M68K_CF_EMAC:
3252 mac = "emac";
3253 break;
3254 case EF_M68K_CF_EMAC_B:
3255 mac = "emac_b";
3256 break;
3257 }
3258 if (mac)
3259 {
3260 strcat (buf, ", ");
3261 strcat (buf, mac);
3262 }
3263 }
3264 break;
3265
3266 case EM_CYGNUS_MEP:
3267 switch (e_flags & EF_MEP_CPU_MASK)
3268 {
3269 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3270 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3271 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3272 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3273 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3274 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3275 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3276 }
3277
3278 switch (e_flags & EF_MEP_COP_MASK)
3279 {
3280 case EF_MEP_COP_NONE: break;
3281 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3282 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3283 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3284 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3285 default: strcat (buf, _("<unknown MeP copro type>")); break;
3286 }
3287
3288 if (e_flags & EF_MEP_LIBRARY)
3289 strcat (buf, ", Built for Library");
3290
3291 if (e_flags & EF_MEP_INDEX_MASK)
3292 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3293 e_flags & EF_MEP_INDEX_MASK);
3294
3295 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3296 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3297 e_flags & ~ EF_MEP_ALL_FLAGS);
3298 break;
3299
3300 case EM_PPC:
3301 if (e_flags & EF_PPC_EMB)
3302 strcat (buf, ", emb");
3303
3304 if (e_flags & EF_PPC_RELOCATABLE)
3305 strcat (buf, _(", relocatable"));
3306
3307 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3308 strcat (buf, _(", relocatable-lib"));
3309 break;
3310
3311 case EM_PPC64:
3312 if (e_flags & EF_PPC64_ABI)
3313 {
3314 char abi[] = ", abiv0";
3315
3316 abi[6] += e_flags & EF_PPC64_ABI;
3317 strcat (buf, abi);
3318 }
3319 break;
3320
3321 case EM_V800:
3322 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3323 strcat (buf, ", RH850 ABI");
3324
3325 if (e_flags & EF_V800_850E3)
3326 strcat (buf, ", V3 architecture");
3327
3328 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3329 strcat (buf, ", FPU not used");
3330
3331 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3332 strcat (buf, ", regmode: COMMON");
3333
3334 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3335 strcat (buf, ", r4 not used");
3336
3337 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3338 strcat (buf, ", r30 not used");
3339
3340 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3341 strcat (buf, ", r5 not used");
3342
3343 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3344 strcat (buf, ", r2 not used");
3345
3346 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3347 {
3348 switch (e_flags & - e_flags)
3349 {
3350 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3351 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3352 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3353 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3354 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3355 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3356 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3357 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3358 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3359 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3360 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3361 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3362 default: break;
3363 }
3364 }
3365 break;
3366
3367 case EM_V850:
3368 case EM_CYGNUS_V850:
3369 switch (e_flags & EF_V850_ARCH)
3370 {
3371 case E_V850E3V5_ARCH:
3372 strcat (buf, ", v850e3v5");
3373 break;
3374 case E_V850E2V3_ARCH:
3375 strcat (buf, ", v850e2v3");
3376 break;
3377 case E_V850E2_ARCH:
3378 strcat (buf, ", v850e2");
3379 break;
3380 case E_V850E1_ARCH:
3381 strcat (buf, ", v850e1");
3382 break;
3383 case E_V850E_ARCH:
3384 strcat (buf, ", v850e");
3385 break;
3386 case E_V850_ARCH:
3387 strcat (buf, ", v850");
3388 break;
3389 default:
3390 strcat (buf, _(", unknown v850 architecture variant"));
3391 break;
3392 }
3393 break;
3394
3395 case EM_M32R:
3396 case EM_CYGNUS_M32R:
3397 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3398 strcat (buf, ", m32r");
3399 break;
3400
3401 case EM_MIPS:
3402 case EM_MIPS_RS3_LE:
3403 if (e_flags & EF_MIPS_NOREORDER)
3404 strcat (buf, ", noreorder");
3405
3406 if (e_flags & EF_MIPS_PIC)
3407 strcat (buf, ", pic");
3408
3409 if (e_flags & EF_MIPS_CPIC)
3410 strcat (buf, ", cpic");
3411
3412 if (e_flags & EF_MIPS_UCODE)
3413 strcat (buf, ", ugen_reserved");
3414
3415 if (e_flags & EF_MIPS_ABI2)
3416 strcat (buf, ", abi2");
3417
3418 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3419 strcat (buf, ", odk first");
3420
3421 if (e_flags & EF_MIPS_32BITMODE)
3422 strcat (buf, ", 32bitmode");
3423
3424 if (e_flags & EF_MIPS_NAN2008)
3425 strcat (buf, ", nan2008");
3426
3427 if (e_flags & EF_MIPS_FP64)
3428 strcat (buf, ", fp64");
3429
3430 switch ((e_flags & EF_MIPS_MACH))
3431 {
3432 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3433 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3434 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3435 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3436 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3437 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3438 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3439 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3440 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3441 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3442 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3443 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3444 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3445 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3446 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3447 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3448 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3449 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3450 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3451 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
3452 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
3453 case 0:
3454 /* We simply ignore the field in this case to avoid confusion:
3455 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3456 extension. */
3457 break;
3458 default: strcat (buf, _(", unknown CPU")); break;
3459 }
3460
3461 switch ((e_flags & EF_MIPS_ABI))
3462 {
3463 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3464 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3465 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3466 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3467 case 0:
3468 /* We simply ignore the field in this case to avoid confusion:
3469 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3470 This means it is likely to be an o32 file, but not for
3471 sure. */
3472 break;
3473 default: strcat (buf, _(", unknown ABI")); break;
3474 }
3475
3476 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3477 strcat (buf, ", mdmx");
3478
3479 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3480 strcat (buf, ", mips16");
3481
3482 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3483 strcat (buf, ", micromips");
3484
3485 switch ((e_flags & EF_MIPS_ARCH))
3486 {
3487 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3488 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3489 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3490 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3491 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3492 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3493 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3494 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3495 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3496 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3497 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3498 default: strcat (buf, _(", unknown ISA")); break;
3499 }
3500 break;
3501
3502 case EM_NDS32:
3503 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3504 break;
3505
3506 case EM_NFP:
3507 switch (EF_NFP_MACH (e_flags))
3508 {
3509 case E_NFP_MACH_3200:
3510 strcat (buf, ", NFP-32xx");
3511 break;
3512 case E_NFP_MACH_6000:
3513 strcat (buf, ", NFP-6xxx");
3514 break;
3515 }
3516 break;
3517
3518 case EM_RISCV:
3519 if (e_flags & EF_RISCV_RVC)
3520 strcat (buf, ", RVC");
3521
3522 if (e_flags & EF_RISCV_RVE)
3523 strcat (buf, ", RVE");
3524
3525 switch (e_flags & EF_RISCV_FLOAT_ABI)
3526 {
3527 case EF_RISCV_FLOAT_ABI_SOFT:
3528 strcat (buf, ", soft-float ABI");
3529 break;
3530
3531 case EF_RISCV_FLOAT_ABI_SINGLE:
3532 strcat (buf, ", single-float ABI");
3533 break;
3534
3535 case EF_RISCV_FLOAT_ABI_DOUBLE:
3536 strcat (buf, ", double-float ABI");
3537 break;
3538
3539 case EF_RISCV_FLOAT_ABI_QUAD:
3540 strcat (buf, ", quad-float ABI");
3541 break;
3542 }
3543 break;
3544
3545 case EM_SH:
3546 switch ((e_flags & EF_SH_MACH_MASK))
3547 {
3548 case EF_SH1: strcat (buf, ", sh1"); break;
3549 case EF_SH2: strcat (buf, ", sh2"); break;
3550 case EF_SH3: strcat (buf, ", sh3"); break;
3551 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3552 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3553 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3554 case EF_SH3E: strcat (buf, ", sh3e"); break;
3555 case EF_SH4: strcat (buf, ", sh4"); break;
3556 case EF_SH5: strcat (buf, ", sh5"); break;
3557 case EF_SH2E: strcat (buf, ", sh2e"); break;
3558 case EF_SH4A: strcat (buf, ", sh4a"); break;
3559 case EF_SH2A: strcat (buf, ", sh2a"); break;
3560 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3561 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3562 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3563 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3564 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3565 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3566 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3567 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3568 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3569 default: strcat (buf, _(", unknown ISA")); break;
3570 }
3571
3572 if (e_flags & EF_SH_PIC)
3573 strcat (buf, ", pic");
3574
3575 if (e_flags & EF_SH_FDPIC)
3576 strcat (buf, ", fdpic");
3577 break;
3578
3579 case EM_OR1K:
3580 if (e_flags & EF_OR1K_NODELAY)
3581 strcat (buf, ", no delay");
3582 break;
3583
3584 case EM_SPARCV9:
3585 if (e_flags & EF_SPARC_32PLUS)
3586 strcat (buf, ", v8+");
3587
3588 if (e_flags & EF_SPARC_SUN_US1)
3589 strcat (buf, ", ultrasparcI");
3590
3591 if (e_flags & EF_SPARC_SUN_US3)
3592 strcat (buf, ", ultrasparcIII");
3593
3594 if (e_flags & EF_SPARC_HAL_R1)
3595 strcat (buf, ", halr1");
3596
3597 if (e_flags & EF_SPARC_LEDATA)
3598 strcat (buf, ", ledata");
3599
3600 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3601 strcat (buf, ", tso");
3602
3603 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3604 strcat (buf, ", pso");
3605
3606 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3607 strcat (buf, ", rmo");
3608 break;
3609
3610 case EM_PARISC:
3611 switch (e_flags & EF_PARISC_ARCH)
3612 {
3613 case EFA_PARISC_1_0:
3614 strcpy (buf, ", PA-RISC 1.0");
3615 break;
3616 case EFA_PARISC_1_1:
3617 strcpy (buf, ", PA-RISC 1.1");
3618 break;
3619 case EFA_PARISC_2_0:
3620 strcpy (buf, ", PA-RISC 2.0");
3621 break;
3622 default:
3623 break;
3624 }
3625 if (e_flags & EF_PARISC_TRAPNIL)
3626 strcat (buf, ", trapnil");
3627 if (e_flags & EF_PARISC_EXT)
3628 strcat (buf, ", ext");
3629 if (e_flags & EF_PARISC_LSB)
3630 strcat (buf, ", lsb");
3631 if (e_flags & EF_PARISC_WIDE)
3632 strcat (buf, ", wide");
3633 if (e_flags & EF_PARISC_NO_KABP)
3634 strcat (buf, ", no kabp");
3635 if (e_flags & EF_PARISC_LAZYSWAP)
3636 strcat (buf, ", lazyswap");
3637 break;
3638
3639 case EM_PJ:
3640 case EM_PJ_OLD:
3641 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3642 strcat (buf, ", new calling convention");
3643
3644 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3645 strcat (buf, ", gnu calling convention");
3646 break;
3647
3648 case EM_IA_64:
3649 if ((e_flags & EF_IA_64_ABI64))
3650 strcat (buf, ", 64-bit");
3651 else
3652 strcat (buf, ", 32-bit");
3653 if ((e_flags & EF_IA_64_REDUCEDFP))
3654 strcat (buf, ", reduced fp model");
3655 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3656 strcat (buf, ", no function descriptors, constant gp");
3657 else if ((e_flags & EF_IA_64_CONS_GP))
3658 strcat (buf, ", constant gp");
3659 if ((e_flags & EF_IA_64_ABSOLUTE))
3660 strcat (buf, ", absolute");
3661 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3662 {
3663 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3664 strcat (buf, ", vms_linkages");
3665 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3666 {
3667 case EF_IA_64_VMS_COMCOD_SUCCESS:
3668 break;
3669 case EF_IA_64_VMS_COMCOD_WARNING:
3670 strcat (buf, ", warning");
3671 break;
3672 case EF_IA_64_VMS_COMCOD_ERROR:
3673 strcat (buf, ", error");
3674 break;
3675 case EF_IA_64_VMS_COMCOD_ABORT:
3676 strcat (buf, ", abort");
3677 break;
3678 default:
3679 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3680 e_flags & EF_IA_64_VMS_COMCOD);
3681 strcat (buf, ", <unknown>");
3682 }
3683 }
3684 break;
3685
3686 case EM_VAX:
3687 if ((e_flags & EF_VAX_NONPIC))
3688 strcat (buf, ", non-PIC");
3689 if ((e_flags & EF_VAX_DFLOAT))
3690 strcat (buf, ", D-Float");
3691 if ((e_flags & EF_VAX_GFLOAT))
3692 strcat (buf, ", G-Float");
3693 break;
3694
3695 case EM_VISIUM:
3696 if (e_flags & EF_VISIUM_ARCH_MCM)
3697 strcat (buf, ", mcm");
3698 else if (e_flags & EF_VISIUM_ARCH_MCM24)
3699 strcat (buf, ", mcm24");
3700 if (e_flags & EF_VISIUM_ARCH_GR6)
3701 strcat (buf, ", gr6");
3702 break;
3703
3704 case EM_RL78:
3705 switch (e_flags & E_FLAG_RL78_CPU_MASK)
3706 {
3707 case E_FLAG_RL78_ANY_CPU: break;
3708 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3709 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3710 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3711 }
3712 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3713 strcat (buf, ", 64-bit doubles");
3714 break;
3715
3716 case EM_RX:
3717 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3718 strcat (buf, ", 64-bit doubles");
3719 if (e_flags & E_FLAG_RX_DSP)
3720 strcat (buf, ", dsp");
3721 if (e_flags & E_FLAG_RX_PID)
3722 strcat (buf, ", pid");
3723 if (e_flags & E_FLAG_RX_ABI)
3724 strcat (buf, ", RX ABI");
3725 if (e_flags & E_FLAG_RX_SINSNS_SET)
3726 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3727 ? ", uses String instructions" : ", bans String instructions");
3728 if (e_flags & E_FLAG_RX_V2)
3729 strcat (buf, ", V2");
3730 if (e_flags & E_FLAG_RX_V3)
3731 strcat (buf, ", V3");
3732 break;
3733
3734 case EM_S390:
3735 if (e_flags & EF_S390_HIGH_GPRS)
3736 strcat (buf, ", highgprs");
3737 break;
3738
3739 case EM_TI_C6000:
3740 if ((e_flags & EF_C6000_REL))
3741 strcat (buf, ", relocatable module");
3742 break;
3743
3744 case EM_MSP430:
3745 strcat (buf, _(": architecture variant: "));
3746 switch (e_flags & EF_MSP430_MACH)
3747 {
3748 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3749 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3750 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3751 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3752 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3753 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3754 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3755 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3756 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3757 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3758 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3759 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3760 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3761 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3762 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
3763 default:
3764 strcat (buf, _(": unknown")); break;
3765 }
3766
3767 if (e_flags & ~ EF_MSP430_MACH)
3768 strcat (buf, _(": unknown extra flag bits also present"));
3769 }
3770 }
3771
3772 return buf;
3773 }
3774
3775 static const char *
3776 get_osabi_name (Filedata * filedata, unsigned int osabi)
3777 {
3778 static char buff[32];
3779
3780 switch (osabi)
3781 {
3782 case ELFOSABI_NONE: return "UNIX - System V";
3783 case ELFOSABI_HPUX: return "UNIX - HP-UX";
3784 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
3785 case ELFOSABI_GNU: return "UNIX - GNU";
3786 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
3787 case ELFOSABI_AIX: return "UNIX - AIX";
3788 case ELFOSABI_IRIX: return "UNIX - IRIX";
3789 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
3790 case ELFOSABI_TRU64: return "UNIX - TRU64";
3791 case ELFOSABI_MODESTO: return "Novell - Modesto";
3792 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
3793 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
3794 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
3795 case ELFOSABI_AROS: return "AROS";
3796 case ELFOSABI_FENIXOS: return "FenixOS";
3797 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
3798 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
3799 default:
3800 if (osabi >= 64)
3801 switch (filedata->file_header.e_machine)
3802 {
3803 case EM_ARM:
3804 switch (osabi)
3805 {
3806 case ELFOSABI_ARM: return "ARM";
3807 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
3808 default:
3809 break;
3810 }
3811 break;
3812
3813 case EM_MSP430:
3814 case EM_MSP430_OLD:
3815 case EM_VISIUM:
3816 switch (osabi)
3817 {
3818 case ELFOSABI_STANDALONE: return _("Standalone App");
3819 default:
3820 break;
3821 }
3822 break;
3823
3824 case EM_TI_C6000:
3825 switch (osabi)
3826 {
3827 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
3828 case ELFOSABI_C6000_LINUX: return "Linux C6000";
3829 default:
3830 break;
3831 }
3832 break;
3833
3834 default:
3835 break;
3836 }
3837 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3838 return buff;
3839 }
3840 }
3841
3842 static const char *
3843 get_aarch64_segment_type (unsigned long type)
3844 {
3845 switch (type)
3846 {
3847 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
3848 default: return NULL;
3849 }
3850 }
3851
3852 static const char *
3853 get_arm_segment_type (unsigned long type)
3854 {
3855 switch (type)
3856 {
3857 case PT_ARM_EXIDX: return "EXIDX";
3858 default: return NULL;
3859 }
3860 }
3861
3862 static const char *
3863 get_s390_segment_type (unsigned long type)
3864 {
3865 switch (type)
3866 {
3867 case PT_S390_PGSTE: return "S390_PGSTE";
3868 default: return NULL;
3869 }
3870 }
3871
3872 static const char *
3873 get_mips_segment_type (unsigned long type)
3874 {
3875 switch (type)
3876 {
3877 case PT_MIPS_REGINFO: return "REGINFO";
3878 case PT_MIPS_RTPROC: return "RTPROC";
3879 case PT_MIPS_OPTIONS: return "OPTIONS";
3880 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
3881 default: return NULL;
3882 }
3883 }
3884
3885 static const char *
3886 get_parisc_segment_type (unsigned long type)
3887 {
3888 switch (type)
3889 {
3890 case PT_HP_TLS: return "HP_TLS";
3891 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
3892 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
3893 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
3894 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
3895 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
3896 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
3897 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
3898 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
3899 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
3900 case PT_HP_PARALLEL: return "HP_PARALLEL";
3901 case PT_HP_FASTBIND: return "HP_FASTBIND";
3902 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
3903 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
3904 case PT_HP_STACK: return "HP_STACK";
3905 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
3906 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
3907 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
3908 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
3909 default: return NULL;
3910 }
3911 }
3912
3913 static const char *
3914 get_ia64_segment_type (unsigned long type)
3915 {
3916 switch (type)
3917 {
3918 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
3919 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
3920 case PT_HP_TLS: return "HP_TLS";
3921 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
3922 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
3923 case PT_IA_64_HP_STACK: return "HP_STACK";
3924 default: return NULL;
3925 }
3926 }
3927
3928 static const char *
3929 get_tic6x_segment_type (unsigned long type)
3930 {
3931 switch (type)
3932 {
3933 case PT_C6000_PHATTR: return "C6000_PHATTR";
3934 default: return NULL;
3935 }
3936 }
3937
3938 static const char *
3939 get_solaris_segment_type (unsigned long type)
3940 {
3941 switch (type)
3942 {
3943 case 0x6464e550: return "PT_SUNW_UNWIND";
3944 case 0x6474e550: return "PT_SUNW_EH_FRAME";
3945 case 0x6ffffff7: return "PT_LOSUNW";
3946 case 0x6ffffffa: return "PT_SUNWBSS";
3947 case 0x6ffffffb: return "PT_SUNWSTACK";
3948 case 0x6ffffffc: return "PT_SUNWDTRACE";
3949 case 0x6ffffffd: return "PT_SUNWCAP";
3950 case 0x6fffffff: return "PT_HISUNW";
3951 default: return NULL;
3952 }
3953 }
3954
3955 static const char *
3956 get_segment_type (Filedata * filedata, unsigned long p_type)
3957 {
3958 static char buff[32];
3959
3960 switch (p_type)
3961 {
3962 case PT_NULL: return "NULL";
3963 case PT_LOAD: return "LOAD";
3964 case PT_DYNAMIC: return "DYNAMIC";
3965 case PT_INTERP: return "INTERP";
3966 case PT_NOTE: return "NOTE";
3967 case PT_SHLIB: return "SHLIB";
3968 case PT_PHDR: return "PHDR";
3969 case PT_TLS: return "TLS";
3970 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
3971 case PT_GNU_STACK: return "GNU_STACK";
3972 case PT_GNU_RELRO: return "GNU_RELRO";
3973 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
3974
3975 default:
3976 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
3977 {
3978 sprintf (buff, "GNU_MBIND+%#lx",
3979 p_type - PT_GNU_MBIND_LO);
3980 }
3981 else if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
3982 {
3983 const char * result;
3984
3985 switch (filedata->file_header.e_machine)
3986 {
3987 case EM_AARCH64:
3988 result = get_aarch64_segment_type (p_type);
3989 break;
3990 case EM_ARM:
3991 result = get_arm_segment_type (p_type);
3992 break;
3993 case EM_MIPS:
3994 case EM_MIPS_RS3_LE:
3995 result = get_mips_segment_type (p_type);
3996 break;
3997 case EM_PARISC:
3998 result = get_parisc_segment_type (p_type);
3999 break;
4000 case EM_IA_64:
4001 result = get_ia64_segment_type (p_type);
4002 break;
4003 case EM_TI_C6000:
4004 result = get_tic6x_segment_type (p_type);
4005 break;
4006 case EM_S390:
4007 case EM_S390_OLD:
4008 result = get_s390_segment_type (p_type);
4009 break;
4010 default:
4011 result = NULL;
4012 break;
4013 }
4014
4015 if (result != NULL)
4016 return result;
4017
4018 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4019 }
4020 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4021 {
4022 const char * result;
4023
4024 switch (filedata->file_header.e_machine)
4025 {
4026 case EM_PARISC:
4027 result = get_parisc_segment_type (p_type);
4028 break;
4029 case EM_IA_64:
4030 result = get_ia64_segment_type (p_type);
4031 break;
4032 default:
4033 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4034 result = get_solaris_segment_type (p_type);
4035 else
4036 result = NULL;
4037 break;
4038 }
4039
4040 if (result != NULL)
4041 return result;
4042
4043 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4044 }
4045 else
4046 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4047
4048 return buff;
4049 }
4050 }
4051
4052 static const char *
4053 get_arc_section_type_name (unsigned int sh_type)
4054 {
4055 switch (sh_type)
4056 {
4057 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4058 default:
4059 break;
4060 }
4061 return NULL;
4062 }
4063
4064 static const char *
4065 get_mips_section_type_name (unsigned int sh_type)
4066 {
4067 switch (sh_type)
4068 {
4069 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4070 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4071 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4072 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4073 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4074 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4075 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4076 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4077 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4078 case SHT_MIPS_RELD: return "MIPS_RELD";
4079 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4080 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4081 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4082 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4083 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4084 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4085 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4086 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4087 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4088 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4089 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4090 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4091 case SHT_MIPS_LINE: return "MIPS_LINE";
4092 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4093 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4094 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4095 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4096 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4097 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4098 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4099 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4100 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4101 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4102 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4103 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4104 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4105 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4106 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4107 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4108 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4109 default:
4110 break;
4111 }
4112 return NULL;
4113 }
4114
4115 static const char *
4116 get_parisc_section_type_name (unsigned int sh_type)
4117 {
4118 switch (sh_type)
4119 {
4120 case SHT_PARISC_EXT: return "PARISC_EXT";
4121 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4122 case SHT_PARISC_DOC: return "PARISC_DOC";
4123 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4124 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4125 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4126 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4127 default: return NULL;
4128 }
4129 }
4130
4131 static const char *
4132 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4133 {
4134 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4135 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4136 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4137
4138 switch (sh_type)
4139 {
4140 case SHT_IA_64_EXT: return "IA_64_EXT";
4141 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4142 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4143 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4144 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4145 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4146 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4147 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4148 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4149 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4150 default:
4151 break;
4152 }
4153 return NULL;
4154 }
4155
4156 static const char *
4157 get_x86_64_section_type_name (unsigned int sh_type)
4158 {
4159 switch (sh_type)
4160 {
4161 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4162 default: return NULL;
4163 }
4164 }
4165
4166 static const char *
4167 get_aarch64_section_type_name (unsigned int sh_type)
4168 {
4169 switch (sh_type)
4170 {
4171 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4172 default: return NULL;
4173 }
4174 }
4175
4176 static const char *
4177 get_arm_section_type_name (unsigned int sh_type)
4178 {
4179 switch (sh_type)
4180 {
4181 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4182 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4183 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4184 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4185 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4186 default: return NULL;
4187 }
4188 }
4189
4190 static const char *
4191 get_tic6x_section_type_name (unsigned int sh_type)
4192 {
4193 switch (sh_type)
4194 {
4195 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4196 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4197 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4198 case SHT_TI_ICODE: return "TI_ICODE";
4199 case SHT_TI_XREF: return "TI_XREF";
4200 case SHT_TI_HANDLER: return "TI_HANDLER";
4201 case SHT_TI_INITINFO: return "TI_INITINFO";
4202 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4203 default: return NULL;
4204 }
4205 }
4206
4207 static const char *
4208 get_msp430x_section_type_name (unsigned int sh_type)
4209 {
4210 switch (sh_type)
4211 {
4212 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4213 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4214 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4215 default: return NULL;
4216 }
4217 }
4218
4219 static const char *
4220 get_nfp_section_type_name (unsigned int sh_type)
4221 {
4222 switch (sh_type)
4223 {
4224 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4225 case SHT_NFP_INITREG: return "NFP_INITREG";
4226 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4227 default: return NULL;
4228 }
4229 }
4230
4231 static const char *
4232 get_v850_section_type_name (unsigned int sh_type)
4233 {
4234 switch (sh_type)
4235 {
4236 case SHT_V850_SCOMMON: return "V850 Small Common";
4237 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4238 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4239 case SHT_RENESAS_IOP: return "RENESAS IOP";
4240 case SHT_RENESAS_INFO: return "RENESAS INFO";
4241 default: return NULL;
4242 }
4243 }
4244
4245 static const char *
4246 get_riscv_section_type_name (unsigned int sh_type)
4247 {
4248 switch (sh_type)
4249 {
4250 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4251 default: return NULL;
4252 }
4253 }
4254
4255 static const char *
4256 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4257 {
4258 static char buff[32];
4259 const char * result;
4260
4261 switch (sh_type)
4262 {
4263 case SHT_NULL: return "NULL";
4264 case SHT_PROGBITS: return "PROGBITS";
4265 case SHT_SYMTAB: return "SYMTAB";
4266 case SHT_STRTAB: return "STRTAB";
4267 case SHT_RELA: return "RELA";
4268 case SHT_HASH: return "HASH";
4269 case SHT_DYNAMIC: return "DYNAMIC";
4270 case SHT_NOTE: return "NOTE";
4271 case SHT_NOBITS: return "NOBITS";
4272 case SHT_REL: return "REL";
4273 case SHT_SHLIB: return "SHLIB";
4274 case SHT_DYNSYM: return "DYNSYM";
4275 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4276 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4277 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4278 case SHT_GNU_HASH: return "GNU_HASH";
4279 case SHT_GROUP: return "GROUP";
4280 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4281 case SHT_GNU_verdef: return "VERDEF";
4282 case SHT_GNU_verneed: return "VERNEED";
4283 case SHT_GNU_versym: return "VERSYM";
4284 case 0x6ffffff0: return "VERSYM";
4285 case 0x6ffffffc: return "VERDEF";
4286 case 0x7ffffffd: return "AUXILIARY";
4287 case 0x7fffffff: return "FILTER";
4288 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4289
4290 default:
4291 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4292 {
4293 switch (filedata->file_header.e_machine)
4294 {
4295 case EM_ARC:
4296 case EM_ARC_COMPACT:
4297 case EM_ARC_COMPACT2:
4298 result = get_arc_section_type_name (sh_type);
4299 break;
4300 case EM_MIPS:
4301 case EM_MIPS_RS3_LE:
4302 result = get_mips_section_type_name (sh_type);
4303 break;
4304 case EM_PARISC:
4305 result = get_parisc_section_type_name (sh_type);
4306 break;
4307 case EM_IA_64:
4308 result = get_ia64_section_type_name (filedata, sh_type);
4309 break;
4310 case EM_X86_64:
4311 case EM_L1OM:
4312 case EM_K1OM:
4313 result = get_x86_64_section_type_name (sh_type);
4314 break;
4315 case EM_AARCH64:
4316 result = get_aarch64_section_type_name (sh_type);
4317 break;
4318 case EM_ARM:
4319 result = get_arm_section_type_name (sh_type);
4320 break;
4321 case EM_TI_C6000:
4322 result = get_tic6x_section_type_name (sh_type);
4323 break;
4324 case EM_MSP430:
4325 result = get_msp430x_section_type_name (sh_type);
4326 break;
4327 case EM_NFP:
4328 result = get_nfp_section_type_name (sh_type);
4329 break;
4330 case EM_V800:
4331 case EM_V850:
4332 case EM_CYGNUS_V850:
4333 result = get_v850_section_type_name (sh_type);
4334 break;
4335 case EM_RISCV:
4336 result = get_riscv_section_type_name (sh_type);
4337 break;
4338 default:
4339 result = NULL;
4340 break;
4341 }
4342
4343 if (result != NULL)
4344 return result;
4345
4346 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4347 }
4348 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4349 {
4350 switch (filedata->file_header.e_machine)
4351 {
4352 case EM_IA_64:
4353 result = get_ia64_section_type_name (filedata, sh_type);
4354 break;
4355 default:
4356 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4357 result = get_solaris_section_type (sh_type);
4358 else
4359 {
4360 switch (sh_type)
4361 {
4362 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4363 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4364 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4365 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4366 default:
4367 result = NULL;
4368 break;
4369 }
4370 }
4371 break;
4372 }
4373
4374 if (result != NULL)
4375 return result;
4376
4377 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4378 }
4379 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4380 {
4381 switch (filedata->file_header.e_machine)
4382 {
4383 case EM_V800:
4384 case EM_V850:
4385 case EM_CYGNUS_V850:
4386 result = get_v850_section_type_name (sh_type);
4387 break;
4388 default:
4389 result = NULL;
4390 break;
4391 }
4392
4393 if (result != NULL)
4394 return result;
4395
4396 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4397 }
4398 else
4399 /* This message is probably going to be displayed in a 15
4400 character wide field, so put the hex value first. */
4401 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4402
4403 return buff;
4404 }
4405 }
4406
4407 #define OPTION_DEBUG_DUMP 512
4408 #define OPTION_DYN_SYMS 513
4409 #define OPTION_DWARF_DEPTH 514
4410 #define OPTION_DWARF_START 515
4411 #define OPTION_DWARF_CHECK 516
4412 #ifdef HAVE_LIBCTF
4413 #define OPTION_CTF_DUMP 517
4414 #define OPTION_CTF_PARENT 518
4415 #define OPTION_CTF_SYMBOLS 519
4416 #define OPTION_CTF_STRINGS 520
4417 #endif
4418
4419 static struct option options[] =
4420 {
4421 {"all", no_argument, 0, 'a'},
4422 {"file-header", no_argument, 0, 'h'},
4423 {"program-headers", no_argument, 0, 'l'},
4424 {"headers", no_argument, 0, 'e'},
4425 {"histogram", no_argument, 0, 'I'},
4426 {"segments", no_argument, 0, 'l'},
4427 {"sections", no_argument, 0, 'S'},
4428 {"section-headers", no_argument, 0, 'S'},
4429 {"section-groups", no_argument, 0, 'g'},
4430 {"section-details", no_argument, 0, 't'},
4431 {"full-section-name",no_argument, 0, 'N'},
4432 {"symbols", no_argument, 0, 's'},
4433 {"syms", no_argument, 0, 's'},
4434 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
4435 {"relocs", no_argument, 0, 'r'},
4436 {"notes", no_argument, 0, 'n'},
4437 {"dynamic", no_argument, 0, 'd'},
4438 {"arch-specific", no_argument, 0, 'A'},
4439 {"version-info", no_argument, 0, 'V'},
4440 {"use-dynamic", no_argument, 0, 'D'},
4441 {"unwind", no_argument, 0, 'u'},
4442 {"archive-index", no_argument, 0, 'c'},
4443 {"hex-dump", required_argument, 0, 'x'},
4444 {"relocated-dump", required_argument, 0, 'R'},
4445 {"string-dump", required_argument, 0, 'p'},
4446 {"decompress", no_argument, 0, 'z'},
4447 #ifdef SUPPORT_DISASSEMBLY
4448 {"instruction-dump", required_argument, 0, 'i'},
4449 #endif
4450 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
4451
4452 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4453 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4454 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
4455
4456 #ifdef HAVE_LIBCTF
4457 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
4458
4459 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4460 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4461 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
4462 #endif
4463
4464 {"version", no_argument, 0, 'v'},
4465 {"wide", no_argument, 0, 'W'},
4466 {"help", no_argument, 0, 'H'},
4467 {0, no_argument, 0, 0}
4468 };
4469
4470 static void
4471 usage (FILE * stream)
4472 {
4473 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4474 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4475 fprintf (stream, _(" Options are:\n\
4476 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
4477 -h --file-header Display the ELF file header\n\
4478 -l --program-headers Display the program headers\n\
4479 --segments An alias for --program-headers\n\
4480 -S --section-headers Display the sections' header\n\
4481 --sections An alias for --section-headers\n\
4482 -g --section-groups Display the section groups\n\
4483 -t --section-details Display the section details\n\
4484 -e --headers Equivalent to: -h -l -S\n\
4485 -s --syms Display the symbol table\n\
4486 --symbols An alias for --syms\n\
4487 --dyn-syms Display the dynamic symbol table\n\
4488 -n --notes Display the core notes (if present)\n\
4489 -r --relocs Display the relocations (if present)\n\
4490 -u --unwind Display the unwind info (if present)\n\
4491 -d --dynamic Display the dynamic section (if present)\n\
4492 -V --version-info Display the version sections (if present)\n\
4493 -A --arch-specific Display architecture specific information (if any)\n\
4494 -c --archive-index Display the symbol/file index in an archive\n\
4495 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
4496 -x --hex-dump=<number|name>\n\
4497 Dump the contents of section <number|name> as bytes\n\
4498 -p --string-dump=<number|name>\n\
4499 Dump the contents of section <number|name> as strings\n\
4500 -R --relocated-dump=<number|name>\n\
4501 Dump the contents of section <number|name> as relocated bytes\n\
4502 -z --decompress Decompress section before dumping it\n\
4503 -w[lLiaprmfFsoRtUuTgAckK] or\n\
4504 --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
4505 =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
4506 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
4507 =addr,=cu_index,=links,=follow-links]\n\
4508 Display the contents of DWARF debug sections\n"));
4509 fprintf (stream, _("\
4510 --dwarf-depth=N Do not display DIEs at depth N or greater\n\
4511 --dwarf-start=N Display DIEs starting with N, at the same depth\n\
4512 or deeper\n"));
4513 #ifdef HAVE_LIBCTF
4514 fprintf (stream, _("\
4515 --ctf=<number|name> Display CTF info from section <number|name>\n\
4516 --ctf-parent=<number|name>\n\
4517 Use section <number|name> as the CTF parent\n\n\
4518 --ctf-symbols=<number|name>\n\
4519 Use section <number|name> as the CTF external symtab\n\n\
4520 --ctf-strings=<number|name>\n\
4521 Use section <number|name> as the CTF external strtab\n\n"));
4522 #endif
4523
4524 #ifdef SUPPORT_DISASSEMBLY
4525 fprintf (stream, _("\
4526 -i --instruction-dump=<number|name>\n\
4527 Disassemble the contents of section <number|name>\n"));
4528 #endif
4529 fprintf (stream, _("\
4530 -I --histogram Display histogram of bucket list lengths\n\
4531 -W --wide Allow output width to exceed 80 characters\n\
4532 @<file> Read options from <file>\n\
4533 -H --help Display this information\n\
4534 -v --version Display the version number of readelf\n"));
4535
4536 if (REPORT_BUGS_TO[0] && stream == stdout)
4537 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4538
4539 exit (stream == stdout ? 0 : 1);
4540 }
4541
4542 /* Record the fact that the user wants the contents of section number
4543 SECTION to be displayed using the method(s) encoded as flags bits
4544 in TYPE. Note, TYPE can be zero if we are creating the array for
4545 the first time. */
4546
4547 static void
4548 request_dump_bynumber (Filedata * filedata, unsigned int section, dump_type type)
4549 {
4550 if (section >= filedata->num_dump_sects)
4551 {
4552 dump_type * new_dump_sects;
4553
4554 new_dump_sects = (dump_type *) calloc (section + 1,
4555 sizeof (* new_dump_sects));
4556
4557 if (new_dump_sects == NULL)
4558 error (_("Out of memory allocating dump request table.\n"));
4559 else
4560 {
4561 if (filedata->dump_sects)
4562 {
4563 /* Copy current flag settings. */
4564 memcpy (new_dump_sects, filedata->dump_sects,
4565 filedata->num_dump_sects * sizeof (* new_dump_sects));
4566
4567 free (filedata->dump_sects);
4568 }
4569
4570 filedata->dump_sects = new_dump_sects;
4571 filedata->num_dump_sects = section + 1;
4572 }
4573 }
4574
4575 if (filedata->dump_sects)
4576 filedata->dump_sects[section] |= type;
4577 }
4578
4579 /* Request a dump by section name. */
4580
4581 static void
4582 request_dump_byname (const char * section, dump_type type)
4583 {
4584 struct dump_list_entry * new_request;
4585
4586 new_request = (struct dump_list_entry *)
4587 malloc (sizeof (struct dump_list_entry));
4588 if (!new_request)
4589 error (_("Out of memory allocating dump request table.\n"));
4590
4591 new_request->name = strdup (section);
4592 if (!new_request->name)
4593 error (_("Out of memory allocating dump request table.\n"));
4594
4595 new_request->type = type;
4596
4597 new_request->next = dump_sects_byname;
4598 dump_sects_byname = new_request;
4599 }
4600
4601 static inline void
4602 request_dump (Filedata * filedata, dump_type type)
4603 {
4604 int section;
4605 char * cp;
4606
4607 do_dump++;
4608 section = strtoul (optarg, & cp, 0);
4609
4610 if (! *cp && section >= 0)
4611 request_dump_bynumber (filedata, section, type);
4612 else
4613 request_dump_byname (optarg, type);
4614 }
4615
4616 static void
4617 parse_args (Filedata * filedata, int argc, char ** argv)
4618 {
4619 int c;
4620
4621 if (argc < 2)
4622 usage (stderr);
4623
4624 while ((c = getopt_long
4625 (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4626 {
4627 switch (c)
4628 {
4629 case 0:
4630 /* Long options. */
4631 break;
4632 case 'H':
4633 usage (stdout);
4634 break;
4635
4636 case 'a':
4637 do_syms = TRUE;
4638 do_reloc = TRUE;
4639 do_unwind = TRUE;
4640 do_dynamic = TRUE;
4641 do_header = TRUE;
4642 do_sections = TRUE;
4643 do_section_groups = TRUE;
4644 do_segments = TRUE;
4645 do_version = TRUE;
4646 do_histogram = TRUE;
4647 do_arch = TRUE;
4648 do_notes = TRUE;
4649 break;
4650 case 'g':
4651 do_section_groups = TRUE;
4652 break;
4653 case 't':
4654 case 'N':
4655 do_sections = TRUE;
4656 do_section_details = TRUE;
4657 break;
4658 case 'e':
4659 do_header = TRUE;
4660 do_sections = TRUE;
4661 do_segments = TRUE;
4662 break;
4663 case 'A':
4664 do_arch = TRUE;
4665 break;
4666 case 'D':
4667 do_using_dynamic = TRUE;
4668 break;
4669 case 'r':
4670 do_reloc = TRUE;
4671 break;
4672 case 'u':
4673 do_unwind = TRUE;
4674 break;
4675 case 'h':
4676 do_header = TRUE;
4677 break;
4678 case 'l':
4679 do_segments = TRUE;
4680 break;
4681 case 's':
4682 do_syms = TRUE;
4683 break;
4684 case 'S':
4685 do_sections = TRUE;
4686 break;
4687 case 'd':
4688 do_dynamic = TRUE;
4689 break;
4690 case 'I':
4691 do_histogram = TRUE;
4692 break;
4693 case 'n':
4694 do_notes = TRUE;
4695 break;
4696 case 'c':
4697 do_archive_index = TRUE;
4698 break;
4699 case 'x':
4700 request_dump (filedata, HEX_DUMP);
4701 break;
4702 case 'p':
4703 request_dump (filedata, STRING_DUMP);
4704 break;
4705 case 'R':
4706 request_dump (filedata, RELOC_DUMP);
4707 break;
4708 case 'z':
4709 decompress_dumps = TRUE;
4710 break;
4711 case 'w':
4712 do_dump = TRUE;
4713 if (optarg == 0)
4714 {
4715 do_debugging = TRUE;
4716 dwarf_select_sections_all ();
4717 }
4718 else
4719 {
4720 do_debugging = FALSE;
4721 dwarf_select_sections_by_letters (optarg);
4722 }
4723 break;
4724 case OPTION_DEBUG_DUMP:
4725 do_dump = TRUE;
4726 if (optarg == 0)
4727 do_debugging = TRUE;
4728 else
4729 {
4730 do_debugging = FALSE;
4731 dwarf_select_sections_by_names (optarg);
4732 }
4733 break;
4734 case OPTION_DWARF_DEPTH:
4735 {
4736 char *cp;
4737
4738 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4739 }
4740 break;
4741 case OPTION_DWARF_START:
4742 {
4743 char *cp;
4744
4745 dwarf_start_die = strtoul (optarg, & cp, 0);
4746 }
4747 break;
4748 case OPTION_DWARF_CHECK:
4749 dwarf_check = TRUE;
4750 break;
4751 #ifdef HAVE_LIBCTF
4752 case OPTION_CTF_DUMP:
4753 do_ctf = TRUE;
4754 request_dump (filedata, CTF_DUMP);
4755 break;
4756 case OPTION_CTF_SYMBOLS:
4757 dump_ctf_symtab_name = strdup (optarg);
4758 break;
4759 case OPTION_CTF_STRINGS:
4760 dump_ctf_strtab_name = strdup (optarg);
4761 break;
4762 case OPTION_CTF_PARENT:
4763 dump_ctf_parent_name = strdup (optarg);
4764 break;
4765 #endif
4766 case OPTION_DYN_SYMS:
4767 do_dyn_syms = TRUE;
4768 break;
4769 #ifdef SUPPORT_DISASSEMBLY
4770 case 'i':
4771 request_dump (filedata, DISASS_DUMP);
4772 break;
4773 #endif
4774 case 'v':
4775 print_version (program_name);
4776 break;
4777 case 'V':
4778 do_version = TRUE;
4779 break;
4780 case 'W':
4781 do_wide = TRUE;
4782 break;
4783 default:
4784 /* xgettext:c-format */
4785 error (_("Invalid option '-%c'\n"), c);
4786 /* Fall through. */
4787 case '?':
4788 usage (stderr);
4789 }
4790 }
4791
4792 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4793 && !do_segments && !do_header && !do_dump && !do_version
4794 && !do_histogram && !do_debugging && !do_arch && !do_notes
4795 && !do_section_groups && !do_archive_index
4796 && !do_dyn_syms)
4797 usage (stderr);
4798 }
4799
4800 static const char *
4801 get_elf_class (unsigned int elf_class)
4802 {
4803 static char buff[32];
4804
4805 switch (elf_class)
4806 {
4807 case ELFCLASSNONE: return _("none");
4808 case ELFCLASS32: return "ELF32";
4809 case ELFCLASS64: return "ELF64";
4810 default:
4811 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
4812 return buff;
4813 }
4814 }
4815
4816 static const char *
4817 get_data_encoding (unsigned int encoding)
4818 {
4819 static char buff[32];
4820
4821 switch (encoding)
4822 {
4823 case ELFDATANONE: return _("none");
4824 case ELFDATA2LSB: return _("2's complement, little endian");
4825 case ELFDATA2MSB: return _("2's complement, big endian");
4826 default:
4827 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
4828 return buff;
4829 }
4830 }
4831
4832 /* Decode the data held in 'filedata->file_header'. */
4833
4834 static bfd_boolean
4835 process_file_header (Filedata * filedata)
4836 {
4837 Elf_Internal_Ehdr * header = & filedata->file_header;
4838
4839 if ( header->e_ident[EI_MAG0] != ELFMAG0
4840 || header->e_ident[EI_MAG1] != ELFMAG1
4841 || header->e_ident[EI_MAG2] != ELFMAG2
4842 || header->e_ident[EI_MAG3] != ELFMAG3)
4843 {
4844 error
4845 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
4846 return FALSE;
4847 }
4848
4849 init_dwarf_regnames (header->e_machine);
4850
4851 if (do_header)
4852 {
4853 unsigned i;
4854
4855 printf (_("ELF Header:\n"));
4856 printf (_(" Magic: "));
4857 for (i = 0; i < EI_NIDENT; i++)
4858 printf ("%2.2x ", header->e_ident[i]);
4859 printf ("\n");
4860 printf (_(" Class: %s\n"),
4861 get_elf_class (header->e_ident[EI_CLASS]));
4862 printf (_(" Data: %s\n"),
4863 get_data_encoding (header->e_ident[EI_DATA]));
4864 printf (_(" Version: %d%s\n"),
4865 header->e_ident[EI_VERSION],
4866 (header->e_ident[EI_VERSION] == EV_CURRENT
4867 ? _(" (current)")
4868 : (header->e_ident[EI_VERSION] != EV_NONE
4869 ? _(" <unknown>")
4870 : "")));
4871 printf (_(" OS/ABI: %s\n"),
4872 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
4873 printf (_(" ABI Version: %d\n"),
4874 header->e_ident[EI_ABIVERSION]);
4875 printf (_(" Type: %s\n"),
4876 get_file_type (header->e_type));
4877 printf (_(" Machine: %s\n"),
4878 get_machine_name (header->e_machine));
4879 printf (_(" Version: 0x%lx\n"),
4880 header->e_version);
4881
4882 printf (_(" Entry point address: "));
4883 print_vma (header->e_entry, PREFIX_HEX);
4884 printf (_("\n Start of program headers: "));
4885 print_vma (header->e_phoff, DEC);
4886 printf (_(" (bytes into file)\n Start of section headers: "));
4887 print_vma (header->e_shoff, DEC);
4888 printf (_(" (bytes into file)\n"));
4889
4890 printf (_(" Flags: 0x%lx%s\n"),
4891 header->e_flags,
4892 get_machine_flags (filedata, header->e_flags, header->e_machine));
4893 printf (_(" Size of this header: %u (bytes)\n"),
4894 header->e_ehsize);
4895 printf (_(" Size of program headers: %u (bytes)\n"),
4896 header->e_phentsize);
4897 printf (_(" Number of program headers: %u"),
4898 header->e_phnum);
4899 if (filedata->section_headers != NULL
4900 && header->e_phnum == PN_XNUM
4901 && filedata->section_headers[0].sh_info != 0)
4902 {
4903 header->e_phnum = filedata->section_headers[0].sh_info;
4904 printf (" (%u)", header->e_phnum);
4905 }
4906 putc ('\n', stdout);
4907 printf (_(" Size of section headers: %u (bytes)\n"),
4908 header->e_shentsize);
4909 printf (_(" Number of section headers: %u"),
4910 header->e_shnum);
4911 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
4912 {
4913 header->e_shnum = filedata->section_headers[0].sh_size;
4914 printf (" (%u)", header->e_shnum);
4915 }
4916 putc ('\n', stdout);
4917 printf (_(" Section header string table index: %u"),
4918 header->e_shstrndx);
4919 if (filedata->section_headers != NULL
4920 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
4921 {
4922 header->e_shstrndx = filedata->section_headers[0].sh_link;
4923 printf (" (%u)", header->e_shstrndx);
4924 }
4925 if (header->e_shstrndx != SHN_UNDEF
4926 && header->e_shstrndx >= header->e_shnum)
4927 {
4928 header->e_shstrndx = SHN_UNDEF;
4929 printf (_(" <corrupt: out of range>"));
4930 }
4931 putc ('\n', stdout);
4932 }
4933
4934 if (filedata->section_headers != NULL)
4935 {
4936 if (header->e_phnum == PN_XNUM
4937 && filedata->section_headers[0].sh_info != 0)
4938 header->e_phnum = filedata->section_headers[0].sh_info;
4939 if (header->e_shnum == SHN_UNDEF)
4940 header->e_shnum = filedata->section_headers[0].sh_size;
4941 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
4942 header->e_shstrndx = filedata->section_headers[0].sh_link;
4943 if (header->e_shstrndx >= header->e_shnum)
4944 header->e_shstrndx = SHN_UNDEF;
4945 free (filedata->section_headers);
4946 filedata->section_headers = NULL;
4947 }
4948
4949 return TRUE;
4950 }
4951
4952 /* Read in the program headers from FILEDATA and store them in PHEADERS.
4953 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
4954
4955 static bfd_boolean
4956 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
4957 {
4958 Elf32_External_Phdr * phdrs;
4959 Elf32_External_Phdr * external;
4960 Elf_Internal_Phdr * internal;
4961 unsigned int i;
4962 unsigned int size = filedata->file_header.e_phentsize;
4963 unsigned int num = filedata->file_header.e_phnum;
4964
4965 /* PR binutils/17531: Cope with unexpected section header sizes. */
4966 if (size == 0 || num == 0)
4967 return FALSE;
4968 if (size < sizeof * phdrs)
4969 {
4970 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
4971 return FALSE;
4972 }
4973 if (size > sizeof * phdrs)
4974 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
4975
4976 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
4977 size, num, _("program headers"));
4978 if (phdrs == NULL)
4979 return FALSE;
4980
4981 for (i = 0, internal = pheaders, external = phdrs;
4982 i < filedata->file_header.e_phnum;
4983 i++, internal++, external++)
4984 {
4985 internal->p_type = BYTE_GET (external->p_type);
4986 internal->p_offset = BYTE_GET (external->p_offset);
4987 internal->p_vaddr = BYTE_GET (external->p_vaddr);
4988 internal->p_paddr = BYTE_GET (external->p_paddr);
4989 internal->p_filesz = BYTE_GET (external->p_filesz);
4990 internal->p_memsz = BYTE_GET (external->p_memsz);
4991 internal->p_flags = BYTE_GET (external->p_flags);
4992 internal->p_align = BYTE_GET (external->p_align);
4993 }
4994
4995 free (phdrs);
4996 return TRUE;
4997 }
4998
4999 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5000 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5001
5002 static bfd_boolean
5003 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5004 {
5005 Elf64_External_Phdr * phdrs;
5006 Elf64_External_Phdr * external;
5007 Elf_Internal_Phdr * internal;
5008 unsigned int i;
5009 unsigned int size = filedata->file_header.e_phentsize;
5010 unsigned int num = filedata->file_header.e_phnum;
5011
5012 /* PR binutils/17531: Cope with unexpected section header sizes. */
5013 if (size == 0 || num == 0)
5014 return FALSE;
5015 if (size < sizeof * phdrs)
5016 {
5017 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5018 return FALSE;
5019 }
5020 if (size > sizeof * phdrs)
5021 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5022
5023 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5024 size, num, _("program headers"));
5025 if (!phdrs)
5026 return FALSE;
5027
5028 for (i = 0, internal = pheaders, external = phdrs;
5029 i < filedata->file_header.e_phnum;
5030 i++, internal++, external++)
5031 {
5032 internal->p_type = BYTE_GET (external->p_type);
5033 internal->p_flags = BYTE_GET (external->p_flags);
5034 internal->p_offset = BYTE_GET (external->p_offset);
5035 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5036 internal->p_paddr = BYTE_GET (external->p_paddr);
5037 internal->p_filesz = BYTE_GET (external->p_filesz);
5038 internal->p_memsz = BYTE_GET (external->p_memsz);
5039 internal->p_align = BYTE_GET (external->p_align);
5040 }
5041
5042 free (phdrs);
5043 return TRUE;
5044 }
5045
5046 /* Returns TRUE if the program headers were read into `program_headers'. */
5047
5048 static bfd_boolean
5049 get_program_headers (Filedata * filedata)
5050 {
5051 Elf_Internal_Phdr * phdrs;
5052
5053 /* Check cache of prior read. */
5054 if (filedata->program_headers != NULL)
5055 return TRUE;
5056
5057 /* Be kind to memory checkers by looking for
5058 e_phnum values which we know must be invalid. */
5059 if (filedata->file_header.e_phnum
5060 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5061 >= filedata->file_size)
5062 {
5063 error (_("Too many program headers - %#x - the file is not that big\n"),
5064 filedata->file_header.e_phnum);
5065 return FALSE;
5066 }
5067
5068 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5069 sizeof (Elf_Internal_Phdr));
5070 if (phdrs == NULL)
5071 {
5072 error (_("Out of memory reading %u program headers\n"),
5073 filedata->file_header.e_phnum);
5074 return FALSE;
5075 }
5076
5077 if (is_32bit_elf
5078 ? get_32bit_program_headers (filedata, phdrs)
5079 : get_64bit_program_headers (filedata, phdrs))
5080 {
5081 filedata->program_headers = phdrs;
5082 return TRUE;
5083 }
5084
5085 free (phdrs);
5086 return FALSE;
5087 }
5088
5089 /* Returns TRUE if the program headers were loaded. */
5090
5091 static bfd_boolean
5092 process_program_headers (Filedata * filedata)
5093 {
5094 Elf_Internal_Phdr * segment;
5095 unsigned int i;
5096 Elf_Internal_Phdr * previous_load = NULL;
5097
5098 if (filedata->file_header.e_phnum == 0)
5099 {
5100 /* PR binutils/12467. */
5101 if (filedata->file_header.e_phoff != 0)
5102 {
5103 warn (_("possibly corrupt ELF header - it has a non-zero program"
5104 " header offset, but no program headers\n"));
5105 return FALSE;
5106 }
5107 else if (do_segments)
5108 printf (_("\nThere are no program headers in this file.\n"));
5109 return TRUE;
5110 }
5111
5112 if (do_segments && !do_header)
5113 {
5114 printf (_("\nElf file type is %s\n"), get_file_type (filedata->file_header.e_type));
5115 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5116 printf (ngettext ("There is %d program header, starting at offset %s\n",
5117 "There are %d program headers, starting at offset %s\n",
5118 filedata->file_header.e_phnum),
5119 filedata->file_header.e_phnum,
5120 bfd_vmatoa ("u", filedata->file_header.e_phoff));
5121 }
5122
5123 if (! get_program_headers (filedata))
5124 return TRUE;
5125
5126 if (do_segments)
5127 {
5128 if (filedata->file_header.e_phnum > 1)
5129 printf (_("\nProgram Headers:\n"));
5130 else
5131 printf (_("\nProgram Headers:\n"));
5132
5133 if (is_32bit_elf)
5134 printf
5135 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5136 else if (do_wide)
5137 printf
5138 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5139 else
5140 {
5141 printf
5142 (_(" Type Offset VirtAddr PhysAddr\n"));
5143 printf
5144 (_(" FileSiz MemSiz Flags Align\n"));
5145 }
5146 }
5147
5148 dynamic_addr = 0;
5149 dynamic_size = 0;
5150
5151 for (i = 0, segment = filedata->program_headers;
5152 i < filedata->file_header.e_phnum;
5153 i++, segment++)
5154 {
5155 if (do_segments)
5156 {
5157 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
5158
5159 if (is_32bit_elf)
5160 {
5161 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5162 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5163 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5164 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5165 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5166 printf ("%c%c%c ",
5167 (segment->p_flags & PF_R ? 'R' : ' '),
5168 (segment->p_flags & PF_W ? 'W' : ' '),
5169 (segment->p_flags & PF_X ? 'E' : ' '));
5170 printf ("%#lx", (unsigned long) segment->p_align);
5171 }
5172 else if (do_wide)
5173 {
5174 if ((unsigned long) segment->p_offset == segment->p_offset)
5175 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5176 else
5177 {
5178 print_vma (segment->p_offset, FULL_HEX);
5179 putchar (' ');
5180 }
5181
5182 print_vma (segment->p_vaddr, FULL_HEX);
5183 putchar (' ');
5184 print_vma (segment->p_paddr, FULL_HEX);
5185 putchar (' ');
5186
5187 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5188 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5189 else
5190 {
5191 print_vma (segment->p_filesz, FULL_HEX);
5192 putchar (' ');
5193 }
5194
5195 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5196 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5197 else
5198 {
5199 print_vma (segment->p_memsz, FULL_HEX);
5200 }
5201
5202 printf (" %c%c%c ",
5203 (segment->p_flags & PF_R ? 'R' : ' '),
5204 (segment->p_flags & PF_W ? 'W' : ' '),
5205 (segment->p_flags & PF_X ? 'E' : ' '));
5206
5207 if ((unsigned long) segment->p_align == segment->p_align)
5208 printf ("%#lx", (unsigned long) segment->p_align);
5209 else
5210 {
5211 print_vma (segment->p_align, PREFIX_HEX);
5212 }
5213 }
5214 else
5215 {
5216 print_vma (segment->p_offset, FULL_HEX);
5217 putchar (' ');
5218 print_vma (segment->p_vaddr, FULL_HEX);
5219 putchar (' ');
5220 print_vma (segment->p_paddr, FULL_HEX);
5221 printf ("\n ");
5222 print_vma (segment->p_filesz, FULL_HEX);
5223 putchar (' ');
5224 print_vma (segment->p_memsz, FULL_HEX);
5225 printf (" %c%c%c ",
5226 (segment->p_flags & PF_R ? 'R' : ' '),
5227 (segment->p_flags & PF_W ? 'W' : ' '),
5228 (segment->p_flags & PF_X ? 'E' : ' '));
5229 print_vma (segment->p_align, PREFIX_HEX);
5230 }
5231
5232 putc ('\n', stdout);
5233 }
5234
5235 switch (segment->p_type)
5236 {
5237 case PT_LOAD:
5238 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5239 required by the ELF standard, several programs, including the Linux
5240 kernel, make use of non-ordered segments. */
5241 if (previous_load
5242 && previous_load->p_vaddr > segment->p_vaddr)
5243 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5244 #endif
5245 if (segment->p_memsz < segment->p_filesz)
5246 error (_("the segment's file size is larger than its memory size\n"));
5247 previous_load = segment;
5248 break;
5249
5250 case PT_PHDR:
5251 /* PR 20815 - Verify that the program header is loaded into memory. */
5252 if (i > 0 && previous_load != NULL)
5253 error (_("the PHDR segment must occur before any LOAD segment\n"));
5254 if (filedata->file_header.e_machine != EM_PARISC)
5255 {
5256 unsigned int j;
5257
5258 for (j = 1; j < filedata->file_header.e_phnum; j++)
5259 if (filedata->program_headers[j].p_vaddr <= segment->p_vaddr
5260 && (filedata->program_headers[j].p_vaddr
5261 + filedata->program_headers[j].p_memsz)
5262 >= (segment->p_vaddr + segment->p_filesz))
5263 break;
5264 if (j == filedata->file_header.e_phnum)
5265 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5266 }
5267 break;
5268
5269 case PT_DYNAMIC:
5270 if (dynamic_addr)
5271 error (_("more than one dynamic segment\n"));
5272
5273 /* By default, assume that the .dynamic section is the first
5274 section in the DYNAMIC segment. */
5275 dynamic_addr = segment->p_offset;
5276 dynamic_size = segment->p_filesz;
5277
5278 /* Try to locate the .dynamic section. If there is
5279 a section header table, we can easily locate it. */
5280 if (filedata->section_headers != NULL)
5281 {
5282 Elf_Internal_Shdr * sec;
5283
5284 sec = find_section (filedata, ".dynamic");
5285 if (sec == NULL || sec->sh_size == 0)
5286 {
5287 /* A corresponding .dynamic section is expected, but on
5288 IA-64/OpenVMS it is OK for it to be missing. */
5289 if (!is_ia64_vms (filedata))
5290 error (_("no .dynamic section in the dynamic segment\n"));
5291 break;
5292 }
5293
5294 if (sec->sh_type == SHT_NOBITS)
5295 {
5296 dynamic_size = 0;
5297 break;
5298 }
5299
5300 dynamic_addr = sec->sh_offset;
5301 dynamic_size = sec->sh_size;
5302
5303 if (dynamic_addr < segment->p_offset
5304 || dynamic_addr > segment->p_offset + segment->p_filesz)
5305 warn (_("the .dynamic section is not contained"
5306 " within the dynamic segment\n"));
5307 else if (dynamic_addr > segment->p_offset)
5308 warn (_("the .dynamic section is not the first section"
5309 " in the dynamic segment.\n"));
5310 }
5311
5312 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5313 segment. Check this after matching against the section headers
5314 so we don't warn on debuginfo file (which have NOBITS .dynamic
5315 sections). */
5316 if (dynamic_addr > filedata->file_size
5317 || dynamic_size > filedata->file_size - dynamic_addr)
5318 {
5319 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5320 dynamic_addr = dynamic_size = 0;
5321 }
5322 break;
5323
5324 case PT_INTERP:
5325 if (fseek (filedata->handle, archive_file_offset + (long) segment->p_offset,
5326 SEEK_SET))
5327 error (_("Unable to find program interpreter name\n"));
5328 else
5329 {
5330 char fmt [32];
5331 int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
5332
5333 if (ret >= (int) sizeof (fmt) || ret < 0)
5334 error (_("Internal error: failed to create format string to display program interpreter\n"));
5335
5336 program_interpreter[0] = 0;
5337 if (fscanf (filedata->handle, fmt, program_interpreter) <= 0)
5338 error (_("Unable to read program interpreter name\n"));
5339
5340 if (do_segments)
5341 printf (_(" [Requesting program interpreter: %s]\n"),
5342 program_interpreter);
5343 }
5344 break;
5345 }
5346 }
5347
5348 if (do_segments
5349 && filedata->section_headers != NULL
5350 && filedata->string_table != NULL)
5351 {
5352 printf (_("\n Section to Segment mapping:\n"));
5353 printf (_(" Segment Sections...\n"));
5354
5355 for (i = 0; i < filedata->file_header.e_phnum; i++)
5356 {
5357 unsigned int j;
5358 Elf_Internal_Shdr * section;
5359
5360 segment = filedata->program_headers + i;
5361 section = filedata->section_headers + 1;
5362
5363 printf (" %2.2d ", i);
5364
5365 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5366 {
5367 if (!ELF_TBSS_SPECIAL (section, segment)
5368 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5369 printf ("%s ", printable_section_name (filedata, section));
5370 }
5371
5372 putc ('\n',stdout);
5373 }
5374 }
5375
5376 return TRUE;
5377 }
5378
5379
5380 /* Find the file offset corresponding to VMA by using the program headers. */
5381
5382 static long
5383 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5384 {
5385 Elf_Internal_Phdr * seg;
5386
5387 if (! get_program_headers (filedata))
5388 {
5389 warn (_("Cannot interpret virtual addresses without program headers.\n"));
5390 return (long) vma;
5391 }
5392
5393 for (seg = filedata->program_headers;
5394 seg < filedata->program_headers + filedata->file_header.e_phnum;
5395 ++seg)
5396 {
5397 if (seg->p_type != PT_LOAD)
5398 continue;
5399
5400 if (vma >= (seg->p_vaddr & -seg->p_align)
5401 && vma + size <= seg->p_vaddr + seg->p_filesz)
5402 return vma - seg->p_vaddr + seg->p_offset;
5403 }
5404
5405 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5406 (unsigned long) vma);
5407 return (long) vma;
5408 }
5409
5410
5411 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5412 If PROBE is true, this is just a probe and we do not generate any error
5413 messages if the load fails. */
5414
5415 static bfd_boolean
5416 get_32bit_section_headers (Filedata * filedata, bfd_boolean probe)
5417 {
5418 Elf32_External_Shdr * shdrs;
5419 Elf_Internal_Shdr * internal;
5420 unsigned int i;
5421 unsigned int size = filedata->file_header.e_shentsize;
5422 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5423
5424 /* PR binutils/17531: Cope with unexpected section header sizes. */
5425 if (size == 0 || num == 0)
5426 return FALSE;
5427 if (size < sizeof * shdrs)
5428 {
5429 if (! probe)
5430 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5431 return FALSE;
5432 }
5433 if (!probe && size > sizeof * shdrs)
5434 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5435
5436 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5437 size, num,
5438 probe ? NULL : _("section headers"));
5439 if (shdrs == NULL)
5440 return FALSE;
5441
5442 free (filedata->section_headers);
5443 filedata->section_headers = (Elf_Internal_Shdr *)
5444 cmalloc (num, sizeof (Elf_Internal_Shdr));
5445 if (filedata->section_headers == NULL)
5446 {
5447 if (!probe)
5448 error (_("Out of memory reading %u section headers\n"), num);
5449 free (shdrs);
5450 return FALSE;
5451 }
5452
5453 for (i = 0, internal = filedata->section_headers;
5454 i < num;
5455 i++, internal++)
5456 {
5457 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5458 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5459 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5460 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5461 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5462 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5463 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5464 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5465 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5466 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5467 if (!probe && internal->sh_link > num)
5468 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5469 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5470 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5471 }
5472
5473 free (shdrs);
5474 return TRUE;
5475 }
5476
5477 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
5478
5479 static bfd_boolean
5480 get_64bit_section_headers (Filedata * filedata, bfd_boolean probe)
5481 {
5482 Elf64_External_Shdr * shdrs;
5483 Elf_Internal_Shdr * internal;
5484 unsigned int i;
5485 unsigned int size = filedata->file_header.e_shentsize;
5486 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5487
5488 /* PR binutils/17531: Cope with unexpected section header sizes. */
5489 if (size == 0 || num == 0)
5490 return FALSE;
5491
5492 if (size < sizeof * shdrs)
5493 {
5494 if (! probe)
5495 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5496 return FALSE;
5497 }
5498
5499 if (! probe && size > sizeof * shdrs)
5500 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5501
5502 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5503 filedata->file_header.e_shoff,
5504 size, num,
5505 probe ? NULL : _("section headers"));
5506 if (shdrs == NULL)
5507 return FALSE;
5508
5509 free (filedata->section_headers);
5510 filedata->section_headers = (Elf_Internal_Shdr *)
5511 cmalloc (num, sizeof (Elf_Internal_Shdr));
5512 if (filedata->section_headers == NULL)
5513 {
5514 if (! probe)
5515 error (_("Out of memory reading %u section headers\n"), num);
5516 free (shdrs);
5517 return FALSE;
5518 }
5519
5520 for (i = 0, internal = filedata->section_headers;
5521 i < num;
5522 i++, internal++)
5523 {
5524 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5525 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5526 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5527 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5528 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5529 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5530 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5531 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5532 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5533 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5534 if (!probe && internal->sh_link > num)
5535 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5536 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5537 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5538 }
5539
5540 free (shdrs);
5541 return TRUE;
5542 }
5543
5544 static Elf_Internal_Sym *
5545 get_32bit_elf_symbols (Filedata * filedata,
5546 Elf_Internal_Shdr * section,
5547 unsigned long * num_syms_return)
5548 {
5549 unsigned long number = 0;
5550 Elf32_External_Sym * esyms = NULL;
5551 Elf_External_Sym_Shndx * shndx = NULL;
5552 Elf_Internal_Sym * isyms = NULL;
5553 Elf_Internal_Sym * psym;
5554 unsigned int j;
5555 elf_section_list * entry;
5556
5557 if (section->sh_size == 0)
5558 {
5559 if (num_syms_return != NULL)
5560 * num_syms_return = 0;
5561 return NULL;
5562 }
5563
5564 /* Run some sanity checks first. */
5565 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5566 {
5567 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5568 printable_section_name (filedata, section),
5569 (unsigned long) section->sh_entsize);
5570 goto exit_point;
5571 }
5572
5573 if (section->sh_size > filedata->file_size)
5574 {
5575 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5576 printable_section_name (filedata, section),
5577 (unsigned long) section->sh_size);
5578 goto exit_point;
5579 }
5580
5581 number = section->sh_size / section->sh_entsize;
5582
5583 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5584 {
5585 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5586 (unsigned long) section->sh_size,
5587 printable_section_name (filedata, section),
5588 (unsigned long) section->sh_entsize);
5589 goto exit_point;
5590 }
5591
5592 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5593 section->sh_size, _("symbols"));
5594 if (esyms == NULL)
5595 goto exit_point;
5596
5597 shndx = NULL;
5598 for (entry = symtab_shndx_list; entry != NULL; entry = entry->next)
5599 {
5600 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5601 continue;
5602
5603 if (shndx != NULL)
5604 {
5605 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5606 free (shndx);
5607 }
5608
5609 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5610 entry->hdr->sh_offset,
5611 1, entry->hdr->sh_size,
5612 _("symbol table section indices"));
5613 if (shndx == NULL)
5614 goto exit_point;
5615
5616 /* PR17531: file: heap-buffer-overflow */
5617 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5618 {
5619 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5620 printable_section_name (filedata, entry->hdr),
5621 (unsigned long) entry->hdr->sh_size,
5622 (unsigned long) section->sh_size);
5623 goto exit_point;
5624 }
5625 }
5626
5627 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5628
5629 if (isyms == NULL)
5630 {
5631 error (_("Out of memory reading %lu symbols\n"),
5632 (unsigned long) number);
5633 goto exit_point;
5634 }
5635
5636 for (j = 0, psym = isyms; j < number; j++, psym++)
5637 {
5638 psym->st_name = BYTE_GET (esyms[j].st_name);
5639 psym->st_value = BYTE_GET (esyms[j].st_value);
5640 psym->st_size = BYTE_GET (esyms[j].st_size);
5641 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5642 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5643 psym->st_shndx
5644 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5645 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5646 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5647 psym->st_info = BYTE_GET (esyms[j].st_info);
5648 psym->st_other = BYTE_GET (esyms[j].st_other);
5649 }
5650
5651 exit_point:
5652 free (shndx);
5653 free (esyms);
5654
5655 if (num_syms_return != NULL)
5656 * num_syms_return = isyms == NULL ? 0 : number;
5657
5658 return isyms;
5659 }
5660
5661 static Elf_Internal_Sym *
5662 get_64bit_elf_symbols (Filedata * filedata,
5663 Elf_Internal_Shdr * section,
5664 unsigned long * num_syms_return)
5665 {
5666 unsigned long number = 0;
5667 Elf64_External_Sym * esyms = NULL;
5668 Elf_External_Sym_Shndx * shndx = NULL;
5669 Elf_Internal_Sym * isyms = NULL;
5670 Elf_Internal_Sym * psym;
5671 unsigned int j;
5672 elf_section_list * entry;
5673
5674 if (section->sh_size == 0)
5675 {
5676 if (num_syms_return != NULL)
5677 * num_syms_return = 0;
5678 return NULL;
5679 }
5680
5681 /* Run some sanity checks first. */
5682 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5683 {
5684 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5685 printable_section_name (filedata, section),
5686 (unsigned long) section->sh_entsize);
5687 goto exit_point;
5688 }
5689
5690 if (section->sh_size > filedata->file_size)
5691 {
5692 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5693 printable_section_name (filedata, section),
5694 (unsigned long) section->sh_size);
5695 goto exit_point;
5696 }
5697
5698 number = section->sh_size / section->sh_entsize;
5699
5700 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
5701 {
5702 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5703 (unsigned long) section->sh_size,
5704 printable_section_name (filedata, section),
5705 (unsigned long) section->sh_entsize);
5706 goto exit_point;
5707 }
5708
5709 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5710 section->sh_size, _("symbols"));
5711 if (!esyms)
5712 goto exit_point;
5713
5714 shndx = NULL;
5715 for (entry = symtab_shndx_list; entry != NULL; entry = entry->next)
5716 {
5717 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5718 continue;
5719
5720 if (shndx != NULL)
5721 {
5722 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5723 free (shndx);
5724 }
5725
5726 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5727 entry->hdr->sh_offset,
5728 1, entry->hdr->sh_size,
5729 _("symbol table section indices"));
5730 if (shndx == NULL)
5731 goto exit_point;
5732
5733 /* PR17531: file: heap-buffer-overflow */
5734 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5735 {
5736 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5737 printable_section_name (filedata, entry->hdr),
5738 (unsigned long) entry->hdr->sh_size,
5739 (unsigned long) section->sh_size);
5740 goto exit_point;
5741 }
5742 }
5743
5744 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5745
5746 if (isyms == NULL)
5747 {
5748 error (_("Out of memory reading %lu symbols\n"),
5749 (unsigned long) number);
5750 goto exit_point;
5751 }
5752
5753 for (j = 0, psym = isyms; j < number; j++, psym++)
5754 {
5755 psym->st_name = BYTE_GET (esyms[j].st_name);
5756 psym->st_info = BYTE_GET (esyms[j].st_info);
5757 psym->st_other = BYTE_GET (esyms[j].st_other);
5758 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5759
5760 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5761 psym->st_shndx
5762 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5763 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5764 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5765
5766 psym->st_value = BYTE_GET (esyms[j].st_value);
5767 psym->st_size = BYTE_GET (esyms[j].st_size);
5768 }
5769
5770 exit_point:
5771 free (shndx);
5772 free (esyms);
5773
5774 if (num_syms_return != NULL)
5775 * num_syms_return = isyms == NULL ? 0 : number;
5776
5777 return isyms;
5778 }
5779
5780 static const char *
5781 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
5782 {
5783 static char buff[1024];
5784 char * p = buff;
5785 unsigned int field_size = is_32bit_elf ? 8 : 16;
5786 signed int sindex;
5787 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
5788 bfd_vma os_flags = 0;
5789 bfd_vma proc_flags = 0;
5790 bfd_vma unknown_flags = 0;
5791 static const struct
5792 {
5793 const char * str;
5794 unsigned int len;
5795 }
5796 flags [] =
5797 {
5798 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
5799 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
5800 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
5801 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
5802 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
5803 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
5804 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
5805 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
5806 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
5807 /* 9 */ { STRING_COMMA_LEN ("TLS") },
5808 /* IA-64 specific. */
5809 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
5810 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
5811 /* IA-64 OpenVMS specific. */
5812 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
5813 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
5814 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
5815 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
5816 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
5817 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
5818 /* Generic. */
5819 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
5820 /* SPARC specific. */
5821 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
5822 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
5823 /* ARM specific. */
5824 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
5825 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
5826 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
5827 /* GNU specific. */
5828 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
5829 /* VLE specific. */
5830 /* 25 */ { STRING_COMMA_LEN ("VLE") },
5831 };
5832
5833 if (do_section_details)
5834 {
5835 sprintf (buff, "[%*.*lx]: ",
5836 field_size, field_size, (unsigned long) sh_flags);
5837 p += field_size + 4;
5838 }
5839
5840 while (sh_flags)
5841 {
5842 bfd_vma flag;
5843
5844 flag = sh_flags & - sh_flags;
5845 sh_flags &= ~ flag;
5846
5847 if (do_section_details)
5848 {
5849 switch (flag)
5850 {
5851 case SHF_WRITE: sindex = 0; break;
5852 case SHF_ALLOC: sindex = 1; break;
5853 case SHF_EXECINSTR: sindex = 2; break;
5854 case SHF_MERGE: sindex = 3; break;
5855 case SHF_STRINGS: sindex = 4; break;
5856 case SHF_INFO_LINK: sindex = 5; break;
5857 case SHF_LINK_ORDER: sindex = 6; break;
5858 case SHF_OS_NONCONFORMING: sindex = 7; break;
5859 case SHF_GROUP: sindex = 8; break;
5860 case SHF_TLS: sindex = 9; break;
5861 case SHF_EXCLUDE: sindex = 18; break;
5862 case SHF_COMPRESSED: sindex = 20; break;
5863 case SHF_GNU_MBIND: sindex = 24; break;
5864
5865 default:
5866 sindex = -1;
5867 switch (filedata->file_header.e_machine)
5868 {
5869 case EM_IA_64:
5870 if (flag == SHF_IA_64_SHORT)
5871 sindex = 10;
5872 else if (flag == SHF_IA_64_NORECOV)
5873 sindex = 11;
5874 #ifdef BFD64
5875 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
5876 switch (flag)
5877 {
5878 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
5879 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
5880 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
5881 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
5882 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
5883 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
5884 default: break;
5885 }
5886 #endif
5887 break;
5888
5889 case EM_386:
5890 case EM_IAMCU:
5891 case EM_X86_64:
5892 case EM_L1OM:
5893 case EM_K1OM:
5894 case EM_OLD_SPARCV9:
5895 case EM_SPARC32PLUS:
5896 case EM_SPARCV9:
5897 case EM_SPARC:
5898 if (flag == SHF_ORDERED)
5899 sindex = 19;
5900 break;
5901
5902 case EM_ARM:
5903 switch (flag)
5904 {
5905 case SHF_ENTRYSECT: sindex = 21; break;
5906 case SHF_ARM_PURECODE: sindex = 22; break;
5907 case SHF_COMDEF: sindex = 23; break;
5908 default: break;
5909 }
5910 break;
5911 case EM_PPC:
5912 if (flag == SHF_PPC_VLE)
5913 sindex = 25;
5914 break;
5915
5916 default:
5917 break;
5918 }
5919 }
5920
5921 if (sindex != -1)
5922 {
5923 if (p != buff + field_size + 4)
5924 {
5925 if (size < (10 + 2))
5926 {
5927 warn (_("Internal error: not enough buffer room for section flag info"));
5928 return _("<unknown>");
5929 }
5930 size -= 2;
5931 *p++ = ',';
5932 *p++ = ' ';
5933 }
5934
5935 size -= flags [sindex].len;
5936 p = stpcpy (p, flags [sindex].str);
5937 }
5938 else if (flag & SHF_MASKOS)
5939 os_flags |= flag;
5940 else if (flag & SHF_MASKPROC)
5941 proc_flags |= flag;
5942 else
5943 unknown_flags |= flag;
5944 }
5945 else
5946 {
5947 switch (flag)
5948 {
5949 case SHF_WRITE: *p = 'W'; break;
5950 case SHF_ALLOC: *p = 'A'; break;
5951 case SHF_EXECINSTR: *p = 'X'; break;
5952 case SHF_MERGE: *p = 'M'; break;
5953 case SHF_STRINGS: *p = 'S'; break;
5954 case SHF_INFO_LINK: *p = 'I'; break;
5955 case SHF_LINK_ORDER: *p = 'L'; break;
5956 case SHF_OS_NONCONFORMING: *p = 'O'; break;
5957 case SHF_GROUP: *p = 'G'; break;
5958 case SHF_TLS: *p = 'T'; break;
5959 case SHF_EXCLUDE: *p = 'E'; break;
5960 case SHF_COMPRESSED: *p = 'C'; break;
5961 case SHF_GNU_MBIND: *p = 'D'; break;
5962
5963 default:
5964 if ((filedata->file_header.e_machine == EM_X86_64
5965 || filedata->file_header.e_machine == EM_L1OM
5966 || filedata->file_header.e_machine == EM_K1OM)
5967 && flag == SHF_X86_64_LARGE)
5968 *p = 'l';
5969 else if (filedata->file_header.e_machine == EM_ARM
5970 && flag == SHF_ARM_PURECODE)
5971 *p = 'y';
5972 else if (filedata->file_header.e_machine == EM_PPC
5973 && flag == SHF_PPC_VLE)
5974 *p = 'v';
5975 else if (flag & SHF_MASKOS)
5976 {
5977 *p = 'o';
5978 sh_flags &= ~ SHF_MASKOS;
5979 }
5980 else if (flag & SHF_MASKPROC)
5981 {
5982 *p = 'p';
5983 sh_flags &= ~ SHF_MASKPROC;
5984 }
5985 else
5986 *p = 'x';
5987 break;
5988 }
5989 p++;
5990 }
5991 }
5992
5993 if (do_section_details)
5994 {
5995 if (os_flags)
5996 {
5997 size -= 5 + field_size;
5998 if (p != buff + field_size + 4)
5999 {
6000 if (size < (2 + 1))
6001 {
6002 warn (_("Internal error: not enough buffer room for section flag info"));
6003 return _("<unknown>");
6004 }
6005 size -= 2;
6006 *p++ = ',';
6007 *p++ = ' ';
6008 }
6009 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6010 (unsigned long) os_flags);
6011 p += 5 + field_size;
6012 }
6013 if (proc_flags)
6014 {
6015 size -= 7 + field_size;
6016 if (p != buff + field_size + 4)
6017 {
6018 if (size < (2 + 1))
6019 {
6020 warn (_("Internal error: not enough buffer room for section flag info"));
6021 return _("<unknown>");
6022 }
6023 size -= 2;
6024 *p++ = ',';
6025 *p++ = ' ';
6026 }
6027 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6028 (unsigned long) proc_flags);
6029 p += 7 + field_size;
6030 }
6031 if (unknown_flags)
6032 {
6033 size -= 10 + field_size;
6034 if (p != buff + field_size + 4)
6035 {
6036 if (size < (2 + 1))
6037 {
6038 warn (_("Internal error: not enough buffer room for section flag info"));
6039 return _("<unknown>");
6040 }
6041 size -= 2;
6042 *p++ = ',';
6043 *p++ = ' ';
6044 }
6045 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6046 (unsigned long) unknown_flags);
6047 p += 10 + field_size;
6048 }
6049 }
6050
6051 *p = '\0';
6052 return buff;
6053 }
6054
6055 static unsigned int
6056 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6057 {
6058 if (is_32bit_elf)
6059 {
6060 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6061
6062 if (size < sizeof (* echdr))
6063 {
6064 error (_("Compressed section is too small even for a compression header\n"));
6065 return 0;
6066 }
6067
6068 chdr->ch_type = BYTE_GET (echdr->ch_type);
6069 chdr->ch_size = BYTE_GET (echdr->ch_size);
6070 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6071 return sizeof (*echdr);
6072 }
6073 else
6074 {
6075 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6076
6077 if (size < sizeof (* echdr))
6078 {
6079 error (_("Compressed section is too small even for a compression header\n"));
6080 return 0;
6081 }
6082
6083 chdr->ch_type = BYTE_GET (echdr->ch_type);
6084 chdr->ch_size = BYTE_GET (echdr->ch_size);
6085 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6086 return sizeof (*echdr);
6087 }
6088 }
6089
6090 static bfd_boolean
6091 process_section_headers (Filedata * filedata)
6092 {
6093 Elf_Internal_Shdr * section;
6094 unsigned int i;
6095
6096 filedata->section_headers = NULL;
6097
6098 if (filedata->file_header.e_shnum == 0)
6099 {
6100 /* PR binutils/12467. */
6101 if (filedata->file_header.e_shoff != 0)
6102 {
6103 warn (_("possibly corrupt ELF file header - it has a non-zero"
6104 " section header offset, but no section headers\n"));
6105 return FALSE;
6106 }
6107 else if (do_sections)
6108 printf (_("\nThere are no sections in this file.\n"));
6109
6110 return TRUE;
6111 }
6112
6113 if (do_sections && !do_header)
6114 printf (ngettext ("There is %d section header, "
6115 "starting at offset 0x%lx:\n",
6116 "There are %d section headers, "
6117 "starting at offset 0x%lx:\n",
6118 filedata->file_header.e_shnum),
6119 filedata->file_header.e_shnum,
6120 (unsigned long) filedata->file_header.e_shoff);
6121
6122 if (is_32bit_elf)
6123 {
6124 if (! get_32bit_section_headers (filedata, FALSE))
6125 return FALSE;
6126 }
6127 else
6128 {
6129 if (! get_64bit_section_headers (filedata, FALSE))
6130 return FALSE;
6131 }
6132
6133 /* Read in the string table, so that we have names to display. */
6134 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6135 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6136 {
6137 section = filedata->section_headers + filedata->file_header.e_shstrndx;
6138
6139 if (section->sh_size != 0)
6140 {
6141 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6142 1, section->sh_size,
6143 _("string table"));
6144
6145 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6146 }
6147 }
6148
6149 /* Scan the sections for the dynamic symbol table
6150 and dynamic string table and debug sections. */
6151 dynamic_symbols = NULL;
6152 dynamic_strings = NULL;
6153 dynamic_syminfo = NULL;
6154 symtab_shndx_list = NULL;
6155
6156 eh_addr_size = is_32bit_elf ? 4 : 8;
6157 switch (filedata->file_header.e_machine)
6158 {
6159 case EM_MIPS:
6160 case EM_MIPS_RS3_LE:
6161 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6162 FDE addresses. However, the ABI also has a semi-official ILP32
6163 variant for which the normal FDE address size rules apply.
6164
6165 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6166 section, where XX is the size of longs in bits. Unfortunately,
6167 earlier compilers provided no way of distinguishing ILP32 objects
6168 from LP64 objects, so if there's any doubt, we should assume that
6169 the official LP64 form is being used. */
6170 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6171 && find_section (filedata, ".gcc_compiled_long32") == NULL)
6172 eh_addr_size = 8;
6173 break;
6174
6175 case EM_H8_300:
6176 case EM_H8_300H:
6177 switch (filedata->file_header.e_flags & EF_H8_MACH)
6178 {
6179 case E_H8_MACH_H8300:
6180 case E_H8_MACH_H8300HN:
6181 case E_H8_MACH_H8300SN:
6182 case E_H8_MACH_H8300SXN:
6183 eh_addr_size = 2;
6184 break;
6185 case E_H8_MACH_H8300H:
6186 case E_H8_MACH_H8300S:
6187 case E_H8_MACH_H8300SX:
6188 eh_addr_size = 4;
6189 break;
6190 }
6191 break;
6192
6193 case EM_M32C_OLD:
6194 case EM_M32C:
6195 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6196 {
6197 case EF_M32C_CPU_M16C:
6198 eh_addr_size = 2;
6199 break;
6200 }
6201 break;
6202 }
6203
6204 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6205 do \
6206 { \
6207 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6208 if (section->sh_entsize != expected_entsize) \
6209 { \
6210 char buf[40]; \
6211 sprintf_vma (buf, section->sh_entsize); \
6212 /* Note: coded this way so that there is a single string for \
6213 translation. */ \
6214 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6215 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6216 (unsigned) expected_entsize); \
6217 section->sh_entsize = expected_entsize; \
6218 } \
6219 } \
6220 while (0)
6221
6222 #define CHECK_ENTSIZE(section, i, type) \
6223 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
6224 sizeof (Elf64_External_##type))
6225
6226 for (i = 0, section = filedata->section_headers;
6227 i < filedata->file_header.e_shnum;
6228 i++, section++)
6229 {
6230 char * name = SECTION_NAME (section);
6231
6232 if (section->sh_type == SHT_DYNSYM)
6233 {
6234 if (dynamic_symbols != NULL)
6235 {
6236 error (_("File contains multiple dynamic symbol tables\n"));
6237 continue;
6238 }
6239
6240 CHECK_ENTSIZE (section, i, Sym);
6241 dynamic_symbols = GET_ELF_SYMBOLS (filedata, section, & num_dynamic_syms);
6242 }
6243 else if (section->sh_type == SHT_STRTAB
6244 && streq (name, ".dynstr"))
6245 {
6246 if (dynamic_strings != NULL)
6247 {
6248 error (_("File contains multiple dynamic string tables\n"));
6249 continue;
6250 }
6251
6252 dynamic_strings = (char *) get_data (NULL, filedata, section->sh_offset,
6253 1, section->sh_size,
6254 _("dynamic strings"));
6255 dynamic_strings_length = dynamic_strings == NULL ? 0 : section->sh_size;
6256 }
6257 else if (section->sh_type == SHT_SYMTAB_SHNDX)
6258 {
6259 elf_section_list * entry = xmalloc (sizeof * entry);
6260
6261 entry->hdr = section;
6262 entry->next = symtab_shndx_list;
6263 symtab_shndx_list = entry;
6264 }
6265 else if (section->sh_type == SHT_SYMTAB)
6266 CHECK_ENTSIZE (section, i, Sym);
6267 else if (section->sh_type == SHT_GROUP)
6268 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6269 else if (section->sh_type == SHT_REL)
6270 CHECK_ENTSIZE (section, i, Rel);
6271 else if (section->sh_type == SHT_RELA)
6272 CHECK_ENTSIZE (section, i, Rela);
6273 else if ((do_debugging || do_debug_info || do_debug_abbrevs
6274 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6275 || do_debug_aranges || do_debug_frames || do_debug_macinfo
6276 || do_debug_str || do_debug_loc || do_debug_ranges
6277 || do_debug_addr || do_debug_cu_index || do_debug_links)
6278 && (const_strneq (name, ".debug_")
6279 || const_strneq (name, ".zdebug_")))
6280 {
6281 if (name[1] == 'z')
6282 name += sizeof (".zdebug_") - 1;
6283 else
6284 name += sizeof (".debug_") - 1;
6285
6286 if (do_debugging
6287 || (do_debug_info && const_strneq (name, "info"))
6288 || (do_debug_info && const_strneq (name, "types"))
6289 || (do_debug_abbrevs && const_strneq (name, "abbrev"))
6290 || (do_debug_lines && strcmp (name, "line") == 0)
6291 || (do_debug_lines && const_strneq (name, "line."))
6292 || (do_debug_pubnames && const_strneq (name, "pubnames"))
6293 || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
6294 || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
6295 || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
6296 || (do_debug_aranges && const_strneq (name, "aranges"))
6297 || (do_debug_ranges && const_strneq (name, "ranges"))
6298 || (do_debug_ranges && const_strneq (name, "rnglists"))
6299 || (do_debug_frames && const_strneq (name, "frame"))
6300 || (do_debug_macinfo && const_strneq (name, "macinfo"))
6301 || (do_debug_macinfo && const_strneq (name, "macro"))
6302 || (do_debug_str && const_strneq (name, "str"))
6303 || (do_debug_loc && const_strneq (name, "loc"))
6304 || (do_debug_loc && const_strneq (name, "loclists"))
6305 || (do_debug_addr && const_strneq (name, "addr"))
6306 || (do_debug_cu_index && const_strneq (name, "cu_index"))
6307 || (do_debug_cu_index && const_strneq (name, "tu_index"))
6308 )
6309 request_dump_bynumber (filedata, i, DEBUG_DUMP);
6310 }
6311 /* Linkonce section to be combined with .debug_info at link time. */
6312 else if ((do_debugging || do_debug_info)
6313 && const_strneq (name, ".gnu.linkonce.wi."))
6314 request_dump_bynumber (filedata, i, DEBUG_DUMP);
6315 else if (do_debug_frames && streq (name, ".eh_frame"))
6316 request_dump_bynumber (filedata, i, DEBUG_DUMP);
6317 else if (do_gdb_index && (streq (name, ".gdb_index")
6318 || streq (name, ".debug_names")))
6319 request_dump_bynumber (filedata, i, DEBUG_DUMP);
6320 /* Trace sections for Itanium VMS. */
6321 else if ((do_debugging || do_trace_info || do_trace_abbrevs
6322 || do_trace_aranges)
6323 && const_strneq (name, ".trace_"))
6324 {
6325 name += sizeof (".trace_") - 1;
6326
6327 if (do_debugging
6328 || (do_trace_info && streq (name, "info"))
6329 || (do_trace_abbrevs && streq (name, "abbrev"))
6330 || (do_trace_aranges && streq (name, "aranges"))
6331 )
6332 request_dump_bynumber (filedata, i, DEBUG_DUMP);
6333 }
6334 else if ((do_debugging || do_debug_links)
6335 && (const_strneq (name, ".gnu_debuglink")
6336 || const_strneq (name, ".gnu_debugaltlink")))
6337 request_dump_bynumber (filedata, i, DEBUG_DUMP);
6338 }
6339
6340 if (! do_sections)
6341 return TRUE;
6342
6343 if (filedata->file_header.e_shnum > 1)
6344 printf (_("\nSection Headers:\n"));
6345 else
6346 printf (_("\nSection Header:\n"));
6347
6348 if (is_32bit_elf)
6349 {
6350 if (do_section_details)
6351 {
6352 printf (_(" [Nr] Name\n"));
6353 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
6354 }
6355 else
6356 printf
6357 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
6358 }
6359 else if (do_wide)
6360 {
6361 if (do_section_details)
6362 {
6363 printf (_(" [Nr] Name\n"));
6364 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
6365 }
6366 else
6367 printf
6368 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
6369 }
6370 else
6371 {
6372 if (do_section_details)
6373 {
6374 printf (_(" [Nr] Name\n"));
6375 printf (_(" Type Address Offset Link\n"));
6376 printf (_(" Size EntSize Info Align\n"));
6377 }
6378 else
6379 {
6380 printf (_(" [Nr] Name Type Address Offset\n"));
6381 printf (_(" Size EntSize Flags Link Info Align\n"));
6382 }
6383 }
6384
6385 if (do_section_details)
6386 printf (_(" Flags\n"));
6387
6388 for (i = 0, section = filedata->section_headers;
6389 i < filedata->file_header.e_shnum;
6390 i++, section++)
6391 {
6392 /* Run some sanity checks on the section header. */
6393
6394 /* Check the sh_link field. */
6395 switch (section->sh_type)
6396 {
6397 case SHT_REL:
6398 case SHT_RELA:
6399 if (section->sh_link == 0
6400 && (filedata->file_header.e_type == ET_EXEC
6401 || filedata->file_header.e_type == ET_DYN))
6402 /* A dynamic relocation section where all entries use a
6403 zero symbol index need not specify a symtab section. */
6404 break;
6405 /* Fall through. */
6406 case SHT_SYMTAB_SHNDX:
6407 case SHT_GROUP:
6408 case SHT_HASH:
6409 case SHT_GNU_HASH:
6410 case SHT_GNU_versym:
6411 if (section->sh_link == 0
6412 || section->sh_link >= filedata->file_header.e_shnum
6413 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6414 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6415 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6416 i, section->sh_link);
6417 break;
6418
6419 case SHT_DYNAMIC:
6420 case SHT_SYMTAB:
6421 case SHT_DYNSYM:
6422 case SHT_GNU_verneed:
6423 case SHT_GNU_verdef:
6424 case SHT_GNU_LIBLIST:
6425 if (section->sh_link == 0
6426 || section->sh_link >= filedata->file_header.e_shnum
6427 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6428 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6429 i, section->sh_link);
6430 break;
6431
6432 case SHT_INIT_ARRAY:
6433 case SHT_FINI_ARRAY:
6434 case SHT_PREINIT_ARRAY:
6435 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6436 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6437 i, section->sh_link);
6438 break;
6439
6440 default:
6441 /* FIXME: Add support for target specific section types. */
6442 #if 0 /* Currently we do not check other section types as there are too
6443 many special cases. Stab sections for example have a type
6444 of SHT_PROGBITS but an sh_link field that links to the .stabstr
6445 section. */
6446 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6447 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6448 i, section->sh_link);
6449 #endif
6450 break;
6451 }
6452
6453 /* Check the sh_info field. */
6454 switch (section->sh_type)
6455 {
6456 case SHT_REL:
6457 case SHT_RELA:
6458 if (section->sh_info == 0
6459 && (filedata->file_header.e_type == ET_EXEC
6460 || filedata->file_header.e_type == ET_DYN))
6461 /* Dynamic relocations apply to segments, so they do not
6462 need to specify the section they relocate. */
6463 break;
6464 if (section->sh_info == 0
6465 || section->sh_info >= filedata->file_header.e_shnum
6466 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
6467 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
6468 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
6469 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
6470 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
6471 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
6472 /* FIXME: Are other section types valid ? */
6473 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
6474 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
6475 i, section->sh_info);
6476 break;
6477
6478 case SHT_DYNAMIC:
6479 case SHT_HASH:
6480 case SHT_SYMTAB_SHNDX:
6481 case SHT_INIT_ARRAY:
6482 case SHT_FINI_ARRAY:
6483 case SHT_PREINIT_ARRAY:
6484 if (section->sh_info != 0)
6485 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6486 i, section->sh_info);
6487 break;
6488
6489 case SHT_GROUP:
6490 case SHT_SYMTAB:
6491 case SHT_DYNSYM:
6492 /* A symbol index - we assume that it is valid. */
6493 break;
6494
6495 default:
6496 /* FIXME: Add support for target specific section types. */
6497 if (section->sh_type == SHT_NOBITS)
6498 /* NOBITS section headers with non-zero sh_info fields can be
6499 created when a binary is stripped of everything but its debug
6500 information. The stripped sections have their headers
6501 preserved but their types set to SHT_NOBITS. So do not check
6502 this type of section. */
6503 ;
6504 else if (section->sh_flags & SHF_INFO_LINK)
6505 {
6506 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
6507 warn (_("[%2u]: Expected link to another section in info field"), i);
6508 }
6509 else if (section->sh_type < SHT_LOOS
6510 && (section->sh_flags & SHF_GNU_MBIND) == 0
6511 && section->sh_info != 0)
6512 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6513 i, section->sh_info);
6514 break;
6515 }
6516
6517 /* Check the sh_size field. */
6518 if (section->sh_size > filedata->file_size
6519 && section->sh_type != SHT_NOBITS
6520 && section->sh_type != SHT_NULL
6521 && section->sh_type < SHT_LOOS)
6522 warn (_("Size of section %u is larger than the entire file!\n"), i);
6523
6524 printf (" [%2u] ", i);
6525 if (do_section_details)
6526 printf ("%s\n ", printable_section_name (filedata, section));
6527 else
6528 print_symbol (-17, SECTION_NAME (section));
6529
6530 printf (do_wide ? " %-15s " : " %-15.15s ",
6531 get_section_type_name (filedata, section->sh_type));
6532
6533 if (is_32bit_elf)
6534 {
6535 const char * link_too_big = NULL;
6536
6537 print_vma (section->sh_addr, LONG_HEX);
6538
6539 printf ( " %6.6lx %6.6lx %2.2lx",
6540 (unsigned long) section->sh_offset,
6541 (unsigned long) section->sh_size,
6542 (unsigned long) section->sh_entsize);
6543
6544 if (do_section_details)
6545 fputs (" ", stdout);
6546 else
6547 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6548
6549 if (section->sh_link >= filedata->file_header.e_shnum)
6550 {
6551 link_too_big = "";
6552 /* The sh_link value is out of range. Normally this indicates
6553 an error but it can have special values in Solaris binaries. */
6554 switch (filedata->file_header.e_machine)
6555 {
6556 case EM_386:
6557 case EM_IAMCU:
6558 case EM_X86_64:
6559 case EM_L1OM:
6560 case EM_K1OM:
6561 case EM_OLD_SPARCV9:
6562 case EM_SPARC32PLUS:
6563 case EM_SPARCV9:
6564 case EM_SPARC:
6565 if (section->sh_link == (SHN_BEFORE & 0xffff))
6566 link_too_big = "BEFORE";
6567 else if (section->sh_link == (SHN_AFTER & 0xffff))
6568 link_too_big = "AFTER";
6569 break;
6570 default:
6571 break;
6572 }
6573 }
6574
6575 if (do_section_details)
6576 {
6577 if (link_too_big != NULL && * link_too_big)
6578 printf ("<%s> ", link_too_big);
6579 else
6580 printf ("%2u ", section->sh_link);
6581 printf ("%3u %2lu\n", section->sh_info,
6582 (unsigned long) section->sh_addralign);
6583 }
6584 else
6585 printf ("%2u %3u %2lu\n",
6586 section->sh_link,
6587 section->sh_info,
6588 (unsigned long) section->sh_addralign);
6589
6590 if (link_too_big && ! * link_too_big)
6591 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
6592 i, section->sh_link);
6593 }
6594 else if (do_wide)
6595 {
6596 print_vma (section->sh_addr, LONG_HEX);
6597
6598 if ((long) section->sh_offset == section->sh_offset)
6599 printf (" %6.6lx", (unsigned long) section->sh_offset);
6600 else
6601 {
6602 putchar (' ');
6603 print_vma (section->sh_offset, LONG_HEX);
6604 }
6605
6606 if ((unsigned long) section->sh_size == section->sh_size)
6607 printf (" %6.6lx", (unsigned long) section->sh_size);
6608 else
6609 {
6610 putchar (' ');
6611 print_vma (section->sh_size, LONG_HEX);
6612 }
6613
6614 if ((unsigned long) section->sh_entsize == section->sh_entsize)
6615 printf (" %2.2lx", (unsigned long) section->sh_entsize);
6616 else
6617 {
6618 putchar (' ');
6619 print_vma (section->sh_entsize, LONG_HEX);
6620 }
6621
6622 if (do_section_details)
6623 fputs (" ", stdout);
6624 else
6625 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6626
6627 printf ("%2u %3u ", section->sh_link, section->sh_info);
6628
6629 if ((unsigned long) section->sh_addralign == section->sh_addralign)
6630 printf ("%2lu\n", (unsigned long) section->sh_addralign);
6631 else
6632 {
6633 print_vma (section->sh_addralign, DEC);
6634 putchar ('\n');
6635 }
6636 }
6637 else if (do_section_details)
6638 {
6639 putchar (' ');
6640 print_vma (section->sh_addr, LONG_HEX);
6641 if ((long) section->sh_offset == section->sh_offset)
6642 printf (" %16.16lx", (unsigned long) section->sh_offset);
6643 else
6644 {
6645 printf (" ");
6646 print_vma (section->sh_offset, LONG_HEX);
6647 }
6648 printf (" %u\n ", section->sh_link);
6649 print_vma (section->sh_size, LONG_HEX);
6650 putchar (' ');
6651 print_vma (section->sh_entsize, LONG_HEX);
6652
6653 printf (" %-16u %lu\n",
6654 section->sh_info,
6655 (unsigned long) section->sh_addralign);
6656 }
6657 else
6658 {
6659 putchar (' ');
6660 print_vma (section->sh_addr, LONG_HEX);
6661 if ((long) section->sh_offset == section->sh_offset)
6662 printf (" %8.8lx", (unsigned long) section->sh_offset);
6663 else
6664 {
6665 printf (" ");
6666 print_vma (section->sh_offset, LONG_HEX);
6667 }
6668 printf ("\n ");
6669 print_vma (section->sh_size, LONG_HEX);
6670 printf (" ");
6671 print_vma (section->sh_entsize, LONG_HEX);
6672
6673 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6674
6675 printf (" %2u %3u %lu\n",
6676 section->sh_link,
6677 section->sh_info,
6678 (unsigned long) section->sh_addralign);
6679 }
6680
6681 if (do_section_details)
6682 {
6683 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
6684 if ((section->sh_flags & SHF_COMPRESSED) != 0)
6685 {
6686 /* Minimum section size is 12 bytes for 32-bit compression
6687 header + 12 bytes for compressed data header. */
6688 unsigned char buf[24];
6689
6690 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
6691 if (get_data (&buf, filedata, section->sh_offset, 1,
6692 sizeof (buf), _("compression header")))
6693 {
6694 Elf_Internal_Chdr chdr;
6695
6696 (void) get_compression_header (&chdr, buf, sizeof (buf));
6697
6698 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
6699 printf (" ZLIB, ");
6700 else
6701 printf (_(" [<unknown>: 0x%x], "),
6702 chdr.ch_type);
6703 print_vma (chdr.ch_size, LONG_HEX);
6704 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
6705 }
6706 }
6707 }
6708 }
6709
6710 if (!do_section_details)
6711 {
6712 /* The ordering of the letters shown here matches the ordering of the
6713 corresponding SHF_xxx values, and hence the order in which these
6714 letters will be displayed to the user. */
6715 printf (_("Key to Flags:\n\
6716 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
6717 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
6718 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
6719 if (filedata->file_header.e_machine == EM_X86_64
6720 || filedata->file_header.e_machine == EM_L1OM
6721 || filedata->file_header.e_machine == EM_K1OM)
6722 printf (_("l (large), "));
6723 else if (filedata->file_header.e_machine == EM_ARM)
6724 printf (_("y (purecode), "));
6725 else if (filedata->file_header.e_machine == EM_PPC)
6726 printf (_("v (VLE), "));
6727 printf ("p (processor specific)\n");
6728 }
6729
6730 return TRUE;
6731 }
6732
6733 static const char *
6734 get_group_flags (unsigned int flags)
6735 {
6736 static char buff[128];
6737
6738 if (flags == 0)
6739 return "";
6740 else if (flags == GRP_COMDAT)
6741 return "COMDAT ";
6742
6743 snprintf (buff, 14, _("[0x%x: "), flags);
6744
6745 flags &= ~ GRP_COMDAT;
6746 if (flags & GRP_MASKOS)
6747 {
6748 strcat (buff, "<OS specific>");
6749 flags &= ~ GRP_MASKOS;
6750 }
6751
6752 if (flags & GRP_MASKPROC)
6753 {
6754 strcat (buff, "<PROC specific>");
6755 flags &= ~ GRP_MASKPROC;
6756 }
6757
6758 if (flags)
6759 strcat (buff, "<unknown>");
6760
6761 strcat (buff, "]");
6762 return buff;
6763 }
6764
6765 static bfd_boolean
6766 process_section_groups (Filedata * filedata)
6767 {
6768 Elf_Internal_Shdr * section;
6769 unsigned int i;
6770 struct group * group;
6771 Elf_Internal_Shdr * symtab_sec;
6772 Elf_Internal_Shdr * strtab_sec;
6773 Elf_Internal_Sym * symtab;
6774 unsigned long num_syms;
6775 char * strtab;
6776 size_t strtab_size;
6777
6778 /* Don't process section groups unless needed. */
6779 if (!do_unwind && !do_section_groups)
6780 return TRUE;
6781
6782 if (filedata->file_header.e_shnum == 0)
6783 {
6784 if (do_section_groups)
6785 printf (_("\nThere are no sections to group in this file.\n"));
6786
6787 return TRUE;
6788 }
6789
6790 if (filedata->section_headers == NULL)
6791 {
6792 error (_("Section headers are not available!\n"));
6793 /* PR 13622: This can happen with a corrupt ELF header. */
6794 return FALSE;
6795 }
6796
6797 section_headers_groups = (struct group **) calloc (filedata->file_header.e_shnum,
6798 sizeof (struct group *));
6799
6800 if (section_headers_groups == NULL)
6801 {
6802 error (_("Out of memory reading %u section group headers\n"),
6803 filedata->file_header.e_shnum);
6804 return FALSE;
6805 }
6806
6807 /* Scan the sections for the group section. */
6808 group_count = 0;
6809 for (i = 0, section = filedata->section_headers;
6810 i < filedata->file_header.e_shnum;
6811 i++, section++)
6812 if (section->sh_type == SHT_GROUP)
6813 group_count++;
6814
6815 if (group_count == 0)
6816 {
6817 if (do_section_groups)
6818 printf (_("\nThere are no section groups in this file.\n"));
6819
6820 return TRUE;
6821 }
6822
6823 section_groups = (struct group *) calloc (group_count, sizeof (struct group));
6824
6825 if (section_groups == NULL)
6826 {
6827 error (_("Out of memory reading %lu groups\n"),
6828 (unsigned long) group_count);
6829 return FALSE;
6830 }
6831
6832 symtab_sec = NULL;
6833 strtab_sec = NULL;
6834 symtab = NULL;
6835 num_syms = 0;
6836 strtab = NULL;
6837 strtab_size = 0;
6838 for (i = 0, section = filedata->section_headers, group = section_groups;
6839 i < filedata->file_header.e_shnum;
6840 i++, section++)
6841 {
6842 if (section->sh_type == SHT_GROUP)
6843 {
6844 const char * name = printable_section_name (filedata, section);
6845 const char * group_name;
6846 unsigned char * start;
6847 unsigned char * indices;
6848 unsigned int entry, j, size;
6849 Elf_Internal_Shdr * sec;
6850 Elf_Internal_Sym * sym;
6851
6852 /* Get the symbol table. */
6853 if (section->sh_link >= filedata->file_header.e_shnum
6854 || ((sec = filedata->section_headers + section->sh_link)->sh_type
6855 != SHT_SYMTAB))
6856 {
6857 error (_("Bad sh_link in group section `%s'\n"), name);
6858 continue;
6859 }
6860
6861 if (symtab_sec != sec)
6862 {
6863 symtab_sec = sec;
6864 if (symtab)
6865 free (symtab);
6866 symtab = GET_ELF_SYMBOLS (filedata, symtab_sec, & num_syms);
6867 }
6868
6869 if (symtab == NULL)
6870 {
6871 error (_("Corrupt header in group section `%s'\n"), name);
6872 continue;
6873 }
6874
6875 if (section->sh_info >= num_syms)
6876 {
6877 error (_("Bad sh_info in group section `%s'\n"), name);
6878 continue;
6879 }
6880
6881 sym = symtab + section->sh_info;
6882
6883 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6884 {
6885 if (sym->st_shndx == 0
6886 || sym->st_shndx >= filedata->file_header.e_shnum)
6887 {
6888 error (_("Bad sh_info in group section `%s'\n"), name);
6889 continue;
6890 }
6891
6892 group_name = SECTION_NAME (filedata->section_headers + sym->st_shndx);
6893 strtab_sec = NULL;
6894 if (strtab)
6895 free (strtab);
6896 strtab = NULL;
6897 strtab_size = 0;
6898 }
6899 else
6900 {
6901 /* Get the string table. */
6902 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
6903 {
6904 strtab_sec = NULL;
6905 if (strtab)
6906 free (strtab);
6907 strtab = NULL;
6908 strtab_size = 0;
6909 }
6910 else if (strtab_sec
6911 != (sec = filedata->section_headers + symtab_sec->sh_link))
6912 {
6913 strtab_sec = sec;
6914 if (strtab)
6915 free (strtab);
6916
6917 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
6918 1, strtab_sec->sh_size,
6919 _("string table"));
6920 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
6921 }
6922 group_name = sym->st_name < strtab_size
6923 ? strtab + sym->st_name : _("<corrupt>");
6924 }
6925
6926 /* PR 17531: file: loop. */
6927 if (section->sh_entsize > section->sh_size)
6928 {
6929 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
6930 printable_section_name (filedata, section),
6931 (unsigned long) section->sh_entsize,
6932 (unsigned long) section->sh_size);
6933 continue;
6934 }
6935
6936 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
6937 1, section->sh_size,
6938 _("section data"));
6939 if (start == NULL)
6940 continue;
6941
6942 indices = start;
6943 size = (section->sh_size / section->sh_entsize) - 1;
6944 entry = byte_get (indices, 4);
6945 indices += 4;
6946
6947 if (do_section_groups)
6948 {
6949 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
6950 get_group_flags (entry), i, name, group_name, size);
6951
6952 printf (_(" [Index] Name\n"));
6953 }
6954
6955 group->group_index = i;
6956
6957 for (j = 0; j < size; j++)
6958 {
6959 struct group_list * g;
6960
6961 entry = byte_get (indices, 4);
6962 indices += 4;
6963
6964 if (entry >= filedata->file_header.e_shnum)
6965 {
6966 static unsigned num_group_errors = 0;
6967
6968 if (num_group_errors ++ < 10)
6969 {
6970 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
6971 entry, i, filedata->file_header.e_shnum - 1);
6972 if (num_group_errors == 10)
6973 warn (_("Further error messages about overlarge group section indices suppressed\n"));
6974 }
6975 continue;
6976 }
6977
6978 if (section_headers_groups [entry] != NULL)
6979 {
6980 if (entry)
6981 {
6982 static unsigned num_errs = 0;
6983
6984 if (num_errs ++ < 10)
6985 {
6986 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
6987 entry, i,
6988 section_headers_groups [entry]->group_index);
6989 if (num_errs == 10)
6990 warn (_("Further error messages about already contained group sections suppressed\n"));
6991 }
6992 continue;
6993 }
6994 else
6995 {
6996 /* Intel C/C++ compiler may put section 0 in a
6997 section group. We just warn it the first time
6998 and ignore it afterwards. */
6999 static bfd_boolean warned = FALSE;
7000 if (!warned)
7001 {
7002 error (_("section 0 in group section [%5u]\n"),
7003 section_headers_groups [entry]->group_index);
7004 warned = TRUE;
7005 }
7006 }
7007 }
7008
7009 section_headers_groups [entry] = group;
7010
7011 if (do_section_groups)
7012 {
7013 sec = filedata->section_headers + entry;
7014 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
7015 }
7016
7017 g = (struct group_list *) xmalloc (sizeof (struct group_list));
7018 g->section_index = entry;
7019 g->next = group->root;
7020 group->root = g;
7021 }
7022
7023 if (start)
7024 free (start);
7025
7026 group++;
7027 }
7028 }
7029
7030 if (symtab)
7031 free (symtab);
7032 if (strtab)
7033 free (strtab);
7034 return TRUE;
7035 }
7036
7037 /* Data used to display dynamic fixups. */
7038
7039 struct ia64_vms_dynfixup
7040 {
7041 bfd_vma needed_ident; /* Library ident number. */
7042 bfd_vma needed; /* Index in the dstrtab of the library name. */
7043 bfd_vma fixup_needed; /* Index of the library. */
7044 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7045 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7046 };
7047
7048 /* Data used to display dynamic relocations. */
7049
7050 struct ia64_vms_dynimgrela
7051 {
7052 bfd_vma img_rela_cnt; /* Number of relocations. */
7053 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7054 };
7055
7056 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7057 library). */
7058
7059 static bfd_boolean
7060 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7061 struct ia64_vms_dynfixup * fixup,
7062 const char * strtab,
7063 unsigned int strtab_sz)
7064 {
7065 Elf64_External_VMS_IMAGE_FIXUP * imfs;
7066 long i;
7067 const char * lib_name;
7068
7069 imfs = get_data (NULL, filedata, dynamic_addr + fixup->fixup_rela_off,
7070 1, fixup->fixup_rela_cnt * sizeof (*imfs),
7071 _("dynamic section image fixups"));
7072 if (!imfs)
7073 return FALSE;
7074
7075 if (fixup->needed < strtab_sz)
7076 lib_name = strtab + fixup->needed;
7077 else
7078 {
7079 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7080 (unsigned long) fixup->needed);
7081 lib_name = "???";
7082 }
7083 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7084 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7085 printf
7086 (_("Seg Offset Type SymVec DataType\n"));
7087
7088 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7089 {
7090 unsigned int type;
7091 const char *rtype;
7092
7093 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7094 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7095 type = BYTE_GET (imfs [i].type);
7096 rtype = elf_ia64_reloc_type (type);
7097 if (rtype == NULL)
7098 printf (" 0x%08x ", type);
7099 else
7100 printf (" %-32s ", rtype);
7101 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7102 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7103 }
7104
7105 free (imfs);
7106 return TRUE;
7107 }
7108
7109 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7110
7111 static bfd_boolean
7112 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
7113 {
7114 Elf64_External_VMS_IMAGE_RELA *imrs;
7115 long i;
7116
7117 imrs = get_data (NULL, filedata, dynamic_addr + imgrela->img_rela_off,
7118 1, imgrela->img_rela_cnt * sizeof (*imrs),
7119 _("dynamic section image relocations"));
7120 if (!imrs)
7121 return FALSE;
7122
7123 printf (_("\nImage relocs\n"));
7124 printf
7125 (_("Seg Offset Type Addend Seg Sym Off\n"));
7126
7127 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7128 {
7129 unsigned int type;
7130 const char *rtype;
7131
7132 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7133 printf ("%08" BFD_VMA_FMT "x ",
7134 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7135 type = BYTE_GET (imrs [i].type);
7136 rtype = elf_ia64_reloc_type (type);
7137 if (rtype == NULL)
7138 printf ("0x%08x ", type);
7139 else
7140 printf ("%-31s ", rtype);
7141 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7142 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7143 printf ("%08" BFD_VMA_FMT "x\n",
7144 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7145 }
7146
7147 free (imrs);
7148 return TRUE;
7149 }
7150
7151 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
7152
7153 static bfd_boolean
7154 process_ia64_vms_dynamic_relocs (Filedata * filedata)
7155 {
7156 struct ia64_vms_dynfixup fixup;
7157 struct ia64_vms_dynimgrela imgrela;
7158 Elf_Internal_Dyn *entry;
7159 bfd_vma strtab_off = 0;
7160 bfd_vma strtab_sz = 0;
7161 char *strtab = NULL;
7162 bfd_boolean res = TRUE;
7163
7164 memset (&fixup, 0, sizeof (fixup));
7165 memset (&imgrela, 0, sizeof (imgrela));
7166
7167 /* Note: the order of the entries is specified by the OpenVMS specs. */
7168 for (entry = dynamic_section;
7169 entry < dynamic_section + dynamic_nent;
7170 entry++)
7171 {
7172 switch (entry->d_tag)
7173 {
7174 case DT_IA_64_VMS_STRTAB_OFFSET:
7175 strtab_off = entry->d_un.d_val;
7176 break;
7177 case DT_STRSZ:
7178 strtab_sz = entry->d_un.d_val;
7179 if (strtab == NULL)
7180 strtab = get_data (NULL, filedata, dynamic_addr + strtab_off,
7181 1, strtab_sz, _("dynamic string section"));
7182 break;
7183
7184 case DT_IA_64_VMS_NEEDED_IDENT:
7185 fixup.needed_ident = entry->d_un.d_val;
7186 break;
7187 case DT_NEEDED:
7188 fixup.needed = entry->d_un.d_val;
7189 break;
7190 case DT_IA_64_VMS_FIXUP_NEEDED:
7191 fixup.fixup_needed = entry->d_un.d_val;
7192 break;
7193 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7194 fixup.fixup_rela_cnt = entry->d_un.d_val;
7195 break;
7196 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7197 fixup.fixup_rela_off = entry->d_un.d_val;
7198 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7199 res = FALSE;
7200 break;
7201 case DT_IA_64_VMS_IMG_RELA_CNT:
7202 imgrela.img_rela_cnt = entry->d_un.d_val;
7203 break;
7204 case DT_IA_64_VMS_IMG_RELA_OFF:
7205 imgrela.img_rela_off = entry->d_un.d_val;
7206 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7207 res = FALSE;
7208 break;
7209
7210 default:
7211 break;
7212 }
7213 }
7214
7215 if (strtab != NULL)
7216 free (strtab);
7217
7218 return res;
7219 }
7220
7221 static struct
7222 {
7223 const char * name;
7224 int reloc;
7225 int size;
7226 int rela;
7227 }
7228 dynamic_relocations [] =
7229 {
7230 { "REL", DT_REL, DT_RELSZ, FALSE },
7231 { "RELA", DT_RELA, DT_RELASZ, TRUE },
7232 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7233 };
7234
7235 /* Process the reloc section. */
7236
7237 static bfd_boolean
7238 process_relocs (Filedata * filedata)
7239 {
7240 unsigned long rel_size;
7241 unsigned long rel_offset;
7242
7243 if (!do_reloc)
7244 return TRUE;
7245
7246 if (do_using_dynamic)
7247 {
7248 int is_rela;
7249 const char * name;
7250 bfd_boolean has_dynamic_reloc;
7251 unsigned int i;
7252
7253 has_dynamic_reloc = FALSE;
7254
7255 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7256 {
7257 is_rela = dynamic_relocations [i].rela;
7258 name = dynamic_relocations [i].name;
7259 rel_size = dynamic_info [dynamic_relocations [i].size];
7260 rel_offset = dynamic_info [dynamic_relocations [i].reloc];
7261
7262 if (rel_size)
7263 has_dynamic_reloc = TRUE;
7264
7265 if (is_rela == UNKNOWN)
7266 {
7267 if (dynamic_relocations [i].reloc == DT_JMPREL)
7268 switch (dynamic_info[DT_PLTREL])
7269 {
7270 case DT_REL:
7271 is_rela = FALSE;
7272 break;
7273 case DT_RELA:
7274 is_rela = TRUE;
7275 break;
7276 }
7277 }
7278
7279 if (rel_size)
7280 {
7281 printf
7282 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7283 name, rel_offset, rel_size);
7284
7285 dump_relocations (filedata,
7286 offset_from_vma (filedata, rel_offset, rel_size),
7287 rel_size,
7288 dynamic_symbols, num_dynamic_syms,
7289 dynamic_strings, dynamic_strings_length,
7290 is_rela, TRUE /* is_dynamic */);
7291 }
7292 }
7293
7294 if (is_ia64_vms (filedata))
7295 if (process_ia64_vms_dynamic_relocs (filedata))
7296 has_dynamic_reloc = TRUE;
7297
7298 if (! has_dynamic_reloc)
7299 printf (_("\nThere are no dynamic relocations in this file.\n"));
7300 }
7301 else
7302 {
7303 Elf_Internal_Shdr * section;
7304 unsigned long i;
7305 bfd_boolean found = FALSE;
7306
7307 for (i = 0, section = filedata->section_headers;
7308 i < filedata->file_header.e_shnum;
7309 i++, section++)
7310 {
7311 if ( section->sh_type != SHT_RELA
7312 && section->sh_type != SHT_REL)
7313 continue;
7314
7315 rel_offset = section->sh_offset;
7316 rel_size = section->sh_size;
7317
7318 if (rel_size)
7319 {
7320 Elf_Internal_Shdr * strsec;
7321 int is_rela;
7322 unsigned long num_rela;
7323
7324 printf (_("\nRelocation section "));
7325
7326 if (filedata->string_table == NULL)
7327 printf ("%d", section->sh_name);
7328 else
7329 printf ("'%s'", printable_section_name (filedata, section));
7330
7331 num_rela = rel_size / section->sh_entsize;
7332 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7333 " at offset 0x%lx contains %lu entries:\n",
7334 num_rela),
7335 rel_offset, num_rela);
7336
7337 is_rela = section->sh_type == SHT_RELA;
7338
7339 if (section->sh_link != 0
7340 && section->sh_link < filedata->file_header.e_shnum)
7341 {
7342 Elf_Internal_Shdr * symsec;
7343 Elf_Internal_Sym * symtab;
7344 unsigned long nsyms;
7345 unsigned long strtablen = 0;
7346 char * strtab = NULL;
7347
7348 symsec = filedata->section_headers + section->sh_link;
7349 if (symsec->sh_type != SHT_SYMTAB
7350 && symsec->sh_type != SHT_DYNSYM)
7351 continue;
7352
7353 symtab = GET_ELF_SYMBOLS (filedata, symsec, & nsyms);
7354
7355 if (symtab == NULL)
7356 continue;
7357
7358 if (symsec->sh_link != 0
7359 && symsec->sh_link < filedata->file_header.e_shnum)
7360 {
7361 strsec = filedata->section_headers + symsec->sh_link;
7362
7363 strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7364 1, strsec->sh_size,
7365 _("string table"));
7366 strtablen = strtab == NULL ? 0 : strsec->sh_size;
7367 }
7368
7369 dump_relocations (filedata, rel_offset, rel_size,
7370 symtab, nsyms, strtab, strtablen,
7371 is_rela,
7372 symsec->sh_type == SHT_DYNSYM);
7373 if (strtab)
7374 free (strtab);
7375 free (symtab);
7376 }
7377 else
7378 dump_relocations (filedata, rel_offset, rel_size,
7379 NULL, 0, NULL, 0, is_rela,
7380 FALSE /* is_dynamic */);
7381
7382 found = TRUE;
7383 }
7384 }
7385
7386 if (! found)
7387 {
7388 /* Users sometimes forget the -D option, so try to be helpful. */
7389 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7390 {
7391 if (dynamic_info [dynamic_relocations [i].size])
7392 {
7393 printf (_("\nThere are no static relocations in this file."));
7394 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
7395
7396 break;
7397 }
7398 }
7399 if (i == ARRAY_SIZE (dynamic_relocations))
7400 printf (_("\nThere are no relocations in this file.\n"));
7401 }
7402 }
7403
7404 return TRUE;
7405 }
7406
7407 /* An absolute address consists of a section and an offset. If the
7408 section is NULL, the offset itself is the address, otherwise, the
7409 address equals to LOAD_ADDRESS(section) + offset. */
7410
7411 struct absaddr
7412 {
7413 unsigned short section;
7414 bfd_vma offset;
7415 };
7416
7417 #define ABSADDR(a) \
7418 ((a).section \
7419 ? filedata->section_headers [(a).section].sh_addr + (a).offset \
7420 : (a).offset)
7421
7422 /* Find the nearest symbol at or below ADDR. Returns the symbol
7423 name, if found, and the offset from the symbol to ADDR. */
7424
7425 static void
7426 find_symbol_for_address (Filedata * filedata,
7427 Elf_Internal_Sym * symtab,
7428 unsigned long nsyms,
7429 const char * strtab,
7430 unsigned long strtab_size,
7431 struct absaddr addr,
7432 const char ** symname,
7433 bfd_vma * offset)
7434 {
7435 bfd_vma dist = 0x100000;
7436 Elf_Internal_Sym * sym;
7437 Elf_Internal_Sym * beg;
7438 Elf_Internal_Sym * end;
7439 Elf_Internal_Sym * best = NULL;
7440
7441 REMOVE_ARCH_BITS (addr.offset);
7442 beg = symtab;
7443 end = symtab + nsyms;
7444
7445 while (beg < end)
7446 {
7447 bfd_vma value;
7448
7449 sym = beg + (end - beg) / 2;
7450
7451 value = sym->st_value;
7452 REMOVE_ARCH_BITS (value);
7453
7454 if (sym->st_name != 0
7455 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
7456 && addr.offset >= value
7457 && addr.offset - value < dist)
7458 {
7459 best = sym;
7460 dist = addr.offset - value;
7461 if (!dist)
7462 break;
7463 }
7464
7465 if (addr.offset < value)
7466 end = sym;
7467 else
7468 beg = sym + 1;
7469 }
7470
7471 if (best)
7472 {
7473 *symname = (best->st_name >= strtab_size
7474 ? _("<corrupt>") : strtab + best->st_name);
7475 *offset = dist;
7476 return;
7477 }
7478
7479 *symname = NULL;
7480 *offset = addr.offset;
7481 }
7482
7483 static /* signed */ int
7484 symcmp (const void *p, const void *q)
7485 {
7486 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
7487 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
7488
7489 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
7490 }
7491
7492 /* Process the unwind section. */
7493
7494 #include "unwind-ia64.h"
7495
7496 struct ia64_unw_table_entry
7497 {
7498 struct absaddr start;
7499 struct absaddr end;
7500 struct absaddr info;
7501 };
7502
7503 struct ia64_unw_aux_info
7504 {
7505 struct ia64_unw_table_entry * table; /* Unwind table. */
7506 unsigned long table_len; /* Length of unwind table. */
7507 unsigned char * info; /* Unwind info. */
7508 unsigned long info_size; /* Size of unwind info. */
7509 bfd_vma info_addr; /* Starting address of unwind info. */
7510 bfd_vma seg_base; /* Starting address of segment. */
7511 Elf_Internal_Sym * symtab; /* The symbol table. */
7512 unsigned long nsyms; /* Number of symbols. */
7513 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
7514 unsigned long nfuns; /* Number of entries in funtab. */
7515 char * strtab; /* The string table. */
7516 unsigned long strtab_size; /* Size of string table. */
7517 };
7518
7519 static bfd_boolean
7520 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
7521 {
7522 struct ia64_unw_table_entry * tp;
7523 unsigned long j, nfuns;
7524 int in_body;
7525 bfd_boolean res = TRUE;
7526
7527 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7528 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7529 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7530 aux->funtab[nfuns++] = aux->symtab[j];
7531 aux->nfuns = nfuns;
7532 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7533
7534 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7535 {
7536 bfd_vma stamp;
7537 bfd_vma offset;
7538 const unsigned char * dp;
7539 const unsigned char * head;
7540 const unsigned char * end;
7541 const char * procname;
7542
7543 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
7544 aux->strtab_size, tp->start, &procname, &offset);
7545
7546 fputs ("\n<", stdout);
7547
7548 if (procname)
7549 {
7550 fputs (procname, stdout);
7551
7552 if (offset)
7553 printf ("+%lx", (unsigned long) offset);
7554 }
7555
7556 fputs (">: [", stdout);
7557 print_vma (tp->start.offset, PREFIX_HEX);
7558 fputc ('-', stdout);
7559 print_vma (tp->end.offset, PREFIX_HEX);
7560 printf ("], info at +0x%lx\n",
7561 (unsigned long) (tp->info.offset - aux->seg_base));
7562
7563 /* PR 17531: file: 86232b32. */
7564 if (aux->info == NULL)
7565 continue;
7566
7567 /* PR 17531: file: 0997b4d1. */
7568 if ((ABSADDR (tp->info) - aux->info_addr) >= aux->info_size)
7569 {
7570 warn (_("Invalid offset %lx in table entry %ld\n"),
7571 (long) tp->info.offset, (long) (tp - aux->table));
7572 res = FALSE;
7573 continue;
7574 }
7575
7576 head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
7577 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
7578
7579 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
7580 (unsigned) UNW_VER (stamp),
7581 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
7582 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
7583 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
7584 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
7585
7586 if (UNW_VER (stamp) != 1)
7587 {
7588 printf (_("\tUnknown version.\n"));
7589 continue;
7590 }
7591
7592 in_body = 0;
7593 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
7594 /* PR 17531: file: 16ceda89. */
7595 if (end > aux->info + aux->info_size)
7596 end = aux->info + aux->info_size;
7597 for (dp = head + 8; dp < end;)
7598 dp = unw_decode (dp, in_body, & in_body, end);
7599 }
7600
7601 free (aux->funtab);
7602
7603 return res;
7604 }
7605
7606 static bfd_boolean
7607 slurp_ia64_unwind_table (Filedata * filedata,
7608 struct ia64_unw_aux_info * aux,
7609 Elf_Internal_Shdr * sec)
7610 {
7611 unsigned long size, nrelas, i;
7612 Elf_Internal_Phdr * seg;
7613 struct ia64_unw_table_entry * tep;
7614 Elf_Internal_Shdr * relsec;
7615 Elf_Internal_Rela * rela;
7616 Elf_Internal_Rela * rp;
7617 unsigned char * table;
7618 unsigned char * tp;
7619 Elf_Internal_Sym * sym;
7620 const char * relname;
7621
7622 aux->table_len = 0;
7623
7624 /* First, find the starting address of the segment that includes
7625 this section: */
7626
7627 if (filedata->file_header.e_phnum)
7628 {
7629 if (! get_program_headers (filedata))
7630 return FALSE;
7631
7632 for (seg = filedata->program_headers;
7633 seg < filedata->program_headers + filedata->file_header.e_phnum;
7634 ++seg)
7635 {
7636 if (seg->p_type != PT_LOAD)
7637 continue;
7638
7639 if (sec->sh_addr >= seg->p_vaddr
7640 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
7641 {
7642 aux->seg_base = seg->p_vaddr;
7643 break;
7644 }
7645 }
7646 }
7647
7648 /* Second, build the unwind table from the contents of the unwind section: */
7649 size = sec->sh_size;
7650 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
7651 _("unwind table"));
7652 if (!table)
7653 return FALSE;
7654
7655 aux->table_len = size / (3 * eh_addr_size);
7656 aux->table = (struct ia64_unw_table_entry *)
7657 xcmalloc (aux->table_len, sizeof (aux->table[0]));
7658 tep = aux->table;
7659
7660 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
7661 {
7662 tep->start.section = SHN_UNDEF;
7663 tep->end.section = SHN_UNDEF;
7664 tep->info.section = SHN_UNDEF;
7665 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7666 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7667 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
7668 tep->start.offset += aux->seg_base;
7669 tep->end.offset += aux->seg_base;
7670 tep->info.offset += aux->seg_base;
7671 }
7672 free (table);
7673
7674 /* Third, apply any relocations to the unwind table: */
7675 for (relsec = filedata->section_headers;
7676 relsec < filedata->section_headers + filedata->file_header.e_shnum;
7677 ++relsec)
7678 {
7679 if (relsec->sh_type != SHT_RELA
7680 || relsec->sh_info >= filedata->file_header.e_shnum
7681 || filedata->section_headers + relsec->sh_info != sec)
7682 continue;
7683
7684 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
7685 & rela, & nrelas))
7686 {
7687 free (aux->table);
7688 aux->table = NULL;
7689 aux->table_len = 0;
7690 return FALSE;
7691 }
7692
7693 for (rp = rela; rp < rela + nrelas; ++rp)
7694 {
7695 unsigned int sym_ndx;
7696 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
7697 relname = elf_ia64_reloc_type (r_type);
7698
7699 /* PR 17531: file: 9fa67536. */
7700 if (relname == NULL)
7701 {
7702 warn (_("Skipping unknown relocation type: %u\n"), r_type);
7703 continue;
7704 }
7705
7706 if (! const_strneq (relname, "R_IA64_SEGREL"))
7707 {
7708 warn (_("Skipping unexpected relocation type: %s\n"), relname);
7709 continue;
7710 }
7711
7712 i = rp->r_offset / (3 * eh_addr_size);
7713
7714 /* PR 17531: file: 5bc8d9bf. */
7715 if (i >= aux->table_len)
7716 {
7717 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
7718 continue;
7719 }
7720
7721 sym_ndx = get_reloc_symindex (rp->r_info);
7722 if (sym_ndx >= aux->nsyms)
7723 {
7724 warn (_("Skipping reloc with invalid symbol index: %u\n"),
7725 sym_ndx);
7726 continue;
7727 }
7728 sym = aux->symtab + sym_ndx;
7729
7730 switch (rp->r_offset / eh_addr_size % 3)
7731 {
7732 case 0:
7733 aux->table[i].start.section = sym->st_shndx;
7734 aux->table[i].start.offset = rp->r_addend + sym->st_value;
7735 break;
7736 case 1:
7737 aux->table[i].end.section = sym->st_shndx;
7738 aux->table[i].end.offset = rp->r_addend + sym->st_value;
7739 break;
7740 case 2:
7741 aux->table[i].info.section = sym->st_shndx;
7742 aux->table[i].info.offset = rp->r_addend + sym->st_value;
7743 break;
7744 default:
7745 break;
7746 }
7747 }
7748
7749 free (rela);
7750 }
7751
7752 return TRUE;
7753 }
7754
7755 static bfd_boolean
7756 ia64_process_unwind (Filedata * filedata)
7757 {
7758 Elf_Internal_Shdr * sec;
7759 Elf_Internal_Shdr * unwsec = NULL;
7760 Elf_Internal_Shdr * strsec;
7761 unsigned long i, unwcount = 0, unwstart = 0;
7762 struct ia64_unw_aux_info aux;
7763 bfd_boolean res = TRUE;
7764
7765 memset (& aux, 0, sizeof (aux));
7766
7767 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
7768 {
7769 if (sec->sh_type == SHT_SYMTAB
7770 && sec->sh_link < filedata->file_header.e_shnum)
7771 {
7772 aux.symtab = GET_ELF_SYMBOLS (filedata, sec, & aux.nsyms);
7773
7774 strsec = filedata->section_headers + sec->sh_link;
7775 if (aux.strtab != NULL)
7776 {
7777 error (_("Multiple auxillary string tables encountered\n"));
7778 free (aux.strtab);
7779 res = FALSE;
7780 }
7781 aux.strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7782 1, strsec->sh_size,
7783 _("string table"));
7784 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7785 }
7786 else if (sec->sh_type == SHT_IA_64_UNWIND)
7787 unwcount++;
7788 }
7789
7790 if (!unwcount)
7791 printf (_("\nThere are no unwind sections in this file.\n"));
7792
7793 while (unwcount-- > 0)
7794 {
7795 char * suffix;
7796 size_t len, len2;
7797
7798 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
7799 i < filedata->file_header.e_shnum; ++i, ++sec)
7800 if (sec->sh_type == SHT_IA_64_UNWIND)
7801 {
7802 unwsec = sec;
7803 break;
7804 }
7805 /* We have already counted the number of SHT_IA64_UNWIND
7806 sections so the loop above should never fail. */
7807 assert (unwsec != NULL);
7808
7809 unwstart = i + 1;
7810 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
7811
7812 if ((unwsec->sh_flags & SHF_GROUP) != 0)
7813 {
7814 /* We need to find which section group it is in. */
7815 struct group_list * g;
7816
7817 if (section_headers_groups == NULL
7818 || section_headers_groups [i] == NULL)
7819 i = filedata->file_header.e_shnum;
7820 else
7821 {
7822 g = section_headers_groups [i]->root;
7823
7824 for (; g != NULL; g = g->next)
7825 {
7826 sec = filedata->section_headers + g->section_index;
7827
7828 if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
7829 break;
7830 }
7831
7832 if (g == NULL)
7833 i = filedata->file_header.e_shnum;
7834 }
7835 }
7836 else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
7837 {
7838 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
7839 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
7840 suffix = SECTION_NAME (unwsec) + len;
7841 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum;
7842 ++i, ++sec)
7843 if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
7844 && streq (SECTION_NAME (sec) + len2, suffix))
7845 break;
7846 }
7847 else
7848 {
7849 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
7850 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
7851 len = sizeof (ELF_STRING_ia64_unwind) - 1;
7852 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
7853 suffix = "";
7854 if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
7855 suffix = SECTION_NAME (unwsec) + len;
7856 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum;
7857 ++i, ++sec)
7858 if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
7859 && streq (SECTION_NAME (sec) + len2, suffix))
7860 break;
7861 }
7862
7863 if (i == filedata->file_header.e_shnum)
7864 {
7865 printf (_("\nCould not find unwind info section for "));
7866
7867 if (filedata->string_table == NULL)
7868 printf ("%d", unwsec->sh_name);
7869 else
7870 printf ("'%s'", printable_section_name (filedata, unwsec));
7871 }
7872 else
7873 {
7874 aux.info_addr = sec->sh_addr;
7875 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
7876 sec->sh_size,
7877 _("unwind info"));
7878 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
7879
7880 printf (_("\nUnwind section "));
7881
7882 if (filedata->string_table == NULL)
7883 printf ("%d", unwsec->sh_name);
7884 else
7885 printf ("'%s'", printable_section_name (filedata, unwsec));
7886
7887 printf (_(" at offset 0x%lx contains %lu entries:\n"),
7888 (unsigned long) unwsec->sh_offset,
7889 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
7890
7891 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
7892 && aux.table_len > 0)
7893 dump_ia64_unwind (filedata, & aux);
7894
7895 if (aux.table)
7896 free ((char *) aux.table);
7897 if (aux.info)
7898 free ((char *) aux.info);
7899 aux.table = NULL;
7900 aux.info = NULL;
7901 }
7902 }
7903
7904 if (aux.symtab)
7905 free (aux.symtab);
7906 if (aux.strtab)
7907 free ((char *) aux.strtab);
7908
7909 return res;
7910 }
7911
7912 struct hppa_unw_table_entry
7913 {
7914 struct absaddr start;
7915 struct absaddr end;
7916 unsigned int Cannot_unwind:1; /* 0 */
7917 unsigned int Millicode:1; /* 1 */
7918 unsigned int Millicode_save_sr0:1; /* 2 */
7919 unsigned int Region_description:2; /* 3..4 */
7920 unsigned int reserved1:1; /* 5 */
7921 unsigned int Entry_SR:1; /* 6 */
7922 unsigned int Entry_FR:4; /* Number saved 7..10 */
7923 unsigned int Entry_GR:5; /* Number saved 11..15 */
7924 unsigned int Args_stored:1; /* 16 */
7925 unsigned int Variable_Frame:1; /* 17 */
7926 unsigned int Separate_Package_Body:1; /* 18 */
7927 unsigned int Frame_Extension_Millicode:1; /* 19 */
7928 unsigned int Stack_Overflow_Check:1; /* 20 */
7929 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
7930 unsigned int Ada_Region:1; /* 22 */
7931 unsigned int cxx_info:1; /* 23 */
7932 unsigned int cxx_try_catch:1; /* 24 */
7933 unsigned int sched_entry_seq:1; /* 25 */
7934 unsigned int reserved2:1; /* 26 */
7935 unsigned int Save_SP:1; /* 27 */
7936 unsigned int Save_RP:1; /* 28 */
7937 unsigned int Save_MRP_in_frame:1; /* 29 */
7938 unsigned int extn_ptr_defined:1; /* 30 */
7939 unsigned int Cleanup_defined:1; /* 31 */
7940
7941 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
7942 unsigned int HP_UX_interrupt_marker:1; /* 1 */
7943 unsigned int Large_frame:1; /* 2 */
7944 unsigned int Pseudo_SP_Set:1; /* 3 */
7945 unsigned int reserved4:1; /* 4 */
7946 unsigned int Total_frame_size:27; /* 5..31 */
7947 };
7948
7949 struct hppa_unw_aux_info
7950 {
7951 struct hppa_unw_table_entry * table; /* Unwind table. */
7952 unsigned long table_len; /* Length of unwind table. */
7953 bfd_vma seg_base; /* Starting address of segment. */
7954 Elf_Internal_Sym * symtab; /* The symbol table. */
7955 unsigned long nsyms; /* Number of symbols. */
7956 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
7957 unsigned long nfuns; /* Number of entries in funtab. */
7958 char * strtab; /* The string table. */
7959 unsigned long strtab_size; /* Size of string table. */
7960 };
7961
7962 static bfd_boolean
7963 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
7964 {
7965 struct hppa_unw_table_entry * tp;
7966 unsigned long j, nfuns;
7967 bfd_boolean res = TRUE;
7968
7969 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7970 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7971 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7972 aux->funtab[nfuns++] = aux->symtab[j];
7973 aux->nfuns = nfuns;
7974 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7975
7976 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7977 {
7978 bfd_vma offset;
7979 const char * procname;
7980
7981 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
7982 aux->strtab_size, tp->start, &procname,
7983 &offset);
7984
7985 fputs ("\n<", stdout);
7986
7987 if (procname)
7988 {
7989 fputs (procname, stdout);
7990
7991 if (offset)
7992 printf ("+%lx", (unsigned long) offset);
7993 }
7994
7995 fputs (">: [", stdout);
7996 print_vma (tp->start.offset, PREFIX_HEX);
7997 fputc ('-', stdout);
7998 print_vma (tp->end.offset, PREFIX_HEX);
7999 printf ("]\n\t");
8000
8001 #define PF(_m) if (tp->_m) printf (#_m " ");
8002 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8003 PF(Cannot_unwind);
8004 PF(Millicode);
8005 PF(Millicode_save_sr0);
8006 /* PV(Region_description); */
8007 PF(Entry_SR);
8008 PV(Entry_FR);
8009 PV(Entry_GR);
8010 PF(Args_stored);
8011 PF(Variable_Frame);
8012 PF(Separate_Package_Body);
8013 PF(Frame_Extension_Millicode);
8014 PF(Stack_Overflow_Check);
8015 PF(Two_Instruction_SP_Increment);
8016 PF(Ada_Region);
8017 PF(cxx_info);
8018 PF(cxx_try_catch);
8019 PF(sched_entry_seq);
8020 PF(Save_SP);
8021 PF(Save_RP);
8022 PF(Save_MRP_in_frame);
8023 PF(extn_ptr_defined);
8024 PF(Cleanup_defined);
8025 PF(MPE_XL_interrupt_marker);
8026 PF(HP_UX_interrupt_marker);
8027 PF(Large_frame);
8028 PF(Pseudo_SP_Set);
8029 PV(Total_frame_size);
8030 #undef PF
8031 #undef PV
8032 }
8033
8034 printf ("\n");
8035
8036 free (aux->funtab);
8037
8038 return res;
8039 }
8040
8041 static bfd_boolean
8042 slurp_hppa_unwind_table (Filedata * filedata,
8043 struct hppa_unw_aux_info * aux,
8044 Elf_Internal_Shdr * sec)
8045 {
8046 unsigned long size, unw_ent_size, nentries, nrelas, i;
8047 Elf_Internal_Phdr * seg;
8048 struct hppa_unw_table_entry * tep;
8049 Elf_Internal_Shdr * relsec;
8050 Elf_Internal_Rela * rela;
8051 Elf_Internal_Rela * rp;
8052 unsigned char * table;
8053 unsigned char * tp;
8054 Elf_Internal_Sym * sym;
8055 const char * relname;
8056
8057 /* First, find the starting address of the segment that includes
8058 this section. */
8059 if (filedata->file_header.e_phnum)
8060 {
8061 if (! get_program_headers (filedata))
8062 return FALSE;
8063
8064 for (seg = filedata->program_headers;
8065 seg < filedata->program_headers + filedata->file_header.e_phnum;
8066 ++seg)
8067 {
8068 if (seg->p_type != PT_LOAD)
8069 continue;
8070
8071 if (sec->sh_addr >= seg->p_vaddr
8072 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8073 {
8074 aux->seg_base = seg->p_vaddr;
8075 break;
8076 }
8077 }
8078 }
8079
8080 /* Second, build the unwind table from the contents of the unwind
8081 section. */
8082 size = sec->sh_size;
8083 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8084 _("unwind table"));
8085 if (!table)
8086 return FALSE;
8087
8088 unw_ent_size = 16;
8089 nentries = size / unw_ent_size;
8090 size = unw_ent_size * nentries;
8091
8092 tep = aux->table = (struct hppa_unw_table_entry *)
8093 xcmalloc (nentries, sizeof (aux->table[0]));
8094
8095 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
8096 {
8097 unsigned int tmp1, tmp2;
8098
8099 tep->start.section = SHN_UNDEF;
8100 tep->end.section = SHN_UNDEF;
8101
8102 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8103 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8104 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8105 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8106
8107 tep->start.offset += aux->seg_base;
8108 tep->end.offset += aux->seg_base;
8109
8110 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8111 tep->Millicode = (tmp1 >> 30) & 0x1;
8112 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8113 tep->Region_description = (tmp1 >> 27) & 0x3;
8114 tep->reserved1 = (tmp1 >> 26) & 0x1;
8115 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8116 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8117 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8118 tep->Args_stored = (tmp1 >> 15) & 0x1;
8119 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8120 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8121 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8122 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8123 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8124 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8125 tep->cxx_info = (tmp1 >> 8) & 0x1;
8126 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8127 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8128 tep->reserved2 = (tmp1 >> 5) & 0x1;
8129 tep->Save_SP = (tmp1 >> 4) & 0x1;
8130 tep->Save_RP = (tmp1 >> 3) & 0x1;
8131 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8132 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8133 tep->Cleanup_defined = tmp1 & 0x1;
8134
8135 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8136 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8137 tep->Large_frame = (tmp2 >> 29) & 0x1;
8138 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8139 tep->reserved4 = (tmp2 >> 27) & 0x1;
8140 tep->Total_frame_size = tmp2 & 0x7ffffff;
8141 }
8142 free (table);
8143
8144 /* Third, apply any relocations to the unwind table. */
8145 for (relsec = filedata->section_headers;
8146 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8147 ++relsec)
8148 {
8149 if (relsec->sh_type != SHT_RELA
8150 || relsec->sh_info >= filedata->file_header.e_shnum
8151 || filedata->section_headers + relsec->sh_info != sec)
8152 continue;
8153
8154 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8155 & rela, & nrelas))
8156 return FALSE;
8157
8158 for (rp = rela; rp < rela + nrelas; ++rp)
8159 {
8160 unsigned int sym_ndx;
8161 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8162 relname = elf_hppa_reloc_type (r_type);
8163
8164 if (relname == NULL)
8165 {
8166 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8167 continue;
8168 }
8169
8170 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
8171 if (! const_strneq (relname, "R_PARISC_SEGREL"))
8172 {
8173 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8174 continue;
8175 }
8176
8177 i = rp->r_offset / unw_ent_size;
8178 if (i >= aux->table_len)
8179 {
8180 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8181 continue;
8182 }
8183
8184 sym_ndx = get_reloc_symindex (rp->r_info);
8185 if (sym_ndx >= aux->nsyms)
8186 {
8187 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8188 sym_ndx);
8189 continue;
8190 }
8191 sym = aux->symtab + sym_ndx;
8192
8193 switch ((rp->r_offset % unw_ent_size) / 4)
8194 {
8195 case 0:
8196 aux->table[i].start.section = sym->st_shndx;
8197 aux->table[i].start.offset = sym->st_value + rp->r_addend;
8198 break;
8199 case 1:
8200 aux->table[i].end.section = sym->st_shndx;
8201 aux->table[i].end.offset = sym->st_value + rp->r_addend;
8202 break;
8203 default:
8204 break;
8205 }
8206 }
8207
8208 free (rela);
8209 }
8210
8211 aux->table_len = nentries;
8212
8213 return TRUE;
8214 }
8215
8216 static bfd_boolean
8217 hppa_process_unwind (Filedata * filedata)
8218 {
8219 struct hppa_unw_aux_info aux;
8220 Elf_Internal_Shdr * unwsec = NULL;
8221 Elf_Internal_Shdr * strsec;
8222 Elf_Internal_Shdr * sec;
8223 unsigned long i;
8224 bfd_boolean res = TRUE;
8225
8226 if (filedata->string_table == NULL)
8227 return FALSE;
8228
8229 memset (& aux, 0, sizeof (aux));
8230
8231 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8232 {
8233 if (sec->sh_type == SHT_SYMTAB
8234 && sec->sh_link < filedata->file_header.e_shnum)
8235 {
8236 aux.symtab = GET_ELF_SYMBOLS (filedata, sec, & aux.nsyms);
8237
8238 strsec = filedata->section_headers + sec->sh_link;
8239 if (aux.strtab != NULL)
8240 {
8241 error (_("Multiple auxillary string tables encountered\n"));
8242 free (aux.strtab);
8243 res = FALSE;
8244 }
8245 aux.strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
8246 1, strsec->sh_size,
8247 _("string table"));
8248 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
8249 }
8250 else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
8251 unwsec = sec;
8252 }
8253
8254 if (!unwsec)
8255 printf (_("\nThere are no unwind sections in this file.\n"));
8256
8257 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8258 {
8259 if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
8260 {
8261 unsigned long num_unwind = sec->sh_size / 16;
8262
8263 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8264 "contains %lu entry:\n",
8265 "\nUnwind section '%s' at offset 0x%lx "
8266 "contains %lu entries:\n",
8267 num_unwind),
8268 printable_section_name (filedata, sec),
8269 (unsigned long) sec->sh_offset,
8270 num_unwind);
8271
8272 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8273 res = FALSE;
8274
8275 if (res && aux.table_len > 0)
8276 {
8277 if (! dump_hppa_unwind (filedata, &aux))
8278 res = FALSE;
8279 }
8280
8281 if (aux.table)
8282 free ((char *) aux.table);
8283 aux.table = NULL;
8284 }
8285 }
8286
8287 if (aux.symtab)
8288 free (aux.symtab);
8289 if (aux.strtab)
8290 free ((char *) aux.strtab);
8291
8292 return res;
8293 }
8294
8295 struct arm_section
8296 {
8297 unsigned char * data; /* The unwind data. */
8298 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
8299 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
8300 unsigned long nrelas; /* The number of relocations. */
8301 unsigned int rel_type; /* REL or RELA ? */
8302 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
8303 };
8304
8305 struct arm_unw_aux_info
8306 {
8307 Filedata * filedata; /* The file containing the unwind sections. */
8308 Elf_Internal_Sym * symtab; /* The file's symbol table. */
8309 unsigned long nsyms; /* Number of symbols. */
8310 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8311 unsigned long nfuns; /* Number of these symbols. */
8312 char * strtab; /* The file's string table. */
8313 unsigned long strtab_size; /* Size of string table. */
8314 };
8315
8316 static const char *
8317 arm_print_vma_and_name (Filedata * filedata,
8318 struct arm_unw_aux_info * aux,
8319 bfd_vma fn,
8320 struct absaddr addr)
8321 {
8322 const char *procname;
8323 bfd_vma sym_offset;
8324
8325 if (addr.section == SHN_UNDEF)
8326 addr.offset = fn;
8327
8328 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8329 aux->strtab_size, addr, &procname,
8330 &sym_offset);
8331
8332 print_vma (fn, PREFIX_HEX);
8333
8334 if (procname)
8335 {
8336 fputs (" <", stdout);
8337 fputs (procname, stdout);
8338
8339 if (sym_offset)
8340 printf ("+0x%lx", (unsigned long) sym_offset);
8341 fputc ('>', stdout);
8342 }
8343
8344 return procname;
8345 }
8346
8347 static void
8348 arm_free_section (struct arm_section *arm_sec)
8349 {
8350 if (arm_sec->data != NULL)
8351 free (arm_sec->data);
8352
8353 if (arm_sec->rela != NULL)
8354 free (arm_sec->rela);
8355 }
8356
8357 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8358 cached section and install SEC instead.
8359 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8360 and return its valued in * WORDP, relocating if necessary.
8361 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
8362 relocation's offset in ADDR.
8363 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
8364 into the string table of the symbol associated with the reloc. If no
8365 reloc was applied store -1 there.
8366 5) Return TRUE upon success, FALSE otherwise. */
8367
8368 static bfd_boolean
8369 get_unwind_section_word (Filedata * filedata,
8370 struct arm_unw_aux_info * aux,
8371 struct arm_section * arm_sec,
8372 Elf_Internal_Shdr * sec,
8373 bfd_vma word_offset,
8374 unsigned int * wordp,
8375 struct absaddr * addr,
8376 bfd_vma * sym_name)
8377 {
8378 Elf_Internal_Rela *rp;
8379 Elf_Internal_Sym *sym;
8380 const char * relname;
8381 unsigned int word;
8382 bfd_boolean wrapped;
8383
8384 if (sec == NULL || arm_sec == NULL)
8385 return FALSE;
8386
8387 addr->section = SHN_UNDEF;
8388 addr->offset = 0;
8389
8390 if (sym_name != NULL)
8391 *sym_name = (bfd_vma) -1;
8392
8393 /* If necessary, update the section cache. */
8394 if (sec != arm_sec->sec)
8395 {
8396 Elf_Internal_Shdr *relsec;
8397
8398 arm_free_section (arm_sec);
8399
8400 arm_sec->sec = sec;
8401 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
8402 sec->sh_size, _("unwind data"));
8403 arm_sec->rela = NULL;
8404 arm_sec->nrelas = 0;
8405
8406 for (relsec = filedata->section_headers;
8407 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8408 ++relsec)
8409 {
8410 if (relsec->sh_info >= filedata->file_header.e_shnum
8411 || filedata->section_headers + relsec->sh_info != sec
8412 /* PR 15745: Check the section type as well. */
8413 || (relsec->sh_type != SHT_REL
8414 && relsec->sh_type != SHT_RELA))
8415 continue;
8416
8417 arm_sec->rel_type = relsec->sh_type;
8418 if (relsec->sh_type == SHT_REL)
8419 {
8420 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
8421 relsec->sh_size,
8422 & arm_sec->rela, & arm_sec->nrelas))
8423 return FALSE;
8424 }
8425 else /* relsec->sh_type == SHT_RELA */
8426 {
8427 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
8428 relsec->sh_size,
8429 & arm_sec->rela, & arm_sec->nrelas))
8430 return FALSE;
8431 }
8432 break;
8433 }
8434
8435 arm_sec->next_rela = arm_sec->rela;
8436 }
8437
8438 /* If there is no unwind data we can do nothing. */
8439 if (arm_sec->data == NULL)
8440 return FALSE;
8441
8442 /* If the offset is invalid then fail. */
8443 if (/* PR 21343 *//* PR 18879 */
8444 sec->sh_size < 4
8445 || word_offset > (sec->sh_size - 4)
8446 || ((bfd_signed_vma) word_offset) < 0)
8447 return FALSE;
8448
8449 /* Get the word at the required offset. */
8450 word = byte_get (arm_sec->data + word_offset, 4);
8451
8452 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
8453 if (arm_sec->rela == NULL)
8454 {
8455 * wordp = word;
8456 return TRUE;
8457 }
8458
8459 /* Look through the relocs to find the one that applies to the provided offset. */
8460 wrapped = FALSE;
8461 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
8462 {
8463 bfd_vma prelval, offset;
8464
8465 if (rp->r_offset > word_offset && !wrapped)
8466 {
8467 rp = arm_sec->rela;
8468 wrapped = TRUE;
8469 }
8470 if (rp->r_offset > word_offset)
8471 break;
8472
8473 if (rp->r_offset & 3)
8474 {
8475 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
8476 (unsigned long) rp->r_offset);
8477 continue;
8478 }
8479
8480 if (rp->r_offset < word_offset)
8481 continue;
8482
8483 /* PR 17531: file: 027-161405-0.004 */
8484 if (aux->symtab == NULL)
8485 continue;
8486
8487 if (arm_sec->rel_type == SHT_REL)
8488 {
8489 offset = word & 0x7fffffff;
8490 if (offset & 0x40000000)
8491 offset |= ~ (bfd_vma) 0x7fffffff;
8492 }
8493 else if (arm_sec->rel_type == SHT_RELA)
8494 offset = rp->r_addend;
8495 else
8496 {
8497 error (_("Unknown section relocation type %d encountered\n"),
8498 arm_sec->rel_type);
8499 break;
8500 }
8501
8502 /* PR 17531 file: 027-1241568-0.004. */
8503 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
8504 {
8505 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
8506 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
8507 break;
8508 }
8509
8510 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
8511 offset += sym->st_value;
8512 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
8513
8514 /* Check that we are processing the expected reloc type. */
8515 if (filedata->file_header.e_machine == EM_ARM)
8516 {
8517 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
8518 if (relname == NULL)
8519 {
8520 warn (_("Skipping unknown ARM relocation type: %d\n"),
8521 (int) ELF32_R_TYPE (rp->r_info));
8522 continue;
8523 }
8524
8525 if (streq (relname, "R_ARM_NONE"))
8526 continue;
8527
8528 if (! streq (relname, "R_ARM_PREL31"))
8529 {
8530 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
8531 continue;
8532 }
8533 }
8534 else if (filedata->file_header.e_machine == EM_TI_C6000)
8535 {
8536 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
8537 if (relname == NULL)
8538 {
8539 warn (_("Skipping unknown C6000 relocation type: %d\n"),
8540 (int) ELF32_R_TYPE (rp->r_info));
8541 continue;
8542 }
8543
8544 if (streq (relname, "R_C6000_NONE"))
8545 continue;
8546
8547 if (! streq (relname, "R_C6000_PREL31"))
8548 {
8549 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
8550 continue;
8551 }
8552
8553 prelval >>= 1;
8554 }
8555 else
8556 {
8557 /* This function currently only supports ARM and TI unwinders. */
8558 warn (_("Only TI and ARM unwinders are currently supported\n"));
8559 break;
8560 }
8561
8562 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
8563 addr->section = sym->st_shndx;
8564 addr->offset = offset;
8565
8566 if (sym_name)
8567 * sym_name = sym->st_name;
8568 break;
8569 }
8570
8571 *wordp = word;
8572 arm_sec->next_rela = rp;
8573
8574 return TRUE;
8575 }
8576
8577 static const char *tic6x_unwind_regnames[16] =
8578 {
8579 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
8580 "A14", "A13", "A12", "A11", "A10",
8581 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
8582 };
8583
8584 static void
8585 decode_tic6x_unwind_regmask (unsigned int mask)
8586 {
8587 int i;
8588
8589 for (i = 12; mask; mask >>= 1, i--)
8590 {
8591 if (mask & 1)
8592 {
8593 fputs (tic6x_unwind_regnames[i], stdout);
8594 if (mask > 1)
8595 fputs (", ", stdout);
8596 }
8597 }
8598 }
8599
8600 #define ADVANCE \
8601 if (remaining == 0 && more_words) \
8602 { \
8603 data_offset += 4; \
8604 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
8605 data_offset, & word, & addr, NULL)) \
8606 return FALSE; \
8607 remaining = 4; \
8608 more_words--; \
8609 } \
8610
8611 #define GET_OP(OP) \
8612 ADVANCE; \
8613 if (remaining) \
8614 { \
8615 remaining--; \
8616 (OP) = word >> 24; \
8617 word <<= 8; \
8618 } \
8619 else \
8620 { \
8621 printf (_("[Truncated opcode]\n")); \
8622 return FALSE; \
8623 } \
8624 printf ("0x%02x ", OP)
8625
8626 static bfd_boolean
8627 decode_arm_unwind_bytecode (Filedata * filedata,
8628 struct arm_unw_aux_info * aux,
8629 unsigned int word,
8630 unsigned int remaining,
8631 unsigned int more_words,
8632 bfd_vma data_offset,
8633 Elf_Internal_Shdr * data_sec,
8634 struct arm_section * data_arm_sec)
8635 {
8636 struct absaddr addr;
8637 bfd_boolean res = TRUE;
8638
8639 /* Decode the unwinding instructions. */
8640 while (1)
8641 {
8642 unsigned int op, op2;
8643
8644 ADVANCE;
8645 if (remaining == 0)
8646 break;
8647 remaining--;
8648 op = word >> 24;
8649 word <<= 8;
8650
8651 printf (" 0x%02x ", op);
8652
8653 if ((op & 0xc0) == 0x00)
8654 {
8655 int offset = ((op & 0x3f) << 2) + 4;
8656
8657 printf (" vsp = vsp + %d", offset);
8658 }
8659 else if ((op & 0xc0) == 0x40)
8660 {
8661 int offset = ((op & 0x3f) << 2) + 4;
8662
8663 printf (" vsp = vsp - %d", offset);
8664 }
8665 else if ((op & 0xf0) == 0x80)
8666 {
8667 GET_OP (op2);
8668 if (op == 0x80 && op2 == 0)
8669 printf (_("Refuse to unwind"));
8670 else
8671 {
8672 unsigned int mask = ((op & 0x0f) << 8) | op2;
8673 bfd_boolean first = TRUE;
8674 int i;
8675
8676 printf ("pop {");
8677 for (i = 0; i < 12; i++)
8678 if (mask & (1 << i))
8679 {
8680 if (first)
8681 first = FALSE;
8682 else
8683 printf (", ");
8684 printf ("r%d", 4 + i);
8685 }
8686 printf ("}");
8687 }
8688 }
8689 else if ((op & 0xf0) == 0x90)
8690 {
8691 if (op == 0x9d || op == 0x9f)
8692 printf (_(" [Reserved]"));
8693 else
8694 printf (" vsp = r%d", op & 0x0f);
8695 }
8696 else if ((op & 0xf0) == 0xa0)
8697 {
8698 int end = 4 + (op & 0x07);
8699 bfd_boolean first = TRUE;
8700 int i;
8701
8702 printf (" pop {");
8703 for (i = 4; i <= end; i++)
8704 {
8705 if (first)
8706 first = FALSE;
8707 else
8708 printf (", ");
8709 printf ("r%d", i);
8710 }
8711 if (op & 0x08)
8712 {
8713 if (!first)
8714 printf (", ");
8715 printf ("r14");
8716 }
8717 printf ("}");
8718 }
8719 else if (op == 0xb0)
8720 printf (_(" finish"));
8721 else if (op == 0xb1)
8722 {
8723 GET_OP (op2);
8724 if (op2 == 0 || (op2 & 0xf0) != 0)
8725 printf (_("[Spare]"));
8726 else
8727 {
8728 unsigned int mask = op2 & 0x0f;
8729 bfd_boolean first = TRUE;
8730 int i;
8731
8732 printf ("pop {");
8733 for (i = 0; i < 12; i++)
8734 if (mask & (1 << i))
8735 {
8736 if (first)
8737 first = FALSE;
8738 else
8739 printf (", ");
8740 printf ("r%d", i);
8741 }
8742 printf ("}");
8743 }
8744 }
8745 else if (op == 0xb2)
8746 {
8747 unsigned char buf[9];
8748 unsigned int i, len;
8749 unsigned long offset;
8750
8751 for (i = 0; i < sizeof (buf); i++)
8752 {
8753 GET_OP (buf[i]);
8754 if ((buf[i] & 0x80) == 0)
8755 break;
8756 }
8757 if (i == sizeof (buf))
8758 {
8759 error (_("corrupt change to vsp"));
8760 res = FALSE;
8761 }
8762 else
8763 {
8764 offset = read_uleb128 (buf, &len, buf + i + 1);
8765 assert (len == i + 1);
8766 offset = offset * 4 + 0x204;
8767 printf ("vsp = vsp + %ld", offset);
8768 }
8769 }
8770 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
8771 {
8772 unsigned int first, last;
8773
8774 GET_OP (op2);
8775 first = op2 >> 4;
8776 last = op2 & 0x0f;
8777 if (op == 0xc8)
8778 first = first + 16;
8779 printf ("pop {D%d", first);
8780 if (last)
8781 printf ("-D%d", first + last);
8782 printf ("}");
8783 }
8784 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
8785 {
8786 unsigned int count = op & 0x07;
8787
8788 printf ("pop {D8");
8789 if (count)
8790 printf ("-D%d", 8 + count);
8791 printf ("}");
8792 }
8793 else if (op >= 0xc0 && op <= 0xc5)
8794 {
8795 unsigned int count = op & 0x07;
8796
8797 printf (" pop {wR10");
8798 if (count)
8799 printf ("-wR%d", 10 + count);
8800 printf ("}");
8801 }
8802 else if (op == 0xc6)
8803 {
8804 unsigned int first, last;
8805
8806 GET_OP (op2);
8807 first = op2 >> 4;
8808 last = op2 & 0x0f;
8809 printf ("pop {wR%d", first);
8810 if (last)
8811 printf ("-wR%d", first + last);
8812 printf ("}");
8813 }
8814 else if (op == 0xc7)
8815 {
8816 GET_OP (op2);
8817 if (op2 == 0 || (op2 & 0xf0) != 0)
8818 printf (_("[Spare]"));
8819 else
8820 {
8821 unsigned int mask = op2 & 0x0f;
8822 bfd_boolean first = TRUE;
8823 int i;
8824
8825 printf ("pop {");
8826 for (i = 0; i < 4; i++)
8827 if (mask & (1 << i))
8828 {
8829 if (first)
8830 first = FALSE;
8831 else
8832 printf (", ");
8833 printf ("wCGR%d", i);
8834 }
8835 printf ("}");
8836 }
8837 }
8838 else
8839 {
8840 printf (_(" [unsupported opcode]"));
8841 res = FALSE;
8842 }
8843
8844 printf ("\n");
8845 }
8846
8847 return res;
8848 }
8849
8850 static bfd_boolean
8851 decode_tic6x_unwind_bytecode (Filedata * filedata,
8852 struct arm_unw_aux_info * aux,
8853 unsigned int word,
8854 unsigned int remaining,
8855 unsigned int more_words,
8856 bfd_vma data_offset,
8857 Elf_Internal_Shdr * data_sec,
8858 struct arm_section * data_arm_sec)
8859 {
8860 struct absaddr addr;
8861
8862 /* Decode the unwinding instructions. */
8863 while (1)
8864 {
8865 unsigned int op, op2;
8866
8867 ADVANCE;
8868 if (remaining == 0)
8869 break;
8870 remaining--;
8871 op = word >> 24;
8872 word <<= 8;
8873
8874 printf (" 0x%02x ", op);
8875
8876 if ((op & 0xc0) == 0x00)
8877 {
8878 int offset = ((op & 0x3f) << 3) + 8;
8879 printf (" sp = sp + %d", offset);
8880 }
8881 else if ((op & 0xc0) == 0x80)
8882 {
8883 GET_OP (op2);
8884 if (op == 0x80 && op2 == 0)
8885 printf (_("Refuse to unwind"));
8886 else
8887 {
8888 unsigned int mask = ((op & 0x1f) << 8) | op2;
8889 if (op & 0x20)
8890 printf ("pop compact {");
8891 else
8892 printf ("pop {");
8893
8894 decode_tic6x_unwind_regmask (mask);
8895 printf("}");
8896 }
8897 }
8898 else if ((op & 0xf0) == 0xc0)
8899 {
8900 unsigned int reg;
8901 unsigned int nregs;
8902 unsigned int i;
8903 const char *name;
8904 struct
8905 {
8906 unsigned int offset;
8907 unsigned int reg;
8908 } regpos[16];
8909
8910 /* Scan entire instruction first so that GET_OP output is not
8911 interleaved with disassembly. */
8912 nregs = 0;
8913 for (i = 0; nregs < (op & 0xf); i++)
8914 {
8915 GET_OP (op2);
8916 reg = op2 >> 4;
8917 if (reg != 0xf)
8918 {
8919 regpos[nregs].offset = i * 2;
8920 regpos[nregs].reg = reg;
8921 nregs++;
8922 }
8923
8924 reg = op2 & 0xf;
8925 if (reg != 0xf)
8926 {
8927 regpos[nregs].offset = i * 2 + 1;
8928 regpos[nregs].reg = reg;
8929 nregs++;
8930 }
8931 }
8932
8933 printf (_("pop frame {"));
8934 if (nregs == 0)
8935 {
8936 printf (_("*corrupt* - no registers specified"));
8937 }
8938 else
8939 {
8940 reg = nregs - 1;
8941 for (i = i * 2; i > 0; i--)
8942 {
8943 if (regpos[reg].offset == i - 1)
8944 {
8945 name = tic6x_unwind_regnames[regpos[reg].reg];
8946 if (reg > 0)
8947 reg--;
8948 }
8949 else
8950 name = _("[pad]");
8951
8952 fputs (name, stdout);
8953 if (i > 1)
8954 printf (", ");
8955 }
8956 }
8957
8958 printf ("}");
8959 }
8960 else if (op == 0xd0)
8961 printf (" MOV FP, SP");
8962 else if (op == 0xd1)
8963 printf (" __c6xabi_pop_rts");
8964 else if (op == 0xd2)
8965 {
8966 unsigned char buf[9];
8967 unsigned int i, len;
8968 unsigned long offset;
8969
8970 for (i = 0; i < sizeof (buf); i++)
8971 {
8972 GET_OP (buf[i]);
8973 if ((buf[i] & 0x80) == 0)
8974 break;
8975 }
8976 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
8977 if (i == sizeof (buf))
8978 {
8979 warn (_("Corrupt stack pointer adjustment detected\n"));
8980 return FALSE;
8981 }
8982
8983 offset = read_uleb128 (buf, &len, buf + i + 1);
8984 assert (len == i + 1);
8985 offset = offset * 8 + 0x408;
8986 printf (_("sp = sp + %ld"), offset);
8987 }
8988 else if ((op & 0xf0) == 0xe0)
8989 {
8990 if ((op & 0x0f) == 7)
8991 printf (" RETURN");
8992 else
8993 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
8994 }
8995 else
8996 {
8997 printf (_(" [unsupported opcode]"));
8998 }
8999 putchar ('\n');
9000 }
9001
9002 return TRUE;
9003 }
9004
9005 static bfd_vma
9006 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9007 {
9008 bfd_vma offset;
9009
9010 offset = word & 0x7fffffff;
9011 if (offset & 0x40000000)
9012 offset |= ~ (bfd_vma) 0x7fffffff;
9013
9014 if (filedata->file_header.e_machine == EM_TI_C6000)
9015 offset <<= 1;
9016
9017 return offset + where;
9018 }
9019
9020 static bfd_boolean
9021 decode_arm_unwind (Filedata * filedata,
9022 struct arm_unw_aux_info * aux,
9023 unsigned int word,
9024 unsigned int remaining,
9025 bfd_vma data_offset,
9026 Elf_Internal_Shdr * data_sec,
9027 struct arm_section * data_arm_sec)
9028 {
9029 int per_index;
9030 unsigned int more_words = 0;
9031 struct absaddr addr;
9032 bfd_vma sym_name = (bfd_vma) -1;
9033 bfd_boolean res = TRUE;
9034
9035 if (remaining == 0)
9036 {
9037 /* Fetch the first word.
9038 Note - when decoding an object file the address extracted
9039 here will always be 0. So we also pass in the sym_name
9040 parameter so that we can find the symbol associated with
9041 the personality routine. */
9042 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9043 & word, & addr, & sym_name))
9044 return FALSE;
9045
9046 remaining = 4;
9047 }
9048
9049 if ((word & 0x80000000) == 0)
9050 {
9051 /* Expand prel31 for personality routine. */
9052 bfd_vma fn;
9053 const char *procname;
9054
9055 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9056 printf (_(" Personality routine: "));
9057 if (fn == 0
9058 && addr.section == SHN_UNDEF && addr.offset == 0
9059 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9060 {
9061 procname = aux->strtab + sym_name;
9062 print_vma (fn, PREFIX_HEX);
9063 if (procname)
9064 {
9065 fputs (" <", stdout);
9066 fputs (procname, stdout);
9067 fputc ('>', stdout);
9068 }
9069 }
9070 else
9071 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
9072 fputc ('\n', stdout);
9073
9074 /* The GCC personality routines use the standard compact
9075 encoding, starting with one byte giving the number of
9076 words. */
9077 if (procname != NULL
9078 && (const_strneq (procname, "__gcc_personality_v0")
9079 || const_strneq (procname, "__gxx_personality_v0")
9080 || const_strneq (procname, "__gcj_personality_v0")
9081 || const_strneq (procname, "__gnu_objc_personality_v0")))
9082 {
9083 remaining = 0;
9084 more_words = 1;
9085 ADVANCE;
9086 if (!remaining)
9087 {
9088 printf (_(" [Truncated data]\n"));
9089 return FALSE;
9090 }
9091 more_words = word >> 24;
9092 word <<= 8;
9093 remaining--;
9094 per_index = -1;
9095 }
9096 else
9097 return TRUE;
9098 }
9099 else
9100 {
9101 /* ARM EHABI Section 6.3:
9102
9103 An exception-handling table entry for the compact model looks like:
9104
9105 31 30-28 27-24 23-0
9106 -- ----- ----- ----
9107 1 0 index Data for personalityRoutine[index] */
9108
9109 if (filedata->file_header.e_machine == EM_ARM
9110 && (word & 0x70000000))
9111 {
9112 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
9113 res = FALSE;
9114 }
9115
9116 per_index = (word >> 24) & 0x7f;
9117 printf (_(" Compact model index: %d\n"), per_index);
9118 if (per_index == 0)
9119 {
9120 more_words = 0;
9121 word <<= 8;
9122 remaining--;
9123 }
9124 else if (per_index < 3)
9125 {
9126 more_words = (word >> 16) & 0xff;
9127 word <<= 16;
9128 remaining -= 2;
9129 }
9130 }
9131
9132 switch (filedata->file_header.e_machine)
9133 {
9134 case EM_ARM:
9135 if (per_index < 3)
9136 {
9137 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
9138 data_offset, data_sec, data_arm_sec))
9139 res = FALSE;
9140 }
9141 else
9142 {
9143 warn (_("Unknown ARM compact model index encountered\n"));
9144 printf (_(" [reserved]\n"));
9145 res = FALSE;
9146 }
9147 break;
9148
9149 case EM_TI_C6000:
9150 if (per_index < 3)
9151 {
9152 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9153 data_offset, data_sec, data_arm_sec))
9154 res = FALSE;
9155 }
9156 else if (per_index < 5)
9157 {
9158 if (((word >> 17) & 0x7f) == 0x7f)
9159 printf (_(" Restore stack from frame pointer\n"));
9160 else
9161 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9162 printf (_(" Registers restored: "));
9163 if (per_index == 4)
9164 printf (" (compact) ");
9165 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9166 putchar ('\n');
9167 printf (_(" Return register: %s\n"),
9168 tic6x_unwind_regnames[word & 0xf]);
9169 }
9170 else
9171 printf (_(" [reserved (%d)]\n"), per_index);
9172 break;
9173
9174 default:
9175 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9176 filedata->file_header.e_machine);
9177 res = FALSE;
9178 }
9179
9180 /* Decode the descriptors. Not implemented. */
9181
9182 return res;
9183 }
9184
9185 static bfd_boolean
9186 dump_arm_unwind (Filedata * filedata,
9187 struct arm_unw_aux_info * aux,
9188 Elf_Internal_Shdr * exidx_sec)
9189 {
9190 struct arm_section exidx_arm_sec, extab_arm_sec;
9191 unsigned int i, exidx_len;
9192 unsigned long j, nfuns;
9193 bfd_boolean res = TRUE;
9194
9195 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9196 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9197 exidx_len = exidx_sec->sh_size / 8;
9198
9199 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9200 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9201 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9202 aux->funtab[nfuns++] = aux->symtab[j];
9203 aux->nfuns = nfuns;
9204 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9205
9206 for (i = 0; i < exidx_len; i++)
9207 {
9208 unsigned int exidx_fn, exidx_entry;
9209 struct absaddr fn_addr, entry_addr;
9210 bfd_vma fn;
9211
9212 fputc ('\n', stdout);
9213
9214 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9215 8 * i, & exidx_fn, & fn_addr, NULL)
9216 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9217 8 * i + 4, & exidx_entry, & entry_addr, NULL))
9218 {
9219 free (aux->funtab);
9220 arm_free_section (& exidx_arm_sec);
9221 arm_free_section (& extab_arm_sec);
9222 return FALSE;
9223 }
9224
9225 /* ARM EHABI, Section 5:
9226 An index table entry consists of 2 words.
9227 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
9228 if (exidx_fn & 0x80000000)
9229 {
9230 warn (_("corrupt index table entry: %x\n"), exidx_fn);
9231 res = FALSE;
9232 }
9233
9234 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9235
9236 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9237 fputs (": ", stdout);
9238
9239 if (exidx_entry == 1)
9240 {
9241 print_vma (exidx_entry, PREFIX_HEX);
9242 fputs (" [cantunwind]\n", stdout);
9243 }
9244 else if (exidx_entry & 0x80000000)
9245 {
9246 print_vma (exidx_entry, PREFIX_HEX);
9247 fputc ('\n', stdout);
9248 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9249 }
9250 else
9251 {
9252 bfd_vma table, table_offset = 0;
9253 Elf_Internal_Shdr *table_sec;
9254
9255 fputs ("@", stdout);
9256 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9257 print_vma (table, PREFIX_HEX);
9258 printf ("\n");
9259
9260 /* Locate the matching .ARM.extab. */
9261 if (entry_addr.section != SHN_UNDEF
9262 && entry_addr.section < filedata->file_header.e_shnum)
9263 {
9264 table_sec = filedata->section_headers + entry_addr.section;
9265 table_offset = entry_addr.offset;
9266 /* PR 18879 */
9267 if (table_offset > table_sec->sh_size
9268 || ((bfd_signed_vma) table_offset) < 0)
9269 {
9270 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9271 (unsigned long) table_offset,
9272 printable_section_name (filedata, table_sec));
9273 res = FALSE;
9274 continue;
9275 }
9276 }
9277 else
9278 {
9279 table_sec = find_section_by_address (filedata, table);
9280 if (table_sec != NULL)
9281 table_offset = table - table_sec->sh_addr;
9282 }
9283
9284 if (table_sec == NULL)
9285 {
9286 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9287 (unsigned long) table);
9288 res = FALSE;
9289 continue;
9290 }
9291
9292 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9293 &extab_arm_sec))
9294 res = FALSE;
9295 }
9296 }
9297
9298 printf ("\n");
9299
9300 free (aux->funtab);
9301 arm_free_section (&exidx_arm_sec);
9302 arm_free_section (&extab_arm_sec);
9303
9304 return res;
9305 }
9306
9307 /* Used for both ARM and C6X unwinding tables. */
9308
9309 static bfd_boolean
9310 arm_process_unwind (Filedata * filedata)
9311 {
9312 struct arm_unw_aux_info aux;
9313 Elf_Internal_Shdr *unwsec = NULL;
9314 Elf_Internal_Shdr *strsec;
9315 Elf_Internal_Shdr *sec;
9316 unsigned long i;
9317 unsigned int sec_type;
9318 bfd_boolean res = TRUE;
9319
9320 switch (filedata->file_header.e_machine)
9321 {
9322 case EM_ARM:
9323 sec_type = SHT_ARM_EXIDX;
9324 break;
9325
9326 case EM_TI_C6000:
9327 sec_type = SHT_C6000_UNWIND;
9328 break;
9329
9330 default:
9331 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9332 filedata->file_header.e_machine);
9333 return FALSE;
9334 }
9335
9336 if (filedata->string_table == NULL)
9337 return FALSE;
9338
9339 memset (& aux, 0, sizeof (aux));
9340 aux.filedata = filedata;
9341
9342 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9343 {
9344 if (sec->sh_type == SHT_SYMTAB && sec->sh_link < filedata->file_header.e_shnum)
9345 {
9346 aux.symtab = GET_ELF_SYMBOLS (filedata, sec, & aux.nsyms);
9347
9348 strsec = filedata->section_headers + sec->sh_link;
9349
9350 /* PR binutils/17531 file: 011-12666-0.004. */
9351 if (aux.strtab != NULL)
9352 {
9353 error (_("Multiple string tables found in file.\n"));
9354 free (aux.strtab);
9355 res = FALSE;
9356 }
9357 aux.strtab = get_data (NULL, filedata, strsec->sh_offset,
9358 1, strsec->sh_size, _("string table"));
9359 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
9360 }
9361 else if (sec->sh_type == sec_type)
9362 unwsec = sec;
9363 }
9364
9365 if (unwsec == NULL)
9366 printf (_("\nThere are no unwind sections in this file.\n"));
9367 else
9368 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9369 {
9370 if (sec->sh_type == sec_type)
9371 {
9372 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
9373 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9374 "contains %lu entry:\n",
9375 "\nUnwind section '%s' at offset 0x%lx "
9376 "contains %lu entries:\n",
9377 num_unwind),
9378 printable_section_name (filedata, sec),
9379 (unsigned long) sec->sh_offset,
9380 num_unwind);
9381
9382 if (! dump_arm_unwind (filedata, &aux, sec))
9383 res = FALSE;
9384 }
9385 }
9386
9387 if (aux.symtab)
9388 free (aux.symtab);
9389 if (aux.strtab)
9390 free ((char *) aux.strtab);
9391
9392 return res;
9393 }
9394
9395 static bfd_boolean
9396 process_unwind (Filedata * filedata)
9397 {
9398 struct unwind_handler
9399 {
9400 unsigned int machtype;
9401 bfd_boolean (* handler)(Filedata *);
9402 } handlers[] =
9403 {
9404 { EM_ARM, arm_process_unwind },
9405 { EM_IA_64, ia64_process_unwind },
9406 { EM_PARISC, hppa_process_unwind },
9407 { EM_TI_C6000, arm_process_unwind },
9408 { 0, NULL }
9409 };
9410 int i;
9411
9412 if (!do_unwind)
9413 return TRUE;
9414
9415 for (i = 0; handlers[i].handler != NULL; i++)
9416 if (filedata->file_header.e_machine == handlers[i].machtype)
9417 return handlers[i].handler (filedata);
9418
9419 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
9420 get_machine_name (filedata->file_header.e_machine));
9421 return TRUE;
9422 }
9423
9424 static void
9425 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
9426 {
9427 switch (entry->d_tag)
9428 {
9429 case DT_AARCH64_BTI_PLT:
9430 case DT_AARCH64_PAC_PLT:
9431 break;
9432 default:
9433 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9434 break;
9435 }
9436 putchar ('\n');
9437 }
9438
9439 static void
9440 dynamic_section_mips_val (Elf_Internal_Dyn * entry)
9441 {
9442 switch (entry->d_tag)
9443 {
9444 case DT_MIPS_FLAGS:
9445 if (entry->d_un.d_val == 0)
9446 printf (_("NONE"));
9447 else
9448 {
9449 static const char * opts[] =
9450 {
9451 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
9452 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
9453 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
9454 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
9455 "RLD_ORDER_SAFE"
9456 };
9457 unsigned int cnt;
9458 bfd_boolean first = TRUE;
9459
9460 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
9461 if (entry->d_un.d_val & (1 << cnt))
9462 {
9463 printf ("%s%s", first ? "" : " ", opts[cnt]);
9464 first = FALSE;
9465 }
9466 }
9467 break;
9468
9469 case DT_MIPS_IVERSION:
9470 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
9471 printf (_("Interface Version: %s"), GET_DYNAMIC_NAME (entry->d_un.d_val));
9472 else
9473 {
9474 char buf[40];
9475 sprintf_vma (buf, entry->d_un.d_ptr);
9476 /* Note: coded this way so that there is a single string for translation. */
9477 printf (_("<corrupt: %s>"), buf);
9478 }
9479 break;
9480
9481 case DT_MIPS_TIME_STAMP:
9482 {
9483 char timebuf[128];
9484 struct tm * tmp;
9485 time_t atime = entry->d_un.d_val;
9486
9487 tmp = gmtime (&atime);
9488 /* PR 17531: file: 6accc532. */
9489 if (tmp == NULL)
9490 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
9491 else
9492 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
9493 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9494 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9495 printf (_("Time Stamp: %s"), timebuf);
9496 }
9497 break;
9498
9499 case DT_MIPS_RLD_VERSION:
9500 case DT_MIPS_LOCAL_GOTNO:
9501 case DT_MIPS_CONFLICTNO:
9502 case DT_MIPS_LIBLISTNO:
9503 case DT_MIPS_SYMTABNO:
9504 case DT_MIPS_UNREFEXTNO:
9505 case DT_MIPS_HIPAGENO:
9506 case DT_MIPS_DELTA_CLASS_NO:
9507 case DT_MIPS_DELTA_INSTANCE_NO:
9508 case DT_MIPS_DELTA_RELOC_NO:
9509 case DT_MIPS_DELTA_SYM_NO:
9510 case DT_MIPS_DELTA_CLASSSYM_NO:
9511 case DT_MIPS_COMPACT_SIZE:
9512 print_vma (entry->d_un.d_val, DEC);
9513 break;
9514
9515 default:
9516 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9517 }
9518 putchar ('\n');
9519 }
9520
9521 static void
9522 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
9523 {
9524 switch (entry->d_tag)
9525 {
9526 case DT_HP_DLD_FLAGS:
9527 {
9528 static struct
9529 {
9530 long int bit;
9531 const char * str;
9532 }
9533 flags[] =
9534 {
9535 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
9536 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
9537 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
9538 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
9539 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
9540 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
9541 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
9542 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
9543 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
9544 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
9545 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
9546 { DT_HP_GST, "HP_GST" },
9547 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
9548 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
9549 { DT_HP_NODELETE, "HP_NODELETE" },
9550 { DT_HP_GROUP, "HP_GROUP" },
9551 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
9552 };
9553 bfd_boolean first = TRUE;
9554 size_t cnt;
9555 bfd_vma val = entry->d_un.d_val;
9556
9557 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
9558 if (val & flags[cnt].bit)
9559 {
9560 if (! first)
9561 putchar (' ');
9562 fputs (flags[cnt].str, stdout);
9563 first = FALSE;
9564 val ^= flags[cnt].bit;
9565 }
9566
9567 if (val != 0 || first)
9568 {
9569 if (! first)
9570 putchar (' ');
9571 print_vma (val, HEX);
9572 }
9573 }
9574 break;
9575
9576 default:
9577 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9578 break;
9579 }
9580 putchar ('\n');
9581 }
9582
9583 #ifdef BFD64
9584
9585 /* VMS vs Unix time offset and factor. */
9586
9587 #define VMS_EPOCH_OFFSET 35067168000000000LL
9588 #define VMS_GRANULARITY_FACTOR 10000000
9589
9590 /* Display a VMS time in a human readable format. */
9591
9592 static void
9593 print_vms_time (bfd_int64_t vmstime)
9594 {
9595 struct tm *tm;
9596 time_t unxtime;
9597
9598 unxtime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
9599 tm = gmtime (&unxtime);
9600 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
9601 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
9602 tm->tm_hour, tm->tm_min, tm->tm_sec);
9603 }
9604 #endif /* BFD64 */
9605
9606 static void
9607 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
9608 {
9609 switch (entry->d_tag)
9610 {
9611 case DT_IA_64_PLT_RESERVE:
9612 /* First 3 slots reserved. */
9613 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9614 printf (" -- ");
9615 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
9616 break;
9617
9618 case DT_IA_64_VMS_LINKTIME:
9619 #ifdef BFD64
9620 print_vms_time (entry->d_un.d_val);
9621 #endif
9622 break;
9623
9624 case DT_IA_64_VMS_LNKFLAGS:
9625 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9626 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
9627 printf (" CALL_DEBUG");
9628 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
9629 printf (" NOP0BUFS");
9630 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
9631 printf (" P0IMAGE");
9632 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
9633 printf (" MKTHREADS");
9634 if (entry->d_un.d_val & VMS_LF_UPCALLS)
9635 printf (" UPCALLS");
9636 if (entry->d_un.d_val & VMS_LF_IMGSTA)
9637 printf (" IMGSTA");
9638 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
9639 printf (" INITIALIZE");
9640 if (entry->d_un.d_val & VMS_LF_MAIN)
9641 printf (" MAIN");
9642 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
9643 printf (" EXE_INIT");
9644 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
9645 printf (" TBK_IN_IMG");
9646 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
9647 printf (" DBG_IN_IMG");
9648 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
9649 printf (" TBK_IN_DSF");
9650 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
9651 printf (" DBG_IN_DSF");
9652 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
9653 printf (" SIGNATURES");
9654 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
9655 printf (" REL_SEG_OFF");
9656 break;
9657
9658 default:
9659 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9660 break;
9661 }
9662 putchar ('\n');
9663 }
9664
9665 static bfd_boolean
9666 get_32bit_dynamic_section (Filedata * filedata)
9667 {
9668 Elf32_External_Dyn * edyn;
9669 Elf32_External_Dyn * ext;
9670 Elf_Internal_Dyn * entry;
9671
9672 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata, dynamic_addr, 1,
9673 dynamic_size, _("dynamic section"));
9674 if (!edyn)
9675 return FALSE;
9676
9677 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
9678 might not have the luxury of section headers. Look for the DT_NULL
9679 terminator to determine the number of entries. */
9680 for (ext = edyn, dynamic_nent = 0;
9681 (char *) (ext + 1) <= (char *) edyn + dynamic_size;
9682 ext++)
9683 {
9684 dynamic_nent++;
9685 if (BYTE_GET (ext->d_tag) == DT_NULL)
9686 break;
9687 }
9688
9689 dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
9690 sizeof (* entry));
9691 if (dynamic_section == NULL)
9692 {
9693 error (_("Out of memory allocating space for %lu dynamic entries\n"),
9694 (unsigned long) dynamic_nent);
9695 free (edyn);
9696 return FALSE;
9697 }
9698
9699 for (ext = edyn, entry = dynamic_section;
9700 entry < dynamic_section + dynamic_nent;
9701 ext++, entry++)
9702 {
9703 entry->d_tag = BYTE_GET (ext->d_tag);
9704 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9705 }
9706
9707 free (edyn);
9708
9709 return TRUE;
9710 }
9711
9712 static bfd_boolean
9713 get_64bit_dynamic_section (Filedata * filedata)
9714 {
9715 Elf64_External_Dyn * edyn;
9716 Elf64_External_Dyn * ext;
9717 Elf_Internal_Dyn * entry;
9718
9719 /* Read in the data. */
9720 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata, dynamic_addr, 1,
9721 dynamic_size, _("dynamic section"));
9722 if (!edyn)
9723 return FALSE;
9724
9725 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
9726 might not have the luxury of section headers. Look for the DT_NULL
9727 terminator to determine the number of entries. */
9728 for (ext = edyn, dynamic_nent = 0;
9729 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
9730 (char *) (ext + 1) <= (char *) edyn + dynamic_size;
9731 ext++)
9732 {
9733 dynamic_nent++;
9734 if (BYTE_GET (ext->d_tag) == DT_NULL)
9735 break;
9736 }
9737
9738 dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
9739 sizeof (* entry));
9740 if (dynamic_section == NULL)
9741 {
9742 error (_("Out of memory allocating space for %lu dynamic entries\n"),
9743 (unsigned long) dynamic_nent);
9744 free (edyn);
9745 return FALSE;
9746 }
9747
9748 /* Convert from external to internal formats. */
9749 for (ext = edyn, entry = dynamic_section;
9750 entry < dynamic_section + dynamic_nent;
9751 ext++, entry++)
9752 {
9753 entry->d_tag = BYTE_GET (ext->d_tag);
9754 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
9755 }
9756
9757 free (edyn);
9758
9759 return TRUE;
9760 }
9761
9762 static void
9763 print_dynamic_flags (bfd_vma flags)
9764 {
9765 bfd_boolean first = TRUE;
9766
9767 while (flags)
9768 {
9769 bfd_vma flag;
9770
9771 flag = flags & - flags;
9772 flags &= ~ flag;
9773
9774 if (first)
9775 first = FALSE;
9776 else
9777 putc (' ', stdout);
9778
9779 switch (flag)
9780 {
9781 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
9782 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
9783 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
9784 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
9785 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
9786 default: fputs (_("unknown"), stdout); break;
9787 }
9788 }
9789 puts ("");
9790 }
9791
9792 /* Parse and display the contents of the dynamic section. */
9793
9794 static bfd_boolean
9795 process_dynamic_section (Filedata * filedata)
9796 {
9797 Elf_Internal_Dyn * entry;
9798
9799 if (dynamic_size == 0)
9800 {
9801 if (do_dynamic)
9802 printf (_("\nThere is no dynamic section in this file.\n"));
9803
9804 return TRUE;
9805 }
9806
9807 if (is_32bit_elf)
9808 {
9809 if (! get_32bit_dynamic_section (filedata))
9810 return FALSE;
9811 }
9812 else
9813 {
9814 if (! get_64bit_dynamic_section (filedata))
9815 return FALSE;
9816 }
9817
9818 /* Find the appropriate symbol table. */
9819 if (dynamic_symbols == NULL)
9820 {
9821 for (entry = dynamic_section;
9822 entry < dynamic_section + dynamic_nent;
9823 ++entry)
9824 {
9825 Elf_Internal_Shdr section;
9826
9827 if (entry->d_tag != DT_SYMTAB)
9828 continue;
9829
9830 dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
9831
9832 /* Since we do not know how big the symbol table is,
9833 we default to reading in the entire file (!) and
9834 processing that. This is overkill, I know, but it
9835 should work. */
9836 section.sh_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
9837 if ((bfd_size_type) section.sh_offset > filedata->file_size)
9838 {
9839 /* See PR 21379 for a reproducer. */
9840 error (_("Invalid DT_SYMTAB entry: %lx"), (long) section.sh_offset);
9841 return FALSE;
9842 }
9843
9844 if (archive_file_offset != 0)
9845 section.sh_size = archive_file_size - section.sh_offset;
9846 else
9847 section.sh_size = filedata->file_size - section.sh_offset;
9848
9849 if (is_32bit_elf)
9850 section.sh_entsize = sizeof (Elf32_External_Sym);
9851 else
9852 section.sh_entsize = sizeof (Elf64_External_Sym);
9853 section.sh_name = filedata->string_table_length;
9854
9855 if (dynamic_symbols != NULL)
9856 {
9857 error (_("Multiple dynamic symbol table sections found\n"));
9858 free (dynamic_symbols);
9859 }
9860 dynamic_symbols = GET_ELF_SYMBOLS (filedata, &section, & num_dynamic_syms);
9861 if (num_dynamic_syms < 1)
9862 {
9863 error (_("Unable to determine the number of symbols to load\n"));
9864 continue;
9865 }
9866 }
9867 }
9868
9869 /* Similarly find a string table. */
9870 if (dynamic_strings == NULL)
9871 {
9872 for (entry = dynamic_section;
9873 entry < dynamic_section + dynamic_nent;
9874 ++entry)
9875 {
9876 unsigned long offset;
9877 long str_tab_len;
9878
9879 if (entry->d_tag != DT_STRTAB)
9880 continue;
9881
9882 dynamic_info[DT_STRTAB] = entry->d_un.d_val;
9883
9884 /* Since we do not know how big the string table is,
9885 we default to reading in the entire file (!) and
9886 processing that. This is overkill, I know, but it
9887 should work. */
9888
9889 offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
9890
9891 if (archive_file_offset != 0)
9892 str_tab_len = archive_file_size - offset;
9893 else
9894 str_tab_len = filedata->file_size - offset;
9895
9896 if (str_tab_len < 1)
9897 {
9898 error
9899 (_("Unable to determine the length of the dynamic string table\n"));
9900 continue;
9901 }
9902
9903 if (dynamic_strings != NULL)
9904 {
9905 error (_("Multiple dynamic string tables found\n"));
9906 free (dynamic_strings);
9907 }
9908
9909 dynamic_strings = (char *) get_data (NULL, filedata, offset, 1,
9910 str_tab_len,
9911 _("dynamic string table"));
9912 dynamic_strings_length = dynamic_strings == NULL ? 0 : str_tab_len;
9913 }
9914 }
9915
9916 /* And find the syminfo section if available. */
9917 if (dynamic_syminfo == NULL)
9918 {
9919 unsigned long syminsz = 0;
9920
9921 for (entry = dynamic_section;
9922 entry < dynamic_section + dynamic_nent;
9923 ++entry)
9924 {
9925 if (entry->d_tag == DT_SYMINENT)
9926 {
9927 /* Note: these braces are necessary to avoid a syntax
9928 error from the SunOS4 C compiler. */
9929 /* PR binutils/17531: A corrupt file can trigger this test.
9930 So do not use an assert, instead generate an error message. */
9931 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
9932 error (_("Bad value (%d) for SYMINENT entry\n"),
9933 (int) entry->d_un.d_val);
9934 }
9935 else if (entry->d_tag == DT_SYMINSZ)
9936 syminsz = entry->d_un.d_val;
9937 else if (entry->d_tag == DT_SYMINFO)
9938 dynamic_syminfo_offset = offset_from_vma (filedata, entry->d_un.d_val,
9939 syminsz);
9940 }
9941
9942 if (dynamic_syminfo_offset != 0 && syminsz != 0)
9943 {
9944 Elf_External_Syminfo * extsyminfo;
9945 Elf_External_Syminfo * extsym;
9946 Elf_Internal_Syminfo * syminfo;
9947
9948 /* There is a syminfo section. Read the data. */
9949 extsyminfo = (Elf_External_Syminfo *)
9950 get_data (NULL, filedata, dynamic_syminfo_offset, 1, syminsz,
9951 _("symbol information"));
9952 if (!extsyminfo)
9953 return FALSE;
9954
9955 if (dynamic_syminfo != NULL)
9956 {
9957 error (_("Multiple dynamic symbol information sections found\n"));
9958 free (dynamic_syminfo);
9959 }
9960 dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
9961 if (dynamic_syminfo == NULL)
9962 {
9963 error (_("Out of memory allocating %lu byte for dynamic symbol info\n"),
9964 (unsigned long) syminsz);
9965 return FALSE;
9966 }
9967
9968 dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
9969 for (syminfo = dynamic_syminfo, extsym = extsyminfo;
9970 syminfo < dynamic_syminfo + dynamic_syminfo_nent;
9971 ++syminfo, ++extsym)
9972 {
9973 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
9974 syminfo->si_flags = BYTE_GET (extsym->si_flags);
9975 }
9976
9977 free (extsyminfo);
9978 }
9979 }
9980
9981 if (do_dynamic && dynamic_addr)
9982 printf (ngettext ("\nDynamic section at offset 0x%lx "
9983 "contains %lu entry:\n",
9984 "\nDynamic section at offset 0x%lx "
9985 "contains %lu entries:\n",
9986 dynamic_nent),
9987 dynamic_addr, (unsigned long) dynamic_nent);
9988 if (do_dynamic)
9989 printf (_(" Tag Type Name/Value\n"));
9990
9991 for (entry = dynamic_section;
9992 entry < dynamic_section + dynamic_nent;
9993 entry++)
9994 {
9995 if (do_dynamic)
9996 {
9997 const char * dtype;
9998
9999 putchar (' ');
10000 print_vma (entry->d_tag, FULL_HEX);
10001 dtype = get_dynamic_type (filedata, entry->d_tag);
10002 printf (" (%s)%*s", dtype,
10003 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
10004 }
10005
10006 switch (entry->d_tag)
10007 {
10008 case DT_FLAGS:
10009 if (do_dynamic)
10010 print_dynamic_flags (entry->d_un.d_val);
10011 break;
10012
10013 case DT_AUXILIARY:
10014 case DT_FILTER:
10015 case DT_CONFIG:
10016 case DT_DEPAUDIT:
10017 case DT_AUDIT:
10018 if (do_dynamic)
10019 {
10020 switch (entry->d_tag)
10021 {
10022 case DT_AUXILIARY:
10023 printf (_("Auxiliary library"));
10024 break;
10025
10026 case DT_FILTER:
10027 printf (_("Filter library"));
10028 break;
10029
10030 case DT_CONFIG:
10031 printf (_("Configuration file"));
10032 break;
10033
10034 case DT_DEPAUDIT:
10035 printf (_("Dependency audit library"));
10036 break;
10037
10038 case DT_AUDIT:
10039 printf (_("Audit library"));
10040 break;
10041 }
10042
10043 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
10044 printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
10045 else
10046 {
10047 printf (": ");
10048 print_vma (entry->d_un.d_val, PREFIX_HEX);
10049 putchar ('\n');
10050 }
10051 }
10052 break;
10053
10054 case DT_FEATURE:
10055 if (do_dynamic)
10056 {
10057 printf (_("Flags:"));
10058
10059 if (entry->d_un.d_val == 0)
10060 printf (_(" None\n"));
10061 else
10062 {
10063 unsigned long int val = entry->d_un.d_val;
10064
10065 if (val & DTF_1_PARINIT)
10066 {
10067 printf (" PARINIT");
10068 val ^= DTF_1_PARINIT;
10069 }
10070 if (val & DTF_1_CONFEXP)
10071 {
10072 printf (" CONFEXP");
10073 val ^= DTF_1_CONFEXP;
10074 }
10075 if (val != 0)
10076 printf (" %lx", val);
10077 puts ("");
10078 }
10079 }
10080 break;
10081
10082 case DT_POSFLAG_1:
10083 if (do_dynamic)
10084 {
10085 printf (_("Flags:"));
10086
10087 if (entry->d_un.d_val == 0)
10088 printf (_(" None\n"));
10089 else
10090 {
10091 unsigned long int val = entry->d_un.d_val;
10092
10093 if (val & DF_P1_LAZYLOAD)
10094 {
10095 printf (" LAZYLOAD");
10096 val ^= DF_P1_LAZYLOAD;
10097 }
10098 if (val & DF_P1_GROUPPERM)
10099 {
10100 printf (" GROUPPERM");
10101 val ^= DF_P1_GROUPPERM;
10102 }
10103 if (val != 0)
10104 printf (" %lx", val);
10105 puts ("");
10106 }
10107 }
10108 break;
10109
10110 case DT_FLAGS_1:
10111 if (do_dynamic)
10112 {
10113 printf (_("Flags:"));
10114 if (entry->d_un.d_val == 0)
10115 printf (_(" None\n"));
10116 else
10117 {
10118 unsigned long int val = entry->d_un.d_val;
10119
10120 if (val & DF_1_NOW)
10121 {
10122 printf (" NOW");
10123 val ^= DF_1_NOW;
10124 }
10125 if (val & DF_1_GLOBAL)
10126 {
10127 printf (" GLOBAL");
10128 val ^= DF_1_GLOBAL;
10129 }
10130 if (val & DF_1_GROUP)
10131 {
10132 printf (" GROUP");
10133 val ^= DF_1_GROUP;
10134 }
10135 if (val & DF_1_NODELETE)
10136 {
10137 printf (" NODELETE");
10138 val ^= DF_1_NODELETE;
10139 }
10140 if (val & DF_1_LOADFLTR)
10141 {
10142 printf (" LOADFLTR");
10143 val ^= DF_1_LOADFLTR;
10144 }
10145 if (val & DF_1_INITFIRST)
10146 {
10147 printf (" INITFIRST");
10148 val ^= DF_1_INITFIRST;
10149 }
10150 if (val & DF_1_NOOPEN)
10151 {
10152 printf (" NOOPEN");
10153 val ^= DF_1_NOOPEN;
10154 }
10155 if (val & DF_1_ORIGIN)
10156 {
10157 printf (" ORIGIN");
10158 val ^= DF_1_ORIGIN;
10159 }
10160 if (val & DF_1_DIRECT)
10161 {
10162 printf (" DIRECT");
10163 val ^= DF_1_DIRECT;
10164 }
10165 if (val & DF_1_TRANS)
10166 {
10167 printf (" TRANS");
10168 val ^= DF_1_TRANS;
10169 }
10170 if (val & DF_1_INTERPOSE)
10171 {
10172 printf (" INTERPOSE");
10173 val ^= DF_1_INTERPOSE;
10174 }
10175 if (val & DF_1_NODEFLIB)
10176 {
10177 printf (" NODEFLIB");
10178 val ^= DF_1_NODEFLIB;
10179 }
10180 if (val & DF_1_NODUMP)
10181 {
10182 printf (" NODUMP");
10183 val ^= DF_1_NODUMP;
10184 }
10185 if (val & DF_1_CONFALT)
10186 {
10187 printf (" CONFALT");
10188 val ^= DF_1_CONFALT;
10189 }
10190 if (val & DF_1_ENDFILTEE)
10191 {
10192 printf (" ENDFILTEE");
10193 val ^= DF_1_ENDFILTEE;
10194 }
10195 if (val & DF_1_DISPRELDNE)
10196 {
10197 printf (" DISPRELDNE");
10198 val ^= DF_1_DISPRELDNE;
10199 }
10200 if (val & DF_1_DISPRELPND)
10201 {
10202 printf (" DISPRELPND");
10203 val ^= DF_1_DISPRELPND;
10204 }
10205 if (val & DF_1_NODIRECT)
10206 {
10207 printf (" NODIRECT");
10208 val ^= DF_1_NODIRECT;
10209 }
10210 if (val & DF_1_IGNMULDEF)
10211 {
10212 printf (" IGNMULDEF");
10213 val ^= DF_1_IGNMULDEF;
10214 }
10215 if (val & DF_1_NOKSYMS)
10216 {
10217 printf (" NOKSYMS");
10218 val ^= DF_1_NOKSYMS;
10219 }
10220 if (val & DF_1_NOHDR)
10221 {
10222 printf (" NOHDR");
10223 val ^= DF_1_NOHDR;
10224 }
10225 if (val & DF_1_EDITED)
10226 {
10227 printf (" EDITED");
10228 val ^= DF_1_EDITED;
10229 }
10230 if (val & DF_1_NORELOC)
10231 {
10232 printf (" NORELOC");
10233 val ^= DF_1_NORELOC;
10234 }
10235 if (val & DF_1_SYMINTPOSE)
10236 {
10237 printf (" SYMINTPOSE");
10238 val ^= DF_1_SYMINTPOSE;
10239 }
10240 if (val & DF_1_GLOBAUDIT)
10241 {
10242 printf (" GLOBAUDIT");
10243 val ^= DF_1_GLOBAUDIT;
10244 }
10245 if (val & DF_1_SINGLETON)
10246 {
10247 printf (" SINGLETON");
10248 val ^= DF_1_SINGLETON;
10249 }
10250 if (val & DF_1_STUB)
10251 {
10252 printf (" STUB");
10253 val ^= DF_1_STUB;
10254 }
10255 if (val & DF_1_PIE)
10256 {
10257 printf (" PIE");
10258 val ^= DF_1_PIE;
10259 }
10260 if (val & DF_1_KMOD)
10261 {
10262 printf (" KMOD");
10263 val ^= DF_1_KMOD;
10264 }
10265 if (val & DF_1_WEAKFILTER)
10266 {
10267 printf (" WEAKFILTER");
10268 val ^= DF_1_WEAKFILTER;
10269 }
10270 if (val & DF_1_NOCOMMON)
10271 {
10272 printf (" NOCOMMON");
10273 val ^= DF_1_NOCOMMON;
10274 }
10275 if (val != 0)
10276 printf (" %lx", val);
10277 puts ("");
10278 }
10279 }
10280 break;
10281
10282 case DT_PLTREL:
10283 dynamic_info[entry->d_tag] = entry->d_un.d_val;
10284 if (do_dynamic)
10285 puts (get_dynamic_type (filedata, entry->d_un.d_val));
10286 break;
10287
10288 case DT_NULL :
10289 case DT_NEEDED :
10290 case DT_PLTGOT :
10291 case DT_HASH :
10292 case DT_STRTAB :
10293 case DT_SYMTAB :
10294 case DT_RELA :
10295 case DT_INIT :
10296 case DT_FINI :
10297 case DT_SONAME :
10298 case DT_RPATH :
10299 case DT_SYMBOLIC:
10300 case DT_REL :
10301 case DT_DEBUG :
10302 case DT_TEXTREL :
10303 case DT_JMPREL :
10304 case DT_RUNPATH :
10305 dynamic_info[entry->d_tag] = entry->d_un.d_val;
10306
10307 if (do_dynamic)
10308 {
10309 char * name;
10310
10311 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
10312 name = GET_DYNAMIC_NAME (entry->d_un.d_val);
10313 else
10314 name = NULL;
10315
10316 if (name)
10317 {
10318 switch (entry->d_tag)
10319 {
10320 case DT_NEEDED:
10321 printf (_("Shared library: [%s]"), name);
10322
10323 if (streq (name, program_interpreter))
10324 printf (_(" program interpreter"));
10325 break;
10326
10327 case DT_SONAME:
10328 printf (_("Library soname: [%s]"), name);
10329 break;
10330
10331 case DT_RPATH:
10332 printf (_("Library rpath: [%s]"), name);
10333 break;
10334
10335 case DT_RUNPATH:
10336 printf (_("Library runpath: [%s]"), name);
10337 break;
10338
10339 default:
10340 print_vma (entry->d_un.d_val, PREFIX_HEX);
10341 break;
10342 }
10343 }
10344 else
10345 print_vma (entry->d_un.d_val, PREFIX_HEX);
10346
10347 putchar ('\n');
10348 }
10349 break;
10350
10351 case DT_PLTRELSZ:
10352 case DT_RELASZ :
10353 case DT_STRSZ :
10354 case DT_RELSZ :
10355 case DT_RELAENT :
10356 case DT_SYMENT :
10357 case DT_RELENT :
10358 dynamic_info[entry->d_tag] = entry->d_un.d_val;
10359 /* Fall through. */
10360 case DT_PLTPADSZ:
10361 case DT_MOVEENT :
10362 case DT_MOVESZ :
10363 case DT_INIT_ARRAYSZ:
10364 case DT_FINI_ARRAYSZ:
10365 case DT_GNU_CONFLICTSZ:
10366 case DT_GNU_LIBLISTSZ:
10367 if (do_dynamic)
10368 {
10369 print_vma (entry->d_un.d_val, UNSIGNED);
10370 printf (_(" (bytes)\n"));
10371 }
10372 break;
10373
10374 case DT_VERDEFNUM:
10375 case DT_VERNEEDNUM:
10376 case DT_RELACOUNT:
10377 case DT_RELCOUNT:
10378 if (do_dynamic)
10379 {
10380 print_vma (entry->d_un.d_val, UNSIGNED);
10381 putchar ('\n');
10382 }
10383 break;
10384
10385 case DT_SYMINSZ:
10386 case DT_SYMINENT:
10387 case DT_SYMINFO:
10388 case DT_USED:
10389 case DT_INIT_ARRAY:
10390 case DT_FINI_ARRAY:
10391 if (do_dynamic)
10392 {
10393 if (entry->d_tag == DT_USED
10394 && VALID_DYNAMIC_NAME (entry->d_un.d_val))
10395 {
10396 char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
10397
10398 if (*name)
10399 {
10400 printf (_("Not needed object: [%s]\n"), name);
10401 break;
10402 }
10403 }
10404
10405 print_vma (entry->d_un.d_val, PREFIX_HEX);
10406 putchar ('\n');
10407 }
10408 break;
10409
10410 case DT_BIND_NOW:
10411 /* The value of this entry is ignored. */
10412 if (do_dynamic)
10413 putchar ('\n');
10414 break;
10415
10416 case DT_GNU_PRELINKED:
10417 if (do_dynamic)
10418 {
10419 struct tm * tmp;
10420 time_t atime = entry->d_un.d_val;
10421
10422 tmp = gmtime (&atime);
10423 /* PR 17533 file: 041-1244816-0.004. */
10424 if (tmp == NULL)
10425 printf (_("<corrupt time val: %lx"),
10426 (unsigned long) atime);
10427 else
10428 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
10429 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10430 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10431
10432 }
10433 break;
10434
10435 case DT_GNU_HASH:
10436 dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10437 if (do_dynamic)
10438 {
10439 print_vma (entry->d_un.d_val, PREFIX_HEX);
10440 putchar ('\n');
10441 }
10442 break;
10443
10444 default:
10445 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
10446 version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
10447 entry->d_un.d_val;
10448
10449 if (do_dynamic)
10450 {
10451 switch (filedata->file_header.e_machine)
10452 {
10453 case EM_AARCH64:
10454 dynamic_section_aarch64_val (entry);
10455 break;
10456 case EM_MIPS:
10457 case EM_MIPS_RS3_LE:
10458 dynamic_section_mips_val (entry);
10459 break;
10460 case EM_PARISC:
10461 dynamic_section_parisc_val (entry);
10462 break;
10463 case EM_IA_64:
10464 dynamic_section_ia64_val (entry);
10465 break;
10466 default:
10467 print_vma (entry->d_un.d_val, PREFIX_HEX);
10468 putchar ('\n');
10469 }
10470 }
10471 break;
10472 }
10473 }
10474
10475 return TRUE;
10476 }
10477
10478 static char *
10479 get_ver_flags (unsigned int flags)
10480 {
10481 static char buff[128];
10482
10483 buff[0] = 0;
10484
10485 if (flags == 0)
10486 return _("none");
10487
10488 if (flags & VER_FLG_BASE)
10489 strcat (buff, "BASE");
10490
10491 if (flags & VER_FLG_WEAK)
10492 {
10493 if (flags & VER_FLG_BASE)
10494 strcat (buff, " | ");
10495
10496 strcat (buff, "WEAK");
10497 }
10498
10499 if (flags & VER_FLG_INFO)
10500 {
10501 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
10502 strcat (buff, " | ");
10503
10504 strcat (buff, "INFO");
10505 }
10506
10507 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
10508 {
10509 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
10510 strcat (buff, " | ");
10511
10512 strcat (buff, _("<unknown>"));
10513 }
10514
10515 return buff;
10516 }
10517
10518 /* Display the contents of the version sections. */
10519
10520 static bfd_boolean
10521 process_version_sections (Filedata * filedata)
10522 {
10523 Elf_Internal_Shdr * section;
10524 unsigned i;
10525 bfd_boolean found = FALSE;
10526
10527 if (! do_version)
10528 return TRUE;
10529
10530 for (i = 0, section = filedata->section_headers;
10531 i < filedata->file_header.e_shnum;
10532 i++, section++)
10533 {
10534 switch (section->sh_type)
10535 {
10536 case SHT_GNU_verdef:
10537 {
10538 Elf_External_Verdef * edefs;
10539 unsigned long idx;
10540 unsigned long cnt;
10541 char * endbuf;
10542
10543 found = TRUE;
10544
10545 printf (ngettext ("\nVersion definition section '%s' "
10546 "contains %u entry:\n",
10547 "\nVersion definition section '%s' "
10548 "contains %u entries:\n",
10549 section->sh_info),
10550 printable_section_name (filedata, section),
10551 section->sh_info);
10552
10553 printf (_(" Addr: 0x"));
10554 printf_vma (section->sh_addr);
10555 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
10556 (unsigned long) section->sh_offset, section->sh_link,
10557 printable_section_name_from_index (filedata, section->sh_link));
10558
10559 edefs = (Elf_External_Verdef *)
10560 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
10561 _("version definition section"));
10562 if (!edefs)
10563 break;
10564 endbuf = (char *) edefs + section->sh_size;
10565
10566 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
10567 {
10568 char * vstart;
10569 Elf_External_Verdef * edef;
10570 Elf_Internal_Verdef ent;
10571 Elf_External_Verdaux * eaux;
10572 Elf_Internal_Verdaux aux;
10573 unsigned long isum;
10574 int j;
10575
10576 vstart = ((char *) edefs) + idx;
10577 if (vstart + sizeof (*edef) > endbuf)
10578 break;
10579
10580 edef = (Elf_External_Verdef *) vstart;
10581
10582 ent.vd_version = BYTE_GET (edef->vd_version);
10583 ent.vd_flags = BYTE_GET (edef->vd_flags);
10584 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
10585 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
10586 ent.vd_hash = BYTE_GET (edef->vd_hash);
10587 ent.vd_aux = BYTE_GET (edef->vd_aux);
10588 ent.vd_next = BYTE_GET (edef->vd_next);
10589
10590 printf (_(" %#06lx: Rev: %d Flags: %s"),
10591 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
10592
10593 printf (_(" Index: %d Cnt: %d "),
10594 ent.vd_ndx, ent.vd_cnt);
10595
10596 /* Check for overflow. */
10597 if (ent.vd_aux > (size_t) (endbuf - vstart))
10598 break;
10599
10600 vstart += ent.vd_aux;
10601
10602 if (vstart + sizeof (*eaux) > endbuf)
10603 break;
10604 eaux = (Elf_External_Verdaux *) vstart;
10605
10606 aux.vda_name = BYTE_GET (eaux->vda_name);
10607 aux.vda_next = BYTE_GET (eaux->vda_next);
10608
10609 if (VALID_DYNAMIC_NAME (aux.vda_name))
10610 printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
10611 else
10612 printf (_("Name index: %ld\n"), aux.vda_name);
10613
10614 isum = idx + ent.vd_aux;
10615
10616 for (j = 1; j < ent.vd_cnt; j++)
10617 {
10618 if (aux.vda_next < sizeof (*eaux)
10619 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
10620 {
10621 warn (_("Invalid vda_next field of %lx\n"),
10622 aux.vda_next);
10623 j = ent.vd_cnt;
10624 break;
10625 }
10626 /* Check for overflow. */
10627 if (aux.vda_next > (size_t) (endbuf - vstart))
10628 break;
10629
10630 isum += aux.vda_next;
10631 vstart += aux.vda_next;
10632
10633 if (vstart + sizeof (*eaux) > endbuf)
10634 break;
10635 eaux = (Elf_External_Verdaux *) vstart;
10636
10637 aux.vda_name = BYTE_GET (eaux->vda_name);
10638 aux.vda_next = BYTE_GET (eaux->vda_next);
10639
10640 if (VALID_DYNAMIC_NAME (aux.vda_name))
10641 printf (_(" %#06lx: Parent %d: %s\n"),
10642 isum, j, GET_DYNAMIC_NAME (aux.vda_name));
10643 else
10644 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
10645 isum, j, aux.vda_name);
10646 }
10647
10648 if (j < ent.vd_cnt)
10649 printf (_(" Version def aux past end of section\n"));
10650
10651 /* PR 17531:
10652 file: id:000001,src:000172+005151,op:splice,rep:2. */
10653 if (ent.vd_next < sizeof (*edef)
10654 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
10655 {
10656 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
10657 cnt = section->sh_info;
10658 break;
10659 }
10660 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
10661 break;
10662
10663 idx += ent.vd_next;
10664 }
10665
10666 if (cnt < section->sh_info)
10667 printf (_(" Version definition past end of section\n"));
10668
10669 free (edefs);
10670 }
10671 break;
10672
10673 case SHT_GNU_verneed:
10674 {
10675 Elf_External_Verneed * eneed;
10676 unsigned long idx;
10677 unsigned long cnt;
10678 char * endbuf;
10679
10680 found = TRUE;
10681
10682 printf (ngettext ("\nVersion needs section '%s' "
10683 "contains %u entry:\n",
10684 "\nVersion needs section '%s' "
10685 "contains %u entries:\n",
10686 section->sh_info),
10687 printable_section_name (filedata, section), section->sh_info);
10688
10689 printf (_(" Addr: 0x"));
10690 printf_vma (section->sh_addr);
10691 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
10692 (unsigned long) section->sh_offset, section->sh_link,
10693 printable_section_name_from_index (filedata, section->sh_link));
10694
10695 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
10696 section->sh_offset, 1,
10697 section->sh_size,
10698 _("Version Needs section"));
10699 if (!eneed)
10700 break;
10701 endbuf = (char *) eneed + section->sh_size;
10702
10703 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
10704 {
10705 Elf_External_Verneed * entry;
10706 Elf_Internal_Verneed ent;
10707 unsigned long isum;
10708 int j;
10709 char * vstart;
10710
10711 vstart = ((char *) eneed) + idx;
10712 if (vstart + sizeof (*entry) > endbuf)
10713 break;
10714
10715 entry = (Elf_External_Verneed *) vstart;
10716
10717 ent.vn_version = BYTE_GET (entry->vn_version);
10718 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
10719 ent.vn_file = BYTE_GET (entry->vn_file);
10720 ent.vn_aux = BYTE_GET (entry->vn_aux);
10721 ent.vn_next = BYTE_GET (entry->vn_next);
10722
10723 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
10724
10725 if (VALID_DYNAMIC_NAME (ent.vn_file))
10726 printf (_(" File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
10727 else
10728 printf (_(" File: %lx"), ent.vn_file);
10729
10730 printf (_(" Cnt: %d\n"), ent.vn_cnt);
10731
10732 /* Check for overflow. */
10733 if (ent.vn_aux > (size_t) (endbuf - vstart))
10734 break;
10735 vstart += ent.vn_aux;
10736
10737 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
10738 {
10739 Elf_External_Vernaux * eaux;
10740 Elf_Internal_Vernaux aux;
10741
10742 if (vstart + sizeof (*eaux) > endbuf)
10743 break;
10744 eaux = (Elf_External_Vernaux *) vstart;
10745
10746 aux.vna_hash = BYTE_GET (eaux->vna_hash);
10747 aux.vna_flags = BYTE_GET (eaux->vna_flags);
10748 aux.vna_other = BYTE_GET (eaux->vna_other);
10749 aux.vna_name = BYTE_GET (eaux->vna_name);
10750 aux.vna_next = BYTE_GET (eaux->vna_next);
10751
10752 if (VALID_DYNAMIC_NAME (aux.vna_name))
10753 printf (_(" %#06lx: Name: %s"),
10754 isum, GET_DYNAMIC_NAME (aux.vna_name));
10755 else
10756 printf (_(" %#06lx: Name index: %lx"),
10757 isum, aux.vna_name);
10758
10759 printf (_(" Flags: %s Version: %d\n"),
10760 get_ver_flags (aux.vna_flags), aux.vna_other);
10761
10762 if (aux.vna_next < sizeof (*eaux)
10763 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
10764 {
10765 warn (_("Invalid vna_next field of %lx\n"),
10766 aux.vna_next);
10767 j = ent.vn_cnt;
10768 break;
10769 }
10770 /* Check for overflow. */
10771 if (aux.vna_next > (size_t) (endbuf - vstart))
10772 break;
10773 isum += aux.vna_next;
10774 vstart += aux.vna_next;
10775 }
10776
10777 if (j < ent.vn_cnt)
10778 warn (_("Missing Version Needs auxillary information\n"));
10779
10780 if (ent.vn_next < sizeof (*entry)
10781 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
10782 {
10783 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
10784 cnt = section->sh_info;
10785 break;
10786 }
10787 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
10788 break;
10789 idx += ent.vn_next;
10790 }
10791
10792 if (cnt < section->sh_info)
10793 warn (_("Missing Version Needs information\n"));
10794
10795 free (eneed);
10796 }
10797 break;
10798
10799 case SHT_GNU_versym:
10800 {
10801 Elf_Internal_Shdr * link_section;
10802 size_t total;
10803 unsigned int cnt;
10804 unsigned char * edata;
10805 unsigned short * data;
10806 char * strtab;
10807 Elf_Internal_Sym * symbols;
10808 Elf_Internal_Shdr * string_sec;
10809 unsigned long num_syms;
10810 long off;
10811
10812 if (section->sh_link >= filedata->file_header.e_shnum)
10813 break;
10814
10815 link_section = filedata->section_headers + section->sh_link;
10816 total = section->sh_size / sizeof (Elf_External_Versym);
10817
10818 if (link_section->sh_link >= filedata->file_header.e_shnum)
10819 break;
10820
10821 found = TRUE;
10822
10823 symbols = GET_ELF_SYMBOLS (filedata, link_section, & num_syms);
10824 if (symbols == NULL)
10825 break;
10826
10827 string_sec = filedata->section_headers + link_section->sh_link;
10828
10829 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
10830 string_sec->sh_size,
10831 _("version string table"));
10832 if (!strtab)
10833 {
10834 free (symbols);
10835 break;
10836 }
10837
10838 printf (ngettext ("\nVersion symbols section '%s' "
10839 "contains %lu entry:\n",
10840 "\nVersion symbols section '%s' "
10841 "contains %lu entries:\n",
10842 total),
10843 printable_section_name (filedata, section), (unsigned long) total);
10844
10845 printf (_(" Addr: "));
10846 printf_vma (section->sh_addr);
10847 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
10848 (unsigned long) section->sh_offset, section->sh_link,
10849 printable_section_name (filedata, link_section));
10850
10851 off = offset_from_vma (filedata,
10852 version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
10853 total * sizeof (short));
10854 edata = (unsigned char *) get_data (NULL, filedata, off, total,
10855 sizeof (short),
10856 _("version symbol data"));
10857 if (!edata)
10858 {
10859 free (strtab);
10860 free (symbols);
10861 break;
10862 }
10863
10864 data = (short unsigned int *) cmalloc (total, sizeof (short));
10865
10866 for (cnt = total; cnt --;)
10867 data[cnt] = byte_get (edata + cnt * sizeof (short),
10868 sizeof (short));
10869
10870 free (edata);
10871
10872 for (cnt = 0; cnt < total; cnt += 4)
10873 {
10874 int j, nn;
10875 char *name;
10876 char *invalid = _("*invalid*");
10877
10878 printf (" %03x:", cnt);
10879
10880 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
10881 switch (data[cnt + j])
10882 {
10883 case 0:
10884 fputs (_(" 0 (*local*) "), stdout);
10885 break;
10886
10887 case 1:
10888 fputs (_(" 1 (*global*) "), stdout);
10889 break;
10890
10891 default:
10892 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
10893 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
10894
10895 /* If this index value is greater than the size of the symbols
10896 array, break to avoid an out-of-bounds read. */
10897 if ((unsigned long)(cnt + j) >= num_syms)
10898 {
10899 warn (_("invalid index into symbol array\n"));
10900 break;
10901 }
10902
10903 name = NULL;
10904 if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
10905 {
10906 Elf_Internal_Verneed ivn;
10907 unsigned long offset;
10908
10909 offset = offset_from_vma
10910 (filedata, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
10911 sizeof (Elf_External_Verneed));
10912
10913 do
10914 {
10915 Elf_Internal_Vernaux ivna;
10916 Elf_External_Verneed evn;
10917 Elf_External_Vernaux evna;
10918 unsigned long a_off;
10919
10920 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
10921 _("version need")) == NULL)
10922 break;
10923
10924 ivn.vn_aux = BYTE_GET (evn.vn_aux);
10925 ivn.vn_next = BYTE_GET (evn.vn_next);
10926
10927 a_off = offset + ivn.vn_aux;
10928
10929 do
10930 {
10931 if (get_data (&evna, filedata, a_off, sizeof (evna),
10932 1, _("version need aux (2)")) == NULL)
10933 {
10934 ivna.vna_next = 0;
10935 ivna.vna_other = 0;
10936 }
10937 else
10938 {
10939 ivna.vna_next = BYTE_GET (evna.vna_next);
10940 ivna.vna_other = BYTE_GET (evna.vna_other);
10941 }
10942
10943 a_off += ivna.vna_next;
10944 }
10945 while (ivna.vna_other != data[cnt + j]
10946 && ivna.vna_next != 0);
10947
10948 if (ivna.vna_other == data[cnt + j])
10949 {
10950 ivna.vna_name = BYTE_GET (evna.vna_name);
10951
10952 if (ivna.vna_name >= string_sec->sh_size)
10953 name = invalid;
10954 else
10955 name = strtab + ivna.vna_name;
10956 break;
10957 }
10958
10959 offset += ivn.vn_next;
10960 }
10961 while (ivn.vn_next);
10962 }
10963
10964 if (data[cnt + j] != 0x8001
10965 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
10966 {
10967 Elf_Internal_Verdef ivd;
10968 Elf_External_Verdef evd;
10969 unsigned long offset;
10970
10971 offset = offset_from_vma
10972 (filedata, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
10973 sizeof evd);
10974
10975 do
10976 {
10977 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
10978 _("version def")) == NULL)
10979 {
10980 ivd.vd_next = 0;
10981 /* PR 17531: file: 046-1082287-0.004. */
10982 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
10983 break;
10984 }
10985 else
10986 {
10987 ivd.vd_next = BYTE_GET (evd.vd_next);
10988 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
10989 }
10990
10991 offset += ivd.vd_next;
10992 }
10993 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
10994 && ivd.vd_next != 0);
10995
10996 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
10997 {
10998 Elf_External_Verdaux evda;
10999 Elf_Internal_Verdaux ivda;
11000
11001 ivd.vd_aux = BYTE_GET (evd.vd_aux);
11002
11003 if (get_data (&evda, filedata,
11004 offset - ivd.vd_next + ivd.vd_aux,
11005 sizeof (evda), 1,
11006 _("version def aux")) == NULL)
11007 break;
11008
11009 ivda.vda_name = BYTE_GET (evda.vda_name);
11010
11011 if (ivda.vda_name >= string_sec->sh_size)
11012 name = invalid;
11013 else if (name != NULL && name != invalid)
11014 name = _("*both*");
11015 else
11016 name = strtab + ivda.vda_name;
11017 }
11018 }
11019 if (name != NULL)
11020 nn += printf ("(%s%-*s",
11021 name,
11022 12 - (int) strlen (name),
11023 ")");
11024
11025 if (nn < 18)
11026 printf ("%*c", 18 - nn, ' ');
11027 }
11028
11029 putchar ('\n');
11030 }
11031
11032 free (data);
11033 free (strtab);
11034 free (symbols);
11035 }
11036 break;
11037
11038 default:
11039 break;
11040 }
11041 }
11042
11043 if (! found)
11044 printf (_("\nNo version information found in this file.\n"));
11045
11046 return TRUE;
11047 }
11048
11049 static const char *
11050 get_symbol_binding (Filedata * filedata, unsigned int binding)
11051 {
11052 static char buff[32];
11053
11054 switch (binding)
11055 {
11056 case STB_LOCAL: return "LOCAL";
11057 case STB_GLOBAL: return "GLOBAL";
11058 case STB_WEAK: return "WEAK";
11059 default:
11060 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
11061 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
11062 binding);
11063 else if (binding >= STB_LOOS && binding <= STB_HIOS)
11064 {
11065 if (binding == STB_GNU_UNIQUE
11066 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
11067 /* GNU is still using the default value 0. */
11068 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
11069 return "UNIQUE";
11070 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
11071 }
11072 else
11073 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
11074 return buff;
11075 }
11076 }
11077
11078 static const char *
11079 get_symbol_type (Filedata * filedata, unsigned int type)
11080 {
11081 static char buff[32];
11082
11083 switch (type)
11084 {
11085 case STT_NOTYPE: return "NOTYPE";
11086 case STT_OBJECT: return "OBJECT";
11087 case STT_FUNC: return "FUNC";
11088 case STT_SECTION: return "SECTION";
11089 case STT_FILE: return "FILE";
11090 case STT_COMMON: return "COMMON";
11091 case STT_TLS: return "TLS";
11092 case STT_RELC: return "RELC";
11093 case STT_SRELC: return "SRELC";
11094 default:
11095 if (type >= STT_LOPROC && type <= STT_HIPROC)
11096 {
11097 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
11098 return "THUMB_FUNC";
11099
11100 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
11101 return "REGISTER";
11102
11103 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
11104 return "PARISC_MILLI";
11105
11106 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
11107 }
11108 else if (type >= STT_LOOS && type <= STT_HIOS)
11109 {
11110 if (filedata->file_header.e_machine == EM_PARISC)
11111 {
11112 if (type == STT_HP_OPAQUE)
11113 return "HP_OPAQUE";
11114 if (type == STT_HP_STUB)
11115 return "HP_STUB";
11116 }
11117
11118 if (type == STT_GNU_IFUNC
11119 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
11120 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD
11121 /* GNU is still using the default value 0. */
11122 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
11123 return "IFUNC";
11124
11125 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
11126 }
11127 else
11128 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
11129 return buff;
11130 }
11131 }
11132
11133 static const char *
11134 get_symbol_visibility (unsigned int visibility)
11135 {
11136 switch (visibility)
11137 {
11138 case STV_DEFAULT: return "DEFAULT";
11139 case STV_INTERNAL: return "INTERNAL";
11140 case STV_HIDDEN: return "HIDDEN";
11141 case STV_PROTECTED: return "PROTECTED";
11142 default:
11143 error (_("Unrecognized visibility value: %u"), visibility);
11144 return _("<unknown>");
11145 }
11146 }
11147
11148 static const char *
11149 get_solaris_symbol_visibility (unsigned int visibility)
11150 {
11151 switch (visibility)
11152 {
11153 case 4: return "EXPORTED";
11154 case 5: return "SINGLETON";
11155 case 6: return "ELIMINATE";
11156 default: return get_symbol_visibility (visibility);
11157 }
11158 }
11159
11160 static const char *
11161 get_aarch64_symbol_other (unsigned int other)
11162 {
11163 static char buf[32];
11164
11165 if (other & STO_AARCH64_VARIANT_PCS)
11166 {
11167 other &= ~STO_AARCH64_VARIANT_PCS;
11168 if (other == 0)
11169 return "VARIANT_PCS";
11170 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
11171 return buf;
11172 }
11173 return NULL;
11174 }
11175
11176 static const char *
11177 get_mips_symbol_other (unsigned int other)
11178 {
11179 switch (other)
11180 {
11181 case STO_OPTIONAL: return "OPTIONAL";
11182 case STO_MIPS_PLT: return "MIPS PLT";
11183 case STO_MIPS_PIC: return "MIPS PIC";
11184 case STO_MICROMIPS: return "MICROMIPS";
11185 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
11186 case STO_MIPS16: return "MIPS16";
11187 default: return NULL;
11188 }
11189 }
11190
11191 static const char *
11192 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
11193 {
11194 if (is_ia64_vms (filedata))
11195 {
11196 static char res[32];
11197
11198 res[0] = 0;
11199
11200 /* Function types is for images and .STB files only. */
11201 switch (filedata->file_header.e_type)
11202 {
11203 case ET_DYN:
11204 case ET_EXEC:
11205 switch (VMS_ST_FUNC_TYPE (other))
11206 {
11207 case VMS_SFT_CODE_ADDR:
11208 strcat (res, " CA");
11209 break;
11210 case VMS_SFT_SYMV_IDX:
11211 strcat (res, " VEC");
11212 break;
11213 case VMS_SFT_FD:
11214 strcat (res, " FD");
11215 break;
11216 case VMS_SFT_RESERVE:
11217 strcat (res, " RSV");
11218 break;
11219 default:
11220 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
11221 VMS_ST_FUNC_TYPE (other));
11222 strcat (res, " <unknown>");
11223 break;
11224 }
11225 break;
11226 default:
11227 break;
11228 }
11229 switch (VMS_ST_LINKAGE (other))
11230 {
11231 case VMS_STL_IGNORE:
11232 strcat (res, " IGN");
11233 break;
11234 case VMS_STL_RESERVE:
11235 strcat (res, " RSV");
11236 break;
11237 case VMS_STL_STD:
11238 strcat (res, " STD");
11239 break;
11240 case VMS_STL_LNK:
11241 strcat (res, " LNK");
11242 break;
11243 default:
11244 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
11245 VMS_ST_LINKAGE (other));
11246 strcat (res, " <unknown>");
11247 break;
11248 }
11249
11250 if (res[0] != 0)
11251 return res + 1;
11252 else
11253 return res;
11254 }
11255 return NULL;
11256 }
11257
11258 static const char *
11259 get_ppc64_symbol_other (unsigned int other)
11260 {
11261 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
11262 return NULL;
11263
11264 other >>= STO_PPC64_LOCAL_BIT;
11265 if (other <= 6)
11266 {
11267 static char buf[32];
11268 if (other >= 2)
11269 other = ppc64_decode_local_entry (other);
11270 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
11271 return buf;
11272 }
11273 return NULL;
11274 }
11275
11276 static const char *
11277 get_symbol_other (Filedata * filedata, unsigned int other)
11278 {
11279 const char * result = NULL;
11280 static char buff [32];
11281
11282 if (other == 0)
11283 return "";
11284
11285 switch (filedata->file_header.e_machine)
11286 {
11287 case EM_AARCH64:
11288 result = get_aarch64_symbol_other (other);
11289 break;
11290 case EM_MIPS:
11291 result = get_mips_symbol_other (other);
11292 break;
11293 case EM_IA_64:
11294 result = get_ia64_symbol_other (filedata, other);
11295 break;
11296 case EM_PPC64:
11297 result = get_ppc64_symbol_other (other);
11298 break;
11299 default:
11300 result = NULL;
11301 break;
11302 }
11303
11304 if (result)
11305 return result;
11306
11307 snprintf (buff, sizeof buff, _("<other>: %x"), other);
11308 return buff;
11309 }
11310
11311 static const char *
11312 get_symbol_index_type (Filedata * filedata, unsigned int type)
11313 {
11314 static char buff[32];
11315
11316 switch (type)
11317 {
11318 case SHN_UNDEF: return "UND";
11319 case SHN_ABS: return "ABS";
11320 case SHN_COMMON: return "COM";
11321 default:
11322 if (type == SHN_IA_64_ANSI_COMMON
11323 && filedata->file_header.e_machine == EM_IA_64
11324 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
11325 return "ANSI_COM";
11326 else if ((filedata->file_header.e_machine == EM_X86_64
11327 || filedata->file_header.e_machine == EM_L1OM
11328 || filedata->file_header.e_machine == EM_K1OM)
11329 && type == SHN_X86_64_LCOMMON)
11330 return "LARGE_COM";
11331 else if ((type == SHN_MIPS_SCOMMON
11332 && filedata->file_header.e_machine == EM_MIPS)
11333 || (type == SHN_TIC6X_SCOMMON
11334 && filedata->file_header.e_machine == EM_TI_C6000))
11335 return "SCOM";
11336 else if (type == SHN_MIPS_SUNDEFINED
11337 && filedata->file_header.e_machine == EM_MIPS)
11338 return "SUND";
11339 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
11340 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
11341 else if (type >= SHN_LOOS && type <= SHN_HIOS)
11342 sprintf (buff, "OS [0x%04x]", type & 0xffff);
11343 else if (type >= SHN_LORESERVE)
11344 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
11345 else if (type >= filedata->file_header.e_shnum)
11346 sprintf (buff, _("bad section index[%3d]"), type);
11347 else
11348 sprintf (buff, "%3d", type);
11349 break;
11350 }
11351
11352 return buff;
11353 }
11354
11355 static bfd_vma *
11356 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
11357 {
11358 unsigned char * e_data;
11359 bfd_vma * i_data;
11360
11361 /* If the size_t type is smaller than the bfd_size_type, eg because
11362 you are building a 32-bit tool on a 64-bit host, then make sure
11363 that when (number) is cast to (size_t) no information is lost. */
11364 if (sizeof (size_t) < sizeof (bfd_size_type)
11365 && (bfd_size_type) ((size_t) number) != number)
11366 {
11367 error (_("Size truncation prevents reading %s elements of size %u\n"),
11368 bfd_vmatoa ("u", number), ent_size);
11369 return NULL;
11370 }
11371
11372 /* Be kind to memory chekers (eg valgrind, address sanitizer) by not
11373 attempting to allocate memory when the read is bound to fail. */
11374 if (ent_size * number > filedata->file_size)
11375 {
11376 error (_("Invalid number of dynamic entries: %s\n"),
11377 bfd_vmatoa ("u", number));
11378 return NULL;
11379 }
11380
11381 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
11382 if (e_data == NULL)
11383 {
11384 error (_("Out of memory reading %s dynamic entries\n"),
11385 bfd_vmatoa ("u", number));
11386 return NULL;
11387 }
11388
11389 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
11390 {
11391 error (_("Unable to read in %s bytes of dynamic data\n"),
11392 bfd_vmatoa ("u", number * ent_size));
11393 free (e_data);
11394 return NULL;
11395 }
11396
11397 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
11398 if (i_data == NULL)
11399 {
11400 error (_("Out of memory allocating space for %s dynamic entries\n"),
11401 bfd_vmatoa ("u", number));
11402 free (e_data);
11403 return NULL;
11404 }
11405
11406 while (number--)
11407 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11408
11409 free (e_data);
11410
11411 return i_data;
11412 }
11413
11414 static void
11415 print_dynamic_symbol (Filedata * filedata, bfd_vma si, unsigned long hn)
11416 {
11417 Elf_Internal_Sym * psym;
11418 int n;
11419
11420 n = print_vma (si, DEC_5);
11421 if (n < 5)
11422 fputs (&" "[n], stdout);
11423 printf (" %3lu: ", hn);
11424
11425 if (dynamic_symbols == NULL || si >= num_dynamic_syms)
11426 {
11427 printf (_("<No info available for dynamic symbol number %lu>\n"),
11428 (unsigned long) si);
11429 return;
11430 }
11431
11432 psym = dynamic_symbols + si;
11433 print_vma (psym->st_value, LONG_HEX);
11434 putchar (' ');
11435 print_vma (psym->st_size, DEC_5);
11436
11437 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
11438 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
11439
11440 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
11441 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
11442 else
11443 {
11444 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
11445
11446 printf (" %-7s", get_symbol_visibility (vis));
11447 /* Check to see if any other bits in the st_other field are set.
11448 Note - displaying this information disrupts the layout of the
11449 table being generated, but for the moment this case is very
11450 rare. */
11451 if (psym->st_other ^ vis)
11452 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
11453 }
11454
11455 printf (" %3.3s ", get_symbol_index_type (filedata, psym->st_shndx));
11456 if (VALID_DYNAMIC_NAME (psym->st_name))
11457 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
11458 else
11459 printf (_(" <corrupt: %14ld>"), psym->st_name);
11460 putchar ('\n');
11461 }
11462
11463 static const char *
11464 get_symbol_version_string (Filedata * filedata,
11465 bfd_boolean is_dynsym,
11466 const char * strtab,
11467 unsigned long int strtab_size,
11468 unsigned int si,
11469 Elf_Internal_Sym * psym,
11470 enum versioned_symbol_info * sym_info,
11471 unsigned short * vna_other)
11472 {
11473 unsigned char data[2];
11474 unsigned short vers_data;
11475 unsigned long offset;
11476 unsigned short max_vd_ndx;
11477
11478 if (!is_dynsym
11479 || version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
11480 return NULL;
11481
11482 offset = offset_from_vma (filedata, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11483 sizeof data + si * sizeof (vers_data));
11484
11485 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
11486 sizeof (data), 1, _("version data")) == NULL)
11487 return NULL;
11488
11489 vers_data = byte_get (data, 2);
11490
11491 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
11492 return NULL;
11493
11494 max_vd_ndx = 0;
11495
11496 /* Usually we'd only see verdef for defined symbols, and verneed for
11497 undefined symbols. However, symbols defined by the linker in
11498 .dynbss for variables copied from a shared library in order to
11499 avoid text relocations are defined yet have verneed. We could
11500 use a heuristic to detect the special case, for example, check
11501 for verneed first on symbols defined in SHT_NOBITS sections, but
11502 it is simpler and more reliable to just look for both verdef and
11503 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
11504
11505 if (psym->st_shndx != SHN_UNDEF
11506 && vers_data != 0x8001
11507 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
11508 {
11509 Elf_Internal_Verdef ivd;
11510 Elf_Internal_Verdaux ivda;
11511 Elf_External_Verdaux evda;
11512 unsigned long off;
11513
11514 off = offset_from_vma (filedata,
11515 version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
11516 sizeof (Elf_External_Verdef));
11517
11518 do
11519 {
11520 Elf_External_Verdef evd;
11521
11522 if (get_data (&evd, filedata, off, sizeof (evd), 1,
11523 _("version def")) == NULL)
11524 {
11525 ivd.vd_ndx = 0;
11526 ivd.vd_aux = 0;
11527 ivd.vd_next = 0;
11528 ivd.vd_flags = 0;
11529 }
11530 else
11531 {
11532 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
11533 ivd.vd_aux = BYTE_GET (evd.vd_aux);
11534 ivd.vd_next = BYTE_GET (evd.vd_next);
11535 ivd.vd_flags = BYTE_GET (evd.vd_flags);
11536 }
11537
11538 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
11539 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
11540
11541 off += ivd.vd_next;
11542 }
11543 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
11544
11545 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
11546 {
11547 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
11548 return NULL;
11549
11550 off -= ivd.vd_next;
11551 off += ivd.vd_aux;
11552
11553 if (get_data (&evda, filedata, off, sizeof (evda), 1,
11554 _("version def aux")) != NULL)
11555 {
11556 ivda.vda_name = BYTE_GET (evda.vda_name);
11557
11558 if (psym->st_name != ivda.vda_name)
11559 {
11560 *sym_info = ((vers_data & VERSYM_HIDDEN) != 0
11561 ? symbol_hidden : symbol_public);
11562 return (ivda.vda_name < strtab_size
11563 ? strtab + ivda.vda_name : _("<corrupt>"));
11564 }
11565 }
11566 }
11567 }
11568
11569 if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11570 {
11571 Elf_External_Verneed evn;
11572 Elf_Internal_Verneed ivn;
11573 Elf_Internal_Vernaux ivna;
11574
11575 offset = offset_from_vma (filedata,
11576 version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11577 sizeof evn);
11578 do
11579 {
11580 unsigned long vna_off;
11581
11582 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
11583 _("version need")) == NULL)
11584 {
11585 ivna.vna_next = 0;
11586 ivna.vna_other = 0;
11587 ivna.vna_name = 0;
11588 break;
11589 }
11590
11591 ivn.vn_aux = BYTE_GET (evn.vn_aux);
11592 ivn.vn_next = BYTE_GET (evn.vn_next);
11593
11594 vna_off = offset + ivn.vn_aux;
11595
11596 do
11597 {
11598 Elf_External_Vernaux evna;
11599
11600 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
11601 _("version need aux (3)")) == NULL)
11602 {
11603 ivna.vna_next = 0;
11604 ivna.vna_other = 0;
11605 ivna.vna_name = 0;
11606 }
11607 else
11608 {
11609 ivna.vna_other = BYTE_GET (evna.vna_other);
11610 ivna.vna_next = BYTE_GET (evna.vna_next);
11611 ivna.vna_name = BYTE_GET (evna.vna_name);
11612 }
11613
11614 vna_off += ivna.vna_next;
11615 }
11616 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
11617
11618 if (ivna.vna_other == vers_data)
11619 break;
11620
11621 offset += ivn.vn_next;
11622 }
11623 while (ivn.vn_next != 0);
11624
11625 if (ivna.vna_other == vers_data)
11626 {
11627 *sym_info = symbol_undefined;
11628 *vna_other = ivna.vna_other;
11629 return (ivna.vna_name < strtab_size
11630 ? strtab + ivna.vna_name : _("<corrupt>"));
11631 }
11632 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
11633 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
11634 return _("<corrupt>");
11635 }
11636 return NULL;
11637 }
11638
11639 /* Dump the symbol table. */
11640 static bfd_boolean
11641 process_symbol_table (Filedata * filedata)
11642 {
11643 Elf_Internal_Shdr * section;
11644 bfd_size_type nbuckets = 0;
11645 bfd_size_type nchains = 0;
11646 bfd_vma * buckets = NULL;
11647 bfd_vma * chains = NULL;
11648 bfd_vma ngnubuckets = 0;
11649 bfd_vma * gnubuckets = NULL;
11650 bfd_vma * gnuchains = NULL;
11651 bfd_vma gnusymidx = 0;
11652 bfd_size_type ngnuchains = 0;
11653
11654 if (!do_syms && !do_dyn_syms && !do_histogram)
11655 return TRUE;
11656
11657 if (dynamic_info[DT_HASH]
11658 && (do_histogram
11659 || (do_using_dynamic
11660 && !do_dyn_syms
11661 && dynamic_strings != NULL)))
11662 {
11663 unsigned char nb[8];
11664 unsigned char nc[8];
11665 unsigned int hash_ent_size = 4;
11666
11667 if ((filedata->file_header.e_machine == EM_ALPHA
11668 || filedata->file_header.e_machine == EM_S390
11669 || filedata->file_header.e_machine == EM_S390_OLD)
11670 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11671 hash_ent_size = 8;
11672
11673 if (fseek (filedata->handle,
11674 (archive_file_offset
11675 + offset_from_vma (filedata, dynamic_info[DT_HASH],
11676 sizeof nb + sizeof nc)),
11677 SEEK_SET))
11678 {
11679 error (_("Unable to seek to start of dynamic information\n"));
11680 goto no_hash;
11681 }
11682
11683 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11684 {
11685 error (_("Failed to read in number of buckets\n"));
11686 goto no_hash;
11687 }
11688
11689 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11690 {
11691 error (_("Failed to read in number of chains\n"));
11692 goto no_hash;
11693 }
11694
11695 nbuckets = byte_get (nb, hash_ent_size);
11696 nchains = byte_get (nc, hash_ent_size);
11697
11698 buckets = get_dynamic_data (filedata, nbuckets, hash_ent_size);
11699 chains = get_dynamic_data (filedata, nchains, hash_ent_size);
11700
11701 no_hash:
11702 if (buckets == NULL || chains == NULL)
11703 {
11704 if (do_using_dynamic)
11705 return FALSE;
11706 free (buckets);
11707 free (chains);
11708 buckets = NULL;
11709 chains = NULL;
11710 nbuckets = 0;
11711 nchains = 0;
11712 }
11713 }
11714
11715 if (dynamic_info_DT_GNU_HASH
11716 && (do_histogram
11717 || (do_using_dynamic
11718 && !do_dyn_syms
11719 && dynamic_strings != NULL)))
11720 {
11721 unsigned char nb[16];
11722 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
11723 bfd_vma buckets_vma;
11724
11725 if (fseek (filedata->handle,
11726 (archive_file_offset
11727 + offset_from_vma (filedata, dynamic_info_DT_GNU_HASH,
11728 sizeof nb)),
11729 SEEK_SET))
11730 {
11731 error (_("Unable to seek to start of dynamic information\n"));
11732 goto no_gnu_hash;
11733 }
11734
11735 if (fread (nb, 16, 1, filedata->handle) != 1)
11736 {
11737 error (_("Failed to read in number of buckets\n"));
11738 goto no_gnu_hash;
11739 }
11740
11741 ngnubuckets = byte_get (nb, 4);
11742 gnusymidx = byte_get (nb + 4, 4);
11743 bitmaskwords = byte_get (nb + 8, 4);
11744 buckets_vma = dynamic_info_DT_GNU_HASH + 16;
11745 if (is_32bit_elf)
11746 buckets_vma += bitmaskwords * 4;
11747 else
11748 buckets_vma += bitmaskwords * 8;
11749
11750 if (fseek (filedata->handle,
11751 (archive_file_offset
11752 + offset_from_vma (filedata, buckets_vma, 4)),
11753 SEEK_SET))
11754 {
11755 error (_("Unable to seek to start of dynamic information\n"));
11756 goto no_gnu_hash;
11757 }
11758
11759 gnubuckets = get_dynamic_data (filedata, ngnubuckets, 4);
11760
11761 if (gnubuckets == NULL)
11762 goto no_gnu_hash;
11763
11764 for (i = 0; i < ngnubuckets; i++)
11765 if (gnubuckets[i] != 0)
11766 {
11767 if (gnubuckets[i] < gnusymidx)
11768 return FALSE;
11769
11770 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
11771 maxchain = gnubuckets[i];
11772 }
11773
11774 if (maxchain == 0xffffffff)
11775 goto no_gnu_hash;
11776
11777 maxchain -= gnusymidx;
11778
11779 if (fseek (filedata->handle,
11780 (archive_file_offset
11781 + offset_from_vma (filedata, buckets_vma
11782 + 4 * (ngnubuckets + maxchain), 4)),
11783 SEEK_SET))
11784 {
11785 error (_("Unable to seek to start of dynamic information\n"));
11786 goto no_gnu_hash;
11787 }
11788
11789 do
11790 {
11791 if (fread (nb, 4, 1, filedata->handle) != 1)
11792 {
11793 error (_("Failed to determine last chain length\n"));
11794 goto no_gnu_hash;
11795 }
11796
11797 if (maxchain + 1 == 0)
11798 goto no_gnu_hash;
11799
11800 ++maxchain;
11801 }
11802 while ((byte_get (nb, 4) & 1) == 0);
11803
11804 if (fseek (filedata->handle,
11805 (archive_file_offset
11806 + offset_from_vma (filedata, buckets_vma + 4 * ngnubuckets, 4)),
11807 SEEK_SET))
11808 {
11809 error (_("Unable to seek to start of dynamic information\n"));
11810 goto no_gnu_hash;
11811 }
11812
11813 gnuchains = get_dynamic_data (filedata, maxchain, 4);
11814 ngnuchains = maxchain;
11815
11816 no_gnu_hash:
11817 if (gnuchains == NULL)
11818 {
11819 free (gnubuckets);
11820 gnubuckets = NULL;
11821 ngnubuckets = 0;
11822 if (do_using_dynamic)
11823 return FALSE;
11824 }
11825 }
11826
11827 if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
11828 && do_syms
11829 && do_using_dynamic
11830 && dynamic_strings != NULL
11831 && dynamic_symbols != NULL)
11832 {
11833 unsigned long hn;
11834
11835 if (dynamic_info[DT_HASH])
11836 {
11837 bfd_vma si;
11838 char *visited;
11839
11840 printf (_("\nSymbol table for image:\n"));
11841 if (is_32bit_elf)
11842 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
11843 else
11844 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
11845
11846 visited = xcmalloc (nchains, 1);
11847 memset (visited, 0, nchains);
11848 for (hn = 0; hn < nbuckets; hn++)
11849 {
11850 for (si = buckets[hn]; si > 0; si = chains[si])
11851 {
11852 print_dynamic_symbol (filedata, si, hn);
11853 if (si >= nchains || visited[si])
11854 {
11855 error (_("histogram chain is corrupt\n"));
11856 break;
11857 }
11858 visited[si] = 1;
11859 }
11860 }
11861 free (visited);
11862 }
11863
11864 if (dynamic_info_DT_GNU_HASH)
11865 {
11866 printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
11867 if (is_32bit_elf)
11868 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
11869 else
11870 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
11871
11872 for (hn = 0; hn < ngnubuckets; ++hn)
11873 if (gnubuckets[hn] != 0)
11874 {
11875 bfd_vma si = gnubuckets[hn];
11876 bfd_vma off = si - gnusymidx;
11877
11878 do
11879 {
11880 print_dynamic_symbol (filedata, si, hn);
11881 si++;
11882 }
11883 while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
11884 }
11885 }
11886 }
11887 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
11888 && filedata->section_headers != NULL)
11889 {
11890 unsigned int i;
11891
11892 for (i = 0, section = filedata->section_headers;
11893 i < filedata->file_header.e_shnum;
11894 i++, section++)
11895 {
11896 unsigned int si;
11897 char * strtab = NULL;
11898 unsigned long int strtab_size = 0;
11899 Elf_Internal_Sym * symtab;
11900 Elf_Internal_Sym * psym;
11901 unsigned long num_syms;
11902
11903 if ((section->sh_type != SHT_SYMTAB
11904 && section->sh_type != SHT_DYNSYM)
11905 || (!do_syms
11906 && section->sh_type == SHT_SYMTAB))
11907 continue;
11908
11909 if (section->sh_entsize == 0)
11910 {
11911 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
11912 printable_section_name (filedata, section));
11913 continue;
11914 }
11915
11916 num_syms = section->sh_size / section->sh_entsize;
11917 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
11918 "\nSymbol table '%s' contains %lu entries:\n",
11919 num_syms),
11920 printable_section_name (filedata, section),
11921 num_syms);
11922
11923 if (is_32bit_elf)
11924 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
11925 else
11926 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
11927
11928 symtab = GET_ELF_SYMBOLS (filedata, section, & num_syms);
11929 if (symtab == NULL)
11930 continue;
11931
11932 if (section->sh_link == filedata->file_header.e_shstrndx)
11933 {
11934 strtab = filedata->string_table;
11935 strtab_size = filedata->string_table_length;
11936 }
11937 else if (section->sh_link < filedata->file_header.e_shnum)
11938 {
11939 Elf_Internal_Shdr * string_sec;
11940
11941 string_sec = filedata->section_headers + section->sh_link;
11942
11943 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
11944 1, string_sec->sh_size,
11945 _("string table"));
11946 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
11947 }
11948
11949 for (si = 0, psym = symtab; si < num_syms; si++, psym++)
11950 {
11951 const char *version_string;
11952 enum versioned_symbol_info sym_info;
11953 unsigned short vna_other;
11954
11955 printf ("%6d: ", si);
11956 print_vma (psym->st_value, LONG_HEX);
11957 putchar (' ');
11958 print_vma (psym->st_size, DEC_5);
11959 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
11960 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
11961 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
11962 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
11963 else
11964 {
11965 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
11966
11967 printf (" %-7s", get_symbol_visibility (vis));
11968 /* Check to see if any other bits in the st_other field are set.
11969 Note - displaying this information disrupts the layout of the
11970 table being generated, but for the moment this case is very rare. */
11971 if (psym->st_other ^ vis)
11972 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
11973 }
11974 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
11975 print_symbol (25, psym->st_name < strtab_size
11976 ? strtab + psym->st_name : _("<corrupt>"));
11977
11978 version_string
11979 = get_symbol_version_string (filedata,
11980 section->sh_type == SHT_DYNSYM,
11981 strtab, strtab_size, si,
11982 psym, &sym_info, &vna_other);
11983 if (version_string)
11984 {
11985 if (sym_info == symbol_undefined)
11986 printf ("@%s (%d)", version_string, vna_other);
11987 else
11988 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
11989 version_string);
11990 }
11991
11992 putchar ('\n');
11993
11994 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
11995 && si >= section->sh_info
11996 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
11997 && filedata->file_header.e_machine != EM_MIPS
11998 /* Solaris binaries have been found to violate this requirement as
11999 well. Not sure if this is a bug or an ABI requirement. */
12000 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12001 warn (_("local symbol %u found at index >= %s's sh_info value of %u\n"),
12002 si, printable_section_name (filedata, section), section->sh_info);
12003 }
12004
12005 free (symtab);
12006 if (strtab != filedata->string_table)
12007 free (strtab);
12008 }
12009 }
12010 else if (do_syms)
12011 printf
12012 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
12013
12014 if (do_histogram && buckets != NULL)
12015 {
12016 unsigned long * lengths;
12017 unsigned long * counts;
12018 unsigned long hn;
12019 bfd_vma si;
12020 unsigned long maxlength = 0;
12021 unsigned long nzero_counts = 0;
12022 unsigned long nsyms = 0;
12023 char *visited;
12024
12025 printf (ngettext ("\nHistogram for bucket list length "
12026 "(total of %lu bucket):\n",
12027 "\nHistogram for bucket list length "
12028 "(total of %lu buckets):\n",
12029 (unsigned long) nbuckets),
12030 (unsigned long) nbuckets);
12031
12032 lengths = (unsigned long *) calloc (nbuckets, sizeof (*lengths));
12033 if (lengths == NULL)
12034 {
12035 error (_("Out of memory allocating space for histogram buckets\n"));
12036 return FALSE;
12037 }
12038 visited = xcmalloc (nchains, 1);
12039 memset (visited, 0, nchains);
12040
12041 printf (_(" Length Number %% of total Coverage\n"));
12042 for (hn = 0; hn < nbuckets; ++hn)
12043 {
12044 for (si = buckets[hn]; si > 0; si = chains[si])
12045 {
12046 ++nsyms;
12047 if (maxlength < ++lengths[hn])
12048 ++maxlength;
12049 if (si >= nchains || visited[si])
12050 {
12051 error (_("histogram chain is corrupt\n"));
12052 break;
12053 }
12054 visited[si] = 1;
12055 }
12056 }
12057 free (visited);
12058
12059 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
12060 if (counts == NULL)
12061 {
12062 free (lengths);
12063 error (_("Out of memory allocating space for histogram counts\n"));
12064 return FALSE;
12065 }
12066
12067 for (hn = 0; hn < nbuckets; ++hn)
12068 ++counts[lengths[hn]];
12069
12070 if (nbuckets > 0)
12071 {
12072 unsigned long i;
12073 printf (" 0 %-10lu (%5.1f%%)\n",
12074 counts[0], (counts[0] * 100.0) / nbuckets);
12075 for (i = 1; i <= maxlength; ++i)
12076 {
12077 nzero_counts += counts[i] * i;
12078 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
12079 i, counts[i], (counts[i] * 100.0) / nbuckets,
12080 (nzero_counts * 100.0) / nsyms);
12081 }
12082 }
12083
12084 free (counts);
12085 free (lengths);
12086 }
12087
12088 if (buckets != NULL)
12089 {
12090 free (buckets);
12091 free (chains);
12092 }
12093
12094 if (do_histogram && gnubuckets != NULL)
12095 {
12096 unsigned long * lengths;
12097 unsigned long * counts;
12098 unsigned long hn;
12099 unsigned long maxlength = 0;
12100 unsigned long nzero_counts = 0;
12101 unsigned long nsyms = 0;
12102
12103 printf (ngettext ("\nHistogram for `.gnu.hash' bucket list length "
12104 "(total of %lu bucket):\n",
12105 "\nHistogram for `.gnu.hash' bucket list length "
12106 "(total of %lu buckets):\n",
12107 (unsigned long) ngnubuckets),
12108 (unsigned long) ngnubuckets);
12109
12110 lengths = (unsigned long *) calloc (ngnubuckets, sizeof (*lengths));
12111 if (lengths == NULL)
12112 {
12113 error (_("Out of memory allocating space for gnu histogram buckets\n"));
12114 return FALSE;
12115 }
12116
12117 printf (_(" Length Number %% of total Coverage\n"));
12118
12119 for (hn = 0; hn < ngnubuckets; ++hn)
12120 if (gnubuckets[hn] != 0)
12121 {
12122 bfd_vma off, length = 1;
12123
12124 for (off = gnubuckets[hn] - gnusymidx;
12125 /* PR 17531 file: 010-77222-0.004. */
12126 off < ngnuchains && (gnuchains[off] & 1) == 0;
12127 ++off)
12128 ++length;
12129 lengths[hn] = length;
12130 if (length > maxlength)
12131 maxlength = length;
12132 nsyms += length;
12133 }
12134
12135 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
12136 if (counts == NULL)
12137 {
12138 free (lengths);
12139 error (_("Out of memory allocating space for gnu histogram counts\n"));
12140 return FALSE;
12141 }
12142
12143 for (hn = 0; hn < ngnubuckets; ++hn)
12144 ++counts[lengths[hn]];
12145
12146 if (ngnubuckets > 0)
12147 {
12148 unsigned long j;
12149 printf (" 0 %-10lu (%5.1f%%)\n",
12150 counts[0], (counts[0] * 100.0) / ngnubuckets);
12151 for (j = 1; j <= maxlength; ++j)
12152 {
12153 nzero_counts += counts[j] * j;
12154 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
12155 j, counts[j], (counts[j] * 100.0) / ngnubuckets,
12156 (nzero_counts * 100.0) / nsyms);
12157 }
12158 }
12159
12160 free (counts);
12161 free (lengths);
12162 free (gnubuckets);
12163 free (gnuchains);
12164 }
12165
12166 return TRUE;
12167 }
12168
12169 static bfd_boolean
12170 process_syminfo (Filedata * filedata ATTRIBUTE_UNUSED)
12171 {
12172 unsigned int i;
12173
12174 if (dynamic_syminfo == NULL
12175 || !do_dynamic)
12176 /* No syminfo, this is ok. */
12177 return TRUE;
12178
12179 /* There better should be a dynamic symbol section. */
12180 if (dynamic_symbols == NULL || dynamic_strings == NULL)
12181 return FALSE;
12182
12183 if (dynamic_addr)
12184 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
12185 "contains %d entry:\n",
12186 "\nDynamic info segment at offset 0x%lx "
12187 "contains %d entries:\n",
12188 dynamic_syminfo_nent),
12189 dynamic_syminfo_offset, dynamic_syminfo_nent);
12190
12191 printf (_(" Num: Name BoundTo Flags\n"));
12192 for (i = 0; i < dynamic_syminfo_nent; ++i)
12193 {
12194 unsigned short int flags = dynamic_syminfo[i].si_flags;
12195
12196 printf ("%4d: ", i);
12197 if (i >= num_dynamic_syms)
12198 printf (_("<corrupt index>"));
12199 else if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
12200 print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
12201 else
12202 printf (_("<corrupt: %19ld>"), dynamic_symbols[i].st_name);
12203 putchar (' ');
12204
12205 switch (dynamic_syminfo[i].si_boundto)
12206 {
12207 case SYMINFO_BT_SELF:
12208 fputs ("SELF ", stdout);
12209 break;
12210 case SYMINFO_BT_PARENT:
12211 fputs ("PARENT ", stdout);
12212 break;
12213 default:
12214 if (dynamic_syminfo[i].si_boundto > 0
12215 && dynamic_syminfo[i].si_boundto < dynamic_nent
12216 && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
12217 {
12218 print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
12219 putchar (' ' );
12220 }
12221 else
12222 printf ("%-10d ", dynamic_syminfo[i].si_boundto);
12223 break;
12224 }
12225
12226 if (flags & SYMINFO_FLG_DIRECT)
12227 printf (" DIRECT");
12228 if (flags & SYMINFO_FLG_PASSTHRU)
12229 printf (" PASSTHRU");
12230 if (flags & SYMINFO_FLG_COPY)
12231 printf (" COPY");
12232 if (flags & SYMINFO_FLG_LAZYLOAD)
12233 printf (" LAZYLOAD");
12234
12235 puts ("");
12236 }
12237
12238 return TRUE;
12239 }
12240
12241 #define IN_RANGE(START,END,ADDR,OFF) \
12242 (((ADDR) >= (START)) && ((ADDR) + (OFF) < (END)))
12243
12244 /* Check to see if the given reloc needs to be handled in a target specific
12245 manner. If so then process the reloc and return TRUE otherwise return
12246 FALSE.
12247
12248 If called with reloc == NULL, then this is a signal that reloc processing
12249 for the current section has finished, and any saved state should be
12250 discarded. */
12251
12252 static bfd_boolean
12253 target_specific_reloc_handling (Filedata * filedata,
12254 Elf_Internal_Rela * reloc,
12255 unsigned char * start,
12256 unsigned char * end,
12257 Elf_Internal_Sym * symtab,
12258 unsigned long num_syms)
12259 {
12260 unsigned int reloc_type = 0;
12261 unsigned long sym_index = 0;
12262
12263 if (reloc)
12264 {
12265 reloc_type = get_reloc_type (filedata, reloc->r_info);
12266 sym_index = get_reloc_symindex (reloc->r_info);
12267 }
12268
12269 switch (filedata->file_header.e_machine)
12270 {
12271 case EM_MSP430:
12272 case EM_MSP430_OLD:
12273 {
12274 static Elf_Internal_Sym * saved_sym = NULL;
12275
12276 if (reloc == NULL)
12277 {
12278 saved_sym = NULL;
12279 return TRUE;
12280 }
12281
12282 switch (reloc_type)
12283 {
12284 case 10: /* R_MSP430_SYM_DIFF */
12285 if (uses_msp430x_relocs (filedata))
12286 break;
12287 /* Fall through. */
12288 case 21: /* R_MSP430X_SYM_DIFF */
12289 /* PR 21139. */
12290 if (sym_index >= num_syms)
12291 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
12292 sym_index);
12293 else
12294 saved_sym = symtab + sym_index;
12295 return TRUE;
12296
12297 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
12298 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
12299 goto handle_sym_diff;
12300
12301 case 5: /* R_MSP430_16_BYTE */
12302 case 9: /* R_MSP430_8 */
12303 if (uses_msp430x_relocs (filedata))
12304 break;
12305 goto handle_sym_diff;
12306
12307 case 2: /* R_MSP430_ABS16 */
12308 case 15: /* R_MSP430X_ABS16 */
12309 if (! uses_msp430x_relocs (filedata))
12310 break;
12311 goto handle_sym_diff;
12312
12313 handle_sym_diff:
12314 if (saved_sym != NULL)
12315 {
12316 int reloc_size = reloc_type == 1 ? 4 : 2;
12317 bfd_vma value;
12318
12319 if (sym_index >= num_syms)
12320 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
12321 sym_index);
12322 else
12323 {
12324 value = reloc->r_addend + (symtab[sym_index].st_value
12325 - saved_sym->st_value);
12326
12327 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
12328 byte_put (start + reloc->r_offset, value, reloc_size);
12329 else
12330 /* PR 21137 */
12331 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
12332 (long) reloc->r_offset);
12333 }
12334
12335 saved_sym = NULL;
12336 return TRUE;
12337 }
12338 break;
12339
12340 default:
12341 if (saved_sym != NULL)
12342 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
12343 break;
12344 }
12345 break;
12346 }
12347
12348 case EM_MN10300:
12349 case EM_CYGNUS_MN10300:
12350 {
12351 static Elf_Internal_Sym * saved_sym = NULL;
12352
12353 if (reloc == NULL)
12354 {
12355 saved_sym = NULL;
12356 return TRUE;
12357 }
12358
12359 switch (reloc_type)
12360 {
12361 case 34: /* R_MN10300_ALIGN */
12362 return TRUE;
12363 case 33: /* R_MN10300_SYM_DIFF */
12364 if (sym_index >= num_syms)
12365 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
12366 sym_index);
12367 else
12368 saved_sym = symtab + sym_index;
12369 return TRUE;
12370
12371 case 1: /* R_MN10300_32 */
12372 case 2: /* R_MN10300_16 */
12373 if (saved_sym != NULL)
12374 {
12375 int reloc_size = reloc_type == 1 ? 4 : 2;
12376 bfd_vma value;
12377
12378 if (sym_index >= num_syms)
12379 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
12380 sym_index);
12381 else
12382 {
12383 value = reloc->r_addend + (symtab[sym_index].st_value
12384 - saved_sym->st_value);
12385
12386 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
12387 byte_put (start + reloc->r_offset, value, reloc_size);
12388 else
12389 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
12390 (long) reloc->r_offset);
12391 }
12392
12393 saved_sym = NULL;
12394 return TRUE;
12395 }
12396 break;
12397 default:
12398 if (saved_sym != NULL)
12399 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
12400 break;
12401 }
12402 break;
12403 }
12404
12405 case EM_RL78:
12406 {
12407 static bfd_vma saved_sym1 = 0;
12408 static bfd_vma saved_sym2 = 0;
12409 static bfd_vma value;
12410
12411 if (reloc == NULL)
12412 {
12413 saved_sym1 = saved_sym2 = 0;
12414 return TRUE;
12415 }
12416
12417 switch (reloc_type)
12418 {
12419 case 0x80: /* R_RL78_SYM. */
12420 saved_sym1 = saved_sym2;
12421 if (sym_index >= num_syms)
12422 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
12423 sym_index);
12424 else
12425 {
12426 saved_sym2 = symtab[sym_index].st_value;
12427 saved_sym2 += reloc->r_addend;
12428 }
12429 return TRUE;
12430
12431 case 0x83: /* R_RL78_OPsub. */
12432 value = saved_sym1 - saved_sym2;
12433 saved_sym2 = saved_sym1 = 0;
12434 return TRUE;
12435 break;
12436
12437 case 0x41: /* R_RL78_ABS32. */
12438 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
12439 byte_put (start + reloc->r_offset, value, 4);
12440 else
12441 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
12442 (long) reloc->r_offset);
12443 value = 0;
12444 return TRUE;
12445
12446 case 0x43: /* R_RL78_ABS16. */
12447 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
12448 byte_put (start + reloc->r_offset, value, 2);
12449 else
12450 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
12451 (long) reloc->r_offset);
12452 value = 0;
12453 return TRUE;
12454
12455 default:
12456 break;
12457 }
12458 break;
12459 }
12460 }
12461
12462 return FALSE;
12463 }
12464
12465 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
12466 DWARF debug sections. This is a target specific test. Note - we do not
12467 go through the whole including-target-headers-multiple-times route, (as
12468 we have already done with <elf/h8.h>) because this would become very
12469 messy and even then this function would have to contain target specific
12470 information (the names of the relocs instead of their numeric values).
12471 FIXME: This is not the correct way to solve this problem. The proper way
12472 is to have target specific reloc sizing and typing functions created by
12473 the reloc-macros.h header, in the same way that it already creates the
12474 reloc naming functions. */
12475
12476 static bfd_boolean
12477 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12478 {
12479 /* Please keep this table alpha-sorted for ease of visual lookup. */
12480 switch (filedata->file_header.e_machine)
12481 {
12482 case EM_386:
12483 case EM_IAMCU:
12484 return reloc_type == 1; /* R_386_32. */
12485 case EM_68K:
12486 return reloc_type == 1; /* R_68K_32. */
12487 case EM_860:
12488 return reloc_type == 1; /* R_860_32. */
12489 case EM_960:
12490 return reloc_type == 2; /* R_960_32. */
12491 case EM_AARCH64:
12492 return (reloc_type == 258
12493 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
12494 case EM_BPF:
12495 return reloc_type == 11; /* R_BPF_DATA_32 */
12496 case EM_ADAPTEVA_EPIPHANY:
12497 return reloc_type == 3;
12498 case EM_ALPHA:
12499 return reloc_type == 1; /* R_ALPHA_REFLONG. */
12500 case EM_ARC:
12501 return reloc_type == 1; /* R_ARC_32. */
12502 case EM_ARC_COMPACT:
12503 case EM_ARC_COMPACT2:
12504 return reloc_type == 4; /* R_ARC_32. */
12505 case EM_ARM:
12506 return reloc_type == 2; /* R_ARM_ABS32 */
12507 case EM_AVR_OLD:
12508 case EM_AVR:
12509 return reloc_type == 1;
12510 case EM_BLACKFIN:
12511 return reloc_type == 0x12; /* R_byte4_data. */
12512 case EM_CRIS:
12513 return reloc_type == 3; /* R_CRIS_32. */
12514 case EM_CR16:
12515 return reloc_type == 3; /* R_CR16_NUM32. */
12516 case EM_CRX:
12517 return reloc_type == 15; /* R_CRX_NUM32. */
12518 case EM_CSKY:
12519 return reloc_type == 1; /* R_CKCORE_ADDR32. */
12520 case EM_CYGNUS_FRV:
12521 return reloc_type == 1;
12522 case EM_CYGNUS_D10V:
12523 case EM_D10V:
12524 return reloc_type == 6; /* R_D10V_32. */
12525 case EM_CYGNUS_D30V:
12526 case EM_D30V:
12527 return reloc_type == 12; /* R_D30V_32_NORMAL. */
12528 case EM_DLX:
12529 return reloc_type == 3; /* R_DLX_RELOC_32. */
12530 case EM_CYGNUS_FR30:
12531 case EM_FR30:
12532 return reloc_type == 3; /* R_FR30_32. */
12533 case EM_FT32:
12534 return reloc_type == 1; /* R_FT32_32. */
12535 case EM_H8S:
12536 case EM_H8_300:
12537 case EM_H8_300H:
12538 return reloc_type == 1; /* R_H8_DIR32. */
12539 case EM_IA_64:
12540 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
12541 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
12542 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
12543 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
12544 case EM_IP2K_OLD:
12545 case EM_IP2K:
12546 return reloc_type == 2; /* R_IP2K_32. */
12547 case EM_IQ2000:
12548 return reloc_type == 2; /* R_IQ2000_32. */
12549 case EM_LATTICEMICO32:
12550 return reloc_type == 3; /* R_LM32_32. */
12551 case EM_M32C_OLD:
12552 case EM_M32C:
12553 return reloc_type == 3; /* R_M32C_32. */
12554 case EM_M32R:
12555 return reloc_type == 34; /* R_M32R_32_RELA. */
12556 case EM_68HC11:
12557 case EM_68HC12:
12558 return reloc_type == 6; /* R_M68HC11_32. */
12559 case EM_S12Z:
12560 return reloc_type == 7 || /* R_S12Z_EXT32 */
12561 reloc_type == 6; /* R_S12Z_CW32. */
12562 case EM_MCORE:
12563 return reloc_type == 1; /* R_MCORE_ADDR32. */
12564 case EM_CYGNUS_MEP:
12565 return reloc_type == 4; /* R_MEP_32. */
12566 case EM_METAG:
12567 return reloc_type == 2; /* R_METAG_ADDR32. */
12568 case EM_MICROBLAZE:
12569 return reloc_type == 1; /* R_MICROBLAZE_32. */
12570 case EM_MIPS:
12571 return reloc_type == 2; /* R_MIPS_32. */
12572 case EM_MMIX:
12573 return reloc_type == 4; /* R_MMIX_32. */
12574 case EM_CYGNUS_MN10200:
12575 case EM_MN10200:
12576 return reloc_type == 1; /* R_MN10200_32. */
12577 case EM_CYGNUS_MN10300:
12578 case EM_MN10300:
12579 return reloc_type == 1; /* R_MN10300_32. */
12580 case EM_MOXIE:
12581 return reloc_type == 1; /* R_MOXIE_32. */
12582 case EM_MSP430_OLD:
12583 case EM_MSP430:
12584 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
12585 case EM_MT:
12586 return reloc_type == 2; /* R_MT_32. */
12587 case EM_NDS32:
12588 return reloc_type == 20; /* R_NDS32_RELA. */
12589 case EM_ALTERA_NIOS2:
12590 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
12591 case EM_NIOS32:
12592 return reloc_type == 1; /* R_NIOS_32. */
12593 case EM_OR1K:
12594 return reloc_type == 1; /* R_OR1K_32. */
12595 case EM_PARISC:
12596 return (reloc_type == 1 /* R_PARISC_DIR32. */
12597 || reloc_type == 2 /* R_PARISC_DIR21L. */
12598 || reloc_type == 41); /* R_PARISC_SECREL32. */
12599 case EM_PJ:
12600 case EM_PJ_OLD:
12601 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
12602 case EM_PPC64:
12603 return reloc_type == 1; /* R_PPC64_ADDR32. */
12604 case EM_PPC:
12605 return reloc_type == 1; /* R_PPC_ADDR32. */
12606 case EM_TI_PRU:
12607 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
12608 case EM_RISCV:
12609 return reloc_type == 1; /* R_RISCV_32. */
12610 case EM_RL78:
12611 return reloc_type == 1; /* R_RL78_DIR32. */
12612 case EM_RX:
12613 return reloc_type == 1; /* R_RX_DIR32. */
12614 case EM_S370:
12615 return reloc_type == 1; /* R_I370_ADDR31. */
12616 case EM_S390_OLD:
12617 case EM_S390:
12618 return reloc_type == 4; /* R_S390_32. */
12619 case EM_SCORE:
12620 return reloc_type == 8; /* R_SCORE_ABS32. */
12621 case EM_SH:
12622 return reloc_type == 1; /* R_SH_DIR32. */
12623 case EM_SPARC32PLUS:
12624 case EM_SPARCV9:
12625 case EM_SPARC:
12626 return reloc_type == 3 /* R_SPARC_32. */
12627 || reloc_type == 23; /* R_SPARC_UA32. */
12628 case EM_SPU:
12629 return reloc_type == 6; /* R_SPU_ADDR32 */
12630 case EM_TI_C6000:
12631 return reloc_type == 1; /* R_C6000_ABS32. */
12632 case EM_TILEGX:
12633 return reloc_type == 2; /* R_TILEGX_32. */
12634 case EM_TILEPRO:
12635 return reloc_type == 1; /* R_TILEPRO_32. */
12636 case EM_CYGNUS_V850:
12637 case EM_V850:
12638 return reloc_type == 6; /* R_V850_ABS32. */
12639 case EM_V800:
12640 return reloc_type == 0x33; /* R_V810_WORD. */
12641 case EM_VAX:
12642 return reloc_type == 1; /* R_VAX_32. */
12643 case EM_VISIUM:
12644 return reloc_type == 3; /* R_VISIUM_32. */
12645 case EM_WEBASSEMBLY:
12646 return reloc_type == 1; /* R_WASM32_32. */
12647 case EM_X86_64:
12648 case EM_L1OM:
12649 case EM_K1OM:
12650 return reloc_type == 10; /* R_X86_64_32. */
12651 case EM_XC16X:
12652 case EM_C166:
12653 return reloc_type == 3; /* R_XC16C_ABS_32. */
12654 case EM_XGATE:
12655 return reloc_type == 4; /* R_XGATE_32. */
12656 case EM_XSTORMY16:
12657 return reloc_type == 1; /* R_XSTROMY16_32. */
12658 case EM_XTENSA_OLD:
12659 case EM_XTENSA:
12660 return reloc_type == 1; /* R_XTENSA_32. */
12661 default:
12662 {
12663 static unsigned int prev_warn = 0;
12664
12665 /* Avoid repeating the same warning multiple times. */
12666 if (prev_warn != filedata->file_header.e_machine)
12667 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
12668 filedata->file_header.e_machine);
12669 prev_warn = filedata->file_header.e_machine;
12670 return FALSE;
12671 }
12672 }
12673 }
12674
12675 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12676 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
12677
12678 static bfd_boolean
12679 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
12680 {
12681 switch (filedata->file_header.e_machine)
12682 /* Please keep this table alpha-sorted for ease of visual lookup. */
12683 {
12684 case EM_386:
12685 case EM_IAMCU:
12686 return reloc_type == 2; /* R_386_PC32. */
12687 case EM_68K:
12688 return reloc_type == 4; /* R_68K_PC32. */
12689 case EM_AARCH64:
12690 return reloc_type == 261; /* R_AARCH64_PREL32 */
12691 case EM_ADAPTEVA_EPIPHANY:
12692 return reloc_type == 6;
12693 case EM_ALPHA:
12694 return reloc_type == 10; /* R_ALPHA_SREL32. */
12695 case EM_ARC_COMPACT:
12696 case EM_ARC_COMPACT2:
12697 return reloc_type == 49; /* R_ARC_32_PCREL. */
12698 case EM_ARM:
12699 return reloc_type == 3; /* R_ARM_REL32 */
12700 case EM_AVR_OLD:
12701 case EM_AVR:
12702 return reloc_type == 36; /* R_AVR_32_PCREL. */
12703 case EM_MICROBLAZE:
12704 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
12705 case EM_OR1K:
12706 return reloc_type == 9; /* R_OR1K_32_PCREL. */
12707 case EM_PARISC:
12708 return reloc_type == 9; /* R_PARISC_PCREL32. */
12709 case EM_PPC:
12710 return reloc_type == 26; /* R_PPC_REL32. */
12711 case EM_PPC64:
12712 return reloc_type == 26; /* R_PPC64_REL32. */
12713 case EM_RISCV:
12714 return reloc_type == 57; /* R_RISCV_32_PCREL. */
12715 case EM_S390_OLD:
12716 case EM_S390:
12717 return reloc_type == 5; /* R_390_PC32. */
12718 case EM_SH:
12719 return reloc_type == 2; /* R_SH_REL32. */
12720 case EM_SPARC32PLUS:
12721 case EM_SPARCV9:
12722 case EM_SPARC:
12723 return reloc_type == 6; /* R_SPARC_DISP32. */
12724 case EM_SPU:
12725 return reloc_type == 13; /* R_SPU_REL32. */
12726 case EM_TILEGX:
12727 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
12728 case EM_TILEPRO:
12729 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
12730 case EM_VISIUM:
12731 return reloc_type == 6; /* R_VISIUM_32_PCREL */
12732 case EM_X86_64:
12733 case EM_L1OM:
12734 case EM_K1OM:
12735 return reloc_type == 2; /* R_X86_64_PC32. */
12736 case EM_XTENSA_OLD:
12737 case EM_XTENSA:
12738 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
12739 default:
12740 /* Do not abort or issue an error message here. Not all targets use
12741 pc-relative 32-bit relocs in their DWARF debug information and we
12742 have already tested for target coverage in is_32bit_abs_reloc. A
12743 more helpful warning message will be generated by apply_relocations
12744 anyway, so just return. */
12745 return FALSE;
12746 }
12747 }
12748
12749 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12750 a 64-bit absolute RELA relocation used in DWARF debug sections. */
12751
12752 static bfd_boolean
12753 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12754 {
12755 switch (filedata->file_header.e_machine)
12756 {
12757 case EM_AARCH64:
12758 return reloc_type == 257; /* R_AARCH64_ABS64. */
12759 case EM_ALPHA:
12760 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
12761 case EM_IA_64:
12762 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
12763 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
12764 case EM_PARISC:
12765 return reloc_type == 80; /* R_PARISC_DIR64. */
12766 case EM_PPC64:
12767 return reloc_type == 38; /* R_PPC64_ADDR64. */
12768 case EM_RISCV:
12769 return reloc_type == 2; /* R_RISCV_64. */
12770 case EM_SPARC32PLUS:
12771 case EM_SPARCV9:
12772 case EM_SPARC:
12773 return reloc_type == 32 /* R_SPARC_64. */
12774 || reloc_type == 54; /* R_SPARC_UA64. */
12775 case EM_X86_64:
12776 case EM_L1OM:
12777 case EM_K1OM:
12778 return reloc_type == 1; /* R_X86_64_64. */
12779 case EM_S390_OLD:
12780 case EM_S390:
12781 return reloc_type == 22; /* R_S390_64. */
12782 case EM_TILEGX:
12783 return reloc_type == 1; /* R_TILEGX_64. */
12784 case EM_MIPS:
12785 return reloc_type == 18; /* R_MIPS_64. */
12786 default:
12787 return FALSE;
12788 }
12789 }
12790
12791 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
12792 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
12793
12794 static bfd_boolean
12795 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
12796 {
12797 switch (filedata->file_header.e_machine)
12798 {
12799 case EM_AARCH64:
12800 return reloc_type == 260; /* R_AARCH64_PREL64. */
12801 case EM_ALPHA:
12802 return reloc_type == 11; /* R_ALPHA_SREL64. */
12803 case EM_IA_64:
12804 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
12805 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
12806 case EM_PARISC:
12807 return reloc_type == 72; /* R_PARISC_PCREL64. */
12808 case EM_PPC64:
12809 return reloc_type == 44; /* R_PPC64_REL64. */
12810 case EM_SPARC32PLUS:
12811 case EM_SPARCV9:
12812 case EM_SPARC:
12813 return reloc_type == 46; /* R_SPARC_DISP64. */
12814 case EM_X86_64:
12815 case EM_L1OM:
12816 case EM_K1OM:
12817 return reloc_type == 24; /* R_X86_64_PC64. */
12818 case EM_S390_OLD:
12819 case EM_S390:
12820 return reloc_type == 23; /* R_S390_PC64. */
12821 case EM_TILEGX:
12822 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
12823 default:
12824 return FALSE;
12825 }
12826 }
12827
12828 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12829 a 24-bit absolute RELA relocation used in DWARF debug sections. */
12830
12831 static bfd_boolean
12832 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12833 {
12834 switch (filedata->file_header.e_machine)
12835 {
12836 case EM_CYGNUS_MN10200:
12837 case EM_MN10200:
12838 return reloc_type == 4; /* R_MN10200_24. */
12839 case EM_FT32:
12840 return reloc_type == 5; /* R_FT32_20. */
12841 default:
12842 return FALSE;
12843 }
12844 }
12845
12846 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12847 a 16-bit absolute RELA relocation used in DWARF debug sections. */
12848
12849 static bfd_boolean
12850 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12851 {
12852 /* Please keep this table alpha-sorted for ease of visual lookup. */
12853 switch (filedata->file_header.e_machine)
12854 {
12855 case EM_ARC:
12856 case EM_ARC_COMPACT:
12857 case EM_ARC_COMPACT2:
12858 return reloc_type == 2; /* R_ARC_16. */
12859 case EM_ADAPTEVA_EPIPHANY:
12860 return reloc_type == 5;
12861 case EM_AVR_OLD:
12862 case EM_AVR:
12863 return reloc_type == 4; /* R_AVR_16. */
12864 case EM_CYGNUS_D10V:
12865 case EM_D10V:
12866 return reloc_type == 3; /* R_D10V_16. */
12867 case EM_FT32:
12868 return reloc_type == 2; /* R_FT32_16. */
12869 case EM_H8S:
12870 case EM_H8_300:
12871 case EM_H8_300H:
12872 return reloc_type == R_H8_DIR16;
12873 case EM_IP2K_OLD:
12874 case EM_IP2K:
12875 return reloc_type == 1; /* R_IP2K_16. */
12876 case EM_M32C_OLD:
12877 case EM_M32C:
12878 return reloc_type == 1; /* R_M32C_16 */
12879 case EM_CYGNUS_MN10200:
12880 case EM_MN10200:
12881 return reloc_type == 2; /* R_MN10200_16. */
12882 case EM_CYGNUS_MN10300:
12883 case EM_MN10300:
12884 return reloc_type == 2; /* R_MN10300_16. */
12885 case EM_MSP430:
12886 if (uses_msp430x_relocs (filedata))
12887 return reloc_type == 2; /* R_MSP430_ABS16. */
12888 /* Fall through. */
12889 case EM_MSP430_OLD:
12890 return reloc_type == 5; /* R_MSP430_16_BYTE. */
12891 case EM_NDS32:
12892 return reloc_type == 19; /* R_NDS32_RELA. */
12893 case EM_ALTERA_NIOS2:
12894 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
12895 case EM_NIOS32:
12896 return reloc_type == 9; /* R_NIOS_16. */
12897 case EM_OR1K:
12898 return reloc_type == 2; /* R_OR1K_16. */
12899 case EM_RISCV:
12900 return reloc_type == 55; /* R_RISCV_SET16. */
12901 case EM_TI_PRU:
12902 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
12903 case EM_TI_C6000:
12904 return reloc_type == 2; /* R_C6000_ABS16. */
12905 case EM_VISIUM:
12906 return reloc_type == 2; /* R_VISIUM_16. */
12907 case EM_XC16X:
12908 case EM_C166:
12909 return reloc_type == 2; /* R_XC16C_ABS_16. */
12910 case EM_XGATE:
12911 return reloc_type == 3; /* R_XGATE_16. */
12912 default:
12913 return FALSE;
12914 }
12915 }
12916
12917 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12918 a 8-bit absolute RELA relocation used in DWARF debug sections. */
12919
12920 static bfd_boolean
12921 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12922 {
12923 switch (filedata->file_header.e_machine)
12924 {
12925 case EM_RISCV:
12926 return reloc_type == 54; /* R_RISCV_SET8. */
12927 default:
12928 return FALSE;
12929 }
12930 }
12931
12932 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12933 a 6-bit absolute RELA relocation used in DWARF debug sections. */
12934
12935 static bfd_boolean
12936 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
12937 {
12938 switch (filedata->file_header.e_machine)
12939 {
12940 case EM_RISCV:
12941 return reloc_type == 53; /* R_RISCV_SET6. */
12942 default:
12943 return FALSE;
12944 }
12945 }
12946
12947 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12948 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
12949
12950 static bfd_boolean
12951 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
12952 {
12953 /* Please keep this table alpha-sorted for ease of visual lookup. */
12954 switch (filedata->file_header.e_machine)
12955 {
12956 case EM_RISCV:
12957 return reloc_type == 35; /* R_RISCV_ADD32. */
12958 default:
12959 return FALSE;
12960 }
12961 }
12962
12963 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12964 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
12965
12966 static bfd_boolean
12967 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
12968 {
12969 /* Please keep this table alpha-sorted for ease of visual lookup. */
12970 switch (filedata->file_header.e_machine)
12971 {
12972 case EM_RISCV:
12973 return reloc_type == 39; /* R_RISCV_SUB32. */
12974 default:
12975 return FALSE;
12976 }
12977 }
12978
12979 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12980 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
12981
12982 static bfd_boolean
12983 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
12984 {
12985 /* Please keep this table alpha-sorted for ease of visual lookup. */
12986 switch (filedata->file_header.e_machine)
12987 {
12988 case EM_RISCV:
12989 return reloc_type == 36; /* R_RISCV_ADD64. */
12990 default:
12991 return FALSE;
12992 }
12993 }
12994
12995 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
12996 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
12997
12998 static bfd_boolean
12999 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13000 {
13001 /* Please keep this table alpha-sorted for ease of visual lookup. */
13002 switch (filedata->file_header.e_machine)
13003 {
13004 case EM_RISCV:
13005 return reloc_type == 40; /* R_RISCV_SUB64. */
13006 default:
13007 return FALSE;
13008 }
13009 }
13010
13011 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13012 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
13013
13014 static bfd_boolean
13015 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13016 {
13017 /* Please keep this table alpha-sorted for ease of visual lookup. */
13018 switch (filedata->file_header.e_machine)
13019 {
13020 case EM_RISCV:
13021 return reloc_type == 34; /* R_RISCV_ADD16. */
13022 default:
13023 return FALSE;
13024 }
13025 }
13026
13027 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13028 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
13029
13030 static bfd_boolean
13031 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13032 {
13033 /* Please keep this table alpha-sorted for ease of visual lookup. */
13034 switch (filedata->file_header.e_machine)
13035 {
13036 case EM_RISCV:
13037 return reloc_type == 38; /* R_RISCV_SUB16. */
13038 default:
13039 return FALSE;
13040 }
13041 }
13042
13043 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13044 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
13045
13046 static bfd_boolean
13047 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13048 {
13049 /* Please keep this table alpha-sorted for ease of visual lookup. */
13050 switch (filedata->file_header.e_machine)
13051 {
13052 case EM_RISCV:
13053 return reloc_type == 33; /* R_RISCV_ADD8. */
13054 default:
13055 return FALSE;
13056 }
13057 }
13058
13059 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13060 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
13061
13062 static bfd_boolean
13063 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13064 {
13065 /* Please keep this table alpha-sorted for ease of visual lookup. */
13066 switch (filedata->file_header.e_machine)
13067 {
13068 case EM_RISCV:
13069 return reloc_type == 37; /* R_RISCV_SUB8. */
13070 default:
13071 return FALSE;
13072 }
13073 }
13074
13075 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13076 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
13077
13078 static bfd_boolean
13079 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13080 {
13081 switch (filedata->file_header.e_machine)
13082 {
13083 case EM_RISCV:
13084 return reloc_type == 52; /* R_RISCV_SUB6. */
13085 default:
13086 return FALSE;
13087 }
13088 }
13089
13090 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
13091 relocation entries (possibly formerly used for SHT_GROUP sections). */
13092
13093 static bfd_boolean
13094 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
13095 {
13096 switch (filedata->file_header.e_machine)
13097 {
13098 case EM_386: /* R_386_NONE. */
13099 case EM_68K: /* R_68K_NONE. */
13100 case EM_ADAPTEVA_EPIPHANY:
13101 case EM_ALPHA: /* R_ALPHA_NONE. */
13102 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
13103 case EM_ARC: /* R_ARC_NONE. */
13104 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
13105 case EM_ARC_COMPACT: /* R_ARC_NONE. */
13106 case EM_ARM: /* R_ARM_NONE. */
13107 case EM_C166: /* R_XC16X_NONE. */
13108 case EM_CRIS: /* R_CRIS_NONE. */
13109 case EM_FT32: /* R_FT32_NONE. */
13110 case EM_IA_64: /* R_IA64_NONE. */
13111 case EM_K1OM: /* R_X86_64_NONE. */
13112 case EM_L1OM: /* R_X86_64_NONE. */
13113 case EM_M32R: /* R_M32R_NONE. */
13114 case EM_MIPS: /* R_MIPS_NONE. */
13115 case EM_MN10300: /* R_MN10300_NONE. */
13116 case EM_MOXIE: /* R_MOXIE_NONE. */
13117 case EM_NIOS32: /* R_NIOS_NONE. */
13118 case EM_OR1K: /* R_OR1K_NONE. */
13119 case EM_PARISC: /* R_PARISC_NONE. */
13120 case EM_PPC64: /* R_PPC64_NONE. */
13121 case EM_PPC: /* R_PPC_NONE. */
13122 case EM_RISCV: /* R_RISCV_NONE. */
13123 case EM_S390: /* R_390_NONE. */
13124 case EM_S390_OLD:
13125 case EM_SH: /* R_SH_NONE. */
13126 case EM_SPARC32PLUS:
13127 case EM_SPARC: /* R_SPARC_NONE. */
13128 case EM_SPARCV9:
13129 case EM_TILEGX: /* R_TILEGX_NONE. */
13130 case EM_TILEPRO: /* R_TILEPRO_NONE. */
13131 case EM_TI_C6000:/* R_C6000_NONE. */
13132 case EM_X86_64: /* R_X86_64_NONE. */
13133 case EM_XC16X:
13134 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
13135 return reloc_type == 0;
13136
13137 case EM_AARCH64:
13138 return reloc_type == 0 || reloc_type == 256;
13139 case EM_AVR_OLD:
13140 case EM_AVR:
13141 return (reloc_type == 0 /* R_AVR_NONE. */
13142 || reloc_type == 30 /* R_AVR_DIFF8. */
13143 || reloc_type == 31 /* R_AVR_DIFF16. */
13144 || reloc_type == 32 /* R_AVR_DIFF32. */);
13145 case EM_METAG:
13146 return reloc_type == 3; /* R_METAG_NONE. */
13147 case EM_NDS32:
13148 return (reloc_type == 0 /* R_XTENSA_NONE. */
13149 || reloc_type == 204 /* R_NDS32_DIFF8. */
13150 || reloc_type == 205 /* R_NDS32_DIFF16. */
13151 || reloc_type == 206 /* R_NDS32_DIFF32. */
13152 || reloc_type == 207 /* R_NDS32_ULEB128. */);
13153 case EM_TI_PRU:
13154 return (reloc_type == 0 /* R_PRU_NONE. */
13155 || reloc_type == 65 /* R_PRU_DIFF8. */
13156 || reloc_type == 66 /* R_PRU_DIFF16. */
13157 || reloc_type == 67 /* R_PRU_DIFF32. */);
13158 case EM_XTENSA_OLD:
13159 case EM_XTENSA:
13160 return (reloc_type == 0 /* R_XTENSA_NONE. */
13161 || reloc_type == 17 /* R_XTENSA_DIFF8. */
13162 || reloc_type == 18 /* R_XTENSA_DIFF16. */
13163 || reloc_type == 19 /* R_XTENSA_DIFF32. */);
13164 }
13165 return FALSE;
13166 }
13167
13168 /* Returns TRUE if there is a relocation against
13169 section NAME at OFFSET bytes. */
13170
13171 bfd_boolean
13172 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
13173 {
13174 Elf_Internal_Rela * relocs;
13175 Elf_Internal_Rela * rp;
13176
13177 if (dsec == NULL || dsec->reloc_info == NULL)
13178 return FALSE;
13179
13180 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
13181
13182 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
13183 if (rp->r_offset == offset)
13184 return TRUE;
13185
13186 return FALSE;
13187 }
13188
13189 /* Apply relocations to a section.
13190 Returns TRUE upon success, FALSE otherwise.
13191 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
13192 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
13193 will be set to the number of relocs loaded.
13194
13195 Note: So far support has been added only for those relocations
13196 which can be found in debug sections. FIXME: Add support for
13197 more relocations ? */
13198
13199 static bfd_boolean
13200 apply_relocations (Filedata * filedata,
13201 const Elf_Internal_Shdr * section,
13202 unsigned char * start,
13203 bfd_size_type size,
13204 void ** relocs_return,
13205 unsigned long * num_relocs_return)
13206 {
13207 Elf_Internal_Shdr * relsec;
13208 unsigned char * end = start + size;
13209
13210 if (relocs_return != NULL)
13211 {
13212 * (Elf_Internal_Rela **) relocs_return = NULL;
13213 * num_relocs_return = 0;
13214 }
13215
13216 if (filedata->file_header.e_type != ET_REL)
13217 /* No relocs to apply. */
13218 return TRUE;
13219
13220 /* Find the reloc section associated with the section. */
13221 for (relsec = filedata->section_headers;
13222 relsec < filedata->section_headers + filedata->file_header.e_shnum;
13223 ++relsec)
13224 {
13225 bfd_boolean is_rela;
13226 unsigned long num_relocs;
13227 Elf_Internal_Rela * relocs;
13228 Elf_Internal_Rela * rp;
13229 Elf_Internal_Shdr * symsec;
13230 Elf_Internal_Sym * symtab;
13231 unsigned long num_syms;
13232 Elf_Internal_Sym * sym;
13233
13234 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
13235 || relsec->sh_info >= filedata->file_header.e_shnum
13236 || filedata->section_headers + relsec->sh_info != section
13237 || relsec->sh_size == 0
13238 || relsec->sh_link >= filedata->file_header.e_shnum)
13239 continue;
13240
13241 is_rela = relsec->sh_type == SHT_RELA;
13242
13243 if (is_rela)
13244 {
13245 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
13246 relsec->sh_size, & relocs, & num_relocs))
13247 return FALSE;
13248 }
13249 else
13250 {
13251 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
13252 relsec->sh_size, & relocs, & num_relocs))
13253 return FALSE;
13254 }
13255
13256 /* SH uses RELA but uses in place value instead of the addend field. */
13257 if (filedata->file_header.e_machine == EM_SH)
13258 is_rela = FALSE;
13259
13260 symsec = filedata->section_headers + relsec->sh_link;
13261 if (symsec->sh_type != SHT_SYMTAB
13262 && symsec->sh_type != SHT_DYNSYM)
13263 return FALSE;
13264 symtab = GET_ELF_SYMBOLS (filedata, symsec, & num_syms);
13265
13266 for (rp = relocs; rp < relocs + num_relocs; ++rp)
13267 {
13268 bfd_vma addend;
13269 unsigned int reloc_type;
13270 unsigned int reloc_size;
13271 bfd_boolean reloc_inplace = FALSE;
13272 bfd_boolean reloc_subtract = FALSE;
13273 unsigned char * rloc;
13274 unsigned long sym_index;
13275
13276 reloc_type = get_reloc_type (filedata, rp->r_info);
13277
13278 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
13279 continue;
13280 else if (is_none_reloc (filedata, reloc_type))
13281 continue;
13282 else if (is_32bit_abs_reloc (filedata, reloc_type)
13283 || is_32bit_pcrel_reloc (filedata, reloc_type))
13284 reloc_size = 4;
13285 else if (is_64bit_abs_reloc (filedata, reloc_type)
13286 || is_64bit_pcrel_reloc (filedata, reloc_type))
13287 reloc_size = 8;
13288 else if (is_24bit_abs_reloc (filedata, reloc_type))
13289 reloc_size = 3;
13290 else if (is_16bit_abs_reloc (filedata, reloc_type))
13291 reloc_size = 2;
13292 else if (is_8bit_abs_reloc (filedata, reloc_type)
13293 || is_6bit_abs_reloc (filedata, reloc_type))
13294 reloc_size = 1;
13295 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
13296 reloc_type))
13297 || is_32bit_inplace_add_reloc (filedata, reloc_type))
13298 {
13299 reloc_size = 4;
13300 reloc_inplace = TRUE;
13301 }
13302 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
13303 reloc_type))
13304 || is_64bit_inplace_add_reloc (filedata, reloc_type))
13305 {
13306 reloc_size = 8;
13307 reloc_inplace = TRUE;
13308 }
13309 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
13310 reloc_type))
13311 || is_16bit_inplace_add_reloc (filedata, reloc_type))
13312 {
13313 reloc_size = 2;
13314 reloc_inplace = TRUE;
13315 }
13316 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
13317 reloc_type))
13318 || is_8bit_inplace_add_reloc (filedata, reloc_type))
13319 {
13320 reloc_size = 1;
13321 reloc_inplace = TRUE;
13322 }
13323 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
13324 reloc_type)))
13325 {
13326 reloc_size = 1;
13327 reloc_inplace = TRUE;
13328 }
13329 else
13330 {
13331 static unsigned int prev_reloc = 0;
13332
13333 if (reloc_type != prev_reloc)
13334 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
13335 reloc_type, printable_section_name (filedata, section));
13336 prev_reloc = reloc_type;
13337 continue;
13338 }
13339
13340 rloc = start + rp->r_offset;
13341 if ((rloc + reloc_size) > end || (rloc < start))
13342 {
13343 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
13344 (unsigned long) rp->r_offset,
13345 printable_section_name (filedata, section));
13346 continue;
13347 }
13348
13349 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
13350 if (sym_index >= num_syms)
13351 {
13352 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
13353 sym_index, printable_section_name (filedata, section));
13354 continue;
13355 }
13356 sym = symtab + sym_index;
13357
13358 /* If the reloc has a symbol associated with it,
13359 make sure that it is of an appropriate type.
13360
13361 Relocations against symbols without type can happen.
13362 Gcc -feliminate-dwarf2-dups may generate symbols
13363 without type for debug info.
13364
13365 Icc generates relocations against function symbols
13366 instead of local labels.
13367
13368 Relocations against object symbols can happen, eg when
13369 referencing a global array. For an example of this see
13370 the _clz.o binary in libgcc.a. */
13371 if (sym != symtab
13372 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
13373 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
13374 {
13375 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
13376 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
13377 printable_section_name (filedata, relsec),
13378 (long int)(rp - relocs));
13379 continue;
13380 }
13381
13382 addend = 0;
13383 if (is_rela)
13384 addend += rp->r_addend;
13385 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
13386 partial_inplace. */
13387 if (!is_rela
13388 || (filedata->file_header.e_machine == EM_XTENSA
13389 && reloc_type == 1)
13390 || ((filedata->file_header.e_machine == EM_PJ
13391 || filedata->file_header.e_machine == EM_PJ_OLD)
13392 && reloc_type == 1)
13393 || ((filedata->file_header.e_machine == EM_D30V
13394 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
13395 && reloc_type == 12)
13396 || reloc_inplace)
13397 {
13398 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
13399 addend += byte_get (rloc, reloc_size) & 0x3f;
13400 else
13401 addend += byte_get (rloc, reloc_size);
13402 }
13403
13404 if (is_32bit_pcrel_reloc (filedata, reloc_type)
13405 || is_64bit_pcrel_reloc (filedata, reloc_type))
13406 {
13407 /* On HPPA, all pc-relative relocations are biased by 8. */
13408 if (filedata->file_header.e_machine == EM_PARISC)
13409 addend -= 8;
13410 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
13411 reloc_size);
13412 }
13413 else if (is_6bit_abs_reloc (filedata, reloc_type)
13414 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
13415 {
13416 if (reloc_subtract)
13417 addend -= sym->st_value;
13418 else
13419 addend += sym->st_value;
13420 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
13421 byte_put (rloc, addend, reloc_size);
13422 }
13423 else if (reloc_subtract)
13424 byte_put (rloc, addend - sym->st_value, reloc_size);
13425 else
13426 byte_put (rloc, addend + sym->st_value, reloc_size);
13427 }
13428
13429 free (symtab);
13430 /* Let the target specific reloc processing code know that
13431 we have finished with these relocs. */
13432 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
13433
13434 if (relocs_return)
13435 {
13436 * (Elf_Internal_Rela **) relocs_return = relocs;
13437 * num_relocs_return = num_relocs;
13438 }
13439 else
13440 free (relocs);
13441
13442 break;
13443 }
13444
13445 return TRUE;
13446 }
13447
13448 #ifdef SUPPORT_DISASSEMBLY
13449 static bfd_boolean
13450 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
13451 {
13452 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
13453
13454 /* FIXME: XXX -- to be done --- XXX */
13455
13456 return TRUE;
13457 }
13458 #endif
13459
13460 /* Reads in the contents of SECTION from FILE, returning a pointer
13461 to a malloc'ed buffer or NULL if something went wrong. */
13462
13463 static char *
13464 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
13465 {
13466 bfd_size_type num_bytes = section->sh_size;
13467
13468 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
13469 {
13470 printf (_("Section '%s' has no data to dump.\n"),
13471 printable_section_name (filedata, section));
13472 return NULL;
13473 }
13474
13475 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
13476 _("section contents"));
13477 }
13478
13479 /* Uncompresses a section that was compressed using zlib, in place. */
13480
13481 static bfd_boolean
13482 uncompress_section_contents (unsigned char ** buffer,
13483 dwarf_size_type uncompressed_size,
13484 dwarf_size_type * size)
13485 {
13486 dwarf_size_type compressed_size = *size;
13487 unsigned char * compressed_buffer = *buffer;
13488 unsigned char * uncompressed_buffer;
13489 z_stream strm;
13490 int rc;
13491
13492 /* It is possible the section consists of several compressed
13493 buffers concatenated together, so we uncompress in a loop. */
13494 /* PR 18313: The state field in the z_stream structure is supposed
13495 to be invisible to the user (ie us), but some compilers will
13496 still complain about it being used without initialisation. So
13497 we first zero the entire z_stream structure and then set the fields
13498 that we need. */
13499 memset (& strm, 0, sizeof strm);
13500 strm.avail_in = compressed_size;
13501 strm.next_in = (Bytef *) compressed_buffer;
13502 strm.avail_out = uncompressed_size;
13503 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
13504
13505 rc = inflateInit (& strm);
13506 while (strm.avail_in > 0)
13507 {
13508 if (rc != Z_OK)
13509 goto fail;
13510 strm.next_out = ((Bytef *) uncompressed_buffer
13511 + (uncompressed_size - strm.avail_out));
13512 rc = inflate (&strm, Z_FINISH);
13513 if (rc != Z_STREAM_END)
13514 goto fail;
13515 rc = inflateReset (& strm);
13516 }
13517 rc = inflateEnd (& strm);
13518 if (rc != Z_OK
13519 || strm.avail_out != 0)
13520 goto fail;
13521
13522 *buffer = uncompressed_buffer;
13523 *size = uncompressed_size;
13524 return TRUE;
13525
13526 fail:
13527 free (uncompressed_buffer);
13528 /* Indicate decompression failure. */
13529 *buffer = NULL;
13530 return FALSE;
13531 }
13532
13533 static bfd_boolean
13534 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
13535 {
13536 Elf_Internal_Shdr * relsec;
13537 bfd_size_type num_bytes;
13538 unsigned char * data;
13539 unsigned char * end;
13540 unsigned char * real_start;
13541 unsigned char * start;
13542 bfd_boolean some_strings_shown;
13543
13544 real_start = start = (unsigned char *) get_section_contents (section, filedata);
13545 if (start == NULL)
13546 /* PR 21820: Do not fail if the section was empty. */
13547 return (section->sh_size == 0 || section->sh_type == SHT_NOBITS) ? TRUE : FALSE;
13548
13549 num_bytes = section->sh_size;
13550
13551 printf (_("\nString dump of section '%s':\n"), printable_section_name (filedata, section));
13552
13553 if (decompress_dumps)
13554 {
13555 dwarf_size_type new_size = num_bytes;
13556 dwarf_size_type uncompressed_size = 0;
13557
13558 if ((section->sh_flags & SHF_COMPRESSED) != 0)
13559 {
13560 Elf_Internal_Chdr chdr;
13561 unsigned int compression_header_size
13562 = get_compression_header (& chdr, (unsigned char *) start,
13563 num_bytes);
13564
13565 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
13566 {
13567 warn (_("section '%s' has unsupported compress type: %d\n"),
13568 printable_section_name (filedata, section), chdr.ch_type);
13569 return FALSE;
13570 }
13571 uncompressed_size = chdr.ch_size;
13572 start += compression_header_size;
13573 new_size -= compression_header_size;
13574 }
13575 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
13576 {
13577 /* Read the zlib header. In this case, it should be "ZLIB"
13578 followed by the uncompressed section size, 8 bytes in
13579 big-endian order. */
13580 uncompressed_size = start[4]; uncompressed_size <<= 8;
13581 uncompressed_size += start[5]; uncompressed_size <<= 8;
13582 uncompressed_size += start[6]; uncompressed_size <<= 8;
13583 uncompressed_size += start[7]; uncompressed_size <<= 8;
13584 uncompressed_size += start[8]; uncompressed_size <<= 8;
13585 uncompressed_size += start[9]; uncompressed_size <<= 8;
13586 uncompressed_size += start[10]; uncompressed_size <<= 8;
13587 uncompressed_size += start[11];
13588 start += 12;
13589 new_size -= 12;
13590 }
13591
13592 if (uncompressed_size)
13593 {
13594 if (uncompress_section_contents (& start,
13595 uncompressed_size, & new_size))
13596 num_bytes = new_size;
13597 else
13598 {
13599 error (_("Unable to decompress section %s\n"),
13600 printable_section_name (filedata, section));
13601 return FALSE;
13602 }
13603 }
13604 else
13605 start = real_start;
13606 }
13607
13608 /* If the section being dumped has relocations against it the user might
13609 be expecting these relocations to have been applied. Check for this
13610 case and issue a warning message in order to avoid confusion.
13611 FIXME: Maybe we ought to have an option that dumps a section with
13612 relocs applied ? */
13613 for (relsec = filedata->section_headers;
13614 relsec < filedata->section_headers + filedata->file_header.e_shnum;
13615 ++relsec)
13616 {
13617 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
13618 || relsec->sh_info >= filedata->file_header.e_shnum
13619 || filedata->section_headers + relsec->sh_info != section
13620 || relsec->sh_size == 0
13621 || relsec->sh_link >= filedata->file_header.e_shnum)
13622 continue;
13623
13624 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
13625 break;
13626 }
13627
13628 data = start;
13629 end = start + num_bytes;
13630 some_strings_shown = FALSE;
13631
13632 while (data < end)
13633 {
13634 while (!ISPRINT (* data))
13635 if (++ data >= end)
13636 break;
13637
13638 if (data < end)
13639 {
13640 size_t maxlen = end - data;
13641
13642 #ifndef __MSVCRT__
13643 /* PR 11128: Use two separate invocations in order to work
13644 around bugs in the Solaris 8 implementation of printf. */
13645 printf (" [%6tx] ", data - start);
13646 #else
13647 printf (" [%6Ix] ", (size_t) (data - start));
13648 #endif
13649 if (maxlen > 0)
13650 {
13651 print_symbol ((int) maxlen, (const char *) data);
13652 putchar ('\n');
13653 data += strnlen ((const char *) data, maxlen);
13654 }
13655 else
13656 {
13657 printf (_("<corrupt>\n"));
13658 data = end;
13659 }
13660 some_strings_shown = TRUE;
13661 }
13662 }
13663
13664 if (! some_strings_shown)
13665 printf (_(" No strings found in this section."));
13666
13667 free (real_start);
13668
13669 putchar ('\n');
13670 return TRUE;
13671 }
13672
13673 static bfd_boolean
13674 dump_section_as_bytes (Elf_Internal_Shdr * section,
13675 Filedata * filedata,
13676 bfd_boolean relocate)
13677 {
13678 Elf_Internal_Shdr * relsec;
13679 bfd_size_type bytes;
13680 bfd_size_type section_size;
13681 bfd_vma addr;
13682 unsigned char * data;
13683 unsigned char * real_start;
13684 unsigned char * start;
13685
13686 real_start = start = (unsigned char *) get_section_contents (section, filedata);
13687 if (start == NULL)
13688 /* PR 21820: Do not fail if the section was empty. */
13689 return (section->sh_size == 0 || section->sh_type == SHT_NOBITS) ? TRUE : FALSE;
13690
13691 section_size = section->sh_size;
13692
13693 printf (_("\nHex dump of section '%s':\n"), printable_section_name (filedata, section));
13694
13695 if (decompress_dumps)
13696 {
13697 dwarf_size_type new_size = section_size;
13698 dwarf_size_type uncompressed_size = 0;
13699
13700 if ((section->sh_flags & SHF_COMPRESSED) != 0)
13701 {
13702 Elf_Internal_Chdr chdr;
13703 unsigned int compression_header_size
13704 = get_compression_header (& chdr, start, section_size);
13705
13706 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
13707 {
13708 warn (_("section '%s' has unsupported compress type: %d\n"),
13709 printable_section_name (filedata, section), chdr.ch_type);
13710 return FALSE;
13711 }
13712 uncompressed_size = chdr.ch_size;
13713 start += compression_header_size;
13714 new_size -= compression_header_size;
13715 }
13716 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
13717 {
13718 /* Read the zlib header. In this case, it should be "ZLIB"
13719 followed by the uncompressed section size, 8 bytes in
13720 big-endian order. */
13721 uncompressed_size = start[4]; uncompressed_size <<= 8;
13722 uncompressed_size += start[5]; uncompressed_size <<= 8;
13723 uncompressed_size += start[6]; uncompressed_size <<= 8;
13724 uncompressed_size += start[7]; uncompressed_size <<= 8;
13725 uncompressed_size += start[8]; uncompressed_size <<= 8;
13726 uncompressed_size += start[9]; uncompressed_size <<= 8;
13727 uncompressed_size += start[10]; uncompressed_size <<= 8;
13728 uncompressed_size += start[11];
13729 start += 12;
13730 new_size -= 12;
13731 }
13732
13733 if (uncompressed_size)
13734 {
13735 if (uncompress_section_contents (& start, uncompressed_size,
13736 & new_size))
13737 {
13738 section_size = new_size;
13739 }
13740 else
13741 {
13742 error (_("Unable to decompress section %s\n"),
13743 printable_section_name (filedata, section));
13744 /* FIXME: Print the section anyway ? */
13745 return FALSE;
13746 }
13747 }
13748 else
13749 start = real_start;
13750 }
13751
13752 if (relocate)
13753 {
13754 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
13755 return FALSE;
13756 }
13757 else
13758 {
13759 /* If the section being dumped has relocations against it the user might
13760 be expecting these relocations to have been applied. Check for this
13761 case and issue a warning message in order to avoid confusion.
13762 FIXME: Maybe we ought to have an option that dumps a section with
13763 relocs applied ? */
13764 for (relsec = filedata->section_headers;
13765 relsec < filedata->section_headers + filedata->file_header.e_shnum;
13766 ++relsec)
13767 {
13768 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
13769 || relsec->sh_info >= filedata->file_header.e_shnum
13770 || filedata->section_headers + relsec->sh_info != section
13771 || relsec->sh_size == 0
13772 || relsec->sh_link >= filedata->file_header.e_shnum)
13773 continue;
13774
13775 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
13776 break;
13777 }
13778 }
13779
13780 addr = section->sh_addr;
13781 bytes = section_size;
13782 data = start;
13783
13784 while (bytes)
13785 {
13786 int j;
13787 int k;
13788 int lbytes;
13789
13790 lbytes = (bytes > 16 ? 16 : bytes);
13791
13792 printf (" 0x%8.8lx ", (unsigned long) addr);
13793
13794 for (j = 0; j < 16; j++)
13795 {
13796 if (j < lbytes)
13797 printf ("%2.2x", data[j]);
13798 else
13799 printf (" ");
13800
13801 if ((j & 3) == 3)
13802 printf (" ");
13803 }
13804
13805 for (j = 0; j < lbytes; j++)
13806 {
13807 k = data[j];
13808 if (k >= ' ' && k < 0x7f)
13809 printf ("%c", k);
13810 else
13811 printf (".");
13812 }
13813
13814 putchar ('\n');
13815
13816 data += lbytes;
13817 addr += lbytes;
13818 bytes -= lbytes;
13819 }
13820
13821 free (real_start);
13822
13823 putchar ('\n');
13824 return TRUE;
13825 }
13826
13827 #ifdef HAVE_LIBCTF
13828 static ctf_sect_t *
13829 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
13830 {
13831 buf->cts_name = SECTION_NAME (shdr);
13832 buf->cts_type = shdr->sh_type;
13833 buf->cts_flags = shdr->sh_flags;
13834 buf->cts_size = shdr->sh_size;
13835 buf->cts_entsize = shdr->sh_entsize;
13836 buf->cts_offset = (off64_t) shdr->sh_offset;
13837
13838 return buf;
13839 }
13840
13841 /* Formatting callback function passed to ctf_dump. Returns either the pointer
13842 it is passed, or a pointer to newly-allocated storage, in which case
13843 dump_ctf() will free it when it no longer needs it. */
13844
13845 static char *dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
13846 char *s, void *arg)
13847 {
13848 char *spaces = arg;
13849 char *new_s;
13850
13851 if (asprintf (&new_s, "%s%s", spaces, s) < 0)
13852 return s;
13853 return new_s;
13854 }
13855
13856 static bfd_boolean
13857 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
13858 {
13859 Elf_Internal_Shdr * parent_sec = NULL;
13860 Elf_Internal_Shdr * symtab_sec = NULL;
13861 Elf_Internal_Shdr * strtab_sec = NULL;
13862 void * data = NULL;
13863 void * symdata = NULL;
13864 void * strdata = NULL;
13865 void * parentdata = NULL;
13866 ctf_sect_t ctfsect, symsect, strsect, parentsect;
13867 ctf_sect_t * symsectp = NULL;
13868 ctf_sect_t * strsectp = NULL;
13869 ctf_file_t * ctf = NULL;
13870 ctf_file_t * parent = NULL;
13871
13872 const char *things[] = {"Labels", "Data objects", "Function objects",
13873 "Variables", "Types", "Strings", ""};
13874 const char **thing;
13875 int err;
13876 bfd_boolean ret = FALSE;
13877 size_t i;
13878
13879 shdr_to_ctf_sect (&ctfsect, section, filedata);
13880 data = get_section_contents (section, filedata);
13881 ctfsect.cts_data = data;
13882
13883 if (dump_ctf_symtab_name)
13884 {
13885 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
13886 {
13887 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
13888 goto fail;
13889 }
13890 if ((symdata = (void *) get_data (NULL, filedata,
13891 symtab_sec->sh_offset, 1,
13892 symtab_sec->sh_size,
13893 _("symbols"))) == NULL)
13894 goto fail;
13895 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
13896 symsect.cts_data = symdata;
13897 }
13898 if (dump_ctf_strtab_name)
13899 {
13900 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
13901 {
13902 error (_("No string table section named %s\n"),
13903 dump_ctf_strtab_name);
13904 goto fail;
13905 }
13906 if ((strdata = (void *) get_data (NULL, filedata,
13907 strtab_sec->sh_offset, 1,
13908 strtab_sec->sh_size,
13909 _("strings"))) == NULL)
13910 goto fail;
13911 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
13912 strsect.cts_data = strdata;
13913 }
13914 if (dump_ctf_parent_name)
13915 {
13916 if ((parent_sec = find_section (filedata, dump_ctf_parent_name)) == NULL)
13917 {
13918 error (_("No CTF parent section named %s\n"), dump_ctf_parent_name);
13919 goto fail;
13920 }
13921 if ((parentdata = (void *) get_data (NULL, filedata,
13922 parent_sec->sh_offset, 1,
13923 parent_sec->sh_size,
13924 _("CTF parent"))) == NULL)
13925 goto fail;
13926 shdr_to_ctf_sect (&parentsect, parent_sec, filedata);
13927 parentsect.cts_data = parentdata;
13928 }
13929
13930 /* Load the CTF file and dump it. */
13931
13932 if ((ctf = ctf_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
13933 {
13934 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
13935 goto fail;
13936 }
13937
13938 if (parentdata)
13939 {
13940 if ((parent = ctf_bufopen (&parentsect, symsectp, strsectp, &err)) == NULL)
13941 {
13942 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
13943 goto fail;
13944 }
13945
13946 ctf_import (ctf, parent);
13947 }
13948
13949 ret = TRUE;
13950
13951 printf (_("\nDump of CTF section '%s':\n"),
13952 printable_section_name (filedata, section));
13953
13954 for (i = 1, thing = things; *thing[0]; thing++, i++)
13955 {
13956 ctf_dump_state_t *s = NULL;
13957 char *item;
13958
13959 printf ("\n %s:\n", *thing);
13960 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
13961 (void *) " ")) != NULL)
13962 {
13963 printf ("%s\n", item);
13964 free (item);
13965 }
13966
13967 if (ctf_errno (ctf))
13968 {
13969 error (_("Iteration failed: %s, %s\n"), *thing,
13970 ctf_errmsg (ctf_errno (ctf)));
13971 ret = FALSE;
13972 }
13973 }
13974
13975 fail:
13976 ctf_file_close (ctf);
13977 ctf_file_close (parent);
13978 free (parentdata);
13979 free (data);
13980 free (symdata);
13981 free (strdata);
13982 return ret;
13983 }
13984 #endif /* HAVE_LIBCTF */
13985
13986 static bfd_boolean
13987 load_specific_debug_section (enum dwarf_section_display_enum debug,
13988 const Elf_Internal_Shdr * sec,
13989 void * data)
13990 {
13991 struct dwarf_section * section = &debug_displays [debug].section;
13992 char buf [64];
13993 Filedata * filedata = (Filedata *) data;
13994
13995 if (section->start != NULL)
13996 {
13997 /* If it is already loaded, do nothing. */
13998 if (streq (section->filename, filedata->file_name))
13999 return TRUE;
14000 free (section->start);
14001 }
14002
14003 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
14004 section->address = sec->sh_addr;
14005 section->user_data = NULL;
14006 section->filename = filedata->file_name;
14007 section->start = (unsigned char *) get_data (NULL, filedata,
14008 sec->sh_offset, 1,
14009 sec->sh_size, buf);
14010 if (section->start == NULL)
14011 section->size = 0;
14012 else
14013 {
14014 unsigned char *start = section->start;
14015 dwarf_size_type size = sec->sh_size;
14016 dwarf_size_type uncompressed_size = 0;
14017
14018 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
14019 {
14020 Elf_Internal_Chdr chdr;
14021 unsigned int compression_header_size;
14022
14023 if (size < (is_32bit_elf
14024 ? sizeof (Elf32_External_Chdr)
14025 : sizeof (Elf64_External_Chdr)))
14026 {
14027 warn (_("compressed section %s is too small to contain a compression header"),
14028 section->name);
14029 return FALSE;
14030 }
14031
14032 compression_header_size = get_compression_header (&chdr, start, size);
14033
14034 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14035 {
14036 warn (_("section '%s' has unsupported compress type: %d\n"),
14037 section->name, chdr.ch_type);
14038 return FALSE;
14039 }
14040 uncompressed_size = chdr.ch_size;
14041 start += compression_header_size;
14042 size -= compression_header_size;
14043 }
14044 else if (size > 12 && streq ((char *) start, "ZLIB"))
14045 {
14046 /* Read the zlib header. In this case, it should be "ZLIB"
14047 followed by the uncompressed section size, 8 bytes in
14048 big-endian order. */
14049 uncompressed_size = start[4]; uncompressed_size <<= 8;
14050 uncompressed_size += start[5]; uncompressed_size <<= 8;
14051 uncompressed_size += start[6]; uncompressed_size <<= 8;
14052 uncompressed_size += start[7]; uncompressed_size <<= 8;
14053 uncompressed_size += start[8]; uncompressed_size <<= 8;
14054 uncompressed_size += start[9]; uncompressed_size <<= 8;
14055 uncompressed_size += start[10]; uncompressed_size <<= 8;
14056 uncompressed_size += start[11];
14057 start += 12;
14058 size -= 12;
14059 }
14060
14061 if (uncompressed_size)
14062 {
14063 if (uncompress_section_contents (&start, uncompressed_size,
14064 &size))
14065 {
14066 /* Free the compressed buffer, update the section buffer
14067 and the section size if uncompress is successful. */
14068 free (section->start);
14069 section->start = start;
14070 }
14071 else
14072 {
14073 error (_("Unable to decompress section %s\n"),
14074 printable_section_name (filedata, sec));
14075 return FALSE;
14076 }
14077 }
14078
14079 section->size = size;
14080 }
14081
14082 if (section->start == NULL)
14083 return FALSE;
14084
14085 if (debug_displays [debug].relocate)
14086 {
14087 if (! apply_relocations (filedata, sec, section->start, section->size,
14088 & section->reloc_info, & section->num_relocs))
14089 return FALSE;
14090 }
14091 else
14092 {
14093 section->reloc_info = NULL;
14094 section->num_relocs = 0;
14095 }
14096
14097 return TRUE;
14098 }
14099
14100 /* If this is not NULL, load_debug_section will only look for sections
14101 within the list of sections given here. */
14102 static unsigned int * section_subset = NULL;
14103
14104 bfd_boolean
14105 load_debug_section (enum dwarf_section_display_enum debug, void * data)
14106 {
14107 struct dwarf_section * section = &debug_displays [debug].section;
14108 Elf_Internal_Shdr * sec;
14109 Filedata * filedata = (Filedata *) data;
14110
14111 /* Without section headers we cannot find any sections. */
14112 if (filedata->section_headers == NULL)
14113 return FALSE;
14114
14115 if (filedata->string_table == NULL
14116 && filedata->file_header.e_shstrndx != SHN_UNDEF
14117 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
14118 {
14119 Elf_Internal_Shdr * strs;
14120
14121 /* Read in the string table, so that we have section names to scan. */
14122 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
14123
14124 if (strs != NULL && strs->sh_size != 0)
14125 {
14126 filedata->string_table
14127 = (char *) get_data (NULL, filedata, strs->sh_offset,
14128 1, strs->sh_size, _("string table"));
14129
14130 filedata->string_table_length
14131 = filedata->string_table != NULL ? strs->sh_size : 0;
14132 }
14133 }
14134
14135 /* Locate the debug section. */
14136 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
14137 if (sec != NULL)
14138 section->name = section->uncompressed_name;
14139 else
14140 {
14141 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
14142 if (sec != NULL)
14143 section->name = section->compressed_name;
14144 }
14145 if (sec == NULL)
14146 return FALSE;
14147
14148 /* If we're loading from a subset of sections, and we've loaded
14149 a section matching this name before, it's likely that it's a
14150 different one. */
14151 if (section_subset != NULL)
14152 free_debug_section (debug);
14153
14154 return load_specific_debug_section (debug, sec, data);
14155 }
14156
14157 void
14158 free_debug_section (enum dwarf_section_display_enum debug)
14159 {
14160 struct dwarf_section * section = &debug_displays [debug].section;
14161
14162 if (section->start == NULL)
14163 return;
14164
14165 free ((char *) section->start);
14166 section->start = NULL;
14167 section->address = 0;
14168 section->size = 0;
14169 }
14170
14171 static bfd_boolean
14172 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
14173 {
14174 char * name = SECTION_NAME (section);
14175 const char * print_name = printable_section_name (filedata, section);
14176 bfd_size_type length;
14177 bfd_boolean result = TRUE;
14178 int i;
14179
14180 length = section->sh_size;
14181 if (length == 0)
14182 {
14183 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
14184 return TRUE;
14185 }
14186 if (section->sh_type == SHT_NOBITS)
14187 {
14188 /* There is no point in dumping the contents of a debugging section
14189 which has the NOBITS type - the bits in the file will be random.
14190 This can happen when a file containing a .eh_frame section is
14191 stripped with the --only-keep-debug command line option. */
14192 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
14193 print_name);
14194 return FALSE;
14195 }
14196
14197 if (const_strneq (name, ".gnu.linkonce.wi."))
14198 name = ".debug_info";
14199
14200 /* See if we know how to display the contents of this section. */
14201 for (i = 0; i < max; i++)
14202 {
14203 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
14204 struct dwarf_section_display * display = debug_displays + i;
14205 struct dwarf_section * sec = & display->section;
14206
14207 if (streq (sec->uncompressed_name, name)
14208 || (id == line && const_strneq (name, ".debug_line."))
14209 || streq (sec->compressed_name, name))
14210 {
14211 bfd_boolean secondary = (section != find_section (filedata, name));
14212
14213 if (secondary)
14214 free_debug_section (id);
14215
14216 if (i == line && const_strneq (name, ".debug_line."))
14217 sec->name = name;
14218 else if (streq (sec->uncompressed_name, name))
14219 sec->name = sec->uncompressed_name;
14220 else
14221 sec->name = sec->compressed_name;
14222
14223 if (load_specific_debug_section (id, section, filedata))
14224 {
14225 /* If this debug section is part of a CU/TU set in a .dwp file,
14226 restrict load_debug_section to the sections in that set. */
14227 section_subset = find_cu_tu_set (filedata, shndx);
14228
14229 result &= display->display (sec, filedata);
14230
14231 section_subset = NULL;
14232
14233 if (secondary || (id != info && id != abbrev))
14234 free_debug_section (id);
14235 }
14236 break;
14237 }
14238 }
14239
14240 if (i == max)
14241 {
14242 printf (_("Unrecognized debug section: %s\n"), print_name);
14243 result = FALSE;
14244 }
14245
14246 return result;
14247 }
14248
14249 /* Set DUMP_SECTS for all sections where dumps were requested
14250 based on section name. */
14251
14252 static void
14253 initialise_dumps_byname (Filedata * filedata)
14254 {
14255 struct dump_list_entry * cur;
14256
14257 for (cur = dump_sects_byname; cur; cur = cur->next)
14258 {
14259 unsigned int i;
14260 bfd_boolean any = FALSE;
14261
14262 for (i = 0; i < filedata->file_header.e_shnum; i++)
14263 if (streq (SECTION_NAME (filedata->section_headers + i), cur->name))
14264 {
14265 request_dump_bynumber (filedata, i, cur->type);
14266 any = TRUE;
14267 }
14268
14269 if (!any)
14270 warn (_("Section '%s' was not dumped because it does not exist!\n"),
14271 cur->name);
14272 }
14273 }
14274
14275 static bfd_boolean
14276 process_section_contents (Filedata * filedata)
14277 {
14278 Elf_Internal_Shdr * section;
14279 unsigned int i;
14280 bfd_boolean res = TRUE;
14281
14282 if (! do_dump)
14283 return TRUE;
14284
14285 initialise_dumps_byname (filedata);
14286
14287 for (i = 0, section = filedata->section_headers;
14288 i < filedata->file_header.e_shnum && i < filedata->num_dump_sects;
14289 i++, section++)
14290 {
14291 dump_type dump = filedata->dump_sects[i];
14292
14293 #ifdef SUPPORT_DISASSEMBLY
14294 if (dump & DISASS_DUMP)
14295 {
14296 if (! disassemble_section (section, filedata))
14297 res = FALSE;
14298 }
14299 #endif
14300 if (dump & HEX_DUMP)
14301 {
14302 if (! dump_section_as_bytes (section, filedata, FALSE))
14303 res = FALSE;
14304 }
14305
14306 if (dump & RELOC_DUMP)
14307 {
14308 if (! dump_section_as_bytes (section, filedata, TRUE))
14309 res = FALSE;
14310 }
14311
14312 if (dump & STRING_DUMP)
14313 {
14314 if (! dump_section_as_strings (section, filedata))
14315 res = FALSE;
14316 }
14317
14318 if (dump & DEBUG_DUMP)
14319 {
14320 if (! display_debug_section (i, section, filedata))
14321 res = FALSE;
14322 }
14323
14324 #ifdef HAVE_LIBCTF
14325 if (dump & CTF_DUMP)
14326 {
14327 if (! dump_section_as_ctf (section, filedata))
14328 res = FALSE;
14329 }
14330 #endif
14331 }
14332
14333 /* Check to see if the user requested a
14334 dump of a section that does not exist. */
14335 while (i < filedata->num_dump_sects)
14336 {
14337 if (filedata->dump_sects[i])
14338 {
14339 warn (_("Section %d was not dumped because it does not exist!\n"), i);
14340 res = FALSE;
14341 }
14342 i++;
14343 }
14344
14345 return res;
14346 }
14347
14348 static void
14349 process_mips_fpe_exception (int mask)
14350 {
14351 if (mask)
14352 {
14353 bfd_boolean first = TRUE;
14354
14355 if (mask & OEX_FPU_INEX)
14356 fputs ("INEX", stdout), first = FALSE;
14357 if (mask & OEX_FPU_UFLO)
14358 printf ("%sUFLO", first ? "" : "|"), first = FALSE;
14359 if (mask & OEX_FPU_OFLO)
14360 printf ("%sOFLO", first ? "" : "|"), first = FALSE;
14361 if (mask & OEX_FPU_DIV0)
14362 printf ("%sDIV0", first ? "" : "|"), first = FALSE;
14363 if (mask & OEX_FPU_INVAL)
14364 printf ("%sINVAL", first ? "" : "|");
14365 }
14366 else
14367 fputs ("0", stdout);
14368 }
14369
14370 /* Display's the value of TAG at location P. If TAG is
14371 greater than 0 it is assumed to be an unknown tag, and
14372 a message is printed to this effect. Otherwise it is
14373 assumed that a message has already been printed.
14374
14375 If the bottom bit of TAG is set it assumed to have a
14376 string value, otherwise it is assumed to have an integer
14377 value.
14378
14379 Returns an updated P pointing to the first unread byte
14380 beyond the end of TAG's value.
14381
14382 Reads at or beyond END will not be made. */
14383
14384 static unsigned char *
14385 display_tag_value (signed int tag,
14386 unsigned char * p,
14387 const unsigned char * const end)
14388 {
14389 unsigned long val;
14390
14391 if (tag > 0)
14392 printf (" Tag_unknown_%d: ", tag);
14393
14394 if (p >= end)
14395 {
14396 warn (_("<corrupt tag>\n"));
14397 }
14398 else if (tag & 1)
14399 {
14400 /* PR 17531 file: 027-19978-0.004. */
14401 size_t maxlen = (end - p) - 1;
14402
14403 putchar ('"');
14404 if (maxlen > 0)
14405 {
14406 print_symbol ((int) maxlen, (const char *) p);
14407 p += strnlen ((char *) p, maxlen) + 1;
14408 }
14409 else
14410 {
14411 printf (_("<corrupt string tag>"));
14412 p = (unsigned char *) end;
14413 }
14414 printf ("\"\n");
14415 }
14416 else
14417 {
14418 unsigned int len;
14419
14420 val = read_uleb128 (p, &len, end);
14421 p += len;
14422 printf ("%ld (0x%lx)\n", val, val);
14423 }
14424
14425 assert (p <= end);
14426 return p;
14427 }
14428
14429 /* ARC ABI attributes section. */
14430
14431 static unsigned char *
14432 display_arc_attribute (unsigned char * p,
14433 const unsigned char * const end)
14434 {
14435 unsigned int tag;
14436 unsigned int len;
14437 unsigned int val;
14438
14439 tag = read_uleb128 (p, &len, end);
14440 p += len;
14441
14442 switch (tag)
14443 {
14444 case Tag_ARC_PCS_config:
14445 val = read_uleb128 (p, &len, end);
14446 p += len;
14447 printf (" Tag_ARC_PCS_config: ");
14448 switch (val)
14449 {
14450 case 0:
14451 printf (_("Absent/Non standard\n"));
14452 break;
14453 case 1:
14454 printf (_("Bare metal/mwdt\n"));
14455 break;
14456 case 2:
14457 printf (_("Bare metal/newlib\n"));
14458 break;
14459 case 3:
14460 printf (_("Linux/uclibc\n"));
14461 break;
14462 case 4:
14463 printf (_("Linux/glibc\n"));
14464 break;
14465 default:
14466 printf (_("Unknown\n"));
14467 break;
14468 }
14469 break;
14470
14471 case Tag_ARC_CPU_base:
14472 val = read_uleb128 (p, &len, end);
14473 p += len;
14474 printf (" Tag_ARC_CPU_base: ");
14475 switch (val)
14476 {
14477 default:
14478 case TAG_CPU_NONE:
14479 printf (_("Absent\n"));
14480 break;
14481 case TAG_CPU_ARC6xx:
14482 printf ("ARC6xx\n");
14483 break;
14484 case TAG_CPU_ARC7xx:
14485 printf ("ARC7xx\n");
14486 break;
14487 case TAG_CPU_ARCEM:
14488 printf ("ARCEM\n");
14489 break;
14490 case TAG_CPU_ARCHS:
14491 printf ("ARCHS\n");
14492 break;
14493 }
14494 break;
14495
14496 case Tag_ARC_CPU_variation:
14497 val = read_uleb128 (p, &len, end);
14498 p += len;
14499 printf (" Tag_ARC_CPU_variation: ");
14500 switch (val)
14501 {
14502 default:
14503 if (val > 0 && val < 16)
14504 printf ("Core%d\n", val);
14505 else
14506 printf ("Unknown\n");
14507 break;
14508
14509 case 0:
14510 printf (_("Absent\n"));
14511 break;
14512 }
14513 break;
14514
14515 case Tag_ARC_CPU_name:
14516 printf (" Tag_ARC_CPU_name: ");
14517 p = display_tag_value (-1, p, end);
14518 break;
14519
14520 case Tag_ARC_ABI_rf16:
14521 val = read_uleb128 (p, &len, end);
14522 p += len;
14523 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
14524 break;
14525
14526 case Tag_ARC_ABI_osver:
14527 val = read_uleb128 (p, &len, end);
14528 p += len;
14529 printf (" Tag_ARC_ABI_osver: v%d\n", val);
14530 break;
14531
14532 case Tag_ARC_ABI_pic:
14533 case Tag_ARC_ABI_sda:
14534 val = read_uleb128 (p, &len, end);
14535 p += len;
14536 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
14537 : " Tag_ARC_ABI_pic: ");
14538 switch (val)
14539 {
14540 case 0:
14541 printf (_("Absent\n"));
14542 break;
14543 case 1:
14544 printf ("MWDT\n");
14545 break;
14546 case 2:
14547 printf ("GNU\n");
14548 break;
14549 default:
14550 printf (_("Unknown\n"));
14551 break;
14552 }
14553 break;
14554
14555 case Tag_ARC_ABI_tls:
14556 val = read_uleb128 (p, &len, end);
14557 p += len;
14558 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
14559 break;
14560
14561 case Tag_ARC_ABI_enumsize:
14562 val = read_uleb128 (p, &len, end);
14563 p += len;
14564 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
14565 _("smallest"));
14566 break;
14567
14568 case Tag_ARC_ABI_exceptions:
14569 val = read_uleb128 (p, &len, end);
14570 p += len;
14571 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
14572 : _("default"));
14573 break;
14574
14575 case Tag_ARC_ABI_double_size:
14576 val = read_uleb128 (p, &len, end);
14577 p += len;
14578 printf (" Tag_ARC_ABI_double_size: %d\n", val);
14579 break;
14580
14581 case Tag_ARC_ISA_config:
14582 printf (" Tag_ARC_ISA_config: ");
14583 p = display_tag_value (-1, p, end);
14584 break;
14585
14586 case Tag_ARC_ISA_apex:
14587 printf (" Tag_ARC_ISA_apex: ");
14588 p = display_tag_value (-1, p, end);
14589 break;
14590
14591 case Tag_ARC_ISA_mpy_option:
14592 val = read_uleb128 (p, &len, end);
14593 p += len;
14594 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
14595 break;
14596
14597 case Tag_ARC_ATR_version:
14598 val = read_uleb128 (p, &len, end);
14599 p += len;
14600 printf (" Tag_ARC_ATR_version: %d\n", val);
14601 break;
14602
14603 default:
14604 return display_tag_value (tag & 1, p, end);
14605 }
14606
14607 return p;
14608 }
14609
14610 /* ARM EABI attributes section. */
14611 typedef struct
14612 {
14613 unsigned int tag;
14614 const char * name;
14615 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
14616 unsigned int type;
14617 const char ** table;
14618 } arm_attr_public_tag;
14619
14620 static const char * arm_attr_tag_CPU_arch[] =
14621 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
14622 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
14623 "v8-M.mainline", "", "", "", "v8.1-M.mainline"};
14624 static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
14625 static const char * arm_attr_tag_THUMB_ISA_use[] =
14626 {"No", "Thumb-1", "Thumb-2", "Yes"};
14627 static const char * arm_attr_tag_FP_arch[] =
14628 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
14629 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
14630 static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
14631 static const char * arm_attr_tag_Advanced_SIMD_arch[] =
14632 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
14633 "NEON for ARMv8.1"};
14634 static const char * arm_attr_tag_PCS_config[] =
14635 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
14636 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
14637 static const char * arm_attr_tag_ABI_PCS_R9_use[] =
14638 {"V6", "SB", "TLS", "Unused"};
14639 static const char * arm_attr_tag_ABI_PCS_RW_data[] =
14640 {"Absolute", "PC-relative", "SB-relative", "None"};
14641 static const char * arm_attr_tag_ABI_PCS_RO_data[] =
14642 {"Absolute", "PC-relative", "None"};
14643 static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
14644 {"None", "direct", "GOT-indirect"};
14645 static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
14646 {"None", "??? 1", "2", "??? 3", "4"};
14647 static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
14648 static const char * arm_attr_tag_ABI_FP_denormal[] =
14649 {"Unused", "Needed", "Sign only"};
14650 static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
14651 static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
14652 static const char * arm_attr_tag_ABI_FP_number_model[] =
14653 {"Unused", "Finite", "RTABI", "IEEE 754"};
14654 static const char * arm_attr_tag_ABI_enum_size[] =
14655 {"Unused", "small", "int", "forced to int"};
14656 static const char * arm_attr_tag_ABI_HardFP_use[] =
14657 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
14658 static const char * arm_attr_tag_ABI_VFP_args[] =
14659 {"AAPCS", "VFP registers", "custom", "compatible"};
14660 static const char * arm_attr_tag_ABI_WMMX_args[] =
14661 {"AAPCS", "WMMX registers", "custom"};
14662 static const char * arm_attr_tag_ABI_optimization_goals[] =
14663 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
14664 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
14665 static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
14666 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
14667 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
14668 static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
14669 static const char * arm_attr_tag_FP_HP_extension[] =
14670 {"Not Allowed", "Allowed"};
14671 static const char * arm_attr_tag_ABI_FP_16bit_format[] =
14672 {"None", "IEEE 754", "Alternative Format"};
14673 static const char * arm_attr_tag_DSP_extension[] =
14674 {"Follow architecture", "Allowed"};
14675 static const char * arm_attr_tag_MPextension_use[] =
14676 {"Not Allowed", "Allowed"};
14677 static const char * arm_attr_tag_DIV_use[] =
14678 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
14679 "Allowed in v7-A with integer division extension"};
14680 static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
14681 static const char * arm_attr_tag_Virtualization_use[] =
14682 {"Not Allowed", "TrustZone", "Virtualization Extensions",
14683 "TrustZone and Virtualization Extensions"};
14684 static const char * arm_attr_tag_MPextension_use_legacy[] =
14685 {"Not Allowed", "Allowed"};
14686
14687 static const char * arm_attr_tag_MVE_arch[] =
14688 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
14689
14690 #define LOOKUP(id, name) \
14691 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
14692 static arm_attr_public_tag arm_attr_public_tags[] =
14693 {
14694 {4, "CPU_raw_name", 1, NULL},
14695 {5, "CPU_name", 1, NULL},
14696 LOOKUP(6, CPU_arch),
14697 {7, "CPU_arch_profile", 0, NULL},
14698 LOOKUP(8, ARM_ISA_use),
14699 LOOKUP(9, THUMB_ISA_use),
14700 LOOKUP(10, FP_arch),
14701 LOOKUP(11, WMMX_arch),
14702 LOOKUP(12, Advanced_SIMD_arch),
14703 LOOKUP(13, PCS_config),
14704 LOOKUP(14, ABI_PCS_R9_use),
14705 LOOKUP(15, ABI_PCS_RW_data),
14706 LOOKUP(16, ABI_PCS_RO_data),
14707 LOOKUP(17, ABI_PCS_GOT_use),
14708 LOOKUP(18, ABI_PCS_wchar_t),
14709 LOOKUP(19, ABI_FP_rounding),
14710 LOOKUP(20, ABI_FP_denormal),
14711 LOOKUP(21, ABI_FP_exceptions),
14712 LOOKUP(22, ABI_FP_user_exceptions),
14713 LOOKUP(23, ABI_FP_number_model),
14714 {24, "ABI_align_needed", 0, NULL},
14715 {25, "ABI_align_preserved", 0, NULL},
14716 LOOKUP(26, ABI_enum_size),
14717 LOOKUP(27, ABI_HardFP_use),
14718 LOOKUP(28, ABI_VFP_args),
14719 LOOKUP(29, ABI_WMMX_args),
14720 LOOKUP(30, ABI_optimization_goals),
14721 LOOKUP(31, ABI_FP_optimization_goals),
14722 {32, "compatibility", 0, NULL},
14723 LOOKUP(34, CPU_unaligned_access),
14724 LOOKUP(36, FP_HP_extension),
14725 LOOKUP(38, ABI_FP_16bit_format),
14726 LOOKUP(42, MPextension_use),
14727 LOOKUP(44, DIV_use),
14728 LOOKUP(46, DSP_extension),
14729 LOOKUP(48, MVE_arch),
14730 {64, "nodefaults", 0, NULL},
14731 {65, "also_compatible_with", 0, NULL},
14732 LOOKUP(66, T2EE_use),
14733 {67, "conformance", 1, NULL},
14734 LOOKUP(68, Virtualization_use),
14735 LOOKUP(70, MPextension_use_legacy)
14736 };
14737 #undef LOOKUP
14738
14739 static unsigned char *
14740 display_arm_attribute (unsigned char * p,
14741 const unsigned char * const end)
14742 {
14743 unsigned int tag;
14744 unsigned int len;
14745 unsigned int val;
14746 arm_attr_public_tag * attr;
14747 unsigned i;
14748 unsigned int type;
14749
14750 tag = read_uleb128 (p, &len, end);
14751 p += len;
14752 attr = NULL;
14753 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
14754 {
14755 if (arm_attr_public_tags[i].tag == tag)
14756 {
14757 attr = &arm_attr_public_tags[i];
14758 break;
14759 }
14760 }
14761
14762 if (attr)
14763 {
14764 printf (" Tag_%s: ", attr->name);
14765 switch (attr->type)
14766 {
14767 case 0:
14768 switch (tag)
14769 {
14770 case 7: /* Tag_CPU_arch_profile. */
14771 val = read_uleb128 (p, &len, end);
14772 p += len;
14773 switch (val)
14774 {
14775 case 0: printf (_("None\n")); break;
14776 case 'A': printf (_("Application\n")); break;
14777 case 'R': printf (_("Realtime\n")); break;
14778 case 'M': printf (_("Microcontroller\n")); break;
14779 case 'S': printf (_("Application or Realtime\n")); break;
14780 default: printf ("??? (%d)\n", val); break;
14781 }
14782 break;
14783
14784 case 24: /* Tag_align_needed. */
14785 val = read_uleb128 (p, &len, end);
14786 p += len;
14787 switch (val)
14788 {
14789 case 0: printf (_("None\n")); break;
14790 case 1: printf (_("8-byte\n")); break;
14791 case 2: printf (_("4-byte\n")); break;
14792 case 3: printf ("??? 3\n"); break;
14793 default:
14794 if (val <= 12)
14795 printf (_("8-byte and up to %d-byte extended\n"),
14796 1 << val);
14797 else
14798 printf ("??? (%d)\n", val);
14799 break;
14800 }
14801 break;
14802
14803 case 25: /* Tag_align_preserved. */
14804 val = read_uleb128 (p, &len, end);
14805 p += len;
14806 switch (val)
14807 {
14808 case 0: printf (_("None\n")); break;
14809 case 1: printf (_("8-byte, except leaf SP\n")); break;
14810 case 2: printf (_("8-byte\n")); break;
14811 case 3: printf ("??? 3\n"); break;
14812 default:
14813 if (val <= 12)
14814 printf (_("8-byte and up to %d-byte extended\n"),
14815 1 << val);
14816 else
14817 printf ("??? (%d)\n", val);
14818 break;
14819 }
14820 break;
14821
14822 case 32: /* Tag_compatibility. */
14823 {
14824 val = read_uleb128 (p, &len, end);
14825 p += len;
14826 printf (_("flag = %d, vendor = "), val);
14827 if (p < end - 1)
14828 {
14829 size_t maxlen = (end - p) - 1;
14830
14831 print_symbol ((int) maxlen, (const char *) p);
14832 p += strnlen ((char *) p, maxlen) + 1;
14833 }
14834 else
14835 {
14836 printf (_("<corrupt>"));
14837 p = (unsigned char *) end;
14838 }
14839 putchar ('\n');
14840 }
14841 break;
14842
14843 case 64: /* Tag_nodefaults. */
14844 /* PR 17531: file: 001-505008-0.01. */
14845 if (p < end)
14846 p++;
14847 printf (_("True\n"));
14848 break;
14849
14850 case 65: /* Tag_also_compatible_with. */
14851 val = read_uleb128 (p, &len, end);
14852 p += len;
14853 if (val == 6 /* Tag_CPU_arch. */)
14854 {
14855 val = read_uleb128 (p, &len, end);
14856 p += len;
14857 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
14858 printf ("??? (%d)\n", val);
14859 else
14860 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
14861 }
14862 else
14863 printf ("???\n");
14864 while (p < end && *(p++) != '\0' /* NUL terminator. */)
14865 ;
14866 break;
14867
14868 default:
14869 printf (_("<unknown: %d>\n"), tag);
14870 break;
14871 }
14872 return p;
14873
14874 case 1:
14875 return display_tag_value (-1, p, end);
14876 case 2:
14877 return display_tag_value (0, p, end);
14878
14879 default:
14880 assert (attr->type & 0x80);
14881 val = read_uleb128 (p, &len, end);
14882 p += len;
14883 type = attr->type & 0x7f;
14884 if (val >= type)
14885 printf ("??? (%d)\n", val);
14886 else
14887 printf ("%s\n", attr->table[val]);
14888 return p;
14889 }
14890 }
14891
14892 return display_tag_value (tag, p, end);
14893 }
14894
14895 static unsigned char *
14896 display_gnu_attribute (unsigned char * p,
14897 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
14898 const unsigned char * const end)
14899 {
14900 int tag;
14901 unsigned int len;
14902 unsigned int val;
14903
14904 tag = read_uleb128 (p, &len, end);
14905 p += len;
14906
14907 /* Tag_compatibility is the only generic GNU attribute defined at
14908 present. */
14909 if (tag == 32)
14910 {
14911 val = read_uleb128 (p, &len, end);
14912 p += len;
14913
14914 printf (_("flag = %d, vendor = "), val);
14915 if (p == end)
14916 {
14917 printf (_("<corrupt>\n"));
14918 warn (_("corrupt vendor attribute\n"));
14919 }
14920 else
14921 {
14922 if (p < end - 1)
14923 {
14924 size_t maxlen = (end - p) - 1;
14925
14926 print_symbol ((int) maxlen, (const char *) p);
14927 p += strnlen ((char *) p, maxlen) + 1;
14928 }
14929 else
14930 {
14931 printf (_("<corrupt>"));
14932 p = (unsigned char *) end;
14933 }
14934 putchar ('\n');
14935 }
14936 return p;
14937 }
14938
14939 if ((tag & 2) == 0 && display_proc_gnu_attribute)
14940 return display_proc_gnu_attribute (p, tag, end);
14941
14942 return display_tag_value (tag, p, end);
14943 }
14944
14945 static unsigned char *
14946 display_power_gnu_attribute (unsigned char * p,
14947 unsigned int tag,
14948 const unsigned char * const end)
14949 {
14950 unsigned int len;
14951 unsigned int val;
14952
14953 if (tag == Tag_GNU_Power_ABI_FP)
14954 {
14955 val = read_uleb128 (p, &len, end);
14956 p += len;
14957 printf (" Tag_GNU_Power_ABI_FP: ");
14958 if (len == 0)
14959 {
14960 printf (_("<corrupt>\n"));
14961 return p;
14962 }
14963
14964 if (val > 15)
14965 printf ("(%#x), ", val);
14966
14967 switch (val & 3)
14968 {
14969 case 0:
14970 printf (_("unspecified hard/soft float, "));
14971 break;
14972 case 1:
14973 printf (_("hard float, "));
14974 break;
14975 case 2:
14976 printf (_("soft float, "));
14977 break;
14978 case 3:
14979 printf (_("single-precision hard float, "));
14980 break;
14981 }
14982
14983 switch (val & 0xC)
14984 {
14985 case 0:
14986 printf (_("unspecified long double\n"));
14987 break;
14988 case 4:
14989 printf (_("128-bit IBM long double\n"));
14990 break;
14991 case 8:
14992 printf (_("64-bit long double\n"));
14993 break;
14994 case 12:
14995 printf (_("128-bit IEEE long double\n"));
14996 break;
14997 }
14998 return p;
14999 }
15000
15001 if (tag == Tag_GNU_Power_ABI_Vector)
15002 {
15003 val = read_uleb128 (p, &len, end);
15004 p += len;
15005 printf (" Tag_GNU_Power_ABI_Vector: ");
15006 if (len == 0)
15007 {
15008 printf (_("<corrupt>\n"));
15009 return p;
15010 }
15011
15012 if (val > 3)
15013 printf ("(%#x), ", val);
15014
15015 switch (val & 3)
15016 {
15017 case 0:
15018 printf (_("unspecified\n"));
15019 break;
15020 case 1:
15021 printf (_("generic\n"));
15022 break;
15023 case 2:
15024 printf ("AltiVec\n");
15025 break;
15026 case 3:
15027 printf ("SPE\n");
15028 break;
15029 }
15030 return p;
15031 }
15032
15033 if (tag == Tag_GNU_Power_ABI_Struct_Return)
15034 {
15035 val = read_uleb128 (p, &len, end);
15036 p += len;
15037 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
15038 if (len == 0)
15039 {
15040 printf (_("<corrupt>\n"));
15041 return p;
15042 }
15043
15044 if (val > 2)
15045 printf ("(%#x), ", val);
15046
15047 switch (val & 3)
15048 {
15049 case 0:
15050 printf (_("unspecified\n"));
15051 break;
15052 case 1:
15053 printf ("r3/r4\n");
15054 break;
15055 case 2:
15056 printf (_("memory\n"));
15057 break;
15058 case 3:
15059 printf ("???\n");
15060 break;
15061 }
15062 return p;
15063 }
15064
15065 return display_tag_value (tag & 1, p, end);
15066 }
15067
15068 static unsigned char *
15069 display_s390_gnu_attribute (unsigned char * p,
15070 unsigned int tag,
15071 const unsigned char * const end)
15072 {
15073 unsigned int len;
15074 int val;
15075
15076 if (tag == Tag_GNU_S390_ABI_Vector)
15077 {
15078 val = read_uleb128 (p, &len, end);
15079 p += len;
15080 printf (" Tag_GNU_S390_ABI_Vector: ");
15081
15082 switch (val)
15083 {
15084 case 0:
15085 printf (_("any\n"));
15086 break;
15087 case 1:
15088 printf (_("software\n"));
15089 break;
15090 case 2:
15091 printf (_("hardware\n"));
15092 break;
15093 default:
15094 printf ("??? (%d)\n", val);
15095 break;
15096 }
15097 return p;
15098 }
15099
15100 return display_tag_value (tag & 1, p, end);
15101 }
15102
15103 static void
15104 display_sparc_hwcaps (unsigned int mask)
15105 {
15106 if (mask)
15107 {
15108 bfd_boolean first = TRUE;
15109
15110 if (mask & ELF_SPARC_HWCAP_MUL32)
15111 fputs ("mul32", stdout), first = FALSE;
15112 if (mask & ELF_SPARC_HWCAP_DIV32)
15113 printf ("%sdiv32", first ? "" : "|"), first = FALSE;
15114 if (mask & ELF_SPARC_HWCAP_FSMULD)
15115 printf ("%sfsmuld", first ? "" : "|"), first = FALSE;
15116 if (mask & ELF_SPARC_HWCAP_V8PLUS)
15117 printf ("%sv8plus", first ? "" : "|"), first = FALSE;
15118 if (mask & ELF_SPARC_HWCAP_POPC)
15119 printf ("%spopc", first ? "" : "|"), first = FALSE;
15120 if (mask & ELF_SPARC_HWCAP_VIS)
15121 printf ("%svis", first ? "" : "|"), first = FALSE;
15122 if (mask & ELF_SPARC_HWCAP_VIS2)
15123 printf ("%svis2", first ? "" : "|"), first = FALSE;
15124 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
15125 printf ("%sASIBlkInit", first ? "" : "|"), first = FALSE;
15126 if (mask & ELF_SPARC_HWCAP_FMAF)
15127 printf ("%sfmaf", first ? "" : "|"), first = FALSE;
15128 if (mask & ELF_SPARC_HWCAP_VIS3)
15129 printf ("%svis3", first ? "" : "|"), first = FALSE;
15130 if (mask & ELF_SPARC_HWCAP_HPC)
15131 printf ("%shpc", first ? "" : "|"), first = FALSE;
15132 if (mask & ELF_SPARC_HWCAP_RANDOM)
15133 printf ("%srandom", first ? "" : "|"), first = FALSE;
15134 if (mask & ELF_SPARC_HWCAP_TRANS)
15135 printf ("%strans", first ? "" : "|"), first = FALSE;
15136 if (mask & ELF_SPARC_HWCAP_FJFMAU)
15137 printf ("%sfjfmau", first ? "" : "|"), first = FALSE;
15138 if (mask & ELF_SPARC_HWCAP_IMA)
15139 printf ("%sima", first ? "" : "|"), first = FALSE;
15140 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
15141 printf ("%scspare", first ? "" : "|"), first = FALSE;
15142 }
15143 else
15144 fputc ('0', stdout);
15145 fputc ('\n', stdout);
15146 }
15147
15148 static void
15149 display_sparc_hwcaps2 (unsigned int mask)
15150 {
15151 if (mask)
15152 {
15153 bfd_boolean first = TRUE;
15154
15155 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
15156 fputs ("fjathplus", stdout), first = FALSE;
15157 if (mask & ELF_SPARC_HWCAP2_VIS3B)
15158 printf ("%svis3b", first ? "" : "|"), first = FALSE;
15159 if (mask & ELF_SPARC_HWCAP2_ADP)
15160 printf ("%sadp", first ? "" : "|"), first = FALSE;
15161 if (mask & ELF_SPARC_HWCAP2_SPARC5)
15162 printf ("%ssparc5", first ? "" : "|"), first = FALSE;
15163 if (mask & ELF_SPARC_HWCAP2_MWAIT)
15164 printf ("%smwait", first ? "" : "|"), first = FALSE;
15165 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
15166 printf ("%sxmpmul", first ? "" : "|"), first = FALSE;
15167 if (mask & ELF_SPARC_HWCAP2_XMONT)
15168 printf ("%sxmont2", first ? "" : "|"), first = FALSE;
15169 if (mask & ELF_SPARC_HWCAP2_NSEC)
15170 printf ("%snsec", first ? "" : "|"), first = FALSE;
15171 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
15172 printf ("%sfjathhpc", first ? "" : "|"), first = FALSE;
15173 if (mask & ELF_SPARC_HWCAP2_FJDES)
15174 printf ("%sfjdes", first ? "" : "|"), first = FALSE;
15175 if (mask & ELF_SPARC_HWCAP2_FJAES)
15176 printf ("%sfjaes", first ? "" : "|"), first = FALSE;
15177 }
15178 else
15179 fputc ('0', stdout);
15180 fputc ('\n', stdout);
15181 }
15182
15183 static unsigned char *
15184 display_sparc_gnu_attribute (unsigned char * p,
15185 unsigned int tag,
15186 const unsigned char * const end)
15187 {
15188 unsigned int len;
15189 int val;
15190
15191 if (tag == Tag_GNU_Sparc_HWCAPS)
15192 {
15193 val = read_uleb128 (p, &len, end);
15194 p += len;
15195 printf (" Tag_GNU_Sparc_HWCAPS: ");
15196 display_sparc_hwcaps (val);
15197 return p;
15198 }
15199 if (tag == Tag_GNU_Sparc_HWCAPS2)
15200 {
15201 val = read_uleb128 (p, &len, end);
15202 p += len;
15203 printf (" Tag_GNU_Sparc_HWCAPS2: ");
15204 display_sparc_hwcaps2 (val);
15205 return p;
15206 }
15207
15208 return display_tag_value (tag, p, end);
15209 }
15210
15211 static void
15212 print_mips_fp_abi_value (unsigned int val)
15213 {
15214 switch (val)
15215 {
15216 case Val_GNU_MIPS_ABI_FP_ANY:
15217 printf (_("Hard or soft float\n"));
15218 break;
15219 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15220 printf (_("Hard float (double precision)\n"));
15221 break;
15222 case Val_GNU_MIPS_ABI_FP_SINGLE:
15223 printf (_("Hard float (single precision)\n"));
15224 break;
15225 case Val_GNU_MIPS_ABI_FP_SOFT:
15226 printf (_("Soft float\n"));
15227 break;
15228 case Val_GNU_MIPS_ABI_FP_OLD_64:
15229 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
15230 break;
15231 case Val_GNU_MIPS_ABI_FP_XX:
15232 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
15233 break;
15234 case Val_GNU_MIPS_ABI_FP_64:
15235 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
15236 break;
15237 case Val_GNU_MIPS_ABI_FP_64A:
15238 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
15239 break;
15240 case Val_GNU_MIPS_ABI_FP_NAN2008:
15241 printf (_("NaN 2008 compatibility\n"));
15242 break;
15243 default:
15244 printf ("??? (%d)\n", val);
15245 break;
15246 }
15247 }
15248
15249 static unsigned char *
15250 display_mips_gnu_attribute (unsigned char * p,
15251 unsigned int tag,
15252 const unsigned char * const end)
15253 {
15254 if (tag == Tag_GNU_MIPS_ABI_FP)
15255 {
15256 unsigned int len;
15257 unsigned int val;
15258
15259 val = read_uleb128 (p, &len, end);
15260 p += len;
15261 printf (" Tag_GNU_MIPS_ABI_FP: ");
15262
15263 print_mips_fp_abi_value (val);
15264
15265 return p;
15266 }
15267
15268 if (tag == Tag_GNU_MIPS_ABI_MSA)
15269 {
15270 unsigned int len;
15271 unsigned int val;
15272
15273 val = read_uleb128 (p, &len, end);
15274 p += len;
15275 printf (" Tag_GNU_MIPS_ABI_MSA: ");
15276
15277 switch (val)
15278 {
15279 case Val_GNU_MIPS_ABI_MSA_ANY:
15280 printf (_("Any MSA or not\n"));
15281 break;
15282 case Val_GNU_MIPS_ABI_MSA_128:
15283 printf (_("128-bit MSA\n"));
15284 break;
15285 default:
15286 printf ("??? (%d)\n", val);
15287 break;
15288 }
15289 return p;
15290 }
15291
15292 return display_tag_value (tag & 1, p, end);
15293 }
15294
15295 static unsigned char *
15296 display_tic6x_attribute (unsigned char * p,
15297 const unsigned char * const end)
15298 {
15299 unsigned int tag;
15300 unsigned int len;
15301 int val;
15302
15303 tag = read_uleb128 (p, &len, end);
15304 p += len;
15305
15306 switch (tag)
15307 {
15308 case Tag_ISA:
15309 val = read_uleb128 (p, &len, end);
15310 p += len;
15311 printf (" Tag_ISA: ");
15312
15313 switch (val)
15314 {
15315 case C6XABI_Tag_ISA_none:
15316 printf (_("None\n"));
15317 break;
15318 case C6XABI_Tag_ISA_C62X:
15319 printf ("C62x\n");
15320 break;
15321 case C6XABI_Tag_ISA_C67X:
15322 printf ("C67x\n");
15323 break;
15324 case C6XABI_Tag_ISA_C67XP:
15325 printf ("C67x+\n");
15326 break;
15327 case C6XABI_Tag_ISA_C64X:
15328 printf ("C64x\n");
15329 break;
15330 case C6XABI_Tag_ISA_C64XP:
15331 printf ("C64x+\n");
15332 break;
15333 case C6XABI_Tag_ISA_C674X:
15334 printf ("C674x\n");
15335 break;
15336 default:
15337 printf ("??? (%d)\n", val);
15338 break;
15339 }
15340 return p;
15341
15342 case Tag_ABI_wchar_t:
15343 val = read_uleb128 (p, &len, end);
15344 p += len;
15345 printf (" Tag_ABI_wchar_t: ");
15346 switch (val)
15347 {
15348 case 0:
15349 printf (_("Not used\n"));
15350 break;
15351 case 1:
15352 printf (_("2 bytes\n"));
15353 break;
15354 case 2:
15355 printf (_("4 bytes\n"));
15356 break;
15357 default:
15358 printf ("??? (%d)\n", val);
15359 break;
15360 }
15361 return p;
15362
15363 case Tag_ABI_stack_align_needed:
15364 val = read_uleb128 (p, &len, end);
15365 p += len;
15366 printf (" Tag_ABI_stack_align_needed: ");
15367 switch (val)
15368 {
15369 case 0:
15370 printf (_("8-byte\n"));
15371 break;
15372 case 1:
15373 printf (_("16-byte\n"));
15374 break;
15375 default:
15376 printf ("??? (%d)\n", val);
15377 break;
15378 }
15379 return p;
15380
15381 case Tag_ABI_stack_align_preserved:
15382 val = read_uleb128 (p, &len, end);
15383 p += len;
15384 printf (" Tag_ABI_stack_align_preserved: ");
15385 switch (val)
15386 {
15387 case 0:
15388 printf (_("8-byte\n"));
15389 break;
15390 case 1:
15391 printf (_("16-byte\n"));
15392 break;
15393 default:
15394 printf ("??? (%d)\n", val);
15395 break;
15396 }
15397 return p;
15398
15399 case Tag_ABI_DSBT:
15400 val = read_uleb128 (p, &len, end);
15401 p += len;
15402 printf (" Tag_ABI_DSBT: ");
15403 switch (val)
15404 {
15405 case 0:
15406 printf (_("DSBT addressing not used\n"));
15407 break;
15408 case 1:
15409 printf (_("DSBT addressing used\n"));
15410 break;
15411 default:
15412 printf ("??? (%d)\n", val);
15413 break;
15414 }
15415 return p;
15416
15417 case Tag_ABI_PID:
15418 val = read_uleb128 (p, &len, end);
15419 p += len;
15420 printf (" Tag_ABI_PID: ");
15421 switch (val)
15422 {
15423 case 0:
15424 printf (_("Data addressing position-dependent\n"));
15425 break;
15426 case 1:
15427 printf (_("Data addressing position-independent, GOT near DP\n"));
15428 break;
15429 case 2:
15430 printf (_("Data addressing position-independent, GOT far from DP\n"));
15431 break;
15432 default:
15433 printf ("??? (%d)\n", val);
15434 break;
15435 }
15436 return p;
15437
15438 case Tag_ABI_PIC:
15439 val = read_uleb128 (p, &len, end);
15440 p += len;
15441 printf (" Tag_ABI_PIC: ");
15442 switch (val)
15443 {
15444 case 0:
15445 printf (_("Code addressing position-dependent\n"));
15446 break;
15447 case 1:
15448 printf (_("Code addressing position-independent\n"));
15449 break;
15450 default:
15451 printf ("??? (%d)\n", val);
15452 break;
15453 }
15454 return p;
15455
15456 case Tag_ABI_array_object_alignment:
15457 val = read_uleb128 (p, &len, end);
15458 p += len;
15459 printf (" Tag_ABI_array_object_alignment: ");
15460 switch (val)
15461 {
15462 case 0:
15463 printf (_("8-byte\n"));
15464 break;
15465 case 1:
15466 printf (_("4-byte\n"));
15467 break;
15468 case 2:
15469 printf (_("16-byte\n"));
15470 break;
15471 default:
15472 printf ("??? (%d)\n", val);
15473 break;
15474 }
15475 return p;
15476
15477 case Tag_ABI_array_object_align_expected:
15478 val = read_uleb128 (p, &len, end);
15479 p += len;
15480 printf (" Tag_ABI_array_object_align_expected: ");
15481 switch (val)
15482 {
15483 case 0:
15484 printf (_("8-byte\n"));
15485 break;
15486 case 1:
15487 printf (_("4-byte\n"));
15488 break;
15489 case 2:
15490 printf (_("16-byte\n"));
15491 break;
15492 default:
15493 printf ("??? (%d)\n", val);
15494 break;
15495 }
15496 return p;
15497
15498 case Tag_ABI_compatibility:
15499 {
15500 val = read_uleb128 (p, &len, end);
15501 p += len;
15502 printf (" Tag_ABI_compatibility: ");
15503 printf (_("flag = %d, vendor = "), val);
15504 if (p < end - 1)
15505 {
15506 size_t maxlen = (end - p) - 1;
15507
15508 print_symbol ((int) maxlen, (const char *) p);
15509 p += strnlen ((char *) p, maxlen) + 1;
15510 }
15511 else
15512 {
15513 printf (_("<corrupt>"));
15514 p = (unsigned char *) end;
15515 }
15516 putchar ('\n');
15517 return p;
15518 }
15519
15520 case Tag_ABI_conformance:
15521 {
15522 printf (" Tag_ABI_conformance: \"");
15523 if (p < end - 1)
15524 {
15525 size_t maxlen = (end - p) - 1;
15526
15527 print_symbol ((int) maxlen, (const char *) p);
15528 p += strnlen ((char *) p, maxlen) + 1;
15529 }
15530 else
15531 {
15532 printf (_("<corrupt>"));
15533 p = (unsigned char *) end;
15534 }
15535 printf ("\"\n");
15536 return p;
15537 }
15538 }
15539
15540 return display_tag_value (tag, p, end);
15541 }
15542
15543 static void
15544 display_raw_attribute (unsigned char * p, unsigned char const * const end)
15545 {
15546 unsigned long addr = 0;
15547 size_t bytes = end - p;
15548
15549 assert (end >= p);
15550 while (bytes)
15551 {
15552 int j;
15553 int k;
15554 int lbytes = (bytes > 16 ? 16 : bytes);
15555
15556 printf (" 0x%8.8lx ", addr);
15557
15558 for (j = 0; j < 16; j++)
15559 {
15560 if (j < lbytes)
15561 printf ("%2.2x", p[j]);
15562 else
15563 printf (" ");
15564
15565 if ((j & 3) == 3)
15566 printf (" ");
15567 }
15568
15569 for (j = 0; j < lbytes; j++)
15570 {
15571 k = p[j];
15572 if (k >= ' ' && k < 0x7f)
15573 printf ("%c", k);
15574 else
15575 printf (".");
15576 }
15577
15578 putchar ('\n');
15579
15580 p += lbytes;
15581 bytes -= lbytes;
15582 addr += lbytes;
15583 }
15584
15585 putchar ('\n');
15586 }
15587
15588 static unsigned char *
15589 display_msp430x_attribute (unsigned char * p,
15590 const unsigned char * const end)
15591 {
15592 unsigned int len;
15593 unsigned int val;
15594 unsigned int tag;
15595
15596 tag = read_uleb128 (p, & len, end);
15597 p += len;
15598
15599 switch (tag)
15600 {
15601 case OFBA_MSPABI_Tag_ISA:
15602 val = read_uleb128 (p, &len, end);
15603 p += len;
15604 printf (" Tag_ISA: ");
15605 switch (val)
15606 {
15607 case 0: printf (_("None\n")); break;
15608 case 1: printf (_("MSP430\n")); break;
15609 case 2: printf (_("MSP430X\n")); break;
15610 default: printf ("??? (%d)\n", val); break;
15611 }
15612 break;
15613
15614 case OFBA_MSPABI_Tag_Code_Model:
15615 val = read_uleb128 (p, &len, end);
15616 p += len;
15617 printf (" Tag_Code_Model: ");
15618 switch (val)
15619 {
15620 case 0: printf (_("None\n")); break;
15621 case 1: printf (_("Small\n")); break;
15622 case 2: printf (_("Large\n")); break;
15623 default: printf ("??? (%d)\n", val); break;
15624 }
15625 break;
15626
15627 case OFBA_MSPABI_Tag_Data_Model:
15628 val = read_uleb128 (p, &len, end);
15629 p += len;
15630 printf (" Tag_Data_Model: ");
15631 switch (val)
15632 {
15633 case 0: printf (_("None\n")); break;
15634 case 1: printf (_("Small\n")); break;
15635 case 2: printf (_("Large\n")); break;
15636 case 3: printf (_("Restricted Large\n")); break;
15637 default: printf ("??? (%d)\n", val); break;
15638 }
15639 break;
15640
15641 default:
15642 printf (_(" <unknown tag %d>: "), tag);
15643
15644 if (tag & 1)
15645 {
15646 putchar ('"');
15647 if (p < end - 1)
15648 {
15649 size_t maxlen = (end - p) - 1;
15650
15651 print_symbol ((int) maxlen, (const char *) p);
15652 p += strnlen ((char *) p, maxlen) + 1;
15653 }
15654 else
15655 {
15656 printf (_("<corrupt>"));
15657 p = (unsigned char *) end;
15658 }
15659 printf ("\"\n");
15660 }
15661 else
15662 {
15663 val = read_uleb128 (p, &len, end);
15664 p += len;
15665 printf ("%d (0x%x)\n", val, val);
15666 }
15667 break;
15668 }
15669
15670 assert (p <= end);
15671 return p;
15672 }
15673
15674 struct riscv_attr_tag_t {
15675 const char *name;
15676 int tag;
15677 };
15678
15679 static struct riscv_attr_tag_t riscv_attr_tag[] =
15680 {
15681 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
15682 T(arch),
15683 T(priv_spec),
15684 T(priv_spec_minor),
15685 T(priv_spec_revision),
15686 T(unaligned_access),
15687 T(stack_align),
15688 #undef T
15689 };
15690
15691 static unsigned char *
15692 display_riscv_attribute (unsigned char *p,
15693 const unsigned char * const end)
15694 {
15695 unsigned int len;
15696 int val;
15697 int tag;
15698 struct riscv_attr_tag_t *attr = NULL;
15699 unsigned i;
15700
15701 tag = read_uleb128 (p, &len, end);
15702 p += len;
15703
15704 /* Find the name of attribute. */
15705 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
15706 {
15707 if (riscv_attr_tag[i].tag == tag)
15708 {
15709 attr = &riscv_attr_tag[i];
15710 break;
15711 }
15712 }
15713
15714 if (attr)
15715 printf (" %s: ", attr->name);
15716 else
15717 return display_tag_value (tag, p, end);
15718
15719 switch (tag)
15720 {
15721 case Tag_RISCV_priv_spec:
15722 case Tag_RISCV_priv_spec_minor:
15723 case Tag_RISCV_priv_spec_revision:
15724 val = read_uleb128 (p, &len, end);
15725 p += len;
15726 printf (_("%d\n"), val);
15727 break;
15728 case Tag_RISCV_unaligned_access:
15729 val = read_uleb128 (p, &len, end);
15730 p += len;
15731 switch (val)
15732 {
15733 case 0:
15734 printf (_("No unaligned access\n"));
15735 break;
15736 case 1:
15737 printf (_("Unaligned access\n"));
15738 break;
15739 }
15740 break;
15741 case Tag_RISCV_stack_align:
15742 val = read_uleb128 (p, &len, end);
15743 p += len;
15744 printf (_("%d-bytes\n"), val);
15745 break;
15746 case Tag_RISCV_arch:
15747 p = display_tag_value (-1, p, end);
15748 break;
15749 default:
15750 return display_tag_value (tag, p, end);
15751 }
15752
15753 return p;
15754 }
15755
15756 static bfd_boolean
15757 process_attributes (Filedata * filedata,
15758 const char * public_name,
15759 unsigned int proc_type,
15760 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
15761 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
15762 {
15763 Elf_Internal_Shdr * sect;
15764 unsigned i;
15765 bfd_boolean res = TRUE;
15766
15767 /* Find the section header so that we get the size. */
15768 for (i = 0, sect = filedata->section_headers;
15769 i < filedata->file_header.e_shnum;
15770 i++, sect++)
15771 {
15772 unsigned char * contents;
15773 unsigned char * p;
15774
15775 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
15776 continue;
15777
15778 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
15779 sect->sh_size, _("attributes"));
15780 if (contents == NULL)
15781 {
15782 res = FALSE;
15783 continue;
15784 }
15785
15786 p = contents;
15787 /* The first character is the version of the attributes.
15788 Currently only version 1, (aka 'A') is recognised here. */
15789 if (*p != 'A')
15790 {
15791 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
15792 res = FALSE;
15793 }
15794 else
15795 {
15796 bfd_vma section_len;
15797
15798 section_len = sect->sh_size - 1;
15799 p++;
15800
15801 while (section_len > 0)
15802 {
15803 bfd_vma attr_len;
15804 unsigned int namelen;
15805 bfd_boolean public_section;
15806 bfd_boolean gnu_section;
15807
15808 if (section_len <= 4)
15809 {
15810 error (_("Tag section ends prematurely\n"));
15811 res = FALSE;
15812 break;
15813 }
15814 attr_len = byte_get (p, 4);
15815 p += 4;
15816
15817 if (attr_len > section_len)
15818 {
15819 error (_("Bad attribute length (%u > %u)\n"),
15820 (unsigned) attr_len, (unsigned) section_len);
15821 attr_len = section_len;
15822 res = FALSE;
15823 }
15824 /* PR 17531: file: 001-101425-0.004 */
15825 else if (attr_len < 5)
15826 {
15827 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
15828 res = FALSE;
15829 break;
15830 }
15831
15832 section_len -= attr_len;
15833 attr_len -= 4;
15834
15835 namelen = strnlen ((char *) p, attr_len) + 1;
15836 if (namelen == 0 || namelen >= attr_len)
15837 {
15838 error (_("Corrupt attribute section name\n"));
15839 res = FALSE;
15840 break;
15841 }
15842
15843 printf (_("Attribute Section: "));
15844 print_symbol (INT_MAX, (const char *) p);
15845 putchar ('\n');
15846
15847 if (public_name && streq ((char *) p, public_name))
15848 public_section = TRUE;
15849 else
15850 public_section = FALSE;
15851
15852 if (streq ((char *) p, "gnu"))
15853 gnu_section = TRUE;
15854 else
15855 gnu_section = FALSE;
15856
15857 p += namelen;
15858 attr_len -= namelen;
15859
15860 while (attr_len > 0 && p < contents + sect->sh_size)
15861 {
15862 int tag;
15863 int val;
15864 bfd_vma size;
15865 unsigned char * end;
15866
15867 /* PR binutils/17531: Safe handling of corrupt files. */
15868 if (attr_len < 6)
15869 {
15870 error (_("Unused bytes at end of section\n"));
15871 res = FALSE;
15872 section_len = 0;
15873 break;
15874 }
15875
15876 tag = *(p++);
15877 size = byte_get (p, 4);
15878 if (size > attr_len)
15879 {
15880 error (_("Bad subsection length (%u > %u)\n"),
15881 (unsigned) size, (unsigned) attr_len);
15882 res = FALSE;
15883 size = attr_len;
15884 }
15885 /* PR binutils/17531: Safe handling of corrupt files. */
15886 if (size < 6)
15887 {
15888 error (_("Bad subsection length (%u < 6)\n"),
15889 (unsigned) size);
15890 res = FALSE;
15891 section_len = 0;
15892 break;
15893 }
15894
15895 attr_len -= size;
15896 end = p + size - 1;
15897 assert (end <= contents + sect->sh_size);
15898 p += 4;
15899
15900 switch (tag)
15901 {
15902 case 1:
15903 printf (_("File Attributes\n"));
15904 break;
15905 case 2:
15906 printf (_("Section Attributes:"));
15907 goto do_numlist;
15908 case 3:
15909 printf (_("Symbol Attributes:"));
15910 /* Fall through. */
15911 do_numlist:
15912 for (;;)
15913 {
15914 unsigned int j;
15915
15916 val = read_uleb128 (p, &j, end);
15917 p += j;
15918 if (val == 0)
15919 break;
15920 printf (" %d", val);
15921 }
15922 printf ("\n");
15923 break;
15924 default:
15925 printf (_("Unknown tag: %d\n"), tag);
15926 public_section = FALSE;
15927 break;
15928 }
15929
15930 if (public_section && display_pub_attribute != NULL)
15931 {
15932 while (p < end)
15933 p = display_pub_attribute (p, end);
15934 assert (p == end);
15935 }
15936 else if (gnu_section && display_proc_gnu_attribute != NULL)
15937 {
15938 while (p < end)
15939 p = display_gnu_attribute (p,
15940 display_proc_gnu_attribute,
15941 end);
15942 assert (p == end);
15943 }
15944 else if (p < end)
15945 {
15946 printf (_(" Unknown attribute:\n"));
15947 display_raw_attribute (p, end);
15948 p = end;
15949 }
15950 else
15951 attr_len = 0;
15952 }
15953 }
15954 }
15955
15956 free (contents);
15957 }
15958
15959 return res;
15960 }
15961
15962 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
15963 Print the Address, Access and Initial fields of an entry at VMA ADDR
15964 and return the VMA of the next entry, or -1 if there was a problem.
15965 Does not read from DATA_END or beyond. */
15966
15967 static bfd_vma
15968 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
15969 unsigned char * data_end)
15970 {
15971 printf (" ");
15972 print_vma (addr, LONG_HEX);
15973 printf (" ");
15974 if (addr < pltgot + 0xfff0)
15975 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
15976 else
15977 printf ("%10s", "");
15978 printf (" ");
15979 if (data == NULL)
15980 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
15981 else
15982 {
15983 bfd_vma entry;
15984 unsigned char * from = data + addr - pltgot;
15985
15986 if (from + (is_32bit_elf ? 4 : 8) > data_end)
15987 {
15988 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
15989 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
15990 return (bfd_vma) -1;
15991 }
15992 else
15993 {
15994 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
15995 print_vma (entry, LONG_HEX);
15996 }
15997 }
15998 return addr + (is_32bit_elf ? 4 : 8);
15999 }
16000
16001 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
16002 PLTGOT. Print the Address and Initial fields of an entry at VMA
16003 ADDR and return the VMA of the next entry. */
16004
16005 static bfd_vma
16006 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
16007 {
16008 printf (" ");
16009 print_vma (addr, LONG_HEX);
16010 printf (" ");
16011 if (data == NULL)
16012 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
16013 else
16014 {
16015 bfd_vma entry;
16016
16017 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
16018 print_vma (entry, LONG_HEX);
16019 }
16020 return addr + (is_32bit_elf ? 4 : 8);
16021 }
16022
16023 static void
16024 print_mips_ases (unsigned int mask)
16025 {
16026 if (mask & AFL_ASE_DSP)
16027 fputs ("\n\tDSP ASE", stdout);
16028 if (mask & AFL_ASE_DSPR2)
16029 fputs ("\n\tDSP R2 ASE", stdout);
16030 if (mask & AFL_ASE_DSPR3)
16031 fputs ("\n\tDSP R3 ASE", stdout);
16032 if (mask & AFL_ASE_EVA)
16033 fputs ("\n\tEnhanced VA Scheme", stdout);
16034 if (mask & AFL_ASE_MCU)
16035 fputs ("\n\tMCU (MicroController) ASE", stdout);
16036 if (mask & AFL_ASE_MDMX)
16037 fputs ("\n\tMDMX ASE", stdout);
16038 if (mask & AFL_ASE_MIPS3D)
16039 fputs ("\n\tMIPS-3D ASE", stdout);
16040 if (mask & AFL_ASE_MT)
16041 fputs ("\n\tMT ASE", stdout);
16042 if (mask & AFL_ASE_SMARTMIPS)
16043 fputs ("\n\tSmartMIPS ASE", stdout);
16044 if (mask & AFL_ASE_VIRT)
16045 fputs ("\n\tVZ ASE", stdout);
16046 if (mask & AFL_ASE_MSA)
16047 fputs ("\n\tMSA ASE", stdout);
16048 if (mask & AFL_ASE_MIPS16)
16049 fputs ("\n\tMIPS16 ASE", stdout);
16050 if (mask & AFL_ASE_MICROMIPS)
16051 fputs ("\n\tMICROMIPS ASE", stdout);
16052 if (mask & AFL_ASE_XPA)
16053 fputs ("\n\tXPA ASE", stdout);
16054 if (mask & AFL_ASE_MIPS16E2)
16055 fputs ("\n\tMIPS16e2 ASE", stdout);
16056 if (mask & AFL_ASE_CRC)
16057 fputs ("\n\tCRC ASE", stdout);
16058 if (mask & AFL_ASE_GINV)
16059 fputs ("\n\tGINV ASE", stdout);
16060 if (mask & AFL_ASE_LOONGSON_MMI)
16061 fputs ("\n\tLoongson MMI ASE", stdout);
16062 if (mask & AFL_ASE_LOONGSON_CAM)
16063 fputs ("\n\tLoongson CAM ASE", stdout);
16064 if (mask & AFL_ASE_LOONGSON_EXT)
16065 fputs ("\n\tLoongson EXT ASE", stdout);
16066 if (mask & AFL_ASE_LOONGSON_EXT2)
16067 fputs ("\n\tLoongson EXT2 ASE", stdout);
16068 if (mask == 0)
16069 fprintf (stdout, "\n\t%s", _("None"));
16070 else if ((mask & ~AFL_ASE_MASK) != 0)
16071 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16072 }
16073
16074 static void
16075 print_mips_isa_ext (unsigned int isa_ext)
16076 {
16077 switch (isa_ext)
16078 {
16079 case 0:
16080 fputs (_("None"), stdout);
16081 break;
16082 case AFL_EXT_XLR:
16083 fputs ("RMI XLR", stdout);
16084 break;
16085 case AFL_EXT_OCTEON3:
16086 fputs ("Cavium Networks Octeon3", stdout);
16087 break;
16088 case AFL_EXT_OCTEON2:
16089 fputs ("Cavium Networks Octeon2", stdout);
16090 break;
16091 case AFL_EXT_OCTEONP:
16092 fputs ("Cavium Networks OcteonP", stdout);
16093 break;
16094 case AFL_EXT_OCTEON:
16095 fputs ("Cavium Networks Octeon", stdout);
16096 break;
16097 case AFL_EXT_5900:
16098 fputs ("Toshiba R5900", stdout);
16099 break;
16100 case AFL_EXT_4650:
16101 fputs ("MIPS R4650", stdout);
16102 break;
16103 case AFL_EXT_4010:
16104 fputs ("LSI R4010", stdout);
16105 break;
16106 case AFL_EXT_4100:
16107 fputs ("NEC VR4100", stdout);
16108 break;
16109 case AFL_EXT_3900:
16110 fputs ("Toshiba R3900", stdout);
16111 break;
16112 case AFL_EXT_10000:
16113 fputs ("MIPS R10000", stdout);
16114 break;
16115 case AFL_EXT_SB1:
16116 fputs ("Broadcom SB-1", stdout);
16117 break;
16118 case AFL_EXT_4111:
16119 fputs ("NEC VR4111/VR4181", stdout);
16120 break;
16121 case AFL_EXT_4120:
16122 fputs ("NEC VR4120", stdout);
16123 break;
16124 case AFL_EXT_5400:
16125 fputs ("NEC VR5400", stdout);
16126 break;
16127 case AFL_EXT_5500:
16128 fputs ("NEC VR5500", stdout);
16129 break;
16130 case AFL_EXT_LOONGSON_2E:
16131 fputs ("ST Microelectronics Loongson 2E", stdout);
16132 break;
16133 case AFL_EXT_LOONGSON_2F:
16134 fputs ("ST Microelectronics Loongson 2F", stdout);
16135 break;
16136 case AFL_EXT_INTERAPTIV_MR2:
16137 fputs ("Imagination interAptiv MR2", stdout);
16138 break;
16139 default:
16140 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
16141 }
16142 }
16143
16144 static signed int
16145 get_mips_reg_size (int reg_size)
16146 {
16147 return (reg_size == AFL_REG_NONE) ? 0
16148 : (reg_size == AFL_REG_32) ? 32
16149 : (reg_size == AFL_REG_64) ? 64
16150 : (reg_size == AFL_REG_128) ? 128
16151 : -1;
16152 }
16153
16154 static bfd_boolean
16155 process_mips_specific (Filedata * filedata)
16156 {
16157 Elf_Internal_Dyn * entry;
16158 Elf_Internal_Shdr *sect = NULL;
16159 size_t liblist_offset = 0;
16160 size_t liblistno = 0;
16161 size_t conflictsno = 0;
16162 size_t options_offset = 0;
16163 size_t conflicts_offset = 0;
16164 size_t pltrelsz = 0;
16165 size_t pltrel = 0;
16166 bfd_vma pltgot = 0;
16167 bfd_vma mips_pltgot = 0;
16168 bfd_vma jmprel = 0;
16169 bfd_vma local_gotno = 0;
16170 bfd_vma gotsym = 0;
16171 bfd_vma symtabno = 0;
16172 bfd_boolean res = TRUE;
16173
16174 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
16175 display_mips_gnu_attribute))
16176 res = FALSE;
16177
16178 sect = find_section (filedata, ".MIPS.abiflags");
16179
16180 if (sect != NULL)
16181 {
16182 Elf_External_ABIFlags_v0 *abiflags_ext;
16183 Elf_Internal_ABIFlags_v0 abiflags_in;
16184
16185 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
16186 {
16187 error (_("Corrupt MIPS ABI Flags section.\n"));
16188 res = FALSE;
16189 }
16190 else
16191 {
16192 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
16193 sect->sh_size, _("MIPS ABI Flags section"));
16194 if (abiflags_ext)
16195 {
16196 abiflags_in.version = BYTE_GET (abiflags_ext->version);
16197 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
16198 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
16199 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
16200 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
16201 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
16202 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
16203 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
16204 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
16205 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
16206 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
16207
16208 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
16209 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
16210 if (abiflags_in.isa_rev > 1)
16211 printf ("r%d", abiflags_in.isa_rev);
16212 printf ("\nGPR size: %d",
16213 get_mips_reg_size (abiflags_in.gpr_size));
16214 printf ("\nCPR1 size: %d",
16215 get_mips_reg_size (abiflags_in.cpr1_size));
16216 printf ("\nCPR2 size: %d",
16217 get_mips_reg_size (abiflags_in.cpr2_size));
16218 fputs ("\nFP ABI: ", stdout);
16219 print_mips_fp_abi_value (abiflags_in.fp_abi);
16220 fputs ("ISA Extension: ", stdout);
16221 print_mips_isa_ext (abiflags_in.isa_ext);
16222 fputs ("\nASEs:", stdout);
16223 print_mips_ases (abiflags_in.ases);
16224 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
16225 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
16226 fputc ('\n', stdout);
16227 free (abiflags_ext);
16228 }
16229 }
16230 }
16231
16232 /* We have a lot of special sections. Thanks SGI! */
16233 if (dynamic_section == NULL)
16234 {
16235 /* No dynamic information available. See if there is static GOT. */
16236 sect = find_section (filedata, ".got");
16237 if (sect != NULL)
16238 {
16239 unsigned char *data_end;
16240 unsigned char *data;
16241 bfd_vma ent, end;
16242 int addr_size;
16243
16244 pltgot = sect->sh_addr;
16245
16246 ent = pltgot;
16247 addr_size = (is_32bit_elf ? 4 : 8);
16248 end = pltgot + sect->sh_size;
16249
16250 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
16251 end - pltgot, 1,
16252 _("Global Offset Table data"));
16253 /* PR 12855: Null data is handled gracefully throughout. */
16254 data_end = data + (end - pltgot);
16255
16256 printf (_("\nStatic GOT:\n"));
16257 printf (_(" Canonical gp value: "));
16258 print_vma (ent + 0x7ff0, LONG_HEX);
16259 printf ("\n\n");
16260
16261 /* In a dynamic binary GOT[0] is reserved for the dynamic
16262 loader to store the lazy resolver pointer, however in
16263 a static binary it may well have been omitted and GOT
16264 reduced to a table of addresses.
16265 PR 21344: Check for the entry being fully available
16266 before fetching it. */
16267 if (data
16268 && data + ent - pltgot + addr_size <= data_end
16269 && byte_get (data + ent - pltgot, addr_size) == 0)
16270 {
16271 printf (_(" Reserved entries:\n"));
16272 printf (_(" %*s %10s %*s\n"),
16273 addr_size * 2, _("Address"), _("Access"),
16274 addr_size * 2, _("Value"));
16275 ent = print_mips_got_entry (data, pltgot, ent, data_end);
16276 printf ("\n");
16277 if (ent == (bfd_vma) -1)
16278 goto sgot_print_fail;
16279
16280 /* Check for the MSB of GOT[1] being set, identifying a
16281 GNU object. This entry will be used by some runtime
16282 loaders, to store the module pointer. Otherwise this
16283 is an ordinary local entry.
16284 PR 21344: Check for the entry being fully available
16285 before fetching it. */
16286 if (data
16287 && data + ent - pltgot + addr_size <= data_end
16288 && (byte_get (data + ent - pltgot, addr_size)
16289 >> (addr_size * 8 - 1)) != 0)
16290 {
16291 ent = print_mips_got_entry (data, pltgot, ent, data_end);
16292 printf ("\n");
16293 if (ent == (bfd_vma) -1)
16294 goto sgot_print_fail;
16295 }
16296 printf ("\n");
16297 }
16298
16299 if (data != NULL && ent < end)
16300 {
16301 printf (_(" Local entries:\n"));
16302 printf (" %*s %10s %*s\n",
16303 addr_size * 2, _("Address"), _("Access"),
16304 addr_size * 2, _("Value"));
16305 while (ent < end)
16306 {
16307 ent = print_mips_got_entry (data, pltgot, ent, data_end);
16308 printf ("\n");
16309 if (ent == (bfd_vma) -1)
16310 goto sgot_print_fail;
16311 }
16312 printf ("\n");
16313 }
16314
16315 sgot_print_fail:
16316 if (data)
16317 free (data);
16318 }
16319 return res;
16320 }
16321
16322 for (entry = dynamic_section;
16323 /* PR 17531 file: 012-50589-0.004. */
16324 entry < dynamic_section + dynamic_nent && entry->d_tag != DT_NULL;
16325 ++entry)
16326 switch (entry->d_tag)
16327 {
16328 case DT_MIPS_LIBLIST:
16329 liblist_offset
16330 = offset_from_vma (filedata, entry->d_un.d_val,
16331 liblistno * sizeof (Elf32_External_Lib));
16332 break;
16333 case DT_MIPS_LIBLISTNO:
16334 liblistno = entry->d_un.d_val;
16335 break;
16336 case DT_MIPS_OPTIONS:
16337 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
16338 break;
16339 case DT_MIPS_CONFLICT:
16340 conflicts_offset
16341 = offset_from_vma (filedata, entry->d_un.d_val,
16342 conflictsno * sizeof (Elf32_External_Conflict));
16343 break;
16344 case DT_MIPS_CONFLICTNO:
16345 conflictsno = entry->d_un.d_val;
16346 break;
16347 case DT_PLTGOT:
16348 pltgot = entry->d_un.d_ptr;
16349 break;
16350 case DT_MIPS_LOCAL_GOTNO:
16351 local_gotno = entry->d_un.d_val;
16352 break;
16353 case DT_MIPS_GOTSYM:
16354 gotsym = entry->d_un.d_val;
16355 break;
16356 case DT_MIPS_SYMTABNO:
16357 symtabno = entry->d_un.d_val;
16358 break;
16359 case DT_MIPS_PLTGOT:
16360 mips_pltgot = entry->d_un.d_ptr;
16361 break;
16362 case DT_PLTREL:
16363 pltrel = entry->d_un.d_val;
16364 break;
16365 case DT_PLTRELSZ:
16366 pltrelsz = entry->d_un.d_val;
16367 break;
16368 case DT_JMPREL:
16369 jmprel = entry->d_un.d_ptr;
16370 break;
16371 default:
16372 break;
16373 }
16374
16375 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
16376 {
16377 Elf32_External_Lib * elib;
16378 size_t cnt;
16379
16380 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
16381 liblistno,
16382 sizeof (Elf32_External_Lib),
16383 _("liblist section data"));
16384 if (elib)
16385 {
16386 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
16387 "\nSection '.liblist' contains %lu entries:\n",
16388 (unsigned long) liblistno),
16389 (unsigned long) liblistno);
16390 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
16391 stdout);
16392
16393 for (cnt = 0; cnt < liblistno; ++cnt)
16394 {
16395 Elf32_Lib liblist;
16396 time_t atime;
16397 char timebuf[128];
16398 struct tm * tmp;
16399
16400 liblist.l_name = BYTE_GET (elib[cnt].l_name);
16401 atime = BYTE_GET (elib[cnt].l_time_stamp);
16402 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
16403 liblist.l_version = BYTE_GET (elib[cnt].l_version);
16404 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
16405
16406 tmp = gmtime (&atime);
16407 snprintf (timebuf, sizeof (timebuf),
16408 "%04u-%02u-%02uT%02u:%02u:%02u",
16409 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
16410 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
16411
16412 printf ("%3lu: ", (unsigned long) cnt);
16413 if (VALID_DYNAMIC_NAME (liblist.l_name))
16414 print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
16415 else
16416 printf (_("<corrupt: %9ld>"), liblist.l_name);
16417 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
16418 liblist.l_version);
16419
16420 if (liblist.l_flags == 0)
16421 puts (_(" NONE"));
16422 else
16423 {
16424 static const struct
16425 {
16426 const char * name;
16427 int bit;
16428 }
16429 l_flags_vals[] =
16430 {
16431 { " EXACT_MATCH", LL_EXACT_MATCH },
16432 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
16433 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
16434 { " EXPORTS", LL_EXPORTS },
16435 { " DELAY_LOAD", LL_DELAY_LOAD },
16436 { " DELTA", LL_DELTA }
16437 };
16438 int flags = liblist.l_flags;
16439 size_t fcnt;
16440
16441 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
16442 if ((flags & l_flags_vals[fcnt].bit) != 0)
16443 {
16444 fputs (l_flags_vals[fcnt].name, stdout);
16445 flags ^= l_flags_vals[fcnt].bit;
16446 }
16447 if (flags != 0)
16448 printf (" %#x", (unsigned int) flags);
16449
16450 puts ("");
16451 }
16452 }
16453
16454 free (elib);
16455 }
16456 else
16457 res = FALSE;
16458 }
16459
16460 if (options_offset != 0)
16461 {
16462 Elf_External_Options * eopt;
16463 Elf_Internal_Options * iopt;
16464 Elf_Internal_Options * option;
16465 size_t offset;
16466 int cnt;
16467 sect = filedata->section_headers;
16468
16469 /* Find the section header so that we get the size. */
16470 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
16471 /* PR 17533 file: 012-277276-0.004. */
16472 if (sect == NULL)
16473 {
16474 error (_("No MIPS_OPTIONS header found\n"));
16475 return FALSE;
16476 }
16477 /* PR 24243 */
16478 if (sect->sh_size < sizeof (* eopt))
16479 {
16480 error (_("The MIPS options section is too small.\n"));
16481 return FALSE;
16482 }
16483
16484 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
16485 sect->sh_size, _("options"));
16486 if (eopt)
16487 {
16488 iopt = (Elf_Internal_Options *)
16489 cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
16490 if (iopt == NULL)
16491 {
16492 error (_("Out of memory allocating space for MIPS options\n"));
16493 return FALSE;
16494 }
16495
16496 offset = cnt = 0;
16497 option = iopt;
16498
16499 while (offset <= sect->sh_size - sizeof (* eopt))
16500 {
16501 Elf_External_Options * eoption;
16502
16503 eoption = (Elf_External_Options *) ((char *) eopt + offset);
16504
16505 option->kind = BYTE_GET (eoption->kind);
16506 option->size = BYTE_GET (eoption->size);
16507 option->section = BYTE_GET (eoption->section);
16508 option->info = BYTE_GET (eoption->info);
16509
16510 /* PR 17531: file: ffa0fa3b. */
16511 if (option->size < sizeof (* eopt)
16512 || offset + option->size > sect->sh_size)
16513 {
16514 error (_("Invalid size (%u) for MIPS option\n"), option->size);
16515 return FALSE;
16516 }
16517 offset += option->size;
16518
16519 ++option;
16520 ++cnt;
16521 }
16522
16523 printf (ngettext ("\nSection '%s' contains %d entry:\n",
16524 "\nSection '%s' contains %d entries:\n",
16525 cnt),
16526 printable_section_name (filedata, sect), cnt);
16527
16528 option = iopt;
16529 offset = 0;
16530
16531 while (cnt-- > 0)
16532 {
16533 size_t len;
16534
16535 switch (option->kind)
16536 {
16537 case ODK_NULL:
16538 /* This shouldn't happen. */
16539 printf (" NULL %d %lx", option->section, option->info);
16540 break;
16541 case ODK_REGINFO:
16542 printf (" REGINFO ");
16543 if (filedata->file_header.e_machine == EM_MIPS)
16544 {
16545 /* 32bit form. */
16546 Elf32_External_RegInfo * ereg;
16547 Elf32_RegInfo reginfo;
16548
16549 ereg = (Elf32_External_RegInfo *) (option + 1);
16550 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
16551 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
16552 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
16553 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
16554 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
16555 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
16556
16557 printf ("GPR %08lx GP 0x%lx\n",
16558 reginfo.ri_gprmask,
16559 (unsigned long) reginfo.ri_gp_value);
16560 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
16561 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
16562 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
16563 }
16564 else
16565 {
16566 /* 64 bit form. */
16567 Elf64_External_RegInfo * ereg;
16568 Elf64_Internal_RegInfo reginfo;
16569
16570 ereg = (Elf64_External_RegInfo *) (option + 1);
16571 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
16572 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
16573 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
16574 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
16575 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
16576 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
16577
16578 printf ("GPR %08lx GP 0x",
16579 reginfo.ri_gprmask);
16580 printf_vma (reginfo.ri_gp_value);
16581 printf ("\n");
16582
16583 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
16584 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
16585 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
16586 }
16587 ++option;
16588 continue;
16589 case ODK_EXCEPTIONS:
16590 fputs (" EXCEPTIONS fpe_min(", stdout);
16591 process_mips_fpe_exception (option->info & OEX_FPU_MIN);
16592 fputs (") fpe_max(", stdout);
16593 process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
16594 fputs (")", stdout);
16595
16596 if (option->info & OEX_PAGE0)
16597 fputs (" PAGE0", stdout);
16598 if (option->info & OEX_SMM)
16599 fputs (" SMM", stdout);
16600 if (option->info & OEX_FPDBUG)
16601 fputs (" FPDBUG", stdout);
16602 if (option->info & OEX_DISMISS)
16603 fputs (" DISMISS", stdout);
16604 break;
16605 case ODK_PAD:
16606 fputs (" PAD ", stdout);
16607 if (option->info & OPAD_PREFIX)
16608 fputs (" PREFIX", stdout);
16609 if (option->info & OPAD_POSTFIX)
16610 fputs (" POSTFIX", stdout);
16611 if (option->info & OPAD_SYMBOL)
16612 fputs (" SYMBOL", stdout);
16613 break;
16614 case ODK_HWPATCH:
16615 fputs (" HWPATCH ", stdout);
16616 if (option->info & OHW_R4KEOP)
16617 fputs (" R4KEOP", stdout);
16618 if (option->info & OHW_R8KPFETCH)
16619 fputs (" R8KPFETCH", stdout);
16620 if (option->info & OHW_R5KEOP)
16621 fputs (" R5KEOP", stdout);
16622 if (option->info & OHW_R5KCVTL)
16623 fputs (" R5KCVTL", stdout);
16624 break;
16625 case ODK_FILL:
16626 fputs (" FILL ", stdout);
16627 /* XXX Print content of info word? */
16628 break;
16629 case ODK_TAGS:
16630 fputs (" TAGS ", stdout);
16631 /* XXX Print content of info word? */
16632 break;
16633 case ODK_HWAND:
16634 fputs (" HWAND ", stdout);
16635 if (option->info & OHWA0_R4KEOP_CHECKED)
16636 fputs (" R4KEOP_CHECKED", stdout);
16637 if (option->info & OHWA0_R4KEOP_CLEAN)
16638 fputs (" R4KEOP_CLEAN", stdout);
16639 break;
16640 case ODK_HWOR:
16641 fputs (" HWOR ", stdout);
16642 if (option->info & OHWA0_R4KEOP_CHECKED)
16643 fputs (" R4KEOP_CHECKED", stdout);
16644 if (option->info & OHWA0_R4KEOP_CLEAN)
16645 fputs (" R4KEOP_CLEAN", stdout);
16646 break;
16647 case ODK_GP_GROUP:
16648 printf (" GP_GROUP %#06lx self-contained %#06lx",
16649 option->info & OGP_GROUP,
16650 (option->info & OGP_SELF) >> 16);
16651 break;
16652 case ODK_IDENT:
16653 printf (" IDENT %#06lx self-contained %#06lx",
16654 option->info & OGP_GROUP,
16655 (option->info & OGP_SELF) >> 16);
16656 break;
16657 default:
16658 /* This shouldn't happen. */
16659 printf (" %3d ??? %d %lx",
16660 option->kind, option->section, option->info);
16661 break;
16662 }
16663
16664 len = sizeof (* eopt);
16665 while (len < option->size)
16666 {
16667 unsigned char datum = * ((unsigned char *) eopt + offset + len);
16668
16669 if (ISPRINT (datum))
16670 printf ("%c", datum);
16671 else
16672 printf ("\\%03o", datum);
16673 len ++;
16674 }
16675 fputs ("\n", stdout);
16676
16677 offset += option->size;
16678 ++option;
16679 }
16680
16681 free (eopt);
16682 }
16683 else
16684 res = FALSE;
16685 }
16686
16687 if (conflicts_offset != 0 && conflictsno != 0)
16688 {
16689 Elf32_Conflict * iconf;
16690 size_t cnt;
16691
16692 if (dynamic_symbols == NULL)
16693 {
16694 error (_("conflict list found without a dynamic symbol table\n"));
16695 return FALSE;
16696 }
16697
16698 /* PR 21345 - print a slightly more helpful error message
16699 if we are sure that the cmalloc will fail. */
16700 if (conflictsno * sizeof (* iconf) > filedata->file_size)
16701 {
16702 error (_("Overlarge number of conflicts detected: %lx\n"),
16703 (long) conflictsno);
16704 return FALSE;
16705 }
16706
16707 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
16708 if (iconf == NULL)
16709 {
16710 error (_("Out of memory allocating space for dynamic conflicts\n"));
16711 return FALSE;
16712 }
16713
16714 if (is_32bit_elf)
16715 {
16716 Elf32_External_Conflict * econf32;
16717
16718 econf32 = (Elf32_External_Conflict *)
16719 get_data (NULL, filedata, conflicts_offset, conflictsno,
16720 sizeof (* econf32), _("conflict"));
16721 if (!econf32)
16722 return FALSE;
16723
16724 for (cnt = 0; cnt < conflictsno; ++cnt)
16725 iconf[cnt] = BYTE_GET (econf32[cnt]);
16726
16727 free (econf32);
16728 }
16729 else
16730 {
16731 Elf64_External_Conflict * econf64;
16732
16733 econf64 = (Elf64_External_Conflict *)
16734 get_data (NULL, filedata, conflicts_offset, conflictsno,
16735 sizeof (* econf64), _("conflict"));
16736 if (!econf64)
16737 return FALSE;
16738
16739 for (cnt = 0; cnt < conflictsno; ++cnt)
16740 iconf[cnt] = BYTE_GET (econf64[cnt]);
16741
16742 free (econf64);
16743 }
16744
16745 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
16746 "\nSection '.conflict' contains %lu entries:\n",
16747 (unsigned long) conflictsno),
16748 (unsigned long) conflictsno);
16749 puts (_(" Num: Index Value Name"));
16750
16751 for (cnt = 0; cnt < conflictsno; ++cnt)
16752 {
16753 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
16754
16755 if (iconf[cnt] >= num_dynamic_syms)
16756 printf (_("<corrupt symbol index>"));
16757 else
16758 {
16759 Elf_Internal_Sym * psym;
16760
16761 psym = & dynamic_symbols[iconf[cnt]];
16762 print_vma (psym->st_value, FULL_HEX);
16763 putchar (' ');
16764 if (VALID_DYNAMIC_NAME (psym->st_name))
16765 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
16766 else
16767 printf (_("<corrupt: %14ld>"), psym->st_name);
16768 }
16769 putchar ('\n');
16770 }
16771
16772 free (iconf);
16773 }
16774
16775 if (pltgot != 0 && local_gotno != 0)
16776 {
16777 bfd_vma ent, local_end, global_end;
16778 size_t i, offset;
16779 unsigned char * data;
16780 unsigned char * data_end;
16781 int addr_size;
16782
16783 ent = pltgot;
16784 addr_size = (is_32bit_elf ? 4 : 8);
16785 local_end = pltgot + local_gotno * addr_size;
16786
16787 /* PR binutils/17533 file: 012-111227-0.004 */
16788 if (symtabno < gotsym)
16789 {
16790 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
16791 (unsigned long) gotsym, (unsigned long) symtabno);
16792 return FALSE;
16793 }
16794
16795 global_end = local_end + (symtabno - gotsym) * addr_size;
16796 /* PR 17531: file: 54c91a34. */
16797 if (global_end < local_end)
16798 {
16799 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
16800 return FALSE;
16801 }
16802
16803 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
16804 data = (unsigned char *) get_data (NULL, filedata, offset,
16805 global_end - pltgot, 1,
16806 _("Global Offset Table data"));
16807 /* PR 12855: Null data is handled gracefully throughout. */
16808 data_end = data + (global_end - pltgot);
16809
16810 printf (_("\nPrimary GOT:\n"));
16811 printf (_(" Canonical gp value: "));
16812 print_vma (pltgot + 0x7ff0, LONG_HEX);
16813 printf ("\n\n");
16814
16815 printf (_(" Reserved entries:\n"));
16816 printf (_(" %*s %10s %*s Purpose\n"),
16817 addr_size * 2, _("Address"), _("Access"),
16818 addr_size * 2, _("Initial"));
16819 ent = print_mips_got_entry (data, pltgot, ent, data_end);
16820 printf (_(" Lazy resolver\n"));
16821 if (ent == (bfd_vma) -1)
16822 goto got_print_fail;
16823
16824 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
16825 This entry will be used by some runtime loaders, to store the
16826 module pointer. Otherwise this is an ordinary local entry.
16827 PR 21344: Check for the entry being fully available before
16828 fetching it. */
16829 if (data
16830 && data + ent - pltgot + addr_size <= data_end
16831 && (byte_get (data + ent - pltgot, addr_size)
16832 >> (addr_size * 8 - 1)) != 0)
16833 {
16834 ent = print_mips_got_entry (data, pltgot, ent, data_end);
16835 printf (_(" Module pointer (GNU extension)\n"));
16836 if (ent == (bfd_vma) -1)
16837 goto got_print_fail;
16838 }
16839 printf ("\n");
16840
16841 if (data != NULL && ent < local_end)
16842 {
16843 printf (_(" Local entries:\n"));
16844 printf (" %*s %10s %*s\n",
16845 addr_size * 2, _("Address"), _("Access"),
16846 addr_size * 2, _("Initial"));
16847 while (ent < local_end)
16848 {
16849 ent = print_mips_got_entry (data, pltgot, ent, data_end);
16850 printf ("\n");
16851 if (ent == (bfd_vma) -1)
16852 goto got_print_fail;
16853 }
16854 printf ("\n");
16855 }
16856
16857 if (data != NULL && gotsym < symtabno)
16858 {
16859 int sym_width;
16860
16861 printf (_(" Global entries:\n"));
16862 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
16863 addr_size * 2, _("Address"),
16864 _("Access"),
16865 addr_size * 2, _("Initial"),
16866 addr_size * 2, _("Sym.Val."),
16867 _("Type"),
16868 /* Note for translators: "Ndx" = abbreviated form of "Index". */
16869 _("Ndx"), _("Name"));
16870
16871 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
16872
16873 for (i = gotsym; i < symtabno; i++)
16874 {
16875 ent = print_mips_got_entry (data, pltgot, ent, data_end);
16876 printf (" ");
16877
16878 if (dynamic_symbols == NULL)
16879 printf (_("<no dynamic symbols>"));
16880 else if (i < num_dynamic_syms)
16881 {
16882 Elf_Internal_Sym * psym = dynamic_symbols + i;
16883
16884 print_vma (psym->st_value, LONG_HEX);
16885 printf (" %-7s %3s ",
16886 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
16887 get_symbol_index_type (filedata, psym->st_shndx));
16888
16889 if (VALID_DYNAMIC_NAME (psym->st_name))
16890 print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
16891 else
16892 printf (_("<corrupt: %14ld>"), psym->st_name);
16893 }
16894 else
16895 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
16896 (unsigned long) i);
16897
16898 printf ("\n");
16899 if (ent == (bfd_vma) -1)
16900 break;
16901 }
16902 printf ("\n");
16903 }
16904
16905 got_print_fail:
16906 if (data)
16907 free (data);
16908 }
16909
16910 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
16911 {
16912 bfd_vma ent, end;
16913 size_t offset, rel_offset;
16914 unsigned long count, i;
16915 unsigned char * data;
16916 int addr_size, sym_width;
16917 Elf_Internal_Rela * rels;
16918
16919 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
16920 if (pltrel == DT_RELA)
16921 {
16922 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
16923 return FALSE;
16924 }
16925 else
16926 {
16927 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
16928 return FALSE;
16929 }
16930
16931 ent = mips_pltgot;
16932 addr_size = (is_32bit_elf ? 4 : 8);
16933 end = mips_pltgot + (2 + count) * addr_size;
16934
16935 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
16936 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
16937 1, _("Procedure Linkage Table data"));
16938 if (data == NULL)
16939 return FALSE;
16940
16941 printf ("\nPLT GOT:\n\n");
16942 printf (_(" Reserved entries:\n"));
16943 printf (_(" %*s %*s Purpose\n"),
16944 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
16945 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
16946 printf (_(" PLT lazy resolver\n"));
16947 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
16948 printf (_(" Module pointer\n"));
16949 printf ("\n");
16950
16951 printf (_(" Entries:\n"));
16952 printf (" %*s %*s %*s %-7s %3s %s\n",
16953 addr_size * 2, _("Address"),
16954 addr_size * 2, _("Initial"),
16955 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
16956 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
16957 for (i = 0; i < count; i++)
16958 {
16959 unsigned long idx = get_reloc_symindex (rels[i].r_info);
16960
16961 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
16962 printf (" ");
16963
16964 if (idx >= num_dynamic_syms)
16965 printf (_("<corrupt symbol index: %lu>"), idx);
16966 else
16967 {
16968 Elf_Internal_Sym * psym = dynamic_symbols + idx;
16969
16970 print_vma (psym->st_value, LONG_HEX);
16971 printf (" %-7s %3s ",
16972 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
16973 get_symbol_index_type (filedata, psym->st_shndx));
16974 if (VALID_DYNAMIC_NAME (psym->st_name))
16975 print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
16976 else
16977 printf (_("<corrupt: %14ld>"), psym->st_name);
16978 }
16979 printf ("\n");
16980 }
16981 printf ("\n");
16982
16983 if (data)
16984 free (data);
16985 free (rels);
16986 }
16987
16988 return res;
16989 }
16990
16991 static bfd_boolean
16992 process_nds32_specific (Filedata * filedata)
16993 {
16994 Elf_Internal_Shdr *sect = NULL;
16995
16996 sect = find_section (filedata, ".nds32_e_flags");
16997 if (sect != NULL)
16998 {
16999 unsigned int *flag;
17000
17001 printf ("\nNDS32 elf flags section:\n");
17002 flag = get_data (NULL, filedata, sect->sh_offset, 1,
17003 sect->sh_size, _("NDS32 elf flags section"));
17004
17005 if (! flag)
17006 return FALSE;
17007
17008 switch ((*flag) & 0x3)
17009 {
17010 case 0:
17011 printf ("(VEC_SIZE):\tNo entry.\n");
17012 break;
17013 case 1:
17014 printf ("(VEC_SIZE):\t4 bytes\n");
17015 break;
17016 case 2:
17017 printf ("(VEC_SIZE):\t16 bytes\n");
17018 break;
17019 case 3:
17020 printf ("(VEC_SIZE):\treserved\n");
17021 break;
17022 }
17023 }
17024
17025 return TRUE;
17026 }
17027
17028 static bfd_boolean
17029 process_gnu_liblist (Filedata * filedata)
17030 {
17031 Elf_Internal_Shdr * section;
17032 Elf_Internal_Shdr * string_sec;
17033 Elf32_External_Lib * elib;
17034 char * strtab;
17035 size_t strtab_size;
17036 size_t cnt;
17037 unsigned long num_liblist;
17038 unsigned i;
17039 bfd_boolean res = TRUE;
17040
17041 if (! do_arch)
17042 return TRUE;
17043
17044 for (i = 0, section = filedata->section_headers;
17045 i < filedata->file_header.e_shnum;
17046 i++, section++)
17047 {
17048 switch (section->sh_type)
17049 {
17050 case SHT_GNU_LIBLIST:
17051 if (section->sh_link >= filedata->file_header.e_shnum)
17052 break;
17053
17054 elib = (Elf32_External_Lib *)
17055 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
17056 _("liblist section data"));
17057
17058 if (elib == NULL)
17059 {
17060 res = FALSE;
17061 break;
17062 }
17063
17064 string_sec = filedata->section_headers + section->sh_link;
17065 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
17066 string_sec->sh_size,
17067 _("liblist string table"));
17068 if (strtab == NULL
17069 || section->sh_entsize != sizeof (Elf32_External_Lib))
17070 {
17071 free (elib);
17072 free (strtab);
17073 res = FALSE;
17074 break;
17075 }
17076 strtab_size = string_sec->sh_size;
17077
17078 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
17079 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
17080 "\nLibrary list section '%s' contains %lu entries:\n",
17081 num_liblist),
17082 printable_section_name (filedata, section),
17083 num_liblist);
17084
17085 puts (_(" Library Time Stamp Checksum Version Flags"));
17086
17087 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
17088 ++cnt)
17089 {
17090 Elf32_Lib liblist;
17091 time_t atime;
17092 char timebuf[128];
17093 struct tm * tmp;
17094
17095 liblist.l_name = BYTE_GET (elib[cnt].l_name);
17096 atime = BYTE_GET (elib[cnt].l_time_stamp);
17097 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
17098 liblist.l_version = BYTE_GET (elib[cnt].l_version);
17099 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
17100
17101 tmp = gmtime (&atime);
17102 snprintf (timebuf, sizeof (timebuf),
17103 "%04u-%02u-%02uT%02u:%02u:%02u",
17104 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
17105 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
17106
17107 printf ("%3lu: ", (unsigned long) cnt);
17108 if (do_wide)
17109 printf ("%-20s", liblist.l_name < strtab_size
17110 ? strtab + liblist.l_name : _("<corrupt>"));
17111 else
17112 printf ("%-20.20s", liblist.l_name < strtab_size
17113 ? strtab + liblist.l_name : _("<corrupt>"));
17114 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
17115 liblist.l_version, liblist.l_flags);
17116 }
17117
17118 free (elib);
17119 free (strtab);
17120 }
17121 }
17122
17123 return res;
17124 }
17125
17126 static const char *
17127 get_note_type (Filedata * filedata, unsigned e_type)
17128 {
17129 static char buff[64];
17130
17131 if (filedata->file_header.e_type == ET_CORE)
17132 switch (e_type)
17133 {
17134 case NT_AUXV:
17135 return _("NT_AUXV (auxiliary vector)");
17136 case NT_PRSTATUS:
17137 return _("NT_PRSTATUS (prstatus structure)");
17138 case NT_FPREGSET:
17139 return _("NT_FPREGSET (floating point registers)");
17140 case NT_PRPSINFO:
17141 return _("NT_PRPSINFO (prpsinfo structure)");
17142 case NT_TASKSTRUCT:
17143 return _("NT_TASKSTRUCT (task structure)");
17144 case NT_PRXFPREG:
17145 return _("NT_PRXFPREG (user_xfpregs structure)");
17146 case NT_PPC_VMX:
17147 return _("NT_PPC_VMX (ppc Altivec registers)");
17148 case NT_PPC_VSX:
17149 return _("NT_PPC_VSX (ppc VSX registers)");
17150 case NT_PPC_TAR:
17151 return _("NT_PPC_TAR (ppc TAR register)");
17152 case NT_PPC_PPR:
17153 return _("NT_PPC_PPR (ppc PPR register)");
17154 case NT_PPC_DSCR:
17155 return _("NT_PPC_DSCR (ppc DSCR register)");
17156 case NT_PPC_EBB:
17157 return _("NT_PPC_EBB (ppc EBB registers)");
17158 case NT_PPC_PMU:
17159 return _("NT_PPC_PMU (ppc PMU registers)");
17160 case NT_PPC_TM_CGPR:
17161 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
17162 case NT_PPC_TM_CFPR:
17163 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
17164 case NT_PPC_TM_CVMX:
17165 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
17166 case NT_PPC_TM_CVSX:
17167 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
17168 case NT_PPC_TM_SPR:
17169 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
17170 case NT_PPC_TM_CTAR:
17171 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
17172 case NT_PPC_TM_CPPR:
17173 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
17174 case NT_PPC_TM_CDSCR:
17175 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
17176 case NT_386_TLS:
17177 return _("NT_386_TLS (x86 TLS information)");
17178 case NT_386_IOPERM:
17179 return _("NT_386_IOPERM (x86 I/O permissions)");
17180 case NT_X86_XSTATE:
17181 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
17182 case NT_S390_HIGH_GPRS:
17183 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
17184 case NT_S390_TIMER:
17185 return _("NT_S390_TIMER (s390 timer register)");
17186 case NT_S390_TODCMP:
17187 return _("NT_S390_TODCMP (s390 TOD comparator register)");
17188 case NT_S390_TODPREG:
17189 return _("NT_S390_TODPREG (s390 TOD programmable register)");
17190 case NT_S390_CTRS:
17191 return _("NT_S390_CTRS (s390 control registers)");
17192 case NT_S390_PREFIX:
17193 return _("NT_S390_PREFIX (s390 prefix register)");
17194 case NT_S390_LAST_BREAK:
17195 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
17196 case NT_S390_SYSTEM_CALL:
17197 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
17198 case NT_S390_TDB:
17199 return _("NT_S390_TDB (s390 transaction diagnostic block)");
17200 case NT_S390_VXRS_LOW:
17201 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
17202 case NT_S390_VXRS_HIGH:
17203 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
17204 case NT_S390_GS_CB:
17205 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
17206 case NT_S390_GS_BC:
17207 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
17208 case NT_ARM_VFP:
17209 return _("NT_ARM_VFP (arm VFP registers)");
17210 case NT_ARM_TLS:
17211 return _("NT_ARM_TLS (AArch TLS registers)");
17212 case NT_ARM_HW_BREAK:
17213 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
17214 case NT_ARM_HW_WATCH:
17215 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
17216 case NT_PSTATUS:
17217 return _("NT_PSTATUS (pstatus structure)");
17218 case NT_FPREGS:
17219 return _("NT_FPREGS (floating point registers)");
17220 case NT_PSINFO:
17221 return _("NT_PSINFO (psinfo structure)");
17222 case NT_LWPSTATUS:
17223 return _("NT_LWPSTATUS (lwpstatus_t structure)");
17224 case NT_LWPSINFO:
17225 return _("NT_LWPSINFO (lwpsinfo_t structure)");
17226 case NT_WIN32PSTATUS:
17227 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
17228 case NT_SIGINFO:
17229 return _("NT_SIGINFO (siginfo_t data)");
17230 case NT_FILE:
17231 return _("NT_FILE (mapped files)");
17232 default:
17233 break;
17234 }
17235 else
17236 switch (e_type)
17237 {
17238 case NT_VERSION:
17239 return _("NT_VERSION (version)");
17240 case NT_ARCH:
17241 return _("NT_ARCH (architecture)");
17242 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
17243 return _("OPEN");
17244 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
17245 return _("func");
17246 default:
17247 break;
17248 }
17249
17250 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
17251 return buff;
17252 }
17253
17254 static bfd_boolean
17255 print_core_note (Elf_Internal_Note *pnote)
17256 {
17257 unsigned int addr_size = is_32bit_elf ? 4 : 8;
17258 bfd_vma count, page_size;
17259 unsigned char *descdata, *filenames, *descend;
17260
17261 if (pnote->type != NT_FILE)
17262 {
17263 if (do_wide)
17264 printf ("\n");
17265 return TRUE;
17266 }
17267
17268 #ifndef BFD64
17269 if (!is_32bit_elf)
17270 {
17271 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
17272 /* Still "successful". */
17273 return TRUE;
17274 }
17275 #endif
17276
17277 if (pnote->descsz < 2 * addr_size)
17278 {
17279 error (_(" Malformed note - too short for header\n"));
17280 return FALSE;
17281 }
17282
17283 descdata = (unsigned char *) pnote->descdata;
17284 descend = descdata + pnote->descsz;
17285
17286 if (descdata[pnote->descsz - 1] != '\0')
17287 {
17288 error (_(" Malformed note - does not end with \\0\n"));
17289 return FALSE;
17290 }
17291
17292 count = byte_get (descdata, addr_size);
17293 descdata += addr_size;
17294
17295 page_size = byte_get (descdata, addr_size);
17296 descdata += addr_size;
17297
17298 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
17299 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
17300 {
17301 error (_(" Malformed note - too short for supplied file count\n"));
17302 return FALSE;
17303 }
17304
17305 printf (_(" Page size: "));
17306 print_vma (page_size, DEC);
17307 printf ("\n");
17308
17309 printf (_(" %*s%*s%*s\n"),
17310 (int) (2 + 2 * addr_size), _("Start"),
17311 (int) (4 + 2 * addr_size), _("End"),
17312 (int) (4 + 2 * addr_size), _("Page Offset"));
17313 filenames = descdata + count * 3 * addr_size;
17314 while (count-- > 0)
17315 {
17316 bfd_vma start, end, file_ofs;
17317
17318 if (filenames == descend)
17319 {
17320 error (_(" Malformed note - filenames end too early\n"));
17321 return FALSE;
17322 }
17323
17324 start = byte_get (descdata, addr_size);
17325 descdata += addr_size;
17326 end = byte_get (descdata, addr_size);
17327 descdata += addr_size;
17328 file_ofs = byte_get (descdata, addr_size);
17329 descdata += addr_size;
17330
17331 printf (" ");
17332 print_vma (start, FULL_HEX);
17333 printf (" ");
17334 print_vma (end, FULL_HEX);
17335 printf (" ");
17336 print_vma (file_ofs, FULL_HEX);
17337 printf ("\n %s\n", filenames);
17338
17339 filenames += 1 + strlen ((char *) filenames);
17340 }
17341
17342 return TRUE;
17343 }
17344
17345 static const char *
17346 get_gnu_elf_note_type (unsigned e_type)
17347 {
17348 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
17349 switch (e_type)
17350 {
17351 case NT_GNU_ABI_TAG:
17352 return _("NT_GNU_ABI_TAG (ABI version tag)");
17353 case NT_GNU_HWCAP:
17354 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
17355 case NT_GNU_BUILD_ID:
17356 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
17357 case NT_GNU_GOLD_VERSION:
17358 return _("NT_GNU_GOLD_VERSION (gold version)");
17359 case NT_GNU_PROPERTY_TYPE_0:
17360 return _("NT_GNU_PROPERTY_TYPE_0");
17361 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
17362 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
17363 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
17364 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
17365 default:
17366 {
17367 static char buff[64];
17368
17369 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
17370 return buff;
17371 }
17372 }
17373 }
17374
17375 static void
17376 decode_x86_compat_isa (unsigned int bitmask)
17377 {
17378 while (bitmask)
17379 {
17380 unsigned int bit = bitmask & (- bitmask);
17381
17382 bitmask &= ~ bit;
17383 switch (bit)
17384 {
17385 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
17386 printf ("i486");
17387 break;
17388 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
17389 printf ("586");
17390 break;
17391 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
17392 printf ("686");
17393 break;
17394 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
17395 printf ("SSE");
17396 break;
17397 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
17398 printf ("SSE2");
17399 break;
17400 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
17401 printf ("SSE3");
17402 break;
17403 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
17404 printf ("SSSE3");
17405 break;
17406 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
17407 printf ("SSE4_1");
17408 break;
17409 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
17410 printf ("SSE4_2");
17411 break;
17412 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
17413 printf ("AVX");
17414 break;
17415 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
17416 printf ("AVX2");
17417 break;
17418 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
17419 printf ("AVX512F");
17420 break;
17421 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
17422 printf ("AVX512CD");
17423 break;
17424 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
17425 printf ("AVX512ER");
17426 break;
17427 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
17428 printf ("AVX512PF");
17429 break;
17430 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
17431 printf ("AVX512VL");
17432 break;
17433 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
17434 printf ("AVX512DQ");
17435 break;
17436 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
17437 printf ("AVX512BW");
17438 break;
17439 default:
17440 printf (_("<unknown: %x>"), bit);
17441 break;
17442 }
17443 if (bitmask)
17444 printf (", ");
17445 }
17446 }
17447
17448 static void
17449 decode_x86_isa (unsigned int bitmask)
17450 {
17451 if (!bitmask)
17452 {
17453 printf (_("<None>"));
17454 return;
17455 }
17456
17457 while (bitmask)
17458 {
17459 unsigned int bit = bitmask & (- bitmask);
17460
17461 bitmask &= ~ bit;
17462 switch (bit)
17463 {
17464 case GNU_PROPERTY_X86_ISA_1_CMOV:
17465 printf ("CMOV");
17466 break;
17467 case GNU_PROPERTY_X86_ISA_1_SSE:
17468 printf ("SSE");
17469 break;
17470 case GNU_PROPERTY_X86_ISA_1_SSE2:
17471 printf ("SSE2");
17472 break;
17473 case GNU_PROPERTY_X86_ISA_1_SSE3:
17474 printf ("SSE3");
17475 break;
17476 case GNU_PROPERTY_X86_ISA_1_SSSE3:
17477 printf ("SSSE3");
17478 break;
17479 case GNU_PROPERTY_X86_ISA_1_SSE4_1:
17480 printf ("SSE4_1");
17481 break;
17482 case GNU_PROPERTY_X86_ISA_1_SSE4_2:
17483 printf ("SSE4_2");
17484 break;
17485 case GNU_PROPERTY_X86_ISA_1_AVX:
17486 printf ("AVX");
17487 break;
17488 case GNU_PROPERTY_X86_ISA_1_AVX2:
17489 printf ("AVX2");
17490 break;
17491 case GNU_PROPERTY_X86_ISA_1_FMA:
17492 printf ("FMA");
17493 break;
17494 case GNU_PROPERTY_X86_ISA_1_AVX512F:
17495 printf ("AVX512F");
17496 break;
17497 case GNU_PROPERTY_X86_ISA_1_AVX512CD:
17498 printf ("AVX512CD");
17499 break;
17500 case GNU_PROPERTY_X86_ISA_1_AVX512ER:
17501 printf ("AVX512ER");
17502 break;
17503 case GNU_PROPERTY_X86_ISA_1_AVX512PF:
17504 printf ("AVX512PF");
17505 break;
17506 case GNU_PROPERTY_X86_ISA_1_AVX512VL:
17507 printf ("AVX512VL");
17508 break;
17509 case GNU_PROPERTY_X86_ISA_1_AVX512DQ:
17510 printf ("AVX512DQ");
17511 break;
17512 case GNU_PROPERTY_X86_ISA_1_AVX512BW:
17513 printf ("AVX512BW");
17514 break;
17515 case GNU_PROPERTY_X86_ISA_1_AVX512_4FMAPS:
17516 printf ("AVX512_4FMAPS");
17517 break;
17518 case GNU_PROPERTY_X86_ISA_1_AVX512_4VNNIW:
17519 printf ("AVX512_4VNNIW");
17520 break;
17521 case GNU_PROPERTY_X86_ISA_1_AVX512_BITALG:
17522 printf ("AVX512_BITALG");
17523 break;
17524 case GNU_PROPERTY_X86_ISA_1_AVX512_IFMA:
17525 printf ("AVX512_IFMA");
17526 break;
17527 case GNU_PROPERTY_X86_ISA_1_AVX512_VBMI:
17528 printf ("AVX512_VBMI");
17529 break;
17530 case GNU_PROPERTY_X86_ISA_1_AVX512_VBMI2:
17531 printf ("AVX512_VBMI2");
17532 break;
17533 case GNU_PROPERTY_X86_ISA_1_AVX512_VNNI:
17534 printf ("AVX512_VNNI");
17535 break;
17536 case GNU_PROPERTY_X86_ISA_1_AVX512_BF16:
17537 printf ("AVX512_BF16");
17538 break;
17539 default:
17540 printf (_("<unknown: %x>"), bit);
17541 break;
17542 }
17543 if (bitmask)
17544 printf (", ");
17545 }
17546 }
17547
17548 static void
17549 decode_x86_feature_1 (unsigned int bitmask)
17550 {
17551 if (!bitmask)
17552 {
17553 printf (_("<None>"));
17554 return;
17555 }
17556
17557 while (bitmask)
17558 {
17559 unsigned int bit = bitmask & (- bitmask);
17560
17561 bitmask &= ~ bit;
17562 switch (bit)
17563 {
17564 case GNU_PROPERTY_X86_FEATURE_1_IBT:
17565 printf ("IBT");
17566 break;
17567 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
17568 printf ("SHSTK");
17569 break;
17570 default:
17571 printf (_("<unknown: %x>"), bit);
17572 break;
17573 }
17574 if (bitmask)
17575 printf (", ");
17576 }
17577 }
17578
17579 static void
17580 decode_x86_feature_2 (unsigned int bitmask)
17581 {
17582 if (!bitmask)
17583 {
17584 printf (_("<None>"));
17585 return;
17586 }
17587
17588 while (bitmask)
17589 {
17590 unsigned int bit = bitmask & (- bitmask);
17591
17592 bitmask &= ~ bit;
17593 switch (bit)
17594 {
17595 case GNU_PROPERTY_X86_FEATURE_2_X86:
17596 printf ("x86");
17597 break;
17598 case GNU_PROPERTY_X86_FEATURE_2_X87:
17599 printf ("x87");
17600 break;
17601 case GNU_PROPERTY_X86_FEATURE_2_MMX:
17602 printf ("MMX");
17603 break;
17604 case GNU_PROPERTY_X86_FEATURE_2_XMM:
17605 printf ("XMM");
17606 break;
17607 case GNU_PROPERTY_X86_FEATURE_2_YMM:
17608 printf ("YMM");
17609 break;
17610 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
17611 printf ("ZMM");
17612 break;
17613 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
17614 printf ("FXSR");
17615 break;
17616 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
17617 printf ("XSAVE");
17618 break;
17619 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
17620 printf ("XSAVEOPT");
17621 break;
17622 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
17623 printf ("XSAVEC");
17624 break;
17625 default:
17626 printf (_("<unknown: %x>"), bit);
17627 break;
17628 }
17629 if (bitmask)
17630 printf (", ");
17631 }
17632 }
17633
17634 static void
17635 decode_aarch64_feature_1_and (unsigned int bitmask)
17636 {
17637 while (bitmask)
17638 {
17639 unsigned int bit = bitmask & (- bitmask);
17640
17641 bitmask &= ~ bit;
17642 switch (bit)
17643 {
17644 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
17645 printf ("BTI");
17646 break;
17647
17648 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
17649 printf ("PAC");
17650 break;
17651
17652 default:
17653 printf (_("<unknown: %x>"), bit);
17654 break;
17655 }
17656 if (bitmask)
17657 printf (", ");
17658 }
17659 }
17660
17661 static void
17662 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
17663 {
17664 unsigned char * ptr = (unsigned char *) pnote->descdata;
17665 unsigned char * ptr_end = ptr + pnote->descsz;
17666 unsigned int size = is_32bit_elf ? 4 : 8;
17667
17668 printf (_(" Properties: "));
17669
17670 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
17671 {
17672 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
17673 return;
17674 }
17675
17676 while (ptr < ptr_end)
17677 {
17678 unsigned int j;
17679 unsigned int type;
17680 unsigned int datasz;
17681
17682 if ((size_t) (ptr_end - ptr) < 8)
17683 {
17684 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
17685 break;
17686 }
17687
17688 type = byte_get (ptr, 4);
17689 datasz = byte_get (ptr + 4, 4);
17690
17691 ptr += 8;
17692
17693 if (datasz > (size_t) (ptr_end - ptr))
17694 {
17695 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
17696 type, datasz);
17697 break;
17698 }
17699
17700 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
17701 {
17702 if (filedata->file_header.e_machine == EM_X86_64
17703 || filedata->file_header.e_machine == EM_IAMCU
17704 || filedata->file_header.e_machine == EM_386)
17705 {
17706 unsigned int bitmask;
17707
17708 if (datasz == 4)
17709 bitmask = byte_get (ptr, 4);
17710 else
17711 bitmask = 0;
17712
17713 switch (type)
17714 {
17715 case GNU_PROPERTY_X86_ISA_1_USED:
17716 if (datasz != 4)
17717 printf (_("x86 ISA used: <corrupt length: %#x> "),
17718 datasz);
17719 else
17720 {
17721 printf ("x86 ISA used: ");
17722 decode_x86_isa (bitmask);
17723 }
17724 goto next;
17725
17726 case GNU_PROPERTY_X86_ISA_1_NEEDED:
17727 if (datasz != 4)
17728 printf (_("x86 ISA needed: <corrupt length: %#x> "),
17729 datasz);
17730 else
17731 {
17732 printf ("x86 ISA needed: ");
17733 decode_x86_isa (bitmask);
17734 }
17735 goto next;
17736
17737 case GNU_PROPERTY_X86_FEATURE_1_AND:
17738 if (datasz != 4)
17739 printf (_("x86 feature: <corrupt length: %#x> "),
17740 datasz);
17741 else
17742 {
17743 printf ("x86 feature: ");
17744 decode_x86_feature_1 (bitmask);
17745 }
17746 goto next;
17747
17748 case GNU_PROPERTY_X86_FEATURE_2_USED:
17749 if (datasz != 4)
17750 printf (_("x86 feature used: <corrupt length: %#x> "),
17751 datasz);
17752 else
17753 {
17754 printf ("x86 feature used: ");
17755 decode_x86_feature_2 (bitmask);
17756 }
17757 goto next;
17758
17759 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
17760 if (datasz != 4)
17761 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
17762 else
17763 {
17764 printf ("x86 feature needed: ");
17765 decode_x86_feature_2 (bitmask);
17766 }
17767 goto next;
17768
17769 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
17770 if (datasz != 4)
17771 printf (_("x86 ISA used: <corrupt length: %#x> "),
17772 datasz);
17773 else
17774 {
17775 printf ("x86 ISA used: ");
17776 decode_x86_compat_isa (bitmask);
17777 }
17778 goto next;
17779
17780 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
17781 if (datasz != 4)
17782 printf (_("x86 ISA needed: <corrupt length: %#x> "),
17783 datasz);
17784 else
17785 {
17786 printf ("x86 ISA needed: ");
17787 decode_x86_compat_isa (bitmask);
17788 }
17789 goto next;
17790
17791 default:
17792 break;
17793 }
17794 }
17795 else if (filedata->file_header.e_machine == EM_AARCH64)
17796 {
17797 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
17798 {
17799 printf ("AArch64 feature: ");
17800 if (datasz != 4)
17801 printf (_("<corrupt length: %#x> "), datasz);
17802 else
17803 decode_aarch64_feature_1_and (byte_get (ptr, 4));
17804 goto next;
17805 }
17806 }
17807 }
17808 else
17809 {
17810 switch (type)
17811 {
17812 case GNU_PROPERTY_STACK_SIZE:
17813 printf (_("stack size: "));
17814 if (datasz != size)
17815 printf (_("<corrupt length: %#x> "), datasz);
17816 else
17817 printf ("%#lx", (unsigned long) byte_get (ptr, size));
17818 goto next;
17819
17820 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
17821 printf ("no copy on protected ");
17822 if (datasz)
17823 printf (_("<corrupt length: %#x> "), datasz);
17824 goto next;
17825
17826 default:
17827 break;
17828 }
17829 }
17830
17831 if (type < GNU_PROPERTY_LOPROC)
17832 printf (_("<unknown type %#x data: "), type);
17833 else if (type < GNU_PROPERTY_LOUSER)
17834 printf (_("<procesor-specific type %#x data: "), type);
17835 else
17836 printf (_("<application-specific type %#x data: "), type);
17837 for (j = 0; j < datasz; ++j)
17838 printf ("%02x ", ptr[j] & 0xff);
17839 printf (">");
17840
17841 next:
17842 ptr += ((datasz + (size - 1)) & ~ (size - 1));
17843 if (ptr == ptr_end)
17844 break;
17845
17846 if (do_wide)
17847 printf (", ");
17848 else
17849 printf ("\n\t");
17850 }
17851
17852 printf ("\n");
17853 }
17854
17855 static bfd_boolean
17856 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
17857 {
17858 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
17859 switch (pnote->type)
17860 {
17861 case NT_GNU_BUILD_ID:
17862 {
17863 unsigned long i;
17864
17865 printf (_(" Build ID: "));
17866 for (i = 0; i < pnote->descsz; ++i)
17867 printf ("%02x", pnote->descdata[i] & 0xff);
17868 printf ("\n");
17869 }
17870 break;
17871
17872 case NT_GNU_ABI_TAG:
17873 {
17874 unsigned long os, major, minor, subminor;
17875 const char *osname;
17876
17877 /* PR 17531: file: 030-599401-0.004. */
17878 if (pnote->descsz < 16)
17879 {
17880 printf (_(" <corrupt GNU_ABI_TAG>\n"));
17881 break;
17882 }
17883
17884 os = byte_get ((unsigned char *) pnote->descdata, 4);
17885 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
17886 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
17887 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
17888
17889 switch (os)
17890 {
17891 case GNU_ABI_TAG_LINUX:
17892 osname = "Linux";
17893 break;
17894 case GNU_ABI_TAG_HURD:
17895 osname = "Hurd";
17896 break;
17897 case GNU_ABI_TAG_SOLARIS:
17898 osname = "Solaris";
17899 break;
17900 case GNU_ABI_TAG_FREEBSD:
17901 osname = "FreeBSD";
17902 break;
17903 case GNU_ABI_TAG_NETBSD:
17904 osname = "NetBSD";
17905 break;
17906 case GNU_ABI_TAG_SYLLABLE:
17907 osname = "Syllable";
17908 break;
17909 case GNU_ABI_TAG_NACL:
17910 osname = "NaCl";
17911 break;
17912 default:
17913 osname = "Unknown";
17914 break;
17915 }
17916
17917 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
17918 major, minor, subminor);
17919 }
17920 break;
17921
17922 case NT_GNU_GOLD_VERSION:
17923 {
17924 unsigned long i;
17925
17926 printf (_(" Version: "));
17927 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
17928 printf ("%c", pnote->descdata[i]);
17929 printf ("\n");
17930 }
17931 break;
17932
17933 case NT_GNU_HWCAP:
17934 {
17935 unsigned long num_entries, mask;
17936
17937 /* Hardware capabilities information. Word 0 is the number of entries.
17938 Word 1 is a bitmask of enabled entries. The rest of the descriptor
17939 is a series of entries, where each entry is a single byte followed
17940 by a nul terminated string. The byte gives the bit number to test
17941 if enabled in the bitmask. */
17942 printf (_(" Hardware Capabilities: "));
17943 if (pnote->descsz < 8)
17944 {
17945 error (_("<corrupt GNU_HWCAP>\n"));
17946 return FALSE;
17947 }
17948 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
17949 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
17950 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
17951 /* FIXME: Add code to display the entries... */
17952 }
17953 break;
17954
17955 case NT_GNU_PROPERTY_TYPE_0:
17956 print_gnu_property_note (filedata, pnote);
17957 break;
17958
17959 default:
17960 /* Handle unrecognised types. An error message should have already been
17961 created by get_gnu_elf_note_type(), so all that we need to do is to
17962 display the data. */
17963 {
17964 unsigned long i;
17965
17966 printf (_(" Description data: "));
17967 for (i = 0; i < pnote->descsz; ++i)
17968 printf ("%02x ", pnote->descdata[i] & 0xff);
17969 printf ("\n");
17970 }
17971 break;
17972 }
17973
17974 return TRUE;
17975 }
17976
17977 static const char *
17978 get_v850_elf_note_type (enum v850_notes n_type)
17979 {
17980 static char buff[64];
17981
17982 switch (n_type)
17983 {
17984 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
17985 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
17986 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
17987 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
17988 case V850_NOTE_CACHE_INFO: return _("Use of cache");
17989 case V850_NOTE_MMU_INFO: return _("Use of MMU");
17990 default:
17991 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
17992 return buff;
17993 }
17994 }
17995
17996 static bfd_boolean
17997 print_v850_note (Elf_Internal_Note * pnote)
17998 {
17999 unsigned int val;
18000
18001 if (pnote->descsz != 4)
18002 return FALSE;
18003
18004 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
18005
18006 if (val == 0)
18007 {
18008 printf (_("not set\n"));
18009 return TRUE;
18010 }
18011
18012 switch (pnote->type)
18013 {
18014 case V850_NOTE_ALIGNMENT:
18015 switch (val)
18016 {
18017 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return TRUE;
18018 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return TRUE;
18019 }
18020 break;
18021
18022 case V850_NOTE_DATA_SIZE:
18023 switch (val)
18024 {
18025 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return TRUE;
18026 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return TRUE;
18027 }
18028 break;
18029
18030 case V850_NOTE_FPU_INFO:
18031 switch (val)
18032 {
18033 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return TRUE;
18034 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return TRUE;
18035 }
18036 break;
18037
18038 case V850_NOTE_MMU_INFO:
18039 case V850_NOTE_CACHE_INFO:
18040 case V850_NOTE_SIMD_INFO:
18041 if (val == EF_RH850_SIMD)
18042 {
18043 printf (_("yes\n"));
18044 return TRUE;
18045 }
18046 break;
18047
18048 default:
18049 /* An 'unknown note type' message will already have been displayed. */
18050 break;
18051 }
18052
18053 printf (_("unknown value: %x\n"), val);
18054 return FALSE;
18055 }
18056
18057 static bfd_boolean
18058 process_netbsd_elf_note (Elf_Internal_Note * pnote)
18059 {
18060 unsigned int version;
18061
18062 switch (pnote->type)
18063 {
18064 case NT_NETBSD_IDENT:
18065 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
18066 if ((version / 10000) % 100)
18067 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
18068 version, version / 100000000, (version / 1000000) % 100,
18069 (version / 10000) % 100 > 26 ? "Z" : "",
18070 'A' + (version / 10000) % 26);
18071 else
18072 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
18073 version, version / 100000000, (version / 1000000) % 100,
18074 (version / 100) % 100);
18075 return TRUE;
18076
18077 case NT_NETBSD_MARCH:
18078 printf (" NetBSD\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
18079 pnote->descdata);
18080 return TRUE;
18081
18082 default:
18083 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n", pnote->descsz,
18084 pnote->type);
18085 return FALSE;
18086 }
18087 }
18088
18089 static const char *
18090 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
18091 {
18092 switch (e_type)
18093 {
18094 case NT_FREEBSD_THRMISC:
18095 return _("NT_THRMISC (thrmisc structure)");
18096 case NT_FREEBSD_PROCSTAT_PROC:
18097 return _("NT_PROCSTAT_PROC (proc data)");
18098 case NT_FREEBSD_PROCSTAT_FILES:
18099 return _("NT_PROCSTAT_FILES (files data)");
18100 case NT_FREEBSD_PROCSTAT_VMMAP:
18101 return _("NT_PROCSTAT_VMMAP (vmmap data)");
18102 case NT_FREEBSD_PROCSTAT_GROUPS:
18103 return _("NT_PROCSTAT_GROUPS (groups data)");
18104 case NT_FREEBSD_PROCSTAT_UMASK:
18105 return _("NT_PROCSTAT_UMASK (umask data)");
18106 case NT_FREEBSD_PROCSTAT_RLIMIT:
18107 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
18108 case NT_FREEBSD_PROCSTAT_OSREL:
18109 return _("NT_PROCSTAT_OSREL (osreldate data)");
18110 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
18111 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
18112 case NT_FREEBSD_PROCSTAT_AUXV:
18113 return _("NT_PROCSTAT_AUXV (auxv data)");
18114 case NT_FREEBSD_PTLWPINFO:
18115 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
18116 }
18117 return get_note_type (filedata, e_type);
18118 }
18119
18120 static const char *
18121 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
18122 {
18123 static char buff[64];
18124
18125 if (e_type == NT_NETBSDCORE_PROCINFO)
18126 return _("NetBSD procinfo structure");
18127
18128 /* As of Jan 2002 there are no other machine-independent notes
18129 defined for NetBSD core files. If the note type is less
18130 than the start of the machine-dependent note types, we don't
18131 understand it. */
18132
18133 if (e_type < NT_NETBSDCORE_FIRSTMACH)
18134 {
18135 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18136 return buff;
18137 }
18138
18139 switch (filedata->file_header.e_machine)
18140 {
18141 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
18142 and PT_GETFPREGS == mach+2. */
18143
18144 case EM_OLD_ALPHA:
18145 case EM_ALPHA:
18146 case EM_SPARC:
18147 case EM_SPARC32PLUS:
18148 case EM_SPARCV9:
18149 switch (e_type)
18150 {
18151 case NT_NETBSDCORE_FIRSTMACH + 0:
18152 return _("PT_GETREGS (reg structure)");
18153 case NT_NETBSDCORE_FIRSTMACH + 2:
18154 return _("PT_GETFPREGS (fpreg structure)");
18155 default:
18156 break;
18157 }
18158 break;
18159
18160 /* On all other arch's, PT_GETREGS == mach+1 and
18161 PT_GETFPREGS == mach+3. */
18162 default:
18163 switch (e_type)
18164 {
18165 case NT_NETBSDCORE_FIRSTMACH + 1:
18166 return _("PT_GETREGS (reg structure)");
18167 case NT_NETBSDCORE_FIRSTMACH + 3:
18168 return _("PT_GETFPREGS (fpreg structure)");
18169 default:
18170 break;
18171 }
18172 }
18173
18174 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
18175 e_type - NT_NETBSDCORE_FIRSTMACH);
18176 return buff;
18177 }
18178
18179 static const char *
18180 get_stapsdt_note_type (unsigned e_type)
18181 {
18182 static char buff[64];
18183
18184 switch (e_type)
18185 {
18186 case NT_STAPSDT:
18187 return _("NT_STAPSDT (SystemTap probe descriptors)");
18188
18189 default:
18190 break;
18191 }
18192
18193 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18194 return buff;
18195 }
18196
18197 static bfd_boolean
18198 print_stapsdt_note (Elf_Internal_Note *pnote)
18199 {
18200 size_t len, maxlen;
18201 unsigned long addr_size = is_32bit_elf ? 4 : 8;
18202 char *data = pnote->descdata;
18203 char *data_end = pnote->descdata + pnote->descsz;
18204 bfd_vma pc, base_addr, semaphore;
18205 char *provider, *probe, *arg_fmt;
18206
18207 if (pnote->descsz < (addr_size * 3))
18208 goto stapdt_note_too_small;
18209
18210 pc = byte_get ((unsigned char *) data, addr_size);
18211 data += addr_size;
18212
18213 base_addr = byte_get ((unsigned char *) data, addr_size);
18214 data += addr_size;
18215
18216 semaphore = byte_get ((unsigned char *) data, addr_size);
18217 data += addr_size;
18218
18219 if (data >= data_end)
18220 goto stapdt_note_too_small;
18221 maxlen = data_end - data;
18222 len = strnlen (data, maxlen);
18223 if (len < maxlen)
18224 {
18225 provider = data;
18226 data += len + 1;
18227 }
18228 else
18229 goto stapdt_note_too_small;
18230
18231 if (data >= data_end)
18232 goto stapdt_note_too_small;
18233 maxlen = data_end - data;
18234 len = strnlen (data, maxlen);
18235 if (len < maxlen)
18236 {
18237 probe = data;
18238 data += len + 1;
18239 }
18240 else
18241 goto stapdt_note_too_small;
18242
18243 if (data >= data_end)
18244 goto stapdt_note_too_small;
18245 maxlen = data_end - data;
18246 len = strnlen (data, maxlen);
18247 if (len < maxlen)
18248 {
18249 arg_fmt = data;
18250 data += len + 1;
18251 }
18252 else
18253 goto stapdt_note_too_small;
18254
18255 printf (_(" Provider: %s\n"), provider);
18256 printf (_(" Name: %s\n"), probe);
18257 printf (_(" Location: "));
18258 print_vma (pc, FULL_HEX);
18259 printf (_(", Base: "));
18260 print_vma (base_addr, FULL_HEX);
18261 printf (_(", Semaphore: "));
18262 print_vma (semaphore, FULL_HEX);
18263 printf ("\n");
18264 printf (_(" Arguments: %s\n"), arg_fmt);
18265
18266 return data == data_end;
18267
18268 stapdt_note_too_small:
18269 printf (_(" <corrupt - note is too small>\n"));
18270 error (_("corrupt stapdt note - the data size is too small\n"));
18271 return FALSE;
18272 }
18273
18274 static const char *
18275 get_ia64_vms_note_type (unsigned e_type)
18276 {
18277 static char buff[64];
18278
18279 switch (e_type)
18280 {
18281 case NT_VMS_MHD:
18282 return _("NT_VMS_MHD (module header)");
18283 case NT_VMS_LNM:
18284 return _("NT_VMS_LNM (language name)");
18285 case NT_VMS_SRC:
18286 return _("NT_VMS_SRC (source files)");
18287 case NT_VMS_TITLE:
18288 return "NT_VMS_TITLE";
18289 case NT_VMS_EIDC:
18290 return _("NT_VMS_EIDC (consistency check)");
18291 case NT_VMS_FPMODE:
18292 return _("NT_VMS_FPMODE (FP mode)");
18293 case NT_VMS_LINKTIME:
18294 return "NT_VMS_LINKTIME";
18295 case NT_VMS_IMGNAM:
18296 return _("NT_VMS_IMGNAM (image name)");
18297 case NT_VMS_IMGID:
18298 return _("NT_VMS_IMGID (image id)");
18299 case NT_VMS_LINKID:
18300 return _("NT_VMS_LINKID (link id)");
18301 case NT_VMS_IMGBID:
18302 return _("NT_VMS_IMGBID (build id)");
18303 case NT_VMS_GSTNAM:
18304 return _("NT_VMS_GSTNAM (sym table name)");
18305 case NT_VMS_ORIG_DYN:
18306 return "NT_VMS_ORIG_DYN";
18307 case NT_VMS_PATCHTIME:
18308 return "NT_VMS_PATCHTIME";
18309 default:
18310 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18311 return buff;
18312 }
18313 }
18314
18315 static bfd_boolean
18316 print_ia64_vms_note (Elf_Internal_Note * pnote)
18317 {
18318 int maxlen = pnote->descsz;
18319
18320 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
18321 goto desc_size_fail;
18322
18323 switch (pnote->type)
18324 {
18325 case NT_VMS_MHD:
18326 if (maxlen <= 36)
18327 goto desc_size_fail;
18328
18329 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
18330
18331 printf (_(" Creation date : %.17s\n"), pnote->descdata);
18332 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
18333 if (l + 34 < maxlen)
18334 {
18335 printf (_(" Module name : %s\n"), pnote->descdata + 34);
18336 if (l + 35 < maxlen)
18337 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
18338 else
18339 printf (_(" Module version : <missing>\n"));
18340 }
18341 else
18342 {
18343 printf (_(" Module name : <missing>\n"));
18344 printf (_(" Module version : <missing>\n"));
18345 }
18346 break;
18347
18348 case NT_VMS_LNM:
18349 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
18350 break;
18351
18352 #ifdef BFD64
18353 case NT_VMS_FPMODE:
18354 printf (_(" Floating Point mode: "));
18355 if (maxlen < 8)
18356 goto desc_size_fail;
18357 /* FIXME: Generate an error if descsz > 8 ? */
18358
18359 printf ("0x%016" BFD_VMA_FMT "x\n",
18360 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
18361 break;
18362
18363 case NT_VMS_LINKTIME:
18364 printf (_(" Link time: "));
18365 if (maxlen < 8)
18366 goto desc_size_fail;
18367 /* FIXME: Generate an error if descsz > 8 ? */
18368
18369 print_vms_time
18370 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
18371 printf ("\n");
18372 break;
18373
18374 case NT_VMS_PATCHTIME:
18375 printf (_(" Patch time: "));
18376 if (maxlen < 8)
18377 goto desc_size_fail;
18378 /* FIXME: Generate an error if descsz > 8 ? */
18379
18380 print_vms_time
18381 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
18382 printf ("\n");
18383 break;
18384
18385 case NT_VMS_ORIG_DYN:
18386 if (maxlen < 34)
18387 goto desc_size_fail;
18388
18389 printf (_(" Major id: %u, minor id: %u\n"),
18390 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
18391 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
18392 printf (_(" Last modified : "));
18393 print_vms_time
18394 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
18395 printf (_("\n Link flags : "));
18396 printf ("0x%016" BFD_VMA_FMT "x\n",
18397 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
18398 printf (_(" Header flags: 0x%08x\n"),
18399 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
18400 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
18401 break;
18402 #endif
18403
18404 case NT_VMS_IMGNAM:
18405 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
18406 break;
18407
18408 case NT_VMS_GSTNAM:
18409 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
18410 break;
18411
18412 case NT_VMS_IMGID:
18413 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
18414 break;
18415
18416 case NT_VMS_LINKID:
18417 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
18418 break;
18419
18420 default:
18421 return FALSE;
18422 }
18423
18424 return TRUE;
18425
18426 desc_size_fail:
18427 printf (_(" <corrupt - data size is too small>\n"));
18428 error (_("corrupt IA64 note: data size is too small\n"));
18429 return FALSE;
18430 }
18431
18432 /* Find the symbol associated with a build attribute that is attached
18433 to address OFFSET. If PNAME is non-NULL then store the name of
18434 the symbol (if found) in the provided pointer, Returns NULL if a
18435 symbol could not be found. */
18436
18437 static Elf_Internal_Sym *
18438 get_symbol_for_build_attribute (Filedata * filedata,
18439 unsigned long offset,
18440 bfd_boolean is_open_attr,
18441 const char ** pname)
18442 {
18443 static Filedata * saved_filedata = NULL;
18444 static char * strtab;
18445 static unsigned long strtablen;
18446 static Elf_Internal_Sym * symtab;
18447 static unsigned long nsyms;
18448 Elf_Internal_Sym * saved_sym = NULL;
18449 Elf_Internal_Sym * sym;
18450
18451 if (filedata->section_headers != NULL
18452 && (saved_filedata == NULL || filedata != saved_filedata))
18453 {
18454 Elf_Internal_Shdr * symsec;
18455
18456 /* Load the symbol and string sections. */
18457 for (symsec = filedata->section_headers;
18458 symsec < filedata->section_headers + filedata->file_header.e_shnum;
18459 symsec ++)
18460 {
18461 if (symsec->sh_type == SHT_SYMTAB)
18462 {
18463 symtab = GET_ELF_SYMBOLS (filedata, symsec, & nsyms);
18464
18465 if (symsec->sh_link < filedata->file_header.e_shnum)
18466 {
18467 Elf_Internal_Shdr * strtab_sec = filedata->section_headers + symsec->sh_link;
18468
18469 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
18470 1, strtab_sec->sh_size,
18471 _("string table"));
18472 strtablen = strtab != NULL ? strtab_sec->sh_size : 0;
18473 }
18474 }
18475 }
18476 saved_filedata = filedata;
18477 }
18478
18479 if (symtab == NULL || strtab == NULL)
18480 return NULL;
18481
18482 /* Find a symbol whose value matches offset. */
18483 for (sym = symtab; sym < symtab + nsyms; sym ++)
18484 if (sym->st_value == offset)
18485 {
18486 if (sym->st_name >= strtablen)
18487 /* Huh ? This should not happen. */
18488 continue;
18489
18490 if (strtab[sym->st_name] == 0)
18491 continue;
18492
18493 /* The AArch64 and ARM architectures define mapping symbols
18494 (eg $d, $x, $t) which we want to ignore. */
18495 if (strtab[sym->st_name] == '$'
18496 && strtab[sym->st_name + 1] != 0
18497 && strtab[sym->st_name + 2] == 0)
18498 continue;
18499
18500 if (is_open_attr)
18501 {
18502 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
18503 and FILE or OBJECT symbols over NOTYPE symbols. We skip
18504 FUNC symbols entirely. */
18505 switch (ELF_ST_TYPE (sym->st_info))
18506 {
18507 case STT_OBJECT:
18508 case STT_FILE:
18509 saved_sym = sym;
18510 if (sym->st_size)
18511 {
18512 /* If the symbol has a size associated
18513 with it then we can stop searching. */
18514 sym = symtab + nsyms;
18515 }
18516 continue;
18517
18518 case STT_FUNC:
18519 /* Ignore function symbols. */
18520 continue;
18521
18522 default:
18523 break;
18524 }
18525
18526 switch (ELF_ST_BIND (sym->st_info))
18527 {
18528 case STB_GLOBAL:
18529 if (saved_sym == NULL
18530 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
18531 saved_sym = sym;
18532 break;
18533
18534 case STB_LOCAL:
18535 if (saved_sym == NULL)
18536 saved_sym = sym;
18537 break;
18538
18539 default:
18540 break;
18541 }
18542 }
18543 else
18544 {
18545 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
18546 continue;
18547
18548 saved_sym = sym;
18549 break;
18550 }
18551 }
18552
18553 if (saved_sym && pname)
18554 * pname = strtab + saved_sym->st_name;
18555
18556 return saved_sym;
18557 }
18558
18559 /* Returns true iff addr1 and addr2 are in the same section. */
18560
18561 static bfd_boolean
18562 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
18563 {
18564 Elf_Internal_Shdr * a1;
18565 Elf_Internal_Shdr * a2;
18566
18567 a1 = find_section_by_address (filedata, addr1);
18568 a2 = find_section_by_address (filedata, addr2);
18569
18570 return a1 == a2 && a1 != NULL;
18571 }
18572
18573 static bfd_boolean
18574 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
18575 Filedata * filedata)
18576 {
18577 static unsigned long global_offset = 0;
18578 static unsigned long global_end = 0;
18579 static unsigned long func_offset = 0;
18580 static unsigned long func_end = 0;
18581
18582 Elf_Internal_Sym * sym;
18583 const char * name;
18584 unsigned long start;
18585 unsigned long end;
18586 bfd_boolean is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
18587
18588 switch (pnote->descsz)
18589 {
18590 case 0:
18591 /* A zero-length description means that the range of
18592 the previous note of the same type should be used. */
18593 if (is_open_attr)
18594 {
18595 if (global_end > global_offset)
18596 printf (_(" Applies to region from %#lx to %#lx\n"),
18597 global_offset, global_end);
18598 else
18599 printf (_(" Applies to region from %#lx\n"), global_offset);
18600 }
18601 else
18602 {
18603 if (func_end > func_offset)
18604 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
18605 else
18606 printf (_(" Applies to region from %#lx\n"), func_offset);
18607 }
18608 return TRUE;
18609
18610 case 4:
18611 start = byte_get ((unsigned char *) pnote->descdata, 4);
18612 end = 0;
18613 break;
18614
18615 case 8:
18616 if (is_32bit_elf)
18617 {
18618 /* FIXME: We should check that version 3+ notes are being used here... */
18619 start = byte_get ((unsigned char *) pnote->descdata, 4);
18620 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
18621 }
18622 else
18623 {
18624 start = byte_get ((unsigned char *) pnote->descdata, 8);
18625 end = 0;
18626 }
18627 break;
18628
18629 case 16:
18630 start = byte_get ((unsigned char *) pnote->descdata, 8);
18631 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
18632 break;
18633
18634 default:
18635 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
18636 printf (_(" <invalid descsz>"));
18637 return FALSE;
18638 }
18639
18640 name = NULL;
18641 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
18642 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
18643 in order to avoid them being confused with the start address of the
18644 first function in the file... */
18645 if (sym == NULL && is_open_attr)
18646 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
18647 & name);
18648
18649 if (end == 0 && sym != NULL && sym->st_size > 0)
18650 end = start + sym->st_size;
18651
18652 if (is_open_attr)
18653 {
18654 /* FIXME: Need to properly allow for section alignment.
18655 16 is just the alignment used on x86_64. */
18656 if (global_end > 0
18657 && start > BFD_ALIGN (global_end, 16)
18658 /* Build notes are not guaranteed to be organised in order of
18659 increasing address, but we should find the all of the notes
18660 for one section in the same place. */
18661 && same_section (filedata, start, global_end))
18662 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
18663 global_end + 1, start - 1);
18664
18665 printf (_(" Applies to region from %#lx"), start);
18666 global_offset = start;
18667
18668 if (end)
18669 {
18670 printf (_(" to %#lx"), end);
18671 global_end = end;
18672 }
18673 }
18674 else
18675 {
18676 printf (_(" Applies to region from %#lx"), start);
18677 func_offset = start;
18678
18679 if (end)
18680 {
18681 printf (_(" to %#lx"), end);
18682 func_end = end;
18683 }
18684 }
18685
18686 if (sym && name)
18687 printf (_(" (%s)"), name);
18688
18689 printf ("\n");
18690 return TRUE;
18691 }
18692
18693 static bfd_boolean
18694 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
18695 {
18696 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
18697 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
18698 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
18699 char name_type;
18700 char name_attribute;
18701 const char * expected_types;
18702 const char * name = pnote->namedata;
18703 const char * text;
18704 signed int left;
18705
18706 if (name == NULL || pnote->namesz < 2)
18707 {
18708 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
18709 print_symbol (-20, _(" <corrupt name>"));
18710 return FALSE;
18711 }
18712
18713 if (do_wide)
18714 left = 28;
18715 else
18716 left = 20;
18717
18718 /* Version 2 of the spec adds a "GA" prefix to the name field. */
18719 if (name[0] == 'G' && name[1] == 'A')
18720 {
18721 if (pnote->namesz < 4)
18722 {
18723 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
18724 print_symbol (-20, _(" <corrupt name>"));
18725 return FALSE;
18726 }
18727
18728 printf ("GA");
18729 name += 2;
18730 left -= 2;
18731 }
18732
18733 switch ((name_type = * name))
18734 {
18735 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
18736 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
18737 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
18738 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
18739 printf ("%c", * name);
18740 left --;
18741 break;
18742 default:
18743 error (_("unrecognised attribute type in name field: %d\n"), name_type);
18744 print_symbol (-20, _("<unknown name type>"));
18745 return FALSE;
18746 }
18747
18748 ++ name;
18749 text = NULL;
18750
18751 switch ((name_attribute = * name))
18752 {
18753 case GNU_BUILD_ATTRIBUTE_VERSION:
18754 text = _("<version>");
18755 expected_types = string_expected;
18756 ++ name;
18757 break;
18758 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
18759 text = _("<stack prot>");
18760 expected_types = "!+*";
18761 ++ name;
18762 break;
18763 case GNU_BUILD_ATTRIBUTE_RELRO:
18764 text = _("<relro>");
18765 expected_types = bool_expected;
18766 ++ name;
18767 break;
18768 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
18769 text = _("<stack size>");
18770 expected_types = number_expected;
18771 ++ name;
18772 break;
18773 case GNU_BUILD_ATTRIBUTE_TOOL:
18774 text = _("<tool>");
18775 expected_types = string_expected;
18776 ++ name;
18777 break;
18778 case GNU_BUILD_ATTRIBUTE_ABI:
18779 text = _("<ABI>");
18780 expected_types = "$*";
18781 ++ name;
18782 break;
18783 case GNU_BUILD_ATTRIBUTE_PIC:
18784 text = _("<PIC>");
18785 expected_types = number_expected;
18786 ++ name;
18787 break;
18788 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
18789 text = _("<short enum>");
18790 expected_types = bool_expected;
18791 ++ name;
18792 break;
18793 default:
18794 if (ISPRINT (* name))
18795 {
18796 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
18797
18798 if (len > left && ! do_wide)
18799 len = left;
18800 printf ("%.*s:", len, name);
18801 left -= len;
18802 name += len;
18803 }
18804 else
18805 {
18806 static char tmpbuf [128];
18807
18808 error (_("unrecognised byte in name field: %d\n"), * name);
18809 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
18810 text = tmpbuf;
18811 name ++;
18812 }
18813 expected_types = "*$!+";
18814 break;
18815 }
18816
18817 if (text)
18818 left -= printf ("%s", text);
18819
18820 if (strchr (expected_types, name_type) == NULL)
18821 warn (_("attribute does not have an expected type (%c)\n"), name_type);
18822
18823 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
18824 {
18825 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
18826 (unsigned long) pnote->namesz,
18827 (long) (name - pnote->namedata));
18828 return FALSE;
18829 }
18830
18831 if (left < 1 && ! do_wide)
18832 return TRUE;
18833
18834 switch (name_type)
18835 {
18836 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
18837 {
18838 unsigned int bytes;
18839 unsigned long long val = 0;
18840 unsigned int shift = 0;
18841 char * decoded = NULL;
18842
18843 bytes = pnote->namesz - (name - pnote->namedata);
18844 if (bytes > 0)
18845 /* The -1 is because the name field is always 0 terminated, and we
18846 want to be able to ensure that the shift in the while loop below
18847 will not overflow. */
18848 -- bytes;
18849
18850 if (bytes > sizeof (val))
18851 {
18852 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
18853 bytes);
18854 bytes = sizeof (val);
18855 }
18856 /* We do not bother to warn if bytes == 0 as this can
18857 happen with some early versions of the gcc plugin. */
18858
18859 while (bytes --)
18860 {
18861 unsigned long byte = (* name ++) & 0xff;
18862
18863 val |= byte << shift;
18864 shift += 8;
18865 }
18866
18867 switch (name_attribute)
18868 {
18869 case GNU_BUILD_ATTRIBUTE_PIC:
18870 switch (val)
18871 {
18872 case 0: decoded = "static"; break;
18873 case 1: decoded = "pic"; break;
18874 case 2: decoded = "PIC"; break;
18875 case 3: decoded = "pie"; break;
18876 case 4: decoded = "PIE"; break;
18877 default: break;
18878 }
18879 break;
18880 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
18881 switch (val)
18882 {
18883 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
18884 case 0: decoded = "off"; break;
18885 case 1: decoded = "on"; break;
18886 case 2: decoded = "all"; break;
18887 case 3: decoded = "strong"; break;
18888 case 4: decoded = "explicit"; break;
18889 default: break;
18890 }
18891 break;
18892 default:
18893 break;
18894 }
18895
18896 if (decoded != NULL)
18897 {
18898 print_symbol (-left, decoded);
18899 left = 0;
18900 }
18901 else if (val == 0)
18902 {
18903 printf ("0x0");
18904 left -= 3;
18905 }
18906 else
18907 {
18908 if (do_wide)
18909 left -= printf ("0x%llx", val);
18910 else
18911 left -= printf ("0x%-.*llx", left, val);
18912 }
18913 }
18914 break;
18915 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
18916 left -= print_symbol (- left, name);
18917 break;
18918 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
18919 left -= print_symbol (- left, "true");
18920 break;
18921 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
18922 left -= print_symbol (- left, "false");
18923 break;
18924 }
18925
18926 if (do_wide && left > 0)
18927 printf ("%-*s", left, " ");
18928
18929 return TRUE;
18930 }
18931
18932 /* Note that by the ELF standard, the name field is already null byte
18933 terminated, and namesz includes the terminating null byte.
18934 I.E. the value of namesz for the name "FSF" is 4.
18935
18936 If the value of namesz is zero, there is no name present. */
18937
18938 static bfd_boolean
18939 process_note (Elf_Internal_Note * pnote,
18940 Filedata * filedata)
18941 {
18942 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
18943 const char * nt;
18944
18945 if (pnote->namesz == 0)
18946 /* If there is no note name, then use the default set of
18947 note type strings. */
18948 nt = get_note_type (filedata, pnote->type);
18949
18950 else if (const_strneq (pnote->namedata, "GNU"))
18951 /* GNU-specific object file notes. */
18952 nt = get_gnu_elf_note_type (pnote->type);
18953
18954 else if (const_strneq (pnote->namedata, "FreeBSD"))
18955 /* FreeBSD-specific core file notes. */
18956 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
18957
18958 else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
18959 /* NetBSD-specific core file notes. */
18960 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
18961
18962 else if (const_strneq (pnote->namedata, "NetBSD"))
18963 /* NetBSD-specific core file notes. */
18964 return process_netbsd_elf_note (pnote);
18965
18966 else if (strneq (pnote->namedata, "SPU/", 4))
18967 {
18968 /* SPU-specific core file notes. */
18969 nt = pnote->namedata + 4;
18970 name = "SPU";
18971 }
18972
18973 else if (const_strneq (pnote->namedata, "IPF/VMS"))
18974 /* VMS/ia64-specific file notes. */
18975 nt = get_ia64_vms_note_type (pnote->type);
18976
18977 else if (const_strneq (pnote->namedata, "stapsdt"))
18978 nt = get_stapsdt_note_type (pnote->type);
18979
18980 else
18981 /* Don't recognize this note name; just use the default set of
18982 note type strings. */
18983 nt = get_note_type (filedata, pnote->type);
18984
18985 printf (" ");
18986
18987 if (((const_strneq (pnote->namedata, "GA")
18988 && strchr ("*$!+", pnote->namedata[2]) != NULL)
18989 || strchr ("*$!+", pnote->namedata[0]) != NULL)
18990 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
18991 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
18992 print_gnu_build_attribute_name (pnote);
18993 else
18994 print_symbol (-20, name);
18995
18996 if (do_wide)
18997 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
18998 else
18999 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
19000
19001 if (const_strneq (pnote->namedata, "IPF/VMS"))
19002 return print_ia64_vms_note (pnote);
19003 else if (const_strneq (pnote->namedata, "GNU"))
19004 return print_gnu_note (filedata, pnote);
19005 else if (const_strneq (pnote->namedata, "stapsdt"))
19006 return print_stapsdt_note (pnote);
19007 else if (const_strneq (pnote->namedata, "CORE"))
19008 return print_core_note (pnote);
19009 else if (((const_strneq (pnote->namedata, "GA")
19010 && strchr ("*$!+", pnote->namedata[2]) != NULL)
19011 || strchr ("*$!+", pnote->namedata[0]) != NULL)
19012 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
19013 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
19014 return print_gnu_build_attribute_description (pnote, filedata);
19015
19016 if (pnote->descsz)
19017 {
19018 unsigned long i;
19019
19020 printf (_(" description data: "));
19021 for (i = 0; i < pnote->descsz; i++)
19022 printf ("%02x ", pnote->descdata[i]);
19023 if (!do_wide)
19024 printf ("\n");
19025 }
19026
19027 if (do_wide)
19028 printf ("\n");
19029
19030 return TRUE;
19031 }
19032
19033 static bfd_boolean
19034 process_notes_at (Filedata * filedata,
19035 Elf_Internal_Shdr * section,
19036 bfd_vma offset,
19037 bfd_vma length,
19038 bfd_vma align)
19039 {
19040 Elf_External_Note * pnotes;
19041 Elf_External_Note * external;
19042 char * end;
19043 bfd_boolean res = TRUE;
19044
19045 if (length <= 0)
19046 return FALSE;
19047
19048 if (section)
19049 {
19050 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
19051 if (pnotes)
19052 {
19053 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
19054 return FALSE;
19055 }
19056 }
19057 else
19058 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
19059 _("notes"));
19060
19061 if (pnotes == NULL)
19062 return FALSE;
19063
19064 external = pnotes;
19065
19066 if (section)
19067 printf (_("\nDisplaying notes found in: %s\n"), printable_section_name (filedata, section));
19068 else
19069 printf (_("\nDisplaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
19070 (unsigned long) offset, (unsigned long) length);
19071
19072 /* NB: Some note sections may have alignment value of 0 or 1. gABI
19073 specifies that notes should be aligned to 4 bytes in 32-bit
19074 objects and to 8 bytes in 64-bit objects. As a Linux extension,
19075 we also support 4 byte alignment in 64-bit objects. If section
19076 alignment is less than 4, we treate alignment as 4 bytes. */
19077 if (align < 4)
19078 align = 4;
19079 else if (align != 4 && align != 8)
19080 {
19081 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
19082 (long) align);
19083 return FALSE;
19084 }
19085
19086 printf (_(" %-20s %10s\tDescription\n"), _("Owner"), _("Data size"));
19087
19088 end = (char *) pnotes + length;
19089 while ((char *) external < end)
19090 {
19091 Elf_Internal_Note inote;
19092 size_t min_notesz;
19093 char * next;
19094 char * temp = NULL;
19095 size_t data_remaining = end - (char *) external;
19096
19097 if (!is_ia64_vms (filedata))
19098 {
19099 /* PR binutils/15191
19100 Make sure that there is enough data to read. */
19101 min_notesz = offsetof (Elf_External_Note, name);
19102 if (data_remaining < min_notesz)
19103 {
19104 warn (ngettext ("Corrupt note: only %ld byte remains, "
19105 "not enough for a full note\n",
19106 "Corrupt note: only %ld bytes remain, "
19107 "not enough for a full note\n",
19108 data_remaining),
19109 (long) data_remaining);
19110 break;
19111 }
19112 data_remaining -= min_notesz;
19113
19114 inote.type = BYTE_GET (external->type);
19115 inote.namesz = BYTE_GET (external->namesz);
19116 inote.namedata = external->name;
19117 inote.descsz = BYTE_GET (external->descsz);
19118 inote.descdata = ((char *) external
19119 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
19120 inote.descpos = offset + (inote.descdata - (char *) pnotes);
19121 next = ((char *) external
19122 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
19123 }
19124 else
19125 {
19126 Elf64_External_VMS_Note *vms_external;
19127
19128 /* PR binutils/15191
19129 Make sure that there is enough data to read. */
19130 min_notesz = offsetof (Elf64_External_VMS_Note, name);
19131 if (data_remaining < min_notesz)
19132 {
19133 warn (ngettext ("Corrupt note: only %ld byte remains, "
19134 "not enough for a full note\n",
19135 "Corrupt note: only %ld bytes remain, "
19136 "not enough for a full note\n",
19137 data_remaining),
19138 (long) data_remaining);
19139 break;
19140 }
19141 data_remaining -= min_notesz;
19142
19143 vms_external = (Elf64_External_VMS_Note *) external;
19144 inote.type = BYTE_GET (vms_external->type);
19145 inote.namesz = BYTE_GET (vms_external->namesz);
19146 inote.namedata = vms_external->name;
19147 inote.descsz = BYTE_GET (vms_external->descsz);
19148 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
19149 inote.descpos = offset + (inote.descdata - (char *) pnotes);
19150 next = inote.descdata + align_power (inote.descsz, 3);
19151 }
19152
19153 /* PR 17531: file: 3443835e. */
19154 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
19155 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
19156 || (size_t) (inote.descdata - inote.namedata) > data_remaining
19157 || (size_t) (next - inote.descdata) < inote.descsz
19158 || ((size_t) (next - inote.descdata)
19159 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
19160 {
19161 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
19162 (unsigned long) ((char *) external - (char *) pnotes));
19163 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
19164 inote.type, inote.namesz, inote.descsz, (int) align);
19165 break;
19166 }
19167
19168 external = (Elf_External_Note *) next;
19169
19170 /* Verify that name is null terminated. It appears that at least
19171 one version of Linux (RedHat 6.0) generates corefiles that don't
19172 comply with the ELF spec by failing to include the null byte in
19173 namesz. */
19174 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
19175 {
19176 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
19177 {
19178 temp = (char *) malloc (inote.namesz + 1);
19179 if (temp == NULL)
19180 {
19181 error (_("Out of memory allocating space for inote name\n"));
19182 res = FALSE;
19183 break;
19184 }
19185
19186 memcpy (temp, inote.namedata, inote.namesz);
19187 inote.namedata = temp;
19188 }
19189 inote.namedata[inote.namesz] = 0;
19190 }
19191
19192 if (! process_note (& inote, filedata))
19193 res = FALSE;
19194
19195 if (temp != NULL)
19196 {
19197 free (temp);
19198 temp = NULL;
19199 }
19200 }
19201
19202 free (pnotes);
19203
19204 return res;
19205 }
19206
19207 static bfd_boolean
19208 process_corefile_note_segments (Filedata * filedata)
19209 {
19210 Elf_Internal_Phdr * segment;
19211 unsigned int i;
19212 bfd_boolean res = TRUE;
19213
19214 if (! get_program_headers (filedata))
19215 return TRUE;
19216
19217 for (i = 0, segment = filedata->program_headers;
19218 i < filedata->file_header.e_phnum;
19219 i++, segment++)
19220 {
19221 if (segment->p_type == PT_NOTE)
19222 if (! process_notes_at (filedata, NULL,
19223 (bfd_vma) segment->p_offset,
19224 (bfd_vma) segment->p_filesz,
19225 (bfd_vma) segment->p_align))
19226 res = FALSE;
19227 }
19228
19229 return res;
19230 }
19231
19232 static bfd_boolean
19233 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
19234 {
19235 Elf_External_Note * pnotes;
19236 Elf_External_Note * external;
19237 char * end;
19238 bfd_boolean res = TRUE;
19239
19240 if (length <= 0)
19241 return FALSE;
19242
19243 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
19244 _("v850 notes"));
19245 if (pnotes == NULL)
19246 return FALSE;
19247
19248 external = pnotes;
19249 end = (char*) pnotes + length;
19250
19251 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
19252 (unsigned long) offset, (unsigned long) length);
19253
19254 while ((char *) external + sizeof (Elf_External_Note) < end)
19255 {
19256 Elf_External_Note * next;
19257 Elf_Internal_Note inote;
19258
19259 inote.type = BYTE_GET (external->type);
19260 inote.namesz = BYTE_GET (external->namesz);
19261 inote.namedata = external->name;
19262 inote.descsz = BYTE_GET (external->descsz);
19263 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
19264 inote.descpos = offset + (inote.descdata - (char *) pnotes);
19265
19266 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
19267 {
19268 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
19269 inote.descdata = inote.namedata;
19270 inote.namesz = 0;
19271 }
19272
19273 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
19274
19275 if ( ((char *) next > end)
19276 || ((char *) next < (char *) pnotes))
19277 {
19278 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
19279 (unsigned long) ((char *) external - (char *) pnotes));
19280 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
19281 inote.type, inote.namesz, inote.descsz);
19282 break;
19283 }
19284
19285 external = next;
19286
19287 /* Prevent out-of-bounds indexing. */
19288 if ( inote.namedata + inote.namesz > end
19289 || inote.namedata + inote.namesz < inote.namedata)
19290 {
19291 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
19292 (unsigned long) ((char *) external - (char *) pnotes));
19293 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
19294 inote.type, inote.namesz, inote.descsz);
19295 break;
19296 }
19297
19298 printf (" %s: ", get_v850_elf_note_type (inote.type));
19299
19300 if (! print_v850_note (& inote))
19301 {
19302 res = FALSE;
19303 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
19304 inote.namesz, inote.descsz);
19305 }
19306 }
19307
19308 free (pnotes);
19309
19310 return res;
19311 }
19312
19313 static bfd_boolean
19314 process_note_sections (Filedata * filedata)
19315 {
19316 Elf_Internal_Shdr * section;
19317 unsigned long i;
19318 unsigned int n = 0;
19319 bfd_boolean res = TRUE;
19320
19321 for (i = 0, section = filedata->section_headers;
19322 i < filedata->file_header.e_shnum && section != NULL;
19323 i++, section++)
19324 {
19325 if (section->sh_type == SHT_NOTE)
19326 {
19327 if (! process_notes_at (filedata, section,
19328 (bfd_vma) section->sh_offset,
19329 (bfd_vma) section->sh_size,
19330 (bfd_vma) section->sh_addralign))
19331 res = FALSE;
19332 n++;
19333 }
19334
19335 if (( filedata->file_header.e_machine == EM_V800
19336 || filedata->file_header.e_machine == EM_V850
19337 || filedata->file_header.e_machine == EM_CYGNUS_V850)
19338 && section->sh_type == SHT_RENESAS_INFO)
19339 {
19340 if (! process_v850_notes (filedata,
19341 (bfd_vma) section->sh_offset,
19342 (bfd_vma) section->sh_size))
19343 res = FALSE;
19344 n++;
19345 }
19346 }
19347
19348 if (n == 0)
19349 /* Try processing NOTE segments instead. */
19350 return process_corefile_note_segments (filedata);
19351
19352 return res;
19353 }
19354
19355 static bfd_boolean
19356 process_notes (Filedata * filedata)
19357 {
19358 /* If we have not been asked to display the notes then do nothing. */
19359 if (! do_notes)
19360 return TRUE;
19361
19362 if (filedata->file_header.e_type != ET_CORE)
19363 return process_note_sections (filedata);
19364
19365 /* No program headers means no NOTE segment. */
19366 if (filedata->file_header.e_phnum > 0)
19367 return process_corefile_note_segments (filedata);
19368
19369 printf (_("No note segments present in the core file.\n"));
19370 return TRUE;
19371 }
19372
19373 static unsigned char *
19374 display_public_gnu_attributes (unsigned char * start,
19375 const unsigned char * const end)
19376 {
19377 printf (_(" Unknown GNU attribute: %s\n"), start);
19378
19379 start += strnlen ((char *) start, end - start);
19380 display_raw_attribute (start, end);
19381
19382 return (unsigned char *) end;
19383 }
19384
19385 static unsigned char *
19386 display_generic_attribute (unsigned char * start,
19387 unsigned int tag,
19388 const unsigned char * const end)
19389 {
19390 if (tag == 0)
19391 return (unsigned char *) end;
19392
19393 return display_tag_value (tag, start, end);
19394 }
19395
19396 static bfd_boolean
19397 process_arch_specific (Filedata * filedata)
19398 {
19399 if (! do_arch)
19400 return TRUE;
19401
19402 switch (filedata->file_header.e_machine)
19403 {
19404 case EM_ARC:
19405 case EM_ARC_COMPACT:
19406 case EM_ARC_COMPACT2:
19407 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
19408 display_arc_attribute,
19409 display_generic_attribute);
19410 case EM_ARM:
19411 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
19412 display_arm_attribute,
19413 display_generic_attribute);
19414
19415 case EM_MIPS:
19416 case EM_MIPS_RS3_LE:
19417 return process_mips_specific (filedata);
19418
19419 case EM_MSP430:
19420 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
19421 display_msp430x_attribute,
19422 display_generic_attribute);
19423
19424 case EM_RISCV:
19425 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
19426 display_riscv_attribute,
19427 display_generic_attribute);
19428
19429 case EM_NDS32:
19430 return process_nds32_specific (filedata);
19431
19432 case EM_PPC:
19433 case EM_PPC64:
19434 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
19435 display_power_gnu_attribute);
19436
19437 case EM_S390:
19438 case EM_S390_OLD:
19439 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
19440 display_s390_gnu_attribute);
19441
19442 case EM_SPARC:
19443 case EM_SPARC32PLUS:
19444 case EM_SPARCV9:
19445 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
19446 display_sparc_gnu_attribute);
19447
19448 case EM_TI_C6000:
19449 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
19450 display_tic6x_attribute,
19451 display_generic_attribute);
19452
19453 default:
19454 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
19455 display_public_gnu_attributes,
19456 display_generic_attribute);
19457 }
19458 }
19459
19460 static bfd_boolean
19461 get_file_header (Filedata * filedata)
19462 {
19463 /* Read in the identity array. */
19464 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
19465 return FALSE;
19466
19467 /* Determine how to read the rest of the header. */
19468 switch (filedata->file_header.e_ident[EI_DATA])
19469 {
19470 default:
19471 case ELFDATANONE:
19472 case ELFDATA2LSB:
19473 byte_get = byte_get_little_endian;
19474 byte_put = byte_put_little_endian;
19475 break;
19476 case ELFDATA2MSB:
19477 byte_get = byte_get_big_endian;
19478 byte_put = byte_put_big_endian;
19479 break;
19480 }
19481
19482 /* For now we only support 32 bit and 64 bit ELF files. */
19483 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
19484
19485 /* Read in the rest of the header. */
19486 if (is_32bit_elf)
19487 {
19488 Elf32_External_Ehdr ehdr32;
19489
19490 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
19491 return FALSE;
19492
19493 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
19494 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
19495 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
19496 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
19497 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
19498 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
19499 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
19500 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
19501 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
19502 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
19503 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
19504 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
19505 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
19506 }
19507 else
19508 {
19509 Elf64_External_Ehdr ehdr64;
19510
19511 /* If we have been compiled with sizeof (bfd_vma) == 4, then
19512 we will not be able to cope with the 64bit data found in
19513 64 ELF files. Detect this now and abort before we start
19514 overwriting things. */
19515 if (sizeof (bfd_vma) < 8)
19516 {
19517 error (_("This instance of readelf has been built without support for a\n\
19518 64 bit data type and so it cannot read 64 bit ELF files.\n"));
19519 return FALSE;
19520 }
19521
19522 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
19523 return FALSE;
19524
19525 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
19526 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
19527 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
19528 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
19529 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
19530 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
19531 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
19532 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
19533 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
19534 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
19535 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
19536 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
19537 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
19538 }
19539
19540 if (filedata->file_header.e_shoff)
19541 {
19542 /* There may be some extensions in the first section header. Don't
19543 bomb if we can't read it. */
19544 if (is_32bit_elf)
19545 get_32bit_section_headers (filedata, TRUE);
19546 else
19547 get_64bit_section_headers (filedata, TRUE);
19548 }
19549
19550 return TRUE;
19551 }
19552
19553 static void
19554 close_file (Filedata * filedata)
19555 {
19556 if (filedata)
19557 {
19558 if (filedata->handle)
19559 fclose (filedata->handle);
19560 free (filedata);
19561 }
19562 }
19563
19564 void
19565 close_debug_file (void * data)
19566 {
19567 close_file ((Filedata *) data);
19568 }
19569
19570 static Filedata *
19571 open_file (const char * pathname)
19572 {
19573 struct stat statbuf;
19574 Filedata * filedata = NULL;
19575
19576 if (stat (pathname, & statbuf) < 0
19577 || ! S_ISREG (statbuf.st_mode))
19578 goto fail;
19579
19580 filedata = calloc (1, sizeof * filedata);
19581 if (filedata == NULL)
19582 goto fail;
19583
19584 filedata->handle = fopen (pathname, "rb");
19585 if (filedata->handle == NULL)
19586 goto fail;
19587
19588 filedata->file_size = (bfd_size_type) statbuf.st_size;
19589 filedata->file_name = pathname;
19590
19591 if (! get_file_header (filedata))
19592 goto fail;
19593
19594 if (filedata->file_header.e_shoff)
19595 {
19596 bfd_boolean res;
19597
19598 /* Read the section headers again, this time for real. */
19599 if (is_32bit_elf)
19600 res = get_32bit_section_headers (filedata, FALSE);
19601 else
19602 res = get_64bit_section_headers (filedata, FALSE);
19603
19604 if (!res)
19605 goto fail;
19606 }
19607
19608 return filedata;
19609
19610 fail:
19611 if (filedata)
19612 {
19613 if (filedata->handle)
19614 fclose (filedata->handle);
19615 free (filedata);
19616 }
19617 return NULL;
19618 }
19619
19620 void *
19621 open_debug_file (const char * pathname)
19622 {
19623 return open_file (pathname);
19624 }
19625
19626 /* Process one ELF object file according to the command line options.
19627 This file may actually be stored in an archive. The file is
19628 positioned at the start of the ELF object. Returns TRUE if no
19629 problems were encountered, FALSE otherwise. */
19630
19631 static bfd_boolean
19632 process_object (Filedata * filedata)
19633 {
19634 bfd_boolean have_separate_files;
19635 unsigned int i;
19636 bfd_boolean res = TRUE;
19637
19638 if (! get_file_header (filedata))
19639 {
19640 error (_("%s: Failed to read file header\n"), filedata->file_name);
19641 return FALSE;
19642 }
19643
19644 /* Initialise per file variables. */
19645 for (i = ARRAY_SIZE (version_info); i--;)
19646 version_info[i] = 0;
19647
19648 for (i = ARRAY_SIZE (dynamic_info); i--;)
19649 dynamic_info[i] = 0;
19650 dynamic_info_DT_GNU_HASH = 0;
19651
19652 /* Process the file. */
19653 if (show_name)
19654 printf (_("\nFile: %s\n"), filedata->file_name);
19655
19656 /* Initialise the dump_sects array from the cmdline_dump_sects array.
19657 Note we do this even if cmdline_dump_sects is empty because we
19658 must make sure that the dump_sets array is zeroed out before each
19659 object file is processed. */
19660 if (filedata->num_dump_sects > cmdline.num_dump_sects)
19661 memset (filedata->dump_sects, 0, filedata->num_dump_sects * sizeof (* filedata->dump_sects));
19662
19663 if (cmdline.num_dump_sects > 0)
19664 {
19665 if (filedata->num_dump_sects == 0)
19666 /* A sneaky way of allocating the dump_sects array. */
19667 request_dump_bynumber (filedata, cmdline.num_dump_sects, 0);
19668
19669 assert (filedata->num_dump_sects >= cmdline.num_dump_sects);
19670 memcpy (filedata->dump_sects, cmdline.dump_sects,
19671 cmdline.num_dump_sects * sizeof (* filedata->dump_sects));
19672 }
19673
19674 if (! process_file_header (filedata))
19675 return FALSE;
19676
19677 if (! process_section_headers (filedata))
19678 {
19679 /* Without loaded section headers we cannot process lots of things. */
19680 do_unwind = do_version = do_dump = do_arch = FALSE;
19681
19682 if (! do_using_dynamic)
19683 do_syms = do_dyn_syms = do_reloc = FALSE;
19684 }
19685
19686 if (! process_section_groups (filedata))
19687 /* Without loaded section groups we cannot process unwind. */
19688 do_unwind = FALSE;
19689
19690 if (process_program_headers (filedata))
19691 process_dynamic_section (filedata);
19692 else
19693 res = FALSE;
19694
19695 if (! process_relocs (filedata))
19696 res = FALSE;
19697
19698 if (! process_unwind (filedata))
19699 res = FALSE;
19700
19701 if (! process_symbol_table (filedata))
19702 res = FALSE;
19703
19704 if (! process_syminfo (filedata))
19705 res = FALSE;
19706
19707 if (! process_version_sections (filedata))
19708 res = FALSE;
19709
19710 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
19711 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
19712 else
19713 have_separate_files = FALSE;
19714
19715 if (! process_section_contents (filedata))
19716 res = FALSE;
19717
19718 if (have_separate_files)
19719 {
19720 separate_info * d;
19721
19722 for (d = first_separate_info; d != NULL; d = d->next)
19723 {
19724 if (! process_section_headers (d->handle))
19725 res = FALSE;
19726 else if (! process_section_contents (d->handle))
19727 res = FALSE;
19728 }
19729
19730 /* The file handles are closed by the call to free_debug_memory() below. */
19731 }
19732
19733 if (! process_notes (filedata))
19734 res = FALSE;
19735
19736 if (! process_gnu_liblist (filedata))
19737 res = FALSE;
19738
19739 if (! process_arch_specific (filedata))
19740 res = FALSE;
19741
19742 free (filedata->program_headers);
19743 filedata->program_headers = NULL;
19744
19745 free (filedata->section_headers);
19746 filedata->section_headers = NULL;
19747
19748 free (filedata->string_table);
19749 filedata->string_table = NULL;
19750 filedata->string_table_length = 0;
19751
19752 if (dynamic_strings)
19753 {
19754 free (dynamic_strings);
19755 dynamic_strings = NULL;
19756 dynamic_strings_length = 0;
19757 }
19758
19759 if (dynamic_symbols)
19760 {
19761 free (dynamic_symbols);
19762 dynamic_symbols = NULL;
19763 num_dynamic_syms = 0;
19764 }
19765
19766 if (dynamic_syminfo)
19767 {
19768 free (dynamic_syminfo);
19769 dynamic_syminfo = NULL;
19770 }
19771
19772 if (dynamic_section)
19773 {
19774 free (dynamic_section);
19775 dynamic_section = NULL;
19776 }
19777
19778 if (section_headers_groups)
19779 {
19780 free (section_headers_groups);
19781 section_headers_groups = NULL;
19782 }
19783
19784 if (section_groups)
19785 {
19786 struct group_list * g;
19787 struct group_list * next;
19788
19789 for (i = 0; i < group_count; i++)
19790 {
19791 for (g = section_groups [i].root; g != NULL; g = next)
19792 {
19793 next = g->next;
19794 free (g);
19795 }
19796 }
19797
19798 free (section_groups);
19799 section_groups = NULL;
19800 }
19801
19802 free_debug_memory ();
19803
19804 return res;
19805 }
19806
19807 /* Process an ELF archive.
19808 On entry the file is positioned just after the ARMAG string.
19809 Returns TRUE upon success, FALSE otherwise. */
19810
19811 static bfd_boolean
19812 process_archive (Filedata * filedata, bfd_boolean is_thin_archive)
19813 {
19814 struct archive_info arch;
19815 struct archive_info nested_arch;
19816 size_t got;
19817 bfd_boolean ret = TRUE;
19818
19819 show_name = TRUE;
19820
19821 /* The ARCH structure is used to hold information about this archive. */
19822 arch.file_name = NULL;
19823 arch.file = NULL;
19824 arch.index_array = NULL;
19825 arch.sym_table = NULL;
19826 arch.longnames = NULL;
19827
19828 /* The NESTED_ARCH structure is used as a single-item cache of information
19829 about a nested archive (when members of a thin archive reside within
19830 another regular archive file). */
19831 nested_arch.file_name = NULL;
19832 nested_arch.file = NULL;
19833 nested_arch.index_array = NULL;
19834 nested_arch.sym_table = NULL;
19835 nested_arch.longnames = NULL;
19836
19837 if (setup_archive (&arch, filedata->file_name, filedata->handle,
19838 is_thin_archive, do_archive_index) != 0)
19839 {
19840 ret = FALSE;
19841 goto out;
19842 }
19843
19844 if (do_archive_index)
19845 {
19846 if (arch.sym_table == NULL)
19847 error (_("%s: unable to dump the index as none was found\n"), filedata->file_name);
19848 else
19849 {
19850 unsigned long i, l;
19851 unsigned long current_pos;
19852
19853 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes in the symbol table)\n"),
19854 filedata->file_name, (unsigned long) arch.index_num, arch.sym_size);
19855
19856 current_pos = ftell (filedata->handle);
19857
19858 for (i = l = 0; i < arch.index_num; i++)
19859 {
19860 if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
19861 {
19862 char * member_name;
19863
19864 member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
19865
19866 if (member_name != NULL)
19867 {
19868 char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
19869
19870 if (qualified_name != NULL)
19871 {
19872 printf (_("Contents of binary %s at offset "), qualified_name);
19873 (void) print_vma (arch.index_array[i], PREFIX_HEX);
19874 putchar ('\n');
19875 free (qualified_name);
19876 }
19877 }
19878 }
19879
19880 if (l >= arch.sym_size)
19881 {
19882 error (_("%s: end of the symbol table reached before the end of the index\n"),
19883 filedata->file_name);
19884 ret = FALSE;
19885 break;
19886 }
19887 /* PR 17531: file: 0b6630b2. */
19888 printf ("\t%.*s\n", (int) (arch.sym_size - l), arch.sym_table + l);
19889 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
19890 }
19891
19892 if (arch.uses_64bit_indices)
19893 l = (l + 7) & ~ 7;
19894 else
19895 l += l & 1;
19896
19897 if (l < arch.sym_size)
19898 {
19899 error (ngettext ("%s: %ld byte remains in the symbol table, "
19900 "but without corresponding entries in "
19901 "the index table\n",
19902 "%s: %ld bytes remain in the symbol table, "
19903 "but without corresponding entries in "
19904 "the index table\n",
19905 arch.sym_size - l),
19906 filedata->file_name, arch.sym_size - l);
19907 ret = FALSE;
19908 }
19909
19910 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
19911 {
19912 error (_("%s: failed to seek back to start of object files in the archive\n"),
19913 filedata->file_name);
19914 ret = FALSE;
19915 goto out;
19916 }
19917 }
19918
19919 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
19920 && !do_segments && !do_header && !do_dump && !do_version
19921 && !do_histogram && !do_debugging && !do_arch && !do_notes
19922 && !do_section_groups && !do_dyn_syms)
19923 {
19924 ret = TRUE; /* Archive index only. */
19925 goto out;
19926 }
19927 }
19928
19929 while (1)
19930 {
19931 char * name;
19932 size_t namelen;
19933 char * qualified_name;
19934
19935 /* Read the next archive header. */
19936 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
19937 {
19938 error (_("%s: failed to seek to next archive header\n"), arch.file_name);
19939 return FALSE;
19940 }
19941 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
19942 if (got != sizeof arch.arhdr)
19943 {
19944 if (got == 0)
19945 break;
19946 /* PR 24049 - we cannot use filedata->file_name as this will
19947 have already been freed. */
19948 error (_("%s: failed to read archive header\n"), arch.file_name);
19949
19950 ret = FALSE;
19951 break;
19952 }
19953 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
19954 {
19955 error (_("%s: did not find a valid archive header\n"), arch.file_name);
19956 ret = FALSE;
19957 break;
19958 }
19959
19960 arch.next_arhdr_offset += sizeof arch.arhdr;
19961
19962 archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
19963 if (archive_file_size & 01)
19964 ++archive_file_size;
19965
19966 name = get_archive_member_name (&arch, &nested_arch);
19967 if (name == NULL)
19968 {
19969 error (_("%s: bad archive file name\n"), arch.file_name);
19970 ret = FALSE;
19971 break;
19972 }
19973 namelen = strlen (name);
19974
19975 qualified_name = make_qualified_name (&arch, &nested_arch, name);
19976 if (qualified_name == NULL)
19977 {
19978 error (_("%s: bad archive file name\n"), arch.file_name);
19979 ret = FALSE;
19980 break;
19981 }
19982
19983 if (is_thin_archive && arch.nested_member_origin == 0)
19984 {
19985 /* This is a proxy for an external member of a thin archive. */
19986 Filedata * member_filedata;
19987 char * member_file_name = adjust_relative_path
19988 (filedata->file_name, name, namelen);
19989
19990 if (member_file_name == NULL)
19991 {
19992 ret = FALSE;
19993 break;
19994 }
19995
19996 member_filedata = open_file (member_file_name);
19997 if (member_filedata == NULL)
19998 {
19999 error (_("Input file '%s' is not readable.\n"), member_file_name);
20000 free (member_file_name);
20001 ret = FALSE;
20002 break;
20003 }
20004
20005 archive_file_offset = arch.nested_member_origin;
20006 member_filedata->file_name = qualified_name;
20007
20008 if (! process_object (member_filedata))
20009 ret = FALSE;
20010
20011 close_file (member_filedata);
20012 free (member_file_name);
20013 }
20014 else if (is_thin_archive)
20015 {
20016 Filedata thin_filedata;
20017
20018 memset (&thin_filedata, 0, sizeof (thin_filedata));
20019
20020 /* PR 15140: Allow for corrupt thin archives. */
20021 if (nested_arch.file == NULL)
20022 {
20023 error (_("%s: contains corrupt thin archive: %s\n"),
20024 qualified_name, name);
20025 ret = FALSE;
20026 break;
20027 }
20028
20029 /* This is a proxy for a member of a nested archive. */
20030 archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
20031
20032 /* The nested archive file will have been opened and setup by
20033 get_archive_member_name. */
20034 if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
20035 {
20036 error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
20037 ret = FALSE;
20038 break;
20039 }
20040
20041 thin_filedata.handle = nested_arch.file;
20042 thin_filedata.file_name = qualified_name;
20043
20044 if (! process_object (& thin_filedata))
20045 ret = FALSE;
20046 }
20047 else
20048 {
20049 archive_file_offset = arch.next_arhdr_offset;
20050 arch.next_arhdr_offset += archive_file_size;
20051
20052 filedata->file_name = qualified_name;
20053 if (! process_object (filedata))
20054 ret = FALSE;
20055 }
20056
20057 if (filedata->dump_sects != NULL)
20058 {
20059 free (filedata->dump_sects);
20060 filedata->dump_sects = NULL;
20061 filedata->num_dump_sects = 0;
20062 }
20063
20064 free (qualified_name);
20065 }
20066
20067 out:
20068 if (nested_arch.file != NULL)
20069 fclose (nested_arch.file);
20070 release_archive (&nested_arch);
20071 release_archive (&arch);
20072
20073 return ret;
20074 }
20075
20076 static bfd_boolean
20077 process_file (char * file_name)
20078 {
20079 Filedata * filedata = NULL;
20080 struct stat statbuf;
20081 char armag[SARMAG];
20082 bfd_boolean ret = TRUE;
20083
20084 if (stat (file_name, &statbuf) < 0)
20085 {
20086 if (errno == ENOENT)
20087 error (_("'%s': No such file\n"), file_name);
20088 else
20089 error (_("Could not locate '%s'. System error message: %s\n"),
20090 file_name, strerror (errno));
20091 return FALSE;
20092 }
20093
20094 if (! S_ISREG (statbuf.st_mode))
20095 {
20096 error (_("'%s' is not an ordinary file\n"), file_name);
20097 return FALSE;
20098 }
20099
20100 filedata = calloc (1, sizeof * filedata);
20101 if (filedata == NULL)
20102 {
20103 error (_("Out of memory allocating file data structure\n"));
20104 return FALSE;
20105 }
20106
20107 filedata->file_name = file_name;
20108 filedata->handle = fopen (file_name, "rb");
20109 if (filedata->handle == NULL)
20110 {
20111 error (_("Input file '%s' is not readable.\n"), file_name);
20112 free (filedata);
20113 return FALSE;
20114 }
20115
20116 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
20117 {
20118 error (_("%s: Failed to read file's magic number\n"), file_name);
20119 fclose (filedata->handle);
20120 free (filedata);
20121 return FALSE;
20122 }
20123
20124 filedata->file_size = (bfd_size_type) statbuf.st_size;
20125
20126 if (memcmp (armag, ARMAG, SARMAG) == 0)
20127 {
20128 if (! process_archive (filedata, FALSE))
20129 ret = FALSE;
20130 }
20131 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
20132 {
20133 if ( ! process_archive (filedata, TRUE))
20134 ret = FALSE;
20135 }
20136 else
20137 {
20138 if (do_archive_index)
20139 error (_("File %s is not an archive so its index cannot be displayed.\n"),
20140 file_name);
20141
20142 rewind (filedata->handle);
20143 archive_file_size = archive_file_offset = 0;
20144
20145 if (! process_object (filedata))
20146 ret = FALSE;
20147 }
20148
20149 fclose (filedata->handle);
20150 free (filedata);
20151
20152 return ret;
20153 }
20154
20155 #ifdef SUPPORT_DISASSEMBLY
20156 /* Needed by the i386 disassembler. For extra credit, someone could
20157 fix this so that we insert symbolic addresses here, esp for GOT/PLT
20158 symbols. */
20159
20160 void
20161 print_address (unsigned int addr, FILE * outfile)
20162 {
20163 fprintf (outfile,"0x%8.8x", addr);
20164 }
20165
20166 /* Needed by the i386 disassembler. */
20167
20168 void
20169 db_task_printsym (unsigned int addr)
20170 {
20171 print_address (addr, stderr);
20172 }
20173 #endif
20174
20175 int
20176 main (int argc, char ** argv)
20177 {
20178 int err;
20179
20180 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
20181 setlocale (LC_MESSAGES, "");
20182 #endif
20183 #if defined (HAVE_SETLOCALE)
20184 setlocale (LC_CTYPE, "");
20185 #endif
20186 bindtextdomain (PACKAGE, LOCALEDIR);
20187 textdomain (PACKAGE);
20188
20189 expandargv (&argc, &argv);
20190
20191 cmdline.file_name = "<cmdline>";
20192 parse_args (& cmdline, argc, argv);
20193
20194 if (optind < (argc - 1))
20195 show_name = TRUE;
20196 else if (optind >= argc)
20197 {
20198 warn (_("Nothing to do.\n"));
20199 usage (stderr);
20200 }
20201
20202 err = FALSE;
20203 while (optind < argc)
20204 if (! process_file (argv[optind++]))
20205 err = TRUE;
20206
20207 if (cmdline.dump_sects != NULL)
20208 free (cmdline.dump_sects);
20209
20210 #ifdef HAVE_LIBCTF
20211 free (dump_ctf_symtab_name);
20212 free (dump_ctf_strtab_name);
20213 free (dump_ctf_parent_name);
20214 #endif
20215
20216 return err ? EXIT_FAILURE : EXIT_SUCCESS;
20217 }