Use bool in binutils
[deliverable/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23 \f
24 /* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #ifdef HAVE_WCHAR_H
48 #include <wchar.h>
49 #endif
50
51 #if __GNUC__ >= 2
52 /* Define BFD64 here, even if our default architecture is 32 bit ELF
53 as this will allow us to read in and parse 64bit and 32bit ELF files.
54 Only do this if we believe that the compiler can support a 64 bit
55 data type. For now we only rely on GCC being able to do this. */
56 #define BFD64
57 #endif
58
59 #include "bfd.h"
60 #include "bucomm.h"
61 #include "elfcomm.h"
62 #include "dwarf.h"
63 #include "ctf-api.h"
64 #include "demangle.h"
65
66 #include "elf/common.h"
67 #include "elf/external.h"
68 #include "elf/internal.h"
69
70
71 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
72 we can obtain the H8 reloc numbers. We need these for the
73 get_reloc_size() function. We include h8.h again after defining
74 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
75
76 #include "elf/h8.h"
77 #undef _ELF_H8_H
78
79 /* Undo the effects of #including reloc-macros.h. */
80
81 #undef START_RELOC_NUMBERS
82 #undef RELOC_NUMBER
83 #undef FAKE_RELOC
84 #undef EMPTY_RELOC
85 #undef END_RELOC_NUMBERS
86 #undef _RELOC_MACROS_H
87
88 /* The following headers use the elf/reloc-macros.h file to
89 automatically generate relocation recognition functions
90 such as elf_mips_reloc_type() */
91
92 #define RELOC_MACROS_GEN_FUNC
93
94 #include "elf/aarch64.h"
95 #include "elf/alpha.h"
96 #include "elf/arc.h"
97 #include "elf/arm.h"
98 #include "elf/avr.h"
99 #include "elf/bfin.h"
100 #include "elf/cr16.h"
101 #include "elf/cris.h"
102 #include "elf/crx.h"
103 #include "elf/csky.h"
104 #include "elf/d10v.h"
105 #include "elf/d30v.h"
106 #include "elf/dlx.h"
107 #include "elf/bpf.h"
108 #include "elf/epiphany.h"
109 #include "elf/fr30.h"
110 #include "elf/frv.h"
111 #include "elf/ft32.h"
112 #include "elf/h8.h"
113 #include "elf/hppa.h"
114 #include "elf/i386.h"
115 #include "elf/i370.h"
116 #include "elf/i860.h"
117 #include "elf/i960.h"
118 #include "elf/ia64.h"
119 #include "elf/ip2k.h"
120 #include "elf/lm32.h"
121 #include "elf/iq2000.h"
122 #include "elf/m32c.h"
123 #include "elf/m32r.h"
124 #include "elf/m68k.h"
125 #include "elf/m68hc11.h"
126 #include "elf/s12z.h"
127 #include "elf/mcore.h"
128 #include "elf/mep.h"
129 #include "elf/metag.h"
130 #include "elf/microblaze.h"
131 #include "elf/mips.h"
132 #include "elf/mmix.h"
133 #include "elf/mn10200.h"
134 #include "elf/mn10300.h"
135 #include "elf/moxie.h"
136 #include "elf/mt.h"
137 #include "elf/msp430.h"
138 #include "elf/nds32.h"
139 #include "elf/nfp.h"
140 #include "elf/nios2.h"
141 #include "elf/or1k.h"
142 #include "elf/pj.h"
143 #include "elf/ppc.h"
144 #include "elf/ppc64.h"
145 #include "elf/pru.h"
146 #include "elf/riscv.h"
147 #include "elf/rl78.h"
148 #include "elf/rx.h"
149 #include "elf/s390.h"
150 #include "elf/score.h"
151 #include "elf/sh.h"
152 #include "elf/sparc.h"
153 #include "elf/spu.h"
154 #include "elf/tic6x.h"
155 #include "elf/tilegx.h"
156 #include "elf/tilepro.h"
157 #include "elf/v850.h"
158 #include "elf/vax.h"
159 #include "elf/visium.h"
160 #include "elf/wasm32.h"
161 #include "elf/x86-64.h"
162 #include "elf/xc16x.h"
163 #include "elf/xgate.h"
164 #include "elf/xstormy16.h"
165 #include "elf/xtensa.h"
166 #include "elf/z80.h"
167
168 #include "getopt.h"
169 #include "libiberty.h"
170 #include "safe-ctype.h"
171 #include "filenames.h"
172
173 #ifndef offsetof
174 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
175 #endif
176
177 typedef struct elf_section_list
178 {
179 Elf_Internal_Shdr * hdr;
180 struct elf_section_list * next;
181 } elf_section_list;
182
183 /* Flag bits indicating particular types of dump. */
184 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
185 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
186 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
187 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
188 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
189 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
190
191 typedef unsigned char dump_type;
192
193 /* A linked list of the section names for which dumps were requested. */
194 struct dump_list_entry
195 {
196 char * name;
197 dump_type type;
198 struct dump_list_entry * next;
199 };
200
201 /* A dynamic array of flags indicating for which sections a dump
202 has been requested via command line switches. */
203 struct dump_data
204 {
205 dump_type * dump_sects;
206 unsigned int num_dump_sects;
207 };
208
209 static struct dump_data cmdline;
210
211 static struct dump_list_entry * dump_sects_byname;
212
213 char * program_name = "readelf";
214
215 static bool show_name = false;
216 static bool do_dynamic = false;
217 static bool do_syms = false;
218 static bool do_dyn_syms = false;
219 static bool do_lto_syms = false;
220 static bool do_reloc = false;
221 static bool do_sections = false;
222 static bool do_section_groups = false;
223 static bool do_section_details = false;
224 static bool do_segments = false;
225 static bool do_unwind = false;
226 static bool do_using_dynamic = false;
227 static bool do_header = false;
228 static bool do_dump = false;
229 static bool do_version = false;
230 static bool do_histogram = false;
231 static bool do_debugging = false;
232 static bool do_ctf = false;
233 static bool do_arch = false;
234 static bool do_notes = false;
235 static bool do_archive_index = false;
236 static bool check_all = false;
237 static bool is_32bit_elf = false;
238 static bool decompress_dumps = false;
239 static bool do_not_show_symbol_truncation = false;
240 static bool do_demangle = false; /* Pretty print C++ symbol names. */
241 static bool process_links = false;
242 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
243
244 static char *dump_ctf_parent_name;
245 static char *dump_ctf_symtab_name;
246 static char *dump_ctf_strtab_name;
247
248 struct group_list
249 {
250 struct group_list * next;
251 unsigned int section_index;
252 };
253
254 struct group
255 {
256 struct group_list * root;
257 unsigned int group_index;
258 };
259
260 typedef struct filedata
261 {
262 const char * file_name;
263 bool is_separate;
264 FILE * handle;
265 bfd_size_type file_size;
266 Elf_Internal_Ehdr file_header;
267 Elf_Internal_Shdr * section_headers;
268 Elf_Internal_Phdr * program_headers;
269 char * string_table;
270 unsigned long string_table_length;
271 unsigned long archive_file_offset;
272 unsigned long archive_file_size;
273 unsigned long dynamic_addr;
274 bfd_size_type dynamic_size;
275 size_t dynamic_nent;
276 Elf_Internal_Dyn * dynamic_section;
277 Elf_Internal_Shdr * dynamic_strtab_section;
278 char * dynamic_strings;
279 unsigned long dynamic_strings_length;
280 Elf_Internal_Shdr * dynamic_symtab_section;
281 unsigned long num_dynamic_syms;
282 Elf_Internal_Sym * dynamic_symbols;
283 bfd_vma version_info[16];
284 unsigned int dynamic_syminfo_nent;
285 Elf_Internal_Syminfo * dynamic_syminfo;
286 unsigned long dynamic_syminfo_offset;
287 bfd_size_type nbuckets;
288 bfd_size_type nchains;
289 bfd_vma * buckets;
290 bfd_vma * chains;
291 bfd_size_type ngnubuckets;
292 bfd_size_type ngnuchains;
293 bfd_vma * gnubuckets;
294 bfd_vma * gnuchains;
295 bfd_vma * mipsxlat;
296 bfd_vma gnusymidx;
297 char program_interpreter[PATH_MAX];
298 bfd_vma dynamic_info[DT_ENCODING];
299 bfd_vma dynamic_info_DT_GNU_HASH;
300 bfd_vma dynamic_info_DT_MIPS_XHASH;
301 elf_section_list * symtab_shndx_list;
302 size_t group_count;
303 struct group * section_groups;
304 struct group ** section_headers_groups;
305 /* A dynamic array of flags indicating for which sections a dump of
306 some kind has been requested. It is reset on a per-object file
307 basis and then initialised from the cmdline_dump_sects array,
308 the results of interpreting the -w switch, and the
309 dump_sects_byname list. */
310 struct dump_data dump;
311 } Filedata;
312
313 /* How to print a vma value. */
314 typedef enum print_mode
315 {
316 HEX,
317 DEC,
318 DEC_5,
319 UNSIGNED,
320 PREFIX_HEX,
321 FULL_HEX,
322 LONG_HEX
323 }
324 print_mode;
325
326 /* Versioned symbol info. */
327 enum versioned_symbol_info
328 {
329 symbol_undefined,
330 symbol_hidden,
331 symbol_public
332 };
333
334 static const char * get_symbol_version_string
335 (Filedata *, bool, const char *, unsigned long, unsigned,
336 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
337
338 #define UNKNOWN -1
339
340 #define SECTION_NAME(X) \
341 (filedata->string_table + (X)->sh_name)
342
343 #define SECTION_NAME_VALID(X) \
344 ((X) != NULL \
345 && filedata->string_table != NULL \
346 && (X)->sh_name < filedata->string_table_length)
347
348 #define SECTION_NAME_PRINT(X) \
349 ((X) == NULL ? _("<none>") \
350 : filedata->string_table == NULL ? _("<no-strings>") \
351 : (X)->sh_name >= filedata->string_table_length ? _("<corrupt>") \
352 : filedata->string_table + (X)->sh_name)
353
354 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
355
356 #define GET_ELF_SYMBOLS(file, section, sym_count) \
357 (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count) \
358 : get_64bit_elf_symbols (file, section, sym_count))
359
360 #define VALID_SYMBOL_NAME(strtab, strtab_size, offset) \
361 (strtab != NULL && offset < strtab_size)
362 #define VALID_DYNAMIC_NAME(filedata, offset) \
363 VALID_SYMBOL_NAME (filedata->dynamic_strings, \
364 filedata->dynamic_strings_length, offset)
365 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
366 already been called and verified that the string exists. */
367 #define GET_DYNAMIC_NAME(filedata, offset) \
368 (filedata->dynamic_strings + offset)
369
370 #define REMOVE_ARCH_BITS(ADDR) \
371 do \
372 { \
373 if (filedata->file_header.e_machine == EM_ARM) \
374 (ADDR) &= ~1; \
375 } \
376 while (0)
377
378 /* Get the correct GNU hash section name. */
379 #define GNU_HASH_SECTION_NAME(filedata) \
380 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
381 \f
382 /* Print a BFD_VMA to an internal buffer, for use in error messages.
383 BFD_FMA_FMT can't be used in translated strings. */
384
385 static const char *
386 bfd_vmatoa (char *fmtch, bfd_vma value)
387 {
388 /* bfd_vmatoa is used more then once in a printf call for output.
389 Cycle through an array of buffers. */
390 static int buf_pos = 0;
391 static struct bfd_vmatoa_buf
392 {
393 char place[64];
394 } buf[4];
395 char *ret;
396 char fmt[32];
397
398 ret = buf[buf_pos++].place;
399 buf_pos %= ARRAY_SIZE (buf);
400
401 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
402 snprintf (ret, sizeof (buf[0].place), fmt, value);
403 return ret;
404 }
405
406 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
407 OFFSET + the offset of the current archive member, if we are examining an
408 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
409 allocate a buffer using malloc and fill that. In either case return the
410 pointer to the start of the retrieved data or NULL if something went wrong.
411 If something does go wrong and REASON is not NULL then emit an error
412 message using REASON as part of the context. */
413
414 static void *
415 get_data (void * var,
416 Filedata * filedata,
417 unsigned long offset,
418 bfd_size_type size,
419 bfd_size_type nmemb,
420 const char * reason)
421 {
422 void * mvar;
423 bfd_size_type amt = size * nmemb;
424
425 if (size == 0 || nmemb == 0)
426 return NULL;
427
428 /* If the size_t type is smaller than the bfd_size_type, eg because
429 you are building a 32-bit tool on a 64-bit host, then make sure
430 that when the sizes are cast to (size_t) no information is lost. */
431 if ((size_t) size != size
432 || (size_t) nmemb != nmemb
433 || (size_t) amt != amt)
434 {
435 if (reason)
436 error (_("Size truncation prevents reading %s"
437 " elements of size %s for %s\n"),
438 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
439 return NULL;
440 }
441
442 /* Check for size overflow. */
443 if (amt / size != nmemb || (size_t) amt + 1 == 0)
444 {
445 if (reason)
446 error (_("Size overflow prevents reading %s"
447 " elements of size %s for %s\n"),
448 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
449 return NULL;
450 }
451
452 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
453 attempting to allocate memory when the read is bound to fail. */
454 if (filedata->archive_file_offset > filedata->file_size
455 || offset > filedata->file_size - filedata->archive_file_offset
456 || amt > filedata->file_size - filedata->archive_file_offset - offset)
457 {
458 if (reason)
459 error (_("Reading %s bytes extends past end of file for %s\n"),
460 bfd_vmatoa ("u", amt), reason);
461 return NULL;
462 }
463
464 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
465 SEEK_SET))
466 {
467 if (reason)
468 error (_("Unable to seek to 0x%lx for %s\n"),
469 filedata->archive_file_offset + offset, reason);
470 return NULL;
471 }
472
473 mvar = var;
474 if (mvar == NULL)
475 {
476 /* + 1 so that we can '\0' terminate invalid string table sections. */
477 mvar = malloc ((size_t) amt + 1);
478
479 if (mvar == NULL)
480 {
481 if (reason)
482 error (_("Out of memory allocating %s bytes for %s\n"),
483 bfd_vmatoa ("u", amt), reason);
484 return NULL;
485 }
486
487 ((char *) mvar)[amt] = '\0';
488 }
489
490 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
491 {
492 if (reason)
493 error (_("Unable to read in %s bytes of %s\n"),
494 bfd_vmatoa ("u", amt), reason);
495 if (mvar != var)
496 free (mvar);
497 return NULL;
498 }
499
500 return mvar;
501 }
502
503 /* Print a VMA value in the MODE specified.
504 Returns the number of characters displayed. */
505
506 static unsigned int
507 print_vma (bfd_vma vma, print_mode mode)
508 {
509 unsigned int nc = 0;
510
511 switch (mode)
512 {
513 case FULL_HEX:
514 nc = printf ("0x");
515 /* Fall through. */
516 case LONG_HEX:
517 #ifdef BFD64
518 if (is_32bit_elf)
519 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
520 #endif
521 printf_vma (vma);
522 return nc + 16;
523
524 case DEC_5:
525 if (vma <= 99999)
526 return printf ("%5" BFD_VMA_FMT "d", vma);
527 /* Fall through. */
528 case PREFIX_HEX:
529 nc = printf ("0x");
530 /* Fall through. */
531 case HEX:
532 return nc + printf ("%" BFD_VMA_FMT "x", vma);
533
534 case DEC:
535 return printf ("%" BFD_VMA_FMT "d", vma);
536
537 case UNSIGNED:
538 return printf ("%" BFD_VMA_FMT "u", vma);
539
540 default:
541 /* FIXME: Report unrecognised mode ? */
542 return 0;
543 }
544 }
545
546 /* Display a symbol on stdout. Handles the display of control characters and
547 multibye characters (assuming the host environment supports them).
548
549 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
550
551 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
552 abs(WIDTH) - 5 characters followed by "[...]".
553
554 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
555 padding as necessary.
556
557 Returns the number of emitted characters. */
558
559 static unsigned int
560 print_symbol (signed int width, const char * symbol)
561 {
562 bool extra_padding = false;
563 bool do_dots = false;
564 signed int num_printed = 0;
565 #ifdef HAVE_MBSTATE_T
566 mbstate_t state;
567 #endif
568 unsigned int width_remaining;
569 const void * alloced_symbol = NULL;
570
571 if (width < 0)
572 {
573 /* Keep the width positive. This helps the code below. */
574 width = - width;
575 extra_padding = true;
576 }
577 else if (width == 0)
578 return 0;
579
580 if (do_wide)
581 /* Set the remaining width to a very large value.
582 This simplifies the code below. */
583 width_remaining = INT_MAX;
584 else
585 {
586 width_remaining = width;
587 if (! do_not_show_symbol_truncation
588 && (int) strlen (symbol) > width)
589 {
590 width_remaining -= 5;
591 if ((int) width_remaining < 0)
592 width_remaining = 0;
593 do_dots = true;
594 }
595 }
596
597 #ifdef HAVE_MBSTATE_T
598 /* Initialise the multibyte conversion state. */
599 memset (& state, 0, sizeof (state));
600 #endif
601
602 if (do_demangle && *symbol)
603 {
604 const char * res = cplus_demangle (symbol, demangle_flags);
605
606 if (res != NULL)
607 alloced_symbol = symbol = res;
608 }
609
610 while (width_remaining)
611 {
612 size_t n;
613 const char c = *symbol++;
614
615 if (c == 0)
616 break;
617
618 /* Do not print control characters directly as they can affect terminal
619 settings. Such characters usually appear in the names generated
620 by the assembler for local labels. */
621 if (ISCNTRL (c))
622 {
623 if (width_remaining < 2)
624 break;
625
626 printf ("^%c", c + 0x40);
627 width_remaining -= 2;
628 num_printed += 2;
629 }
630 else if (ISPRINT (c))
631 {
632 putchar (c);
633 width_remaining --;
634 num_printed ++;
635 }
636 else
637 {
638 #ifdef HAVE_MBSTATE_T
639 wchar_t w;
640 #endif
641 /* Let printf do the hard work of displaying multibyte characters. */
642 printf ("%.1s", symbol - 1);
643 width_remaining --;
644 num_printed ++;
645
646 #ifdef HAVE_MBSTATE_T
647 /* Try to find out how many bytes made up the character that was
648 just printed. Advance the symbol pointer past the bytes that
649 were displayed. */
650 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
651 #else
652 n = 1;
653 #endif
654 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
655 symbol += (n - 1);
656 }
657 }
658
659 if (do_dots)
660 num_printed += printf ("[...]");
661
662 if (extra_padding && num_printed < width)
663 {
664 /* Fill in the remaining spaces. */
665 printf ("%-*s", width - num_printed, " ");
666 num_printed = width;
667 }
668
669 free ((void *) alloced_symbol);
670 return num_printed;
671 }
672
673 /* Returns a pointer to a static buffer containing a printable version of
674 the given section's name. Like print_symbol, except that it does not try
675 to print multibyte characters, it just interprets them as hex values. */
676
677 static const char *
678 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
679 {
680 #define MAX_PRINT_SEC_NAME_LEN 256
681 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
682 const char * name = SECTION_NAME_PRINT (sec);
683 char * buf = sec_name_buf;
684 char c;
685 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
686
687 while ((c = * name ++) != 0)
688 {
689 if (ISCNTRL (c))
690 {
691 if (remaining < 2)
692 break;
693
694 * buf ++ = '^';
695 * buf ++ = c + 0x40;
696 remaining -= 2;
697 }
698 else if (ISPRINT (c))
699 {
700 * buf ++ = c;
701 remaining -= 1;
702 }
703 else
704 {
705 static char hex[17] = "0123456789ABCDEF";
706
707 if (remaining < 4)
708 break;
709 * buf ++ = '<';
710 * buf ++ = hex[(c & 0xf0) >> 4];
711 * buf ++ = hex[c & 0x0f];
712 * buf ++ = '>';
713 remaining -= 4;
714 }
715
716 if (remaining == 0)
717 break;
718 }
719
720 * buf = 0;
721 return sec_name_buf;
722 }
723
724 static const char *
725 printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
726 {
727 if (ndx >= filedata->file_header.e_shnum)
728 return _("<corrupt>");
729
730 return printable_section_name (filedata, filedata->section_headers + ndx);
731 }
732
733 /* Return a pointer to section NAME, or NULL if no such section exists. */
734
735 static Elf_Internal_Shdr *
736 find_section (Filedata * filedata, const char * name)
737 {
738 unsigned int i;
739
740 if (filedata->section_headers == NULL)
741 return NULL;
742
743 for (i = 0; i < filedata->file_header.e_shnum; i++)
744 if (SECTION_NAME_VALID (filedata->section_headers + i)
745 && streq (SECTION_NAME (filedata->section_headers + i), name))
746 return filedata->section_headers + i;
747
748 return NULL;
749 }
750
751 /* Return a pointer to a section containing ADDR, or NULL if no such
752 section exists. */
753
754 static Elf_Internal_Shdr *
755 find_section_by_address (Filedata * filedata, bfd_vma addr)
756 {
757 unsigned int i;
758
759 if (filedata->section_headers == NULL)
760 return NULL;
761
762 for (i = 0; i < filedata->file_header.e_shnum; i++)
763 {
764 Elf_Internal_Shdr *sec = filedata->section_headers + i;
765
766 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
767 return sec;
768 }
769
770 return NULL;
771 }
772
773 static Elf_Internal_Shdr *
774 find_section_by_type (Filedata * filedata, unsigned int type)
775 {
776 unsigned int i;
777
778 if (filedata->section_headers == NULL)
779 return NULL;
780
781 for (i = 0; i < filedata->file_header.e_shnum; i++)
782 {
783 Elf_Internal_Shdr *sec = filedata->section_headers + i;
784
785 if (sec->sh_type == type)
786 return sec;
787 }
788
789 return NULL;
790 }
791
792 /* Return a pointer to section NAME, or NULL if no such section exists,
793 restricted to the list of sections given in SET. */
794
795 static Elf_Internal_Shdr *
796 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
797 {
798 unsigned int i;
799
800 if (filedata->section_headers == NULL)
801 return NULL;
802
803 if (set != NULL)
804 {
805 while ((i = *set++) > 0)
806 {
807 /* See PR 21156 for a reproducer. */
808 if (i >= filedata->file_header.e_shnum)
809 continue; /* FIXME: Should we issue an error message ? */
810
811 if (SECTION_NAME_VALID (filedata->section_headers + i)
812 && streq (SECTION_NAME (filedata->section_headers + i), name))
813 return filedata->section_headers + i;
814 }
815 }
816
817 return find_section (filedata, name);
818 }
819
820 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
821 This OS has so many departures from the ELF standard that we test it at
822 many places. */
823
824 static inline bool
825 is_ia64_vms (Filedata * filedata)
826 {
827 return filedata->file_header.e_machine == EM_IA_64
828 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
829 }
830
831 /* Guess the relocation size commonly used by the specific machines. */
832
833 static bool
834 guess_is_rela (unsigned int e_machine)
835 {
836 switch (e_machine)
837 {
838 /* Targets that use REL relocations. */
839 case EM_386:
840 case EM_IAMCU:
841 case EM_960:
842 case EM_ARM:
843 case EM_D10V:
844 case EM_CYGNUS_D10V:
845 case EM_DLX:
846 case EM_MIPS:
847 case EM_MIPS_RS3_LE:
848 case EM_CYGNUS_M32R:
849 case EM_SCORE:
850 case EM_XGATE:
851 case EM_NFP:
852 case EM_BPF:
853 return false;
854
855 /* Targets that use RELA relocations. */
856 case EM_68K:
857 case EM_860:
858 case EM_AARCH64:
859 case EM_ADAPTEVA_EPIPHANY:
860 case EM_ALPHA:
861 case EM_ALTERA_NIOS2:
862 case EM_ARC:
863 case EM_ARC_COMPACT:
864 case EM_ARC_COMPACT2:
865 case EM_AVR:
866 case EM_AVR_OLD:
867 case EM_BLACKFIN:
868 case EM_CR16:
869 case EM_CRIS:
870 case EM_CRX:
871 case EM_CSKY:
872 case EM_D30V:
873 case EM_CYGNUS_D30V:
874 case EM_FR30:
875 case EM_FT32:
876 case EM_CYGNUS_FR30:
877 case EM_CYGNUS_FRV:
878 case EM_H8S:
879 case EM_H8_300:
880 case EM_H8_300H:
881 case EM_IA_64:
882 case EM_IP2K:
883 case EM_IP2K_OLD:
884 case EM_IQ2000:
885 case EM_LATTICEMICO32:
886 case EM_M32C_OLD:
887 case EM_M32C:
888 case EM_M32R:
889 case EM_MCORE:
890 case EM_CYGNUS_MEP:
891 case EM_METAG:
892 case EM_MMIX:
893 case EM_MN10200:
894 case EM_CYGNUS_MN10200:
895 case EM_MN10300:
896 case EM_CYGNUS_MN10300:
897 case EM_MOXIE:
898 case EM_MSP430:
899 case EM_MSP430_OLD:
900 case EM_MT:
901 case EM_NDS32:
902 case EM_NIOS32:
903 case EM_OR1K:
904 case EM_PPC64:
905 case EM_PPC:
906 case EM_TI_PRU:
907 case EM_RISCV:
908 case EM_RL78:
909 case EM_RX:
910 case EM_S390:
911 case EM_S390_OLD:
912 case EM_SH:
913 case EM_SPARC:
914 case EM_SPARC32PLUS:
915 case EM_SPARCV9:
916 case EM_SPU:
917 case EM_TI_C6000:
918 case EM_TILEGX:
919 case EM_TILEPRO:
920 case EM_V800:
921 case EM_V850:
922 case EM_CYGNUS_V850:
923 case EM_VAX:
924 case EM_VISIUM:
925 case EM_X86_64:
926 case EM_L1OM:
927 case EM_K1OM:
928 case EM_XSTORMY16:
929 case EM_XTENSA:
930 case EM_XTENSA_OLD:
931 case EM_MICROBLAZE:
932 case EM_MICROBLAZE_OLD:
933 case EM_WEBASSEMBLY:
934 return true;
935
936 case EM_68HC05:
937 case EM_68HC08:
938 case EM_68HC11:
939 case EM_68HC16:
940 case EM_FX66:
941 case EM_ME16:
942 case EM_MMA:
943 case EM_NCPU:
944 case EM_NDR1:
945 case EM_PCP:
946 case EM_ST100:
947 case EM_ST19:
948 case EM_ST7:
949 case EM_ST9PLUS:
950 case EM_STARCORE:
951 case EM_SVX:
952 case EM_TINYJ:
953 default:
954 warn (_("Don't know about relocations on this machine architecture\n"));
955 return false;
956 }
957 }
958
959 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
960 Returns TRUE upon success, FALSE otherwise. If successful then a
961 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
962 and the number of relocs loaded is placed in *NRELASP. It is the caller's
963 responsibility to free the allocated buffer. */
964
965 static bool
966 slurp_rela_relocs (Filedata * filedata,
967 unsigned long rel_offset,
968 unsigned long rel_size,
969 Elf_Internal_Rela ** relasp,
970 unsigned long * nrelasp)
971 {
972 Elf_Internal_Rela * relas;
973 size_t nrelas;
974 unsigned int i;
975
976 if (is_32bit_elf)
977 {
978 Elf32_External_Rela * erelas;
979
980 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
981 rel_size, _("32-bit relocation data"));
982 if (!erelas)
983 return false;
984
985 nrelas = rel_size / sizeof (Elf32_External_Rela);
986
987 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
988 sizeof (Elf_Internal_Rela));
989
990 if (relas == NULL)
991 {
992 free (erelas);
993 error (_("out of memory parsing relocs\n"));
994 return false;
995 }
996
997 for (i = 0; i < nrelas; i++)
998 {
999 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1000 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1001 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1002 }
1003
1004 free (erelas);
1005 }
1006 else
1007 {
1008 Elf64_External_Rela * erelas;
1009
1010 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1011 rel_size, _("64-bit relocation data"));
1012 if (!erelas)
1013 return false;
1014
1015 nrelas = rel_size / sizeof (Elf64_External_Rela);
1016
1017 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1018 sizeof (Elf_Internal_Rela));
1019
1020 if (relas == NULL)
1021 {
1022 free (erelas);
1023 error (_("out of memory parsing relocs\n"));
1024 return false;
1025 }
1026
1027 for (i = 0; i < nrelas; i++)
1028 {
1029 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1030 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1031 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1032
1033 /* The #ifdef BFD64 below is to prevent a compile time
1034 warning. We know that if we do not have a 64 bit data
1035 type that we will never execute this code anyway. */
1036 #ifdef BFD64
1037 if (filedata->file_header.e_machine == EM_MIPS
1038 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1039 {
1040 /* In little-endian objects, r_info isn't really a
1041 64-bit little-endian value: it has a 32-bit
1042 little-endian symbol index followed by four
1043 individual byte fields. Reorder INFO
1044 accordingly. */
1045 bfd_vma inf = relas[i].r_info;
1046 inf = (((inf & 0xffffffff) << 32)
1047 | ((inf >> 56) & 0xff)
1048 | ((inf >> 40) & 0xff00)
1049 | ((inf >> 24) & 0xff0000)
1050 | ((inf >> 8) & 0xff000000));
1051 relas[i].r_info = inf;
1052 }
1053 #endif /* BFD64 */
1054 }
1055
1056 free (erelas);
1057 }
1058
1059 *relasp = relas;
1060 *nrelasp = nrelas;
1061 return true;
1062 }
1063
1064 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1065 Returns TRUE upon success, FALSE otherwise. If successful then a
1066 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1067 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1068 responsibility to free the allocated buffer. */
1069
1070 static bool
1071 slurp_rel_relocs (Filedata * filedata,
1072 unsigned long rel_offset,
1073 unsigned long rel_size,
1074 Elf_Internal_Rela ** relsp,
1075 unsigned long * nrelsp)
1076 {
1077 Elf_Internal_Rela * rels;
1078 size_t nrels;
1079 unsigned int i;
1080
1081 if (is_32bit_elf)
1082 {
1083 Elf32_External_Rel * erels;
1084
1085 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1086 rel_size, _("32-bit relocation data"));
1087 if (!erels)
1088 return false;
1089
1090 nrels = rel_size / sizeof (Elf32_External_Rel);
1091
1092 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1093
1094 if (rels == NULL)
1095 {
1096 free (erels);
1097 error (_("out of memory parsing relocs\n"));
1098 return false;
1099 }
1100
1101 for (i = 0; i < nrels; i++)
1102 {
1103 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1104 rels[i].r_info = BYTE_GET (erels[i].r_info);
1105 rels[i].r_addend = 0;
1106 }
1107
1108 free (erels);
1109 }
1110 else
1111 {
1112 Elf64_External_Rel * erels;
1113
1114 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1115 rel_size, _("64-bit relocation data"));
1116 if (!erels)
1117 return false;
1118
1119 nrels = rel_size / sizeof (Elf64_External_Rel);
1120
1121 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1122
1123 if (rels == NULL)
1124 {
1125 free (erels);
1126 error (_("out of memory parsing relocs\n"));
1127 return false;
1128 }
1129
1130 for (i = 0; i < nrels; i++)
1131 {
1132 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1133 rels[i].r_info = BYTE_GET (erels[i].r_info);
1134 rels[i].r_addend = 0;
1135
1136 /* The #ifdef BFD64 below is to prevent a compile time
1137 warning. We know that if we do not have a 64 bit data
1138 type that we will never execute this code anyway. */
1139 #ifdef BFD64
1140 if (filedata->file_header.e_machine == EM_MIPS
1141 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1142 {
1143 /* In little-endian objects, r_info isn't really a
1144 64-bit little-endian value: it has a 32-bit
1145 little-endian symbol index followed by four
1146 individual byte fields. Reorder INFO
1147 accordingly. */
1148 bfd_vma inf = rels[i].r_info;
1149 inf = (((inf & 0xffffffff) << 32)
1150 | ((inf >> 56) & 0xff)
1151 | ((inf >> 40) & 0xff00)
1152 | ((inf >> 24) & 0xff0000)
1153 | ((inf >> 8) & 0xff000000));
1154 rels[i].r_info = inf;
1155 }
1156 #endif /* BFD64 */
1157 }
1158
1159 free (erels);
1160 }
1161
1162 *relsp = rels;
1163 *nrelsp = nrels;
1164 return true;
1165 }
1166
1167 /* Returns the reloc type extracted from the reloc info field. */
1168
1169 static unsigned int
1170 get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1171 {
1172 if (is_32bit_elf)
1173 return ELF32_R_TYPE (reloc_info);
1174
1175 switch (filedata->file_header.e_machine)
1176 {
1177 case EM_MIPS:
1178 /* Note: We assume that reloc_info has already been adjusted for us. */
1179 return ELF64_MIPS_R_TYPE (reloc_info);
1180
1181 case EM_SPARCV9:
1182 return ELF64_R_TYPE_ID (reloc_info);
1183
1184 default:
1185 return ELF64_R_TYPE (reloc_info);
1186 }
1187 }
1188
1189 /* Return the symbol index extracted from the reloc info field. */
1190
1191 static bfd_vma
1192 get_reloc_symindex (bfd_vma reloc_info)
1193 {
1194 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1195 }
1196
1197 static inline bool
1198 uses_msp430x_relocs (Filedata * filedata)
1199 {
1200 return
1201 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1202 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1203 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1204 /* TI compiler uses ELFOSABI_NONE. */
1205 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1206 }
1207
1208 /* Display the contents of the relocation data found at the specified
1209 offset. */
1210
1211 static bool
1212 dump_relocations (Filedata * filedata,
1213 unsigned long rel_offset,
1214 unsigned long rel_size,
1215 Elf_Internal_Sym * symtab,
1216 unsigned long nsyms,
1217 char * strtab,
1218 unsigned long strtablen,
1219 int is_rela,
1220 bool is_dynsym)
1221 {
1222 unsigned long i;
1223 Elf_Internal_Rela * rels;
1224 bool res = true;
1225
1226 if (is_rela == UNKNOWN)
1227 is_rela = guess_is_rela (filedata->file_header.e_machine);
1228
1229 if (is_rela)
1230 {
1231 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1232 return false;
1233 }
1234 else
1235 {
1236 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1237 return false;
1238 }
1239
1240 if (is_32bit_elf)
1241 {
1242 if (is_rela)
1243 {
1244 if (do_wide)
1245 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1246 else
1247 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1248 }
1249 else
1250 {
1251 if (do_wide)
1252 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1253 else
1254 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1255 }
1256 }
1257 else
1258 {
1259 if (is_rela)
1260 {
1261 if (do_wide)
1262 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1263 else
1264 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1265 }
1266 else
1267 {
1268 if (do_wide)
1269 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1270 else
1271 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1272 }
1273 }
1274
1275 for (i = 0; i < rel_size; i++)
1276 {
1277 const char * rtype;
1278 bfd_vma offset;
1279 bfd_vma inf;
1280 bfd_vma symtab_index;
1281 bfd_vma type;
1282
1283 offset = rels[i].r_offset;
1284 inf = rels[i].r_info;
1285
1286 type = get_reloc_type (filedata, inf);
1287 symtab_index = get_reloc_symindex (inf);
1288
1289 if (is_32bit_elf)
1290 {
1291 printf ("%8.8lx %8.8lx ",
1292 (unsigned long) offset & 0xffffffff,
1293 (unsigned long) inf & 0xffffffff);
1294 }
1295 else
1296 {
1297 printf (do_wide
1298 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1299 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
1300 offset, inf);
1301 }
1302
1303 switch (filedata->file_header.e_machine)
1304 {
1305 default:
1306 rtype = NULL;
1307 break;
1308
1309 case EM_AARCH64:
1310 rtype = elf_aarch64_reloc_type (type);
1311 break;
1312
1313 case EM_M32R:
1314 case EM_CYGNUS_M32R:
1315 rtype = elf_m32r_reloc_type (type);
1316 break;
1317
1318 case EM_386:
1319 case EM_IAMCU:
1320 rtype = elf_i386_reloc_type (type);
1321 break;
1322
1323 case EM_68HC11:
1324 case EM_68HC12:
1325 rtype = elf_m68hc11_reloc_type (type);
1326 break;
1327
1328 case EM_S12Z:
1329 rtype = elf_s12z_reloc_type (type);
1330 break;
1331
1332 case EM_68K:
1333 rtype = elf_m68k_reloc_type (type);
1334 break;
1335
1336 case EM_960:
1337 rtype = elf_i960_reloc_type (type);
1338 break;
1339
1340 case EM_AVR:
1341 case EM_AVR_OLD:
1342 rtype = elf_avr_reloc_type (type);
1343 break;
1344
1345 case EM_OLD_SPARCV9:
1346 case EM_SPARC32PLUS:
1347 case EM_SPARCV9:
1348 case EM_SPARC:
1349 rtype = elf_sparc_reloc_type (type);
1350 break;
1351
1352 case EM_SPU:
1353 rtype = elf_spu_reloc_type (type);
1354 break;
1355
1356 case EM_V800:
1357 rtype = v800_reloc_type (type);
1358 break;
1359 case EM_V850:
1360 case EM_CYGNUS_V850:
1361 rtype = v850_reloc_type (type);
1362 break;
1363
1364 case EM_D10V:
1365 case EM_CYGNUS_D10V:
1366 rtype = elf_d10v_reloc_type (type);
1367 break;
1368
1369 case EM_D30V:
1370 case EM_CYGNUS_D30V:
1371 rtype = elf_d30v_reloc_type (type);
1372 break;
1373
1374 case EM_DLX:
1375 rtype = elf_dlx_reloc_type (type);
1376 break;
1377
1378 case EM_SH:
1379 rtype = elf_sh_reloc_type (type);
1380 break;
1381
1382 case EM_MN10300:
1383 case EM_CYGNUS_MN10300:
1384 rtype = elf_mn10300_reloc_type (type);
1385 break;
1386
1387 case EM_MN10200:
1388 case EM_CYGNUS_MN10200:
1389 rtype = elf_mn10200_reloc_type (type);
1390 break;
1391
1392 case EM_FR30:
1393 case EM_CYGNUS_FR30:
1394 rtype = elf_fr30_reloc_type (type);
1395 break;
1396
1397 case EM_CYGNUS_FRV:
1398 rtype = elf_frv_reloc_type (type);
1399 break;
1400
1401 case EM_CSKY:
1402 rtype = elf_csky_reloc_type (type);
1403 break;
1404
1405 case EM_FT32:
1406 rtype = elf_ft32_reloc_type (type);
1407 break;
1408
1409 case EM_MCORE:
1410 rtype = elf_mcore_reloc_type (type);
1411 break;
1412
1413 case EM_MMIX:
1414 rtype = elf_mmix_reloc_type (type);
1415 break;
1416
1417 case EM_MOXIE:
1418 rtype = elf_moxie_reloc_type (type);
1419 break;
1420
1421 case EM_MSP430:
1422 if (uses_msp430x_relocs (filedata))
1423 {
1424 rtype = elf_msp430x_reloc_type (type);
1425 break;
1426 }
1427 /* Fall through. */
1428 case EM_MSP430_OLD:
1429 rtype = elf_msp430_reloc_type (type);
1430 break;
1431
1432 case EM_NDS32:
1433 rtype = elf_nds32_reloc_type (type);
1434 break;
1435
1436 case EM_PPC:
1437 rtype = elf_ppc_reloc_type (type);
1438 break;
1439
1440 case EM_PPC64:
1441 rtype = elf_ppc64_reloc_type (type);
1442 break;
1443
1444 case EM_MIPS:
1445 case EM_MIPS_RS3_LE:
1446 rtype = elf_mips_reloc_type (type);
1447 break;
1448
1449 case EM_RISCV:
1450 rtype = elf_riscv_reloc_type (type);
1451 break;
1452
1453 case EM_ALPHA:
1454 rtype = elf_alpha_reloc_type (type);
1455 break;
1456
1457 case EM_ARM:
1458 rtype = elf_arm_reloc_type (type);
1459 break;
1460
1461 case EM_ARC:
1462 case EM_ARC_COMPACT:
1463 case EM_ARC_COMPACT2:
1464 rtype = elf_arc_reloc_type (type);
1465 break;
1466
1467 case EM_PARISC:
1468 rtype = elf_hppa_reloc_type (type);
1469 break;
1470
1471 case EM_H8_300:
1472 case EM_H8_300H:
1473 case EM_H8S:
1474 rtype = elf_h8_reloc_type (type);
1475 break;
1476
1477 case EM_OR1K:
1478 rtype = elf_or1k_reloc_type (type);
1479 break;
1480
1481 case EM_PJ:
1482 case EM_PJ_OLD:
1483 rtype = elf_pj_reloc_type (type);
1484 break;
1485 case EM_IA_64:
1486 rtype = elf_ia64_reloc_type (type);
1487 break;
1488
1489 case EM_CRIS:
1490 rtype = elf_cris_reloc_type (type);
1491 break;
1492
1493 case EM_860:
1494 rtype = elf_i860_reloc_type (type);
1495 break;
1496
1497 case EM_X86_64:
1498 case EM_L1OM:
1499 case EM_K1OM:
1500 rtype = elf_x86_64_reloc_type (type);
1501 break;
1502
1503 case EM_S370:
1504 rtype = i370_reloc_type (type);
1505 break;
1506
1507 case EM_S390_OLD:
1508 case EM_S390:
1509 rtype = elf_s390_reloc_type (type);
1510 break;
1511
1512 case EM_SCORE:
1513 rtype = elf_score_reloc_type (type);
1514 break;
1515
1516 case EM_XSTORMY16:
1517 rtype = elf_xstormy16_reloc_type (type);
1518 break;
1519
1520 case EM_CRX:
1521 rtype = elf_crx_reloc_type (type);
1522 break;
1523
1524 case EM_VAX:
1525 rtype = elf_vax_reloc_type (type);
1526 break;
1527
1528 case EM_VISIUM:
1529 rtype = elf_visium_reloc_type (type);
1530 break;
1531
1532 case EM_BPF:
1533 rtype = elf_bpf_reloc_type (type);
1534 break;
1535
1536 case EM_ADAPTEVA_EPIPHANY:
1537 rtype = elf_epiphany_reloc_type (type);
1538 break;
1539
1540 case EM_IP2K:
1541 case EM_IP2K_OLD:
1542 rtype = elf_ip2k_reloc_type (type);
1543 break;
1544
1545 case EM_IQ2000:
1546 rtype = elf_iq2000_reloc_type (type);
1547 break;
1548
1549 case EM_XTENSA_OLD:
1550 case EM_XTENSA:
1551 rtype = elf_xtensa_reloc_type (type);
1552 break;
1553
1554 case EM_LATTICEMICO32:
1555 rtype = elf_lm32_reloc_type (type);
1556 break;
1557
1558 case EM_M32C_OLD:
1559 case EM_M32C:
1560 rtype = elf_m32c_reloc_type (type);
1561 break;
1562
1563 case EM_MT:
1564 rtype = elf_mt_reloc_type (type);
1565 break;
1566
1567 case EM_BLACKFIN:
1568 rtype = elf_bfin_reloc_type (type);
1569 break;
1570
1571 case EM_CYGNUS_MEP:
1572 rtype = elf_mep_reloc_type (type);
1573 break;
1574
1575 case EM_CR16:
1576 rtype = elf_cr16_reloc_type (type);
1577 break;
1578
1579 case EM_MICROBLAZE:
1580 case EM_MICROBLAZE_OLD:
1581 rtype = elf_microblaze_reloc_type (type);
1582 break;
1583
1584 case EM_RL78:
1585 rtype = elf_rl78_reloc_type (type);
1586 break;
1587
1588 case EM_RX:
1589 rtype = elf_rx_reloc_type (type);
1590 break;
1591
1592 case EM_METAG:
1593 rtype = elf_metag_reloc_type (type);
1594 break;
1595
1596 case EM_XC16X:
1597 case EM_C166:
1598 rtype = elf_xc16x_reloc_type (type);
1599 break;
1600
1601 case EM_TI_C6000:
1602 rtype = elf_tic6x_reloc_type (type);
1603 break;
1604
1605 case EM_TILEGX:
1606 rtype = elf_tilegx_reloc_type (type);
1607 break;
1608
1609 case EM_TILEPRO:
1610 rtype = elf_tilepro_reloc_type (type);
1611 break;
1612
1613 case EM_WEBASSEMBLY:
1614 rtype = elf_wasm32_reloc_type (type);
1615 break;
1616
1617 case EM_XGATE:
1618 rtype = elf_xgate_reloc_type (type);
1619 break;
1620
1621 case EM_ALTERA_NIOS2:
1622 rtype = elf_nios2_reloc_type (type);
1623 break;
1624
1625 case EM_TI_PRU:
1626 rtype = elf_pru_reloc_type (type);
1627 break;
1628
1629 case EM_NFP:
1630 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1631 rtype = elf_nfp3200_reloc_type (type);
1632 else
1633 rtype = elf_nfp_reloc_type (type);
1634 break;
1635
1636 case EM_Z80:
1637 rtype = elf_z80_reloc_type (type);
1638 break;
1639 }
1640
1641 if (rtype == NULL)
1642 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1643 else
1644 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1645
1646 if (filedata->file_header.e_machine == EM_ALPHA
1647 && rtype != NULL
1648 && streq (rtype, "R_ALPHA_LITUSE")
1649 && is_rela)
1650 {
1651 switch (rels[i].r_addend)
1652 {
1653 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1654 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1655 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1656 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1657 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1658 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1659 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1660 default: rtype = NULL;
1661 }
1662
1663 if (rtype)
1664 printf (" (%s)", rtype);
1665 else
1666 {
1667 putchar (' ');
1668 printf (_("<unknown addend: %lx>"),
1669 (unsigned long) rels[i].r_addend);
1670 res = false;
1671 }
1672 }
1673 else if (symtab_index)
1674 {
1675 if (symtab == NULL || symtab_index >= nsyms)
1676 {
1677 error (_(" bad symbol index: %08lx in reloc\n"),
1678 (unsigned long) symtab_index);
1679 res = false;
1680 }
1681 else
1682 {
1683 Elf_Internal_Sym * psym;
1684 const char * version_string;
1685 enum versioned_symbol_info sym_info;
1686 unsigned short vna_other;
1687
1688 psym = symtab + symtab_index;
1689
1690 version_string
1691 = get_symbol_version_string (filedata, is_dynsym,
1692 strtab, strtablen,
1693 symtab_index,
1694 psym,
1695 &sym_info,
1696 &vna_other);
1697
1698 printf (" ");
1699
1700 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1701 {
1702 const char * name;
1703 unsigned int len;
1704 unsigned int width = is_32bit_elf ? 8 : 14;
1705
1706 /* Relocations against GNU_IFUNC symbols do not use the value
1707 of the symbol as the address to relocate against. Instead
1708 they invoke the function named by the symbol and use its
1709 result as the address for relocation.
1710
1711 To indicate this to the user, do not display the value of
1712 the symbol in the "Symbols's Value" field. Instead show
1713 its name followed by () as a hint that the symbol is
1714 invoked. */
1715
1716 if (strtab == NULL
1717 || psym->st_name == 0
1718 || psym->st_name >= strtablen)
1719 name = "??";
1720 else
1721 name = strtab + psym->st_name;
1722
1723 len = print_symbol (width, name);
1724 if (version_string)
1725 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1726 version_string);
1727 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1728 }
1729 else
1730 {
1731 print_vma (psym->st_value, LONG_HEX);
1732
1733 printf (is_32bit_elf ? " " : " ");
1734 }
1735
1736 if (psym->st_name == 0)
1737 {
1738 const char * sec_name = "<null>";
1739 char name_buf[40];
1740
1741 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1742 {
1743 if (psym->st_shndx < filedata->file_header.e_shnum)
1744 sec_name = SECTION_NAME_PRINT (filedata->section_headers
1745 + psym->st_shndx);
1746 else if (psym->st_shndx == SHN_ABS)
1747 sec_name = "ABS";
1748 else if (psym->st_shndx == SHN_COMMON)
1749 sec_name = "COMMON";
1750 else if ((filedata->file_header.e_machine == EM_MIPS
1751 && psym->st_shndx == SHN_MIPS_SCOMMON)
1752 || (filedata->file_header.e_machine == EM_TI_C6000
1753 && psym->st_shndx == SHN_TIC6X_SCOMMON))
1754 sec_name = "SCOMMON";
1755 else if (filedata->file_header.e_machine == EM_MIPS
1756 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1757 sec_name = "SUNDEF";
1758 else if ((filedata->file_header.e_machine == EM_X86_64
1759 || filedata->file_header.e_machine == EM_L1OM
1760 || filedata->file_header.e_machine == EM_K1OM)
1761 && psym->st_shndx == SHN_X86_64_LCOMMON)
1762 sec_name = "LARGE_COMMON";
1763 else if (filedata->file_header.e_machine == EM_IA_64
1764 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1765 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1766 sec_name = "ANSI_COM";
1767 else if (is_ia64_vms (filedata)
1768 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1769 sec_name = "VMS_SYMVEC";
1770 else
1771 {
1772 sprintf (name_buf, "<section 0x%x>",
1773 (unsigned int) psym->st_shndx);
1774 sec_name = name_buf;
1775 }
1776 }
1777 print_symbol (22, sec_name);
1778 }
1779 else if (strtab == NULL)
1780 printf (_("<string table index: %3ld>"), psym->st_name);
1781 else if (psym->st_name >= strtablen)
1782 {
1783 error (_("<corrupt string table index: %3ld>\n"),
1784 psym->st_name);
1785 res = false;
1786 }
1787 else
1788 {
1789 print_symbol (22, strtab + psym->st_name);
1790 if (version_string)
1791 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1792 version_string);
1793 }
1794
1795 if (is_rela)
1796 {
1797 bfd_vma off = rels[i].r_addend;
1798
1799 if ((bfd_signed_vma) off < 0)
1800 printf (" - %" BFD_VMA_FMT "x", - off);
1801 else
1802 printf (" + %" BFD_VMA_FMT "x", off);
1803 }
1804 }
1805 }
1806 else if (is_rela)
1807 {
1808 bfd_vma off = rels[i].r_addend;
1809
1810 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1811 if ((bfd_signed_vma) off < 0)
1812 printf ("-%" BFD_VMA_FMT "x", - off);
1813 else
1814 printf ("%" BFD_VMA_FMT "x", off);
1815 }
1816
1817 if (filedata->file_header.e_machine == EM_SPARCV9
1818 && rtype != NULL
1819 && streq (rtype, "R_SPARC_OLO10"))
1820 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1821
1822 putchar ('\n');
1823
1824 #ifdef BFD64
1825 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
1826 {
1827 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1828 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1829 const char * rtype2 = elf_mips_reloc_type (type2);
1830 const char * rtype3 = elf_mips_reloc_type (type3);
1831
1832 printf (" Type2: ");
1833
1834 if (rtype2 == NULL)
1835 printf (_("unrecognized: %-7lx"),
1836 (unsigned long) type2 & 0xffffffff);
1837 else
1838 printf ("%-17.17s", rtype2);
1839
1840 printf ("\n Type3: ");
1841
1842 if (rtype3 == NULL)
1843 printf (_("unrecognized: %-7lx"),
1844 (unsigned long) type3 & 0xffffffff);
1845 else
1846 printf ("%-17.17s", rtype3);
1847
1848 putchar ('\n');
1849 }
1850 #endif /* BFD64 */
1851 }
1852
1853 free (rels);
1854
1855 return res;
1856 }
1857
1858 static const char *
1859 get_aarch64_dynamic_type (unsigned long type)
1860 {
1861 switch (type)
1862 {
1863 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1864 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
1865 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
1866 default:
1867 return NULL;
1868 }
1869 }
1870
1871 static const char *
1872 get_mips_dynamic_type (unsigned long type)
1873 {
1874 switch (type)
1875 {
1876 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1877 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1878 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1879 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1880 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1881 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1882 case DT_MIPS_MSYM: return "MIPS_MSYM";
1883 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1884 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1885 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1886 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1887 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1888 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1889 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1890 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1891 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1892 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1893 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1894 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1895 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1896 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1897 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1898 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1899 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1900 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1901 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1902 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1903 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1904 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1905 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1906 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1907 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1908 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1909 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1910 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1911 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1912 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1913 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1914 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1915 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1916 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1917 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1918 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1919 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1920 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1921 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1922 case DT_MIPS_XHASH: return "MIPS_XHASH";
1923 default:
1924 return NULL;
1925 }
1926 }
1927
1928 static const char *
1929 get_sparc64_dynamic_type (unsigned long type)
1930 {
1931 switch (type)
1932 {
1933 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1934 default:
1935 return NULL;
1936 }
1937 }
1938
1939 static const char *
1940 get_ppc_dynamic_type (unsigned long type)
1941 {
1942 switch (type)
1943 {
1944 case DT_PPC_GOT: return "PPC_GOT";
1945 case DT_PPC_OPT: return "PPC_OPT";
1946 default:
1947 return NULL;
1948 }
1949 }
1950
1951 static const char *
1952 get_ppc64_dynamic_type (unsigned long type)
1953 {
1954 switch (type)
1955 {
1956 case DT_PPC64_GLINK: return "PPC64_GLINK";
1957 case DT_PPC64_OPD: return "PPC64_OPD";
1958 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1959 case DT_PPC64_OPT: return "PPC64_OPT";
1960 default:
1961 return NULL;
1962 }
1963 }
1964
1965 static const char *
1966 get_parisc_dynamic_type (unsigned long type)
1967 {
1968 switch (type)
1969 {
1970 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1971 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1972 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1973 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1974 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1975 case DT_HP_PREINIT: return "HP_PREINIT";
1976 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1977 case DT_HP_NEEDED: return "HP_NEEDED";
1978 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1979 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1980 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1981 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1982 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
1983 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
1984 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
1985 case DT_HP_FILTERED: return "HP_FILTERED";
1986 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
1987 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1988 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
1989 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
1990 case DT_PLT: return "PLT";
1991 case DT_PLT_SIZE: return "PLT_SIZE";
1992 case DT_DLT: return "DLT";
1993 case DT_DLT_SIZE: return "DLT_SIZE";
1994 default:
1995 return NULL;
1996 }
1997 }
1998
1999 static const char *
2000 get_ia64_dynamic_type (unsigned long type)
2001 {
2002 switch (type)
2003 {
2004 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2005 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2006 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2007 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2008 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2009 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2010 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2011 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2012 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2013 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2014 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2015 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2016 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2017 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2018 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2019 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2020 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2021 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2022 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2023 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2024 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2025 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2026 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2027 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2028 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2029 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2030 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2031 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2032 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2033 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2034 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2035 default:
2036 return NULL;
2037 }
2038 }
2039
2040 static const char *
2041 get_solaris_section_type (unsigned long type)
2042 {
2043 switch (type)
2044 {
2045 case 0x6fffffee: return "SUNW_ancillary";
2046 case 0x6fffffef: return "SUNW_capchain";
2047 case 0x6ffffff0: return "SUNW_capinfo";
2048 case 0x6ffffff1: return "SUNW_symsort";
2049 case 0x6ffffff2: return "SUNW_tlssort";
2050 case 0x6ffffff3: return "SUNW_LDYNSYM";
2051 case 0x6ffffff4: return "SUNW_dof";
2052 case 0x6ffffff5: return "SUNW_cap";
2053 case 0x6ffffff6: return "SUNW_SIGNATURE";
2054 case 0x6ffffff7: return "SUNW_ANNOTATE";
2055 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2056 case 0x6ffffff9: return "SUNW_DEBUG";
2057 case 0x6ffffffa: return "SUNW_move";
2058 case 0x6ffffffb: return "SUNW_COMDAT";
2059 case 0x6ffffffc: return "SUNW_syminfo";
2060 case 0x6ffffffd: return "SUNW_verdef";
2061 case 0x6ffffffe: return "SUNW_verneed";
2062 case 0x6fffffff: return "SUNW_versym";
2063 case 0x70000000: return "SPARC_GOTDATA";
2064 default: return NULL;
2065 }
2066 }
2067
2068 static const char *
2069 get_alpha_dynamic_type (unsigned long type)
2070 {
2071 switch (type)
2072 {
2073 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2074 default: return NULL;
2075 }
2076 }
2077
2078 static const char *
2079 get_score_dynamic_type (unsigned long type)
2080 {
2081 switch (type)
2082 {
2083 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2084 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2085 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2086 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2087 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2088 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2089 default: return NULL;
2090 }
2091 }
2092
2093 static const char *
2094 get_tic6x_dynamic_type (unsigned long type)
2095 {
2096 switch (type)
2097 {
2098 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2099 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2100 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2101 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2102 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2103 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2104 default: return NULL;
2105 }
2106 }
2107
2108 static const char *
2109 get_nios2_dynamic_type (unsigned long type)
2110 {
2111 switch (type)
2112 {
2113 case DT_NIOS2_GP: return "NIOS2_GP";
2114 default: return NULL;
2115 }
2116 }
2117
2118 static const char *
2119 get_solaris_dynamic_type (unsigned long type)
2120 {
2121 switch (type)
2122 {
2123 case 0x6000000d: return "SUNW_AUXILIARY";
2124 case 0x6000000e: return "SUNW_RTLDINF";
2125 case 0x6000000f: return "SUNW_FILTER";
2126 case 0x60000010: return "SUNW_CAP";
2127 case 0x60000011: return "SUNW_SYMTAB";
2128 case 0x60000012: return "SUNW_SYMSZ";
2129 case 0x60000013: return "SUNW_SORTENT";
2130 case 0x60000014: return "SUNW_SYMSORT";
2131 case 0x60000015: return "SUNW_SYMSORTSZ";
2132 case 0x60000016: return "SUNW_TLSSORT";
2133 case 0x60000017: return "SUNW_TLSSORTSZ";
2134 case 0x60000018: return "SUNW_CAPINFO";
2135 case 0x60000019: return "SUNW_STRPAD";
2136 case 0x6000001a: return "SUNW_CAPCHAIN";
2137 case 0x6000001b: return "SUNW_LDMACH";
2138 case 0x6000001d: return "SUNW_CAPCHAINENT";
2139 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2140 case 0x60000021: return "SUNW_PARENT";
2141 case 0x60000023: return "SUNW_ASLR";
2142 case 0x60000025: return "SUNW_RELAX";
2143 case 0x60000029: return "SUNW_NXHEAP";
2144 case 0x6000002b: return "SUNW_NXSTACK";
2145
2146 case 0x70000001: return "SPARC_REGISTER";
2147 case 0x7ffffffd: return "AUXILIARY";
2148 case 0x7ffffffe: return "USED";
2149 case 0x7fffffff: return "FILTER";
2150
2151 default: return NULL;
2152 }
2153 }
2154
2155 static const char *
2156 get_dynamic_type (Filedata * filedata, unsigned long type)
2157 {
2158 static char buff[64];
2159
2160 switch (type)
2161 {
2162 case DT_NULL: return "NULL";
2163 case DT_NEEDED: return "NEEDED";
2164 case DT_PLTRELSZ: return "PLTRELSZ";
2165 case DT_PLTGOT: return "PLTGOT";
2166 case DT_HASH: return "HASH";
2167 case DT_STRTAB: return "STRTAB";
2168 case DT_SYMTAB: return "SYMTAB";
2169 case DT_RELA: return "RELA";
2170 case DT_RELASZ: return "RELASZ";
2171 case DT_RELAENT: return "RELAENT";
2172 case DT_STRSZ: return "STRSZ";
2173 case DT_SYMENT: return "SYMENT";
2174 case DT_INIT: return "INIT";
2175 case DT_FINI: return "FINI";
2176 case DT_SONAME: return "SONAME";
2177 case DT_RPATH: return "RPATH";
2178 case DT_SYMBOLIC: return "SYMBOLIC";
2179 case DT_REL: return "REL";
2180 case DT_RELSZ: return "RELSZ";
2181 case DT_RELENT: return "RELENT";
2182 case DT_PLTREL: return "PLTREL";
2183 case DT_DEBUG: return "DEBUG";
2184 case DT_TEXTREL: return "TEXTREL";
2185 case DT_JMPREL: return "JMPREL";
2186 case DT_BIND_NOW: return "BIND_NOW";
2187 case DT_INIT_ARRAY: return "INIT_ARRAY";
2188 case DT_FINI_ARRAY: return "FINI_ARRAY";
2189 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2190 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2191 case DT_RUNPATH: return "RUNPATH";
2192 case DT_FLAGS: return "FLAGS";
2193
2194 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2195 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2196 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2197
2198 case DT_CHECKSUM: return "CHECKSUM";
2199 case DT_PLTPADSZ: return "PLTPADSZ";
2200 case DT_MOVEENT: return "MOVEENT";
2201 case DT_MOVESZ: return "MOVESZ";
2202 case DT_FEATURE: return "FEATURE";
2203 case DT_POSFLAG_1: return "POSFLAG_1";
2204 case DT_SYMINSZ: return "SYMINSZ";
2205 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2206
2207 case DT_ADDRRNGLO: return "ADDRRNGLO";
2208 case DT_CONFIG: return "CONFIG";
2209 case DT_DEPAUDIT: return "DEPAUDIT";
2210 case DT_AUDIT: return "AUDIT";
2211 case DT_PLTPAD: return "PLTPAD";
2212 case DT_MOVETAB: return "MOVETAB";
2213 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2214
2215 case DT_VERSYM: return "VERSYM";
2216
2217 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2218 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2219 case DT_RELACOUNT: return "RELACOUNT";
2220 case DT_RELCOUNT: return "RELCOUNT";
2221 case DT_FLAGS_1: return "FLAGS_1";
2222 case DT_VERDEF: return "VERDEF";
2223 case DT_VERDEFNUM: return "VERDEFNUM";
2224 case DT_VERNEED: return "VERNEED";
2225 case DT_VERNEEDNUM: return "VERNEEDNUM";
2226
2227 case DT_AUXILIARY: return "AUXILIARY";
2228 case DT_USED: return "USED";
2229 case DT_FILTER: return "FILTER";
2230
2231 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2232 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2233 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2234 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2235 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2236 case DT_GNU_HASH: return "GNU_HASH";
2237 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2238
2239 default:
2240 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2241 {
2242 const char * result;
2243
2244 switch (filedata->file_header.e_machine)
2245 {
2246 case EM_AARCH64:
2247 result = get_aarch64_dynamic_type (type);
2248 break;
2249 case EM_MIPS:
2250 case EM_MIPS_RS3_LE:
2251 result = get_mips_dynamic_type (type);
2252 break;
2253 case EM_SPARCV9:
2254 result = get_sparc64_dynamic_type (type);
2255 break;
2256 case EM_PPC:
2257 result = get_ppc_dynamic_type (type);
2258 break;
2259 case EM_PPC64:
2260 result = get_ppc64_dynamic_type (type);
2261 break;
2262 case EM_IA_64:
2263 result = get_ia64_dynamic_type (type);
2264 break;
2265 case EM_ALPHA:
2266 result = get_alpha_dynamic_type (type);
2267 break;
2268 case EM_SCORE:
2269 result = get_score_dynamic_type (type);
2270 break;
2271 case EM_TI_C6000:
2272 result = get_tic6x_dynamic_type (type);
2273 break;
2274 case EM_ALTERA_NIOS2:
2275 result = get_nios2_dynamic_type (type);
2276 break;
2277 default:
2278 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2279 result = get_solaris_dynamic_type (type);
2280 else
2281 result = NULL;
2282 break;
2283 }
2284
2285 if (result != NULL)
2286 return result;
2287
2288 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2289 }
2290 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2291 || (filedata->file_header.e_machine == EM_PARISC
2292 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2293 {
2294 const char * result;
2295
2296 switch (filedata->file_header.e_machine)
2297 {
2298 case EM_PARISC:
2299 result = get_parisc_dynamic_type (type);
2300 break;
2301 case EM_IA_64:
2302 result = get_ia64_dynamic_type (type);
2303 break;
2304 default:
2305 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2306 result = get_solaris_dynamic_type (type);
2307 else
2308 result = NULL;
2309 break;
2310 }
2311
2312 if (result != NULL)
2313 return result;
2314
2315 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2316 type);
2317 }
2318 else
2319 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2320
2321 return buff;
2322 }
2323 }
2324
2325 static char *
2326 get_file_type (unsigned e_type)
2327 {
2328 static char buff[64];
2329
2330 switch (e_type)
2331 {
2332 case ET_NONE: return _("NONE (None)");
2333 case ET_REL: return _("REL (Relocatable file)");
2334 case ET_EXEC: return _("EXEC (Executable file)");
2335 case ET_DYN: return _("DYN (Shared object file)");
2336 case ET_CORE: return _("CORE (Core file)");
2337
2338 default:
2339 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2340 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2341 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2342 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2343 else
2344 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2345 return buff;
2346 }
2347 }
2348
2349 static char *
2350 get_machine_name (unsigned e_machine)
2351 {
2352 static char buff[64]; /* XXX */
2353
2354 switch (e_machine)
2355 {
2356 /* Please keep this switch table sorted by increasing EM_ value. */
2357 /* 0 */
2358 case EM_NONE: return _("None");
2359 case EM_M32: return "WE32100";
2360 case EM_SPARC: return "Sparc";
2361 case EM_386: return "Intel 80386";
2362 case EM_68K: return "MC68000";
2363 case EM_88K: return "MC88000";
2364 case EM_IAMCU: return "Intel MCU";
2365 case EM_860: return "Intel 80860";
2366 case EM_MIPS: return "MIPS R3000";
2367 case EM_S370: return "IBM System/370";
2368 /* 10 */
2369 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2370 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2371 case EM_PARISC: return "HPPA";
2372 case EM_VPP550: return "Fujitsu VPP500";
2373 case EM_SPARC32PLUS: return "Sparc v8+" ;
2374 case EM_960: return "Intel 80960";
2375 case EM_PPC: return "PowerPC";
2376 /* 20 */
2377 case EM_PPC64: return "PowerPC64";
2378 case EM_S390_OLD:
2379 case EM_S390: return "IBM S/390";
2380 case EM_SPU: return "SPU";
2381 /* 30 */
2382 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2383 case EM_FR20: return "Fujitsu FR20";
2384 case EM_RH32: return "TRW RH32";
2385 case EM_MCORE: return "MCORE";
2386 /* 40 */
2387 case EM_ARM: return "ARM";
2388 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2389 case EM_SH: return "Renesas / SuperH SH";
2390 case EM_SPARCV9: return "Sparc v9";
2391 case EM_TRICORE: return "Siemens Tricore";
2392 case EM_ARC: return "ARC";
2393 case EM_H8_300: return "Renesas H8/300";
2394 case EM_H8_300H: return "Renesas H8/300H";
2395 case EM_H8S: return "Renesas H8S";
2396 case EM_H8_500: return "Renesas H8/500";
2397 /* 50 */
2398 case EM_IA_64: return "Intel IA-64";
2399 case EM_MIPS_X: return "Stanford MIPS-X";
2400 case EM_COLDFIRE: return "Motorola Coldfire";
2401 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2402 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2403 case EM_PCP: return "Siemens PCP";
2404 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2405 case EM_NDR1: return "Denso NDR1 microprocesspr";
2406 case EM_STARCORE: return "Motorola Star*Core processor";
2407 case EM_ME16: return "Toyota ME16 processor";
2408 /* 60 */
2409 case EM_ST100: return "STMicroelectronics ST100 processor";
2410 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2411 case EM_X86_64: return "Advanced Micro Devices X86-64";
2412 case EM_PDSP: return "Sony DSP processor";
2413 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2414 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2415 case EM_FX66: return "Siemens FX66 microcontroller";
2416 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2417 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2418 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2419 /* 70 */
2420 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2421 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2422 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2423 case EM_SVX: return "Silicon Graphics SVx";
2424 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2425 case EM_VAX: return "Digital VAX";
2426 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2427 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2428 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2429 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2430 /* 80 */
2431 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2432 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2433 case EM_PRISM: return "Vitesse Prism";
2434 case EM_AVR_OLD:
2435 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2436 case EM_CYGNUS_FR30:
2437 case EM_FR30: return "Fujitsu FR30";
2438 case EM_CYGNUS_D10V:
2439 case EM_D10V: return "d10v";
2440 case EM_CYGNUS_D30V:
2441 case EM_D30V: return "d30v";
2442 case EM_CYGNUS_V850:
2443 case EM_V850: return "Renesas V850";
2444 case EM_CYGNUS_M32R:
2445 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2446 case EM_CYGNUS_MN10300:
2447 case EM_MN10300: return "mn10300";
2448 /* 90 */
2449 case EM_CYGNUS_MN10200:
2450 case EM_MN10200: return "mn10200";
2451 case EM_PJ: return "picoJava";
2452 case EM_OR1K: return "OpenRISC 1000";
2453 case EM_ARC_COMPACT: return "ARCompact";
2454 case EM_XTENSA_OLD:
2455 case EM_XTENSA: return "Tensilica Xtensa Processor";
2456 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2457 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2458 case EM_NS32K: return "National Semiconductor 32000 series";
2459 case EM_TPC: return "Tenor Network TPC processor";
2460 case EM_SNP1K: return "Trebia SNP 1000 processor";
2461 /* 100 */
2462 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2463 case EM_IP2K_OLD:
2464 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2465 case EM_MAX: return "MAX Processor";
2466 case EM_CR: return "National Semiconductor CompactRISC";
2467 case EM_F2MC16: return "Fujitsu F2MC16";
2468 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2469 case EM_BLACKFIN: return "Analog Devices Blackfin";
2470 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2471 case EM_SEP: return "Sharp embedded microprocessor";
2472 case EM_ARCA: return "Arca RISC microprocessor";
2473 /* 110 */
2474 case EM_UNICORE: return "Unicore";
2475 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2476 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2477 case EM_ALTERA_NIOS2: return "Altera Nios II";
2478 case EM_CRX: return "National Semiconductor CRX microprocessor";
2479 case EM_XGATE: return "Motorola XGATE embedded processor";
2480 case EM_C166:
2481 case EM_XC16X: return "Infineon Technologies xc16x";
2482 case EM_M16C: return "Renesas M16C series microprocessors";
2483 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2484 case EM_CE: return "Freescale Communication Engine RISC core";
2485 /* 120 */
2486 case EM_M32C: return "Renesas M32c";
2487 /* 130 */
2488 case EM_TSK3000: return "Altium TSK3000 core";
2489 case EM_RS08: return "Freescale RS08 embedded processor";
2490 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2491 case EM_SCORE: return "SUNPLUS S+Core";
2492 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2493 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2494 case EM_LATTICEMICO32: return "Lattice Mico32";
2495 case EM_SE_C17: return "Seiko Epson C17 family";
2496 /* 140 */
2497 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2498 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2499 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2500 case EM_TI_PRU: return "TI PRU I/O processor";
2501 /* 160 */
2502 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2503 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2504 case EM_R32C: return "Renesas R32C series microprocessors";
2505 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2506 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2507 case EM_8051: return "Intel 8051 and variants";
2508 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2509 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2510 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2511 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2512 /* 170 */
2513 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2514 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2515 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2516 case EM_RX: return "Renesas RX";
2517 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2518 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2519 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2520 case EM_CR16:
2521 case EM_MICROBLAZE:
2522 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2523 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2524 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2525 /* 180 */
2526 case EM_L1OM: return "Intel L1OM";
2527 case EM_K1OM: return "Intel K1OM";
2528 case EM_INTEL182: return "Intel (reserved)";
2529 case EM_AARCH64: return "AArch64";
2530 case EM_ARM184: return "ARM (reserved)";
2531 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2532 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2533 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2534 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2535 /* 190 */
2536 case EM_CUDA: return "NVIDIA CUDA architecture";
2537 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2538 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2539 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2540 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2541 case EM_ARC_COMPACT2: return "ARCv2";
2542 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2543 case EM_RL78: return "Renesas RL78";
2544 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2545 case EM_78K0R: return "Renesas 78K0R";
2546 /* 200 */
2547 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2548 case EM_BA1: return "Beyond BA1 CPU architecture";
2549 case EM_BA2: return "Beyond BA2 CPU architecture";
2550 case EM_XCORE: return "XMOS xCORE processor family";
2551 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2552 case EM_INTELGT: return "Intel Graphics Technology";
2553 /* 210 */
2554 case EM_KM32: return "KM211 KM32 32-bit processor";
2555 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2556 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2557 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2558 case EM_KVARC: return "KM211 KVARC processor";
2559 case EM_CDP: return "Paneve CDP architecture family";
2560 case EM_COGE: return "Cognitive Smart Memory Processor";
2561 case EM_COOL: return "Bluechip Systems CoolEngine";
2562 case EM_NORC: return "Nanoradio Optimized RISC";
2563 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2564 /* 220 */
2565 case EM_Z80: return "Zilog Z80";
2566 case EM_VISIUM: return "CDS VISIUMcore processor";
2567 case EM_FT32: return "FTDI Chip FT32";
2568 case EM_MOXIE: return "Moxie";
2569 case EM_AMDGPU: return "AMD GPU";
2570 /* 230 (all reserved) */
2571 /* 240 */
2572 case EM_RISCV: return "RISC-V";
2573 case EM_LANAI: return "Lanai 32-bit processor";
2574 case EM_CEVA: return "CEVA Processor Architecture Family";
2575 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2576 case EM_BPF: return "Linux BPF";
2577 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2578 case EM_IMG1: return "Imagination Technologies";
2579 /* 250 */
2580 case EM_NFP: return "Netronome Flow Processor";
2581 case EM_VE: return "NEC Vector Engine";
2582 case EM_CSKY: return "C-SKY";
2583 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2584 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2585 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2586 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2587 case EM_65816: return "WDC 65816/65C816";
2588 case EM_LOONGARCH: return "Loongson Loongarch";
2589 case EM_KF32: return "ChipON KungFu32";
2590
2591 /* Large numbers... */
2592 case EM_MT: return "Morpho Techologies MT processor";
2593 case EM_ALPHA: return "Alpha";
2594 case EM_WEBASSEMBLY: return "Web Assembly";
2595 case EM_DLX: return "OpenDLX";
2596 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2597 case EM_IQ2000: return "Vitesse IQ2000";
2598 case EM_M32C_OLD:
2599 case EM_NIOS32: return "Altera Nios";
2600 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2601 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2602 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2603 case EM_S12Z: return "Freescale S12Z";
2604
2605 default:
2606 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2607 return buff;
2608 }
2609 }
2610
2611 static void
2612 decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2613 {
2614 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2615 other compilers don't specify an architecture type in the e_flags, and
2616 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2617 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2618 architectures.
2619
2620 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2621 but also sets a specific architecture type in the e_flags field.
2622
2623 However, when decoding the flags we don't worry if we see an
2624 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2625 ARCEM architecture type. */
2626
2627 switch (e_flags & EF_ARC_MACH_MSK)
2628 {
2629 /* We only expect these to occur for EM_ARC_COMPACT2. */
2630 case EF_ARC_CPU_ARCV2EM:
2631 strcat (buf, ", ARC EM");
2632 break;
2633 case EF_ARC_CPU_ARCV2HS:
2634 strcat (buf, ", ARC HS");
2635 break;
2636
2637 /* We only expect these to occur for EM_ARC_COMPACT. */
2638 case E_ARC_MACH_ARC600:
2639 strcat (buf, ", ARC600");
2640 break;
2641 case E_ARC_MACH_ARC601:
2642 strcat (buf, ", ARC601");
2643 break;
2644 case E_ARC_MACH_ARC700:
2645 strcat (buf, ", ARC700");
2646 break;
2647
2648 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2649 new ELF with new architecture being read by an old version of
2650 readelf, or (c) An ELF built with non-GNU compiler that does not
2651 set the architecture in the e_flags. */
2652 default:
2653 if (e_machine == EM_ARC_COMPACT)
2654 strcat (buf, ", Unknown ARCompact");
2655 else
2656 strcat (buf, ", Unknown ARC");
2657 break;
2658 }
2659
2660 switch (e_flags & EF_ARC_OSABI_MSK)
2661 {
2662 case E_ARC_OSABI_ORIG:
2663 strcat (buf, ", (ABI:legacy)");
2664 break;
2665 case E_ARC_OSABI_V2:
2666 strcat (buf, ", (ABI:v2)");
2667 break;
2668 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2669 case E_ARC_OSABI_V3:
2670 strcat (buf, ", v3 no-legacy-syscalls ABI");
2671 break;
2672 case E_ARC_OSABI_V4:
2673 strcat (buf, ", v4 ABI");
2674 break;
2675 default:
2676 strcat (buf, ", unrecognised ARC OSABI flag");
2677 break;
2678 }
2679 }
2680
2681 static void
2682 decode_ARM_machine_flags (unsigned e_flags, char buf[])
2683 {
2684 unsigned eabi;
2685 bool unknown = false;
2686
2687 eabi = EF_ARM_EABI_VERSION (e_flags);
2688 e_flags &= ~ EF_ARM_EABIMASK;
2689
2690 /* Handle "generic" ARM flags. */
2691 if (e_flags & EF_ARM_RELEXEC)
2692 {
2693 strcat (buf, ", relocatable executable");
2694 e_flags &= ~ EF_ARM_RELEXEC;
2695 }
2696
2697 if (e_flags & EF_ARM_PIC)
2698 {
2699 strcat (buf, ", position independent");
2700 e_flags &= ~ EF_ARM_PIC;
2701 }
2702
2703 /* Now handle EABI specific flags. */
2704 switch (eabi)
2705 {
2706 default:
2707 strcat (buf, ", <unrecognized EABI>");
2708 if (e_flags)
2709 unknown = true;
2710 break;
2711
2712 case EF_ARM_EABI_VER1:
2713 strcat (buf, ", Version1 EABI");
2714 while (e_flags)
2715 {
2716 unsigned flag;
2717
2718 /* Process flags one bit at a time. */
2719 flag = e_flags & - e_flags;
2720 e_flags &= ~ flag;
2721
2722 switch (flag)
2723 {
2724 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2725 strcat (buf, ", sorted symbol tables");
2726 break;
2727
2728 default:
2729 unknown = true;
2730 break;
2731 }
2732 }
2733 break;
2734
2735 case EF_ARM_EABI_VER2:
2736 strcat (buf, ", Version2 EABI");
2737 while (e_flags)
2738 {
2739 unsigned flag;
2740
2741 /* Process flags one bit at a time. */
2742 flag = e_flags & - e_flags;
2743 e_flags &= ~ flag;
2744
2745 switch (flag)
2746 {
2747 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2748 strcat (buf, ", sorted symbol tables");
2749 break;
2750
2751 case EF_ARM_DYNSYMSUSESEGIDX:
2752 strcat (buf, ", dynamic symbols use segment index");
2753 break;
2754
2755 case EF_ARM_MAPSYMSFIRST:
2756 strcat (buf, ", mapping symbols precede others");
2757 break;
2758
2759 default:
2760 unknown = true;
2761 break;
2762 }
2763 }
2764 break;
2765
2766 case EF_ARM_EABI_VER3:
2767 strcat (buf, ", Version3 EABI");
2768 break;
2769
2770 case EF_ARM_EABI_VER4:
2771 strcat (buf, ", Version4 EABI");
2772 while (e_flags)
2773 {
2774 unsigned flag;
2775
2776 /* Process flags one bit at a time. */
2777 flag = e_flags & - e_flags;
2778 e_flags &= ~ flag;
2779
2780 switch (flag)
2781 {
2782 case EF_ARM_BE8:
2783 strcat (buf, ", BE8");
2784 break;
2785
2786 case EF_ARM_LE8:
2787 strcat (buf, ", LE8");
2788 break;
2789
2790 default:
2791 unknown = true;
2792 break;
2793 }
2794 }
2795 break;
2796
2797 case EF_ARM_EABI_VER5:
2798 strcat (buf, ", Version5 EABI");
2799 while (e_flags)
2800 {
2801 unsigned flag;
2802
2803 /* Process flags one bit at a time. */
2804 flag = e_flags & - e_flags;
2805 e_flags &= ~ flag;
2806
2807 switch (flag)
2808 {
2809 case EF_ARM_BE8:
2810 strcat (buf, ", BE8");
2811 break;
2812
2813 case EF_ARM_LE8:
2814 strcat (buf, ", LE8");
2815 break;
2816
2817 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
2818 strcat (buf, ", soft-float ABI");
2819 break;
2820
2821 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
2822 strcat (buf, ", hard-float ABI");
2823 break;
2824
2825 default:
2826 unknown = true;
2827 break;
2828 }
2829 }
2830 break;
2831
2832 case EF_ARM_EABI_UNKNOWN:
2833 strcat (buf, ", GNU EABI");
2834 while (e_flags)
2835 {
2836 unsigned flag;
2837
2838 /* Process flags one bit at a time. */
2839 flag = e_flags & - e_flags;
2840 e_flags &= ~ flag;
2841
2842 switch (flag)
2843 {
2844 case EF_ARM_INTERWORK:
2845 strcat (buf, ", interworking enabled");
2846 break;
2847
2848 case EF_ARM_APCS_26:
2849 strcat (buf, ", uses APCS/26");
2850 break;
2851
2852 case EF_ARM_APCS_FLOAT:
2853 strcat (buf, ", uses APCS/float");
2854 break;
2855
2856 case EF_ARM_PIC:
2857 strcat (buf, ", position independent");
2858 break;
2859
2860 case EF_ARM_ALIGN8:
2861 strcat (buf, ", 8 bit structure alignment");
2862 break;
2863
2864 case EF_ARM_NEW_ABI:
2865 strcat (buf, ", uses new ABI");
2866 break;
2867
2868 case EF_ARM_OLD_ABI:
2869 strcat (buf, ", uses old ABI");
2870 break;
2871
2872 case EF_ARM_SOFT_FLOAT:
2873 strcat (buf, ", software FP");
2874 break;
2875
2876 case EF_ARM_VFP_FLOAT:
2877 strcat (buf, ", VFP");
2878 break;
2879
2880 case EF_ARM_MAVERICK_FLOAT:
2881 strcat (buf, ", Maverick FP");
2882 break;
2883
2884 default:
2885 unknown = true;
2886 break;
2887 }
2888 }
2889 }
2890
2891 if (unknown)
2892 strcat (buf,_(", <unknown>"));
2893 }
2894
2895 static void
2896 decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2897 {
2898 --size; /* Leave space for null terminator. */
2899
2900 switch (e_flags & EF_AVR_MACH)
2901 {
2902 case E_AVR_MACH_AVR1:
2903 strncat (buf, ", avr:1", size);
2904 break;
2905 case E_AVR_MACH_AVR2:
2906 strncat (buf, ", avr:2", size);
2907 break;
2908 case E_AVR_MACH_AVR25:
2909 strncat (buf, ", avr:25", size);
2910 break;
2911 case E_AVR_MACH_AVR3:
2912 strncat (buf, ", avr:3", size);
2913 break;
2914 case E_AVR_MACH_AVR31:
2915 strncat (buf, ", avr:31", size);
2916 break;
2917 case E_AVR_MACH_AVR35:
2918 strncat (buf, ", avr:35", size);
2919 break;
2920 case E_AVR_MACH_AVR4:
2921 strncat (buf, ", avr:4", size);
2922 break;
2923 case E_AVR_MACH_AVR5:
2924 strncat (buf, ", avr:5", size);
2925 break;
2926 case E_AVR_MACH_AVR51:
2927 strncat (buf, ", avr:51", size);
2928 break;
2929 case E_AVR_MACH_AVR6:
2930 strncat (buf, ", avr:6", size);
2931 break;
2932 case E_AVR_MACH_AVRTINY:
2933 strncat (buf, ", avr:100", size);
2934 break;
2935 case E_AVR_MACH_XMEGA1:
2936 strncat (buf, ", avr:101", size);
2937 break;
2938 case E_AVR_MACH_XMEGA2:
2939 strncat (buf, ", avr:102", size);
2940 break;
2941 case E_AVR_MACH_XMEGA3:
2942 strncat (buf, ", avr:103", size);
2943 break;
2944 case E_AVR_MACH_XMEGA4:
2945 strncat (buf, ", avr:104", size);
2946 break;
2947 case E_AVR_MACH_XMEGA5:
2948 strncat (buf, ", avr:105", size);
2949 break;
2950 case E_AVR_MACH_XMEGA6:
2951 strncat (buf, ", avr:106", size);
2952 break;
2953 case E_AVR_MACH_XMEGA7:
2954 strncat (buf, ", avr:107", size);
2955 break;
2956 default:
2957 strncat (buf, ", avr:<unknown>", size);
2958 break;
2959 }
2960
2961 size -= strlen (buf);
2962 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2963 strncat (buf, ", link-relax", size);
2964 }
2965
2966 static void
2967 decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2968 {
2969 unsigned abi;
2970 unsigned arch;
2971 unsigned config;
2972 unsigned version;
2973 bool has_fpu = false;
2974 unsigned int r = 0;
2975
2976 static const char *ABI_STRINGS[] =
2977 {
2978 "ABI v0", /* use r5 as return register; only used in N1213HC */
2979 "ABI v1", /* use r0 as return register */
2980 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2981 "ABI v2fp", /* for FPU */
2982 "AABI",
2983 "ABI2 FP+"
2984 };
2985 static const char *VER_STRINGS[] =
2986 {
2987 "Andes ELF V1.3 or older",
2988 "Andes ELF V1.3.1",
2989 "Andes ELF V1.4"
2990 };
2991 static const char *ARCH_STRINGS[] =
2992 {
2993 "",
2994 "Andes Star v1.0",
2995 "Andes Star v2.0",
2996 "Andes Star v3.0",
2997 "Andes Star v3.0m"
2998 };
2999
3000 abi = EF_NDS_ABI & e_flags;
3001 arch = EF_NDS_ARCH & e_flags;
3002 config = EF_NDS_INST & e_flags;
3003 version = EF_NDS32_ELF_VERSION & e_flags;
3004
3005 memset (buf, 0, size);
3006
3007 switch (abi)
3008 {
3009 case E_NDS_ABI_V0:
3010 case E_NDS_ABI_V1:
3011 case E_NDS_ABI_V2:
3012 case E_NDS_ABI_V2FP:
3013 case E_NDS_ABI_AABI:
3014 case E_NDS_ABI_V2FP_PLUS:
3015 /* In case there are holes in the array. */
3016 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3017 break;
3018
3019 default:
3020 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3021 break;
3022 }
3023
3024 switch (version)
3025 {
3026 case E_NDS32_ELF_VER_1_2:
3027 case E_NDS32_ELF_VER_1_3:
3028 case E_NDS32_ELF_VER_1_4:
3029 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3030 break;
3031
3032 default:
3033 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3034 break;
3035 }
3036
3037 if (E_NDS_ABI_V0 == abi)
3038 {
3039 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3040 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3041 if (arch == E_NDS_ARCH_STAR_V1_0)
3042 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3043 return;
3044 }
3045
3046 switch (arch)
3047 {
3048 case E_NDS_ARCH_STAR_V1_0:
3049 case E_NDS_ARCH_STAR_V2_0:
3050 case E_NDS_ARCH_STAR_V3_0:
3051 case E_NDS_ARCH_STAR_V3_M:
3052 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3053 break;
3054
3055 default:
3056 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3057 /* ARCH version determines how the e_flags are interpreted.
3058 If it is unknown, we cannot proceed. */
3059 return;
3060 }
3061
3062 /* Newer ABI; Now handle architecture specific flags. */
3063 if (arch == E_NDS_ARCH_STAR_V1_0)
3064 {
3065 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3066 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3067
3068 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3069 r += snprintf (buf + r, size -r, ", MAC");
3070
3071 if (config & E_NDS32_HAS_DIV_INST)
3072 r += snprintf (buf + r, size -r, ", DIV");
3073
3074 if (config & E_NDS32_HAS_16BIT_INST)
3075 r += snprintf (buf + r, size -r, ", 16b");
3076 }
3077 else
3078 {
3079 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3080 {
3081 if (version <= E_NDS32_ELF_VER_1_3)
3082 r += snprintf (buf + r, size -r, ", [B8]");
3083 else
3084 r += snprintf (buf + r, size -r, ", EX9");
3085 }
3086
3087 if (config & E_NDS32_HAS_MAC_DX_INST)
3088 r += snprintf (buf + r, size -r, ", MAC_DX");
3089
3090 if (config & E_NDS32_HAS_DIV_DX_INST)
3091 r += snprintf (buf + r, size -r, ", DIV_DX");
3092
3093 if (config & E_NDS32_HAS_16BIT_INST)
3094 {
3095 if (version <= E_NDS32_ELF_VER_1_3)
3096 r += snprintf (buf + r, size -r, ", 16b");
3097 else
3098 r += snprintf (buf + r, size -r, ", IFC");
3099 }
3100 }
3101
3102 if (config & E_NDS32_HAS_EXT_INST)
3103 r += snprintf (buf + r, size -r, ", PERF1");
3104
3105 if (config & E_NDS32_HAS_EXT2_INST)
3106 r += snprintf (buf + r, size -r, ", PERF2");
3107
3108 if (config & E_NDS32_HAS_FPU_INST)
3109 {
3110 has_fpu = true;
3111 r += snprintf (buf + r, size -r, ", FPU_SP");
3112 }
3113
3114 if (config & E_NDS32_HAS_FPU_DP_INST)
3115 {
3116 has_fpu = true;
3117 r += snprintf (buf + r, size -r, ", FPU_DP");
3118 }
3119
3120 if (config & E_NDS32_HAS_FPU_MAC_INST)
3121 {
3122 has_fpu = true;
3123 r += snprintf (buf + r, size -r, ", FPU_MAC");
3124 }
3125
3126 if (has_fpu)
3127 {
3128 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3129 {
3130 case E_NDS32_FPU_REG_8SP_4DP:
3131 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3132 break;
3133 case E_NDS32_FPU_REG_16SP_8DP:
3134 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3135 break;
3136 case E_NDS32_FPU_REG_32SP_16DP:
3137 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3138 break;
3139 case E_NDS32_FPU_REG_32SP_32DP:
3140 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3141 break;
3142 }
3143 }
3144
3145 if (config & E_NDS32_HAS_AUDIO_INST)
3146 r += snprintf (buf + r, size -r, ", AUDIO");
3147
3148 if (config & E_NDS32_HAS_STRING_INST)
3149 r += snprintf (buf + r, size -r, ", STR");
3150
3151 if (config & E_NDS32_HAS_REDUCED_REGS)
3152 r += snprintf (buf + r, size -r, ", 16REG");
3153
3154 if (config & E_NDS32_HAS_VIDEO_INST)
3155 {
3156 if (version <= E_NDS32_ELF_VER_1_3)
3157 r += snprintf (buf + r, size -r, ", VIDEO");
3158 else
3159 r += snprintf (buf + r, size -r, ", SATURATION");
3160 }
3161
3162 if (config & E_NDS32_HAS_ENCRIPT_INST)
3163 r += snprintf (buf + r, size -r, ", ENCRP");
3164
3165 if (config & E_NDS32_HAS_L2C_INST)
3166 r += snprintf (buf + r, size -r, ", L2C");
3167 }
3168
3169 static char *
3170 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3171 {
3172 static char buf[1024];
3173
3174 buf[0] = '\0';
3175
3176 if (e_flags)
3177 {
3178 switch (e_machine)
3179 {
3180 default:
3181 break;
3182
3183 case EM_ARC_COMPACT2:
3184 case EM_ARC_COMPACT:
3185 decode_ARC_machine_flags (e_flags, e_machine, buf);
3186 break;
3187
3188 case EM_ARM:
3189 decode_ARM_machine_flags (e_flags, buf);
3190 break;
3191
3192 case EM_AVR:
3193 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3194 break;
3195
3196 case EM_BLACKFIN:
3197 if (e_flags & EF_BFIN_PIC)
3198 strcat (buf, ", PIC");
3199
3200 if (e_flags & EF_BFIN_FDPIC)
3201 strcat (buf, ", FDPIC");
3202
3203 if (e_flags & EF_BFIN_CODE_IN_L1)
3204 strcat (buf, ", code in L1");
3205
3206 if (e_flags & EF_BFIN_DATA_IN_L1)
3207 strcat (buf, ", data in L1");
3208
3209 break;
3210
3211 case EM_CYGNUS_FRV:
3212 switch (e_flags & EF_FRV_CPU_MASK)
3213 {
3214 case EF_FRV_CPU_GENERIC:
3215 break;
3216
3217 default:
3218 strcat (buf, ", fr???");
3219 break;
3220
3221 case EF_FRV_CPU_FR300:
3222 strcat (buf, ", fr300");
3223 break;
3224
3225 case EF_FRV_CPU_FR400:
3226 strcat (buf, ", fr400");
3227 break;
3228 case EF_FRV_CPU_FR405:
3229 strcat (buf, ", fr405");
3230 break;
3231
3232 case EF_FRV_CPU_FR450:
3233 strcat (buf, ", fr450");
3234 break;
3235
3236 case EF_FRV_CPU_FR500:
3237 strcat (buf, ", fr500");
3238 break;
3239 case EF_FRV_CPU_FR550:
3240 strcat (buf, ", fr550");
3241 break;
3242
3243 case EF_FRV_CPU_SIMPLE:
3244 strcat (buf, ", simple");
3245 break;
3246 case EF_FRV_CPU_TOMCAT:
3247 strcat (buf, ", tomcat");
3248 break;
3249 }
3250 break;
3251
3252 case EM_68K:
3253 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3254 strcat (buf, ", m68000");
3255 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3256 strcat (buf, ", cpu32");
3257 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3258 strcat (buf, ", fido_a");
3259 else
3260 {
3261 char const * isa = _("unknown");
3262 char const * mac = _("unknown mac");
3263 char const * additional = NULL;
3264
3265 switch (e_flags & EF_M68K_CF_ISA_MASK)
3266 {
3267 case EF_M68K_CF_ISA_A_NODIV:
3268 isa = "A";
3269 additional = ", nodiv";
3270 break;
3271 case EF_M68K_CF_ISA_A:
3272 isa = "A";
3273 break;
3274 case EF_M68K_CF_ISA_A_PLUS:
3275 isa = "A+";
3276 break;
3277 case EF_M68K_CF_ISA_B_NOUSP:
3278 isa = "B";
3279 additional = ", nousp";
3280 break;
3281 case EF_M68K_CF_ISA_B:
3282 isa = "B";
3283 break;
3284 case EF_M68K_CF_ISA_C:
3285 isa = "C";
3286 break;
3287 case EF_M68K_CF_ISA_C_NODIV:
3288 isa = "C";
3289 additional = ", nodiv";
3290 break;
3291 }
3292 strcat (buf, ", cf, isa ");
3293 strcat (buf, isa);
3294 if (additional)
3295 strcat (buf, additional);
3296 if (e_flags & EF_M68K_CF_FLOAT)
3297 strcat (buf, ", float");
3298 switch (e_flags & EF_M68K_CF_MAC_MASK)
3299 {
3300 case 0:
3301 mac = NULL;
3302 break;
3303 case EF_M68K_CF_MAC:
3304 mac = "mac";
3305 break;
3306 case EF_M68K_CF_EMAC:
3307 mac = "emac";
3308 break;
3309 case EF_M68K_CF_EMAC_B:
3310 mac = "emac_b";
3311 break;
3312 }
3313 if (mac)
3314 {
3315 strcat (buf, ", ");
3316 strcat (buf, mac);
3317 }
3318 }
3319 break;
3320
3321 case EM_CYGNUS_MEP:
3322 switch (e_flags & EF_MEP_CPU_MASK)
3323 {
3324 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3325 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3326 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3327 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3328 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3329 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3330 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3331 }
3332
3333 switch (e_flags & EF_MEP_COP_MASK)
3334 {
3335 case EF_MEP_COP_NONE: break;
3336 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3337 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3338 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3339 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3340 default: strcat (buf, _("<unknown MeP copro type>")); break;
3341 }
3342
3343 if (e_flags & EF_MEP_LIBRARY)
3344 strcat (buf, ", Built for Library");
3345
3346 if (e_flags & EF_MEP_INDEX_MASK)
3347 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3348 e_flags & EF_MEP_INDEX_MASK);
3349
3350 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3351 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3352 e_flags & ~ EF_MEP_ALL_FLAGS);
3353 break;
3354
3355 case EM_PPC:
3356 if (e_flags & EF_PPC_EMB)
3357 strcat (buf, ", emb");
3358
3359 if (e_flags & EF_PPC_RELOCATABLE)
3360 strcat (buf, _(", relocatable"));
3361
3362 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3363 strcat (buf, _(", relocatable-lib"));
3364 break;
3365
3366 case EM_PPC64:
3367 if (e_flags & EF_PPC64_ABI)
3368 {
3369 char abi[] = ", abiv0";
3370
3371 abi[6] += e_flags & EF_PPC64_ABI;
3372 strcat (buf, abi);
3373 }
3374 break;
3375
3376 case EM_V800:
3377 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3378 strcat (buf, ", RH850 ABI");
3379
3380 if (e_flags & EF_V800_850E3)
3381 strcat (buf, ", V3 architecture");
3382
3383 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3384 strcat (buf, ", FPU not used");
3385
3386 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3387 strcat (buf, ", regmode: COMMON");
3388
3389 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3390 strcat (buf, ", r4 not used");
3391
3392 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3393 strcat (buf, ", r30 not used");
3394
3395 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3396 strcat (buf, ", r5 not used");
3397
3398 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3399 strcat (buf, ", r2 not used");
3400
3401 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3402 {
3403 switch (e_flags & - e_flags)
3404 {
3405 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3406 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3407 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3408 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3409 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3410 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3411 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3412 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3413 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3414 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3415 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3416 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3417 default: break;
3418 }
3419 }
3420 break;
3421
3422 case EM_V850:
3423 case EM_CYGNUS_V850:
3424 switch (e_flags & EF_V850_ARCH)
3425 {
3426 case E_V850E3V5_ARCH:
3427 strcat (buf, ", v850e3v5");
3428 break;
3429 case E_V850E2V3_ARCH:
3430 strcat (buf, ", v850e2v3");
3431 break;
3432 case E_V850E2_ARCH:
3433 strcat (buf, ", v850e2");
3434 break;
3435 case E_V850E1_ARCH:
3436 strcat (buf, ", v850e1");
3437 break;
3438 case E_V850E_ARCH:
3439 strcat (buf, ", v850e");
3440 break;
3441 case E_V850_ARCH:
3442 strcat (buf, ", v850");
3443 break;
3444 default:
3445 strcat (buf, _(", unknown v850 architecture variant"));
3446 break;
3447 }
3448 break;
3449
3450 case EM_M32R:
3451 case EM_CYGNUS_M32R:
3452 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3453 strcat (buf, ", m32r");
3454 break;
3455
3456 case EM_MIPS:
3457 case EM_MIPS_RS3_LE:
3458 if (e_flags & EF_MIPS_NOREORDER)
3459 strcat (buf, ", noreorder");
3460
3461 if (e_flags & EF_MIPS_PIC)
3462 strcat (buf, ", pic");
3463
3464 if (e_flags & EF_MIPS_CPIC)
3465 strcat (buf, ", cpic");
3466
3467 if (e_flags & EF_MIPS_UCODE)
3468 strcat (buf, ", ugen_reserved");
3469
3470 if (e_flags & EF_MIPS_ABI2)
3471 strcat (buf, ", abi2");
3472
3473 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3474 strcat (buf, ", odk first");
3475
3476 if (e_flags & EF_MIPS_32BITMODE)
3477 strcat (buf, ", 32bitmode");
3478
3479 if (e_flags & EF_MIPS_NAN2008)
3480 strcat (buf, ", nan2008");
3481
3482 if (e_flags & EF_MIPS_FP64)
3483 strcat (buf, ", fp64");
3484
3485 switch ((e_flags & EF_MIPS_MACH))
3486 {
3487 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3488 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3489 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3490 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3491 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3492 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3493 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3494 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3495 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3496 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3497 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3498 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3499 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3500 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3501 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3502 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3503 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3504 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3505 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3506 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
3507 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
3508 case 0:
3509 /* We simply ignore the field in this case to avoid confusion:
3510 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3511 extension. */
3512 break;
3513 default: strcat (buf, _(", unknown CPU")); break;
3514 }
3515
3516 switch ((e_flags & EF_MIPS_ABI))
3517 {
3518 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3519 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3520 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3521 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3522 case 0:
3523 /* We simply ignore the field in this case to avoid confusion:
3524 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3525 This means it is likely to be an o32 file, but not for
3526 sure. */
3527 break;
3528 default: strcat (buf, _(", unknown ABI")); break;
3529 }
3530
3531 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3532 strcat (buf, ", mdmx");
3533
3534 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3535 strcat (buf, ", mips16");
3536
3537 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3538 strcat (buf, ", micromips");
3539
3540 switch ((e_flags & EF_MIPS_ARCH))
3541 {
3542 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3543 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3544 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3545 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3546 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3547 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3548 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3549 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3550 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3551 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3552 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3553 default: strcat (buf, _(", unknown ISA")); break;
3554 }
3555 break;
3556
3557 case EM_NDS32:
3558 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3559 break;
3560
3561 case EM_NFP:
3562 switch (EF_NFP_MACH (e_flags))
3563 {
3564 case E_NFP_MACH_3200:
3565 strcat (buf, ", NFP-32xx");
3566 break;
3567 case E_NFP_MACH_6000:
3568 strcat (buf, ", NFP-6xxx");
3569 break;
3570 }
3571 break;
3572
3573 case EM_RISCV:
3574 if (e_flags & EF_RISCV_RVC)
3575 strcat (buf, ", RVC");
3576
3577 if (e_flags & EF_RISCV_RVE)
3578 strcat (buf, ", RVE");
3579
3580 switch (e_flags & EF_RISCV_FLOAT_ABI)
3581 {
3582 case EF_RISCV_FLOAT_ABI_SOFT:
3583 strcat (buf, ", soft-float ABI");
3584 break;
3585
3586 case EF_RISCV_FLOAT_ABI_SINGLE:
3587 strcat (buf, ", single-float ABI");
3588 break;
3589
3590 case EF_RISCV_FLOAT_ABI_DOUBLE:
3591 strcat (buf, ", double-float ABI");
3592 break;
3593
3594 case EF_RISCV_FLOAT_ABI_QUAD:
3595 strcat (buf, ", quad-float ABI");
3596 break;
3597 }
3598 break;
3599
3600 case EM_SH:
3601 switch ((e_flags & EF_SH_MACH_MASK))
3602 {
3603 case EF_SH1: strcat (buf, ", sh1"); break;
3604 case EF_SH2: strcat (buf, ", sh2"); break;
3605 case EF_SH3: strcat (buf, ", sh3"); break;
3606 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3607 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3608 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3609 case EF_SH3E: strcat (buf, ", sh3e"); break;
3610 case EF_SH4: strcat (buf, ", sh4"); break;
3611 case EF_SH5: strcat (buf, ", sh5"); break;
3612 case EF_SH2E: strcat (buf, ", sh2e"); break;
3613 case EF_SH4A: strcat (buf, ", sh4a"); break;
3614 case EF_SH2A: strcat (buf, ", sh2a"); break;
3615 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3616 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3617 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3618 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3619 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3620 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3621 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3622 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3623 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3624 default: strcat (buf, _(", unknown ISA")); break;
3625 }
3626
3627 if (e_flags & EF_SH_PIC)
3628 strcat (buf, ", pic");
3629
3630 if (e_flags & EF_SH_FDPIC)
3631 strcat (buf, ", fdpic");
3632 break;
3633
3634 case EM_OR1K:
3635 if (e_flags & EF_OR1K_NODELAY)
3636 strcat (buf, ", no delay");
3637 break;
3638
3639 case EM_SPARCV9:
3640 if (e_flags & EF_SPARC_32PLUS)
3641 strcat (buf, ", v8+");
3642
3643 if (e_flags & EF_SPARC_SUN_US1)
3644 strcat (buf, ", ultrasparcI");
3645
3646 if (e_flags & EF_SPARC_SUN_US3)
3647 strcat (buf, ", ultrasparcIII");
3648
3649 if (e_flags & EF_SPARC_HAL_R1)
3650 strcat (buf, ", halr1");
3651
3652 if (e_flags & EF_SPARC_LEDATA)
3653 strcat (buf, ", ledata");
3654
3655 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3656 strcat (buf, ", tso");
3657
3658 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3659 strcat (buf, ", pso");
3660
3661 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3662 strcat (buf, ", rmo");
3663 break;
3664
3665 case EM_PARISC:
3666 switch (e_flags & EF_PARISC_ARCH)
3667 {
3668 case EFA_PARISC_1_0:
3669 strcpy (buf, ", PA-RISC 1.0");
3670 break;
3671 case EFA_PARISC_1_1:
3672 strcpy (buf, ", PA-RISC 1.1");
3673 break;
3674 case EFA_PARISC_2_0:
3675 strcpy (buf, ", PA-RISC 2.0");
3676 break;
3677 default:
3678 break;
3679 }
3680 if (e_flags & EF_PARISC_TRAPNIL)
3681 strcat (buf, ", trapnil");
3682 if (e_flags & EF_PARISC_EXT)
3683 strcat (buf, ", ext");
3684 if (e_flags & EF_PARISC_LSB)
3685 strcat (buf, ", lsb");
3686 if (e_flags & EF_PARISC_WIDE)
3687 strcat (buf, ", wide");
3688 if (e_flags & EF_PARISC_NO_KABP)
3689 strcat (buf, ", no kabp");
3690 if (e_flags & EF_PARISC_LAZYSWAP)
3691 strcat (buf, ", lazyswap");
3692 break;
3693
3694 case EM_PJ:
3695 case EM_PJ_OLD:
3696 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3697 strcat (buf, ", new calling convention");
3698
3699 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3700 strcat (buf, ", gnu calling convention");
3701 break;
3702
3703 case EM_IA_64:
3704 if ((e_flags & EF_IA_64_ABI64))
3705 strcat (buf, ", 64-bit");
3706 else
3707 strcat (buf, ", 32-bit");
3708 if ((e_flags & EF_IA_64_REDUCEDFP))
3709 strcat (buf, ", reduced fp model");
3710 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3711 strcat (buf, ", no function descriptors, constant gp");
3712 else if ((e_flags & EF_IA_64_CONS_GP))
3713 strcat (buf, ", constant gp");
3714 if ((e_flags & EF_IA_64_ABSOLUTE))
3715 strcat (buf, ", absolute");
3716 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3717 {
3718 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3719 strcat (buf, ", vms_linkages");
3720 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3721 {
3722 case EF_IA_64_VMS_COMCOD_SUCCESS:
3723 break;
3724 case EF_IA_64_VMS_COMCOD_WARNING:
3725 strcat (buf, ", warning");
3726 break;
3727 case EF_IA_64_VMS_COMCOD_ERROR:
3728 strcat (buf, ", error");
3729 break;
3730 case EF_IA_64_VMS_COMCOD_ABORT:
3731 strcat (buf, ", abort");
3732 break;
3733 default:
3734 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3735 e_flags & EF_IA_64_VMS_COMCOD);
3736 strcat (buf, ", <unknown>");
3737 }
3738 }
3739 break;
3740
3741 case EM_VAX:
3742 if ((e_flags & EF_VAX_NONPIC))
3743 strcat (buf, ", non-PIC");
3744 if ((e_flags & EF_VAX_DFLOAT))
3745 strcat (buf, ", D-Float");
3746 if ((e_flags & EF_VAX_GFLOAT))
3747 strcat (buf, ", G-Float");
3748 break;
3749
3750 case EM_VISIUM:
3751 if (e_flags & EF_VISIUM_ARCH_MCM)
3752 strcat (buf, ", mcm");
3753 else if (e_flags & EF_VISIUM_ARCH_MCM24)
3754 strcat (buf, ", mcm24");
3755 if (e_flags & EF_VISIUM_ARCH_GR6)
3756 strcat (buf, ", gr6");
3757 break;
3758
3759 case EM_RL78:
3760 switch (e_flags & E_FLAG_RL78_CPU_MASK)
3761 {
3762 case E_FLAG_RL78_ANY_CPU: break;
3763 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3764 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3765 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3766 }
3767 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3768 strcat (buf, ", 64-bit doubles");
3769 break;
3770
3771 case EM_RX:
3772 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3773 strcat (buf, ", 64-bit doubles");
3774 if (e_flags & E_FLAG_RX_DSP)
3775 strcat (buf, ", dsp");
3776 if (e_flags & E_FLAG_RX_PID)
3777 strcat (buf, ", pid");
3778 if (e_flags & E_FLAG_RX_ABI)
3779 strcat (buf, ", RX ABI");
3780 if (e_flags & E_FLAG_RX_SINSNS_SET)
3781 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3782 ? ", uses String instructions" : ", bans String instructions");
3783 if (e_flags & E_FLAG_RX_V2)
3784 strcat (buf, ", V2");
3785 if (e_flags & E_FLAG_RX_V3)
3786 strcat (buf, ", V3");
3787 break;
3788
3789 case EM_S390:
3790 if (e_flags & EF_S390_HIGH_GPRS)
3791 strcat (buf, ", highgprs");
3792 break;
3793
3794 case EM_TI_C6000:
3795 if ((e_flags & EF_C6000_REL))
3796 strcat (buf, ", relocatable module");
3797 break;
3798
3799 case EM_MSP430:
3800 strcat (buf, _(": architecture variant: "));
3801 switch (e_flags & EF_MSP430_MACH)
3802 {
3803 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3804 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3805 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3806 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3807 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3808 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3809 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3810 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3811 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3812 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3813 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3814 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3815 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3816 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3817 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
3818 default:
3819 strcat (buf, _(": unknown")); break;
3820 }
3821
3822 if (e_flags & ~ EF_MSP430_MACH)
3823 strcat (buf, _(": unknown extra flag bits also present"));
3824 break;
3825
3826 case EM_Z80:
3827 switch (e_flags & EF_Z80_MACH_MSK)
3828 {
3829 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
3830 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
3831 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
3832 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
3833 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
3834 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
3835 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
3836 default:
3837 strcat (buf, _(", unknown")); break;
3838 }
3839 break;
3840 }
3841 }
3842
3843 return buf;
3844 }
3845
3846 static const char *
3847 get_osabi_name (Filedata * filedata, unsigned int osabi)
3848 {
3849 static char buff[32];
3850
3851 switch (osabi)
3852 {
3853 case ELFOSABI_NONE: return "UNIX - System V";
3854 case ELFOSABI_HPUX: return "UNIX - HP-UX";
3855 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
3856 case ELFOSABI_GNU: return "UNIX - GNU";
3857 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
3858 case ELFOSABI_AIX: return "UNIX - AIX";
3859 case ELFOSABI_IRIX: return "UNIX - IRIX";
3860 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
3861 case ELFOSABI_TRU64: return "UNIX - TRU64";
3862 case ELFOSABI_MODESTO: return "Novell - Modesto";
3863 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
3864 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
3865 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
3866 case ELFOSABI_AROS: return "AROS";
3867 case ELFOSABI_FENIXOS: return "FenixOS";
3868 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
3869 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
3870 default:
3871 if (osabi >= 64)
3872 switch (filedata->file_header.e_machine)
3873 {
3874 case EM_ARM:
3875 switch (osabi)
3876 {
3877 case ELFOSABI_ARM: return "ARM";
3878 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
3879 default:
3880 break;
3881 }
3882 break;
3883
3884 case EM_MSP430:
3885 case EM_MSP430_OLD:
3886 case EM_VISIUM:
3887 switch (osabi)
3888 {
3889 case ELFOSABI_STANDALONE: return _("Standalone App");
3890 default:
3891 break;
3892 }
3893 break;
3894
3895 case EM_TI_C6000:
3896 switch (osabi)
3897 {
3898 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
3899 case ELFOSABI_C6000_LINUX: return "Linux C6000";
3900 default:
3901 break;
3902 }
3903 break;
3904
3905 default:
3906 break;
3907 }
3908 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
3909 return buff;
3910 }
3911 }
3912
3913 static const char *
3914 get_aarch64_segment_type (unsigned long type)
3915 {
3916 switch (type)
3917 {
3918 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
3919 default: return NULL;
3920 }
3921 }
3922
3923 static const char *
3924 get_arm_segment_type (unsigned long type)
3925 {
3926 switch (type)
3927 {
3928 case PT_ARM_EXIDX: return "EXIDX";
3929 default: return NULL;
3930 }
3931 }
3932
3933 static const char *
3934 get_s390_segment_type (unsigned long type)
3935 {
3936 switch (type)
3937 {
3938 case PT_S390_PGSTE: return "S390_PGSTE";
3939 default: return NULL;
3940 }
3941 }
3942
3943 static const char *
3944 get_mips_segment_type (unsigned long type)
3945 {
3946 switch (type)
3947 {
3948 case PT_MIPS_REGINFO: return "REGINFO";
3949 case PT_MIPS_RTPROC: return "RTPROC";
3950 case PT_MIPS_OPTIONS: return "OPTIONS";
3951 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
3952 default: return NULL;
3953 }
3954 }
3955
3956 static const char *
3957 get_parisc_segment_type (unsigned long type)
3958 {
3959 switch (type)
3960 {
3961 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
3962 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
3963 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
3964 default: return NULL;
3965 }
3966 }
3967
3968 static const char *
3969 get_ia64_segment_type (unsigned long type)
3970 {
3971 switch (type)
3972 {
3973 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
3974 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
3975 default: return NULL;
3976 }
3977 }
3978
3979 static const char *
3980 get_tic6x_segment_type (unsigned long type)
3981 {
3982 switch (type)
3983 {
3984 case PT_C6000_PHATTR: return "C6000_PHATTR";
3985 default: return NULL;
3986 }
3987 }
3988
3989 static const char *
3990 get_hpux_segment_type (unsigned long type, unsigned e_machine)
3991 {
3992 if (e_machine == EM_PARISC)
3993 switch (type)
3994 {
3995 case PT_HP_TLS: return "HP_TLS";
3996 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
3997 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
3998 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
3999 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4000 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4001 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4002 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4003 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4004 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4005 case PT_HP_PARALLEL: return "HP_PARALLEL";
4006 case PT_HP_FASTBIND: return "HP_FASTBIND";
4007 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4008 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4009 case PT_HP_STACK: return "HP_STACK";
4010 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4011 default: return NULL;
4012 }
4013
4014 if (e_machine == EM_IA_64)
4015 switch (type)
4016 {
4017 case PT_HP_TLS: return "HP_TLS";
4018 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4019 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4020 case PT_IA_64_HP_STACK: return "HP_STACK";
4021 default: return NULL;
4022 }
4023
4024 return NULL;
4025 }
4026
4027 static const char *
4028 get_solaris_segment_type (unsigned long type)
4029 {
4030 switch (type)
4031 {
4032 case 0x6464e550: return "PT_SUNW_UNWIND";
4033 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4034 case 0x6ffffff7: return "PT_LOSUNW";
4035 case 0x6ffffffa: return "PT_SUNWBSS";
4036 case 0x6ffffffb: return "PT_SUNWSTACK";
4037 case 0x6ffffffc: return "PT_SUNWDTRACE";
4038 case 0x6ffffffd: return "PT_SUNWCAP";
4039 case 0x6fffffff: return "PT_HISUNW";
4040 default: return NULL;
4041 }
4042 }
4043
4044 static const char *
4045 get_segment_type (Filedata * filedata, unsigned long p_type)
4046 {
4047 static char buff[32];
4048
4049 switch (p_type)
4050 {
4051 case PT_NULL: return "NULL";
4052 case PT_LOAD: return "LOAD";
4053 case PT_DYNAMIC: return "DYNAMIC";
4054 case PT_INTERP: return "INTERP";
4055 case PT_NOTE: return "NOTE";
4056 case PT_SHLIB: return "SHLIB";
4057 case PT_PHDR: return "PHDR";
4058 case PT_TLS: return "TLS";
4059 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4060 case PT_GNU_STACK: return "GNU_STACK";
4061 case PT_GNU_RELRO: return "GNU_RELRO";
4062 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4063
4064 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4065 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4066 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4067
4068 default:
4069 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4070 {
4071 const char * result;
4072
4073 switch (filedata->file_header.e_machine)
4074 {
4075 case EM_AARCH64:
4076 result = get_aarch64_segment_type (p_type);
4077 break;
4078 case EM_ARM:
4079 result = get_arm_segment_type (p_type);
4080 break;
4081 case EM_MIPS:
4082 case EM_MIPS_RS3_LE:
4083 result = get_mips_segment_type (p_type);
4084 break;
4085 case EM_PARISC:
4086 result = get_parisc_segment_type (p_type);
4087 break;
4088 case EM_IA_64:
4089 result = get_ia64_segment_type (p_type);
4090 break;
4091 case EM_TI_C6000:
4092 result = get_tic6x_segment_type (p_type);
4093 break;
4094 case EM_S390:
4095 case EM_S390_OLD:
4096 result = get_s390_segment_type (p_type);
4097 break;
4098 default:
4099 result = NULL;
4100 break;
4101 }
4102
4103 if (result != NULL)
4104 return result;
4105
4106 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4107 }
4108 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4109 {
4110 const char * result = NULL;
4111
4112 switch (filedata->file_header.e_ident[EI_OSABI])
4113 {
4114 case ELFOSABI_GNU:
4115 case ELFOSABI_FREEBSD:
4116 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4117 {
4118 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4119 result = buff;
4120 }
4121 break;
4122 case ELFOSABI_HPUX:
4123 result = get_hpux_segment_type (p_type,
4124 filedata->file_header.e_machine);
4125 break;
4126 case ELFOSABI_SOLARIS:
4127 result = get_solaris_segment_type (p_type);
4128 break;
4129 default:
4130 break;
4131 }
4132 if (result != NULL)
4133 return result;
4134
4135 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4136 }
4137 else
4138 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4139
4140 return buff;
4141 }
4142 }
4143
4144 static const char *
4145 get_arc_section_type_name (unsigned int sh_type)
4146 {
4147 switch (sh_type)
4148 {
4149 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4150 default:
4151 break;
4152 }
4153 return NULL;
4154 }
4155
4156 static const char *
4157 get_mips_section_type_name (unsigned int sh_type)
4158 {
4159 switch (sh_type)
4160 {
4161 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4162 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4163 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4164 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4165 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4166 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4167 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4168 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4169 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4170 case SHT_MIPS_RELD: return "MIPS_RELD";
4171 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4172 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4173 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4174 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4175 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4176 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4177 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4178 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4179 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4180 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4181 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4182 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4183 case SHT_MIPS_LINE: return "MIPS_LINE";
4184 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4185 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4186 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4187 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4188 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4189 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4190 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4191 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4192 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4193 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4194 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4195 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4196 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4197 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4198 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4199 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4200 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4201 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4202 default:
4203 break;
4204 }
4205 return NULL;
4206 }
4207
4208 static const char *
4209 get_parisc_section_type_name (unsigned int sh_type)
4210 {
4211 switch (sh_type)
4212 {
4213 case SHT_PARISC_EXT: return "PARISC_EXT";
4214 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4215 case SHT_PARISC_DOC: return "PARISC_DOC";
4216 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4217 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4218 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4219 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4220 default: return NULL;
4221 }
4222 }
4223
4224 static const char *
4225 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4226 {
4227 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4228 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4229 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4230
4231 switch (sh_type)
4232 {
4233 case SHT_IA_64_EXT: return "IA_64_EXT";
4234 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4235 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4236 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4237 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4238 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4239 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4240 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4241 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4242 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4243 default:
4244 break;
4245 }
4246 return NULL;
4247 }
4248
4249 static const char *
4250 get_x86_64_section_type_name (unsigned int sh_type)
4251 {
4252 switch (sh_type)
4253 {
4254 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4255 default: return NULL;
4256 }
4257 }
4258
4259 static const char *
4260 get_aarch64_section_type_name (unsigned int sh_type)
4261 {
4262 switch (sh_type)
4263 {
4264 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4265 default: return NULL;
4266 }
4267 }
4268
4269 static const char *
4270 get_arm_section_type_name (unsigned int sh_type)
4271 {
4272 switch (sh_type)
4273 {
4274 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4275 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4276 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4277 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4278 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4279 default: return NULL;
4280 }
4281 }
4282
4283 static const char *
4284 get_tic6x_section_type_name (unsigned int sh_type)
4285 {
4286 switch (sh_type)
4287 {
4288 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4289 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4290 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4291 case SHT_TI_ICODE: return "TI_ICODE";
4292 case SHT_TI_XREF: return "TI_XREF";
4293 case SHT_TI_HANDLER: return "TI_HANDLER";
4294 case SHT_TI_INITINFO: return "TI_INITINFO";
4295 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4296 default: return NULL;
4297 }
4298 }
4299
4300 static const char *
4301 get_msp430_section_type_name (unsigned int sh_type)
4302 {
4303 switch (sh_type)
4304 {
4305 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4306 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4307 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4308 default: return NULL;
4309 }
4310 }
4311
4312 static const char *
4313 get_nfp_section_type_name (unsigned int sh_type)
4314 {
4315 switch (sh_type)
4316 {
4317 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4318 case SHT_NFP_INITREG: return "NFP_INITREG";
4319 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4320 default: return NULL;
4321 }
4322 }
4323
4324 static const char *
4325 get_v850_section_type_name (unsigned int sh_type)
4326 {
4327 switch (sh_type)
4328 {
4329 case SHT_V850_SCOMMON: return "V850 Small Common";
4330 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4331 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4332 case SHT_RENESAS_IOP: return "RENESAS IOP";
4333 case SHT_RENESAS_INFO: return "RENESAS INFO";
4334 default: return NULL;
4335 }
4336 }
4337
4338 static const char *
4339 get_riscv_section_type_name (unsigned int sh_type)
4340 {
4341 switch (sh_type)
4342 {
4343 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4344 default: return NULL;
4345 }
4346 }
4347
4348 static const char *
4349 get_csky_section_type_name (unsigned int sh_type)
4350 {
4351 switch (sh_type)
4352 {
4353 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4354 default: return NULL;
4355 }
4356 }
4357
4358 static const char *
4359 get_section_type_name (Filedata * filedata, unsigned int sh_type)
4360 {
4361 static char buff[32];
4362 const char * result;
4363
4364 switch (sh_type)
4365 {
4366 case SHT_NULL: return "NULL";
4367 case SHT_PROGBITS: return "PROGBITS";
4368 case SHT_SYMTAB: return "SYMTAB";
4369 case SHT_STRTAB: return "STRTAB";
4370 case SHT_RELA: return "RELA";
4371 case SHT_HASH: return "HASH";
4372 case SHT_DYNAMIC: return "DYNAMIC";
4373 case SHT_NOTE: return "NOTE";
4374 case SHT_NOBITS: return "NOBITS";
4375 case SHT_REL: return "REL";
4376 case SHT_SHLIB: return "SHLIB";
4377 case SHT_DYNSYM: return "DYNSYM";
4378 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4379 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4380 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4381 case SHT_GNU_HASH: return "GNU_HASH";
4382 case SHT_GROUP: return "GROUP";
4383 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4384 case SHT_GNU_verdef: return "VERDEF";
4385 case SHT_GNU_verneed: return "VERNEED";
4386 case SHT_GNU_versym: return "VERSYM";
4387 case 0x6ffffff0: return "VERSYM";
4388 case 0x6ffffffc: return "VERDEF";
4389 case 0x7ffffffd: return "AUXILIARY";
4390 case 0x7fffffff: return "FILTER";
4391 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4392
4393 default:
4394 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4395 {
4396 switch (filedata->file_header.e_machine)
4397 {
4398 case EM_ARC:
4399 case EM_ARC_COMPACT:
4400 case EM_ARC_COMPACT2:
4401 result = get_arc_section_type_name (sh_type);
4402 break;
4403 case EM_MIPS:
4404 case EM_MIPS_RS3_LE:
4405 result = get_mips_section_type_name (sh_type);
4406 break;
4407 case EM_PARISC:
4408 result = get_parisc_section_type_name (sh_type);
4409 break;
4410 case EM_IA_64:
4411 result = get_ia64_section_type_name (filedata, sh_type);
4412 break;
4413 case EM_X86_64:
4414 case EM_L1OM:
4415 case EM_K1OM:
4416 result = get_x86_64_section_type_name (sh_type);
4417 break;
4418 case EM_AARCH64:
4419 result = get_aarch64_section_type_name (sh_type);
4420 break;
4421 case EM_ARM:
4422 result = get_arm_section_type_name (sh_type);
4423 break;
4424 case EM_TI_C6000:
4425 result = get_tic6x_section_type_name (sh_type);
4426 break;
4427 case EM_MSP430:
4428 result = get_msp430_section_type_name (sh_type);
4429 break;
4430 case EM_NFP:
4431 result = get_nfp_section_type_name (sh_type);
4432 break;
4433 case EM_V800:
4434 case EM_V850:
4435 case EM_CYGNUS_V850:
4436 result = get_v850_section_type_name (sh_type);
4437 break;
4438 case EM_RISCV:
4439 result = get_riscv_section_type_name (sh_type);
4440 break;
4441 case EM_CSKY:
4442 result = get_csky_section_type_name (sh_type);
4443 break;
4444 default:
4445 result = NULL;
4446 break;
4447 }
4448
4449 if (result != NULL)
4450 return result;
4451
4452 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4453 }
4454 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4455 {
4456 switch (filedata->file_header.e_machine)
4457 {
4458 case EM_IA_64:
4459 result = get_ia64_section_type_name (filedata, sh_type);
4460 break;
4461 default:
4462 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4463 result = get_solaris_section_type (sh_type);
4464 else
4465 {
4466 switch (sh_type)
4467 {
4468 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4469 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4470 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4471 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4472 default:
4473 result = NULL;
4474 break;
4475 }
4476 }
4477 break;
4478 }
4479
4480 if (result != NULL)
4481 return result;
4482
4483 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4484 }
4485 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4486 {
4487 switch (filedata->file_header.e_machine)
4488 {
4489 case EM_V800:
4490 case EM_V850:
4491 case EM_CYGNUS_V850:
4492 result = get_v850_section_type_name (sh_type);
4493 break;
4494 default:
4495 result = NULL;
4496 break;
4497 }
4498
4499 if (result != NULL)
4500 return result;
4501
4502 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4503 }
4504 else
4505 /* This message is probably going to be displayed in a 15
4506 character wide field, so put the hex value first. */
4507 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4508
4509 return buff;
4510 }
4511 }
4512
4513 enum long_option_values
4514 {
4515 OPTION_DEBUG_DUMP = 512,
4516 OPTION_DYN_SYMS,
4517 OPTION_LTO_SYMS,
4518 OPTION_DWARF_DEPTH,
4519 OPTION_DWARF_START,
4520 OPTION_DWARF_CHECK,
4521 OPTION_CTF_DUMP,
4522 OPTION_CTF_PARENT,
4523 OPTION_CTF_SYMBOLS,
4524 OPTION_CTF_STRINGS,
4525 OPTION_WITH_SYMBOL_VERSIONS,
4526 OPTION_RECURSE_LIMIT,
4527 OPTION_NO_RECURSE_LIMIT,
4528 OPTION_NO_DEMANGLING
4529 };
4530
4531 static struct option options[] =
4532 {
4533 /* Note - This table is alpha-sorted on the 'val'
4534 field in order to make adding new options easier. */
4535 {"arch-specific", no_argument, 0, 'A'},
4536 {"all", no_argument, 0, 'a'},
4537 {"demangle", optional_argument, 0, 'C'},
4538 {"archive-index", no_argument, 0, 'c'},
4539 {"use-dynamic", no_argument, 0, 'D'},
4540 {"dynamic", no_argument, 0, 'd'},
4541 {"headers", no_argument, 0, 'e'},
4542 {"section-groups", no_argument, 0, 'g'},
4543 {"help", no_argument, 0, 'H'},
4544 {"file-header", no_argument, 0, 'h'},
4545 {"histogram", no_argument, 0, 'I'},
4546 {"lint", no_argument, 0, 'L'},
4547 {"enable-checks", no_argument, 0, 'L'},
4548 {"program-headers", no_argument, 0, 'l'},
4549 {"segments", no_argument, 0, 'l'},
4550 {"full-section-name",no_argument, 0, 'N'},
4551 {"notes", no_argument, 0, 'n'},
4552 {"process-links", no_argument, 0, 'P'},
4553 {"string-dump", required_argument, 0, 'p'},
4554 {"relocated-dump", required_argument, 0, 'R'},
4555 {"relocs", no_argument, 0, 'r'},
4556 {"section-headers", no_argument, 0, 'S'},
4557 {"sections", no_argument, 0, 'S'},
4558 {"symbols", no_argument, 0, 's'},
4559 {"syms", no_argument, 0, 's'},
4560 {"silent-truncation",no_argument, 0, 'T'},
4561 {"section-details", no_argument, 0, 't'},
4562 {"unwind", no_argument, 0, 'u'},
4563 {"version-info", no_argument, 0, 'V'},
4564 {"version", no_argument, 0, 'v'},
4565 {"wide", no_argument, 0, 'W'},
4566 {"hex-dump", required_argument, 0, 'x'},
4567 {"decompress", no_argument, 0, 'z'},
4568
4569 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4570 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4571 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4572 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4573 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
4574 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
4575 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
4576 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4577 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4578 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
4579 #ifdef ENABLE_LIBCTF
4580 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
4581 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4582 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4583 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
4584 #endif
4585
4586 {0, no_argument, 0, 0}
4587 };
4588
4589 static void
4590 usage (FILE * stream)
4591 {
4592 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4593 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4594 fprintf (stream, _(" Options are:\n\
4595 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
4596 -h --file-header Display the ELF file header\n\
4597 -l --program-headers Display the program headers\n\
4598 --segments An alias for --program-headers\n\
4599 -S --section-headers Display the sections' header\n\
4600 --sections An alias for --section-headers\n\
4601 -g --section-groups Display the section groups\n\
4602 -t --section-details Display the section details\n\
4603 -e --headers Equivalent to: -h -l -S\n\
4604 -s --syms Display the symbol table\n\
4605 --symbols An alias for --syms\n\
4606 --dyn-syms Display the dynamic symbol table\n\
4607 --lto-syms Display LTO symbol tables\n\
4608 -C --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
4609 The STYLE, if specified, can be `auto' (the default),\n\
4610 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
4611 or `gnat'\n\
4612 --no-demangle Do not demangle low-level symbol names. (This is the default)\n\
4613 --recurse-limit Enable a demangling recursion limit. (This is the default)\n\
4614 --no-recurse-limit Disable a demangling recursion limit\n\
4615 -n --notes Display the core notes (if present)\n\
4616 -r --relocs Display the relocations (if present)\n\
4617 -u --unwind Display the unwind info (if present)\n\
4618 -d --dynamic Display the dynamic section (if present)\n\
4619 -V --version-info Display the version sections (if present)\n\
4620 -A --arch-specific Display architecture specific information (if any)\n\
4621 -c --archive-index Display the symbol/file index in an archive\n\
4622 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
4623 -L --lint|--enable-checks Display warning messages for possible problems\n\
4624 -x --hex-dump=<number|name>\n\
4625 Dump the contents of section <number|name> as bytes\n\
4626 -p --string-dump=<number|name>\n\
4627 Dump the contents of section <number|name> as strings\n\
4628 -R --relocated-dump=<number|name>\n\
4629 Dump the contents of section <number|name> as relocated bytes\n\
4630 -z --decompress Decompress section before dumping it\n\
4631 -w[lLiaprmfFsoORtUuTgAc] or\n\
4632 --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
4633 =frames-interp,=str,=str-offsets,=loc,=Ranges,=pubtypes,\n\
4634 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
4635 =addr,=cu_index]\n\
4636 Display the contents of DWARF debug sections\n\
4637 -wk,--debug-dump=links Display the contents of sections that link to separate debuginfo files\n\
4638 -P,--process-links Display the contents of non-debug sections in separate debuginfo files. (Implies -wK)\n"));
4639 #if DEFAULT_FOR_FOLLOW_LINKS
4640 fprintf (stream, _("\
4641 -wK,--debug-dump=follow-links Follow links to separate debug info files (default)\n\
4642 -wN,--debug-dump=no-follow-links Do not follow links to separate debug info files\n\
4643 "));
4644 #else
4645 fprintf (stream, _("\
4646 -wK,--debug-dump=follow-links Follow links to separate debug info files\n\
4647 -wN,--debug-dump=no-follow-links Do not follow links to separate debug info files (default)\n\
4648 "));
4649 #endif
4650 fprintf (stream, _("\
4651 --dwarf-depth=N Do not display DIEs at depth N or greater\n\
4652 --dwarf-start=N Display DIEs starting with N, at the same depth\n\
4653 or deeper\n"));
4654 #ifdef ENABLE_LIBCTF
4655 fprintf (stream, _("\
4656 --ctf=<number|name> Display CTF info from section <number|name>\n\
4657 --ctf-parent=<number|name>\n\
4658 Use section <number|name> as the CTF parent\n\n\
4659 --ctf-symbols=<number|name>\n\
4660 Use section <number|name> as the CTF external symtab\n\n\
4661 --ctf-strings=<number|name>\n\
4662 Use section <number|name> as the CTF external strtab\n\n"));
4663 #endif
4664
4665 #ifdef SUPPORT_DISASSEMBLY
4666 fprintf (stream, _("\
4667 -i --instruction-dump=<number|name>\n\
4668 Disassemble the contents of section <number|name>\n"));
4669 #endif
4670 fprintf (stream, _("\
4671 -I --histogram Display histogram of bucket list lengths\n\
4672 -W --wide Allow output width to exceed 80 characters\n\
4673 -T --silent-truncation If a symbol name is truncated, do not add a suffix [...]\n\
4674 @<file> Read options from <file>\n\
4675 -H --help Display this information\n\
4676 -v --version Display the version number of readelf\n"));
4677
4678 if (REPORT_BUGS_TO[0] && stream == stdout)
4679 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4680
4681 exit (stream == stdout ? 0 : 1);
4682 }
4683
4684 /* Record the fact that the user wants the contents of section number
4685 SECTION to be displayed using the method(s) encoded as flags bits
4686 in TYPE. Note, TYPE can be zero if we are creating the array for
4687 the first time. */
4688
4689 static void
4690 request_dump_bynumber (struct dump_data *dumpdata,
4691 unsigned int section, dump_type type)
4692 {
4693 if (section >= dumpdata->num_dump_sects)
4694 {
4695 dump_type * new_dump_sects;
4696
4697 new_dump_sects = (dump_type *) calloc (section + 1,
4698 sizeof (* new_dump_sects));
4699
4700 if (new_dump_sects == NULL)
4701 error (_("Out of memory allocating dump request table.\n"));
4702 else
4703 {
4704 if (dumpdata->dump_sects)
4705 {
4706 /* Copy current flag settings. */
4707 memcpy (new_dump_sects, dumpdata->dump_sects,
4708 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
4709
4710 free (dumpdata->dump_sects);
4711 }
4712
4713 dumpdata->dump_sects = new_dump_sects;
4714 dumpdata->num_dump_sects = section + 1;
4715 }
4716 }
4717
4718 if (dumpdata->dump_sects)
4719 dumpdata->dump_sects[section] |= type;
4720 }
4721
4722 /* Request a dump by section name. */
4723
4724 static void
4725 request_dump_byname (const char * section, dump_type type)
4726 {
4727 struct dump_list_entry * new_request;
4728
4729 new_request = (struct dump_list_entry *)
4730 malloc (sizeof (struct dump_list_entry));
4731 if (!new_request)
4732 error (_("Out of memory allocating dump request table.\n"));
4733
4734 new_request->name = strdup (section);
4735 if (!new_request->name)
4736 error (_("Out of memory allocating dump request table.\n"));
4737
4738 new_request->type = type;
4739
4740 new_request->next = dump_sects_byname;
4741 dump_sects_byname = new_request;
4742 }
4743
4744 static inline void
4745 request_dump (struct dump_data *dumpdata, dump_type type)
4746 {
4747 int section;
4748 char * cp;
4749
4750 do_dump = true;
4751 section = strtoul (optarg, & cp, 0);
4752
4753 if (! *cp && section >= 0)
4754 request_dump_bynumber (dumpdata, section, type);
4755 else
4756 request_dump_byname (optarg, type);
4757 }
4758
4759 static void
4760 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
4761 {
4762 int c;
4763
4764 if (argc < 2)
4765 usage (stderr);
4766
4767 while ((c = getopt_long
4768 (argc, argv, "ACDHILNPR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4769 {
4770 switch (c)
4771 {
4772 case 0:
4773 /* Long options. */
4774 break;
4775 case 'H':
4776 usage (stdout);
4777 break;
4778
4779 case 'a':
4780 do_syms = true;
4781 do_reloc = true;
4782 do_unwind = true;
4783 do_dynamic = true;
4784 do_header = true;
4785 do_sections = true;
4786 do_section_groups = true;
4787 do_segments = true;
4788 do_version = true;
4789 do_histogram = true;
4790 do_arch = true;
4791 do_notes = true;
4792 break;
4793
4794 case 'g':
4795 do_section_groups = true;
4796 break;
4797 case 't':
4798 case 'N':
4799 do_sections = true;
4800 do_section_details = true;
4801 break;
4802 case 'e':
4803 do_header = true;
4804 do_sections = true;
4805 do_segments = true;
4806 break;
4807 case 'A':
4808 do_arch = true;
4809 break;
4810 case 'D':
4811 do_using_dynamic = true;
4812 break;
4813 case 'r':
4814 do_reloc = true;
4815 break;
4816 case 'u':
4817 do_unwind = true;
4818 break;
4819 case 'h':
4820 do_header = true;
4821 break;
4822 case 'l':
4823 do_segments = true;
4824 break;
4825 case 's':
4826 do_syms = true;
4827 break;
4828 case 'S':
4829 do_sections = true;
4830 break;
4831 case 'd':
4832 do_dynamic = true;
4833 break;
4834 case 'I':
4835 do_histogram = true;
4836 break;
4837 case 'n':
4838 do_notes = true;
4839 break;
4840 case 'c':
4841 do_archive_index = true;
4842 break;
4843 case 'L':
4844 do_checks = true;
4845 break;
4846 case 'P':
4847 process_links = true;
4848 do_follow_links = true;
4849 break;
4850 case 'x':
4851 request_dump (dumpdata, HEX_DUMP);
4852 break;
4853 case 'p':
4854 request_dump (dumpdata, STRING_DUMP);
4855 break;
4856 case 'R':
4857 request_dump (dumpdata, RELOC_DUMP);
4858 break;
4859 case 'z':
4860 decompress_dumps = true;
4861 break;
4862 case 'w':
4863 do_dump = true;
4864 if (optarg == NULL)
4865 {
4866 do_debugging = true;
4867 dwarf_select_sections_all ();
4868 }
4869 else
4870 {
4871 do_debugging = false;
4872 dwarf_select_sections_by_letters (optarg);
4873 }
4874 break;
4875 case OPTION_DEBUG_DUMP:
4876 do_dump = true;
4877 if (optarg == NULL)
4878 do_debugging = true;
4879 else
4880 {
4881 do_debugging = false;
4882 dwarf_select_sections_by_names (optarg);
4883 }
4884 break;
4885 case OPTION_DWARF_DEPTH:
4886 {
4887 char *cp;
4888
4889 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4890 }
4891 break;
4892 case OPTION_DWARF_START:
4893 {
4894 char *cp;
4895
4896 dwarf_start_die = strtoul (optarg, & cp, 0);
4897 }
4898 break;
4899 case OPTION_DWARF_CHECK:
4900 dwarf_check = true;
4901 break;
4902 case OPTION_CTF_DUMP:
4903 do_ctf = true;
4904 request_dump (dumpdata, CTF_DUMP);
4905 break;
4906 case OPTION_CTF_SYMBOLS:
4907 free (dump_ctf_symtab_name);
4908 dump_ctf_symtab_name = strdup (optarg);
4909 break;
4910 case OPTION_CTF_STRINGS:
4911 free (dump_ctf_strtab_name);
4912 dump_ctf_strtab_name = strdup (optarg);
4913 break;
4914 case OPTION_CTF_PARENT:
4915 free (dump_ctf_parent_name);
4916 dump_ctf_parent_name = strdup (optarg);
4917 break;
4918 case OPTION_DYN_SYMS:
4919 do_dyn_syms = true;
4920 break;
4921 case OPTION_LTO_SYMS:
4922 do_lto_syms = true;
4923 break;
4924 #ifdef SUPPORT_DISASSEMBLY
4925 case 'i':
4926 request_dump (dumpdata, DISASS_DUMP);
4927 break;
4928 #endif
4929 case 'v':
4930 print_version (program_name);
4931 break;
4932 case 'V':
4933 do_version = true;
4934 break;
4935 case 'W':
4936 do_wide = true;
4937 break;
4938 case 'T':
4939 do_not_show_symbol_truncation = true;
4940 break;
4941 case 'C':
4942 do_demangle = true;
4943 if (optarg != NULL)
4944 {
4945 enum demangling_styles style;
4946
4947 style = cplus_demangle_name_to_style (optarg);
4948 if (style == unknown_demangling)
4949 error (_("unknown demangling style `%s'"), optarg);
4950
4951 cplus_demangle_set_style (style);
4952 }
4953 break;
4954 case OPTION_NO_DEMANGLING:
4955 do_demangle = false;
4956 break;
4957 case OPTION_RECURSE_LIMIT:
4958 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
4959 break;
4960 case OPTION_NO_RECURSE_LIMIT:
4961 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
4962 break;
4963 case OPTION_WITH_SYMBOL_VERSIONS:
4964 /* Ignored for backward compatibility. */
4965 break;
4966
4967 default:
4968 /* xgettext:c-format */
4969 error (_("Invalid option '-%c'\n"), c);
4970 /* Fall through. */
4971 case '?':
4972 usage (stderr);
4973 }
4974 }
4975
4976 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
4977 && !do_segments && !do_header && !do_dump && !do_version
4978 && !do_histogram && !do_debugging && !do_arch && !do_notes
4979 && !do_section_groups && !do_archive_index
4980 && !do_dyn_syms && !do_lto_syms)
4981 {
4982 if (do_checks)
4983 {
4984 check_all = true;
4985 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
4986 do_segments = do_header = do_dump = do_version = true;
4987 do_histogram = do_debugging = do_arch = do_notes = true;
4988 do_section_groups = do_archive_index = do_dyn_syms = true;
4989 do_lto_syms = true;
4990 }
4991 else
4992 usage (stderr);
4993 }
4994 }
4995
4996 static const char *
4997 get_elf_class (unsigned int elf_class)
4998 {
4999 static char buff[32];
5000
5001 switch (elf_class)
5002 {
5003 case ELFCLASSNONE: return _("none");
5004 case ELFCLASS32: return "ELF32";
5005 case ELFCLASS64: return "ELF64";
5006 default:
5007 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5008 return buff;
5009 }
5010 }
5011
5012 static const char *
5013 get_data_encoding (unsigned int encoding)
5014 {
5015 static char buff[32];
5016
5017 switch (encoding)
5018 {
5019 case ELFDATANONE: return _("none");
5020 case ELFDATA2LSB: return _("2's complement, little endian");
5021 case ELFDATA2MSB: return _("2's complement, big endian");
5022 default:
5023 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5024 return buff;
5025 }
5026 }
5027
5028 /* Decode the data held in 'filedata->file_header'. */
5029
5030 static bool
5031 process_file_header (Filedata * filedata)
5032 {
5033 Elf_Internal_Ehdr * header = & filedata->file_header;
5034
5035 if ( header->e_ident[EI_MAG0] != ELFMAG0
5036 || header->e_ident[EI_MAG1] != ELFMAG1
5037 || header->e_ident[EI_MAG2] != ELFMAG2
5038 || header->e_ident[EI_MAG3] != ELFMAG3)
5039 {
5040 error
5041 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5042 return false;
5043 }
5044
5045 if (! filedata->is_separate)
5046 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5047
5048 if (do_header)
5049 {
5050 unsigned i;
5051
5052 if (filedata->is_separate)
5053 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5054 else
5055 printf (_("ELF Header:\n"));
5056 printf (_(" Magic: "));
5057 for (i = 0; i < EI_NIDENT; i++)
5058 printf ("%2.2x ", header->e_ident[i]);
5059 printf ("\n");
5060 printf (_(" Class: %s\n"),
5061 get_elf_class (header->e_ident[EI_CLASS]));
5062 printf (_(" Data: %s\n"),
5063 get_data_encoding (header->e_ident[EI_DATA]));
5064 printf (_(" Version: %d%s\n"),
5065 header->e_ident[EI_VERSION],
5066 (header->e_ident[EI_VERSION] == EV_CURRENT
5067 ? _(" (current)")
5068 : (header->e_ident[EI_VERSION] != EV_NONE
5069 ? _(" <unknown>")
5070 : "")));
5071 printf (_(" OS/ABI: %s\n"),
5072 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5073 printf (_(" ABI Version: %d\n"),
5074 header->e_ident[EI_ABIVERSION]);
5075 printf (_(" Type: %s\n"),
5076 get_file_type (header->e_type));
5077 printf (_(" Machine: %s\n"),
5078 get_machine_name (header->e_machine));
5079 printf (_(" Version: 0x%lx\n"),
5080 header->e_version);
5081
5082 printf (_(" Entry point address: "));
5083 print_vma (header->e_entry, PREFIX_HEX);
5084 printf (_("\n Start of program headers: "));
5085 print_vma (header->e_phoff, DEC);
5086 printf (_(" (bytes into file)\n Start of section headers: "));
5087 print_vma (header->e_shoff, DEC);
5088 printf (_(" (bytes into file)\n"));
5089
5090 printf (_(" Flags: 0x%lx%s\n"),
5091 header->e_flags,
5092 get_machine_flags (filedata, header->e_flags, header->e_machine));
5093 printf (_(" Size of this header: %u (bytes)\n"),
5094 header->e_ehsize);
5095 printf (_(" Size of program headers: %u (bytes)\n"),
5096 header->e_phentsize);
5097 printf (_(" Number of program headers: %u"),
5098 header->e_phnum);
5099 if (filedata->section_headers != NULL
5100 && header->e_phnum == PN_XNUM
5101 && filedata->section_headers[0].sh_info != 0)
5102 {
5103 header->e_phnum = filedata->section_headers[0].sh_info;
5104 printf (" (%u)", header->e_phnum);
5105 }
5106 putc ('\n', stdout);
5107 printf (_(" Size of section headers: %u (bytes)\n"),
5108 header->e_shentsize);
5109 printf (_(" Number of section headers: %u"),
5110 header->e_shnum);
5111 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5112 {
5113 header->e_shnum = filedata->section_headers[0].sh_size;
5114 printf (" (%u)", header->e_shnum);
5115 }
5116 putc ('\n', stdout);
5117 printf (_(" Section header string table index: %u"),
5118 header->e_shstrndx);
5119 if (filedata->section_headers != NULL
5120 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5121 {
5122 header->e_shstrndx = filedata->section_headers[0].sh_link;
5123 printf (" (%u)", header->e_shstrndx);
5124 }
5125 if (header->e_shstrndx != SHN_UNDEF
5126 && header->e_shstrndx >= header->e_shnum)
5127 {
5128 header->e_shstrndx = SHN_UNDEF;
5129 printf (_(" <corrupt: out of range>"));
5130 }
5131 putc ('\n', stdout);
5132 }
5133
5134 if (filedata->section_headers != NULL)
5135 {
5136 if (header->e_phnum == PN_XNUM
5137 && filedata->section_headers[0].sh_info != 0)
5138 header->e_phnum = filedata->section_headers[0].sh_info;
5139 if (header->e_shnum == SHN_UNDEF)
5140 header->e_shnum = filedata->section_headers[0].sh_size;
5141 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5142 header->e_shstrndx = filedata->section_headers[0].sh_link;
5143 if (header->e_shstrndx >= header->e_shnum)
5144 header->e_shstrndx = SHN_UNDEF;
5145 free (filedata->section_headers);
5146 filedata->section_headers = NULL;
5147 }
5148
5149 return true;
5150 }
5151
5152 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5153 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5154
5155 static bool
5156 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5157 {
5158 Elf32_External_Phdr * phdrs;
5159 Elf32_External_Phdr * external;
5160 Elf_Internal_Phdr * internal;
5161 unsigned int i;
5162 unsigned int size = filedata->file_header.e_phentsize;
5163 unsigned int num = filedata->file_header.e_phnum;
5164
5165 /* PR binutils/17531: Cope with unexpected section header sizes. */
5166 if (size == 0 || num == 0)
5167 return false;
5168 if (size < sizeof * phdrs)
5169 {
5170 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5171 return false;
5172 }
5173 if (size > sizeof * phdrs)
5174 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5175
5176 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5177 size, num, _("program headers"));
5178 if (phdrs == NULL)
5179 return false;
5180
5181 for (i = 0, internal = pheaders, external = phdrs;
5182 i < filedata->file_header.e_phnum;
5183 i++, internal++, external++)
5184 {
5185 internal->p_type = BYTE_GET (external->p_type);
5186 internal->p_offset = BYTE_GET (external->p_offset);
5187 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5188 internal->p_paddr = BYTE_GET (external->p_paddr);
5189 internal->p_filesz = BYTE_GET (external->p_filesz);
5190 internal->p_memsz = BYTE_GET (external->p_memsz);
5191 internal->p_flags = BYTE_GET (external->p_flags);
5192 internal->p_align = BYTE_GET (external->p_align);
5193 }
5194
5195 free (phdrs);
5196 return true;
5197 }
5198
5199 /* Read in the program headers from FILEDATA and store them in PHEADERS.
5200 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5201
5202 static bool
5203 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5204 {
5205 Elf64_External_Phdr * phdrs;
5206 Elf64_External_Phdr * external;
5207 Elf_Internal_Phdr * internal;
5208 unsigned int i;
5209 unsigned int size = filedata->file_header.e_phentsize;
5210 unsigned int num = filedata->file_header.e_phnum;
5211
5212 /* PR binutils/17531: Cope with unexpected section header sizes. */
5213 if (size == 0 || num == 0)
5214 return false;
5215 if (size < sizeof * phdrs)
5216 {
5217 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5218 return false;
5219 }
5220 if (size > sizeof * phdrs)
5221 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5222
5223 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5224 size, num, _("program headers"));
5225 if (!phdrs)
5226 return false;
5227
5228 for (i = 0, internal = pheaders, external = phdrs;
5229 i < filedata->file_header.e_phnum;
5230 i++, internal++, external++)
5231 {
5232 internal->p_type = BYTE_GET (external->p_type);
5233 internal->p_flags = BYTE_GET (external->p_flags);
5234 internal->p_offset = BYTE_GET (external->p_offset);
5235 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5236 internal->p_paddr = BYTE_GET (external->p_paddr);
5237 internal->p_filesz = BYTE_GET (external->p_filesz);
5238 internal->p_memsz = BYTE_GET (external->p_memsz);
5239 internal->p_align = BYTE_GET (external->p_align);
5240 }
5241
5242 free (phdrs);
5243 return true;
5244 }
5245
5246 /* Returns TRUE if the program headers were read into `program_headers'. */
5247
5248 static bool
5249 get_program_headers (Filedata * filedata)
5250 {
5251 Elf_Internal_Phdr * phdrs;
5252
5253 /* Check cache of prior read. */
5254 if (filedata->program_headers != NULL)
5255 return true;
5256
5257 /* Be kind to memory checkers by looking for
5258 e_phnum values which we know must be invalid. */
5259 if (filedata->file_header.e_phnum
5260 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5261 >= filedata->file_size)
5262 {
5263 error (_("Too many program headers - %#x - the file is not that big\n"),
5264 filedata->file_header.e_phnum);
5265 return false;
5266 }
5267
5268 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5269 sizeof (Elf_Internal_Phdr));
5270 if (phdrs == NULL)
5271 {
5272 error (_("Out of memory reading %u program headers\n"),
5273 filedata->file_header.e_phnum);
5274 return false;
5275 }
5276
5277 if (is_32bit_elf
5278 ? get_32bit_program_headers (filedata, phdrs)
5279 : get_64bit_program_headers (filedata, phdrs))
5280 {
5281 filedata->program_headers = phdrs;
5282 return true;
5283 }
5284
5285 free (phdrs);
5286 return false;
5287 }
5288
5289 /* Returns TRUE if the program headers were loaded. */
5290
5291 static bool
5292 process_program_headers (Filedata * filedata)
5293 {
5294 Elf_Internal_Phdr * segment;
5295 unsigned int i;
5296 Elf_Internal_Phdr * previous_load = NULL;
5297
5298 filedata->dynamic_addr = 0;
5299 filedata->dynamic_size = 0;
5300
5301 if (filedata->file_header.e_phnum == 0)
5302 {
5303 /* PR binutils/12467. */
5304 if (filedata->file_header.e_phoff != 0)
5305 {
5306 warn (_("possibly corrupt ELF header - it has a non-zero program"
5307 " header offset, but no program headers\n"));
5308 return false;
5309 }
5310 else if (do_segments)
5311 {
5312 if (filedata->is_separate)
5313 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5314 filedata->file_name);
5315 else
5316 printf (_("\nThere are no program headers in this file.\n"));
5317 }
5318 return true;
5319 }
5320
5321 if (do_segments && !do_header)
5322 {
5323 if (filedata->is_separate)
5324 printf ("\nIn linked file '%s' the ELF file type is %s\n",
5325 filedata->file_name,
5326 get_file_type (filedata->file_header.e_type));
5327 else
5328 printf (_("\nElf file type is %s\n"), get_file_type (filedata->file_header.e_type));
5329 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5330 printf (ngettext ("There is %d program header, starting at offset %s\n",
5331 "There are %d program headers, starting at offset %s\n",
5332 filedata->file_header.e_phnum),
5333 filedata->file_header.e_phnum,
5334 bfd_vmatoa ("u", filedata->file_header.e_phoff));
5335 }
5336
5337 if (! get_program_headers (filedata))
5338 return true;
5339
5340 if (do_segments)
5341 {
5342 if (filedata->file_header.e_phnum > 1)
5343 printf (_("\nProgram Headers:\n"));
5344 else
5345 printf (_("\nProgram Headers:\n"));
5346
5347 if (is_32bit_elf)
5348 printf
5349 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5350 else if (do_wide)
5351 printf
5352 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5353 else
5354 {
5355 printf
5356 (_(" Type Offset VirtAddr PhysAddr\n"));
5357 printf
5358 (_(" FileSiz MemSiz Flags Align\n"));
5359 }
5360 }
5361
5362 for (i = 0, segment = filedata->program_headers;
5363 i < filedata->file_header.e_phnum;
5364 i++, segment++)
5365 {
5366 if (do_segments)
5367 {
5368 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
5369
5370 if (is_32bit_elf)
5371 {
5372 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5373 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5374 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5375 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5376 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5377 printf ("%c%c%c ",
5378 (segment->p_flags & PF_R ? 'R' : ' '),
5379 (segment->p_flags & PF_W ? 'W' : ' '),
5380 (segment->p_flags & PF_X ? 'E' : ' '));
5381 printf ("%#lx", (unsigned long) segment->p_align);
5382 }
5383 else if (do_wide)
5384 {
5385 if ((unsigned long) segment->p_offset == segment->p_offset)
5386 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5387 else
5388 {
5389 print_vma (segment->p_offset, FULL_HEX);
5390 putchar (' ');
5391 }
5392
5393 print_vma (segment->p_vaddr, FULL_HEX);
5394 putchar (' ');
5395 print_vma (segment->p_paddr, FULL_HEX);
5396 putchar (' ');
5397
5398 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5399 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5400 else
5401 {
5402 print_vma (segment->p_filesz, FULL_HEX);
5403 putchar (' ');
5404 }
5405
5406 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5407 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5408 else
5409 {
5410 print_vma (segment->p_memsz, FULL_HEX);
5411 }
5412
5413 printf (" %c%c%c ",
5414 (segment->p_flags & PF_R ? 'R' : ' '),
5415 (segment->p_flags & PF_W ? 'W' : ' '),
5416 (segment->p_flags & PF_X ? 'E' : ' '));
5417
5418 if ((unsigned long) segment->p_align == segment->p_align)
5419 printf ("%#lx", (unsigned long) segment->p_align);
5420 else
5421 {
5422 print_vma (segment->p_align, PREFIX_HEX);
5423 }
5424 }
5425 else
5426 {
5427 print_vma (segment->p_offset, FULL_HEX);
5428 putchar (' ');
5429 print_vma (segment->p_vaddr, FULL_HEX);
5430 putchar (' ');
5431 print_vma (segment->p_paddr, FULL_HEX);
5432 printf ("\n ");
5433 print_vma (segment->p_filesz, FULL_HEX);
5434 putchar (' ');
5435 print_vma (segment->p_memsz, FULL_HEX);
5436 printf (" %c%c%c ",
5437 (segment->p_flags & PF_R ? 'R' : ' '),
5438 (segment->p_flags & PF_W ? 'W' : ' '),
5439 (segment->p_flags & PF_X ? 'E' : ' '));
5440 print_vma (segment->p_align, PREFIX_HEX);
5441 }
5442
5443 putc ('\n', stdout);
5444 }
5445
5446 switch (segment->p_type)
5447 {
5448 case PT_LOAD:
5449 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5450 required by the ELF standard, several programs, including the Linux
5451 kernel, make use of non-ordered segments. */
5452 if (previous_load
5453 && previous_load->p_vaddr > segment->p_vaddr)
5454 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5455 #endif
5456 if (segment->p_memsz < segment->p_filesz)
5457 error (_("the segment's file size is larger than its memory size\n"));
5458 previous_load = segment;
5459 break;
5460
5461 case PT_PHDR:
5462 /* PR 20815 - Verify that the program header is loaded into memory. */
5463 if (i > 0 && previous_load != NULL)
5464 error (_("the PHDR segment must occur before any LOAD segment\n"));
5465 if (filedata->file_header.e_machine != EM_PARISC)
5466 {
5467 unsigned int j;
5468
5469 for (j = 1; j < filedata->file_header.e_phnum; j++)
5470 {
5471 Elf_Internal_Phdr *load = filedata->program_headers + j;
5472 if (load->p_type == PT_LOAD
5473 && load->p_offset <= segment->p_offset
5474 && (load->p_offset + load->p_filesz
5475 >= segment->p_offset + segment->p_filesz)
5476 && load->p_vaddr <= segment->p_vaddr
5477 && (load->p_vaddr + load->p_filesz
5478 >= segment->p_vaddr + segment->p_filesz))
5479 break;
5480 }
5481 if (j == filedata->file_header.e_phnum)
5482 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5483 }
5484 break;
5485
5486 case PT_DYNAMIC:
5487 if (filedata->dynamic_addr)
5488 error (_("more than one dynamic segment\n"));
5489
5490 /* By default, assume that the .dynamic section is the first
5491 section in the DYNAMIC segment. */
5492 filedata->dynamic_addr = segment->p_offset;
5493 filedata->dynamic_size = segment->p_filesz;
5494
5495 /* Try to locate the .dynamic section. If there is
5496 a section header table, we can easily locate it. */
5497 if (filedata->section_headers != NULL)
5498 {
5499 Elf_Internal_Shdr * sec;
5500
5501 sec = find_section (filedata, ".dynamic");
5502 if (sec == NULL || sec->sh_size == 0)
5503 {
5504 /* A corresponding .dynamic section is expected, but on
5505 IA-64/OpenVMS it is OK for it to be missing. */
5506 if (!is_ia64_vms (filedata))
5507 error (_("no .dynamic section in the dynamic segment\n"));
5508 break;
5509 }
5510
5511 if (sec->sh_type == SHT_NOBITS)
5512 {
5513 filedata->dynamic_size = 0;
5514 break;
5515 }
5516
5517 filedata->dynamic_addr = sec->sh_offset;
5518 filedata->dynamic_size = sec->sh_size;
5519
5520 /* The PT_DYNAMIC segment, which is used by the run-time
5521 loader, should exactly match the .dynamic section. */
5522 if (do_checks
5523 && (filedata->dynamic_addr != segment->p_offset
5524 || filedata->dynamic_size != segment->p_filesz))
5525 warn (_("\
5526 the .dynamic section is not the same as the dynamic segment\n"));
5527 }
5528
5529 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5530 segment. Check this after matching against the section headers
5531 so we don't warn on debuginfo file (which have NOBITS .dynamic
5532 sections). */
5533 if (filedata->dynamic_addr > filedata->file_size
5534 || (filedata->dynamic_size
5535 > filedata->file_size - filedata->dynamic_addr))
5536 {
5537 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5538 filedata->dynamic_addr = filedata->dynamic_size = 0;
5539 }
5540 break;
5541
5542 case PT_INTERP:
5543 if (fseek (filedata->handle,
5544 filedata->archive_file_offset + (long) segment->p_offset,
5545 SEEK_SET))
5546 error (_("Unable to find program interpreter name\n"));
5547 else
5548 {
5549 char fmt [32];
5550 int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
5551
5552 if (ret >= (int) sizeof (fmt) || ret < 0)
5553 error (_("Internal error: failed to create format string to display program interpreter\n"));
5554
5555 filedata->program_interpreter[0] = 0;
5556 if (fscanf (filedata->handle, fmt,
5557 filedata->program_interpreter) <= 0)
5558 error (_("Unable to read program interpreter name\n"));
5559
5560 if (do_segments)
5561 printf (_(" [Requesting program interpreter: %s]\n"),
5562 filedata->program_interpreter);
5563 }
5564 break;
5565 }
5566 }
5567
5568 if (do_segments
5569 && filedata->section_headers != NULL
5570 && filedata->string_table != NULL)
5571 {
5572 printf (_("\n Section to Segment mapping:\n"));
5573 printf (_(" Segment Sections...\n"));
5574
5575 for (i = 0; i < filedata->file_header.e_phnum; i++)
5576 {
5577 unsigned int j;
5578 Elf_Internal_Shdr * section;
5579
5580 segment = filedata->program_headers + i;
5581 section = filedata->section_headers + 1;
5582
5583 printf (" %2.2d ", i);
5584
5585 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5586 {
5587 if (!ELF_TBSS_SPECIAL (section, segment)
5588 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5589 printf ("%s ", printable_section_name (filedata, section));
5590 }
5591
5592 putc ('\n',stdout);
5593 }
5594 }
5595
5596 return true;
5597 }
5598
5599
5600 /* Find the file offset corresponding to VMA by using the program headers. */
5601
5602 static long
5603 offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5604 {
5605 Elf_Internal_Phdr * seg;
5606
5607 if (! get_program_headers (filedata))
5608 {
5609 warn (_("Cannot interpret virtual addresses without program headers.\n"));
5610 return (long) vma;
5611 }
5612
5613 for (seg = filedata->program_headers;
5614 seg < filedata->program_headers + filedata->file_header.e_phnum;
5615 ++seg)
5616 {
5617 if (seg->p_type != PT_LOAD)
5618 continue;
5619
5620 if (vma >= (seg->p_vaddr & -seg->p_align)
5621 && vma + size <= seg->p_vaddr + seg->p_filesz)
5622 return vma - seg->p_vaddr + seg->p_offset;
5623 }
5624
5625 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5626 (unsigned long) vma);
5627 return (long) vma;
5628 }
5629
5630
5631 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5632 If PROBE is true, this is just a probe and we do not generate any error
5633 messages if the load fails. */
5634
5635 static bool
5636 get_32bit_section_headers (Filedata * filedata, bool probe)
5637 {
5638 Elf32_External_Shdr * shdrs;
5639 Elf_Internal_Shdr * internal;
5640 unsigned int i;
5641 unsigned int size = filedata->file_header.e_shentsize;
5642 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5643
5644 /* PR binutils/17531: Cope with unexpected section header sizes. */
5645 if (size == 0 || num == 0)
5646 return false;
5647 if (size < sizeof * shdrs)
5648 {
5649 if (! probe)
5650 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5651 return false;
5652 }
5653 if (!probe && size > sizeof * shdrs)
5654 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5655
5656 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5657 size, num,
5658 probe ? NULL : _("section headers"));
5659 if (shdrs == NULL)
5660 return false;
5661
5662 free (filedata->section_headers);
5663 filedata->section_headers = (Elf_Internal_Shdr *)
5664 cmalloc (num, sizeof (Elf_Internal_Shdr));
5665 if (filedata->section_headers == NULL)
5666 {
5667 if (!probe)
5668 error (_("Out of memory reading %u section headers\n"), num);
5669 free (shdrs);
5670 return false;
5671 }
5672
5673 for (i = 0, internal = filedata->section_headers;
5674 i < num;
5675 i++, internal++)
5676 {
5677 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5678 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5679 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5680 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5681 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5682 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5683 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5684 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5685 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5686 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5687 if (!probe && internal->sh_link > num)
5688 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5689 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5690 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5691 }
5692
5693 free (shdrs);
5694 return true;
5695 }
5696
5697 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
5698
5699 static bool
5700 get_64bit_section_headers (Filedata * filedata, bool probe)
5701 {
5702 Elf64_External_Shdr * shdrs;
5703 Elf_Internal_Shdr * internal;
5704 unsigned int i;
5705 unsigned int size = filedata->file_header.e_shentsize;
5706 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5707
5708 /* PR binutils/17531: Cope with unexpected section header sizes. */
5709 if (size == 0 || num == 0)
5710 return false;
5711
5712 if (size < sizeof * shdrs)
5713 {
5714 if (! probe)
5715 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5716 return false;
5717 }
5718
5719 if (! probe && size > sizeof * shdrs)
5720 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5721
5722 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5723 filedata->file_header.e_shoff,
5724 size, num,
5725 probe ? NULL : _("section headers"));
5726 if (shdrs == NULL)
5727 return false;
5728
5729 free (filedata->section_headers);
5730 filedata->section_headers = (Elf_Internal_Shdr *)
5731 cmalloc (num, sizeof (Elf_Internal_Shdr));
5732 if (filedata->section_headers == NULL)
5733 {
5734 if (! probe)
5735 error (_("Out of memory reading %u section headers\n"), num);
5736 free (shdrs);
5737 return false;
5738 }
5739
5740 for (i = 0, internal = filedata->section_headers;
5741 i < num;
5742 i++, internal++)
5743 {
5744 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5745 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5746 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5747 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5748 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5749 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5750 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5751 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5752 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5753 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5754 if (!probe && internal->sh_link > num)
5755 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5756 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5757 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5758 }
5759
5760 free (shdrs);
5761 return true;
5762 }
5763
5764 static Elf_Internal_Sym *
5765 get_32bit_elf_symbols (Filedata * filedata,
5766 Elf_Internal_Shdr * section,
5767 unsigned long * num_syms_return)
5768 {
5769 unsigned long number = 0;
5770 Elf32_External_Sym * esyms = NULL;
5771 Elf_External_Sym_Shndx * shndx = NULL;
5772 Elf_Internal_Sym * isyms = NULL;
5773 Elf_Internal_Sym * psym;
5774 unsigned int j;
5775 elf_section_list * entry;
5776
5777 if (section->sh_size == 0)
5778 {
5779 if (num_syms_return != NULL)
5780 * num_syms_return = 0;
5781 return NULL;
5782 }
5783
5784 /* Run some sanity checks first. */
5785 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5786 {
5787 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5788 printable_section_name (filedata, section),
5789 (unsigned long) section->sh_entsize);
5790 goto exit_point;
5791 }
5792
5793 if (section->sh_size > filedata->file_size)
5794 {
5795 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5796 printable_section_name (filedata, section),
5797 (unsigned long) section->sh_size);
5798 goto exit_point;
5799 }
5800
5801 number = section->sh_size / section->sh_entsize;
5802
5803 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5804 {
5805 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5806 (unsigned long) section->sh_size,
5807 printable_section_name (filedata, section),
5808 (unsigned long) section->sh_entsize);
5809 goto exit_point;
5810 }
5811
5812 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5813 section->sh_size, _("symbols"));
5814 if (esyms == NULL)
5815 goto exit_point;
5816
5817 shndx = NULL;
5818 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
5819 {
5820 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5821 continue;
5822
5823 if (shndx != NULL)
5824 {
5825 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5826 free (shndx);
5827 }
5828
5829 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5830 entry->hdr->sh_offset,
5831 1, entry->hdr->sh_size,
5832 _("symbol table section indices"));
5833 if (shndx == NULL)
5834 goto exit_point;
5835
5836 /* PR17531: file: heap-buffer-overflow */
5837 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5838 {
5839 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5840 printable_section_name (filedata, entry->hdr),
5841 (unsigned long) entry->hdr->sh_size,
5842 (unsigned long) section->sh_size);
5843 goto exit_point;
5844 }
5845 }
5846
5847 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5848
5849 if (isyms == NULL)
5850 {
5851 error (_("Out of memory reading %lu symbols\n"),
5852 (unsigned long) number);
5853 goto exit_point;
5854 }
5855
5856 for (j = 0, psym = isyms; j < number; j++, psym++)
5857 {
5858 psym->st_name = BYTE_GET (esyms[j].st_name);
5859 psym->st_value = BYTE_GET (esyms[j].st_value);
5860 psym->st_size = BYTE_GET (esyms[j].st_size);
5861 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5862 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5863 psym->st_shndx
5864 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5865 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5866 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5867 psym->st_info = BYTE_GET (esyms[j].st_info);
5868 psym->st_other = BYTE_GET (esyms[j].st_other);
5869 }
5870
5871 exit_point:
5872 free (shndx);
5873 free (esyms);
5874
5875 if (num_syms_return != NULL)
5876 * num_syms_return = isyms == NULL ? 0 : number;
5877
5878 return isyms;
5879 }
5880
5881 static Elf_Internal_Sym *
5882 get_64bit_elf_symbols (Filedata * filedata,
5883 Elf_Internal_Shdr * section,
5884 unsigned long * num_syms_return)
5885 {
5886 unsigned long number = 0;
5887 Elf64_External_Sym * esyms = NULL;
5888 Elf_External_Sym_Shndx * shndx = NULL;
5889 Elf_Internal_Sym * isyms = NULL;
5890 Elf_Internal_Sym * psym;
5891 unsigned int j;
5892 elf_section_list * entry;
5893
5894 if (section->sh_size == 0)
5895 {
5896 if (num_syms_return != NULL)
5897 * num_syms_return = 0;
5898 return NULL;
5899 }
5900
5901 /* Run some sanity checks first. */
5902 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
5903 {
5904 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
5905 printable_section_name (filedata, section),
5906 (unsigned long) section->sh_entsize);
5907 goto exit_point;
5908 }
5909
5910 if (section->sh_size > filedata->file_size)
5911 {
5912 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
5913 printable_section_name (filedata, section),
5914 (unsigned long) section->sh_size);
5915 goto exit_point;
5916 }
5917
5918 number = section->sh_size / section->sh_entsize;
5919
5920 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
5921 {
5922 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
5923 (unsigned long) section->sh_size,
5924 printable_section_name (filedata, section),
5925 (unsigned long) section->sh_entsize);
5926 goto exit_point;
5927 }
5928
5929 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
5930 section->sh_size, _("symbols"));
5931 if (!esyms)
5932 goto exit_point;
5933
5934 shndx = NULL;
5935 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
5936 {
5937 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5938 continue;
5939
5940 if (shndx != NULL)
5941 {
5942 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5943 free (shndx);
5944 }
5945
5946 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5947 entry->hdr->sh_offset,
5948 1, entry->hdr->sh_size,
5949 _("symbol table section indices"));
5950 if (shndx == NULL)
5951 goto exit_point;
5952
5953 /* PR17531: file: heap-buffer-overflow */
5954 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5955 {
5956 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5957 printable_section_name (filedata, entry->hdr),
5958 (unsigned long) entry->hdr->sh_size,
5959 (unsigned long) section->sh_size);
5960 goto exit_point;
5961 }
5962 }
5963
5964 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
5965
5966 if (isyms == NULL)
5967 {
5968 error (_("Out of memory reading %lu symbols\n"),
5969 (unsigned long) number);
5970 goto exit_point;
5971 }
5972
5973 for (j = 0, psym = isyms; j < number; j++, psym++)
5974 {
5975 psym->st_name = BYTE_GET (esyms[j].st_name);
5976 psym->st_info = BYTE_GET (esyms[j].st_info);
5977 psym->st_other = BYTE_GET (esyms[j].st_other);
5978 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
5979
5980 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
5981 psym->st_shndx
5982 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
5983 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5984 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
5985
5986 psym->st_value = BYTE_GET (esyms[j].st_value);
5987 psym->st_size = BYTE_GET (esyms[j].st_size);
5988 }
5989
5990 exit_point:
5991 free (shndx);
5992 free (esyms);
5993
5994 if (num_syms_return != NULL)
5995 * num_syms_return = isyms == NULL ? 0 : number;
5996
5997 return isyms;
5998 }
5999
6000 static const char *
6001 get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6002 {
6003 static char buff[1024];
6004 char * p = buff;
6005 unsigned int field_size = is_32bit_elf ? 8 : 16;
6006 signed int sindex;
6007 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6008 bfd_vma os_flags = 0;
6009 bfd_vma proc_flags = 0;
6010 bfd_vma unknown_flags = 0;
6011 static const struct
6012 {
6013 const char * str;
6014 unsigned int len;
6015 }
6016 flags [] =
6017 {
6018 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6019 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6020 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6021 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6022 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6023 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6024 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6025 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6026 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6027 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6028 /* IA-64 specific. */
6029 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6030 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6031 /* IA-64 OpenVMS specific. */
6032 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6033 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6034 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6035 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6036 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6037 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6038 /* Generic. */
6039 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6040 /* SPARC specific. */
6041 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6042 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6043 /* ARM specific. */
6044 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6045 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6046 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6047 /* GNU specific. */
6048 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6049 /* VLE specific. */
6050 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6051 /* GNU specific. */
6052 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6053 };
6054
6055 if (do_section_details)
6056 {
6057 sprintf (buff, "[%*.*lx]: ",
6058 field_size, field_size, (unsigned long) sh_flags);
6059 p += field_size + 4;
6060 }
6061
6062 while (sh_flags)
6063 {
6064 bfd_vma flag;
6065
6066 flag = sh_flags & - sh_flags;
6067 sh_flags &= ~ flag;
6068
6069 if (do_section_details)
6070 {
6071 switch (flag)
6072 {
6073 case SHF_WRITE: sindex = 0; break;
6074 case SHF_ALLOC: sindex = 1; break;
6075 case SHF_EXECINSTR: sindex = 2; break;
6076 case SHF_MERGE: sindex = 3; break;
6077 case SHF_STRINGS: sindex = 4; break;
6078 case SHF_INFO_LINK: sindex = 5; break;
6079 case SHF_LINK_ORDER: sindex = 6; break;
6080 case SHF_OS_NONCONFORMING: sindex = 7; break;
6081 case SHF_GROUP: sindex = 8; break;
6082 case SHF_TLS: sindex = 9; break;
6083 case SHF_EXCLUDE: sindex = 18; break;
6084 case SHF_COMPRESSED: sindex = 20; break;
6085
6086 default:
6087 sindex = -1;
6088 switch (filedata->file_header.e_machine)
6089 {
6090 case EM_IA_64:
6091 if (flag == SHF_IA_64_SHORT)
6092 sindex = 10;
6093 else if (flag == SHF_IA_64_NORECOV)
6094 sindex = 11;
6095 #ifdef BFD64
6096 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6097 switch (flag)
6098 {
6099 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6100 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6101 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6102 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6103 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6104 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6105 default: break;
6106 }
6107 #endif
6108 break;
6109
6110 case EM_386:
6111 case EM_IAMCU:
6112 case EM_X86_64:
6113 case EM_L1OM:
6114 case EM_K1OM:
6115 case EM_OLD_SPARCV9:
6116 case EM_SPARC32PLUS:
6117 case EM_SPARCV9:
6118 case EM_SPARC:
6119 if (flag == SHF_ORDERED)
6120 sindex = 19;
6121 break;
6122
6123 case EM_ARM:
6124 switch (flag)
6125 {
6126 case SHF_ENTRYSECT: sindex = 21; break;
6127 case SHF_ARM_PURECODE: sindex = 22; break;
6128 case SHF_COMDEF: sindex = 23; break;
6129 default: break;
6130 }
6131 break;
6132 case EM_PPC:
6133 if (flag == SHF_PPC_VLE)
6134 sindex = 25;
6135 break;
6136 default:
6137 break;
6138 }
6139
6140 switch (filedata->file_header.e_ident[EI_OSABI])
6141 {
6142 case ELFOSABI_GNU:
6143 case ELFOSABI_FREEBSD:
6144 if (flag == SHF_GNU_RETAIN)
6145 sindex = 26;
6146 /* Fall through */
6147 case ELFOSABI_NONE:
6148 if (flag == SHF_GNU_MBIND)
6149 /* We should not recognize SHF_GNU_MBIND for
6150 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6151 not set the EI_OSABI header byte. */
6152 sindex = 24;
6153 break;
6154 default:
6155 break;
6156 }
6157 break;
6158 }
6159
6160 if (sindex != -1)
6161 {
6162 if (p != buff + field_size + 4)
6163 {
6164 if (size < (10 + 2))
6165 {
6166 warn (_("Internal error: not enough buffer room for section flag info"));
6167 return _("<unknown>");
6168 }
6169 size -= 2;
6170 *p++ = ',';
6171 *p++ = ' ';
6172 }
6173
6174 size -= flags [sindex].len;
6175 p = stpcpy (p, flags [sindex].str);
6176 }
6177 else if (flag & SHF_MASKOS)
6178 os_flags |= flag;
6179 else if (flag & SHF_MASKPROC)
6180 proc_flags |= flag;
6181 else
6182 unknown_flags |= flag;
6183 }
6184 else
6185 {
6186 switch (flag)
6187 {
6188 case SHF_WRITE: *p = 'W'; break;
6189 case SHF_ALLOC: *p = 'A'; break;
6190 case SHF_EXECINSTR: *p = 'X'; break;
6191 case SHF_MERGE: *p = 'M'; break;
6192 case SHF_STRINGS: *p = 'S'; break;
6193 case SHF_INFO_LINK: *p = 'I'; break;
6194 case SHF_LINK_ORDER: *p = 'L'; break;
6195 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6196 case SHF_GROUP: *p = 'G'; break;
6197 case SHF_TLS: *p = 'T'; break;
6198 case SHF_EXCLUDE: *p = 'E'; break;
6199 case SHF_COMPRESSED: *p = 'C'; break;
6200
6201 default:
6202 if ((filedata->file_header.e_machine == EM_X86_64
6203 || filedata->file_header.e_machine == EM_L1OM
6204 || filedata->file_header.e_machine == EM_K1OM)
6205 && flag == SHF_X86_64_LARGE)
6206 *p = 'l';
6207 else if (filedata->file_header.e_machine == EM_ARM
6208 && flag == SHF_ARM_PURECODE)
6209 *p = 'y';
6210 else if (filedata->file_header.e_machine == EM_PPC
6211 && flag == SHF_PPC_VLE)
6212 *p = 'v';
6213 else if (flag & SHF_MASKOS)
6214 {
6215 switch (filedata->file_header.e_ident[EI_OSABI])
6216 {
6217 case ELFOSABI_GNU:
6218 case ELFOSABI_FREEBSD:
6219 if (flag == SHF_GNU_RETAIN)
6220 {
6221 *p = 'R';
6222 break;
6223 }
6224 /* Fall through */
6225 case ELFOSABI_NONE:
6226 if (flag == SHF_GNU_MBIND)
6227 {
6228 /* We should not recognize SHF_GNU_MBIND for
6229 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6230 not set the EI_OSABI header byte. */
6231 *p = 'D';
6232 break;
6233 }
6234 /* Fall through */
6235 default:
6236 *p = 'o';
6237 sh_flags &= ~SHF_MASKOS;
6238 break;
6239 }
6240 }
6241 else if (flag & SHF_MASKPROC)
6242 {
6243 *p = 'p';
6244 sh_flags &= ~ SHF_MASKPROC;
6245 }
6246 else
6247 *p = 'x';
6248 break;
6249 }
6250 p++;
6251 }
6252 }
6253
6254 if (do_section_details)
6255 {
6256 if (os_flags)
6257 {
6258 size -= 5 + field_size;
6259 if (p != buff + field_size + 4)
6260 {
6261 if (size < (2 + 1))
6262 {
6263 warn (_("Internal error: not enough buffer room for section flag info"));
6264 return _("<unknown>");
6265 }
6266 size -= 2;
6267 *p++ = ',';
6268 *p++ = ' ';
6269 }
6270 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6271 (unsigned long) os_flags);
6272 p += 5 + field_size;
6273 }
6274 if (proc_flags)
6275 {
6276 size -= 7 + field_size;
6277 if (p != buff + field_size + 4)
6278 {
6279 if (size < (2 + 1))
6280 {
6281 warn (_("Internal error: not enough buffer room for section flag info"));
6282 return _("<unknown>");
6283 }
6284 size -= 2;
6285 *p++ = ',';
6286 *p++ = ' ';
6287 }
6288 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6289 (unsigned long) proc_flags);
6290 p += 7 + field_size;
6291 }
6292 if (unknown_flags)
6293 {
6294 size -= 10 + field_size;
6295 if (p != buff + field_size + 4)
6296 {
6297 if (size < (2 + 1))
6298 {
6299 warn (_("Internal error: not enough buffer room for section flag info"));
6300 return _("<unknown>");
6301 }
6302 size -= 2;
6303 *p++ = ',';
6304 *p++ = ' ';
6305 }
6306 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6307 (unsigned long) unknown_flags);
6308 p += 10 + field_size;
6309 }
6310 }
6311
6312 *p = '\0';
6313 return buff;
6314 }
6315
6316 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
6317 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6318 {
6319 if (is_32bit_elf)
6320 {
6321 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6322
6323 if (size < sizeof (* echdr))
6324 {
6325 error (_("Compressed section is too small even for a compression header\n"));
6326 return 0;
6327 }
6328
6329 chdr->ch_type = BYTE_GET (echdr->ch_type);
6330 chdr->ch_size = BYTE_GET (echdr->ch_size);
6331 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6332 return sizeof (*echdr);
6333 }
6334 else
6335 {
6336 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6337
6338 if (size < sizeof (* echdr))
6339 {
6340 error (_("Compressed section is too small even for a compression header\n"));
6341 return 0;
6342 }
6343
6344 chdr->ch_type = BYTE_GET (echdr->ch_type);
6345 chdr->ch_size = BYTE_GET (echdr->ch_size);
6346 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6347 return sizeof (*echdr);
6348 }
6349 }
6350
6351 static bool
6352 process_section_headers (Filedata * filedata)
6353 {
6354 Elf_Internal_Shdr * section;
6355 unsigned int i;
6356
6357 free (filedata->section_headers);
6358 filedata->section_headers = NULL;
6359 free (filedata->dynamic_symbols);
6360 filedata->dynamic_symbols = NULL;
6361 filedata->num_dynamic_syms = 0;
6362 free (filedata->dynamic_strings);
6363 filedata->dynamic_strings = NULL;
6364 filedata->dynamic_strings_length = 0;
6365 free (filedata->dynamic_syminfo);
6366 filedata->dynamic_syminfo = NULL;
6367 while (filedata->symtab_shndx_list != NULL)
6368 {
6369 elf_section_list *next = filedata->symtab_shndx_list->next;
6370 free (filedata->symtab_shndx_list);
6371 filedata->symtab_shndx_list = next;
6372 }
6373
6374 if (filedata->file_header.e_shnum == 0)
6375 {
6376 /* PR binutils/12467. */
6377 if (filedata->file_header.e_shoff != 0)
6378 {
6379 warn (_("possibly corrupt ELF file header - it has a non-zero"
6380 " section header offset, but no section headers\n"));
6381 return false;
6382 }
6383 else if (do_sections)
6384 printf (_("\nThere are no sections in this file.\n"));
6385
6386 return true;
6387 }
6388
6389 if (do_sections && !do_header)
6390 {
6391 if (filedata->is_separate && process_links)
6392 printf (_("In linked file '%s': "), filedata->file_name);
6393 if (! filedata->is_separate || process_links)
6394 printf (ngettext ("There is %d section header, "
6395 "starting at offset 0x%lx:\n",
6396 "There are %d section headers, "
6397 "starting at offset 0x%lx:\n",
6398 filedata->file_header.e_shnum),
6399 filedata->file_header.e_shnum,
6400 (unsigned long) filedata->file_header.e_shoff);
6401 }
6402
6403 if (is_32bit_elf)
6404 {
6405 if (! get_32bit_section_headers (filedata, false))
6406 return false;
6407 }
6408 else
6409 {
6410 if (! get_64bit_section_headers (filedata, false))
6411 return false;
6412 }
6413
6414 /* Read in the string table, so that we have names to display. */
6415 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6416 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6417 {
6418 section = filedata->section_headers + filedata->file_header.e_shstrndx;
6419
6420 if (section->sh_size != 0)
6421 {
6422 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6423 1, section->sh_size,
6424 _("string table"));
6425
6426 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6427 }
6428 }
6429
6430 /* Scan the sections for the dynamic symbol table
6431 and dynamic string table and debug sections. */
6432 eh_addr_size = is_32bit_elf ? 4 : 8;
6433 switch (filedata->file_header.e_machine)
6434 {
6435 case EM_MIPS:
6436 case EM_MIPS_RS3_LE:
6437 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6438 FDE addresses. However, the ABI also has a semi-official ILP32
6439 variant for which the normal FDE address size rules apply.
6440
6441 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6442 section, where XX is the size of longs in bits. Unfortunately,
6443 earlier compilers provided no way of distinguishing ILP32 objects
6444 from LP64 objects, so if there's any doubt, we should assume that
6445 the official LP64 form is being used. */
6446 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6447 && find_section (filedata, ".gcc_compiled_long32") == NULL)
6448 eh_addr_size = 8;
6449 break;
6450
6451 case EM_H8_300:
6452 case EM_H8_300H:
6453 switch (filedata->file_header.e_flags & EF_H8_MACH)
6454 {
6455 case E_H8_MACH_H8300:
6456 case E_H8_MACH_H8300HN:
6457 case E_H8_MACH_H8300SN:
6458 case E_H8_MACH_H8300SXN:
6459 eh_addr_size = 2;
6460 break;
6461 case E_H8_MACH_H8300H:
6462 case E_H8_MACH_H8300S:
6463 case E_H8_MACH_H8300SX:
6464 eh_addr_size = 4;
6465 break;
6466 }
6467 break;
6468
6469 case EM_M32C_OLD:
6470 case EM_M32C:
6471 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6472 {
6473 case EF_M32C_CPU_M16C:
6474 eh_addr_size = 2;
6475 break;
6476 }
6477 break;
6478 }
6479
6480 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6481 do \
6482 { \
6483 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6484 if (section->sh_entsize != expected_entsize) \
6485 { \
6486 char buf[40]; \
6487 sprintf_vma (buf, section->sh_entsize); \
6488 /* Note: coded this way so that there is a single string for \
6489 translation. */ \
6490 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6491 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6492 (unsigned) expected_entsize); \
6493 section->sh_entsize = expected_entsize; \
6494 } \
6495 } \
6496 while (0)
6497
6498 #define CHECK_ENTSIZE(section, i, type) \
6499 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
6500 sizeof (Elf64_External_##type))
6501
6502 for (i = 0, section = filedata->section_headers;
6503 i < filedata->file_header.e_shnum;
6504 i++, section++)
6505 {
6506 char * name = SECTION_NAME_PRINT (section);
6507
6508 /* Run some sanity checks on the headers and
6509 possibly fill in some file data as well. */
6510 switch (section->sh_type)
6511 {
6512 case SHT_DYNSYM:
6513 if (filedata->dynamic_symbols != NULL)
6514 {
6515 error (_("File contains multiple dynamic symbol tables\n"));
6516 continue;
6517 }
6518
6519 CHECK_ENTSIZE (section, i, Sym);
6520 filedata->dynamic_symbols
6521 = GET_ELF_SYMBOLS (filedata, section, &filedata->num_dynamic_syms);
6522 filedata->dynamic_symtab_section = section;
6523 break;
6524
6525 case SHT_STRTAB:
6526 if (streq (name, ".dynstr"))
6527 {
6528 if (filedata->dynamic_strings != NULL)
6529 {
6530 error (_("File contains multiple dynamic string tables\n"));
6531 continue;
6532 }
6533
6534 filedata->dynamic_strings
6535 = (char *) get_data (NULL, filedata, section->sh_offset,
6536 1, section->sh_size, _("dynamic strings"));
6537 filedata->dynamic_strings_length
6538 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
6539 filedata->dynamic_strtab_section = section;
6540 }
6541 break;
6542
6543 case SHT_SYMTAB_SHNDX:
6544 {
6545 elf_section_list * entry = xmalloc (sizeof * entry);
6546
6547 entry->hdr = section;
6548 entry->next = filedata->symtab_shndx_list;
6549 filedata->symtab_shndx_list = entry;
6550 }
6551 break;
6552
6553 case SHT_SYMTAB:
6554 CHECK_ENTSIZE (section, i, Sym);
6555 break;
6556
6557 case SHT_GROUP:
6558 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6559 break;
6560
6561 case SHT_REL:
6562 CHECK_ENTSIZE (section, i, Rel);
6563 if (do_checks && section->sh_size == 0)
6564 warn (_("Section '%s': zero-sized relocation section\n"), name);
6565 break;
6566
6567 case SHT_RELA:
6568 CHECK_ENTSIZE (section, i, Rela);
6569 if (do_checks && section->sh_size == 0)
6570 warn (_("Section '%s': zero-sized relocation section\n"), name);
6571 break;
6572
6573 case SHT_NOTE:
6574 case SHT_PROGBITS:
6575 /* Having a zero sized section is not illegal according to the
6576 ELF standard, but it might be an indication that something
6577 is wrong. So issue a warning if we are running in lint mode. */
6578 if (do_checks && section->sh_size == 0)
6579 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6580 break;
6581
6582 default:
6583 break;
6584 }
6585
6586 if ((do_debugging || do_debug_info || do_debug_abbrevs
6587 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6588 || do_debug_aranges || do_debug_frames || do_debug_macinfo
6589 || do_debug_str || do_debug_str_offsets || do_debug_loc
6590 || do_debug_ranges
6591 || do_debug_addr || do_debug_cu_index || do_debug_links)
6592 && (const_strneq (name, ".debug_")
6593 || const_strneq (name, ".zdebug_")))
6594 {
6595 if (name[1] == 'z')
6596 name += sizeof (".zdebug_") - 1;
6597 else
6598 name += sizeof (".debug_") - 1;
6599
6600 if (do_debugging
6601 || (do_debug_info && const_strneq (name, "info"))
6602 || (do_debug_info && const_strneq (name, "types"))
6603 || (do_debug_abbrevs && const_strneq (name, "abbrev"))
6604 || (do_debug_lines && strcmp (name, "line") == 0)
6605 || (do_debug_lines && const_strneq (name, "line."))
6606 || (do_debug_pubnames && const_strneq (name, "pubnames"))
6607 || (do_debug_pubtypes && const_strneq (name, "pubtypes"))
6608 || (do_debug_pubnames && const_strneq (name, "gnu_pubnames"))
6609 || (do_debug_pubtypes && const_strneq (name, "gnu_pubtypes"))
6610 || (do_debug_aranges && const_strneq (name, "aranges"))
6611 || (do_debug_ranges && const_strneq (name, "ranges"))
6612 || (do_debug_ranges && const_strneq (name, "rnglists"))
6613 || (do_debug_frames && const_strneq (name, "frame"))
6614 || (do_debug_macinfo && const_strneq (name, "macinfo"))
6615 || (do_debug_macinfo && const_strneq (name, "macro"))
6616 || (do_debug_str && const_strneq (name, "str"))
6617 || (do_debug_links && const_strneq (name, "sup"))
6618 || (do_debug_str_offsets && const_strneq (name, "str_offsets"))
6619 || (do_debug_loc && const_strneq (name, "loc"))
6620 || (do_debug_loc && const_strneq (name, "loclists"))
6621 || (do_debug_addr && const_strneq (name, "addr"))
6622 || (do_debug_cu_index && const_strneq (name, "cu_index"))
6623 || (do_debug_cu_index && const_strneq (name, "tu_index"))
6624 )
6625 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6626 }
6627 /* Linkonce section to be combined with .debug_info at link time. */
6628 else if ((do_debugging || do_debug_info)
6629 && const_strneq (name, ".gnu.linkonce.wi."))
6630 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6631 else if (do_debug_frames && streq (name, ".eh_frame"))
6632 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6633 else if (do_gdb_index && (streq (name, ".gdb_index")
6634 || streq (name, ".debug_names")))
6635 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6636 /* Trace sections for Itanium VMS. */
6637 else if ((do_debugging || do_trace_info || do_trace_abbrevs
6638 || do_trace_aranges)
6639 && const_strneq (name, ".trace_"))
6640 {
6641 name += sizeof (".trace_") - 1;
6642
6643 if (do_debugging
6644 || (do_trace_info && streq (name, "info"))
6645 || (do_trace_abbrevs && streq (name, "abbrev"))
6646 || (do_trace_aranges && streq (name, "aranges"))
6647 )
6648 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6649 }
6650 else if ((do_debugging || do_debug_links)
6651 && (const_strneq (name, ".gnu_debuglink")
6652 || const_strneq (name, ".gnu_debugaltlink")))
6653 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6654 }
6655
6656 if (! do_sections)
6657 return true;
6658
6659 if (filedata->is_separate && ! process_links)
6660 return true;
6661
6662 if (filedata->is_separate)
6663 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
6664 else if (filedata->file_header.e_shnum > 1)
6665 printf (_("\nSection Headers:\n"));
6666 else
6667 printf (_("\nSection Header:\n"));
6668
6669 if (is_32bit_elf)
6670 {
6671 if (do_section_details)
6672 {
6673 printf (_(" [Nr] Name\n"));
6674 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
6675 }
6676 else
6677 printf
6678 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
6679 }
6680 else if (do_wide)
6681 {
6682 if (do_section_details)
6683 {
6684 printf (_(" [Nr] Name\n"));
6685 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
6686 }
6687 else
6688 printf
6689 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
6690 }
6691 else
6692 {
6693 if (do_section_details)
6694 {
6695 printf (_(" [Nr] Name\n"));
6696 printf (_(" Type Address Offset Link\n"));
6697 printf (_(" Size EntSize Info Align\n"));
6698 }
6699 else
6700 {
6701 printf (_(" [Nr] Name Type Address Offset\n"));
6702 printf (_(" Size EntSize Flags Link Info Align\n"));
6703 }
6704 }
6705
6706 if (do_section_details)
6707 printf (_(" Flags\n"));
6708
6709 for (i = 0, section = filedata->section_headers;
6710 i < filedata->file_header.e_shnum;
6711 i++, section++)
6712 {
6713 /* Run some sanity checks on the section header. */
6714
6715 /* Check the sh_link field. */
6716 switch (section->sh_type)
6717 {
6718 case SHT_REL:
6719 case SHT_RELA:
6720 if (section->sh_link == 0
6721 && (filedata->file_header.e_type == ET_EXEC
6722 || filedata->file_header.e_type == ET_DYN))
6723 /* A dynamic relocation section where all entries use a
6724 zero symbol index need not specify a symtab section. */
6725 break;
6726 /* Fall through. */
6727 case SHT_SYMTAB_SHNDX:
6728 case SHT_GROUP:
6729 case SHT_HASH:
6730 case SHT_GNU_HASH:
6731 case SHT_GNU_versym:
6732 if (section->sh_link == 0
6733 || section->sh_link >= filedata->file_header.e_shnum
6734 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6735 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6736 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6737 i, section->sh_link);
6738 break;
6739
6740 case SHT_DYNAMIC:
6741 case SHT_SYMTAB:
6742 case SHT_DYNSYM:
6743 case SHT_GNU_verneed:
6744 case SHT_GNU_verdef:
6745 case SHT_GNU_LIBLIST:
6746 if (section->sh_link == 0
6747 || section->sh_link >= filedata->file_header.e_shnum
6748 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6749 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6750 i, section->sh_link);
6751 break;
6752
6753 case SHT_INIT_ARRAY:
6754 case SHT_FINI_ARRAY:
6755 case SHT_PREINIT_ARRAY:
6756 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6757 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6758 i, section->sh_link);
6759 break;
6760
6761 default:
6762 /* FIXME: Add support for target specific section types. */
6763 #if 0 /* Currently we do not check other section types as there are too
6764 many special cases. Stab sections for example have a type
6765 of SHT_PROGBITS but an sh_link field that links to the .stabstr
6766 section. */
6767 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6768 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6769 i, section->sh_link);
6770 #endif
6771 break;
6772 }
6773
6774 /* Check the sh_info field. */
6775 switch (section->sh_type)
6776 {
6777 case SHT_REL:
6778 case SHT_RELA:
6779 if (section->sh_info == 0
6780 && (filedata->file_header.e_type == ET_EXEC
6781 || filedata->file_header.e_type == ET_DYN))
6782 /* Dynamic relocations apply to segments, so they do not
6783 need to specify the section they relocate. */
6784 break;
6785 if (section->sh_info == 0
6786 || section->sh_info >= filedata->file_header.e_shnum
6787 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
6788 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
6789 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
6790 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
6791 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
6792 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
6793 /* FIXME: Are other section types valid ? */
6794 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
6795 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
6796 i, section->sh_info);
6797 break;
6798
6799 case SHT_DYNAMIC:
6800 case SHT_HASH:
6801 case SHT_SYMTAB_SHNDX:
6802 case SHT_INIT_ARRAY:
6803 case SHT_FINI_ARRAY:
6804 case SHT_PREINIT_ARRAY:
6805 if (section->sh_info != 0)
6806 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6807 i, section->sh_info);
6808 break;
6809
6810 case SHT_GROUP:
6811 case SHT_SYMTAB:
6812 case SHT_DYNSYM:
6813 /* A symbol index - we assume that it is valid. */
6814 break;
6815
6816 default:
6817 /* FIXME: Add support for target specific section types. */
6818 if (section->sh_type == SHT_NOBITS)
6819 /* NOBITS section headers with non-zero sh_info fields can be
6820 created when a binary is stripped of everything but its debug
6821 information. The stripped sections have their headers
6822 preserved but their types set to SHT_NOBITS. So do not check
6823 this type of section. */
6824 ;
6825 else if (section->sh_flags & SHF_INFO_LINK)
6826 {
6827 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
6828 warn (_("[%2u]: Expected link to another section in info field"), i);
6829 }
6830 else if (section->sh_type < SHT_LOOS
6831 && (section->sh_flags & SHF_GNU_MBIND) == 0
6832 && section->sh_info != 0)
6833 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6834 i, section->sh_info);
6835 break;
6836 }
6837
6838 /* Check the sh_size field. */
6839 if (section->sh_size > filedata->file_size
6840 && section->sh_type != SHT_NOBITS
6841 && section->sh_type != SHT_NULL
6842 && section->sh_type < SHT_LOOS)
6843 warn (_("Size of section %u is larger than the entire file!\n"), i);
6844
6845 printf (" [%2u] ", i);
6846 if (do_section_details)
6847 printf ("%s\n ", printable_section_name (filedata, section));
6848 else
6849 print_symbol (-17, SECTION_NAME_PRINT (section));
6850
6851 printf (do_wide ? " %-15s " : " %-15.15s ",
6852 get_section_type_name (filedata, section->sh_type));
6853
6854 if (is_32bit_elf)
6855 {
6856 const char * link_too_big = NULL;
6857
6858 print_vma (section->sh_addr, LONG_HEX);
6859
6860 printf ( " %6.6lx %6.6lx %2.2lx",
6861 (unsigned long) section->sh_offset,
6862 (unsigned long) section->sh_size,
6863 (unsigned long) section->sh_entsize);
6864
6865 if (do_section_details)
6866 fputs (" ", stdout);
6867 else
6868 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6869
6870 if (section->sh_link >= filedata->file_header.e_shnum)
6871 {
6872 link_too_big = "";
6873 /* The sh_link value is out of range. Normally this indicates
6874 an error but it can have special values in Solaris binaries. */
6875 switch (filedata->file_header.e_machine)
6876 {
6877 case EM_386:
6878 case EM_IAMCU:
6879 case EM_X86_64:
6880 case EM_L1OM:
6881 case EM_K1OM:
6882 case EM_OLD_SPARCV9:
6883 case EM_SPARC32PLUS:
6884 case EM_SPARCV9:
6885 case EM_SPARC:
6886 if (section->sh_link == (SHN_BEFORE & 0xffff))
6887 link_too_big = "BEFORE";
6888 else if (section->sh_link == (SHN_AFTER & 0xffff))
6889 link_too_big = "AFTER";
6890 break;
6891 default:
6892 break;
6893 }
6894 }
6895
6896 if (do_section_details)
6897 {
6898 if (link_too_big != NULL && * link_too_big)
6899 printf ("<%s> ", link_too_big);
6900 else
6901 printf ("%2u ", section->sh_link);
6902 printf ("%3u %2lu\n", section->sh_info,
6903 (unsigned long) section->sh_addralign);
6904 }
6905 else
6906 printf ("%2u %3u %2lu\n",
6907 section->sh_link,
6908 section->sh_info,
6909 (unsigned long) section->sh_addralign);
6910
6911 if (link_too_big && ! * link_too_big)
6912 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
6913 i, section->sh_link);
6914 }
6915 else if (do_wide)
6916 {
6917 print_vma (section->sh_addr, LONG_HEX);
6918
6919 if ((long) section->sh_offset == section->sh_offset)
6920 printf (" %6.6lx", (unsigned long) section->sh_offset);
6921 else
6922 {
6923 putchar (' ');
6924 print_vma (section->sh_offset, LONG_HEX);
6925 }
6926
6927 if ((unsigned long) section->sh_size == section->sh_size)
6928 printf (" %6.6lx", (unsigned long) section->sh_size);
6929 else
6930 {
6931 putchar (' ');
6932 print_vma (section->sh_size, LONG_HEX);
6933 }
6934
6935 if ((unsigned long) section->sh_entsize == section->sh_entsize)
6936 printf (" %2.2lx", (unsigned long) section->sh_entsize);
6937 else
6938 {
6939 putchar (' ');
6940 print_vma (section->sh_entsize, LONG_HEX);
6941 }
6942
6943 if (do_section_details)
6944 fputs (" ", stdout);
6945 else
6946 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6947
6948 printf ("%2u %3u ", section->sh_link, section->sh_info);
6949
6950 if ((unsigned long) section->sh_addralign == section->sh_addralign)
6951 printf ("%2lu\n", (unsigned long) section->sh_addralign);
6952 else
6953 {
6954 print_vma (section->sh_addralign, DEC);
6955 putchar ('\n');
6956 }
6957 }
6958 else if (do_section_details)
6959 {
6960 putchar (' ');
6961 print_vma (section->sh_addr, LONG_HEX);
6962 if ((long) section->sh_offset == section->sh_offset)
6963 printf (" %16.16lx", (unsigned long) section->sh_offset);
6964 else
6965 {
6966 printf (" ");
6967 print_vma (section->sh_offset, LONG_HEX);
6968 }
6969 printf (" %u\n ", section->sh_link);
6970 print_vma (section->sh_size, LONG_HEX);
6971 putchar (' ');
6972 print_vma (section->sh_entsize, LONG_HEX);
6973
6974 printf (" %-16u %lu\n",
6975 section->sh_info,
6976 (unsigned long) section->sh_addralign);
6977 }
6978 else
6979 {
6980 putchar (' ');
6981 print_vma (section->sh_addr, LONG_HEX);
6982 if ((long) section->sh_offset == section->sh_offset)
6983 printf (" %8.8lx", (unsigned long) section->sh_offset);
6984 else
6985 {
6986 printf (" ");
6987 print_vma (section->sh_offset, LONG_HEX);
6988 }
6989 printf ("\n ");
6990 print_vma (section->sh_size, LONG_HEX);
6991 printf (" ");
6992 print_vma (section->sh_entsize, LONG_HEX);
6993
6994 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
6995
6996 printf (" %2u %3u %lu\n",
6997 section->sh_link,
6998 section->sh_info,
6999 (unsigned long) section->sh_addralign);
7000 }
7001
7002 if (do_section_details)
7003 {
7004 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7005 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7006 {
7007 /* Minimum section size is 12 bytes for 32-bit compression
7008 header + 12 bytes for compressed data header. */
7009 unsigned char buf[24];
7010
7011 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7012 if (get_data (&buf, filedata, section->sh_offset, 1,
7013 sizeof (buf), _("compression header")))
7014 {
7015 Elf_Internal_Chdr chdr;
7016
7017 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7018 printf (_(" [<corrupt>]\n"));
7019 else
7020 {
7021 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7022 printf (" ZLIB, ");
7023 else
7024 printf (_(" [<unknown>: 0x%x], "),
7025 chdr.ch_type);
7026 print_vma (chdr.ch_size, LONG_HEX);
7027 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7028 }
7029 }
7030 }
7031 }
7032 }
7033
7034 if (!do_section_details)
7035 {
7036 /* The ordering of the letters shown here matches the ordering of the
7037 corresponding SHF_xxx values, and hence the order in which these
7038 letters will be displayed to the user. */
7039 printf (_("Key to Flags:\n\
7040 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7041 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7042 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7043 switch (filedata->file_header.e_ident[EI_OSABI])
7044 {
7045 case ELFOSABI_GNU:
7046 case ELFOSABI_FREEBSD:
7047 printf (_("R (retain), "));
7048 /* Fall through */
7049 case ELFOSABI_NONE:
7050 printf (_("D (mbind), "));
7051 break;
7052 default:
7053 break;
7054 }
7055 if (filedata->file_header.e_machine == EM_X86_64
7056 || filedata->file_header.e_machine == EM_L1OM
7057 || filedata->file_header.e_machine == EM_K1OM)
7058 printf (_("l (large), "));
7059 else if (filedata->file_header.e_machine == EM_ARM)
7060 printf (_("y (purecode), "));
7061 else if (filedata->file_header.e_machine == EM_PPC)
7062 printf (_("v (VLE), "));
7063 printf ("p (processor specific)\n");
7064 }
7065
7066 return true;
7067 }
7068
7069 static bool
7070 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7071 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7072 char **strtab, unsigned long *strtablen)
7073 {
7074 *strtab = NULL;
7075 *strtablen = 0;
7076 *symtab = GET_ELF_SYMBOLS (filedata, symsec, nsyms);
7077
7078 if (*symtab == NULL)
7079 return false;
7080
7081 if (symsec->sh_link != 0)
7082 {
7083 Elf_Internal_Shdr *strsec;
7084
7085 if (symsec->sh_link >= filedata->file_header.e_shnum)
7086 {
7087 error (_("Bad sh_link in symbol table section\n"));
7088 free (*symtab);
7089 *symtab = NULL;
7090 *nsyms = 0;
7091 return false;
7092 }
7093
7094 strsec = filedata->section_headers + symsec->sh_link;
7095
7096 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7097 1, strsec->sh_size, _("string table"));
7098 if (*strtab == NULL)
7099 {
7100 free (*symtab);
7101 *symtab = NULL;
7102 *nsyms = 0;
7103 return false;
7104 }
7105 *strtablen = strsec->sh_size;
7106 }
7107 return true;
7108 }
7109
7110 static const char *
7111 get_group_flags (unsigned int flags)
7112 {
7113 static char buff[128];
7114
7115 if (flags == 0)
7116 return "";
7117 else if (flags == GRP_COMDAT)
7118 return "COMDAT ";
7119
7120 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7121 flags,
7122 flags & GRP_MASKOS ? _("<OS specific>") : "",
7123 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7124 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7125 ? _("<unknown>") : ""));
7126
7127 return buff;
7128 }
7129
7130 static bool
7131 process_section_groups (Filedata * filedata)
7132 {
7133 Elf_Internal_Shdr * section;
7134 unsigned int i;
7135 struct group * group;
7136 Elf_Internal_Shdr * symtab_sec;
7137 Elf_Internal_Shdr * strtab_sec;
7138 Elf_Internal_Sym * symtab;
7139 unsigned long num_syms;
7140 char * strtab;
7141 size_t strtab_size;
7142
7143 /* Don't process section groups unless needed. */
7144 if (!do_unwind && !do_section_groups)
7145 return true;
7146
7147 if (filedata->file_header.e_shnum == 0)
7148 {
7149 if (do_section_groups)
7150 {
7151 if (filedata->is_separate)
7152 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7153 filedata->file_name);
7154 else
7155 printf (_("\nThere are no section groups in this file.\n"));
7156 }
7157 return true;
7158 }
7159
7160 if (filedata->section_headers == NULL)
7161 {
7162 error (_("Section headers are not available!\n"));
7163 /* PR 13622: This can happen with a corrupt ELF header. */
7164 return false;
7165 }
7166
7167 filedata->section_headers_groups
7168 = (struct group **) calloc (filedata->file_header.e_shnum,
7169 sizeof (struct group *));
7170
7171 if (filedata->section_headers_groups == NULL)
7172 {
7173 error (_("Out of memory reading %u section group headers\n"),
7174 filedata->file_header.e_shnum);
7175 return false;
7176 }
7177
7178 /* Scan the sections for the group section. */
7179 filedata->group_count = 0;
7180 for (i = 0, section = filedata->section_headers;
7181 i < filedata->file_header.e_shnum;
7182 i++, section++)
7183 if (section->sh_type == SHT_GROUP)
7184 filedata->group_count++;
7185
7186 if (filedata->group_count == 0)
7187 {
7188 if (do_section_groups)
7189 {
7190 if (filedata->is_separate)
7191 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7192 filedata->file_name);
7193 else
7194 printf (_("\nThere are no section groups in this file.\n"));
7195 }
7196
7197 return true;
7198 }
7199
7200 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7201 sizeof (struct group));
7202
7203 if (filedata->section_groups == NULL)
7204 {
7205 error (_("Out of memory reading %lu groups\n"),
7206 (unsigned long) filedata->group_count);
7207 return false;
7208 }
7209
7210 symtab_sec = NULL;
7211 strtab_sec = NULL;
7212 symtab = NULL;
7213 num_syms = 0;
7214 strtab = NULL;
7215 strtab_size = 0;
7216
7217 if (filedata->is_separate)
7218 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7219
7220 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7221 i < filedata->file_header.e_shnum;
7222 i++, section++)
7223 {
7224 if (section->sh_type == SHT_GROUP)
7225 {
7226 const char * name = printable_section_name (filedata, section);
7227 const char * group_name;
7228 unsigned char * start;
7229 unsigned char * indices;
7230 unsigned int entry, j, size;
7231 Elf_Internal_Shdr * sec;
7232 Elf_Internal_Sym * sym;
7233
7234 /* Get the symbol table. */
7235 if (section->sh_link >= filedata->file_header.e_shnum
7236 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7237 != SHT_SYMTAB))
7238 {
7239 error (_("Bad sh_link in group section `%s'\n"), name);
7240 continue;
7241 }
7242
7243 if (symtab_sec != sec)
7244 {
7245 symtab_sec = sec;
7246 free (symtab);
7247 symtab = GET_ELF_SYMBOLS (filedata, symtab_sec, & num_syms);
7248 }
7249
7250 if (symtab == NULL)
7251 {
7252 error (_("Corrupt header in group section `%s'\n"), name);
7253 continue;
7254 }
7255
7256 if (section->sh_info >= num_syms)
7257 {
7258 error (_("Bad sh_info in group section `%s'\n"), name);
7259 continue;
7260 }
7261
7262 sym = symtab + section->sh_info;
7263
7264 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7265 {
7266 if (sym->st_shndx == 0
7267 || sym->st_shndx >= filedata->file_header.e_shnum)
7268 {
7269 error (_("Bad sh_info in group section `%s'\n"), name);
7270 continue;
7271 }
7272
7273 group_name = SECTION_NAME_PRINT (filedata->section_headers
7274 + sym->st_shndx);
7275 strtab_sec = NULL;
7276 free (strtab);
7277 strtab = NULL;
7278 strtab_size = 0;
7279 }
7280 else
7281 {
7282 /* Get the string table. */
7283 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7284 {
7285 strtab_sec = NULL;
7286 free (strtab);
7287 strtab = NULL;
7288 strtab_size = 0;
7289 }
7290 else if (strtab_sec
7291 != (sec = filedata->section_headers + symtab_sec->sh_link))
7292 {
7293 strtab_sec = sec;
7294 free (strtab);
7295
7296 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
7297 1, strtab_sec->sh_size,
7298 _("string table"));
7299 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
7300 }
7301 group_name = sym->st_name < strtab_size
7302 ? strtab + sym->st_name : _("<corrupt>");
7303 }
7304
7305 /* PR 17531: file: loop. */
7306 if (section->sh_entsize > section->sh_size)
7307 {
7308 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
7309 printable_section_name (filedata, section),
7310 (unsigned long) section->sh_entsize,
7311 (unsigned long) section->sh_size);
7312 continue;
7313 }
7314
7315 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
7316 1, section->sh_size,
7317 _("section data"));
7318 if (start == NULL)
7319 continue;
7320
7321 indices = start;
7322 size = (section->sh_size / section->sh_entsize) - 1;
7323 entry = byte_get (indices, 4);
7324 indices += 4;
7325
7326 if (do_section_groups)
7327 {
7328 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
7329 get_group_flags (entry), i, name, group_name, size);
7330
7331 printf (_(" [Index] Name\n"));
7332 }
7333
7334 group->group_index = i;
7335
7336 for (j = 0; j < size; j++)
7337 {
7338 struct group_list * g;
7339
7340 entry = byte_get (indices, 4);
7341 indices += 4;
7342
7343 if (entry >= filedata->file_header.e_shnum)
7344 {
7345 static unsigned num_group_errors = 0;
7346
7347 if (num_group_errors ++ < 10)
7348 {
7349 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
7350 entry, i, filedata->file_header.e_shnum - 1);
7351 if (num_group_errors == 10)
7352 warn (_("Further error messages about overlarge group section indices suppressed\n"));
7353 }
7354 continue;
7355 }
7356
7357 if (filedata->section_headers_groups [entry] != NULL)
7358 {
7359 if (entry)
7360 {
7361 static unsigned num_errs = 0;
7362
7363 if (num_errs ++ < 10)
7364 {
7365 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7366 entry, i,
7367 filedata->section_headers_groups [entry]->group_index);
7368 if (num_errs == 10)
7369 warn (_("Further error messages about already contained group sections suppressed\n"));
7370 }
7371 continue;
7372 }
7373 else
7374 {
7375 /* Intel C/C++ compiler may put section 0 in a
7376 section group. We just warn it the first time
7377 and ignore it afterwards. */
7378 static bool warned = false;
7379 if (!warned)
7380 {
7381 error (_("section 0 in group section [%5u]\n"),
7382 filedata->section_headers_groups [entry]->group_index);
7383 warned = true;
7384 }
7385 }
7386 }
7387
7388 filedata->section_headers_groups [entry] = group;
7389
7390 if (do_section_groups)
7391 {
7392 sec = filedata->section_headers + entry;
7393 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
7394 }
7395
7396 g = (struct group_list *) xmalloc (sizeof (struct group_list));
7397 g->section_index = entry;
7398 g->next = group->root;
7399 group->root = g;
7400 }
7401
7402 free (start);
7403
7404 group++;
7405 }
7406 }
7407
7408 free (symtab);
7409 free (strtab);
7410 return true;
7411 }
7412
7413 /* Data used to display dynamic fixups. */
7414
7415 struct ia64_vms_dynfixup
7416 {
7417 bfd_vma needed_ident; /* Library ident number. */
7418 bfd_vma needed; /* Index in the dstrtab of the library name. */
7419 bfd_vma fixup_needed; /* Index of the library. */
7420 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7421 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7422 };
7423
7424 /* Data used to display dynamic relocations. */
7425
7426 struct ia64_vms_dynimgrela
7427 {
7428 bfd_vma img_rela_cnt; /* Number of relocations. */
7429 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7430 };
7431
7432 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7433 library). */
7434
7435 static bool
7436 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7437 struct ia64_vms_dynfixup * fixup,
7438 const char * strtab,
7439 unsigned int strtab_sz)
7440 {
7441 Elf64_External_VMS_IMAGE_FIXUP * imfs;
7442 long i;
7443 const char * lib_name;
7444
7445 imfs = get_data (NULL, filedata,
7446 filedata->dynamic_addr + fixup->fixup_rela_off,
7447 sizeof (*imfs), fixup->fixup_rela_cnt,
7448 _("dynamic section image fixups"));
7449 if (!imfs)
7450 return false;
7451
7452 if (fixup->needed < strtab_sz)
7453 lib_name = strtab + fixup->needed;
7454 else
7455 {
7456 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7457 (unsigned long) fixup->needed);
7458 lib_name = "???";
7459 }
7460
7461 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7462 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7463 printf
7464 (_("Seg Offset Type SymVec DataType\n"));
7465
7466 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7467 {
7468 unsigned int type;
7469 const char *rtype;
7470
7471 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7472 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7473 type = BYTE_GET (imfs [i].type);
7474 rtype = elf_ia64_reloc_type (type);
7475 if (rtype == NULL)
7476 printf (" 0x%08x ", type);
7477 else
7478 printf (" %-32s ", rtype);
7479 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7480 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7481 }
7482
7483 free (imfs);
7484 return true;
7485 }
7486
7487 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7488
7489 static bool
7490 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
7491 {
7492 Elf64_External_VMS_IMAGE_RELA *imrs;
7493 long i;
7494
7495 imrs = get_data (NULL, filedata,
7496 filedata->dynamic_addr + imgrela->img_rela_off,
7497 sizeof (*imrs), imgrela->img_rela_cnt,
7498 _("dynamic section image relocations"));
7499 if (!imrs)
7500 return false;
7501
7502 printf (_("\nImage relocs\n"));
7503 printf
7504 (_("Seg Offset Type Addend Seg Sym Off\n"));
7505
7506 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7507 {
7508 unsigned int type;
7509 const char *rtype;
7510
7511 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7512 printf ("%08" BFD_VMA_FMT "x ",
7513 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7514 type = BYTE_GET (imrs [i].type);
7515 rtype = elf_ia64_reloc_type (type);
7516 if (rtype == NULL)
7517 printf ("0x%08x ", type);
7518 else
7519 printf ("%-31s ", rtype);
7520 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7521 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7522 printf ("%08" BFD_VMA_FMT "x\n",
7523 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7524 }
7525
7526 free (imrs);
7527 return true;
7528 }
7529
7530 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
7531
7532 static bool
7533 process_ia64_vms_dynamic_relocs (Filedata * filedata)
7534 {
7535 struct ia64_vms_dynfixup fixup;
7536 struct ia64_vms_dynimgrela imgrela;
7537 Elf_Internal_Dyn *entry;
7538 bfd_vma strtab_off = 0;
7539 bfd_vma strtab_sz = 0;
7540 char *strtab = NULL;
7541 bool res = true;
7542
7543 memset (&fixup, 0, sizeof (fixup));
7544 memset (&imgrela, 0, sizeof (imgrela));
7545
7546 /* Note: the order of the entries is specified by the OpenVMS specs. */
7547 for (entry = filedata->dynamic_section;
7548 entry < filedata->dynamic_section + filedata->dynamic_nent;
7549 entry++)
7550 {
7551 switch (entry->d_tag)
7552 {
7553 case DT_IA_64_VMS_STRTAB_OFFSET:
7554 strtab_off = entry->d_un.d_val;
7555 break;
7556 case DT_STRSZ:
7557 strtab_sz = entry->d_un.d_val;
7558 if (strtab == NULL)
7559 strtab = get_data (NULL, filedata,
7560 filedata->dynamic_addr + strtab_off,
7561 1, strtab_sz, _("dynamic string section"));
7562 if (strtab == NULL)
7563 strtab_sz = 0;
7564 break;
7565
7566 case DT_IA_64_VMS_NEEDED_IDENT:
7567 fixup.needed_ident = entry->d_un.d_val;
7568 break;
7569 case DT_NEEDED:
7570 fixup.needed = entry->d_un.d_val;
7571 break;
7572 case DT_IA_64_VMS_FIXUP_NEEDED:
7573 fixup.fixup_needed = entry->d_un.d_val;
7574 break;
7575 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7576 fixup.fixup_rela_cnt = entry->d_un.d_val;
7577 break;
7578 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7579 fixup.fixup_rela_off = entry->d_un.d_val;
7580 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7581 res = false;
7582 break;
7583 case DT_IA_64_VMS_IMG_RELA_CNT:
7584 imgrela.img_rela_cnt = entry->d_un.d_val;
7585 break;
7586 case DT_IA_64_VMS_IMG_RELA_OFF:
7587 imgrela.img_rela_off = entry->d_un.d_val;
7588 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7589 res = false;
7590 break;
7591
7592 default:
7593 break;
7594 }
7595 }
7596
7597 free (strtab);
7598
7599 return res;
7600 }
7601
7602 static struct
7603 {
7604 const char * name;
7605 int reloc;
7606 int size;
7607 int rela;
7608 }
7609 dynamic_relocations [] =
7610 {
7611 { "REL", DT_REL, DT_RELSZ, false },
7612 { "RELA", DT_RELA, DT_RELASZ, true },
7613 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7614 };
7615
7616 /* Process the reloc section. */
7617
7618 static bool
7619 process_relocs (Filedata * filedata)
7620 {
7621 unsigned long rel_size;
7622 unsigned long rel_offset;
7623
7624 if (!do_reloc)
7625 return true;
7626
7627 if (do_using_dynamic)
7628 {
7629 int is_rela;
7630 const char * name;
7631 bool has_dynamic_reloc;
7632 unsigned int i;
7633
7634 has_dynamic_reloc = false;
7635
7636 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7637 {
7638 is_rela = dynamic_relocations [i].rela;
7639 name = dynamic_relocations [i].name;
7640 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
7641 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
7642
7643 if (rel_size)
7644 has_dynamic_reloc = true;
7645
7646 if (is_rela == UNKNOWN)
7647 {
7648 if (dynamic_relocations [i].reloc == DT_JMPREL)
7649 switch (filedata->dynamic_info[DT_PLTREL])
7650 {
7651 case DT_REL:
7652 is_rela = false;
7653 break;
7654 case DT_RELA:
7655 is_rela = true;
7656 break;
7657 }
7658 }
7659
7660 if (rel_size)
7661 {
7662 if (filedata->is_separate)
7663 printf
7664 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
7665 filedata->file_name, name, rel_offset, rel_size);
7666 else
7667 printf
7668 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7669 name, rel_offset, rel_size);
7670
7671
7672 dump_relocations (filedata,
7673 offset_from_vma (filedata, rel_offset, rel_size),
7674 rel_size,
7675 filedata->dynamic_symbols,
7676 filedata->num_dynamic_syms,
7677 filedata->dynamic_strings,
7678 filedata->dynamic_strings_length,
7679 is_rela, true /* is_dynamic */);
7680 }
7681 }
7682
7683 if (is_ia64_vms (filedata))
7684 if (process_ia64_vms_dynamic_relocs (filedata))
7685 has_dynamic_reloc = true;
7686
7687 if (! has_dynamic_reloc)
7688 {
7689 if (filedata->is_separate)
7690 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
7691 filedata->file_name);
7692 else
7693 printf (_("\nThere are no dynamic relocations in this file.\n"));
7694 }
7695 }
7696 else
7697 {
7698 Elf_Internal_Shdr * section;
7699 unsigned long i;
7700 bool found = false;
7701
7702 for (i = 0, section = filedata->section_headers;
7703 i < filedata->file_header.e_shnum;
7704 i++, section++)
7705 {
7706 if ( section->sh_type != SHT_RELA
7707 && section->sh_type != SHT_REL)
7708 continue;
7709
7710 rel_offset = section->sh_offset;
7711 rel_size = section->sh_size;
7712
7713 if (rel_size)
7714 {
7715 int is_rela;
7716 unsigned long num_rela;
7717
7718 if (filedata->is_separate)
7719 printf (_("\nIn linked file '%s' relocation section "),
7720 filedata->file_name);
7721 else
7722 printf (_("\nRelocation section "));
7723
7724 if (filedata->string_table == NULL)
7725 printf ("%d", section->sh_name);
7726 else
7727 printf ("'%s'", printable_section_name (filedata, section));
7728
7729 num_rela = rel_size / section->sh_entsize;
7730 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7731 " at offset 0x%lx contains %lu entries:\n",
7732 num_rela),
7733 rel_offset, num_rela);
7734
7735 is_rela = section->sh_type == SHT_RELA;
7736
7737 if (section->sh_link != 0
7738 && section->sh_link < filedata->file_header.e_shnum)
7739 {
7740 Elf_Internal_Shdr * symsec;
7741 Elf_Internal_Sym * symtab;
7742 unsigned long nsyms;
7743 unsigned long strtablen = 0;
7744 char * strtab = NULL;
7745
7746 symsec = filedata->section_headers + section->sh_link;
7747 if (symsec->sh_type != SHT_SYMTAB
7748 && symsec->sh_type != SHT_DYNSYM)
7749 continue;
7750
7751 if (!get_symtab (filedata, symsec,
7752 &symtab, &nsyms, &strtab, &strtablen))
7753 continue;
7754
7755 dump_relocations (filedata, rel_offset, rel_size,
7756 symtab, nsyms, strtab, strtablen,
7757 is_rela,
7758 symsec->sh_type == SHT_DYNSYM);
7759 free (strtab);
7760 free (symtab);
7761 }
7762 else
7763 dump_relocations (filedata, rel_offset, rel_size,
7764 NULL, 0, NULL, 0, is_rela,
7765 false /* is_dynamic */);
7766
7767 found = true;
7768 }
7769 }
7770
7771 if (! found)
7772 {
7773 /* Users sometimes forget the -D option, so try to be helpful. */
7774 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7775 {
7776 if (filedata->dynamic_info[dynamic_relocations [i].size])
7777 {
7778 if (filedata->is_separate)
7779 printf (_("\nThere are no static relocations in linked file '%s'."),
7780 filedata->file_name);
7781 else
7782 printf (_("\nThere are no static relocations in this file."));
7783 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
7784
7785 break;
7786 }
7787 }
7788 if (i == ARRAY_SIZE (dynamic_relocations))
7789 {
7790 if (filedata->is_separate)
7791 printf (_("\nThere are no relocations in linked file '%s'.\n"),
7792 filedata->file_name);
7793 else
7794 printf (_("\nThere are no relocations in this file.\n"));
7795 }
7796 }
7797 }
7798
7799 return true;
7800 }
7801
7802 /* An absolute address consists of a section and an offset. If the
7803 section is NULL, the offset itself is the address, otherwise, the
7804 address equals to LOAD_ADDRESS(section) + offset. */
7805
7806 struct absaddr
7807 {
7808 unsigned short section;
7809 bfd_vma offset;
7810 };
7811
7812 /* Find the nearest symbol at or below ADDR. Returns the symbol
7813 name, if found, and the offset from the symbol to ADDR. */
7814
7815 static void
7816 find_symbol_for_address (Filedata * filedata,
7817 Elf_Internal_Sym * symtab,
7818 unsigned long nsyms,
7819 const char * strtab,
7820 unsigned long strtab_size,
7821 struct absaddr addr,
7822 const char ** symname,
7823 bfd_vma * offset)
7824 {
7825 bfd_vma dist = 0x100000;
7826 Elf_Internal_Sym * sym;
7827 Elf_Internal_Sym * beg;
7828 Elf_Internal_Sym * end;
7829 Elf_Internal_Sym * best = NULL;
7830
7831 REMOVE_ARCH_BITS (addr.offset);
7832 beg = symtab;
7833 end = symtab + nsyms;
7834
7835 while (beg < end)
7836 {
7837 bfd_vma value;
7838
7839 sym = beg + (end - beg) / 2;
7840
7841 value = sym->st_value;
7842 REMOVE_ARCH_BITS (value);
7843
7844 if (sym->st_name != 0
7845 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
7846 && addr.offset >= value
7847 && addr.offset - value < dist)
7848 {
7849 best = sym;
7850 dist = addr.offset - value;
7851 if (!dist)
7852 break;
7853 }
7854
7855 if (addr.offset < value)
7856 end = sym;
7857 else
7858 beg = sym + 1;
7859 }
7860
7861 if (best)
7862 {
7863 *symname = (best->st_name >= strtab_size
7864 ? _("<corrupt>") : strtab + best->st_name);
7865 *offset = dist;
7866 return;
7867 }
7868
7869 *symname = NULL;
7870 *offset = addr.offset;
7871 }
7872
7873 static /* signed */ int
7874 symcmp (const void *p, const void *q)
7875 {
7876 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
7877 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
7878
7879 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
7880 }
7881
7882 /* Process the unwind section. */
7883
7884 #include "unwind-ia64.h"
7885
7886 struct ia64_unw_table_entry
7887 {
7888 struct absaddr start;
7889 struct absaddr end;
7890 struct absaddr info;
7891 };
7892
7893 struct ia64_unw_aux_info
7894 {
7895 struct ia64_unw_table_entry * table; /* Unwind table. */
7896 unsigned long table_len; /* Length of unwind table. */
7897 unsigned char * info; /* Unwind info. */
7898 unsigned long info_size; /* Size of unwind info. */
7899 bfd_vma info_addr; /* Starting address of unwind info. */
7900 bfd_vma seg_base; /* Starting address of segment. */
7901 Elf_Internal_Sym * symtab; /* The symbol table. */
7902 unsigned long nsyms; /* Number of symbols. */
7903 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
7904 unsigned long nfuns; /* Number of entries in funtab. */
7905 char * strtab; /* The string table. */
7906 unsigned long strtab_size; /* Size of string table. */
7907 };
7908
7909 static bool
7910 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
7911 {
7912 struct ia64_unw_table_entry * tp;
7913 unsigned long j, nfuns;
7914 int in_body;
7915 bool res = true;
7916
7917 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7918 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7919 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7920 aux->funtab[nfuns++] = aux->symtab[j];
7921 aux->nfuns = nfuns;
7922 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7923
7924 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7925 {
7926 bfd_vma stamp;
7927 bfd_vma offset;
7928 const unsigned char * dp;
7929 const unsigned char * head;
7930 const unsigned char * end;
7931 const char * procname;
7932
7933 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
7934 aux->strtab_size, tp->start, &procname, &offset);
7935
7936 fputs ("\n<", stdout);
7937
7938 if (procname)
7939 {
7940 fputs (procname, stdout);
7941
7942 if (offset)
7943 printf ("+%lx", (unsigned long) offset);
7944 }
7945
7946 fputs (">: [", stdout);
7947 print_vma (tp->start.offset, PREFIX_HEX);
7948 fputc ('-', stdout);
7949 print_vma (tp->end.offset, PREFIX_HEX);
7950 printf ("], info at +0x%lx\n",
7951 (unsigned long) (tp->info.offset - aux->seg_base));
7952
7953 /* PR 17531: file: 86232b32. */
7954 if (aux->info == NULL)
7955 continue;
7956
7957 offset = tp->info.offset;
7958 if (tp->info.section)
7959 {
7960 if (tp->info.section >= filedata->file_header.e_shnum)
7961 {
7962 warn (_("Invalid section %u in table entry %ld\n"),
7963 tp->info.section, (long) (tp - aux->table));
7964 res = false;
7965 continue;
7966 }
7967 offset += filedata->section_headers[tp->info.section].sh_addr;
7968 }
7969 offset -= aux->info_addr;
7970 /* PR 17531: file: 0997b4d1. */
7971 if (offset >= aux->info_size
7972 || aux->info_size - offset < 8)
7973 {
7974 warn (_("Invalid offset %lx in table entry %ld\n"),
7975 (long) tp->info.offset, (long) (tp - aux->table));
7976 res = false;
7977 continue;
7978 }
7979
7980 head = aux->info + offset;
7981 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
7982
7983 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
7984 (unsigned) UNW_VER (stamp),
7985 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
7986 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
7987 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
7988 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
7989
7990 if (UNW_VER (stamp) != 1)
7991 {
7992 printf (_("\tUnknown version.\n"));
7993 continue;
7994 }
7995
7996 in_body = 0;
7997 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
7998 /* PR 17531: file: 16ceda89. */
7999 if (end > aux->info + aux->info_size)
8000 end = aux->info + aux->info_size;
8001 for (dp = head + 8; dp < end;)
8002 dp = unw_decode (dp, in_body, & in_body, end);
8003 }
8004
8005 free (aux->funtab);
8006
8007 return res;
8008 }
8009
8010 static bool
8011 slurp_ia64_unwind_table (Filedata * filedata,
8012 struct ia64_unw_aux_info * aux,
8013 Elf_Internal_Shdr * sec)
8014 {
8015 unsigned long size, nrelas, i;
8016 Elf_Internal_Phdr * seg;
8017 struct ia64_unw_table_entry * tep;
8018 Elf_Internal_Shdr * relsec;
8019 Elf_Internal_Rela * rela;
8020 Elf_Internal_Rela * rp;
8021 unsigned char * table;
8022 unsigned char * tp;
8023 Elf_Internal_Sym * sym;
8024 const char * relname;
8025
8026 aux->table_len = 0;
8027
8028 /* First, find the starting address of the segment that includes
8029 this section: */
8030
8031 if (filedata->file_header.e_phnum)
8032 {
8033 if (! get_program_headers (filedata))
8034 return false;
8035
8036 for (seg = filedata->program_headers;
8037 seg < filedata->program_headers + filedata->file_header.e_phnum;
8038 ++seg)
8039 {
8040 if (seg->p_type != PT_LOAD)
8041 continue;
8042
8043 if (sec->sh_addr >= seg->p_vaddr
8044 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8045 {
8046 aux->seg_base = seg->p_vaddr;
8047 break;
8048 }
8049 }
8050 }
8051
8052 /* Second, build the unwind table from the contents of the unwind section: */
8053 size = sec->sh_size;
8054 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8055 _("unwind table"));
8056 if (!table)
8057 return false;
8058
8059 aux->table_len = size / (3 * eh_addr_size);
8060 aux->table = (struct ia64_unw_table_entry *)
8061 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8062 tep = aux->table;
8063
8064 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8065 {
8066 tep->start.section = SHN_UNDEF;
8067 tep->end.section = SHN_UNDEF;
8068 tep->info.section = SHN_UNDEF;
8069 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8070 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8071 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8072 tep->start.offset += aux->seg_base;
8073 tep->end.offset += aux->seg_base;
8074 tep->info.offset += aux->seg_base;
8075 }
8076 free (table);
8077
8078 /* Third, apply any relocations to the unwind table: */
8079 for (relsec = filedata->section_headers;
8080 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8081 ++relsec)
8082 {
8083 if (relsec->sh_type != SHT_RELA
8084 || relsec->sh_info >= filedata->file_header.e_shnum
8085 || filedata->section_headers + relsec->sh_info != sec)
8086 continue;
8087
8088 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8089 & rela, & nrelas))
8090 {
8091 free (aux->table);
8092 aux->table = NULL;
8093 aux->table_len = 0;
8094 return false;
8095 }
8096
8097 for (rp = rela; rp < rela + nrelas; ++rp)
8098 {
8099 unsigned int sym_ndx;
8100 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8101 relname = elf_ia64_reloc_type (r_type);
8102
8103 /* PR 17531: file: 9fa67536. */
8104 if (relname == NULL)
8105 {
8106 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8107 continue;
8108 }
8109
8110 if (! const_strneq (relname, "R_IA64_SEGREL"))
8111 {
8112 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8113 continue;
8114 }
8115
8116 i = rp->r_offset / (3 * eh_addr_size);
8117
8118 /* PR 17531: file: 5bc8d9bf. */
8119 if (i >= aux->table_len)
8120 {
8121 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8122 continue;
8123 }
8124
8125 sym_ndx = get_reloc_symindex (rp->r_info);
8126 if (sym_ndx >= aux->nsyms)
8127 {
8128 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8129 sym_ndx);
8130 continue;
8131 }
8132 sym = aux->symtab + sym_ndx;
8133
8134 switch (rp->r_offset / eh_addr_size % 3)
8135 {
8136 case 0:
8137 aux->table[i].start.section = sym->st_shndx;
8138 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8139 break;
8140 case 1:
8141 aux->table[i].end.section = sym->st_shndx;
8142 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8143 break;
8144 case 2:
8145 aux->table[i].info.section = sym->st_shndx;
8146 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8147 break;
8148 default:
8149 break;
8150 }
8151 }
8152
8153 free (rela);
8154 }
8155
8156 return true;
8157 }
8158
8159 static bool
8160 ia64_process_unwind (Filedata * filedata)
8161 {
8162 Elf_Internal_Shdr * sec;
8163 Elf_Internal_Shdr * unwsec = NULL;
8164 unsigned long i, unwcount = 0, unwstart = 0;
8165 struct ia64_unw_aux_info aux;
8166 bool res = true;
8167
8168 memset (& aux, 0, sizeof (aux));
8169
8170 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8171 {
8172 if (sec->sh_type == SHT_SYMTAB)
8173 {
8174 if (aux.symtab)
8175 {
8176 error (_("Multiple symbol tables encountered\n"));
8177 free (aux.symtab);
8178 aux.symtab = NULL;
8179 free (aux.strtab);
8180 aux.strtab = NULL;
8181 }
8182 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8183 &aux.strtab, &aux.strtab_size))
8184 return false;
8185 }
8186 else if (sec->sh_type == SHT_IA_64_UNWIND)
8187 unwcount++;
8188 }
8189
8190 if (!unwcount)
8191 printf (_("\nThere are no unwind sections in this file.\n"));
8192
8193 while (unwcount-- > 0)
8194 {
8195 char * suffix;
8196 size_t len, len2;
8197
8198 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8199 i < filedata->file_header.e_shnum; ++i, ++sec)
8200 if (sec->sh_type == SHT_IA_64_UNWIND)
8201 {
8202 unwsec = sec;
8203 break;
8204 }
8205 /* We have already counted the number of SHT_IA64_UNWIND
8206 sections so the loop above should never fail. */
8207 assert (unwsec != NULL);
8208
8209 unwstart = i + 1;
8210 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8211
8212 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8213 {
8214 /* We need to find which section group it is in. */
8215 struct group_list * g;
8216
8217 if (filedata->section_headers_groups == NULL
8218 || filedata->section_headers_groups[i] == NULL)
8219 i = filedata->file_header.e_shnum;
8220 else
8221 {
8222 g = filedata->section_headers_groups[i]->root;
8223
8224 for (; g != NULL; g = g->next)
8225 {
8226 sec = filedata->section_headers + g->section_index;
8227
8228 if (SECTION_NAME_VALID (sec)
8229 && streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
8230 break;
8231 }
8232
8233 if (g == NULL)
8234 i = filedata->file_header.e_shnum;
8235 }
8236 }
8237 else if (SECTION_NAME_VALID (unwsec)
8238 && strneq (SECTION_NAME (unwsec),
8239 ELF_STRING_ia64_unwind_once, len))
8240 {
8241 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8242 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8243 suffix = SECTION_NAME (unwsec) + len;
8244 for (i = 0, sec = filedata->section_headers;
8245 i < filedata->file_header.e_shnum;
8246 ++i, ++sec)
8247 if (SECTION_NAME_VALID (sec)
8248 && strneq (SECTION_NAME (sec),
8249 ELF_STRING_ia64_unwind_info_once, len2)
8250 && streq (SECTION_NAME (sec) + len2, suffix))
8251 break;
8252 }
8253 else
8254 {
8255 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8256 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8257 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8258 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8259 suffix = "";
8260 if (SECTION_NAME_VALID (unwsec)
8261 && strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
8262 suffix = SECTION_NAME (unwsec) + len;
8263 for (i = 0, sec = filedata->section_headers;
8264 i < filedata->file_header.e_shnum;
8265 ++i, ++sec)
8266 if (SECTION_NAME_VALID (sec)
8267 && strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
8268 && streq (SECTION_NAME (sec) + len2, suffix))
8269 break;
8270 }
8271
8272 if (i == filedata->file_header.e_shnum)
8273 {
8274 printf (_("\nCould not find unwind info section for "));
8275
8276 if (filedata->string_table == NULL)
8277 printf ("%d", unwsec->sh_name);
8278 else
8279 printf ("'%s'", printable_section_name (filedata, unwsec));
8280 }
8281 else
8282 {
8283 aux.info_addr = sec->sh_addr;
8284 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
8285 sec->sh_size,
8286 _("unwind info"));
8287 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
8288
8289 printf (_("\nUnwind section "));
8290
8291 if (filedata->string_table == NULL)
8292 printf ("%d", unwsec->sh_name);
8293 else
8294 printf ("'%s'", printable_section_name (filedata, unwsec));
8295
8296 printf (_(" at offset 0x%lx contains %lu entries:\n"),
8297 (unsigned long) unwsec->sh_offset,
8298 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
8299
8300 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
8301 && aux.table_len > 0)
8302 dump_ia64_unwind (filedata, & aux);
8303
8304 free ((char *) aux.table);
8305 free ((char *) aux.info);
8306 aux.table = NULL;
8307 aux.info = NULL;
8308 }
8309 }
8310
8311 free (aux.symtab);
8312 free ((char *) aux.strtab);
8313
8314 return res;
8315 }
8316
8317 struct hppa_unw_table_entry
8318 {
8319 struct absaddr start;
8320 struct absaddr end;
8321 unsigned int Cannot_unwind:1; /* 0 */
8322 unsigned int Millicode:1; /* 1 */
8323 unsigned int Millicode_save_sr0:1; /* 2 */
8324 unsigned int Region_description:2; /* 3..4 */
8325 unsigned int reserved1:1; /* 5 */
8326 unsigned int Entry_SR:1; /* 6 */
8327 unsigned int Entry_FR:4; /* Number saved 7..10 */
8328 unsigned int Entry_GR:5; /* Number saved 11..15 */
8329 unsigned int Args_stored:1; /* 16 */
8330 unsigned int Variable_Frame:1; /* 17 */
8331 unsigned int Separate_Package_Body:1; /* 18 */
8332 unsigned int Frame_Extension_Millicode:1; /* 19 */
8333 unsigned int Stack_Overflow_Check:1; /* 20 */
8334 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8335 unsigned int Ada_Region:1; /* 22 */
8336 unsigned int cxx_info:1; /* 23 */
8337 unsigned int cxx_try_catch:1; /* 24 */
8338 unsigned int sched_entry_seq:1; /* 25 */
8339 unsigned int reserved2:1; /* 26 */
8340 unsigned int Save_SP:1; /* 27 */
8341 unsigned int Save_RP:1; /* 28 */
8342 unsigned int Save_MRP_in_frame:1; /* 29 */
8343 unsigned int extn_ptr_defined:1; /* 30 */
8344 unsigned int Cleanup_defined:1; /* 31 */
8345
8346 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8347 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8348 unsigned int Large_frame:1; /* 2 */
8349 unsigned int Pseudo_SP_Set:1; /* 3 */
8350 unsigned int reserved4:1; /* 4 */
8351 unsigned int Total_frame_size:27; /* 5..31 */
8352 };
8353
8354 struct hppa_unw_aux_info
8355 {
8356 struct hppa_unw_table_entry * table; /* Unwind table. */
8357 unsigned long table_len; /* Length of unwind table. */
8358 bfd_vma seg_base; /* Starting address of segment. */
8359 Elf_Internal_Sym * symtab; /* The symbol table. */
8360 unsigned long nsyms; /* Number of symbols. */
8361 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8362 unsigned long nfuns; /* Number of entries in funtab. */
8363 char * strtab; /* The string table. */
8364 unsigned long strtab_size; /* Size of string table. */
8365 };
8366
8367 static bool
8368 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
8369 {
8370 struct hppa_unw_table_entry * tp;
8371 unsigned long j, nfuns;
8372 bool res = true;
8373
8374 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8375 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8376 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8377 aux->funtab[nfuns++] = aux->symtab[j];
8378 aux->nfuns = nfuns;
8379 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8380
8381 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8382 {
8383 bfd_vma offset;
8384 const char * procname;
8385
8386 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8387 aux->strtab_size, tp->start, &procname,
8388 &offset);
8389
8390 fputs ("\n<", stdout);
8391
8392 if (procname)
8393 {
8394 fputs (procname, stdout);
8395
8396 if (offset)
8397 printf ("+%lx", (unsigned long) offset);
8398 }
8399
8400 fputs (">: [", stdout);
8401 print_vma (tp->start.offset, PREFIX_HEX);
8402 fputc ('-', stdout);
8403 print_vma (tp->end.offset, PREFIX_HEX);
8404 printf ("]\n\t");
8405
8406 #define PF(_m) if (tp->_m) printf (#_m " ");
8407 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8408 PF(Cannot_unwind);
8409 PF(Millicode);
8410 PF(Millicode_save_sr0);
8411 /* PV(Region_description); */
8412 PF(Entry_SR);
8413 PV(Entry_FR);
8414 PV(Entry_GR);
8415 PF(Args_stored);
8416 PF(Variable_Frame);
8417 PF(Separate_Package_Body);
8418 PF(Frame_Extension_Millicode);
8419 PF(Stack_Overflow_Check);
8420 PF(Two_Instruction_SP_Increment);
8421 PF(Ada_Region);
8422 PF(cxx_info);
8423 PF(cxx_try_catch);
8424 PF(sched_entry_seq);
8425 PF(Save_SP);
8426 PF(Save_RP);
8427 PF(Save_MRP_in_frame);
8428 PF(extn_ptr_defined);
8429 PF(Cleanup_defined);
8430 PF(MPE_XL_interrupt_marker);
8431 PF(HP_UX_interrupt_marker);
8432 PF(Large_frame);
8433 PF(Pseudo_SP_Set);
8434 PV(Total_frame_size);
8435 #undef PF
8436 #undef PV
8437 }
8438
8439 printf ("\n");
8440
8441 free (aux->funtab);
8442
8443 return res;
8444 }
8445
8446 static bool
8447 slurp_hppa_unwind_table (Filedata * filedata,
8448 struct hppa_unw_aux_info * aux,
8449 Elf_Internal_Shdr * sec)
8450 {
8451 unsigned long size, unw_ent_size, nentries, nrelas, i;
8452 Elf_Internal_Phdr * seg;
8453 struct hppa_unw_table_entry * tep;
8454 Elf_Internal_Shdr * relsec;
8455 Elf_Internal_Rela * rela;
8456 Elf_Internal_Rela * rp;
8457 unsigned char * table;
8458 unsigned char * tp;
8459 Elf_Internal_Sym * sym;
8460 const char * relname;
8461
8462 /* First, find the starting address of the segment that includes
8463 this section. */
8464 if (filedata->file_header.e_phnum)
8465 {
8466 if (! get_program_headers (filedata))
8467 return false;
8468
8469 for (seg = filedata->program_headers;
8470 seg < filedata->program_headers + filedata->file_header.e_phnum;
8471 ++seg)
8472 {
8473 if (seg->p_type != PT_LOAD)
8474 continue;
8475
8476 if (sec->sh_addr >= seg->p_vaddr
8477 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8478 {
8479 aux->seg_base = seg->p_vaddr;
8480 break;
8481 }
8482 }
8483 }
8484
8485 /* Second, build the unwind table from the contents of the unwind
8486 section. */
8487 size = sec->sh_size;
8488 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8489 _("unwind table"));
8490 if (!table)
8491 return false;
8492
8493 unw_ent_size = 16;
8494 nentries = size / unw_ent_size;
8495 size = unw_ent_size * nentries;
8496
8497 aux->table_len = nentries;
8498 tep = aux->table = (struct hppa_unw_table_entry *)
8499 xcmalloc (nentries, sizeof (aux->table[0]));
8500
8501 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
8502 {
8503 unsigned int tmp1, tmp2;
8504
8505 tep->start.section = SHN_UNDEF;
8506 tep->end.section = SHN_UNDEF;
8507
8508 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8509 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8510 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8511 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8512
8513 tep->start.offset += aux->seg_base;
8514 tep->end.offset += aux->seg_base;
8515
8516 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8517 tep->Millicode = (tmp1 >> 30) & 0x1;
8518 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8519 tep->Region_description = (tmp1 >> 27) & 0x3;
8520 tep->reserved1 = (tmp1 >> 26) & 0x1;
8521 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8522 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8523 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8524 tep->Args_stored = (tmp1 >> 15) & 0x1;
8525 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8526 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8527 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8528 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8529 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8530 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8531 tep->cxx_info = (tmp1 >> 8) & 0x1;
8532 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8533 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8534 tep->reserved2 = (tmp1 >> 5) & 0x1;
8535 tep->Save_SP = (tmp1 >> 4) & 0x1;
8536 tep->Save_RP = (tmp1 >> 3) & 0x1;
8537 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8538 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8539 tep->Cleanup_defined = tmp1 & 0x1;
8540
8541 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8542 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8543 tep->Large_frame = (tmp2 >> 29) & 0x1;
8544 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8545 tep->reserved4 = (tmp2 >> 27) & 0x1;
8546 tep->Total_frame_size = tmp2 & 0x7ffffff;
8547 }
8548 free (table);
8549
8550 /* Third, apply any relocations to the unwind table. */
8551 for (relsec = filedata->section_headers;
8552 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8553 ++relsec)
8554 {
8555 if (relsec->sh_type != SHT_RELA
8556 || relsec->sh_info >= filedata->file_header.e_shnum
8557 || filedata->section_headers + relsec->sh_info != sec)
8558 continue;
8559
8560 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8561 & rela, & nrelas))
8562 return false;
8563
8564 for (rp = rela; rp < rela + nrelas; ++rp)
8565 {
8566 unsigned int sym_ndx;
8567 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8568 relname = elf_hppa_reloc_type (r_type);
8569
8570 if (relname == NULL)
8571 {
8572 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8573 continue;
8574 }
8575
8576 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
8577 if (! const_strneq (relname, "R_PARISC_SEGREL"))
8578 {
8579 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8580 continue;
8581 }
8582
8583 i = rp->r_offset / unw_ent_size;
8584 if (i >= aux->table_len)
8585 {
8586 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8587 continue;
8588 }
8589
8590 sym_ndx = get_reloc_symindex (rp->r_info);
8591 if (sym_ndx >= aux->nsyms)
8592 {
8593 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8594 sym_ndx);
8595 continue;
8596 }
8597 sym = aux->symtab + sym_ndx;
8598
8599 switch ((rp->r_offset % unw_ent_size) / 4)
8600 {
8601 case 0:
8602 aux->table[i].start.section = sym->st_shndx;
8603 aux->table[i].start.offset = sym->st_value + rp->r_addend;
8604 break;
8605 case 1:
8606 aux->table[i].end.section = sym->st_shndx;
8607 aux->table[i].end.offset = sym->st_value + rp->r_addend;
8608 break;
8609 default:
8610 break;
8611 }
8612 }
8613
8614 free (rela);
8615 }
8616
8617 return true;
8618 }
8619
8620 static bool
8621 hppa_process_unwind (Filedata * filedata)
8622 {
8623 struct hppa_unw_aux_info aux;
8624 Elf_Internal_Shdr * unwsec = NULL;
8625 Elf_Internal_Shdr * sec;
8626 unsigned long i;
8627 bool res = true;
8628
8629 if (filedata->string_table == NULL)
8630 return false;
8631
8632 memset (& aux, 0, sizeof (aux));
8633
8634 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8635 {
8636 if (sec->sh_type == SHT_SYMTAB)
8637 {
8638 if (aux.symtab)
8639 {
8640 error (_("Multiple symbol tables encountered\n"));
8641 free (aux.symtab);
8642 aux.symtab = NULL;
8643 free (aux.strtab);
8644 aux.strtab = NULL;
8645 }
8646 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8647 &aux.strtab, &aux.strtab_size))
8648 return false;
8649 }
8650 else if (SECTION_NAME_VALID (sec)
8651 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
8652 unwsec = sec;
8653 }
8654
8655 if (!unwsec)
8656 printf (_("\nThere are no unwind sections in this file.\n"));
8657
8658 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8659 {
8660 if (SECTION_NAME_VALID (sec)
8661 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
8662 {
8663 unsigned long num_unwind = sec->sh_size / 16;
8664
8665 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8666 "contains %lu entry:\n",
8667 "\nUnwind section '%s' at offset 0x%lx "
8668 "contains %lu entries:\n",
8669 num_unwind),
8670 printable_section_name (filedata, sec),
8671 (unsigned long) sec->sh_offset,
8672 num_unwind);
8673
8674 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8675 res = false;
8676
8677 if (res && aux.table_len > 0)
8678 {
8679 if (! dump_hppa_unwind (filedata, &aux))
8680 res = false;
8681 }
8682
8683 free ((char *) aux.table);
8684 aux.table = NULL;
8685 }
8686 }
8687
8688 free (aux.symtab);
8689 free ((char *) aux.strtab);
8690
8691 return res;
8692 }
8693
8694 struct arm_section
8695 {
8696 unsigned char * data; /* The unwind data. */
8697 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
8698 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
8699 unsigned long nrelas; /* The number of relocations. */
8700 unsigned int rel_type; /* REL or RELA ? */
8701 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
8702 };
8703
8704 struct arm_unw_aux_info
8705 {
8706 Filedata * filedata; /* The file containing the unwind sections. */
8707 Elf_Internal_Sym * symtab; /* The file's symbol table. */
8708 unsigned long nsyms; /* Number of symbols. */
8709 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8710 unsigned long nfuns; /* Number of these symbols. */
8711 char * strtab; /* The file's string table. */
8712 unsigned long strtab_size; /* Size of string table. */
8713 };
8714
8715 static const char *
8716 arm_print_vma_and_name (Filedata * filedata,
8717 struct arm_unw_aux_info * aux,
8718 bfd_vma fn,
8719 struct absaddr addr)
8720 {
8721 const char *procname;
8722 bfd_vma sym_offset;
8723
8724 if (addr.section == SHN_UNDEF)
8725 addr.offset = fn;
8726
8727 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8728 aux->strtab_size, addr, &procname,
8729 &sym_offset);
8730
8731 print_vma (fn, PREFIX_HEX);
8732
8733 if (procname)
8734 {
8735 fputs (" <", stdout);
8736 fputs (procname, stdout);
8737
8738 if (sym_offset)
8739 printf ("+0x%lx", (unsigned long) sym_offset);
8740 fputc ('>', stdout);
8741 }
8742
8743 return procname;
8744 }
8745
8746 static void
8747 arm_free_section (struct arm_section *arm_sec)
8748 {
8749 free (arm_sec->data);
8750 free (arm_sec->rela);
8751 }
8752
8753 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8754 cached section and install SEC instead.
8755 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8756 and return its valued in * WORDP, relocating if necessary.
8757 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
8758 relocation's offset in ADDR.
8759 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
8760 into the string table of the symbol associated with the reloc. If no
8761 reloc was applied store -1 there.
8762 5) Return TRUE upon success, FALSE otherwise. */
8763
8764 static bool
8765 get_unwind_section_word (Filedata * filedata,
8766 struct arm_unw_aux_info * aux,
8767 struct arm_section * arm_sec,
8768 Elf_Internal_Shdr * sec,
8769 bfd_vma word_offset,
8770 unsigned int * wordp,
8771 struct absaddr * addr,
8772 bfd_vma * sym_name)
8773 {
8774 Elf_Internal_Rela *rp;
8775 Elf_Internal_Sym *sym;
8776 const char * relname;
8777 unsigned int word;
8778 bool wrapped;
8779
8780 if (sec == NULL || arm_sec == NULL)
8781 return false;
8782
8783 addr->section = SHN_UNDEF;
8784 addr->offset = 0;
8785
8786 if (sym_name != NULL)
8787 *sym_name = (bfd_vma) -1;
8788
8789 /* If necessary, update the section cache. */
8790 if (sec != arm_sec->sec)
8791 {
8792 Elf_Internal_Shdr *relsec;
8793
8794 arm_free_section (arm_sec);
8795
8796 arm_sec->sec = sec;
8797 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
8798 sec->sh_size, _("unwind data"));
8799 arm_sec->rela = NULL;
8800 arm_sec->nrelas = 0;
8801
8802 for (relsec = filedata->section_headers;
8803 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8804 ++relsec)
8805 {
8806 if (relsec->sh_info >= filedata->file_header.e_shnum
8807 || filedata->section_headers + relsec->sh_info != sec
8808 /* PR 15745: Check the section type as well. */
8809 || (relsec->sh_type != SHT_REL
8810 && relsec->sh_type != SHT_RELA))
8811 continue;
8812
8813 arm_sec->rel_type = relsec->sh_type;
8814 if (relsec->sh_type == SHT_REL)
8815 {
8816 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
8817 relsec->sh_size,
8818 & arm_sec->rela, & arm_sec->nrelas))
8819 return false;
8820 }
8821 else /* relsec->sh_type == SHT_RELA */
8822 {
8823 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
8824 relsec->sh_size,
8825 & arm_sec->rela, & arm_sec->nrelas))
8826 return false;
8827 }
8828 break;
8829 }
8830
8831 arm_sec->next_rela = arm_sec->rela;
8832 }
8833
8834 /* If there is no unwind data we can do nothing. */
8835 if (arm_sec->data == NULL)
8836 return false;
8837
8838 /* If the offset is invalid then fail. */
8839 if (/* PR 21343 *//* PR 18879 */
8840 sec->sh_size < 4
8841 || word_offset > (sec->sh_size - 4)
8842 || ((bfd_signed_vma) word_offset) < 0)
8843 return false;
8844
8845 /* Get the word at the required offset. */
8846 word = byte_get (arm_sec->data + word_offset, 4);
8847
8848 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
8849 if (arm_sec->rela == NULL)
8850 {
8851 * wordp = word;
8852 return true;
8853 }
8854
8855 /* Look through the relocs to find the one that applies to the provided offset. */
8856 wrapped = false;
8857 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
8858 {
8859 bfd_vma prelval, offset;
8860
8861 if (rp->r_offset > word_offset && !wrapped)
8862 {
8863 rp = arm_sec->rela;
8864 wrapped = true;
8865 }
8866 if (rp->r_offset > word_offset)
8867 break;
8868
8869 if (rp->r_offset & 3)
8870 {
8871 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
8872 (unsigned long) rp->r_offset);
8873 continue;
8874 }
8875
8876 if (rp->r_offset < word_offset)
8877 continue;
8878
8879 /* PR 17531: file: 027-161405-0.004 */
8880 if (aux->symtab == NULL)
8881 continue;
8882
8883 if (arm_sec->rel_type == SHT_REL)
8884 {
8885 offset = word & 0x7fffffff;
8886 if (offset & 0x40000000)
8887 offset |= ~ (bfd_vma) 0x7fffffff;
8888 }
8889 else if (arm_sec->rel_type == SHT_RELA)
8890 offset = rp->r_addend;
8891 else
8892 {
8893 error (_("Unknown section relocation type %d encountered\n"),
8894 arm_sec->rel_type);
8895 break;
8896 }
8897
8898 /* PR 17531 file: 027-1241568-0.004. */
8899 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
8900 {
8901 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
8902 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
8903 break;
8904 }
8905
8906 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
8907 offset += sym->st_value;
8908 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
8909
8910 /* Check that we are processing the expected reloc type. */
8911 if (filedata->file_header.e_machine == EM_ARM)
8912 {
8913 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
8914 if (relname == NULL)
8915 {
8916 warn (_("Skipping unknown ARM relocation type: %d\n"),
8917 (int) ELF32_R_TYPE (rp->r_info));
8918 continue;
8919 }
8920
8921 if (streq (relname, "R_ARM_NONE"))
8922 continue;
8923
8924 if (! streq (relname, "R_ARM_PREL31"))
8925 {
8926 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
8927 continue;
8928 }
8929 }
8930 else if (filedata->file_header.e_machine == EM_TI_C6000)
8931 {
8932 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
8933 if (relname == NULL)
8934 {
8935 warn (_("Skipping unknown C6000 relocation type: %d\n"),
8936 (int) ELF32_R_TYPE (rp->r_info));
8937 continue;
8938 }
8939
8940 if (streq (relname, "R_C6000_NONE"))
8941 continue;
8942
8943 if (! streq (relname, "R_C6000_PREL31"))
8944 {
8945 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
8946 continue;
8947 }
8948
8949 prelval >>= 1;
8950 }
8951 else
8952 {
8953 /* This function currently only supports ARM and TI unwinders. */
8954 warn (_("Only TI and ARM unwinders are currently supported\n"));
8955 break;
8956 }
8957
8958 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
8959 addr->section = sym->st_shndx;
8960 addr->offset = offset;
8961
8962 if (sym_name)
8963 * sym_name = sym->st_name;
8964 break;
8965 }
8966
8967 *wordp = word;
8968 arm_sec->next_rela = rp;
8969
8970 return true;
8971 }
8972
8973 static const char *tic6x_unwind_regnames[16] =
8974 {
8975 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
8976 "A14", "A13", "A12", "A11", "A10",
8977 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
8978 };
8979
8980 static void
8981 decode_tic6x_unwind_regmask (unsigned int mask)
8982 {
8983 int i;
8984
8985 for (i = 12; mask; mask >>= 1, i--)
8986 {
8987 if (mask & 1)
8988 {
8989 fputs (tic6x_unwind_regnames[i], stdout);
8990 if (mask > 1)
8991 fputs (", ", stdout);
8992 }
8993 }
8994 }
8995
8996 #define ADVANCE \
8997 if (remaining == 0 && more_words) \
8998 { \
8999 data_offset += 4; \
9000 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9001 data_offset, & word, & addr, NULL)) \
9002 return false; \
9003 remaining = 4; \
9004 more_words--; \
9005 } \
9006
9007 #define GET_OP(OP) \
9008 ADVANCE; \
9009 if (remaining) \
9010 { \
9011 remaining--; \
9012 (OP) = word >> 24; \
9013 word <<= 8; \
9014 } \
9015 else \
9016 { \
9017 printf (_("[Truncated opcode]\n")); \
9018 return false; \
9019 } \
9020 printf ("0x%02x ", OP)
9021
9022 static bool
9023 decode_arm_unwind_bytecode (Filedata * filedata,
9024 struct arm_unw_aux_info * aux,
9025 unsigned int word,
9026 unsigned int remaining,
9027 unsigned int more_words,
9028 bfd_vma data_offset,
9029 Elf_Internal_Shdr * data_sec,
9030 struct arm_section * data_arm_sec)
9031 {
9032 struct absaddr addr;
9033 bool res = true;
9034
9035 /* Decode the unwinding instructions. */
9036 while (1)
9037 {
9038 unsigned int op, op2;
9039
9040 ADVANCE;
9041 if (remaining == 0)
9042 break;
9043 remaining--;
9044 op = word >> 24;
9045 word <<= 8;
9046
9047 printf (" 0x%02x ", op);
9048
9049 if ((op & 0xc0) == 0x00)
9050 {
9051 int offset = ((op & 0x3f) << 2) + 4;
9052
9053 printf (" vsp = vsp + %d", offset);
9054 }
9055 else if ((op & 0xc0) == 0x40)
9056 {
9057 int offset = ((op & 0x3f) << 2) + 4;
9058
9059 printf (" vsp = vsp - %d", offset);
9060 }
9061 else if ((op & 0xf0) == 0x80)
9062 {
9063 GET_OP (op2);
9064 if (op == 0x80 && op2 == 0)
9065 printf (_("Refuse to unwind"));
9066 else
9067 {
9068 unsigned int mask = ((op & 0x0f) << 8) | op2;
9069 bool first = true;
9070 int i;
9071
9072 printf ("pop {");
9073 for (i = 0; i < 12; i++)
9074 if (mask & (1 << i))
9075 {
9076 if (first)
9077 first = false;
9078 else
9079 printf (", ");
9080 printf ("r%d", 4 + i);
9081 }
9082 printf ("}");
9083 }
9084 }
9085 else if ((op & 0xf0) == 0x90)
9086 {
9087 if (op == 0x9d || op == 0x9f)
9088 printf (_(" [Reserved]"));
9089 else
9090 printf (" vsp = r%d", op & 0x0f);
9091 }
9092 else if ((op & 0xf0) == 0xa0)
9093 {
9094 int end = 4 + (op & 0x07);
9095 bool first = true;
9096 int i;
9097
9098 printf (" pop {");
9099 for (i = 4; i <= end; i++)
9100 {
9101 if (first)
9102 first = false;
9103 else
9104 printf (", ");
9105 printf ("r%d", i);
9106 }
9107 if (op & 0x08)
9108 {
9109 if (!first)
9110 printf (", ");
9111 printf ("r14");
9112 }
9113 printf ("}");
9114 }
9115 else if (op == 0xb0)
9116 printf (_(" finish"));
9117 else if (op == 0xb1)
9118 {
9119 GET_OP (op2);
9120 if (op2 == 0 || (op2 & 0xf0) != 0)
9121 printf (_("[Spare]"));
9122 else
9123 {
9124 unsigned int mask = op2 & 0x0f;
9125 bool first = true;
9126 int i;
9127
9128 printf ("pop {");
9129 for (i = 0; i < 12; i++)
9130 if (mask & (1 << i))
9131 {
9132 if (first)
9133 first = false;
9134 else
9135 printf (", ");
9136 printf ("r%d", i);
9137 }
9138 printf ("}");
9139 }
9140 }
9141 else if (op == 0xb2)
9142 {
9143 unsigned char buf[9];
9144 unsigned int i, len;
9145 unsigned long offset;
9146
9147 for (i = 0; i < sizeof (buf); i++)
9148 {
9149 GET_OP (buf[i]);
9150 if ((buf[i] & 0x80) == 0)
9151 break;
9152 }
9153 if (i == sizeof (buf))
9154 {
9155 error (_("corrupt change to vsp\n"));
9156 res = false;
9157 }
9158 else
9159 {
9160 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9161 assert (len == i + 1);
9162 offset = offset * 4 + 0x204;
9163 printf ("vsp = vsp + %ld", offset);
9164 }
9165 }
9166 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9167 {
9168 unsigned int first, last;
9169
9170 GET_OP (op2);
9171 first = op2 >> 4;
9172 last = op2 & 0x0f;
9173 if (op == 0xc8)
9174 first = first + 16;
9175 printf ("pop {D%d", first);
9176 if (last)
9177 printf ("-D%d", first + last);
9178 printf ("}");
9179 }
9180 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9181 {
9182 unsigned int count = op & 0x07;
9183
9184 printf ("pop {D8");
9185 if (count)
9186 printf ("-D%d", 8 + count);
9187 printf ("}");
9188 }
9189 else if (op >= 0xc0 && op <= 0xc5)
9190 {
9191 unsigned int count = op & 0x07;
9192
9193 printf (" pop {wR10");
9194 if (count)
9195 printf ("-wR%d", 10 + count);
9196 printf ("}");
9197 }
9198 else if (op == 0xc6)
9199 {
9200 unsigned int first, last;
9201
9202 GET_OP (op2);
9203 first = op2 >> 4;
9204 last = op2 & 0x0f;
9205 printf ("pop {wR%d", first);
9206 if (last)
9207 printf ("-wR%d", first + last);
9208 printf ("}");
9209 }
9210 else if (op == 0xc7)
9211 {
9212 GET_OP (op2);
9213 if (op2 == 0 || (op2 & 0xf0) != 0)
9214 printf (_("[Spare]"));
9215 else
9216 {
9217 unsigned int mask = op2 & 0x0f;
9218 bool first = true;
9219 int i;
9220
9221 printf ("pop {");
9222 for (i = 0; i < 4; i++)
9223 if (mask & (1 << i))
9224 {
9225 if (first)
9226 first = false;
9227 else
9228 printf (", ");
9229 printf ("wCGR%d", i);
9230 }
9231 printf ("}");
9232 }
9233 }
9234 else
9235 {
9236 printf (_(" [unsupported opcode]"));
9237 res = false;
9238 }
9239
9240 printf ("\n");
9241 }
9242
9243 return res;
9244 }
9245
9246 static bool
9247 decode_tic6x_unwind_bytecode (Filedata * filedata,
9248 struct arm_unw_aux_info * aux,
9249 unsigned int word,
9250 unsigned int remaining,
9251 unsigned int more_words,
9252 bfd_vma data_offset,
9253 Elf_Internal_Shdr * data_sec,
9254 struct arm_section * data_arm_sec)
9255 {
9256 struct absaddr addr;
9257
9258 /* Decode the unwinding instructions. */
9259 while (1)
9260 {
9261 unsigned int op, op2;
9262
9263 ADVANCE;
9264 if (remaining == 0)
9265 break;
9266 remaining--;
9267 op = word >> 24;
9268 word <<= 8;
9269
9270 printf (" 0x%02x ", op);
9271
9272 if ((op & 0xc0) == 0x00)
9273 {
9274 int offset = ((op & 0x3f) << 3) + 8;
9275 printf (" sp = sp + %d", offset);
9276 }
9277 else if ((op & 0xc0) == 0x80)
9278 {
9279 GET_OP (op2);
9280 if (op == 0x80 && op2 == 0)
9281 printf (_("Refuse to unwind"));
9282 else
9283 {
9284 unsigned int mask = ((op & 0x1f) << 8) | op2;
9285 if (op & 0x20)
9286 printf ("pop compact {");
9287 else
9288 printf ("pop {");
9289
9290 decode_tic6x_unwind_regmask (mask);
9291 printf("}");
9292 }
9293 }
9294 else if ((op & 0xf0) == 0xc0)
9295 {
9296 unsigned int reg;
9297 unsigned int nregs;
9298 unsigned int i;
9299 const char *name;
9300 struct
9301 {
9302 unsigned int offset;
9303 unsigned int reg;
9304 } regpos[16];
9305
9306 /* Scan entire instruction first so that GET_OP output is not
9307 interleaved with disassembly. */
9308 nregs = 0;
9309 for (i = 0; nregs < (op & 0xf); i++)
9310 {
9311 GET_OP (op2);
9312 reg = op2 >> 4;
9313 if (reg != 0xf)
9314 {
9315 regpos[nregs].offset = i * 2;
9316 regpos[nregs].reg = reg;
9317 nregs++;
9318 }
9319
9320 reg = op2 & 0xf;
9321 if (reg != 0xf)
9322 {
9323 regpos[nregs].offset = i * 2 + 1;
9324 regpos[nregs].reg = reg;
9325 nregs++;
9326 }
9327 }
9328
9329 printf (_("pop frame {"));
9330 if (nregs == 0)
9331 {
9332 printf (_("*corrupt* - no registers specified"));
9333 }
9334 else
9335 {
9336 reg = nregs - 1;
9337 for (i = i * 2; i > 0; i--)
9338 {
9339 if (regpos[reg].offset == i - 1)
9340 {
9341 name = tic6x_unwind_regnames[regpos[reg].reg];
9342 if (reg > 0)
9343 reg--;
9344 }
9345 else
9346 name = _("[pad]");
9347
9348 fputs (name, stdout);
9349 if (i > 1)
9350 printf (", ");
9351 }
9352 }
9353
9354 printf ("}");
9355 }
9356 else if (op == 0xd0)
9357 printf (" MOV FP, SP");
9358 else if (op == 0xd1)
9359 printf (" __c6xabi_pop_rts");
9360 else if (op == 0xd2)
9361 {
9362 unsigned char buf[9];
9363 unsigned int i, len;
9364 unsigned long offset;
9365
9366 for (i = 0; i < sizeof (buf); i++)
9367 {
9368 GET_OP (buf[i]);
9369 if ((buf[i] & 0x80) == 0)
9370 break;
9371 }
9372 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9373 if (i == sizeof (buf))
9374 {
9375 warn (_("Corrupt stack pointer adjustment detected\n"));
9376 return false;
9377 }
9378
9379 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9380 assert (len == i + 1);
9381 offset = offset * 8 + 0x408;
9382 printf (_("sp = sp + %ld"), offset);
9383 }
9384 else if ((op & 0xf0) == 0xe0)
9385 {
9386 if ((op & 0x0f) == 7)
9387 printf (" RETURN");
9388 else
9389 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9390 }
9391 else
9392 {
9393 printf (_(" [unsupported opcode]"));
9394 }
9395 putchar ('\n');
9396 }
9397
9398 return true;
9399 }
9400
9401 static bfd_vma
9402 arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9403 {
9404 bfd_vma offset;
9405
9406 offset = word & 0x7fffffff;
9407 if (offset & 0x40000000)
9408 offset |= ~ (bfd_vma) 0x7fffffff;
9409
9410 if (filedata->file_header.e_machine == EM_TI_C6000)
9411 offset <<= 1;
9412
9413 return offset + where;
9414 }
9415
9416 static bool
9417 decode_arm_unwind (Filedata * filedata,
9418 struct arm_unw_aux_info * aux,
9419 unsigned int word,
9420 unsigned int remaining,
9421 bfd_vma data_offset,
9422 Elf_Internal_Shdr * data_sec,
9423 struct arm_section * data_arm_sec)
9424 {
9425 int per_index;
9426 unsigned int more_words = 0;
9427 struct absaddr addr;
9428 bfd_vma sym_name = (bfd_vma) -1;
9429 bool res = true;
9430
9431 if (remaining == 0)
9432 {
9433 /* Fetch the first word.
9434 Note - when decoding an object file the address extracted
9435 here will always be 0. So we also pass in the sym_name
9436 parameter so that we can find the symbol associated with
9437 the personality routine. */
9438 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9439 & word, & addr, & sym_name))
9440 return false;
9441
9442 remaining = 4;
9443 }
9444 else
9445 {
9446 addr.section = SHN_UNDEF;
9447 addr.offset = 0;
9448 }
9449
9450 if ((word & 0x80000000) == 0)
9451 {
9452 /* Expand prel31 for personality routine. */
9453 bfd_vma fn;
9454 const char *procname;
9455
9456 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9457 printf (_(" Personality routine: "));
9458 if (fn == 0
9459 && addr.section == SHN_UNDEF && addr.offset == 0
9460 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9461 {
9462 procname = aux->strtab + sym_name;
9463 print_vma (fn, PREFIX_HEX);
9464 if (procname)
9465 {
9466 fputs (" <", stdout);
9467 fputs (procname, stdout);
9468 fputc ('>', stdout);
9469 }
9470 }
9471 else
9472 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
9473 fputc ('\n', stdout);
9474
9475 /* The GCC personality routines use the standard compact
9476 encoding, starting with one byte giving the number of
9477 words. */
9478 if (procname != NULL
9479 && (const_strneq (procname, "__gcc_personality_v0")
9480 || const_strneq (procname, "__gxx_personality_v0")
9481 || const_strneq (procname, "__gcj_personality_v0")
9482 || const_strneq (procname, "__gnu_objc_personality_v0")))
9483 {
9484 remaining = 0;
9485 more_words = 1;
9486 ADVANCE;
9487 if (!remaining)
9488 {
9489 printf (_(" [Truncated data]\n"));
9490 return false;
9491 }
9492 more_words = word >> 24;
9493 word <<= 8;
9494 remaining--;
9495 per_index = -1;
9496 }
9497 else
9498 return true;
9499 }
9500 else
9501 {
9502 /* ARM EHABI Section 6.3:
9503
9504 An exception-handling table entry for the compact model looks like:
9505
9506 31 30-28 27-24 23-0
9507 -- ----- ----- ----
9508 1 0 index Data for personalityRoutine[index] */
9509
9510 if (filedata->file_header.e_machine == EM_ARM
9511 && (word & 0x70000000))
9512 {
9513 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
9514 res = false;
9515 }
9516
9517 per_index = (word >> 24) & 0x7f;
9518 printf (_(" Compact model index: %d\n"), per_index);
9519 if (per_index == 0)
9520 {
9521 more_words = 0;
9522 word <<= 8;
9523 remaining--;
9524 }
9525 else if (per_index < 3)
9526 {
9527 more_words = (word >> 16) & 0xff;
9528 word <<= 16;
9529 remaining -= 2;
9530 }
9531 }
9532
9533 switch (filedata->file_header.e_machine)
9534 {
9535 case EM_ARM:
9536 if (per_index < 3)
9537 {
9538 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
9539 data_offset, data_sec, data_arm_sec))
9540 res = false;
9541 }
9542 else
9543 {
9544 warn (_("Unknown ARM compact model index encountered\n"));
9545 printf (_(" [reserved]\n"));
9546 res = false;
9547 }
9548 break;
9549
9550 case EM_TI_C6000:
9551 if (per_index < 3)
9552 {
9553 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9554 data_offset, data_sec, data_arm_sec))
9555 res = false;
9556 }
9557 else if (per_index < 5)
9558 {
9559 if (((word >> 17) & 0x7f) == 0x7f)
9560 printf (_(" Restore stack from frame pointer\n"));
9561 else
9562 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9563 printf (_(" Registers restored: "));
9564 if (per_index == 4)
9565 printf (" (compact) ");
9566 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9567 putchar ('\n');
9568 printf (_(" Return register: %s\n"),
9569 tic6x_unwind_regnames[word & 0xf]);
9570 }
9571 else
9572 printf (_(" [reserved (%d)]\n"), per_index);
9573 break;
9574
9575 default:
9576 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9577 filedata->file_header.e_machine);
9578 res = false;
9579 }
9580
9581 /* Decode the descriptors. Not implemented. */
9582
9583 return res;
9584 }
9585
9586 static bool
9587 dump_arm_unwind (Filedata * filedata,
9588 struct arm_unw_aux_info * aux,
9589 Elf_Internal_Shdr * exidx_sec)
9590 {
9591 struct arm_section exidx_arm_sec, extab_arm_sec;
9592 unsigned int i, exidx_len;
9593 unsigned long j, nfuns;
9594 bool res = true;
9595
9596 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9597 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9598 exidx_len = exidx_sec->sh_size / 8;
9599
9600 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9601 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9602 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9603 aux->funtab[nfuns++] = aux->symtab[j];
9604 aux->nfuns = nfuns;
9605 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9606
9607 for (i = 0; i < exidx_len; i++)
9608 {
9609 unsigned int exidx_fn, exidx_entry;
9610 struct absaddr fn_addr, entry_addr;
9611 bfd_vma fn;
9612
9613 fputc ('\n', stdout);
9614
9615 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9616 8 * i, & exidx_fn, & fn_addr, NULL)
9617 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9618 8 * i + 4, & exidx_entry, & entry_addr, NULL))
9619 {
9620 free (aux->funtab);
9621 arm_free_section (& exidx_arm_sec);
9622 arm_free_section (& extab_arm_sec);
9623 return false;
9624 }
9625
9626 /* ARM EHABI, Section 5:
9627 An index table entry consists of 2 words.
9628 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
9629 if (exidx_fn & 0x80000000)
9630 {
9631 warn (_("corrupt index table entry: %x\n"), exidx_fn);
9632 res = false;
9633 }
9634
9635 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9636
9637 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9638 fputs (": ", stdout);
9639
9640 if (exidx_entry == 1)
9641 {
9642 print_vma (exidx_entry, PREFIX_HEX);
9643 fputs (" [cantunwind]\n", stdout);
9644 }
9645 else if (exidx_entry & 0x80000000)
9646 {
9647 print_vma (exidx_entry, PREFIX_HEX);
9648 fputc ('\n', stdout);
9649 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9650 }
9651 else
9652 {
9653 bfd_vma table, table_offset = 0;
9654 Elf_Internal_Shdr *table_sec;
9655
9656 fputs ("@", stdout);
9657 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9658 print_vma (table, PREFIX_HEX);
9659 printf ("\n");
9660
9661 /* Locate the matching .ARM.extab. */
9662 if (entry_addr.section != SHN_UNDEF
9663 && entry_addr.section < filedata->file_header.e_shnum)
9664 {
9665 table_sec = filedata->section_headers + entry_addr.section;
9666 table_offset = entry_addr.offset;
9667 /* PR 18879 */
9668 if (table_offset > table_sec->sh_size
9669 || ((bfd_signed_vma) table_offset) < 0)
9670 {
9671 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9672 (unsigned long) table_offset,
9673 printable_section_name (filedata, table_sec));
9674 res = false;
9675 continue;
9676 }
9677 }
9678 else
9679 {
9680 table_sec = find_section_by_address (filedata, table);
9681 if (table_sec != NULL)
9682 table_offset = table - table_sec->sh_addr;
9683 }
9684
9685 if (table_sec == NULL)
9686 {
9687 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9688 (unsigned long) table);
9689 res = false;
9690 continue;
9691 }
9692
9693 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9694 &extab_arm_sec))
9695 res = false;
9696 }
9697 }
9698
9699 printf ("\n");
9700
9701 free (aux->funtab);
9702 arm_free_section (&exidx_arm_sec);
9703 arm_free_section (&extab_arm_sec);
9704
9705 return res;
9706 }
9707
9708 /* Used for both ARM and C6X unwinding tables. */
9709
9710 static bool
9711 arm_process_unwind (Filedata * filedata)
9712 {
9713 struct arm_unw_aux_info aux;
9714 Elf_Internal_Shdr *unwsec = NULL;
9715 Elf_Internal_Shdr *sec;
9716 unsigned long i;
9717 unsigned int sec_type;
9718 bool res = true;
9719
9720 switch (filedata->file_header.e_machine)
9721 {
9722 case EM_ARM:
9723 sec_type = SHT_ARM_EXIDX;
9724 break;
9725
9726 case EM_TI_C6000:
9727 sec_type = SHT_C6000_UNWIND;
9728 break;
9729
9730 default:
9731 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9732 filedata->file_header.e_machine);
9733 return false;
9734 }
9735
9736 if (filedata->string_table == NULL)
9737 return false;
9738
9739 memset (& aux, 0, sizeof (aux));
9740 aux.filedata = filedata;
9741
9742 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9743 {
9744 if (sec->sh_type == SHT_SYMTAB)
9745 {
9746 if (aux.symtab)
9747 {
9748 error (_("Multiple symbol tables encountered\n"));
9749 free (aux.symtab);
9750 aux.symtab = NULL;
9751 free (aux.strtab);
9752 aux.strtab = NULL;
9753 }
9754 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9755 &aux.strtab, &aux.strtab_size))
9756 return false;
9757 }
9758 else if (sec->sh_type == sec_type)
9759 unwsec = sec;
9760 }
9761
9762 if (unwsec == NULL)
9763 printf (_("\nThere are no unwind sections in this file.\n"));
9764 else
9765 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9766 {
9767 if (sec->sh_type == sec_type)
9768 {
9769 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
9770 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9771 "contains %lu entry:\n",
9772 "\nUnwind section '%s' at offset 0x%lx "
9773 "contains %lu entries:\n",
9774 num_unwind),
9775 printable_section_name (filedata, sec),
9776 (unsigned long) sec->sh_offset,
9777 num_unwind);
9778
9779 if (! dump_arm_unwind (filedata, &aux, sec))
9780 res = false;
9781 }
9782 }
9783
9784 free (aux.symtab);
9785 free ((char *) aux.strtab);
9786
9787 return res;
9788 }
9789
9790 static bool
9791 process_unwind (Filedata * filedata)
9792 {
9793 struct unwind_handler
9794 {
9795 unsigned int machtype;
9796 bool (* handler)(Filedata *);
9797 } handlers[] =
9798 {
9799 { EM_ARM, arm_process_unwind },
9800 { EM_IA_64, ia64_process_unwind },
9801 { EM_PARISC, hppa_process_unwind },
9802 { EM_TI_C6000, arm_process_unwind },
9803 { 0, NULL }
9804 };
9805 int i;
9806
9807 if (!do_unwind)
9808 return true;
9809
9810 for (i = 0; handlers[i].handler != NULL; i++)
9811 if (filedata->file_header.e_machine == handlers[i].machtype)
9812 return handlers[i].handler (filedata);
9813
9814 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
9815 get_machine_name (filedata->file_header.e_machine));
9816 return true;
9817 }
9818
9819 static void
9820 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
9821 {
9822 switch (entry->d_tag)
9823 {
9824 case DT_AARCH64_BTI_PLT:
9825 case DT_AARCH64_PAC_PLT:
9826 break;
9827 default:
9828 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9829 break;
9830 }
9831 putchar ('\n');
9832 }
9833
9834 static void
9835 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
9836 {
9837 switch (entry->d_tag)
9838 {
9839 case DT_MIPS_FLAGS:
9840 if (entry->d_un.d_val == 0)
9841 printf (_("NONE"));
9842 else
9843 {
9844 static const char * opts[] =
9845 {
9846 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
9847 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
9848 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
9849 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
9850 "RLD_ORDER_SAFE"
9851 };
9852 unsigned int cnt;
9853 bool first = true;
9854
9855 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
9856 if (entry->d_un.d_val & (1 << cnt))
9857 {
9858 printf ("%s%s", first ? "" : " ", opts[cnt]);
9859 first = false;
9860 }
9861 }
9862 break;
9863
9864 case DT_MIPS_IVERSION:
9865 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
9866 printf (_("Interface Version: %s"),
9867 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
9868 else
9869 {
9870 char buf[40];
9871 sprintf_vma (buf, entry->d_un.d_ptr);
9872 /* Note: coded this way so that there is a single string for translation. */
9873 printf (_("<corrupt: %s>"), buf);
9874 }
9875 break;
9876
9877 case DT_MIPS_TIME_STAMP:
9878 {
9879 char timebuf[128];
9880 struct tm * tmp;
9881 time_t atime = entry->d_un.d_val;
9882
9883 tmp = gmtime (&atime);
9884 /* PR 17531: file: 6accc532. */
9885 if (tmp == NULL)
9886 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
9887 else
9888 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
9889 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9890 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9891 printf (_("Time Stamp: %s"), timebuf);
9892 }
9893 break;
9894
9895 case DT_MIPS_RLD_VERSION:
9896 case DT_MIPS_LOCAL_GOTNO:
9897 case DT_MIPS_CONFLICTNO:
9898 case DT_MIPS_LIBLISTNO:
9899 case DT_MIPS_SYMTABNO:
9900 case DT_MIPS_UNREFEXTNO:
9901 case DT_MIPS_HIPAGENO:
9902 case DT_MIPS_DELTA_CLASS_NO:
9903 case DT_MIPS_DELTA_INSTANCE_NO:
9904 case DT_MIPS_DELTA_RELOC_NO:
9905 case DT_MIPS_DELTA_SYM_NO:
9906 case DT_MIPS_DELTA_CLASSSYM_NO:
9907 case DT_MIPS_COMPACT_SIZE:
9908 print_vma (entry->d_un.d_val, DEC);
9909 break;
9910
9911 case DT_MIPS_XHASH:
9912 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
9913 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
9914 /* Falls through. */
9915
9916 default:
9917 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9918 }
9919 putchar ('\n');
9920 }
9921
9922 static void
9923 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
9924 {
9925 switch (entry->d_tag)
9926 {
9927 case DT_HP_DLD_FLAGS:
9928 {
9929 static struct
9930 {
9931 long int bit;
9932 const char * str;
9933 }
9934 flags[] =
9935 {
9936 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
9937 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
9938 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
9939 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
9940 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
9941 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
9942 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
9943 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
9944 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
9945 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
9946 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
9947 { DT_HP_GST, "HP_GST" },
9948 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
9949 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
9950 { DT_HP_NODELETE, "HP_NODELETE" },
9951 { DT_HP_GROUP, "HP_GROUP" },
9952 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
9953 };
9954 bool first = true;
9955 size_t cnt;
9956 bfd_vma val = entry->d_un.d_val;
9957
9958 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
9959 if (val & flags[cnt].bit)
9960 {
9961 if (! first)
9962 putchar (' ');
9963 fputs (flags[cnt].str, stdout);
9964 first = false;
9965 val ^= flags[cnt].bit;
9966 }
9967
9968 if (val != 0 || first)
9969 {
9970 if (! first)
9971 putchar (' ');
9972 print_vma (val, HEX);
9973 }
9974 }
9975 break;
9976
9977 default:
9978 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9979 break;
9980 }
9981 putchar ('\n');
9982 }
9983
9984 #ifdef BFD64
9985
9986 /* VMS vs Unix time offset and factor. */
9987
9988 #define VMS_EPOCH_OFFSET 35067168000000000LL
9989 #define VMS_GRANULARITY_FACTOR 10000000
9990 #ifndef INT64_MIN
9991 #define INT64_MIN (-9223372036854775807LL - 1)
9992 #endif
9993
9994 /* Display a VMS time in a human readable format. */
9995
9996 static void
9997 print_vms_time (bfd_int64_t vmstime)
9998 {
9999 struct tm *tm = NULL;
10000 time_t unxtime;
10001
10002 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10003 {
10004 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10005 unxtime = vmstime;
10006 if (unxtime == vmstime)
10007 tm = gmtime (&unxtime);
10008 }
10009 if (tm != NULL)
10010 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10011 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10012 tm->tm_hour, tm->tm_min, tm->tm_sec);
10013 }
10014 #endif /* BFD64 */
10015
10016 static void
10017 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10018 {
10019 switch (entry->d_tag)
10020 {
10021 case DT_IA_64_PLT_RESERVE:
10022 /* First 3 slots reserved. */
10023 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10024 printf (" -- ");
10025 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10026 break;
10027
10028 case DT_IA_64_VMS_LINKTIME:
10029 #ifdef BFD64
10030 print_vms_time (entry->d_un.d_val);
10031 #endif
10032 break;
10033
10034 case DT_IA_64_VMS_LNKFLAGS:
10035 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10036 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10037 printf (" CALL_DEBUG");
10038 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10039 printf (" NOP0BUFS");
10040 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10041 printf (" P0IMAGE");
10042 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10043 printf (" MKTHREADS");
10044 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10045 printf (" UPCALLS");
10046 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10047 printf (" IMGSTA");
10048 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10049 printf (" INITIALIZE");
10050 if (entry->d_un.d_val & VMS_LF_MAIN)
10051 printf (" MAIN");
10052 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10053 printf (" EXE_INIT");
10054 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10055 printf (" TBK_IN_IMG");
10056 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10057 printf (" DBG_IN_IMG");
10058 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10059 printf (" TBK_IN_DSF");
10060 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10061 printf (" DBG_IN_DSF");
10062 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10063 printf (" SIGNATURES");
10064 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10065 printf (" REL_SEG_OFF");
10066 break;
10067
10068 default:
10069 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10070 break;
10071 }
10072 putchar ('\n');
10073 }
10074
10075 static bool
10076 get_32bit_dynamic_section (Filedata * filedata)
10077 {
10078 Elf32_External_Dyn * edyn;
10079 Elf32_External_Dyn * ext;
10080 Elf_Internal_Dyn * entry;
10081
10082 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10083 filedata->dynamic_addr, 1,
10084 filedata->dynamic_size,
10085 _("dynamic section"));
10086 if (!edyn)
10087 return false;
10088
10089 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10090 might not have the luxury of section headers. Look for the DT_NULL
10091 terminator to determine the number of entries. */
10092 for (ext = edyn, filedata->dynamic_nent = 0;
10093 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10094 ext++)
10095 {
10096 filedata->dynamic_nent++;
10097 if (BYTE_GET (ext->d_tag) == DT_NULL)
10098 break;
10099 }
10100
10101 filedata->dynamic_section
10102 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10103 if (filedata->dynamic_section == NULL)
10104 {
10105 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10106 (unsigned long) filedata->dynamic_nent);
10107 free (edyn);
10108 return false;
10109 }
10110
10111 for (ext = edyn, entry = filedata->dynamic_section;
10112 entry < filedata->dynamic_section + filedata->dynamic_nent;
10113 ext++, entry++)
10114 {
10115 entry->d_tag = BYTE_GET (ext->d_tag);
10116 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10117 }
10118
10119 free (edyn);
10120
10121 return true;
10122 }
10123
10124 static bool
10125 get_64bit_dynamic_section (Filedata * filedata)
10126 {
10127 Elf64_External_Dyn * edyn;
10128 Elf64_External_Dyn * ext;
10129 Elf_Internal_Dyn * entry;
10130
10131 /* Read in the data. */
10132 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10133 filedata->dynamic_addr, 1,
10134 filedata->dynamic_size,
10135 _("dynamic section"));
10136 if (!edyn)
10137 return false;
10138
10139 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10140 might not have the luxury of section headers. Look for the DT_NULL
10141 terminator to determine the number of entries. */
10142 for (ext = edyn, filedata->dynamic_nent = 0;
10143 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10144 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10145 ext++)
10146 {
10147 filedata->dynamic_nent++;
10148 if (BYTE_GET (ext->d_tag) == DT_NULL)
10149 break;
10150 }
10151
10152 filedata->dynamic_section
10153 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10154 if (filedata->dynamic_section == NULL)
10155 {
10156 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10157 (unsigned long) filedata->dynamic_nent);
10158 free (edyn);
10159 return false;
10160 }
10161
10162 /* Convert from external to internal formats. */
10163 for (ext = edyn, entry = filedata->dynamic_section;
10164 entry < filedata->dynamic_section + filedata->dynamic_nent;
10165 ext++, entry++)
10166 {
10167 entry->d_tag = BYTE_GET (ext->d_tag);
10168 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10169 }
10170
10171 free (edyn);
10172
10173 return true;
10174 }
10175
10176 static void
10177 print_dynamic_flags (bfd_vma flags)
10178 {
10179 bool first = true;
10180
10181 while (flags)
10182 {
10183 bfd_vma flag;
10184
10185 flag = flags & - flags;
10186 flags &= ~ flag;
10187
10188 if (first)
10189 first = false;
10190 else
10191 putc (' ', stdout);
10192
10193 switch (flag)
10194 {
10195 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10196 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10197 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10198 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10199 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10200 default: fputs (_("unknown"), stdout); break;
10201 }
10202 }
10203 puts ("");
10204 }
10205
10206 static bfd_vma *
10207 get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10208 {
10209 unsigned char * e_data;
10210 bfd_vma * i_data;
10211
10212 /* If the size_t type is smaller than the bfd_size_type, eg because
10213 you are building a 32-bit tool on a 64-bit host, then make sure
10214 that when (number) is cast to (size_t) no information is lost. */
10215 if (sizeof (size_t) < sizeof (bfd_size_type)
10216 && (bfd_size_type) ((size_t) number) != number)
10217 {
10218 error (_("Size truncation prevents reading %s elements of size %u\n"),
10219 bfd_vmatoa ("u", number), ent_size);
10220 return NULL;
10221 }
10222
10223 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10224 attempting to allocate memory when the read is bound to fail. */
10225 if (ent_size * number > filedata->file_size)
10226 {
10227 error (_("Invalid number of dynamic entries: %s\n"),
10228 bfd_vmatoa ("u", number));
10229 return NULL;
10230 }
10231
10232 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10233 if (e_data == NULL)
10234 {
10235 error (_("Out of memory reading %s dynamic entries\n"),
10236 bfd_vmatoa ("u", number));
10237 return NULL;
10238 }
10239
10240 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10241 {
10242 error (_("Unable to read in %s bytes of dynamic data\n"),
10243 bfd_vmatoa ("u", number * ent_size));
10244 free (e_data);
10245 return NULL;
10246 }
10247
10248 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10249 if (i_data == NULL)
10250 {
10251 error (_("Out of memory allocating space for %s dynamic entries\n"),
10252 bfd_vmatoa ("u", number));
10253 free (e_data);
10254 return NULL;
10255 }
10256
10257 while (number--)
10258 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10259
10260 free (e_data);
10261
10262 return i_data;
10263 }
10264
10265 static unsigned long
10266 get_num_dynamic_syms (Filedata * filedata)
10267 {
10268 unsigned long num_of_syms = 0;
10269
10270 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10271 return num_of_syms;
10272
10273 if (filedata->dynamic_info[DT_HASH])
10274 {
10275 unsigned char nb[8];
10276 unsigned char nc[8];
10277 unsigned int hash_ent_size = 4;
10278
10279 if ((filedata->file_header.e_machine == EM_ALPHA
10280 || filedata->file_header.e_machine == EM_S390
10281 || filedata->file_header.e_machine == EM_S390_OLD)
10282 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10283 hash_ent_size = 8;
10284
10285 if (fseek (filedata->handle,
10286 (filedata->archive_file_offset
10287 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10288 sizeof nb + sizeof nc)),
10289 SEEK_SET))
10290 {
10291 error (_("Unable to seek to start of dynamic information\n"));
10292 goto no_hash;
10293 }
10294
10295 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10296 {
10297 error (_("Failed to read in number of buckets\n"));
10298 goto no_hash;
10299 }
10300
10301 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10302 {
10303 error (_("Failed to read in number of chains\n"));
10304 goto no_hash;
10305 }
10306
10307 filedata->nbuckets = byte_get (nb, hash_ent_size);
10308 filedata->nchains = byte_get (nc, hash_ent_size);
10309
10310 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10311 {
10312 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10313 hash_ent_size);
10314 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10315 hash_ent_size);
10316
10317 if (filedata->buckets != NULL && filedata->chains != NULL)
10318 num_of_syms = filedata->nchains;
10319 }
10320 no_hash:
10321 if (num_of_syms == 0)
10322 {
10323 free (filedata->buckets);
10324 filedata->buckets = NULL;
10325 free (filedata->chains);
10326 filedata->chains = NULL;
10327 filedata->nbuckets = 0;
10328 }
10329 }
10330
10331 if (filedata->dynamic_info_DT_GNU_HASH)
10332 {
10333 unsigned char nb[16];
10334 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10335 bfd_vma buckets_vma;
10336 unsigned long hn;
10337
10338 if (fseek (filedata->handle,
10339 (filedata->archive_file_offset
10340 + offset_from_vma (filedata,
10341 filedata->dynamic_info_DT_GNU_HASH,
10342 sizeof nb)),
10343 SEEK_SET))
10344 {
10345 error (_("Unable to seek to start of dynamic information\n"));
10346 goto no_gnu_hash;
10347 }
10348
10349 if (fread (nb, 16, 1, filedata->handle) != 1)
10350 {
10351 error (_("Failed to read in number of buckets\n"));
10352 goto no_gnu_hash;
10353 }
10354
10355 filedata->ngnubuckets = byte_get (nb, 4);
10356 filedata->gnusymidx = byte_get (nb + 4, 4);
10357 bitmaskwords = byte_get (nb + 8, 4);
10358 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10359 if (is_32bit_elf)
10360 buckets_vma += bitmaskwords * 4;
10361 else
10362 buckets_vma += bitmaskwords * 8;
10363
10364 if (fseek (filedata->handle,
10365 (filedata->archive_file_offset
10366 + offset_from_vma (filedata, buckets_vma, 4)),
10367 SEEK_SET))
10368 {
10369 error (_("Unable to seek to start of dynamic information\n"));
10370 goto no_gnu_hash;
10371 }
10372
10373 filedata->gnubuckets
10374 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10375
10376 if (filedata->gnubuckets == NULL)
10377 goto no_gnu_hash;
10378
10379 for (i = 0; i < filedata->ngnubuckets; i++)
10380 if (filedata->gnubuckets[i] != 0)
10381 {
10382 if (filedata->gnubuckets[i] < filedata->gnusymidx)
10383 goto no_gnu_hash;
10384
10385 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10386 maxchain = filedata->gnubuckets[i];
10387 }
10388
10389 if (maxchain == 0xffffffff)
10390 goto no_gnu_hash;
10391
10392 maxchain -= filedata->gnusymidx;
10393
10394 if (fseek (filedata->handle,
10395 (filedata->archive_file_offset
10396 + offset_from_vma (filedata,
10397 buckets_vma + 4 * (filedata->ngnubuckets
10398 + maxchain),
10399 4)),
10400 SEEK_SET))
10401 {
10402 error (_("Unable to seek to start of dynamic information\n"));
10403 goto no_gnu_hash;
10404 }
10405
10406 do
10407 {
10408 if (fread (nb, 4, 1, filedata->handle) != 1)
10409 {
10410 error (_("Failed to determine last chain length\n"));
10411 goto no_gnu_hash;
10412 }
10413
10414 if (maxchain + 1 == 0)
10415 goto no_gnu_hash;
10416
10417 ++maxchain;
10418 }
10419 while ((byte_get (nb, 4) & 1) == 0);
10420
10421 if (fseek (filedata->handle,
10422 (filedata->archive_file_offset
10423 + offset_from_vma (filedata, (buckets_vma
10424 + 4 * filedata->ngnubuckets),
10425 4)),
10426 SEEK_SET))
10427 {
10428 error (_("Unable to seek to start of dynamic information\n"));
10429 goto no_gnu_hash;
10430 }
10431
10432 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10433 filedata->ngnuchains = maxchain;
10434
10435 if (filedata->gnuchains == NULL)
10436 goto no_gnu_hash;
10437
10438 if (filedata->dynamic_info_DT_MIPS_XHASH)
10439 {
10440 if (fseek (filedata->handle,
10441 (filedata->archive_file_offset
10442 + offset_from_vma (filedata, (buckets_vma
10443 + 4 * (filedata->ngnubuckets
10444 + maxchain)), 4)),
10445 SEEK_SET))
10446 {
10447 error (_("Unable to seek to start of dynamic information\n"));
10448 goto no_gnu_hash;
10449 }
10450
10451 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
10452 if (filedata->mipsxlat == NULL)
10453 goto no_gnu_hash;
10454 }
10455
10456 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10457 if (filedata->gnubuckets[hn] != 0)
10458 {
10459 bfd_vma si = filedata->gnubuckets[hn];
10460 bfd_vma off = si - filedata->gnusymidx;
10461
10462 do
10463 {
10464 if (filedata->dynamic_info_DT_MIPS_XHASH)
10465 {
10466 if (off < filedata->ngnuchains
10467 && filedata->mipsxlat[off] >= num_of_syms)
10468 num_of_syms = filedata->mipsxlat[off] + 1;
10469 }
10470 else
10471 {
10472 if (si >= num_of_syms)
10473 num_of_syms = si + 1;
10474 }
10475 si++;
10476 }
10477 while (off < filedata->ngnuchains
10478 && (filedata->gnuchains[off++] & 1) == 0);
10479 }
10480
10481 if (num_of_syms == 0)
10482 {
10483 no_gnu_hash:
10484 free (filedata->mipsxlat);
10485 filedata->mipsxlat = NULL;
10486 free (filedata->gnuchains);
10487 filedata->gnuchains = NULL;
10488 free (filedata->gnubuckets);
10489 filedata->gnubuckets = NULL;
10490 filedata->ngnubuckets = 0;
10491 filedata->ngnuchains = 0;
10492 }
10493 }
10494
10495 return num_of_syms;
10496 }
10497
10498 /* Parse and display the contents of the dynamic section. */
10499
10500 static bool
10501 process_dynamic_section (Filedata * filedata)
10502 {
10503 Elf_Internal_Dyn * entry;
10504
10505 if (filedata->dynamic_size == 0)
10506 {
10507 if (do_dynamic)
10508 {
10509 if (filedata->is_separate)
10510 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
10511 filedata->file_name);
10512 else
10513 printf (_("\nThere is no dynamic section in this file.\n"));
10514 }
10515
10516 return true;
10517 }
10518
10519 if (is_32bit_elf)
10520 {
10521 if (! get_32bit_dynamic_section (filedata))
10522 return false;
10523 }
10524 else
10525 {
10526 if (! get_64bit_dynamic_section (filedata))
10527 return false;
10528 }
10529
10530 /* Find the appropriate symbol table. */
10531 if (filedata->dynamic_symbols == NULL || do_histogram)
10532 {
10533 unsigned long num_of_syms;
10534
10535 for (entry = filedata->dynamic_section;
10536 entry < filedata->dynamic_section + filedata->dynamic_nent;
10537 ++entry)
10538 if (entry->d_tag == DT_SYMTAB)
10539 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10540 else if (entry->d_tag == DT_SYMENT)
10541 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10542 else if (entry->d_tag == DT_HASH)
10543 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10544 else if (entry->d_tag == DT_GNU_HASH)
10545 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10546 else if ((filedata->file_header.e_machine == EM_MIPS
10547 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10548 && entry->d_tag == DT_MIPS_XHASH)
10549 {
10550 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10551 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10552 }
10553
10554 num_of_syms = get_num_dynamic_syms (filedata);
10555
10556 if (num_of_syms != 0
10557 && filedata->dynamic_symbols == NULL
10558 && filedata->dynamic_info[DT_SYMTAB]
10559 && filedata->dynamic_info[DT_SYMENT])
10560 {
10561 Elf_Internal_Phdr *seg;
10562 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
10563
10564 if (! get_program_headers (filedata))
10565 {
10566 error (_("Cannot interpret virtual addresses "
10567 "without program headers.\n"));
10568 return false;
10569 }
10570
10571 for (seg = filedata->program_headers;
10572 seg < filedata->program_headers + filedata->file_header.e_phnum;
10573 ++seg)
10574 {
10575 if (seg->p_type != PT_LOAD)
10576 continue;
10577
10578 if (seg->p_offset + seg->p_filesz > filedata->file_size)
10579 {
10580 /* See PR 21379 for a reproducer. */
10581 error (_("Invalid PT_LOAD entry\n"));
10582 return false;
10583 }
10584
10585 if (vma >= (seg->p_vaddr & -seg->p_align)
10586 && vma < seg->p_vaddr + seg->p_filesz)
10587 {
10588 /* Since we do not know how big the symbol table is,
10589 we default to reading in up to the end of PT_LOAD
10590 segment and processing that. This is overkill, I
10591 know, but it should work. */
10592 Elf_Internal_Shdr section;
10593 section.sh_offset = (vma - seg->p_vaddr
10594 + seg->p_offset);
10595 section.sh_size = (num_of_syms
10596 * filedata->dynamic_info[DT_SYMENT]);
10597 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
10598
10599 if (do_checks
10600 && filedata->dynamic_symtab_section != NULL
10601 && ((filedata->dynamic_symtab_section->sh_offset
10602 != section.sh_offset)
10603 || (filedata->dynamic_symtab_section->sh_size
10604 != section.sh_size)
10605 || (filedata->dynamic_symtab_section->sh_entsize
10606 != section.sh_entsize)))
10607 warn (_("\
10608 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
10609
10610 section.sh_name = filedata->string_table_length;
10611 filedata->dynamic_symbols
10612 = GET_ELF_SYMBOLS (filedata, &section,
10613 &filedata->num_dynamic_syms);
10614 if (filedata->dynamic_symbols == NULL
10615 || filedata->num_dynamic_syms != num_of_syms)
10616 {
10617 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
10618 return false;
10619 }
10620 break;
10621 }
10622 }
10623 }
10624 }
10625
10626 /* Similarly find a string table. */
10627 if (filedata->dynamic_strings == NULL)
10628 for (entry = filedata->dynamic_section;
10629 entry < filedata->dynamic_section + filedata->dynamic_nent;
10630 ++entry)
10631 {
10632 if (entry->d_tag == DT_STRTAB)
10633 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
10634
10635 if (entry->d_tag == DT_STRSZ)
10636 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
10637
10638 if (filedata->dynamic_info[DT_STRTAB]
10639 && filedata->dynamic_info[DT_STRSZ])
10640 {
10641 unsigned long offset;
10642 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10643
10644 offset = offset_from_vma (filedata,
10645 filedata->dynamic_info[DT_STRTAB],
10646 str_tab_len);
10647 if (do_checks
10648 && filedata->dynamic_strtab_section
10649 && ((filedata->dynamic_strtab_section->sh_offset
10650 != (file_ptr) offset)
10651 || (filedata->dynamic_strtab_section->sh_size
10652 != str_tab_len)))
10653 warn (_("\
10654 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
10655
10656 filedata->dynamic_strings
10657 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
10658 _("dynamic string table"));
10659 if (filedata->dynamic_strings == NULL)
10660 {
10661 error (_("Corrupt DT_STRTAB dynamic entry\n"));
10662 break;
10663 }
10664
10665 filedata->dynamic_strings_length = str_tab_len;
10666 break;
10667 }
10668 }
10669
10670 /* And find the syminfo section if available. */
10671 if (filedata->dynamic_syminfo == NULL)
10672 {
10673 unsigned long syminsz = 0;
10674
10675 for (entry = filedata->dynamic_section;
10676 entry < filedata->dynamic_section + filedata->dynamic_nent;
10677 ++entry)
10678 {
10679 if (entry->d_tag == DT_SYMINENT)
10680 {
10681 /* Note: these braces are necessary to avoid a syntax
10682 error from the SunOS4 C compiler. */
10683 /* PR binutils/17531: A corrupt file can trigger this test.
10684 So do not use an assert, instead generate an error message. */
10685 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
10686 error (_("Bad value (%d) for SYMINENT entry\n"),
10687 (int) entry->d_un.d_val);
10688 }
10689 else if (entry->d_tag == DT_SYMINSZ)
10690 syminsz = entry->d_un.d_val;
10691 else if (entry->d_tag == DT_SYMINFO)
10692 filedata->dynamic_syminfo_offset
10693 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
10694 }
10695
10696 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
10697 {
10698 Elf_External_Syminfo * extsyminfo;
10699 Elf_External_Syminfo * extsym;
10700 Elf_Internal_Syminfo * syminfo;
10701
10702 /* There is a syminfo section. Read the data. */
10703 extsyminfo = (Elf_External_Syminfo *)
10704 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
10705 1, syminsz, _("symbol information"));
10706 if (!extsyminfo)
10707 return false;
10708
10709 if (filedata->dynamic_syminfo != NULL)
10710 {
10711 error (_("Multiple dynamic symbol information sections found\n"));
10712 free (filedata->dynamic_syminfo);
10713 }
10714 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
10715 if (filedata->dynamic_syminfo == NULL)
10716 {
10717 error (_("Out of memory allocating %lu bytes "
10718 "for dynamic symbol info\n"),
10719 (unsigned long) syminsz);
10720 return false;
10721 }
10722
10723 filedata->dynamic_syminfo_nent
10724 = syminsz / sizeof (Elf_External_Syminfo);
10725 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
10726 syminfo < (filedata->dynamic_syminfo
10727 + filedata->dynamic_syminfo_nent);
10728 ++syminfo, ++extsym)
10729 {
10730 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
10731 syminfo->si_flags = BYTE_GET (extsym->si_flags);
10732 }
10733
10734 free (extsyminfo);
10735 }
10736 }
10737
10738 if (do_dynamic && filedata->dynamic_addr)
10739 {
10740 if (filedata->dynamic_nent == 1)
10741 {
10742 if (filedata->is_separate)
10743 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains 1 entry:\n"),
10744 filedata->file_name,
10745 filedata->dynamic_addr);
10746 else
10747 printf (_("\nDynamic section at offset 0x%lx contains 1 entry:\n"),
10748 filedata->dynamic_addr);
10749 }
10750 else
10751 {
10752 if (filedata->is_separate)
10753 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n"),
10754 filedata->file_name,
10755 filedata->dynamic_addr,
10756 (unsigned long) filedata->dynamic_nent);
10757 else
10758 printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
10759 filedata->dynamic_addr,
10760 (unsigned long) filedata->dynamic_nent);
10761 }
10762 }
10763 if (do_dynamic)
10764 printf (_(" Tag Type Name/Value\n"));
10765
10766 for (entry = filedata->dynamic_section;
10767 entry < filedata->dynamic_section + filedata->dynamic_nent;
10768 entry++)
10769 {
10770 if (do_dynamic)
10771 {
10772 const char * dtype;
10773
10774 putchar (' ');
10775 print_vma (entry->d_tag, FULL_HEX);
10776 dtype = get_dynamic_type (filedata, entry->d_tag);
10777 printf (" (%s)%*s", dtype,
10778 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
10779 }
10780
10781 switch (entry->d_tag)
10782 {
10783 case DT_FLAGS:
10784 if (do_dynamic)
10785 print_dynamic_flags (entry->d_un.d_val);
10786 break;
10787
10788 case DT_AUXILIARY:
10789 case DT_FILTER:
10790 case DT_CONFIG:
10791 case DT_DEPAUDIT:
10792 case DT_AUDIT:
10793 if (do_dynamic)
10794 {
10795 switch (entry->d_tag)
10796 {
10797 case DT_AUXILIARY:
10798 printf (_("Auxiliary library"));
10799 break;
10800
10801 case DT_FILTER:
10802 printf (_("Filter library"));
10803 break;
10804
10805 case DT_CONFIG:
10806 printf (_("Configuration file"));
10807 break;
10808
10809 case DT_DEPAUDIT:
10810 printf (_("Dependency audit library"));
10811 break;
10812
10813 case DT_AUDIT:
10814 printf (_("Audit library"));
10815 break;
10816 }
10817
10818 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
10819 printf (": [%s]\n",
10820 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
10821 else
10822 {
10823 printf (": ");
10824 print_vma (entry->d_un.d_val, PREFIX_HEX);
10825 putchar ('\n');
10826 }
10827 }
10828 break;
10829
10830 case DT_FEATURE:
10831 if (do_dynamic)
10832 {
10833 printf (_("Flags:"));
10834
10835 if (entry->d_un.d_val == 0)
10836 printf (_(" None\n"));
10837 else
10838 {
10839 unsigned long int val = entry->d_un.d_val;
10840
10841 if (val & DTF_1_PARINIT)
10842 {
10843 printf (" PARINIT");
10844 val ^= DTF_1_PARINIT;
10845 }
10846 if (val & DTF_1_CONFEXP)
10847 {
10848 printf (" CONFEXP");
10849 val ^= DTF_1_CONFEXP;
10850 }
10851 if (val != 0)
10852 printf (" %lx", val);
10853 puts ("");
10854 }
10855 }
10856 break;
10857
10858 case DT_POSFLAG_1:
10859 if (do_dynamic)
10860 {
10861 printf (_("Flags:"));
10862
10863 if (entry->d_un.d_val == 0)
10864 printf (_(" None\n"));
10865 else
10866 {
10867 unsigned long int val = entry->d_un.d_val;
10868
10869 if (val & DF_P1_LAZYLOAD)
10870 {
10871 printf (" LAZYLOAD");
10872 val ^= DF_P1_LAZYLOAD;
10873 }
10874 if (val & DF_P1_GROUPPERM)
10875 {
10876 printf (" GROUPPERM");
10877 val ^= DF_P1_GROUPPERM;
10878 }
10879 if (val != 0)
10880 printf (" %lx", val);
10881 puts ("");
10882 }
10883 }
10884 break;
10885
10886 case DT_FLAGS_1:
10887 if (do_dynamic)
10888 {
10889 printf (_("Flags:"));
10890 if (entry->d_un.d_val == 0)
10891 printf (_(" None\n"));
10892 else
10893 {
10894 unsigned long int val = entry->d_un.d_val;
10895
10896 if (val & DF_1_NOW)
10897 {
10898 printf (" NOW");
10899 val ^= DF_1_NOW;
10900 }
10901 if (val & DF_1_GLOBAL)
10902 {
10903 printf (" GLOBAL");
10904 val ^= DF_1_GLOBAL;
10905 }
10906 if (val & DF_1_GROUP)
10907 {
10908 printf (" GROUP");
10909 val ^= DF_1_GROUP;
10910 }
10911 if (val & DF_1_NODELETE)
10912 {
10913 printf (" NODELETE");
10914 val ^= DF_1_NODELETE;
10915 }
10916 if (val & DF_1_LOADFLTR)
10917 {
10918 printf (" LOADFLTR");
10919 val ^= DF_1_LOADFLTR;
10920 }
10921 if (val & DF_1_INITFIRST)
10922 {
10923 printf (" INITFIRST");
10924 val ^= DF_1_INITFIRST;
10925 }
10926 if (val & DF_1_NOOPEN)
10927 {
10928 printf (" NOOPEN");
10929 val ^= DF_1_NOOPEN;
10930 }
10931 if (val & DF_1_ORIGIN)
10932 {
10933 printf (" ORIGIN");
10934 val ^= DF_1_ORIGIN;
10935 }
10936 if (val & DF_1_DIRECT)
10937 {
10938 printf (" DIRECT");
10939 val ^= DF_1_DIRECT;
10940 }
10941 if (val & DF_1_TRANS)
10942 {
10943 printf (" TRANS");
10944 val ^= DF_1_TRANS;
10945 }
10946 if (val & DF_1_INTERPOSE)
10947 {
10948 printf (" INTERPOSE");
10949 val ^= DF_1_INTERPOSE;
10950 }
10951 if (val & DF_1_NODEFLIB)
10952 {
10953 printf (" NODEFLIB");
10954 val ^= DF_1_NODEFLIB;
10955 }
10956 if (val & DF_1_NODUMP)
10957 {
10958 printf (" NODUMP");
10959 val ^= DF_1_NODUMP;
10960 }
10961 if (val & DF_1_CONFALT)
10962 {
10963 printf (" CONFALT");
10964 val ^= DF_1_CONFALT;
10965 }
10966 if (val & DF_1_ENDFILTEE)
10967 {
10968 printf (" ENDFILTEE");
10969 val ^= DF_1_ENDFILTEE;
10970 }
10971 if (val & DF_1_DISPRELDNE)
10972 {
10973 printf (" DISPRELDNE");
10974 val ^= DF_1_DISPRELDNE;
10975 }
10976 if (val & DF_1_DISPRELPND)
10977 {
10978 printf (" DISPRELPND");
10979 val ^= DF_1_DISPRELPND;
10980 }
10981 if (val & DF_1_NODIRECT)
10982 {
10983 printf (" NODIRECT");
10984 val ^= DF_1_NODIRECT;
10985 }
10986 if (val & DF_1_IGNMULDEF)
10987 {
10988 printf (" IGNMULDEF");
10989 val ^= DF_1_IGNMULDEF;
10990 }
10991 if (val & DF_1_NOKSYMS)
10992 {
10993 printf (" NOKSYMS");
10994 val ^= DF_1_NOKSYMS;
10995 }
10996 if (val & DF_1_NOHDR)
10997 {
10998 printf (" NOHDR");
10999 val ^= DF_1_NOHDR;
11000 }
11001 if (val & DF_1_EDITED)
11002 {
11003 printf (" EDITED");
11004 val ^= DF_1_EDITED;
11005 }
11006 if (val & DF_1_NORELOC)
11007 {
11008 printf (" NORELOC");
11009 val ^= DF_1_NORELOC;
11010 }
11011 if (val & DF_1_SYMINTPOSE)
11012 {
11013 printf (" SYMINTPOSE");
11014 val ^= DF_1_SYMINTPOSE;
11015 }
11016 if (val & DF_1_GLOBAUDIT)
11017 {
11018 printf (" GLOBAUDIT");
11019 val ^= DF_1_GLOBAUDIT;
11020 }
11021 if (val & DF_1_SINGLETON)
11022 {
11023 printf (" SINGLETON");
11024 val ^= DF_1_SINGLETON;
11025 }
11026 if (val & DF_1_STUB)
11027 {
11028 printf (" STUB");
11029 val ^= DF_1_STUB;
11030 }
11031 if (val & DF_1_PIE)
11032 {
11033 printf (" PIE");
11034 val ^= DF_1_PIE;
11035 }
11036 if (val & DF_1_KMOD)
11037 {
11038 printf (" KMOD");
11039 val ^= DF_1_KMOD;
11040 }
11041 if (val & DF_1_WEAKFILTER)
11042 {
11043 printf (" WEAKFILTER");
11044 val ^= DF_1_WEAKFILTER;
11045 }
11046 if (val & DF_1_NOCOMMON)
11047 {
11048 printf (" NOCOMMON");
11049 val ^= DF_1_NOCOMMON;
11050 }
11051 if (val != 0)
11052 printf (" %lx", val);
11053 puts ("");
11054 }
11055 }
11056 break;
11057
11058 case DT_PLTREL:
11059 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11060 if (do_dynamic)
11061 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11062 break;
11063
11064 case DT_NULL :
11065 case DT_NEEDED :
11066 case DT_PLTGOT :
11067 case DT_HASH :
11068 case DT_STRTAB :
11069 case DT_SYMTAB :
11070 case DT_RELA :
11071 case DT_INIT :
11072 case DT_FINI :
11073 case DT_SONAME :
11074 case DT_RPATH :
11075 case DT_SYMBOLIC:
11076 case DT_REL :
11077 case DT_DEBUG :
11078 case DT_TEXTREL :
11079 case DT_JMPREL :
11080 case DT_RUNPATH :
11081 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11082
11083 if (do_dynamic)
11084 {
11085 char * name;
11086
11087 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11088 name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
11089 else
11090 name = NULL;
11091
11092 if (name)
11093 {
11094 switch (entry->d_tag)
11095 {
11096 case DT_NEEDED:
11097 printf (_("Shared library: [%s]"), name);
11098
11099 if (streq (name, filedata->program_interpreter))
11100 printf (_(" program interpreter"));
11101 break;
11102
11103 case DT_SONAME:
11104 printf (_("Library soname: [%s]"), name);
11105 break;
11106
11107 case DT_RPATH:
11108 printf (_("Library rpath: [%s]"), name);
11109 break;
11110
11111 case DT_RUNPATH:
11112 printf (_("Library runpath: [%s]"), name);
11113 break;
11114
11115 default:
11116 print_vma (entry->d_un.d_val, PREFIX_HEX);
11117 break;
11118 }
11119 }
11120 else
11121 print_vma (entry->d_un.d_val, PREFIX_HEX);
11122
11123 putchar ('\n');
11124 }
11125 break;
11126
11127 case DT_PLTRELSZ:
11128 case DT_RELASZ :
11129 case DT_STRSZ :
11130 case DT_RELSZ :
11131 case DT_RELAENT :
11132 case DT_SYMENT :
11133 case DT_RELENT :
11134 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11135 /* Fall through. */
11136 case DT_PLTPADSZ:
11137 case DT_MOVEENT :
11138 case DT_MOVESZ :
11139 case DT_INIT_ARRAYSZ:
11140 case DT_FINI_ARRAYSZ:
11141 case DT_GNU_CONFLICTSZ:
11142 case DT_GNU_LIBLISTSZ:
11143 if (do_dynamic)
11144 {
11145 print_vma (entry->d_un.d_val, UNSIGNED);
11146 printf (_(" (bytes)\n"));
11147 }
11148 break;
11149
11150 case DT_VERDEFNUM:
11151 case DT_VERNEEDNUM:
11152 case DT_RELACOUNT:
11153 case DT_RELCOUNT:
11154 if (do_dynamic)
11155 {
11156 print_vma (entry->d_un.d_val, UNSIGNED);
11157 putchar ('\n');
11158 }
11159 break;
11160
11161 case DT_SYMINSZ:
11162 case DT_SYMINENT:
11163 case DT_SYMINFO:
11164 case DT_USED:
11165 case DT_INIT_ARRAY:
11166 case DT_FINI_ARRAY:
11167 if (do_dynamic)
11168 {
11169 if (entry->d_tag == DT_USED
11170 && VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11171 {
11172 char * name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
11173
11174 if (*name)
11175 {
11176 printf (_("Not needed object: [%s]\n"), name);
11177 break;
11178 }
11179 }
11180
11181 print_vma (entry->d_un.d_val, PREFIX_HEX);
11182 putchar ('\n');
11183 }
11184 break;
11185
11186 case DT_BIND_NOW:
11187 /* The value of this entry is ignored. */
11188 if (do_dynamic)
11189 putchar ('\n');
11190 break;
11191
11192 case DT_GNU_PRELINKED:
11193 if (do_dynamic)
11194 {
11195 struct tm * tmp;
11196 time_t atime = entry->d_un.d_val;
11197
11198 tmp = gmtime (&atime);
11199 /* PR 17533 file: 041-1244816-0.004. */
11200 if (tmp == NULL)
11201 printf (_("<corrupt time val: %lx"),
11202 (unsigned long) atime);
11203 else
11204 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11205 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11206 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11207
11208 }
11209 break;
11210
11211 case DT_GNU_HASH:
11212 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11213 if (do_dynamic)
11214 {
11215 print_vma (entry->d_un.d_val, PREFIX_HEX);
11216 putchar ('\n');
11217 }
11218 break;
11219
11220 case DT_GNU_FLAGS_1:
11221 if (do_dynamic)
11222 {
11223 printf (_("Flags:"));
11224 if (entry->d_un.d_val == 0)
11225 printf (_(" None\n"));
11226 else
11227 {
11228 unsigned long int val = entry->d_un.d_val;
11229
11230 if (val & DF_GNU_1_UNIQUE)
11231 {
11232 printf (" UNIQUE");
11233 val ^= DF_GNU_1_UNIQUE;
11234 }
11235 if (val != 0)
11236 printf (" %lx", val);
11237 puts ("");
11238 }
11239 }
11240 break;
11241
11242 default:
11243 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11244 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11245 = entry->d_un.d_val;
11246
11247 if (do_dynamic)
11248 {
11249 switch (filedata->file_header.e_machine)
11250 {
11251 case EM_AARCH64:
11252 dynamic_section_aarch64_val (entry);
11253 break;
11254 case EM_MIPS:
11255 case EM_MIPS_RS3_LE:
11256 dynamic_section_mips_val (filedata, entry);
11257 break;
11258 case EM_PARISC:
11259 dynamic_section_parisc_val (entry);
11260 break;
11261 case EM_IA_64:
11262 dynamic_section_ia64_val (entry);
11263 break;
11264 default:
11265 print_vma (entry->d_un.d_val, PREFIX_HEX);
11266 putchar ('\n');
11267 }
11268 }
11269 break;
11270 }
11271 }
11272
11273 return true;
11274 }
11275
11276 static char *
11277 get_ver_flags (unsigned int flags)
11278 {
11279 static char buff[128];
11280
11281 buff[0] = 0;
11282
11283 if (flags == 0)
11284 return _("none");
11285
11286 if (flags & VER_FLG_BASE)
11287 strcat (buff, "BASE");
11288
11289 if (flags & VER_FLG_WEAK)
11290 {
11291 if (flags & VER_FLG_BASE)
11292 strcat (buff, " | ");
11293
11294 strcat (buff, "WEAK");
11295 }
11296
11297 if (flags & VER_FLG_INFO)
11298 {
11299 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
11300 strcat (buff, " | ");
11301
11302 strcat (buff, "INFO");
11303 }
11304
11305 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11306 {
11307 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11308 strcat (buff, " | ");
11309
11310 strcat (buff, _("<unknown>"));
11311 }
11312
11313 return buff;
11314 }
11315
11316 /* Display the contents of the version sections. */
11317
11318 static bool
11319 process_version_sections (Filedata * filedata)
11320 {
11321 Elf_Internal_Shdr * section;
11322 unsigned i;
11323 bool found = false;
11324
11325 if (! do_version)
11326 return true;
11327
11328 for (i = 0, section = filedata->section_headers;
11329 i < filedata->file_header.e_shnum;
11330 i++, section++)
11331 {
11332 switch (section->sh_type)
11333 {
11334 case SHT_GNU_verdef:
11335 {
11336 Elf_External_Verdef * edefs;
11337 unsigned long idx;
11338 unsigned long cnt;
11339 char * endbuf;
11340
11341 found = true;
11342
11343 if (filedata->is_separate)
11344 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11345 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11346 section->sh_info),
11347 filedata->file_name,
11348 printable_section_name (filedata, section),
11349 section->sh_info);
11350 else
11351 printf (ngettext ("\nVersion definition section '%s' "
11352 "contains %u entry:\n",
11353 "\nVersion definition section '%s' "
11354 "contains %u entries:\n",
11355 section->sh_info),
11356 printable_section_name (filedata, section),
11357 section->sh_info);
11358
11359 printf (_(" Addr: 0x"));
11360 printf_vma (section->sh_addr);
11361 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11362 (unsigned long) section->sh_offset, section->sh_link,
11363 printable_section_name_from_index (filedata, section->sh_link));
11364
11365 edefs = (Elf_External_Verdef *)
11366 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
11367 _("version definition section"));
11368 if (!edefs)
11369 break;
11370 endbuf = (char *) edefs + section->sh_size;
11371
11372 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11373 {
11374 char * vstart;
11375 Elf_External_Verdef * edef;
11376 Elf_Internal_Verdef ent;
11377 Elf_External_Verdaux * eaux;
11378 Elf_Internal_Verdaux aux;
11379 unsigned long isum;
11380 int j;
11381
11382 vstart = ((char *) edefs) + idx;
11383 if (vstart + sizeof (*edef) > endbuf)
11384 break;
11385
11386 edef = (Elf_External_Verdef *) vstart;
11387
11388 ent.vd_version = BYTE_GET (edef->vd_version);
11389 ent.vd_flags = BYTE_GET (edef->vd_flags);
11390 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11391 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11392 ent.vd_hash = BYTE_GET (edef->vd_hash);
11393 ent.vd_aux = BYTE_GET (edef->vd_aux);
11394 ent.vd_next = BYTE_GET (edef->vd_next);
11395
11396 printf (_(" %#06lx: Rev: %d Flags: %s"),
11397 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11398
11399 printf (_(" Index: %d Cnt: %d "),
11400 ent.vd_ndx, ent.vd_cnt);
11401
11402 /* Check for overflow. */
11403 if (ent.vd_aux > (size_t) (endbuf - vstart))
11404 break;
11405
11406 vstart += ent.vd_aux;
11407
11408 if (vstart + sizeof (*eaux) > endbuf)
11409 break;
11410 eaux = (Elf_External_Verdaux *) vstart;
11411
11412 aux.vda_name = BYTE_GET (eaux->vda_name);
11413 aux.vda_next = BYTE_GET (eaux->vda_next);
11414
11415 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11416 printf (_("Name: %s\n"),
11417 GET_DYNAMIC_NAME (filedata, aux.vda_name));
11418 else
11419 printf (_("Name index: %ld\n"), aux.vda_name);
11420
11421 isum = idx + ent.vd_aux;
11422
11423 for (j = 1; j < ent.vd_cnt; j++)
11424 {
11425 if (aux.vda_next < sizeof (*eaux)
11426 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11427 {
11428 warn (_("Invalid vda_next field of %lx\n"),
11429 aux.vda_next);
11430 j = ent.vd_cnt;
11431 break;
11432 }
11433 /* Check for overflow. */
11434 if (aux.vda_next > (size_t) (endbuf - vstart))
11435 break;
11436
11437 isum += aux.vda_next;
11438 vstart += aux.vda_next;
11439
11440 if (vstart + sizeof (*eaux) > endbuf)
11441 break;
11442 eaux = (Elf_External_Verdaux *) vstart;
11443
11444 aux.vda_name = BYTE_GET (eaux->vda_name);
11445 aux.vda_next = BYTE_GET (eaux->vda_next);
11446
11447 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11448 printf (_(" %#06lx: Parent %d: %s\n"),
11449 isum, j,
11450 GET_DYNAMIC_NAME (filedata, aux.vda_name));
11451 else
11452 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
11453 isum, j, aux.vda_name);
11454 }
11455
11456 if (j < ent.vd_cnt)
11457 printf (_(" Version def aux past end of section\n"));
11458
11459 /* PR 17531:
11460 file: id:000001,src:000172+005151,op:splice,rep:2. */
11461 if (ent.vd_next < sizeof (*edef)
11462 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11463 {
11464 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11465 cnt = section->sh_info;
11466 break;
11467 }
11468 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
11469 break;
11470
11471 idx += ent.vd_next;
11472 }
11473
11474 if (cnt < section->sh_info)
11475 printf (_(" Version definition past end of section\n"));
11476
11477 free (edefs);
11478 }
11479 break;
11480
11481 case SHT_GNU_verneed:
11482 {
11483 Elf_External_Verneed * eneed;
11484 unsigned long idx;
11485 unsigned long cnt;
11486 char * endbuf;
11487
11488 found = true;
11489
11490 if (filedata->is_separate)
11491 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
11492 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
11493 section->sh_info),
11494 filedata->file_name,
11495 printable_section_name (filedata, section),
11496 section->sh_info);
11497 else
11498 printf (ngettext ("\nVersion needs section '%s' "
11499 "contains %u entry:\n",
11500 "\nVersion needs section '%s' "
11501 "contains %u entries:\n",
11502 section->sh_info),
11503 printable_section_name (filedata, section),
11504 section->sh_info);
11505
11506 printf (_(" Addr: 0x"));
11507 printf_vma (section->sh_addr);
11508 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11509 (unsigned long) section->sh_offset, section->sh_link,
11510 printable_section_name_from_index (filedata, section->sh_link));
11511
11512 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
11513 section->sh_offset, 1,
11514 section->sh_size,
11515 _("Version Needs section"));
11516 if (!eneed)
11517 break;
11518 endbuf = (char *) eneed + section->sh_size;
11519
11520 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11521 {
11522 Elf_External_Verneed * entry;
11523 Elf_Internal_Verneed ent;
11524 unsigned long isum;
11525 int j;
11526 char * vstart;
11527
11528 vstart = ((char *) eneed) + idx;
11529 if (vstart + sizeof (*entry) > endbuf)
11530 break;
11531
11532 entry = (Elf_External_Verneed *) vstart;
11533
11534 ent.vn_version = BYTE_GET (entry->vn_version);
11535 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
11536 ent.vn_file = BYTE_GET (entry->vn_file);
11537 ent.vn_aux = BYTE_GET (entry->vn_aux);
11538 ent.vn_next = BYTE_GET (entry->vn_next);
11539
11540 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
11541
11542 if (VALID_DYNAMIC_NAME (filedata, ent.vn_file))
11543 printf (_(" File: %s"),
11544 GET_DYNAMIC_NAME (filedata, ent.vn_file));
11545 else
11546 printf (_(" File: %lx"), ent.vn_file);
11547
11548 printf (_(" Cnt: %d\n"), ent.vn_cnt);
11549
11550 /* Check for overflow. */
11551 if (ent.vn_aux > (size_t) (endbuf - vstart))
11552 break;
11553 vstart += ent.vn_aux;
11554
11555 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11556 {
11557 Elf_External_Vernaux * eaux;
11558 Elf_Internal_Vernaux aux;
11559
11560 if (vstart + sizeof (*eaux) > endbuf)
11561 break;
11562 eaux = (Elf_External_Vernaux *) vstart;
11563
11564 aux.vna_hash = BYTE_GET (eaux->vna_hash);
11565 aux.vna_flags = BYTE_GET (eaux->vna_flags);
11566 aux.vna_other = BYTE_GET (eaux->vna_other);
11567 aux.vna_name = BYTE_GET (eaux->vna_name);
11568 aux.vna_next = BYTE_GET (eaux->vna_next);
11569
11570 if (VALID_DYNAMIC_NAME (filedata, aux.vna_name))
11571 printf (_(" %#06lx: Name: %s"),
11572 isum, GET_DYNAMIC_NAME (filedata, aux.vna_name));
11573 else
11574 printf (_(" %#06lx: Name index: %lx"),
11575 isum, aux.vna_name);
11576
11577 printf (_(" Flags: %s Version: %d\n"),
11578 get_ver_flags (aux.vna_flags), aux.vna_other);
11579
11580 if (aux.vna_next < sizeof (*eaux)
11581 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
11582 {
11583 warn (_("Invalid vna_next field of %lx\n"),
11584 aux.vna_next);
11585 j = ent.vn_cnt;
11586 break;
11587 }
11588 /* Check for overflow. */
11589 if (aux.vna_next > (size_t) (endbuf - vstart))
11590 break;
11591 isum += aux.vna_next;
11592 vstart += aux.vna_next;
11593 }
11594
11595 if (j < ent.vn_cnt)
11596 warn (_("Missing Version Needs auxiliary information\n"));
11597
11598 if (ent.vn_next < sizeof (*entry)
11599 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
11600 {
11601 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
11602 cnt = section->sh_info;
11603 break;
11604 }
11605 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
11606 break;
11607 idx += ent.vn_next;
11608 }
11609
11610 if (cnt < section->sh_info)
11611 warn (_("Missing Version Needs information\n"));
11612
11613 free (eneed);
11614 }
11615 break;
11616
11617 case SHT_GNU_versym:
11618 {
11619 Elf_Internal_Shdr * link_section;
11620 size_t total;
11621 unsigned int cnt;
11622 unsigned char * edata;
11623 unsigned short * data;
11624 char * strtab;
11625 Elf_Internal_Sym * symbols;
11626 Elf_Internal_Shdr * string_sec;
11627 unsigned long num_syms;
11628 long off;
11629
11630 if (section->sh_link >= filedata->file_header.e_shnum)
11631 break;
11632
11633 link_section = filedata->section_headers + section->sh_link;
11634 total = section->sh_size / sizeof (Elf_External_Versym);
11635
11636 if (link_section->sh_link >= filedata->file_header.e_shnum)
11637 break;
11638
11639 found = true;
11640
11641 symbols = GET_ELF_SYMBOLS (filedata, link_section, & num_syms);
11642 if (symbols == NULL)
11643 break;
11644
11645 string_sec = filedata->section_headers + link_section->sh_link;
11646
11647 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
11648 string_sec->sh_size,
11649 _("version string table"));
11650 if (!strtab)
11651 {
11652 free (symbols);
11653 break;
11654 }
11655
11656 if (filedata->is_separate)
11657 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
11658 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
11659 total),
11660 filedata->file_name,
11661 printable_section_name (filedata, section),
11662 (unsigned long) total);
11663 else
11664 printf (ngettext ("\nVersion symbols section '%s' "
11665 "contains %lu entry:\n",
11666 "\nVersion symbols section '%s' "
11667 "contains %lu entries:\n",
11668 total),
11669 printable_section_name (filedata, section),
11670 (unsigned long) total);
11671
11672 printf (_(" Addr: 0x"));
11673 printf_vma (section->sh_addr);
11674 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11675 (unsigned long) section->sh_offset, section->sh_link,
11676 printable_section_name (filedata, link_section));
11677
11678 off = offset_from_vma (filedata,
11679 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11680 total * sizeof (short));
11681 edata = (unsigned char *) get_data (NULL, filedata, off,
11682 sizeof (short), total,
11683 _("version symbol data"));
11684 if (!edata)
11685 {
11686 free (strtab);
11687 free (symbols);
11688 break;
11689 }
11690
11691 data = (short unsigned int *) cmalloc (total, sizeof (short));
11692
11693 for (cnt = total; cnt --;)
11694 data[cnt] = byte_get (edata + cnt * sizeof (short),
11695 sizeof (short));
11696
11697 free (edata);
11698
11699 for (cnt = 0; cnt < total; cnt += 4)
11700 {
11701 int j, nn;
11702 char *name;
11703 char *invalid = _("*invalid*");
11704
11705 printf (" %03x:", cnt);
11706
11707 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
11708 switch (data[cnt + j])
11709 {
11710 case 0:
11711 fputs (_(" 0 (*local*) "), stdout);
11712 break;
11713
11714 case 1:
11715 fputs (_(" 1 (*global*) "), stdout);
11716 break;
11717
11718 default:
11719 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
11720 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
11721
11722 /* If this index value is greater than the size of the symbols
11723 array, break to avoid an out-of-bounds read. */
11724 if ((unsigned long)(cnt + j) >= num_syms)
11725 {
11726 warn (_("invalid index into symbol array\n"));
11727 break;
11728 }
11729
11730 name = NULL;
11731 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11732 {
11733 Elf_Internal_Verneed ivn;
11734 unsigned long offset;
11735
11736 offset = offset_from_vma
11737 (filedata,
11738 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11739 sizeof (Elf_External_Verneed));
11740
11741 do
11742 {
11743 Elf_Internal_Vernaux ivna;
11744 Elf_External_Verneed evn;
11745 Elf_External_Vernaux evna;
11746 unsigned long a_off;
11747
11748 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
11749 _("version need")) == NULL)
11750 break;
11751
11752 ivn.vn_aux = BYTE_GET (evn.vn_aux);
11753 ivn.vn_next = BYTE_GET (evn.vn_next);
11754
11755 a_off = offset + ivn.vn_aux;
11756
11757 do
11758 {
11759 if (get_data (&evna, filedata, a_off, sizeof (evna),
11760 1, _("version need aux (2)")) == NULL)
11761 {
11762 ivna.vna_next = 0;
11763 ivna.vna_other = 0;
11764 }
11765 else
11766 {
11767 ivna.vna_next = BYTE_GET (evna.vna_next);
11768 ivna.vna_other = BYTE_GET (evna.vna_other);
11769 }
11770
11771 a_off += ivna.vna_next;
11772 }
11773 while (ivna.vna_other != data[cnt + j]
11774 && ivna.vna_next != 0);
11775
11776 if (ivna.vna_other == data[cnt + j])
11777 {
11778 ivna.vna_name = BYTE_GET (evna.vna_name);
11779
11780 if (ivna.vna_name >= string_sec->sh_size)
11781 name = invalid;
11782 else
11783 name = strtab + ivna.vna_name;
11784 break;
11785 }
11786
11787 offset += ivn.vn_next;
11788 }
11789 while (ivn.vn_next);
11790 }
11791
11792 if (data[cnt + j] != 0x8001
11793 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
11794 {
11795 Elf_Internal_Verdef ivd;
11796 Elf_External_Verdef evd;
11797 unsigned long offset;
11798
11799 offset = offset_from_vma
11800 (filedata,
11801 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
11802 sizeof evd);
11803
11804 do
11805 {
11806 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
11807 _("version def")) == NULL)
11808 {
11809 ivd.vd_next = 0;
11810 /* PR 17531: file: 046-1082287-0.004. */
11811 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
11812 break;
11813 }
11814 else
11815 {
11816 ivd.vd_next = BYTE_GET (evd.vd_next);
11817 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
11818 }
11819
11820 offset += ivd.vd_next;
11821 }
11822 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
11823 && ivd.vd_next != 0);
11824
11825 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
11826 {
11827 Elf_External_Verdaux evda;
11828 Elf_Internal_Verdaux ivda;
11829
11830 ivd.vd_aux = BYTE_GET (evd.vd_aux);
11831
11832 if (get_data (&evda, filedata,
11833 offset - ivd.vd_next + ivd.vd_aux,
11834 sizeof (evda), 1,
11835 _("version def aux")) == NULL)
11836 break;
11837
11838 ivda.vda_name = BYTE_GET (evda.vda_name);
11839
11840 if (ivda.vda_name >= string_sec->sh_size)
11841 name = invalid;
11842 else if (name != NULL && name != invalid)
11843 name = _("*both*");
11844 else
11845 name = strtab + ivda.vda_name;
11846 }
11847 }
11848 if (name != NULL)
11849 nn += printf ("(%s%-*s",
11850 name,
11851 12 - (int) strlen (name),
11852 ")");
11853
11854 if (nn < 18)
11855 printf ("%*c", 18 - nn, ' ');
11856 }
11857
11858 putchar ('\n');
11859 }
11860
11861 free (data);
11862 free (strtab);
11863 free (symbols);
11864 }
11865 break;
11866
11867 default:
11868 break;
11869 }
11870 }
11871
11872 if (! found)
11873 {
11874 if (filedata->is_separate)
11875 printf (_("\nNo version information found in linked file '%s'.\n"),
11876 filedata->file_name);
11877 else
11878 printf (_("\nNo version information found in this file.\n"));
11879 }
11880
11881 return true;
11882 }
11883
11884 static const char *
11885 get_symbol_binding (Filedata * filedata, unsigned int binding)
11886 {
11887 static char buff[64];
11888
11889 switch (binding)
11890 {
11891 case STB_LOCAL: return "LOCAL";
11892 case STB_GLOBAL: return "GLOBAL";
11893 case STB_WEAK: return "WEAK";
11894 default:
11895 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
11896 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
11897 binding);
11898 else if (binding >= STB_LOOS && binding <= STB_HIOS)
11899 {
11900 if (binding == STB_GNU_UNIQUE
11901 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
11902 return "UNIQUE";
11903 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
11904 }
11905 else
11906 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
11907 return buff;
11908 }
11909 }
11910
11911 static const char *
11912 get_symbol_type (Filedata * filedata, unsigned int type)
11913 {
11914 static char buff[64];
11915
11916 switch (type)
11917 {
11918 case STT_NOTYPE: return "NOTYPE";
11919 case STT_OBJECT: return "OBJECT";
11920 case STT_FUNC: return "FUNC";
11921 case STT_SECTION: return "SECTION";
11922 case STT_FILE: return "FILE";
11923 case STT_COMMON: return "COMMON";
11924 case STT_TLS: return "TLS";
11925 case STT_RELC: return "RELC";
11926 case STT_SRELC: return "SRELC";
11927 default:
11928 if (type >= STT_LOPROC && type <= STT_HIPROC)
11929 {
11930 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
11931 return "THUMB_FUNC";
11932
11933 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
11934 return "REGISTER";
11935
11936 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
11937 return "PARISC_MILLI";
11938
11939 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
11940 }
11941 else if (type >= STT_LOOS && type <= STT_HIOS)
11942 {
11943 if (filedata->file_header.e_machine == EM_PARISC)
11944 {
11945 if (type == STT_HP_OPAQUE)
11946 return "HP_OPAQUE";
11947 if (type == STT_HP_STUB)
11948 return "HP_STUB";
11949 }
11950
11951 if (type == STT_GNU_IFUNC
11952 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
11953 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
11954 return "IFUNC";
11955
11956 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
11957 }
11958 else
11959 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
11960 return buff;
11961 }
11962 }
11963
11964 static const char *
11965 get_symbol_visibility (unsigned int visibility)
11966 {
11967 switch (visibility)
11968 {
11969 case STV_DEFAULT: return "DEFAULT";
11970 case STV_INTERNAL: return "INTERNAL";
11971 case STV_HIDDEN: return "HIDDEN";
11972 case STV_PROTECTED: return "PROTECTED";
11973 default:
11974 error (_("Unrecognized visibility value: %u\n"), visibility);
11975 return _("<unknown>");
11976 }
11977 }
11978
11979 static const char *
11980 get_alpha_symbol_other (unsigned int other)
11981 {
11982 switch (other)
11983 {
11984 case STO_ALPHA_NOPV: return "NOPV";
11985 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
11986 default:
11987 error (_("Unrecognized alpha specific other value: %u\n"), other);
11988 return _("<unknown>");
11989 }
11990 }
11991
11992 static const char *
11993 get_solaris_symbol_visibility (unsigned int visibility)
11994 {
11995 switch (visibility)
11996 {
11997 case 4: return "EXPORTED";
11998 case 5: return "SINGLETON";
11999 case 6: return "ELIMINATE";
12000 default: return get_symbol_visibility (visibility);
12001 }
12002 }
12003
12004 static const char *
12005 get_aarch64_symbol_other (unsigned int other)
12006 {
12007 static char buf[32];
12008
12009 if (other & STO_AARCH64_VARIANT_PCS)
12010 {
12011 other &= ~STO_AARCH64_VARIANT_PCS;
12012 if (other == 0)
12013 return "VARIANT_PCS";
12014 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12015 return buf;
12016 }
12017 return NULL;
12018 }
12019
12020 static const char *
12021 get_mips_symbol_other (unsigned int other)
12022 {
12023 switch (other)
12024 {
12025 case STO_OPTIONAL: return "OPTIONAL";
12026 case STO_MIPS_PLT: return "MIPS PLT";
12027 case STO_MIPS_PIC: return "MIPS PIC";
12028 case STO_MICROMIPS: return "MICROMIPS";
12029 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12030 case STO_MIPS16: return "MIPS16";
12031 default: return NULL;
12032 }
12033 }
12034
12035 static const char *
12036 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12037 {
12038 if (is_ia64_vms (filedata))
12039 {
12040 static char res[32];
12041
12042 res[0] = 0;
12043
12044 /* Function types is for images and .STB files only. */
12045 switch (filedata->file_header.e_type)
12046 {
12047 case ET_DYN:
12048 case ET_EXEC:
12049 switch (VMS_ST_FUNC_TYPE (other))
12050 {
12051 case VMS_SFT_CODE_ADDR:
12052 strcat (res, " CA");
12053 break;
12054 case VMS_SFT_SYMV_IDX:
12055 strcat (res, " VEC");
12056 break;
12057 case VMS_SFT_FD:
12058 strcat (res, " FD");
12059 break;
12060 case VMS_SFT_RESERVE:
12061 strcat (res, " RSV");
12062 break;
12063 default:
12064 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12065 VMS_ST_FUNC_TYPE (other));
12066 strcat (res, " <unknown>");
12067 break;
12068 }
12069 break;
12070 default:
12071 break;
12072 }
12073 switch (VMS_ST_LINKAGE (other))
12074 {
12075 case VMS_STL_IGNORE:
12076 strcat (res, " IGN");
12077 break;
12078 case VMS_STL_RESERVE:
12079 strcat (res, " RSV");
12080 break;
12081 case VMS_STL_STD:
12082 strcat (res, " STD");
12083 break;
12084 case VMS_STL_LNK:
12085 strcat (res, " LNK");
12086 break;
12087 default:
12088 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12089 VMS_ST_LINKAGE (other));
12090 strcat (res, " <unknown>");
12091 break;
12092 }
12093
12094 if (res[0] != 0)
12095 return res + 1;
12096 else
12097 return res;
12098 }
12099 return NULL;
12100 }
12101
12102 static const char *
12103 get_ppc64_symbol_other (unsigned int other)
12104 {
12105 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12106 return NULL;
12107
12108 other >>= STO_PPC64_LOCAL_BIT;
12109 if (other <= 6)
12110 {
12111 static char buf[64];
12112 if (other >= 2)
12113 other = ppc64_decode_local_entry (other);
12114 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12115 return buf;
12116 }
12117 return NULL;
12118 }
12119
12120 static const char *
12121 get_symbol_other (Filedata * filedata, unsigned int other)
12122 {
12123 const char * result = NULL;
12124 static char buff [64];
12125
12126 if (other == 0)
12127 return "";
12128
12129 switch (filedata->file_header.e_machine)
12130 {
12131 case EM_ALPHA:
12132 result = get_alpha_symbol_other (other);
12133 break;
12134 case EM_AARCH64:
12135 result = get_aarch64_symbol_other (other);
12136 break;
12137 case EM_MIPS:
12138 result = get_mips_symbol_other (other);
12139 break;
12140 case EM_IA_64:
12141 result = get_ia64_symbol_other (filedata, other);
12142 break;
12143 case EM_PPC64:
12144 result = get_ppc64_symbol_other (other);
12145 break;
12146 default:
12147 result = NULL;
12148 break;
12149 }
12150
12151 if (result)
12152 return result;
12153
12154 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12155 return buff;
12156 }
12157
12158 static const char *
12159 get_symbol_index_type (Filedata * filedata, unsigned int type)
12160 {
12161 static char buff[32];
12162
12163 switch (type)
12164 {
12165 case SHN_UNDEF: return "UND";
12166 case SHN_ABS: return "ABS";
12167 case SHN_COMMON: return "COM";
12168 default:
12169 if (type == SHN_IA_64_ANSI_COMMON
12170 && filedata->file_header.e_machine == EM_IA_64
12171 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12172 return "ANSI_COM";
12173 else if ((filedata->file_header.e_machine == EM_X86_64
12174 || filedata->file_header.e_machine == EM_L1OM
12175 || filedata->file_header.e_machine == EM_K1OM)
12176 && type == SHN_X86_64_LCOMMON)
12177 return "LARGE_COM";
12178 else if ((type == SHN_MIPS_SCOMMON
12179 && filedata->file_header.e_machine == EM_MIPS)
12180 || (type == SHN_TIC6X_SCOMMON
12181 && filedata->file_header.e_machine == EM_TI_C6000))
12182 return "SCOM";
12183 else if (type == SHN_MIPS_SUNDEFINED
12184 && filedata->file_header.e_machine == EM_MIPS)
12185 return "SUND";
12186 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12187 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12188 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12189 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12190 else if (type >= SHN_LORESERVE)
12191 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12192 else if (filedata->file_header.e_shnum != 0
12193 && type >= filedata->file_header.e_shnum)
12194 sprintf (buff, _("bad section index[%3d]"), type);
12195 else
12196 sprintf (buff, "%3d", type);
12197 break;
12198 }
12199
12200 return buff;
12201 }
12202
12203 static const char *
12204 get_symbol_version_string (Filedata * filedata,
12205 bool is_dynsym,
12206 const char * strtab,
12207 unsigned long int strtab_size,
12208 unsigned int si,
12209 Elf_Internal_Sym * psym,
12210 enum versioned_symbol_info * sym_info,
12211 unsigned short * vna_other)
12212 {
12213 unsigned char data[2];
12214 unsigned short vers_data;
12215 unsigned long offset;
12216 unsigned short max_vd_ndx;
12217
12218 if (!is_dynsym
12219 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12220 return NULL;
12221
12222 offset = offset_from_vma (filedata,
12223 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12224 sizeof data + si * sizeof (vers_data));
12225
12226 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12227 sizeof (data), 1, _("version data")) == NULL)
12228 return NULL;
12229
12230 vers_data = byte_get (data, 2);
12231
12232 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12233 return NULL;
12234
12235 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12236 max_vd_ndx = 0;
12237
12238 /* Usually we'd only see verdef for defined symbols, and verneed for
12239 undefined symbols. However, symbols defined by the linker in
12240 .dynbss for variables copied from a shared library in order to
12241 avoid text relocations are defined yet have verneed. We could
12242 use a heuristic to detect the special case, for example, check
12243 for verneed first on symbols defined in SHT_NOBITS sections, but
12244 it is simpler and more reliable to just look for both verdef and
12245 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12246
12247 if (psym->st_shndx != SHN_UNDEF
12248 && vers_data != 0x8001
12249 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12250 {
12251 Elf_Internal_Verdef ivd;
12252 Elf_Internal_Verdaux ivda;
12253 Elf_External_Verdaux evda;
12254 unsigned long off;
12255
12256 off = offset_from_vma (filedata,
12257 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12258 sizeof (Elf_External_Verdef));
12259
12260 do
12261 {
12262 Elf_External_Verdef evd;
12263
12264 if (get_data (&evd, filedata, off, sizeof (evd), 1,
12265 _("version def")) == NULL)
12266 {
12267 ivd.vd_ndx = 0;
12268 ivd.vd_aux = 0;
12269 ivd.vd_next = 0;
12270 ivd.vd_flags = 0;
12271 }
12272 else
12273 {
12274 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12275 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12276 ivd.vd_next = BYTE_GET (evd.vd_next);
12277 ivd.vd_flags = BYTE_GET (evd.vd_flags);
12278 }
12279
12280 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12281 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12282
12283 off += ivd.vd_next;
12284 }
12285 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
12286
12287 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12288 {
12289 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
12290 return NULL;
12291
12292 off -= ivd.vd_next;
12293 off += ivd.vd_aux;
12294
12295 if (get_data (&evda, filedata, off, sizeof (evda), 1,
12296 _("version def aux")) != NULL)
12297 {
12298 ivda.vda_name = BYTE_GET (evda.vda_name);
12299
12300 if (psym->st_name != ivda.vda_name)
12301 return (ivda.vda_name < strtab_size
12302 ? strtab + ivda.vda_name : _("<corrupt>"));
12303 }
12304 }
12305 }
12306
12307 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12308 {
12309 Elf_External_Verneed evn;
12310 Elf_Internal_Verneed ivn;
12311 Elf_Internal_Vernaux ivna;
12312
12313 offset = offset_from_vma (filedata,
12314 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12315 sizeof evn);
12316 do
12317 {
12318 unsigned long vna_off;
12319
12320 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12321 _("version need")) == NULL)
12322 {
12323 ivna.vna_next = 0;
12324 ivna.vna_other = 0;
12325 ivna.vna_name = 0;
12326 break;
12327 }
12328
12329 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12330 ivn.vn_next = BYTE_GET (evn.vn_next);
12331
12332 vna_off = offset + ivn.vn_aux;
12333
12334 do
12335 {
12336 Elf_External_Vernaux evna;
12337
12338 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
12339 _("version need aux (3)")) == NULL)
12340 {
12341 ivna.vna_next = 0;
12342 ivna.vna_other = 0;
12343 ivna.vna_name = 0;
12344 }
12345 else
12346 {
12347 ivna.vna_other = BYTE_GET (evna.vna_other);
12348 ivna.vna_next = BYTE_GET (evna.vna_next);
12349 ivna.vna_name = BYTE_GET (evna.vna_name);
12350 }
12351
12352 vna_off += ivna.vna_next;
12353 }
12354 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
12355
12356 if (ivna.vna_other == vers_data)
12357 break;
12358
12359 offset += ivn.vn_next;
12360 }
12361 while (ivn.vn_next != 0);
12362
12363 if (ivna.vna_other == vers_data)
12364 {
12365 *sym_info = symbol_undefined;
12366 *vna_other = ivna.vna_other;
12367 return (ivna.vna_name < strtab_size
12368 ? strtab + ivna.vna_name : _("<corrupt>"));
12369 }
12370 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12371 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12372 return _("<corrupt>");
12373 }
12374 return NULL;
12375 }
12376
12377 static void
12378 print_dynamic_symbol (Filedata *filedata, unsigned long si,
12379 Elf_Internal_Sym *symtab,
12380 Elf_Internal_Shdr *section,
12381 char *strtab, size_t strtab_size)
12382 {
12383 const char *version_string;
12384 enum versioned_symbol_info sym_info;
12385 unsigned short vna_other;
12386 Elf_Internal_Sym *psym = symtab + si;
12387
12388 printf ("%6ld: ", si);
12389 print_vma (psym->st_value, LONG_HEX);
12390 putchar (' ');
12391 print_vma (psym->st_size, DEC_5);
12392 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12393 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12394 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12395 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12396 else
12397 {
12398 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
12399
12400 printf (" %-7s", get_symbol_visibility (vis));
12401 /* Check to see if any other bits in the st_other field are set.
12402 Note - displaying this information disrupts the layout of the
12403 table being generated, but for the moment this case is very rare. */
12404 if (psym->st_other ^ vis)
12405 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
12406 }
12407 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
12408
12409 bool is_valid = VALID_SYMBOL_NAME (strtab, strtab_size, psym->st_name);
12410 const char * sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
12411
12412 version_string
12413 = get_symbol_version_string (filedata,
12414 (section == NULL
12415 || section->sh_type == SHT_DYNSYM),
12416 strtab, strtab_size, si,
12417 psym, &sym_info, &vna_other);
12418
12419 int len_avail = 21;
12420 if (! do_wide && version_string != NULL)
12421 {
12422 char buffer[16];
12423
12424 len_avail -= 1 + strlen (version_string);
12425
12426 if (sym_info == symbol_undefined)
12427 len_avail -= sprintf (buffer," (%d)", vna_other);
12428 else if (sym_info != symbol_hidden)
12429 len_avail -= 1;
12430 }
12431
12432 print_symbol (len_avail, sstr);
12433
12434 if (version_string)
12435 {
12436 if (sym_info == symbol_undefined)
12437 printf ("@%s (%d)", version_string, vna_other);
12438 else
12439 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12440 version_string);
12441 }
12442
12443 putchar ('\n');
12444
12445 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12446 && section != NULL
12447 && si >= section->sh_info
12448 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
12449 && filedata->file_header.e_machine != EM_MIPS
12450 /* Solaris binaries have been found to violate this requirement as
12451 well. Not sure if this is a bug or an ABI requirement. */
12452 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12453 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12454 si, printable_section_name (filedata, section), section->sh_info);
12455 }
12456
12457 static const char *
12458 get_lto_kind (unsigned int kind)
12459 {
12460 switch (kind)
12461 {
12462 case 0: return "DEF";
12463 case 1: return "WEAKDEF";
12464 case 2: return "UNDEF";
12465 case 3: return "WEAKUNDEF";
12466 case 4: return "COMMON";
12467 default:
12468 break;
12469 }
12470
12471 static char buffer[30];
12472 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
12473 sprintf (buffer, "<unknown: %u>", kind);
12474 return buffer;
12475 }
12476
12477 static const char *
12478 get_lto_visibility (unsigned int visibility)
12479 {
12480 switch (visibility)
12481 {
12482 case 0: return "DEFAULT";
12483 case 1: return "PROTECTED";
12484 case 2: return "INTERNAL";
12485 case 3: return "HIDDEN";
12486 default:
12487 break;
12488 }
12489
12490 static char buffer[30];
12491 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
12492 sprintf (buffer, "<unknown: %u>", visibility);
12493 return buffer;
12494 }
12495
12496 static const char *
12497 get_lto_sym_type (unsigned int sym_type)
12498 {
12499 switch (sym_type)
12500 {
12501 case 0: return "UNKNOWN";
12502 case 1: return "FUNCTION";
12503 case 2: return "VARIABLE";
12504 default:
12505 break;
12506 }
12507
12508 static char buffer[30];
12509 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
12510 sprintf (buffer, "<unknown: %u>", sym_type);
12511 return buffer;
12512 }
12513
12514 /* Display an LTO format symbol table.
12515 FIXME: The format of LTO symbol tables is not formalized.
12516 So this code could need changing in the future. */
12517
12518 static bool
12519 display_lto_symtab (Filedata * filedata,
12520 Elf_Internal_Shdr * section)
12521 {
12522 if (section->sh_size == 0)
12523 {
12524 if (filedata->is_separate)
12525 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
12526 printable_section_name (filedata, section),
12527 filedata->file_name);
12528 else
12529 printf (_("\nLTO Symbol table '%s' is empty!\n"),
12530 printable_section_name (filedata, section));
12531
12532 return true;
12533 }
12534
12535 if (section->sh_size > filedata->file_size)
12536 {
12537 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
12538 printable_section_name (filedata, section),
12539 (unsigned long) section->sh_size);
12540 return false;
12541 }
12542
12543 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
12544 section->sh_size, 1, _("LTO symbols"));
12545 if (alloced_data == NULL)
12546 return false;
12547
12548 /* Look for extended data for the symbol table. */
12549 Elf_Internal_Shdr * ext;
12550 void * ext_data_orig = NULL;
12551 char * ext_data = NULL;
12552 char * ext_data_end = NULL;
12553 char * ext_name = NULL;
12554
12555 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
12556 SECTION_NAME (section) + sizeof (".gnu.lto_.symtab.") - 1) > 0
12557 && ext_name != NULL /* Paranoia. */
12558 && (ext = find_section (filedata, ext_name)) != NULL)
12559 {
12560 if (ext->sh_size < 3)
12561 error (_("LTO Symbol extension table '%s' is empty!\n"),
12562 printable_section_name (filedata, ext));
12563 else
12564 {
12565 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
12566 ext->sh_size, 1,
12567 _("LTO ext symbol data"));
12568 if (ext_data != NULL)
12569 {
12570 ext_data_end = ext_data + ext->sh_size;
12571 if (* ext_data++ != 1)
12572 error (_("Unexpected version number in symbol extension table\n"));
12573 }
12574 }
12575 }
12576
12577 const unsigned char * data = (const unsigned char *) alloced_data;
12578 const unsigned char * end = data + section->sh_size;
12579
12580 if (filedata->is_separate)
12581 printf (_("\nIn linked file '%s': "), filedata->file_name);
12582 else
12583 printf ("\n");
12584
12585 if (ext_data_orig != NULL)
12586 {
12587 if (do_wide)
12588 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
12589 printable_section_name (filedata, section),
12590 printable_section_name (filedata, ext));
12591 else
12592 {
12593 printf (_("LTO Symbol table '%s'\n"),
12594 printable_section_name (filedata, section));
12595 printf (_(" and extension table '%s' contain:\n"),
12596 printable_section_name (filedata, ext));
12597 }
12598 }
12599 else
12600 printf (_("LTO Symbol table '%s' contains:\n"),
12601 printable_section_name (filedata, section));
12602
12603 /* FIXME: Add a wide version. */
12604 if (ext_data_orig != NULL)
12605 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
12606 else
12607 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
12608
12609 /* FIXME: We do not handle style prefixes. */
12610
12611 while (data < end)
12612 {
12613 const unsigned char * sym_name = data;
12614 data += strnlen ((const char *) sym_name, end - data) + 1;
12615 if (data >= end)
12616 goto fail;
12617
12618 const unsigned char * comdat_key = data;
12619 data += strnlen ((const char *) comdat_key, end - data) + 1;
12620 if (data >= end)
12621 goto fail;
12622
12623 if (data + 2 + 8 + 4 > end)
12624 goto fail;
12625
12626 unsigned int kind = *data++;
12627 unsigned int visibility = *data++;
12628
12629 elf_vma size = byte_get (data, 8);
12630 data += 8;
12631
12632 elf_vma slot = byte_get (data, 4);
12633 data += 4;
12634
12635 if (ext_data != NULL)
12636 {
12637 if (ext_data < (ext_data_end - 1))
12638 {
12639 unsigned int sym_type = * ext_data ++;
12640 unsigned int sec_kind = * ext_data ++;
12641
12642 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
12643 * comdat_key == 0 ? "-" : (char *) comdat_key,
12644 get_lto_kind (kind),
12645 get_lto_visibility (visibility),
12646 (long) size,
12647 (long) slot,
12648 get_lto_sym_type (sym_type),
12649 (long) sec_kind);
12650 print_symbol (6, (const char *) sym_name);
12651 }
12652 else
12653 {
12654 error (_("Ran out of LTO symbol extension data\n"));
12655 ext_data = NULL;
12656 /* FIXME: return FAIL result ? */
12657 }
12658 }
12659 else
12660 {
12661 printf (" %10s %10s %11s %08lx %08lx _",
12662 * comdat_key == 0 ? "-" : (char *) comdat_key,
12663 get_lto_kind (kind),
12664 get_lto_visibility (visibility),
12665 (long) size,
12666 (long) slot);
12667 print_symbol (21, (const char *) sym_name);
12668 }
12669 putchar ('\n');
12670 }
12671
12672 if (ext_data != NULL && ext_data < ext_data_end)
12673 {
12674 error (_("Data remains in the LTO symbol extension table\n"));
12675 goto fail;
12676 }
12677
12678 free (alloced_data);
12679 free (ext_data_orig);
12680 free (ext_name);
12681 return true;
12682
12683 fail:
12684 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
12685 free (alloced_data);
12686 free (ext_data_orig);
12687 free (ext_name);
12688 return false;
12689 }
12690
12691 /* Display LTO symbol tables. */
12692
12693 static bool
12694 process_lto_symbol_tables (Filedata * filedata)
12695 {
12696 Elf_Internal_Shdr * section;
12697 unsigned int i;
12698 bool res = true;
12699
12700 if (!do_lto_syms)
12701 return true;
12702
12703 if (filedata->section_headers == NULL)
12704 return true;
12705
12706 for (i = 0, section = filedata->section_headers;
12707 i < filedata->file_header.e_shnum;
12708 i++, section++)
12709 if (SECTION_NAME_VALID (section)
12710 && startswith (SECTION_NAME (section), ".gnu.lto_.symtab."))
12711 res &= display_lto_symtab (filedata, section);
12712
12713 return res;
12714 }
12715
12716 /* Dump the symbol table. */
12717
12718 static bool
12719 process_symbol_table (Filedata * filedata)
12720 {
12721 Elf_Internal_Shdr * section;
12722
12723 if (!do_syms && !do_dyn_syms && !do_histogram)
12724 return true;
12725
12726 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
12727 && do_syms
12728 && do_using_dynamic
12729 && filedata->dynamic_strings != NULL
12730 && filedata->dynamic_symbols != NULL)
12731 {
12732 unsigned long si;
12733
12734 if (filedata->is_separate)
12735 {
12736 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
12737 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
12738 filedata->num_dynamic_syms),
12739 filedata->file_name,
12740 filedata->num_dynamic_syms);
12741 }
12742 else
12743 {
12744 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
12745 "\nSymbol table for image contains %lu entries:\n",
12746 filedata->num_dynamic_syms),
12747 filedata->num_dynamic_syms);
12748 }
12749 if (is_32bit_elf)
12750 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12751 else
12752 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12753
12754 for (si = 0; si < filedata->num_dynamic_syms; si++)
12755 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
12756 filedata->dynamic_strings,
12757 filedata->dynamic_strings_length);
12758 }
12759 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
12760 && filedata->section_headers != NULL)
12761 {
12762 unsigned int i;
12763
12764 for (i = 0, section = filedata->section_headers;
12765 i < filedata->file_header.e_shnum;
12766 i++, section++)
12767 {
12768 char * strtab = NULL;
12769 unsigned long int strtab_size = 0;
12770 Elf_Internal_Sym * symtab;
12771 unsigned long si, num_syms;
12772
12773 if ((section->sh_type != SHT_SYMTAB
12774 && section->sh_type != SHT_DYNSYM)
12775 || (!do_syms
12776 && section->sh_type == SHT_SYMTAB))
12777 continue;
12778
12779 if (section->sh_entsize == 0)
12780 {
12781 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
12782 printable_section_name (filedata, section));
12783 continue;
12784 }
12785
12786 num_syms = section->sh_size / section->sh_entsize;
12787
12788 if (filedata->is_separate)
12789 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
12790 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
12791 num_syms),
12792 filedata->file_name,
12793 printable_section_name (filedata, section),
12794 num_syms);
12795 else
12796 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
12797 "\nSymbol table '%s' contains %lu entries:\n",
12798 num_syms),
12799 printable_section_name (filedata, section),
12800 num_syms);
12801
12802 if (is_32bit_elf)
12803 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12804 else
12805 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12806
12807 symtab = GET_ELF_SYMBOLS (filedata, section, & num_syms);
12808 if (symtab == NULL)
12809 continue;
12810
12811 if (section->sh_link == filedata->file_header.e_shstrndx)
12812 {
12813 strtab = filedata->string_table;
12814 strtab_size = filedata->string_table_length;
12815 }
12816 else if (section->sh_link < filedata->file_header.e_shnum)
12817 {
12818 Elf_Internal_Shdr * string_sec;
12819
12820 string_sec = filedata->section_headers + section->sh_link;
12821
12822 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
12823 1, string_sec->sh_size,
12824 _("string table"));
12825 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
12826 }
12827
12828 for (si = 0; si < num_syms; si++)
12829 print_dynamic_symbol (filedata, si, symtab, section,
12830 strtab, strtab_size);
12831
12832 free (symtab);
12833 if (strtab != filedata->string_table)
12834 free (strtab);
12835 }
12836 }
12837 else if (do_syms)
12838 printf
12839 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
12840
12841 if (do_histogram && filedata->buckets != NULL)
12842 {
12843 unsigned long * lengths;
12844 unsigned long * counts;
12845 unsigned long hn;
12846 bfd_vma si;
12847 unsigned long maxlength = 0;
12848 unsigned long nzero_counts = 0;
12849 unsigned long nsyms = 0;
12850 char *visited;
12851
12852 printf (ngettext ("\nHistogram for bucket list length "
12853 "(total of %lu bucket):\n",
12854 "\nHistogram for bucket list length "
12855 "(total of %lu buckets):\n",
12856 (unsigned long) filedata->nbuckets),
12857 (unsigned long) filedata->nbuckets);
12858
12859 lengths = (unsigned long *) calloc (filedata->nbuckets,
12860 sizeof (*lengths));
12861 if (lengths == NULL)
12862 {
12863 error (_("Out of memory allocating space for histogram buckets\n"));
12864 goto err_out;
12865 }
12866 visited = xcmalloc (filedata->nchains, 1);
12867 memset (visited, 0, filedata->nchains);
12868
12869 printf (_(" Length Number %% of total Coverage\n"));
12870 for (hn = 0; hn < filedata->nbuckets; ++hn)
12871 {
12872 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
12873 {
12874 ++nsyms;
12875 if (maxlength < ++lengths[hn])
12876 ++maxlength;
12877 if (si >= filedata->nchains || visited[si])
12878 {
12879 error (_("histogram chain is corrupt\n"));
12880 break;
12881 }
12882 visited[si] = 1;
12883 }
12884 }
12885 free (visited);
12886
12887 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
12888 if (counts == NULL)
12889 {
12890 free (lengths);
12891 error (_("Out of memory allocating space for histogram counts\n"));
12892 goto err_out;
12893 }
12894
12895 for (hn = 0; hn < filedata->nbuckets; ++hn)
12896 ++counts[lengths[hn]];
12897
12898 if (filedata->nbuckets > 0)
12899 {
12900 unsigned long i;
12901 printf (" 0 %-10lu (%5.1f%%)\n",
12902 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
12903 for (i = 1; i <= maxlength; ++i)
12904 {
12905 nzero_counts += counts[i] * i;
12906 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
12907 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
12908 (nzero_counts * 100.0) / nsyms);
12909 }
12910 }
12911
12912 free (counts);
12913 free (lengths);
12914 }
12915
12916 free (filedata->buckets);
12917 filedata->buckets = NULL;
12918 filedata->nbuckets = 0;
12919 free (filedata->chains);
12920 filedata->chains = NULL;
12921
12922 if (do_histogram && filedata->gnubuckets != NULL)
12923 {
12924 unsigned long * lengths;
12925 unsigned long * counts;
12926 unsigned long hn;
12927 unsigned long maxlength = 0;
12928 unsigned long nzero_counts = 0;
12929 unsigned long nsyms = 0;
12930
12931 printf (ngettext ("\nHistogram for `%s' bucket list length "
12932 "(total of %lu bucket):\n",
12933 "\nHistogram for `%s' bucket list length "
12934 "(total of %lu buckets):\n",
12935 (unsigned long) filedata->ngnubuckets),
12936 GNU_HASH_SECTION_NAME (filedata),
12937 (unsigned long) filedata->ngnubuckets);
12938
12939 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
12940 sizeof (*lengths));
12941 if (lengths == NULL)
12942 {
12943 error (_("Out of memory allocating space for gnu histogram buckets\n"));
12944 goto err_out;
12945 }
12946
12947 printf (_(" Length Number %% of total Coverage\n"));
12948
12949 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12950 if (filedata->gnubuckets[hn] != 0)
12951 {
12952 bfd_vma off, length = 1;
12953
12954 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
12955 /* PR 17531 file: 010-77222-0.004. */
12956 off < filedata->ngnuchains
12957 && (filedata->gnuchains[off] & 1) == 0;
12958 ++off)
12959 ++length;
12960 lengths[hn] = length;
12961 if (length > maxlength)
12962 maxlength = length;
12963 nsyms += length;
12964 }
12965
12966 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
12967 if (counts == NULL)
12968 {
12969 free (lengths);
12970 error (_("Out of memory allocating space for gnu histogram counts\n"));
12971 goto err_out;
12972 }
12973
12974 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12975 ++counts[lengths[hn]];
12976
12977 if (filedata->ngnubuckets > 0)
12978 {
12979 unsigned long j;
12980 printf (" 0 %-10lu (%5.1f%%)\n",
12981 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
12982 for (j = 1; j <= maxlength; ++j)
12983 {
12984 nzero_counts += counts[j] * j;
12985 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
12986 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
12987 (nzero_counts * 100.0) / nsyms);
12988 }
12989 }
12990
12991 free (counts);
12992 free (lengths);
12993 }
12994 free (filedata->gnubuckets);
12995 filedata->gnubuckets = NULL;
12996 filedata->ngnubuckets = 0;
12997 free (filedata->gnuchains);
12998 filedata->gnuchains = NULL;
12999 filedata->ngnuchains = 0;
13000 free (filedata->mipsxlat);
13001 filedata->mipsxlat = NULL;
13002 return true;
13003
13004 err_out:
13005 free (filedata->gnubuckets);
13006 filedata->gnubuckets = NULL;
13007 filedata->ngnubuckets = 0;
13008 free (filedata->gnuchains);
13009 filedata->gnuchains = NULL;
13010 filedata->ngnuchains = 0;
13011 free (filedata->mipsxlat);
13012 filedata->mipsxlat = NULL;
13013 free (filedata->buckets);
13014 filedata->buckets = NULL;
13015 filedata->nbuckets = 0;
13016 free (filedata->chains);
13017 filedata->chains = NULL;
13018 return false;
13019 }
13020
13021 static bool
13022 process_syminfo (Filedata * filedata)
13023 {
13024 unsigned int i;
13025
13026 if (filedata->dynamic_syminfo == NULL
13027 || !do_dynamic)
13028 /* No syminfo, this is ok. */
13029 return true;
13030
13031 /* There better should be a dynamic symbol section. */
13032 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13033 return false;
13034
13035 if (filedata->is_separate)
13036 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13037 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13038 filedata->dynamic_syminfo_nent),
13039 filedata->file_name,
13040 filedata->dynamic_syminfo_offset,
13041 filedata->dynamic_syminfo_nent);
13042 else
13043 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13044 "contains %d entry:\n",
13045 "\nDynamic info segment at offset 0x%lx "
13046 "contains %d entries:\n",
13047 filedata->dynamic_syminfo_nent),
13048 filedata->dynamic_syminfo_offset,
13049 filedata->dynamic_syminfo_nent);
13050
13051 printf (_(" Num: Name BoundTo Flags\n"));
13052 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13053 {
13054 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13055
13056 printf ("%4d: ", i);
13057 if (i >= filedata->num_dynamic_syms)
13058 printf (_("<corrupt index>"));
13059 else if (VALID_DYNAMIC_NAME (filedata, filedata->dynamic_symbols[i].st_name))
13060 print_symbol (30, GET_DYNAMIC_NAME (filedata,
13061 filedata->dynamic_symbols[i].st_name));
13062 else
13063 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13064 putchar (' ');
13065
13066 switch (filedata->dynamic_syminfo[i].si_boundto)
13067 {
13068 case SYMINFO_BT_SELF:
13069 fputs ("SELF ", stdout);
13070 break;
13071 case SYMINFO_BT_PARENT:
13072 fputs ("PARENT ", stdout);
13073 break;
13074 default:
13075 if (filedata->dynamic_syminfo[i].si_boundto > 0
13076 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13077 && VALID_DYNAMIC_NAME (filedata,
13078 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13079 {
13080 print_symbol (10, GET_DYNAMIC_NAME (filedata,
13081 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13082 putchar (' ' );
13083 }
13084 else
13085 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13086 break;
13087 }
13088
13089 if (flags & SYMINFO_FLG_DIRECT)
13090 printf (" DIRECT");
13091 if (flags & SYMINFO_FLG_PASSTHRU)
13092 printf (" PASSTHRU");
13093 if (flags & SYMINFO_FLG_COPY)
13094 printf (" COPY");
13095 if (flags & SYMINFO_FLG_LAZYLOAD)
13096 printf (" LAZYLOAD");
13097
13098 puts ("");
13099 }
13100
13101 return true;
13102 }
13103
13104 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13105 is contained by the region START .. END. The types of ADDR, START
13106 and END should all be the same. Note both ADDR + NELEM and END
13107 point to just beyond the end of the regions that are being tested. */
13108 #define IN_RANGE(START,END,ADDR,NELEM) \
13109 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13110
13111 /* Check to see if the given reloc needs to be handled in a target specific
13112 manner. If so then process the reloc and return TRUE otherwise return
13113 FALSE.
13114
13115 If called with reloc == NULL, then this is a signal that reloc processing
13116 for the current section has finished, and any saved state should be
13117 discarded. */
13118
13119 static bool
13120 target_specific_reloc_handling (Filedata * filedata,
13121 Elf_Internal_Rela * reloc,
13122 unsigned char * start,
13123 unsigned char * end,
13124 Elf_Internal_Sym * symtab,
13125 unsigned long num_syms)
13126 {
13127 unsigned int reloc_type = 0;
13128 unsigned long sym_index = 0;
13129
13130 if (reloc)
13131 {
13132 reloc_type = get_reloc_type (filedata, reloc->r_info);
13133 sym_index = get_reloc_symindex (reloc->r_info);
13134 }
13135
13136 switch (filedata->file_header.e_machine)
13137 {
13138 case EM_MSP430:
13139 case EM_MSP430_OLD:
13140 {
13141 static Elf_Internal_Sym * saved_sym = NULL;
13142
13143 if (reloc == NULL)
13144 {
13145 saved_sym = NULL;
13146 return true;
13147 }
13148
13149 switch (reloc_type)
13150 {
13151 case 10: /* R_MSP430_SYM_DIFF */
13152 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13153 if (uses_msp430x_relocs (filedata))
13154 break;
13155 /* Fall through. */
13156 case 21: /* R_MSP430X_SYM_DIFF */
13157 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13158 /* PR 21139. */
13159 if (sym_index >= num_syms)
13160 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13161 sym_index);
13162 else
13163 saved_sym = symtab + sym_index;
13164 return true;
13165
13166 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13167 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13168 goto handle_sym_diff;
13169
13170 case 5: /* R_MSP430_16_BYTE */
13171 case 9: /* R_MSP430_8 */
13172 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13173 if (uses_msp430x_relocs (filedata))
13174 break;
13175 goto handle_sym_diff;
13176
13177 case 2: /* R_MSP430_ABS16 */
13178 case 15: /* R_MSP430X_ABS16 */
13179 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13180 if (! uses_msp430x_relocs (filedata))
13181 break;
13182 goto handle_sym_diff;
13183
13184 handle_sym_diff:
13185 if (saved_sym != NULL)
13186 {
13187 bfd_vma value;
13188 unsigned int reloc_size = 0;
13189 int leb_ret = 0;
13190 switch (reloc_type)
13191 {
13192 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13193 reloc_size = 4;
13194 break;
13195 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13196 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13197 if (reloc->r_offset < (size_t) (end - start))
13198 read_leb128 (start + reloc->r_offset, end, false,
13199 &reloc_size, &leb_ret);
13200 break;
13201 default:
13202 reloc_size = 2;
13203 break;
13204 }
13205
13206 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
13207 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13208 "ULEB128 value\n"),
13209 (long) reloc->r_offset);
13210 else if (sym_index >= num_syms)
13211 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13212 sym_index);
13213 else
13214 {
13215 value = reloc->r_addend + (symtab[sym_index].st_value
13216 - saved_sym->st_value);
13217
13218 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13219 byte_put (start + reloc->r_offset, value, reloc_size);
13220 else
13221 /* PR 21137 */
13222 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13223 (long) reloc->r_offset);
13224 }
13225
13226 saved_sym = NULL;
13227 return true;
13228 }
13229 break;
13230
13231 default:
13232 if (saved_sym != NULL)
13233 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13234 break;
13235 }
13236 break;
13237 }
13238
13239 case EM_MN10300:
13240 case EM_CYGNUS_MN10300:
13241 {
13242 static Elf_Internal_Sym * saved_sym = NULL;
13243
13244 if (reloc == NULL)
13245 {
13246 saved_sym = NULL;
13247 return true;
13248 }
13249
13250 switch (reloc_type)
13251 {
13252 case 34: /* R_MN10300_ALIGN */
13253 return true;
13254 case 33: /* R_MN10300_SYM_DIFF */
13255 if (sym_index >= num_syms)
13256 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13257 sym_index);
13258 else
13259 saved_sym = symtab + sym_index;
13260 return true;
13261
13262 case 1: /* R_MN10300_32 */
13263 case 2: /* R_MN10300_16 */
13264 if (saved_sym != NULL)
13265 {
13266 int reloc_size = reloc_type == 1 ? 4 : 2;
13267 bfd_vma value;
13268
13269 if (sym_index >= num_syms)
13270 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13271 sym_index);
13272 else
13273 {
13274 value = reloc->r_addend + (symtab[sym_index].st_value
13275 - saved_sym->st_value);
13276
13277 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13278 byte_put (start + reloc->r_offset, value, reloc_size);
13279 else
13280 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13281 (long) reloc->r_offset);
13282 }
13283
13284 saved_sym = NULL;
13285 return true;
13286 }
13287 break;
13288 default:
13289 if (saved_sym != NULL)
13290 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
13291 break;
13292 }
13293 break;
13294 }
13295
13296 case EM_RL78:
13297 {
13298 static bfd_vma saved_sym1 = 0;
13299 static bfd_vma saved_sym2 = 0;
13300 static bfd_vma value;
13301
13302 if (reloc == NULL)
13303 {
13304 saved_sym1 = saved_sym2 = 0;
13305 return true;
13306 }
13307
13308 switch (reloc_type)
13309 {
13310 case 0x80: /* R_RL78_SYM. */
13311 saved_sym1 = saved_sym2;
13312 if (sym_index >= num_syms)
13313 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13314 sym_index);
13315 else
13316 {
13317 saved_sym2 = symtab[sym_index].st_value;
13318 saved_sym2 += reloc->r_addend;
13319 }
13320 return true;
13321
13322 case 0x83: /* R_RL78_OPsub. */
13323 value = saved_sym1 - saved_sym2;
13324 saved_sym2 = saved_sym1 = 0;
13325 return true;
13326 break;
13327
13328 case 0x41: /* R_RL78_ABS32. */
13329 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
13330 byte_put (start + reloc->r_offset, value, 4);
13331 else
13332 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13333 (long) reloc->r_offset);
13334 value = 0;
13335 return true;
13336
13337 case 0x43: /* R_RL78_ABS16. */
13338 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
13339 byte_put (start + reloc->r_offset, value, 2);
13340 else
13341 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13342 (long) reloc->r_offset);
13343 value = 0;
13344 return true;
13345
13346 default:
13347 break;
13348 }
13349 break;
13350 }
13351 }
13352
13353 return false;
13354 }
13355
13356 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13357 DWARF debug sections. This is a target specific test. Note - we do not
13358 go through the whole including-target-headers-multiple-times route, (as
13359 we have already done with <elf/h8.h>) because this would become very
13360 messy and even then this function would have to contain target specific
13361 information (the names of the relocs instead of their numeric values).
13362 FIXME: This is not the correct way to solve this problem. The proper way
13363 is to have target specific reloc sizing and typing functions created by
13364 the reloc-macros.h header, in the same way that it already creates the
13365 reloc naming functions. */
13366
13367 static bool
13368 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13369 {
13370 /* Please keep this table alpha-sorted for ease of visual lookup. */
13371 switch (filedata->file_header.e_machine)
13372 {
13373 case EM_386:
13374 case EM_IAMCU:
13375 return reloc_type == 1; /* R_386_32. */
13376 case EM_68K:
13377 return reloc_type == 1; /* R_68K_32. */
13378 case EM_860:
13379 return reloc_type == 1; /* R_860_32. */
13380 case EM_960:
13381 return reloc_type == 2; /* R_960_32. */
13382 case EM_AARCH64:
13383 return (reloc_type == 258
13384 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
13385 case EM_BPF:
13386 return reloc_type == 11; /* R_BPF_DATA_32 */
13387 case EM_ADAPTEVA_EPIPHANY:
13388 return reloc_type == 3;
13389 case EM_ALPHA:
13390 return reloc_type == 1; /* R_ALPHA_REFLONG. */
13391 case EM_ARC:
13392 return reloc_type == 1; /* R_ARC_32. */
13393 case EM_ARC_COMPACT:
13394 case EM_ARC_COMPACT2:
13395 return reloc_type == 4; /* R_ARC_32. */
13396 case EM_ARM:
13397 return reloc_type == 2; /* R_ARM_ABS32 */
13398 case EM_AVR_OLD:
13399 case EM_AVR:
13400 return reloc_type == 1;
13401 case EM_BLACKFIN:
13402 return reloc_type == 0x12; /* R_byte4_data. */
13403 case EM_CRIS:
13404 return reloc_type == 3; /* R_CRIS_32. */
13405 case EM_CR16:
13406 return reloc_type == 3; /* R_CR16_NUM32. */
13407 case EM_CRX:
13408 return reloc_type == 15; /* R_CRX_NUM32. */
13409 case EM_CSKY:
13410 return reloc_type == 1; /* R_CKCORE_ADDR32. */
13411 case EM_CYGNUS_FRV:
13412 return reloc_type == 1;
13413 case EM_CYGNUS_D10V:
13414 case EM_D10V:
13415 return reloc_type == 6; /* R_D10V_32. */
13416 case EM_CYGNUS_D30V:
13417 case EM_D30V:
13418 return reloc_type == 12; /* R_D30V_32_NORMAL. */
13419 case EM_DLX:
13420 return reloc_type == 3; /* R_DLX_RELOC_32. */
13421 case EM_CYGNUS_FR30:
13422 case EM_FR30:
13423 return reloc_type == 3; /* R_FR30_32. */
13424 case EM_FT32:
13425 return reloc_type == 1; /* R_FT32_32. */
13426 case EM_H8S:
13427 case EM_H8_300:
13428 case EM_H8_300H:
13429 return reloc_type == 1; /* R_H8_DIR32. */
13430 case EM_IA_64:
13431 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
13432 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
13433 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
13434 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
13435 case EM_IP2K_OLD:
13436 case EM_IP2K:
13437 return reloc_type == 2; /* R_IP2K_32. */
13438 case EM_IQ2000:
13439 return reloc_type == 2; /* R_IQ2000_32. */
13440 case EM_LATTICEMICO32:
13441 return reloc_type == 3; /* R_LM32_32. */
13442 case EM_M32C_OLD:
13443 case EM_M32C:
13444 return reloc_type == 3; /* R_M32C_32. */
13445 case EM_M32R:
13446 return reloc_type == 34; /* R_M32R_32_RELA. */
13447 case EM_68HC11:
13448 case EM_68HC12:
13449 return reloc_type == 6; /* R_M68HC11_32. */
13450 case EM_S12Z:
13451 return reloc_type == 7 || /* R_S12Z_EXT32 */
13452 reloc_type == 6; /* R_S12Z_CW32. */
13453 case EM_MCORE:
13454 return reloc_type == 1; /* R_MCORE_ADDR32. */
13455 case EM_CYGNUS_MEP:
13456 return reloc_type == 4; /* R_MEP_32. */
13457 case EM_METAG:
13458 return reloc_type == 2; /* R_METAG_ADDR32. */
13459 case EM_MICROBLAZE:
13460 return reloc_type == 1; /* R_MICROBLAZE_32. */
13461 case EM_MIPS:
13462 return reloc_type == 2; /* R_MIPS_32. */
13463 case EM_MMIX:
13464 return reloc_type == 4; /* R_MMIX_32. */
13465 case EM_CYGNUS_MN10200:
13466 case EM_MN10200:
13467 return reloc_type == 1; /* R_MN10200_32. */
13468 case EM_CYGNUS_MN10300:
13469 case EM_MN10300:
13470 return reloc_type == 1; /* R_MN10300_32. */
13471 case EM_MOXIE:
13472 return reloc_type == 1; /* R_MOXIE_32. */
13473 case EM_MSP430_OLD:
13474 case EM_MSP430:
13475 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
13476 case EM_MT:
13477 return reloc_type == 2; /* R_MT_32. */
13478 case EM_NDS32:
13479 return reloc_type == 20; /* R_NDS32_RELA. */
13480 case EM_ALTERA_NIOS2:
13481 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
13482 case EM_NIOS32:
13483 return reloc_type == 1; /* R_NIOS_32. */
13484 case EM_OR1K:
13485 return reloc_type == 1; /* R_OR1K_32. */
13486 case EM_PARISC:
13487 return (reloc_type == 1 /* R_PARISC_DIR32. */
13488 || reloc_type == 2 /* R_PARISC_DIR21L. */
13489 || reloc_type == 41); /* R_PARISC_SECREL32. */
13490 case EM_PJ:
13491 case EM_PJ_OLD:
13492 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
13493 case EM_PPC64:
13494 return reloc_type == 1; /* R_PPC64_ADDR32. */
13495 case EM_PPC:
13496 return reloc_type == 1; /* R_PPC_ADDR32. */
13497 case EM_TI_PRU:
13498 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
13499 case EM_RISCV:
13500 return reloc_type == 1; /* R_RISCV_32. */
13501 case EM_RL78:
13502 return reloc_type == 1; /* R_RL78_DIR32. */
13503 case EM_RX:
13504 return reloc_type == 1; /* R_RX_DIR32. */
13505 case EM_S370:
13506 return reloc_type == 1; /* R_I370_ADDR31. */
13507 case EM_S390_OLD:
13508 case EM_S390:
13509 return reloc_type == 4; /* R_S390_32. */
13510 case EM_SCORE:
13511 return reloc_type == 8; /* R_SCORE_ABS32. */
13512 case EM_SH:
13513 return reloc_type == 1; /* R_SH_DIR32. */
13514 case EM_SPARC32PLUS:
13515 case EM_SPARCV9:
13516 case EM_SPARC:
13517 return reloc_type == 3 /* R_SPARC_32. */
13518 || reloc_type == 23; /* R_SPARC_UA32. */
13519 case EM_SPU:
13520 return reloc_type == 6; /* R_SPU_ADDR32 */
13521 case EM_TI_C6000:
13522 return reloc_type == 1; /* R_C6000_ABS32. */
13523 case EM_TILEGX:
13524 return reloc_type == 2; /* R_TILEGX_32. */
13525 case EM_TILEPRO:
13526 return reloc_type == 1; /* R_TILEPRO_32. */
13527 case EM_CYGNUS_V850:
13528 case EM_V850:
13529 return reloc_type == 6; /* R_V850_ABS32. */
13530 case EM_V800:
13531 return reloc_type == 0x33; /* R_V810_WORD. */
13532 case EM_VAX:
13533 return reloc_type == 1; /* R_VAX_32. */
13534 case EM_VISIUM:
13535 return reloc_type == 3; /* R_VISIUM_32. */
13536 case EM_WEBASSEMBLY:
13537 return reloc_type == 1; /* R_WASM32_32. */
13538 case EM_X86_64:
13539 case EM_L1OM:
13540 case EM_K1OM:
13541 return reloc_type == 10; /* R_X86_64_32. */
13542 case EM_XC16X:
13543 case EM_C166:
13544 return reloc_type == 3; /* R_XC16C_ABS_32. */
13545 case EM_XGATE:
13546 return reloc_type == 4; /* R_XGATE_32. */
13547 case EM_XSTORMY16:
13548 return reloc_type == 1; /* R_XSTROMY16_32. */
13549 case EM_XTENSA_OLD:
13550 case EM_XTENSA:
13551 return reloc_type == 1; /* R_XTENSA_32. */
13552 case EM_Z80:
13553 return reloc_type == 6; /* R_Z80_32. */
13554 default:
13555 {
13556 static unsigned int prev_warn = 0;
13557
13558 /* Avoid repeating the same warning multiple times. */
13559 if (prev_warn != filedata->file_header.e_machine)
13560 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
13561 filedata->file_header.e_machine);
13562 prev_warn = filedata->file_header.e_machine;
13563 return false;
13564 }
13565 }
13566 }
13567
13568 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13569 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
13570
13571 static bool
13572 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13573 {
13574 switch (filedata->file_header.e_machine)
13575 /* Please keep this table alpha-sorted for ease of visual lookup. */
13576 {
13577 case EM_386:
13578 case EM_IAMCU:
13579 return reloc_type == 2; /* R_386_PC32. */
13580 case EM_68K:
13581 return reloc_type == 4; /* R_68K_PC32. */
13582 case EM_AARCH64:
13583 return reloc_type == 261; /* R_AARCH64_PREL32 */
13584 case EM_ADAPTEVA_EPIPHANY:
13585 return reloc_type == 6;
13586 case EM_ALPHA:
13587 return reloc_type == 10; /* R_ALPHA_SREL32. */
13588 case EM_ARC_COMPACT:
13589 case EM_ARC_COMPACT2:
13590 return reloc_type == 49; /* R_ARC_32_PCREL. */
13591 case EM_ARM:
13592 return reloc_type == 3; /* R_ARM_REL32 */
13593 case EM_AVR_OLD:
13594 case EM_AVR:
13595 return reloc_type == 36; /* R_AVR_32_PCREL. */
13596 case EM_MICROBLAZE:
13597 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
13598 case EM_OR1K:
13599 return reloc_type == 9; /* R_OR1K_32_PCREL. */
13600 case EM_PARISC:
13601 return reloc_type == 9; /* R_PARISC_PCREL32. */
13602 case EM_PPC:
13603 return reloc_type == 26; /* R_PPC_REL32. */
13604 case EM_PPC64:
13605 return reloc_type == 26; /* R_PPC64_REL32. */
13606 case EM_RISCV:
13607 return reloc_type == 57; /* R_RISCV_32_PCREL. */
13608 case EM_S390_OLD:
13609 case EM_S390:
13610 return reloc_type == 5; /* R_390_PC32. */
13611 case EM_SH:
13612 return reloc_type == 2; /* R_SH_REL32. */
13613 case EM_SPARC32PLUS:
13614 case EM_SPARCV9:
13615 case EM_SPARC:
13616 return reloc_type == 6; /* R_SPARC_DISP32. */
13617 case EM_SPU:
13618 return reloc_type == 13; /* R_SPU_REL32. */
13619 case EM_TILEGX:
13620 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
13621 case EM_TILEPRO:
13622 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
13623 case EM_VISIUM:
13624 return reloc_type == 6; /* R_VISIUM_32_PCREL */
13625 case EM_X86_64:
13626 case EM_L1OM:
13627 case EM_K1OM:
13628 return reloc_type == 2; /* R_X86_64_PC32. */
13629 case EM_VAX:
13630 return reloc_type == 4; /* R_VAX_PCREL32. */
13631 case EM_XTENSA_OLD:
13632 case EM_XTENSA:
13633 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
13634 default:
13635 /* Do not abort or issue an error message here. Not all targets use
13636 pc-relative 32-bit relocs in their DWARF debug information and we
13637 have already tested for target coverage in is_32bit_abs_reloc. A
13638 more helpful warning message will be generated by apply_relocations
13639 anyway, so just return. */
13640 return false;
13641 }
13642 }
13643
13644 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13645 a 64-bit absolute RELA relocation used in DWARF debug sections. */
13646
13647 static bool
13648 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13649 {
13650 switch (filedata->file_header.e_machine)
13651 {
13652 case EM_AARCH64:
13653 return reloc_type == 257; /* R_AARCH64_ABS64. */
13654 case EM_ALPHA:
13655 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
13656 case EM_IA_64:
13657 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
13658 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
13659 case EM_PARISC:
13660 return reloc_type == 80; /* R_PARISC_DIR64. */
13661 case EM_PPC64:
13662 return reloc_type == 38; /* R_PPC64_ADDR64. */
13663 case EM_RISCV:
13664 return reloc_type == 2; /* R_RISCV_64. */
13665 case EM_SPARC32PLUS:
13666 case EM_SPARCV9:
13667 case EM_SPARC:
13668 return reloc_type == 32 /* R_SPARC_64. */
13669 || reloc_type == 54; /* R_SPARC_UA64. */
13670 case EM_X86_64:
13671 case EM_L1OM:
13672 case EM_K1OM:
13673 return reloc_type == 1; /* R_X86_64_64. */
13674 case EM_S390_OLD:
13675 case EM_S390:
13676 return reloc_type == 22; /* R_S390_64. */
13677 case EM_TILEGX:
13678 return reloc_type == 1; /* R_TILEGX_64. */
13679 case EM_MIPS:
13680 return reloc_type == 18; /* R_MIPS_64. */
13681 default:
13682 return false;
13683 }
13684 }
13685
13686 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
13687 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
13688
13689 static bool
13690 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13691 {
13692 switch (filedata->file_header.e_machine)
13693 {
13694 case EM_AARCH64:
13695 return reloc_type == 260; /* R_AARCH64_PREL64. */
13696 case EM_ALPHA:
13697 return reloc_type == 11; /* R_ALPHA_SREL64. */
13698 case EM_IA_64:
13699 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
13700 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
13701 case EM_PARISC:
13702 return reloc_type == 72; /* R_PARISC_PCREL64. */
13703 case EM_PPC64:
13704 return reloc_type == 44; /* R_PPC64_REL64. */
13705 case EM_SPARC32PLUS:
13706 case EM_SPARCV9:
13707 case EM_SPARC:
13708 return reloc_type == 46; /* R_SPARC_DISP64. */
13709 case EM_X86_64:
13710 case EM_L1OM:
13711 case EM_K1OM:
13712 return reloc_type == 24; /* R_X86_64_PC64. */
13713 case EM_S390_OLD:
13714 case EM_S390:
13715 return reloc_type == 23; /* R_S390_PC64. */
13716 case EM_TILEGX:
13717 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
13718 default:
13719 return false;
13720 }
13721 }
13722
13723 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13724 a 24-bit absolute RELA relocation used in DWARF debug sections. */
13725
13726 static bool
13727 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13728 {
13729 switch (filedata->file_header.e_machine)
13730 {
13731 case EM_CYGNUS_MN10200:
13732 case EM_MN10200:
13733 return reloc_type == 4; /* R_MN10200_24. */
13734 case EM_FT32:
13735 return reloc_type == 5; /* R_FT32_20. */
13736 case EM_Z80:
13737 return reloc_type == 5; /* R_Z80_24. */
13738 default:
13739 return false;
13740 }
13741 }
13742
13743 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13744 a 16-bit absolute RELA relocation used in DWARF debug sections. */
13745
13746 static bool
13747 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13748 {
13749 /* Please keep this table alpha-sorted for ease of visual lookup. */
13750 switch (filedata->file_header.e_machine)
13751 {
13752 case EM_ARC:
13753 case EM_ARC_COMPACT:
13754 case EM_ARC_COMPACT2:
13755 return reloc_type == 2; /* R_ARC_16. */
13756 case EM_ADAPTEVA_EPIPHANY:
13757 return reloc_type == 5;
13758 case EM_AVR_OLD:
13759 case EM_AVR:
13760 return reloc_type == 4; /* R_AVR_16. */
13761 case EM_CYGNUS_D10V:
13762 case EM_D10V:
13763 return reloc_type == 3; /* R_D10V_16. */
13764 case EM_FT32:
13765 return reloc_type == 2; /* R_FT32_16. */
13766 case EM_H8S:
13767 case EM_H8_300:
13768 case EM_H8_300H:
13769 return reloc_type == R_H8_DIR16;
13770 case EM_IP2K_OLD:
13771 case EM_IP2K:
13772 return reloc_type == 1; /* R_IP2K_16. */
13773 case EM_M32C_OLD:
13774 case EM_M32C:
13775 return reloc_type == 1; /* R_M32C_16 */
13776 case EM_CYGNUS_MN10200:
13777 case EM_MN10200:
13778 return reloc_type == 2; /* R_MN10200_16. */
13779 case EM_CYGNUS_MN10300:
13780 case EM_MN10300:
13781 return reloc_type == 2; /* R_MN10300_16. */
13782 case EM_MSP430:
13783 if (uses_msp430x_relocs (filedata))
13784 return reloc_type == 2; /* R_MSP430_ABS16. */
13785 /* Fall through. */
13786 case EM_MSP430_OLD:
13787 return reloc_type == 5; /* R_MSP430_16_BYTE. */
13788 case EM_NDS32:
13789 return reloc_type == 19; /* R_NDS32_RELA. */
13790 case EM_ALTERA_NIOS2:
13791 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
13792 case EM_NIOS32:
13793 return reloc_type == 9; /* R_NIOS_16. */
13794 case EM_OR1K:
13795 return reloc_type == 2; /* R_OR1K_16. */
13796 case EM_RISCV:
13797 return reloc_type == 55; /* R_RISCV_SET16. */
13798 case EM_TI_PRU:
13799 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
13800 case EM_TI_C6000:
13801 return reloc_type == 2; /* R_C6000_ABS16. */
13802 case EM_VISIUM:
13803 return reloc_type == 2; /* R_VISIUM_16. */
13804 case EM_XC16X:
13805 case EM_C166:
13806 return reloc_type == 2; /* R_XC16C_ABS_16. */
13807 case EM_XGATE:
13808 return reloc_type == 3; /* R_XGATE_16. */
13809 case EM_Z80:
13810 return reloc_type == 4; /* R_Z80_16. */
13811 default:
13812 return false;
13813 }
13814 }
13815
13816 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13817 a 8-bit absolute RELA relocation used in DWARF debug sections. */
13818
13819 static bool
13820 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13821 {
13822 switch (filedata->file_header.e_machine)
13823 {
13824 case EM_RISCV:
13825 return reloc_type == 54; /* R_RISCV_SET8. */
13826 case EM_Z80:
13827 return reloc_type == 1; /* R_Z80_8. */
13828 default:
13829 return false;
13830 }
13831 }
13832
13833 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13834 a 6-bit absolute RELA relocation used in DWARF debug sections. */
13835
13836 static bool
13837 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13838 {
13839 switch (filedata->file_header.e_machine)
13840 {
13841 case EM_RISCV:
13842 return reloc_type == 53; /* R_RISCV_SET6. */
13843 default:
13844 return false;
13845 }
13846 }
13847
13848 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13849 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
13850
13851 static bool
13852 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13853 {
13854 /* Please keep this table alpha-sorted for ease of visual lookup. */
13855 switch (filedata->file_header.e_machine)
13856 {
13857 case EM_RISCV:
13858 return reloc_type == 35; /* R_RISCV_ADD32. */
13859 default:
13860 return false;
13861 }
13862 }
13863
13864 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13865 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
13866
13867 static bool
13868 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13869 {
13870 /* Please keep this table alpha-sorted for ease of visual lookup. */
13871 switch (filedata->file_header.e_machine)
13872 {
13873 case EM_RISCV:
13874 return reloc_type == 39; /* R_RISCV_SUB32. */
13875 default:
13876 return false;
13877 }
13878 }
13879
13880 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13881 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
13882
13883 static bool
13884 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13885 {
13886 /* Please keep this table alpha-sorted for ease of visual lookup. */
13887 switch (filedata->file_header.e_machine)
13888 {
13889 case EM_RISCV:
13890 return reloc_type == 36; /* R_RISCV_ADD64. */
13891 default:
13892 return false;
13893 }
13894 }
13895
13896 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13897 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
13898
13899 static bool
13900 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13901 {
13902 /* Please keep this table alpha-sorted for ease of visual lookup. */
13903 switch (filedata->file_header.e_machine)
13904 {
13905 case EM_RISCV:
13906 return reloc_type == 40; /* R_RISCV_SUB64. */
13907 default:
13908 return false;
13909 }
13910 }
13911
13912 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13913 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
13914
13915 static bool
13916 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13917 {
13918 /* Please keep this table alpha-sorted for ease of visual lookup. */
13919 switch (filedata->file_header.e_machine)
13920 {
13921 case EM_RISCV:
13922 return reloc_type == 34; /* R_RISCV_ADD16. */
13923 default:
13924 return false;
13925 }
13926 }
13927
13928 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13929 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
13930
13931 static bool
13932 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13933 {
13934 /* Please keep this table alpha-sorted for ease of visual lookup. */
13935 switch (filedata->file_header.e_machine)
13936 {
13937 case EM_RISCV:
13938 return reloc_type == 38; /* R_RISCV_SUB16. */
13939 default:
13940 return false;
13941 }
13942 }
13943
13944 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13945 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
13946
13947 static bool
13948 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13949 {
13950 /* Please keep this table alpha-sorted for ease of visual lookup. */
13951 switch (filedata->file_header.e_machine)
13952 {
13953 case EM_RISCV:
13954 return reloc_type == 33; /* R_RISCV_ADD8. */
13955 default:
13956 return false;
13957 }
13958 }
13959
13960 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13961 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
13962
13963 static bool
13964 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13965 {
13966 /* Please keep this table alpha-sorted for ease of visual lookup. */
13967 switch (filedata->file_header.e_machine)
13968 {
13969 case EM_RISCV:
13970 return reloc_type == 37; /* R_RISCV_SUB8. */
13971 default:
13972 return false;
13973 }
13974 }
13975
13976 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13977 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
13978
13979 static bool
13980 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13981 {
13982 switch (filedata->file_header.e_machine)
13983 {
13984 case EM_RISCV:
13985 return reloc_type == 52; /* R_RISCV_SUB6. */
13986 default:
13987 return false;
13988 }
13989 }
13990
13991 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
13992 relocation entries (possibly formerly used for SHT_GROUP sections). */
13993
13994 static bool
13995 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
13996 {
13997 switch (filedata->file_header.e_machine)
13998 {
13999 case EM_386: /* R_386_NONE. */
14000 case EM_68K: /* R_68K_NONE. */
14001 case EM_ADAPTEVA_EPIPHANY:
14002 case EM_ALPHA: /* R_ALPHA_NONE. */
14003 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14004 case EM_ARC: /* R_ARC_NONE. */
14005 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14006 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14007 case EM_ARM: /* R_ARM_NONE. */
14008 case EM_C166: /* R_XC16X_NONE. */
14009 case EM_CRIS: /* R_CRIS_NONE. */
14010 case EM_FT32: /* R_FT32_NONE. */
14011 case EM_IA_64: /* R_IA64_NONE. */
14012 case EM_K1OM: /* R_X86_64_NONE. */
14013 case EM_L1OM: /* R_X86_64_NONE. */
14014 case EM_M32R: /* R_M32R_NONE. */
14015 case EM_MIPS: /* R_MIPS_NONE. */
14016 case EM_MN10300: /* R_MN10300_NONE. */
14017 case EM_MOXIE: /* R_MOXIE_NONE. */
14018 case EM_NIOS32: /* R_NIOS_NONE. */
14019 case EM_OR1K: /* R_OR1K_NONE. */
14020 case EM_PARISC: /* R_PARISC_NONE. */
14021 case EM_PPC64: /* R_PPC64_NONE. */
14022 case EM_PPC: /* R_PPC_NONE. */
14023 case EM_RISCV: /* R_RISCV_NONE. */
14024 case EM_S390: /* R_390_NONE. */
14025 case EM_S390_OLD:
14026 case EM_SH: /* R_SH_NONE. */
14027 case EM_SPARC32PLUS:
14028 case EM_SPARC: /* R_SPARC_NONE. */
14029 case EM_SPARCV9:
14030 case EM_TILEGX: /* R_TILEGX_NONE. */
14031 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14032 case EM_TI_C6000:/* R_C6000_NONE. */
14033 case EM_X86_64: /* R_X86_64_NONE. */
14034 case EM_XC16X:
14035 case EM_Z80: /* R_Z80_NONE. */
14036 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14037 return reloc_type == 0;
14038
14039 case EM_AARCH64:
14040 return reloc_type == 0 || reloc_type == 256;
14041 case EM_AVR_OLD:
14042 case EM_AVR:
14043 return (reloc_type == 0 /* R_AVR_NONE. */
14044 || reloc_type == 30 /* R_AVR_DIFF8. */
14045 || reloc_type == 31 /* R_AVR_DIFF16. */
14046 || reloc_type == 32 /* R_AVR_DIFF32. */);
14047 case EM_METAG:
14048 return reloc_type == 3; /* R_METAG_NONE. */
14049 case EM_NDS32:
14050 return (reloc_type == 0 /* R_XTENSA_NONE. */
14051 || reloc_type == 204 /* R_NDS32_DIFF8. */
14052 || reloc_type == 205 /* R_NDS32_DIFF16. */
14053 || reloc_type == 206 /* R_NDS32_DIFF32. */
14054 || reloc_type == 207 /* R_NDS32_ULEB128. */);
14055 case EM_TI_PRU:
14056 return (reloc_type == 0 /* R_PRU_NONE. */
14057 || reloc_type == 65 /* R_PRU_DIFF8. */
14058 || reloc_type == 66 /* R_PRU_DIFF16. */
14059 || reloc_type == 67 /* R_PRU_DIFF32. */);
14060 case EM_XTENSA_OLD:
14061 case EM_XTENSA:
14062 return (reloc_type == 0 /* R_XTENSA_NONE. */
14063 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14064 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14065 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14066 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14067 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14068 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14069 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14070 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14071 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14072 }
14073 return false;
14074 }
14075
14076 /* Returns TRUE if there is a relocation against
14077 section NAME at OFFSET bytes. */
14078
14079 bool
14080 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14081 {
14082 Elf_Internal_Rela * relocs;
14083 Elf_Internal_Rela * rp;
14084
14085 if (dsec == NULL || dsec->reloc_info == NULL)
14086 return false;
14087
14088 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14089
14090 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14091 if (rp->r_offset == offset)
14092 return true;
14093
14094 return false;
14095 }
14096
14097 /* Apply relocations to a section.
14098 Returns TRUE upon success, FALSE otherwise.
14099 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14100 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14101 will be set to the number of relocs loaded.
14102
14103 Note: So far support has been added only for those relocations
14104 which can be found in debug sections. FIXME: Add support for
14105 more relocations ? */
14106
14107 static bool
14108 apply_relocations (Filedata * filedata,
14109 const Elf_Internal_Shdr * section,
14110 unsigned char * start,
14111 bfd_size_type size,
14112 void ** relocs_return,
14113 unsigned long * num_relocs_return)
14114 {
14115 Elf_Internal_Shdr * relsec;
14116 unsigned char * end = start + size;
14117
14118 if (relocs_return != NULL)
14119 {
14120 * (Elf_Internal_Rela **) relocs_return = NULL;
14121 * num_relocs_return = 0;
14122 }
14123
14124 if (filedata->file_header.e_type != ET_REL)
14125 /* No relocs to apply. */
14126 return true;
14127
14128 /* Find the reloc section associated with the section. */
14129 for (relsec = filedata->section_headers;
14130 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14131 ++relsec)
14132 {
14133 bool is_rela;
14134 unsigned long num_relocs;
14135 Elf_Internal_Rela * relocs;
14136 Elf_Internal_Rela * rp;
14137 Elf_Internal_Shdr * symsec;
14138 Elf_Internal_Sym * symtab;
14139 unsigned long num_syms;
14140 Elf_Internal_Sym * sym;
14141
14142 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14143 || relsec->sh_info >= filedata->file_header.e_shnum
14144 || filedata->section_headers + relsec->sh_info != section
14145 || relsec->sh_size == 0
14146 || relsec->sh_link >= filedata->file_header.e_shnum)
14147 continue;
14148
14149 symsec = filedata->section_headers + relsec->sh_link;
14150 if (symsec->sh_type != SHT_SYMTAB
14151 && symsec->sh_type != SHT_DYNSYM)
14152 return false;
14153
14154 is_rela = relsec->sh_type == SHT_RELA;
14155
14156 if (is_rela)
14157 {
14158 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14159 relsec->sh_size, & relocs, & num_relocs))
14160 return false;
14161 }
14162 else
14163 {
14164 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14165 relsec->sh_size, & relocs, & num_relocs))
14166 return false;
14167 }
14168
14169 /* SH uses RELA but uses in place value instead of the addend field. */
14170 if (filedata->file_header.e_machine == EM_SH)
14171 is_rela = false;
14172
14173 symtab = GET_ELF_SYMBOLS (filedata, symsec, & num_syms);
14174
14175 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14176 {
14177 bfd_vma addend;
14178 unsigned int reloc_type;
14179 unsigned int reloc_size;
14180 bool reloc_inplace = false;
14181 bool reloc_subtract = false;
14182 unsigned char *rloc;
14183 unsigned long sym_index;
14184
14185 reloc_type = get_reloc_type (filedata, rp->r_info);
14186
14187 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14188 continue;
14189 else if (is_none_reloc (filedata, reloc_type))
14190 continue;
14191 else if (is_32bit_abs_reloc (filedata, reloc_type)
14192 || is_32bit_pcrel_reloc (filedata, reloc_type))
14193 reloc_size = 4;
14194 else if (is_64bit_abs_reloc (filedata, reloc_type)
14195 || is_64bit_pcrel_reloc (filedata, reloc_type))
14196 reloc_size = 8;
14197 else if (is_24bit_abs_reloc (filedata, reloc_type))
14198 reloc_size = 3;
14199 else if (is_16bit_abs_reloc (filedata, reloc_type))
14200 reloc_size = 2;
14201 else if (is_8bit_abs_reloc (filedata, reloc_type)
14202 || is_6bit_abs_reloc (filedata, reloc_type))
14203 reloc_size = 1;
14204 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14205 reloc_type))
14206 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14207 {
14208 reloc_size = 4;
14209 reloc_inplace = true;
14210 }
14211 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14212 reloc_type))
14213 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14214 {
14215 reloc_size = 8;
14216 reloc_inplace = true;
14217 }
14218 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14219 reloc_type))
14220 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14221 {
14222 reloc_size = 2;
14223 reloc_inplace = true;
14224 }
14225 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14226 reloc_type))
14227 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14228 {
14229 reloc_size = 1;
14230 reloc_inplace = true;
14231 }
14232 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14233 reloc_type)))
14234 {
14235 reloc_size = 1;
14236 reloc_inplace = true;
14237 }
14238 else
14239 {
14240 static unsigned int prev_reloc = 0;
14241
14242 if (reloc_type != prev_reloc)
14243 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
14244 reloc_type, printable_section_name (filedata, section));
14245 prev_reloc = reloc_type;
14246 continue;
14247 }
14248
14249 rloc = start + rp->r_offset;
14250 if (!IN_RANGE (start, end, rloc, reloc_size))
14251 {
14252 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14253 (unsigned long) rp->r_offset,
14254 printable_section_name (filedata, section));
14255 continue;
14256 }
14257
14258 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14259 if (sym_index >= num_syms)
14260 {
14261 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
14262 sym_index, printable_section_name (filedata, section));
14263 continue;
14264 }
14265 sym = symtab + sym_index;
14266
14267 /* If the reloc has a symbol associated with it,
14268 make sure that it is of an appropriate type.
14269
14270 Relocations against symbols without type can happen.
14271 Gcc -feliminate-dwarf2-dups may generate symbols
14272 without type for debug info.
14273
14274 Icc generates relocations against function symbols
14275 instead of local labels.
14276
14277 Relocations against object symbols can happen, eg when
14278 referencing a global array. For an example of this see
14279 the _clz.o binary in libgcc.a. */
14280 if (sym != symtab
14281 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
14282 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
14283 {
14284 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
14285 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14286 printable_section_name (filedata, relsec),
14287 (long int)(rp - relocs));
14288 continue;
14289 }
14290
14291 addend = 0;
14292 if (is_rela)
14293 addend += rp->r_addend;
14294 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14295 partial_inplace. */
14296 if (!is_rela
14297 || (filedata->file_header.e_machine == EM_XTENSA
14298 && reloc_type == 1)
14299 || ((filedata->file_header.e_machine == EM_PJ
14300 || filedata->file_header.e_machine == EM_PJ_OLD)
14301 && reloc_type == 1)
14302 || ((filedata->file_header.e_machine == EM_D30V
14303 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
14304 && reloc_type == 12)
14305 || reloc_inplace)
14306 {
14307 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14308 addend += byte_get (rloc, reloc_size) & 0x3f;
14309 else
14310 addend += byte_get (rloc, reloc_size);
14311 }
14312
14313 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14314 || is_64bit_pcrel_reloc (filedata, reloc_type))
14315 {
14316 /* On HPPA, all pc-relative relocations are biased by 8. */
14317 if (filedata->file_header.e_machine == EM_PARISC)
14318 addend -= 8;
14319 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
14320 reloc_size);
14321 }
14322 else if (is_6bit_abs_reloc (filedata, reloc_type)
14323 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14324 {
14325 if (reloc_subtract)
14326 addend -= sym->st_value;
14327 else
14328 addend += sym->st_value;
14329 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14330 byte_put (rloc, addend, reloc_size);
14331 }
14332 else if (reloc_subtract)
14333 byte_put (rloc, addend - sym->st_value, reloc_size);
14334 else
14335 byte_put (rloc, addend + sym->st_value, reloc_size);
14336 }
14337
14338 free (symtab);
14339 /* Let the target specific reloc processing code know that
14340 we have finished with these relocs. */
14341 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
14342
14343 if (relocs_return)
14344 {
14345 * (Elf_Internal_Rela **) relocs_return = relocs;
14346 * num_relocs_return = num_relocs;
14347 }
14348 else
14349 free (relocs);
14350
14351 break;
14352 }
14353
14354 return true;
14355 }
14356
14357 #ifdef SUPPORT_DISASSEMBLY
14358 static bool
14359 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
14360 {
14361 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
14362
14363 /* FIXME: XXX -- to be done --- XXX */
14364
14365 return true;
14366 }
14367 #endif
14368
14369 /* Reads in the contents of SECTION from FILE, returning a pointer
14370 to a malloc'ed buffer or NULL if something went wrong. */
14371
14372 static char *
14373 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
14374 {
14375 bfd_size_type num_bytes = section->sh_size;
14376
14377 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14378 {
14379 printf (_("Section '%s' has no data to dump.\n"),
14380 printable_section_name (filedata, section));
14381 return NULL;
14382 }
14383
14384 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
14385 _("section contents"));
14386 }
14387
14388 /* Uncompresses a section that was compressed using zlib, in place. */
14389
14390 static bool
14391 uncompress_section_contents (unsigned char ** buffer,
14392 dwarf_size_type uncompressed_size,
14393 dwarf_size_type * size)
14394 {
14395 dwarf_size_type compressed_size = *size;
14396 unsigned char * compressed_buffer = *buffer;
14397 unsigned char * uncompressed_buffer;
14398 z_stream strm;
14399 int rc;
14400
14401 /* It is possible the section consists of several compressed
14402 buffers concatenated together, so we uncompress in a loop. */
14403 /* PR 18313: The state field in the z_stream structure is supposed
14404 to be invisible to the user (ie us), but some compilers will
14405 still complain about it being used without initialisation. So
14406 we first zero the entire z_stream structure and then set the fields
14407 that we need. */
14408 memset (& strm, 0, sizeof strm);
14409 strm.avail_in = compressed_size;
14410 strm.next_in = (Bytef *) compressed_buffer;
14411 strm.avail_out = uncompressed_size;
14412 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
14413
14414 rc = inflateInit (& strm);
14415 while (strm.avail_in > 0)
14416 {
14417 if (rc != Z_OK)
14418 break;
14419 strm.next_out = ((Bytef *) uncompressed_buffer
14420 + (uncompressed_size - strm.avail_out));
14421 rc = inflate (&strm, Z_FINISH);
14422 if (rc != Z_STREAM_END)
14423 break;
14424 rc = inflateReset (& strm);
14425 }
14426 if (inflateEnd (& strm) != Z_OK
14427 || rc != Z_OK
14428 || strm.avail_out != 0)
14429 goto fail;
14430
14431 *buffer = uncompressed_buffer;
14432 *size = uncompressed_size;
14433 return true;
14434
14435 fail:
14436 free (uncompressed_buffer);
14437 /* Indicate decompression failure. */
14438 *buffer = NULL;
14439 return false;
14440 }
14441
14442 static bool
14443 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
14444 {
14445 Elf_Internal_Shdr *relsec;
14446 bfd_size_type num_bytes;
14447 unsigned char *data;
14448 unsigned char *end;
14449 unsigned char *real_start;
14450 unsigned char *start;
14451 bool some_strings_shown;
14452
14453 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14454 if (start == NULL)
14455 /* PR 21820: Do not fail if the section was empty. */
14456 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14457
14458 num_bytes = section->sh_size;
14459
14460 if (filedata->is_separate)
14461 printf (_("\nString dump of section '%s' in linked file %s:\n"),
14462 printable_section_name (filedata, section),
14463 filedata->file_name);
14464 else
14465 printf (_("\nString dump of section '%s':\n"),
14466 printable_section_name (filedata, section));
14467
14468 if (decompress_dumps)
14469 {
14470 dwarf_size_type new_size = num_bytes;
14471 dwarf_size_type uncompressed_size = 0;
14472
14473 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14474 {
14475 Elf_Internal_Chdr chdr;
14476 unsigned int compression_header_size
14477 = get_compression_header (& chdr, (unsigned char *) start,
14478 num_bytes);
14479 if (compression_header_size == 0)
14480 /* An error message will have already been generated
14481 by get_compression_header. */
14482 goto error_out;
14483
14484 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14485 {
14486 warn (_("section '%s' has unsupported compress type: %d\n"),
14487 printable_section_name (filedata, section), chdr.ch_type);
14488 goto error_out;
14489 }
14490 uncompressed_size = chdr.ch_size;
14491 start += compression_header_size;
14492 new_size -= compression_header_size;
14493 }
14494 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14495 {
14496 /* Read the zlib header. In this case, it should be "ZLIB"
14497 followed by the uncompressed section size, 8 bytes in
14498 big-endian order. */
14499 uncompressed_size = start[4]; uncompressed_size <<= 8;
14500 uncompressed_size += start[5]; uncompressed_size <<= 8;
14501 uncompressed_size += start[6]; uncompressed_size <<= 8;
14502 uncompressed_size += start[7]; uncompressed_size <<= 8;
14503 uncompressed_size += start[8]; uncompressed_size <<= 8;
14504 uncompressed_size += start[9]; uncompressed_size <<= 8;
14505 uncompressed_size += start[10]; uncompressed_size <<= 8;
14506 uncompressed_size += start[11];
14507 start += 12;
14508 new_size -= 12;
14509 }
14510
14511 if (uncompressed_size)
14512 {
14513 if (uncompress_section_contents (& start,
14514 uncompressed_size, & new_size))
14515 num_bytes = new_size;
14516 else
14517 {
14518 error (_("Unable to decompress section %s\n"),
14519 printable_section_name (filedata, section));
14520 goto error_out;
14521 }
14522 }
14523 else
14524 start = real_start;
14525 }
14526
14527 /* If the section being dumped has relocations against it the user might
14528 be expecting these relocations to have been applied. Check for this
14529 case and issue a warning message in order to avoid confusion.
14530 FIXME: Maybe we ought to have an option that dumps a section with
14531 relocs applied ? */
14532 for (relsec = filedata->section_headers;
14533 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14534 ++relsec)
14535 {
14536 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14537 || relsec->sh_info >= filedata->file_header.e_shnum
14538 || filedata->section_headers + relsec->sh_info != section
14539 || relsec->sh_size == 0
14540 || relsec->sh_link >= filedata->file_header.e_shnum)
14541 continue;
14542
14543 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14544 break;
14545 }
14546
14547 data = start;
14548 end = start + num_bytes;
14549 some_strings_shown = false;
14550
14551 #ifdef HAVE_MBSTATE_T
14552 mbstate_t state;
14553 /* Initialise the multibyte conversion state. */
14554 memset (& state, 0, sizeof (state));
14555 #endif
14556
14557 bool continuing = false;
14558
14559 while (data < end)
14560 {
14561 while (!ISPRINT (* data))
14562 if (++ data >= end)
14563 break;
14564
14565 if (data < end)
14566 {
14567 size_t maxlen = end - data;
14568
14569 if (continuing)
14570 {
14571 printf (" ");
14572 continuing = false;
14573 }
14574 else
14575 {
14576 printf (" [%6lx] ", (unsigned long) (data - start));
14577 }
14578
14579 if (maxlen > 0)
14580 {
14581 char c = 0;
14582
14583 while (maxlen)
14584 {
14585 c = *data++;
14586
14587 if (c == 0)
14588 break;
14589
14590 /* PR 25543: Treat new-lines as string-ending characters. */
14591 if (c == '\n')
14592 {
14593 printf ("\\n\n");
14594 if (*data != 0)
14595 continuing = true;
14596 break;
14597 }
14598
14599 /* Do not print control characters directly as they can affect terminal
14600 settings. Such characters usually appear in the names generated
14601 by the assembler for local labels. */
14602 if (ISCNTRL (c))
14603 {
14604 printf ("^%c", c + 0x40);
14605 }
14606 else if (ISPRINT (c))
14607 {
14608 putchar (c);
14609 }
14610 else
14611 {
14612 size_t n;
14613 #ifdef HAVE_MBSTATE_T
14614 wchar_t w;
14615 #endif
14616 /* Let printf do the hard work of displaying multibyte characters. */
14617 printf ("%.1s", data - 1);
14618 #ifdef HAVE_MBSTATE_T
14619 /* Try to find out how many bytes made up the character that was
14620 just printed. Advance the symbol pointer past the bytes that
14621 were displayed. */
14622 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
14623 #else
14624 n = 1;
14625 #endif
14626 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
14627 data += (n - 1);
14628 }
14629 }
14630
14631 if (c != '\n')
14632 putchar ('\n');
14633 }
14634 else
14635 {
14636 printf (_("<corrupt>\n"));
14637 data = end;
14638 }
14639 some_strings_shown = true;
14640 }
14641 }
14642
14643 if (! some_strings_shown)
14644 printf (_(" No strings found in this section."));
14645
14646 free (real_start);
14647
14648 putchar ('\n');
14649 return true;
14650
14651 error_out:
14652 free (real_start);
14653 return false;
14654 }
14655
14656 static bool
14657 dump_section_as_bytes (Elf_Internal_Shdr *section,
14658 Filedata *filedata,
14659 bool relocate)
14660 {
14661 Elf_Internal_Shdr * relsec;
14662 bfd_size_type bytes;
14663 bfd_size_type section_size;
14664 bfd_vma addr;
14665 unsigned char * data;
14666 unsigned char * real_start;
14667 unsigned char * start;
14668
14669 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14670 if (start == NULL)
14671 /* PR 21820: Do not fail if the section was empty. */
14672 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14673
14674 section_size = section->sh_size;
14675
14676 if (filedata->is_separate)
14677 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
14678 printable_section_name (filedata, section),
14679 filedata->file_name);
14680 else
14681 printf (_("\nHex dump of section '%s':\n"),
14682 printable_section_name (filedata, section));
14683
14684 if (decompress_dumps)
14685 {
14686 dwarf_size_type new_size = section_size;
14687 dwarf_size_type uncompressed_size = 0;
14688
14689 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14690 {
14691 Elf_Internal_Chdr chdr;
14692 unsigned int compression_header_size
14693 = get_compression_header (& chdr, start, section_size);
14694
14695 if (compression_header_size == 0)
14696 /* An error message will have already been generated
14697 by get_compression_header. */
14698 goto error_out;
14699
14700 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14701 {
14702 warn (_("section '%s' has unsupported compress type: %d\n"),
14703 printable_section_name (filedata, section), chdr.ch_type);
14704 goto error_out;
14705 }
14706 uncompressed_size = chdr.ch_size;
14707 start += compression_header_size;
14708 new_size -= compression_header_size;
14709 }
14710 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14711 {
14712 /* Read the zlib header. In this case, it should be "ZLIB"
14713 followed by the uncompressed section size, 8 bytes in
14714 big-endian order. */
14715 uncompressed_size = start[4]; uncompressed_size <<= 8;
14716 uncompressed_size += start[5]; uncompressed_size <<= 8;
14717 uncompressed_size += start[6]; uncompressed_size <<= 8;
14718 uncompressed_size += start[7]; uncompressed_size <<= 8;
14719 uncompressed_size += start[8]; uncompressed_size <<= 8;
14720 uncompressed_size += start[9]; uncompressed_size <<= 8;
14721 uncompressed_size += start[10]; uncompressed_size <<= 8;
14722 uncompressed_size += start[11];
14723 start += 12;
14724 new_size -= 12;
14725 }
14726
14727 if (uncompressed_size)
14728 {
14729 if (uncompress_section_contents (& start, uncompressed_size,
14730 & new_size))
14731 {
14732 section_size = new_size;
14733 }
14734 else
14735 {
14736 error (_("Unable to decompress section %s\n"),
14737 printable_section_name (filedata, section));
14738 /* FIXME: Print the section anyway ? */
14739 goto error_out;
14740 }
14741 }
14742 else
14743 start = real_start;
14744 }
14745
14746 if (relocate)
14747 {
14748 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
14749 goto error_out;
14750 }
14751 else
14752 {
14753 /* If the section being dumped has relocations against it the user might
14754 be expecting these relocations to have been applied. Check for this
14755 case and issue a warning message in order to avoid confusion.
14756 FIXME: Maybe we ought to have an option that dumps a section with
14757 relocs applied ? */
14758 for (relsec = filedata->section_headers;
14759 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14760 ++relsec)
14761 {
14762 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14763 || relsec->sh_info >= filedata->file_header.e_shnum
14764 || filedata->section_headers + relsec->sh_info != section
14765 || relsec->sh_size == 0
14766 || relsec->sh_link >= filedata->file_header.e_shnum)
14767 continue;
14768
14769 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14770 break;
14771 }
14772 }
14773
14774 addr = section->sh_addr;
14775 bytes = section_size;
14776 data = start;
14777
14778 while (bytes)
14779 {
14780 int j;
14781 int k;
14782 int lbytes;
14783
14784 lbytes = (bytes > 16 ? 16 : bytes);
14785
14786 printf (" 0x%8.8lx ", (unsigned long) addr);
14787
14788 for (j = 0; j < 16; j++)
14789 {
14790 if (j < lbytes)
14791 printf ("%2.2x", data[j]);
14792 else
14793 printf (" ");
14794
14795 if ((j & 3) == 3)
14796 printf (" ");
14797 }
14798
14799 for (j = 0; j < lbytes; j++)
14800 {
14801 k = data[j];
14802 if (k >= ' ' && k < 0x7f)
14803 printf ("%c", k);
14804 else
14805 printf (".");
14806 }
14807
14808 putchar ('\n');
14809
14810 data += lbytes;
14811 addr += lbytes;
14812 bytes -= lbytes;
14813 }
14814
14815 free (real_start);
14816
14817 putchar ('\n');
14818 return true;
14819
14820 error_out:
14821 free (real_start);
14822 return false;
14823 }
14824
14825 #ifdef ENABLE_LIBCTF
14826 static ctf_sect_t *
14827 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
14828 {
14829 buf->cts_name = SECTION_NAME_PRINT (shdr);
14830 buf->cts_size = shdr->sh_size;
14831 buf->cts_entsize = shdr->sh_entsize;
14832
14833 return buf;
14834 }
14835
14836 /* Formatting callback function passed to ctf_dump. Returns either the pointer
14837 it is passed, or a pointer to newly-allocated storage, in which case
14838 dump_ctf() will free it when it no longer needs it. */
14839
14840 static char *
14841 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
14842 char *s, void *arg)
14843 {
14844 const char *blanks = arg;
14845 char *new_s;
14846
14847 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
14848 return s;
14849 return new_s;
14850 }
14851
14852 /* Dump CTF errors/warnings. */
14853 static void
14854 dump_ctf_errs (ctf_dict_t *fp)
14855 {
14856 ctf_next_t *it = NULL;
14857 char *errtext;
14858 int is_warning;
14859 int err;
14860
14861 /* Dump accumulated errors and warnings. */
14862 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
14863 {
14864 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
14865 errtext);
14866 free (errtext);
14867 }
14868 if (err != ECTF_NEXT_END)
14869 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
14870 }
14871
14872 /* Dump one CTF archive member. */
14873
14874 static int
14875 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
14876 {
14877 ctf_dict_t *parent = (ctf_dict_t *) arg;
14878 const char *things[] = {"Header", "Labels", "Data objects",
14879 "Function objects", "Variables", "Types", "Strings",
14880 ""};
14881 const char **thing;
14882 size_t i;
14883 int err = 0;
14884
14885 /* Only print out the name of non-default-named archive members.
14886 The name .ctf appears everywhere, even for things that aren't
14887 really archives, so printing it out is liable to be confusing.
14888
14889 The parent, if there is one, is the default-owned archive member:
14890 avoid importing it into itself. (This does no harm, but looks
14891 confusing.) */
14892
14893 if (strcmp (name, ".ctf") != 0)
14894 {
14895 printf (_("\nCTF archive member: %s:\n"), name);
14896 ctf_import (ctf, parent);
14897 }
14898
14899 for (i = 0, thing = things; *thing[0]; thing++, i++)
14900 {
14901 ctf_dump_state_t *s = NULL;
14902 char *item;
14903
14904 printf ("\n %s:\n", *thing);
14905 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
14906 (void *) " ")) != NULL)
14907 {
14908 printf ("%s\n", item);
14909 free (item);
14910 }
14911
14912 if (ctf_errno (ctf))
14913 {
14914 error (_("Iteration failed: %s, %s\n"), *thing,
14915 ctf_errmsg (ctf_errno (ctf)));
14916 err = 1;
14917 goto out;
14918 }
14919 }
14920
14921 out:
14922 dump_ctf_errs (ctf);
14923 return err;
14924 }
14925
14926 static bool
14927 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
14928 {
14929 Elf_Internal_Shdr * parent_sec = NULL;
14930 Elf_Internal_Shdr * symtab_sec = NULL;
14931 Elf_Internal_Shdr * strtab_sec = NULL;
14932 void * data = NULL;
14933 void * symdata = NULL;
14934 void * strdata = NULL;
14935 void * parentdata = NULL;
14936 ctf_sect_t ctfsect, symsect, strsect, parentsect;
14937 ctf_sect_t * symsectp = NULL;
14938 ctf_sect_t * strsectp = NULL;
14939 ctf_archive_t * ctfa = NULL;
14940 ctf_archive_t * parenta = NULL, *lookparent;
14941 ctf_dict_t * parent = NULL;
14942
14943 int err;
14944 bool ret = false;
14945
14946 shdr_to_ctf_sect (&ctfsect, section, filedata);
14947 data = get_section_contents (section, filedata);
14948 ctfsect.cts_data = data;
14949
14950 if (!dump_ctf_symtab_name)
14951 dump_ctf_symtab_name = strdup (".dynsym");
14952
14953 if (!dump_ctf_strtab_name)
14954 dump_ctf_strtab_name = strdup (".dynstr");
14955
14956 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
14957 {
14958 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
14959 {
14960 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
14961 goto fail;
14962 }
14963 if ((symdata = (void *) get_data (NULL, filedata,
14964 symtab_sec->sh_offset, 1,
14965 symtab_sec->sh_size,
14966 _("symbols"))) == NULL)
14967 goto fail;
14968 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
14969 symsect.cts_data = symdata;
14970 }
14971
14972 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
14973 {
14974 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
14975 {
14976 error (_("No string table section named %s\n"),
14977 dump_ctf_strtab_name);
14978 goto fail;
14979 }
14980 if ((strdata = (void *) get_data (NULL, filedata,
14981 strtab_sec->sh_offset, 1,
14982 strtab_sec->sh_size,
14983 _("strings"))) == NULL)
14984 goto fail;
14985 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
14986 strsect.cts_data = strdata;
14987 }
14988
14989 if (dump_ctf_parent_name)
14990 {
14991 if ((parent_sec = find_section (filedata, dump_ctf_parent_name)) == NULL)
14992 {
14993 error (_("No CTF parent section named %s\n"), dump_ctf_parent_name);
14994 goto fail;
14995 }
14996 if ((parentdata = (void *) get_data (NULL, filedata,
14997 parent_sec->sh_offset, 1,
14998 parent_sec->sh_size,
14999 _("CTF parent"))) == NULL)
15000 goto fail;
15001 shdr_to_ctf_sect (&parentsect, parent_sec, filedata);
15002 parentsect.cts_data = parentdata;
15003 }
15004
15005 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15006 libctf papers over the difference, so we can pretend it is always an
15007 archive. Possibly open the parent as well, if one was specified. */
15008
15009 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15010 {
15011 dump_ctf_errs (NULL);
15012 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15013 goto fail;
15014 }
15015
15016 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15017 != ELFDATA2MSB);
15018
15019 if (parentdata)
15020 {
15021 if ((parenta = ctf_arc_bufopen (&parentsect, symsectp, strsectp,
15022 &err)) == NULL)
15023 {
15024 dump_ctf_errs (NULL);
15025 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15026 goto fail;
15027 }
15028 lookparent = parenta;
15029 }
15030 else
15031 lookparent = ctfa;
15032
15033 /* Assume that the applicable parent archive member is the default one.
15034 (This is what all known implementations are expected to do, if they
15035 put CTFs and their parents in archives together.) */
15036 if ((parent = ctf_dict_open (lookparent, NULL, &err)) == NULL)
15037 {
15038 dump_ctf_errs (NULL);
15039 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15040 goto fail;
15041 }
15042
15043 ret = true;
15044
15045 if (filedata->is_separate)
15046 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15047 printable_section_name (filedata, section),
15048 filedata->file_name);
15049 else
15050 printf (_("\nDump of CTF section '%s':\n"),
15051 printable_section_name (filedata, section));
15052
15053 if ((err = ctf_archive_iter (ctfa, dump_ctf_archive_member, parent)) != 0)
15054 {
15055 dump_ctf_errs (NULL);
15056 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15057 ret = false;
15058 }
15059
15060 fail:
15061 ctf_dict_close (parent);
15062 ctf_close (ctfa);
15063 ctf_close (parenta);
15064 free (parentdata);
15065 free (data);
15066 free (symdata);
15067 free (strdata);
15068 return ret;
15069 }
15070 #endif
15071
15072 static bool
15073 load_specific_debug_section (enum dwarf_section_display_enum debug,
15074 const Elf_Internal_Shdr * sec,
15075 void * data)
15076 {
15077 struct dwarf_section * section = &debug_displays [debug].section;
15078 char buf [64];
15079 Filedata * filedata = (Filedata *) data;
15080
15081 if (section->start != NULL)
15082 {
15083 /* If it is already loaded, do nothing. */
15084 if (streq (section->filename, filedata->file_name))
15085 return true;
15086 free (section->start);
15087 }
15088
15089 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15090 section->address = sec->sh_addr;
15091 section->filename = filedata->file_name;
15092 section->start = (unsigned char *) get_data (NULL, filedata,
15093 sec->sh_offset, 1,
15094 sec->sh_size, buf);
15095 if (section->start == NULL)
15096 section->size = 0;
15097 else
15098 {
15099 unsigned char *start = section->start;
15100 dwarf_size_type size = sec->sh_size;
15101 dwarf_size_type uncompressed_size = 0;
15102
15103 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15104 {
15105 Elf_Internal_Chdr chdr;
15106 unsigned int compression_header_size;
15107
15108 if (size < (is_32bit_elf
15109 ? sizeof (Elf32_External_Chdr)
15110 : sizeof (Elf64_External_Chdr)))
15111 {
15112 warn (_("compressed section %s is too small to contain a compression header\n"),
15113 section->name);
15114 return false;
15115 }
15116
15117 compression_header_size = get_compression_header (&chdr, start, size);
15118 if (compression_header_size == 0)
15119 /* An error message will have already been generated
15120 by get_compression_header. */
15121 return false;
15122
15123 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15124 {
15125 warn (_("section '%s' has unsupported compress type: %d\n"),
15126 section->name, chdr.ch_type);
15127 return false;
15128 }
15129 uncompressed_size = chdr.ch_size;
15130 start += compression_header_size;
15131 size -= compression_header_size;
15132 }
15133 else if (size > 12 && streq ((char *) start, "ZLIB"))
15134 {
15135 /* Read the zlib header. In this case, it should be "ZLIB"
15136 followed by the uncompressed section size, 8 bytes in
15137 big-endian order. */
15138 uncompressed_size = start[4]; uncompressed_size <<= 8;
15139 uncompressed_size += start[5]; uncompressed_size <<= 8;
15140 uncompressed_size += start[6]; uncompressed_size <<= 8;
15141 uncompressed_size += start[7]; uncompressed_size <<= 8;
15142 uncompressed_size += start[8]; uncompressed_size <<= 8;
15143 uncompressed_size += start[9]; uncompressed_size <<= 8;
15144 uncompressed_size += start[10]; uncompressed_size <<= 8;
15145 uncompressed_size += start[11];
15146 start += 12;
15147 size -= 12;
15148 }
15149
15150 if (uncompressed_size)
15151 {
15152 if (uncompress_section_contents (&start, uncompressed_size,
15153 &size))
15154 {
15155 /* Free the compressed buffer, update the section buffer
15156 and the section size if uncompress is successful. */
15157 free (section->start);
15158 section->start = start;
15159 }
15160 else
15161 {
15162 error (_("Unable to decompress section %s\n"),
15163 printable_section_name (filedata, sec));
15164 return false;
15165 }
15166 }
15167
15168 section->size = size;
15169 }
15170
15171 if (section->start == NULL)
15172 return false;
15173
15174 if (debug_displays [debug].relocate)
15175 {
15176 if (! apply_relocations (filedata, sec, section->start, section->size,
15177 & section->reloc_info, & section->num_relocs))
15178 return false;
15179 }
15180 else
15181 {
15182 section->reloc_info = NULL;
15183 section->num_relocs = 0;
15184 }
15185
15186 return true;
15187 }
15188
15189 #if HAVE_LIBDEBUGINFOD
15190 /* Return a hex string representation of the build-id. */
15191 unsigned char *
15192 get_build_id (void * data)
15193 {
15194 Filedata * filedata = (Filedata *) data;
15195 Elf_Internal_Shdr * shdr;
15196 unsigned long i;
15197
15198 /* Iterate through notes to find note.gnu.build-id.
15199 FIXME: Only the first note in any note section is examined. */
15200 for (i = 0, shdr = filedata->section_headers;
15201 i < filedata->file_header.e_shnum && shdr != NULL;
15202 i++, shdr++)
15203 {
15204 if (shdr->sh_type != SHT_NOTE)
15205 continue;
15206
15207 char * next;
15208 char * end;
15209 size_t data_remaining;
15210 size_t min_notesz;
15211 Elf_External_Note * enote;
15212 Elf_Internal_Note inote;
15213
15214 bfd_vma offset = shdr->sh_offset;
15215 bfd_vma align = shdr->sh_addralign;
15216 bfd_vma length = shdr->sh_size;
15217
15218 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15219 if (enote == NULL)
15220 continue;
15221
15222 if (align < 4)
15223 align = 4;
15224 else if (align != 4 && align != 8)
15225 {
15226 free (enote);
15227 continue;
15228 }
15229
15230 end = (char *) enote + length;
15231 data_remaining = end - (char *) enote;
15232
15233 if (!is_ia64_vms (filedata))
15234 {
15235 min_notesz = offsetof (Elf_External_Note, name);
15236 if (data_remaining < min_notesz)
15237 {
15238 warn (_("\
15239 malformed note encountered in section %s whilst scanning for build-id note\n"),
15240 printable_section_name (filedata, shdr));
15241 free (enote);
15242 continue;
15243 }
15244 data_remaining -= min_notesz;
15245
15246 inote.type = BYTE_GET (enote->type);
15247 inote.namesz = BYTE_GET (enote->namesz);
15248 inote.namedata = enote->name;
15249 inote.descsz = BYTE_GET (enote->descsz);
15250 inote.descdata = ((char *) enote
15251 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15252 inote.descpos = offset + (inote.descdata - (char *) enote);
15253 next = ((char *) enote
15254 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15255 }
15256 else
15257 {
15258 Elf64_External_VMS_Note *vms_enote;
15259
15260 /* PR binutils/15191
15261 Make sure that there is enough data to read. */
15262 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15263 if (data_remaining < min_notesz)
15264 {
15265 warn (_("\
15266 malformed note encountered in section %s whilst scanning for build-id note\n"),
15267 printable_section_name (filedata, shdr));
15268 free (enote);
15269 continue;
15270 }
15271 data_remaining -= min_notesz;
15272
15273 vms_enote = (Elf64_External_VMS_Note *) enote;
15274 inote.type = BYTE_GET (vms_enote->type);
15275 inote.namesz = BYTE_GET (vms_enote->namesz);
15276 inote.namedata = vms_enote->name;
15277 inote.descsz = BYTE_GET (vms_enote->descsz);
15278 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15279 inote.descpos = offset + (inote.descdata - (char *) enote);
15280 next = inote.descdata + align_power (inote.descsz, 3);
15281 }
15282
15283 /* Skip malformed notes. */
15284 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15285 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15286 || (size_t) (next - inote.descdata) < inote.descsz
15287 || ((size_t) (next - inote.descdata)
15288 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15289 {
15290 warn (_("\
15291 malformed note encountered in section %s whilst scanning for build-id note\n"),
15292 printable_section_name (filedata, shdr));
15293 free (enote);
15294 continue;
15295 }
15296
15297 /* Check if this is the build-id note. If so then convert the build-id
15298 bytes to a hex string. */
15299 if (inote.namesz > 0
15300 && const_strneq (inote.namedata, "GNU")
15301 && inote.type == NT_GNU_BUILD_ID)
15302 {
15303 unsigned long j;
15304 char * build_id;
15305
15306 build_id = malloc (inote.descsz * 2 + 1);
15307 if (build_id == NULL)
15308 {
15309 free (enote);
15310 return NULL;
15311 }
15312
15313 for (j = 0; j < inote.descsz; ++j)
15314 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15315 build_id[inote.descsz * 2] = '\0';
15316 free (enote);
15317
15318 return (unsigned char *) build_id;
15319 }
15320 free (enote);
15321 }
15322
15323 return NULL;
15324 }
15325 #endif /* HAVE_LIBDEBUGINFOD */
15326
15327 /* If this is not NULL, load_debug_section will only look for sections
15328 within the list of sections given here. */
15329 static unsigned int * section_subset = NULL;
15330
15331 bool
15332 load_debug_section (enum dwarf_section_display_enum debug, void * data)
15333 {
15334 struct dwarf_section * section = &debug_displays [debug].section;
15335 Elf_Internal_Shdr * sec;
15336 Filedata * filedata = (Filedata *) data;
15337
15338 /* Without section headers we cannot find any sections. */
15339 if (filedata->section_headers == NULL)
15340 return false;
15341
15342 if (filedata->string_table == NULL
15343 && filedata->file_header.e_shstrndx != SHN_UNDEF
15344 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
15345 {
15346 Elf_Internal_Shdr * strs;
15347
15348 /* Read in the string table, so that we have section names to scan. */
15349 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15350
15351 if (strs != NULL && strs->sh_size != 0)
15352 {
15353 filedata->string_table
15354 = (char *) get_data (NULL, filedata, strs->sh_offset,
15355 1, strs->sh_size, _("string table"));
15356
15357 filedata->string_table_length
15358 = filedata->string_table != NULL ? strs->sh_size : 0;
15359 }
15360 }
15361
15362 /* Locate the debug section. */
15363 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
15364 if (sec != NULL)
15365 section->name = section->uncompressed_name;
15366 else
15367 {
15368 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
15369 if (sec != NULL)
15370 section->name = section->compressed_name;
15371 }
15372 if (sec == NULL)
15373 return false;
15374
15375 /* If we're loading from a subset of sections, and we've loaded
15376 a section matching this name before, it's likely that it's a
15377 different one. */
15378 if (section_subset != NULL)
15379 free_debug_section (debug);
15380
15381 return load_specific_debug_section (debug, sec, data);
15382 }
15383
15384 void
15385 free_debug_section (enum dwarf_section_display_enum debug)
15386 {
15387 struct dwarf_section * section = &debug_displays [debug].section;
15388
15389 if (section->start == NULL)
15390 return;
15391
15392 free ((char *) section->start);
15393 section->start = NULL;
15394 section->address = 0;
15395 section->size = 0;
15396
15397 free (section->reloc_info);
15398 section->reloc_info = NULL;
15399 section->num_relocs = 0;
15400 }
15401
15402 static bool
15403 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
15404 {
15405 char * name = SECTION_NAME_VALID (section) ? SECTION_NAME (section) : "";
15406 const char * print_name = printable_section_name (filedata, section);
15407 bfd_size_type length;
15408 bool result = true;
15409 int i;
15410
15411 length = section->sh_size;
15412 if (length == 0)
15413 {
15414 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
15415 return true;
15416 }
15417 if (section->sh_type == SHT_NOBITS)
15418 {
15419 /* There is no point in dumping the contents of a debugging section
15420 which has the NOBITS type - the bits in the file will be random.
15421 This can happen when a file containing a .eh_frame section is
15422 stripped with the --only-keep-debug command line option. */
15423 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
15424 print_name);
15425 return false;
15426 }
15427
15428 if (const_strneq (name, ".gnu.linkonce.wi."))
15429 name = ".debug_info";
15430
15431 /* See if we know how to display the contents of this section. */
15432 for (i = 0; i < max; i++)
15433 {
15434 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
15435 struct dwarf_section_display * display = debug_displays + i;
15436 struct dwarf_section * sec = & display->section;
15437
15438 if (streq (sec->uncompressed_name, name)
15439 || (id == line && const_strneq (name, ".debug_line."))
15440 || streq (sec->compressed_name, name))
15441 {
15442 bool secondary = (section != find_section (filedata, name));
15443
15444 if (secondary)
15445 free_debug_section (id);
15446
15447 if (i == line && const_strneq (name, ".debug_line."))
15448 sec->name = name;
15449 else if (streq (sec->uncompressed_name, name))
15450 sec->name = sec->uncompressed_name;
15451 else
15452 sec->name = sec->compressed_name;
15453
15454 if (load_specific_debug_section (id, section, filedata))
15455 {
15456 /* If this debug section is part of a CU/TU set in a .dwp file,
15457 restrict load_debug_section to the sections in that set. */
15458 section_subset = find_cu_tu_set (filedata, shndx);
15459
15460 result &= display->display (sec, filedata);
15461
15462 section_subset = NULL;
15463
15464 if (secondary || (id != info && id != abbrev && id != debug_addr))
15465 free_debug_section (id);
15466 }
15467 break;
15468 }
15469 }
15470
15471 if (i == max)
15472 {
15473 printf (_("Unrecognized debug section: %s\n"), print_name);
15474 result = false;
15475 }
15476
15477 return result;
15478 }
15479
15480 /* Set DUMP_SECTS for all sections where dumps were requested
15481 based on section name. */
15482
15483 static void
15484 initialise_dumps_byname (Filedata * filedata)
15485 {
15486 struct dump_list_entry * cur;
15487
15488 for (cur = dump_sects_byname; cur; cur = cur->next)
15489 {
15490 unsigned int i;
15491 bool any = false;
15492
15493 for (i = 0; i < filedata->file_header.e_shnum; i++)
15494 if (SECTION_NAME_VALID (filedata->section_headers + i)
15495 && streq (SECTION_NAME (filedata->section_headers + i), cur->name))
15496 {
15497 request_dump_bynumber (&filedata->dump, i, cur->type);
15498 any = true;
15499 }
15500
15501 if (!any && !filedata->is_separate)
15502 warn (_("Section '%s' was not dumped because it does not exist\n"),
15503 cur->name);
15504 }
15505 }
15506
15507 static bool
15508 process_section_contents (Filedata * filedata)
15509 {
15510 Elf_Internal_Shdr * section;
15511 unsigned int i;
15512 bool res = true;
15513
15514 if (! do_dump)
15515 return true;
15516
15517 initialise_dumps_byname (filedata);
15518
15519 for (i = 0, section = filedata->section_headers;
15520 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
15521 i++, section++)
15522 {
15523 dump_type dump = filedata->dump.dump_sects[i];
15524
15525 if (filedata->is_separate && ! process_links)
15526 dump &= DEBUG_DUMP;
15527
15528 #ifdef SUPPORT_DISASSEMBLY
15529 if (dump & DISASS_DUMP)
15530 {
15531 if (! disassemble_section (section, filedata))
15532 res = false;
15533 }
15534 #endif
15535 if (dump & HEX_DUMP)
15536 {
15537 if (! dump_section_as_bytes (section, filedata, false))
15538 res = false;
15539 }
15540
15541 if (dump & RELOC_DUMP)
15542 {
15543 if (! dump_section_as_bytes (section, filedata, true))
15544 res = false;
15545 }
15546
15547 if (dump & STRING_DUMP)
15548 {
15549 if (! dump_section_as_strings (section, filedata))
15550 res = false;
15551 }
15552
15553 if (dump & DEBUG_DUMP)
15554 {
15555 if (! display_debug_section (i, section, filedata))
15556 res = false;
15557 }
15558
15559 #ifdef ENABLE_LIBCTF
15560 if (dump & CTF_DUMP)
15561 {
15562 if (! dump_section_as_ctf (section, filedata))
15563 res = false;
15564 }
15565 #endif
15566 }
15567
15568 if (! filedata->is_separate)
15569 {
15570 /* Check to see if the user requested a
15571 dump of a section that does not exist. */
15572 for (; i < filedata->dump.num_dump_sects; i++)
15573 if (filedata->dump.dump_sects[i])
15574 {
15575 warn (_("Section %d was not dumped because it does not exist!\n"), i);
15576 res = false;
15577 }
15578 }
15579
15580 return res;
15581 }
15582
15583 static void
15584 process_mips_fpe_exception (int mask)
15585 {
15586 if (mask)
15587 {
15588 bool first = true;
15589
15590 if (mask & OEX_FPU_INEX)
15591 fputs ("INEX", stdout), first = false;
15592 if (mask & OEX_FPU_UFLO)
15593 printf ("%sUFLO", first ? "" : "|"), first = false;
15594 if (mask & OEX_FPU_OFLO)
15595 printf ("%sOFLO", first ? "" : "|"), first = false;
15596 if (mask & OEX_FPU_DIV0)
15597 printf ("%sDIV0", first ? "" : "|"), first = false;
15598 if (mask & OEX_FPU_INVAL)
15599 printf ("%sINVAL", first ? "" : "|");
15600 }
15601 else
15602 fputs ("0", stdout);
15603 }
15604
15605 /* Display's the value of TAG at location P. If TAG is
15606 greater than 0 it is assumed to be an unknown tag, and
15607 a message is printed to this effect. Otherwise it is
15608 assumed that a message has already been printed.
15609
15610 If the bottom bit of TAG is set it assumed to have a
15611 string value, otherwise it is assumed to have an integer
15612 value.
15613
15614 Returns an updated P pointing to the first unread byte
15615 beyond the end of TAG's value.
15616
15617 Reads at or beyond END will not be made. */
15618
15619 static unsigned char *
15620 display_tag_value (signed int tag,
15621 unsigned char * p,
15622 const unsigned char * const end)
15623 {
15624 unsigned long val;
15625
15626 if (tag > 0)
15627 printf (" Tag_unknown_%d: ", tag);
15628
15629 if (p >= end)
15630 {
15631 warn (_("<corrupt tag>\n"));
15632 }
15633 else if (tag & 1)
15634 {
15635 /* PR 17531 file: 027-19978-0.004. */
15636 size_t maxlen = (end - p) - 1;
15637
15638 putchar ('"');
15639 if (maxlen > 0)
15640 {
15641 print_symbol ((int) maxlen, (const char *) p);
15642 p += strnlen ((char *) p, maxlen) + 1;
15643 }
15644 else
15645 {
15646 printf (_("<corrupt string tag>"));
15647 p = (unsigned char *) end;
15648 }
15649 printf ("\"\n");
15650 }
15651 else
15652 {
15653 READ_ULEB (val, p, end);
15654 printf ("%ld (0x%lx)\n", val, val);
15655 }
15656
15657 assert (p <= end);
15658 return p;
15659 }
15660
15661 /* ARC ABI attributes section. */
15662
15663 static unsigned char *
15664 display_arc_attribute (unsigned char * p,
15665 const unsigned char * const end)
15666 {
15667 unsigned int tag;
15668 unsigned int val;
15669
15670 READ_ULEB (tag, p, end);
15671
15672 switch (tag)
15673 {
15674 case Tag_ARC_PCS_config:
15675 READ_ULEB (val, p, end);
15676 printf (" Tag_ARC_PCS_config: ");
15677 switch (val)
15678 {
15679 case 0:
15680 printf (_("Absent/Non standard\n"));
15681 break;
15682 case 1:
15683 printf (_("Bare metal/mwdt\n"));
15684 break;
15685 case 2:
15686 printf (_("Bare metal/newlib\n"));
15687 break;
15688 case 3:
15689 printf (_("Linux/uclibc\n"));
15690 break;
15691 case 4:
15692 printf (_("Linux/glibc\n"));
15693 break;
15694 default:
15695 printf (_("Unknown\n"));
15696 break;
15697 }
15698 break;
15699
15700 case Tag_ARC_CPU_base:
15701 READ_ULEB (val, p, end);
15702 printf (" Tag_ARC_CPU_base: ");
15703 switch (val)
15704 {
15705 default:
15706 case TAG_CPU_NONE:
15707 printf (_("Absent\n"));
15708 break;
15709 case TAG_CPU_ARC6xx:
15710 printf ("ARC6xx\n");
15711 break;
15712 case TAG_CPU_ARC7xx:
15713 printf ("ARC7xx\n");
15714 break;
15715 case TAG_CPU_ARCEM:
15716 printf ("ARCEM\n");
15717 break;
15718 case TAG_CPU_ARCHS:
15719 printf ("ARCHS\n");
15720 break;
15721 }
15722 break;
15723
15724 case Tag_ARC_CPU_variation:
15725 READ_ULEB (val, p, end);
15726 printf (" Tag_ARC_CPU_variation: ");
15727 switch (val)
15728 {
15729 default:
15730 if (val > 0 && val < 16)
15731 printf ("Core%d\n", val);
15732 else
15733 printf ("Unknown\n");
15734 break;
15735
15736 case 0:
15737 printf (_("Absent\n"));
15738 break;
15739 }
15740 break;
15741
15742 case Tag_ARC_CPU_name:
15743 printf (" Tag_ARC_CPU_name: ");
15744 p = display_tag_value (-1, p, end);
15745 break;
15746
15747 case Tag_ARC_ABI_rf16:
15748 READ_ULEB (val, p, end);
15749 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
15750 break;
15751
15752 case Tag_ARC_ABI_osver:
15753 READ_ULEB (val, p, end);
15754 printf (" Tag_ARC_ABI_osver: v%d\n", val);
15755 break;
15756
15757 case Tag_ARC_ABI_pic:
15758 case Tag_ARC_ABI_sda:
15759 READ_ULEB (val, p, end);
15760 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
15761 : " Tag_ARC_ABI_pic: ");
15762 switch (val)
15763 {
15764 case 0:
15765 printf (_("Absent\n"));
15766 break;
15767 case 1:
15768 printf ("MWDT\n");
15769 break;
15770 case 2:
15771 printf ("GNU\n");
15772 break;
15773 default:
15774 printf (_("Unknown\n"));
15775 break;
15776 }
15777 break;
15778
15779 case Tag_ARC_ABI_tls:
15780 READ_ULEB (val, p, end);
15781 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
15782 break;
15783
15784 case Tag_ARC_ABI_enumsize:
15785 READ_ULEB (val, p, end);
15786 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
15787 _("smallest"));
15788 break;
15789
15790 case Tag_ARC_ABI_exceptions:
15791 READ_ULEB (val, p, end);
15792 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
15793 : _("default"));
15794 break;
15795
15796 case Tag_ARC_ABI_double_size:
15797 READ_ULEB (val, p, end);
15798 printf (" Tag_ARC_ABI_double_size: %d\n", val);
15799 break;
15800
15801 case Tag_ARC_ISA_config:
15802 printf (" Tag_ARC_ISA_config: ");
15803 p = display_tag_value (-1, p, end);
15804 break;
15805
15806 case Tag_ARC_ISA_apex:
15807 printf (" Tag_ARC_ISA_apex: ");
15808 p = display_tag_value (-1, p, end);
15809 break;
15810
15811 case Tag_ARC_ISA_mpy_option:
15812 READ_ULEB (val, p, end);
15813 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
15814 break;
15815
15816 case Tag_ARC_ATR_version:
15817 READ_ULEB (val, p, end);
15818 printf (" Tag_ARC_ATR_version: %d\n", val);
15819 break;
15820
15821 default:
15822 return display_tag_value (tag & 1, p, end);
15823 }
15824
15825 return p;
15826 }
15827
15828 /* ARM EABI attributes section. */
15829 typedef struct
15830 {
15831 unsigned int tag;
15832 const char * name;
15833 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
15834 unsigned int type;
15835 const char *const *table;
15836 } arm_attr_public_tag;
15837
15838 static const char *const arm_attr_tag_CPU_arch[] =
15839 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
15840 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
15841 "v8-M.mainline", "", "", "", "v8.1-M.mainline"};
15842 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
15843 static const char *const arm_attr_tag_THUMB_ISA_use[] =
15844 {"No", "Thumb-1", "Thumb-2", "Yes"};
15845 static const char *const arm_attr_tag_FP_arch[] =
15846 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
15847 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
15848 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
15849 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
15850 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
15851 "NEON for ARMv8.1"};
15852 static const char *const arm_attr_tag_PCS_config[] =
15853 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
15854 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
15855 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
15856 {"V6", "SB", "TLS", "Unused"};
15857 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
15858 {"Absolute", "PC-relative", "SB-relative", "None"};
15859 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
15860 {"Absolute", "PC-relative", "None"};
15861 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
15862 {"None", "direct", "GOT-indirect"};
15863 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
15864 {"None", "??? 1", "2", "??? 3", "4"};
15865 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
15866 static const char *const arm_attr_tag_ABI_FP_denormal[] =
15867 {"Unused", "Needed", "Sign only"};
15868 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
15869 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
15870 static const char *const arm_attr_tag_ABI_FP_number_model[] =
15871 {"Unused", "Finite", "RTABI", "IEEE 754"};
15872 static const char *const arm_attr_tag_ABI_enum_size[] =
15873 {"Unused", "small", "int", "forced to int"};
15874 static const char *const arm_attr_tag_ABI_HardFP_use[] =
15875 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
15876 static const char *const arm_attr_tag_ABI_VFP_args[] =
15877 {"AAPCS", "VFP registers", "custom", "compatible"};
15878 static const char *const arm_attr_tag_ABI_WMMX_args[] =
15879 {"AAPCS", "WMMX registers", "custom"};
15880 static const char *const arm_attr_tag_ABI_optimization_goals[] =
15881 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
15882 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
15883 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
15884 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
15885 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
15886 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
15887 static const char *const arm_attr_tag_FP_HP_extension[] =
15888 {"Not Allowed", "Allowed"};
15889 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
15890 {"None", "IEEE 754", "Alternative Format"};
15891 static const char *const arm_attr_tag_DSP_extension[] =
15892 {"Follow architecture", "Allowed"};
15893 static const char *const arm_attr_tag_MPextension_use[] =
15894 {"Not Allowed", "Allowed"};
15895 static const char *const arm_attr_tag_DIV_use[] =
15896 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
15897 "Allowed in v7-A with integer division extension"};
15898 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
15899 static const char *const arm_attr_tag_Virtualization_use[] =
15900 {"Not Allowed", "TrustZone", "Virtualization Extensions",
15901 "TrustZone and Virtualization Extensions"};
15902 static const char *const arm_attr_tag_MPextension_use_legacy[] =
15903 {"Not Allowed", "Allowed"};
15904
15905 static const char *const arm_attr_tag_MVE_arch[] =
15906 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
15907
15908 #define LOOKUP(id, name) \
15909 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
15910 static arm_attr_public_tag arm_attr_public_tags[] =
15911 {
15912 {4, "CPU_raw_name", 1, NULL},
15913 {5, "CPU_name", 1, NULL},
15914 LOOKUP(6, CPU_arch),
15915 {7, "CPU_arch_profile", 0, NULL},
15916 LOOKUP(8, ARM_ISA_use),
15917 LOOKUP(9, THUMB_ISA_use),
15918 LOOKUP(10, FP_arch),
15919 LOOKUP(11, WMMX_arch),
15920 LOOKUP(12, Advanced_SIMD_arch),
15921 LOOKUP(13, PCS_config),
15922 LOOKUP(14, ABI_PCS_R9_use),
15923 LOOKUP(15, ABI_PCS_RW_data),
15924 LOOKUP(16, ABI_PCS_RO_data),
15925 LOOKUP(17, ABI_PCS_GOT_use),
15926 LOOKUP(18, ABI_PCS_wchar_t),
15927 LOOKUP(19, ABI_FP_rounding),
15928 LOOKUP(20, ABI_FP_denormal),
15929 LOOKUP(21, ABI_FP_exceptions),
15930 LOOKUP(22, ABI_FP_user_exceptions),
15931 LOOKUP(23, ABI_FP_number_model),
15932 {24, "ABI_align_needed", 0, NULL},
15933 {25, "ABI_align_preserved", 0, NULL},
15934 LOOKUP(26, ABI_enum_size),
15935 LOOKUP(27, ABI_HardFP_use),
15936 LOOKUP(28, ABI_VFP_args),
15937 LOOKUP(29, ABI_WMMX_args),
15938 LOOKUP(30, ABI_optimization_goals),
15939 LOOKUP(31, ABI_FP_optimization_goals),
15940 {32, "compatibility", 0, NULL},
15941 LOOKUP(34, CPU_unaligned_access),
15942 LOOKUP(36, FP_HP_extension),
15943 LOOKUP(38, ABI_FP_16bit_format),
15944 LOOKUP(42, MPextension_use),
15945 LOOKUP(44, DIV_use),
15946 LOOKUP(46, DSP_extension),
15947 LOOKUP(48, MVE_arch),
15948 {64, "nodefaults", 0, NULL},
15949 {65, "also_compatible_with", 0, NULL},
15950 LOOKUP(66, T2EE_use),
15951 {67, "conformance", 1, NULL},
15952 LOOKUP(68, Virtualization_use),
15953 LOOKUP(70, MPextension_use_legacy)
15954 };
15955 #undef LOOKUP
15956
15957 static unsigned char *
15958 display_arm_attribute (unsigned char * p,
15959 const unsigned char * const end)
15960 {
15961 unsigned int tag;
15962 unsigned int val;
15963 arm_attr_public_tag * attr;
15964 unsigned i;
15965 unsigned int type;
15966
15967 READ_ULEB (tag, p, end);
15968 attr = NULL;
15969 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
15970 {
15971 if (arm_attr_public_tags[i].tag == tag)
15972 {
15973 attr = &arm_attr_public_tags[i];
15974 break;
15975 }
15976 }
15977
15978 if (attr)
15979 {
15980 printf (" Tag_%s: ", attr->name);
15981 switch (attr->type)
15982 {
15983 case 0:
15984 switch (tag)
15985 {
15986 case 7: /* Tag_CPU_arch_profile. */
15987 READ_ULEB (val, p, end);
15988 switch (val)
15989 {
15990 case 0: printf (_("None\n")); break;
15991 case 'A': printf (_("Application\n")); break;
15992 case 'R': printf (_("Realtime\n")); break;
15993 case 'M': printf (_("Microcontroller\n")); break;
15994 case 'S': printf (_("Application or Realtime\n")); break;
15995 default: printf ("??? (%d)\n", val); break;
15996 }
15997 break;
15998
15999 case 24: /* Tag_align_needed. */
16000 READ_ULEB (val, p, end);
16001 switch (val)
16002 {
16003 case 0: printf (_("None\n")); break;
16004 case 1: printf (_("8-byte\n")); break;
16005 case 2: printf (_("4-byte\n")); break;
16006 case 3: printf ("??? 3\n"); break;
16007 default:
16008 if (val <= 12)
16009 printf (_("8-byte and up to %d-byte extended\n"),
16010 1 << val);
16011 else
16012 printf ("??? (%d)\n", val);
16013 break;
16014 }
16015 break;
16016
16017 case 25: /* Tag_align_preserved. */
16018 READ_ULEB (val, p, end);
16019 switch (val)
16020 {
16021 case 0: printf (_("None\n")); break;
16022 case 1: printf (_("8-byte, except leaf SP\n")); break;
16023 case 2: printf (_("8-byte\n")); break;
16024 case 3: printf ("??? 3\n"); break;
16025 default:
16026 if (val <= 12)
16027 printf (_("8-byte and up to %d-byte extended\n"),
16028 1 << val);
16029 else
16030 printf ("??? (%d)\n", val);
16031 break;
16032 }
16033 break;
16034
16035 case 32: /* Tag_compatibility. */
16036 {
16037 READ_ULEB (val, p, end);
16038 printf (_("flag = %d, vendor = "), val);
16039 if (p < end - 1)
16040 {
16041 size_t maxlen = (end - p) - 1;
16042
16043 print_symbol ((int) maxlen, (const char *) p);
16044 p += strnlen ((char *) p, maxlen) + 1;
16045 }
16046 else
16047 {
16048 printf (_("<corrupt>"));
16049 p = (unsigned char *) end;
16050 }
16051 putchar ('\n');
16052 }
16053 break;
16054
16055 case 64: /* Tag_nodefaults. */
16056 /* PR 17531: file: 001-505008-0.01. */
16057 if (p < end)
16058 p++;
16059 printf (_("True\n"));
16060 break;
16061
16062 case 65: /* Tag_also_compatible_with. */
16063 READ_ULEB (val, p, end);
16064 if (val == 6 /* Tag_CPU_arch. */)
16065 {
16066 READ_ULEB (val, p, end);
16067 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16068 printf ("??? (%d)\n", val);
16069 else
16070 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16071 }
16072 else
16073 printf ("???\n");
16074 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16075 ;
16076 break;
16077
16078 default:
16079 printf (_("<unknown: %d>\n"), tag);
16080 break;
16081 }
16082 return p;
16083
16084 case 1:
16085 return display_tag_value (-1, p, end);
16086 case 2:
16087 return display_tag_value (0, p, end);
16088
16089 default:
16090 assert (attr->type & 0x80);
16091 READ_ULEB (val, p, end);
16092 type = attr->type & 0x7f;
16093 if (val >= type)
16094 printf ("??? (%d)\n", val);
16095 else
16096 printf ("%s\n", attr->table[val]);
16097 return p;
16098 }
16099 }
16100
16101 return display_tag_value (tag, p, end);
16102 }
16103
16104 static unsigned char *
16105 display_gnu_attribute (unsigned char * p,
16106 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16107 const unsigned char * const end)
16108 {
16109 unsigned int tag;
16110 unsigned int val;
16111
16112 READ_ULEB (tag, p, end);
16113
16114 /* Tag_compatibility is the only generic GNU attribute defined at
16115 present. */
16116 if (tag == 32)
16117 {
16118 READ_ULEB (val, p, end);
16119
16120 printf (_("flag = %d, vendor = "), val);
16121 if (p == end)
16122 {
16123 printf (_("<corrupt>\n"));
16124 warn (_("corrupt vendor attribute\n"));
16125 }
16126 else
16127 {
16128 if (p < end - 1)
16129 {
16130 size_t maxlen = (end - p) - 1;
16131
16132 print_symbol ((int) maxlen, (const char *) p);
16133 p += strnlen ((char *) p, maxlen) + 1;
16134 }
16135 else
16136 {
16137 printf (_("<corrupt>"));
16138 p = (unsigned char *) end;
16139 }
16140 putchar ('\n');
16141 }
16142 return p;
16143 }
16144
16145 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16146 return display_proc_gnu_attribute (p, tag, end);
16147
16148 return display_tag_value (tag, p, end);
16149 }
16150
16151 static unsigned char *
16152 display_m68k_gnu_attribute (unsigned char * p,
16153 unsigned int tag,
16154 const unsigned char * const end)
16155 {
16156 unsigned int val;
16157
16158 if (tag == Tag_GNU_M68K_ABI_FP)
16159 {
16160 printf (" Tag_GNU_M68K_ABI_FP: ");
16161 if (p == end)
16162 {
16163 printf (_("<corrupt>\n"));
16164 return p;
16165 }
16166 READ_ULEB (val, p, end);
16167
16168 if (val > 3)
16169 printf ("(%#x), ", val);
16170
16171 switch (val & 3)
16172 {
16173 case 0:
16174 printf (_("unspecified hard/soft float\n"));
16175 break;
16176 case 1:
16177 printf (_("hard float\n"));
16178 break;
16179 case 2:
16180 printf (_("soft float\n"));
16181 break;
16182 }
16183 return p;
16184 }
16185
16186 return display_tag_value (tag & 1, p, end);
16187 }
16188
16189 static unsigned char *
16190 display_power_gnu_attribute (unsigned char * p,
16191 unsigned int tag,
16192 const unsigned char * const end)
16193 {
16194 unsigned int val;
16195
16196 if (tag == Tag_GNU_Power_ABI_FP)
16197 {
16198 printf (" Tag_GNU_Power_ABI_FP: ");
16199 if (p == end)
16200 {
16201 printf (_("<corrupt>\n"));
16202 return p;
16203 }
16204 READ_ULEB (val, p, end);
16205
16206 if (val > 15)
16207 printf ("(%#x), ", val);
16208
16209 switch (val & 3)
16210 {
16211 case 0:
16212 printf (_("unspecified hard/soft float, "));
16213 break;
16214 case 1:
16215 printf (_("hard float, "));
16216 break;
16217 case 2:
16218 printf (_("soft float, "));
16219 break;
16220 case 3:
16221 printf (_("single-precision hard float, "));
16222 break;
16223 }
16224
16225 switch (val & 0xC)
16226 {
16227 case 0:
16228 printf (_("unspecified long double\n"));
16229 break;
16230 case 4:
16231 printf (_("128-bit IBM long double\n"));
16232 break;
16233 case 8:
16234 printf (_("64-bit long double\n"));
16235 break;
16236 case 12:
16237 printf (_("128-bit IEEE long double\n"));
16238 break;
16239 }
16240 return p;
16241 }
16242
16243 if (tag == Tag_GNU_Power_ABI_Vector)
16244 {
16245 printf (" Tag_GNU_Power_ABI_Vector: ");
16246 if (p == end)
16247 {
16248 printf (_("<corrupt>\n"));
16249 return p;
16250 }
16251 READ_ULEB (val, p, end);
16252
16253 if (val > 3)
16254 printf ("(%#x), ", val);
16255
16256 switch (val & 3)
16257 {
16258 case 0:
16259 printf (_("unspecified\n"));
16260 break;
16261 case 1:
16262 printf (_("generic\n"));
16263 break;
16264 case 2:
16265 printf ("AltiVec\n");
16266 break;
16267 case 3:
16268 printf ("SPE\n");
16269 break;
16270 }
16271 return p;
16272 }
16273
16274 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16275 {
16276 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
16277 if (p == end)
16278 {
16279 printf (_("<corrupt>\n"));
16280 return p;
16281 }
16282 READ_ULEB (val, p, end);
16283
16284 if (val > 2)
16285 printf ("(%#x), ", val);
16286
16287 switch (val & 3)
16288 {
16289 case 0:
16290 printf (_("unspecified\n"));
16291 break;
16292 case 1:
16293 printf ("r3/r4\n");
16294 break;
16295 case 2:
16296 printf (_("memory\n"));
16297 break;
16298 case 3:
16299 printf ("???\n");
16300 break;
16301 }
16302 return p;
16303 }
16304
16305 return display_tag_value (tag & 1, p, end);
16306 }
16307
16308 static unsigned char *
16309 display_s390_gnu_attribute (unsigned char * p,
16310 unsigned int tag,
16311 const unsigned char * const end)
16312 {
16313 unsigned int val;
16314
16315 if (tag == Tag_GNU_S390_ABI_Vector)
16316 {
16317 printf (" Tag_GNU_S390_ABI_Vector: ");
16318 READ_ULEB (val, p, end);
16319
16320 switch (val)
16321 {
16322 case 0:
16323 printf (_("any\n"));
16324 break;
16325 case 1:
16326 printf (_("software\n"));
16327 break;
16328 case 2:
16329 printf (_("hardware\n"));
16330 break;
16331 default:
16332 printf ("??? (%d)\n", val);
16333 break;
16334 }
16335 return p;
16336 }
16337
16338 return display_tag_value (tag & 1, p, end);
16339 }
16340
16341 static void
16342 display_sparc_hwcaps (unsigned int mask)
16343 {
16344 if (mask)
16345 {
16346 bool first = true;
16347
16348 if (mask & ELF_SPARC_HWCAP_MUL32)
16349 fputs ("mul32", stdout), first = false;
16350 if (mask & ELF_SPARC_HWCAP_DIV32)
16351 printf ("%sdiv32", first ? "" : "|"), first = false;
16352 if (mask & ELF_SPARC_HWCAP_FSMULD)
16353 printf ("%sfsmuld", first ? "" : "|"), first = false;
16354 if (mask & ELF_SPARC_HWCAP_V8PLUS)
16355 printf ("%sv8plus", first ? "" : "|"), first = false;
16356 if (mask & ELF_SPARC_HWCAP_POPC)
16357 printf ("%spopc", first ? "" : "|"), first = false;
16358 if (mask & ELF_SPARC_HWCAP_VIS)
16359 printf ("%svis", first ? "" : "|"), first = false;
16360 if (mask & ELF_SPARC_HWCAP_VIS2)
16361 printf ("%svis2", first ? "" : "|"), first = false;
16362 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
16363 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
16364 if (mask & ELF_SPARC_HWCAP_FMAF)
16365 printf ("%sfmaf", first ? "" : "|"), first = false;
16366 if (mask & ELF_SPARC_HWCAP_VIS3)
16367 printf ("%svis3", first ? "" : "|"), first = false;
16368 if (mask & ELF_SPARC_HWCAP_HPC)
16369 printf ("%shpc", first ? "" : "|"), first = false;
16370 if (mask & ELF_SPARC_HWCAP_RANDOM)
16371 printf ("%srandom", first ? "" : "|"), first = false;
16372 if (mask & ELF_SPARC_HWCAP_TRANS)
16373 printf ("%strans", first ? "" : "|"), first = false;
16374 if (mask & ELF_SPARC_HWCAP_FJFMAU)
16375 printf ("%sfjfmau", first ? "" : "|"), first = false;
16376 if (mask & ELF_SPARC_HWCAP_IMA)
16377 printf ("%sima", first ? "" : "|"), first = false;
16378 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
16379 printf ("%scspare", first ? "" : "|"), first = false;
16380 }
16381 else
16382 fputc ('0', stdout);
16383 fputc ('\n', stdout);
16384 }
16385
16386 static void
16387 display_sparc_hwcaps2 (unsigned int mask)
16388 {
16389 if (mask)
16390 {
16391 bool first = true;
16392
16393 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
16394 fputs ("fjathplus", stdout), first = false;
16395 if (mask & ELF_SPARC_HWCAP2_VIS3B)
16396 printf ("%svis3b", first ? "" : "|"), first = false;
16397 if (mask & ELF_SPARC_HWCAP2_ADP)
16398 printf ("%sadp", first ? "" : "|"), first = false;
16399 if (mask & ELF_SPARC_HWCAP2_SPARC5)
16400 printf ("%ssparc5", first ? "" : "|"), first = false;
16401 if (mask & ELF_SPARC_HWCAP2_MWAIT)
16402 printf ("%smwait", first ? "" : "|"), first = false;
16403 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
16404 printf ("%sxmpmul", first ? "" : "|"), first = false;
16405 if (mask & ELF_SPARC_HWCAP2_XMONT)
16406 printf ("%sxmont2", first ? "" : "|"), first = false;
16407 if (mask & ELF_SPARC_HWCAP2_NSEC)
16408 printf ("%snsec", first ? "" : "|"), first = false;
16409 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
16410 printf ("%sfjathhpc", first ? "" : "|"), first = false;
16411 if (mask & ELF_SPARC_HWCAP2_FJDES)
16412 printf ("%sfjdes", first ? "" : "|"), first = false;
16413 if (mask & ELF_SPARC_HWCAP2_FJAES)
16414 printf ("%sfjaes", first ? "" : "|"), first = false;
16415 }
16416 else
16417 fputc ('0', stdout);
16418 fputc ('\n', stdout);
16419 }
16420
16421 static unsigned char *
16422 display_sparc_gnu_attribute (unsigned char * p,
16423 unsigned int tag,
16424 const unsigned char * const end)
16425 {
16426 unsigned int val;
16427
16428 if (tag == Tag_GNU_Sparc_HWCAPS)
16429 {
16430 READ_ULEB (val, p, end);
16431 printf (" Tag_GNU_Sparc_HWCAPS: ");
16432 display_sparc_hwcaps (val);
16433 return p;
16434 }
16435 if (tag == Tag_GNU_Sparc_HWCAPS2)
16436 {
16437 READ_ULEB (val, p, end);
16438 printf (" Tag_GNU_Sparc_HWCAPS2: ");
16439 display_sparc_hwcaps2 (val);
16440 return p;
16441 }
16442
16443 return display_tag_value (tag, p, end);
16444 }
16445
16446 static void
16447 print_mips_fp_abi_value (unsigned int val)
16448 {
16449 switch (val)
16450 {
16451 case Val_GNU_MIPS_ABI_FP_ANY:
16452 printf (_("Hard or soft float\n"));
16453 break;
16454 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16455 printf (_("Hard float (double precision)\n"));
16456 break;
16457 case Val_GNU_MIPS_ABI_FP_SINGLE:
16458 printf (_("Hard float (single precision)\n"));
16459 break;
16460 case Val_GNU_MIPS_ABI_FP_SOFT:
16461 printf (_("Soft float\n"));
16462 break;
16463 case Val_GNU_MIPS_ABI_FP_OLD_64:
16464 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16465 break;
16466 case Val_GNU_MIPS_ABI_FP_XX:
16467 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
16468 break;
16469 case Val_GNU_MIPS_ABI_FP_64:
16470 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
16471 break;
16472 case Val_GNU_MIPS_ABI_FP_64A:
16473 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16474 break;
16475 case Val_GNU_MIPS_ABI_FP_NAN2008:
16476 printf (_("NaN 2008 compatibility\n"));
16477 break;
16478 default:
16479 printf ("??? (%d)\n", val);
16480 break;
16481 }
16482 }
16483
16484 static unsigned char *
16485 display_mips_gnu_attribute (unsigned char * p,
16486 unsigned int tag,
16487 const unsigned char * const end)
16488 {
16489 if (tag == Tag_GNU_MIPS_ABI_FP)
16490 {
16491 unsigned int val;
16492
16493 printf (" Tag_GNU_MIPS_ABI_FP: ");
16494 READ_ULEB (val, p, end);
16495 print_mips_fp_abi_value (val);
16496 return p;
16497 }
16498
16499 if (tag == Tag_GNU_MIPS_ABI_MSA)
16500 {
16501 unsigned int val;
16502
16503 printf (" Tag_GNU_MIPS_ABI_MSA: ");
16504 READ_ULEB (val, p, end);
16505
16506 switch (val)
16507 {
16508 case Val_GNU_MIPS_ABI_MSA_ANY:
16509 printf (_("Any MSA or not\n"));
16510 break;
16511 case Val_GNU_MIPS_ABI_MSA_128:
16512 printf (_("128-bit MSA\n"));
16513 break;
16514 default:
16515 printf ("??? (%d)\n", val);
16516 break;
16517 }
16518 return p;
16519 }
16520
16521 return display_tag_value (tag & 1, p, end);
16522 }
16523
16524 static unsigned char *
16525 display_tic6x_attribute (unsigned char * p,
16526 const unsigned char * const end)
16527 {
16528 unsigned int tag;
16529 unsigned int val;
16530
16531 READ_ULEB (tag, p, end);
16532
16533 switch (tag)
16534 {
16535 case Tag_ISA:
16536 printf (" Tag_ISA: ");
16537 READ_ULEB (val, p, end);
16538
16539 switch (val)
16540 {
16541 case C6XABI_Tag_ISA_none:
16542 printf (_("None\n"));
16543 break;
16544 case C6XABI_Tag_ISA_C62X:
16545 printf ("C62x\n");
16546 break;
16547 case C6XABI_Tag_ISA_C67X:
16548 printf ("C67x\n");
16549 break;
16550 case C6XABI_Tag_ISA_C67XP:
16551 printf ("C67x+\n");
16552 break;
16553 case C6XABI_Tag_ISA_C64X:
16554 printf ("C64x\n");
16555 break;
16556 case C6XABI_Tag_ISA_C64XP:
16557 printf ("C64x+\n");
16558 break;
16559 case C6XABI_Tag_ISA_C674X:
16560 printf ("C674x\n");
16561 break;
16562 default:
16563 printf ("??? (%d)\n", val);
16564 break;
16565 }
16566 return p;
16567
16568 case Tag_ABI_wchar_t:
16569 printf (" Tag_ABI_wchar_t: ");
16570 READ_ULEB (val, p, end);
16571 switch (val)
16572 {
16573 case 0:
16574 printf (_("Not used\n"));
16575 break;
16576 case 1:
16577 printf (_("2 bytes\n"));
16578 break;
16579 case 2:
16580 printf (_("4 bytes\n"));
16581 break;
16582 default:
16583 printf ("??? (%d)\n", val);
16584 break;
16585 }
16586 return p;
16587
16588 case Tag_ABI_stack_align_needed:
16589 printf (" Tag_ABI_stack_align_needed: ");
16590 READ_ULEB (val, p, end);
16591 switch (val)
16592 {
16593 case 0:
16594 printf (_("8-byte\n"));
16595 break;
16596 case 1:
16597 printf (_("16-byte\n"));
16598 break;
16599 default:
16600 printf ("??? (%d)\n", val);
16601 break;
16602 }
16603 return p;
16604
16605 case Tag_ABI_stack_align_preserved:
16606 READ_ULEB (val, p, end);
16607 printf (" Tag_ABI_stack_align_preserved: ");
16608 switch (val)
16609 {
16610 case 0:
16611 printf (_("8-byte\n"));
16612 break;
16613 case 1:
16614 printf (_("16-byte\n"));
16615 break;
16616 default:
16617 printf ("??? (%d)\n", val);
16618 break;
16619 }
16620 return p;
16621
16622 case Tag_ABI_DSBT:
16623 READ_ULEB (val, p, end);
16624 printf (" Tag_ABI_DSBT: ");
16625 switch (val)
16626 {
16627 case 0:
16628 printf (_("DSBT addressing not used\n"));
16629 break;
16630 case 1:
16631 printf (_("DSBT addressing used\n"));
16632 break;
16633 default:
16634 printf ("??? (%d)\n", val);
16635 break;
16636 }
16637 return p;
16638
16639 case Tag_ABI_PID:
16640 READ_ULEB (val, p, end);
16641 printf (" Tag_ABI_PID: ");
16642 switch (val)
16643 {
16644 case 0:
16645 printf (_("Data addressing position-dependent\n"));
16646 break;
16647 case 1:
16648 printf (_("Data addressing position-independent, GOT near DP\n"));
16649 break;
16650 case 2:
16651 printf (_("Data addressing position-independent, GOT far from DP\n"));
16652 break;
16653 default:
16654 printf ("??? (%d)\n", val);
16655 break;
16656 }
16657 return p;
16658
16659 case Tag_ABI_PIC:
16660 READ_ULEB (val, p, end);
16661 printf (" Tag_ABI_PIC: ");
16662 switch (val)
16663 {
16664 case 0:
16665 printf (_("Code addressing position-dependent\n"));
16666 break;
16667 case 1:
16668 printf (_("Code addressing position-independent\n"));
16669 break;
16670 default:
16671 printf ("??? (%d)\n", val);
16672 break;
16673 }
16674 return p;
16675
16676 case Tag_ABI_array_object_alignment:
16677 READ_ULEB (val, p, end);
16678 printf (" Tag_ABI_array_object_alignment: ");
16679 switch (val)
16680 {
16681 case 0:
16682 printf (_("8-byte\n"));
16683 break;
16684 case 1:
16685 printf (_("4-byte\n"));
16686 break;
16687 case 2:
16688 printf (_("16-byte\n"));
16689 break;
16690 default:
16691 printf ("??? (%d)\n", val);
16692 break;
16693 }
16694 return p;
16695
16696 case Tag_ABI_array_object_align_expected:
16697 READ_ULEB (val, p, end);
16698 printf (" Tag_ABI_array_object_align_expected: ");
16699 switch (val)
16700 {
16701 case 0:
16702 printf (_("8-byte\n"));
16703 break;
16704 case 1:
16705 printf (_("4-byte\n"));
16706 break;
16707 case 2:
16708 printf (_("16-byte\n"));
16709 break;
16710 default:
16711 printf ("??? (%d)\n", val);
16712 break;
16713 }
16714 return p;
16715
16716 case Tag_ABI_compatibility:
16717 {
16718 READ_ULEB (val, p, end);
16719 printf (" Tag_ABI_compatibility: ");
16720 printf (_("flag = %d, vendor = "), val);
16721 if (p < end - 1)
16722 {
16723 size_t maxlen = (end - p) - 1;
16724
16725 print_symbol ((int) maxlen, (const char *) p);
16726 p += strnlen ((char *) p, maxlen) + 1;
16727 }
16728 else
16729 {
16730 printf (_("<corrupt>"));
16731 p = (unsigned char *) end;
16732 }
16733 putchar ('\n');
16734 return p;
16735 }
16736
16737 case Tag_ABI_conformance:
16738 {
16739 printf (" Tag_ABI_conformance: \"");
16740 if (p < end - 1)
16741 {
16742 size_t maxlen = (end - p) - 1;
16743
16744 print_symbol ((int) maxlen, (const char *) p);
16745 p += strnlen ((char *) p, maxlen) + 1;
16746 }
16747 else
16748 {
16749 printf (_("<corrupt>"));
16750 p = (unsigned char *) end;
16751 }
16752 printf ("\"\n");
16753 return p;
16754 }
16755 }
16756
16757 return display_tag_value (tag, p, end);
16758 }
16759
16760 static void
16761 display_raw_attribute (unsigned char * p, unsigned char const * const end)
16762 {
16763 unsigned long addr = 0;
16764 size_t bytes = end - p;
16765
16766 assert (end >= p);
16767 while (bytes)
16768 {
16769 int j;
16770 int k;
16771 int lbytes = (bytes > 16 ? 16 : bytes);
16772
16773 printf (" 0x%8.8lx ", addr);
16774
16775 for (j = 0; j < 16; j++)
16776 {
16777 if (j < lbytes)
16778 printf ("%2.2x", p[j]);
16779 else
16780 printf (" ");
16781
16782 if ((j & 3) == 3)
16783 printf (" ");
16784 }
16785
16786 for (j = 0; j < lbytes; j++)
16787 {
16788 k = p[j];
16789 if (k >= ' ' && k < 0x7f)
16790 printf ("%c", k);
16791 else
16792 printf (".");
16793 }
16794
16795 putchar ('\n');
16796
16797 p += lbytes;
16798 bytes -= lbytes;
16799 addr += lbytes;
16800 }
16801
16802 putchar ('\n');
16803 }
16804
16805 static unsigned char *
16806 display_msp430_attribute (unsigned char * p,
16807 const unsigned char * const end)
16808 {
16809 unsigned int val;
16810 unsigned int tag;
16811
16812 READ_ULEB (tag, p, end);
16813
16814 switch (tag)
16815 {
16816 case OFBA_MSPABI_Tag_ISA:
16817 printf (" Tag_ISA: ");
16818 READ_ULEB (val, p, end);
16819 switch (val)
16820 {
16821 case 0: printf (_("None\n")); break;
16822 case 1: printf (_("MSP430\n")); break;
16823 case 2: printf (_("MSP430X\n")); break;
16824 default: printf ("??? (%d)\n", val); break;
16825 }
16826 break;
16827
16828 case OFBA_MSPABI_Tag_Code_Model:
16829 printf (" Tag_Code_Model: ");
16830 READ_ULEB (val, p, end);
16831 switch (val)
16832 {
16833 case 0: printf (_("None\n")); break;
16834 case 1: printf (_("Small\n")); break;
16835 case 2: printf (_("Large\n")); break;
16836 default: printf ("??? (%d)\n", val); break;
16837 }
16838 break;
16839
16840 case OFBA_MSPABI_Tag_Data_Model:
16841 printf (" Tag_Data_Model: ");
16842 READ_ULEB (val, p, end);
16843 switch (val)
16844 {
16845 case 0: printf (_("None\n")); break;
16846 case 1: printf (_("Small\n")); break;
16847 case 2: printf (_("Large\n")); break;
16848 case 3: printf (_("Restricted Large\n")); break;
16849 default: printf ("??? (%d)\n", val); break;
16850 }
16851 break;
16852
16853 default:
16854 printf (_(" <unknown tag %d>: "), tag);
16855
16856 if (tag & 1)
16857 {
16858 putchar ('"');
16859 if (p < end - 1)
16860 {
16861 size_t maxlen = (end - p) - 1;
16862
16863 print_symbol ((int) maxlen, (const char *) p);
16864 p += strnlen ((char *) p, maxlen) + 1;
16865 }
16866 else
16867 {
16868 printf (_("<corrupt>"));
16869 p = (unsigned char *) end;
16870 }
16871 printf ("\"\n");
16872 }
16873 else
16874 {
16875 READ_ULEB (val, p, end);
16876 printf ("%d (0x%x)\n", val, val);
16877 }
16878 break;
16879 }
16880
16881 assert (p <= end);
16882 return p;
16883 }
16884
16885 static unsigned char *
16886 display_msp430_gnu_attribute (unsigned char * p,
16887 unsigned int tag,
16888 const unsigned char * const end)
16889 {
16890 if (tag == Tag_GNU_MSP430_Data_Region)
16891 {
16892 unsigned int val;
16893
16894 printf (" Tag_GNU_MSP430_Data_Region: ");
16895 READ_ULEB (val, p, end);
16896
16897 switch (val)
16898 {
16899 case Val_GNU_MSP430_Data_Region_Any:
16900 printf (_("Any Region\n"));
16901 break;
16902 case Val_GNU_MSP430_Data_Region_Lower:
16903 printf (_("Lower Region Only\n"));
16904 break;
16905 default:
16906 printf ("??? (%u)\n", val);
16907 }
16908 return p;
16909 }
16910 return display_tag_value (tag & 1, p, end);
16911 }
16912
16913 struct riscv_attr_tag_t {
16914 const char *name;
16915 unsigned int tag;
16916 };
16917
16918 static struct riscv_attr_tag_t riscv_attr_tag[] =
16919 {
16920 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
16921 T(arch),
16922 T(priv_spec),
16923 T(priv_spec_minor),
16924 T(priv_spec_revision),
16925 T(unaligned_access),
16926 T(stack_align),
16927 #undef T
16928 };
16929
16930 static unsigned char *
16931 display_riscv_attribute (unsigned char *p,
16932 const unsigned char * const end)
16933 {
16934 unsigned int val;
16935 unsigned int tag;
16936 struct riscv_attr_tag_t *attr = NULL;
16937 unsigned i;
16938
16939 READ_ULEB (tag, p, end);
16940
16941 /* Find the name of attribute. */
16942 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
16943 {
16944 if (riscv_attr_tag[i].tag == tag)
16945 {
16946 attr = &riscv_attr_tag[i];
16947 break;
16948 }
16949 }
16950
16951 if (attr)
16952 printf (" %s: ", attr->name);
16953 else
16954 return display_tag_value (tag, p, end);
16955
16956 switch (tag)
16957 {
16958 case Tag_RISCV_priv_spec:
16959 case Tag_RISCV_priv_spec_minor:
16960 case Tag_RISCV_priv_spec_revision:
16961 READ_ULEB (val, p, end);
16962 printf (_("%u\n"), val);
16963 break;
16964 case Tag_RISCV_unaligned_access:
16965 READ_ULEB (val, p, end);
16966 switch (val)
16967 {
16968 case 0:
16969 printf (_("No unaligned access\n"));
16970 break;
16971 case 1:
16972 printf (_("Unaligned access\n"));
16973 break;
16974 }
16975 break;
16976 case Tag_RISCV_stack_align:
16977 READ_ULEB (val, p, end);
16978 printf (_("%u-bytes\n"), val);
16979 break;
16980 case Tag_RISCV_arch:
16981 p = display_tag_value (-1, p, end);
16982 break;
16983 default:
16984 return display_tag_value (tag, p, end);
16985 }
16986
16987 return p;
16988 }
16989
16990 static unsigned char *
16991 display_csky_attribute (unsigned char * p,
16992 const unsigned char * const end)
16993 {
16994 unsigned int tag;
16995 unsigned int val;
16996 READ_ULEB (tag, p, end);
16997
16998 if (tag >= Tag_CSKY_MAX)
16999 {
17000 return display_tag_value (-1, p, end);
17001 }
17002
17003 switch (tag)
17004 {
17005 case Tag_CSKY_ARCH_NAME:
17006 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17007 return display_tag_value (-1, p, end);
17008 case Tag_CSKY_CPU_NAME:
17009 printf (" Tag_CSKY_CPU_NAME:\t\t");
17010 return display_tag_value (-1, p, end);
17011
17012 case Tag_CSKY_ISA_FLAGS:
17013 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17014 return display_tag_value (0, p, end);
17015 case Tag_CSKY_ISA_EXT_FLAGS:
17016 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17017 return display_tag_value (0, p, end);
17018
17019 case Tag_CSKY_DSP_VERSION:
17020 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17021 READ_ULEB (val, p, end);
17022 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17023 printf ("DSP Extension\n");
17024 else if (val == VAL_CSKY_DSP_VERSION_2)
17025 printf ("DSP 2.0\n");
17026 break;
17027
17028 case Tag_CSKY_VDSP_VERSION:
17029 printf (" Tag_CSKY_VDSP_VERSION:\t");
17030 READ_ULEB (val, p, end);
17031 printf ("VDSP Version %d\n", val);
17032 break;
17033
17034 case Tag_CSKY_FPU_VERSION:
17035 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17036 READ_ULEB (val, p, end);
17037 if (val == VAL_CSKY_FPU_VERSION_1)
17038 printf ("ABIV1 FPU Version 1\n");
17039 else if (val == VAL_CSKY_FPU_VERSION_2)
17040 printf ("FPU Version 2\n");
17041 break;
17042
17043 case Tag_CSKY_FPU_ABI:
17044 printf (" Tag_CSKY_FPU_ABI:\t\t");
17045 READ_ULEB (val, p, end);
17046 if (val == VAL_CSKY_FPU_ABI_HARD)
17047 printf ("Hard\n");
17048 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17049 printf ("SoftFP\n");
17050 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17051 printf ("Soft\n");
17052 break;
17053 case Tag_CSKY_FPU_ROUNDING:
17054 READ_ULEB (val, p, end);
17055 if (val == 1) {
17056 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17057 printf ("Needed\n");
17058 }
17059 break;
17060 case Tag_CSKY_FPU_DENORMAL:
17061 READ_ULEB (val, p, end);
17062 if (val == 1) {
17063 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17064 printf ("Needed\n");
17065 }
17066 break;
17067 case Tag_CSKY_FPU_Exception:
17068 READ_ULEB (val, p, end);
17069 if (val == 1) {
17070 printf (" Tag_CSKY_FPU_Exception:\t");
17071 printf ("Needed\n");
17072 }
17073 break;
17074 case Tag_CSKY_FPU_NUMBER_MODULE:
17075 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17076 return display_tag_value (-1, p, end);
17077 case Tag_CSKY_FPU_HARDFP:
17078 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17079 READ_ULEB (val, p, end);
17080 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17081 printf (" Half");
17082 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17083 printf (" Single");
17084 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17085 printf (" Double");
17086 printf ("\n");
17087 break;
17088 default:
17089 return display_tag_value (tag, p, end);
17090 }
17091 return p;
17092 }
17093
17094 static bool
17095 process_attributes (Filedata * filedata,
17096 const char * public_name,
17097 unsigned int proc_type,
17098 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17099 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17100 {
17101 Elf_Internal_Shdr * sect;
17102 unsigned i;
17103 bool res = true;
17104
17105 /* Find the section header so that we get the size. */
17106 for (i = 0, sect = filedata->section_headers;
17107 i < filedata->file_header.e_shnum;
17108 i++, sect++)
17109 {
17110 unsigned char * contents;
17111 unsigned char * p;
17112
17113 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17114 continue;
17115
17116 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17117 sect->sh_size, _("attributes"));
17118 if (contents == NULL)
17119 {
17120 res = false;
17121 continue;
17122 }
17123
17124 p = contents;
17125 /* The first character is the version of the attributes.
17126 Currently only version 1, (aka 'A') is recognised here. */
17127 if (*p != 'A')
17128 {
17129 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17130 res = false;
17131 }
17132 else
17133 {
17134 bfd_vma section_len;
17135
17136 section_len = sect->sh_size - 1;
17137 p++;
17138
17139 while (section_len > 0)
17140 {
17141 bfd_vma attr_len;
17142 unsigned int namelen;
17143 bool public_section;
17144 bool gnu_section;
17145
17146 if (section_len <= 4)
17147 {
17148 error (_("Tag section ends prematurely\n"));
17149 res = false;
17150 break;
17151 }
17152 attr_len = byte_get (p, 4);
17153 p += 4;
17154
17155 if (attr_len > section_len)
17156 {
17157 error (_("Bad attribute length (%u > %u)\n"),
17158 (unsigned) attr_len, (unsigned) section_len);
17159 attr_len = section_len;
17160 res = false;
17161 }
17162 /* PR 17531: file: 001-101425-0.004 */
17163 else if (attr_len < 5)
17164 {
17165 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17166 res = false;
17167 break;
17168 }
17169
17170 section_len -= attr_len;
17171 attr_len -= 4;
17172
17173 namelen = strnlen ((char *) p, attr_len) + 1;
17174 if (namelen == 0 || namelen >= attr_len)
17175 {
17176 error (_("Corrupt attribute section name\n"));
17177 res = false;
17178 break;
17179 }
17180
17181 printf (_("Attribute Section: "));
17182 print_symbol (INT_MAX, (const char *) p);
17183 putchar ('\n');
17184
17185 if (public_name && streq ((char *) p, public_name))
17186 public_section = true;
17187 else
17188 public_section = false;
17189
17190 if (streq ((char *) p, "gnu"))
17191 gnu_section = true;
17192 else
17193 gnu_section = false;
17194
17195 p += namelen;
17196 attr_len -= namelen;
17197
17198 while (attr_len > 0 && p < contents + sect->sh_size)
17199 {
17200 int tag;
17201 unsigned int val;
17202 bfd_vma size;
17203 unsigned char * end;
17204
17205 /* PR binutils/17531: Safe handling of corrupt files. */
17206 if (attr_len < 6)
17207 {
17208 error (_("Unused bytes at end of section\n"));
17209 res = false;
17210 section_len = 0;
17211 break;
17212 }
17213
17214 tag = *(p++);
17215 size = byte_get (p, 4);
17216 if (size > attr_len)
17217 {
17218 error (_("Bad subsection length (%u > %u)\n"),
17219 (unsigned) size, (unsigned) attr_len);
17220 res = false;
17221 size = attr_len;
17222 }
17223 /* PR binutils/17531: Safe handling of corrupt files. */
17224 if (size < 6)
17225 {
17226 error (_("Bad subsection length (%u < 6)\n"),
17227 (unsigned) size);
17228 res = false;
17229 section_len = 0;
17230 break;
17231 }
17232
17233 attr_len -= size;
17234 end = p + size - 1;
17235 assert (end <= contents + sect->sh_size);
17236 p += 4;
17237
17238 switch (tag)
17239 {
17240 case 1:
17241 printf (_("File Attributes\n"));
17242 break;
17243 case 2:
17244 printf (_("Section Attributes:"));
17245 goto do_numlist;
17246 case 3:
17247 printf (_("Symbol Attributes:"));
17248 /* Fall through. */
17249 do_numlist:
17250 for (;;)
17251 {
17252 READ_ULEB (val, p, end);
17253 if (val == 0)
17254 break;
17255 printf (" %d", val);
17256 }
17257 printf ("\n");
17258 break;
17259 default:
17260 printf (_("Unknown tag: %d\n"), tag);
17261 public_section = false;
17262 break;
17263 }
17264
17265 if (public_section && display_pub_attribute != NULL)
17266 {
17267 while (p < end)
17268 p = display_pub_attribute (p, end);
17269 assert (p == end);
17270 }
17271 else if (gnu_section && display_proc_gnu_attribute != NULL)
17272 {
17273 while (p < end)
17274 p = display_gnu_attribute (p,
17275 display_proc_gnu_attribute,
17276 end);
17277 assert (p == end);
17278 }
17279 else if (p < end)
17280 {
17281 printf (_(" Unknown attribute:\n"));
17282 display_raw_attribute (p, end);
17283 p = end;
17284 }
17285 else
17286 attr_len = 0;
17287 }
17288 }
17289 }
17290
17291 free (contents);
17292 }
17293
17294 return res;
17295 }
17296
17297 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17298 Print the Address, Access and Initial fields of an entry at VMA ADDR
17299 and return the VMA of the next entry, or -1 if there was a problem.
17300 Does not read from DATA_END or beyond. */
17301
17302 static bfd_vma
17303 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17304 unsigned char * data_end)
17305 {
17306 printf (" ");
17307 print_vma (addr, LONG_HEX);
17308 printf (" ");
17309 if (addr < pltgot + 0xfff0)
17310 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17311 else
17312 printf ("%10s", "");
17313 printf (" ");
17314 if (data == NULL)
17315 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17316 else
17317 {
17318 bfd_vma entry;
17319 unsigned char * from = data + addr - pltgot;
17320
17321 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17322 {
17323 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17324 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17325 return (bfd_vma) -1;
17326 }
17327 else
17328 {
17329 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17330 print_vma (entry, LONG_HEX);
17331 }
17332 }
17333 return addr + (is_32bit_elf ? 4 : 8);
17334 }
17335
17336 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17337 PLTGOT. Print the Address and Initial fields of an entry at VMA
17338 ADDR and return the VMA of the next entry. */
17339
17340 static bfd_vma
17341 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
17342 {
17343 printf (" ");
17344 print_vma (addr, LONG_HEX);
17345 printf (" ");
17346 if (data == NULL)
17347 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17348 else
17349 {
17350 bfd_vma entry;
17351
17352 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17353 print_vma (entry, LONG_HEX);
17354 }
17355 return addr + (is_32bit_elf ? 4 : 8);
17356 }
17357
17358 static void
17359 print_mips_ases (unsigned int mask)
17360 {
17361 if (mask & AFL_ASE_DSP)
17362 fputs ("\n\tDSP ASE", stdout);
17363 if (mask & AFL_ASE_DSPR2)
17364 fputs ("\n\tDSP R2 ASE", stdout);
17365 if (mask & AFL_ASE_DSPR3)
17366 fputs ("\n\tDSP R3 ASE", stdout);
17367 if (mask & AFL_ASE_EVA)
17368 fputs ("\n\tEnhanced VA Scheme", stdout);
17369 if (mask & AFL_ASE_MCU)
17370 fputs ("\n\tMCU (MicroController) ASE", stdout);
17371 if (mask & AFL_ASE_MDMX)
17372 fputs ("\n\tMDMX ASE", stdout);
17373 if (mask & AFL_ASE_MIPS3D)
17374 fputs ("\n\tMIPS-3D ASE", stdout);
17375 if (mask & AFL_ASE_MT)
17376 fputs ("\n\tMT ASE", stdout);
17377 if (mask & AFL_ASE_SMARTMIPS)
17378 fputs ("\n\tSmartMIPS ASE", stdout);
17379 if (mask & AFL_ASE_VIRT)
17380 fputs ("\n\tVZ ASE", stdout);
17381 if (mask & AFL_ASE_MSA)
17382 fputs ("\n\tMSA ASE", stdout);
17383 if (mask & AFL_ASE_MIPS16)
17384 fputs ("\n\tMIPS16 ASE", stdout);
17385 if (mask & AFL_ASE_MICROMIPS)
17386 fputs ("\n\tMICROMIPS ASE", stdout);
17387 if (mask & AFL_ASE_XPA)
17388 fputs ("\n\tXPA ASE", stdout);
17389 if (mask & AFL_ASE_MIPS16E2)
17390 fputs ("\n\tMIPS16e2 ASE", stdout);
17391 if (mask & AFL_ASE_CRC)
17392 fputs ("\n\tCRC ASE", stdout);
17393 if (mask & AFL_ASE_GINV)
17394 fputs ("\n\tGINV ASE", stdout);
17395 if (mask & AFL_ASE_LOONGSON_MMI)
17396 fputs ("\n\tLoongson MMI ASE", stdout);
17397 if (mask & AFL_ASE_LOONGSON_CAM)
17398 fputs ("\n\tLoongson CAM ASE", stdout);
17399 if (mask & AFL_ASE_LOONGSON_EXT)
17400 fputs ("\n\tLoongson EXT ASE", stdout);
17401 if (mask & AFL_ASE_LOONGSON_EXT2)
17402 fputs ("\n\tLoongson EXT2 ASE", stdout);
17403 if (mask == 0)
17404 fprintf (stdout, "\n\t%s", _("None"));
17405 else if ((mask & ~AFL_ASE_MASK) != 0)
17406 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
17407 }
17408
17409 static void
17410 print_mips_isa_ext (unsigned int isa_ext)
17411 {
17412 switch (isa_ext)
17413 {
17414 case 0:
17415 fputs (_("None"), stdout);
17416 break;
17417 case AFL_EXT_XLR:
17418 fputs ("RMI XLR", stdout);
17419 break;
17420 case AFL_EXT_OCTEON3:
17421 fputs ("Cavium Networks Octeon3", stdout);
17422 break;
17423 case AFL_EXT_OCTEON2:
17424 fputs ("Cavium Networks Octeon2", stdout);
17425 break;
17426 case AFL_EXT_OCTEONP:
17427 fputs ("Cavium Networks OcteonP", stdout);
17428 break;
17429 case AFL_EXT_OCTEON:
17430 fputs ("Cavium Networks Octeon", stdout);
17431 break;
17432 case AFL_EXT_5900:
17433 fputs ("Toshiba R5900", stdout);
17434 break;
17435 case AFL_EXT_4650:
17436 fputs ("MIPS R4650", stdout);
17437 break;
17438 case AFL_EXT_4010:
17439 fputs ("LSI R4010", stdout);
17440 break;
17441 case AFL_EXT_4100:
17442 fputs ("NEC VR4100", stdout);
17443 break;
17444 case AFL_EXT_3900:
17445 fputs ("Toshiba R3900", stdout);
17446 break;
17447 case AFL_EXT_10000:
17448 fputs ("MIPS R10000", stdout);
17449 break;
17450 case AFL_EXT_SB1:
17451 fputs ("Broadcom SB-1", stdout);
17452 break;
17453 case AFL_EXT_4111:
17454 fputs ("NEC VR4111/VR4181", stdout);
17455 break;
17456 case AFL_EXT_4120:
17457 fputs ("NEC VR4120", stdout);
17458 break;
17459 case AFL_EXT_5400:
17460 fputs ("NEC VR5400", stdout);
17461 break;
17462 case AFL_EXT_5500:
17463 fputs ("NEC VR5500", stdout);
17464 break;
17465 case AFL_EXT_LOONGSON_2E:
17466 fputs ("ST Microelectronics Loongson 2E", stdout);
17467 break;
17468 case AFL_EXT_LOONGSON_2F:
17469 fputs ("ST Microelectronics Loongson 2F", stdout);
17470 break;
17471 case AFL_EXT_INTERAPTIV_MR2:
17472 fputs ("Imagination interAptiv MR2", stdout);
17473 break;
17474 default:
17475 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
17476 }
17477 }
17478
17479 static signed int
17480 get_mips_reg_size (int reg_size)
17481 {
17482 return (reg_size == AFL_REG_NONE) ? 0
17483 : (reg_size == AFL_REG_32) ? 32
17484 : (reg_size == AFL_REG_64) ? 64
17485 : (reg_size == AFL_REG_128) ? 128
17486 : -1;
17487 }
17488
17489 static bool
17490 process_mips_specific (Filedata * filedata)
17491 {
17492 Elf_Internal_Dyn * entry;
17493 Elf_Internal_Shdr *sect = NULL;
17494 size_t liblist_offset = 0;
17495 size_t liblistno = 0;
17496 size_t conflictsno = 0;
17497 size_t options_offset = 0;
17498 size_t conflicts_offset = 0;
17499 size_t pltrelsz = 0;
17500 size_t pltrel = 0;
17501 bfd_vma pltgot = 0;
17502 bfd_vma mips_pltgot = 0;
17503 bfd_vma jmprel = 0;
17504 bfd_vma local_gotno = 0;
17505 bfd_vma gotsym = 0;
17506 bfd_vma symtabno = 0;
17507 bool res = true;
17508
17509 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
17510 display_mips_gnu_attribute))
17511 res = false;
17512
17513 sect = find_section (filedata, ".MIPS.abiflags");
17514
17515 if (sect != NULL)
17516 {
17517 Elf_External_ABIFlags_v0 *abiflags_ext;
17518 Elf_Internal_ABIFlags_v0 abiflags_in;
17519
17520 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
17521 {
17522 error (_("Corrupt MIPS ABI Flags section.\n"));
17523 res = false;
17524 }
17525 else
17526 {
17527 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
17528 sect->sh_size, _("MIPS ABI Flags section"));
17529 if (abiflags_ext)
17530 {
17531 abiflags_in.version = BYTE_GET (abiflags_ext->version);
17532 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
17533 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
17534 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
17535 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
17536 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
17537 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
17538 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
17539 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
17540 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
17541 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
17542
17543 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
17544 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
17545 if (abiflags_in.isa_rev > 1)
17546 printf ("r%d", abiflags_in.isa_rev);
17547 printf ("\nGPR size: %d",
17548 get_mips_reg_size (abiflags_in.gpr_size));
17549 printf ("\nCPR1 size: %d",
17550 get_mips_reg_size (abiflags_in.cpr1_size));
17551 printf ("\nCPR2 size: %d",
17552 get_mips_reg_size (abiflags_in.cpr2_size));
17553 fputs ("\nFP ABI: ", stdout);
17554 print_mips_fp_abi_value (abiflags_in.fp_abi);
17555 fputs ("ISA Extension: ", stdout);
17556 print_mips_isa_ext (abiflags_in.isa_ext);
17557 fputs ("\nASEs:", stdout);
17558 print_mips_ases (abiflags_in.ases);
17559 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
17560 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
17561 fputc ('\n', stdout);
17562 free (abiflags_ext);
17563 }
17564 }
17565 }
17566
17567 /* We have a lot of special sections. Thanks SGI! */
17568 if (filedata->dynamic_section == NULL)
17569 {
17570 /* No dynamic information available. See if there is static GOT. */
17571 sect = find_section (filedata, ".got");
17572 if (sect != NULL)
17573 {
17574 unsigned char *data_end;
17575 unsigned char *data;
17576 bfd_vma ent, end;
17577 int addr_size;
17578
17579 pltgot = sect->sh_addr;
17580
17581 ent = pltgot;
17582 addr_size = (is_32bit_elf ? 4 : 8);
17583 end = pltgot + sect->sh_size;
17584
17585 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
17586 end - pltgot, 1,
17587 _("Global Offset Table data"));
17588 /* PR 12855: Null data is handled gracefully throughout. */
17589 data_end = data + (end - pltgot);
17590
17591 printf (_("\nStatic GOT:\n"));
17592 printf (_(" Canonical gp value: "));
17593 print_vma (ent + 0x7ff0, LONG_HEX);
17594 printf ("\n\n");
17595
17596 /* In a dynamic binary GOT[0] is reserved for the dynamic
17597 loader to store the lazy resolver pointer, however in
17598 a static binary it may well have been omitted and GOT
17599 reduced to a table of addresses.
17600 PR 21344: Check for the entry being fully available
17601 before fetching it. */
17602 if (data
17603 && data + ent - pltgot + addr_size <= data_end
17604 && byte_get (data + ent - pltgot, addr_size) == 0)
17605 {
17606 printf (_(" Reserved entries:\n"));
17607 printf (_(" %*s %10s %*s\n"),
17608 addr_size * 2, _("Address"), _("Access"),
17609 addr_size * 2, _("Value"));
17610 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17611 printf ("\n");
17612 if (ent == (bfd_vma) -1)
17613 goto sgot_print_fail;
17614
17615 /* Check for the MSB of GOT[1] being set, identifying a
17616 GNU object. This entry will be used by some runtime
17617 loaders, to store the module pointer. Otherwise this
17618 is an ordinary local entry.
17619 PR 21344: Check for the entry being fully available
17620 before fetching it. */
17621 if (data
17622 && data + ent - pltgot + addr_size <= data_end
17623 && (byte_get (data + ent - pltgot, addr_size)
17624 >> (addr_size * 8 - 1)) != 0)
17625 {
17626 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17627 printf ("\n");
17628 if (ent == (bfd_vma) -1)
17629 goto sgot_print_fail;
17630 }
17631 printf ("\n");
17632 }
17633
17634 if (data != NULL && ent < end)
17635 {
17636 printf (_(" Local entries:\n"));
17637 printf (" %*s %10s %*s\n",
17638 addr_size * 2, _("Address"), _("Access"),
17639 addr_size * 2, _("Value"));
17640 while (ent < end)
17641 {
17642 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17643 printf ("\n");
17644 if (ent == (bfd_vma) -1)
17645 goto sgot_print_fail;
17646 }
17647 printf ("\n");
17648 }
17649
17650 sgot_print_fail:
17651 free (data);
17652 }
17653 return res;
17654 }
17655
17656 for (entry = filedata->dynamic_section;
17657 /* PR 17531 file: 012-50589-0.004. */
17658 (entry < filedata->dynamic_section + filedata->dynamic_nent
17659 && entry->d_tag != DT_NULL);
17660 ++entry)
17661 switch (entry->d_tag)
17662 {
17663 case DT_MIPS_LIBLIST:
17664 liblist_offset
17665 = offset_from_vma (filedata, entry->d_un.d_val,
17666 liblistno * sizeof (Elf32_External_Lib));
17667 break;
17668 case DT_MIPS_LIBLISTNO:
17669 liblistno = entry->d_un.d_val;
17670 break;
17671 case DT_MIPS_OPTIONS:
17672 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
17673 break;
17674 case DT_MIPS_CONFLICT:
17675 conflicts_offset
17676 = offset_from_vma (filedata, entry->d_un.d_val,
17677 conflictsno * sizeof (Elf32_External_Conflict));
17678 break;
17679 case DT_MIPS_CONFLICTNO:
17680 conflictsno = entry->d_un.d_val;
17681 break;
17682 case DT_PLTGOT:
17683 pltgot = entry->d_un.d_ptr;
17684 break;
17685 case DT_MIPS_LOCAL_GOTNO:
17686 local_gotno = entry->d_un.d_val;
17687 break;
17688 case DT_MIPS_GOTSYM:
17689 gotsym = entry->d_un.d_val;
17690 break;
17691 case DT_MIPS_SYMTABNO:
17692 symtabno = entry->d_un.d_val;
17693 break;
17694 case DT_MIPS_PLTGOT:
17695 mips_pltgot = entry->d_un.d_ptr;
17696 break;
17697 case DT_PLTREL:
17698 pltrel = entry->d_un.d_val;
17699 break;
17700 case DT_PLTRELSZ:
17701 pltrelsz = entry->d_un.d_val;
17702 break;
17703 case DT_JMPREL:
17704 jmprel = entry->d_un.d_ptr;
17705 break;
17706 default:
17707 break;
17708 }
17709
17710 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
17711 {
17712 Elf32_External_Lib * elib;
17713 size_t cnt;
17714
17715 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
17716 sizeof (Elf32_External_Lib),
17717 liblistno,
17718 _("liblist section data"));
17719 if (elib)
17720 {
17721 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
17722 "\nSection '.liblist' contains %lu entries:\n",
17723 (unsigned long) liblistno),
17724 (unsigned long) liblistno);
17725 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
17726 stdout);
17727
17728 for (cnt = 0; cnt < liblistno; ++cnt)
17729 {
17730 Elf32_Lib liblist;
17731 time_t atime;
17732 char timebuf[128];
17733 struct tm * tmp;
17734
17735 liblist.l_name = BYTE_GET (elib[cnt].l_name);
17736 atime = BYTE_GET (elib[cnt].l_time_stamp);
17737 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
17738 liblist.l_version = BYTE_GET (elib[cnt].l_version);
17739 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
17740
17741 tmp = gmtime (&atime);
17742 snprintf (timebuf, sizeof (timebuf),
17743 "%04u-%02u-%02uT%02u:%02u:%02u",
17744 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
17745 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
17746
17747 printf ("%3lu: ", (unsigned long) cnt);
17748 if (VALID_DYNAMIC_NAME (filedata, liblist.l_name))
17749 print_symbol (20, GET_DYNAMIC_NAME (filedata, liblist.l_name));
17750 else
17751 printf (_("<corrupt: %9ld>"), liblist.l_name);
17752 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
17753 liblist.l_version);
17754
17755 if (liblist.l_flags == 0)
17756 puts (_(" NONE"));
17757 else
17758 {
17759 static const struct
17760 {
17761 const char * name;
17762 int bit;
17763 }
17764 l_flags_vals[] =
17765 {
17766 { " EXACT_MATCH", LL_EXACT_MATCH },
17767 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
17768 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
17769 { " EXPORTS", LL_EXPORTS },
17770 { " DELAY_LOAD", LL_DELAY_LOAD },
17771 { " DELTA", LL_DELTA }
17772 };
17773 int flags = liblist.l_flags;
17774 size_t fcnt;
17775
17776 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
17777 if ((flags & l_flags_vals[fcnt].bit) != 0)
17778 {
17779 fputs (l_flags_vals[fcnt].name, stdout);
17780 flags ^= l_flags_vals[fcnt].bit;
17781 }
17782 if (flags != 0)
17783 printf (" %#x", (unsigned int) flags);
17784
17785 puts ("");
17786 }
17787 }
17788
17789 free (elib);
17790 }
17791 else
17792 res = false;
17793 }
17794
17795 if (options_offset != 0)
17796 {
17797 Elf_External_Options * eopt;
17798 size_t offset;
17799 int cnt;
17800 sect = filedata->section_headers;
17801
17802 /* Find the section header so that we get the size. */
17803 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
17804 /* PR 17533 file: 012-277276-0.004. */
17805 if (sect == NULL)
17806 {
17807 error (_("No MIPS_OPTIONS header found\n"));
17808 return false;
17809 }
17810 /* PR 24243 */
17811 if (sect->sh_size < sizeof (* eopt))
17812 {
17813 error (_("The MIPS options section is too small.\n"));
17814 return false;
17815 }
17816
17817 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
17818 sect->sh_size, _("options"));
17819 if (eopt)
17820 {
17821 Elf_Internal_Options option;
17822
17823 offset = cnt = 0;
17824 while (offset <= sect->sh_size - sizeof (* eopt))
17825 {
17826 Elf_External_Options * eoption;
17827 unsigned int optsize;
17828
17829 eoption = (Elf_External_Options *) ((char *) eopt + offset);
17830
17831 optsize = BYTE_GET (eoption->size);
17832
17833 /* PR 17531: file: ffa0fa3b. */
17834 if (optsize < sizeof (* eopt)
17835 || optsize > sect->sh_size - offset)
17836 {
17837 error (_("Invalid size (%u) for MIPS option\n"),
17838 optsize);
17839 free (eopt);
17840 return false;
17841 }
17842 offset += optsize;
17843 ++cnt;
17844 }
17845
17846 printf (ngettext ("\nSection '%s' contains %d entry:\n",
17847 "\nSection '%s' contains %d entries:\n",
17848 cnt),
17849 printable_section_name (filedata, sect), cnt);
17850
17851 offset = 0;
17852 while (cnt-- > 0)
17853 {
17854 size_t len;
17855 Elf_External_Options * eoption;
17856
17857 eoption = (Elf_External_Options *) ((char *) eopt + offset);
17858
17859 option.kind = BYTE_GET (eoption->kind);
17860 option.size = BYTE_GET (eoption->size);
17861 option.section = BYTE_GET (eoption->section);
17862 option.info = BYTE_GET (eoption->info);
17863
17864 switch (option.kind)
17865 {
17866 case ODK_NULL:
17867 /* This shouldn't happen. */
17868 printf (" NULL %" PRId16 " %" PRIx32,
17869 option.section, option.info);
17870 break;
17871
17872 case ODK_REGINFO:
17873 printf (" REGINFO ");
17874 if (filedata->file_header.e_machine == EM_MIPS)
17875 {
17876 Elf32_External_RegInfo * ereg;
17877 Elf32_RegInfo reginfo;
17878
17879 /* 32bit form. */
17880 if (option.size < (sizeof (Elf_External_Options)
17881 + sizeof (Elf32_External_RegInfo)))
17882 {
17883 printf (_("<corrupt>\n"));
17884 error (_("Truncated MIPS REGINFO option\n"));
17885 cnt = 0;
17886 break;
17887 }
17888
17889 ereg = (Elf32_External_RegInfo *) (eoption + 1);
17890
17891 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
17892 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
17893 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
17894 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
17895 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
17896 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
17897
17898 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
17899 reginfo.ri_gprmask, reginfo.ri_gp_value);
17900 printf (" "
17901 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
17902 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
17903 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
17904 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
17905 }
17906 else
17907 {
17908 /* 64 bit form. */
17909 Elf64_External_RegInfo * ereg;
17910 Elf64_Internal_RegInfo reginfo;
17911
17912 if (option.size < (sizeof (Elf_External_Options)
17913 + sizeof (Elf64_External_RegInfo)))
17914 {
17915 printf (_("<corrupt>\n"));
17916 error (_("Truncated MIPS REGINFO option\n"));
17917 cnt = 0;
17918 break;
17919 }
17920
17921 ereg = (Elf64_External_RegInfo *) (eoption + 1);
17922 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
17923 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
17924 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
17925 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
17926 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
17927 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
17928
17929 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
17930 reginfo.ri_gprmask, reginfo.ri_gp_value);
17931 printf (" "
17932 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
17933 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
17934 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
17935 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
17936 }
17937 offset += option.size;
17938 continue;
17939
17940 case ODK_EXCEPTIONS:
17941 fputs (" EXCEPTIONS fpe_min(", stdout);
17942 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
17943 fputs (") fpe_max(", stdout);
17944 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
17945 fputs (")", stdout);
17946
17947 if (option.info & OEX_PAGE0)
17948 fputs (" PAGE0", stdout);
17949 if (option.info & OEX_SMM)
17950 fputs (" SMM", stdout);
17951 if (option.info & OEX_FPDBUG)
17952 fputs (" FPDBUG", stdout);
17953 if (option.info & OEX_DISMISS)
17954 fputs (" DISMISS", stdout);
17955 break;
17956
17957 case ODK_PAD:
17958 fputs (" PAD ", stdout);
17959 if (option.info & OPAD_PREFIX)
17960 fputs (" PREFIX", stdout);
17961 if (option.info & OPAD_POSTFIX)
17962 fputs (" POSTFIX", stdout);
17963 if (option.info & OPAD_SYMBOL)
17964 fputs (" SYMBOL", stdout);
17965 break;
17966
17967 case ODK_HWPATCH:
17968 fputs (" HWPATCH ", stdout);
17969 if (option.info & OHW_R4KEOP)
17970 fputs (" R4KEOP", stdout);
17971 if (option.info & OHW_R8KPFETCH)
17972 fputs (" R8KPFETCH", stdout);
17973 if (option.info & OHW_R5KEOP)
17974 fputs (" R5KEOP", stdout);
17975 if (option.info & OHW_R5KCVTL)
17976 fputs (" R5KCVTL", stdout);
17977 break;
17978
17979 case ODK_FILL:
17980 fputs (" FILL ", stdout);
17981 /* XXX Print content of info word? */
17982 break;
17983
17984 case ODK_TAGS:
17985 fputs (" TAGS ", stdout);
17986 /* XXX Print content of info word? */
17987 break;
17988
17989 case ODK_HWAND:
17990 fputs (" HWAND ", stdout);
17991 if (option.info & OHWA0_R4KEOP_CHECKED)
17992 fputs (" R4KEOP_CHECKED", stdout);
17993 if (option.info & OHWA0_R4KEOP_CLEAN)
17994 fputs (" R4KEOP_CLEAN", stdout);
17995 break;
17996
17997 case ODK_HWOR:
17998 fputs (" HWOR ", stdout);
17999 if (option.info & OHWA0_R4KEOP_CHECKED)
18000 fputs (" R4KEOP_CHECKED", stdout);
18001 if (option.info & OHWA0_R4KEOP_CLEAN)
18002 fputs (" R4KEOP_CLEAN", stdout);
18003 break;
18004
18005 case ODK_GP_GROUP:
18006 printf (" GP_GROUP %#06x self-contained %#06x",
18007 option.info & OGP_GROUP,
18008 (option.info & OGP_SELF) >> 16);
18009 break;
18010
18011 case ODK_IDENT:
18012 printf (" IDENT %#06x self-contained %#06x",
18013 option.info & OGP_GROUP,
18014 (option.info & OGP_SELF) >> 16);
18015 break;
18016
18017 default:
18018 /* This shouldn't happen. */
18019 printf (" %3d ??? %" PRId16 " %" PRIx32,
18020 option.kind, option.section, option.info);
18021 break;
18022 }
18023
18024 len = sizeof (* eopt);
18025 while (len < option.size)
18026 {
18027 unsigned char datum = *((unsigned char *) eoption + len);
18028
18029 if (ISPRINT (datum))
18030 printf ("%c", datum);
18031 else
18032 printf ("\\%03o", datum);
18033 len ++;
18034 }
18035 fputs ("\n", stdout);
18036
18037 offset += option.size;
18038 }
18039 free (eopt);
18040 }
18041 else
18042 res = false;
18043 }
18044
18045 if (conflicts_offset != 0 && conflictsno != 0)
18046 {
18047 Elf32_Conflict * iconf;
18048 size_t cnt;
18049
18050 if (filedata->dynamic_symbols == NULL)
18051 {
18052 error (_("conflict list found without a dynamic symbol table\n"));
18053 return false;
18054 }
18055
18056 /* PR 21345 - print a slightly more helpful error message
18057 if we are sure that the cmalloc will fail. */
18058 if (conflictsno > filedata->file_size / sizeof (* iconf))
18059 {
18060 error (_("Overlarge number of conflicts detected: %lx\n"),
18061 (long) conflictsno);
18062 return false;
18063 }
18064
18065 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18066 if (iconf == NULL)
18067 {
18068 error (_("Out of memory allocating space for dynamic conflicts\n"));
18069 return false;
18070 }
18071
18072 if (is_32bit_elf)
18073 {
18074 Elf32_External_Conflict * econf32;
18075
18076 econf32 = (Elf32_External_Conflict *)
18077 get_data (NULL, filedata, conflicts_offset,
18078 sizeof (*econf32), conflictsno, _("conflict"));
18079 if (!econf32)
18080 {
18081 free (iconf);
18082 return false;
18083 }
18084
18085 for (cnt = 0; cnt < conflictsno; ++cnt)
18086 iconf[cnt] = BYTE_GET (econf32[cnt]);
18087
18088 free (econf32);
18089 }
18090 else
18091 {
18092 Elf64_External_Conflict * econf64;
18093
18094 econf64 = (Elf64_External_Conflict *)
18095 get_data (NULL, filedata, conflicts_offset,
18096 sizeof (*econf64), conflictsno, _("conflict"));
18097 if (!econf64)
18098 {
18099 free (iconf);
18100 return false;
18101 }
18102
18103 for (cnt = 0; cnt < conflictsno; ++cnt)
18104 iconf[cnt] = BYTE_GET (econf64[cnt]);
18105
18106 free (econf64);
18107 }
18108
18109 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18110 "\nSection '.conflict' contains %lu entries:\n",
18111 (unsigned long) conflictsno),
18112 (unsigned long) conflictsno);
18113 puts (_(" Num: Index Value Name"));
18114
18115 for (cnt = 0; cnt < conflictsno; ++cnt)
18116 {
18117 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18118
18119 if (iconf[cnt] >= filedata->num_dynamic_syms)
18120 printf (_("<corrupt symbol index>"));
18121 else
18122 {
18123 Elf_Internal_Sym * psym;
18124
18125 psym = & filedata->dynamic_symbols[iconf[cnt]];
18126 print_vma (psym->st_value, FULL_HEX);
18127 putchar (' ');
18128 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18129 print_symbol (25, GET_DYNAMIC_NAME (filedata, psym->st_name));
18130 else
18131 printf (_("<corrupt: %14ld>"), psym->st_name);
18132 }
18133 putchar ('\n');
18134 }
18135
18136 free (iconf);
18137 }
18138
18139 if (pltgot != 0 && local_gotno != 0)
18140 {
18141 bfd_vma ent, local_end, global_end;
18142 size_t i, offset;
18143 unsigned char * data;
18144 unsigned char * data_end;
18145 int addr_size;
18146
18147 ent = pltgot;
18148 addr_size = (is_32bit_elf ? 4 : 8);
18149 local_end = pltgot + local_gotno * addr_size;
18150
18151 /* PR binutils/17533 file: 012-111227-0.004 */
18152 if (symtabno < gotsym)
18153 {
18154 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18155 (unsigned long) gotsym, (unsigned long) symtabno);
18156 return false;
18157 }
18158
18159 global_end = local_end + (symtabno - gotsym) * addr_size;
18160 /* PR 17531: file: 54c91a34. */
18161 if (global_end < local_end)
18162 {
18163 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18164 return false;
18165 }
18166
18167 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18168 data = (unsigned char *) get_data (NULL, filedata, offset,
18169 global_end - pltgot, 1,
18170 _("Global Offset Table data"));
18171 /* PR 12855: Null data is handled gracefully throughout. */
18172 data_end = data + (global_end - pltgot);
18173
18174 printf (_("\nPrimary GOT:\n"));
18175 printf (_(" Canonical gp value: "));
18176 print_vma (pltgot + 0x7ff0, LONG_HEX);
18177 printf ("\n\n");
18178
18179 printf (_(" Reserved entries:\n"));
18180 printf (_(" %*s %10s %*s Purpose\n"),
18181 addr_size * 2, _("Address"), _("Access"),
18182 addr_size * 2, _("Initial"));
18183 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18184 printf (_(" Lazy resolver\n"));
18185 if (ent == (bfd_vma) -1)
18186 goto got_print_fail;
18187
18188 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18189 This entry will be used by some runtime loaders, to store the
18190 module pointer. Otherwise this is an ordinary local entry.
18191 PR 21344: Check for the entry being fully available before
18192 fetching it. */
18193 if (data
18194 && data + ent - pltgot + addr_size <= data_end
18195 && (byte_get (data + ent - pltgot, addr_size)
18196 >> (addr_size * 8 - 1)) != 0)
18197 {
18198 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18199 printf (_(" Module pointer (GNU extension)\n"));
18200 if (ent == (bfd_vma) -1)
18201 goto got_print_fail;
18202 }
18203 printf ("\n");
18204
18205 if (data != NULL && ent < local_end)
18206 {
18207 printf (_(" Local entries:\n"));
18208 printf (" %*s %10s %*s\n",
18209 addr_size * 2, _("Address"), _("Access"),
18210 addr_size * 2, _("Initial"));
18211 while (ent < local_end)
18212 {
18213 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18214 printf ("\n");
18215 if (ent == (bfd_vma) -1)
18216 goto got_print_fail;
18217 }
18218 printf ("\n");
18219 }
18220
18221 if (data != NULL && gotsym < symtabno)
18222 {
18223 int sym_width;
18224
18225 printf (_(" Global entries:\n"));
18226 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
18227 addr_size * 2, _("Address"),
18228 _("Access"),
18229 addr_size * 2, _("Initial"),
18230 addr_size * 2, _("Sym.Val."),
18231 _("Type"),
18232 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18233 _("Ndx"), _("Name"));
18234
18235 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
18236
18237 for (i = gotsym; i < symtabno; i++)
18238 {
18239 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18240 printf (" ");
18241
18242 if (filedata->dynamic_symbols == NULL)
18243 printf (_("<no dynamic symbols>"));
18244 else if (i < filedata->num_dynamic_syms)
18245 {
18246 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
18247
18248 print_vma (psym->st_value, LONG_HEX);
18249 printf (" %-7s %3s ",
18250 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18251 get_symbol_index_type (filedata, psym->st_shndx));
18252
18253 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18254 print_symbol (sym_width,
18255 GET_DYNAMIC_NAME (filedata, psym->st_name));
18256 else
18257 printf (_("<corrupt: %14ld>"), psym->st_name);
18258 }
18259 else
18260 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18261 (unsigned long) i);
18262
18263 printf ("\n");
18264 if (ent == (bfd_vma) -1)
18265 break;
18266 }
18267 printf ("\n");
18268 }
18269
18270 got_print_fail:
18271 free (data);
18272 }
18273
18274 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18275 {
18276 bfd_vma ent, end;
18277 size_t offset, rel_offset;
18278 unsigned long count, i;
18279 unsigned char * data;
18280 int addr_size, sym_width;
18281 Elf_Internal_Rela * rels;
18282
18283 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
18284 if (pltrel == DT_RELA)
18285 {
18286 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18287 return false;
18288 }
18289 else
18290 {
18291 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18292 return false;
18293 }
18294
18295 ent = mips_pltgot;
18296 addr_size = (is_32bit_elf ? 4 : 8);
18297 end = mips_pltgot + (2 + count) * addr_size;
18298
18299 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18300 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
18301 1, _("Procedure Linkage Table data"));
18302 if (data == NULL)
18303 {
18304 free (rels);
18305 return false;
18306 }
18307
18308 printf ("\nPLT GOT:\n\n");
18309 printf (_(" Reserved entries:\n"));
18310 printf (_(" %*s %*s Purpose\n"),
18311 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
18312 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18313 printf (_(" PLT lazy resolver\n"));
18314 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18315 printf (_(" Module pointer\n"));
18316 printf ("\n");
18317
18318 printf (_(" Entries:\n"));
18319 printf (" %*s %*s %*s %-7s %3s %s\n",
18320 addr_size * 2, _("Address"),
18321 addr_size * 2, _("Initial"),
18322 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
18323 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18324 for (i = 0; i < count; i++)
18325 {
18326 unsigned long idx = get_reloc_symindex (rels[i].r_info);
18327
18328 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18329 printf (" ");
18330
18331 if (idx >= filedata->num_dynamic_syms)
18332 printf (_("<corrupt symbol index: %lu>"), idx);
18333 else
18334 {
18335 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
18336
18337 print_vma (psym->st_value, LONG_HEX);
18338 printf (" %-7s %3s ",
18339 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18340 get_symbol_index_type (filedata, psym->st_shndx));
18341 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18342 print_symbol (sym_width,
18343 GET_DYNAMIC_NAME (filedata, psym->st_name));
18344 else
18345 printf (_("<corrupt: %14ld>"), psym->st_name);
18346 }
18347 printf ("\n");
18348 }
18349 printf ("\n");
18350
18351 free (data);
18352 free (rels);
18353 }
18354
18355 return res;
18356 }
18357
18358 static bool
18359 process_nds32_specific (Filedata * filedata)
18360 {
18361 Elf_Internal_Shdr *sect = NULL;
18362
18363 sect = find_section (filedata, ".nds32_e_flags");
18364 if (sect != NULL && sect->sh_size >= 4)
18365 {
18366 unsigned char *buf;
18367 unsigned int flag;
18368
18369 printf ("\nNDS32 elf flags section:\n");
18370 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18371 _("NDS32 elf flags section"));
18372
18373 if (buf == NULL)
18374 return false;
18375
18376 flag = byte_get (buf, 4);
18377 free (buf);
18378 switch (flag & 0x3)
18379 {
18380 case 0:
18381 printf ("(VEC_SIZE):\tNo entry.\n");
18382 break;
18383 case 1:
18384 printf ("(VEC_SIZE):\t4 bytes\n");
18385 break;
18386 case 2:
18387 printf ("(VEC_SIZE):\t16 bytes\n");
18388 break;
18389 case 3:
18390 printf ("(VEC_SIZE):\treserved\n");
18391 break;
18392 }
18393 }
18394
18395 return true;
18396 }
18397
18398 static bool
18399 process_gnu_liblist (Filedata * filedata)
18400 {
18401 Elf_Internal_Shdr * section;
18402 Elf_Internal_Shdr * string_sec;
18403 Elf32_External_Lib * elib;
18404 char * strtab;
18405 size_t strtab_size;
18406 size_t cnt;
18407 unsigned long num_liblist;
18408 unsigned i;
18409 bool res = true;
18410
18411 if (! do_arch)
18412 return true;
18413
18414 for (i = 0, section = filedata->section_headers;
18415 i < filedata->file_header.e_shnum;
18416 i++, section++)
18417 {
18418 switch (section->sh_type)
18419 {
18420 case SHT_GNU_LIBLIST:
18421 if (section->sh_link >= filedata->file_header.e_shnum)
18422 break;
18423
18424 elib = (Elf32_External_Lib *)
18425 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
18426 _("liblist section data"));
18427
18428 if (elib == NULL)
18429 {
18430 res = false;
18431 break;
18432 }
18433
18434 string_sec = filedata->section_headers + section->sh_link;
18435 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
18436 string_sec->sh_size,
18437 _("liblist string table"));
18438 if (strtab == NULL
18439 || section->sh_entsize != sizeof (Elf32_External_Lib))
18440 {
18441 free (elib);
18442 free (strtab);
18443 res = false;
18444 break;
18445 }
18446 strtab_size = string_sec->sh_size;
18447
18448 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
18449 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
18450 "\nLibrary list section '%s' contains %lu entries:\n",
18451 num_liblist),
18452 printable_section_name (filedata, section),
18453 num_liblist);
18454
18455 puts (_(" Library Time Stamp Checksum Version Flags"));
18456
18457 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
18458 ++cnt)
18459 {
18460 Elf32_Lib liblist;
18461 time_t atime;
18462 char timebuf[128];
18463 struct tm * tmp;
18464
18465 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18466 atime = BYTE_GET (elib[cnt].l_time_stamp);
18467 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18468 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18469 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18470
18471 tmp = gmtime (&atime);
18472 snprintf (timebuf, sizeof (timebuf),
18473 "%04u-%02u-%02uT%02u:%02u:%02u",
18474 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18475 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18476
18477 printf ("%3lu: ", (unsigned long) cnt);
18478 if (do_wide)
18479 printf ("%-20s", liblist.l_name < strtab_size
18480 ? strtab + liblist.l_name : _("<corrupt>"));
18481 else
18482 printf ("%-20.20s", liblist.l_name < strtab_size
18483 ? strtab + liblist.l_name : _("<corrupt>"));
18484 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
18485 liblist.l_version, liblist.l_flags);
18486 }
18487
18488 free (elib);
18489 free (strtab);
18490 }
18491 }
18492
18493 return res;
18494 }
18495
18496 static const char *
18497 get_note_type (Filedata * filedata, unsigned e_type)
18498 {
18499 static char buff[64];
18500
18501 if (filedata->file_header.e_type == ET_CORE)
18502 switch (e_type)
18503 {
18504 case NT_AUXV:
18505 return _("NT_AUXV (auxiliary vector)");
18506 case NT_PRSTATUS:
18507 return _("NT_PRSTATUS (prstatus structure)");
18508 case NT_FPREGSET:
18509 return _("NT_FPREGSET (floating point registers)");
18510 case NT_PRPSINFO:
18511 return _("NT_PRPSINFO (prpsinfo structure)");
18512 case NT_TASKSTRUCT:
18513 return _("NT_TASKSTRUCT (task structure)");
18514 case NT_GDB_TDESC:
18515 return _("NT_GDB_TDESC (GDB XML target description)");
18516 case NT_PRXFPREG:
18517 return _("NT_PRXFPREG (user_xfpregs structure)");
18518 case NT_PPC_VMX:
18519 return _("NT_PPC_VMX (ppc Altivec registers)");
18520 case NT_PPC_VSX:
18521 return _("NT_PPC_VSX (ppc VSX registers)");
18522 case NT_PPC_TAR:
18523 return _("NT_PPC_TAR (ppc TAR register)");
18524 case NT_PPC_PPR:
18525 return _("NT_PPC_PPR (ppc PPR register)");
18526 case NT_PPC_DSCR:
18527 return _("NT_PPC_DSCR (ppc DSCR register)");
18528 case NT_PPC_EBB:
18529 return _("NT_PPC_EBB (ppc EBB registers)");
18530 case NT_PPC_PMU:
18531 return _("NT_PPC_PMU (ppc PMU registers)");
18532 case NT_PPC_TM_CGPR:
18533 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
18534 case NT_PPC_TM_CFPR:
18535 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
18536 case NT_PPC_TM_CVMX:
18537 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
18538 case NT_PPC_TM_CVSX:
18539 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
18540 case NT_PPC_TM_SPR:
18541 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
18542 case NT_PPC_TM_CTAR:
18543 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
18544 case NT_PPC_TM_CPPR:
18545 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
18546 case NT_PPC_TM_CDSCR:
18547 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
18548 case NT_386_TLS:
18549 return _("NT_386_TLS (x86 TLS information)");
18550 case NT_386_IOPERM:
18551 return _("NT_386_IOPERM (x86 I/O permissions)");
18552 case NT_X86_XSTATE:
18553 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
18554 case NT_X86_CET:
18555 return _("NT_X86_CET (x86 CET state)");
18556 case NT_S390_HIGH_GPRS:
18557 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
18558 case NT_S390_TIMER:
18559 return _("NT_S390_TIMER (s390 timer register)");
18560 case NT_S390_TODCMP:
18561 return _("NT_S390_TODCMP (s390 TOD comparator register)");
18562 case NT_S390_TODPREG:
18563 return _("NT_S390_TODPREG (s390 TOD programmable register)");
18564 case NT_S390_CTRS:
18565 return _("NT_S390_CTRS (s390 control registers)");
18566 case NT_S390_PREFIX:
18567 return _("NT_S390_PREFIX (s390 prefix register)");
18568 case NT_S390_LAST_BREAK:
18569 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
18570 case NT_S390_SYSTEM_CALL:
18571 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
18572 case NT_S390_TDB:
18573 return _("NT_S390_TDB (s390 transaction diagnostic block)");
18574 case NT_S390_VXRS_LOW:
18575 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
18576 case NT_S390_VXRS_HIGH:
18577 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
18578 case NT_S390_GS_CB:
18579 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
18580 case NT_S390_GS_BC:
18581 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
18582 case NT_ARM_VFP:
18583 return _("NT_ARM_VFP (arm VFP registers)");
18584 case NT_ARM_TLS:
18585 return _("NT_ARM_TLS (AArch TLS registers)");
18586 case NT_ARM_HW_BREAK:
18587 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
18588 case NT_ARM_HW_WATCH:
18589 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
18590 case NT_ARC_V2:
18591 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
18592 case NT_RISCV_CSR:
18593 return _("NT_RISCV_CSR (RISC-V control and status registers)");
18594 case NT_PSTATUS:
18595 return _("NT_PSTATUS (pstatus structure)");
18596 case NT_FPREGS:
18597 return _("NT_FPREGS (floating point registers)");
18598 case NT_PSINFO:
18599 return _("NT_PSINFO (psinfo structure)");
18600 case NT_LWPSTATUS:
18601 return _("NT_LWPSTATUS (lwpstatus_t structure)");
18602 case NT_LWPSINFO:
18603 return _("NT_LWPSINFO (lwpsinfo_t structure)");
18604 case NT_WIN32PSTATUS:
18605 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
18606 case NT_SIGINFO:
18607 return _("NT_SIGINFO (siginfo_t data)");
18608 case NT_FILE:
18609 return _("NT_FILE (mapped files)");
18610 default:
18611 break;
18612 }
18613 else
18614 switch (e_type)
18615 {
18616 case NT_VERSION:
18617 return _("NT_VERSION (version)");
18618 case NT_ARCH:
18619 return _("NT_ARCH (architecture)");
18620 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18621 return _("OPEN");
18622 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18623 return _("func");
18624 default:
18625 break;
18626 }
18627
18628 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18629 return buff;
18630 }
18631
18632 static bool
18633 print_core_note (Elf_Internal_Note *pnote)
18634 {
18635 unsigned int addr_size = is_32bit_elf ? 4 : 8;
18636 bfd_vma count, page_size;
18637 unsigned char *descdata, *filenames, *descend;
18638
18639 if (pnote->type != NT_FILE)
18640 {
18641 if (do_wide)
18642 printf ("\n");
18643 return true;
18644 }
18645
18646 #ifndef BFD64
18647 if (!is_32bit_elf)
18648 {
18649 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
18650 /* Still "successful". */
18651 return true;
18652 }
18653 #endif
18654
18655 if (pnote->descsz < 2 * addr_size)
18656 {
18657 error (_(" Malformed note - too short for header\n"));
18658 return false;
18659 }
18660
18661 descdata = (unsigned char *) pnote->descdata;
18662 descend = descdata + pnote->descsz;
18663
18664 if (descdata[pnote->descsz - 1] != '\0')
18665 {
18666 error (_(" Malformed note - does not end with \\0\n"));
18667 return false;
18668 }
18669
18670 count = byte_get (descdata, addr_size);
18671 descdata += addr_size;
18672
18673 page_size = byte_get (descdata, addr_size);
18674 descdata += addr_size;
18675
18676 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
18677 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
18678 {
18679 error (_(" Malformed note - too short for supplied file count\n"));
18680 return false;
18681 }
18682
18683 printf (_(" Page size: "));
18684 print_vma (page_size, DEC);
18685 printf ("\n");
18686
18687 printf (_(" %*s%*s%*s\n"),
18688 (int) (2 + 2 * addr_size), _("Start"),
18689 (int) (4 + 2 * addr_size), _("End"),
18690 (int) (4 + 2 * addr_size), _("Page Offset"));
18691 filenames = descdata + count * 3 * addr_size;
18692 while (count-- > 0)
18693 {
18694 bfd_vma start, end, file_ofs;
18695
18696 if (filenames == descend)
18697 {
18698 error (_(" Malformed note - filenames end too early\n"));
18699 return false;
18700 }
18701
18702 start = byte_get (descdata, addr_size);
18703 descdata += addr_size;
18704 end = byte_get (descdata, addr_size);
18705 descdata += addr_size;
18706 file_ofs = byte_get (descdata, addr_size);
18707 descdata += addr_size;
18708
18709 printf (" ");
18710 print_vma (start, FULL_HEX);
18711 printf (" ");
18712 print_vma (end, FULL_HEX);
18713 printf (" ");
18714 print_vma (file_ofs, FULL_HEX);
18715 printf ("\n %s\n", filenames);
18716
18717 filenames += 1 + strlen ((char *) filenames);
18718 }
18719
18720 return true;
18721 }
18722
18723 static const char *
18724 get_gnu_elf_note_type (unsigned e_type)
18725 {
18726 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
18727 switch (e_type)
18728 {
18729 case NT_GNU_ABI_TAG:
18730 return _("NT_GNU_ABI_TAG (ABI version tag)");
18731 case NT_GNU_HWCAP:
18732 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
18733 case NT_GNU_BUILD_ID:
18734 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
18735 case NT_GNU_GOLD_VERSION:
18736 return _("NT_GNU_GOLD_VERSION (gold version)");
18737 case NT_GNU_PROPERTY_TYPE_0:
18738 return _("NT_GNU_PROPERTY_TYPE_0");
18739 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18740 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
18741 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18742 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
18743 default:
18744 {
18745 static char buff[64];
18746
18747 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18748 return buff;
18749 }
18750 }
18751 }
18752
18753 static void
18754 decode_x86_compat_isa (unsigned int bitmask)
18755 {
18756 while (bitmask)
18757 {
18758 unsigned int bit = bitmask & (- bitmask);
18759
18760 bitmask &= ~ bit;
18761 switch (bit)
18762 {
18763 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
18764 printf ("i486");
18765 break;
18766 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
18767 printf ("586");
18768 break;
18769 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
18770 printf ("686");
18771 break;
18772 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
18773 printf ("SSE");
18774 break;
18775 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
18776 printf ("SSE2");
18777 break;
18778 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
18779 printf ("SSE3");
18780 break;
18781 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
18782 printf ("SSSE3");
18783 break;
18784 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
18785 printf ("SSE4_1");
18786 break;
18787 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
18788 printf ("SSE4_2");
18789 break;
18790 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
18791 printf ("AVX");
18792 break;
18793 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
18794 printf ("AVX2");
18795 break;
18796 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
18797 printf ("AVX512F");
18798 break;
18799 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
18800 printf ("AVX512CD");
18801 break;
18802 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
18803 printf ("AVX512ER");
18804 break;
18805 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
18806 printf ("AVX512PF");
18807 break;
18808 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
18809 printf ("AVX512VL");
18810 break;
18811 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
18812 printf ("AVX512DQ");
18813 break;
18814 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
18815 printf ("AVX512BW");
18816 break;
18817 default:
18818 printf (_("<unknown: %x>"), bit);
18819 break;
18820 }
18821 if (bitmask)
18822 printf (", ");
18823 }
18824 }
18825
18826 static void
18827 decode_x86_compat_2_isa (unsigned int bitmask)
18828 {
18829 if (!bitmask)
18830 {
18831 printf (_("<None>"));
18832 return;
18833 }
18834
18835 while (bitmask)
18836 {
18837 unsigned int bit = bitmask & (- bitmask);
18838
18839 bitmask &= ~ bit;
18840 switch (bit)
18841 {
18842 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
18843 printf ("CMOV");
18844 break;
18845 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
18846 printf ("SSE");
18847 break;
18848 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
18849 printf ("SSE2");
18850 break;
18851 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
18852 printf ("SSE3");
18853 break;
18854 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
18855 printf ("SSSE3");
18856 break;
18857 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
18858 printf ("SSE4_1");
18859 break;
18860 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
18861 printf ("SSE4_2");
18862 break;
18863 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
18864 printf ("AVX");
18865 break;
18866 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
18867 printf ("AVX2");
18868 break;
18869 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
18870 printf ("FMA");
18871 break;
18872 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
18873 printf ("AVX512F");
18874 break;
18875 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
18876 printf ("AVX512CD");
18877 break;
18878 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
18879 printf ("AVX512ER");
18880 break;
18881 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
18882 printf ("AVX512PF");
18883 break;
18884 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
18885 printf ("AVX512VL");
18886 break;
18887 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
18888 printf ("AVX512DQ");
18889 break;
18890 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
18891 printf ("AVX512BW");
18892 break;
18893 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
18894 printf ("AVX512_4FMAPS");
18895 break;
18896 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
18897 printf ("AVX512_4VNNIW");
18898 break;
18899 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
18900 printf ("AVX512_BITALG");
18901 break;
18902 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
18903 printf ("AVX512_IFMA");
18904 break;
18905 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
18906 printf ("AVX512_VBMI");
18907 break;
18908 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
18909 printf ("AVX512_VBMI2");
18910 break;
18911 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
18912 printf ("AVX512_VNNI");
18913 break;
18914 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
18915 printf ("AVX512_BF16");
18916 break;
18917 default:
18918 printf (_("<unknown: %x>"), bit);
18919 break;
18920 }
18921 if (bitmask)
18922 printf (", ");
18923 }
18924 }
18925
18926 static void
18927 decode_x86_isa (unsigned int bitmask)
18928 {
18929 while (bitmask)
18930 {
18931 unsigned int bit = bitmask & (- bitmask);
18932
18933 bitmask &= ~ bit;
18934 switch (bit)
18935 {
18936 case GNU_PROPERTY_X86_ISA_1_BASELINE:
18937 printf ("x86-64-baseline");
18938 break;
18939 case GNU_PROPERTY_X86_ISA_1_V2:
18940 printf ("x86-64-v2");
18941 break;
18942 case GNU_PROPERTY_X86_ISA_1_V3:
18943 printf ("x86-64-v3");
18944 break;
18945 case GNU_PROPERTY_X86_ISA_1_V4:
18946 printf ("x86-64-v4");
18947 break;
18948 default:
18949 printf (_("<unknown: %x>"), bit);
18950 break;
18951 }
18952 if (bitmask)
18953 printf (", ");
18954 }
18955 }
18956
18957 static void
18958 decode_x86_feature_1 (unsigned int bitmask)
18959 {
18960 if (!bitmask)
18961 {
18962 printf (_("<None>"));
18963 return;
18964 }
18965
18966 while (bitmask)
18967 {
18968 unsigned int bit = bitmask & (- bitmask);
18969
18970 bitmask &= ~ bit;
18971 switch (bit)
18972 {
18973 case GNU_PROPERTY_X86_FEATURE_1_IBT:
18974 printf ("IBT");
18975 break;
18976 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
18977 printf ("SHSTK");
18978 break;
18979 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
18980 printf ("LAM_U48");
18981 break;
18982 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
18983 printf ("LAM_U57");
18984 break;
18985 default:
18986 printf (_("<unknown: %x>"), bit);
18987 break;
18988 }
18989 if (bitmask)
18990 printf (", ");
18991 }
18992 }
18993
18994 static void
18995 decode_x86_feature_2 (unsigned int bitmask)
18996 {
18997 if (!bitmask)
18998 {
18999 printf (_("<None>"));
19000 return;
19001 }
19002
19003 while (bitmask)
19004 {
19005 unsigned int bit = bitmask & (- bitmask);
19006
19007 bitmask &= ~ bit;
19008 switch (bit)
19009 {
19010 case GNU_PROPERTY_X86_FEATURE_2_X86:
19011 printf ("x86");
19012 break;
19013 case GNU_PROPERTY_X86_FEATURE_2_X87:
19014 printf ("x87");
19015 break;
19016 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19017 printf ("MMX");
19018 break;
19019 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19020 printf ("XMM");
19021 break;
19022 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19023 printf ("YMM");
19024 break;
19025 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19026 printf ("ZMM");
19027 break;
19028 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19029 printf ("TMM");
19030 break;
19031 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19032 printf ("MASK");
19033 break;
19034 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19035 printf ("FXSR");
19036 break;
19037 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19038 printf ("XSAVE");
19039 break;
19040 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19041 printf ("XSAVEOPT");
19042 break;
19043 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19044 printf ("XSAVEC");
19045 break;
19046 default:
19047 printf (_("<unknown: %x>"), bit);
19048 break;
19049 }
19050 if (bitmask)
19051 printf (", ");
19052 }
19053 }
19054
19055 static void
19056 decode_aarch64_feature_1_and (unsigned int bitmask)
19057 {
19058 while (bitmask)
19059 {
19060 unsigned int bit = bitmask & (- bitmask);
19061
19062 bitmask &= ~ bit;
19063 switch (bit)
19064 {
19065 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19066 printf ("BTI");
19067 break;
19068
19069 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19070 printf ("PAC");
19071 break;
19072
19073 default:
19074 printf (_("<unknown: %x>"), bit);
19075 break;
19076 }
19077 if (bitmask)
19078 printf (", ");
19079 }
19080 }
19081
19082 static void
19083 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19084 {
19085 unsigned char * ptr = (unsigned char *) pnote->descdata;
19086 unsigned char * ptr_end = ptr + pnote->descsz;
19087 unsigned int size = is_32bit_elf ? 4 : 8;
19088
19089 printf (_(" Properties: "));
19090
19091 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19092 {
19093 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19094 return;
19095 }
19096
19097 while (ptr < ptr_end)
19098 {
19099 unsigned int j;
19100 unsigned int type;
19101 unsigned int datasz;
19102
19103 if ((size_t) (ptr_end - ptr) < 8)
19104 {
19105 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19106 break;
19107 }
19108
19109 type = byte_get (ptr, 4);
19110 datasz = byte_get (ptr + 4, 4);
19111
19112 ptr += 8;
19113
19114 if (datasz > (size_t) (ptr_end - ptr))
19115 {
19116 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19117 type, datasz);
19118 break;
19119 }
19120
19121 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19122 {
19123 if (filedata->file_header.e_machine == EM_X86_64
19124 || filedata->file_header.e_machine == EM_IAMCU
19125 || filedata->file_header.e_machine == EM_386)
19126 {
19127 unsigned int bitmask;
19128
19129 if (datasz == 4)
19130 bitmask = byte_get (ptr, 4);
19131 else
19132 bitmask = 0;
19133
19134 switch (type)
19135 {
19136 case GNU_PROPERTY_X86_ISA_1_USED:
19137 if (datasz != 4)
19138 printf (_("x86 ISA used: <corrupt length: %#x> "),
19139 datasz);
19140 else
19141 {
19142 printf ("x86 ISA used: ");
19143 decode_x86_isa (bitmask);
19144 }
19145 goto next;
19146
19147 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19148 if (datasz != 4)
19149 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19150 datasz);
19151 else
19152 {
19153 printf ("x86 ISA needed: ");
19154 decode_x86_isa (bitmask);
19155 }
19156 goto next;
19157
19158 case GNU_PROPERTY_X86_FEATURE_1_AND:
19159 if (datasz != 4)
19160 printf (_("x86 feature: <corrupt length: %#x> "),
19161 datasz);
19162 else
19163 {
19164 printf ("x86 feature: ");
19165 decode_x86_feature_1 (bitmask);
19166 }
19167 goto next;
19168
19169 case GNU_PROPERTY_X86_FEATURE_2_USED:
19170 if (datasz != 4)
19171 printf (_("x86 feature used: <corrupt length: %#x> "),
19172 datasz);
19173 else
19174 {
19175 printf ("x86 feature used: ");
19176 decode_x86_feature_2 (bitmask);
19177 }
19178 goto next;
19179
19180 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19181 if (datasz != 4)
19182 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19183 else
19184 {
19185 printf ("x86 feature needed: ");
19186 decode_x86_feature_2 (bitmask);
19187 }
19188 goto next;
19189
19190 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19191 if (datasz != 4)
19192 printf (_("x86 ISA used: <corrupt length: %#x> "),
19193 datasz);
19194 else
19195 {
19196 printf ("x86 ISA used: ");
19197 decode_x86_compat_isa (bitmask);
19198 }
19199 goto next;
19200
19201 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19202 if (datasz != 4)
19203 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19204 datasz);
19205 else
19206 {
19207 printf ("x86 ISA needed: ");
19208 decode_x86_compat_isa (bitmask);
19209 }
19210 goto next;
19211
19212 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19213 if (datasz != 4)
19214 printf (_("x86 ISA used: <corrupt length: %#x> "),
19215 datasz);
19216 else
19217 {
19218 printf ("x86 ISA used: ");
19219 decode_x86_compat_2_isa (bitmask);
19220 }
19221 goto next;
19222
19223 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19224 if (datasz != 4)
19225 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19226 datasz);
19227 else
19228 {
19229 printf ("x86 ISA needed: ");
19230 decode_x86_compat_2_isa (bitmask);
19231 }
19232 goto next;
19233
19234 default:
19235 break;
19236 }
19237 }
19238 else if (filedata->file_header.e_machine == EM_AARCH64)
19239 {
19240 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19241 {
19242 printf ("AArch64 feature: ");
19243 if (datasz != 4)
19244 printf (_("<corrupt length: %#x> "), datasz);
19245 else
19246 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19247 goto next;
19248 }
19249 }
19250 }
19251 else
19252 {
19253 switch (type)
19254 {
19255 case GNU_PROPERTY_STACK_SIZE:
19256 printf (_("stack size: "));
19257 if (datasz != size)
19258 printf (_("<corrupt length: %#x> "), datasz);
19259 else
19260 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19261 goto next;
19262
19263 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19264 printf ("no copy on protected ");
19265 if (datasz)
19266 printf (_("<corrupt length: %#x> "), datasz);
19267 goto next;
19268
19269 default:
19270 break;
19271 }
19272 }
19273
19274 if (type < GNU_PROPERTY_LOPROC)
19275 printf (_("<unknown type %#x data: "), type);
19276 else if (type < GNU_PROPERTY_LOUSER)
19277 printf (_("<processor-specific type %#x data: "), type);
19278 else
19279 printf (_("<application-specific type %#x data: "), type);
19280 for (j = 0; j < datasz; ++j)
19281 printf ("%02x ", ptr[j] & 0xff);
19282 printf (">");
19283
19284 next:
19285 ptr += ((datasz + (size - 1)) & ~ (size - 1));
19286 if (ptr == ptr_end)
19287 break;
19288
19289 if (do_wide)
19290 printf (", ");
19291 else
19292 printf ("\n\t");
19293 }
19294
19295 printf ("\n");
19296 }
19297
19298 static bool
19299 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
19300 {
19301 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
19302 switch (pnote->type)
19303 {
19304 case NT_GNU_BUILD_ID:
19305 {
19306 unsigned long i;
19307
19308 printf (_(" Build ID: "));
19309 for (i = 0; i < pnote->descsz; ++i)
19310 printf ("%02x", pnote->descdata[i] & 0xff);
19311 printf ("\n");
19312 }
19313 break;
19314
19315 case NT_GNU_ABI_TAG:
19316 {
19317 unsigned long os, major, minor, subminor;
19318 const char *osname;
19319
19320 /* PR 17531: file: 030-599401-0.004. */
19321 if (pnote->descsz < 16)
19322 {
19323 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19324 break;
19325 }
19326
19327 os = byte_get ((unsigned char *) pnote->descdata, 4);
19328 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19329 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
19330 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
19331
19332 switch (os)
19333 {
19334 case GNU_ABI_TAG_LINUX:
19335 osname = "Linux";
19336 break;
19337 case GNU_ABI_TAG_HURD:
19338 osname = "Hurd";
19339 break;
19340 case GNU_ABI_TAG_SOLARIS:
19341 osname = "Solaris";
19342 break;
19343 case GNU_ABI_TAG_FREEBSD:
19344 osname = "FreeBSD";
19345 break;
19346 case GNU_ABI_TAG_NETBSD:
19347 osname = "NetBSD";
19348 break;
19349 case GNU_ABI_TAG_SYLLABLE:
19350 osname = "Syllable";
19351 break;
19352 case GNU_ABI_TAG_NACL:
19353 osname = "NaCl";
19354 break;
19355 default:
19356 osname = "Unknown";
19357 break;
19358 }
19359
19360 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
19361 major, minor, subminor);
19362 }
19363 break;
19364
19365 case NT_GNU_GOLD_VERSION:
19366 {
19367 unsigned long i;
19368
19369 printf (_(" Version: "));
19370 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
19371 printf ("%c", pnote->descdata[i]);
19372 printf ("\n");
19373 }
19374 break;
19375
19376 case NT_GNU_HWCAP:
19377 {
19378 unsigned long num_entries, mask;
19379
19380 /* Hardware capabilities information. Word 0 is the number of entries.
19381 Word 1 is a bitmask of enabled entries. The rest of the descriptor
19382 is a series of entries, where each entry is a single byte followed
19383 by a nul terminated string. The byte gives the bit number to test
19384 if enabled in the bitmask. */
19385 printf (_(" Hardware Capabilities: "));
19386 if (pnote->descsz < 8)
19387 {
19388 error (_("<corrupt GNU_HWCAP>\n"));
19389 return false;
19390 }
19391 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
19392 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19393 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
19394 /* FIXME: Add code to display the entries... */
19395 }
19396 break;
19397
19398 case NT_GNU_PROPERTY_TYPE_0:
19399 print_gnu_property_note (filedata, pnote);
19400 break;
19401
19402 default:
19403 /* Handle unrecognised types. An error message should have already been
19404 created by get_gnu_elf_note_type(), so all that we need to do is to
19405 display the data. */
19406 {
19407 unsigned long i;
19408
19409 printf (_(" Description data: "));
19410 for (i = 0; i < pnote->descsz; ++i)
19411 printf ("%02x ", pnote->descdata[i] & 0xff);
19412 printf ("\n");
19413 }
19414 break;
19415 }
19416
19417 return true;
19418 }
19419
19420 static const char *
19421 get_v850_elf_note_type (enum v850_notes n_type)
19422 {
19423 static char buff[64];
19424
19425 switch (n_type)
19426 {
19427 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
19428 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
19429 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
19430 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
19431 case V850_NOTE_CACHE_INFO: return _("Use of cache");
19432 case V850_NOTE_MMU_INFO: return _("Use of MMU");
19433 default:
19434 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
19435 return buff;
19436 }
19437 }
19438
19439 static bool
19440 print_v850_note (Elf_Internal_Note * pnote)
19441 {
19442 unsigned int val;
19443
19444 if (pnote->descsz != 4)
19445 return false;
19446
19447 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
19448
19449 if (val == 0)
19450 {
19451 printf (_("not set\n"));
19452 return true;
19453 }
19454
19455 switch (pnote->type)
19456 {
19457 case V850_NOTE_ALIGNMENT:
19458 switch (val)
19459 {
19460 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
19461 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
19462 }
19463 break;
19464
19465 case V850_NOTE_DATA_SIZE:
19466 switch (val)
19467 {
19468 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
19469 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
19470 }
19471 break;
19472
19473 case V850_NOTE_FPU_INFO:
19474 switch (val)
19475 {
19476 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
19477 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
19478 }
19479 break;
19480
19481 case V850_NOTE_MMU_INFO:
19482 case V850_NOTE_CACHE_INFO:
19483 case V850_NOTE_SIMD_INFO:
19484 if (val == EF_RH850_SIMD)
19485 {
19486 printf (_("yes\n"));
19487 return true;
19488 }
19489 break;
19490
19491 default:
19492 /* An 'unknown note type' message will already have been displayed. */
19493 break;
19494 }
19495
19496 printf (_("unknown value: %x\n"), val);
19497 return false;
19498 }
19499
19500 static bool
19501 process_netbsd_elf_note (Elf_Internal_Note * pnote)
19502 {
19503 unsigned int version;
19504
19505 switch (pnote->type)
19506 {
19507 case NT_NETBSD_IDENT:
19508 if (pnote->descsz < 1)
19509 break;
19510 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19511 if ((version / 10000) % 100)
19512 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
19513 version, version / 100000000, (version / 1000000) % 100,
19514 (version / 10000) % 100 > 26 ? "Z" : "",
19515 'A' + (version / 10000) % 26);
19516 else
19517 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
19518 version, version / 100000000, (version / 1000000) % 100,
19519 (version / 100) % 100);
19520 return true;
19521
19522 case NT_NETBSD_MARCH:
19523 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
19524 pnote->descdata);
19525 return true;
19526
19527 #ifdef NT_NETBSD_PAX
19528 case NT_NETBSD_PAX:
19529 if (pnote->descsz < 1)
19530 break;
19531 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19532 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
19533 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
19534 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
19535 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
19536 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
19537 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
19538 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
19539 return true;
19540 #endif
19541 }
19542
19543 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
19544 pnote->descsz, pnote->type);
19545 return false;
19546 }
19547
19548 static const char *
19549 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19550 {
19551 switch (e_type)
19552 {
19553 case NT_FREEBSD_THRMISC:
19554 return _("NT_THRMISC (thrmisc structure)");
19555 case NT_FREEBSD_PROCSTAT_PROC:
19556 return _("NT_PROCSTAT_PROC (proc data)");
19557 case NT_FREEBSD_PROCSTAT_FILES:
19558 return _("NT_PROCSTAT_FILES (files data)");
19559 case NT_FREEBSD_PROCSTAT_VMMAP:
19560 return _("NT_PROCSTAT_VMMAP (vmmap data)");
19561 case NT_FREEBSD_PROCSTAT_GROUPS:
19562 return _("NT_PROCSTAT_GROUPS (groups data)");
19563 case NT_FREEBSD_PROCSTAT_UMASK:
19564 return _("NT_PROCSTAT_UMASK (umask data)");
19565 case NT_FREEBSD_PROCSTAT_RLIMIT:
19566 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
19567 case NT_FREEBSD_PROCSTAT_OSREL:
19568 return _("NT_PROCSTAT_OSREL (osreldate data)");
19569 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
19570 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
19571 case NT_FREEBSD_PROCSTAT_AUXV:
19572 return _("NT_PROCSTAT_AUXV (auxv data)");
19573 case NT_FREEBSD_PTLWPINFO:
19574 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
19575 }
19576 return get_note_type (filedata, e_type);
19577 }
19578
19579 static const char *
19580 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19581 {
19582 static char buff[64];
19583
19584 switch (e_type)
19585 {
19586 case NT_NETBSDCORE_PROCINFO:
19587 /* NetBSD core "procinfo" structure. */
19588 return _("NetBSD procinfo structure");
19589
19590 #ifdef NT_NETBSDCORE_AUXV
19591 case NT_NETBSDCORE_AUXV:
19592 return _("NetBSD ELF auxiliary vector data");
19593 #endif
19594
19595 #ifdef NT_NETBSDCORE_LWPSTATUS
19596 case NT_NETBSDCORE_LWPSTATUS:
19597 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
19598 #endif
19599
19600 default:
19601 /* As of Jan 2020 there are no other machine-independent notes
19602 defined for NetBSD core files. If the note type is less
19603 than the start of the machine-dependent note types, we don't
19604 understand it. */
19605
19606 if (e_type < NT_NETBSDCORE_FIRSTMACH)
19607 {
19608 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19609 return buff;
19610 }
19611 break;
19612 }
19613
19614 switch (filedata->file_header.e_machine)
19615 {
19616 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
19617 and PT_GETFPREGS == mach+2. */
19618
19619 case EM_OLD_ALPHA:
19620 case EM_ALPHA:
19621 case EM_SPARC:
19622 case EM_SPARC32PLUS:
19623 case EM_SPARCV9:
19624 switch (e_type)
19625 {
19626 case NT_NETBSDCORE_FIRSTMACH + 0:
19627 return _("PT_GETREGS (reg structure)");
19628 case NT_NETBSDCORE_FIRSTMACH + 2:
19629 return _("PT_GETFPREGS (fpreg structure)");
19630 default:
19631 break;
19632 }
19633 break;
19634
19635 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
19636 There's also old PT___GETREGS40 == mach + 1 for old reg
19637 structure which lacks GBR. */
19638 case EM_SH:
19639 switch (e_type)
19640 {
19641 case NT_NETBSDCORE_FIRSTMACH + 1:
19642 return _("PT___GETREGS40 (old reg structure)");
19643 case NT_NETBSDCORE_FIRSTMACH + 3:
19644 return _("PT_GETREGS (reg structure)");
19645 case NT_NETBSDCORE_FIRSTMACH + 5:
19646 return _("PT_GETFPREGS (fpreg structure)");
19647 default:
19648 break;
19649 }
19650 break;
19651
19652 /* On all other arch's, PT_GETREGS == mach+1 and
19653 PT_GETFPREGS == mach+3. */
19654 default:
19655 switch (e_type)
19656 {
19657 case NT_NETBSDCORE_FIRSTMACH + 1:
19658 return _("PT_GETREGS (reg structure)");
19659 case NT_NETBSDCORE_FIRSTMACH + 3:
19660 return _("PT_GETFPREGS (fpreg structure)");
19661 default:
19662 break;
19663 }
19664 }
19665
19666 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
19667 e_type - NT_NETBSDCORE_FIRSTMACH);
19668 return buff;
19669 }
19670
19671 static const char *
19672 get_stapsdt_note_type (unsigned e_type)
19673 {
19674 static char buff[64];
19675
19676 switch (e_type)
19677 {
19678 case NT_STAPSDT:
19679 return _("NT_STAPSDT (SystemTap probe descriptors)");
19680
19681 default:
19682 break;
19683 }
19684
19685 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19686 return buff;
19687 }
19688
19689 static bool
19690 print_stapsdt_note (Elf_Internal_Note *pnote)
19691 {
19692 size_t len, maxlen;
19693 unsigned long addr_size = is_32bit_elf ? 4 : 8;
19694 char *data = pnote->descdata;
19695 char *data_end = pnote->descdata + pnote->descsz;
19696 bfd_vma pc, base_addr, semaphore;
19697 char *provider, *probe, *arg_fmt;
19698
19699 if (pnote->descsz < (addr_size * 3))
19700 goto stapdt_note_too_small;
19701
19702 pc = byte_get ((unsigned char *) data, addr_size);
19703 data += addr_size;
19704
19705 base_addr = byte_get ((unsigned char *) data, addr_size);
19706 data += addr_size;
19707
19708 semaphore = byte_get ((unsigned char *) data, addr_size);
19709 data += addr_size;
19710
19711 if (data >= data_end)
19712 goto stapdt_note_too_small;
19713 maxlen = data_end - data;
19714 len = strnlen (data, maxlen);
19715 if (len < maxlen)
19716 {
19717 provider = data;
19718 data += len + 1;
19719 }
19720 else
19721 goto stapdt_note_too_small;
19722
19723 if (data >= data_end)
19724 goto stapdt_note_too_small;
19725 maxlen = data_end - data;
19726 len = strnlen (data, maxlen);
19727 if (len < maxlen)
19728 {
19729 probe = data;
19730 data += len + 1;
19731 }
19732 else
19733 goto stapdt_note_too_small;
19734
19735 if (data >= data_end)
19736 goto stapdt_note_too_small;
19737 maxlen = data_end - data;
19738 len = strnlen (data, maxlen);
19739 if (len < maxlen)
19740 {
19741 arg_fmt = data;
19742 data += len + 1;
19743 }
19744 else
19745 goto stapdt_note_too_small;
19746
19747 printf (_(" Provider: %s\n"), provider);
19748 printf (_(" Name: %s\n"), probe);
19749 printf (_(" Location: "));
19750 print_vma (pc, FULL_HEX);
19751 printf (_(", Base: "));
19752 print_vma (base_addr, FULL_HEX);
19753 printf (_(", Semaphore: "));
19754 print_vma (semaphore, FULL_HEX);
19755 printf ("\n");
19756 printf (_(" Arguments: %s\n"), arg_fmt);
19757
19758 return data == data_end;
19759
19760 stapdt_note_too_small:
19761 printf (_(" <corrupt - note is too small>\n"));
19762 error (_("corrupt stapdt note - the data size is too small\n"));
19763 return false;
19764 }
19765
19766 static const char *
19767 get_ia64_vms_note_type (unsigned e_type)
19768 {
19769 static char buff[64];
19770
19771 switch (e_type)
19772 {
19773 case NT_VMS_MHD:
19774 return _("NT_VMS_MHD (module header)");
19775 case NT_VMS_LNM:
19776 return _("NT_VMS_LNM (language name)");
19777 case NT_VMS_SRC:
19778 return _("NT_VMS_SRC (source files)");
19779 case NT_VMS_TITLE:
19780 return "NT_VMS_TITLE";
19781 case NT_VMS_EIDC:
19782 return _("NT_VMS_EIDC (consistency check)");
19783 case NT_VMS_FPMODE:
19784 return _("NT_VMS_FPMODE (FP mode)");
19785 case NT_VMS_LINKTIME:
19786 return "NT_VMS_LINKTIME";
19787 case NT_VMS_IMGNAM:
19788 return _("NT_VMS_IMGNAM (image name)");
19789 case NT_VMS_IMGID:
19790 return _("NT_VMS_IMGID (image id)");
19791 case NT_VMS_LINKID:
19792 return _("NT_VMS_LINKID (link id)");
19793 case NT_VMS_IMGBID:
19794 return _("NT_VMS_IMGBID (build id)");
19795 case NT_VMS_GSTNAM:
19796 return _("NT_VMS_GSTNAM (sym table name)");
19797 case NT_VMS_ORIG_DYN:
19798 return "NT_VMS_ORIG_DYN";
19799 case NT_VMS_PATCHTIME:
19800 return "NT_VMS_PATCHTIME";
19801 default:
19802 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19803 return buff;
19804 }
19805 }
19806
19807 static bool
19808 print_ia64_vms_note (Elf_Internal_Note * pnote)
19809 {
19810 int maxlen = pnote->descsz;
19811
19812 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
19813 goto desc_size_fail;
19814
19815 switch (pnote->type)
19816 {
19817 case NT_VMS_MHD:
19818 if (maxlen <= 36)
19819 goto desc_size_fail;
19820
19821 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
19822
19823 printf (_(" Creation date : %.17s\n"), pnote->descdata);
19824 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
19825 if (l + 34 < maxlen)
19826 {
19827 printf (_(" Module name : %s\n"), pnote->descdata + 34);
19828 if (l + 35 < maxlen)
19829 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
19830 else
19831 printf (_(" Module version : <missing>\n"));
19832 }
19833 else
19834 {
19835 printf (_(" Module name : <missing>\n"));
19836 printf (_(" Module version : <missing>\n"));
19837 }
19838 break;
19839
19840 case NT_VMS_LNM:
19841 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
19842 break;
19843
19844 #ifdef BFD64
19845 case NT_VMS_FPMODE:
19846 printf (_(" Floating Point mode: "));
19847 if (maxlen < 8)
19848 goto desc_size_fail;
19849 /* FIXME: Generate an error if descsz > 8 ? */
19850
19851 printf ("0x%016" BFD_VMA_FMT "x\n",
19852 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
19853 break;
19854
19855 case NT_VMS_LINKTIME:
19856 printf (_(" Link time: "));
19857 if (maxlen < 8)
19858 goto desc_size_fail;
19859 /* FIXME: Generate an error if descsz > 8 ? */
19860
19861 print_vms_time
19862 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
19863 printf ("\n");
19864 break;
19865
19866 case NT_VMS_PATCHTIME:
19867 printf (_(" Patch time: "));
19868 if (maxlen < 8)
19869 goto desc_size_fail;
19870 /* FIXME: Generate an error if descsz > 8 ? */
19871
19872 print_vms_time
19873 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
19874 printf ("\n");
19875 break;
19876
19877 case NT_VMS_ORIG_DYN:
19878 if (maxlen < 34)
19879 goto desc_size_fail;
19880
19881 printf (_(" Major id: %u, minor id: %u\n"),
19882 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
19883 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
19884 printf (_(" Last modified : "));
19885 print_vms_time
19886 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
19887 printf (_("\n Link flags : "));
19888 printf ("0x%016" BFD_VMA_FMT "x\n",
19889 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
19890 printf (_(" Header flags: 0x%08x\n"),
19891 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
19892 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
19893 break;
19894 #endif
19895
19896 case NT_VMS_IMGNAM:
19897 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
19898 break;
19899
19900 case NT_VMS_GSTNAM:
19901 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
19902 break;
19903
19904 case NT_VMS_IMGID:
19905 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
19906 break;
19907
19908 case NT_VMS_LINKID:
19909 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
19910 break;
19911
19912 default:
19913 return false;
19914 }
19915
19916 return true;
19917
19918 desc_size_fail:
19919 printf (_(" <corrupt - data size is too small>\n"));
19920 error (_("corrupt IA64 note: data size is too small\n"));
19921 return false;
19922 }
19923
19924 struct build_attr_cache {
19925 Filedata *filedata;
19926 char *strtab;
19927 unsigned long strtablen;
19928 Elf_Internal_Sym *symtab;
19929 unsigned long nsyms;
19930 } ba_cache;
19931
19932 /* Find the symbol associated with a build attribute that is attached
19933 to address OFFSET. If PNAME is non-NULL then store the name of
19934 the symbol (if found) in the provided pointer, Returns NULL if a
19935 symbol could not be found. */
19936
19937 static Elf_Internal_Sym *
19938 get_symbol_for_build_attribute (Filedata *filedata,
19939 unsigned long offset,
19940 bool is_open_attr,
19941 const char **pname)
19942 {
19943 Elf_Internal_Sym *saved_sym = NULL;
19944 Elf_Internal_Sym *sym;
19945
19946 if (filedata->section_headers != NULL
19947 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
19948 {
19949 Elf_Internal_Shdr * symsec;
19950
19951 free (ba_cache.strtab);
19952 ba_cache.strtab = NULL;
19953 free (ba_cache.symtab);
19954 ba_cache.symtab = NULL;
19955
19956 /* Load the symbol and string sections. */
19957 for (symsec = filedata->section_headers;
19958 symsec < filedata->section_headers + filedata->file_header.e_shnum;
19959 symsec ++)
19960 {
19961 if (symsec->sh_type == SHT_SYMTAB
19962 && get_symtab (filedata, symsec,
19963 &ba_cache.symtab, &ba_cache.nsyms,
19964 &ba_cache.strtab, &ba_cache.strtablen))
19965 break;
19966 }
19967 ba_cache.filedata = filedata;
19968 }
19969
19970 if (ba_cache.symtab == NULL)
19971 return NULL;
19972
19973 /* Find a symbol whose value matches offset. */
19974 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
19975 if (sym->st_value == offset)
19976 {
19977 if (sym->st_name >= ba_cache.strtablen)
19978 /* Huh ? This should not happen. */
19979 continue;
19980
19981 if (ba_cache.strtab[sym->st_name] == 0)
19982 continue;
19983
19984 /* The AArch64 and ARM architectures define mapping symbols
19985 (eg $d, $x, $t) which we want to ignore. */
19986 if (ba_cache.strtab[sym->st_name] == '$'
19987 && ba_cache.strtab[sym->st_name + 1] != 0
19988 && ba_cache.strtab[sym->st_name + 2] == 0)
19989 continue;
19990
19991 if (is_open_attr)
19992 {
19993 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
19994 and FILE or OBJECT symbols over NOTYPE symbols. We skip
19995 FUNC symbols entirely. */
19996 switch (ELF_ST_TYPE (sym->st_info))
19997 {
19998 case STT_OBJECT:
19999 case STT_FILE:
20000 saved_sym = sym;
20001 if (sym->st_size)
20002 {
20003 /* If the symbol has a size associated
20004 with it then we can stop searching. */
20005 sym = ba_cache.symtab + ba_cache.nsyms;
20006 }
20007 continue;
20008
20009 case STT_FUNC:
20010 /* Ignore function symbols. */
20011 continue;
20012
20013 default:
20014 break;
20015 }
20016
20017 switch (ELF_ST_BIND (sym->st_info))
20018 {
20019 case STB_GLOBAL:
20020 if (saved_sym == NULL
20021 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20022 saved_sym = sym;
20023 break;
20024
20025 case STB_LOCAL:
20026 if (saved_sym == NULL)
20027 saved_sym = sym;
20028 break;
20029
20030 default:
20031 break;
20032 }
20033 }
20034 else
20035 {
20036 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20037 continue;
20038
20039 saved_sym = sym;
20040 break;
20041 }
20042 }
20043
20044 if (saved_sym && pname)
20045 * pname = ba_cache.strtab + saved_sym->st_name;
20046
20047 return saved_sym;
20048 }
20049
20050 /* Returns true iff addr1 and addr2 are in the same section. */
20051
20052 static bool
20053 same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20054 {
20055 Elf_Internal_Shdr * a1;
20056 Elf_Internal_Shdr * a2;
20057
20058 a1 = find_section_by_address (filedata, addr1);
20059 a2 = find_section_by_address (filedata, addr2);
20060
20061 return a1 == a2 && a1 != NULL;
20062 }
20063
20064 static bool
20065 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20066 Filedata * filedata)
20067 {
20068 static unsigned long global_offset = 0;
20069 static unsigned long global_end = 0;
20070 static unsigned long func_offset = 0;
20071 static unsigned long func_end = 0;
20072
20073 Elf_Internal_Sym *sym;
20074 const char *name;
20075 unsigned long start;
20076 unsigned long end;
20077 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20078
20079 switch (pnote->descsz)
20080 {
20081 case 0:
20082 /* A zero-length description means that the range of
20083 the previous note of the same type should be used. */
20084 if (is_open_attr)
20085 {
20086 if (global_end > global_offset)
20087 printf (_(" Applies to region from %#lx to %#lx\n"),
20088 global_offset, global_end);
20089 else
20090 printf (_(" Applies to region from %#lx\n"), global_offset);
20091 }
20092 else
20093 {
20094 if (func_end > func_offset)
20095 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20096 else
20097 printf (_(" Applies to region from %#lx\n"), func_offset);
20098 }
20099 return true;
20100
20101 case 4:
20102 start = byte_get ((unsigned char *) pnote->descdata, 4);
20103 end = 0;
20104 break;
20105
20106 case 8:
20107 start = byte_get ((unsigned char *) pnote->descdata, 4);
20108 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20109 break;
20110
20111 case 16:
20112 start = byte_get ((unsigned char *) pnote->descdata, 8);
20113 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20114 break;
20115
20116 default:
20117 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20118 printf (_(" <invalid descsz>"));
20119 return false;
20120 }
20121
20122 name = NULL;
20123 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
20124 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20125 in order to avoid them being confused with the start address of the
20126 first function in the file... */
20127 if (sym == NULL && is_open_attr)
20128 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20129 & name);
20130
20131 if (end == 0 && sym != NULL && sym->st_size > 0)
20132 end = start + sym->st_size;
20133
20134 if (is_open_attr)
20135 {
20136 /* FIXME: Need to properly allow for section alignment.
20137 16 is just the alignment used on x86_64. */
20138 if (global_end > 0
20139 && start > BFD_ALIGN (global_end, 16)
20140 /* Build notes are not guaranteed to be organised in order of
20141 increasing address, but we should find the all of the notes
20142 for one section in the same place. */
20143 && same_section (filedata, start, global_end))
20144 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20145 global_end + 1, start - 1);
20146
20147 printf (_(" Applies to region from %#lx"), start);
20148 global_offset = start;
20149
20150 if (end)
20151 {
20152 printf (_(" to %#lx"), end);
20153 global_end = end;
20154 }
20155 }
20156 else
20157 {
20158 printf (_(" Applies to region from %#lx"), start);
20159 func_offset = start;
20160
20161 if (end)
20162 {
20163 printf (_(" to %#lx"), end);
20164 func_end = end;
20165 }
20166 }
20167
20168 if (sym && name)
20169 printf (_(" (%s)"), name);
20170
20171 printf ("\n");
20172 return true;
20173 }
20174
20175 static bool
20176 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20177 {
20178 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20179 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20180 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
20181 char name_type;
20182 char name_attribute;
20183 const char * expected_types;
20184 const char * name = pnote->namedata;
20185 const char * text;
20186 signed int left;
20187
20188 if (name == NULL || pnote->namesz < 2)
20189 {
20190 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20191 print_symbol (-20, _(" <corrupt name>"));
20192 return false;
20193 }
20194
20195 if (do_wide)
20196 left = 28;
20197 else
20198 left = 20;
20199
20200 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20201 if (name[0] == 'G' && name[1] == 'A')
20202 {
20203 if (pnote->namesz < 4)
20204 {
20205 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20206 print_symbol (-20, _(" <corrupt name>"));
20207 return false;
20208 }
20209
20210 printf ("GA");
20211 name += 2;
20212 left -= 2;
20213 }
20214
20215 switch ((name_type = * name))
20216 {
20217 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20218 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20219 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20220 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20221 printf ("%c", * name);
20222 left --;
20223 break;
20224 default:
20225 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20226 print_symbol (-20, _("<unknown name type>"));
20227 return false;
20228 }
20229
20230 ++ name;
20231 text = NULL;
20232
20233 switch ((name_attribute = * name))
20234 {
20235 case GNU_BUILD_ATTRIBUTE_VERSION:
20236 text = _("<version>");
20237 expected_types = string_expected;
20238 ++ name;
20239 break;
20240 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20241 text = _("<stack prot>");
20242 expected_types = "!+*";
20243 ++ name;
20244 break;
20245 case GNU_BUILD_ATTRIBUTE_RELRO:
20246 text = _("<relro>");
20247 expected_types = bool_expected;
20248 ++ name;
20249 break;
20250 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20251 text = _("<stack size>");
20252 expected_types = number_expected;
20253 ++ name;
20254 break;
20255 case GNU_BUILD_ATTRIBUTE_TOOL:
20256 text = _("<tool>");
20257 expected_types = string_expected;
20258 ++ name;
20259 break;
20260 case GNU_BUILD_ATTRIBUTE_ABI:
20261 text = _("<ABI>");
20262 expected_types = "$*";
20263 ++ name;
20264 break;
20265 case GNU_BUILD_ATTRIBUTE_PIC:
20266 text = _("<PIC>");
20267 expected_types = number_expected;
20268 ++ name;
20269 break;
20270 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20271 text = _("<short enum>");
20272 expected_types = bool_expected;
20273 ++ name;
20274 break;
20275 default:
20276 if (ISPRINT (* name))
20277 {
20278 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20279
20280 if (len > left && ! do_wide)
20281 len = left;
20282 printf ("%.*s:", len, name);
20283 left -= len;
20284 name += len;
20285 }
20286 else
20287 {
20288 static char tmpbuf [128];
20289
20290 error (_("unrecognised byte in name field: %d\n"), * name);
20291 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20292 text = tmpbuf;
20293 name ++;
20294 }
20295 expected_types = "*$!+";
20296 break;
20297 }
20298
20299 if (text)
20300 left -= printf ("%s", text);
20301
20302 if (strchr (expected_types, name_type) == NULL)
20303 warn (_("attribute does not have an expected type (%c)\n"), name_type);
20304
20305 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
20306 {
20307 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
20308 (unsigned long) pnote->namesz,
20309 (long) (name - pnote->namedata));
20310 return false;
20311 }
20312
20313 if (left < 1 && ! do_wide)
20314 return true;
20315
20316 switch (name_type)
20317 {
20318 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20319 {
20320 unsigned int bytes;
20321 unsigned long long val = 0;
20322 unsigned int shift = 0;
20323 char * decoded = NULL;
20324
20325 bytes = pnote->namesz - (name - pnote->namedata);
20326 if (bytes > 0)
20327 /* The -1 is because the name field is always 0 terminated, and we
20328 want to be able to ensure that the shift in the while loop below
20329 will not overflow. */
20330 -- bytes;
20331
20332 if (bytes > sizeof (val))
20333 {
20334 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
20335 bytes);
20336 bytes = sizeof (val);
20337 }
20338 /* We do not bother to warn if bytes == 0 as this can
20339 happen with some early versions of the gcc plugin. */
20340
20341 while (bytes --)
20342 {
20343 unsigned long long byte = *name++ & 0xff;
20344
20345 val |= byte << shift;
20346 shift += 8;
20347 }
20348
20349 switch (name_attribute)
20350 {
20351 case GNU_BUILD_ATTRIBUTE_PIC:
20352 switch (val)
20353 {
20354 case 0: decoded = "static"; break;
20355 case 1: decoded = "pic"; break;
20356 case 2: decoded = "PIC"; break;
20357 case 3: decoded = "pie"; break;
20358 case 4: decoded = "PIE"; break;
20359 default: break;
20360 }
20361 break;
20362 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20363 switch (val)
20364 {
20365 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
20366 case 0: decoded = "off"; break;
20367 case 1: decoded = "on"; break;
20368 case 2: decoded = "all"; break;
20369 case 3: decoded = "strong"; break;
20370 case 4: decoded = "explicit"; break;
20371 default: break;
20372 }
20373 break;
20374 default:
20375 break;
20376 }
20377
20378 if (decoded != NULL)
20379 {
20380 print_symbol (-left, decoded);
20381 left = 0;
20382 }
20383 else if (val == 0)
20384 {
20385 printf ("0x0");
20386 left -= 3;
20387 }
20388 else
20389 {
20390 if (do_wide)
20391 left -= printf ("0x%llx", val);
20392 else
20393 left -= printf ("0x%-.*llx", left, val);
20394 }
20395 }
20396 break;
20397 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20398 left -= print_symbol (- left, name);
20399 break;
20400 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20401 left -= print_symbol (- left, "true");
20402 break;
20403 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20404 left -= print_symbol (- left, "false");
20405 break;
20406 }
20407
20408 if (do_wide && left > 0)
20409 printf ("%-*s", left, " ");
20410
20411 return true;
20412 }
20413
20414 /* Note that by the ELF standard, the name field is already null byte
20415 terminated, and namesz includes the terminating null byte.
20416 I.E. the value of namesz for the name "FSF" is 4.
20417
20418 If the value of namesz is zero, there is no name present. */
20419
20420 static bool
20421 process_note (Elf_Internal_Note * pnote,
20422 Filedata * filedata)
20423 {
20424 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
20425 const char * nt;
20426
20427 if (pnote->namesz == 0)
20428 /* If there is no note name, then use the default set of
20429 note type strings. */
20430 nt = get_note_type (filedata, pnote->type);
20431
20432 else if (const_strneq (pnote->namedata, "GNU"))
20433 /* GNU-specific object file notes. */
20434 nt = get_gnu_elf_note_type (pnote->type);
20435
20436 else if (const_strneq (pnote->namedata, "FreeBSD"))
20437 /* FreeBSD-specific core file notes. */
20438 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
20439
20440 else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
20441 /* NetBSD-specific core file notes. */
20442 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
20443
20444 else if (const_strneq (pnote->namedata, "NetBSD"))
20445 /* NetBSD-specific core file notes. */
20446 return process_netbsd_elf_note (pnote);
20447
20448 else if (const_strneq (pnote->namedata, "PaX"))
20449 /* NetBSD-specific core file notes. */
20450 return process_netbsd_elf_note (pnote);
20451
20452 else if (strneq (pnote->namedata, "SPU/", 4))
20453 {
20454 /* SPU-specific core file notes. */
20455 nt = pnote->namedata + 4;
20456 name = "SPU";
20457 }
20458
20459 else if (const_strneq (pnote->namedata, "IPF/VMS"))
20460 /* VMS/ia64-specific file notes. */
20461 nt = get_ia64_vms_note_type (pnote->type);
20462
20463 else if (const_strneq (pnote->namedata, "stapsdt"))
20464 nt = get_stapsdt_note_type (pnote->type);
20465
20466 else
20467 /* Don't recognize this note name; just use the default set of
20468 note type strings. */
20469 nt = get_note_type (filedata, pnote->type);
20470
20471 printf (" ");
20472
20473 if (((const_strneq (pnote->namedata, "GA")
20474 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20475 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20476 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20477 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20478 print_gnu_build_attribute_name (pnote);
20479 else
20480 print_symbol (-20, name);
20481
20482 if (do_wide)
20483 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
20484 else
20485 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
20486
20487 if (const_strneq (pnote->namedata, "IPF/VMS"))
20488 return print_ia64_vms_note (pnote);
20489 else if (const_strneq (pnote->namedata, "GNU"))
20490 return print_gnu_note (filedata, pnote);
20491 else if (const_strneq (pnote->namedata, "stapsdt"))
20492 return print_stapsdt_note (pnote);
20493 else if (const_strneq (pnote->namedata, "CORE"))
20494 return print_core_note (pnote);
20495 else if (((const_strneq (pnote->namedata, "GA")
20496 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20497 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20498 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20499 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20500 return print_gnu_build_attribute_description (pnote, filedata);
20501
20502 if (pnote->descsz)
20503 {
20504 unsigned long i;
20505
20506 printf (_(" description data: "));
20507 for (i = 0; i < pnote->descsz; i++)
20508 printf ("%02x ", pnote->descdata[i] & 0xff);
20509 if (!do_wide)
20510 printf ("\n");
20511 }
20512
20513 if (do_wide)
20514 printf ("\n");
20515
20516 return true;
20517 }
20518
20519 static bool
20520 process_notes_at (Filedata * filedata,
20521 Elf_Internal_Shdr * section,
20522 bfd_vma offset,
20523 bfd_vma length,
20524 bfd_vma align)
20525 {
20526 Elf_External_Note *pnotes;
20527 Elf_External_Note *external;
20528 char *end;
20529 bool res = true;
20530
20531 if (length <= 0)
20532 return false;
20533
20534 if (section)
20535 {
20536 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
20537 if (pnotes)
20538 {
20539 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
20540 {
20541 free (pnotes);
20542 return false;
20543 }
20544 }
20545 }
20546 else
20547 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20548 _("notes"));
20549
20550 if (pnotes == NULL)
20551 return false;
20552
20553 external = pnotes;
20554
20555 if (filedata->is_separate)
20556 printf (_("In linked file '%s': "), filedata->file_name);
20557 else
20558 printf ("\n");
20559 if (section)
20560 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
20561 else
20562 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
20563 (unsigned long) offset, (unsigned long) length);
20564
20565 /* NB: Some note sections may have alignment value of 0 or 1. gABI
20566 specifies that notes should be aligned to 4 bytes in 32-bit
20567 objects and to 8 bytes in 64-bit objects. As a Linux extension,
20568 we also support 4 byte alignment in 64-bit objects. If section
20569 alignment is less than 4, we treate alignment as 4 bytes. */
20570 if (align < 4)
20571 align = 4;
20572 else if (align != 4 && align != 8)
20573 {
20574 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
20575 (long) align);
20576 free (pnotes);
20577 return false;
20578 }
20579
20580 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
20581
20582 end = (char *) pnotes + length;
20583 while ((char *) external < end)
20584 {
20585 Elf_Internal_Note inote;
20586 size_t min_notesz;
20587 char * next;
20588 char * temp = NULL;
20589 size_t data_remaining = end - (char *) external;
20590
20591 if (!is_ia64_vms (filedata))
20592 {
20593 /* PR binutils/15191
20594 Make sure that there is enough data to read. */
20595 min_notesz = offsetof (Elf_External_Note, name);
20596 if (data_remaining < min_notesz)
20597 {
20598 warn (ngettext ("Corrupt note: only %ld byte remains, "
20599 "not enough for a full note\n",
20600 "Corrupt note: only %ld bytes remain, "
20601 "not enough for a full note\n",
20602 data_remaining),
20603 (long) data_remaining);
20604 break;
20605 }
20606 data_remaining -= min_notesz;
20607
20608 inote.type = BYTE_GET (external->type);
20609 inote.namesz = BYTE_GET (external->namesz);
20610 inote.namedata = external->name;
20611 inote.descsz = BYTE_GET (external->descsz);
20612 inote.descdata = ((char *) external
20613 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
20614 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20615 next = ((char *) external
20616 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
20617 }
20618 else
20619 {
20620 Elf64_External_VMS_Note *vms_external;
20621
20622 /* PR binutils/15191
20623 Make sure that there is enough data to read. */
20624 min_notesz = offsetof (Elf64_External_VMS_Note, name);
20625 if (data_remaining < min_notesz)
20626 {
20627 warn (ngettext ("Corrupt note: only %ld byte remains, "
20628 "not enough for a full note\n",
20629 "Corrupt note: only %ld bytes remain, "
20630 "not enough for a full note\n",
20631 data_remaining),
20632 (long) data_remaining);
20633 break;
20634 }
20635 data_remaining -= min_notesz;
20636
20637 vms_external = (Elf64_External_VMS_Note *) external;
20638 inote.type = BYTE_GET (vms_external->type);
20639 inote.namesz = BYTE_GET (vms_external->namesz);
20640 inote.namedata = vms_external->name;
20641 inote.descsz = BYTE_GET (vms_external->descsz);
20642 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
20643 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20644 next = inote.descdata + align_power (inote.descsz, 3);
20645 }
20646
20647 /* PR 17531: file: 3443835e. */
20648 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
20649 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
20650 || (size_t) (inote.descdata - inote.namedata) > data_remaining
20651 || (size_t) (next - inote.descdata) < inote.descsz
20652 || ((size_t) (next - inote.descdata)
20653 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
20654 {
20655 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
20656 (unsigned long) ((char *) external - (char *) pnotes));
20657 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
20658 inote.type, inote.namesz, inote.descsz, (int) align);
20659 break;
20660 }
20661
20662 external = (Elf_External_Note *) next;
20663
20664 /* Verify that name is null terminated. It appears that at least
20665 one version of Linux (RedHat 6.0) generates corefiles that don't
20666 comply with the ELF spec by failing to include the null byte in
20667 namesz. */
20668 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
20669 {
20670 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
20671 {
20672 temp = (char *) malloc (inote.namesz + 1);
20673 if (temp == NULL)
20674 {
20675 error (_("Out of memory allocating space for inote name\n"));
20676 res = false;
20677 break;
20678 }
20679
20680 memcpy (temp, inote.namedata, inote.namesz);
20681 inote.namedata = temp;
20682 }
20683 inote.namedata[inote.namesz] = 0;
20684 }
20685
20686 if (! process_note (& inote, filedata))
20687 res = false;
20688
20689 free (temp);
20690 temp = NULL;
20691 }
20692
20693 free (pnotes);
20694
20695 return res;
20696 }
20697
20698 static bool
20699 process_corefile_note_segments (Filedata * filedata)
20700 {
20701 Elf_Internal_Phdr *segment;
20702 unsigned int i;
20703 bool res = true;
20704
20705 if (! get_program_headers (filedata))
20706 return true;
20707
20708 for (i = 0, segment = filedata->program_headers;
20709 i < filedata->file_header.e_phnum;
20710 i++, segment++)
20711 {
20712 if (segment->p_type == PT_NOTE)
20713 if (! process_notes_at (filedata, NULL,
20714 (bfd_vma) segment->p_offset,
20715 (bfd_vma) segment->p_filesz,
20716 (bfd_vma) segment->p_align))
20717 res = false;
20718 }
20719
20720 return res;
20721 }
20722
20723 static bool
20724 process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
20725 {
20726 Elf_External_Note * pnotes;
20727 Elf_External_Note * external;
20728 char * end;
20729 bool res = true;
20730
20731 if (length <= 0)
20732 return false;
20733
20734 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20735 _("v850 notes"));
20736 if (pnotes == NULL)
20737 return false;
20738
20739 external = pnotes;
20740 end = (char*) pnotes + length;
20741
20742 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
20743 (unsigned long) offset, (unsigned long) length);
20744
20745 while ((char *) external + sizeof (Elf_External_Note) < end)
20746 {
20747 Elf_External_Note * next;
20748 Elf_Internal_Note inote;
20749
20750 inote.type = BYTE_GET (external->type);
20751 inote.namesz = BYTE_GET (external->namesz);
20752 inote.namedata = external->name;
20753 inote.descsz = BYTE_GET (external->descsz);
20754 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
20755 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20756
20757 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
20758 {
20759 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
20760 inote.descdata = inote.namedata;
20761 inote.namesz = 0;
20762 }
20763
20764 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
20765
20766 if ( ((char *) next > end)
20767 || ((char *) next < (char *) pnotes))
20768 {
20769 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
20770 (unsigned long) ((char *) external - (char *) pnotes));
20771 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20772 inote.type, inote.namesz, inote.descsz);
20773 break;
20774 }
20775
20776 external = next;
20777
20778 /* Prevent out-of-bounds indexing. */
20779 if ( inote.namedata + inote.namesz > end
20780 || inote.namedata + inote.namesz < inote.namedata)
20781 {
20782 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
20783 (unsigned long) ((char *) external - (char *) pnotes));
20784 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20785 inote.type, inote.namesz, inote.descsz);
20786 break;
20787 }
20788
20789 printf (" %s: ", get_v850_elf_note_type (inote.type));
20790
20791 if (! print_v850_note (& inote))
20792 {
20793 res = false;
20794 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
20795 inote.namesz, inote.descsz);
20796 }
20797 }
20798
20799 free (pnotes);
20800
20801 return res;
20802 }
20803
20804 static bool
20805 process_note_sections (Filedata * filedata)
20806 {
20807 Elf_Internal_Shdr *section;
20808 unsigned long i;
20809 unsigned int n = 0;
20810 bool res = true;
20811
20812 for (i = 0, section = filedata->section_headers;
20813 i < filedata->file_header.e_shnum && section != NULL;
20814 i++, section++)
20815 {
20816 if (section->sh_type == SHT_NOTE)
20817 {
20818 if (! process_notes_at (filedata, section,
20819 (bfd_vma) section->sh_offset,
20820 (bfd_vma) section->sh_size,
20821 (bfd_vma) section->sh_addralign))
20822 res = false;
20823 n++;
20824 }
20825
20826 if (( filedata->file_header.e_machine == EM_V800
20827 || filedata->file_header.e_machine == EM_V850
20828 || filedata->file_header.e_machine == EM_CYGNUS_V850)
20829 && section->sh_type == SHT_RENESAS_INFO)
20830 {
20831 if (! process_v850_notes (filedata,
20832 (bfd_vma) section->sh_offset,
20833 (bfd_vma) section->sh_size))
20834 res = false;
20835 n++;
20836 }
20837 }
20838
20839 if (n == 0)
20840 /* Try processing NOTE segments instead. */
20841 return process_corefile_note_segments (filedata);
20842
20843 return res;
20844 }
20845
20846 static bool
20847 process_notes (Filedata * filedata)
20848 {
20849 /* If we have not been asked to display the notes then do nothing. */
20850 if (! do_notes)
20851 return true;
20852
20853 if (filedata->file_header.e_type != ET_CORE)
20854 return process_note_sections (filedata);
20855
20856 /* No program headers means no NOTE segment. */
20857 if (filedata->file_header.e_phnum > 0)
20858 return process_corefile_note_segments (filedata);
20859
20860 if (filedata->is_separate)
20861 printf (_("No notes found in linked file '%s'.\n"),
20862 filedata->file_name);
20863 else
20864 printf (_("No notes found file.\n"));
20865
20866 return true;
20867 }
20868
20869 static unsigned char *
20870 display_public_gnu_attributes (unsigned char * start,
20871 const unsigned char * const end)
20872 {
20873 printf (_(" Unknown GNU attribute: %s\n"), start);
20874
20875 start += strnlen ((char *) start, end - start);
20876 display_raw_attribute (start, end);
20877
20878 return (unsigned char *) end;
20879 }
20880
20881 static unsigned char *
20882 display_generic_attribute (unsigned char * start,
20883 unsigned int tag,
20884 const unsigned char * const end)
20885 {
20886 if (tag == 0)
20887 return (unsigned char *) end;
20888
20889 return display_tag_value (tag, start, end);
20890 }
20891
20892 static bool
20893 process_arch_specific (Filedata * filedata)
20894 {
20895 if (! do_arch)
20896 return true;
20897
20898 switch (filedata->file_header.e_machine)
20899 {
20900 case EM_ARC:
20901 case EM_ARC_COMPACT:
20902 case EM_ARC_COMPACT2:
20903 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
20904 display_arc_attribute,
20905 display_generic_attribute);
20906 case EM_ARM:
20907 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
20908 display_arm_attribute,
20909 display_generic_attribute);
20910
20911 case EM_MIPS:
20912 case EM_MIPS_RS3_LE:
20913 return process_mips_specific (filedata);
20914
20915 case EM_MSP430:
20916 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
20917 display_msp430_attribute,
20918 display_msp430_gnu_attribute);
20919
20920 case EM_RISCV:
20921 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
20922 display_riscv_attribute,
20923 display_generic_attribute);
20924
20925 case EM_NDS32:
20926 return process_nds32_specific (filedata);
20927
20928 case EM_68K:
20929 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20930 display_m68k_gnu_attribute);
20931
20932 case EM_PPC:
20933 case EM_PPC64:
20934 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20935 display_power_gnu_attribute);
20936
20937 case EM_S390:
20938 case EM_S390_OLD:
20939 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20940 display_s390_gnu_attribute);
20941
20942 case EM_SPARC:
20943 case EM_SPARC32PLUS:
20944 case EM_SPARCV9:
20945 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20946 display_sparc_gnu_attribute);
20947
20948 case EM_TI_C6000:
20949 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
20950 display_tic6x_attribute,
20951 display_generic_attribute);
20952
20953 case EM_CSKY:
20954 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
20955 display_csky_attribute, NULL);
20956
20957 default:
20958 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
20959 display_public_gnu_attributes,
20960 display_generic_attribute);
20961 }
20962 }
20963
20964 static bool
20965 get_file_header (Filedata * filedata)
20966 {
20967 /* Read in the identity array. */
20968 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
20969 return false;
20970
20971 /* Determine how to read the rest of the header. */
20972 switch (filedata->file_header.e_ident[EI_DATA])
20973 {
20974 default:
20975 case ELFDATANONE:
20976 case ELFDATA2LSB:
20977 byte_get = byte_get_little_endian;
20978 byte_put = byte_put_little_endian;
20979 break;
20980 case ELFDATA2MSB:
20981 byte_get = byte_get_big_endian;
20982 byte_put = byte_put_big_endian;
20983 break;
20984 }
20985
20986 /* For now we only support 32 bit and 64 bit ELF files. */
20987 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
20988
20989 /* Read in the rest of the header. */
20990 if (is_32bit_elf)
20991 {
20992 Elf32_External_Ehdr ehdr32;
20993
20994 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
20995 return false;
20996
20997 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
20998 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
20999 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
21000 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
21001 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
21002 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
21003 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
21004 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
21005 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
21006 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
21007 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
21008 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
21009 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
21010 }
21011 else
21012 {
21013 Elf64_External_Ehdr ehdr64;
21014
21015 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21016 we will not be able to cope with the 64bit data found in
21017 64 ELF files. Detect this now and abort before we start
21018 overwriting things. */
21019 if (sizeof (bfd_vma) < 8)
21020 {
21021 error (_("This instance of readelf has been built without support for a\n\
21022 64 bit data type and so it cannot read 64 bit ELF files.\n"));
21023 return false;
21024 }
21025
21026 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
21027 return false;
21028
21029 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21030 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21031 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21032 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21033 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21034 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21035 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21036 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21037 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21038 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21039 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21040 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21041 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
21042 }
21043
21044 if (filedata->file_header.e_shoff)
21045 {
21046 /* There may be some extensions in the first section header. Don't
21047 bomb if we can't read it. */
21048 if (is_32bit_elf)
21049 get_32bit_section_headers (filedata, true);
21050 else
21051 get_64bit_section_headers (filedata, true);
21052 }
21053
21054 return true;
21055 }
21056
21057 static void
21058 close_file (Filedata * filedata)
21059 {
21060 if (filedata)
21061 {
21062 if (filedata->handle)
21063 fclose (filedata->handle);
21064 free (filedata);
21065 }
21066 }
21067
21068 void
21069 close_debug_file (void * data)
21070 {
21071 close_file ((Filedata *) data);
21072 }
21073
21074 static Filedata *
21075 open_file (const char * pathname, bool is_separate)
21076 {
21077 struct stat statbuf;
21078 Filedata * filedata = NULL;
21079
21080 if (stat (pathname, & statbuf) < 0
21081 || ! S_ISREG (statbuf.st_mode))
21082 goto fail;
21083
21084 filedata = calloc (1, sizeof * filedata);
21085 if (filedata == NULL)
21086 goto fail;
21087
21088 filedata->handle = fopen (pathname, "rb");
21089 if (filedata->handle == NULL)
21090 goto fail;
21091
21092 filedata->file_size = (bfd_size_type) statbuf.st_size;
21093 filedata->file_name = pathname;
21094 filedata->is_separate = is_separate;
21095
21096 if (! get_file_header (filedata))
21097 goto fail;
21098
21099 if (filedata->file_header.e_shoff)
21100 {
21101 bool res;
21102
21103 /* Read the section headers again, this time for real. */
21104 if (is_32bit_elf)
21105 res = get_32bit_section_headers (filedata, false);
21106 else
21107 res = get_64bit_section_headers (filedata, false);
21108
21109 if (!res)
21110 goto fail;
21111 }
21112
21113 return filedata;
21114
21115 fail:
21116 if (filedata)
21117 {
21118 if (filedata->handle)
21119 fclose (filedata->handle);
21120 free (filedata);
21121 }
21122 return NULL;
21123 }
21124
21125 void *
21126 open_debug_file (const char * pathname)
21127 {
21128 return open_file (pathname, true);
21129 }
21130
21131 static void
21132 initialise_dump_sects (Filedata * filedata)
21133 {
21134 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21135 Note we do this even if cmdline_dump_sects is empty because we
21136 must make sure that the dump_sets array is zeroed out before each
21137 object file is processed. */
21138 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21139 memset (filedata->dump.dump_sects, 0,
21140 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21141
21142 if (cmdline.num_dump_sects > 0)
21143 {
21144 if (filedata->dump.num_dump_sects == 0)
21145 /* A sneaky way of allocating the dump_sects array. */
21146 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21147
21148 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21149 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21150 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21151 }
21152 }
21153
21154 /* Process one ELF object file according to the command line options.
21155 This file may actually be stored in an archive. The file is
21156 positioned at the start of the ELF object. Returns TRUE if no
21157 problems were encountered, FALSE otherwise. */
21158
21159 static bool
21160 process_object (Filedata * filedata)
21161 {
21162 bool have_separate_files;
21163 unsigned int i;
21164 bool res;
21165
21166 if (! get_file_header (filedata))
21167 {
21168 error (_("%s: Failed to read file header\n"), filedata->file_name);
21169 return false;
21170 }
21171
21172 /* Initialise per file variables. */
21173 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21174 filedata->version_info[i] = 0;
21175
21176 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21177 filedata->dynamic_info[i] = 0;
21178 filedata->dynamic_info_DT_GNU_HASH = 0;
21179 filedata->dynamic_info_DT_MIPS_XHASH = 0;
21180
21181 /* Process the file. */
21182 if (show_name)
21183 printf (_("\nFile: %s\n"), filedata->file_name);
21184
21185 initialise_dump_sects (filedata);
21186
21187 if (! process_file_header (filedata))
21188 return false;
21189
21190 if (! process_section_headers (filedata))
21191 {
21192 /* Without loaded section headers we cannot process lots of things. */
21193 do_unwind = do_version = do_dump = do_arch = false;
21194
21195 if (! do_using_dynamic)
21196 do_syms = do_dyn_syms = do_reloc = false;
21197 }
21198
21199 if (! process_section_groups (filedata))
21200 /* Without loaded section groups we cannot process unwind. */
21201 do_unwind = false;
21202
21203 res = process_program_headers (filedata);
21204 if (res)
21205 res = process_dynamic_section (filedata);
21206
21207 if (! process_relocs (filedata))
21208 res = false;
21209
21210 if (! process_unwind (filedata))
21211 res = false;
21212
21213 if (! process_symbol_table (filedata))
21214 res = false;
21215
21216 if (! process_lto_symbol_tables (filedata))
21217 res = false;
21218
21219 if (! process_syminfo (filedata))
21220 res = false;
21221
21222 if (! process_version_sections (filedata))
21223 res = false;
21224
21225 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
21226 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
21227 else
21228 have_separate_files = false;
21229
21230 if (! process_section_contents (filedata))
21231 res = false;
21232
21233 if (have_separate_files)
21234 {
21235 separate_info * d;
21236
21237 for (d = first_separate_info; d != NULL; d = d->next)
21238 {
21239 initialise_dump_sects (d->handle);
21240
21241 if (process_links && ! process_file_header (d->handle))
21242 res = false;
21243 else if (! process_section_headers (d->handle))
21244 res = false;
21245 else if (! process_section_contents (d->handle))
21246 res = false;
21247 else if (process_links)
21248 {
21249 if (! process_section_groups (d->handle))
21250 res = false;
21251 if (! process_program_headers (d->handle))
21252 res = false;
21253 if (! process_dynamic_section (d->handle))
21254 res = false;
21255 if (! process_relocs (d->handle))
21256 res = false;
21257 if (! process_unwind (d->handle))
21258 res = false;
21259 if (! process_symbol_table (d->handle))
21260 res = false;
21261 if (! process_lto_symbol_tables (d->handle))
21262 res = false;
21263 if (! process_syminfo (d->handle))
21264 res = false;
21265 if (! process_version_sections (d->handle))
21266 res = false;
21267 if (! process_notes (d->handle))
21268 res = false;
21269 }
21270 }
21271
21272 /* The file handles are closed by the call to free_debug_memory() below. */
21273 }
21274
21275 if (! process_notes (filedata))
21276 res = false;
21277
21278 if (! process_gnu_liblist (filedata))
21279 res = false;
21280
21281 if (! process_arch_specific (filedata))
21282 res = false;
21283
21284 free (filedata->program_headers);
21285 filedata->program_headers = NULL;
21286
21287 free (filedata->section_headers);
21288 filedata->section_headers = NULL;
21289
21290 free (filedata->string_table);
21291 filedata->string_table = NULL;
21292 filedata->string_table_length = 0;
21293
21294 free (filedata->dump.dump_sects);
21295 filedata->dump.dump_sects = NULL;
21296 filedata->dump.num_dump_sects = 0;
21297
21298 free (filedata->dynamic_strings);
21299 filedata->dynamic_strings = NULL;
21300 filedata->dynamic_strings_length = 0;
21301
21302 free (filedata->dynamic_symbols);
21303 filedata->dynamic_symbols = NULL;
21304 filedata->num_dynamic_syms = 0;
21305
21306 free (filedata->dynamic_syminfo);
21307 filedata->dynamic_syminfo = NULL;
21308
21309 free (filedata->dynamic_section);
21310 filedata->dynamic_section = NULL;
21311
21312 while (filedata->symtab_shndx_list != NULL)
21313 {
21314 elf_section_list *next = filedata->symtab_shndx_list->next;
21315 free (filedata->symtab_shndx_list);
21316 filedata->symtab_shndx_list = next;
21317 }
21318
21319 free (filedata->section_headers_groups);
21320 filedata->section_headers_groups = NULL;
21321
21322 if (filedata->section_groups)
21323 {
21324 struct group_list * g;
21325 struct group_list * next;
21326
21327 for (i = 0; i < filedata->group_count; i++)
21328 {
21329 for (g = filedata->section_groups [i].root; g != NULL; g = next)
21330 {
21331 next = g->next;
21332 free (g);
21333 }
21334 }
21335
21336 free (filedata->section_groups);
21337 filedata->section_groups = NULL;
21338 }
21339
21340 free_debug_memory ();
21341
21342 return res;
21343 }
21344
21345 /* Process an ELF archive.
21346 On entry the file is positioned just after the ARMAG string.
21347 Returns TRUE upon success, FALSE otherwise. */
21348
21349 static bool
21350 process_archive (Filedata * filedata, bool is_thin_archive)
21351 {
21352 struct archive_info arch;
21353 struct archive_info nested_arch;
21354 size_t got;
21355 bool ret = true;
21356
21357 show_name = true;
21358
21359 /* The ARCH structure is used to hold information about this archive. */
21360 arch.file_name = NULL;
21361 arch.file = NULL;
21362 arch.index_array = NULL;
21363 arch.sym_table = NULL;
21364 arch.longnames = NULL;
21365
21366 /* The NESTED_ARCH structure is used as a single-item cache of information
21367 about a nested archive (when members of a thin archive reside within
21368 another regular archive file). */
21369 nested_arch.file_name = NULL;
21370 nested_arch.file = NULL;
21371 nested_arch.index_array = NULL;
21372 nested_arch.sym_table = NULL;
21373 nested_arch.longnames = NULL;
21374
21375 if (setup_archive (&arch, filedata->file_name, filedata->handle,
21376 filedata->file_size, is_thin_archive,
21377 do_archive_index) != 0)
21378 {
21379 ret = false;
21380 goto out;
21381 }
21382
21383 if (do_archive_index)
21384 {
21385 if (arch.sym_table == NULL)
21386 error (_("%s: unable to dump the index as none was found\n"),
21387 filedata->file_name);
21388 else
21389 {
21390 unsigned long i, l;
21391 unsigned long current_pos;
21392
21393 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
21394 "in the symbol table)\n"),
21395 filedata->file_name, (unsigned long) arch.index_num,
21396 arch.sym_size);
21397
21398 current_pos = ftell (filedata->handle);
21399
21400 for (i = l = 0; i < arch.index_num; i++)
21401 {
21402 if (i == 0
21403 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
21404 {
21405 char * member_name
21406 = get_archive_member_name_at (&arch, arch.index_array[i],
21407 &nested_arch);
21408
21409 if (member_name != NULL)
21410 {
21411 char * qualified_name
21412 = make_qualified_name (&arch, &nested_arch,
21413 member_name);
21414
21415 if (qualified_name != NULL)
21416 {
21417 printf (_("Contents of binary %s at offset "),
21418 qualified_name);
21419 (void) print_vma (arch.index_array[i], PREFIX_HEX);
21420 putchar ('\n');
21421 free (qualified_name);
21422 }
21423 free (member_name);
21424 }
21425 }
21426
21427 if (l >= arch.sym_size)
21428 {
21429 error (_("%s: end of the symbol table reached "
21430 "before the end of the index\n"),
21431 filedata->file_name);
21432 ret = false;
21433 break;
21434 }
21435 /* PR 17531: file: 0b6630b2. */
21436 printf ("\t%.*s\n",
21437 (int) (arch.sym_size - l), arch.sym_table + l);
21438 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
21439 }
21440
21441 if (arch.uses_64bit_indices)
21442 l = (l + 7) & ~ 7;
21443 else
21444 l += l & 1;
21445
21446 if (l < arch.sym_size)
21447 {
21448 error (ngettext ("%s: %ld byte remains in the symbol table, "
21449 "but without corresponding entries in "
21450 "the index table\n",
21451 "%s: %ld bytes remain in the symbol table, "
21452 "but without corresponding entries in "
21453 "the index table\n",
21454 arch.sym_size - l),
21455 filedata->file_name, arch.sym_size - l);
21456 ret = false;
21457 }
21458
21459 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
21460 {
21461 error (_("%s: failed to seek back to start of object files "
21462 "in the archive\n"),
21463 filedata->file_name);
21464 ret = false;
21465 goto out;
21466 }
21467 }
21468
21469 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
21470 && !do_segments && !do_header && !do_dump && !do_version
21471 && !do_histogram && !do_debugging && !do_arch && !do_notes
21472 && !do_section_groups && !do_dyn_syms)
21473 {
21474 ret = true; /* Archive index only. */
21475 goto out;
21476 }
21477 }
21478
21479 while (1)
21480 {
21481 char * name;
21482 size_t namelen;
21483 char * qualified_name;
21484
21485 /* Read the next archive header. */
21486 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
21487 {
21488 error (_("%s: failed to seek to next archive header\n"),
21489 arch.file_name);
21490 ret = false;
21491 break;
21492 }
21493 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
21494 if (got != sizeof arch.arhdr)
21495 {
21496 if (got == 0)
21497 break;
21498 /* PR 24049 - we cannot use filedata->file_name as this will
21499 have already been freed. */
21500 error (_("%s: failed to read archive header\n"), arch.file_name);
21501
21502 ret = false;
21503 break;
21504 }
21505 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
21506 {
21507 error (_("%s: did not find a valid archive header\n"),
21508 arch.file_name);
21509 ret = false;
21510 break;
21511 }
21512
21513 arch.next_arhdr_offset += sizeof arch.arhdr;
21514
21515 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
21516 if (filedata->archive_file_size & 01)
21517 ++filedata->archive_file_size;
21518
21519 name = get_archive_member_name (&arch, &nested_arch);
21520 if (name == NULL)
21521 {
21522 error (_("%s: bad archive file name\n"), arch.file_name);
21523 ret = false;
21524 break;
21525 }
21526 namelen = strlen (name);
21527
21528 qualified_name = make_qualified_name (&arch, &nested_arch, name);
21529 if (qualified_name == NULL)
21530 {
21531 error (_("%s: bad archive file name\n"), arch.file_name);
21532 free (name);
21533 ret = false;
21534 break;
21535 }
21536
21537 if (is_thin_archive && arch.nested_member_origin == 0)
21538 {
21539 /* This is a proxy for an external member of a thin archive. */
21540 Filedata * member_filedata;
21541 char * member_file_name = adjust_relative_path
21542 (filedata->file_name, name, namelen);
21543
21544 free (name);
21545 if (member_file_name == NULL)
21546 {
21547 free (qualified_name);
21548 ret = false;
21549 break;
21550 }
21551
21552 member_filedata = open_file (member_file_name, false);
21553 if (member_filedata == NULL)
21554 {
21555 error (_("Input file '%s' is not readable.\n"), member_file_name);
21556 free (member_file_name);
21557 free (qualified_name);
21558 ret = false;
21559 break;
21560 }
21561
21562 filedata->archive_file_offset = arch.nested_member_origin;
21563 member_filedata->file_name = qualified_name;
21564
21565 if (! process_object (member_filedata))
21566 ret = false;
21567
21568 close_file (member_filedata);
21569 free (member_file_name);
21570 }
21571 else if (is_thin_archive)
21572 {
21573 Filedata thin_filedata;
21574
21575 memset (&thin_filedata, 0, sizeof (thin_filedata));
21576
21577 /* PR 15140: Allow for corrupt thin archives. */
21578 if (nested_arch.file == NULL)
21579 {
21580 error (_("%s: contains corrupt thin archive: %s\n"),
21581 qualified_name, name);
21582 free (qualified_name);
21583 free (name);
21584 ret = false;
21585 break;
21586 }
21587 free (name);
21588
21589 /* This is a proxy for a member of a nested archive. */
21590 filedata->archive_file_offset
21591 = arch.nested_member_origin + sizeof arch.arhdr;
21592
21593 /* The nested archive file will have been opened and setup by
21594 get_archive_member_name. */
21595 if (fseek (nested_arch.file, filedata->archive_file_offset,
21596 SEEK_SET) != 0)
21597 {
21598 error (_("%s: failed to seek to archive member.\n"),
21599 nested_arch.file_name);
21600 free (qualified_name);
21601 ret = false;
21602 break;
21603 }
21604
21605 thin_filedata.handle = nested_arch.file;
21606 thin_filedata.file_name = qualified_name;
21607
21608 if (! process_object (& thin_filedata))
21609 ret = false;
21610 }
21611 else
21612 {
21613 free (name);
21614 filedata->archive_file_offset = arch.next_arhdr_offset;
21615 filedata->file_name = qualified_name;
21616 if (! process_object (filedata))
21617 ret = false;
21618 arch.next_arhdr_offset += filedata->archive_file_size;
21619 /* Stop looping with "negative" archive_file_size. */
21620 if (arch.next_arhdr_offset < filedata->archive_file_size)
21621 arch.next_arhdr_offset = -1ul;
21622 }
21623
21624 free (qualified_name);
21625 }
21626
21627 out:
21628 if (nested_arch.file != NULL)
21629 fclose (nested_arch.file);
21630 release_archive (&nested_arch);
21631 release_archive (&arch);
21632
21633 return ret;
21634 }
21635
21636 static bool
21637 process_file (char * file_name)
21638 {
21639 Filedata * filedata = NULL;
21640 struct stat statbuf;
21641 char armag[SARMAG];
21642 bool ret = true;
21643
21644 if (stat (file_name, &statbuf) < 0)
21645 {
21646 if (errno == ENOENT)
21647 error (_("'%s': No such file\n"), file_name);
21648 else
21649 error (_("Could not locate '%s'. System error message: %s\n"),
21650 file_name, strerror (errno));
21651 return false;
21652 }
21653
21654 if (! S_ISREG (statbuf.st_mode))
21655 {
21656 error (_("'%s' is not an ordinary file\n"), file_name);
21657 return false;
21658 }
21659
21660 filedata = calloc (1, sizeof * filedata);
21661 if (filedata == NULL)
21662 {
21663 error (_("Out of memory allocating file data structure\n"));
21664 return false;
21665 }
21666
21667 filedata->file_name = file_name;
21668 filedata->handle = fopen (file_name, "rb");
21669 if (filedata->handle == NULL)
21670 {
21671 error (_("Input file '%s' is not readable.\n"), file_name);
21672 free (filedata);
21673 return false;
21674 }
21675
21676 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
21677 {
21678 error (_("%s: Failed to read file's magic number\n"), file_name);
21679 fclose (filedata->handle);
21680 free (filedata);
21681 return false;
21682 }
21683
21684 filedata->file_size = (bfd_size_type) statbuf.st_size;
21685 filedata->is_separate = false;
21686
21687 if (memcmp (armag, ARMAG, SARMAG) == 0)
21688 {
21689 if (! process_archive (filedata, false))
21690 ret = false;
21691 }
21692 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
21693 {
21694 if ( ! process_archive (filedata, true))
21695 ret = false;
21696 }
21697 else
21698 {
21699 if (do_archive_index && !check_all)
21700 error (_("File %s is not an archive so its index cannot be displayed.\n"),
21701 file_name);
21702
21703 rewind (filedata->handle);
21704 filedata->archive_file_size = filedata->archive_file_offset = 0;
21705
21706 if (! process_object (filedata))
21707 ret = false;
21708 }
21709
21710 fclose (filedata->handle);
21711 free (filedata->section_headers);
21712 free (filedata->program_headers);
21713 free (filedata->string_table);
21714 free (filedata->dump.dump_sects);
21715 free (filedata);
21716
21717 free (ba_cache.strtab);
21718 ba_cache.strtab = NULL;
21719 free (ba_cache.symtab);
21720 ba_cache.symtab = NULL;
21721 ba_cache.filedata = NULL;
21722
21723 return ret;
21724 }
21725
21726 #ifdef SUPPORT_DISASSEMBLY
21727 /* Needed by the i386 disassembler. For extra credit, someone could
21728 fix this so that we insert symbolic addresses here, esp for GOT/PLT
21729 symbols. */
21730
21731 void
21732 print_address (unsigned int addr, FILE * outfile)
21733 {
21734 fprintf (outfile,"0x%8.8x", addr);
21735 }
21736
21737 /* Needed by the i386 disassembler. */
21738
21739 void
21740 db_task_printsym (unsigned int addr)
21741 {
21742 print_address (addr, stderr);
21743 }
21744 #endif
21745
21746 int
21747 main (int argc, char ** argv)
21748 {
21749 int err;
21750
21751 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
21752 setlocale (LC_MESSAGES, "");
21753 #endif
21754 #if defined (HAVE_SETLOCALE)
21755 setlocale (LC_CTYPE, "");
21756 #endif
21757 bindtextdomain (PACKAGE, LOCALEDIR);
21758 textdomain (PACKAGE);
21759
21760 expandargv (&argc, &argv);
21761
21762 parse_args (& cmdline, argc, argv);
21763
21764 if (optind < (argc - 1))
21765 /* When displaying information for more than one file,
21766 prefix the information with the file name. */
21767 show_name = true;
21768 else if (optind >= argc)
21769 {
21770 /* Ensure that the warning is always displayed. */
21771 do_checks = true;
21772
21773 warn (_("Nothing to do.\n"));
21774 usage (stderr);
21775 }
21776
21777 err = false;
21778 while (optind < argc)
21779 if (! process_file (argv[optind++]))
21780 err = true;
21781
21782 free (cmdline.dump_sects);
21783
21784 free (dump_ctf_symtab_name);
21785 free (dump_ctf_strtab_name);
21786 free (dump_ctf_parent_name);
21787
21788 return err ? EXIT_FAILURE : EXIT_SUCCESS;
21789 }
This page took 0.463566 seconds and 5 git commands to generate.