bfd/
[binutils.git] / binutils / readelf.c
blob809712125bce094412db60cf654ebdbd268650f6
1 /* readelf.c -- display contents of an ELF format file
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009 Free Software Foundation, Inc.
5 Originally developed by Eric Youngdale <eric@andante.jic.com>
6 Modifications by Nick Clifton <nickc@redhat.com>
8 This file is part of GNU Binutils.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
25 /* The difference between readelf and objdump:
27 Both programs are capable of displaying the contents of ELF format files,
28 so why does the binutils project have two file dumpers ?
30 The reason is that objdump sees an ELF file through a BFD filter of the
31 world; if BFD has a bug where, say, it disagrees about a machine constant
32 in e_flags, then the odds are good that it will remain internally
33 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
34 GAS sees it the BFD way. There was need for a tool to go find out what
35 the file actually says.
37 This is why the readelf program does not link against the BFD library - it
38 exists as an independent program to help verify the correct working of BFD.
40 There is also the case that readelf can provide more information about an
41 ELF file than is provided by objdump. In particular it can display DWARF
42 debugging information which (at the moment) objdump cannot. */
44 #include "config.h"
45 #include "sysdep.h"
46 #include <assert.h>
47 #include <sys/stat.h>
48 #include <time.h>
49 #ifdef HAVE_ZLIB_H
50 #include <zlib.h>
51 #endif
53 #if __GNUC__ >= 2
54 /* Define BFD64 here, even if our default architecture is 32 bit ELF
55 as this will allow us to read in and parse 64bit and 32bit ELF files.
56 Only do this if we believe that the compiler can support a 64 bit
57 data type. For now we only rely on GCC being able to do this. */
58 #define BFD64
59 #endif
61 #include "bfd.h"
62 #include "bucomm.h"
63 #include "dwarf.h"
65 #include "elf/common.h"
66 #include "elf/external.h"
67 #include "elf/internal.h"
70 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
71 we can obtain the H8 reloc numbers. We need these for the
72 get_reloc_size() function. We include h8.h again after defining
73 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
75 #include "elf/h8.h"
76 #undef _ELF_H8_H
78 /* Undo the effects of #including reloc-macros.h. */
80 #undef START_RELOC_NUMBERS
81 #undef RELOC_NUMBER
82 #undef FAKE_RELOC
83 #undef EMPTY_RELOC
84 #undef END_RELOC_NUMBERS
85 #undef _RELOC_MACROS_H
87 /* The following headers use the elf/reloc-macros.h file to
88 automatically generate relocation recognition functions
89 such as elf_mips_reloc_type() */
91 #define RELOC_MACROS_GEN_FUNC
93 #include "elf/alpha.h"
94 #include "elf/arc.h"
95 #include "elf/arm.h"
96 #include "elf/avr.h"
97 #include "elf/bfin.h"
98 #include "elf/cr16.h"
99 #include "elf/cris.h"
100 #include "elf/crx.h"
101 #include "elf/d10v.h"
102 #include "elf/d30v.h"
103 #include "elf/dlx.h"
104 #include "elf/fr30.h"
105 #include "elf/frv.h"
106 #include "elf/h8.h"
107 #include "elf/hppa.h"
108 #include "elf/i386.h"
109 #include "elf/i370.h"
110 #include "elf/i860.h"
111 #include "elf/i960.h"
112 #include "elf/ia64.h"
113 #include "elf/ip2k.h"
114 #include "elf/lm32.h"
115 #include "elf/iq2000.h"
116 #include "elf/m32c.h"
117 #include "elf/m32r.h"
118 #include "elf/m68k.h"
119 #include "elf/m68hc11.h"
120 #include "elf/mcore.h"
121 #include "elf/mep.h"
122 #include "elf/mips.h"
123 #include "elf/mmix.h"
124 #include "elf/mn10200.h"
125 #include "elf/mn10300.h"
126 #include "elf/mt.h"
127 #include "elf/msp430.h"
128 #include "elf/or32.h"
129 #include "elf/pj.h"
130 #include "elf/ppc.h"
131 #include "elf/ppc64.h"
132 #include "elf/s390.h"
133 #include "elf/score.h"
134 #include "elf/sh.h"
135 #include "elf/sparc.h"
136 #include "elf/spu.h"
137 #include "elf/v850.h"
138 #include "elf/vax.h"
139 #include "elf/x86-64.h"
140 #include "elf/xstormy16.h"
141 #include "elf/xtensa.h"
143 #include "aout/ar.h"
145 #include "getopt.h"
146 #include "libiberty.h"
147 #include "safe-ctype.h"
148 #include "filenames.h"
150 char * program_name = "readelf";
151 int do_wide;
152 static long archive_file_offset;
153 static unsigned long archive_file_size;
154 static unsigned long dynamic_addr;
155 static bfd_size_type dynamic_size;
156 static unsigned int dynamic_nent;
157 static char * dynamic_strings;
158 static unsigned long dynamic_strings_length;
159 static char * string_table;
160 static unsigned long string_table_length;
161 static unsigned long num_dynamic_syms;
162 static Elf_Internal_Sym * dynamic_symbols;
163 static Elf_Internal_Syminfo * dynamic_syminfo;
164 static unsigned long dynamic_syminfo_offset;
165 static unsigned int dynamic_syminfo_nent;
166 static char program_interpreter[PATH_MAX];
167 static bfd_vma dynamic_info[DT_JMPREL + 1];
168 static bfd_vma dynamic_info_DT_GNU_HASH;
169 static bfd_vma version_info[16];
170 static Elf_Internal_Ehdr elf_header;
171 static Elf_Internal_Shdr * section_headers;
172 static Elf_Internal_Phdr * program_headers;
173 static Elf_Internal_Dyn * dynamic_section;
174 static Elf_Internal_Shdr * symtab_shndx_hdr;
175 static int show_name;
176 static int do_dynamic;
177 static int do_syms;
178 static int do_reloc;
179 static int do_sections;
180 static int do_section_groups;
181 static int do_section_details;
182 static int do_segments;
183 static int do_unwind;
184 static int do_using_dynamic;
185 static int do_header;
186 static int do_dump;
187 static int do_version;
188 static int do_histogram;
189 static int do_debugging;
190 static int do_arch;
191 static int do_notes;
192 static int do_archive_index;
193 static int is_32bit_elf;
195 struct group_list
197 struct group_list * next;
198 unsigned int section_index;
201 struct group
203 struct group_list * root;
204 unsigned int group_index;
207 static size_t group_count;
208 static struct group * section_groups;
209 static struct group ** section_headers_groups;
212 /* Flag bits indicating particular types of dump. */
213 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
214 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
215 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
216 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
217 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
219 typedef unsigned char dump_type;
221 /* A linked list of the section names for which dumps were requested. */
222 struct dump_list_entry
224 char * name;
225 dump_type type;
226 struct dump_list_entry * next;
228 static struct dump_list_entry * dump_sects_byname;
230 /* A dynamic array of flags indicating for which sections a dump
231 has been requested via command line switches. */
232 static dump_type * cmdline_dump_sects = NULL;
233 static unsigned int num_cmdline_dump_sects = 0;
235 /* A dynamic array of flags indicating for which sections a dump of
236 some kind has been requested. It is reset on a per-object file
237 basis and then initialised from the cmdline_dump_sects array,
238 the results of interpreting the -w switch, and the
239 dump_sects_byname list. */
240 static dump_type * dump_sects = NULL;
241 static unsigned int num_dump_sects = 0;
244 /* How to print a vma value. */
245 typedef enum print_mode
247 HEX,
248 DEC,
249 DEC_5,
250 UNSIGNED,
251 PREFIX_HEX,
252 FULL_HEX,
253 LONG_HEX
255 print_mode;
257 static void (* byte_put) (unsigned char *, bfd_vma, int);
259 #define UNKNOWN -1
261 #define SECTION_NAME(X) \
262 ((X) == NULL ? "<none>" \
263 : string_table == NULL ? "<no-name>" \
264 : ((X)->sh_name >= string_table_length ? "<corrupt>" \
265 : string_table + (X)->sh_name))
267 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
269 #define BYTE_GET(field) byte_get (field, sizeof (field))
271 #define GET_ELF_SYMBOLS(file, section) \
272 (is_32bit_elf ? get_32bit_elf_symbols (file, section) \
273 : get_64bit_elf_symbols (file, section))
275 #define VALID_DYNAMIC_NAME(offset) ((dynamic_strings != NULL) && (offset < dynamic_strings_length))
276 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
277 already been called and verified that the string exists. */
278 #define GET_DYNAMIC_NAME(offset) (dynamic_strings + offset)
280 /* This is just a bit of syntatic sugar. */
281 #define streq(a,b) (strcmp ((a), (b)) == 0)
282 #define strneq(a,b,n) (strncmp ((a), (b), (n)) == 0)
283 #define const_strneq(a,b) (strncmp ((a), (b), sizeof (b) - 1) == 0)
285 static void *
286 get_data (void * var, FILE * file, long offset, size_t size, size_t nmemb,
287 const char * reason)
289 void * mvar;
291 if (size == 0 || nmemb == 0)
292 return NULL;
294 if (fseek (file, archive_file_offset + offset, SEEK_SET))
296 error (_("Unable to seek to 0x%lx for %s\n"),
297 (unsigned long) archive_file_offset + offset, reason);
298 return NULL;
301 mvar = var;
302 if (mvar == NULL)
304 /* Check for overflow. */
305 if (nmemb < (~(size_t) 0 - 1) / size)
306 /* + 1 so that we can '\0' terminate invalid string table sections. */
307 mvar = malloc (size * nmemb + 1);
309 if (mvar == NULL)
311 error (_("Out of memory allocating 0x%lx bytes for %s\n"),
312 (unsigned long)(size * nmemb), reason);
313 return NULL;
316 ((char *) mvar)[size * nmemb] = '\0';
319 if (fread (mvar, size, nmemb, file) != nmemb)
321 error (_("Unable to read in 0x%lx bytes of %s\n"),
322 (unsigned long)(size * nmemb), reason);
323 if (mvar != var)
324 free (mvar);
325 return NULL;
328 return mvar;
331 static void
332 byte_put_little_endian (unsigned char * field, bfd_vma value, int size)
334 switch (size)
336 case 8:
337 field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
338 field[6] = ((value >> 24) >> 24) & 0xff;
339 field[5] = ((value >> 24) >> 16) & 0xff;
340 field[4] = ((value >> 24) >> 8) & 0xff;
341 /* Fall through. */
342 case 4:
343 field[3] = (value >> 24) & 0xff;
344 field[2] = (value >> 16) & 0xff;
345 /* Fall through. */
346 case 2:
347 field[1] = (value >> 8) & 0xff;
348 /* Fall through. */
349 case 1:
350 field[0] = value & 0xff;
351 break;
353 default:
354 error (_("Unhandled data length: %d\n"), size);
355 abort ();
359 /* Print a VMA value. */
361 static int
362 print_vma (bfd_vma vma, print_mode mode)
364 int nc = 0;
366 switch (mode)
368 case FULL_HEX:
369 nc = printf ("0x");
370 /* Drop through. */
372 case LONG_HEX:
373 #ifdef BFD64
374 if (is_32bit_elf)
375 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
376 #endif
377 printf_vma (vma);
378 return nc + 16;
380 case DEC_5:
381 if (vma <= 99999)
382 return printf ("%5" BFD_VMA_FMT "d", vma);
383 /* Drop through. */
385 case PREFIX_HEX:
386 nc = printf ("0x");
387 /* Drop through. */
389 case HEX:
390 return nc + printf ("%" BFD_VMA_FMT "x", vma);
392 case DEC:
393 return printf ("%" BFD_VMA_FMT "d", vma);
395 case UNSIGNED:
396 return printf ("%" BFD_VMA_FMT "u", vma);
398 return 0;
401 /* Display a symbol on stdout. Handles the display of non-printing characters.
403 If DO_WIDE is not true then format the symbol to be at most WIDTH characters,
404 truncating as necessary. If WIDTH is negative then format the string to be
405 exactly - WIDTH characters, truncating or padding as necessary.
407 Returns the number of emitted characters. */
409 static unsigned int
410 print_symbol (int width, const char * symbol)
412 const char * c;
413 bfd_boolean extra_padding = FALSE;
414 unsigned int num_printed = 0;
416 if (do_wide)
418 /* Set the width to a very large value. This simplifies the code below. */
419 width = INT_MAX;
421 else if (width < 0)
423 /* Keep the width positive. This also helps. */
424 width = - width;
425 extra_padding = TRUE;
428 while (width)
430 int len;
432 c = symbol;
434 /* Look for non-printing symbols inside the symbol's name.
435 This test is triggered in particular by the names generated
436 by the assembler for local labels. */
437 while (ISPRINT (* c))
438 c++;
440 len = c - symbol;
442 if (len)
444 if (len > width)
445 len = width;
447 printf ("%.*s", len, symbol);
449 width -= len;
450 num_printed += len;
453 if (* c == 0 || width == 0)
454 break;
456 /* Now display the non-printing character, if
457 there is room left in which to dipslay it. */
458 if (*c < 32)
460 if (width < 2)
461 break;
463 printf ("^%c", *c + 0x40);
465 width -= 2;
466 num_printed += 2;
468 else
470 if (width < 6)
471 break;
473 printf ("<0x%.2x>", *c);
475 width -= 6;
476 num_printed += 6;
479 symbol = c + 1;
482 if (extra_padding && width > 0)
484 /* Fill in the remaining spaces. */
485 printf ("%-*s", width, " ");
486 num_printed += 2;
489 return num_printed;
492 static void
493 byte_put_big_endian (unsigned char * field, bfd_vma value, int size)
495 switch (size)
497 case 8:
498 field[7] = value & 0xff;
499 field[6] = (value >> 8) & 0xff;
500 field[5] = (value >> 16) & 0xff;
501 field[4] = (value >> 24) & 0xff;
502 value >>= 16;
503 value >>= 16;
504 /* Fall through. */
505 case 4:
506 field[3] = value & 0xff;
507 field[2] = (value >> 8) & 0xff;
508 value >>= 16;
509 /* Fall through. */
510 case 2:
511 field[1] = value & 0xff;
512 value >>= 8;
513 /* Fall through. */
514 case 1:
515 field[0] = value & 0xff;
516 break;
518 default:
519 error (_("Unhandled data length: %d\n"), size);
520 abort ();
524 /* Return a pointer to section NAME, or NULL if no such section exists. */
526 static Elf_Internal_Shdr *
527 find_section (const char * name)
529 unsigned int i;
531 for (i = 0; i < elf_header.e_shnum; i++)
532 if (streq (SECTION_NAME (section_headers + i), name))
533 return section_headers + i;
535 return NULL;
538 /* Guess the relocation size commonly used by the specific machines. */
540 static int
541 guess_is_rela (unsigned int e_machine)
543 switch (e_machine)
545 /* Targets that use REL relocations. */
546 case EM_386:
547 case EM_486:
548 case EM_960:
549 case EM_ARM:
550 case EM_D10V:
551 case EM_CYGNUS_D10V:
552 case EM_DLX:
553 case EM_MIPS:
554 case EM_MIPS_RS3_LE:
555 case EM_CYGNUS_M32R:
556 case EM_OPENRISC:
557 case EM_OR32:
558 case EM_SCORE:
559 return FALSE;
561 /* Targets that use RELA relocations. */
562 case EM_68K:
563 case EM_860:
564 case EM_ALPHA:
565 case EM_ALTERA_NIOS2:
566 case EM_AVR:
567 case EM_AVR_OLD:
568 case EM_BLACKFIN:
569 case EM_CR16:
570 case EM_CR16_OLD:
571 case EM_CRIS:
572 case EM_CRX:
573 case EM_D30V:
574 case EM_CYGNUS_D30V:
575 case EM_FR30:
576 case EM_CYGNUS_FR30:
577 case EM_CYGNUS_FRV:
578 case EM_H8S:
579 case EM_H8_300:
580 case EM_H8_300H:
581 case EM_IA_64:
582 case EM_IP2K:
583 case EM_IP2K_OLD:
584 case EM_IQ2000:
585 case EM_LATTICEMICO32:
586 case EM_M32C_OLD:
587 case EM_M32C:
588 case EM_M32R:
589 case EM_MCORE:
590 case EM_CYGNUS_MEP:
591 case EM_MMIX:
592 case EM_MN10200:
593 case EM_CYGNUS_MN10200:
594 case EM_MN10300:
595 case EM_CYGNUS_MN10300:
596 case EM_MSP430:
597 case EM_MSP430_OLD:
598 case EM_MT:
599 case EM_NIOS32:
600 case EM_PPC64:
601 case EM_PPC:
602 case EM_S390:
603 case EM_S390_OLD:
604 case EM_SH:
605 case EM_SPARC:
606 case EM_SPARC32PLUS:
607 case EM_SPARCV9:
608 case EM_SPU:
609 case EM_V850:
610 case EM_CYGNUS_V850:
611 case EM_VAX:
612 case EM_X86_64:
613 case EM_L1OM:
614 case EM_XSTORMY16:
615 case EM_XTENSA:
616 case EM_XTENSA_OLD:
617 return TRUE;
619 case EM_68HC05:
620 case EM_68HC08:
621 case EM_68HC11:
622 case EM_68HC16:
623 case EM_FX66:
624 case EM_ME16:
625 case EM_MMA:
626 case EM_NCPU:
627 case EM_NDR1:
628 case EM_PCP:
629 case EM_ST100:
630 case EM_ST19:
631 case EM_ST7:
632 case EM_ST9PLUS:
633 case EM_STARCORE:
634 case EM_SVX:
635 case EM_TINYJ:
636 default:
637 warn (_("Don't know about relocations on this machine architecture\n"));
638 return FALSE;
642 static int
643 slurp_rela_relocs (FILE * file,
644 unsigned long rel_offset,
645 unsigned long rel_size,
646 Elf_Internal_Rela ** relasp,
647 unsigned long * nrelasp)
649 Elf_Internal_Rela * relas;
650 unsigned long nrelas;
651 unsigned int i;
653 if (is_32bit_elf)
655 Elf32_External_Rela * erelas;
657 erelas = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
658 if (!erelas)
659 return 0;
661 nrelas = rel_size / sizeof (Elf32_External_Rela);
663 relas = cmalloc (nrelas, sizeof (Elf_Internal_Rela));
665 if (relas == NULL)
667 free (erelas);
668 error (_("out of memory parsing relocs\n"));
669 return 0;
672 for (i = 0; i < nrelas; i++)
674 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
675 relas[i].r_info = BYTE_GET (erelas[i].r_info);
676 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
679 free (erelas);
681 else
683 Elf64_External_Rela * erelas;
685 erelas = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
686 if (!erelas)
687 return 0;
689 nrelas = rel_size / sizeof (Elf64_External_Rela);
691 relas = cmalloc (nrelas, sizeof (Elf_Internal_Rela));
693 if (relas == NULL)
695 free (erelas);
696 error (_("out of memory parsing relocs\n"));
697 return 0;
700 for (i = 0; i < nrelas; i++)
702 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
703 relas[i].r_info = BYTE_GET (erelas[i].r_info);
704 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
706 /* The #ifdef BFD64 below is to prevent a compile time
707 warning. We know that if we do not have a 64 bit data
708 type that we will never execute this code anyway. */
709 #ifdef BFD64
710 if (elf_header.e_machine == EM_MIPS
711 && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
713 /* In little-endian objects, r_info isn't really a
714 64-bit little-endian value: it has a 32-bit
715 little-endian symbol index followed by four
716 individual byte fields. Reorder INFO
717 accordingly. */
718 bfd_vma info = relas[i].r_info;
719 info = (((info & 0xffffffff) << 32)
720 | ((info >> 56) & 0xff)
721 | ((info >> 40) & 0xff00)
722 | ((info >> 24) & 0xff0000)
723 | ((info >> 8) & 0xff000000));
724 relas[i].r_info = info;
726 #endif /* BFD64 */
729 free (erelas);
731 *relasp = relas;
732 *nrelasp = nrelas;
733 return 1;
736 static int
737 slurp_rel_relocs (FILE * file,
738 unsigned long rel_offset,
739 unsigned long rel_size,
740 Elf_Internal_Rela ** relsp,
741 unsigned long * nrelsp)
743 Elf_Internal_Rela * rels;
744 unsigned long nrels;
745 unsigned int i;
747 if (is_32bit_elf)
749 Elf32_External_Rel * erels;
751 erels = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
752 if (!erels)
753 return 0;
755 nrels = rel_size / sizeof (Elf32_External_Rel);
757 rels = cmalloc (nrels, sizeof (Elf_Internal_Rela));
759 if (rels == NULL)
761 free (erels);
762 error (_("out of memory parsing relocs\n"));
763 return 0;
766 for (i = 0; i < nrels; i++)
768 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
769 rels[i].r_info = BYTE_GET (erels[i].r_info);
770 rels[i].r_addend = 0;
773 free (erels);
775 else
777 Elf64_External_Rel * erels;
779 erels = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
780 if (!erels)
781 return 0;
783 nrels = rel_size / sizeof (Elf64_External_Rel);
785 rels = cmalloc (nrels, sizeof (Elf_Internal_Rela));
787 if (rels == NULL)
789 free (erels);
790 error (_("out of memory parsing relocs\n"));
791 return 0;
794 for (i = 0; i < nrels; i++)
796 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
797 rels[i].r_info = BYTE_GET (erels[i].r_info);
798 rels[i].r_addend = 0;
800 /* The #ifdef BFD64 below is to prevent a compile time
801 warning. We know that if we do not have a 64 bit data
802 type that we will never execute this code anyway. */
803 #ifdef BFD64
804 if (elf_header.e_machine == EM_MIPS
805 && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
807 /* In little-endian objects, r_info isn't really a
808 64-bit little-endian value: it has a 32-bit
809 little-endian symbol index followed by four
810 individual byte fields. Reorder INFO
811 accordingly. */
812 bfd_vma info = rels[i].r_info;
813 info = (((info & 0xffffffff) << 32)
814 | ((info >> 56) & 0xff)
815 | ((info >> 40) & 0xff00)
816 | ((info >> 24) & 0xff0000)
817 | ((info >> 8) & 0xff000000));
818 rels[i].r_info = info;
820 #endif /* BFD64 */
823 free (erels);
825 *relsp = rels;
826 *nrelsp = nrels;
827 return 1;
830 /* Returns the reloc type extracted from the reloc info field. */
832 static unsigned int
833 get_reloc_type (bfd_vma reloc_info)
835 if (is_32bit_elf)
836 return ELF32_R_TYPE (reloc_info);
838 switch (elf_header.e_machine)
840 case EM_MIPS:
841 /* Note: We assume that reloc_info has already been adjusted for us. */
842 return ELF64_MIPS_R_TYPE (reloc_info);
844 case EM_SPARCV9:
845 return ELF64_R_TYPE_ID (reloc_info);
847 default:
848 return ELF64_R_TYPE (reloc_info);
852 /* Return the symbol index extracted from the reloc info field. */
854 static bfd_vma
855 get_reloc_symindex (bfd_vma reloc_info)
857 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
860 /* Display the contents of the relocation data found at the specified
861 offset. */
863 static void
864 dump_relocations (FILE * file,
865 unsigned long rel_offset,
866 unsigned long rel_size,
867 Elf_Internal_Sym * symtab,
868 unsigned long nsyms,
869 char * strtab,
870 unsigned long strtablen,
871 int is_rela)
873 unsigned int i;
874 Elf_Internal_Rela * rels;
876 if (is_rela == UNKNOWN)
877 is_rela = guess_is_rela (elf_header.e_machine);
879 if (is_rela)
881 if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
882 return;
884 else
886 if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
887 return;
890 if (is_32bit_elf)
892 if (is_rela)
894 if (do_wide)
895 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
896 else
897 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
899 else
901 if (do_wide)
902 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
903 else
904 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
907 else
909 if (is_rela)
911 if (do_wide)
912 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
913 else
914 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
916 else
918 if (do_wide)
919 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
920 else
921 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
925 for (i = 0; i < rel_size; i++)
927 const char * rtype;
928 bfd_vma offset;
929 bfd_vma info;
930 bfd_vma symtab_index;
931 bfd_vma type;
933 offset = rels[i].r_offset;
934 info = rels[i].r_info;
936 type = get_reloc_type (info);
937 symtab_index = get_reloc_symindex (info);
939 if (is_32bit_elf)
941 printf ("%8.8lx %8.8lx ",
942 (unsigned long) offset & 0xffffffff,
943 (unsigned long) info & 0xffffffff);
945 else
947 #if BFD_HOST_64BIT_LONG
948 printf (do_wide
949 ? "%16.16lx %16.16lx "
950 : "%12.12lx %12.12lx ",
951 offset, info);
952 #elif BFD_HOST_64BIT_LONG_LONG
953 #ifndef __MSVCRT__
954 printf (do_wide
955 ? "%16.16llx %16.16llx "
956 : "%12.12llx %12.12llx ",
957 offset, info);
958 #else
959 printf (do_wide
960 ? "%16.16I64x %16.16I64x "
961 : "%12.12I64x %12.12I64x ",
962 offset, info);
963 #endif
964 #else
965 printf (do_wide
966 ? "%8.8lx%8.8lx %8.8lx%8.8lx "
967 : "%4.4lx%8.8lx %4.4lx%8.8lx ",
968 _bfd_int64_high (offset),
969 _bfd_int64_low (offset),
970 _bfd_int64_high (info),
971 _bfd_int64_low (info));
972 #endif
975 switch (elf_header.e_machine)
977 default:
978 rtype = NULL;
979 break;
981 case EM_M32R:
982 case EM_CYGNUS_M32R:
983 rtype = elf_m32r_reloc_type (type);
984 break;
986 case EM_386:
987 case EM_486:
988 rtype = elf_i386_reloc_type (type);
989 break;
991 case EM_68HC11:
992 case EM_68HC12:
993 rtype = elf_m68hc11_reloc_type (type);
994 break;
996 case EM_68K:
997 rtype = elf_m68k_reloc_type (type);
998 break;
1000 case EM_960:
1001 rtype = elf_i960_reloc_type (type);
1002 break;
1004 case EM_AVR:
1005 case EM_AVR_OLD:
1006 rtype = elf_avr_reloc_type (type);
1007 break;
1009 case EM_OLD_SPARCV9:
1010 case EM_SPARC32PLUS:
1011 case EM_SPARCV9:
1012 case EM_SPARC:
1013 rtype = elf_sparc_reloc_type (type);
1014 break;
1016 case EM_SPU:
1017 rtype = elf_spu_reloc_type (type);
1018 break;
1020 case EM_V850:
1021 case EM_CYGNUS_V850:
1022 rtype = v850_reloc_type (type);
1023 break;
1025 case EM_D10V:
1026 case EM_CYGNUS_D10V:
1027 rtype = elf_d10v_reloc_type (type);
1028 break;
1030 case EM_D30V:
1031 case EM_CYGNUS_D30V:
1032 rtype = elf_d30v_reloc_type (type);
1033 break;
1035 case EM_DLX:
1036 rtype = elf_dlx_reloc_type (type);
1037 break;
1039 case EM_SH:
1040 rtype = elf_sh_reloc_type (type);
1041 break;
1043 case EM_MN10300:
1044 case EM_CYGNUS_MN10300:
1045 rtype = elf_mn10300_reloc_type (type);
1046 break;
1048 case EM_MN10200:
1049 case EM_CYGNUS_MN10200:
1050 rtype = elf_mn10200_reloc_type (type);
1051 break;
1053 case EM_FR30:
1054 case EM_CYGNUS_FR30:
1055 rtype = elf_fr30_reloc_type (type);
1056 break;
1058 case EM_CYGNUS_FRV:
1059 rtype = elf_frv_reloc_type (type);
1060 break;
1062 case EM_MCORE:
1063 rtype = elf_mcore_reloc_type (type);
1064 break;
1066 case EM_MMIX:
1067 rtype = elf_mmix_reloc_type (type);
1068 break;
1070 case EM_MSP430:
1071 case EM_MSP430_OLD:
1072 rtype = elf_msp430_reloc_type (type);
1073 break;
1075 case EM_PPC:
1076 rtype = elf_ppc_reloc_type (type);
1077 break;
1079 case EM_PPC64:
1080 rtype = elf_ppc64_reloc_type (type);
1081 break;
1083 case EM_MIPS:
1084 case EM_MIPS_RS3_LE:
1085 rtype = elf_mips_reloc_type (type);
1086 break;
1088 case EM_ALPHA:
1089 rtype = elf_alpha_reloc_type (type);
1090 break;
1092 case EM_ARM:
1093 rtype = elf_arm_reloc_type (type);
1094 break;
1096 case EM_ARC:
1097 rtype = elf_arc_reloc_type (type);
1098 break;
1100 case EM_PARISC:
1101 rtype = elf_hppa_reloc_type (type);
1102 break;
1104 case EM_H8_300:
1105 case EM_H8_300H:
1106 case EM_H8S:
1107 rtype = elf_h8_reloc_type (type);
1108 break;
1110 case EM_OPENRISC:
1111 case EM_OR32:
1112 rtype = elf_or32_reloc_type (type);
1113 break;
1115 case EM_PJ:
1116 case EM_PJ_OLD:
1117 rtype = elf_pj_reloc_type (type);
1118 break;
1119 case EM_IA_64:
1120 rtype = elf_ia64_reloc_type (type);
1121 break;
1123 case EM_CRIS:
1124 rtype = elf_cris_reloc_type (type);
1125 break;
1127 case EM_860:
1128 rtype = elf_i860_reloc_type (type);
1129 break;
1131 case EM_X86_64:
1132 case EM_L1OM:
1133 rtype = elf_x86_64_reloc_type (type);
1134 break;
1136 case EM_S370:
1137 rtype = i370_reloc_type (type);
1138 break;
1140 case EM_S390_OLD:
1141 case EM_S390:
1142 rtype = elf_s390_reloc_type (type);
1143 break;
1145 case EM_SCORE:
1146 rtype = elf_score_reloc_type (type);
1147 break;
1149 case EM_XSTORMY16:
1150 rtype = elf_xstormy16_reloc_type (type);
1151 break;
1153 case EM_CRX:
1154 rtype = elf_crx_reloc_type (type);
1155 break;
1157 case EM_VAX:
1158 rtype = elf_vax_reloc_type (type);
1159 break;
1161 case EM_IP2K:
1162 case EM_IP2K_OLD:
1163 rtype = elf_ip2k_reloc_type (type);
1164 break;
1166 case EM_IQ2000:
1167 rtype = elf_iq2000_reloc_type (type);
1168 break;
1170 case EM_XTENSA_OLD:
1171 case EM_XTENSA:
1172 rtype = elf_xtensa_reloc_type (type);
1173 break;
1175 case EM_LATTICEMICO32:
1176 rtype = elf_lm32_reloc_type (type);
1177 break;
1179 case EM_M32C_OLD:
1180 case EM_M32C:
1181 rtype = elf_m32c_reloc_type (type);
1182 break;
1184 case EM_MT:
1185 rtype = elf_mt_reloc_type (type);
1186 break;
1188 case EM_BLACKFIN:
1189 rtype = elf_bfin_reloc_type (type);
1190 break;
1192 case EM_CYGNUS_MEP:
1193 rtype = elf_mep_reloc_type (type);
1194 break;
1196 case EM_CR16:
1197 case EM_CR16_OLD:
1198 rtype = elf_cr16_reloc_type (type);
1199 break;
1202 if (rtype == NULL)
1203 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1204 else
1205 printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1207 if (elf_header.e_machine == EM_ALPHA
1208 && rtype != NULL
1209 && streq (rtype, "R_ALPHA_LITUSE")
1210 && is_rela)
1212 switch (rels[i].r_addend)
1214 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1215 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1216 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1217 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1218 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1219 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1220 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1221 default: rtype = NULL;
1223 if (rtype)
1224 printf (" (%s)", rtype);
1225 else
1227 putchar (' ');
1228 printf (_("<unknown addend: %lx>"),
1229 (unsigned long) rels[i].r_addend);
1232 else if (symtab_index)
1234 if (symtab == NULL || symtab_index >= nsyms)
1235 printf (" bad symbol index: %08lx", (unsigned long) symtab_index);
1236 else
1238 Elf_Internal_Sym * psym;
1240 psym = symtab + symtab_index;
1242 printf (" ");
1244 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1246 const char * name;
1247 unsigned int len;
1248 unsigned int width = is_32bit_elf ? 8 : 14;
1250 /* Relocations against GNU_IFUNC symbols do not use the value
1251 of the symbol as the address to relocate against. Instead
1252 they invoke the function named by the symbol and use its
1253 result as the address for relocation.
1255 To indicate this to the user, do not display the value of
1256 the symbol in the "Symbols's Value" field. Instead show
1257 its name followed by () as a hint that the symbol is
1258 invoked. */
1260 if (strtab == NULL
1261 || psym->st_name == 0
1262 || psym->st_name >= strtablen)
1263 name = "??";
1264 else
1265 name = strtab + psym->st_name;
1267 len = print_symbol (width, name);
1268 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1270 else
1272 print_vma (psym->st_value, LONG_HEX);
1274 printf (is_32bit_elf ? " " : " ");
1277 if (psym->st_name == 0)
1279 const char * sec_name = "<null>";
1280 char name_buf[40];
1282 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1284 if (psym->st_shndx < elf_header.e_shnum)
1285 sec_name
1286 = SECTION_NAME (section_headers + psym->st_shndx);
1287 else if (psym->st_shndx == SHN_ABS)
1288 sec_name = "ABS";
1289 else if (psym->st_shndx == SHN_COMMON)
1290 sec_name = "COMMON";
1291 else if (elf_header.e_machine == EM_MIPS
1292 && psym->st_shndx == SHN_MIPS_SCOMMON)
1293 sec_name = "SCOMMON";
1294 else if (elf_header.e_machine == EM_MIPS
1295 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1296 sec_name = "SUNDEF";
1297 else if ((elf_header.e_machine == EM_X86_64
1298 || elf_header.e_machine == EM_L1OM)
1299 && psym->st_shndx == SHN_X86_64_LCOMMON)
1300 sec_name = "LARGE_COMMON";
1301 else if (elf_header.e_machine == EM_IA_64
1302 && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1303 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1304 sec_name = "ANSI_COM";
1305 else if (elf_header.e_machine == EM_IA_64
1306 && (elf_header.e_ident[EI_OSABI]
1307 == ELFOSABI_OPENVMS)
1308 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1309 sec_name = "VMS_SYMVEC";
1310 else
1312 sprintf (name_buf, "<section 0x%x>",
1313 (unsigned int) psym->st_shndx);
1314 sec_name = name_buf;
1317 print_symbol (22, sec_name);
1319 else if (strtab == NULL)
1320 printf (_("<string table index: %3ld>"), psym->st_name);
1321 else if (psym->st_name >= strtablen)
1322 printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1323 else
1324 print_symbol (22, strtab + psym->st_name);
1326 if (is_rela)
1328 long offset = (long) (bfd_signed_vma) rels[i].r_addend;
1330 if (offset < 0)
1331 printf (" - %lx", - offset);
1332 else
1333 printf (" + %lx", offset);
1337 else if (is_rela)
1339 printf ("%*c", is_32bit_elf ?
1340 (do_wide ? 34 : 28) : (do_wide ? 26 : 20), ' ');
1341 print_vma (rels[i].r_addend, LONG_HEX);
1344 if (elf_header.e_machine == EM_SPARCV9
1345 && rtype != NULL
1346 && streq (rtype, "R_SPARC_OLO10"))
1347 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (info));
1349 putchar ('\n');
1351 #ifdef BFD64
1352 if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1354 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (info);
1355 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (info);
1356 const char * rtype2 = elf_mips_reloc_type (type2);
1357 const char * rtype3 = elf_mips_reloc_type (type3);
1359 printf (" Type2: ");
1361 if (rtype2 == NULL)
1362 printf (_("unrecognized: %-7lx"),
1363 (unsigned long) type2 & 0xffffffff);
1364 else
1365 printf ("%-17.17s", rtype2);
1367 printf ("\n Type3: ");
1369 if (rtype3 == NULL)
1370 printf (_("unrecognized: %-7lx"),
1371 (unsigned long) type3 & 0xffffffff);
1372 else
1373 printf ("%-17.17s", rtype3);
1375 putchar ('\n');
1377 #endif /* BFD64 */
1380 free (rels);
1383 static const char *
1384 get_mips_dynamic_type (unsigned long type)
1386 switch (type)
1388 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1389 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1390 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1391 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1392 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1393 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1394 case DT_MIPS_MSYM: return "MIPS_MSYM";
1395 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1396 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1397 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1398 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1399 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1400 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1401 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1402 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1403 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1404 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1405 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1406 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1407 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1408 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1409 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1410 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1411 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1412 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1413 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1414 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1415 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1416 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1417 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1418 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1419 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1420 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1421 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1422 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1423 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1424 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1425 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1426 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1427 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1428 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1429 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1430 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1431 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1432 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1433 default:
1434 return NULL;
1438 static const char *
1439 get_sparc64_dynamic_type (unsigned long type)
1441 switch (type)
1443 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1444 default:
1445 return NULL;
1449 static const char *
1450 get_ppc_dynamic_type (unsigned long type)
1452 switch (type)
1454 case DT_PPC_GOT: return "PPC_GOT";
1455 default:
1456 return NULL;
1460 static const char *
1461 get_ppc64_dynamic_type (unsigned long type)
1463 switch (type)
1465 case DT_PPC64_GLINK: return "PPC64_GLINK";
1466 case DT_PPC64_OPD: return "PPC64_OPD";
1467 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1468 default:
1469 return NULL;
1473 static const char *
1474 get_parisc_dynamic_type (unsigned long type)
1476 switch (type)
1478 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1479 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1480 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1481 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1482 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1483 case DT_HP_PREINIT: return "HP_PREINIT";
1484 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1485 case DT_HP_NEEDED: return "HP_NEEDED";
1486 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1487 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1488 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1489 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1490 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
1491 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
1492 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
1493 case DT_HP_FILTERED: return "HP_FILTERED";
1494 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
1495 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1496 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
1497 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
1498 case DT_PLT: return "PLT";
1499 case DT_PLT_SIZE: return "PLT_SIZE";
1500 case DT_DLT: return "DLT";
1501 case DT_DLT_SIZE: return "DLT_SIZE";
1502 default:
1503 return NULL;
1507 static const char *
1508 get_ia64_dynamic_type (unsigned long type)
1510 switch (type)
1512 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
1513 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
1514 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
1515 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
1516 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1517 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
1518 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
1519 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
1520 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
1521 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
1522 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
1523 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
1524 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
1525 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
1526 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
1527 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
1528 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
1529 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
1530 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
1531 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
1532 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
1533 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
1534 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
1535 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
1536 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
1537 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
1538 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
1539 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
1540 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
1541 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
1542 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
1543 default:
1544 return NULL;
1548 static const char *
1549 get_alpha_dynamic_type (unsigned long type)
1551 switch (type)
1553 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1554 default:
1555 return NULL;
1559 static const char *
1560 get_score_dynamic_type (unsigned long type)
1562 switch (type)
1564 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1565 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
1566 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
1567 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
1568 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
1569 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
1570 default:
1571 return NULL;
1576 static const char *
1577 get_dynamic_type (unsigned long type)
1579 static char buff[64];
1581 switch (type)
1583 case DT_NULL: return "NULL";
1584 case DT_NEEDED: return "NEEDED";
1585 case DT_PLTRELSZ: return "PLTRELSZ";
1586 case DT_PLTGOT: return "PLTGOT";
1587 case DT_HASH: return "HASH";
1588 case DT_STRTAB: return "STRTAB";
1589 case DT_SYMTAB: return "SYMTAB";
1590 case DT_RELA: return "RELA";
1591 case DT_RELASZ: return "RELASZ";
1592 case DT_RELAENT: return "RELAENT";
1593 case DT_STRSZ: return "STRSZ";
1594 case DT_SYMENT: return "SYMENT";
1595 case DT_INIT: return "INIT";
1596 case DT_FINI: return "FINI";
1597 case DT_SONAME: return "SONAME";
1598 case DT_RPATH: return "RPATH";
1599 case DT_SYMBOLIC: return "SYMBOLIC";
1600 case DT_REL: return "REL";
1601 case DT_RELSZ: return "RELSZ";
1602 case DT_RELENT: return "RELENT";
1603 case DT_PLTREL: return "PLTREL";
1604 case DT_DEBUG: return "DEBUG";
1605 case DT_TEXTREL: return "TEXTREL";
1606 case DT_JMPREL: return "JMPREL";
1607 case DT_BIND_NOW: return "BIND_NOW";
1608 case DT_INIT_ARRAY: return "INIT_ARRAY";
1609 case DT_FINI_ARRAY: return "FINI_ARRAY";
1610 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1611 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1612 case DT_RUNPATH: return "RUNPATH";
1613 case DT_FLAGS: return "FLAGS";
1615 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1616 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1618 case DT_CHECKSUM: return "CHECKSUM";
1619 case DT_PLTPADSZ: return "PLTPADSZ";
1620 case DT_MOVEENT: return "MOVEENT";
1621 case DT_MOVESZ: return "MOVESZ";
1622 case DT_FEATURE: return "FEATURE";
1623 case DT_POSFLAG_1: return "POSFLAG_1";
1624 case DT_SYMINSZ: return "SYMINSZ";
1625 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
1627 case DT_ADDRRNGLO: return "ADDRRNGLO";
1628 case DT_CONFIG: return "CONFIG";
1629 case DT_DEPAUDIT: return "DEPAUDIT";
1630 case DT_AUDIT: return "AUDIT";
1631 case DT_PLTPAD: return "PLTPAD";
1632 case DT_MOVETAB: return "MOVETAB";
1633 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
1635 case DT_VERSYM: return "VERSYM";
1637 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
1638 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
1639 case DT_RELACOUNT: return "RELACOUNT";
1640 case DT_RELCOUNT: return "RELCOUNT";
1641 case DT_FLAGS_1: return "FLAGS_1";
1642 case DT_VERDEF: return "VERDEF";
1643 case DT_VERDEFNUM: return "VERDEFNUM";
1644 case DT_VERNEED: return "VERNEED";
1645 case DT_VERNEEDNUM: return "VERNEEDNUM";
1647 case DT_AUXILIARY: return "AUXILIARY";
1648 case DT_USED: return "USED";
1649 case DT_FILTER: return "FILTER";
1651 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1652 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1653 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1654 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1655 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1656 case DT_GNU_HASH: return "GNU_HASH";
1658 default:
1659 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1661 const char * result;
1663 switch (elf_header.e_machine)
1665 case EM_MIPS:
1666 case EM_MIPS_RS3_LE:
1667 result = get_mips_dynamic_type (type);
1668 break;
1669 case EM_SPARCV9:
1670 result = get_sparc64_dynamic_type (type);
1671 break;
1672 case EM_PPC:
1673 result = get_ppc_dynamic_type (type);
1674 break;
1675 case EM_PPC64:
1676 result = get_ppc64_dynamic_type (type);
1677 break;
1678 case EM_IA_64:
1679 result = get_ia64_dynamic_type (type);
1680 break;
1681 case EM_ALPHA:
1682 result = get_alpha_dynamic_type (type);
1683 break;
1684 case EM_SCORE:
1685 result = get_score_dynamic_type (type);
1686 break;
1687 default:
1688 result = NULL;
1689 break;
1692 if (result != NULL)
1693 return result;
1695 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
1697 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
1698 || (elf_header.e_machine == EM_PARISC
1699 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
1701 const char * result;
1703 switch (elf_header.e_machine)
1705 case EM_PARISC:
1706 result = get_parisc_dynamic_type (type);
1707 break;
1708 case EM_IA_64:
1709 result = get_ia64_dynamic_type (type);
1710 break;
1711 default:
1712 result = NULL;
1713 break;
1716 if (result != NULL)
1717 return result;
1719 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
1720 type);
1722 else
1723 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
1725 return buff;
1729 static char *
1730 get_file_type (unsigned e_type)
1732 static char buff[32];
1734 switch (e_type)
1736 case ET_NONE: return _("NONE (None)");
1737 case ET_REL: return _("REL (Relocatable file)");
1738 case ET_EXEC: return _("EXEC (Executable file)");
1739 case ET_DYN: return _("DYN (Shared object file)");
1740 case ET_CORE: return _("CORE (Core file)");
1742 default:
1743 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
1744 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
1745 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
1746 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
1747 else
1748 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
1749 return buff;
1753 static char *
1754 get_machine_name (unsigned e_machine)
1756 static char buff[64]; /* XXX */
1758 switch (e_machine)
1760 case EM_NONE: return _("None");
1761 case EM_M32: return "WE32100";
1762 case EM_SPARC: return "Sparc";
1763 case EM_SPU: return "SPU";
1764 case EM_386: return "Intel 80386";
1765 case EM_68K: return "MC68000";
1766 case EM_88K: return "MC88000";
1767 case EM_486: return "Intel 80486";
1768 case EM_860: return "Intel 80860";
1769 case EM_MIPS: return "MIPS R3000";
1770 case EM_S370: return "IBM System/370";
1771 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
1772 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
1773 case EM_PARISC: return "HPPA";
1774 case EM_PPC_OLD: return "Power PC (old)";
1775 case EM_SPARC32PLUS: return "Sparc v8+" ;
1776 case EM_960: return "Intel 90860";
1777 case EM_PPC: return "PowerPC";
1778 case EM_PPC64: return "PowerPC64";
1779 case EM_V800: return "NEC V800";
1780 case EM_FR20: return "Fujitsu FR20";
1781 case EM_RH32: return "TRW RH32";
1782 case EM_MCORE: return "MCORE";
1783 case EM_ARM: return "ARM";
1784 case EM_OLD_ALPHA: return "Digital Alpha (old)";
1785 case EM_SH: return "Renesas / SuperH SH";
1786 case EM_SPARCV9: return "Sparc v9";
1787 case EM_TRICORE: return "Siemens Tricore";
1788 case EM_ARC: return "ARC";
1789 case EM_H8_300: return "Renesas H8/300";
1790 case EM_H8_300H: return "Renesas H8/300H";
1791 case EM_H8S: return "Renesas H8S";
1792 case EM_H8_500: return "Renesas H8/500";
1793 case EM_IA_64: return "Intel IA-64";
1794 case EM_MIPS_X: return "Stanford MIPS-X";
1795 case EM_COLDFIRE: return "Motorola Coldfire";
1796 case EM_68HC12: return "Motorola M68HC12";
1797 case EM_ALPHA: return "Alpha";
1798 case EM_CYGNUS_D10V:
1799 case EM_D10V: return "d10v";
1800 case EM_CYGNUS_D30V:
1801 case EM_D30V: return "d30v";
1802 case EM_CYGNUS_M32R:
1803 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
1804 case EM_CYGNUS_V850:
1805 case EM_V850: return "NEC v850";
1806 case EM_CYGNUS_MN10300:
1807 case EM_MN10300: return "mn10300";
1808 case EM_CYGNUS_MN10200:
1809 case EM_MN10200: return "mn10200";
1810 case EM_CYGNUS_FR30:
1811 case EM_FR30: return "Fujitsu FR30";
1812 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
1813 case EM_PJ_OLD:
1814 case EM_PJ: return "picoJava";
1815 case EM_MMA: return "Fujitsu Multimedia Accelerator";
1816 case EM_PCP: return "Siemens PCP";
1817 case EM_NCPU: return "Sony nCPU embedded RISC processor";
1818 case EM_NDR1: return "Denso NDR1 microprocesspr";
1819 case EM_STARCORE: return "Motorola Star*Core processor";
1820 case EM_ME16: return "Toyota ME16 processor";
1821 case EM_ST100: return "STMicroelectronics ST100 processor";
1822 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
1823 case EM_FX66: return "Siemens FX66 microcontroller";
1824 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
1825 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
1826 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
1827 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
1828 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
1829 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
1830 case EM_SVX: return "Silicon Graphics SVx";
1831 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
1832 case EM_VAX: return "Digital VAX";
1833 case EM_AVR_OLD:
1834 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
1835 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
1836 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
1837 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
1838 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
1839 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
1840 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
1841 case EM_PRISM: return "Vitesse Prism";
1842 case EM_X86_64: return "Advanced Micro Devices X86-64";
1843 case EM_L1OM: return "Intel L1OM";
1844 case EM_S390_OLD:
1845 case EM_S390: return "IBM S/390";
1846 case EM_SCORE: return "SUNPLUS S+Core";
1847 case EM_XSTORMY16: return "Sanyo Xstormy16 CPU core";
1848 case EM_OPENRISC:
1849 case EM_OR32: return "OpenRISC";
1850 case EM_CRX: return "National Semiconductor CRX microprocessor";
1851 case EM_DLX: return "OpenDLX";
1852 case EM_IP2K_OLD:
1853 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
1854 case EM_IQ2000: return "Vitesse IQ2000";
1855 case EM_XTENSA_OLD:
1856 case EM_XTENSA: return "Tensilica Xtensa Processor";
1857 case EM_LATTICEMICO32: return "Lattice Mico32";
1858 case EM_M32C_OLD:
1859 case EM_M32C: return "Renesas M32c";
1860 case EM_MT: return "Morpho Techologies MT processor";
1861 case EM_BLACKFIN: return "Analog Devices Blackfin";
1862 case EM_NIOS32: return "Altera Nios";
1863 case EM_ALTERA_NIOS2: return "Altera Nios II";
1864 case EM_XC16X: return "Infineon Technologies xc16x";
1865 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
1866 case EM_CR16:
1867 case EM_CR16_OLD: return "National Semiconductor's CR16";
1868 default:
1869 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
1870 return buff;
1874 static void
1875 decode_ARM_machine_flags (unsigned e_flags, char buf[])
1877 unsigned eabi;
1878 int unknown = 0;
1880 eabi = EF_ARM_EABI_VERSION (e_flags);
1881 e_flags &= ~ EF_ARM_EABIMASK;
1883 /* Handle "generic" ARM flags. */
1884 if (e_flags & EF_ARM_RELEXEC)
1886 strcat (buf, ", relocatable executable");
1887 e_flags &= ~ EF_ARM_RELEXEC;
1890 if (e_flags & EF_ARM_HASENTRY)
1892 strcat (buf, ", has entry point");
1893 e_flags &= ~ EF_ARM_HASENTRY;
1896 /* Now handle EABI specific flags. */
1897 switch (eabi)
1899 default:
1900 strcat (buf, ", <unrecognized EABI>");
1901 if (e_flags)
1902 unknown = 1;
1903 break;
1905 case EF_ARM_EABI_VER1:
1906 strcat (buf, ", Version1 EABI");
1907 while (e_flags)
1909 unsigned flag;
1911 /* Process flags one bit at a time. */
1912 flag = e_flags & - e_flags;
1913 e_flags &= ~ flag;
1915 switch (flag)
1917 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1918 strcat (buf, ", sorted symbol tables");
1919 break;
1921 default:
1922 unknown = 1;
1923 break;
1926 break;
1928 case EF_ARM_EABI_VER2:
1929 strcat (buf, ", Version2 EABI");
1930 while (e_flags)
1932 unsigned flag;
1934 /* Process flags one bit at a time. */
1935 flag = e_flags & - e_flags;
1936 e_flags &= ~ flag;
1938 switch (flag)
1940 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1941 strcat (buf, ", sorted symbol tables");
1942 break;
1944 case EF_ARM_DYNSYMSUSESEGIDX:
1945 strcat (buf, ", dynamic symbols use segment index");
1946 break;
1948 case EF_ARM_MAPSYMSFIRST:
1949 strcat (buf, ", mapping symbols precede others");
1950 break;
1952 default:
1953 unknown = 1;
1954 break;
1957 break;
1959 case EF_ARM_EABI_VER3:
1960 strcat (buf, ", Version3 EABI");
1961 break;
1963 case EF_ARM_EABI_VER4:
1964 strcat (buf, ", Version4 EABI");
1965 goto eabi;
1967 case EF_ARM_EABI_VER5:
1968 strcat (buf, ", Version5 EABI");
1969 eabi:
1970 while (e_flags)
1972 unsigned flag;
1974 /* Process flags one bit at a time. */
1975 flag = e_flags & - e_flags;
1976 e_flags &= ~ flag;
1978 switch (flag)
1980 case EF_ARM_BE8:
1981 strcat (buf, ", BE8");
1982 break;
1984 case EF_ARM_LE8:
1985 strcat (buf, ", LE8");
1986 break;
1988 default:
1989 unknown = 1;
1990 break;
1993 break;
1995 case EF_ARM_EABI_UNKNOWN:
1996 strcat (buf, ", GNU EABI");
1997 while (e_flags)
1999 unsigned flag;
2001 /* Process flags one bit at a time. */
2002 flag = e_flags & - e_flags;
2003 e_flags &= ~ flag;
2005 switch (flag)
2007 case EF_ARM_INTERWORK:
2008 strcat (buf, ", interworking enabled");
2009 break;
2011 case EF_ARM_APCS_26:
2012 strcat (buf, ", uses APCS/26");
2013 break;
2015 case EF_ARM_APCS_FLOAT:
2016 strcat (buf, ", uses APCS/float");
2017 break;
2019 case EF_ARM_PIC:
2020 strcat (buf, ", position independent");
2021 break;
2023 case EF_ARM_ALIGN8:
2024 strcat (buf, ", 8 bit structure alignment");
2025 break;
2027 case EF_ARM_NEW_ABI:
2028 strcat (buf, ", uses new ABI");
2029 break;
2031 case EF_ARM_OLD_ABI:
2032 strcat (buf, ", uses old ABI");
2033 break;
2035 case EF_ARM_SOFT_FLOAT:
2036 strcat (buf, ", software FP");
2037 break;
2039 case EF_ARM_VFP_FLOAT:
2040 strcat (buf, ", VFP");
2041 break;
2043 case EF_ARM_MAVERICK_FLOAT:
2044 strcat (buf, ", Maverick FP");
2045 break;
2047 default:
2048 unknown = 1;
2049 break;
2054 if (unknown)
2055 strcat (buf,", <unknown>");
2058 static char *
2059 get_machine_flags (unsigned e_flags, unsigned e_machine)
2061 static char buf[1024];
2063 buf[0] = '\0';
2065 if (e_flags)
2067 switch (e_machine)
2069 default:
2070 break;
2072 case EM_ARM:
2073 decode_ARM_machine_flags (e_flags, buf);
2074 break;
2076 case EM_CYGNUS_FRV:
2077 switch (e_flags & EF_FRV_CPU_MASK)
2079 case EF_FRV_CPU_GENERIC:
2080 break;
2082 default:
2083 strcat (buf, ", fr???");
2084 break;
2086 case EF_FRV_CPU_FR300:
2087 strcat (buf, ", fr300");
2088 break;
2090 case EF_FRV_CPU_FR400:
2091 strcat (buf, ", fr400");
2092 break;
2093 case EF_FRV_CPU_FR405:
2094 strcat (buf, ", fr405");
2095 break;
2097 case EF_FRV_CPU_FR450:
2098 strcat (buf, ", fr450");
2099 break;
2101 case EF_FRV_CPU_FR500:
2102 strcat (buf, ", fr500");
2103 break;
2104 case EF_FRV_CPU_FR550:
2105 strcat (buf, ", fr550");
2106 break;
2108 case EF_FRV_CPU_SIMPLE:
2109 strcat (buf, ", simple");
2110 break;
2111 case EF_FRV_CPU_TOMCAT:
2112 strcat (buf, ", tomcat");
2113 break;
2115 break;
2117 case EM_68K:
2118 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
2119 strcat (buf, ", m68000");
2120 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
2121 strcat (buf, ", cpu32");
2122 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
2123 strcat (buf, ", fido_a");
2124 else
2126 char const * isa = _("unknown");
2127 char const * mac = _("unknown mac");
2128 char const * additional = NULL;
2130 switch (e_flags & EF_M68K_CF_ISA_MASK)
2132 case EF_M68K_CF_ISA_A_NODIV:
2133 isa = "A";
2134 additional = ", nodiv";
2135 break;
2136 case EF_M68K_CF_ISA_A:
2137 isa = "A";
2138 break;
2139 case EF_M68K_CF_ISA_A_PLUS:
2140 isa = "A+";
2141 break;
2142 case EF_M68K_CF_ISA_B_NOUSP:
2143 isa = "B";
2144 additional = ", nousp";
2145 break;
2146 case EF_M68K_CF_ISA_B:
2147 isa = "B";
2148 break;
2150 strcat (buf, ", cf, isa ");
2151 strcat (buf, isa);
2152 if (additional)
2153 strcat (buf, additional);
2154 if (e_flags & EF_M68K_CF_FLOAT)
2155 strcat (buf, ", float");
2156 switch (e_flags & EF_M68K_CF_MAC_MASK)
2158 case 0:
2159 mac = NULL;
2160 break;
2161 case EF_M68K_CF_MAC:
2162 mac = "mac";
2163 break;
2164 case EF_M68K_CF_EMAC:
2165 mac = "emac";
2166 break;
2168 if (mac)
2170 strcat (buf, ", ");
2171 strcat (buf, mac);
2174 break;
2176 case EM_PPC:
2177 if (e_flags & EF_PPC_EMB)
2178 strcat (buf, ", emb");
2180 if (e_flags & EF_PPC_RELOCATABLE)
2181 strcat (buf, ", relocatable");
2183 if (e_flags & EF_PPC_RELOCATABLE_LIB)
2184 strcat (buf, ", relocatable-lib");
2185 break;
2187 case EM_V850:
2188 case EM_CYGNUS_V850:
2189 switch (e_flags & EF_V850_ARCH)
2191 case E_V850E1_ARCH:
2192 strcat (buf, ", v850e1");
2193 break;
2194 case E_V850E_ARCH:
2195 strcat (buf, ", v850e");
2196 break;
2197 case E_V850_ARCH:
2198 strcat (buf, ", v850");
2199 break;
2200 default:
2201 strcat (buf, ", unknown v850 architecture variant");
2202 break;
2204 break;
2206 case EM_M32R:
2207 case EM_CYGNUS_M32R:
2208 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
2209 strcat (buf, ", m32r");
2210 break;
2212 case EM_MIPS:
2213 case EM_MIPS_RS3_LE:
2214 if (e_flags & EF_MIPS_NOREORDER)
2215 strcat (buf, ", noreorder");
2217 if (e_flags & EF_MIPS_PIC)
2218 strcat (buf, ", pic");
2220 if (e_flags & EF_MIPS_CPIC)
2221 strcat (buf, ", cpic");
2223 if (e_flags & EF_MIPS_UCODE)
2224 strcat (buf, ", ugen_reserved");
2226 if (e_flags & EF_MIPS_ABI2)
2227 strcat (buf, ", abi2");
2229 if (e_flags & EF_MIPS_OPTIONS_FIRST)
2230 strcat (buf, ", odk first");
2232 if (e_flags & EF_MIPS_32BITMODE)
2233 strcat (buf, ", 32bitmode");
2235 switch ((e_flags & EF_MIPS_MACH))
2237 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
2238 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
2239 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
2240 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
2241 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
2242 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
2243 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
2244 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
2245 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
2246 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
2247 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
2248 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
2249 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
2250 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
2251 case 0:
2252 /* We simply ignore the field in this case to avoid confusion:
2253 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
2254 extension. */
2255 break;
2256 default: strcat (buf, ", unknown CPU"); break;
2259 switch ((e_flags & EF_MIPS_ABI))
2261 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
2262 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
2263 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
2264 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
2265 case 0:
2266 /* We simply ignore the field in this case to avoid confusion:
2267 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
2268 This means it is likely to be an o32 file, but not for
2269 sure. */
2270 break;
2271 default: strcat (buf, ", unknown ABI"); break;
2274 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
2275 strcat (buf, ", mdmx");
2277 if (e_flags & EF_MIPS_ARCH_ASE_M16)
2278 strcat (buf, ", mips16");
2280 switch ((e_flags & EF_MIPS_ARCH))
2282 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
2283 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
2284 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
2285 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
2286 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
2287 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
2288 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
2289 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
2290 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
2291 default: strcat (buf, ", unknown ISA"); break;
2294 break;
2296 case EM_SH:
2297 switch ((e_flags & EF_SH_MACH_MASK))
2299 case EF_SH1: strcat (buf, ", sh1"); break;
2300 case EF_SH2: strcat (buf, ", sh2"); break;
2301 case EF_SH3: strcat (buf, ", sh3"); break;
2302 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
2303 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
2304 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
2305 case EF_SH3E: strcat (buf, ", sh3e"); break;
2306 case EF_SH4: strcat (buf, ", sh4"); break;
2307 case EF_SH5: strcat (buf, ", sh5"); break;
2308 case EF_SH2E: strcat (buf, ", sh2e"); break;
2309 case EF_SH4A: strcat (buf, ", sh4a"); break;
2310 case EF_SH2A: strcat (buf, ", sh2a"); break;
2311 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
2312 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
2313 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
2314 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
2315 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
2316 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
2317 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
2318 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
2319 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
2320 default: strcat (buf, ", unknown ISA"); break;
2323 break;
2325 case EM_SPARCV9:
2326 if (e_flags & EF_SPARC_32PLUS)
2327 strcat (buf, ", v8+");
2329 if (e_flags & EF_SPARC_SUN_US1)
2330 strcat (buf, ", ultrasparcI");
2332 if (e_flags & EF_SPARC_SUN_US3)
2333 strcat (buf, ", ultrasparcIII");
2335 if (e_flags & EF_SPARC_HAL_R1)
2336 strcat (buf, ", halr1");
2338 if (e_flags & EF_SPARC_LEDATA)
2339 strcat (buf, ", ledata");
2341 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
2342 strcat (buf, ", tso");
2344 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
2345 strcat (buf, ", pso");
2347 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
2348 strcat (buf, ", rmo");
2349 break;
2351 case EM_PARISC:
2352 switch (e_flags & EF_PARISC_ARCH)
2354 case EFA_PARISC_1_0:
2355 strcpy (buf, ", PA-RISC 1.0");
2356 break;
2357 case EFA_PARISC_1_1:
2358 strcpy (buf, ", PA-RISC 1.1");
2359 break;
2360 case EFA_PARISC_2_0:
2361 strcpy (buf, ", PA-RISC 2.0");
2362 break;
2363 default:
2364 break;
2366 if (e_flags & EF_PARISC_TRAPNIL)
2367 strcat (buf, ", trapnil");
2368 if (e_flags & EF_PARISC_EXT)
2369 strcat (buf, ", ext");
2370 if (e_flags & EF_PARISC_LSB)
2371 strcat (buf, ", lsb");
2372 if (e_flags & EF_PARISC_WIDE)
2373 strcat (buf, ", wide");
2374 if (e_flags & EF_PARISC_NO_KABP)
2375 strcat (buf, ", no kabp");
2376 if (e_flags & EF_PARISC_LAZYSWAP)
2377 strcat (buf, ", lazyswap");
2378 break;
2380 case EM_PJ:
2381 case EM_PJ_OLD:
2382 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
2383 strcat (buf, ", new calling convention");
2385 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
2386 strcat (buf, ", gnu calling convention");
2387 break;
2389 case EM_IA_64:
2390 if ((e_flags & EF_IA_64_ABI64))
2391 strcat (buf, ", 64-bit");
2392 else
2393 strcat (buf, ", 32-bit");
2394 if ((e_flags & EF_IA_64_REDUCEDFP))
2395 strcat (buf, ", reduced fp model");
2396 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
2397 strcat (buf, ", no function descriptors, constant gp");
2398 else if ((e_flags & EF_IA_64_CONS_GP))
2399 strcat (buf, ", constant gp");
2400 if ((e_flags & EF_IA_64_ABSOLUTE))
2401 strcat (buf, ", absolute");
2402 break;
2404 case EM_VAX:
2405 if ((e_flags & EF_VAX_NONPIC))
2406 strcat (buf, ", non-PIC");
2407 if ((e_flags & EF_VAX_DFLOAT))
2408 strcat (buf, ", D-Float");
2409 if ((e_flags & EF_VAX_GFLOAT))
2410 strcat (buf, ", G-Float");
2411 break;
2415 return buf;
2418 static const char *
2419 get_osabi_name (unsigned int osabi)
2421 static char buff[32];
2423 switch (osabi)
2425 case ELFOSABI_NONE: return "UNIX - System V";
2426 case ELFOSABI_HPUX: return "UNIX - HP-UX";
2427 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
2428 case ELFOSABI_LINUX: return "UNIX - Linux";
2429 case ELFOSABI_HURD: return "GNU/Hurd";
2430 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
2431 case ELFOSABI_AIX: return "UNIX - AIX";
2432 case ELFOSABI_IRIX: return "UNIX - IRIX";
2433 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
2434 case ELFOSABI_TRU64: return "UNIX - TRU64";
2435 case ELFOSABI_MODESTO: return "Novell - Modesto";
2436 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
2437 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
2438 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
2439 case ELFOSABI_AROS: return "AROS";
2440 case ELFOSABI_STANDALONE: return _("Standalone App");
2441 case ELFOSABI_ARM: return "ARM";
2442 default:
2443 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
2444 return buff;
2448 static const char *
2449 get_arm_segment_type (unsigned long type)
2451 switch (type)
2453 case PT_ARM_EXIDX:
2454 return "EXIDX";
2455 default:
2456 break;
2459 return NULL;
2462 static const char *
2463 get_mips_segment_type (unsigned long type)
2465 switch (type)
2467 case PT_MIPS_REGINFO:
2468 return "REGINFO";
2469 case PT_MIPS_RTPROC:
2470 return "RTPROC";
2471 case PT_MIPS_OPTIONS:
2472 return "OPTIONS";
2473 default:
2474 break;
2477 return NULL;
2480 static const char *
2481 get_parisc_segment_type (unsigned long type)
2483 switch (type)
2485 case PT_HP_TLS: return "HP_TLS";
2486 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
2487 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
2488 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
2489 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
2490 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
2491 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
2492 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
2493 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
2494 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
2495 case PT_HP_PARALLEL: return "HP_PARALLEL";
2496 case PT_HP_FASTBIND: return "HP_FASTBIND";
2497 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
2498 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
2499 case PT_HP_STACK: return "HP_STACK";
2500 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
2501 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
2502 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
2503 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
2504 default:
2505 break;
2508 return NULL;
2511 static const char *
2512 get_ia64_segment_type (unsigned long type)
2514 switch (type)
2516 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
2517 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
2518 case PT_HP_TLS: return "HP_TLS";
2519 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
2520 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
2521 case PT_IA_64_HP_STACK: return "HP_STACK";
2522 default:
2523 break;
2526 return NULL;
2529 static const char *
2530 get_segment_type (unsigned long p_type)
2532 static char buff[32];
2534 switch (p_type)
2536 case PT_NULL: return "NULL";
2537 case PT_LOAD: return "LOAD";
2538 case PT_DYNAMIC: return "DYNAMIC";
2539 case PT_INTERP: return "INTERP";
2540 case PT_NOTE: return "NOTE";
2541 case PT_SHLIB: return "SHLIB";
2542 case PT_PHDR: return "PHDR";
2543 case PT_TLS: return "TLS";
2545 case PT_GNU_EH_FRAME:
2546 return "GNU_EH_FRAME";
2547 case PT_GNU_STACK: return "GNU_STACK";
2548 case PT_GNU_RELRO: return "GNU_RELRO";
2550 default:
2551 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
2553 const char * result;
2555 switch (elf_header.e_machine)
2557 case EM_ARM:
2558 result = get_arm_segment_type (p_type);
2559 break;
2560 case EM_MIPS:
2561 case EM_MIPS_RS3_LE:
2562 result = get_mips_segment_type (p_type);
2563 break;
2564 case EM_PARISC:
2565 result = get_parisc_segment_type (p_type);
2566 break;
2567 case EM_IA_64:
2568 result = get_ia64_segment_type (p_type);
2569 break;
2570 default:
2571 result = NULL;
2572 break;
2575 if (result != NULL)
2576 return result;
2578 sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
2580 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
2582 const char * result;
2584 switch (elf_header.e_machine)
2586 case EM_PARISC:
2587 result = get_parisc_segment_type (p_type);
2588 break;
2589 case EM_IA_64:
2590 result = get_ia64_segment_type (p_type);
2591 break;
2592 default:
2593 result = NULL;
2594 break;
2597 if (result != NULL)
2598 return result;
2600 sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
2602 else
2603 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
2605 return buff;
2609 static const char *
2610 get_mips_section_type_name (unsigned int sh_type)
2612 switch (sh_type)
2614 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2615 case SHT_MIPS_MSYM: return "MIPS_MSYM";
2616 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2617 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
2618 case SHT_MIPS_UCODE: return "MIPS_UCODE";
2619 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
2620 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
2621 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
2622 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
2623 case SHT_MIPS_RELD: return "MIPS_RELD";
2624 case SHT_MIPS_IFACE: return "MIPS_IFACE";
2625 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
2626 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2627 case SHT_MIPS_SHDR: return "MIPS_SHDR";
2628 case SHT_MIPS_FDESC: return "MIPS_FDESC";
2629 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
2630 case SHT_MIPS_DENSE: return "MIPS_DENSE";
2631 case SHT_MIPS_PDESC: return "MIPS_PDESC";
2632 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
2633 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
2634 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
2635 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
2636 case SHT_MIPS_LINE: return "MIPS_LINE";
2637 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
2638 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
2639 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
2640 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
2641 case SHT_MIPS_DWARF: return "MIPS_DWARF";
2642 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
2643 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2644 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
2645 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
2646 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
2647 case SHT_MIPS_XLATE: return "MIPS_XLATE";
2648 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
2649 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
2650 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
2651 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
2652 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
2653 default:
2654 break;
2656 return NULL;
2659 static const char *
2660 get_parisc_section_type_name (unsigned int sh_type)
2662 switch (sh_type)
2664 case SHT_PARISC_EXT: return "PARISC_EXT";
2665 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
2666 case SHT_PARISC_DOC: return "PARISC_DOC";
2667 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
2668 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
2669 case SHT_PARISC_STUBS: return "PARISC_STUBS";
2670 case SHT_PARISC_DLKM: return "PARISC_DLKM";
2671 default:
2672 break;
2674 return NULL;
2677 static const char *
2678 get_ia64_section_type_name (unsigned int sh_type)
2680 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
2681 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
2682 return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
2684 switch (sh_type)
2686 case SHT_IA_64_EXT: return "IA_64_EXT";
2687 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
2688 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
2689 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
2690 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
2691 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
2692 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
2693 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
2694 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
2695 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
2696 default:
2697 break;
2699 return NULL;
2702 static const char *
2703 get_x86_64_section_type_name (unsigned int sh_type)
2705 switch (sh_type)
2707 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
2708 default:
2709 break;
2711 return NULL;
2714 static const char *
2715 get_arm_section_type_name (unsigned int sh_type)
2717 switch (sh_type)
2719 case SHT_ARM_EXIDX:
2720 return "ARM_EXIDX";
2721 case SHT_ARM_PREEMPTMAP:
2722 return "ARM_PREEMPTMAP";
2723 case SHT_ARM_ATTRIBUTES:
2724 return "ARM_ATTRIBUTES";
2725 default:
2726 break;
2728 return NULL;
2731 static const char *
2732 get_section_type_name (unsigned int sh_type)
2734 static char buff[32];
2736 switch (sh_type)
2738 case SHT_NULL: return "NULL";
2739 case SHT_PROGBITS: return "PROGBITS";
2740 case SHT_SYMTAB: return "SYMTAB";
2741 case SHT_STRTAB: return "STRTAB";
2742 case SHT_RELA: return "RELA";
2743 case SHT_HASH: return "HASH";
2744 case SHT_DYNAMIC: return "DYNAMIC";
2745 case SHT_NOTE: return "NOTE";
2746 case SHT_NOBITS: return "NOBITS";
2747 case SHT_REL: return "REL";
2748 case SHT_SHLIB: return "SHLIB";
2749 case SHT_DYNSYM: return "DYNSYM";
2750 case SHT_INIT_ARRAY: return "INIT_ARRAY";
2751 case SHT_FINI_ARRAY: return "FINI_ARRAY";
2752 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2753 case SHT_GNU_HASH: return "GNU_HASH";
2754 case SHT_GROUP: return "GROUP";
2755 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICIES";
2756 case SHT_GNU_verdef: return "VERDEF";
2757 case SHT_GNU_verneed: return "VERNEED";
2758 case SHT_GNU_versym: return "VERSYM";
2759 case 0x6ffffff0: return "VERSYM";
2760 case 0x6ffffffc: return "VERDEF";
2761 case 0x7ffffffd: return "AUXILIARY";
2762 case 0x7fffffff: return "FILTER";
2763 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
2765 default:
2766 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
2768 const char * result;
2770 switch (elf_header.e_machine)
2772 case EM_MIPS:
2773 case EM_MIPS_RS3_LE:
2774 result = get_mips_section_type_name (sh_type);
2775 break;
2776 case EM_PARISC:
2777 result = get_parisc_section_type_name (sh_type);
2778 break;
2779 case EM_IA_64:
2780 result = get_ia64_section_type_name (sh_type);
2781 break;
2782 case EM_X86_64:
2783 case EM_L1OM:
2784 result = get_x86_64_section_type_name (sh_type);
2785 break;
2786 case EM_ARM:
2787 result = get_arm_section_type_name (sh_type);
2788 break;
2789 default:
2790 result = NULL;
2791 break;
2794 if (result != NULL)
2795 return result;
2797 sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
2799 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
2801 const char * result;
2803 switch (elf_header.e_machine)
2805 case EM_IA_64:
2806 result = get_ia64_section_type_name (sh_type);
2807 break;
2808 default:
2809 result = NULL;
2810 break;
2813 if (result != NULL)
2814 return result;
2816 sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
2818 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
2819 sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
2820 else
2821 snprintf (buff, sizeof (buff), _("<unknown>: %x"), sh_type);
2823 return buff;
2827 #define OPTION_DEBUG_DUMP 512
2829 static struct option options[] =
2831 {"all", no_argument, 0, 'a'},
2832 {"file-header", no_argument, 0, 'h'},
2833 {"program-headers", no_argument, 0, 'l'},
2834 {"headers", no_argument, 0, 'e'},
2835 {"histogram", no_argument, 0, 'I'},
2836 {"segments", no_argument, 0, 'l'},
2837 {"sections", no_argument, 0, 'S'},
2838 {"section-headers", no_argument, 0, 'S'},
2839 {"section-groups", no_argument, 0, 'g'},
2840 {"section-details", no_argument, 0, 't'},
2841 {"full-section-name",no_argument, 0, 'N'},
2842 {"symbols", no_argument, 0, 's'},
2843 {"syms", no_argument, 0, 's'},
2844 {"relocs", no_argument, 0, 'r'},
2845 {"notes", no_argument, 0, 'n'},
2846 {"dynamic", no_argument, 0, 'd'},
2847 {"arch-specific", no_argument, 0, 'A'},
2848 {"version-info", no_argument, 0, 'V'},
2849 {"use-dynamic", no_argument, 0, 'D'},
2850 {"unwind", no_argument, 0, 'u'},
2851 {"archive-index", no_argument, 0, 'c'},
2852 {"hex-dump", required_argument, 0, 'x'},
2853 {"relocated-dump", required_argument, 0, 'R'},
2854 {"string-dump", required_argument, 0, 'p'},
2855 #ifdef SUPPORT_DISASSEMBLY
2856 {"instruction-dump", required_argument, 0, 'i'},
2857 #endif
2858 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
2860 {"version", no_argument, 0, 'v'},
2861 {"wide", no_argument, 0, 'W'},
2862 {"help", no_argument, 0, 'H'},
2863 {0, no_argument, 0, 0}
2866 static void
2867 usage (FILE * stream)
2869 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
2870 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
2871 fprintf (stream, _(" Options are:\n\
2872 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
2873 -h --file-header Display the ELF file header\n\
2874 -l --program-headers Display the program headers\n\
2875 --segments An alias for --program-headers\n\
2876 -S --section-headers Display the sections' header\n\
2877 --sections An alias for --section-headers\n\
2878 -g --section-groups Display the section groups\n\
2879 -t --section-details Display the section details\n\
2880 -e --headers Equivalent to: -h -l -S\n\
2881 -s --syms Display the symbol table\n\
2882 --symbols An alias for --syms\n\
2883 -n --notes Display the core notes (if present)\n\
2884 -r --relocs Display the relocations (if present)\n\
2885 -u --unwind Display the unwind info (if present)\n\
2886 -d --dynamic Display the dynamic section (if present)\n\
2887 -V --version-info Display the version sections (if present)\n\
2888 -A --arch-specific Display architecture specific information (if any).\n\
2889 -c --archive-index Display the symbol/file index in an archive\n\
2890 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
2891 -x --hex-dump=<number|name>\n\
2892 Dump the contents of section <number|name> as bytes\n\
2893 -p --string-dump=<number|name>\n\
2894 Dump the contents of section <number|name> as strings\n\
2895 -R --relocated-dump=<number|name>\n\
2896 Dump the contents of section <number|name> as relocated bytes\n\
2897 -w[lLiaprmfFsoR] or\n\
2898 --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=str,=loc,=Ranges]\n\
2899 Display the contents of DWARF2 debug sections\n"));
2900 #ifdef SUPPORT_DISASSEMBLY
2901 fprintf (stream, _("\
2902 -i --instruction-dump=<number|name>\n\
2903 Disassemble the contents of section <number|name>\n"));
2904 #endif
2905 fprintf (stream, _("\
2906 -I --histogram Display histogram of bucket list lengths\n\
2907 -W --wide Allow output width to exceed 80 characters\n\
2908 @<file> Read options from <file>\n\
2909 -H --help Display this information\n\
2910 -v --version Display the version number of readelf\n"));
2912 if (REPORT_BUGS_TO[0] && stream == stdout)
2913 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
2915 exit (stream == stdout ? 0 : 1);
2918 /* Record the fact that the user wants the contents of section number
2919 SECTION to be displayed using the method(s) encoded as flags bits
2920 in TYPE. Note, TYPE can be zero if we are creating the array for
2921 the first time. */
2923 static void
2924 request_dump_bynumber (unsigned int section, dump_type type)
2926 if (section >= num_dump_sects)
2928 dump_type * new_dump_sects;
2930 new_dump_sects = calloc (section + 1, sizeof (* dump_sects));
2932 if (new_dump_sects == NULL)
2933 error (_("Out of memory allocating dump request table.\n"));
2934 else
2936 /* Copy current flag settings. */
2937 memcpy (new_dump_sects, dump_sects, num_dump_sects * sizeof (* dump_sects));
2939 free (dump_sects);
2941 dump_sects = new_dump_sects;
2942 num_dump_sects = section + 1;
2946 if (dump_sects)
2947 dump_sects[section] |= type;
2949 return;
2952 /* Request a dump by section name. */
2954 static void
2955 request_dump_byname (const char * section, dump_type type)
2957 struct dump_list_entry * new_request;
2959 new_request = malloc (sizeof (struct dump_list_entry));
2960 if (!new_request)
2961 error (_("Out of memory allocating dump request table.\n"));
2963 new_request->name = strdup (section);
2964 if (!new_request->name)
2965 error (_("Out of memory allocating dump request table.\n"));
2967 new_request->type = type;
2969 new_request->next = dump_sects_byname;
2970 dump_sects_byname = new_request;
2973 static inline void
2974 request_dump (dump_type type)
2976 int section;
2977 char * cp;
2979 do_dump++;
2980 section = strtoul (optarg, & cp, 0);
2982 if (! *cp && section >= 0)
2983 request_dump_bynumber (section, type);
2984 else
2985 request_dump_byname (optarg, type);
2989 static void
2990 parse_args (int argc, char ** argv)
2992 int c;
2994 if (argc < 2)
2995 usage (stderr);
2997 while ((c = getopt_long
2998 (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:", options, NULL)) != EOF)
3000 switch (c)
3002 case 0:
3003 /* Long options. */
3004 break;
3005 case 'H':
3006 usage (stdout);
3007 break;
3009 case 'a':
3010 do_syms++;
3011 do_reloc++;
3012 do_unwind++;
3013 do_dynamic++;
3014 do_header++;
3015 do_sections++;
3016 do_section_groups++;
3017 do_segments++;
3018 do_version++;
3019 do_histogram++;
3020 do_arch++;
3021 do_notes++;
3022 break;
3023 case 'g':
3024 do_section_groups++;
3025 break;
3026 case 't':
3027 case 'N':
3028 do_sections++;
3029 do_section_details++;
3030 break;
3031 case 'e':
3032 do_header++;
3033 do_sections++;
3034 do_segments++;
3035 break;
3036 case 'A':
3037 do_arch++;
3038 break;
3039 case 'D':
3040 do_using_dynamic++;
3041 break;
3042 case 'r':
3043 do_reloc++;
3044 break;
3045 case 'u':
3046 do_unwind++;
3047 break;
3048 case 'h':
3049 do_header++;
3050 break;
3051 case 'l':
3052 do_segments++;
3053 break;
3054 case 's':
3055 do_syms++;
3056 break;
3057 case 'S':
3058 do_sections++;
3059 break;
3060 case 'd':
3061 do_dynamic++;
3062 break;
3063 case 'I':
3064 do_histogram++;
3065 break;
3066 case 'n':
3067 do_notes++;
3068 break;
3069 case 'c':
3070 do_archive_index++;
3071 break;
3072 case 'x':
3073 request_dump (HEX_DUMP);
3074 break;
3075 case 'p':
3076 request_dump (STRING_DUMP);
3077 break;
3078 case 'R':
3079 request_dump (RELOC_DUMP);
3080 break;
3081 case 'w':
3082 do_dump++;
3083 if (optarg == 0)
3085 do_debugging = 1;
3086 dwarf_select_sections_all ();
3088 else
3090 do_debugging = 0;
3091 dwarf_select_sections_by_letters (optarg);
3093 break;
3094 case OPTION_DEBUG_DUMP:
3095 do_dump++;
3096 if (optarg == 0)
3097 do_debugging = 1;
3098 else
3100 do_debugging = 0;
3101 dwarf_select_sections_by_names (optarg);
3103 break;
3104 #ifdef SUPPORT_DISASSEMBLY
3105 case 'i':
3106 request_dump (DISASS_DUMP);
3107 break;
3108 #endif
3109 case 'v':
3110 print_version (program_name);
3111 break;
3112 case 'V':
3113 do_version++;
3114 break;
3115 case 'W':
3116 do_wide++;
3117 break;
3118 default:
3119 /* xgettext:c-format */
3120 error (_("Invalid option '-%c'\n"), c);
3121 /* Drop through. */
3122 case '?':
3123 usage (stderr);
3127 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
3128 && !do_segments && !do_header && !do_dump && !do_version
3129 && !do_histogram && !do_debugging && !do_arch && !do_notes
3130 && !do_section_groups && !do_archive_index)
3131 usage (stderr);
3132 else if (argc < 3)
3134 warn (_("Nothing to do.\n"));
3135 usage (stderr);
3139 static const char *
3140 get_elf_class (unsigned int elf_class)
3142 static char buff[32];
3144 switch (elf_class)
3146 case ELFCLASSNONE: return _("none");
3147 case ELFCLASS32: return "ELF32";
3148 case ELFCLASS64: return "ELF64";
3149 default:
3150 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
3151 return buff;
3155 static const char *
3156 get_data_encoding (unsigned int encoding)
3158 static char buff[32];
3160 switch (encoding)
3162 case ELFDATANONE: return _("none");
3163 case ELFDATA2LSB: return _("2's complement, little endian");
3164 case ELFDATA2MSB: return _("2's complement, big endian");
3165 default:
3166 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
3167 return buff;
3171 /* Decode the data held in 'elf_header'. */
3173 static int
3174 process_file_header (void)
3176 if ( elf_header.e_ident[EI_MAG0] != ELFMAG0
3177 || elf_header.e_ident[EI_MAG1] != ELFMAG1
3178 || elf_header.e_ident[EI_MAG2] != ELFMAG2
3179 || elf_header.e_ident[EI_MAG3] != ELFMAG3)
3181 error
3182 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
3183 return 0;
3186 init_dwarf_regnames (elf_header.e_machine);
3188 if (do_header)
3190 int i;
3192 printf (_("ELF Header:\n"));
3193 printf (_(" Magic: "));
3194 for (i = 0; i < EI_NIDENT; i++)
3195 printf ("%2.2x ", elf_header.e_ident[i]);
3196 printf ("\n");
3197 printf (_(" Class: %s\n"),
3198 get_elf_class (elf_header.e_ident[EI_CLASS]));
3199 printf (_(" Data: %s\n"),
3200 get_data_encoding (elf_header.e_ident[EI_DATA]));
3201 printf (_(" Version: %d %s\n"),
3202 elf_header.e_ident[EI_VERSION],
3203 (elf_header.e_ident[EI_VERSION] == EV_CURRENT
3204 ? "(current)"
3205 : (elf_header.e_ident[EI_VERSION] != EV_NONE
3206 ? "<unknown: %lx>"
3207 : "")));
3208 printf (_(" OS/ABI: %s\n"),
3209 get_osabi_name (elf_header.e_ident[EI_OSABI]));
3210 printf (_(" ABI Version: %d\n"),
3211 elf_header.e_ident[EI_ABIVERSION]);
3212 printf (_(" Type: %s\n"),
3213 get_file_type (elf_header.e_type));
3214 printf (_(" Machine: %s\n"),
3215 get_machine_name (elf_header.e_machine));
3216 printf (_(" Version: 0x%lx\n"),
3217 (unsigned long) elf_header.e_version);
3219 printf (_(" Entry point address: "));
3220 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3221 printf (_("\n Start of program headers: "));
3222 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3223 printf (_(" (bytes into file)\n Start of section headers: "));
3224 print_vma ((bfd_vma) elf_header.e_shoff, DEC);
3225 printf (_(" (bytes into file)\n"));
3227 printf (_(" Flags: 0x%lx%s\n"),
3228 (unsigned long) elf_header.e_flags,
3229 get_machine_flags (elf_header.e_flags, elf_header.e_machine));
3230 printf (_(" Size of this header: %ld (bytes)\n"),
3231 (long) elf_header.e_ehsize);
3232 printf (_(" Size of program headers: %ld (bytes)\n"),
3233 (long) elf_header.e_phentsize);
3234 printf (_(" Number of program headers: %ld\n"),
3235 (long) elf_header.e_phnum);
3236 printf (_(" Size of section headers: %ld (bytes)\n"),
3237 (long) elf_header.e_shentsize);
3238 printf (_(" Number of section headers: %ld"),
3239 (long) elf_header.e_shnum);
3240 if (section_headers != NULL && elf_header.e_shnum == SHN_UNDEF)
3241 printf (" (%ld)", (long) section_headers[0].sh_size);
3242 putc ('\n', stdout);
3243 printf (_(" Section header string table index: %ld"),
3244 (long) elf_header.e_shstrndx);
3245 if (section_headers != NULL
3246 && elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
3247 printf (" (%u)", section_headers[0].sh_link);
3248 else if (elf_header.e_shstrndx != SHN_UNDEF
3249 && elf_header.e_shstrndx >= elf_header.e_shnum)
3250 printf (" <corrupt: out of range>");
3251 putc ('\n', stdout);
3254 if (section_headers != NULL)
3256 if (elf_header.e_shnum == SHN_UNDEF)
3257 elf_header.e_shnum = section_headers[0].sh_size;
3258 if (elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
3259 elf_header.e_shstrndx = section_headers[0].sh_link;
3260 else if (elf_header.e_shstrndx >= elf_header.e_shnum)
3261 elf_header.e_shstrndx = SHN_UNDEF;
3262 free (section_headers);
3263 section_headers = NULL;
3266 return 1;
3270 static int
3271 get_32bit_program_headers (FILE * file, Elf_Internal_Phdr * program_headers)
3273 Elf32_External_Phdr * phdrs;
3274 Elf32_External_Phdr * external;
3275 Elf_Internal_Phdr * internal;
3276 unsigned int i;
3278 phdrs = get_data (NULL, file, elf_header.e_phoff,
3279 elf_header.e_phentsize, elf_header.e_phnum,
3280 _("program headers"));
3281 if (!phdrs)
3282 return 0;
3284 for (i = 0, internal = program_headers, external = phdrs;
3285 i < elf_header.e_phnum;
3286 i++, internal++, external++)
3288 internal->p_type = BYTE_GET (external->p_type);
3289 internal->p_offset = BYTE_GET (external->p_offset);
3290 internal->p_vaddr = BYTE_GET (external->p_vaddr);
3291 internal->p_paddr = BYTE_GET (external->p_paddr);
3292 internal->p_filesz = BYTE_GET (external->p_filesz);
3293 internal->p_memsz = BYTE_GET (external->p_memsz);
3294 internal->p_flags = BYTE_GET (external->p_flags);
3295 internal->p_align = BYTE_GET (external->p_align);
3298 free (phdrs);
3300 return 1;
3303 static int
3304 get_64bit_program_headers (FILE * file, Elf_Internal_Phdr * program_headers)
3306 Elf64_External_Phdr * phdrs;
3307 Elf64_External_Phdr * external;
3308 Elf_Internal_Phdr * internal;
3309 unsigned int i;
3311 phdrs = get_data (NULL, file, elf_header.e_phoff,
3312 elf_header.e_phentsize, elf_header.e_phnum,
3313 _("program headers"));
3314 if (!phdrs)
3315 return 0;
3317 for (i = 0, internal = program_headers, external = phdrs;
3318 i < elf_header.e_phnum;
3319 i++, internal++, external++)
3321 internal->p_type = BYTE_GET (external->p_type);
3322 internal->p_flags = BYTE_GET (external->p_flags);
3323 internal->p_offset = BYTE_GET (external->p_offset);
3324 internal->p_vaddr = BYTE_GET (external->p_vaddr);
3325 internal->p_paddr = BYTE_GET (external->p_paddr);
3326 internal->p_filesz = BYTE_GET (external->p_filesz);
3327 internal->p_memsz = BYTE_GET (external->p_memsz);
3328 internal->p_align = BYTE_GET (external->p_align);
3331 free (phdrs);
3333 return 1;
3336 /* Returns 1 if the program headers were read into `program_headers'. */
3338 static int
3339 get_program_headers (FILE * file)
3341 Elf_Internal_Phdr * phdrs;
3343 /* Check cache of prior read. */
3344 if (program_headers != NULL)
3345 return 1;
3347 phdrs = cmalloc (elf_header.e_phnum, sizeof (Elf_Internal_Phdr));
3349 if (phdrs == NULL)
3351 error (_("Out of memory\n"));
3352 return 0;
3355 if (is_32bit_elf
3356 ? get_32bit_program_headers (file, phdrs)
3357 : get_64bit_program_headers (file, phdrs))
3359 program_headers = phdrs;
3360 return 1;
3363 free (phdrs);
3364 return 0;
3367 /* Returns 1 if the program headers were loaded. */
3369 static int
3370 process_program_headers (FILE * file)
3372 Elf_Internal_Phdr * segment;
3373 unsigned int i;
3375 if (elf_header.e_phnum == 0)
3377 if (do_segments)
3378 printf (_("\nThere are no program headers in this file.\n"));
3379 return 0;
3382 if (do_segments && !do_header)
3384 printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
3385 printf (_("Entry point "));
3386 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3387 printf (_("\nThere are %d program headers, starting at offset "),
3388 elf_header.e_phnum);
3389 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3390 printf ("\n");
3393 if (! get_program_headers (file))
3394 return 0;
3396 if (do_segments)
3398 if (elf_header.e_phnum > 1)
3399 printf (_("\nProgram Headers:\n"));
3400 else
3401 printf (_("\nProgram Headers:\n"));
3403 if (is_32bit_elf)
3404 printf
3405 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
3406 else if (do_wide)
3407 printf
3408 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
3409 else
3411 printf
3412 (_(" Type Offset VirtAddr PhysAddr\n"));
3413 printf
3414 (_(" FileSiz MemSiz Flags Align\n"));
3418 dynamic_addr = 0;
3419 dynamic_size = 0;
3421 for (i = 0, segment = program_headers;
3422 i < elf_header.e_phnum;
3423 i++, segment++)
3425 if (do_segments)
3427 printf (" %-14.14s ", get_segment_type (segment->p_type));
3429 if (is_32bit_elf)
3431 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3432 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
3433 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
3434 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
3435 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
3436 printf ("%c%c%c ",
3437 (segment->p_flags & PF_R ? 'R' : ' '),
3438 (segment->p_flags & PF_W ? 'W' : ' '),
3439 (segment->p_flags & PF_X ? 'E' : ' '));
3440 printf ("%#lx", (unsigned long) segment->p_align);
3442 else if (do_wide)
3444 if ((unsigned long) segment->p_offset == segment->p_offset)
3445 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3446 else
3448 print_vma (segment->p_offset, FULL_HEX);
3449 putchar (' ');
3452 print_vma (segment->p_vaddr, FULL_HEX);
3453 putchar (' ');
3454 print_vma (segment->p_paddr, FULL_HEX);
3455 putchar (' ');
3457 if ((unsigned long) segment->p_filesz == segment->p_filesz)
3458 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
3459 else
3461 print_vma (segment->p_filesz, FULL_HEX);
3462 putchar (' ');
3465 if ((unsigned long) segment->p_memsz == segment->p_memsz)
3466 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
3467 else
3469 print_vma (segment->p_offset, FULL_HEX);
3472 printf (" %c%c%c ",
3473 (segment->p_flags & PF_R ? 'R' : ' '),
3474 (segment->p_flags & PF_W ? 'W' : ' '),
3475 (segment->p_flags & PF_X ? 'E' : ' '));
3477 if ((unsigned long) segment->p_align == segment->p_align)
3478 printf ("%#lx", (unsigned long) segment->p_align);
3479 else
3481 print_vma (segment->p_align, PREFIX_HEX);
3484 else
3486 print_vma (segment->p_offset, FULL_HEX);
3487 putchar (' ');
3488 print_vma (segment->p_vaddr, FULL_HEX);
3489 putchar (' ');
3490 print_vma (segment->p_paddr, FULL_HEX);
3491 printf ("\n ");
3492 print_vma (segment->p_filesz, FULL_HEX);
3493 putchar (' ');
3494 print_vma (segment->p_memsz, FULL_HEX);
3495 printf (" %c%c%c ",
3496 (segment->p_flags & PF_R ? 'R' : ' '),
3497 (segment->p_flags & PF_W ? 'W' : ' '),
3498 (segment->p_flags & PF_X ? 'E' : ' '));
3499 print_vma (segment->p_align, HEX);
3503 switch (segment->p_type)
3505 case PT_DYNAMIC:
3506 if (dynamic_addr)
3507 error (_("more than one dynamic segment\n"));
3509 /* By default, assume that the .dynamic section is the first
3510 section in the DYNAMIC segment. */
3511 dynamic_addr = segment->p_offset;
3512 dynamic_size = segment->p_filesz;
3514 /* Try to locate the .dynamic section. If there is
3515 a section header table, we can easily locate it. */
3516 if (section_headers != NULL)
3518 Elf_Internal_Shdr * sec;
3520 sec = find_section (".dynamic");
3521 if (sec == NULL || sec->sh_size == 0)
3523 error (_("no .dynamic section in the dynamic segment\n"));
3524 break;
3527 if (sec->sh_type == SHT_NOBITS)
3529 dynamic_size = 0;
3530 break;
3533 dynamic_addr = sec->sh_offset;
3534 dynamic_size = sec->sh_size;
3536 if (dynamic_addr < segment->p_offset
3537 || dynamic_addr > segment->p_offset + segment->p_filesz)
3538 warn (_("the .dynamic section is not contained"
3539 " within the dynamic segment\n"));
3540 else if (dynamic_addr > segment->p_offset)
3541 warn (_("the .dynamic section is not the first section"
3542 " in the dynamic segment.\n"));
3544 break;
3546 case PT_INTERP:
3547 if (fseek (file, archive_file_offset + (long) segment->p_offset,
3548 SEEK_SET))
3549 error (_("Unable to find program interpreter name\n"));
3550 else
3552 char fmt [32];
3553 int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX);
3555 if (ret >= (int) sizeof (fmt) || ret < 0)
3556 error (_("Internal error: failed to create format string to display program interpreter\n"));
3558 program_interpreter[0] = 0;
3559 if (fscanf (file, fmt, program_interpreter) <= 0)
3560 error (_("Unable to read program interpreter name\n"));
3562 if (do_segments)
3563 printf (_("\n [Requesting program interpreter: %s]"),
3564 program_interpreter);
3566 break;
3569 if (do_segments)
3570 putc ('\n', stdout);
3573 if (do_segments && section_headers != NULL && string_table != NULL)
3575 printf (_("\n Section to Segment mapping:\n"));
3576 printf (_(" Segment Sections...\n"));
3578 for (i = 0; i < elf_header.e_phnum; i++)
3580 unsigned int j;
3581 Elf_Internal_Shdr * section;
3583 segment = program_headers + i;
3584 section = section_headers + 1;
3586 printf (" %2.2d ", i);
3588 for (j = 1; j < elf_header.e_shnum; j++, section++)
3590 if (ELF_IS_SECTION_IN_SEGMENT_MEMORY (section, segment))
3591 printf ("%s ", SECTION_NAME (section));
3594 putc ('\n',stdout);
3598 return 1;
3602 /* Find the file offset corresponding to VMA by using the program headers. */
3604 static long
3605 offset_from_vma (FILE * file, bfd_vma vma, bfd_size_type size)
3607 Elf_Internal_Phdr * seg;
3609 if (! get_program_headers (file))
3611 warn (_("Cannot interpret virtual addresses without program headers.\n"));
3612 return (long) vma;
3615 for (seg = program_headers;
3616 seg < program_headers + elf_header.e_phnum;
3617 ++seg)
3619 if (seg->p_type != PT_LOAD)
3620 continue;
3622 if (vma >= (seg->p_vaddr & -seg->p_align)
3623 && vma + size <= seg->p_vaddr + seg->p_filesz)
3624 return vma - seg->p_vaddr + seg->p_offset;
3627 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
3628 (unsigned long) vma);
3629 return (long) vma;
3633 static int
3634 get_32bit_section_headers (FILE * file, unsigned int num)
3636 Elf32_External_Shdr * shdrs;
3637 Elf_Internal_Shdr * internal;
3638 unsigned int i;
3640 shdrs = get_data (NULL, file, elf_header.e_shoff,
3641 elf_header.e_shentsize, num, _("section headers"));
3642 if (!shdrs)
3643 return 0;
3645 section_headers = cmalloc (num, sizeof (Elf_Internal_Shdr));
3647 if (section_headers == NULL)
3649 error (_("Out of memory\n"));
3650 return 0;
3653 for (i = 0, internal = section_headers;
3654 i < num;
3655 i++, internal++)
3657 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3658 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3659 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
3660 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
3661 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3662 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
3663 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3664 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3665 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3666 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
3669 free (shdrs);
3671 return 1;
3674 static int
3675 get_64bit_section_headers (FILE * file, unsigned int num)
3677 Elf64_External_Shdr * shdrs;
3678 Elf_Internal_Shdr * internal;
3679 unsigned int i;
3681 shdrs = get_data (NULL, file, elf_header.e_shoff,
3682 elf_header.e_shentsize, num, _("section headers"));
3683 if (!shdrs)
3684 return 0;
3686 section_headers = cmalloc (num, sizeof (Elf_Internal_Shdr));
3688 if (section_headers == NULL)
3690 error (_("Out of memory\n"));
3691 return 0;
3694 for (i = 0, internal = section_headers;
3695 i < num;
3696 i++, internal++)
3698 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3699 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3700 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
3701 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
3702 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
3703 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
3704 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3705 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3706 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3707 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3710 free (shdrs);
3712 return 1;
3715 static Elf_Internal_Sym *
3716 get_32bit_elf_symbols (FILE * file, Elf_Internal_Shdr * section)
3718 unsigned long number;
3719 Elf32_External_Sym * esyms;
3720 Elf_External_Sym_Shndx * shndx;
3721 Elf_Internal_Sym * isyms;
3722 Elf_Internal_Sym * psym;
3723 unsigned int j;
3725 esyms = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
3726 _("symbols"));
3727 if (!esyms)
3728 return NULL;
3730 shndx = NULL;
3731 if (symtab_shndx_hdr != NULL
3732 && (symtab_shndx_hdr->sh_link
3733 == (unsigned long) (section - section_headers)))
3735 shndx = get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3736 1, symtab_shndx_hdr->sh_size, _("symtab shndx"));
3737 if (!shndx)
3739 free (esyms);
3740 return NULL;
3744 number = section->sh_size / section->sh_entsize;
3745 isyms = cmalloc (number, sizeof (Elf_Internal_Sym));
3747 if (isyms == NULL)
3749 error (_("Out of memory\n"));
3750 if (shndx)
3751 free (shndx);
3752 free (esyms);
3753 return NULL;
3756 for (j = 0, psym = isyms;
3757 j < number;
3758 j++, psym++)
3760 psym->st_name = BYTE_GET (esyms[j].st_name);
3761 psym->st_value = BYTE_GET (esyms[j].st_value);
3762 psym->st_size = BYTE_GET (esyms[j].st_size);
3763 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3764 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
3765 psym->st_shndx
3766 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3767 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
3768 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
3769 psym->st_info = BYTE_GET (esyms[j].st_info);
3770 psym->st_other = BYTE_GET (esyms[j].st_other);
3773 if (shndx)
3774 free (shndx);
3775 free (esyms);
3777 return isyms;
3780 static Elf_Internal_Sym *
3781 get_64bit_elf_symbols (FILE * file, Elf_Internal_Shdr * section)
3783 unsigned long number;
3784 Elf64_External_Sym * esyms;
3785 Elf_External_Sym_Shndx * shndx;
3786 Elf_Internal_Sym * isyms;
3787 Elf_Internal_Sym * psym;
3788 unsigned int j;
3790 esyms = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
3791 _("symbols"));
3792 if (!esyms)
3793 return NULL;
3795 shndx = NULL;
3796 if (symtab_shndx_hdr != NULL
3797 && (symtab_shndx_hdr->sh_link
3798 == (unsigned long) (section - section_headers)))
3800 shndx = get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3801 1, symtab_shndx_hdr->sh_size, _("symtab shndx"));
3802 if (!shndx)
3804 free (esyms);
3805 return NULL;
3809 number = section->sh_size / section->sh_entsize;
3810 isyms = cmalloc (number, sizeof (Elf_Internal_Sym));
3812 if (isyms == NULL)
3814 error (_("Out of memory\n"));
3815 if (shndx)
3816 free (shndx);
3817 free (esyms);
3818 return NULL;
3821 for (j = 0, psym = isyms;
3822 j < number;
3823 j++, psym++)
3825 psym->st_name = BYTE_GET (esyms[j].st_name);
3826 psym->st_info = BYTE_GET (esyms[j].st_info);
3827 psym->st_other = BYTE_GET (esyms[j].st_other);
3828 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3829 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
3830 psym->st_shndx
3831 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3832 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
3833 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
3834 psym->st_value = BYTE_GET (esyms[j].st_value);
3835 psym->st_size = BYTE_GET (esyms[j].st_size);
3838 if (shndx)
3839 free (shndx);
3840 free (esyms);
3842 return isyms;
3845 static const char *
3846 get_elf_section_flags (bfd_vma sh_flags)
3848 static char buff[1024];
3849 char * p = buff;
3850 int field_size = is_32bit_elf ? 8 : 16;
3851 int index, size = sizeof (buff) - (field_size + 4 + 1);
3852 bfd_vma os_flags = 0;
3853 bfd_vma proc_flags = 0;
3854 bfd_vma unknown_flags = 0;
3855 static const struct
3857 const char * str;
3858 int len;
3860 flags [] =
3862 { "WRITE", 5 },
3863 { "ALLOC", 5 },
3864 { "EXEC", 4 },
3865 { "MERGE", 5 },
3866 { "STRINGS", 7 },
3867 { "INFO LINK", 9 },
3868 { "LINK ORDER", 10 },
3869 { "OS NONCONF", 10 },
3870 { "GROUP", 5 },
3871 { "TLS", 3 },
3872 /* IA-64 specific. */
3873 { "SHORT", 5 },
3874 { "NORECOV", 7 },
3875 /* IA-64 OpenVMS specific. */
3876 { "VMS_GLOBAL", 10 },
3877 { "VMS_OVERLAID", 12 },
3878 { "VMS_SHARED", 10 },
3879 { "VMS_VECTOR", 10 },
3880 { "VMS_ALLOC_64BIT", 15 },
3881 { "VMS_PROTECTED", 13}
3884 if (do_section_details)
3886 sprintf (buff, "[%*.*lx]: ",
3887 field_size, field_size, (unsigned long) sh_flags);
3888 p += field_size + 4;
3891 while (sh_flags)
3893 bfd_vma flag;
3895 flag = sh_flags & - sh_flags;
3896 sh_flags &= ~ flag;
3898 if (do_section_details)
3900 switch (flag)
3902 case SHF_WRITE: index = 0; break;
3903 case SHF_ALLOC: index = 1; break;
3904 case SHF_EXECINSTR: index = 2; break;
3905 case SHF_MERGE: index = 3; break;
3906 case SHF_STRINGS: index = 4; break;
3907 case SHF_INFO_LINK: index = 5; break;
3908 case SHF_LINK_ORDER: index = 6; break;
3909 case SHF_OS_NONCONFORMING: index = 7; break;
3910 case SHF_GROUP: index = 8; break;
3911 case SHF_TLS: index = 9; break;
3913 default:
3914 index = -1;
3915 if (elf_header.e_machine == EM_IA_64)
3917 if (flag == SHF_IA_64_SHORT)
3918 index = 10;
3919 else if (flag == SHF_IA_64_NORECOV)
3920 index = 11;
3921 #ifdef BFD64
3922 else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3923 switch (flag)
3925 case SHF_IA_64_VMS_GLOBAL: index = 12; break;
3926 case SHF_IA_64_VMS_OVERLAID: index = 13; break;
3927 case SHF_IA_64_VMS_SHARED: index = 14; break;
3928 case SHF_IA_64_VMS_VECTOR: index = 15; break;
3929 case SHF_IA_64_VMS_ALLOC_64BIT: index = 16; break;
3930 case SHF_IA_64_VMS_PROTECTED: index = 17; break;
3931 default: break;
3933 #endif
3935 break;
3938 if (index != -1)
3940 if (p != buff + field_size + 4)
3942 if (size < (10 + 2))
3943 abort ();
3944 size -= 2;
3945 *p++ = ',';
3946 *p++ = ' ';
3949 size -= flags [index].len;
3950 p = stpcpy (p, flags [index].str);
3952 else if (flag & SHF_MASKOS)
3953 os_flags |= flag;
3954 else if (flag & SHF_MASKPROC)
3955 proc_flags |= flag;
3956 else
3957 unknown_flags |= flag;
3959 else
3961 switch (flag)
3963 case SHF_WRITE: *p = 'W'; break;
3964 case SHF_ALLOC: *p = 'A'; break;
3965 case SHF_EXECINSTR: *p = 'X'; break;
3966 case SHF_MERGE: *p = 'M'; break;
3967 case SHF_STRINGS: *p = 'S'; break;
3968 case SHF_INFO_LINK: *p = 'I'; break;
3969 case SHF_LINK_ORDER: *p = 'L'; break;
3970 case SHF_OS_NONCONFORMING: *p = 'O'; break;
3971 case SHF_GROUP: *p = 'G'; break;
3972 case SHF_TLS: *p = 'T'; break;
3974 default:
3975 if ((elf_header.e_machine == EM_X86_64
3976 || elf_header.e_machine == EM_L1OM)
3977 && flag == SHF_X86_64_LARGE)
3978 *p = 'l';
3979 else if (flag & SHF_MASKOS)
3981 *p = 'o';
3982 sh_flags &= ~ SHF_MASKOS;
3984 else if (flag & SHF_MASKPROC)
3986 *p = 'p';
3987 sh_flags &= ~ SHF_MASKPROC;
3989 else
3990 *p = 'x';
3991 break;
3993 p++;
3997 if (do_section_details)
3999 if (os_flags)
4001 size -= 5 + field_size;
4002 if (p != buff + field_size + 4)
4004 if (size < (2 + 1))
4005 abort ();
4006 size -= 2;
4007 *p++ = ',';
4008 *p++ = ' ';
4010 sprintf (p, "OS (%*.*lx)", field_size, field_size,
4011 (unsigned long) os_flags);
4012 p += 5 + field_size;
4014 if (proc_flags)
4016 size -= 7 + field_size;
4017 if (p != buff + field_size + 4)
4019 if (size < (2 + 1))
4020 abort ();
4021 size -= 2;
4022 *p++ = ',';
4023 *p++ = ' ';
4025 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
4026 (unsigned long) proc_flags);
4027 p += 7 + field_size;
4029 if (unknown_flags)
4031 size -= 10 + field_size;
4032 if (p != buff + field_size + 4)
4034 if (size < (2 + 1))
4035 abort ();
4036 size -= 2;
4037 *p++ = ',';
4038 *p++ = ' ';
4040 sprintf (p, "UNKNOWN (%*.*lx)", field_size, field_size,
4041 (unsigned long) unknown_flags);
4042 p += 10 + field_size;
4046 *p = '\0';
4047 return buff;
4050 static int
4051 process_section_headers (FILE * file)
4053 Elf_Internal_Shdr * section;
4054 unsigned int i;
4056 section_headers = NULL;
4058 if (elf_header.e_shnum == 0)
4060 if (do_sections)
4061 printf (_("\nThere are no sections in this file.\n"));
4063 return 1;
4066 if (do_sections && !do_header)
4067 printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
4068 elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
4070 if (is_32bit_elf)
4072 if (! get_32bit_section_headers (file, elf_header.e_shnum))
4073 return 0;
4075 else if (! get_64bit_section_headers (file, elf_header.e_shnum))
4076 return 0;
4078 /* Read in the string table, so that we have names to display. */
4079 if (elf_header.e_shstrndx != SHN_UNDEF
4080 && elf_header.e_shstrndx < elf_header.e_shnum)
4082 section = section_headers + elf_header.e_shstrndx;
4084 if (section->sh_size != 0)
4086 string_table = get_data (NULL, file, section->sh_offset,
4087 1, section->sh_size, _("string table"));
4089 string_table_length = string_table != NULL ? section->sh_size : 0;
4093 /* Scan the sections for the dynamic symbol table
4094 and dynamic string table and debug sections. */
4095 dynamic_symbols = NULL;
4096 dynamic_strings = NULL;
4097 dynamic_syminfo = NULL;
4098 symtab_shndx_hdr = NULL;
4100 eh_addr_size = is_32bit_elf ? 4 : 8;
4101 switch (elf_header.e_machine)
4103 case EM_MIPS:
4104 case EM_MIPS_RS3_LE:
4105 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
4106 FDE addresses. However, the ABI also has a semi-official ILP32
4107 variant for which the normal FDE address size rules apply.
4109 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
4110 section, where XX is the size of longs in bits. Unfortunately,
4111 earlier compilers provided no way of distinguishing ILP32 objects
4112 from LP64 objects, so if there's any doubt, we should assume that
4113 the official LP64 form is being used. */
4114 if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
4115 && find_section (".gcc_compiled_long32") == NULL)
4116 eh_addr_size = 8;
4117 break;
4119 case EM_H8_300:
4120 case EM_H8_300H:
4121 switch (elf_header.e_flags & EF_H8_MACH)
4123 case E_H8_MACH_H8300:
4124 case E_H8_MACH_H8300HN:
4125 case E_H8_MACH_H8300SN:
4126 case E_H8_MACH_H8300SXN:
4127 eh_addr_size = 2;
4128 break;
4129 case E_H8_MACH_H8300H:
4130 case E_H8_MACH_H8300S:
4131 case E_H8_MACH_H8300SX:
4132 eh_addr_size = 4;
4133 break;
4135 break;
4137 case EM_M32C_OLD:
4138 case EM_M32C:
4139 switch (elf_header.e_flags & EF_M32C_CPU_MASK)
4141 case EF_M32C_CPU_M16C:
4142 eh_addr_size = 2;
4143 break;
4145 break;
4148 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
4149 do \
4151 size_t expected_entsize \
4152 = is_32bit_elf ? size32 : size64; \
4153 if (section->sh_entsize != expected_entsize) \
4154 error (_("Section %d has invalid sh_entsize %lx (expected %lx)\n"), \
4155 i, (unsigned long int) section->sh_entsize, \
4156 (unsigned long int) expected_entsize); \
4157 section->sh_entsize = expected_entsize; \
4159 while (0)
4160 #define CHECK_ENTSIZE(section, i, type) \
4161 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
4162 sizeof (Elf64_External_##type))
4164 for (i = 0, section = section_headers;
4165 i < elf_header.e_shnum;
4166 i++, section++)
4168 char * name = SECTION_NAME (section);
4170 if (section->sh_type == SHT_DYNSYM)
4172 if (dynamic_symbols != NULL)
4174 error (_("File contains multiple dynamic symbol tables\n"));
4175 continue;
4178 CHECK_ENTSIZE (section, i, Sym);
4179 num_dynamic_syms = section->sh_size / section->sh_entsize;
4180 dynamic_symbols = GET_ELF_SYMBOLS (file, section);
4182 else if (section->sh_type == SHT_STRTAB
4183 && streq (name, ".dynstr"))
4185 if (dynamic_strings != NULL)
4187 error (_("File contains multiple dynamic string tables\n"));
4188 continue;
4191 dynamic_strings = get_data (NULL, file, section->sh_offset,
4192 1, section->sh_size, _("dynamic strings"));
4193 dynamic_strings_length = section->sh_size;
4195 else if (section->sh_type == SHT_SYMTAB_SHNDX)
4197 if (symtab_shndx_hdr != NULL)
4199 error (_("File contains multiple symtab shndx tables\n"));
4200 continue;
4202 symtab_shndx_hdr = section;
4204 else if (section->sh_type == SHT_SYMTAB)
4205 CHECK_ENTSIZE (section, i, Sym);
4206 else if (section->sh_type == SHT_GROUP)
4207 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
4208 else if (section->sh_type == SHT_REL)
4209 CHECK_ENTSIZE (section, i, Rel);
4210 else if (section->sh_type == SHT_RELA)
4211 CHECK_ENTSIZE (section, i, Rela);
4212 else if ((do_debugging || do_debug_info || do_debug_abbrevs
4213 || do_debug_lines || do_debug_pubnames
4214 || do_debug_aranges || do_debug_frames || do_debug_macinfo
4215 || do_debug_str || do_debug_loc || do_debug_ranges)
4216 && (const_strneq (name, ".debug_")
4217 || const_strneq (name, ".zdebug_")))
4219 if (name[1] == 'z')
4220 name += sizeof (".zdebug_") - 1;
4221 else
4222 name += sizeof (".debug_") - 1;
4224 if (do_debugging
4225 || (do_debug_info && streq (name, "info"))
4226 || (do_debug_abbrevs && streq (name, "abbrev"))
4227 || (do_debug_lines && streq (name, "line"))
4228 || (do_debug_pubnames && streq (name, "pubnames"))
4229 || (do_debug_aranges && streq (name, "aranges"))
4230 || (do_debug_ranges && streq (name, "ranges"))
4231 || (do_debug_frames && streq (name, "frame"))
4232 || (do_debug_macinfo && streq (name, "macinfo"))
4233 || (do_debug_str && streq (name, "str"))
4234 || (do_debug_loc && streq (name, "loc"))
4236 request_dump_bynumber (i, DEBUG_DUMP);
4238 /* Linkonce section to be combined with .debug_info at link time. */
4239 else if ((do_debugging || do_debug_info)
4240 && const_strneq (name, ".gnu.linkonce.wi."))
4241 request_dump_bynumber (i, DEBUG_DUMP);
4242 else if (do_debug_frames && streq (name, ".eh_frame"))
4243 request_dump_bynumber (i, DEBUG_DUMP);
4246 if (! do_sections)
4247 return 1;
4249 if (elf_header.e_shnum > 1)
4250 printf (_("\nSection Headers:\n"));
4251 else
4252 printf (_("\nSection Header:\n"));
4254 if (is_32bit_elf)
4256 if (do_section_details)
4258 printf (_(" [Nr] Name\n"));
4259 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
4261 else
4262 printf
4263 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
4265 else if (do_wide)
4267 if (do_section_details)
4269 printf (_(" [Nr] Name\n"));
4270 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
4272 else
4273 printf
4274 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
4276 else
4278 if (do_section_details)
4280 printf (_(" [Nr] Name\n"));
4281 printf (_(" Type Address Offset Link\n"));
4282 printf (_(" Size EntSize Info Align\n"));
4284 else
4286 printf (_(" [Nr] Name Type Address Offset\n"));
4287 printf (_(" Size EntSize Flags Link Info Align\n"));
4291 if (do_section_details)
4292 printf (_(" Flags\n"));
4294 for (i = 0, section = section_headers;
4295 i < elf_header.e_shnum;
4296 i++, section++)
4298 if (do_section_details)
4300 printf (" [%2u] %s\n",
4302 SECTION_NAME (section));
4303 if (is_32bit_elf || do_wide)
4304 printf (" %-15.15s ",
4305 get_section_type_name (section->sh_type));
4307 else
4308 printf ((do_wide ? " [%2u] %-17s %-15s "
4309 : " [%2u] %-17.17s %-15.15s "),
4311 SECTION_NAME (section),
4312 get_section_type_name (section->sh_type));
4314 if (is_32bit_elf)
4316 print_vma (section->sh_addr, LONG_HEX);
4318 printf ( " %6.6lx %6.6lx %2.2lx",
4319 (unsigned long) section->sh_offset,
4320 (unsigned long) section->sh_size,
4321 (unsigned long) section->sh_entsize);
4323 if (do_section_details)
4324 fputs (" ", stdout);
4325 else
4326 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4328 printf ("%2u %3u %2lu\n",
4329 section->sh_link,
4330 section->sh_info,
4331 (unsigned long) section->sh_addralign);
4333 else if (do_wide)
4335 print_vma (section->sh_addr, LONG_HEX);
4337 if ((long) section->sh_offset == section->sh_offset)
4338 printf (" %6.6lx", (unsigned long) section->sh_offset);
4339 else
4341 putchar (' ');
4342 print_vma (section->sh_offset, LONG_HEX);
4345 if ((unsigned long) section->sh_size == section->sh_size)
4346 printf (" %6.6lx", (unsigned long) section->sh_size);
4347 else
4349 putchar (' ');
4350 print_vma (section->sh_size, LONG_HEX);
4353 if ((unsigned long) section->sh_entsize == section->sh_entsize)
4354 printf (" %2.2lx", (unsigned long) section->sh_entsize);
4355 else
4357 putchar (' ');
4358 print_vma (section->sh_entsize, LONG_HEX);
4361 if (do_section_details)
4362 fputs (" ", stdout);
4363 else
4364 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4366 printf ("%2u %3u ", section->sh_link, section->sh_info);
4368 if ((unsigned long) section->sh_addralign == section->sh_addralign)
4369 printf ("%2lu\n", (unsigned long) section->sh_addralign);
4370 else
4372 print_vma (section->sh_addralign, DEC);
4373 putchar ('\n');
4376 else if (do_section_details)
4378 printf (" %-15.15s ",
4379 get_section_type_name (section->sh_type));
4380 print_vma (section->sh_addr, LONG_HEX);
4381 if ((long) section->sh_offset == section->sh_offset)
4382 printf (" %16.16lx", (unsigned long) section->sh_offset);
4383 else
4385 printf (" ");
4386 print_vma (section->sh_offset, LONG_HEX);
4388 printf (" %u\n ", section->sh_link);
4389 print_vma (section->sh_size, LONG_HEX);
4390 putchar (' ');
4391 print_vma (section->sh_entsize, LONG_HEX);
4393 printf (" %-16u %lu\n",
4394 section->sh_info,
4395 (unsigned long) section->sh_addralign);
4397 else
4399 putchar (' ');
4400 print_vma (section->sh_addr, LONG_HEX);
4401 if ((long) section->sh_offset == section->sh_offset)
4402 printf (" %8.8lx", (unsigned long) section->sh_offset);
4403 else
4405 printf (" ");
4406 print_vma (section->sh_offset, LONG_HEX);
4408 printf ("\n ");
4409 print_vma (section->sh_size, LONG_HEX);
4410 printf (" ");
4411 print_vma (section->sh_entsize, LONG_HEX);
4413 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4415 printf (" %2u %3u %lu\n",
4416 section->sh_link,
4417 section->sh_info,
4418 (unsigned long) section->sh_addralign);
4421 if (do_section_details)
4422 printf (" %s\n", get_elf_section_flags (section->sh_flags));
4425 if (!do_section_details)
4426 printf (_("Key to Flags:\n\
4427 W (write), A (alloc), X (execute), M (merge), S (strings)\n\
4428 I (info), L (link order), G (group), x (unknown)\n\
4429 O (extra OS processing required) o (OS specific), p (processor specific)\n"));
4431 return 1;
4434 static const char *
4435 get_group_flags (unsigned int flags)
4437 static char buff[32];
4438 switch (flags)
4440 case GRP_COMDAT:
4441 return "COMDAT";
4443 default:
4444 snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x]"), flags);
4445 break;
4447 return buff;
4450 static int
4451 process_section_groups (FILE * file)
4453 Elf_Internal_Shdr * section;
4454 unsigned int i;
4455 struct group * group;
4456 Elf_Internal_Shdr * symtab_sec;
4457 Elf_Internal_Shdr * strtab_sec;
4458 Elf_Internal_Sym * symtab;
4459 char * strtab;
4460 size_t strtab_size;
4462 /* Don't process section groups unless needed. */
4463 if (!do_unwind && !do_section_groups)
4464 return 1;
4466 if (elf_header.e_shnum == 0)
4468 if (do_section_groups)
4469 printf (_("\nThere are no sections in this file.\n"));
4471 return 1;
4474 if (section_headers == NULL)
4476 error (_("Section headers are not available!\n"));
4477 abort ();
4480 section_headers_groups = calloc (elf_header.e_shnum,
4481 sizeof (struct group *));
4483 if (section_headers_groups == NULL)
4485 error (_("Out of memory\n"));
4486 return 0;
4489 /* Scan the sections for the group section. */
4490 group_count = 0;
4491 for (i = 0, section = section_headers;
4492 i < elf_header.e_shnum;
4493 i++, section++)
4494 if (section->sh_type == SHT_GROUP)
4495 group_count++;
4497 if (group_count == 0)
4499 if (do_section_groups)
4500 printf (_("\nThere are no section groups in this file.\n"));
4502 return 1;
4505 section_groups = calloc (group_count, sizeof (struct group));
4507 if (section_groups == NULL)
4509 error (_("Out of memory\n"));
4510 return 0;
4513 symtab_sec = NULL;
4514 strtab_sec = NULL;
4515 symtab = NULL;
4516 strtab = NULL;
4517 strtab_size = 0;
4518 for (i = 0, section = section_headers, group = section_groups;
4519 i < elf_header.e_shnum;
4520 i++, section++)
4522 if (section->sh_type == SHT_GROUP)
4524 char * name = SECTION_NAME (section);
4525 char * group_name;
4526 unsigned char * start;
4527 unsigned char * indices;
4528 unsigned int entry, j, size;
4529 Elf_Internal_Shdr * sec;
4530 Elf_Internal_Sym * sym;
4532 /* Get the symbol table. */
4533 if (section->sh_link >= elf_header.e_shnum
4534 || ((sec = section_headers + section->sh_link)->sh_type
4535 != SHT_SYMTAB))
4537 error (_("Bad sh_link in group section `%s'\n"), name);
4538 continue;
4541 if (symtab_sec != sec)
4543 symtab_sec = sec;
4544 if (symtab)
4545 free (symtab);
4546 symtab = GET_ELF_SYMBOLS (file, symtab_sec);
4549 sym = symtab + section->sh_info;
4551 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4553 if (sym->st_shndx == 0
4554 || sym->st_shndx >= elf_header.e_shnum)
4556 error (_("Bad sh_info in group section `%s'\n"), name);
4557 continue;
4560 group_name = SECTION_NAME (section_headers + sym->st_shndx);
4561 strtab_sec = NULL;
4562 if (strtab)
4563 free (strtab);
4564 strtab = NULL;
4565 strtab_size = 0;
4567 else
4569 /* Get the string table. */
4570 if (symtab_sec->sh_link >= elf_header.e_shnum)
4572 strtab_sec = NULL;
4573 if (strtab)
4574 free (strtab);
4575 strtab = NULL;
4576 strtab_size = 0;
4578 else if (strtab_sec
4579 != (sec = section_headers + symtab_sec->sh_link))
4581 strtab_sec = sec;
4582 if (strtab)
4583 free (strtab);
4584 strtab = get_data (NULL, file, strtab_sec->sh_offset,
4585 1, strtab_sec->sh_size,
4586 _("string table"));
4587 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
4589 group_name = sym->st_name < strtab_size
4590 ? strtab + sym->st_name : "<corrupt>";
4593 start = get_data (NULL, file, section->sh_offset,
4594 1, section->sh_size, _("section data"));
4596 indices = start;
4597 size = (section->sh_size / section->sh_entsize) - 1;
4598 entry = byte_get (indices, 4);
4599 indices += 4;
4601 if (do_section_groups)
4603 printf ("\n%s group section [%5u] `%s' [%s] contains %u sections:\n",
4604 get_group_flags (entry), i, name, group_name, size);
4606 printf (_(" [Index] Name\n"));
4609 group->group_index = i;
4611 for (j = 0; j < size; j++)
4613 struct group_list * g;
4615 entry = byte_get (indices, 4);
4616 indices += 4;
4618 if (entry >= elf_header.e_shnum)
4620 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
4621 entry, i, elf_header.e_shnum - 1);
4622 continue;
4625 if (section_headers_groups [entry] != NULL)
4627 if (entry)
4629 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
4630 entry, i,
4631 section_headers_groups [entry]->group_index);
4632 continue;
4634 else
4636 /* Intel C/C++ compiler may put section 0 in a
4637 section group. We just warn it the first time
4638 and ignore it afterwards. */
4639 static int warned = 0;
4640 if (!warned)
4642 error (_("section 0 in group section [%5u]\n"),
4643 section_headers_groups [entry]->group_index);
4644 warned++;
4649 section_headers_groups [entry] = group;
4651 if (do_section_groups)
4653 sec = section_headers + entry;
4654 printf (" [%5u] %s\n", entry, SECTION_NAME (sec));
4657 g = xmalloc (sizeof (struct group_list));
4658 g->section_index = entry;
4659 g->next = group->root;
4660 group->root = g;
4663 if (start)
4664 free (start);
4666 group++;
4670 if (symtab)
4671 free (symtab);
4672 if (strtab)
4673 free (strtab);
4674 return 1;
4677 static struct
4679 const char * name;
4680 int reloc;
4681 int size;
4682 int rela;
4683 } dynamic_relocations [] =
4685 { "REL", DT_REL, DT_RELSZ, FALSE },
4686 { "RELA", DT_RELA, DT_RELASZ, TRUE },
4687 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
4690 /* Process the reloc section. */
4692 static int
4693 process_relocs (FILE * file)
4695 unsigned long rel_size;
4696 unsigned long rel_offset;
4699 if (!do_reloc)
4700 return 1;
4702 if (do_using_dynamic)
4704 int is_rela;
4705 const char * name;
4706 int has_dynamic_reloc;
4707 unsigned int i;
4709 has_dynamic_reloc = 0;
4711 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
4713 is_rela = dynamic_relocations [i].rela;
4714 name = dynamic_relocations [i].name;
4715 rel_size = dynamic_info [dynamic_relocations [i].size];
4716 rel_offset = dynamic_info [dynamic_relocations [i].reloc];
4718 has_dynamic_reloc |= rel_size;
4720 if (is_rela == UNKNOWN)
4722 if (dynamic_relocations [i].reloc == DT_JMPREL)
4723 switch (dynamic_info[DT_PLTREL])
4725 case DT_REL:
4726 is_rela = FALSE;
4727 break;
4728 case DT_RELA:
4729 is_rela = TRUE;
4730 break;
4734 if (rel_size)
4736 printf
4737 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
4738 name, rel_offset, rel_size);
4740 dump_relocations (file,
4741 offset_from_vma (file, rel_offset, rel_size),
4742 rel_size,
4743 dynamic_symbols, num_dynamic_syms,
4744 dynamic_strings, dynamic_strings_length, is_rela);
4748 if (! has_dynamic_reloc)
4749 printf (_("\nThere are no dynamic relocations in this file.\n"));
4751 else
4753 Elf_Internal_Shdr * section;
4754 unsigned long i;
4755 int found = 0;
4757 for (i = 0, section = section_headers;
4758 i < elf_header.e_shnum;
4759 i++, section++)
4761 if ( section->sh_type != SHT_RELA
4762 && section->sh_type != SHT_REL)
4763 continue;
4765 rel_offset = section->sh_offset;
4766 rel_size = section->sh_size;
4768 if (rel_size)
4770 Elf_Internal_Shdr * strsec;
4771 int is_rela;
4773 printf (_("\nRelocation section "));
4775 if (string_table == NULL)
4776 printf ("%d", section->sh_name);
4777 else
4778 printf (_("'%s'"), SECTION_NAME (section));
4780 printf (_(" at offset 0x%lx contains %lu entries:\n"),
4781 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
4783 is_rela = section->sh_type == SHT_RELA;
4785 if (section->sh_link != 0
4786 && section->sh_link < elf_header.e_shnum)
4788 Elf_Internal_Shdr * symsec;
4789 Elf_Internal_Sym * symtab;
4790 unsigned long nsyms;
4791 unsigned long strtablen = 0;
4792 char * strtab = NULL;
4794 symsec = section_headers + section->sh_link;
4795 if (symsec->sh_type != SHT_SYMTAB
4796 && symsec->sh_type != SHT_DYNSYM)
4797 continue;
4799 nsyms = symsec->sh_size / symsec->sh_entsize;
4800 symtab = GET_ELF_SYMBOLS (file, symsec);
4802 if (symtab == NULL)
4803 continue;
4805 if (symsec->sh_link != 0
4806 && symsec->sh_link < elf_header.e_shnum)
4808 strsec = section_headers + symsec->sh_link;
4810 strtab = get_data (NULL, file, strsec->sh_offset,
4811 1, strsec->sh_size,
4812 _("string table"));
4813 strtablen = strtab == NULL ? 0 : strsec->sh_size;
4816 dump_relocations (file, rel_offset, rel_size,
4817 symtab, nsyms, strtab, strtablen, is_rela);
4818 if (strtab)
4819 free (strtab);
4820 free (symtab);
4822 else
4823 dump_relocations (file, rel_offset, rel_size,
4824 NULL, 0, NULL, 0, is_rela);
4826 found = 1;
4830 if (! found)
4831 printf (_("\nThere are no relocations in this file.\n"));
4834 return 1;
4837 /* Process the unwind section. */
4839 #include "unwind-ia64.h"
4841 /* An absolute address consists of a section and an offset. If the
4842 section is NULL, the offset itself is the address, otherwise, the
4843 address equals to LOAD_ADDRESS(section) + offset. */
4845 struct absaddr
4847 unsigned short section;
4848 bfd_vma offset;
4851 #define ABSADDR(a) \
4852 ((a).section \
4853 ? section_headers [(a).section].sh_addr + (a).offset \
4854 : (a).offset)
4856 struct ia64_unw_aux_info
4858 struct ia64_unw_table_entry
4860 struct absaddr start;
4861 struct absaddr end;
4862 struct absaddr info;
4864 *table; /* Unwind table. */
4865 unsigned long table_len; /* Length of unwind table. */
4866 unsigned char * info; /* Unwind info. */
4867 unsigned long info_size; /* Size of unwind info. */
4868 bfd_vma info_addr; /* starting address of unwind info. */
4869 bfd_vma seg_base; /* Starting address of segment. */
4870 Elf_Internal_Sym * symtab; /* The symbol table. */
4871 unsigned long nsyms; /* Number of symbols. */
4872 char * strtab; /* The string table. */
4873 unsigned long strtab_size; /* Size of string table. */
4876 static void
4877 find_symbol_for_address (Elf_Internal_Sym * symtab,
4878 unsigned long nsyms,
4879 const char * strtab,
4880 unsigned long strtab_size,
4881 struct absaddr addr,
4882 const char ** symname,
4883 bfd_vma * offset)
4885 bfd_vma dist = 0x100000;
4886 Elf_Internal_Sym * sym;
4887 Elf_Internal_Sym * best = NULL;
4888 unsigned long i;
4890 for (i = 0, sym = symtab; i < nsyms; ++i, ++sym)
4892 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
4893 && sym->st_name != 0
4894 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
4895 && addr.offset >= sym->st_value
4896 && addr.offset - sym->st_value < dist)
4898 best = sym;
4899 dist = addr.offset - sym->st_value;
4900 if (!dist)
4901 break;
4904 if (best)
4906 *symname = (best->st_name >= strtab_size
4907 ? "<corrupt>" : strtab + best->st_name);
4908 *offset = dist;
4909 return;
4911 *symname = NULL;
4912 *offset = addr.offset;
4915 static void
4916 dump_ia64_unwind (struct ia64_unw_aux_info * aux)
4918 struct ia64_unw_table_entry * tp;
4919 int in_body;
4921 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
4923 bfd_vma stamp;
4924 bfd_vma offset;
4925 const unsigned char * dp;
4926 const unsigned char * head;
4927 const char * procname;
4929 find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
4930 aux->strtab_size, tp->start, &procname, &offset);
4932 fputs ("\n<", stdout);
4934 if (procname)
4936 fputs (procname, stdout);
4938 if (offset)
4939 printf ("+%lx", (unsigned long) offset);
4942 fputs (">: [", stdout);
4943 print_vma (tp->start.offset, PREFIX_HEX);
4944 fputc ('-', stdout);
4945 print_vma (tp->end.offset, PREFIX_HEX);
4946 printf ("], info at +0x%lx\n",
4947 (unsigned long) (tp->info.offset - aux->seg_base));
4949 head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
4950 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
4952 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
4953 (unsigned) UNW_VER (stamp),
4954 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
4955 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
4956 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
4957 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
4959 if (UNW_VER (stamp) != 1)
4961 printf ("\tUnknown version.\n");
4962 continue;
4965 in_body = 0;
4966 for (dp = head + 8; dp < head + 8 + eh_addr_size * UNW_LENGTH (stamp);)
4967 dp = unw_decode (dp, in_body, & in_body);
4971 static int
4972 slurp_ia64_unwind_table (FILE * file,
4973 struct ia64_unw_aux_info * aux,
4974 Elf_Internal_Shdr * sec)
4976 unsigned long size, nrelas, i;
4977 Elf_Internal_Phdr * seg;
4978 struct ia64_unw_table_entry * tep;
4979 Elf_Internal_Shdr * relsec;
4980 Elf_Internal_Rela * rela;
4981 Elf_Internal_Rela * rp;
4982 unsigned char * table;
4983 unsigned char * tp;
4984 Elf_Internal_Sym * sym;
4985 const char * relname;
4987 /* First, find the starting address of the segment that includes
4988 this section: */
4990 if (elf_header.e_phnum)
4992 if (! get_program_headers (file))
4993 return 0;
4995 for (seg = program_headers;
4996 seg < program_headers + elf_header.e_phnum;
4997 ++seg)
4999 if (seg->p_type != PT_LOAD)
5000 continue;
5002 if (sec->sh_addr >= seg->p_vaddr
5003 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
5005 aux->seg_base = seg->p_vaddr;
5006 break;
5011 /* Second, build the unwind table from the contents of the unwind section: */
5012 size = sec->sh_size;
5013 table = get_data (NULL, file, sec->sh_offset, 1, size, _("unwind table"));
5014 if (!table)
5015 return 0;
5017 aux->table = xcmalloc (size / (3 * eh_addr_size), sizeof (aux->table[0]));
5018 tep = aux->table;
5019 for (tp = table; tp < table + size; ++tep)
5021 tep->start.section = SHN_UNDEF;
5022 tep->end.section = SHN_UNDEF;
5023 tep->info.section = SHN_UNDEF;
5024 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5025 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5026 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5027 tep->start.offset += aux->seg_base;
5028 tep->end.offset += aux->seg_base;
5029 tep->info.offset += aux->seg_base;
5031 free (table);
5033 /* Third, apply any relocations to the unwind table: */
5034 for (relsec = section_headers;
5035 relsec < section_headers + elf_header.e_shnum;
5036 ++relsec)
5038 if (relsec->sh_type != SHT_RELA
5039 || relsec->sh_info >= elf_header.e_shnum
5040 || section_headers + relsec->sh_info != sec)
5041 continue;
5043 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5044 & rela, & nrelas))
5045 return 0;
5047 for (rp = rela; rp < rela + nrelas; ++rp)
5049 relname = elf_ia64_reloc_type (get_reloc_type (rp->r_info));
5050 sym = aux->symtab + get_reloc_symindex (rp->r_info);
5052 if (! const_strneq (relname, "R_IA64_SEGREL"))
5054 warn (_("Skipping unexpected relocation type %s\n"), relname);
5055 continue;
5058 i = rp->r_offset / (3 * eh_addr_size);
5060 switch (rp->r_offset/eh_addr_size % 3)
5062 case 0:
5063 aux->table[i].start.section = sym->st_shndx;
5064 aux->table[i].start.offset += rp->r_addend + sym->st_value;
5065 break;
5066 case 1:
5067 aux->table[i].end.section = sym->st_shndx;
5068 aux->table[i].end.offset += rp->r_addend + sym->st_value;
5069 break;
5070 case 2:
5071 aux->table[i].info.section = sym->st_shndx;
5072 aux->table[i].info.offset += rp->r_addend + sym->st_value;
5073 break;
5074 default:
5075 break;
5079 free (rela);
5082 aux->table_len = size / (3 * eh_addr_size);
5083 return 1;
5086 static int
5087 ia64_process_unwind (FILE * file)
5089 Elf_Internal_Shdr * sec;
5090 Elf_Internal_Shdr * unwsec = NULL;
5091 Elf_Internal_Shdr * strsec;
5092 unsigned long i, unwcount = 0, unwstart = 0;
5093 struct ia64_unw_aux_info aux;
5095 memset (& aux, 0, sizeof (aux));
5097 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5099 if (sec->sh_type == SHT_SYMTAB
5100 && sec->sh_link < elf_header.e_shnum)
5102 aux.nsyms = sec->sh_size / sec->sh_entsize;
5103 aux.symtab = GET_ELF_SYMBOLS (file, sec);
5105 strsec = section_headers + sec->sh_link;
5106 aux.strtab = get_data (NULL, file, strsec->sh_offset,
5107 1, strsec->sh_size, _("string table"));
5108 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5110 else if (sec->sh_type == SHT_IA_64_UNWIND)
5111 unwcount++;
5114 if (!unwcount)
5115 printf (_("\nThere are no unwind sections in this file.\n"));
5117 while (unwcount-- > 0)
5119 char * suffix;
5120 size_t len, len2;
5122 for (i = unwstart, sec = section_headers + unwstart;
5123 i < elf_header.e_shnum; ++i, ++sec)
5124 if (sec->sh_type == SHT_IA_64_UNWIND)
5126 unwsec = sec;
5127 break;
5130 unwstart = i + 1;
5131 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
5133 if ((unwsec->sh_flags & SHF_GROUP) != 0)
5135 /* We need to find which section group it is in. */
5136 struct group_list * g = section_headers_groups [i]->root;
5138 for (; g != NULL; g = g->next)
5140 sec = section_headers + g->section_index;
5142 if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
5143 break;
5146 if (g == NULL)
5147 i = elf_header.e_shnum;
5149 else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
5151 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
5152 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
5153 suffix = SECTION_NAME (unwsec) + len;
5154 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5155 ++i, ++sec)
5156 if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
5157 && streq (SECTION_NAME (sec) + len2, suffix))
5158 break;
5160 else
5162 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
5163 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
5164 len = sizeof (ELF_STRING_ia64_unwind) - 1;
5165 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
5166 suffix = "";
5167 if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
5168 suffix = SECTION_NAME (unwsec) + len;
5169 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5170 ++i, ++sec)
5171 if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
5172 && streq (SECTION_NAME (sec) + len2, suffix))
5173 break;
5176 if (i == elf_header.e_shnum)
5178 printf (_("\nCould not find unwind info section for "));
5180 if (string_table == NULL)
5181 printf ("%d", unwsec->sh_name);
5182 else
5183 printf (_("'%s'"), SECTION_NAME (unwsec));
5185 else
5187 aux.info_size = sec->sh_size;
5188 aux.info_addr = sec->sh_addr;
5189 aux.info = get_data (NULL, file, sec->sh_offset, 1, aux.info_size,
5190 _("unwind info"));
5192 printf (_("\nUnwind section "));
5194 if (string_table == NULL)
5195 printf ("%d", unwsec->sh_name);
5196 else
5197 printf (_("'%s'"), SECTION_NAME (unwsec));
5199 printf (_(" at offset 0x%lx contains %lu entries:\n"),
5200 (unsigned long) unwsec->sh_offset,
5201 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
5203 (void) slurp_ia64_unwind_table (file, & aux, unwsec);
5205 if (aux.table_len > 0)
5206 dump_ia64_unwind (& aux);
5208 if (aux.table)
5209 free ((char *) aux.table);
5210 if (aux.info)
5211 free ((char *) aux.info);
5212 aux.table = NULL;
5213 aux.info = NULL;
5217 if (aux.symtab)
5218 free (aux.symtab);
5219 if (aux.strtab)
5220 free ((char *) aux.strtab);
5222 return 1;
5225 struct hppa_unw_aux_info
5227 struct hppa_unw_table_entry
5229 struct absaddr start;
5230 struct absaddr end;
5231 unsigned int Cannot_unwind:1; /* 0 */
5232 unsigned int Millicode:1; /* 1 */
5233 unsigned int Millicode_save_sr0:1; /* 2 */
5234 unsigned int Region_description:2; /* 3..4 */
5235 unsigned int reserved1:1; /* 5 */
5236 unsigned int Entry_SR:1; /* 6 */
5237 unsigned int Entry_FR:4; /* number saved */ /* 7..10 */
5238 unsigned int Entry_GR:5; /* number saved */ /* 11..15 */
5239 unsigned int Args_stored:1; /* 16 */
5240 unsigned int Variable_Frame:1; /* 17 */
5241 unsigned int Separate_Package_Body:1; /* 18 */
5242 unsigned int Frame_Extension_Millicode:1; /* 19 */
5243 unsigned int Stack_Overflow_Check:1; /* 20 */
5244 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
5245 unsigned int Ada_Region:1; /* 22 */
5246 unsigned int cxx_info:1; /* 23 */
5247 unsigned int cxx_try_catch:1; /* 24 */
5248 unsigned int sched_entry_seq:1; /* 25 */
5249 unsigned int reserved2:1; /* 26 */
5250 unsigned int Save_SP:1; /* 27 */
5251 unsigned int Save_RP:1; /* 28 */
5252 unsigned int Save_MRP_in_frame:1; /* 29 */
5253 unsigned int extn_ptr_defined:1; /* 30 */
5254 unsigned int Cleanup_defined:1; /* 31 */
5256 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
5257 unsigned int HP_UX_interrupt_marker:1; /* 1 */
5258 unsigned int Large_frame:1; /* 2 */
5259 unsigned int Pseudo_SP_Set:1; /* 3 */
5260 unsigned int reserved4:1; /* 4 */
5261 unsigned int Total_frame_size:27; /* 5..31 */
5263 *table; /* Unwind table. */
5264 unsigned long table_len; /* Length of unwind table. */
5265 bfd_vma seg_base; /* Starting address of segment. */
5266 Elf_Internal_Sym * symtab; /* The symbol table. */
5267 unsigned long nsyms; /* Number of symbols. */
5268 char * strtab; /* The string table. */
5269 unsigned long strtab_size; /* Size of string table. */
5272 static void
5273 dump_hppa_unwind (struct hppa_unw_aux_info * aux)
5275 struct hppa_unw_table_entry * tp;
5277 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
5279 bfd_vma offset;
5280 const char * procname;
5282 find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
5283 aux->strtab_size, tp->start, &procname,
5284 &offset);
5286 fputs ("\n<", stdout);
5288 if (procname)
5290 fputs (procname, stdout);
5292 if (offset)
5293 printf ("+%lx", (unsigned long) offset);
5296 fputs (">: [", stdout);
5297 print_vma (tp->start.offset, PREFIX_HEX);
5298 fputc ('-', stdout);
5299 print_vma (tp->end.offset, PREFIX_HEX);
5300 printf ("]\n\t");
5302 #define PF(_m) if (tp->_m) printf (#_m " ");
5303 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
5304 PF(Cannot_unwind);
5305 PF(Millicode);
5306 PF(Millicode_save_sr0);
5307 /* PV(Region_description); */
5308 PF(Entry_SR);
5309 PV(Entry_FR);
5310 PV(Entry_GR);
5311 PF(Args_stored);
5312 PF(Variable_Frame);
5313 PF(Separate_Package_Body);
5314 PF(Frame_Extension_Millicode);
5315 PF(Stack_Overflow_Check);
5316 PF(Two_Instruction_SP_Increment);
5317 PF(Ada_Region);
5318 PF(cxx_info);
5319 PF(cxx_try_catch);
5320 PF(sched_entry_seq);
5321 PF(Save_SP);
5322 PF(Save_RP);
5323 PF(Save_MRP_in_frame);
5324 PF(extn_ptr_defined);
5325 PF(Cleanup_defined);
5326 PF(MPE_XL_interrupt_marker);
5327 PF(HP_UX_interrupt_marker);
5328 PF(Large_frame);
5329 PF(Pseudo_SP_Set);
5330 PV(Total_frame_size);
5331 #undef PF
5332 #undef PV
5335 printf ("\n");
5338 static int
5339 slurp_hppa_unwind_table (FILE * file,
5340 struct hppa_unw_aux_info * aux,
5341 Elf_Internal_Shdr * sec)
5343 unsigned long size, unw_ent_size, nentries, nrelas, i;
5344 Elf_Internal_Phdr * seg;
5345 struct hppa_unw_table_entry * tep;
5346 Elf_Internal_Shdr * relsec;
5347 Elf_Internal_Rela * rela;
5348 Elf_Internal_Rela * rp;
5349 unsigned char * table;
5350 unsigned char * tp;
5351 Elf_Internal_Sym * sym;
5352 const char * relname;
5354 /* First, find the starting address of the segment that includes
5355 this section. */
5357 if (elf_header.e_phnum)
5359 if (! get_program_headers (file))
5360 return 0;
5362 for (seg = program_headers;
5363 seg < program_headers + elf_header.e_phnum;
5364 ++seg)
5366 if (seg->p_type != PT_LOAD)
5367 continue;
5369 if (sec->sh_addr >= seg->p_vaddr
5370 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
5372 aux->seg_base = seg->p_vaddr;
5373 break;
5378 /* Second, build the unwind table from the contents of the unwind
5379 section. */
5380 size = sec->sh_size;
5381 table = get_data (NULL, file, sec->sh_offset, 1, size, _("unwind table"));
5382 if (!table)
5383 return 0;
5385 unw_ent_size = 16;
5386 nentries = size / unw_ent_size;
5387 size = unw_ent_size * nentries;
5389 tep = aux->table = xcmalloc (nentries, sizeof (aux->table[0]));
5391 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
5393 unsigned int tmp1, tmp2;
5395 tep->start.section = SHN_UNDEF;
5396 tep->end.section = SHN_UNDEF;
5398 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
5399 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
5400 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
5401 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
5403 tep->start.offset += aux->seg_base;
5404 tep->end.offset += aux->seg_base;
5406 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
5407 tep->Millicode = (tmp1 >> 30) & 0x1;
5408 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
5409 tep->Region_description = (tmp1 >> 27) & 0x3;
5410 tep->reserved1 = (tmp1 >> 26) & 0x1;
5411 tep->Entry_SR = (tmp1 >> 25) & 0x1;
5412 tep->Entry_FR = (tmp1 >> 21) & 0xf;
5413 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
5414 tep->Args_stored = (tmp1 >> 15) & 0x1;
5415 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
5416 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
5417 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
5418 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
5419 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
5420 tep->Ada_Region = (tmp1 >> 9) & 0x1;
5421 tep->cxx_info = (tmp1 >> 8) & 0x1;
5422 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
5423 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
5424 tep->reserved2 = (tmp1 >> 5) & 0x1;
5425 tep->Save_SP = (tmp1 >> 4) & 0x1;
5426 tep->Save_RP = (tmp1 >> 3) & 0x1;
5427 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
5428 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
5429 tep->Cleanup_defined = tmp1 & 0x1;
5431 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
5432 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
5433 tep->Large_frame = (tmp2 >> 29) & 0x1;
5434 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
5435 tep->reserved4 = (tmp2 >> 27) & 0x1;
5436 tep->Total_frame_size = tmp2 & 0x7ffffff;
5438 free (table);
5440 /* Third, apply any relocations to the unwind table. */
5441 for (relsec = section_headers;
5442 relsec < section_headers + elf_header.e_shnum;
5443 ++relsec)
5445 if (relsec->sh_type != SHT_RELA
5446 || relsec->sh_info >= elf_header.e_shnum
5447 || section_headers + relsec->sh_info != sec)
5448 continue;
5450 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5451 & rela, & nrelas))
5452 return 0;
5454 for (rp = rela; rp < rela + nrelas; ++rp)
5456 relname = elf_hppa_reloc_type (get_reloc_type (rp->r_info));
5457 sym = aux->symtab + get_reloc_symindex (rp->r_info);
5459 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
5460 if (! const_strneq (relname, "R_PARISC_SEGREL"))
5462 warn (_("Skipping unexpected relocation type %s\n"), relname);
5463 continue;
5466 i = rp->r_offset / unw_ent_size;
5468 switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
5470 case 0:
5471 aux->table[i].start.section = sym->st_shndx;
5472 aux->table[i].start.offset += sym->st_value + rp->r_addend;
5473 break;
5474 case 1:
5475 aux->table[i].end.section = sym->st_shndx;
5476 aux->table[i].end.offset += sym->st_value + rp->r_addend;
5477 break;
5478 default:
5479 break;
5483 free (rela);
5486 aux->table_len = nentries;
5488 return 1;
5491 static int
5492 hppa_process_unwind (FILE * file)
5494 struct hppa_unw_aux_info aux;
5495 Elf_Internal_Shdr * unwsec = NULL;
5496 Elf_Internal_Shdr * strsec;
5497 Elf_Internal_Shdr * sec;
5498 unsigned long i;
5500 memset (& aux, 0, sizeof (aux));
5502 if (string_table == NULL)
5503 return 1;
5505 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5507 if (sec->sh_type == SHT_SYMTAB
5508 && sec->sh_link < elf_header.e_shnum)
5510 aux.nsyms = sec->sh_size / sec->sh_entsize;
5511 aux.symtab = GET_ELF_SYMBOLS (file, sec);
5513 strsec = section_headers + sec->sh_link;
5514 aux.strtab = get_data (NULL, file, strsec->sh_offset,
5515 1, strsec->sh_size, _("string table"));
5516 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5518 else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
5519 unwsec = sec;
5522 if (!unwsec)
5523 printf (_("\nThere are no unwind sections in this file.\n"));
5525 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5527 if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
5529 printf (_("\nUnwind section "));
5530 printf (_("'%s'"), SECTION_NAME (sec));
5532 printf (_(" at offset 0x%lx contains %lu entries:\n"),
5533 (unsigned long) sec->sh_offset,
5534 (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
5536 slurp_hppa_unwind_table (file, &aux, sec);
5537 if (aux.table_len > 0)
5538 dump_hppa_unwind (&aux);
5540 if (aux.table)
5541 free ((char *) aux.table);
5542 aux.table = NULL;
5546 if (aux.symtab)
5547 free (aux.symtab);
5548 if (aux.strtab)
5549 free ((char *) aux.strtab);
5551 return 1;
5554 static int
5555 process_unwind (FILE * file)
5557 struct unwind_handler
5559 int machtype;
5560 int (* handler)(FILE *);
5561 } handlers[] =
5563 { EM_IA_64, ia64_process_unwind },
5564 { EM_PARISC, hppa_process_unwind },
5565 { 0, 0 }
5567 int i;
5569 if (!do_unwind)
5570 return 1;
5572 for (i = 0; handlers[i].handler != NULL; i++)
5573 if (elf_header.e_machine == handlers[i].machtype)
5574 return handlers[i].handler (file);
5576 printf (_("\nThere are no unwind sections in this file.\n"));
5577 return 1;
5580 static void
5581 dynamic_section_mips_val (Elf_Internal_Dyn * entry)
5583 switch (entry->d_tag)
5585 case DT_MIPS_FLAGS:
5586 if (entry->d_un.d_val == 0)
5587 printf ("NONE\n");
5588 else
5590 static const char * opts[] =
5592 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
5593 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
5594 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
5595 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
5596 "RLD_ORDER_SAFE"
5598 unsigned int cnt;
5599 int first = 1;
5600 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
5601 if (entry->d_un.d_val & (1 << cnt))
5603 printf ("%s%s", first ? "" : " ", opts[cnt]);
5604 first = 0;
5606 puts ("");
5608 break;
5610 case DT_MIPS_IVERSION:
5611 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
5612 printf ("Interface Version: %s\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
5613 else
5614 printf ("<corrupt: %ld>\n", (long) entry->d_un.d_ptr);
5615 break;
5617 case DT_MIPS_TIME_STAMP:
5619 char timebuf[20];
5620 struct tm * tmp;
5622 time_t time = entry->d_un.d_val;
5623 tmp = gmtime (&time);
5624 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
5625 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
5626 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
5627 printf ("Time Stamp: %s\n", timebuf);
5629 break;
5631 case DT_MIPS_RLD_VERSION:
5632 case DT_MIPS_LOCAL_GOTNO:
5633 case DT_MIPS_CONFLICTNO:
5634 case DT_MIPS_LIBLISTNO:
5635 case DT_MIPS_SYMTABNO:
5636 case DT_MIPS_UNREFEXTNO:
5637 case DT_MIPS_HIPAGENO:
5638 case DT_MIPS_DELTA_CLASS_NO:
5639 case DT_MIPS_DELTA_INSTANCE_NO:
5640 case DT_MIPS_DELTA_RELOC_NO:
5641 case DT_MIPS_DELTA_SYM_NO:
5642 case DT_MIPS_DELTA_CLASSSYM_NO:
5643 case DT_MIPS_COMPACT_SIZE:
5644 printf ("%ld\n", (long) entry->d_un.d_ptr);
5645 break;
5647 default:
5648 printf ("%#lx\n", (unsigned long) entry->d_un.d_ptr);
5653 static void
5654 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
5656 switch (entry->d_tag)
5658 case DT_HP_DLD_FLAGS:
5660 static struct
5662 long int bit;
5663 const char * str;
5665 flags[] =
5667 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
5668 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
5669 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
5670 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
5671 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
5672 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
5673 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
5674 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
5675 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
5676 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
5677 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
5678 { DT_HP_GST, "HP_GST" },
5679 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
5680 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
5681 { DT_HP_NODELETE, "HP_NODELETE" },
5682 { DT_HP_GROUP, "HP_GROUP" },
5683 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
5685 int first = 1;
5686 size_t cnt;
5687 bfd_vma val = entry->d_un.d_val;
5689 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
5690 if (val & flags[cnt].bit)
5692 if (! first)
5693 putchar (' ');
5694 fputs (flags[cnt].str, stdout);
5695 first = 0;
5696 val ^= flags[cnt].bit;
5699 if (val != 0 || first)
5701 if (! first)
5702 putchar (' ');
5703 print_vma (val, HEX);
5706 break;
5708 default:
5709 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5710 break;
5712 putchar ('\n');
5715 static void
5716 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
5718 switch (entry->d_tag)
5720 case DT_IA_64_PLT_RESERVE:
5721 /* First 3 slots reserved. */
5722 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5723 printf (" -- ");
5724 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
5725 break;
5727 default:
5728 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5729 break;
5731 putchar ('\n');
5734 static int
5735 get_32bit_dynamic_section (FILE * file)
5737 Elf32_External_Dyn * edyn;
5738 Elf32_External_Dyn * ext;
5739 Elf_Internal_Dyn * entry;
5741 edyn = get_data (NULL, file, dynamic_addr, 1, dynamic_size,
5742 _("dynamic section"));
5743 if (!edyn)
5744 return 0;
5746 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
5747 might not have the luxury of section headers. Look for the DT_NULL
5748 terminator to determine the number of entries. */
5749 for (ext = edyn, dynamic_nent = 0;
5750 (char *) ext < (char *) edyn + dynamic_size;
5751 ext++)
5753 dynamic_nent++;
5754 if (BYTE_GET (ext->d_tag) == DT_NULL)
5755 break;
5758 dynamic_section = cmalloc (dynamic_nent, sizeof (* entry));
5759 if (dynamic_section == NULL)
5761 error (_("Out of memory\n"));
5762 free (edyn);
5763 return 0;
5766 for (ext = edyn, entry = dynamic_section;
5767 entry < dynamic_section + dynamic_nent;
5768 ext++, entry++)
5770 entry->d_tag = BYTE_GET (ext->d_tag);
5771 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
5774 free (edyn);
5776 return 1;
5779 static int
5780 get_64bit_dynamic_section (FILE * file)
5782 Elf64_External_Dyn * edyn;
5783 Elf64_External_Dyn * ext;
5784 Elf_Internal_Dyn * entry;
5786 edyn = get_data (NULL, file, dynamic_addr, 1, dynamic_size,
5787 _("dynamic section"));
5788 if (!edyn)
5789 return 0;
5791 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
5792 might not have the luxury of section headers. Look for the DT_NULL
5793 terminator to determine the number of entries. */
5794 for (ext = edyn, dynamic_nent = 0;
5795 (char *) ext < (char *) edyn + dynamic_size;
5796 ext++)
5798 dynamic_nent++;
5799 if (BYTE_GET (ext->d_tag) == DT_NULL)
5800 break;
5803 dynamic_section = cmalloc (dynamic_nent, sizeof (* entry));
5804 if (dynamic_section == NULL)
5806 error (_("Out of memory\n"));
5807 free (edyn);
5808 return 0;
5811 for (ext = edyn, entry = dynamic_section;
5812 entry < dynamic_section + dynamic_nent;
5813 ext++, entry++)
5815 entry->d_tag = BYTE_GET (ext->d_tag);
5816 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
5819 free (edyn);
5821 return 1;
5824 static void
5825 print_dynamic_flags (bfd_vma flags)
5827 int first = 1;
5829 while (flags)
5831 bfd_vma flag;
5833 flag = flags & - flags;
5834 flags &= ~ flag;
5836 if (first)
5837 first = 0;
5838 else
5839 putc (' ', stdout);
5841 switch (flag)
5843 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
5844 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
5845 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
5846 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
5847 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
5848 default: fputs ("unknown", stdout); break;
5851 puts ("");
5854 /* Parse and display the contents of the dynamic section. */
5856 static int
5857 process_dynamic_section (FILE * file)
5859 Elf_Internal_Dyn * entry;
5861 if (dynamic_size == 0)
5863 if (do_dynamic)
5864 printf (_("\nThere is no dynamic section in this file.\n"));
5866 return 1;
5869 if (is_32bit_elf)
5871 if (! get_32bit_dynamic_section (file))
5872 return 0;
5874 else if (! get_64bit_dynamic_section (file))
5875 return 0;
5877 /* Find the appropriate symbol table. */
5878 if (dynamic_symbols == NULL)
5880 for (entry = dynamic_section;
5881 entry < dynamic_section + dynamic_nent;
5882 ++entry)
5884 Elf_Internal_Shdr section;
5886 if (entry->d_tag != DT_SYMTAB)
5887 continue;
5889 dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
5891 /* Since we do not know how big the symbol table is,
5892 we default to reading in the entire file (!) and
5893 processing that. This is overkill, I know, but it
5894 should work. */
5895 section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
5897 if (archive_file_offset != 0)
5898 section.sh_size = archive_file_size - section.sh_offset;
5899 else
5901 if (fseek (file, 0, SEEK_END))
5902 error (_("Unable to seek to end of file!\n"));
5904 section.sh_size = ftell (file) - section.sh_offset;
5907 if (is_32bit_elf)
5908 section.sh_entsize = sizeof (Elf32_External_Sym);
5909 else
5910 section.sh_entsize = sizeof (Elf64_External_Sym);
5912 num_dynamic_syms = section.sh_size / section.sh_entsize;
5913 if (num_dynamic_syms < 1)
5915 error (_("Unable to determine the number of symbols to load\n"));
5916 continue;
5919 dynamic_symbols = GET_ELF_SYMBOLS (file, &section);
5923 /* Similarly find a string table. */
5924 if (dynamic_strings == NULL)
5926 for (entry = dynamic_section;
5927 entry < dynamic_section + dynamic_nent;
5928 ++entry)
5930 unsigned long offset;
5931 long str_tab_len;
5933 if (entry->d_tag != DT_STRTAB)
5934 continue;
5936 dynamic_info[DT_STRTAB] = entry->d_un.d_val;
5938 /* Since we do not know how big the string table is,
5939 we default to reading in the entire file (!) and
5940 processing that. This is overkill, I know, but it
5941 should work. */
5943 offset = offset_from_vma (file, entry->d_un.d_val, 0);
5945 if (archive_file_offset != 0)
5946 str_tab_len = archive_file_size - offset;
5947 else
5949 if (fseek (file, 0, SEEK_END))
5950 error (_("Unable to seek to end of file\n"));
5951 str_tab_len = ftell (file) - offset;
5954 if (str_tab_len < 1)
5956 error
5957 (_("Unable to determine the length of the dynamic string table\n"));
5958 continue;
5961 dynamic_strings = get_data (NULL, file, offset, 1, str_tab_len,
5962 _("dynamic string table"));
5963 dynamic_strings_length = str_tab_len;
5964 break;
5968 /* And find the syminfo section if available. */
5969 if (dynamic_syminfo == NULL)
5971 unsigned long syminsz = 0;
5973 for (entry = dynamic_section;
5974 entry < dynamic_section + dynamic_nent;
5975 ++entry)
5977 if (entry->d_tag == DT_SYMINENT)
5979 /* Note: these braces are necessary to avoid a syntax
5980 error from the SunOS4 C compiler. */
5981 assert (sizeof (Elf_External_Syminfo) == entry->d_un.d_val);
5983 else if (entry->d_tag == DT_SYMINSZ)
5984 syminsz = entry->d_un.d_val;
5985 else if (entry->d_tag == DT_SYMINFO)
5986 dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
5987 syminsz);
5990 if (dynamic_syminfo_offset != 0 && syminsz != 0)
5992 Elf_External_Syminfo * extsyminfo;
5993 Elf_External_Syminfo * extsym;
5994 Elf_Internal_Syminfo * syminfo;
5996 /* There is a syminfo section. Read the data. */
5997 extsyminfo = get_data (NULL, file, dynamic_syminfo_offset, 1,
5998 syminsz, _("symbol information"));
5999 if (!extsyminfo)
6000 return 0;
6002 dynamic_syminfo = malloc (syminsz);
6003 if (dynamic_syminfo == NULL)
6005 error (_("Out of memory\n"));
6006 return 0;
6009 dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
6010 for (syminfo = dynamic_syminfo, extsym = extsyminfo;
6011 syminfo < dynamic_syminfo + dynamic_syminfo_nent;
6012 ++syminfo, ++extsym)
6014 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
6015 syminfo->si_flags = BYTE_GET (extsym->si_flags);
6018 free (extsyminfo);
6022 if (do_dynamic && dynamic_addr)
6023 printf (_("\nDynamic section at offset 0x%lx contains %u entries:\n"),
6024 dynamic_addr, dynamic_nent);
6025 if (do_dynamic)
6026 printf (_(" Tag Type Name/Value\n"));
6028 for (entry = dynamic_section;
6029 entry < dynamic_section + dynamic_nent;
6030 entry++)
6032 if (do_dynamic)
6034 const char * dtype;
6036 putchar (' ');
6037 print_vma (entry->d_tag, FULL_HEX);
6038 dtype = get_dynamic_type (entry->d_tag);
6039 printf (" (%s)%*s", dtype,
6040 ((is_32bit_elf ? 27 : 19)
6041 - (int) strlen (dtype)),
6042 " ");
6045 switch (entry->d_tag)
6047 case DT_FLAGS:
6048 if (do_dynamic)
6049 print_dynamic_flags (entry->d_un.d_val);
6050 break;
6052 case DT_AUXILIARY:
6053 case DT_FILTER:
6054 case DT_CONFIG:
6055 case DT_DEPAUDIT:
6056 case DT_AUDIT:
6057 if (do_dynamic)
6059 switch (entry->d_tag)
6061 case DT_AUXILIARY:
6062 printf (_("Auxiliary library"));
6063 break;
6065 case DT_FILTER:
6066 printf (_("Filter library"));
6067 break;
6069 case DT_CONFIG:
6070 printf (_("Configuration file"));
6071 break;
6073 case DT_DEPAUDIT:
6074 printf (_("Dependency audit library"));
6075 break;
6077 case DT_AUDIT:
6078 printf (_("Audit library"));
6079 break;
6082 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
6083 printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
6084 else
6086 printf (": ");
6087 print_vma (entry->d_un.d_val, PREFIX_HEX);
6088 putchar ('\n');
6091 break;
6093 case DT_FEATURE:
6094 if (do_dynamic)
6096 printf (_("Flags:"));
6098 if (entry->d_un.d_val == 0)
6099 printf (_(" None\n"));
6100 else
6102 unsigned long int val = entry->d_un.d_val;
6104 if (val & DTF_1_PARINIT)
6106 printf (" PARINIT");
6107 val ^= DTF_1_PARINIT;
6109 if (val & DTF_1_CONFEXP)
6111 printf (" CONFEXP");
6112 val ^= DTF_1_CONFEXP;
6114 if (val != 0)
6115 printf (" %lx", val);
6116 puts ("");
6119 break;
6121 case DT_POSFLAG_1:
6122 if (do_dynamic)
6124 printf (_("Flags:"));
6126 if (entry->d_un.d_val == 0)
6127 printf (_(" None\n"));
6128 else
6130 unsigned long int val = entry->d_un.d_val;
6132 if (val & DF_P1_LAZYLOAD)
6134 printf (" LAZYLOAD");
6135 val ^= DF_P1_LAZYLOAD;
6137 if (val & DF_P1_GROUPPERM)
6139 printf (" GROUPPERM");
6140 val ^= DF_P1_GROUPPERM;
6142 if (val != 0)
6143 printf (" %lx", val);
6144 puts ("");
6147 break;
6149 case DT_FLAGS_1:
6150 if (do_dynamic)
6152 printf (_("Flags:"));
6153 if (entry->d_un.d_val == 0)
6154 printf (_(" None\n"));
6155 else
6157 unsigned long int val = entry->d_un.d_val;
6159 if (val & DF_1_NOW)
6161 printf (" NOW");
6162 val ^= DF_1_NOW;
6164 if (val & DF_1_GLOBAL)
6166 printf (" GLOBAL");
6167 val ^= DF_1_GLOBAL;
6169 if (val & DF_1_GROUP)
6171 printf (" GROUP");
6172 val ^= DF_1_GROUP;
6174 if (val & DF_1_NODELETE)
6176 printf (" NODELETE");
6177 val ^= DF_1_NODELETE;
6179 if (val & DF_1_LOADFLTR)
6181 printf (" LOADFLTR");
6182 val ^= DF_1_LOADFLTR;
6184 if (val & DF_1_INITFIRST)
6186 printf (" INITFIRST");
6187 val ^= DF_1_INITFIRST;
6189 if (val & DF_1_NOOPEN)
6191 printf (" NOOPEN");
6192 val ^= DF_1_NOOPEN;
6194 if (val & DF_1_ORIGIN)
6196 printf (" ORIGIN");
6197 val ^= DF_1_ORIGIN;
6199 if (val & DF_1_DIRECT)
6201 printf (" DIRECT");
6202 val ^= DF_1_DIRECT;
6204 if (val & DF_1_TRANS)
6206 printf (" TRANS");
6207 val ^= DF_1_TRANS;
6209 if (val & DF_1_INTERPOSE)
6211 printf (" INTERPOSE");
6212 val ^= DF_1_INTERPOSE;
6214 if (val & DF_1_NODEFLIB)
6216 printf (" NODEFLIB");
6217 val ^= DF_1_NODEFLIB;
6219 if (val & DF_1_NODUMP)
6221 printf (" NODUMP");
6222 val ^= DF_1_NODUMP;
6224 if (val & DF_1_CONLFAT)
6226 printf (" CONLFAT");
6227 val ^= DF_1_CONLFAT;
6229 if (val != 0)
6230 printf (" %lx", val);
6231 puts ("");
6234 break;
6236 case DT_PLTREL:
6237 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6238 if (do_dynamic)
6239 puts (get_dynamic_type (entry->d_un.d_val));
6240 break;
6242 case DT_NULL :
6243 case DT_NEEDED :
6244 case DT_PLTGOT :
6245 case DT_HASH :
6246 case DT_STRTAB :
6247 case DT_SYMTAB :
6248 case DT_RELA :
6249 case DT_INIT :
6250 case DT_FINI :
6251 case DT_SONAME :
6252 case DT_RPATH :
6253 case DT_SYMBOLIC:
6254 case DT_REL :
6255 case DT_DEBUG :
6256 case DT_TEXTREL :
6257 case DT_JMPREL :
6258 case DT_RUNPATH :
6259 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6261 if (do_dynamic)
6263 char * name;
6265 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
6266 name = GET_DYNAMIC_NAME (entry->d_un.d_val);
6267 else
6268 name = NULL;
6270 if (name)
6272 switch (entry->d_tag)
6274 case DT_NEEDED:
6275 printf (_("Shared library: [%s]"), name);
6277 if (streq (name, program_interpreter))
6278 printf (_(" program interpreter"));
6279 break;
6281 case DT_SONAME:
6282 printf (_("Library soname: [%s]"), name);
6283 break;
6285 case DT_RPATH:
6286 printf (_("Library rpath: [%s]"), name);
6287 break;
6289 case DT_RUNPATH:
6290 printf (_("Library runpath: [%s]"), name);
6291 break;
6293 default:
6294 print_vma (entry->d_un.d_val, PREFIX_HEX);
6295 break;
6298 else
6299 print_vma (entry->d_un.d_val, PREFIX_HEX);
6301 putchar ('\n');
6303 break;
6305 case DT_PLTRELSZ:
6306 case DT_RELASZ :
6307 case DT_STRSZ :
6308 case DT_RELSZ :
6309 case DT_RELAENT :
6310 case DT_SYMENT :
6311 case DT_RELENT :
6312 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6313 case DT_PLTPADSZ:
6314 case DT_MOVEENT :
6315 case DT_MOVESZ :
6316 case DT_INIT_ARRAYSZ:
6317 case DT_FINI_ARRAYSZ:
6318 case DT_GNU_CONFLICTSZ:
6319 case DT_GNU_LIBLISTSZ:
6320 if (do_dynamic)
6322 print_vma (entry->d_un.d_val, UNSIGNED);
6323 printf (" (bytes)\n");
6325 break;
6327 case DT_VERDEFNUM:
6328 case DT_VERNEEDNUM:
6329 case DT_RELACOUNT:
6330 case DT_RELCOUNT:
6331 if (do_dynamic)
6333 print_vma (entry->d_un.d_val, UNSIGNED);
6334 putchar ('\n');
6336 break;
6338 case DT_SYMINSZ:
6339 case DT_SYMINENT:
6340 case DT_SYMINFO:
6341 case DT_USED:
6342 case DT_INIT_ARRAY:
6343 case DT_FINI_ARRAY:
6344 if (do_dynamic)
6346 if (entry->d_tag == DT_USED
6347 && VALID_DYNAMIC_NAME (entry->d_un.d_val))
6349 char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
6351 if (*name)
6353 printf (_("Not needed object: [%s]\n"), name);
6354 break;
6358 print_vma (entry->d_un.d_val, PREFIX_HEX);
6359 putchar ('\n');
6361 break;
6363 case DT_BIND_NOW:
6364 /* The value of this entry is ignored. */
6365 if (do_dynamic)
6366 putchar ('\n');
6367 break;
6369 case DT_GNU_PRELINKED:
6370 if (do_dynamic)
6372 struct tm * tmp;
6373 time_t time = entry->d_un.d_val;
6375 tmp = gmtime (&time);
6376 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
6377 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
6378 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
6381 break;
6383 case DT_GNU_HASH:
6384 dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
6385 if (do_dynamic)
6387 print_vma (entry->d_un.d_val, PREFIX_HEX);
6388 putchar ('\n');
6390 break;
6392 default:
6393 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
6394 version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
6395 entry->d_un.d_val;
6397 if (do_dynamic)
6399 switch (elf_header.e_machine)
6401 case EM_MIPS:
6402 case EM_MIPS_RS3_LE:
6403 dynamic_section_mips_val (entry);
6404 break;
6405 case EM_PARISC:
6406 dynamic_section_parisc_val (entry);
6407 break;
6408 case EM_IA_64:
6409 dynamic_section_ia64_val (entry);
6410 break;
6411 default:
6412 print_vma (entry->d_un.d_val, PREFIX_HEX);
6413 putchar ('\n');
6416 break;
6420 return 1;
6423 static char *
6424 get_ver_flags (unsigned int flags)
6426 static char buff[32];
6428 buff[0] = 0;
6430 if (flags == 0)
6431 return _("none");
6433 if (flags & VER_FLG_BASE)
6434 strcat (buff, "BASE ");
6436 if (flags & VER_FLG_WEAK)
6438 if (flags & VER_FLG_BASE)
6439 strcat (buff, "| ");
6441 strcat (buff, "WEAK ");
6444 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK))
6445 strcat (buff, "| <unknown>");
6447 return buff;
6450 /* Display the contents of the version sections. */
6452 static int
6453 process_version_sections (FILE * file)
6455 Elf_Internal_Shdr * section;
6456 unsigned i;
6457 int found = 0;
6459 if (! do_version)
6460 return 1;
6462 for (i = 0, section = section_headers;
6463 i < elf_header.e_shnum;
6464 i++, section++)
6466 switch (section->sh_type)
6468 case SHT_GNU_verdef:
6470 Elf_External_Verdef * edefs;
6471 unsigned int idx;
6472 unsigned int cnt;
6473 char * endbuf;
6475 found = 1;
6477 printf
6478 (_("\nVersion definition section '%s' contains %u entries:\n"),
6479 SECTION_NAME (section), section->sh_info);
6481 printf (_(" Addr: 0x"));
6482 printf_vma (section->sh_addr);
6483 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
6484 (unsigned long) section->sh_offset, section->sh_link,
6485 section->sh_link < elf_header.e_shnum
6486 ? SECTION_NAME (section_headers + section->sh_link)
6487 : "<corrupt>");
6489 edefs = get_data (NULL, file, section->sh_offset, 1,
6490 section->sh_size,
6491 _("version definition section"));
6492 endbuf = (char *) edefs + section->sh_size;
6493 if (!edefs)
6494 break;
6496 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
6498 char * vstart;
6499 Elf_External_Verdef * edef;
6500 Elf_Internal_Verdef ent;
6501 Elf_External_Verdaux * eaux;
6502 Elf_Internal_Verdaux aux;
6503 int j;
6504 int isum;
6506 vstart = ((char *) edefs) + idx;
6507 if (vstart + sizeof (*edef) > endbuf)
6508 break;
6510 edef = (Elf_External_Verdef *) vstart;
6512 ent.vd_version = BYTE_GET (edef->vd_version);
6513 ent.vd_flags = BYTE_GET (edef->vd_flags);
6514 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
6515 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
6516 ent.vd_hash = BYTE_GET (edef->vd_hash);
6517 ent.vd_aux = BYTE_GET (edef->vd_aux);
6518 ent.vd_next = BYTE_GET (edef->vd_next);
6520 printf (_(" %#06x: Rev: %d Flags: %s"),
6521 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
6523 printf (_(" Index: %d Cnt: %d "),
6524 ent.vd_ndx, ent.vd_cnt);
6526 vstart += ent.vd_aux;
6528 eaux = (Elf_External_Verdaux *) vstart;
6530 aux.vda_name = BYTE_GET (eaux->vda_name);
6531 aux.vda_next = BYTE_GET (eaux->vda_next);
6533 if (VALID_DYNAMIC_NAME (aux.vda_name))
6534 printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
6535 else
6536 printf (_("Name index: %ld\n"), aux.vda_name);
6538 isum = idx + ent.vd_aux;
6540 for (j = 1; j < ent.vd_cnt; j++)
6542 isum += aux.vda_next;
6543 vstart += aux.vda_next;
6545 eaux = (Elf_External_Verdaux *) vstart;
6546 if (vstart + sizeof (*eaux) > endbuf)
6547 break;
6549 aux.vda_name = BYTE_GET (eaux->vda_name);
6550 aux.vda_next = BYTE_GET (eaux->vda_next);
6552 if (VALID_DYNAMIC_NAME (aux.vda_name))
6553 printf (_(" %#06x: Parent %d: %s\n"),
6554 isum, j, GET_DYNAMIC_NAME (aux.vda_name));
6555 else
6556 printf (_(" %#06x: Parent %d, name index: %ld\n"),
6557 isum, j, aux.vda_name);
6559 if (j < ent.vd_cnt)
6560 printf (_(" Version def aux past end of section\n"));
6562 idx += ent.vd_next;
6564 if (cnt < section->sh_info)
6565 printf (_(" Version definition past end of section\n"));
6567 free (edefs);
6569 break;
6571 case SHT_GNU_verneed:
6573 Elf_External_Verneed * eneed;
6574 unsigned int idx;
6575 unsigned int cnt;
6576 char * endbuf;
6578 found = 1;
6580 printf (_("\nVersion needs section '%s' contains %u entries:\n"),
6581 SECTION_NAME (section), section->sh_info);
6583 printf (_(" Addr: 0x"));
6584 printf_vma (section->sh_addr);
6585 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
6586 (unsigned long) section->sh_offset, section->sh_link,
6587 section->sh_link < elf_header.e_shnum
6588 ? SECTION_NAME (section_headers + section->sh_link)
6589 : "<corrupt>");
6591 eneed = get_data (NULL, file, section->sh_offset, 1,
6592 section->sh_size,
6593 _("version need section"));
6594 endbuf = (char *) eneed + section->sh_size;
6595 if (!eneed)
6596 break;
6598 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
6600 Elf_External_Verneed * entry;
6601 Elf_Internal_Verneed ent;
6602 int j;
6603 int isum;
6604 char * vstart;
6606 vstart = ((char *) eneed) + idx;
6607 if (vstart + sizeof (*entry) > endbuf)
6608 break;
6610 entry = (Elf_External_Verneed *) vstart;
6612 ent.vn_version = BYTE_GET (entry->vn_version);
6613 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
6614 ent.vn_file = BYTE_GET (entry->vn_file);
6615 ent.vn_aux = BYTE_GET (entry->vn_aux);
6616 ent.vn_next = BYTE_GET (entry->vn_next);
6618 printf (_(" %#06x: Version: %d"), idx, ent.vn_version);
6620 if (VALID_DYNAMIC_NAME (ent.vn_file))
6621 printf (_(" File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
6622 else
6623 printf (_(" File: %lx"), ent.vn_file);
6625 printf (_(" Cnt: %d\n"), ent.vn_cnt);
6627 vstart += ent.vn_aux;
6629 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
6631 Elf_External_Vernaux * eaux;
6632 Elf_Internal_Vernaux aux;
6634 if (vstart + sizeof (*eaux) > endbuf)
6635 break;
6636 eaux = (Elf_External_Vernaux *) vstart;
6638 aux.vna_hash = BYTE_GET (eaux->vna_hash);
6639 aux.vna_flags = BYTE_GET (eaux->vna_flags);
6640 aux.vna_other = BYTE_GET (eaux->vna_other);
6641 aux.vna_name = BYTE_GET (eaux->vna_name);
6642 aux.vna_next = BYTE_GET (eaux->vna_next);
6644 if (VALID_DYNAMIC_NAME (aux.vna_name))
6645 printf (_(" %#06x: Name: %s"),
6646 isum, GET_DYNAMIC_NAME (aux.vna_name));
6647 else
6648 printf (_(" %#06x: Name index: %lx"),
6649 isum, aux.vna_name);
6651 printf (_(" Flags: %s Version: %d\n"),
6652 get_ver_flags (aux.vna_flags), aux.vna_other);
6654 isum += aux.vna_next;
6655 vstart += aux.vna_next;
6657 if (j < ent.vn_cnt)
6658 printf (_(" Version need aux past end of section\n"));
6660 idx += ent.vn_next;
6662 if (cnt < section->sh_info)
6663 printf (_(" Version need past end of section\n"));
6665 free (eneed);
6667 break;
6669 case SHT_GNU_versym:
6671 Elf_Internal_Shdr * link_section;
6672 int total;
6673 int cnt;
6674 unsigned char * edata;
6675 unsigned short * data;
6676 char * strtab;
6677 Elf_Internal_Sym * symbols;
6678 Elf_Internal_Shdr * string_sec;
6679 long off;
6681 if (section->sh_link >= elf_header.e_shnum)
6682 break;
6684 link_section = section_headers + section->sh_link;
6685 total = section->sh_size / sizeof (Elf_External_Versym);
6687 if (link_section->sh_link >= elf_header.e_shnum)
6688 break;
6690 found = 1;
6692 symbols = GET_ELF_SYMBOLS (file, link_section);
6694 string_sec = section_headers + link_section->sh_link;
6696 strtab = get_data (NULL, file, string_sec->sh_offset, 1,
6697 string_sec->sh_size, _("version string table"));
6698 if (!strtab)
6699 break;
6701 printf (_("\nVersion symbols section '%s' contains %d entries:\n"),
6702 SECTION_NAME (section), total);
6704 printf (_(" Addr: "));
6705 printf_vma (section->sh_addr);
6706 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
6707 (unsigned long) section->sh_offset, section->sh_link,
6708 SECTION_NAME (link_section));
6710 off = offset_from_vma (file,
6711 version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
6712 total * sizeof (short));
6713 edata = get_data (NULL, file, off, total, sizeof (short),
6714 _("version symbol data"));
6715 if (!edata)
6717 free (strtab);
6718 break;
6721 data = cmalloc (total, sizeof (short));
6723 for (cnt = total; cnt --;)
6724 data[cnt] = byte_get (edata + cnt * sizeof (short),
6725 sizeof (short));
6727 free (edata);
6729 for (cnt = 0; cnt < total; cnt += 4)
6731 int j, nn;
6732 int check_def, check_need;
6733 char * name;
6735 printf (" %03x:", cnt);
6737 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
6738 switch (data[cnt + j])
6740 case 0:
6741 fputs (_(" 0 (*local*) "), stdout);
6742 break;
6744 case 1:
6745 fputs (_(" 1 (*global*) "), stdout);
6746 break;
6748 default:
6749 nn = printf ("%4x%c", data[cnt + j] & 0x7fff,
6750 data[cnt + j] & 0x8000 ? 'h' : ' ');
6752 check_def = 1;
6753 check_need = 1;
6754 if (symbols[cnt + j].st_shndx >= elf_header.e_shnum
6755 || section_headers[symbols[cnt + j].st_shndx].sh_type
6756 != SHT_NOBITS)
6758 if (symbols[cnt + j].st_shndx == SHN_UNDEF)
6759 check_def = 0;
6760 else
6761 check_need = 0;
6764 if (check_need
6765 && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
6767 Elf_Internal_Verneed ivn;
6768 unsigned long offset;
6770 offset = offset_from_vma
6771 (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
6772 sizeof (Elf_External_Verneed));
6776 Elf_Internal_Vernaux ivna;
6777 Elf_External_Verneed evn;
6778 Elf_External_Vernaux evna;
6779 unsigned long a_off;
6781 get_data (&evn, file, offset, sizeof (evn), 1,
6782 _("version need"));
6784 ivn.vn_aux = BYTE_GET (evn.vn_aux);
6785 ivn.vn_next = BYTE_GET (evn.vn_next);
6787 a_off = offset + ivn.vn_aux;
6791 get_data (&evna, file, a_off, sizeof (evna),
6792 1, _("version need aux (2)"));
6794 ivna.vna_next = BYTE_GET (evna.vna_next);
6795 ivna.vna_other = BYTE_GET (evna.vna_other);
6797 a_off += ivna.vna_next;
6799 while (ivna.vna_other != data[cnt + j]
6800 && ivna.vna_next != 0);
6802 if (ivna.vna_other == data[cnt + j])
6804 ivna.vna_name = BYTE_GET (evna.vna_name);
6806 if (ivna.vna_name >= string_sec->sh_size)
6807 name = _("*invalid*");
6808 else
6809 name = strtab + ivna.vna_name;
6810 nn += printf ("(%s%-*s",
6811 name,
6812 12 - (int) strlen (name),
6813 ")");
6814 check_def = 0;
6815 break;
6818 offset += ivn.vn_next;
6820 while (ivn.vn_next);
6823 if (check_def && data[cnt + j] != 0x8001
6824 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
6826 Elf_Internal_Verdef ivd;
6827 Elf_External_Verdef evd;
6828 unsigned long offset;
6830 offset = offset_from_vma
6831 (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
6832 sizeof evd);
6836 get_data (&evd, file, offset, sizeof (evd), 1,
6837 _("version def"));
6839 ivd.vd_next = BYTE_GET (evd.vd_next);
6840 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
6842 offset += ivd.vd_next;
6844 while (ivd.vd_ndx != (data[cnt + j] & 0x7fff)
6845 && ivd.vd_next != 0);
6847 if (ivd.vd_ndx == (data[cnt + j] & 0x7fff))
6849 Elf_External_Verdaux evda;
6850 Elf_Internal_Verdaux ivda;
6852 ivd.vd_aux = BYTE_GET (evd.vd_aux);
6854 get_data (&evda, file,
6855 offset - ivd.vd_next + ivd.vd_aux,
6856 sizeof (evda), 1,
6857 _("version def aux"));
6859 ivda.vda_name = BYTE_GET (evda.vda_name);
6861 if (ivda.vda_name >= string_sec->sh_size)
6862 name = _("*invalid*");
6863 else
6864 name = strtab + ivda.vda_name;
6865 nn += printf ("(%s%-*s",
6866 name,
6867 12 - (int) strlen (name),
6868 ")");
6872 if (nn < 18)
6873 printf ("%*c", 18 - nn, ' ');
6876 putchar ('\n');
6879 free (data);
6880 free (strtab);
6881 free (symbols);
6883 break;
6885 default:
6886 break;
6890 if (! found)
6891 printf (_("\nNo version information found in this file.\n"));
6893 return 1;
6896 static const char *
6897 get_symbol_binding (unsigned int binding)
6899 static char buff[32];
6901 switch (binding)
6903 case STB_LOCAL: return "LOCAL";
6904 case STB_GLOBAL: return "GLOBAL";
6905 case STB_WEAK: return "WEAK";
6906 default:
6907 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
6908 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
6909 binding);
6910 else if (binding >= STB_LOOS && binding <= STB_HIOS)
6912 if (binding == STB_GNU_UNIQUE
6913 && (elf_header.e_ident[EI_OSABI] == ELFOSABI_LINUX
6914 /* GNU/Linux is still using the default value 0. */
6915 || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
6916 return "UNIQUE";
6917 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
6919 else
6920 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
6921 return buff;
6925 static const char *
6926 get_symbol_type (unsigned int type)
6928 static char buff[32];
6930 switch (type)
6932 case STT_NOTYPE: return "NOTYPE";
6933 case STT_OBJECT: return "OBJECT";
6934 case STT_FUNC: return "FUNC";
6935 case STT_SECTION: return "SECTION";
6936 case STT_FILE: return "FILE";
6937 case STT_COMMON: return "COMMON";
6938 case STT_TLS: return "TLS";
6939 case STT_RELC: return "RELC";
6940 case STT_SRELC: return "SRELC";
6941 default:
6942 if (type >= STT_LOPROC && type <= STT_HIPROC)
6944 if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
6945 return "THUMB_FUNC";
6947 if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
6948 return "REGISTER";
6950 if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
6951 return "PARISC_MILLI";
6953 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
6955 else if (type >= STT_LOOS && type <= STT_HIOS)
6957 if (elf_header.e_machine == EM_PARISC)
6959 if (type == STT_HP_OPAQUE)
6960 return "HP_OPAQUE";
6961 if (type == STT_HP_STUB)
6962 return "HP_STUB";
6965 if (type == STT_GNU_IFUNC
6966 && (elf_header.e_ident[EI_OSABI] == ELFOSABI_LINUX
6967 /* GNU/Linux is still using the default value 0. */
6968 || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
6969 return "IFUNC";
6971 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
6973 else
6974 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
6975 return buff;
6979 static const char *
6980 get_symbol_visibility (unsigned int visibility)
6982 switch (visibility)
6984 case STV_DEFAULT: return "DEFAULT";
6985 case STV_INTERNAL: return "INTERNAL";
6986 case STV_HIDDEN: return "HIDDEN";
6987 case STV_PROTECTED: return "PROTECTED";
6988 default: abort ();
6992 static const char *
6993 get_mips_symbol_other (unsigned int other)
6995 switch (other)
6997 case STO_OPTIONAL: return "OPTIONAL";
6998 case STO_MIPS16: return "MIPS16";
6999 case STO_MIPS_PLT: return "MIPS PLT";
7000 case STO_MIPS_PIC: return "MIPS PIC";
7001 default: return NULL;
7005 static const char *
7006 get_symbol_other (unsigned int other)
7008 const char * result = NULL;
7009 static char buff [32];
7011 if (other == 0)
7012 return "";
7014 switch (elf_header.e_machine)
7016 case EM_MIPS:
7017 result = get_mips_symbol_other (other);
7018 default:
7019 break;
7022 if (result)
7023 return result;
7025 snprintf (buff, sizeof buff, _("<other>: %x"), other);
7026 return buff;
7029 static const char *
7030 get_symbol_index_type (unsigned int type)
7032 static char buff[32];
7034 switch (type)
7036 case SHN_UNDEF: return "UND";
7037 case SHN_ABS: return "ABS";
7038 case SHN_COMMON: return "COM";
7039 default:
7040 if (type == SHN_IA_64_ANSI_COMMON
7041 && elf_header.e_machine == EM_IA_64
7042 && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
7043 return "ANSI_COM";
7044 else if ((elf_header.e_machine == EM_X86_64
7045 || elf_header.e_machine == EM_L1OM)
7046 && type == SHN_X86_64_LCOMMON)
7047 return "LARGE_COM";
7048 else if (type == SHN_MIPS_SCOMMON
7049 && elf_header.e_machine == EM_MIPS)
7050 return "SCOM";
7051 else if (type == SHN_MIPS_SUNDEFINED
7052 && elf_header.e_machine == EM_MIPS)
7053 return "SUND";
7054 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
7055 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
7056 else if (type >= SHN_LOOS && type <= SHN_HIOS)
7057 sprintf (buff, "OS [0x%04x]", type & 0xffff);
7058 else if (type >= SHN_LORESERVE)
7059 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
7060 else
7061 sprintf (buff, "%3d", type);
7062 break;
7065 return buff;
7068 static bfd_vma *
7069 get_dynamic_data (FILE * file, unsigned int number, unsigned int ent_size)
7071 unsigned char * e_data;
7072 bfd_vma * i_data;
7074 e_data = cmalloc (number, ent_size);
7076 if (e_data == NULL)
7078 error (_("Out of memory\n"));
7079 return NULL;
7082 if (fread (e_data, ent_size, number, file) != number)
7084 error (_("Unable to read in dynamic data\n"));
7085 return NULL;
7088 i_data = cmalloc (number, sizeof (*i_data));
7090 if (i_data == NULL)
7092 error (_("Out of memory\n"));
7093 free (e_data);
7094 return NULL;
7097 while (number--)
7098 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
7100 free (e_data);
7102 return i_data;
7105 static void
7106 print_dynamic_symbol (bfd_vma si, unsigned long hn)
7108 Elf_Internal_Sym * psym;
7109 int n;
7111 psym = dynamic_symbols + si;
7113 n = print_vma (si, DEC_5);
7114 if (n < 5)
7115 fputs (" " + n, stdout);
7116 printf (" %3lu: ", hn);
7117 print_vma (psym->st_value, LONG_HEX);
7118 putchar (' ');
7119 print_vma (psym->st_size, DEC_5);
7121 printf (" %6s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
7122 printf (" %6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
7123 printf (" %3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
7124 /* Check to see if any other bits in the st_other field are set.
7125 Note - displaying this information disrupts the layout of the
7126 table being generated, but for the moment this case is very
7127 rare. */
7128 if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
7129 printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
7130 printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
7131 if (VALID_DYNAMIC_NAME (psym->st_name))
7132 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
7133 else
7134 printf (" <corrupt: %14ld>", psym->st_name);
7135 putchar ('\n');
7138 /* Dump the symbol table. */
7139 static int
7140 process_symbol_table (FILE * file)
7142 Elf_Internal_Shdr * section;
7143 bfd_vma nbuckets = 0;
7144 bfd_vma nchains = 0;
7145 bfd_vma * buckets = NULL;
7146 bfd_vma * chains = NULL;
7147 bfd_vma ngnubuckets = 0;
7148 bfd_vma * gnubuckets = NULL;
7149 bfd_vma * gnuchains = NULL;
7150 bfd_vma gnusymidx = 0;
7152 if (! do_syms && !do_histogram)
7153 return 1;
7155 if (dynamic_info[DT_HASH]
7156 && (do_histogram
7157 || (do_using_dynamic && dynamic_strings != NULL)))
7159 unsigned char nb[8];
7160 unsigned char nc[8];
7161 int hash_ent_size = 4;
7163 if ((elf_header.e_machine == EM_ALPHA
7164 || elf_header.e_machine == EM_S390
7165 || elf_header.e_machine == EM_S390_OLD)
7166 && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
7167 hash_ent_size = 8;
7169 if (fseek (file,
7170 (archive_file_offset
7171 + offset_from_vma (file, dynamic_info[DT_HASH],
7172 sizeof nb + sizeof nc)),
7173 SEEK_SET))
7175 error (_("Unable to seek to start of dynamic information\n"));
7176 goto no_hash;
7179 if (fread (nb, hash_ent_size, 1, file) != 1)
7181 error (_("Failed to read in number of buckets\n"));
7182 goto no_hash;
7185 if (fread (nc, hash_ent_size, 1, file) != 1)
7187 error (_("Failed to read in number of chains\n"));
7188 goto no_hash;
7191 nbuckets = byte_get (nb, hash_ent_size);
7192 nchains = byte_get (nc, hash_ent_size);
7194 buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
7195 chains = get_dynamic_data (file, nchains, hash_ent_size);
7197 no_hash:
7198 if (buckets == NULL || chains == NULL)
7200 if (do_using_dynamic)
7201 return 0;
7202 free (buckets);
7203 free (chains);
7204 buckets = NULL;
7205 chains = NULL;
7206 nbuckets = 0;
7207 nchains = 0;
7211 if (dynamic_info_DT_GNU_HASH
7212 && (do_histogram
7213 || (do_using_dynamic && dynamic_strings != NULL)))
7215 unsigned char nb[16];
7216 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
7217 bfd_vma buckets_vma;
7219 if (fseek (file,
7220 (archive_file_offset
7221 + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
7222 sizeof nb)),
7223 SEEK_SET))
7225 error (_("Unable to seek to start of dynamic information\n"));
7226 goto no_gnu_hash;
7229 if (fread (nb, 16, 1, file) != 1)
7231 error (_("Failed to read in number of buckets\n"));
7232 goto no_gnu_hash;
7235 ngnubuckets = byte_get (nb, 4);
7236 gnusymidx = byte_get (nb + 4, 4);
7237 bitmaskwords = byte_get (nb + 8, 4);
7238 buckets_vma = dynamic_info_DT_GNU_HASH + 16;
7239 if (is_32bit_elf)
7240 buckets_vma += bitmaskwords * 4;
7241 else
7242 buckets_vma += bitmaskwords * 8;
7244 if (fseek (file,
7245 (archive_file_offset
7246 + offset_from_vma (file, buckets_vma, 4)),
7247 SEEK_SET))
7249 error (_("Unable to seek to start of dynamic information\n"));
7250 goto no_gnu_hash;
7253 gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
7255 if (gnubuckets == NULL)
7256 goto no_gnu_hash;
7258 for (i = 0; i < ngnubuckets; i++)
7259 if (gnubuckets[i] != 0)
7261 if (gnubuckets[i] < gnusymidx)
7262 return 0;
7264 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
7265 maxchain = gnubuckets[i];
7268 if (maxchain == 0xffffffff)
7269 goto no_gnu_hash;
7271 maxchain -= gnusymidx;
7273 if (fseek (file,
7274 (archive_file_offset
7275 + offset_from_vma (file, buckets_vma
7276 + 4 * (ngnubuckets + maxchain), 4)),
7277 SEEK_SET))
7279 error (_("Unable to seek to start of dynamic information\n"));
7280 goto no_gnu_hash;
7285 if (fread (nb, 4, 1, file) != 1)
7287 error (_("Failed to determine last chain length\n"));
7288 goto no_gnu_hash;
7291 if (maxchain + 1 == 0)
7292 goto no_gnu_hash;
7294 ++maxchain;
7296 while ((byte_get (nb, 4) & 1) == 0);
7298 if (fseek (file,
7299 (archive_file_offset
7300 + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
7301 SEEK_SET))
7303 error (_("Unable to seek to start of dynamic information\n"));
7304 goto no_gnu_hash;
7307 gnuchains = get_dynamic_data (file, maxchain, 4);
7309 no_gnu_hash:
7310 if (gnuchains == NULL)
7312 free (gnubuckets);
7313 gnubuckets = NULL;
7314 ngnubuckets = 0;
7315 if (do_using_dynamic)
7316 return 0;
7320 if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
7321 && do_syms
7322 && do_using_dynamic
7323 && dynamic_strings != NULL)
7325 unsigned long hn;
7327 if (dynamic_info[DT_HASH])
7329 bfd_vma si;
7331 printf (_("\nSymbol table for image:\n"));
7332 if (is_32bit_elf)
7333 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7334 else
7335 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7337 for (hn = 0; hn < nbuckets; hn++)
7339 if (! buckets[hn])
7340 continue;
7342 for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
7343 print_dynamic_symbol (si, hn);
7347 if (dynamic_info_DT_GNU_HASH)
7349 printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
7350 if (is_32bit_elf)
7351 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7352 else
7353 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7355 for (hn = 0; hn < ngnubuckets; ++hn)
7356 if (gnubuckets[hn] != 0)
7358 bfd_vma si = gnubuckets[hn];
7359 bfd_vma off = si - gnusymidx;
7363 print_dynamic_symbol (si, hn);
7364 si++;
7366 while ((gnuchains[off++] & 1) == 0);
7370 else if (do_syms && !do_using_dynamic)
7372 unsigned int i;
7374 for (i = 0, section = section_headers;
7375 i < elf_header.e_shnum;
7376 i++, section++)
7378 unsigned int si;
7379 char * strtab = NULL;
7380 unsigned long int strtab_size = 0;
7381 Elf_Internal_Sym * symtab;
7382 Elf_Internal_Sym * psym;
7384 if ( section->sh_type != SHT_SYMTAB
7385 && section->sh_type != SHT_DYNSYM)
7386 continue;
7388 printf (_("\nSymbol table '%s' contains %lu entries:\n"),
7389 SECTION_NAME (section),
7390 (unsigned long) (section->sh_size / section->sh_entsize));
7391 if (is_32bit_elf)
7392 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
7393 else
7394 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
7396 symtab = GET_ELF_SYMBOLS (file, section);
7397 if (symtab == NULL)
7398 continue;
7400 if (section->sh_link == elf_header.e_shstrndx)
7402 strtab = string_table;
7403 strtab_size = string_table_length;
7405 else if (section->sh_link < elf_header.e_shnum)
7407 Elf_Internal_Shdr * string_sec;
7409 string_sec = section_headers + section->sh_link;
7411 strtab = get_data (NULL, file, string_sec->sh_offset,
7412 1, string_sec->sh_size, _("string table"));
7413 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
7416 for (si = 0, psym = symtab;
7417 si < section->sh_size / section->sh_entsize;
7418 si++, psym++)
7420 printf ("%6d: ", si);
7421 print_vma (psym->st_value, LONG_HEX);
7422 putchar (' ');
7423 print_vma (psym->st_size, DEC_5);
7424 printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
7425 printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
7426 printf (" %-3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
7427 /* Check to see if any other bits in the st_other field are set.
7428 Note - displaying this information disrupts the layout of the
7429 table being generated, but for the moment this case is very rare. */
7430 if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
7431 printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
7432 printf (" %4s ", get_symbol_index_type (psym->st_shndx));
7433 print_symbol (25, psym->st_name < strtab_size
7434 ? strtab + psym->st_name : "<corrupt>");
7436 if (section->sh_type == SHT_DYNSYM &&
7437 version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
7439 unsigned char data[2];
7440 unsigned short vers_data;
7441 unsigned long offset;
7442 int is_nobits;
7443 int check_def;
7445 offset = offset_from_vma
7446 (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
7447 sizeof data + si * sizeof (vers_data));
7449 get_data (&data, file, offset + si * sizeof (vers_data),
7450 sizeof (data), 1, _("version data"));
7452 vers_data = byte_get (data, 2);
7454 is_nobits = (psym->st_shndx < elf_header.e_shnum
7455 && section_headers[psym->st_shndx].sh_type
7456 == SHT_NOBITS);
7458 check_def = (psym->st_shndx != SHN_UNDEF);
7460 if ((vers_data & 0x8000) || vers_data > 1)
7462 if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
7463 && (is_nobits || ! check_def))
7465 Elf_External_Verneed evn;
7466 Elf_Internal_Verneed ivn;
7467 Elf_Internal_Vernaux ivna;
7469 /* We must test both. */
7470 offset = offset_from_vma
7471 (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
7472 sizeof evn);
7476 unsigned long vna_off;
7478 get_data (&evn, file, offset, sizeof (evn), 1,
7479 _("version need"));
7481 ivn.vn_aux = BYTE_GET (evn.vn_aux);
7482 ivn.vn_next = BYTE_GET (evn.vn_next);
7484 vna_off = offset + ivn.vn_aux;
7488 Elf_External_Vernaux evna;
7490 get_data (&evna, file, vna_off,
7491 sizeof (evna), 1,
7492 _("version need aux (3)"));
7494 ivna.vna_other = BYTE_GET (evna.vna_other);
7495 ivna.vna_next = BYTE_GET (evna.vna_next);
7496 ivna.vna_name = BYTE_GET (evna.vna_name);
7498 vna_off += ivna.vna_next;
7500 while (ivna.vna_other != vers_data
7501 && ivna.vna_next != 0);
7503 if (ivna.vna_other == vers_data)
7504 break;
7506 offset += ivn.vn_next;
7508 while (ivn.vn_next != 0);
7510 if (ivna.vna_other == vers_data)
7512 printf ("@%s (%d)",
7513 ivna.vna_name < strtab_size
7514 ? strtab + ivna.vna_name : "<corrupt>",
7515 ivna.vna_other);
7516 check_def = 0;
7518 else if (! is_nobits)
7519 error (_("bad dynamic symbol\n"));
7520 else
7521 check_def = 1;
7524 if (check_def)
7526 if (vers_data != 0x8001
7527 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
7529 Elf_Internal_Verdef ivd;
7530 Elf_Internal_Verdaux ivda;
7531 Elf_External_Verdaux evda;
7532 unsigned long offset;
7534 offset = offset_from_vma
7535 (file,
7536 version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
7537 sizeof (Elf_External_Verdef));
7541 Elf_External_Verdef evd;
7543 get_data (&evd, file, offset, sizeof (evd),
7544 1, _("version def"));
7546 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
7547 ivd.vd_aux = BYTE_GET (evd.vd_aux);
7548 ivd.vd_next = BYTE_GET (evd.vd_next);
7550 offset += ivd.vd_next;
7552 while (ivd.vd_ndx != (vers_data & 0x7fff)
7553 && ivd.vd_next != 0);
7555 offset -= ivd.vd_next;
7556 offset += ivd.vd_aux;
7558 get_data (&evda, file, offset, sizeof (evda),
7559 1, _("version def aux"));
7561 ivda.vda_name = BYTE_GET (evda.vda_name);
7563 if (psym->st_name != ivda.vda_name)
7564 printf ((vers_data & 0x8000)
7565 ? "@%s" : "@@%s",
7566 ivda.vda_name < strtab_size
7567 ? strtab + ivda.vda_name : "<corrupt>");
7573 putchar ('\n');
7576 free (symtab);
7577 if (strtab != string_table)
7578 free (strtab);
7581 else if (do_syms)
7582 printf
7583 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
7585 if (do_histogram && buckets != NULL)
7587 unsigned long * lengths;
7588 unsigned long * counts;
7589 unsigned long hn;
7590 bfd_vma si;
7591 unsigned long maxlength = 0;
7592 unsigned long nzero_counts = 0;
7593 unsigned long nsyms = 0;
7595 printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
7596 (unsigned long) nbuckets);
7597 printf (_(" Length Number %% of total Coverage\n"));
7599 lengths = calloc (nbuckets, sizeof (*lengths));
7600 if (lengths == NULL)
7602 error (_("Out of memory\n"));
7603 return 0;
7605 for (hn = 0; hn < nbuckets; ++hn)
7607 for (si = buckets[hn]; si > 0 && si < nchains; si = chains[si])
7609 ++nsyms;
7610 if (maxlength < ++lengths[hn])
7611 ++maxlength;
7615 counts = calloc (maxlength + 1, sizeof (*counts));
7616 if (counts == NULL)
7618 error (_("Out of memory\n"));
7619 return 0;
7622 for (hn = 0; hn < nbuckets; ++hn)
7623 ++counts[lengths[hn]];
7625 if (nbuckets > 0)
7627 unsigned long i;
7628 printf (" 0 %-10lu (%5.1f%%)\n",
7629 counts[0], (counts[0] * 100.0) / nbuckets);
7630 for (i = 1; i <= maxlength; ++i)
7632 nzero_counts += counts[i] * i;
7633 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
7634 i, counts[i], (counts[i] * 100.0) / nbuckets,
7635 (nzero_counts * 100.0) / nsyms);
7639 free (counts);
7640 free (lengths);
7643 if (buckets != NULL)
7645 free (buckets);
7646 free (chains);
7649 if (do_histogram && gnubuckets != NULL)
7651 unsigned long * lengths;
7652 unsigned long * counts;
7653 unsigned long hn;
7654 unsigned long maxlength = 0;
7655 unsigned long nzero_counts = 0;
7656 unsigned long nsyms = 0;
7658 lengths = calloc (ngnubuckets, sizeof (*lengths));
7659 if (lengths == NULL)
7661 error (_("Out of memory\n"));
7662 return 0;
7665 printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
7666 (unsigned long) ngnubuckets);
7667 printf (_(" Length Number %% of total Coverage\n"));
7669 for (hn = 0; hn < ngnubuckets; ++hn)
7670 if (gnubuckets[hn] != 0)
7672 bfd_vma off, length = 1;
7674 for (off = gnubuckets[hn] - gnusymidx;
7675 (gnuchains[off] & 1) == 0; ++off)
7676 ++length;
7677 lengths[hn] = length;
7678 if (length > maxlength)
7679 maxlength = length;
7680 nsyms += length;
7683 counts = calloc (maxlength + 1, sizeof (*counts));
7684 if (counts == NULL)
7686 error (_("Out of memory\n"));
7687 return 0;
7690 for (hn = 0; hn < ngnubuckets; ++hn)
7691 ++counts[lengths[hn]];
7693 if (ngnubuckets > 0)
7695 unsigned long j;
7696 printf (" 0 %-10lu (%5.1f%%)\n",
7697 counts[0], (counts[0] * 100.0) / ngnubuckets);
7698 for (j = 1; j <= maxlength; ++j)
7700 nzero_counts += counts[j] * j;
7701 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
7702 j, counts[j], (counts[j] * 100.0) / ngnubuckets,
7703 (nzero_counts * 100.0) / nsyms);
7707 free (counts);
7708 free (lengths);
7709 free (gnubuckets);
7710 free (gnuchains);
7713 return 1;
7716 static int
7717 process_syminfo (FILE * file ATTRIBUTE_UNUSED)
7719 unsigned int i;
7721 if (dynamic_syminfo == NULL
7722 || !do_dynamic)
7723 /* No syminfo, this is ok. */
7724 return 1;
7726 /* There better should be a dynamic symbol section. */
7727 if (dynamic_symbols == NULL || dynamic_strings == NULL)
7728 return 0;
7730 if (dynamic_addr)
7731 printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
7732 dynamic_syminfo_offset, dynamic_syminfo_nent);
7734 printf (_(" Num: Name BoundTo Flags\n"));
7735 for (i = 0; i < dynamic_syminfo_nent; ++i)
7737 unsigned short int flags = dynamic_syminfo[i].si_flags;
7739 printf ("%4d: ", i);
7740 if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
7741 print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
7742 else
7743 printf ("<corrupt: %19ld>", dynamic_symbols[i].st_name);
7744 putchar (' ');
7746 switch (dynamic_syminfo[i].si_boundto)
7748 case SYMINFO_BT_SELF:
7749 fputs ("SELF ", stdout);
7750 break;
7751 case SYMINFO_BT_PARENT:
7752 fputs ("PARENT ", stdout);
7753 break;
7754 default:
7755 if (dynamic_syminfo[i].si_boundto > 0
7756 && dynamic_syminfo[i].si_boundto < dynamic_nent
7757 && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
7759 print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
7760 putchar (' ' );
7762 else
7763 printf ("%-10d ", dynamic_syminfo[i].si_boundto);
7764 break;
7767 if (flags & SYMINFO_FLG_DIRECT)
7768 printf (" DIRECT");
7769 if (flags & SYMINFO_FLG_PASSTHRU)
7770 printf (" PASSTHRU");
7771 if (flags & SYMINFO_FLG_COPY)
7772 printf (" COPY");
7773 if (flags & SYMINFO_FLG_LAZYLOAD)
7774 printf (" LAZYLOAD");
7776 puts ("");
7779 return 1;
7782 /* Check to see if the given reloc needs to be handled in a target specific
7783 manner. If so then process the reloc and return TRUE otherwise return
7784 FALSE. */
7786 static bfd_boolean
7787 target_specific_reloc_handling (Elf_Internal_Rela * reloc,
7788 unsigned char * start,
7789 Elf_Internal_Sym * symtab)
7791 unsigned int reloc_type = get_reloc_type (reloc->r_info);
7793 switch (elf_header.e_machine)
7795 case EM_MN10300:
7796 case EM_CYGNUS_MN10300:
7798 static Elf_Internal_Sym * saved_sym = NULL;
7800 switch (reloc_type)
7802 case 34: /* R_MN10300_ALIGN */
7803 return TRUE;
7804 case 33: /* R_MN10300_SYM_DIFF */
7805 saved_sym = symtab + get_reloc_symindex (reloc->r_info);
7806 return TRUE;
7807 case 1: /* R_MN10300_32 */
7808 case 2: /* R_MN10300_16 */
7809 if (saved_sym != NULL)
7811 bfd_vma value;
7813 value = reloc->r_addend
7814 + (symtab[get_reloc_symindex (reloc->r_info)].st_value
7815 - saved_sym->st_value);
7817 byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
7819 saved_sym = NULL;
7820 return TRUE;
7822 break;
7823 default:
7824 if (saved_sym != NULL)
7825 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc"));
7826 break;
7828 break;
7832 return FALSE;
7835 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
7836 DWARF debug sections. This is a target specific test. Note - we do not
7837 go through the whole including-target-headers-multiple-times route, (as
7838 we have already done with <elf/h8.h>) because this would become very
7839 messy and even then this function would have to contain target specific
7840 information (the names of the relocs instead of their numeric values).
7841 FIXME: This is not the correct way to solve this problem. The proper way
7842 is to have target specific reloc sizing and typing functions created by
7843 the reloc-macros.h header, in the same way that it already creates the
7844 reloc naming functions. */
7846 static bfd_boolean
7847 is_32bit_abs_reloc (unsigned int reloc_type)
7849 switch (elf_header.e_machine)
7851 case EM_386:
7852 case EM_486:
7853 return reloc_type == 1; /* R_386_32. */
7854 case EM_68K:
7855 return reloc_type == 1; /* R_68K_32. */
7856 case EM_860:
7857 return reloc_type == 1; /* R_860_32. */
7858 case EM_ALPHA:
7859 return reloc_type == 1; /* XXX Is this right ? */
7860 case EM_ARC:
7861 return reloc_type == 1; /* R_ARC_32. */
7862 case EM_ARM:
7863 return reloc_type == 2; /* R_ARM_ABS32 */
7864 case EM_AVR_OLD:
7865 case EM_AVR:
7866 return reloc_type == 1;
7867 case EM_BLACKFIN:
7868 return reloc_type == 0x12; /* R_byte4_data. */
7869 case EM_CRIS:
7870 return reloc_type == 3; /* R_CRIS_32. */
7871 case EM_CR16:
7872 case EM_CR16_OLD:
7873 return reloc_type == 3; /* R_CR16_NUM32. */
7874 case EM_CRX:
7875 return reloc_type == 15; /* R_CRX_NUM32. */
7876 case EM_CYGNUS_FRV:
7877 return reloc_type == 1;
7878 case EM_CYGNUS_D10V:
7879 case EM_D10V:
7880 return reloc_type == 6; /* R_D10V_32. */
7881 case EM_CYGNUS_D30V:
7882 case EM_D30V:
7883 return reloc_type == 12; /* R_D30V_32_NORMAL. */
7884 case EM_DLX:
7885 return reloc_type == 3; /* R_DLX_RELOC_32. */
7886 case EM_CYGNUS_FR30:
7887 case EM_FR30:
7888 return reloc_type == 3; /* R_FR30_32. */
7889 case EM_H8S:
7890 case EM_H8_300:
7891 case EM_H8_300H:
7892 return reloc_type == 1; /* R_H8_DIR32. */
7893 case EM_IA_64:
7894 return reloc_type == 0x65; /* R_IA64_SECREL32LSB. */
7895 case EM_IP2K_OLD:
7896 case EM_IP2K:
7897 return reloc_type == 2; /* R_IP2K_32. */
7898 case EM_IQ2000:
7899 return reloc_type == 2; /* R_IQ2000_32. */
7900 case EM_LATTICEMICO32:
7901 return reloc_type == 3; /* R_LM32_32. */
7902 case EM_M32C_OLD:
7903 case EM_M32C:
7904 return reloc_type == 3; /* R_M32C_32. */
7905 case EM_M32R:
7906 return reloc_type == 34; /* R_M32R_32_RELA. */
7907 case EM_MCORE:
7908 return reloc_type == 1; /* R_MCORE_ADDR32. */
7909 case EM_CYGNUS_MEP:
7910 return reloc_type == 4; /* R_MEP_32. */
7911 case EM_MIPS:
7912 return reloc_type == 2; /* R_MIPS_32. */
7913 case EM_MMIX:
7914 return reloc_type == 4; /* R_MMIX_32. */
7915 case EM_CYGNUS_MN10200:
7916 case EM_MN10200:
7917 return reloc_type == 1; /* R_MN10200_32. */
7918 case EM_CYGNUS_MN10300:
7919 case EM_MN10300:
7920 return reloc_type == 1; /* R_MN10300_32. */
7921 case EM_MSP430_OLD:
7922 case EM_MSP430:
7923 return reloc_type == 1; /* R_MSP43_32. */
7924 case EM_MT:
7925 return reloc_type == 2; /* R_MT_32. */
7926 case EM_ALTERA_NIOS2:
7927 case EM_NIOS32:
7928 return reloc_type == 1; /* R_NIOS_32. */
7929 case EM_OPENRISC:
7930 case EM_OR32:
7931 return reloc_type == 1; /* R_OR32_32. */
7932 case EM_PARISC:
7933 return reloc_type == 1; /* R_PARISC_DIR32. */
7934 case EM_PJ:
7935 case EM_PJ_OLD:
7936 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
7937 case EM_PPC64:
7938 return reloc_type == 1; /* R_PPC64_ADDR32. */
7939 case EM_PPC:
7940 return reloc_type == 1; /* R_PPC_ADDR32. */
7941 case EM_S370:
7942 return reloc_type == 1; /* R_I370_ADDR31. */
7943 case EM_S390_OLD:
7944 case EM_S390:
7945 return reloc_type == 4; /* R_S390_32. */
7946 case EM_SCORE:
7947 return reloc_type == 8; /* R_SCORE_ABS32. */
7948 case EM_SH:
7949 return reloc_type == 1; /* R_SH_DIR32. */
7950 case EM_SPARC32PLUS:
7951 case EM_SPARCV9:
7952 case EM_SPARC:
7953 return reloc_type == 3 /* R_SPARC_32. */
7954 || reloc_type == 23; /* R_SPARC_UA32. */
7955 case EM_SPU:
7956 return reloc_type == 6; /* R_SPU_ADDR32 */
7957 case EM_CYGNUS_V850:
7958 case EM_V850:
7959 return reloc_type == 6; /* R_V850_ABS32. */
7960 case EM_VAX:
7961 return reloc_type == 1; /* R_VAX_32. */
7962 case EM_X86_64:
7963 case EM_L1OM:
7964 return reloc_type == 10; /* R_X86_64_32. */
7965 case EM_XSTORMY16:
7966 return reloc_type == 1; /* R_XSTROMY16_32. */
7967 case EM_XTENSA_OLD:
7968 case EM_XTENSA:
7969 return reloc_type == 1; /* R_XTENSA_32. */
7971 default:
7972 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
7973 elf_header.e_machine);
7974 abort ();
7978 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
7979 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
7981 static bfd_boolean
7982 is_32bit_pcrel_reloc (unsigned int reloc_type)
7984 switch (elf_header.e_machine)
7986 case EM_386:
7987 case EM_486:
7988 return reloc_type == 2; /* R_386_PC32. */
7989 case EM_68K:
7990 return reloc_type == 4; /* R_68K_PC32. */
7991 case EM_ALPHA:
7992 return reloc_type == 10; /* R_ALPHA_SREL32. */
7993 case EM_ARM:
7994 return reloc_type == 3; /* R_ARM_REL32 */
7995 case EM_PARISC:
7996 return reloc_type == 9; /* R_PARISC_PCREL32. */
7997 case EM_PPC:
7998 return reloc_type == 26; /* R_PPC_REL32. */
7999 case EM_PPC64:
8000 return reloc_type == 26; /* R_PPC64_REL32. */
8001 case EM_S390_OLD:
8002 case EM_S390:
8003 return reloc_type == 5; /* R_390_PC32. */
8004 case EM_SH:
8005 return reloc_type == 2; /* R_SH_REL32. */
8006 case EM_SPARC32PLUS:
8007 case EM_SPARCV9:
8008 case EM_SPARC:
8009 return reloc_type == 6; /* R_SPARC_DISP32. */
8010 case EM_SPU:
8011 return reloc_type == 13; /* R_SPU_REL32. */
8012 case EM_X86_64:
8013 case EM_L1OM:
8014 return reloc_type == 2; /* R_X86_64_PC32. */
8015 case EM_XTENSA_OLD:
8016 case EM_XTENSA:
8017 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
8018 default:
8019 /* Do not abort or issue an error message here. Not all targets use
8020 pc-relative 32-bit relocs in their DWARF debug information and we
8021 have already tested for target coverage in is_32bit_abs_reloc. A
8022 more helpful warning message will be generated by apply_relocations
8023 anyway, so just return. */
8024 return FALSE;
8028 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8029 a 64-bit absolute RELA relocation used in DWARF debug sections. */
8031 static bfd_boolean
8032 is_64bit_abs_reloc (unsigned int reloc_type)
8034 switch (elf_header.e_machine)
8036 case EM_ALPHA:
8037 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
8038 case EM_IA_64:
8039 return reloc_type == 0x27; /* R_IA64_DIR64LSB. */
8040 case EM_PARISC:
8041 return reloc_type == 80; /* R_PARISC_DIR64. */
8042 case EM_PPC64:
8043 return reloc_type == 38; /* R_PPC64_ADDR64. */
8044 case EM_SPARC32PLUS:
8045 case EM_SPARCV9:
8046 case EM_SPARC:
8047 return reloc_type == 54; /* R_SPARC_UA64. */
8048 case EM_X86_64:
8049 case EM_L1OM:
8050 return reloc_type == 1; /* R_X86_64_64. */
8051 case EM_S390_OLD:
8052 case EM_S390:
8053 return reloc_type == 22; /* R_S390_64 */
8054 case EM_MIPS:
8055 return reloc_type == 18; /* R_MIPS_64 */
8056 default:
8057 return FALSE;
8061 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
8062 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
8064 static bfd_boolean
8065 is_64bit_pcrel_reloc (unsigned int reloc_type)
8067 switch (elf_header.e_machine)
8069 case EM_ALPHA:
8070 return reloc_type == 11; /* R_ALPHA_SREL64 */
8071 case EM_IA_64:
8072 return reloc_type == 0x4f; /* R_IA64_PCREL64LSB */
8073 case EM_PARISC:
8074 return reloc_type == 72; /* R_PARISC_PCREL64 */
8075 case EM_PPC64:
8076 return reloc_type == 44; /* R_PPC64_REL64 */
8077 case EM_SPARC32PLUS:
8078 case EM_SPARCV9:
8079 case EM_SPARC:
8080 return reloc_type == 46; /* R_SPARC_DISP64 */
8081 case EM_X86_64:
8082 case EM_L1OM:
8083 return reloc_type == 24; /* R_X86_64_PC64 */
8084 case EM_S390_OLD:
8085 case EM_S390:
8086 return reloc_type == 23; /* R_S390_PC64 */
8087 default:
8088 return FALSE;
8092 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8093 a 16-bit absolute RELA relocation used in DWARF debug sections. */
8095 static bfd_boolean
8096 is_16bit_abs_reloc (unsigned int reloc_type)
8098 switch (elf_header.e_machine)
8100 case EM_AVR_OLD:
8101 case EM_AVR:
8102 return reloc_type == 4; /* R_AVR_16. */
8103 case EM_CYGNUS_D10V:
8104 case EM_D10V:
8105 return reloc_type == 3; /* R_D10V_16. */
8106 case EM_H8S:
8107 case EM_H8_300:
8108 case EM_H8_300H:
8109 return reloc_type == R_H8_DIR16;
8110 case EM_IP2K_OLD:
8111 case EM_IP2K:
8112 return reloc_type == 1; /* R_IP2K_16. */
8113 case EM_M32C_OLD:
8114 case EM_M32C:
8115 return reloc_type == 1; /* R_M32C_16 */
8116 case EM_MSP430_OLD:
8117 case EM_MSP430:
8118 return reloc_type == 5; /* R_MSP430_16_BYTE. */
8119 case EM_ALTERA_NIOS2:
8120 case EM_NIOS32:
8121 return reloc_type == 9; /* R_NIOS_16. */
8122 default:
8123 return FALSE;
8127 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
8128 relocation entries (possibly formerly used for SHT_GROUP sections). */
8130 static bfd_boolean
8131 is_none_reloc (unsigned int reloc_type)
8133 switch (elf_header.e_machine)
8135 case EM_68K: /* R_68K_NONE. */
8136 case EM_386: /* R_386_NONE. */
8137 case EM_SPARC32PLUS:
8138 case EM_SPARCV9:
8139 case EM_SPARC: /* R_SPARC_NONE. */
8140 case EM_MIPS: /* R_MIPS_NONE. */
8141 case EM_PARISC: /* R_PARISC_NONE. */
8142 case EM_ALPHA: /* R_ALPHA_NONE. */
8143 case EM_PPC: /* R_PPC_NONE. */
8144 case EM_PPC64: /* R_PPC64_NONE. */
8145 case EM_ARM: /* R_ARM_NONE. */
8146 case EM_IA_64: /* R_IA64_NONE. */
8147 case EM_SH: /* R_SH_NONE. */
8148 case EM_S390_OLD:
8149 case EM_S390: /* R_390_NONE. */
8150 case EM_CRIS: /* R_CRIS_NONE. */
8151 case EM_X86_64: /* R_X86_64_NONE. */
8152 case EM_L1OM: /* R_X86_64_NONE. */
8153 case EM_MN10300: /* R_MN10300_NONE. */
8154 case EM_M32R: /* R_M32R_NONE. */
8155 return reloc_type == 0;
8157 return FALSE;
8160 /* Apply relocations to a section.
8161 Note: So far support has been added only for those relocations
8162 which can be found in debug sections.
8163 FIXME: Add support for more relocations ? */
8165 static void
8166 apply_relocations (void * file,
8167 Elf_Internal_Shdr * section,
8168 unsigned char * start)
8170 Elf_Internal_Shdr * relsec;
8171 unsigned char * end = start + section->sh_size;
8173 if (elf_header.e_type != ET_REL)
8174 return;
8176 /* Find the reloc section associated with the section. */
8177 for (relsec = section_headers;
8178 relsec < section_headers + elf_header.e_shnum;
8179 ++relsec)
8181 bfd_boolean is_rela;
8182 unsigned long num_relocs;
8183 Elf_Internal_Rela * relocs;
8184 Elf_Internal_Rela * rp;
8185 Elf_Internal_Shdr * symsec;
8186 Elf_Internal_Sym * symtab;
8187 Elf_Internal_Sym * sym;
8189 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
8190 || relsec->sh_info >= elf_header.e_shnum
8191 || section_headers + relsec->sh_info != section
8192 || relsec->sh_size == 0
8193 || relsec->sh_link >= elf_header.e_shnum)
8194 continue;
8196 is_rela = relsec->sh_type == SHT_RELA;
8198 if (is_rela)
8200 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
8201 & relocs, & num_relocs))
8202 return;
8204 else
8206 if (!slurp_rel_relocs (file, relsec->sh_offset, relsec->sh_size,
8207 & relocs, & num_relocs))
8208 return;
8211 /* SH uses RELA but uses in place value instead of the addend field. */
8212 if (elf_header.e_machine == EM_SH)
8213 is_rela = FALSE;
8215 symsec = section_headers + relsec->sh_link;
8216 symtab = GET_ELF_SYMBOLS (file, symsec);
8218 for (rp = relocs; rp < relocs + num_relocs; ++rp)
8220 bfd_vma addend;
8221 unsigned int reloc_type;
8222 unsigned int reloc_size;
8223 unsigned char * loc;
8225 reloc_type = get_reloc_type (rp->r_info);
8227 if (target_specific_reloc_handling (rp, start, symtab))
8228 continue;
8229 else if (is_none_reloc (reloc_type))
8230 continue;
8231 else if (is_32bit_abs_reloc (reloc_type)
8232 || is_32bit_pcrel_reloc (reloc_type))
8233 reloc_size = 4;
8234 else if (is_64bit_abs_reloc (reloc_type)
8235 || is_64bit_pcrel_reloc (reloc_type))
8236 reloc_size = 8;
8237 else if (is_16bit_abs_reloc (reloc_type))
8238 reloc_size = 2;
8239 else
8241 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
8242 reloc_type, SECTION_NAME (section));
8243 continue;
8246 loc = start + rp->r_offset;
8247 if ((loc + reloc_size) > end)
8249 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
8250 (unsigned long) rp->r_offset,
8251 SECTION_NAME (section));
8252 continue;
8255 sym = symtab + get_reloc_symindex (rp->r_info);
8257 /* If the reloc has a symbol associated with it,
8258 make sure that it is of an appropriate type.
8260 Relocations against symbols without type can happen.
8261 Gcc -feliminate-dwarf2-dups may generate symbols
8262 without type for debug info.
8264 Icc generates relocations against function symbols
8265 instead of local labels.
8267 Relocations against object symbols can happen, eg when
8268 referencing a global array. For an example of this see
8269 the _clz.o binary in libgcc.a. */
8270 if (sym != symtab
8271 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
8273 warn (_("skipping unexpected symbol type %s in %ld'th relocation in section %s\n"),
8274 get_symbol_type (ELF_ST_TYPE (sym->st_info)),
8275 (long int)(rp - relocs),
8276 SECTION_NAME (relsec));
8277 continue;
8280 addend = is_rela ? rp->r_addend : byte_get (loc, reloc_size);
8282 if (is_32bit_pcrel_reloc (reloc_type)
8283 || is_64bit_pcrel_reloc (reloc_type))
8285 /* On HPPA, all pc-relative relocations are biased by 8. */
8286 if (elf_header.e_machine == EM_PARISC)
8287 addend -= 8;
8288 byte_put (loc, (addend + sym->st_value) - rp->r_offset,
8289 reloc_size);
8291 else
8292 byte_put (loc, addend + sym->st_value, reloc_size);
8295 free (symtab);
8296 free (relocs);
8297 break;
8301 #ifdef SUPPORT_DISASSEMBLY
8302 static int
8303 disassemble_section (Elf_Internal_Shdr * section, FILE * file)
8305 printf (_("\nAssembly dump of section %s\n"),
8306 SECTION_NAME (section));
8308 /* XXX -- to be done --- XXX */
8310 return 1;
8312 #endif
8314 /* Reads in the contents of SECTION from FILE, returning a pointer
8315 to a malloc'ed buffer or NULL if something went wrong. */
8317 static char *
8318 get_section_contents (Elf_Internal_Shdr * section, FILE * file)
8320 bfd_size_type num_bytes;
8322 num_bytes = section->sh_size;
8324 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
8326 printf (_("\nSection '%s' has no data to dump.\n"),
8327 SECTION_NAME (section));
8328 return NULL;
8331 return get_data (NULL, file, section->sh_offset, 1, num_bytes,
8332 _("section contents"));
8336 static void
8337 dump_section_as_strings (Elf_Internal_Shdr * section, FILE * file)
8339 Elf_Internal_Shdr * relsec;
8340 bfd_size_type num_bytes;
8341 bfd_vma addr;
8342 char * data;
8343 char * end;
8344 char * start;
8345 char * name = SECTION_NAME (section);
8346 bfd_boolean some_strings_shown;
8348 start = get_section_contents (section, file);
8349 if (start == NULL)
8350 return;
8352 printf (_("\nString dump of section '%s':\n"), name);
8354 /* If the section being dumped has relocations against it the user might
8355 be expecting these relocations to have been applied. Check for this
8356 case and issue a warning message in order to avoid confusion.
8357 FIXME: Maybe we ought to have an option that dumps a section with
8358 relocs applied ? */
8359 for (relsec = section_headers;
8360 relsec < section_headers + elf_header.e_shnum;
8361 ++relsec)
8363 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
8364 || relsec->sh_info >= elf_header.e_shnum
8365 || section_headers + relsec->sh_info != section
8366 || relsec->sh_size == 0
8367 || relsec->sh_link >= elf_header.e_shnum)
8368 continue;
8370 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
8371 break;
8374 num_bytes = section->sh_size;
8375 addr = section->sh_addr;
8376 data = start;
8377 end = start + num_bytes;
8378 some_strings_shown = FALSE;
8380 while (data < end)
8382 while (!ISPRINT (* data))
8383 if (++ data >= end)
8384 break;
8386 if (data < end)
8388 #ifndef __MSVCRT__
8389 printf (" [%6tx] %s\n", data - start, data);
8390 #else
8391 printf (" [%6Ix] %s\n", (size_t) (data - start), data);
8392 #endif
8393 data += strlen (data);
8394 some_strings_shown = TRUE;
8398 if (! some_strings_shown)
8399 printf (_(" No strings found in this section."));
8401 free (start);
8403 putchar ('\n');
8406 static void
8407 dump_section_as_bytes (Elf_Internal_Shdr * section,
8408 FILE * file,
8409 bfd_boolean relocate)
8411 Elf_Internal_Shdr * relsec;
8412 bfd_size_type bytes;
8413 bfd_vma addr;
8414 unsigned char * data;
8415 unsigned char * start;
8417 start = (unsigned char *) get_section_contents (section, file);
8418 if (start == NULL)
8419 return;
8421 printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section));
8423 if (relocate)
8425 apply_relocations (file, section, start);
8427 else
8429 /* If the section being dumped has relocations against it the user might
8430 be expecting these relocations to have been applied. Check for this
8431 case and issue a warning message in order to avoid confusion.
8432 FIXME: Maybe we ought to have an option that dumps a section with
8433 relocs applied ? */
8434 for (relsec = section_headers;
8435 relsec < section_headers + elf_header.e_shnum;
8436 ++relsec)
8438 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
8439 || relsec->sh_info >= elf_header.e_shnum
8440 || section_headers + relsec->sh_info != section
8441 || relsec->sh_size == 0
8442 || relsec->sh_link >= elf_header.e_shnum)
8443 continue;
8445 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
8446 break;
8450 addr = section->sh_addr;
8451 bytes = section->sh_size;
8452 data = start;
8454 while (bytes)
8456 int j;
8457 int k;
8458 int lbytes;
8460 lbytes = (bytes > 16 ? 16 : bytes);
8462 printf (" 0x%8.8lx ", (unsigned long) addr);
8464 for (j = 0; j < 16; j++)
8466 if (j < lbytes)
8467 printf ("%2.2x", data[j]);
8468 else
8469 printf (" ");
8471 if ((j & 3) == 3)
8472 printf (" ");
8475 for (j = 0; j < lbytes; j++)
8477 k = data[j];
8478 if (k >= ' ' && k < 0x7f)
8479 printf ("%c", k);
8480 else
8481 printf (".");
8484 putchar ('\n');
8486 data += lbytes;
8487 addr += lbytes;
8488 bytes -= lbytes;
8491 free (start);
8493 putchar ('\n');
8496 /* Uncompresses a section that was compressed using zlib, in place.
8497 This is a copy of bfd_uncompress_section_contents, in bfd/compress.c */
8499 static int
8500 uncompress_section_contents (unsigned char ** buffer, dwarf_size_type * size)
8502 #ifndef HAVE_ZLIB_H
8503 /* These are just to quiet gcc. */
8504 buffer = 0;
8505 size = 0;
8506 return FALSE;
8507 #else
8508 dwarf_size_type compressed_size = *size;
8509 unsigned char * compressed_buffer = *buffer;
8510 dwarf_size_type uncompressed_size;
8511 unsigned char * uncompressed_buffer;
8512 z_stream strm;
8513 int rc;
8514 dwarf_size_type header_size = 12;
8516 /* Read the zlib header. In this case, it should be "ZLIB" followed
8517 by the uncompressed section size, 8 bytes in big-endian order. */
8518 if (compressed_size < header_size
8519 || ! streq ((char *) compressed_buffer, "ZLIB"))
8520 return 0;
8522 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
8523 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
8524 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
8525 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
8526 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
8527 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
8528 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
8529 uncompressed_size += compressed_buffer[11];
8531 /* It is possible the section consists of several compressed
8532 buffers concatenated together, so we uncompress in a loop. */
8533 strm.zalloc = NULL;
8534 strm.zfree = NULL;
8535 strm.opaque = NULL;
8536 strm.avail_in = compressed_size - header_size;
8537 strm.next_in = (Bytef *) compressed_buffer + header_size;
8538 strm.avail_out = uncompressed_size;
8539 uncompressed_buffer = xmalloc (uncompressed_size);
8541 rc = inflateInit (& strm);
8542 while (strm.avail_in > 0)
8544 if (rc != Z_OK)
8545 goto fail;
8546 strm.next_out = ((Bytef *) uncompressed_buffer
8547 + (uncompressed_size - strm.avail_out));
8548 rc = inflate (&strm, Z_FINISH);
8549 if (rc != Z_STREAM_END)
8550 goto fail;
8551 rc = inflateReset (& strm);
8553 rc = inflateEnd (& strm);
8554 if (rc != Z_OK
8555 || strm.avail_out != 0)
8556 goto fail;
8558 free (compressed_buffer);
8559 *buffer = uncompressed_buffer;
8560 *size = uncompressed_size;
8561 return 1;
8563 fail:
8564 free (uncompressed_buffer);
8565 return 0;
8566 #endif /* HAVE_ZLIB_H */
8569 static int
8570 load_specific_debug_section (enum dwarf_section_display_enum debug,
8571 Elf_Internal_Shdr * sec, void * file)
8573 struct dwarf_section * section = &debug_displays [debug].section;
8574 char buf [64];
8575 int section_is_compressed;
8577 /* If it is already loaded, do nothing. */
8578 if (section->start != NULL)
8579 return 1;
8581 section_is_compressed = section->name == section->compressed_name;
8583 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
8584 section->address = sec->sh_addr;
8585 section->size = sec->sh_size;
8586 section->start = get_data (NULL, file, sec->sh_offset, 1,
8587 sec->sh_size, buf);
8588 if (section->start == NULL)
8589 return 0;
8591 if (section_is_compressed)
8592 if (! uncompress_section_contents (&section->start, &section->size))
8593 return 0;
8595 if (debug_displays [debug].relocate)
8596 apply_relocations (file, sec, section->start);
8598 return 1;
8602 load_debug_section (enum dwarf_section_display_enum debug, void * file)
8604 struct dwarf_section * section = &debug_displays [debug].section;
8605 Elf_Internal_Shdr * sec;
8607 /* Locate the debug section. */
8608 sec = find_section (section->uncompressed_name);
8609 if (sec != NULL)
8610 section->name = section->uncompressed_name;
8611 else
8613 sec = find_section (section->compressed_name);
8614 if (sec != NULL)
8615 section->name = section->compressed_name;
8617 if (sec == NULL)
8618 return 0;
8620 return load_specific_debug_section (debug, sec, file);
8623 void
8624 free_debug_section (enum dwarf_section_display_enum debug)
8626 struct dwarf_section * section = &debug_displays [debug].section;
8628 if (section->start == NULL)
8629 return;
8631 free ((char *) section->start);
8632 section->start = NULL;
8633 section->address = 0;
8634 section->size = 0;
8637 static int
8638 display_debug_section (Elf_Internal_Shdr * section, FILE * file)
8640 char * name = SECTION_NAME (section);
8641 bfd_size_type length;
8642 int result = 1;
8643 enum dwarf_section_display_enum i;
8645 length = section->sh_size;
8646 if (length == 0)
8648 printf (_("\nSection '%s' has no debugging data.\n"), name);
8649 return 0;
8651 if (section->sh_type == SHT_NOBITS)
8653 /* There is no point in dumping the contents of a debugging section
8654 which has the NOBITS type - the bits in the file will be random.
8655 This can happen when a file containing a .eh_frame section is
8656 stripped with the --only-keep-debug command line option. */
8657 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"), name);
8658 return 0;
8661 if (const_strneq (name, ".gnu.linkonce.wi."))
8662 name = ".debug_info";
8664 /* See if we know how to display the contents of this section. */
8665 for (i = 0; i < max; i++)
8666 if (streq (debug_displays[i].section.uncompressed_name, name)
8667 || streq (debug_displays[i].section.compressed_name, name))
8669 struct dwarf_section * sec = &debug_displays [i].section;
8670 int secondary = (section != find_section (name));
8672 if (secondary)
8673 free_debug_section (i);
8675 if (streq (debug_displays[i].section.uncompressed_name, name))
8676 sec->name = sec->uncompressed_name;
8677 else
8678 sec->name = sec->compressed_name;
8679 if (load_specific_debug_section (i, section, file))
8681 result &= debug_displays[i].display (sec, file);
8683 if (secondary || (i != info && i != abbrev))
8684 free_debug_section (i);
8687 break;
8690 if (i == max)
8692 printf (_("Unrecognized debug section: %s\n"), name);
8693 result = 0;
8696 return result;
8699 /* Set DUMP_SECTS for all sections where dumps were requested
8700 based on section name. */
8702 static void
8703 initialise_dumps_byname (void)
8705 struct dump_list_entry * cur;
8707 for (cur = dump_sects_byname; cur; cur = cur->next)
8709 unsigned int i;
8710 int any;
8712 for (i = 0, any = 0; i < elf_header.e_shnum; i++)
8713 if (streq (SECTION_NAME (section_headers + i), cur->name))
8715 request_dump_bynumber (i, cur->type);
8716 any = 1;
8719 if (!any)
8720 warn (_("Section '%s' was not dumped because it does not exist!\n"),
8721 cur->name);
8725 static void
8726 process_section_contents (FILE * file)
8728 Elf_Internal_Shdr * section;
8729 unsigned int i;
8731 if (! do_dump)
8732 return;
8734 initialise_dumps_byname ();
8736 for (i = 0, section = section_headers;
8737 i < elf_header.e_shnum && i < num_dump_sects;
8738 i++, section++)
8740 #ifdef SUPPORT_DISASSEMBLY
8741 if (dump_sects[i] & DISASS_DUMP)
8742 disassemble_section (section, file);
8743 #endif
8744 if (dump_sects[i] & HEX_DUMP)
8745 dump_section_as_bytes (section, file, FALSE);
8747 if (dump_sects[i] & RELOC_DUMP)
8748 dump_section_as_bytes (section, file, TRUE);
8750 if (dump_sects[i] & STRING_DUMP)
8751 dump_section_as_strings (section, file);
8753 if (dump_sects[i] & DEBUG_DUMP)
8754 display_debug_section (section, file);
8757 /* Check to see if the user requested a
8758 dump of a section that does not exist. */
8759 while (i++ < num_dump_sects)
8760 if (dump_sects[i])
8761 warn (_("Section %d was not dumped because it does not exist!\n"), i);
8764 static void
8765 process_mips_fpe_exception (int mask)
8767 if (mask)
8769 int first = 1;
8770 if (mask & OEX_FPU_INEX)
8771 fputs ("INEX", stdout), first = 0;
8772 if (mask & OEX_FPU_UFLO)
8773 printf ("%sUFLO", first ? "" : "|"), first = 0;
8774 if (mask & OEX_FPU_OFLO)
8775 printf ("%sOFLO", first ? "" : "|"), first = 0;
8776 if (mask & OEX_FPU_DIV0)
8777 printf ("%sDIV0", first ? "" : "|"), first = 0;
8778 if (mask & OEX_FPU_INVAL)
8779 printf ("%sINVAL", first ? "" : "|");
8781 else
8782 fputs ("0", stdout);
8785 /* ARM EABI attributes section. */
8786 typedef struct
8788 int tag;
8789 const char * name;
8790 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
8791 int type;
8792 const char ** table;
8793 } arm_attr_public_tag;
8795 static const char * arm_attr_tag_CPU_arch[] =
8796 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
8797 "v6K", "v7", "v6-M", "v6S-M"};
8798 static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
8799 static const char * arm_attr_tag_THUMB_ISA_use[] =
8800 {"No", "Thumb-1", "Thumb-2"};
8801 static const char * arm_attr_tag_VFP_arch[] =
8802 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16"};
8803 static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
8804 static const char * arm_attr_tag_Advanced_SIMD_arch[] = {"No", "NEONv1"};
8805 static const char * arm_attr_tag_PCS_config[] =
8806 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
8807 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
8808 static const char * arm_attr_tag_ABI_PCS_R9_use[] =
8809 {"V6", "SB", "TLS", "Unused"};
8810 static const char * arm_attr_tag_ABI_PCS_RW_data[] =
8811 {"Absolute", "PC-relative", "SB-relative", "None"};
8812 static const char * arm_attr_tag_ABI_PCS_RO_data[] =
8813 {"Absolute", "PC-relative", "None"};
8814 static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
8815 {"None", "direct", "GOT-indirect"};
8816 static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
8817 {"None", "??? 1", "2", "??? 3", "4"};
8818 static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
8819 static const char * arm_attr_tag_ABI_FP_denormal[] =
8820 {"Unused", "Needed", "Sign only"};
8821 static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
8822 static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
8823 static const char * arm_attr_tag_ABI_FP_number_model[] =
8824 {"Unused", "Finite", "RTABI", "IEEE 754"};
8825 static const char * arm_attr_tag_ABI_align8_needed[] = {"No", "Yes", "4-byte"};
8826 static const char * arm_attr_tag_ABI_align8_preserved[] =
8827 {"No", "Yes, except leaf SP", "Yes"};
8828 static const char * arm_attr_tag_ABI_enum_size[] =
8829 {"Unused", "small", "int", "forced to int"};
8830 static const char * arm_attr_tag_ABI_HardFP_use[] =
8831 {"As Tag_VFP_arch", "SP only", "DP only", "SP and DP"};
8832 static const char * arm_attr_tag_ABI_VFP_args[] =
8833 {"AAPCS", "VFP registers", "custom"};
8834 static const char * arm_attr_tag_ABI_WMMX_args[] =
8835 {"AAPCS", "WMMX registers", "custom"};
8836 static const char * arm_attr_tag_ABI_optimization_goals[] =
8837 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
8838 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
8839 static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
8840 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
8841 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
8842 static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
8843 static const char * arm_attr_tag_VFP_HP_extension[] =
8844 {"Not Allowed", "Allowed"};
8845 static const char * arm_attr_tag_ABI_FP_16bit_format[] =
8846 {"None", "IEEE 754", "Alternative Format"};
8847 static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
8848 static const char * arm_attr_tag_Virtualization_use[] =
8849 {"Not Allowed", "Allowed"};
8850 static const char * arm_attr_tag_MPextension_use[] = {"Not Allowed", "Allowed"};
8852 #define LOOKUP(id, name) \
8853 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
8854 static arm_attr_public_tag arm_attr_public_tags[] =
8856 {4, "CPU_raw_name", 1, NULL},
8857 {5, "CPU_name", 1, NULL},
8858 LOOKUP(6, CPU_arch),
8859 {7, "CPU_arch_profile", 0, NULL},
8860 LOOKUP(8, ARM_ISA_use),
8861 LOOKUP(9, THUMB_ISA_use),
8862 LOOKUP(10, VFP_arch),
8863 LOOKUP(11, WMMX_arch),
8864 LOOKUP(12, Advanced_SIMD_arch),
8865 LOOKUP(13, PCS_config),
8866 LOOKUP(14, ABI_PCS_R9_use),
8867 LOOKUP(15, ABI_PCS_RW_data),
8868 LOOKUP(16, ABI_PCS_RO_data),
8869 LOOKUP(17, ABI_PCS_GOT_use),
8870 LOOKUP(18, ABI_PCS_wchar_t),
8871 LOOKUP(19, ABI_FP_rounding),
8872 LOOKUP(20, ABI_FP_denormal),
8873 LOOKUP(21, ABI_FP_exceptions),
8874 LOOKUP(22, ABI_FP_user_exceptions),
8875 LOOKUP(23, ABI_FP_number_model),
8876 LOOKUP(24, ABI_align8_needed),
8877 LOOKUP(25, ABI_align8_preserved),
8878 LOOKUP(26, ABI_enum_size),
8879 LOOKUP(27, ABI_HardFP_use),
8880 LOOKUP(28, ABI_VFP_args),
8881 LOOKUP(29, ABI_WMMX_args),
8882 LOOKUP(30, ABI_optimization_goals),
8883 LOOKUP(31, ABI_FP_optimization_goals),
8884 {32, "compatibility", 0, NULL},
8885 LOOKUP(34, CPU_unaligned_access),
8886 LOOKUP(36, VFP_HP_extension),
8887 LOOKUP(38, ABI_FP_16bit_format),
8888 {64, "nodefaults", 0, NULL},
8889 {65, "also_compatible_with", 0, NULL},
8890 LOOKUP(66, T2EE_use),
8891 {67, "conformance", 1, NULL},
8892 LOOKUP(68, Virtualization_use),
8893 LOOKUP(70, MPextension_use)
8895 #undef LOOKUP
8897 /* Read an unsigned LEB128 encoded value from p. Set *PLEN to the number of
8898 bytes read. */
8900 static unsigned int
8901 read_uleb128 (unsigned char * p, unsigned int * plen)
8903 unsigned char c;
8904 unsigned int val;
8905 int shift;
8906 int len;
8908 val = 0;
8909 shift = 0;
8910 len = 0;
8913 c = *(p++);
8914 len++;
8915 val |= ((unsigned int)c & 0x7f) << shift;
8916 shift += 7;
8918 while (c & 0x80);
8920 *plen = len;
8921 return val;
8924 static unsigned char *
8925 display_arm_attribute (unsigned char * p)
8927 int tag;
8928 unsigned int len;
8929 int val;
8930 arm_attr_public_tag * attr;
8931 unsigned i;
8932 int type;
8934 tag = read_uleb128 (p, &len);
8935 p += len;
8936 attr = NULL;
8937 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
8939 if (arm_attr_public_tags[i].tag == tag)
8941 attr = &arm_attr_public_tags[i];
8942 break;
8946 if (attr)
8948 printf (" Tag_%s: ", attr->name);
8949 switch (attr->type)
8951 case 0:
8952 switch (tag)
8954 case 7: /* Tag_CPU_arch_profile. */
8955 val = read_uleb128 (p, &len);
8956 p += len;
8957 switch (val)
8959 case 0: printf ("None\n"); break;
8960 case 'A': printf ("Application\n"); break;
8961 case 'R': printf ("Realtime\n"); break;
8962 case 'M': printf ("Microcontroller\n"); break;
8963 default: printf ("??? (%d)\n", val); break;
8965 break;
8967 case 32: /* Tag_compatibility. */
8968 val = read_uleb128 (p, &len);
8969 p += len;
8970 printf ("flag = %d, vendor = %s\n", val, p);
8971 p += strlen ((char *) p) + 1;
8972 break;
8974 case 64: /* Tag_nodefaults. */
8975 p++;
8976 printf ("True\n");
8977 break;
8979 case 65: /* Tag_also_compatible_with. */
8980 val = read_uleb128 (p, &len);
8981 p += len;
8982 if (val == 6 /* Tag_CPU_arch. */)
8984 val = read_uleb128 (p, &len);
8985 p += len;
8986 if ((unsigned int)val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
8987 printf ("??? (%d)\n", val);
8988 else
8989 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
8991 else
8992 printf ("???\n");
8993 while (*(p++) != '\0' /* NUL terminator. */);
8994 break;
8996 default:
8997 abort ();
8999 return p;
9001 case 1:
9002 case 2:
9003 type = attr->type;
9004 break;
9006 default:
9007 assert (attr->type & 0x80);
9008 val = read_uleb128 (p, &len);
9009 p += len;
9010 type = attr->type & 0x7f;
9011 if (val >= type)
9012 printf ("??? (%d)\n", val);
9013 else
9014 printf ("%s\n", attr->table[val]);
9015 return p;
9018 else
9020 if (tag & 1)
9021 type = 1; /* String. */
9022 else
9023 type = 2; /* uleb128. */
9024 printf (" Tag_unknown_%d: ", tag);
9027 if (type == 1)
9029 printf ("\"%s\"\n", p);
9030 p += strlen ((char *) p) + 1;
9032 else
9034 val = read_uleb128 (p, &len);
9035 p += len;
9036 printf ("%d (0x%x)\n", val, val);
9039 return p;
9042 static unsigned char *
9043 display_gnu_attribute (unsigned char * p,
9044 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
9046 int tag;
9047 unsigned int len;
9048 int val;
9049 int type;
9051 tag = read_uleb128 (p, &len);
9052 p += len;
9054 /* Tag_compatibility is the only generic GNU attribute defined at
9055 present. */
9056 if (tag == 32)
9058 val = read_uleb128 (p, &len);
9059 p += len;
9060 printf ("flag = %d, vendor = %s\n", val, p);
9061 p += strlen ((char *) p) + 1;
9062 return p;
9065 if ((tag & 2) == 0 && display_proc_gnu_attribute)
9066 return display_proc_gnu_attribute (p, tag);
9068 if (tag & 1)
9069 type = 1; /* String. */
9070 else
9071 type = 2; /* uleb128. */
9072 printf (" Tag_unknown_%d: ", tag);
9074 if (type == 1)
9076 printf ("\"%s\"\n", p);
9077 p += strlen ((char *) p) + 1;
9079 else
9081 val = read_uleb128 (p, &len);
9082 p += len;
9083 printf ("%d (0x%x)\n", val, val);
9086 return p;
9089 static unsigned char *
9090 display_power_gnu_attribute (unsigned char * p, int tag)
9092 int type;
9093 unsigned int len;
9094 int val;
9096 if (tag == Tag_GNU_Power_ABI_FP)
9098 val = read_uleb128 (p, &len);
9099 p += len;
9100 printf (" Tag_GNU_Power_ABI_FP: ");
9102 switch (val)
9104 case 0:
9105 printf ("Hard or soft float\n");
9106 break;
9107 case 1:
9108 printf ("Hard float\n");
9109 break;
9110 case 2:
9111 printf ("Soft float\n");
9112 break;
9113 case 3:
9114 printf ("Single-precision hard float\n");
9115 break;
9116 default:
9117 printf ("??? (%d)\n", val);
9118 break;
9120 return p;
9123 if (tag == Tag_GNU_Power_ABI_Vector)
9125 val = read_uleb128 (p, &len);
9126 p += len;
9127 printf (" Tag_GNU_Power_ABI_Vector: ");
9128 switch (val)
9130 case 0:
9131 printf ("Any\n");
9132 break;
9133 case 1:
9134 printf ("Generic\n");
9135 break;
9136 case 2:
9137 printf ("AltiVec\n");
9138 break;
9139 case 3:
9140 printf ("SPE\n");
9141 break;
9142 default:
9143 printf ("??? (%d)\n", val);
9144 break;
9146 return p;
9149 if (tag == Tag_GNU_Power_ABI_Struct_Return)
9151 val = read_uleb128 (p, &len);
9152 p += len;
9153 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
9154 switch (val)
9156 case 0:
9157 printf ("Any\n");
9158 break;
9159 case 1:
9160 printf ("r3/r4\n");
9161 break;
9162 case 2:
9163 printf ("Memory\n");
9164 break;
9165 default:
9166 printf ("??? (%d)\n", val);
9167 break;
9169 return p;
9172 if (tag & 1)
9173 type = 1; /* String. */
9174 else
9175 type = 2; /* uleb128. */
9176 printf (" Tag_unknown_%d: ", tag);
9178 if (type == 1)
9180 printf ("\"%s\"\n", p);
9181 p += strlen ((char *) p) + 1;
9183 else
9185 val = read_uleb128 (p, &len);
9186 p += len;
9187 printf ("%d (0x%x)\n", val, val);
9190 return p;
9193 static unsigned char *
9194 display_mips_gnu_attribute (unsigned char * p, int tag)
9196 int type;
9197 unsigned int len;
9198 int val;
9200 if (tag == Tag_GNU_MIPS_ABI_FP)
9202 val = read_uleb128 (p, &len);
9203 p += len;
9204 printf (" Tag_GNU_MIPS_ABI_FP: ");
9206 switch (val)
9208 case 0:
9209 printf ("Hard or soft float\n");
9210 break;
9211 case 1:
9212 printf ("Hard float (-mdouble-float)\n");
9213 break;
9214 case 2:
9215 printf ("Hard float (-msingle-float)\n");
9216 break;
9217 case 3:
9218 printf ("Soft float\n");
9219 break;
9220 case 4:
9221 printf ("64-bit float (-mips32r2 -mfp64)\n");
9222 break;
9223 default:
9224 printf ("??? (%d)\n", val);
9225 break;
9227 return p;
9230 if (tag & 1)
9231 type = 1; /* String. */
9232 else
9233 type = 2; /* uleb128. */
9234 printf (" Tag_unknown_%d: ", tag);
9236 if (type == 1)
9238 printf ("\"%s\"\n", p);
9239 p += strlen ((char *) p) + 1;
9241 else
9243 val = read_uleb128 (p, &len);
9244 p += len;
9245 printf ("%d (0x%x)\n", val, val);
9248 return p;
9251 static int
9252 process_attributes (FILE * file,
9253 const char * public_name,
9254 unsigned int proc_type,
9255 unsigned char * (* display_pub_attribute) (unsigned char *),
9256 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
9258 Elf_Internal_Shdr * sect;
9259 unsigned char * contents;
9260 unsigned char * p;
9261 unsigned char * end;
9262 bfd_vma section_len;
9263 bfd_vma len;
9264 unsigned i;
9266 /* Find the section header so that we get the size. */
9267 for (i = 0, sect = section_headers;
9268 i < elf_header.e_shnum;
9269 i++, sect++)
9271 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
9272 continue;
9274 contents = get_data (NULL, file, sect->sh_offset, 1, sect->sh_size,
9275 _("attributes"));
9276 if (contents == NULL)
9277 continue;
9279 p = contents;
9280 if (*p == 'A')
9282 len = sect->sh_size - 1;
9283 p++;
9285 while (len > 0)
9287 int namelen;
9288 bfd_boolean public_section;
9289 bfd_boolean gnu_section;
9291 section_len = byte_get (p, 4);
9292 p += 4;
9294 if (section_len > len)
9296 printf (_("ERROR: Bad section length (%d > %d)\n"),
9297 (int) section_len, (int) len);
9298 section_len = len;
9301 len -= section_len;
9302 printf ("Attribute Section: %s\n", p);
9304 if (public_name && streq ((char *) p, public_name))
9305 public_section = TRUE;
9306 else
9307 public_section = FALSE;
9309 if (streq ((char *) p, "gnu"))
9310 gnu_section = TRUE;
9311 else
9312 gnu_section = FALSE;
9314 namelen = strlen ((char *) p) + 1;
9315 p += namelen;
9316 section_len -= namelen + 4;
9318 while (section_len > 0)
9320 int tag = *(p++);
9321 int val;
9322 bfd_vma size;
9324 size = byte_get (p, 4);
9325 if (size > section_len)
9327 printf (_("ERROR: Bad subsection length (%d > %d)\n"),
9328 (int) size, (int) section_len);
9329 size = section_len;
9332 section_len -= size;
9333 end = p + size - 1;
9334 p += 4;
9336 switch (tag)
9338 case 1:
9339 printf ("File Attributes\n");
9340 break;
9341 case 2:
9342 printf ("Section Attributes:");
9343 goto do_numlist;
9344 case 3:
9345 printf ("Symbol Attributes:");
9346 do_numlist:
9347 for (;;)
9349 unsigned int i;
9351 val = read_uleb128 (p, &i);
9352 p += i;
9353 if (val == 0)
9354 break;
9355 printf (" %d", val);
9357 printf ("\n");
9358 break;
9359 default:
9360 printf ("Unknown tag: %d\n", tag);
9361 public_section = FALSE;
9362 break;
9365 if (public_section)
9367 while (p < end)
9368 p = display_pub_attribute (p);
9370 else if (gnu_section)
9372 while (p < end)
9373 p = display_gnu_attribute (p,
9374 display_proc_gnu_attribute);
9376 else
9378 /* ??? Do something sensible, like dump hex. */
9379 printf (" Unknown section contexts\n");
9380 p = end;
9385 else
9386 printf (_("Unknown format '%c'\n"), *p);
9388 free (contents);
9390 return 1;
9393 static int
9394 process_arm_specific (FILE * file)
9396 return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
9397 display_arm_attribute, NULL);
9400 static int
9401 process_power_specific (FILE * file)
9403 return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
9404 display_power_gnu_attribute);
9407 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
9408 Print the Address, Access and Initial fields of an entry at VMA ADDR
9409 and return the VMA of the next entry. */
9411 static bfd_vma
9412 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
9414 printf (" ");
9415 print_vma (addr, LONG_HEX);
9416 printf (" ");
9417 if (addr < pltgot + 0xfff0)
9418 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
9419 else
9420 printf ("%10s", "");
9421 printf (" ");
9422 if (data == NULL)
9423 printf ("%*s", is_32bit_elf ? 8 : 16, "<unknown>");
9424 else
9426 bfd_vma entry;
9428 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
9429 print_vma (entry, LONG_HEX);
9431 return addr + (is_32bit_elf ? 4 : 8);
9434 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
9435 PLTGOT. Print the Address and Initial fields of an entry at VMA
9436 ADDR and return the VMA of the next entry. */
9438 static bfd_vma
9439 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
9441 printf (" ");
9442 print_vma (addr, LONG_HEX);
9443 printf (" ");
9444 if (data == NULL)
9445 printf ("%*s", is_32bit_elf ? 8 : 16, "<unknown>");
9446 else
9448 bfd_vma entry;
9450 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
9451 print_vma (entry, LONG_HEX);
9453 return addr + (is_32bit_elf ? 4 : 8);
9456 static int
9457 process_mips_specific (FILE * file)
9459 Elf_Internal_Dyn * entry;
9460 size_t liblist_offset = 0;
9461 size_t liblistno = 0;
9462 size_t conflictsno = 0;
9463 size_t options_offset = 0;
9464 size_t conflicts_offset = 0;
9465 size_t pltrelsz = 0;
9466 size_t pltrel = 0;
9467 bfd_vma pltgot = 0;
9468 bfd_vma mips_pltgot = 0;
9469 bfd_vma jmprel = 0;
9470 bfd_vma local_gotno = 0;
9471 bfd_vma gotsym = 0;
9472 bfd_vma symtabno = 0;
9474 process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
9475 display_mips_gnu_attribute);
9477 /* We have a lot of special sections. Thanks SGI! */
9478 if (dynamic_section == NULL)
9479 /* No information available. */
9480 return 0;
9482 for (entry = dynamic_section; entry->d_tag != DT_NULL; ++entry)
9483 switch (entry->d_tag)
9485 case DT_MIPS_LIBLIST:
9486 liblist_offset
9487 = offset_from_vma (file, entry->d_un.d_val,
9488 liblistno * sizeof (Elf32_External_Lib));
9489 break;
9490 case DT_MIPS_LIBLISTNO:
9491 liblistno = entry->d_un.d_val;
9492 break;
9493 case DT_MIPS_OPTIONS:
9494 options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
9495 break;
9496 case DT_MIPS_CONFLICT:
9497 conflicts_offset
9498 = offset_from_vma (file, entry->d_un.d_val,
9499 conflictsno * sizeof (Elf32_External_Conflict));
9500 break;
9501 case DT_MIPS_CONFLICTNO:
9502 conflictsno = entry->d_un.d_val;
9503 break;
9504 case DT_PLTGOT:
9505 pltgot = entry->d_un.d_ptr;
9506 break;
9507 case DT_MIPS_LOCAL_GOTNO:
9508 local_gotno = entry->d_un.d_val;
9509 break;
9510 case DT_MIPS_GOTSYM:
9511 gotsym = entry->d_un.d_val;
9512 break;
9513 case DT_MIPS_SYMTABNO:
9514 symtabno = entry->d_un.d_val;
9515 break;
9516 case DT_MIPS_PLTGOT:
9517 mips_pltgot = entry->d_un.d_ptr;
9518 break;
9519 case DT_PLTREL:
9520 pltrel = entry->d_un.d_val;
9521 break;
9522 case DT_PLTRELSZ:
9523 pltrelsz = entry->d_un.d_val;
9524 break;
9525 case DT_JMPREL:
9526 jmprel = entry->d_un.d_ptr;
9527 break;
9528 default:
9529 break;
9532 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
9534 Elf32_External_Lib * elib;
9535 size_t cnt;
9537 elib = get_data (NULL, file, liblist_offset,
9538 liblistno, sizeof (Elf32_External_Lib),
9539 _("liblist"));
9540 if (elib)
9542 printf ("\nSection '.liblist' contains %lu entries:\n",
9543 (unsigned long) liblistno);
9544 fputs (" Library Time Stamp Checksum Version Flags\n",
9545 stdout);
9547 for (cnt = 0; cnt < liblistno; ++cnt)
9549 Elf32_Lib liblist;
9550 time_t time;
9551 char timebuf[20];
9552 struct tm * tmp;
9554 liblist.l_name = BYTE_GET (elib[cnt].l_name);
9555 time = BYTE_GET (elib[cnt].l_time_stamp);
9556 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
9557 liblist.l_version = BYTE_GET (elib[cnt].l_version);
9558 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
9560 tmp = gmtime (&time);
9561 snprintf (timebuf, sizeof (timebuf),
9562 "%04u-%02u-%02uT%02u:%02u:%02u",
9563 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9564 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9566 printf ("%3lu: ", (unsigned long) cnt);
9567 if (VALID_DYNAMIC_NAME (liblist.l_name))
9568 print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
9569 else
9570 printf ("<corrupt: %9ld>", liblist.l_name);
9571 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
9572 liblist.l_version);
9574 if (liblist.l_flags == 0)
9575 puts (" NONE");
9576 else
9578 static const struct
9580 const char * name;
9581 int bit;
9583 l_flags_vals[] =
9585 { " EXACT_MATCH", LL_EXACT_MATCH },
9586 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
9587 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
9588 { " EXPORTS", LL_EXPORTS },
9589 { " DELAY_LOAD", LL_DELAY_LOAD },
9590 { " DELTA", LL_DELTA }
9592 int flags = liblist.l_flags;
9593 size_t fcnt;
9595 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
9596 if ((flags & l_flags_vals[fcnt].bit) != 0)
9598 fputs (l_flags_vals[fcnt].name, stdout);
9599 flags ^= l_flags_vals[fcnt].bit;
9601 if (flags != 0)
9602 printf (" %#x", (unsigned int) flags);
9604 puts ("");
9608 free (elib);
9612 if (options_offset != 0)
9614 Elf_External_Options * eopt;
9615 Elf_Internal_Shdr * sect = section_headers;
9616 Elf_Internal_Options * iopt;
9617 Elf_Internal_Options * option;
9618 size_t offset;
9619 int cnt;
9621 /* Find the section header so that we get the size. */
9622 while (sect->sh_type != SHT_MIPS_OPTIONS)
9623 ++sect;
9625 eopt = get_data (NULL, file, options_offset, 1, sect->sh_size,
9626 _("options"));
9627 if (eopt)
9629 iopt = cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
9630 if (iopt == NULL)
9632 error (_("Out of memory\n"));
9633 return 0;
9636 offset = cnt = 0;
9637 option = iopt;
9639 while (offset < sect->sh_size)
9641 Elf_External_Options * eoption;
9643 eoption = (Elf_External_Options *) ((char *) eopt + offset);
9645 option->kind = BYTE_GET (eoption->kind);
9646 option->size = BYTE_GET (eoption->size);
9647 option->section = BYTE_GET (eoption->section);
9648 option->info = BYTE_GET (eoption->info);
9650 offset += option->size;
9652 ++option;
9653 ++cnt;
9656 printf (_("\nSection '%s' contains %d entries:\n"),
9657 SECTION_NAME (sect), cnt);
9659 option = iopt;
9661 while (cnt-- > 0)
9663 size_t len;
9665 switch (option->kind)
9667 case ODK_NULL:
9668 /* This shouldn't happen. */
9669 printf (" NULL %d %lx", option->section, option->info);
9670 break;
9671 case ODK_REGINFO:
9672 printf (" REGINFO ");
9673 if (elf_header.e_machine == EM_MIPS)
9675 /* 32bit form. */
9676 Elf32_External_RegInfo * ereg;
9677 Elf32_RegInfo reginfo;
9679 ereg = (Elf32_External_RegInfo *) (option + 1);
9680 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9681 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9682 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9683 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9684 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9685 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
9687 printf ("GPR %08lx GP 0x%lx\n",
9688 reginfo.ri_gprmask,
9689 (unsigned long) reginfo.ri_gp_value);
9690 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
9691 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9692 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9694 else
9696 /* 64 bit form. */
9697 Elf64_External_RegInfo * ereg;
9698 Elf64_Internal_RegInfo reginfo;
9700 ereg = (Elf64_External_RegInfo *) (option + 1);
9701 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9702 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9703 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9704 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9705 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9706 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
9708 printf ("GPR %08lx GP 0x",
9709 reginfo.ri_gprmask);
9710 printf_vma (reginfo.ri_gp_value);
9711 printf ("\n");
9713 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
9714 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9715 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9717 ++option;
9718 continue;
9719 case ODK_EXCEPTIONS:
9720 fputs (" EXCEPTIONS fpe_min(", stdout);
9721 process_mips_fpe_exception (option->info & OEX_FPU_MIN);
9722 fputs (") fpe_max(", stdout);
9723 process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
9724 fputs (")", stdout);
9726 if (option->info & OEX_PAGE0)
9727 fputs (" PAGE0", stdout);
9728 if (option->info & OEX_SMM)
9729 fputs (" SMM", stdout);
9730 if (option->info & OEX_FPDBUG)
9731 fputs (" FPDBUG", stdout);
9732 if (option->info & OEX_DISMISS)
9733 fputs (" DISMISS", stdout);
9734 break;
9735 case ODK_PAD:
9736 fputs (" PAD ", stdout);
9737 if (option->info & OPAD_PREFIX)
9738 fputs (" PREFIX", stdout);
9739 if (option->info & OPAD_POSTFIX)
9740 fputs (" POSTFIX", stdout);
9741 if (option->info & OPAD_SYMBOL)
9742 fputs (" SYMBOL", stdout);
9743 break;
9744 case ODK_HWPATCH:
9745 fputs (" HWPATCH ", stdout);
9746 if (option->info & OHW_R4KEOP)
9747 fputs (" R4KEOP", stdout);
9748 if (option->info & OHW_R8KPFETCH)
9749 fputs (" R8KPFETCH", stdout);
9750 if (option->info & OHW_R5KEOP)
9751 fputs (" R5KEOP", stdout);
9752 if (option->info & OHW_R5KCVTL)
9753 fputs (" R5KCVTL", stdout);
9754 break;
9755 case ODK_FILL:
9756 fputs (" FILL ", stdout);
9757 /* XXX Print content of info word? */
9758 break;
9759 case ODK_TAGS:
9760 fputs (" TAGS ", stdout);
9761 /* XXX Print content of info word? */
9762 break;
9763 case ODK_HWAND:
9764 fputs (" HWAND ", stdout);
9765 if (option->info & OHWA0_R4KEOP_CHECKED)
9766 fputs (" R4KEOP_CHECKED", stdout);
9767 if (option->info & OHWA0_R4KEOP_CLEAN)
9768 fputs (" R4KEOP_CLEAN", stdout);
9769 break;
9770 case ODK_HWOR:
9771 fputs (" HWOR ", stdout);
9772 if (option->info & OHWA0_R4KEOP_CHECKED)
9773 fputs (" R4KEOP_CHECKED", stdout);
9774 if (option->info & OHWA0_R4KEOP_CLEAN)
9775 fputs (" R4KEOP_CLEAN", stdout);
9776 break;
9777 case ODK_GP_GROUP:
9778 printf (" GP_GROUP %#06lx self-contained %#06lx",
9779 option->info & OGP_GROUP,
9780 (option->info & OGP_SELF) >> 16);
9781 break;
9782 case ODK_IDENT:
9783 printf (" IDENT %#06lx self-contained %#06lx",
9784 option->info & OGP_GROUP,
9785 (option->info & OGP_SELF) >> 16);
9786 break;
9787 default:
9788 /* This shouldn't happen. */
9789 printf (" %3d ??? %d %lx",
9790 option->kind, option->section, option->info);
9791 break;
9794 len = sizeof (* eopt);
9795 while (len < option->size)
9796 if (((char *) option)[len] >= ' '
9797 && ((char *) option)[len] < 0x7f)
9798 printf ("%c", ((char *) option)[len++]);
9799 else
9800 printf ("\\%03o", ((char *) option)[len++]);
9802 fputs ("\n", stdout);
9803 ++option;
9806 free (eopt);
9810 if (conflicts_offset != 0 && conflictsno != 0)
9812 Elf32_Conflict * iconf;
9813 size_t cnt;
9815 if (dynamic_symbols == NULL)
9817 error (_("conflict list found without a dynamic symbol table\n"));
9818 return 0;
9821 iconf = cmalloc (conflictsno, sizeof (* iconf));
9822 if (iconf == NULL)
9824 error (_("Out of memory\n"));
9825 return 0;
9828 if (is_32bit_elf)
9830 Elf32_External_Conflict * econf32;
9832 econf32 = get_data (NULL, file, conflicts_offset,
9833 conflictsno, sizeof (* econf32), _("conflict"));
9834 if (!econf32)
9835 return 0;
9837 for (cnt = 0; cnt < conflictsno; ++cnt)
9838 iconf[cnt] = BYTE_GET (econf32[cnt]);
9840 free (econf32);
9842 else
9844 Elf64_External_Conflict * econf64;
9846 econf64 = get_data (NULL, file, conflicts_offset,
9847 conflictsno, sizeof (* econf64), _("conflict"));
9848 if (!econf64)
9849 return 0;
9851 for (cnt = 0; cnt < conflictsno; ++cnt)
9852 iconf[cnt] = BYTE_GET (econf64[cnt]);
9854 free (econf64);
9857 printf (_("\nSection '.conflict' contains %lu entries:\n"),
9858 (unsigned long) conflictsno);
9859 puts (_(" Num: Index Value Name"));
9861 for (cnt = 0; cnt < conflictsno; ++cnt)
9863 Elf_Internal_Sym * psym = & dynamic_symbols[iconf[cnt]];
9865 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
9866 print_vma (psym->st_value, FULL_HEX);
9867 putchar (' ');
9868 if (VALID_DYNAMIC_NAME (psym->st_name))
9869 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
9870 else
9871 printf ("<corrupt: %14ld>", psym->st_name);
9872 putchar ('\n');
9875 free (iconf);
9878 if (pltgot != 0 && local_gotno != 0)
9880 bfd_vma entry, local_end, global_end;
9881 size_t i, offset;
9882 unsigned char * data;
9883 int addr_size;
9885 entry = pltgot;
9886 addr_size = (is_32bit_elf ? 4 : 8);
9887 local_end = pltgot + local_gotno * addr_size;
9888 global_end = local_end + (symtabno - gotsym) * addr_size;
9890 offset = offset_from_vma (file, pltgot, global_end - pltgot);
9891 data = get_data (NULL, file, offset, global_end - pltgot, 1, _("GOT"));
9892 printf (_("\nPrimary GOT:\n"));
9893 printf (_(" Canonical gp value: "));
9894 print_vma (pltgot + 0x7ff0, LONG_HEX);
9895 printf ("\n\n");
9897 printf (_(" Reserved entries:\n"));
9898 printf (_(" %*s %10s %*s Purpose\n"),
9899 addr_size * 2, "Address", "Access",
9900 addr_size * 2, "Initial");
9901 entry = print_mips_got_entry (data, pltgot, entry);
9902 printf (" Lazy resolver\n");
9903 if (data
9904 && (byte_get (data + entry - pltgot, addr_size)
9905 >> (addr_size * 8 - 1)) != 0)
9907 entry = print_mips_got_entry (data, pltgot, entry);
9908 printf (" Module pointer (GNU extension)\n");
9910 printf ("\n");
9912 if (entry < local_end)
9914 printf (_(" Local entries:\n"));
9915 printf (_(" %*s %10s %*s\n"),
9916 addr_size * 2, "Address", "Access",
9917 addr_size * 2, "Initial");
9918 while (entry < local_end)
9920 entry = print_mips_got_entry (data, pltgot, entry);
9921 printf ("\n");
9923 printf ("\n");
9926 if (gotsym < symtabno)
9928 int sym_width;
9930 printf (_(" Global entries:\n"));
9931 printf (_(" %*s %10s %*s %*s %-7s %3s %s\n"),
9932 addr_size * 2, "Address", "Access",
9933 addr_size * 2, "Initial",
9934 addr_size * 2, "Sym.Val.", "Type", "Ndx", "Name");
9935 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
9936 for (i = gotsym; i < symtabno; i++)
9938 Elf_Internal_Sym * psym;
9940 psym = dynamic_symbols + i;
9941 entry = print_mips_got_entry (data, pltgot, entry);
9942 printf (" ");
9943 print_vma (psym->st_value, LONG_HEX);
9944 printf (" %-7s %3s ",
9945 get_symbol_type (ELF_ST_TYPE (psym->st_info)),
9946 get_symbol_index_type (psym->st_shndx));
9947 if (VALID_DYNAMIC_NAME (psym->st_name))
9948 print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
9949 else
9950 printf ("<corrupt: %14ld>", psym->st_name);
9951 printf ("\n");
9953 printf ("\n");
9956 if (data)
9957 free (data);
9960 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
9962 bfd_vma entry, end;
9963 size_t offset, rel_offset;
9964 unsigned long count, i;
9965 unsigned char * data;
9966 int addr_size, sym_width;
9967 Elf_Internal_Rela * rels;
9969 rel_offset = offset_from_vma (file, jmprel, pltrelsz);
9970 if (pltrel == DT_RELA)
9972 if (!slurp_rela_relocs (file, rel_offset, pltrelsz, &rels, &count))
9973 return 0;
9975 else
9977 if (!slurp_rel_relocs (file, rel_offset, pltrelsz, &rels, &count))
9978 return 0;
9981 entry = mips_pltgot;
9982 addr_size = (is_32bit_elf ? 4 : 8);
9983 end = mips_pltgot + (2 + count) * addr_size;
9985 offset = offset_from_vma (file, mips_pltgot, end - mips_pltgot);
9986 data = get_data (NULL, file, offset, end - mips_pltgot, 1, _("PLT GOT"));
9987 printf (_("\nPLT GOT:\n\n"));
9988 printf (_(" Reserved entries:\n"));
9989 printf (_(" %*s %*s Purpose\n"),
9990 addr_size * 2, "Address", addr_size * 2, "Initial");
9991 entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
9992 printf (" PLT lazy resolver\n");
9993 entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
9994 printf (" Module pointer\n");
9995 printf ("\n");
9997 printf (_(" Entries:\n"));
9998 printf (_(" %*s %*s %*s %-7s %3s %s\n"),
9999 addr_size * 2, "Address",
10000 addr_size * 2, "Initial",
10001 addr_size * 2, "Sym.Val.", "Type", "Ndx", "Name");
10002 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
10003 for (i = 0; i < count; i++)
10005 Elf_Internal_Sym * psym;
10007 psym = dynamic_symbols + get_reloc_symindex (rels[i].r_info);
10008 entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
10009 printf (" ");
10010 print_vma (psym->st_value, LONG_HEX);
10011 printf (" %-7s %3s ",
10012 get_symbol_type (ELF_ST_TYPE (psym->st_info)),
10013 get_symbol_index_type (psym->st_shndx));
10014 if (VALID_DYNAMIC_NAME (psym->st_name))
10015 print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
10016 else
10017 printf ("<corrupt: %14ld>", psym->st_name);
10018 printf ("\n");
10020 printf ("\n");
10022 if (data)
10023 free (data);
10024 free (rels);
10027 return 1;
10030 static int
10031 process_gnu_liblist (FILE * file)
10033 Elf_Internal_Shdr * section;
10034 Elf_Internal_Shdr * string_sec;
10035 Elf32_External_Lib * elib;
10036 char * strtab;
10037 size_t strtab_size;
10038 size_t cnt;
10039 unsigned i;
10041 if (! do_arch)
10042 return 0;
10044 for (i = 0, section = section_headers;
10045 i < elf_header.e_shnum;
10046 i++, section++)
10048 switch (section->sh_type)
10050 case SHT_GNU_LIBLIST:
10051 if (section->sh_link >= elf_header.e_shnum)
10052 break;
10054 elib = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
10055 _("liblist"));
10057 if (elib == NULL)
10058 break;
10059 string_sec = section_headers + section->sh_link;
10061 strtab = get_data (NULL, file, string_sec->sh_offset, 1,
10062 string_sec->sh_size, _("liblist string table"));
10063 strtab_size = string_sec->sh_size;
10065 if (strtab == NULL
10066 || section->sh_entsize != sizeof (Elf32_External_Lib))
10068 free (elib);
10069 break;
10072 printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
10073 SECTION_NAME (section),
10074 (unsigned long) (section->sh_size / sizeof (Elf32_External_Lib)));
10076 puts (" Library Time Stamp Checksum Version Flags");
10078 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
10079 ++cnt)
10081 Elf32_Lib liblist;
10082 time_t time;
10083 char timebuf[20];
10084 struct tm * tmp;
10086 liblist.l_name = BYTE_GET (elib[cnt].l_name);
10087 time = BYTE_GET (elib[cnt].l_time_stamp);
10088 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
10089 liblist.l_version = BYTE_GET (elib[cnt].l_version);
10090 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
10092 tmp = gmtime (&time);
10093 snprintf (timebuf, sizeof (timebuf),
10094 "%04u-%02u-%02uT%02u:%02u:%02u",
10095 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10096 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10098 printf ("%3lu: ", (unsigned long) cnt);
10099 if (do_wide)
10100 printf ("%-20s", liblist.l_name < strtab_size
10101 ? strtab + liblist.l_name : "<corrupt>");
10102 else
10103 printf ("%-20.20s", liblist.l_name < strtab_size
10104 ? strtab + liblist.l_name : "<corrupt>");
10105 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
10106 liblist.l_version, liblist.l_flags);
10109 free (elib);
10113 return 1;
10116 static const char *
10117 get_note_type (unsigned e_type)
10119 static char buff[64];
10121 if (elf_header.e_type == ET_CORE)
10122 switch (e_type)
10124 case NT_AUXV:
10125 return _("NT_AUXV (auxiliary vector)");
10126 case NT_PRSTATUS:
10127 return _("NT_PRSTATUS (prstatus structure)");
10128 case NT_FPREGSET:
10129 return _("NT_FPREGSET (floating point registers)");
10130 case NT_PRPSINFO:
10131 return _("NT_PRPSINFO (prpsinfo structure)");
10132 case NT_TASKSTRUCT:
10133 return _("NT_TASKSTRUCT (task structure)");
10134 case NT_PRXFPREG:
10135 return _("NT_PRXFPREG (user_xfpregs structure)");
10136 case NT_PPC_VMX:
10137 return _("NT_PPC_VMX (ppc Altivec registers)");
10138 case NT_PPC_VSX:
10139 return _("NT_PPC_VSX (ppc VSX registers)");
10140 case NT_PSTATUS:
10141 return _("NT_PSTATUS (pstatus structure)");
10142 case NT_FPREGS:
10143 return _("NT_FPREGS (floating point registers)");
10144 case NT_PSINFO:
10145 return _("NT_PSINFO (psinfo structure)");
10146 case NT_LWPSTATUS:
10147 return _("NT_LWPSTATUS (lwpstatus_t structure)");
10148 case NT_LWPSINFO:
10149 return _("NT_LWPSINFO (lwpsinfo_t structure)");
10150 case NT_WIN32PSTATUS:
10151 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
10152 default:
10153 break;
10155 else
10156 switch (e_type)
10158 case NT_VERSION:
10159 return _("NT_VERSION (version)");
10160 case NT_ARCH:
10161 return _("NT_ARCH (architecture)");
10162 default:
10163 break;
10166 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10167 return buff;
10170 static const char *
10171 get_gnu_elf_note_type (unsigned e_type)
10173 static char buff[64];
10175 switch (e_type)
10177 case NT_GNU_ABI_TAG:
10178 return _("NT_GNU_ABI_TAG (ABI version tag)");
10179 case NT_GNU_HWCAP:
10180 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
10181 case NT_GNU_BUILD_ID:
10182 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
10183 case NT_GNU_GOLD_VERSION:
10184 return _("NT_GNU_GOLD_VERSION (gold version)");
10185 default:
10186 break;
10189 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10190 return buff;
10193 static const char *
10194 get_netbsd_elfcore_note_type (unsigned e_type)
10196 static char buff[64];
10198 if (e_type == NT_NETBSDCORE_PROCINFO)
10200 /* NetBSD core "procinfo" structure. */
10201 return _("NetBSD procinfo structure");
10204 /* As of Jan 2002 there are no other machine-independent notes
10205 defined for NetBSD core files. If the note type is less
10206 than the start of the machine-dependent note types, we don't
10207 understand it. */
10209 if (e_type < NT_NETBSDCORE_FIRSTMACH)
10211 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10212 return buff;
10215 switch (elf_header.e_machine)
10217 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
10218 and PT_GETFPREGS == mach+2. */
10220 case EM_OLD_ALPHA:
10221 case EM_ALPHA:
10222 case EM_SPARC:
10223 case EM_SPARC32PLUS:
10224 case EM_SPARCV9:
10225 switch (e_type)
10227 case NT_NETBSDCORE_FIRSTMACH+0:
10228 return _("PT_GETREGS (reg structure)");
10229 case NT_NETBSDCORE_FIRSTMACH+2:
10230 return _("PT_GETFPREGS (fpreg structure)");
10231 default:
10232 break;
10234 break;
10236 /* On all other arch's, PT_GETREGS == mach+1 and
10237 PT_GETFPREGS == mach+3. */
10238 default:
10239 switch (e_type)
10241 case NT_NETBSDCORE_FIRSTMACH+1:
10242 return _("PT_GETREGS (reg structure)");
10243 case NT_NETBSDCORE_FIRSTMACH+3:
10244 return _("PT_GETFPREGS (fpreg structure)");
10245 default:
10246 break;
10250 snprintf (buff, sizeof (buff), _("PT_FIRSTMACH+%d"),
10251 e_type - NT_NETBSDCORE_FIRSTMACH);
10252 return buff;
10255 /* Note that by the ELF standard, the name field is already null byte
10256 terminated, and namesz includes the terminating null byte.
10257 I.E. the value of namesz for the name "FSF" is 4.
10259 If the value of namesz is zero, there is no name present. */
10260 static int
10261 process_note (Elf_Internal_Note * pnote)
10263 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
10264 const char * nt;
10266 if (pnote->namesz == 0)
10267 /* If there is no note name, then use the default set of
10268 note type strings. */
10269 nt = get_note_type (pnote->type);
10271 else if (const_strneq (pnote->namedata, "GNU"))
10272 /* GNU-specific object file notes. */
10273 nt = get_gnu_elf_note_type (pnote->type);
10275 else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
10276 /* NetBSD-specific core file notes. */
10277 nt = get_netbsd_elfcore_note_type (pnote->type);
10279 else if (strneq (pnote->namedata, "SPU/", 4))
10281 /* SPU-specific core file notes. */
10282 nt = pnote->namedata + 4;
10283 name = "SPU";
10286 else
10287 /* Don't recognize this note name; just use the default set of
10288 note type strings. */
10289 nt = get_note_type (pnote->type);
10291 printf (" %s\t\t0x%08lx\t%s\n", name, pnote->descsz, nt);
10292 return 1;
10296 static int
10297 process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
10299 Elf_External_Note * pnotes;
10300 Elf_External_Note * external;
10301 int res = 1;
10303 if (length <= 0)
10304 return 0;
10306 pnotes = get_data (NULL, file, offset, 1, length, _("notes"));
10307 if (!pnotes)
10308 return 0;
10310 external = pnotes;
10312 printf (_("\nNotes at offset 0x%08lx with length 0x%08lx:\n"),
10313 (unsigned long) offset, (unsigned long) length);
10314 printf (_(" Owner\t\tData size\tDescription\n"));
10316 while (external < (Elf_External_Note *) ((char *) pnotes + length))
10318 Elf_External_Note * next;
10319 Elf_Internal_Note inote;
10320 char * temp = NULL;
10322 inote.type = BYTE_GET (external->type);
10323 inote.namesz = BYTE_GET (external->namesz);
10324 inote.namedata = external->name;
10325 inote.descsz = BYTE_GET (external->descsz);
10326 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
10327 inote.descpos = offset + (inote.descdata - (char *) pnotes);
10329 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
10331 if (((char *) next) > (((char *) pnotes) + length))
10333 warn (_("corrupt note found at offset %lx into core notes\n"),
10334 (unsigned long) ((char *) external - (char *) pnotes));
10335 warn (_(" type: %lx, namesize: %08lx, descsize: %08lx\n"),
10336 inote.type, inote.namesz, inote.descsz);
10337 break;
10340 external = next;
10342 /* Verify that name is null terminated. It appears that at least
10343 one version of Linux (RedHat 6.0) generates corefiles that don't
10344 comply with the ELF spec by failing to include the null byte in
10345 namesz. */
10346 if (inote.namedata[inote.namesz] != '\0')
10348 temp = malloc (inote.namesz + 1);
10350 if (temp == NULL)
10352 error (_("Out of memory\n"));
10353 res = 0;
10354 break;
10357 strncpy (temp, inote.namedata, inote.namesz);
10358 temp[inote.namesz] = 0;
10360 /* warn (_("'%s' NOTE name not properly null terminated\n"), temp); */
10361 inote.namedata = temp;
10364 res &= process_note (& inote);
10366 if (temp != NULL)
10368 free (temp);
10369 temp = NULL;
10373 free (pnotes);
10375 return res;
10378 static int
10379 process_corefile_note_segments (FILE * file)
10381 Elf_Internal_Phdr * segment;
10382 unsigned int i;
10383 int res = 1;
10385 if (! get_program_headers (file))
10386 return 0;
10388 for (i = 0, segment = program_headers;
10389 i < elf_header.e_phnum;
10390 i++, segment++)
10392 if (segment->p_type == PT_NOTE)
10393 res &= process_corefile_note_segment (file,
10394 (bfd_vma) segment->p_offset,
10395 (bfd_vma) segment->p_filesz);
10398 return res;
10401 static int
10402 process_note_sections (FILE * file)
10404 Elf_Internal_Shdr * section;
10405 unsigned long i;
10406 int res = 1;
10408 for (i = 0, section = section_headers;
10409 i < elf_header.e_shnum;
10410 i++, section++)
10411 if (section->sh_type == SHT_NOTE)
10412 res &= process_corefile_note_segment (file,
10413 (bfd_vma) section->sh_offset,
10414 (bfd_vma) section->sh_size);
10416 return res;
10419 static int
10420 process_notes (FILE * file)
10422 /* If we have not been asked to display the notes then do nothing. */
10423 if (! do_notes)
10424 return 1;
10426 if (elf_header.e_type != ET_CORE)
10427 return process_note_sections (file);
10429 /* No program headers means no NOTE segment. */
10430 if (elf_header.e_phnum > 0)
10431 return process_corefile_note_segments (file);
10433 printf (_("No note segments present in the core file.\n"));
10434 return 1;
10437 static int
10438 process_arch_specific (FILE * file)
10440 if (! do_arch)
10441 return 1;
10443 switch (elf_header.e_machine)
10445 case EM_ARM:
10446 return process_arm_specific (file);
10447 case EM_MIPS:
10448 case EM_MIPS_RS3_LE:
10449 return process_mips_specific (file);
10450 break;
10451 case EM_PPC:
10452 return process_power_specific (file);
10453 break;
10454 default:
10455 break;
10457 return 1;
10460 static int
10461 get_file_header (FILE * file)
10463 /* Read in the identity array. */
10464 if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
10465 return 0;
10467 /* Determine how to read the rest of the header. */
10468 switch (elf_header.e_ident[EI_DATA])
10470 default: /* fall through */
10471 case ELFDATANONE: /* fall through */
10472 case ELFDATA2LSB:
10473 byte_get = byte_get_little_endian;
10474 byte_put = byte_put_little_endian;
10475 break;
10476 case ELFDATA2MSB:
10477 byte_get = byte_get_big_endian;
10478 byte_put = byte_put_big_endian;
10479 break;
10482 /* For now we only support 32 bit and 64 bit ELF files. */
10483 is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
10485 /* Read in the rest of the header. */
10486 if (is_32bit_elf)
10488 Elf32_External_Ehdr ehdr32;
10490 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
10491 return 0;
10493 elf_header.e_type = BYTE_GET (ehdr32.e_type);
10494 elf_header.e_machine = BYTE_GET (ehdr32.e_machine);
10495 elf_header.e_version = BYTE_GET (ehdr32.e_version);
10496 elf_header.e_entry = BYTE_GET (ehdr32.e_entry);
10497 elf_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
10498 elf_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
10499 elf_header.e_flags = BYTE_GET (ehdr32.e_flags);
10500 elf_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
10501 elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
10502 elf_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
10503 elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
10504 elf_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
10505 elf_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
10507 else
10509 Elf64_External_Ehdr ehdr64;
10511 /* If we have been compiled with sizeof (bfd_vma) == 4, then
10512 we will not be able to cope with the 64bit data found in
10513 64 ELF files. Detect this now and abort before we start
10514 overwriting things. */
10515 if (sizeof (bfd_vma) < 8)
10517 error (_("This instance of readelf has been built without support for a\n\
10518 64 bit data type and so it cannot read 64 bit ELF files.\n"));
10519 return 0;
10522 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
10523 return 0;
10525 elf_header.e_type = BYTE_GET (ehdr64.e_type);
10526 elf_header.e_machine = BYTE_GET (ehdr64.e_machine);
10527 elf_header.e_version = BYTE_GET (ehdr64.e_version);
10528 elf_header.e_entry = BYTE_GET (ehdr64.e_entry);
10529 elf_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
10530 elf_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
10531 elf_header.e_flags = BYTE_GET (ehdr64.e_flags);
10532 elf_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
10533 elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
10534 elf_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
10535 elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
10536 elf_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
10537 elf_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
10540 if (elf_header.e_shoff)
10542 /* There may be some extensions in the first section header. Don't
10543 bomb if we can't read it. */
10544 if (is_32bit_elf)
10545 get_32bit_section_headers (file, 1);
10546 else
10547 get_64bit_section_headers (file, 1);
10550 return 1;
10553 /* Process one ELF object file according to the command line options.
10554 This file may actually be stored in an archive. The file is
10555 positioned at the start of the ELF object. */
10557 static int
10558 process_object (char * file_name, FILE * file)
10560 unsigned int i;
10562 if (! get_file_header (file))
10564 error (_("%s: Failed to read file header\n"), file_name);
10565 return 1;
10568 /* Initialise per file variables. */
10569 for (i = ARRAY_SIZE (version_info); i--;)
10570 version_info[i] = 0;
10572 for (i = ARRAY_SIZE (dynamic_info); i--;)
10573 dynamic_info[i] = 0;
10575 /* Process the file. */
10576 if (show_name)
10577 printf (_("\nFile: %s\n"), file_name);
10579 /* Initialise the dump_sects array from the cmdline_dump_sects array.
10580 Note we do this even if cmdline_dump_sects is empty because we
10581 must make sure that the dump_sets array is zeroed out before each
10582 object file is processed. */
10583 if (num_dump_sects > num_cmdline_dump_sects)
10584 memset (dump_sects, 0, num_dump_sects * sizeof (* dump_sects));
10586 if (num_cmdline_dump_sects > 0)
10588 if (num_dump_sects == 0)
10589 /* A sneaky way of allocating the dump_sects array. */
10590 request_dump_bynumber (num_cmdline_dump_sects, 0);
10592 assert (num_dump_sects >= num_cmdline_dump_sects);
10593 memcpy (dump_sects, cmdline_dump_sects,
10594 num_cmdline_dump_sects * sizeof (* dump_sects));
10597 if (! process_file_header ())
10598 return 1;
10600 if (! process_section_headers (file))
10602 /* Without loaded section headers we cannot process lots of
10603 things. */
10604 do_unwind = do_version = do_dump = do_arch = 0;
10606 if (! do_using_dynamic)
10607 do_syms = do_reloc = 0;
10610 if (! process_section_groups (file))
10612 /* Without loaded section groups we cannot process unwind. */
10613 do_unwind = 0;
10616 if (process_program_headers (file))
10617 process_dynamic_section (file);
10619 process_relocs (file);
10621 process_unwind (file);
10623 process_symbol_table (file);
10625 process_syminfo (file);
10627 process_version_sections (file);
10629 process_section_contents (file);
10631 process_notes (file);
10633 process_gnu_liblist (file);
10635 process_arch_specific (file);
10637 if (program_headers)
10639 free (program_headers);
10640 program_headers = NULL;
10643 if (section_headers)
10645 free (section_headers);
10646 section_headers = NULL;
10649 if (string_table)
10651 free (string_table);
10652 string_table = NULL;
10653 string_table_length = 0;
10656 if (dynamic_strings)
10658 free (dynamic_strings);
10659 dynamic_strings = NULL;
10660 dynamic_strings_length = 0;
10663 if (dynamic_symbols)
10665 free (dynamic_symbols);
10666 dynamic_symbols = NULL;
10667 num_dynamic_syms = 0;
10670 if (dynamic_syminfo)
10672 free (dynamic_syminfo);
10673 dynamic_syminfo = NULL;
10676 if (section_headers_groups)
10678 free (section_headers_groups);
10679 section_headers_groups = NULL;
10682 if (section_groups)
10684 struct group_list * g;
10685 struct group_list * next;
10687 for (i = 0; i < group_count; i++)
10689 for (g = section_groups [i].root; g != NULL; g = next)
10691 next = g->next;
10692 free (g);
10696 free (section_groups);
10697 section_groups = NULL;
10700 free_debug_memory ();
10702 return 0;
10705 /* Return the path name for a proxy entry in a thin archive, adjusted relative
10706 to the path name of the thin archive itself if necessary. Always returns
10707 a pointer to malloc'ed memory. */
10709 static char *
10710 adjust_relative_path (char * file_name, char * name, int name_len)
10712 char * member_file_name;
10713 const char * base_name = lbasename (file_name);
10715 /* This is a proxy entry for a thin archive member.
10716 If the extended name table contains an absolute path
10717 name, or if the archive is in the current directory,
10718 use the path name as given. Otherwise, we need to
10719 find the member relative to the directory where the
10720 archive is located. */
10721 if (IS_ABSOLUTE_PATH (name) || base_name == file_name)
10723 member_file_name = malloc (name_len + 1);
10724 if (member_file_name == NULL)
10726 error (_("Out of memory\n"));
10727 return NULL;
10729 memcpy (member_file_name, name, name_len);
10730 member_file_name[name_len] = '\0';
10732 else
10734 /* Concatenate the path components of the archive file name
10735 to the relative path name from the extended name table. */
10736 size_t prefix_len = base_name - file_name;
10737 member_file_name = malloc (prefix_len + name_len + 1);
10738 if (member_file_name == NULL)
10740 error (_("Out of memory\n"));
10741 return NULL;
10743 memcpy (member_file_name, file_name, prefix_len);
10744 memcpy (member_file_name + prefix_len, name, name_len);
10745 member_file_name[prefix_len + name_len] = '\0';
10747 return member_file_name;
10750 /* Structure to hold information about an archive file. */
10752 struct archive_info
10754 char * file_name; /* Archive file name. */
10755 FILE * file; /* Open file descriptor. */
10756 unsigned long index_num; /* Number of symbols in table. */
10757 unsigned long * index_array; /* The array of member offsets. */
10758 char * sym_table; /* The symbol table. */
10759 unsigned long sym_size; /* Size of the symbol table. */
10760 char * longnames; /* The long file names table. */
10761 unsigned long longnames_size; /* Size of the long file names table. */
10762 unsigned long nested_member_origin; /* Origin in the nested archive of the current member. */
10763 unsigned long next_arhdr_offset; /* Offset of the next archive header. */
10764 bfd_boolean is_thin_archive; /* TRUE if this is a thin archive. */
10765 struct ar_hdr arhdr; /* Current archive header. */
10768 /* Read the symbol table and long-name table from an archive. */
10770 static int
10771 setup_archive (struct archive_info * arch, char * file_name, FILE * file,
10772 bfd_boolean is_thin_archive, bfd_boolean read_symbols)
10774 size_t got;
10775 unsigned long size;
10777 arch->file_name = strdup (file_name);
10778 arch->file = file;
10779 arch->index_num = 0;
10780 arch->index_array = NULL;
10781 arch->sym_table = NULL;
10782 arch->sym_size = 0;
10783 arch->longnames = NULL;
10784 arch->longnames_size = 0;
10785 arch->nested_member_origin = 0;
10786 arch->is_thin_archive = is_thin_archive;
10787 arch->next_arhdr_offset = SARMAG;
10789 /* Read the first archive member header. */
10790 if (fseek (file, SARMAG, SEEK_SET) != 0)
10792 error (_("%s: failed to seek to first archive header\n"), file_name);
10793 return 1;
10795 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
10796 if (got != sizeof arch->arhdr)
10798 if (got == 0)
10799 return 0;
10801 error (_("%s: failed to read archive header\n"), file_name);
10802 return 1;
10805 /* See if this is the archive symbol table. */
10806 if (const_strneq (arch->arhdr.ar_name, "/ ")
10807 || const_strneq (arch->arhdr.ar_name, "/SYM64/ "))
10809 size = strtoul (arch->arhdr.ar_size, NULL, 10);
10810 size = size + (size & 1);
10812 arch->next_arhdr_offset += sizeof arch->arhdr + size;
10814 if (read_symbols)
10816 unsigned long i;
10817 /* A buffer used to hold numbers read in from an archive index.
10818 These are always 4 bytes long and stored in big-endian format. */
10819 #define SIZEOF_AR_INDEX_NUMBERS 4
10820 unsigned char integer_buffer[SIZEOF_AR_INDEX_NUMBERS];
10821 unsigned char * index_buffer;
10823 /* Check the size of the archive index. */
10824 if (size < SIZEOF_AR_INDEX_NUMBERS)
10826 error (_("%s: the archive index is empty\n"), file_name);
10827 return 1;
10830 /* Read the numer of entries in the archive index. */
10831 got = fread (integer_buffer, 1, sizeof integer_buffer, file);
10832 if (got != sizeof (integer_buffer))
10834 error (_("%s: failed to read archive index\n"), file_name);
10835 return 1;
10837 arch->index_num = byte_get_big_endian (integer_buffer, sizeof integer_buffer);
10838 size -= SIZEOF_AR_INDEX_NUMBERS;
10840 /* Read in the archive index. */
10841 if (size < arch->index_num * SIZEOF_AR_INDEX_NUMBERS)
10843 error (_("%s: the archive index is supposed to have %ld entries, but the size in the header is too small\n"),
10844 file_name, arch->index_num);
10845 return 1;
10847 index_buffer = malloc (arch->index_num * SIZEOF_AR_INDEX_NUMBERS);
10848 if (index_buffer == NULL)
10850 error (_("Out of memory whilst trying to read archive symbol index\n"));
10851 return 1;
10853 got = fread (index_buffer, SIZEOF_AR_INDEX_NUMBERS, arch->index_num, file);
10854 if (got != arch->index_num)
10856 free (index_buffer);
10857 error (_("%s: failed to read archive index\n"), file_name);
10858 return 1;
10860 size -= arch->index_num * SIZEOF_AR_INDEX_NUMBERS;
10862 /* Convert the index numbers into the host's numeric format. */
10863 arch->index_array = malloc (arch->index_num * sizeof (* arch->index_array));
10864 if (arch->index_array == NULL)
10866 free (index_buffer);
10867 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
10868 return 1;
10871 for (i = 0; i < arch->index_num; i++)
10872 arch->index_array[i] = byte_get_big_endian ((unsigned char *) (index_buffer + (i * SIZEOF_AR_INDEX_NUMBERS)),
10873 SIZEOF_AR_INDEX_NUMBERS);
10874 free (index_buffer);
10876 /* The remaining space in the header is taken up by the symbol table. */
10877 if (size < 1)
10879 error (_("%s: the archive has an index but no symbols\n"), file_name);
10880 return 1;
10882 arch->sym_table = malloc (size);
10883 arch->sym_size = size;
10884 if (arch->sym_table == NULL)
10886 error (_("Out of memory whilst trying to read archive index symbol table\n"));
10887 return 1;
10889 got = fread (arch->sym_table, 1, size, file);
10890 if (got != size)
10892 error (_("%s: failed to read archive index symbol table\n"), file_name);
10893 return 1;
10896 else
10898 if (fseek (file, size, SEEK_CUR) != 0)
10900 error (_("%s: failed to skip archive symbol table\n"), file_name);
10901 return 1;
10905 /* Read the next archive header. */
10906 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
10907 if (got != sizeof arch->arhdr)
10909 if (got == 0)
10910 return 0;
10911 error (_("%s: failed to read archive header following archive index\n"), file_name);
10912 return 1;
10915 else if (read_symbols)
10916 printf (_("%s has no archive index\n"), file_name);
10918 if (const_strneq (arch->arhdr.ar_name, "// "))
10920 /* This is the archive string table holding long member names. */
10921 arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
10922 arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
10924 arch->longnames = malloc (arch->longnames_size);
10925 if (arch->longnames == NULL)
10927 error (_("Out of memory reading long symbol names in archive\n"));
10928 return 1;
10931 if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
10933 free (arch->longnames);
10934 arch->longnames = NULL;
10935 error (_("%s: failed to read long symbol name string table\n"), file_name);
10936 return 1;
10939 if ((arch->longnames_size & 1) != 0)
10940 getc (file);
10943 return 0;
10946 /* Release the memory used for the archive information. */
10948 static void
10949 release_archive (struct archive_info * arch)
10951 if (arch->file_name != NULL)
10952 free (arch->file_name);
10953 if (arch->index_array != NULL)
10954 free (arch->index_array);
10955 if (arch->sym_table != NULL)
10956 free (arch->sym_table);
10957 if (arch->longnames != NULL)
10958 free (arch->longnames);
10961 /* Open and setup a nested archive, if not already open. */
10963 static int
10964 setup_nested_archive (struct archive_info * nested_arch, char * member_file_name)
10966 FILE * member_file;
10968 /* Have we already setup this archive? */
10969 if (nested_arch->file_name != NULL
10970 && streq (nested_arch->file_name, member_file_name))
10971 return 0;
10973 /* Close previous file and discard cached information. */
10974 if (nested_arch->file != NULL)
10975 fclose (nested_arch->file);
10976 release_archive (nested_arch);
10978 member_file = fopen (member_file_name, "rb");
10979 if (member_file == NULL)
10980 return 1;
10981 return setup_archive (nested_arch, member_file_name, member_file, FALSE, FALSE);
10984 static char *
10985 get_archive_member_name_at (struct archive_info * arch,
10986 unsigned long offset,
10987 struct archive_info * nested_arch);
10989 /* Get the name of an archive member from the current archive header.
10990 For simple names, this will modify the ar_name field of the current
10991 archive header. For long names, it will return a pointer to the
10992 longnames table. For nested archives, it will open the nested archive
10993 and get the name recursively. NESTED_ARCH is a single-entry cache so
10994 we don't keep rereading the same information from a nested archive. */
10996 static char *
10997 get_archive_member_name (struct archive_info * arch,
10998 struct archive_info * nested_arch)
11000 unsigned long j, k;
11002 if (arch->arhdr.ar_name[0] == '/')
11004 /* We have a long name. */
11005 char * endp;
11006 char * member_file_name;
11007 char * member_name;
11009 arch->nested_member_origin = 0;
11010 k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
11011 if (arch->is_thin_archive && endp != NULL && * endp == ':')
11012 arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
11014 while ((j < arch->longnames_size)
11015 && (arch->longnames[j] != '\n')
11016 && (arch->longnames[j] != '\0'))
11017 j++;
11018 if (arch->longnames[j-1] == '/')
11019 j--;
11020 arch->longnames[j] = '\0';
11022 if (!arch->is_thin_archive || arch->nested_member_origin == 0)
11023 return arch->longnames + k;
11025 /* This is a proxy for a member of a nested archive.
11026 Find the name of the member in that archive. */
11027 member_file_name = adjust_relative_path (arch->file_name, arch->longnames + k, j - k);
11028 if (member_file_name != NULL
11029 && setup_nested_archive (nested_arch, member_file_name) == 0
11030 && (member_name = get_archive_member_name_at (nested_arch, arch->nested_member_origin, NULL)) != NULL)
11032 free (member_file_name);
11033 return member_name;
11035 free (member_file_name);
11037 /* Last resort: just return the name of the nested archive. */
11038 return arch->longnames + k;
11041 /* We have a normal (short) name. */
11042 j = 0;
11043 while ((arch->arhdr.ar_name[j] != '/') && (j < 16))
11044 j++;
11045 arch->arhdr.ar_name[j] = '\0';
11046 return arch->arhdr.ar_name;
11049 /* Get the name of an archive member at a given OFFSET within an archive ARCH. */
11051 static char *
11052 get_archive_member_name_at (struct archive_info * arch,
11053 unsigned long offset,
11054 struct archive_info * nested_arch)
11056 size_t got;
11058 if (fseek (arch->file, offset, SEEK_SET) != 0)
11060 error (_("%s: failed to seek to next file name\n"), arch->file_name);
11061 return NULL;
11063 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
11064 if (got != sizeof arch->arhdr)
11066 error (_("%s: failed to read archive header\n"), arch->file_name);
11067 return NULL;
11069 if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
11071 error (_("%s: did not find a valid archive header\n"), arch->file_name);
11072 return NULL;
11075 return get_archive_member_name (arch, nested_arch);
11078 /* Construct a string showing the name of the archive member, qualified
11079 with the name of the containing archive file. For thin archives, we
11080 use square brackets to denote the indirection. For nested archives,
11081 we show the qualified name of the external member inside the square
11082 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
11084 static char *
11085 make_qualified_name (struct archive_info * arch,
11086 struct archive_info * nested_arch,
11087 char * member_name)
11089 size_t len;
11090 char * name;
11092 len = strlen (arch->file_name) + strlen (member_name) + 3;
11093 if (arch->is_thin_archive && arch->nested_member_origin != 0)
11094 len += strlen (nested_arch->file_name) + 2;
11096 name = malloc (len);
11097 if (name == NULL)
11099 error (_("Out of memory\n"));
11100 return NULL;
11103 if (arch->is_thin_archive && arch->nested_member_origin != 0)
11104 snprintf (name, len, "%s[%s(%s)]", arch->file_name, nested_arch->file_name, member_name);
11105 else if (arch->is_thin_archive)
11106 snprintf (name, len, "%s[%s]", arch->file_name, member_name);
11107 else
11108 snprintf (name, len, "%s(%s)", arch->file_name, member_name);
11110 return name;
11113 /* Process an ELF archive.
11114 On entry the file is positioned just after the ARMAG string. */
11116 static int
11117 process_archive (char * file_name, FILE * file, bfd_boolean is_thin_archive)
11119 struct archive_info arch;
11120 struct archive_info nested_arch;
11121 size_t got;
11122 size_t file_name_size;
11123 int ret;
11125 show_name = 1;
11127 /* The ARCH structure is used to hold information about this archive. */
11128 arch.file_name = NULL;
11129 arch.file = NULL;
11130 arch.index_array = NULL;
11131 arch.sym_table = NULL;
11132 arch.longnames = NULL;
11134 /* The NESTED_ARCH structure is used as a single-item cache of information
11135 about a nested archive (when members of a thin archive reside within
11136 another regular archive file). */
11137 nested_arch.file_name = NULL;
11138 nested_arch.file = NULL;
11139 nested_arch.index_array = NULL;
11140 nested_arch.sym_table = NULL;
11141 nested_arch.longnames = NULL;
11143 if (setup_archive (&arch, file_name, file, is_thin_archive, do_archive_index) != 0)
11145 ret = 1;
11146 goto out;
11149 if (do_archive_index)
11151 if (arch.sym_table == NULL)
11152 error (_("%s: unable to dump the index as none was found\n"), file_name);
11153 else
11155 unsigned int i, l;
11156 unsigned long current_pos;
11158 printf (_("Index of archive %s: (%ld entries, 0x%lx bytes in the symbol table)\n"),
11159 file_name, arch.index_num, arch.sym_size);
11160 current_pos = ftell (file);
11162 for (i = l = 0; i < arch.index_num; i++)
11164 if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
11166 char * member_name;
11168 member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
11170 if (member_name != NULL)
11172 char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
11174 if (qualified_name != NULL)
11176 printf (_("Binary %s contains:\n"), qualified_name);
11177 free (qualified_name);
11182 if (l >= arch.sym_size)
11184 error (_("%s: end of the symbol table reached before the end of the index\n"),
11185 file_name);
11186 break;
11188 printf ("\t%s\n", arch.sym_table + l);
11189 l += strlen (arch.sym_table + l) + 1;
11192 if (l & 01)
11193 ++l;
11194 if (l < arch.sym_size)
11195 error (_("%s: symbols remain in the index symbol table, but without corresponding entries in the index table\n"),
11196 file_name);
11198 if (fseek (file, current_pos, SEEK_SET) != 0)
11200 error (_("%s: failed to seek back to start of object files in the archive\n"), file_name);
11201 ret = 1;
11202 goto out;
11206 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
11207 && !do_segments && !do_header && !do_dump && !do_version
11208 && !do_histogram && !do_debugging && !do_arch && !do_notes
11209 && !do_section_groups)
11211 ret = 0; /* Archive index only. */
11212 goto out;
11216 file_name_size = strlen (file_name);
11217 ret = 0;
11219 while (1)
11221 char * name;
11222 size_t namelen;
11223 char * qualified_name;
11225 /* Read the next archive header. */
11226 if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
11228 error (_("%s: failed to seek to next archive header\n"), file_name);
11229 return 1;
11231 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
11232 if (got != sizeof arch.arhdr)
11234 if (got == 0)
11235 break;
11236 error (_("%s: failed to read archive header\n"), file_name);
11237 ret = 1;
11238 break;
11240 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
11242 error (_("%s: did not find a valid archive header\n"), arch.file_name);
11243 ret = 1;
11244 break;
11247 arch.next_arhdr_offset += sizeof arch.arhdr;
11249 archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
11250 if (archive_file_size & 01)
11251 ++archive_file_size;
11253 name = get_archive_member_name (&arch, &nested_arch);
11254 if (name == NULL)
11256 error (_("%s: bad archive file name\n"), file_name);
11257 ret = 1;
11258 break;
11260 namelen = strlen (name);
11262 qualified_name = make_qualified_name (&arch, &nested_arch, name);
11263 if (qualified_name == NULL)
11265 error (_("%s: bad archive file name\n"), file_name);
11266 ret = 1;
11267 break;
11270 if (is_thin_archive && arch.nested_member_origin == 0)
11272 /* This is a proxy for an external member of a thin archive. */
11273 FILE * member_file;
11274 char * member_file_name = adjust_relative_path (file_name, name, namelen);
11275 if (member_file_name == NULL)
11277 ret = 1;
11278 break;
11281 member_file = fopen (member_file_name, "rb");
11282 if (member_file == NULL)
11284 error (_("Input file '%s' is not readable.\n"), member_file_name);
11285 free (member_file_name);
11286 ret = 1;
11287 break;
11290 archive_file_offset = arch.nested_member_origin;
11292 ret |= process_object (qualified_name, member_file);
11294 fclose (member_file);
11295 free (member_file_name);
11297 else if (is_thin_archive)
11299 /* This is a proxy for a member of a nested archive. */
11300 archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
11302 /* The nested archive file will have been opened and setup by
11303 get_archive_member_name. */
11304 if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
11306 error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
11307 ret = 1;
11308 break;
11311 ret |= process_object (qualified_name, nested_arch.file);
11313 else
11315 archive_file_offset = arch.next_arhdr_offset;
11316 arch.next_arhdr_offset += archive_file_size;
11318 ret |= process_object (qualified_name, file);
11321 free (qualified_name);
11324 out:
11325 if (nested_arch.file != NULL)
11326 fclose (nested_arch.file);
11327 release_archive (&nested_arch);
11328 release_archive (&arch);
11330 return ret;
11333 static int
11334 process_file (char * file_name)
11336 FILE * file;
11337 struct stat statbuf;
11338 char armag[SARMAG];
11339 int ret;
11341 if (stat (file_name, &statbuf) < 0)
11343 if (errno == ENOENT)
11344 error (_("'%s': No such file\n"), file_name);
11345 else
11346 error (_("Could not locate '%s'. System error message: %s\n"),
11347 file_name, strerror (errno));
11348 return 1;
11351 if (! S_ISREG (statbuf.st_mode))
11353 error (_("'%s' is not an ordinary file\n"), file_name);
11354 return 1;
11357 file = fopen (file_name, "rb");
11358 if (file == NULL)
11360 error (_("Input file '%s' is not readable.\n"), file_name);
11361 return 1;
11364 if (fread (armag, SARMAG, 1, file) != 1)
11366 error (_("%s: Failed to read file's magic number\n"), file_name);
11367 fclose (file);
11368 return 1;
11371 if (memcmp (armag, ARMAG, SARMAG) == 0)
11372 ret = process_archive (file_name, file, FALSE);
11373 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
11374 ret = process_archive (file_name, file, TRUE);
11375 else
11377 if (do_archive_index)
11378 error (_("File %s is not an archive so its index cannot be displayed.\n"),
11379 file_name);
11381 rewind (file);
11382 archive_file_size = archive_file_offset = 0;
11383 ret = process_object (file_name, file);
11386 fclose (file);
11388 return ret;
11391 #ifdef SUPPORT_DISASSEMBLY
11392 /* Needed by the i386 disassembler. For extra credit, someone could
11393 fix this so that we insert symbolic addresses here, esp for GOT/PLT
11394 symbols. */
11396 void
11397 print_address (unsigned int addr, FILE * outfile)
11399 fprintf (outfile,"0x%8.8x", addr);
11402 /* Needed by the i386 disassembler. */
11403 void
11404 db_task_printsym (unsigned int addr)
11406 print_address (addr, stderr);
11408 #endif
11411 main (int argc, char ** argv)
11413 int err;
11415 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
11416 setlocale (LC_MESSAGES, "");
11417 #endif
11418 #if defined (HAVE_SETLOCALE)
11419 setlocale (LC_CTYPE, "");
11420 #endif
11421 bindtextdomain (PACKAGE, LOCALEDIR);
11422 textdomain (PACKAGE);
11424 expandargv (&argc, &argv);
11426 parse_args (argc, argv);
11428 if (num_dump_sects > 0)
11430 /* Make a copy of the dump_sects array. */
11431 cmdline_dump_sects = malloc (num_dump_sects * sizeof (* dump_sects));
11432 if (cmdline_dump_sects == NULL)
11433 error (_("Out of memory allocating dump request table.\n"));
11434 else
11436 memcpy (cmdline_dump_sects, dump_sects,
11437 num_dump_sects * sizeof (* dump_sects));
11438 num_cmdline_dump_sects = num_dump_sects;
11442 if (optind < (argc - 1))
11443 show_name = 1;
11445 err = 0;
11446 while (optind < argc)
11447 err |= process_file (argv[optind++]);
11449 if (dump_sects != NULL)
11450 free (dump_sects);
11451 if (cmdline_dump_sects != NULL)
11452 free (cmdline_dump_sects);
11454 return err;