* readelf.c (process_version_sections, process_symbol_table):
[binutils.git] / binutils / readelf.c
blob64630f5f7658a7942b0a9ea70ef582725823ce72
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/microblaze.h"
123 #include "elf/mips.h"
124 #include "elf/mmix.h"
125 #include "elf/mn10200.h"
126 #include "elf/mn10300.h"
127 #include "elf/mt.h"
128 #include "elf/msp430.h"
129 #include "elf/or32.h"
130 #include "elf/pj.h"
131 #include "elf/ppc.h"
132 #include "elf/ppc64.h"
133 #include "elf/s390.h"
134 #include "elf/score.h"
135 #include "elf/sh.h"
136 #include "elf/sparc.h"
137 #include "elf/spu.h"
138 #include "elf/v850.h"
139 #include "elf/vax.h"
140 #include "elf/x86-64.h"
141 #include "elf/xstormy16.h"
142 #include "elf/xtensa.h"
144 #include "aout/ar.h"
146 #include "getopt.h"
147 #include "libiberty.h"
148 #include "safe-ctype.h"
149 #include "filenames.h"
151 char * program_name = "readelf";
152 int do_wide;
153 static long archive_file_offset;
154 static unsigned long archive_file_size;
155 static unsigned long dynamic_addr;
156 static bfd_size_type dynamic_size;
157 static unsigned int dynamic_nent;
158 static char * dynamic_strings;
159 static unsigned long dynamic_strings_length;
160 static char * string_table;
161 static unsigned long string_table_length;
162 static unsigned long num_dynamic_syms;
163 static Elf_Internal_Sym * dynamic_symbols;
164 static Elf_Internal_Syminfo * dynamic_syminfo;
165 static unsigned long dynamic_syminfo_offset;
166 static unsigned int dynamic_syminfo_nent;
167 static char program_interpreter[PATH_MAX];
168 static bfd_vma dynamic_info[DT_JMPREL + 1];
169 static bfd_vma dynamic_info_DT_GNU_HASH;
170 static bfd_vma version_info[16];
171 static Elf_Internal_Ehdr elf_header;
172 static Elf_Internal_Shdr * section_headers;
173 static Elf_Internal_Phdr * program_headers;
174 static Elf_Internal_Dyn * dynamic_section;
175 static Elf_Internal_Shdr * symtab_shndx_hdr;
176 static int show_name;
177 static int do_dynamic;
178 static int do_syms;
179 static int do_reloc;
180 static int do_sections;
181 static int do_section_groups;
182 static int do_section_details;
183 static int do_segments;
184 static int do_unwind;
185 static int do_using_dynamic;
186 static int do_header;
187 static int do_dump;
188 static int do_version;
189 static int do_histogram;
190 static int do_debugging;
191 static int do_arch;
192 static int do_notes;
193 static int do_archive_index;
194 static int is_32bit_elf;
196 struct group_list
198 struct group_list * next;
199 unsigned int section_index;
202 struct group
204 struct group_list * root;
205 unsigned int group_index;
208 static size_t group_count;
209 static struct group * section_groups;
210 static struct group ** section_headers_groups;
213 /* Flag bits indicating particular types of dump. */
214 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
215 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
216 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
217 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
218 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
220 typedef unsigned char dump_type;
222 /* A linked list of the section names for which dumps were requested. */
223 struct dump_list_entry
225 char * name;
226 dump_type type;
227 struct dump_list_entry * next;
229 static struct dump_list_entry * dump_sects_byname;
231 /* A dynamic array of flags indicating for which sections a dump
232 has been requested via command line switches. */
233 static dump_type * cmdline_dump_sects = NULL;
234 static unsigned int num_cmdline_dump_sects = 0;
236 /* A dynamic array of flags indicating for which sections a dump of
237 some kind has been requested. It is reset on a per-object file
238 basis and then initialised from the cmdline_dump_sects array,
239 the results of interpreting the -w switch, and the
240 dump_sects_byname list. */
241 static dump_type * dump_sects = NULL;
242 static unsigned int num_dump_sects = 0;
245 /* How to print a vma value. */
246 typedef enum print_mode
248 HEX,
249 DEC,
250 DEC_5,
251 UNSIGNED,
252 PREFIX_HEX,
253 FULL_HEX,
254 LONG_HEX
256 print_mode;
258 static void (* byte_put) (unsigned char *, bfd_vma, int);
260 #define UNKNOWN -1
262 #define SECTION_NAME(X) \
263 ((X) == NULL ? "<none>" \
264 : string_table == NULL ? "<no-name>" \
265 : ((X)->sh_name >= string_table_length ? "<corrupt>" \
266 : string_table + (X)->sh_name))
268 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
270 #define BYTE_GET(field) byte_get (field, sizeof (field))
272 #define GET_ELF_SYMBOLS(file, section) \
273 (is_32bit_elf ? get_32bit_elf_symbols (file, section) \
274 : get_64bit_elf_symbols (file, section))
276 #define VALID_DYNAMIC_NAME(offset) ((dynamic_strings != NULL) && (offset < dynamic_strings_length))
277 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
278 already been called and verified that the string exists. */
279 #define GET_DYNAMIC_NAME(offset) (dynamic_strings + offset)
281 /* This is just a bit of syntatic sugar. */
282 #define streq(a,b) (strcmp ((a), (b)) == 0)
283 #define strneq(a,b,n) (strncmp ((a), (b), (n)) == 0)
284 #define const_strneq(a,b) (strncmp ((a), (b), sizeof (b) - 1) == 0)
286 static void *
287 get_data (void * var, FILE * file, long offset, size_t size, size_t nmemb,
288 const char * reason)
290 void * mvar;
292 if (size == 0 || nmemb == 0)
293 return NULL;
295 if (fseek (file, archive_file_offset + offset, SEEK_SET))
297 error (_("Unable to seek to 0x%lx for %s\n"),
298 (unsigned long) archive_file_offset + offset, reason);
299 return NULL;
302 mvar = var;
303 if (mvar == NULL)
305 /* Check for overflow. */
306 if (nmemb < (~(size_t) 0 - 1) / size)
307 /* + 1 so that we can '\0' terminate invalid string table sections. */
308 mvar = malloc (size * nmemb + 1);
310 if (mvar == NULL)
312 error (_("Out of memory allocating 0x%lx bytes for %s\n"),
313 (unsigned long)(size * nmemb), reason);
314 return NULL;
317 ((char *) mvar)[size * nmemb] = '\0';
320 if (fread (mvar, size, nmemb, file) != nmemb)
322 error (_("Unable to read in 0x%lx bytes of %s\n"),
323 (unsigned long)(size * nmemb), reason);
324 if (mvar != var)
325 free (mvar);
326 return NULL;
329 return mvar;
332 static void
333 byte_put_little_endian (unsigned char * field, bfd_vma value, int size)
335 switch (size)
337 case 8:
338 field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
339 field[6] = ((value >> 24) >> 24) & 0xff;
340 field[5] = ((value >> 24) >> 16) & 0xff;
341 field[4] = ((value >> 24) >> 8) & 0xff;
342 /* Fall through. */
343 case 4:
344 field[3] = (value >> 24) & 0xff;
345 /* Fall through. */
346 case 3:
347 field[2] = (value >> 16) & 0xff;
348 /* Fall through. */
349 case 2:
350 field[1] = (value >> 8) & 0xff;
351 /* Fall through. */
352 case 1:
353 field[0] = value & 0xff;
354 break;
356 default:
357 error (_("Unhandled data length: %d\n"), size);
358 abort ();
362 /* Print a VMA value. */
364 static int
365 print_vma (bfd_vma vma, print_mode mode)
367 int nc = 0;
369 switch (mode)
371 case FULL_HEX:
372 nc = printf ("0x");
373 /* Drop through. */
375 case LONG_HEX:
376 #ifdef BFD64
377 if (is_32bit_elf)
378 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
379 #endif
380 printf_vma (vma);
381 return nc + 16;
383 case DEC_5:
384 if (vma <= 99999)
385 return printf ("%5" BFD_VMA_FMT "d", vma);
386 /* Drop through. */
388 case PREFIX_HEX:
389 nc = printf ("0x");
390 /* Drop through. */
392 case HEX:
393 return nc + printf ("%" BFD_VMA_FMT "x", vma);
395 case DEC:
396 return printf ("%" BFD_VMA_FMT "d", vma);
398 case UNSIGNED:
399 return printf ("%" BFD_VMA_FMT "u", vma);
401 return 0;
404 /* Display a symbol on stdout. Handles the display of non-printing characters.
406 If DO_WIDE is not true then format the symbol to be at most WIDTH characters,
407 truncating as necessary. If WIDTH is negative then format the string to be
408 exactly - WIDTH characters, truncating or padding as necessary.
410 Returns the number of emitted characters. */
412 static unsigned int
413 print_symbol (int width, const char * symbol)
415 const char * c;
416 bfd_boolean extra_padding = FALSE;
417 unsigned int num_printed = 0;
419 if (do_wide)
421 /* Set the width to a very large value. This simplifies the code below. */
422 width = INT_MAX;
424 else if (width < 0)
426 /* Keep the width positive. This also helps. */
427 width = - width;
428 extra_padding = TRUE;
431 while (width)
433 int len;
435 c = symbol;
437 /* Look for non-printing symbols inside the symbol's name.
438 This test is triggered in particular by the names generated
439 by the assembler for local labels. */
440 while (ISPRINT (* c))
441 c++;
443 len = c - symbol;
445 if (len)
447 if (len > width)
448 len = width;
450 printf ("%.*s", len, symbol);
452 width -= len;
453 num_printed += len;
456 if (* c == 0 || width == 0)
457 break;
459 /* Now display the non-printing character, if
460 there is room left in which to dipslay it. */
461 if (*c < 32)
463 if (width < 2)
464 break;
466 printf ("^%c", *c + 0x40);
468 width -= 2;
469 num_printed += 2;
471 else
473 if (width < 6)
474 break;
476 printf ("<0x%.2x>", *c);
478 width -= 6;
479 num_printed += 6;
482 symbol = c + 1;
485 if (extra_padding && width > 0)
487 /* Fill in the remaining spaces. */
488 printf ("%-*s", width, " ");
489 num_printed += 2;
492 return num_printed;
495 static void
496 byte_put_big_endian (unsigned char * field, bfd_vma value, int size)
498 switch (size)
500 case 8:
501 field[7] = value & 0xff;
502 field[6] = (value >> 8) & 0xff;
503 field[5] = (value >> 16) & 0xff;
504 field[4] = (value >> 24) & 0xff;
505 value >>= 16;
506 value >>= 16;
507 /* Fall through. */
508 case 4:
509 field[3] = value & 0xff;
510 value >>= 8;
511 /* Fall through. */
512 case 3:
513 field[2] = value & 0xff;
514 value >>= 8;
515 /* Fall through. */
516 case 2:
517 field[1] = value & 0xff;
518 value >>= 8;
519 /* Fall through. */
520 case 1:
521 field[0] = value & 0xff;
522 break;
524 default:
525 error (_("Unhandled data length: %d\n"), size);
526 abort ();
530 /* Return a pointer to section NAME, or NULL if no such section exists. */
532 static Elf_Internal_Shdr *
533 find_section (const char * name)
535 unsigned int i;
537 for (i = 0; i < elf_header.e_shnum; i++)
538 if (streq (SECTION_NAME (section_headers + i), name))
539 return section_headers + i;
541 return NULL;
544 /* Guess the relocation size commonly used by the specific machines. */
546 static int
547 guess_is_rela (unsigned int e_machine)
549 switch (e_machine)
551 /* Targets that use REL relocations. */
552 case EM_386:
553 case EM_486:
554 case EM_960:
555 case EM_ARM:
556 case EM_D10V:
557 case EM_CYGNUS_D10V:
558 case EM_DLX:
559 case EM_MIPS:
560 case EM_MIPS_RS3_LE:
561 case EM_CYGNUS_M32R:
562 case EM_OPENRISC:
563 case EM_OR32:
564 case EM_SCORE:
565 return FALSE;
567 /* Targets that use RELA relocations. */
568 case EM_68K:
569 case EM_860:
570 case EM_ALPHA:
571 case EM_ALTERA_NIOS2:
572 case EM_AVR:
573 case EM_AVR_OLD:
574 case EM_BLACKFIN:
575 case EM_CR16:
576 case EM_CR16_OLD:
577 case EM_CRIS:
578 case EM_CRX:
579 case EM_D30V:
580 case EM_CYGNUS_D30V:
581 case EM_FR30:
582 case EM_CYGNUS_FR30:
583 case EM_CYGNUS_FRV:
584 case EM_H8S:
585 case EM_H8_300:
586 case EM_H8_300H:
587 case EM_IA_64:
588 case EM_IP2K:
589 case EM_IP2K_OLD:
590 case EM_IQ2000:
591 case EM_LATTICEMICO32:
592 case EM_M32C_OLD:
593 case EM_M32C:
594 case EM_M32R:
595 case EM_MCORE:
596 case EM_CYGNUS_MEP:
597 case EM_MMIX:
598 case EM_MN10200:
599 case EM_CYGNUS_MN10200:
600 case EM_MN10300:
601 case EM_CYGNUS_MN10300:
602 case EM_MSP430:
603 case EM_MSP430_OLD:
604 case EM_MT:
605 case EM_NIOS32:
606 case EM_PPC64:
607 case EM_PPC:
608 case EM_S390:
609 case EM_S390_OLD:
610 case EM_SH:
611 case EM_SPARC:
612 case EM_SPARC32PLUS:
613 case EM_SPARCV9:
614 case EM_SPU:
615 case EM_V850:
616 case EM_CYGNUS_V850:
617 case EM_VAX:
618 case EM_X86_64:
619 case EM_L1OM:
620 case EM_XSTORMY16:
621 case EM_XTENSA:
622 case EM_XTENSA_OLD:
623 case EM_MICROBLAZE:
624 case EM_MICROBLAZE_OLD:
625 return TRUE;
627 case EM_68HC05:
628 case EM_68HC08:
629 case EM_68HC11:
630 case EM_68HC16:
631 case EM_FX66:
632 case EM_ME16:
633 case EM_MMA:
634 case EM_NCPU:
635 case EM_NDR1:
636 case EM_PCP:
637 case EM_ST100:
638 case EM_ST19:
639 case EM_ST7:
640 case EM_ST9PLUS:
641 case EM_STARCORE:
642 case EM_SVX:
643 case EM_TINYJ:
644 default:
645 warn (_("Don't know about relocations on this machine architecture\n"));
646 return FALSE;
650 static int
651 slurp_rela_relocs (FILE * file,
652 unsigned long rel_offset,
653 unsigned long rel_size,
654 Elf_Internal_Rela ** relasp,
655 unsigned long * nrelasp)
657 Elf_Internal_Rela * relas;
658 unsigned long nrelas;
659 unsigned int i;
661 if (is_32bit_elf)
663 Elf32_External_Rela * erelas;
665 erelas = (Elf32_External_Rela *) get_data (NULL, file, rel_offset, 1,
666 rel_size, _("relocs"));
667 if (!erelas)
668 return 0;
670 nrelas = rel_size / sizeof (Elf32_External_Rela);
672 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
673 sizeof (Elf_Internal_Rela));
675 if (relas == NULL)
677 free (erelas);
678 error (_("out of memory parsing relocs\n"));
679 return 0;
682 for (i = 0; i < nrelas; i++)
684 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
685 relas[i].r_info = BYTE_GET (erelas[i].r_info);
686 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
689 free (erelas);
691 else
693 Elf64_External_Rela * erelas;
695 erelas = (Elf64_External_Rela *) get_data (NULL, file, rel_offset, 1,
696 rel_size, _("relocs"));
697 if (!erelas)
698 return 0;
700 nrelas = rel_size / sizeof (Elf64_External_Rela);
702 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
703 sizeof (Elf_Internal_Rela));
705 if (relas == NULL)
707 free (erelas);
708 error (_("out of memory parsing relocs\n"));
709 return 0;
712 for (i = 0; i < nrelas; i++)
714 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
715 relas[i].r_info = BYTE_GET (erelas[i].r_info);
716 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
718 /* The #ifdef BFD64 below is to prevent a compile time
719 warning. We know that if we do not have a 64 bit data
720 type that we will never execute this code anyway. */
721 #ifdef BFD64
722 if (elf_header.e_machine == EM_MIPS
723 && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
725 /* In little-endian objects, r_info isn't really a
726 64-bit little-endian value: it has a 32-bit
727 little-endian symbol index followed by four
728 individual byte fields. Reorder INFO
729 accordingly. */
730 bfd_vma info = relas[i].r_info;
731 info = (((info & 0xffffffff) << 32)
732 | ((info >> 56) & 0xff)
733 | ((info >> 40) & 0xff00)
734 | ((info >> 24) & 0xff0000)
735 | ((info >> 8) & 0xff000000));
736 relas[i].r_info = info;
738 #endif /* BFD64 */
741 free (erelas);
743 *relasp = relas;
744 *nrelasp = nrelas;
745 return 1;
748 static int
749 slurp_rel_relocs (FILE * file,
750 unsigned long rel_offset,
751 unsigned long rel_size,
752 Elf_Internal_Rela ** relsp,
753 unsigned long * nrelsp)
755 Elf_Internal_Rela * rels;
756 unsigned long nrels;
757 unsigned int i;
759 if (is_32bit_elf)
761 Elf32_External_Rel * erels;
763 erels = (Elf32_External_Rel *) get_data (NULL, file, rel_offset, 1,
764 rel_size, _("relocs"));
765 if (!erels)
766 return 0;
768 nrels = rel_size / sizeof (Elf32_External_Rel);
770 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
772 if (rels == NULL)
774 free (erels);
775 error (_("out of memory parsing relocs\n"));
776 return 0;
779 for (i = 0; i < nrels; i++)
781 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
782 rels[i].r_info = BYTE_GET (erels[i].r_info);
783 rels[i].r_addend = 0;
786 free (erels);
788 else
790 Elf64_External_Rel * erels;
792 erels = (Elf64_External_Rel *) get_data (NULL, file, rel_offset, 1,
793 rel_size, _("relocs"));
794 if (!erels)
795 return 0;
797 nrels = rel_size / sizeof (Elf64_External_Rel);
799 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
801 if (rels == NULL)
803 free (erels);
804 error (_("out of memory parsing relocs\n"));
805 return 0;
808 for (i = 0; i < nrels; i++)
810 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
811 rels[i].r_info = BYTE_GET (erels[i].r_info);
812 rels[i].r_addend = 0;
814 /* The #ifdef BFD64 below is to prevent a compile time
815 warning. We know that if we do not have a 64 bit data
816 type that we will never execute this code anyway. */
817 #ifdef BFD64
818 if (elf_header.e_machine == EM_MIPS
819 && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
821 /* In little-endian objects, r_info isn't really a
822 64-bit little-endian value: it has a 32-bit
823 little-endian symbol index followed by four
824 individual byte fields. Reorder INFO
825 accordingly. */
826 bfd_vma info = rels[i].r_info;
827 info = (((info & 0xffffffff) << 32)
828 | ((info >> 56) & 0xff)
829 | ((info >> 40) & 0xff00)
830 | ((info >> 24) & 0xff0000)
831 | ((info >> 8) & 0xff000000));
832 rels[i].r_info = info;
834 #endif /* BFD64 */
837 free (erels);
839 *relsp = rels;
840 *nrelsp = nrels;
841 return 1;
844 /* Returns the reloc type extracted from the reloc info field. */
846 static unsigned int
847 get_reloc_type (bfd_vma reloc_info)
849 if (is_32bit_elf)
850 return ELF32_R_TYPE (reloc_info);
852 switch (elf_header.e_machine)
854 case EM_MIPS:
855 /* Note: We assume that reloc_info has already been adjusted for us. */
856 return ELF64_MIPS_R_TYPE (reloc_info);
858 case EM_SPARCV9:
859 return ELF64_R_TYPE_ID (reloc_info);
861 default:
862 return ELF64_R_TYPE (reloc_info);
866 /* Return the symbol index extracted from the reloc info field. */
868 static bfd_vma
869 get_reloc_symindex (bfd_vma reloc_info)
871 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
874 /* Display the contents of the relocation data found at the specified
875 offset. */
877 static void
878 dump_relocations (FILE * file,
879 unsigned long rel_offset,
880 unsigned long rel_size,
881 Elf_Internal_Sym * symtab,
882 unsigned long nsyms,
883 char * strtab,
884 unsigned long strtablen,
885 int is_rela)
887 unsigned int i;
888 Elf_Internal_Rela * rels;
890 if (is_rela == UNKNOWN)
891 is_rela = guess_is_rela (elf_header.e_machine);
893 if (is_rela)
895 if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
896 return;
898 else
900 if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
901 return;
904 if (is_32bit_elf)
906 if (is_rela)
908 if (do_wide)
909 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
910 else
911 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
913 else
915 if (do_wide)
916 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
917 else
918 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
921 else
923 if (is_rela)
925 if (do_wide)
926 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
927 else
928 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
930 else
932 if (do_wide)
933 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
934 else
935 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
939 for (i = 0; i < rel_size; i++)
941 const char * rtype;
942 bfd_vma offset;
943 bfd_vma info;
944 bfd_vma symtab_index;
945 bfd_vma type;
947 offset = rels[i].r_offset;
948 info = rels[i].r_info;
950 type = get_reloc_type (info);
951 symtab_index = get_reloc_symindex (info);
953 if (is_32bit_elf)
955 printf ("%8.8lx %8.8lx ",
956 (unsigned long) offset & 0xffffffff,
957 (unsigned long) info & 0xffffffff);
959 else
961 #if BFD_HOST_64BIT_LONG
962 printf (do_wide
963 ? "%16.16lx %16.16lx "
964 : "%12.12lx %12.12lx ",
965 offset, info);
966 #elif BFD_HOST_64BIT_LONG_LONG
967 #ifndef __MSVCRT__
968 printf (do_wide
969 ? "%16.16llx %16.16llx "
970 : "%12.12llx %12.12llx ",
971 offset, info);
972 #else
973 printf (do_wide
974 ? "%16.16I64x %16.16I64x "
975 : "%12.12I64x %12.12I64x ",
976 offset, info);
977 #endif
978 #else
979 printf (do_wide
980 ? "%8.8lx%8.8lx %8.8lx%8.8lx "
981 : "%4.4lx%8.8lx %4.4lx%8.8lx ",
982 _bfd_int64_high (offset),
983 _bfd_int64_low (offset),
984 _bfd_int64_high (info),
985 _bfd_int64_low (info));
986 #endif
989 switch (elf_header.e_machine)
991 default:
992 rtype = NULL;
993 break;
995 case EM_M32R:
996 case EM_CYGNUS_M32R:
997 rtype = elf_m32r_reloc_type (type);
998 break;
1000 case EM_386:
1001 case EM_486:
1002 rtype = elf_i386_reloc_type (type);
1003 break;
1005 case EM_68HC11:
1006 case EM_68HC12:
1007 rtype = elf_m68hc11_reloc_type (type);
1008 break;
1010 case EM_68K:
1011 rtype = elf_m68k_reloc_type (type);
1012 break;
1014 case EM_960:
1015 rtype = elf_i960_reloc_type (type);
1016 break;
1018 case EM_AVR:
1019 case EM_AVR_OLD:
1020 rtype = elf_avr_reloc_type (type);
1021 break;
1023 case EM_OLD_SPARCV9:
1024 case EM_SPARC32PLUS:
1025 case EM_SPARCV9:
1026 case EM_SPARC:
1027 rtype = elf_sparc_reloc_type (type);
1028 break;
1030 case EM_SPU:
1031 rtype = elf_spu_reloc_type (type);
1032 break;
1034 case EM_V850:
1035 case EM_CYGNUS_V850:
1036 rtype = v850_reloc_type (type);
1037 break;
1039 case EM_D10V:
1040 case EM_CYGNUS_D10V:
1041 rtype = elf_d10v_reloc_type (type);
1042 break;
1044 case EM_D30V:
1045 case EM_CYGNUS_D30V:
1046 rtype = elf_d30v_reloc_type (type);
1047 break;
1049 case EM_DLX:
1050 rtype = elf_dlx_reloc_type (type);
1051 break;
1053 case EM_SH:
1054 rtype = elf_sh_reloc_type (type);
1055 break;
1057 case EM_MN10300:
1058 case EM_CYGNUS_MN10300:
1059 rtype = elf_mn10300_reloc_type (type);
1060 break;
1062 case EM_MN10200:
1063 case EM_CYGNUS_MN10200:
1064 rtype = elf_mn10200_reloc_type (type);
1065 break;
1067 case EM_FR30:
1068 case EM_CYGNUS_FR30:
1069 rtype = elf_fr30_reloc_type (type);
1070 break;
1072 case EM_CYGNUS_FRV:
1073 rtype = elf_frv_reloc_type (type);
1074 break;
1076 case EM_MCORE:
1077 rtype = elf_mcore_reloc_type (type);
1078 break;
1080 case EM_MMIX:
1081 rtype = elf_mmix_reloc_type (type);
1082 break;
1084 case EM_MSP430:
1085 case EM_MSP430_OLD:
1086 rtype = elf_msp430_reloc_type (type);
1087 break;
1089 case EM_PPC:
1090 rtype = elf_ppc_reloc_type (type);
1091 break;
1093 case EM_PPC64:
1094 rtype = elf_ppc64_reloc_type (type);
1095 break;
1097 case EM_MIPS:
1098 case EM_MIPS_RS3_LE:
1099 rtype = elf_mips_reloc_type (type);
1100 break;
1102 case EM_ALPHA:
1103 rtype = elf_alpha_reloc_type (type);
1104 break;
1106 case EM_ARM:
1107 rtype = elf_arm_reloc_type (type);
1108 break;
1110 case EM_ARC:
1111 rtype = elf_arc_reloc_type (type);
1112 break;
1114 case EM_PARISC:
1115 rtype = elf_hppa_reloc_type (type);
1116 break;
1118 case EM_H8_300:
1119 case EM_H8_300H:
1120 case EM_H8S:
1121 rtype = elf_h8_reloc_type (type);
1122 break;
1124 case EM_OPENRISC:
1125 case EM_OR32:
1126 rtype = elf_or32_reloc_type (type);
1127 break;
1129 case EM_PJ:
1130 case EM_PJ_OLD:
1131 rtype = elf_pj_reloc_type (type);
1132 break;
1133 case EM_IA_64:
1134 rtype = elf_ia64_reloc_type (type);
1135 break;
1137 case EM_CRIS:
1138 rtype = elf_cris_reloc_type (type);
1139 break;
1141 case EM_860:
1142 rtype = elf_i860_reloc_type (type);
1143 break;
1145 case EM_X86_64:
1146 case EM_L1OM:
1147 rtype = elf_x86_64_reloc_type (type);
1148 break;
1150 case EM_S370:
1151 rtype = i370_reloc_type (type);
1152 break;
1154 case EM_S390_OLD:
1155 case EM_S390:
1156 rtype = elf_s390_reloc_type (type);
1157 break;
1159 case EM_SCORE:
1160 rtype = elf_score_reloc_type (type);
1161 break;
1163 case EM_XSTORMY16:
1164 rtype = elf_xstormy16_reloc_type (type);
1165 break;
1167 case EM_CRX:
1168 rtype = elf_crx_reloc_type (type);
1169 break;
1171 case EM_VAX:
1172 rtype = elf_vax_reloc_type (type);
1173 break;
1175 case EM_IP2K:
1176 case EM_IP2K_OLD:
1177 rtype = elf_ip2k_reloc_type (type);
1178 break;
1180 case EM_IQ2000:
1181 rtype = elf_iq2000_reloc_type (type);
1182 break;
1184 case EM_XTENSA_OLD:
1185 case EM_XTENSA:
1186 rtype = elf_xtensa_reloc_type (type);
1187 break;
1189 case EM_LATTICEMICO32:
1190 rtype = elf_lm32_reloc_type (type);
1191 break;
1193 case EM_M32C_OLD:
1194 case EM_M32C:
1195 rtype = elf_m32c_reloc_type (type);
1196 break;
1198 case EM_MT:
1199 rtype = elf_mt_reloc_type (type);
1200 break;
1202 case EM_BLACKFIN:
1203 rtype = elf_bfin_reloc_type (type);
1204 break;
1206 case EM_CYGNUS_MEP:
1207 rtype = elf_mep_reloc_type (type);
1208 break;
1210 case EM_CR16:
1211 case EM_CR16_OLD:
1212 rtype = elf_cr16_reloc_type (type);
1213 break;
1215 case EM_MICROBLAZE:
1216 case EM_MICROBLAZE_OLD:
1217 rtype = elf_microblaze_reloc_type (type);
1218 break;
1221 if (rtype == NULL)
1222 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1223 else
1224 printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1226 if (elf_header.e_machine == EM_ALPHA
1227 && rtype != NULL
1228 && streq (rtype, "R_ALPHA_LITUSE")
1229 && is_rela)
1231 switch (rels[i].r_addend)
1233 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1234 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1235 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1236 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1237 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1238 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1239 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1240 default: rtype = NULL;
1242 if (rtype)
1243 printf (" (%s)", rtype);
1244 else
1246 putchar (' ');
1247 printf (_("<unknown addend: %lx>"),
1248 (unsigned long) rels[i].r_addend);
1251 else if (symtab_index)
1253 if (symtab == NULL || symtab_index >= nsyms)
1254 printf (" bad symbol index: %08lx", (unsigned long) symtab_index);
1255 else
1257 Elf_Internal_Sym * psym;
1259 psym = symtab + symtab_index;
1261 printf (" ");
1263 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1265 const char * name;
1266 unsigned int len;
1267 unsigned int width = is_32bit_elf ? 8 : 14;
1269 /* Relocations against GNU_IFUNC symbols do not use the value
1270 of the symbol as the address to relocate against. Instead
1271 they invoke the function named by the symbol and use its
1272 result as the address for relocation.
1274 To indicate this to the user, do not display the value of
1275 the symbol in the "Symbols's Value" field. Instead show
1276 its name followed by () as a hint that the symbol is
1277 invoked. */
1279 if (strtab == NULL
1280 || psym->st_name == 0
1281 || psym->st_name >= strtablen)
1282 name = "??";
1283 else
1284 name = strtab + psym->st_name;
1286 len = print_symbol (width, name);
1287 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1289 else
1291 print_vma (psym->st_value, LONG_HEX);
1293 printf (is_32bit_elf ? " " : " ");
1296 if (psym->st_name == 0)
1298 const char * sec_name = "<null>";
1299 char name_buf[40];
1301 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1303 if (psym->st_shndx < elf_header.e_shnum)
1304 sec_name
1305 = SECTION_NAME (section_headers + psym->st_shndx);
1306 else if (psym->st_shndx == SHN_ABS)
1307 sec_name = "ABS";
1308 else if (psym->st_shndx == SHN_COMMON)
1309 sec_name = "COMMON";
1310 else if (elf_header.e_machine == EM_MIPS
1311 && psym->st_shndx == SHN_MIPS_SCOMMON)
1312 sec_name = "SCOMMON";
1313 else if (elf_header.e_machine == EM_MIPS
1314 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1315 sec_name = "SUNDEF";
1316 else if ((elf_header.e_machine == EM_X86_64
1317 || elf_header.e_machine == EM_L1OM)
1318 && psym->st_shndx == SHN_X86_64_LCOMMON)
1319 sec_name = "LARGE_COMMON";
1320 else if (elf_header.e_machine == EM_IA_64
1321 && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1322 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1323 sec_name = "ANSI_COM";
1324 else if (elf_header.e_machine == EM_IA_64
1325 && (elf_header.e_ident[EI_OSABI]
1326 == ELFOSABI_OPENVMS)
1327 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1328 sec_name = "VMS_SYMVEC";
1329 else
1331 sprintf (name_buf, "<section 0x%x>",
1332 (unsigned int) psym->st_shndx);
1333 sec_name = name_buf;
1336 print_symbol (22, sec_name);
1338 else if (strtab == NULL)
1339 printf (_("<string table index: %3ld>"), psym->st_name);
1340 else if (psym->st_name >= strtablen)
1341 printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1342 else
1343 print_symbol (22, strtab + psym->st_name);
1345 if (is_rela)
1347 long offset = (long) (bfd_signed_vma) rels[i].r_addend;
1349 if (offset < 0)
1350 printf (" - %lx", - offset);
1351 else
1352 printf (" + %lx", offset);
1356 else if (is_rela)
1358 printf ("%*c", is_32bit_elf ?
1359 (do_wide ? 34 : 28) : (do_wide ? 26 : 20), ' ');
1360 print_vma (rels[i].r_addend, LONG_HEX);
1363 if (elf_header.e_machine == EM_SPARCV9
1364 && rtype != NULL
1365 && streq (rtype, "R_SPARC_OLO10"))
1366 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (info));
1368 putchar ('\n');
1370 #ifdef BFD64
1371 if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1373 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (info);
1374 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (info);
1375 const char * rtype2 = elf_mips_reloc_type (type2);
1376 const char * rtype3 = elf_mips_reloc_type (type3);
1378 printf (" Type2: ");
1380 if (rtype2 == NULL)
1381 printf (_("unrecognized: %-7lx"),
1382 (unsigned long) type2 & 0xffffffff);
1383 else
1384 printf ("%-17.17s", rtype2);
1386 printf ("\n Type3: ");
1388 if (rtype3 == NULL)
1389 printf (_("unrecognized: %-7lx"),
1390 (unsigned long) type3 & 0xffffffff);
1391 else
1392 printf ("%-17.17s", rtype3);
1394 putchar ('\n');
1396 #endif /* BFD64 */
1399 free (rels);
1402 static const char *
1403 get_mips_dynamic_type (unsigned long type)
1405 switch (type)
1407 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1408 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1409 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1410 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1411 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1412 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1413 case DT_MIPS_MSYM: return "MIPS_MSYM";
1414 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1415 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1416 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1417 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1418 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1419 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1420 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1421 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1422 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1423 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1424 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1425 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1426 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1427 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1428 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1429 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1430 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1431 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1432 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1433 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1434 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1435 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1436 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1437 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1438 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1439 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1440 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1441 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1442 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1443 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1444 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1445 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1446 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1447 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1448 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1449 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1450 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1451 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1452 default:
1453 return NULL;
1457 static const char *
1458 get_sparc64_dynamic_type (unsigned long type)
1460 switch (type)
1462 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1463 default:
1464 return NULL;
1468 static const char *
1469 get_ppc_dynamic_type (unsigned long type)
1471 switch (type)
1473 case DT_PPC_GOT: return "PPC_GOT";
1474 default:
1475 return NULL;
1479 static const char *
1480 get_ppc64_dynamic_type (unsigned long type)
1482 switch (type)
1484 case DT_PPC64_GLINK: return "PPC64_GLINK";
1485 case DT_PPC64_OPD: return "PPC64_OPD";
1486 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1487 default:
1488 return NULL;
1492 static const char *
1493 get_parisc_dynamic_type (unsigned long type)
1495 switch (type)
1497 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1498 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1499 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1500 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1501 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1502 case DT_HP_PREINIT: return "HP_PREINIT";
1503 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1504 case DT_HP_NEEDED: return "HP_NEEDED";
1505 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1506 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1507 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1508 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1509 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
1510 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
1511 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
1512 case DT_HP_FILTERED: return "HP_FILTERED";
1513 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
1514 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1515 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
1516 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
1517 case DT_PLT: return "PLT";
1518 case DT_PLT_SIZE: return "PLT_SIZE";
1519 case DT_DLT: return "DLT";
1520 case DT_DLT_SIZE: return "DLT_SIZE";
1521 default:
1522 return NULL;
1526 static const char *
1527 get_ia64_dynamic_type (unsigned long type)
1529 switch (type)
1531 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
1532 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
1533 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
1534 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
1535 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1536 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
1537 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
1538 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
1539 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
1540 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
1541 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
1542 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
1543 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
1544 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
1545 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
1546 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
1547 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
1548 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
1549 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
1550 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
1551 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
1552 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
1553 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
1554 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
1555 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
1556 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
1557 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
1558 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
1559 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
1560 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
1561 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
1562 default:
1563 return NULL;
1567 static const char *
1568 get_alpha_dynamic_type (unsigned long type)
1570 switch (type)
1572 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1573 default:
1574 return NULL;
1578 static const char *
1579 get_score_dynamic_type (unsigned long type)
1581 switch (type)
1583 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1584 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
1585 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
1586 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
1587 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
1588 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
1589 default:
1590 return NULL;
1595 static const char *
1596 get_dynamic_type (unsigned long type)
1598 static char buff[64];
1600 switch (type)
1602 case DT_NULL: return "NULL";
1603 case DT_NEEDED: return "NEEDED";
1604 case DT_PLTRELSZ: return "PLTRELSZ";
1605 case DT_PLTGOT: return "PLTGOT";
1606 case DT_HASH: return "HASH";
1607 case DT_STRTAB: return "STRTAB";
1608 case DT_SYMTAB: return "SYMTAB";
1609 case DT_RELA: return "RELA";
1610 case DT_RELASZ: return "RELASZ";
1611 case DT_RELAENT: return "RELAENT";
1612 case DT_STRSZ: return "STRSZ";
1613 case DT_SYMENT: return "SYMENT";
1614 case DT_INIT: return "INIT";
1615 case DT_FINI: return "FINI";
1616 case DT_SONAME: return "SONAME";
1617 case DT_RPATH: return "RPATH";
1618 case DT_SYMBOLIC: return "SYMBOLIC";
1619 case DT_REL: return "REL";
1620 case DT_RELSZ: return "RELSZ";
1621 case DT_RELENT: return "RELENT";
1622 case DT_PLTREL: return "PLTREL";
1623 case DT_DEBUG: return "DEBUG";
1624 case DT_TEXTREL: return "TEXTREL";
1625 case DT_JMPREL: return "JMPREL";
1626 case DT_BIND_NOW: return "BIND_NOW";
1627 case DT_INIT_ARRAY: return "INIT_ARRAY";
1628 case DT_FINI_ARRAY: return "FINI_ARRAY";
1629 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1630 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1631 case DT_RUNPATH: return "RUNPATH";
1632 case DT_FLAGS: return "FLAGS";
1634 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1635 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1637 case DT_CHECKSUM: return "CHECKSUM";
1638 case DT_PLTPADSZ: return "PLTPADSZ";
1639 case DT_MOVEENT: return "MOVEENT";
1640 case DT_MOVESZ: return "MOVESZ";
1641 case DT_FEATURE: return "FEATURE";
1642 case DT_POSFLAG_1: return "POSFLAG_1";
1643 case DT_SYMINSZ: return "SYMINSZ";
1644 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
1646 case DT_ADDRRNGLO: return "ADDRRNGLO";
1647 case DT_CONFIG: return "CONFIG";
1648 case DT_DEPAUDIT: return "DEPAUDIT";
1649 case DT_AUDIT: return "AUDIT";
1650 case DT_PLTPAD: return "PLTPAD";
1651 case DT_MOVETAB: return "MOVETAB";
1652 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
1654 case DT_VERSYM: return "VERSYM";
1656 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
1657 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
1658 case DT_RELACOUNT: return "RELACOUNT";
1659 case DT_RELCOUNT: return "RELCOUNT";
1660 case DT_FLAGS_1: return "FLAGS_1";
1661 case DT_VERDEF: return "VERDEF";
1662 case DT_VERDEFNUM: return "VERDEFNUM";
1663 case DT_VERNEED: return "VERNEED";
1664 case DT_VERNEEDNUM: return "VERNEEDNUM";
1666 case DT_AUXILIARY: return "AUXILIARY";
1667 case DT_USED: return "USED";
1668 case DT_FILTER: return "FILTER";
1670 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1671 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1672 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1673 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1674 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1675 case DT_GNU_HASH: return "GNU_HASH";
1677 default:
1678 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1680 const char * result;
1682 switch (elf_header.e_machine)
1684 case EM_MIPS:
1685 case EM_MIPS_RS3_LE:
1686 result = get_mips_dynamic_type (type);
1687 break;
1688 case EM_SPARCV9:
1689 result = get_sparc64_dynamic_type (type);
1690 break;
1691 case EM_PPC:
1692 result = get_ppc_dynamic_type (type);
1693 break;
1694 case EM_PPC64:
1695 result = get_ppc64_dynamic_type (type);
1696 break;
1697 case EM_IA_64:
1698 result = get_ia64_dynamic_type (type);
1699 break;
1700 case EM_ALPHA:
1701 result = get_alpha_dynamic_type (type);
1702 break;
1703 case EM_SCORE:
1704 result = get_score_dynamic_type (type);
1705 break;
1706 default:
1707 result = NULL;
1708 break;
1711 if (result != NULL)
1712 return result;
1714 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
1716 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
1717 || (elf_header.e_machine == EM_PARISC
1718 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
1720 const char * result;
1722 switch (elf_header.e_machine)
1724 case EM_PARISC:
1725 result = get_parisc_dynamic_type (type);
1726 break;
1727 case EM_IA_64:
1728 result = get_ia64_dynamic_type (type);
1729 break;
1730 default:
1731 result = NULL;
1732 break;
1735 if (result != NULL)
1736 return result;
1738 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
1739 type);
1741 else
1742 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
1744 return buff;
1748 static char *
1749 get_file_type (unsigned e_type)
1751 static char buff[32];
1753 switch (e_type)
1755 case ET_NONE: return _("NONE (None)");
1756 case ET_REL: return _("REL (Relocatable file)");
1757 case ET_EXEC: return _("EXEC (Executable file)");
1758 case ET_DYN: return _("DYN (Shared object file)");
1759 case ET_CORE: return _("CORE (Core file)");
1761 default:
1762 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
1763 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
1764 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
1765 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
1766 else
1767 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
1768 return buff;
1772 static char *
1773 get_machine_name (unsigned e_machine)
1775 static char buff[64]; /* XXX */
1777 switch (e_machine)
1779 case EM_NONE: return _("None");
1780 case EM_M32: return "WE32100";
1781 case EM_SPARC: return "Sparc";
1782 case EM_SPU: return "SPU";
1783 case EM_386: return "Intel 80386";
1784 case EM_68K: return "MC68000";
1785 case EM_88K: return "MC88000";
1786 case EM_486: return "Intel 80486";
1787 case EM_860: return "Intel 80860";
1788 case EM_MIPS: return "MIPS R3000";
1789 case EM_S370: return "IBM System/370";
1790 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
1791 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
1792 case EM_PARISC: return "HPPA";
1793 case EM_PPC_OLD: return "Power PC (old)";
1794 case EM_SPARC32PLUS: return "Sparc v8+" ;
1795 case EM_960: return "Intel 90860";
1796 case EM_PPC: return "PowerPC";
1797 case EM_PPC64: return "PowerPC64";
1798 case EM_V800: return "NEC V800";
1799 case EM_FR20: return "Fujitsu FR20";
1800 case EM_RH32: return "TRW RH32";
1801 case EM_MCORE: return "MCORE";
1802 case EM_ARM: return "ARM";
1803 case EM_OLD_ALPHA: return "Digital Alpha (old)";
1804 case EM_SH: return "Renesas / SuperH SH";
1805 case EM_SPARCV9: return "Sparc v9";
1806 case EM_TRICORE: return "Siemens Tricore";
1807 case EM_ARC: return "ARC";
1808 case EM_H8_300: return "Renesas H8/300";
1809 case EM_H8_300H: return "Renesas H8/300H";
1810 case EM_H8S: return "Renesas H8S";
1811 case EM_H8_500: return "Renesas H8/500";
1812 case EM_IA_64: return "Intel IA-64";
1813 case EM_MIPS_X: return "Stanford MIPS-X";
1814 case EM_COLDFIRE: return "Motorola Coldfire";
1815 case EM_68HC12: return "Motorola M68HC12";
1816 case EM_ALPHA: return "Alpha";
1817 case EM_CYGNUS_D10V:
1818 case EM_D10V: return "d10v";
1819 case EM_CYGNUS_D30V:
1820 case EM_D30V: return "d30v";
1821 case EM_CYGNUS_M32R:
1822 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
1823 case EM_CYGNUS_V850:
1824 case EM_V850: return "NEC v850";
1825 case EM_CYGNUS_MN10300:
1826 case EM_MN10300: return "mn10300";
1827 case EM_CYGNUS_MN10200:
1828 case EM_MN10200: return "mn10200";
1829 case EM_CYGNUS_FR30:
1830 case EM_FR30: return "Fujitsu FR30";
1831 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
1832 case EM_PJ_OLD:
1833 case EM_PJ: return "picoJava";
1834 case EM_MMA: return "Fujitsu Multimedia Accelerator";
1835 case EM_PCP: return "Siemens PCP";
1836 case EM_NCPU: return "Sony nCPU embedded RISC processor";
1837 case EM_NDR1: return "Denso NDR1 microprocesspr";
1838 case EM_STARCORE: return "Motorola Star*Core processor";
1839 case EM_ME16: return "Toyota ME16 processor";
1840 case EM_ST100: return "STMicroelectronics ST100 processor";
1841 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
1842 case EM_FX66: return "Siemens FX66 microcontroller";
1843 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
1844 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
1845 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
1846 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
1847 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
1848 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
1849 case EM_SVX: return "Silicon Graphics SVx";
1850 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
1851 case EM_VAX: return "Digital VAX";
1852 case EM_AVR_OLD:
1853 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
1854 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
1855 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
1856 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
1857 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
1858 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
1859 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
1860 case EM_PRISM: return "Vitesse Prism";
1861 case EM_X86_64: return "Advanced Micro Devices X86-64";
1862 case EM_L1OM: return "Intel L1OM";
1863 case EM_S390_OLD:
1864 case EM_S390: return "IBM S/390";
1865 case EM_SCORE: return "SUNPLUS S+Core";
1866 case EM_XSTORMY16: return "Sanyo Xstormy16 CPU core";
1867 case EM_OPENRISC:
1868 case EM_OR32: return "OpenRISC";
1869 case EM_CRX: return "National Semiconductor CRX microprocessor";
1870 case EM_DLX: return "OpenDLX";
1871 case EM_IP2K_OLD:
1872 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
1873 case EM_IQ2000: return "Vitesse IQ2000";
1874 case EM_XTENSA_OLD:
1875 case EM_XTENSA: return "Tensilica Xtensa Processor";
1876 case EM_LATTICEMICO32: return "Lattice Mico32";
1877 case EM_M32C_OLD:
1878 case EM_M32C: return "Renesas M32c";
1879 case EM_MT: return "Morpho Techologies MT processor";
1880 case EM_BLACKFIN: return "Analog Devices Blackfin";
1881 case EM_NIOS32: return "Altera Nios";
1882 case EM_ALTERA_NIOS2: return "Altera Nios II";
1883 case EM_XC16X: return "Infineon Technologies xc16x";
1884 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
1885 case EM_CR16:
1886 case EM_CR16_OLD: return "National Semiconductor's CR16";
1887 case EM_MICROBLAZE: return "Xilinx MicroBlaze";
1888 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
1889 default:
1890 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
1891 return buff;
1895 static void
1896 decode_ARM_machine_flags (unsigned e_flags, char buf[])
1898 unsigned eabi;
1899 int unknown = 0;
1901 eabi = EF_ARM_EABI_VERSION (e_flags);
1902 e_flags &= ~ EF_ARM_EABIMASK;
1904 /* Handle "generic" ARM flags. */
1905 if (e_flags & EF_ARM_RELEXEC)
1907 strcat (buf, ", relocatable executable");
1908 e_flags &= ~ EF_ARM_RELEXEC;
1911 if (e_flags & EF_ARM_HASENTRY)
1913 strcat (buf, ", has entry point");
1914 e_flags &= ~ EF_ARM_HASENTRY;
1917 /* Now handle EABI specific flags. */
1918 switch (eabi)
1920 default:
1921 strcat (buf, ", <unrecognized EABI>");
1922 if (e_flags)
1923 unknown = 1;
1924 break;
1926 case EF_ARM_EABI_VER1:
1927 strcat (buf, ", Version1 EABI");
1928 while (e_flags)
1930 unsigned flag;
1932 /* Process flags one bit at a time. */
1933 flag = e_flags & - e_flags;
1934 e_flags &= ~ flag;
1936 switch (flag)
1938 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1939 strcat (buf, ", sorted symbol tables");
1940 break;
1942 default:
1943 unknown = 1;
1944 break;
1947 break;
1949 case EF_ARM_EABI_VER2:
1950 strcat (buf, ", Version2 EABI");
1951 while (e_flags)
1953 unsigned flag;
1955 /* Process flags one bit at a time. */
1956 flag = e_flags & - e_flags;
1957 e_flags &= ~ flag;
1959 switch (flag)
1961 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1962 strcat (buf, ", sorted symbol tables");
1963 break;
1965 case EF_ARM_DYNSYMSUSESEGIDX:
1966 strcat (buf, ", dynamic symbols use segment index");
1967 break;
1969 case EF_ARM_MAPSYMSFIRST:
1970 strcat (buf, ", mapping symbols precede others");
1971 break;
1973 default:
1974 unknown = 1;
1975 break;
1978 break;
1980 case EF_ARM_EABI_VER3:
1981 strcat (buf, ", Version3 EABI");
1982 break;
1984 case EF_ARM_EABI_VER4:
1985 strcat (buf, ", Version4 EABI");
1986 goto eabi;
1988 case EF_ARM_EABI_VER5:
1989 strcat (buf, ", Version5 EABI");
1990 eabi:
1991 while (e_flags)
1993 unsigned flag;
1995 /* Process flags one bit at a time. */
1996 flag = e_flags & - e_flags;
1997 e_flags &= ~ flag;
1999 switch (flag)
2001 case EF_ARM_BE8:
2002 strcat (buf, ", BE8");
2003 break;
2005 case EF_ARM_LE8:
2006 strcat (buf, ", LE8");
2007 break;
2009 default:
2010 unknown = 1;
2011 break;
2014 break;
2016 case EF_ARM_EABI_UNKNOWN:
2017 strcat (buf, ", GNU EABI");
2018 while (e_flags)
2020 unsigned flag;
2022 /* Process flags one bit at a time. */
2023 flag = e_flags & - e_flags;
2024 e_flags &= ~ flag;
2026 switch (flag)
2028 case EF_ARM_INTERWORK:
2029 strcat (buf, ", interworking enabled");
2030 break;
2032 case EF_ARM_APCS_26:
2033 strcat (buf, ", uses APCS/26");
2034 break;
2036 case EF_ARM_APCS_FLOAT:
2037 strcat (buf, ", uses APCS/float");
2038 break;
2040 case EF_ARM_PIC:
2041 strcat (buf, ", position independent");
2042 break;
2044 case EF_ARM_ALIGN8:
2045 strcat (buf, ", 8 bit structure alignment");
2046 break;
2048 case EF_ARM_NEW_ABI:
2049 strcat (buf, ", uses new ABI");
2050 break;
2052 case EF_ARM_OLD_ABI:
2053 strcat (buf, ", uses old ABI");
2054 break;
2056 case EF_ARM_SOFT_FLOAT:
2057 strcat (buf, ", software FP");
2058 break;
2060 case EF_ARM_VFP_FLOAT:
2061 strcat (buf, ", VFP");
2062 break;
2064 case EF_ARM_MAVERICK_FLOAT:
2065 strcat (buf, ", Maverick FP");
2066 break;
2068 default:
2069 unknown = 1;
2070 break;
2075 if (unknown)
2076 strcat (buf,", <unknown>");
2079 static char *
2080 get_machine_flags (unsigned e_flags, unsigned e_machine)
2082 static char buf[1024];
2084 buf[0] = '\0';
2086 if (e_flags)
2088 switch (e_machine)
2090 default:
2091 break;
2093 case EM_ARM:
2094 decode_ARM_machine_flags (e_flags, buf);
2095 break;
2097 case EM_CYGNUS_FRV:
2098 switch (e_flags & EF_FRV_CPU_MASK)
2100 case EF_FRV_CPU_GENERIC:
2101 break;
2103 default:
2104 strcat (buf, ", fr???");
2105 break;
2107 case EF_FRV_CPU_FR300:
2108 strcat (buf, ", fr300");
2109 break;
2111 case EF_FRV_CPU_FR400:
2112 strcat (buf, ", fr400");
2113 break;
2114 case EF_FRV_CPU_FR405:
2115 strcat (buf, ", fr405");
2116 break;
2118 case EF_FRV_CPU_FR450:
2119 strcat (buf, ", fr450");
2120 break;
2122 case EF_FRV_CPU_FR500:
2123 strcat (buf, ", fr500");
2124 break;
2125 case EF_FRV_CPU_FR550:
2126 strcat (buf, ", fr550");
2127 break;
2129 case EF_FRV_CPU_SIMPLE:
2130 strcat (buf, ", simple");
2131 break;
2132 case EF_FRV_CPU_TOMCAT:
2133 strcat (buf, ", tomcat");
2134 break;
2136 break;
2138 case EM_68K:
2139 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
2140 strcat (buf, ", m68000");
2141 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
2142 strcat (buf, ", cpu32");
2143 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
2144 strcat (buf, ", fido_a");
2145 else
2147 char const * isa = _("unknown");
2148 char const * mac = _("unknown mac");
2149 char const * additional = NULL;
2151 switch (e_flags & EF_M68K_CF_ISA_MASK)
2153 case EF_M68K_CF_ISA_A_NODIV:
2154 isa = "A";
2155 additional = ", nodiv";
2156 break;
2157 case EF_M68K_CF_ISA_A:
2158 isa = "A";
2159 break;
2160 case EF_M68K_CF_ISA_A_PLUS:
2161 isa = "A+";
2162 break;
2163 case EF_M68K_CF_ISA_B_NOUSP:
2164 isa = "B";
2165 additional = ", nousp";
2166 break;
2167 case EF_M68K_CF_ISA_B:
2168 isa = "B";
2169 break;
2171 strcat (buf, ", cf, isa ");
2172 strcat (buf, isa);
2173 if (additional)
2174 strcat (buf, additional);
2175 if (e_flags & EF_M68K_CF_FLOAT)
2176 strcat (buf, ", float");
2177 switch (e_flags & EF_M68K_CF_MAC_MASK)
2179 case 0:
2180 mac = NULL;
2181 break;
2182 case EF_M68K_CF_MAC:
2183 mac = "mac";
2184 break;
2185 case EF_M68K_CF_EMAC:
2186 mac = "emac";
2187 break;
2189 if (mac)
2191 strcat (buf, ", ");
2192 strcat (buf, mac);
2195 break;
2197 case EM_PPC:
2198 if (e_flags & EF_PPC_EMB)
2199 strcat (buf, ", emb");
2201 if (e_flags & EF_PPC_RELOCATABLE)
2202 strcat (buf, ", relocatable");
2204 if (e_flags & EF_PPC_RELOCATABLE_LIB)
2205 strcat (buf, ", relocatable-lib");
2206 break;
2208 case EM_V850:
2209 case EM_CYGNUS_V850:
2210 switch (e_flags & EF_V850_ARCH)
2212 case E_V850E1_ARCH:
2213 strcat (buf, ", v850e1");
2214 break;
2215 case E_V850E_ARCH:
2216 strcat (buf, ", v850e");
2217 break;
2218 case E_V850_ARCH:
2219 strcat (buf, ", v850");
2220 break;
2221 default:
2222 strcat (buf, ", unknown v850 architecture variant");
2223 break;
2225 break;
2227 case EM_M32R:
2228 case EM_CYGNUS_M32R:
2229 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
2230 strcat (buf, ", m32r");
2231 break;
2233 case EM_MIPS:
2234 case EM_MIPS_RS3_LE:
2235 if (e_flags & EF_MIPS_NOREORDER)
2236 strcat (buf, ", noreorder");
2238 if (e_flags & EF_MIPS_PIC)
2239 strcat (buf, ", pic");
2241 if (e_flags & EF_MIPS_CPIC)
2242 strcat (buf, ", cpic");
2244 if (e_flags & EF_MIPS_UCODE)
2245 strcat (buf, ", ugen_reserved");
2247 if (e_flags & EF_MIPS_ABI2)
2248 strcat (buf, ", abi2");
2250 if (e_flags & EF_MIPS_OPTIONS_FIRST)
2251 strcat (buf, ", odk first");
2253 if (e_flags & EF_MIPS_32BITMODE)
2254 strcat (buf, ", 32bitmode");
2256 switch ((e_flags & EF_MIPS_MACH))
2258 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
2259 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
2260 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
2261 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
2262 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
2263 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
2264 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
2265 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
2266 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
2267 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
2268 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
2269 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
2270 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
2271 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
2272 case 0:
2273 /* We simply ignore the field in this case to avoid confusion:
2274 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
2275 extension. */
2276 break;
2277 default: strcat (buf, ", unknown CPU"); break;
2280 switch ((e_flags & EF_MIPS_ABI))
2282 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
2283 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
2284 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
2285 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
2286 case 0:
2287 /* We simply ignore the field in this case to avoid confusion:
2288 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
2289 This means it is likely to be an o32 file, but not for
2290 sure. */
2291 break;
2292 default: strcat (buf, ", unknown ABI"); break;
2295 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
2296 strcat (buf, ", mdmx");
2298 if (e_flags & EF_MIPS_ARCH_ASE_M16)
2299 strcat (buf, ", mips16");
2301 switch ((e_flags & EF_MIPS_ARCH))
2303 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
2304 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
2305 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
2306 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
2307 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
2308 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
2309 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
2310 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
2311 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
2312 default: strcat (buf, ", unknown ISA"); break;
2315 break;
2317 case EM_SH:
2318 switch ((e_flags & EF_SH_MACH_MASK))
2320 case EF_SH1: strcat (buf, ", sh1"); break;
2321 case EF_SH2: strcat (buf, ", sh2"); break;
2322 case EF_SH3: strcat (buf, ", sh3"); break;
2323 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
2324 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
2325 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
2326 case EF_SH3E: strcat (buf, ", sh3e"); break;
2327 case EF_SH4: strcat (buf, ", sh4"); break;
2328 case EF_SH5: strcat (buf, ", sh5"); break;
2329 case EF_SH2E: strcat (buf, ", sh2e"); break;
2330 case EF_SH4A: strcat (buf, ", sh4a"); break;
2331 case EF_SH2A: strcat (buf, ", sh2a"); break;
2332 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
2333 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
2334 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
2335 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
2336 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
2337 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
2338 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
2339 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
2340 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
2341 default: strcat (buf, ", unknown ISA"); break;
2344 break;
2346 case EM_SPARCV9:
2347 if (e_flags & EF_SPARC_32PLUS)
2348 strcat (buf, ", v8+");
2350 if (e_flags & EF_SPARC_SUN_US1)
2351 strcat (buf, ", ultrasparcI");
2353 if (e_flags & EF_SPARC_SUN_US3)
2354 strcat (buf, ", ultrasparcIII");
2356 if (e_flags & EF_SPARC_HAL_R1)
2357 strcat (buf, ", halr1");
2359 if (e_flags & EF_SPARC_LEDATA)
2360 strcat (buf, ", ledata");
2362 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
2363 strcat (buf, ", tso");
2365 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
2366 strcat (buf, ", pso");
2368 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
2369 strcat (buf, ", rmo");
2370 break;
2372 case EM_PARISC:
2373 switch (e_flags & EF_PARISC_ARCH)
2375 case EFA_PARISC_1_0:
2376 strcpy (buf, ", PA-RISC 1.0");
2377 break;
2378 case EFA_PARISC_1_1:
2379 strcpy (buf, ", PA-RISC 1.1");
2380 break;
2381 case EFA_PARISC_2_0:
2382 strcpy (buf, ", PA-RISC 2.0");
2383 break;
2384 default:
2385 break;
2387 if (e_flags & EF_PARISC_TRAPNIL)
2388 strcat (buf, ", trapnil");
2389 if (e_flags & EF_PARISC_EXT)
2390 strcat (buf, ", ext");
2391 if (e_flags & EF_PARISC_LSB)
2392 strcat (buf, ", lsb");
2393 if (e_flags & EF_PARISC_WIDE)
2394 strcat (buf, ", wide");
2395 if (e_flags & EF_PARISC_NO_KABP)
2396 strcat (buf, ", no kabp");
2397 if (e_flags & EF_PARISC_LAZYSWAP)
2398 strcat (buf, ", lazyswap");
2399 break;
2401 case EM_PJ:
2402 case EM_PJ_OLD:
2403 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
2404 strcat (buf, ", new calling convention");
2406 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
2407 strcat (buf, ", gnu calling convention");
2408 break;
2410 case EM_IA_64:
2411 if ((e_flags & EF_IA_64_ABI64))
2412 strcat (buf, ", 64-bit");
2413 else
2414 strcat (buf, ", 32-bit");
2415 if ((e_flags & EF_IA_64_REDUCEDFP))
2416 strcat (buf, ", reduced fp model");
2417 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
2418 strcat (buf, ", no function descriptors, constant gp");
2419 else if ((e_flags & EF_IA_64_CONS_GP))
2420 strcat (buf, ", constant gp");
2421 if ((e_flags & EF_IA_64_ABSOLUTE))
2422 strcat (buf, ", absolute");
2423 break;
2425 case EM_VAX:
2426 if ((e_flags & EF_VAX_NONPIC))
2427 strcat (buf, ", non-PIC");
2428 if ((e_flags & EF_VAX_DFLOAT))
2429 strcat (buf, ", D-Float");
2430 if ((e_flags & EF_VAX_GFLOAT))
2431 strcat (buf, ", G-Float");
2432 break;
2436 return buf;
2439 static const char *
2440 get_osabi_name (unsigned int osabi)
2442 static char buff[32];
2444 switch (osabi)
2446 case ELFOSABI_NONE: return "UNIX - System V";
2447 case ELFOSABI_HPUX: return "UNIX - HP-UX";
2448 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
2449 case ELFOSABI_LINUX: return "UNIX - Linux";
2450 case ELFOSABI_HURD: return "GNU/Hurd";
2451 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
2452 case ELFOSABI_AIX: return "UNIX - AIX";
2453 case ELFOSABI_IRIX: return "UNIX - IRIX";
2454 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
2455 case ELFOSABI_TRU64: return "UNIX - TRU64";
2456 case ELFOSABI_MODESTO: return "Novell - Modesto";
2457 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
2458 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
2459 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
2460 case ELFOSABI_AROS: return "AROS";
2461 case ELFOSABI_STANDALONE: return _("Standalone App");
2462 case ELFOSABI_ARM: return "ARM";
2463 default:
2464 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
2465 return buff;
2469 static const char *
2470 get_arm_segment_type (unsigned long type)
2472 switch (type)
2474 case PT_ARM_EXIDX:
2475 return "EXIDX";
2476 default:
2477 break;
2480 return NULL;
2483 static const char *
2484 get_mips_segment_type (unsigned long type)
2486 switch (type)
2488 case PT_MIPS_REGINFO:
2489 return "REGINFO";
2490 case PT_MIPS_RTPROC:
2491 return "RTPROC";
2492 case PT_MIPS_OPTIONS:
2493 return "OPTIONS";
2494 default:
2495 break;
2498 return NULL;
2501 static const char *
2502 get_parisc_segment_type (unsigned long type)
2504 switch (type)
2506 case PT_HP_TLS: return "HP_TLS";
2507 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
2508 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
2509 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
2510 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
2511 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
2512 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
2513 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
2514 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
2515 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
2516 case PT_HP_PARALLEL: return "HP_PARALLEL";
2517 case PT_HP_FASTBIND: return "HP_FASTBIND";
2518 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
2519 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
2520 case PT_HP_STACK: return "HP_STACK";
2521 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
2522 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
2523 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
2524 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
2525 default:
2526 break;
2529 return NULL;
2532 static const char *
2533 get_ia64_segment_type (unsigned long type)
2535 switch (type)
2537 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
2538 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
2539 case PT_HP_TLS: return "HP_TLS";
2540 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
2541 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
2542 case PT_IA_64_HP_STACK: return "HP_STACK";
2543 default:
2544 break;
2547 return NULL;
2550 static const char *
2551 get_segment_type (unsigned long p_type)
2553 static char buff[32];
2555 switch (p_type)
2557 case PT_NULL: return "NULL";
2558 case PT_LOAD: return "LOAD";
2559 case PT_DYNAMIC: return "DYNAMIC";
2560 case PT_INTERP: return "INTERP";
2561 case PT_NOTE: return "NOTE";
2562 case PT_SHLIB: return "SHLIB";
2563 case PT_PHDR: return "PHDR";
2564 case PT_TLS: return "TLS";
2566 case PT_GNU_EH_FRAME:
2567 return "GNU_EH_FRAME";
2568 case PT_GNU_STACK: return "GNU_STACK";
2569 case PT_GNU_RELRO: return "GNU_RELRO";
2571 default:
2572 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
2574 const char * result;
2576 switch (elf_header.e_machine)
2578 case EM_ARM:
2579 result = get_arm_segment_type (p_type);
2580 break;
2581 case EM_MIPS:
2582 case EM_MIPS_RS3_LE:
2583 result = get_mips_segment_type (p_type);
2584 break;
2585 case EM_PARISC:
2586 result = get_parisc_segment_type (p_type);
2587 break;
2588 case EM_IA_64:
2589 result = get_ia64_segment_type (p_type);
2590 break;
2591 default:
2592 result = NULL;
2593 break;
2596 if (result != NULL)
2597 return result;
2599 sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
2601 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
2603 const char * result;
2605 switch (elf_header.e_machine)
2607 case EM_PARISC:
2608 result = get_parisc_segment_type (p_type);
2609 break;
2610 case EM_IA_64:
2611 result = get_ia64_segment_type (p_type);
2612 break;
2613 default:
2614 result = NULL;
2615 break;
2618 if (result != NULL)
2619 return result;
2621 sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
2623 else
2624 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
2626 return buff;
2630 static const char *
2631 get_mips_section_type_name (unsigned int sh_type)
2633 switch (sh_type)
2635 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2636 case SHT_MIPS_MSYM: return "MIPS_MSYM";
2637 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2638 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
2639 case SHT_MIPS_UCODE: return "MIPS_UCODE";
2640 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
2641 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
2642 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
2643 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
2644 case SHT_MIPS_RELD: return "MIPS_RELD";
2645 case SHT_MIPS_IFACE: return "MIPS_IFACE";
2646 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
2647 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2648 case SHT_MIPS_SHDR: return "MIPS_SHDR";
2649 case SHT_MIPS_FDESC: return "MIPS_FDESC";
2650 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
2651 case SHT_MIPS_DENSE: return "MIPS_DENSE";
2652 case SHT_MIPS_PDESC: return "MIPS_PDESC";
2653 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
2654 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
2655 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
2656 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
2657 case SHT_MIPS_LINE: return "MIPS_LINE";
2658 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
2659 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
2660 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
2661 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
2662 case SHT_MIPS_DWARF: return "MIPS_DWARF";
2663 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
2664 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2665 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
2666 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
2667 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
2668 case SHT_MIPS_XLATE: return "MIPS_XLATE";
2669 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
2670 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
2671 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
2672 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
2673 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
2674 default:
2675 break;
2677 return NULL;
2680 static const char *
2681 get_parisc_section_type_name (unsigned int sh_type)
2683 switch (sh_type)
2685 case SHT_PARISC_EXT: return "PARISC_EXT";
2686 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
2687 case SHT_PARISC_DOC: return "PARISC_DOC";
2688 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
2689 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
2690 case SHT_PARISC_STUBS: return "PARISC_STUBS";
2691 case SHT_PARISC_DLKM: return "PARISC_DLKM";
2692 default:
2693 break;
2695 return NULL;
2698 static const char *
2699 get_ia64_section_type_name (unsigned int sh_type)
2701 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
2702 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
2703 return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
2705 switch (sh_type)
2707 case SHT_IA_64_EXT: return "IA_64_EXT";
2708 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
2709 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
2710 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
2711 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
2712 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
2713 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
2714 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
2715 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
2716 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
2717 default:
2718 break;
2720 return NULL;
2723 static const char *
2724 get_x86_64_section_type_name (unsigned int sh_type)
2726 switch (sh_type)
2728 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
2729 default:
2730 break;
2732 return NULL;
2735 static const char *
2736 get_arm_section_type_name (unsigned int sh_type)
2738 switch (sh_type)
2740 case SHT_ARM_EXIDX: return "ARM_EXIDX";
2741 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
2742 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
2743 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
2744 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
2745 default:
2746 break;
2748 return NULL;
2751 static const char *
2752 get_section_type_name (unsigned int sh_type)
2754 static char buff[32];
2756 switch (sh_type)
2758 case SHT_NULL: return "NULL";
2759 case SHT_PROGBITS: return "PROGBITS";
2760 case SHT_SYMTAB: return "SYMTAB";
2761 case SHT_STRTAB: return "STRTAB";
2762 case SHT_RELA: return "RELA";
2763 case SHT_HASH: return "HASH";
2764 case SHT_DYNAMIC: return "DYNAMIC";
2765 case SHT_NOTE: return "NOTE";
2766 case SHT_NOBITS: return "NOBITS";
2767 case SHT_REL: return "REL";
2768 case SHT_SHLIB: return "SHLIB";
2769 case SHT_DYNSYM: return "DYNSYM";
2770 case SHT_INIT_ARRAY: return "INIT_ARRAY";
2771 case SHT_FINI_ARRAY: return "FINI_ARRAY";
2772 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2773 case SHT_GNU_HASH: return "GNU_HASH";
2774 case SHT_GROUP: return "GROUP";
2775 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICIES";
2776 case SHT_GNU_verdef: return "VERDEF";
2777 case SHT_GNU_verneed: return "VERNEED";
2778 case SHT_GNU_versym: return "VERSYM";
2779 case 0x6ffffff0: return "VERSYM";
2780 case 0x6ffffffc: return "VERDEF";
2781 case 0x7ffffffd: return "AUXILIARY";
2782 case 0x7fffffff: return "FILTER";
2783 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
2785 default:
2786 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
2788 const char * result;
2790 switch (elf_header.e_machine)
2792 case EM_MIPS:
2793 case EM_MIPS_RS3_LE:
2794 result = get_mips_section_type_name (sh_type);
2795 break;
2796 case EM_PARISC:
2797 result = get_parisc_section_type_name (sh_type);
2798 break;
2799 case EM_IA_64:
2800 result = get_ia64_section_type_name (sh_type);
2801 break;
2802 case EM_X86_64:
2803 case EM_L1OM:
2804 result = get_x86_64_section_type_name (sh_type);
2805 break;
2806 case EM_ARM:
2807 result = get_arm_section_type_name (sh_type);
2808 break;
2809 default:
2810 result = NULL;
2811 break;
2814 if (result != NULL)
2815 return result;
2817 sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
2819 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
2821 const char * result;
2823 switch (elf_header.e_machine)
2825 case EM_IA_64:
2826 result = get_ia64_section_type_name (sh_type);
2827 break;
2828 default:
2829 result = NULL;
2830 break;
2833 if (result != NULL)
2834 return result;
2836 sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
2838 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
2839 sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
2840 else
2841 snprintf (buff, sizeof (buff), _("<unknown>: %x"), sh_type);
2843 return buff;
2847 #define OPTION_DEBUG_DUMP 512
2849 static struct option options[] =
2851 {"all", no_argument, 0, 'a'},
2852 {"file-header", no_argument, 0, 'h'},
2853 {"program-headers", no_argument, 0, 'l'},
2854 {"headers", no_argument, 0, 'e'},
2855 {"histogram", no_argument, 0, 'I'},
2856 {"segments", no_argument, 0, 'l'},
2857 {"sections", no_argument, 0, 'S'},
2858 {"section-headers", no_argument, 0, 'S'},
2859 {"section-groups", no_argument, 0, 'g'},
2860 {"section-details", no_argument, 0, 't'},
2861 {"full-section-name",no_argument, 0, 'N'},
2862 {"symbols", no_argument, 0, 's'},
2863 {"syms", no_argument, 0, 's'},
2864 {"relocs", no_argument, 0, 'r'},
2865 {"notes", no_argument, 0, 'n'},
2866 {"dynamic", no_argument, 0, 'd'},
2867 {"arch-specific", no_argument, 0, 'A'},
2868 {"version-info", no_argument, 0, 'V'},
2869 {"use-dynamic", no_argument, 0, 'D'},
2870 {"unwind", no_argument, 0, 'u'},
2871 {"archive-index", no_argument, 0, 'c'},
2872 {"hex-dump", required_argument, 0, 'x'},
2873 {"relocated-dump", required_argument, 0, 'R'},
2874 {"string-dump", required_argument, 0, 'p'},
2875 #ifdef SUPPORT_DISASSEMBLY
2876 {"instruction-dump", required_argument, 0, 'i'},
2877 #endif
2878 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
2880 {"version", no_argument, 0, 'v'},
2881 {"wide", no_argument, 0, 'W'},
2882 {"help", no_argument, 0, 'H'},
2883 {0, no_argument, 0, 0}
2886 static void
2887 usage (FILE * stream)
2889 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
2890 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
2891 fprintf (stream, _(" Options are:\n\
2892 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
2893 -h --file-header Display the ELF file header\n\
2894 -l --program-headers Display the program headers\n\
2895 --segments An alias for --program-headers\n\
2896 -S --section-headers Display the sections' header\n\
2897 --sections An alias for --section-headers\n\
2898 -g --section-groups Display the section groups\n\
2899 -t --section-details Display the section details\n\
2900 -e --headers Equivalent to: -h -l -S\n\
2901 -s --syms Display the symbol table\n\
2902 --symbols An alias for --syms\n\
2903 -n --notes Display the core notes (if present)\n\
2904 -r --relocs Display the relocations (if present)\n\
2905 -u --unwind Display the unwind info (if present)\n\
2906 -d --dynamic Display the dynamic section (if present)\n\
2907 -V --version-info Display the version sections (if present)\n\
2908 -A --arch-specific Display architecture specific information (if any).\n\
2909 -c --archive-index Display the symbol/file index in an archive\n\
2910 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
2911 -x --hex-dump=<number|name>\n\
2912 Dump the contents of section <number|name> as bytes\n\
2913 -p --string-dump=<number|name>\n\
2914 Dump the contents of section <number|name> as strings\n\
2915 -R --relocated-dump=<number|name>\n\
2916 Dump the contents of section <number|name> as relocated bytes\n\
2917 -w[lLiaprmfFsoR] or\n\
2918 --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=str,=loc,=Ranges]\n\
2919 Display the contents of DWARF2 debug sections\n"));
2920 #ifdef SUPPORT_DISASSEMBLY
2921 fprintf (stream, _("\
2922 -i --instruction-dump=<number|name>\n\
2923 Disassemble the contents of section <number|name>\n"));
2924 #endif
2925 fprintf (stream, _("\
2926 -I --histogram Display histogram of bucket list lengths\n\
2927 -W --wide Allow output width to exceed 80 characters\n\
2928 @<file> Read options from <file>\n\
2929 -H --help Display this information\n\
2930 -v --version Display the version number of readelf\n"));
2932 if (REPORT_BUGS_TO[0] && stream == stdout)
2933 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
2935 exit (stream == stdout ? 0 : 1);
2938 /* Record the fact that the user wants the contents of section number
2939 SECTION to be displayed using the method(s) encoded as flags bits
2940 in TYPE. Note, TYPE can be zero if we are creating the array for
2941 the first time. */
2943 static void
2944 request_dump_bynumber (unsigned int section, dump_type type)
2946 if (section >= num_dump_sects)
2948 dump_type * new_dump_sects;
2950 new_dump_sects = (dump_type *) calloc (section + 1,
2951 sizeof (* dump_sects));
2953 if (new_dump_sects == NULL)
2954 error (_("Out of memory allocating dump request table.\n"));
2955 else
2957 /* Copy current flag settings. */
2958 memcpy (new_dump_sects, dump_sects, num_dump_sects * sizeof (* dump_sects));
2960 free (dump_sects);
2962 dump_sects = new_dump_sects;
2963 num_dump_sects = section + 1;
2967 if (dump_sects)
2968 dump_sects[section] |= type;
2970 return;
2973 /* Request a dump by section name. */
2975 static void
2976 request_dump_byname (const char * section, dump_type type)
2978 struct dump_list_entry * new_request;
2980 new_request = (struct dump_list_entry *)
2981 malloc (sizeof (struct dump_list_entry));
2982 if (!new_request)
2983 error (_("Out of memory allocating dump request table.\n"));
2985 new_request->name = strdup (section);
2986 if (!new_request->name)
2987 error (_("Out of memory allocating dump request table.\n"));
2989 new_request->type = type;
2991 new_request->next = dump_sects_byname;
2992 dump_sects_byname = new_request;
2995 static inline void
2996 request_dump (dump_type type)
2998 int section;
2999 char * cp;
3001 do_dump++;
3002 section = strtoul (optarg, & cp, 0);
3004 if (! *cp && section >= 0)
3005 request_dump_bynumber (section, type);
3006 else
3007 request_dump_byname (optarg, type);
3011 static void
3012 parse_args (int argc, char ** argv)
3014 int c;
3016 if (argc < 2)
3017 usage (stderr);
3019 while ((c = getopt_long
3020 (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:", options, NULL)) != EOF)
3022 switch (c)
3024 case 0:
3025 /* Long options. */
3026 break;
3027 case 'H':
3028 usage (stdout);
3029 break;
3031 case 'a':
3032 do_syms++;
3033 do_reloc++;
3034 do_unwind++;
3035 do_dynamic++;
3036 do_header++;
3037 do_sections++;
3038 do_section_groups++;
3039 do_segments++;
3040 do_version++;
3041 do_histogram++;
3042 do_arch++;
3043 do_notes++;
3044 break;
3045 case 'g':
3046 do_section_groups++;
3047 break;
3048 case 't':
3049 case 'N':
3050 do_sections++;
3051 do_section_details++;
3052 break;
3053 case 'e':
3054 do_header++;
3055 do_sections++;
3056 do_segments++;
3057 break;
3058 case 'A':
3059 do_arch++;
3060 break;
3061 case 'D':
3062 do_using_dynamic++;
3063 break;
3064 case 'r':
3065 do_reloc++;
3066 break;
3067 case 'u':
3068 do_unwind++;
3069 break;
3070 case 'h':
3071 do_header++;
3072 break;
3073 case 'l':
3074 do_segments++;
3075 break;
3076 case 's':
3077 do_syms++;
3078 break;
3079 case 'S':
3080 do_sections++;
3081 break;
3082 case 'd':
3083 do_dynamic++;
3084 break;
3085 case 'I':
3086 do_histogram++;
3087 break;
3088 case 'n':
3089 do_notes++;
3090 break;
3091 case 'c':
3092 do_archive_index++;
3093 break;
3094 case 'x':
3095 request_dump (HEX_DUMP);
3096 break;
3097 case 'p':
3098 request_dump (STRING_DUMP);
3099 break;
3100 case 'R':
3101 request_dump (RELOC_DUMP);
3102 break;
3103 case 'w':
3104 do_dump++;
3105 if (optarg == 0)
3107 do_debugging = 1;
3108 dwarf_select_sections_all ();
3110 else
3112 do_debugging = 0;
3113 dwarf_select_sections_by_letters (optarg);
3115 break;
3116 case OPTION_DEBUG_DUMP:
3117 do_dump++;
3118 if (optarg == 0)
3119 do_debugging = 1;
3120 else
3122 do_debugging = 0;
3123 dwarf_select_sections_by_names (optarg);
3125 break;
3126 #ifdef SUPPORT_DISASSEMBLY
3127 case 'i':
3128 request_dump (DISASS_DUMP);
3129 break;
3130 #endif
3131 case 'v':
3132 print_version (program_name);
3133 break;
3134 case 'V':
3135 do_version++;
3136 break;
3137 case 'W':
3138 do_wide++;
3139 break;
3140 default:
3141 /* xgettext:c-format */
3142 error (_("Invalid option '-%c'\n"), c);
3143 /* Drop through. */
3144 case '?':
3145 usage (stderr);
3149 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
3150 && !do_segments && !do_header && !do_dump && !do_version
3151 && !do_histogram && !do_debugging && !do_arch && !do_notes
3152 && !do_section_groups && !do_archive_index)
3153 usage (stderr);
3154 else if (argc < 3)
3156 warn (_("Nothing to do.\n"));
3157 usage (stderr);
3161 static const char *
3162 get_elf_class (unsigned int elf_class)
3164 static char buff[32];
3166 switch (elf_class)
3168 case ELFCLASSNONE: return _("none");
3169 case ELFCLASS32: return "ELF32";
3170 case ELFCLASS64: return "ELF64";
3171 default:
3172 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
3173 return buff;
3177 static const char *
3178 get_data_encoding (unsigned int encoding)
3180 static char buff[32];
3182 switch (encoding)
3184 case ELFDATANONE: return _("none");
3185 case ELFDATA2LSB: return _("2's complement, little endian");
3186 case ELFDATA2MSB: return _("2's complement, big endian");
3187 default:
3188 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
3189 return buff;
3193 /* Decode the data held in 'elf_header'. */
3195 static int
3196 process_file_header (void)
3198 if ( elf_header.e_ident[EI_MAG0] != ELFMAG0
3199 || elf_header.e_ident[EI_MAG1] != ELFMAG1
3200 || elf_header.e_ident[EI_MAG2] != ELFMAG2
3201 || elf_header.e_ident[EI_MAG3] != ELFMAG3)
3203 error
3204 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
3205 return 0;
3208 init_dwarf_regnames (elf_header.e_machine);
3210 if (do_header)
3212 int i;
3214 printf (_("ELF Header:\n"));
3215 printf (_(" Magic: "));
3216 for (i = 0; i < EI_NIDENT; i++)
3217 printf ("%2.2x ", elf_header.e_ident[i]);
3218 printf ("\n");
3219 printf (_(" Class: %s\n"),
3220 get_elf_class (elf_header.e_ident[EI_CLASS]));
3221 printf (_(" Data: %s\n"),
3222 get_data_encoding (elf_header.e_ident[EI_DATA]));
3223 printf (_(" Version: %d %s\n"),
3224 elf_header.e_ident[EI_VERSION],
3225 (elf_header.e_ident[EI_VERSION] == EV_CURRENT
3226 ? "(current)"
3227 : (elf_header.e_ident[EI_VERSION] != EV_NONE
3228 ? "<unknown: %lx>"
3229 : "")));
3230 printf (_(" OS/ABI: %s\n"),
3231 get_osabi_name (elf_header.e_ident[EI_OSABI]));
3232 printf (_(" ABI Version: %d\n"),
3233 elf_header.e_ident[EI_ABIVERSION]);
3234 printf (_(" Type: %s\n"),
3235 get_file_type (elf_header.e_type));
3236 printf (_(" Machine: %s\n"),
3237 get_machine_name (elf_header.e_machine));
3238 printf (_(" Version: 0x%lx\n"),
3239 (unsigned long) elf_header.e_version);
3241 printf (_(" Entry point address: "));
3242 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3243 printf (_("\n Start of program headers: "));
3244 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3245 printf (_(" (bytes into file)\n Start of section headers: "));
3246 print_vma ((bfd_vma) elf_header.e_shoff, DEC);
3247 printf (_(" (bytes into file)\n"));
3249 printf (_(" Flags: 0x%lx%s\n"),
3250 (unsigned long) elf_header.e_flags,
3251 get_machine_flags (elf_header.e_flags, elf_header.e_machine));
3252 printf (_(" Size of this header: %ld (bytes)\n"),
3253 (long) elf_header.e_ehsize);
3254 printf (_(" Size of program headers: %ld (bytes)\n"),
3255 (long) elf_header.e_phentsize);
3256 printf (_(" Number of program headers: %ld\n"),
3257 (long) elf_header.e_phnum);
3258 printf (_(" Size of section headers: %ld (bytes)\n"),
3259 (long) elf_header.e_shentsize);
3260 printf (_(" Number of section headers: %ld"),
3261 (long) elf_header.e_shnum);
3262 if (section_headers != NULL && elf_header.e_shnum == SHN_UNDEF)
3263 printf (" (%ld)", (long) section_headers[0].sh_size);
3264 putc ('\n', stdout);
3265 printf (_(" Section header string table index: %ld"),
3266 (long) elf_header.e_shstrndx);
3267 if (section_headers != NULL
3268 && elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
3269 printf (" (%u)", section_headers[0].sh_link);
3270 else if (elf_header.e_shstrndx != SHN_UNDEF
3271 && elf_header.e_shstrndx >= elf_header.e_shnum)
3272 printf (" <corrupt: out of range>");
3273 putc ('\n', stdout);
3276 if (section_headers != NULL)
3278 if (elf_header.e_shnum == SHN_UNDEF)
3279 elf_header.e_shnum = section_headers[0].sh_size;
3280 if (elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
3281 elf_header.e_shstrndx = section_headers[0].sh_link;
3282 else if (elf_header.e_shstrndx >= elf_header.e_shnum)
3283 elf_header.e_shstrndx = SHN_UNDEF;
3284 free (section_headers);
3285 section_headers = NULL;
3288 return 1;
3292 static int
3293 get_32bit_program_headers (FILE * file, Elf_Internal_Phdr * program_headers)
3295 Elf32_External_Phdr * phdrs;
3296 Elf32_External_Phdr * external;
3297 Elf_Internal_Phdr * internal;
3298 unsigned int i;
3300 phdrs = (Elf32_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
3301 elf_header.e_phentsize,
3302 elf_header.e_phnum,
3303 _("program headers"));
3304 if (!phdrs)
3305 return 0;
3307 for (i = 0, internal = program_headers, external = phdrs;
3308 i < elf_header.e_phnum;
3309 i++, internal++, external++)
3311 internal->p_type = BYTE_GET (external->p_type);
3312 internal->p_offset = BYTE_GET (external->p_offset);
3313 internal->p_vaddr = BYTE_GET (external->p_vaddr);
3314 internal->p_paddr = BYTE_GET (external->p_paddr);
3315 internal->p_filesz = BYTE_GET (external->p_filesz);
3316 internal->p_memsz = BYTE_GET (external->p_memsz);
3317 internal->p_flags = BYTE_GET (external->p_flags);
3318 internal->p_align = BYTE_GET (external->p_align);
3321 free (phdrs);
3323 return 1;
3326 static int
3327 get_64bit_program_headers (FILE * file, Elf_Internal_Phdr * program_headers)
3329 Elf64_External_Phdr * phdrs;
3330 Elf64_External_Phdr * external;
3331 Elf_Internal_Phdr * internal;
3332 unsigned int i;
3334 phdrs = (Elf64_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
3335 elf_header.e_phentsize,
3336 elf_header.e_phnum,
3337 _("program headers"));
3338 if (!phdrs)
3339 return 0;
3341 for (i = 0, internal = program_headers, external = phdrs;
3342 i < elf_header.e_phnum;
3343 i++, internal++, external++)
3345 internal->p_type = BYTE_GET (external->p_type);
3346 internal->p_flags = BYTE_GET (external->p_flags);
3347 internal->p_offset = BYTE_GET (external->p_offset);
3348 internal->p_vaddr = BYTE_GET (external->p_vaddr);
3349 internal->p_paddr = BYTE_GET (external->p_paddr);
3350 internal->p_filesz = BYTE_GET (external->p_filesz);
3351 internal->p_memsz = BYTE_GET (external->p_memsz);
3352 internal->p_align = BYTE_GET (external->p_align);
3355 free (phdrs);
3357 return 1;
3360 /* Returns 1 if the program headers were read into `program_headers'. */
3362 static int
3363 get_program_headers (FILE * file)
3365 Elf_Internal_Phdr * phdrs;
3367 /* Check cache of prior read. */
3368 if (program_headers != NULL)
3369 return 1;
3371 phdrs = (Elf_Internal_Phdr *) cmalloc (elf_header.e_phnum,
3372 sizeof (Elf_Internal_Phdr));
3374 if (phdrs == NULL)
3376 error (_("Out of memory\n"));
3377 return 0;
3380 if (is_32bit_elf
3381 ? get_32bit_program_headers (file, phdrs)
3382 : get_64bit_program_headers (file, phdrs))
3384 program_headers = phdrs;
3385 return 1;
3388 free (phdrs);
3389 return 0;
3392 /* Returns 1 if the program headers were loaded. */
3394 static int
3395 process_program_headers (FILE * file)
3397 Elf_Internal_Phdr * segment;
3398 unsigned int i;
3400 if (elf_header.e_phnum == 0)
3402 if (do_segments)
3403 printf (_("\nThere are no program headers in this file.\n"));
3404 return 0;
3407 if (do_segments && !do_header)
3409 printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
3410 printf (_("Entry point "));
3411 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3412 printf (_("\nThere are %d program headers, starting at offset "),
3413 elf_header.e_phnum);
3414 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3415 printf ("\n");
3418 if (! get_program_headers (file))
3419 return 0;
3421 if (do_segments)
3423 if (elf_header.e_phnum > 1)
3424 printf (_("\nProgram Headers:\n"));
3425 else
3426 printf (_("\nProgram Headers:\n"));
3428 if (is_32bit_elf)
3429 printf
3430 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
3431 else if (do_wide)
3432 printf
3433 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
3434 else
3436 printf
3437 (_(" Type Offset VirtAddr PhysAddr\n"));
3438 printf
3439 (_(" FileSiz MemSiz Flags Align\n"));
3443 dynamic_addr = 0;
3444 dynamic_size = 0;
3446 for (i = 0, segment = program_headers;
3447 i < elf_header.e_phnum;
3448 i++, segment++)
3450 if (do_segments)
3452 printf (" %-14.14s ", get_segment_type (segment->p_type));
3454 if (is_32bit_elf)
3456 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3457 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
3458 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
3459 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
3460 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
3461 printf ("%c%c%c ",
3462 (segment->p_flags & PF_R ? 'R' : ' '),
3463 (segment->p_flags & PF_W ? 'W' : ' '),
3464 (segment->p_flags & PF_X ? 'E' : ' '));
3465 printf ("%#lx", (unsigned long) segment->p_align);
3467 else if (do_wide)
3469 if ((unsigned long) segment->p_offset == segment->p_offset)
3470 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3471 else
3473 print_vma (segment->p_offset, FULL_HEX);
3474 putchar (' ');
3477 print_vma (segment->p_vaddr, FULL_HEX);
3478 putchar (' ');
3479 print_vma (segment->p_paddr, FULL_HEX);
3480 putchar (' ');
3482 if ((unsigned long) segment->p_filesz == segment->p_filesz)
3483 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
3484 else
3486 print_vma (segment->p_filesz, FULL_HEX);
3487 putchar (' ');
3490 if ((unsigned long) segment->p_memsz == segment->p_memsz)
3491 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
3492 else
3494 print_vma (segment->p_offset, FULL_HEX);
3497 printf (" %c%c%c ",
3498 (segment->p_flags & PF_R ? 'R' : ' '),
3499 (segment->p_flags & PF_W ? 'W' : ' '),
3500 (segment->p_flags & PF_X ? 'E' : ' '));
3502 if ((unsigned long) segment->p_align == segment->p_align)
3503 printf ("%#lx", (unsigned long) segment->p_align);
3504 else
3506 print_vma (segment->p_align, PREFIX_HEX);
3509 else
3511 print_vma (segment->p_offset, FULL_HEX);
3512 putchar (' ');
3513 print_vma (segment->p_vaddr, FULL_HEX);
3514 putchar (' ');
3515 print_vma (segment->p_paddr, FULL_HEX);
3516 printf ("\n ");
3517 print_vma (segment->p_filesz, FULL_HEX);
3518 putchar (' ');
3519 print_vma (segment->p_memsz, FULL_HEX);
3520 printf (" %c%c%c ",
3521 (segment->p_flags & PF_R ? 'R' : ' '),
3522 (segment->p_flags & PF_W ? 'W' : ' '),
3523 (segment->p_flags & PF_X ? 'E' : ' '));
3524 print_vma (segment->p_align, HEX);
3528 switch (segment->p_type)
3530 case PT_DYNAMIC:
3531 if (dynamic_addr)
3532 error (_("more than one dynamic segment\n"));
3534 /* By default, assume that the .dynamic section is the first
3535 section in the DYNAMIC segment. */
3536 dynamic_addr = segment->p_offset;
3537 dynamic_size = segment->p_filesz;
3539 /* Try to locate the .dynamic section. If there is
3540 a section header table, we can easily locate it. */
3541 if (section_headers != NULL)
3543 Elf_Internal_Shdr * sec;
3545 sec = find_section (".dynamic");
3546 if (sec == NULL || sec->sh_size == 0)
3548 error (_("no .dynamic section in the dynamic segment\n"));
3549 break;
3552 if (sec->sh_type == SHT_NOBITS)
3554 dynamic_size = 0;
3555 break;
3558 dynamic_addr = sec->sh_offset;
3559 dynamic_size = sec->sh_size;
3561 if (dynamic_addr < segment->p_offset
3562 || dynamic_addr > segment->p_offset + segment->p_filesz)
3563 warn (_("the .dynamic section is not contained"
3564 " within the dynamic segment\n"));
3565 else if (dynamic_addr > segment->p_offset)
3566 warn (_("the .dynamic section is not the first section"
3567 " in the dynamic segment.\n"));
3569 break;
3571 case PT_INTERP:
3572 if (fseek (file, archive_file_offset + (long) segment->p_offset,
3573 SEEK_SET))
3574 error (_("Unable to find program interpreter name\n"));
3575 else
3577 char fmt [32];
3578 int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX);
3580 if (ret >= (int) sizeof (fmt) || ret < 0)
3581 error (_("Internal error: failed to create format string to display program interpreter\n"));
3583 program_interpreter[0] = 0;
3584 if (fscanf (file, fmt, program_interpreter) <= 0)
3585 error (_("Unable to read program interpreter name\n"));
3587 if (do_segments)
3588 printf (_("\n [Requesting program interpreter: %s]"),
3589 program_interpreter);
3591 break;
3594 if (do_segments)
3595 putc ('\n', stdout);
3598 if (do_segments && section_headers != NULL && string_table != NULL)
3600 printf (_("\n Section to Segment mapping:\n"));
3601 printf (_(" Segment Sections...\n"));
3603 for (i = 0; i < elf_header.e_phnum; i++)
3605 unsigned int j;
3606 Elf_Internal_Shdr * section;
3608 segment = program_headers + i;
3609 section = section_headers + 1;
3611 printf (" %2.2d ", i);
3613 for (j = 1; j < elf_header.e_shnum; j++, section++)
3615 if (ELF_IS_SECTION_IN_SEGMENT_MEMORY (section, segment))
3616 printf ("%s ", SECTION_NAME (section));
3619 putc ('\n',stdout);
3623 return 1;
3627 /* Find the file offset corresponding to VMA by using the program headers. */
3629 static long
3630 offset_from_vma (FILE * file, bfd_vma vma, bfd_size_type size)
3632 Elf_Internal_Phdr * seg;
3634 if (! get_program_headers (file))
3636 warn (_("Cannot interpret virtual addresses without program headers.\n"));
3637 return (long) vma;
3640 for (seg = program_headers;
3641 seg < program_headers + elf_header.e_phnum;
3642 ++seg)
3644 if (seg->p_type != PT_LOAD)
3645 continue;
3647 if (vma >= (seg->p_vaddr & -seg->p_align)
3648 && vma + size <= seg->p_vaddr + seg->p_filesz)
3649 return vma - seg->p_vaddr + seg->p_offset;
3652 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
3653 (unsigned long) vma);
3654 return (long) vma;
3658 static int
3659 get_32bit_section_headers (FILE * file, unsigned int num)
3661 Elf32_External_Shdr * shdrs;
3662 Elf_Internal_Shdr * internal;
3663 unsigned int i;
3665 shdrs = (Elf32_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
3666 elf_header.e_shentsize, num,
3667 _("section headers"));
3668 if (!shdrs)
3669 return 0;
3671 section_headers = (Elf_Internal_Shdr *) cmalloc (num,
3672 sizeof (Elf_Internal_Shdr));
3674 if (section_headers == NULL)
3676 error (_("Out of memory\n"));
3677 return 0;
3680 for (i = 0, internal = section_headers;
3681 i < num;
3682 i++, internal++)
3684 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3685 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3686 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
3687 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
3688 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3689 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
3690 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3691 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3692 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3693 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
3696 free (shdrs);
3698 return 1;
3701 static int
3702 get_64bit_section_headers (FILE * file, unsigned int num)
3704 Elf64_External_Shdr * shdrs;
3705 Elf_Internal_Shdr * internal;
3706 unsigned int i;
3708 shdrs = (Elf64_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
3709 elf_header.e_shentsize, num,
3710 _("section headers"));
3711 if (!shdrs)
3712 return 0;
3714 section_headers = (Elf_Internal_Shdr *) cmalloc (num,
3715 sizeof (Elf_Internal_Shdr));
3717 if (section_headers == NULL)
3719 error (_("Out of memory\n"));
3720 return 0;
3723 for (i = 0, internal = section_headers;
3724 i < num;
3725 i++, internal++)
3727 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3728 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3729 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
3730 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
3731 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
3732 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
3733 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3734 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3735 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3736 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3739 free (shdrs);
3741 return 1;
3744 static Elf_Internal_Sym *
3745 get_32bit_elf_symbols (FILE * file, Elf_Internal_Shdr * section)
3747 unsigned long number;
3748 Elf32_External_Sym * esyms;
3749 Elf_External_Sym_Shndx * shndx;
3750 Elf_Internal_Sym * isyms;
3751 Elf_Internal_Sym * psym;
3752 unsigned int j;
3754 esyms = (Elf32_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
3755 section->sh_size, _("symbols"));
3756 if (!esyms)
3757 return NULL;
3759 shndx = NULL;
3760 if (symtab_shndx_hdr != NULL
3761 && (symtab_shndx_hdr->sh_link
3762 == (unsigned long) (section - section_headers)))
3764 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
3765 symtab_shndx_hdr->sh_offset,
3766 1, symtab_shndx_hdr->sh_size,
3767 _("symtab shndx"));
3768 if (!shndx)
3770 free (esyms);
3771 return NULL;
3775 number = section->sh_size / section->sh_entsize;
3776 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
3778 if (isyms == NULL)
3780 error (_("Out of memory\n"));
3781 if (shndx)
3782 free (shndx);
3783 free (esyms);
3784 return NULL;
3787 for (j = 0, psym = isyms;
3788 j < number;
3789 j++, psym++)
3791 psym->st_name = BYTE_GET (esyms[j].st_name);
3792 psym->st_value = BYTE_GET (esyms[j].st_value);
3793 psym->st_size = BYTE_GET (esyms[j].st_size);
3794 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3795 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
3796 psym->st_shndx
3797 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3798 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
3799 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
3800 psym->st_info = BYTE_GET (esyms[j].st_info);
3801 psym->st_other = BYTE_GET (esyms[j].st_other);
3804 if (shndx)
3805 free (shndx);
3806 free (esyms);
3808 return isyms;
3811 static Elf_Internal_Sym *
3812 get_64bit_elf_symbols (FILE * file, Elf_Internal_Shdr * section)
3814 unsigned long number;
3815 Elf64_External_Sym * esyms;
3816 Elf_External_Sym_Shndx * shndx;
3817 Elf_Internal_Sym * isyms;
3818 Elf_Internal_Sym * psym;
3819 unsigned int j;
3821 esyms = (Elf64_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
3822 section->sh_size, _("symbols"));
3823 if (!esyms)
3824 return NULL;
3826 shndx = NULL;
3827 if (symtab_shndx_hdr != NULL
3828 && (symtab_shndx_hdr->sh_link
3829 == (unsigned long) (section - section_headers)))
3831 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
3832 symtab_shndx_hdr->sh_offset,
3833 1, symtab_shndx_hdr->sh_size,
3834 _("symtab shndx"));
3835 if (!shndx)
3837 free (esyms);
3838 return NULL;
3842 number = section->sh_size / section->sh_entsize;
3843 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
3845 if (isyms == NULL)
3847 error (_("Out of memory\n"));
3848 if (shndx)
3849 free (shndx);
3850 free (esyms);
3851 return NULL;
3854 for (j = 0, psym = isyms;
3855 j < number;
3856 j++, psym++)
3858 psym->st_name = BYTE_GET (esyms[j].st_name);
3859 psym->st_info = BYTE_GET (esyms[j].st_info);
3860 psym->st_other = BYTE_GET (esyms[j].st_other);
3861 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3862 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
3863 psym->st_shndx
3864 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3865 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
3866 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
3867 psym->st_value = BYTE_GET (esyms[j].st_value);
3868 psym->st_size = BYTE_GET (esyms[j].st_size);
3871 if (shndx)
3872 free (shndx);
3873 free (esyms);
3875 return isyms;
3878 static const char *
3879 get_elf_section_flags (bfd_vma sh_flags)
3881 static char buff[1024];
3882 char * p = buff;
3883 int field_size = is_32bit_elf ? 8 : 16;
3884 int index, size = sizeof (buff) - (field_size + 4 + 1);
3885 bfd_vma os_flags = 0;
3886 bfd_vma proc_flags = 0;
3887 bfd_vma unknown_flags = 0;
3888 static const struct
3890 const char * str;
3891 int len;
3893 flags [] =
3895 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
3896 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
3897 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
3898 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
3899 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
3900 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
3901 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
3902 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
3903 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
3904 /* 9 */ { STRING_COMMA_LEN ("TLS") },
3905 /* IA-64 specific. */
3906 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
3907 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
3908 /* IA-64 OpenVMS specific. */
3909 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
3910 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
3911 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
3912 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
3913 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
3914 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
3915 /* SPARC specific. */
3916 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
3917 /* 19 */ { STRING_COMMA_LEN ("ORDERED") }
3920 if (do_section_details)
3922 sprintf (buff, "[%*.*lx]: ",
3923 field_size, field_size, (unsigned long) sh_flags);
3924 p += field_size + 4;
3927 while (sh_flags)
3929 bfd_vma flag;
3931 flag = sh_flags & - sh_flags;
3932 sh_flags &= ~ flag;
3934 if (do_section_details)
3936 switch (flag)
3938 case SHF_WRITE: index = 0; break;
3939 case SHF_ALLOC: index = 1; break;
3940 case SHF_EXECINSTR: index = 2; break;
3941 case SHF_MERGE: index = 3; break;
3942 case SHF_STRINGS: index = 4; break;
3943 case SHF_INFO_LINK: index = 5; break;
3944 case SHF_LINK_ORDER: index = 6; break;
3945 case SHF_OS_NONCONFORMING: index = 7; break;
3946 case SHF_GROUP: index = 8; break;
3947 case SHF_TLS: index = 9; break;
3949 default:
3950 index = -1;
3951 switch (elf_header.e_machine)
3953 case EM_IA_64:
3954 if (flag == SHF_IA_64_SHORT)
3955 index = 10;
3956 else if (flag == SHF_IA_64_NORECOV)
3957 index = 11;
3958 #ifdef BFD64
3959 else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3960 switch (flag)
3962 case SHF_IA_64_VMS_GLOBAL: index = 12; break;
3963 case SHF_IA_64_VMS_OVERLAID: index = 13; break;
3964 case SHF_IA_64_VMS_SHARED: index = 14; break;
3965 case SHF_IA_64_VMS_VECTOR: index = 15; break;
3966 case SHF_IA_64_VMS_ALLOC_64BIT: index = 16; break;
3967 case SHF_IA_64_VMS_PROTECTED: index = 17; break;
3968 default: break;
3970 #endif
3971 break;
3973 case EM_OLD_SPARCV9:
3974 case EM_SPARC32PLUS:
3975 case EM_SPARCV9:
3976 case EM_SPARC:
3977 if (flag == SHF_EXCLUDE)
3978 index = 18;
3979 else if (flag == SHF_ORDERED)
3980 index = 19;
3981 break;
3982 default:
3983 break;
3987 if (index != -1)
3989 if (p != buff + field_size + 4)
3991 if (size < (10 + 2))
3992 abort ();
3993 size -= 2;
3994 *p++ = ',';
3995 *p++ = ' ';
3998 size -= flags [index].len;
3999 p = stpcpy (p, flags [index].str);
4001 else if (flag & SHF_MASKOS)
4002 os_flags |= flag;
4003 else if (flag & SHF_MASKPROC)
4004 proc_flags |= flag;
4005 else
4006 unknown_flags |= flag;
4008 else
4010 switch (flag)
4012 case SHF_WRITE: *p = 'W'; break;
4013 case SHF_ALLOC: *p = 'A'; break;
4014 case SHF_EXECINSTR: *p = 'X'; break;
4015 case SHF_MERGE: *p = 'M'; break;
4016 case SHF_STRINGS: *p = 'S'; break;
4017 case SHF_INFO_LINK: *p = 'I'; break;
4018 case SHF_LINK_ORDER: *p = 'L'; break;
4019 case SHF_OS_NONCONFORMING: *p = 'O'; break;
4020 case SHF_GROUP: *p = 'G'; break;
4021 case SHF_TLS: *p = 'T'; break;
4023 default:
4024 if ((elf_header.e_machine == EM_X86_64
4025 || elf_header.e_machine == EM_L1OM)
4026 && flag == SHF_X86_64_LARGE)
4027 *p = 'l';
4028 else if (flag & SHF_MASKOS)
4030 *p = 'o';
4031 sh_flags &= ~ SHF_MASKOS;
4033 else if (flag & SHF_MASKPROC)
4035 *p = 'p';
4036 sh_flags &= ~ SHF_MASKPROC;
4038 else
4039 *p = 'x';
4040 break;
4042 p++;
4046 if (do_section_details)
4048 if (os_flags)
4050 size -= 5 + field_size;
4051 if (p != buff + field_size + 4)
4053 if (size < (2 + 1))
4054 abort ();
4055 size -= 2;
4056 *p++ = ',';
4057 *p++ = ' ';
4059 sprintf (p, "OS (%*.*lx)", field_size, field_size,
4060 (unsigned long) os_flags);
4061 p += 5 + field_size;
4063 if (proc_flags)
4065 size -= 7 + field_size;
4066 if (p != buff + field_size + 4)
4068 if (size < (2 + 1))
4069 abort ();
4070 size -= 2;
4071 *p++ = ',';
4072 *p++ = ' ';
4074 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
4075 (unsigned long) proc_flags);
4076 p += 7 + field_size;
4078 if (unknown_flags)
4080 size -= 10 + field_size;
4081 if (p != buff + field_size + 4)
4083 if (size < (2 + 1))
4084 abort ();
4085 size -= 2;
4086 *p++ = ',';
4087 *p++ = ' ';
4089 sprintf (p, "UNKNOWN (%*.*lx)", field_size, field_size,
4090 (unsigned long) unknown_flags);
4091 p += 10 + field_size;
4095 *p = '\0';
4096 return buff;
4099 static int
4100 process_section_headers (FILE * file)
4102 Elf_Internal_Shdr * section;
4103 unsigned int i;
4105 section_headers = NULL;
4107 if (elf_header.e_shnum == 0)
4109 if (do_sections)
4110 printf (_("\nThere are no sections in this file.\n"));
4112 return 1;
4115 if (do_sections && !do_header)
4116 printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
4117 elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
4119 if (is_32bit_elf)
4121 if (! get_32bit_section_headers (file, elf_header.e_shnum))
4122 return 0;
4124 else if (! get_64bit_section_headers (file, elf_header.e_shnum))
4125 return 0;
4127 /* Read in the string table, so that we have names to display. */
4128 if (elf_header.e_shstrndx != SHN_UNDEF
4129 && elf_header.e_shstrndx < elf_header.e_shnum)
4131 section = section_headers + elf_header.e_shstrndx;
4133 if (section->sh_size != 0)
4135 string_table = (char *) get_data (NULL, file, section->sh_offset,
4136 1, section->sh_size,
4137 _("string table"));
4139 string_table_length = string_table != NULL ? section->sh_size : 0;
4143 /* Scan the sections for the dynamic symbol table
4144 and dynamic string table and debug sections. */
4145 dynamic_symbols = NULL;
4146 dynamic_strings = NULL;
4147 dynamic_syminfo = NULL;
4148 symtab_shndx_hdr = NULL;
4150 eh_addr_size = is_32bit_elf ? 4 : 8;
4151 switch (elf_header.e_machine)
4153 case EM_MIPS:
4154 case EM_MIPS_RS3_LE:
4155 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
4156 FDE addresses. However, the ABI also has a semi-official ILP32
4157 variant for which the normal FDE address size rules apply.
4159 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
4160 section, where XX is the size of longs in bits. Unfortunately,
4161 earlier compilers provided no way of distinguishing ILP32 objects
4162 from LP64 objects, so if there's any doubt, we should assume that
4163 the official LP64 form is being used. */
4164 if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
4165 && find_section (".gcc_compiled_long32") == NULL)
4166 eh_addr_size = 8;
4167 break;
4169 case EM_H8_300:
4170 case EM_H8_300H:
4171 switch (elf_header.e_flags & EF_H8_MACH)
4173 case E_H8_MACH_H8300:
4174 case E_H8_MACH_H8300HN:
4175 case E_H8_MACH_H8300SN:
4176 case E_H8_MACH_H8300SXN:
4177 eh_addr_size = 2;
4178 break;
4179 case E_H8_MACH_H8300H:
4180 case E_H8_MACH_H8300S:
4181 case E_H8_MACH_H8300SX:
4182 eh_addr_size = 4;
4183 break;
4185 break;
4187 case EM_M32C_OLD:
4188 case EM_M32C:
4189 switch (elf_header.e_flags & EF_M32C_CPU_MASK)
4191 case EF_M32C_CPU_M16C:
4192 eh_addr_size = 2;
4193 break;
4195 break;
4198 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
4199 do \
4201 size_t expected_entsize \
4202 = is_32bit_elf ? size32 : size64; \
4203 if (section->sh_entsize != expected_entsize) \
4204 error (_("Section %d has invalid sh_entsize %lx (expected %lx)\n"), \
4205 i, (unsigned long int) section->sh_entsize, \
4206 (unsigned long int) expected_entsize); \
4207 section->sh_entsize = expected_entsize; \
4209 while (0)
4210 #define CHECK_ENTSIZE(section, i, type) \
4211 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
4212 sizeof (Elf64_External_##type))
4214 for (i = 0, section = section_headers;
4215 i < elf_header.e_shnum;
4216 i++, section++)
4218 char * name = SECTION_NAME (section);
4220 if (section->sh_type == SHT_DYNSYM)
4222 if (dynamic_symbols != NULL)
4224 error (_("File contains multiple dynamic symbol tables\n"));
4225 continue;
4228 CHECK_ENTSIZE (section, i, Sym);
4229 num_dynamic_syms = section->sh_size / section->sh_entsize;
4230 dynamic_symbols = GET_ELF_SYMBOLS (file, section);
4232 else if (section->sh_type == SHT_STRTAB
4233 && streq (name, ".dynstr"))
4235 if (dynamic_strings != NULL)
4237 error (_("File contains multiple dynamic string tables\n"));
4238 continue;
4241 dynamic_strings = (char *) get_data (NULL, file, section->sh_offset,
4242 1, section->sh_size,
4243 _("dynamic strings"));
4244 dynamic_strings_length = section->sh_size;
4246 else if (section->sh_type == SHT_SYMTAB_SHNDX)
4248 if (symtab_shndx_hdr != NULL)
4250 error (_("File contains multiple symtab shndx tables\n"));
4251 continue;
4253 symtab_shndx_hdr = section;
4255 else if (section->sh_type == SHT_SYMTAB)
4256 CHECK_ENTSIZE (section, i, Sym);
4257 else if (section->sh_type == SHT_GROUP)
4258 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
4259 else if (section->sh_type == SHT_REL)
4260 CHECK_ENTSIZE (section, i, Rel);
4261 else if (section->sh_type == SHT_RELA)
4262 CHECK_ENTSIZE (section, i, Rela);
4263 else if ((do_debugging || do_debug_info || do_debug_abbrevs
4264 || do_debug_lines || do_debug_pubnames
4265 || do_debug_aranges || do_debug_frames || do_debug_macinfo
4266 || do_debug_str || do_debug_loc || do_debug_ranges)
4267 && (const_strneq (name, ".debug_")
4268 || const_strneq (name, ".zdebug_")))
4270 if (name[1] == 'z')
4271 name += sizeof (".zdebug_") - 1;
4272 else
4273 name += sizeof (".debug_") - 1;
4275 if (do_debugging
4276 || (do_debug_info && streq (name, "info"))
4277 || (do_debug_abbrevs && streq (name, "abbrev"))
4278 || (do_debug_lines && streq (name, "line"))
4279 || (do_debug_pubnames && streq (name, "pubnames"))
4280 || (do_debug_aranges && streq (name, "aranges"))
4281 || (do_debug_ranges && streq (name, "ranges"))
4282 || (do_debug_frames && streq (name, "frame"))
4283 || (do_debug_macinfo && streq (name, "macinfo"))
4284 || (do_debug_str && streq (name, "str"))
4285 || (do_debug_loc && streq (name, "loc"))
4287 request_dump_bynumber (i, DEBUG_DUMP);
4289 /* Linkonce section to be combined with .debug_info at link time. */
4290 else if ((do_debugging || do_debug_info)
4291 && const_strneq (name, ".gnu.linkonce.wi."))
4292 request_dump_bynumber (i, DEBUG_DUMP);
4293 else if (do_debug_frames && streq (name, ".eh_frame"))
4294 request_dump_bynumber (i, DEBUG_DUMP);
4297 if (! do_sections)
4298 return 1;
4300 if (elf_header.e_shnum > 1)
4301 printf (_("\nSection Headers:\n"));
4302 else
4303 printf (_("\nSection Header:\n"));
4305 if (is_32bit_elf)
4307 if (do_section_details)
4309 printf (_(" [Nr] Name\n"));
4310 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
4312 else
4313 printf
4314 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
4316 else if (do_wide)
4318 if (do_section_details)
4320 printf (_(" [Nr] Name\n"));
4321 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
4323 else
4324 printf
4325 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
4327 else
4329 if (do_section_details)
4331 printf (_(" [Nr] Name\n"));
4332 printf (_(" Type Address Offset Link\n"));
4333 printf (_(" Size EntSize Info Align\n"));
4335 else
4337 printf (_(" [Nr] Name Type Address Offset\n"));
4338 printf (_(" Size EntSize Flags Link Info Align\n"));
4342 if (do_section_details)
4343 printf (_(" Flags\n"));
4345 for (i = 0, section = section_headers;
4346 i < elf_header.e_shnum;
4347 i++, section++)
4349 if (do_section_details)
4351 printf (" [%2u] %s\n",
4353 SECTION_NAME (section));
4354 if (is_32bit_elf || do_wide)
4355 printf (" %-15.15s ",
4356 get_section_type_name (section->sh_type));
4358 else
4359 printf ((do_wide ? " [%2u] %-17s %-15s "
4360 : " [%2u] %-17.17s %-15.15s "),
4362 SECTION_NAME (section),
4363 get_section_type_name (section->sh_type));
4365 if (is_32bit_elf)
4367 const char * link_too_big = NULL;
4369 print_vma (section->sh_addr, LONG_HEX);
4371 printf ( " %6.6lx %6.6lx %2.2lx",
4372 (unsigned long) section->sh_offset,
4373 (unsigned long) section->sh_size,
4374 (unsigned long) section->sh_entsize);
4376 if (do_section_details)
4377 fputs (" ", stdout);
4378 else
4379 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4381 if (section->sh_link >= elf_header.e_shnum)
4383 link_too_big = "";
4384 /* The sh_link value is out of range. Normally this indicates
4385 an error but it can have special values in SPARC binaries. */
4386 switch (elf_header.e_machine)
4388 case EM_OLD_SPARCV9:
4389 case EM_SPARC32PLUS:
4390 case EM_SPARCV9:
4391 case EM_SPARC:
4392 if (section->sh_link == (SHN_BEFORE & 0xffff))
4393 link_too_big = "BEFORE";
4394 else if (section->sh_link == (SHN_AFTER & 0xffff))
4395 link_too_big = "AFTER";
4396 break;
4397 default:
4398 break;
4402 if (do_section_details)
4404 if (link_too_big != NULL && * link_too_big)
4405 printf ("<%s> ", link_too_big);
4406 else
4407 printf ("%2u ", section->sh_link);
4408 printf ("%3u %2lu\n", section->sh_info,
4409 (unsigned long) section->sh_addralign);
4411 else
4412 printf ("%2u %3u %2lu\n",
4413 section->sh_link,
4414 section->sh_info,
4415 (unsigned long) section->sh_addralign);
4417 if (link_too_big && ! * link_too_big)
4418 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
4419 i, section->sh_link);
4421 else if (do_wide)
4423 print_vma (section->sh_addr, LONG_HEX);
4425 if ((long) section->sh_offset == section->sh_offset)
4426 printf (" %6.6lx", (unsigned long) section->sh_offset);
4427 else
4429 putchar (' ');
4430 print_vma (section->sh_offset, LONG_HEX);
4433 if ((unsigned long) section->sh_size == section->sh_size)
4434 printf (" %6.6lx", (unsigned long) section->sh_size);
4435 else
4437 putchar (' ');
4438 print_vma (section->sh_size, LONG_HEX);
4441 if ((unsigned long) section->sh_entsize == section->sh_entsize)
4442 printf (" %2.2lx", (unsigned long) section->sh_entsize);
4443 else
4445 putchar (' ');
4446 print_vma (section->sh_entsize, LONG_HEX);
4449 if (do_section_details)
4450 fputs (" ", stdout);
4451 else
4452 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4454 printf ("%2u %3u ", section->sh_link, section->sh_info);
4456 if ((unsigned long) section->sh_addralign == section->sh_addralign)
4457 printf ("%2lu\n", (unsigned long) section->sh_addralign);
4458 else
4460 print_vma (section->sh_addralign, DEC);
4461 putchar ('\n');
4464 else if (do_section_details)
4466 printf (" %-15.15s ",
4467 get_section_type_name (section->sh_type));
4468 print_vma (section->sh_addr, LONG_HEX);
4469 if ((long) section->sh_offset == section->sh_offset)
4470 printf (" %16.16lx", (unsigned long) section->sh_offset);
4471 else
4473 printf (" ");
4474 print_vma (section->sh_offset, LONG_HEX);
4476 printf (" %u\n ", section->sh_link);
4477 print_vma (section->sh_size, LONG_HEX);
4478 putchar (' ');
4479 print_vma (section->sh_entsize, LONG_HEX);
4481 printf (" %-16u %lu\n",
4482 section->sh_info,
4483 (unsigned long) section->sh_addralign);
4485 else
4487 putchar (' ');
4488 print_vma (section->sh_addr, LONG_HEX);
4489 if ((long) section->sh_offset == section->sh_offset)
4490 printf (" %8.8lx", (unsigned long) section->sh_offset);
4491 else
4493 printf (" ");
4494 print_vma (section->sh_offset, LONG_HEX);
4496 printf ("\n ");
4497 print_vma (section->sh_size, LONG_HEX);
4498 printf (" ");
4499 print_vma (section->sh_entsize, LONG_HEX);
4501 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4503 printf (" %2u %3u %lu\n",
4504 section->sh_link,
4505 section->sh_info,
4506 (unsigned long) section->sh_addralign);
4509 if (do_section_details)
4510 printf (" %s\n", get_elf_section_flags (section->sh_flags));
4513 if (!do_section_details)
4514 printf (_("Key to Flags:\n\
4515 W (write), A (alloc), X (execute), M (merge), S (strings)\n\
4516 I (info), L (link order), G (group), x (unknown)\n\
4517 O (extra OS processing required) o (OS specific), p (processor specific)\n"));
4519 return 1;
4522 static const char *
4523 get_group_flags (unsigned int flags)
4525 static char buff[32];
4526 switch (flags)
4528 case GRP_COMDAT:
4529 return "COMDAT";
4531 default:
4532 snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x]"), flags);
4533 break;
4535 return buff;
4538 static int
4539 process_section_groups (FILE * file)
4541 Elf_Internal_Shdr * section;
4542 unsigned int i;
4543 struct group * group;
4544 Elf_Internal_Shdr * symtab_sec;
4545 Elf_Internal_Shdr * strtab_sec;
4546 Elf_Internal_Sym * symtab;
4547 char * strtab;
4548 size_t strtab_size;
4550 /* Don't process section groups unless needed. */
4551 if (!do_unwind && !do_section_groups)
4552 return 1;
4554 if (elf_header.e_shnum == 0)
4556 if (do_section_groups)
4557 printf (_("\nThere are no sections in this file.\n"));
4559 return 1;
4562 if (section_headers == NULL)
4564 error (_("Section headers are not available!\n"));
4565 abort ();
4568 section_headers_groups = (struct group **) calloc (elf_header.e_shnum,
4569 sizeof (struct group *));
4571 if (section_headers_groups == NULL)
4573 error (_("Out of memory\n"));
4574 return 0;
4577 /* Scan the sections for the group section. */
4578 group_count = 0;
4579 for (i = 0, section = section_headers;
4580 i < elf_header.e_shnum;
4581 i++, section++)
4582 if (section->sh_type == SHT_GROUP)
4583 group_count++;
4585 if (group_count == 0)
4587 if (do_section_groups)
4588 printf (_("\nThere are no section groups in this file.\n"));
4590 return 1;
4593 section_groups = (struct group *) calloc (group_count, sizeof (struct group));
4595 if (section_groups == NULL)
4597 error (_("Out of memory\n"));
4598 return 0;
4601 symtab_sec = NULL;
4602 strtab_sec = NULL;
4603 symtab = NULL;
4604 strtab = NULL;
4605 strtab_size = 0;
4606 for (i = 0, section = section_headers, group = section_groups;
4607 i < elf_header.e_shnum;
4608 i++, section++)
4610 if (section->sh_type == SHT_GROUP)
4612 char * name = SECTION_NAME (section);
4613 char * group_name;
4614 unsigned char * start;
4615 unsigned char * indices;
4616 unsigned int entry, j, size;
4617 Elf_Internal_Shdr * sec;
4618 Elf_Internal_Sym * sym;
4620 /* Get the symbol table. */
4621 if (section->sh_link >= elf_header.e_shnum
4622 || ((sec = section_headers + section->sh_link)->sh_type
4623 != SHT_SYMTAB))
4625 error (_("Bad sh_link in group section `%s'\n"), name);
4626 continue;
4629 if (symtab_sec != sec)
4631 symtab_sec = sec;
4632 if (symtab)
4633 free (symtab);
4634 symtab = GET_ELF_SYMBOLS (file, symtab_sec);
4637 sym = symtab + section->sh_info;
4639 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4641 if (sym->st_shndx == 0
4642 || sym->st_shndx >= elf_header.e_shnum)
4644 error (_("Bad sh_info in group section `%s'\n"), name);
4645 continue;
4648 group_name = SECTION_NAME (section_headers + sym->st_shndx);
4649 strtab_sec = NULL;
4650 if (strtab)
4651 free (strtab);
4652 strtab = NULL;
4653 strtab_size = 0;
4655 else
4657 /* Get the string table. */
4658 if (symtab_sec->sh_link >= elf_header.e_shnum)
4660 strtab_sec = NULL;
4661 if (strtab)
4662 free (strtab);
4663 strtab = NULL;
4664 strtab_size = 0;
4666 else if (strtab_sec
4667 != (sec = section_headers + symtab_sec->sh_link))
4669 strtab_sec = sec;
4670 if (strtab)
4671 free (strtab);
4672 strtab = (char *) get_data (NULL, file, strtab_sec->sh_offset,
4673 1, strtab_sec->sh_size,
4674 _("string table"));
4675 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
4677 group_name = sym->st_name < strtab_size
4678 ? strtab + sym->st_name : "<corrupt>";
4681 start = (unsigned char *) get_data (NULL, file, section->sh_offset,
4682 1, section->sh_size,
4683 _("section data"));
4685 indices = start;
4686 size = (section->sh_size / section->sh_entsize) - 1;
4687 entry = byte_get (indices, 4);
4688 indices += 4;
4690 if (do_section_groups)
4692 printf ("\n%s group section [%5u] `%s' [%s] contains %u sections:\n",
4693 get_group_flags (entry), i, name, group_name, size);
4695 printf (_(" [Index] Name\n"));
4698 group->group_index = i;
4700 for (j = 0; j < size; j++)
4702 struct group_list * g;
4704 entry = byte_get (indices, 4);
4705 indices += 4;
4707 if (entry >= elf_header.e_shnum)
4709 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
4710 entry, i, elf_header.e_shnum - 1);
4711 continue;
4714 if (section_headers_groups [entry] != NULL)
4716 if (entry)
4718 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
4719 entry, i,
4720 section_headers_groups [entry]->group_index);
4721 continue;
4723 else
4725 /* Intel C/C++ compiler may put section 0 in a
4726 section group. We just warn it the first time
4727 and ignore it afterwards. */
4728 static int warned = 0;
4729 if (!warned)
4731 error (_("section 0 in group section [%5u]\n"),
4732 section_headers_groups [entry]->group_index);
4733 warned++;
4738 section_headers_groups [entry] = group;
4740 if (do_section_groups)
4742 sec = section_headers + entry;
4743 printf (" [%5u] %s\n", entry, SECTION_NAME (sec));
4746 g = (struct group_list *) xmalloc (sizeof (struct group_list));
4747 g->section_index = entry;
4748 g->next = group->root;
4749 group->root = g;
4752 if (start)
4753 free (start);
4755 group++;
4759 if (symtab)
4760 free (symtab);
4761 if (strtab)
4762 free (strtab);
4763 return 1;
4766 static struct
4768 const char * name;
4769 int reloc;
4770 int size;
4771 int rela;
4772 } dynamic_relocations [] =
4774 { "REL", DT_REL, DT_RELSZ, FALSE },
4775 { "RELA", DT_RELA, DT_RELASZ, TRUE },
4776 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
4779 /* Process the reloc section. */
4781 static int
4782 process_relocs (FILE * file)
4784 unsigned long rel_size;
4785 unsigned long rel_offset;
4788 if (!do_reloc)
4789 return 1;
4791 if (do_using_dynamic)
4793 int is_rela;
4794 const char * name;
4795 int has_dynamic_reloc;
4796 unsigned int i;
4798 has_dynamic_reloc = 0;
4800 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
4802 is_rela = dynamic_relocations [i].rela;
4803 name = dynamic_relocations [i].name;
4804 rel_size = dynamic_info [dynamic_relocations [i].size];
4805 rel_offset = dynamic_info [dynamic_relocations [i].reloc];
4807 has_dynamic_reloc |= rel_size;
4809 if (is_rela == UNKNOWN)
4811 if (dynamic_relocations [i].reloc == DT_JMPREL)
4812 switch (dynamic_info[DT_PLTREL])
4814 case DT_REL:
4815 is_rela = FALSE;
4816 break;
4817 case DT_RELA:
4818 is_rela = TRUE;
4819 break;
4823 if (rel_size)
4825 printf
4826 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
4827 name, rel_offset, rel_size);
4829 dump_relocations (file,
4830 offset_from_vma (file, rel_offset, rel_size),
4831 rel_size,
4832 dynamic_symbols, num_dynamic_syms,
4833 dynamic_strings, dynamic_strings_length, is_rela);
4837 if (! has_dynamic_reloc)
4838 printf (_("\nThere are no dynamic relocations in this file.\n"));
4840 else
4842 Elf_Internal_Shdr * section;
4843 unsigned long i;
4844 int found = 0;
4846 for (i = 0, section = section_headers;
4847 i < elf_header.e_shnum;
4848 i++, section++)
4850 if ( section->sh_type != SHT_RELA
4851 && section->sh_type != SHT_REL)
4852 continue;
4854 rel_offset = section->sh_offset;
4855 rel_size = section->sh_size;
4857 if (rel_size)
4859 Elf_Internal_Shdr * strsec;
4860 int is_rela;
4862 printf (_("\nRelocation section "));
4864 if (string_table == NULL)
4865 printf ("%d", section->sh_name);
4866 else
4867 printf (_("'%s'"), SECTION_NAME (section));
4869 printf (_(" at offset 0x%lx contains %lu entries:\n"),
4870 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
4872 is_rela = section->sh_type == SHT_RELA;
4874 if (section->sh_link != 0
4875 && section->sh_link < elf_header.e_shnum)
4877 Elf_Internal_Shdr * symsec;
4878 Elf_Internal_Sym * symtab;
4879 unsigned long nsyms;
4880 unsigned long strtablen = 0;
4881 char * strtab = NULL;
4883 symsec = section_headers + section->sh_link;
4884 if (symsec->sh_type != SHT_SYMTAB
4885 && symsec->sh_type != SHT_DYNSYM)
4886 continue;
4888 nsyms = symsec->sh_size / symsec->sh_entsize;
4889 symtab = GET_ELF_SYMBOLS (file, symsec);
4891 if (symtab == NULL)
4892 continue;
4894 if (symsec->sh_link != 0
4895 && symsec->sh_link < elf_header.e_shnum)
4897 strsec = section_headers + symsec->sh_link;
4899 strtab = (char *) get_data (NULL, file, strsec->sh_offset,
4900 1, strsec->sh_size,
4901 _("string table"));
4902 strtablen = strtab == NULL ? 0 : strsec->sh_size;
4905 dump_relocations (file, rel_offset, rel_size,
4906 symtab, nsyms, strtab, strtablen, is_rela);
4907 if (strtab)
4908 free (strtab);
4909 free (symtab);
4911 else
4912 dump_relocations (file, rel_offset, rel_size,
4913 NULL, 0, NULL, 0, is_rela);
4915 found = 1;
4919 if (! found)
4920 printf (_("\nThere are no relocations in this file.\n"));
4923 return 1;
4926 /* Process the unwind section. */
4928 #include "unwind-ia64.h"
4930 /* An absolute address consists of a section and an offset. If the
4931 section is NULL, the offset itself is the address, otherwise, the
4932 address equals to LOAD_ADDRESS(section) + offset. */
4934 struct absaddr
4936 unsigned short section;
4937 bfd_vma offset;
4940 #define ABSADDR(a) \
4941 ((a).section \
4942 ? section_headers [(a).section].sh_addr + (a).offset \
4943 : (a).offset)
4945 struct ia64_unw_table_entry
4947 struct absaddr start;
4948 struct absaddr end;
4949 struct absaddr info;
4952 struct ia64_unw_aux_info
4955 struct ia64_unw_table_entry *table; /* Unwind table. */
4956 unsigned long table_len; /* Length of unwind table. */
4957 unsigned char * info; /* Unwind info. */
4958 unsigned long info_size; /* Size of unwind info. */
4959 bfd_vma info_addr; /* starting address of unwind info. */
4960 bfd_vma seg_base; /* Starting address of segment. */
4961 Elf_Internal_Sym * symtab; /* The symbol table. */
4962 unsigned long nsyms; /* Number of symbols. */
4963 char * strtab; /* The string table. */
4964 unsigned long strtab_size; /* Size of string table. */
4967 static void
4968 find_symbol_for_address (Elf_Internal_Sym * symtab,
4969 unsigned long nsyms,
4970 const char * strtab,
4971 unsigned long strtab_size,
4972 struct absaddr addr,
4973 const char ** symname,
4974 bfd_vma * offset)
4976 bfd_vma dist = 0x100000;
4977 Elf_Internal_Sym * sym;
4978 Elf_Internal_Sym * best = NULL;
4979 unsigned long i;
4981 for (i = 0, sym = symtab; i < nsyms; ++i, ++sym)
4983 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
4984 && sym->st_name != 0
4985 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
4986 && addr.offset >= sym->st_value
4987 && addr.offset - sym->st_value < dist)
4989 best = sym;
4990 dist = addr.offset - sym->st_value;
4991 if (!dist)
4992 break;
4995 if (best)
4997 *symname = (best->st_name >= strtab_size
4998 ? "<corrupt>" : strtab + best->st_name);
4999 *offset = dist;
5000 return;
5002 *symname = NULL;
5003 *offset = addr.offset;
5006 static void
5007 dump_ia64_unwind (struct ia64_unw_aux_info * aux)
5009 struct ia64_unw_table_entry * tp;
5010 int in_body;
5012 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
5014 bfd_vma stamp;
5015 bfd_vma offset;
5016 const unsigned char * dp;
5017 const unsigned char * head;
5018 const char * procname;
5020 find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
5021 aux->strtab_size, tp->start, &procname, &offset);
5023 fputs ("\n<", stdout);
5025 if (procname)
5027 fputs (procname, stdout);
5029 if (offset)
5030 printf ("+%lx", (unsigned long) offset);
5033 fputs (">: [", stdout);
5034 print_vma (tp->start.offset, PREFIX_HEX);
5035 fputc ('-', stdout);
5036 print_vma (tp->end.offset, PREFIX_HEX);
5037 printf ("], info at +0x%lx\n",
5038 (unsigned long) (tp->info.offset - aux->seg_base));
5040 head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
5041 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
5043 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
5044 (unsigned) UNW_VER (stamp),
5045 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
5046 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
5047 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
5048 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
5050 if (UNW_VER (stamp) != 1)
5052 printf ("\tUnknown version.\n");
5053 continue;
5056 in_body = 0;
5057 for (dp = head + 8; dp < head + 8 + eh_addr_size * UNW_LENGTH (stamp);)
5058 dp = unw_decode (dp, in_body, & in_body);
5062 static int
5063 slurp_ia64_unwind_table (FILE * file,
5064 struct ia64_unw_aux_info * aux,
5065 Elf_Internal_Shdr * sec)
5067 unsigned long size, nrelas, i;
5068 Elf_Internal_Phdr * seg;
5069 struct ia64_unw_table_entry * tep;
5070 Elf_Internal_Shdr * relsec;
5071 Elf_Internal_Rela * rela;
5072 Elf_Internal_Rela * rp;
5073 unsigned char * table;
5074 unsigned char * tp;
5075 Elf_Internal_Sym * sym;
5076 const char * relname;
5078 /* First, find the starting address of the segment that includes
5079 this section: */
5081 if (elf_header.e_phnum)
5083 if (! get_program_headers (file))
5084 return 0;
5086 for (seg = program_headers;
5087 seg < program_headers + elf_header.e_phnum;
5088 ++seg)
5090 if (seg->p_type != PT_LOAD)
5091 continue;
5093 if (sec->sh_addr >= seg->p_vaddr
5094 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
5096 aux->seg_base = seg->p_vaddr;
5097 break;
5102 /* Second, build the unwind table from the contents of the unwind section: */
5103 size = sec->sh_size;
5104 table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
5105 _("unwind table"));
5106 if (!table)
5107 return 0;
5109 aux->table = (struct ia64_unw_table_entry *)
5110 xcmalloc (size / (3 * eh_addr_size), sizeof (aux->table[0]));
5111 tep = aux->table;
5112 for (tp = table; tp < table + size; ++tep)
5114 tep->start.section = SHN_UNDEF;
5115 tep->end.section = SHN_UNDEF;
5116 tep->info.section = SHN_UNDEF;
5117 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5118 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5119 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5120 tep->start.offset += aux->seg_base;
5121 tep->end.offset += aux->seg_base;
5122 tep->info.offset += aux->seg_base;
5124 free (table);
5126 /* Third, apply any relocations to the unwind table: */
5127 for (relsec = section_headers;
5128 relsec < section_headers + elf_header.e_shnum;
5129 ++relsec)
5131 if (relsec->sh_type != SHT_RELA
5132 || relsec->sh_info >= elf_header.e_shnum
5133 || section_headers + relsec->sh_info != sec)
5134 continue;
5136 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5137 & rela, & nrelas))
5138 return 0;
5140 for (rp = rela; rp < rela + nrelas; ++rp)
5142 relname = elf_ia64_reloc_type (get_reloc_type (rp->r_info));
5143 sym = aux->symtab + get_reloc_symindex (rp->r_info);
5145 if (! const_strneq (relname, "R_IA64_SEGREL"))
5147 warn (_("Skipping unexpected relocation type %s\n"), relname);
5148 continue;
5151 i = rp->r_offset / (3 * eh_addr_size);
5153 switch (rp->r_offset/eh_addr_size % 3)
5155 case 0:
5156 aux->table[i].start.section = sym->st_shndx;
5157 aux->table[i].start.offset += rp->r_addend + sym->st_value;
5158 break;
5159 case 1:
5160 aux->table[i].end.section = sym->st_shndx;
5161 aux->table[i].end.offset += rp->r_addend + sym->st_value;
5162 break;
5163 case 2:
5164 aux->table[i].info.section = sym->st_shndx;
5165 aux->table[i].info.offset += rp->r_addend + sym->st_value;
5166 break;
5167 default:
5168 break;
5172 free (rela);
5175 aux->table_len = size / (3 * eh_addr_size);
5176 return 1;
5179 static int
5180 ia64_process_unwind (FILE * file)
5182 Elf_Internal_Shdr * sec;
5183 Elf_Internal_Shdr * unwsec = NULL;
5184 Elf_Internal_Shdr * strsec;
5185 unsigned long i, unwcount = 0, unwstart = 0;
5186 struct ia64_unw_aux_info aux;
5188 memset (& aux, 0, sizeof (aux));
5190 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5192 if (sec->sh_type == SHT_SYMTAB
5193 && sec->sh_link < elf_header.e_shnum)
5195 aux.nsyms = sec->sh_size / sec->sh_entsize;
5196 aux.symtab = GET_ELF_SYMBOLS (file, sec);
5198 strsec = section_headers + sec->sh_link;
5199 aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
5200 1, strsec->sh_size,
5201 _("string table"));
5202 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5204 else if (sec->sh_type == SHT_IA_64_UNWIND)
5205 unwcount++;
5208 if (!unwcount)
5209 printf (_("\nThere are no unwind sections in this file.\n"));
5211 while (unwcount-- > 0)
5213 char * suffix;
5214 size_t len, len2;
5216 for (i = unwstart, sec = section_headers + unwstart;
5217 i < elf_header.e_shnum; ++i, ++sec)
5218 if (sec->sh_type == SHT_IA_64_UNWIND)
5220 unwsec = sec;
5221 break;
5224 unwstart = i + 1;
5225 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
5227 if ((unwsec->sh_flags & SHF_GROUP) != 0)
5229 /* We need to find which section group it is in. */
5230 struct group_list * g = section_headers_groups [i]->root;
5232 for (; g != NULL; g = g->next)
5234 sec = section_headers + g->section_index;
5236 if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
5237 break;
5240 if (g == NULL)
5241 i = elf_header.e_shnum;
5243 else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
5245 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
5246 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
5247 suffix = SECTION_NAME (unwsec) + len;
5248 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5249 ++i, ++sec)
5250 if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
5251 && streq (SECTION_NAME (sec) + len2, suffix))
5252 break;
5254 else
5256 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
5257 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
5258 len = sizeof (ELF_STRING_ia64_unwind) - 1;
5259 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
5260 suffix = "";
5261 if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
5262 suffix = SECTION_NAME (unwsec) + len;
5263 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5264 ++i, ++sec)
5265 if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
5266 && streq (SECTION_NAME (sec) + len2, suffix))
5267 break;
5270 if (i == elf_header.e_shnum)
5272 printf (_("\nCould not find unwind info section for "));
5274 if (string_table == NULL)
5275 printf ("%d", unwsec->sh_name);
5276 else
5277 printf (_("'%s'"), SECTION_NAME (unwsec));
5279 else
5281 aux.info_size = sec->sh_size;
5282 aux.info_addr = sec->sh_addr;
5283 aux.info = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1,
5284 aux.info_size,
5285 _("unwind info"));
5287 printf (_("\nUnwind section "));
5289 if (string_table == NULL)
5290 printf ("%d", unwsec->sh_name);
5291 else
5292 printf (_("'%s'"), SECTION_NAME (unwsec));
5294 printf (_(" at offset 0x%lx contains %lu entries:\n"),
5295 (unsigned long) unwsec->sh_offset,
5296 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
5298 (void) slurp_ia64_unwind_table (file, & aux, unwsec);
5300 if (aux.table_len > 0)
5301 dump_ia64_unwind (& aux);
5303 if (aux.table)
5304 free ((char *) aux.table);
5305 if (aux.info)
5306 free ((char *) aux.info);
5307 aux.table = NULL;
5308 aux.info = NULL;
5312 if (aux.symtab)
5313 free (aux.symtab);
5314 if (aux.strtab)
5315 free ((char *) aux.strtab);
5317 return 1;
5320 struct hppa_unw_table_entry
5322 struct absaddr start;
5323 struct absaddr end;
5324 unsigned int Cannot_unwind:1; /* 0 */
5325 unsigned int Millicode:1; /* 1 */
5326 unsigned int Millicode_save_sr0:1; /* 2 */
5327 unsigned int Region_description:2; /* 3..4 */
5328 unsigned int reserved1:1; /* 5 */
5329 unsigned int Entry_SR:1; /* 6 */
5330 unsigned int Entry_FR:4; /* number saved */ /* 7..10 */
5331 unsigned int Entry_GR:5; /* number saved */ /* 11..15 */
5332 unsigned int Args_stored:1; /* 16 */
5333 unsigned int Variable_Frame:1; /* 17 */
5334 unsigned int Separate_Package_Body:1; /* 18 */
5335 unsigned int Frame_Extension_Millicode:1; /* 19 */
5336 unsigned int Stack_Overflow_Check:1; /* 20 */
5337 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
5338 unsigned int Ada_Region:1; /* 22 */
5339 unsigned int cxx_info:1; /* 23 */
5340 unsigned int cxx_try_catch:1; /* 24 */
5341 unsigned int sched_entry_seq:1; /* 25 */
5342 unsigned int reserved2:1; /* 26 */
5343 unsigned int Save_SP:1; /* 27 */
5344 unsigned int Save_RP:1; /* 28 */
5345 unsigned int Save_MRP_in_frame:1; /* 29 */
5346 unsigned int extn_ptr_defined:1; /* 30 */
5347 unsigned int Cleanup_defined:1; /* 31 */
5349 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
5350 unsigned int HP_UX_interrupt_marker:1; /* 1 */
5351 unsigned int Large_frame:1; /* 2 */
5352 unsigned int Pseudo_SP_Set:1; /* 3 */
5353 unsigned int reserved4:1; /* 4 */
5354 unsigned int Total_frame_size:27; /* 5..31 */
5357 struct hppa_unw_aux_info
5359 struct hppa_unw_table_entry *table; /* Unwind table. */
5360 unsigned long table_len; /* Length of unwind table. */
5361 bfd_vma seg_base; /* Starting address of segment. */
5362 Elf_Internal_Sym * symtab; /* The symbol table. */
5363 unsigned long nsyms; /* Number of symbols. */
5364 char * strtab; /* The string table. */
5365 unsigned long strtab_size; /* Size of string table. */
5368 static void
5369 dump_hppa_unwind (struct hppa_unw_aux_info * aux)
5371 struct hppa_unw_table_entry * tp;
5373 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
5375 bfd_vma offset;
5376 const char * procname;
5378 find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
5379 aux->strtab_size, tp->start, &procname,
5380 &offset);
5382 fputs ("\n<", stdout);
5384 if (procname)
5386 fputs (procname, stdout);
5388 if (offset)
5389 printf ("+%lx", (unsigned long) offset);
5392 fputs (">: [", stdout);
5393 print_vma (tp->start.offset, PREFIX_HEX);
5394 fputc ('-', stdout);
5395 print_vma (tp->end.offset, PREFIX_HEX);
5396 printf ("]\n\t");
5398 #define PF(_m) if (tp->_m) printf (#_m " ");
5399 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
5400 PF(Cannot_unwind);
5401 PF(Millicode);
5402 PF(Millicode_save_sr0);
5403 /* PV(Region_description); */
5404 PF(Entry_SR);
5405 PV(Entry_FR);
5406 PV(Entry_GR);
5407 PF(Args_stored);
5408 PF(Variable_Frame);
5409 PF(Separate_Package_Body);
5410 PF(Frame_Extension_Millicode);
5411 PF(Stack_Overflow_Check);
5412 PF(Two_Instruction_SP_Increment);
5413 PF(Ada_Region);
5414 PF(cxx_info);
5415 PF(cxx_try_catch);
5416 PF(sched_entry_seq);
5417 PF(Save_SP);
5418 PF(Save_RP);
5419 PF(Save_MRP_in_frame);
5420 PF(extn_ptr_defined);
5421 PF(Cleanup_defined);
5422 PF(MPE_XL_interrupt_marker);
5423 PF(HP_UX_interrupt_marker);
5424 PF(Large_frame);
5425 PF(Pseudo_SP_Set);
5426 PV(Total_frame_size);
5427 #undef PF
5428 #undef PV
5431 printf ("\n");
5434 static int
5435 slurp_hppa_unwind_table (FILE * file,
5436 struct hppa_unw_aux_info * aux,
5437 Elf_Internal_Shdr * sec)
5439 unsigned long size, unw_ent_size, nentries, nrelas, i;
5440 Elf_Internal_Phdr * seg;
5441 struct hppa_unw_table_entry * tep;
5442 Elf_Internal_Shdr * relsec;
5443 Elf_Internal_Rela * rela;
5444 Elf_Internal_Rela * rp;
5445 unsigned char * table;
5446 unsigned char * tp;
5447 Elf_Internal_Sym * sym;
5448 const char * relname;
5450 /* First, find the starting address of the segment that includes
5451 this section. */
5453 if (elf_header.e_phnum)
5455 if (! get_program_headers (file))
5456 return 0;
5458 for (seg = program_headers;
5459 seg < program_headers + elf_header.e_phnum;
5460 ++seg)
5462 if (seg->p_type != PT_LOAD)
5463 continue;
5465 if (sec->sh_addr >= seg->p_vaddr
5466 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
5468 aux->seg_base = seg->p_vaddr;
5469 break;
5474 /* Second, build the unwind table from the contents of the unwind
5475 section. */
5476 size = sec->sh_size;
5477 table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
5478 _("unwind table"));
5479 if (!table)
5480 return 0;
5482 unw_ent_size = 16;
5483 nentries = size / unw_ent_size;
5484 size = unw_ent_size * nentries;
5486 tep = aux->table = (struct hppa_unw_table_entry *)
5487 xcmalloc (nentries, sizeof (aux->table[0]));
5489 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
5491 unsigned int tmp1, tmp2;
5493 tep->start.section = SHN_UNDEF;
5494 tep->end.section = SHN_UNDEF;
5496 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
5497 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
5498 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
5499 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
5501 tep->start.offset += aux->seg_base;
5502 tep->end.offset += aux->seg_base;
5504 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
5505 tep->Millicode = (tmp1 >> 30) & 0x1;
5506 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
5507 tep->Region_description = (tmp1 >> 27) & 0x3;
5508 tep->reserved1 = (tmp1 >> 26) & 0x1;
5509 tep->Entry_SR = (tmp1 >> 25) & 0x1;
5510 tep->Entry_FR = (tmp1 >> 21) & 0xf;
5511 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
5512 tep->Args_stored = (tmp1 >> 15) & 0x1;
5513 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
5514 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
5515 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
5516 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
5517 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
5518 tep->Ada_Region = (tmp1 >> 9) & 0x1;
5519 tep->cxx_info = (tmp1 >> 8) & 0x1;
5520 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
5521 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
5522 tep->reserved2 = (tmp1 >> 5) & 0x1;
5523 tep->Save_SP = (tmp1 >> 4) & 0x1;
5524 tep->Save_RP = (tmp1 >> 3) & 0x1;
5525 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
5526 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
5527 tep->Cleanup_defined = tmp1 & 0x1;
5529 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
5530 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
5531 tep->Large_frame = (tmp2 >> 29) & 0x1;
5532 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
5533 tep->reserved4 = (tmp2 >> 27) & 0x1;
5534 tep->Total_frame_size = tmp2 & 0x7ffffff;
5536 free (table);
5538 /* Third, apply any relocations to the unwind table. */
5539 for (relsec = section_headers;
5540 relsec < section_headers + elf_header.e_shnum;
5541 ++relsec)
5543 if (relsec->sh_type != SHT_RELA
5544 || relsec->sh_info >= elf_header.e_shnum
5545 || section_headers + relsec->sh_info != sec)
5546 continue;
5548 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5549 & rela, & nrelas))
5550 return 0;
5552 for (rp = rela; rp < rela + nrelas; ++rp)
5554 relname = elf_hppa_reloc_type (get_reloc_type (rp->r_info));
5555 sym = aux->symtab + get_reloc_symindex (rp->r_info);
5557 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
5558 if (! const_strneq (relname, "R_PARISC_SEGREL"))
5560 warn (_("Skipping unexpected relocation type %s\n"), relname);
5561 continue;
5564 i = rp->r_offset / unw_ent_size;
5566 switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
5568 case 0:
5569 aux->table[i].start.section = sym->st_shndx;
5570 aux->table[i].start.offset += sym->st_value + rp->r_addend;
5571 break;
5572 case 1:
5573 aux->table[i].end.section = sym->st_shndx;
5574 aux->table[i].end.offset += sym->st_value + rp->r_addend;
5575 break;
5576 default:
5577 break;
5581 free (rela);
5584 aux->table_len = nentries;
5586 return 1;
5589 static int
5590 hppa_process_unwind (FILE * file)
5592 struct hppa_unw_aux_info aux;
5593 Elf_Internal_Shdr * unwsec = NULL;
5594 Elf_Internal_Shdr * strsec;
5595 Elf_Internal_Shdr * sec;
5596 unsigned long i;
5598 memset (& aux, 0, sizeof (aux));
5600 if (string_table == NULL)
5601 return 1;
5603 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5605 if (sec->sh_type == SHT_SYMTAB
5606 && sec->sh_link < elf_header.e_shnum)
5608 aux.nsyms = sec->sh_size / sec->sh_entsize;
5609 aux.symtab = GET_ELF_SYMBOLS (file, sec);
5611 strsec = section_headers + sec->sh_link;
5612 aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
5613 1, strsec->sh_size,
5614 _("string table"));
5615 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5617 else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
5618 unwsec = sec;
5621 if (!unwsec)
5622 printf (_("\nThere are no unwind sections in this file.\n"));
5624 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5626 if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
5628 printf (_("\nUnwind section "));
5629 printf (_("'%s'"), SECTION_NAME (sec));
5631 printf (_(" at offset 0x%lx contains %lu entries:\n"),
5632 (unsigned long) sec->sh_offset,
5633 (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
5635 slurp_hppa_unwind_table (file, &aux, sec);
5636 if (aux.table_len > 0)
5637 dump_hppa_unwind (&aux);
5639 if (aux.table)
5640 free ((char *) aux.table);
5641 aux.table = NULL;
5645 if (aux.symtab)
5646 free (aux.symtab);
5647 if (aux.strtab)
5648 free ((char *) aux.strtab);
5650 return 1;
5653 static int
5654 process_unwind (FILE * file)
5656 struct unwind_handler
5658 int machtype;
5659 int (* handler)(FILE *);
5660 } handlers[] =
5662 { EM_IA_64, ia64_process_unwind },
5663 { EM_PARISC, hppa_process_unwind },
5664 { 0, 0 }
5666 int i;
5668 if (!do_unwind)
5669 return 1;
5671 for (i = 0; handlers[i].handler != NULL; i++)
5672 if (elf_header.e_machine == handlers[i].machtype)
5673 return handlers[i].handler (file);
5675 printf (_("\nThere are no unwind sections in this file.\n"));
5676 return 1;
5679 static void
5680 dynamic_section_mips_val (Elf_Internal_Dyn * entry)
5682 switch (entry->d_tag)
5684 case DT_MIPS_FLAGS:
5685 if (entry->d_un.d_val == 0)
5686 printf ("NONE\n");
5687 else
5689 static const char * opts[] =
5691 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
5692 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
5693 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
5694 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
5695 "RLD_ORDER_SAFE"
5697 unsigned int cnt;
5698 int first = 1;
5699 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
5700 if (entry->d_un.d_val & (1 << cnt))
5702 printf ("%s%s", first ? "" : " ", opts[cnt]);
5703 first = 0;
5705 puts ("");
5707 break;
5709 case DT_MIPS_IVERSION:
5710 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
5711 printf ("Interface Version: %s\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
5712 else
5713 printf ("<corrupt: %ld>\n", (long) entry->d_un.d_ptr);
5714 break;
5716 case DT_MIPS_TIME_STAMP:
5718 char timebuf[20];
5719 struct tm * tmp;
5721 time_t time = entry->d_un.d_val;
5722 tmp = gmtime (&time);
5723 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
5724 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
5725 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
5726 printf ("Time Stamp: %s\n", timebuf);
5728 break;
5730 case DT_MIPS_RLD_VERSION:
5731 case DT_MIPS_LOCAL_GOTNO:
5732 case DT_MIPS_CONFLICTNO:
5733 case DT_MIPS_LIBLISTNO:
5734 case DT_MIPS_SYMTABNO:
5735 case DT_MIPS_UNREFEXTNO:
5736 case DT_MIPS_HIPAGENO:
5737 case DT_MIPS_DELTA_CLASS_NO:
5738 case DT_MIPS_DELTA_INSTANCE_NO:
5739 case DT_MIPS_DELTA_RELOC_NO:
5740 case DT_MIPS_DELTA_SYM_NO:
5741 case DT_MIPS_DELTA_CLASSSYM_NO:
5742 case DT_MIPS_COMPACT_SIZE:
5743 printf ("%ld\n", (long) entry->d_un.d_ptr);
5744 break;
5746 default:
5747 printf ("%#lx\n", (unsigned long) entry->d_un.d_ptr);
5752 static void
5753 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
5755 switch (entry->d_tag)
5757 case DT_HP_DLD_FLAGS:
5759 static struct
5761 long int bit;
5762 const char * str;
5764 flags[] =
5766 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
5767 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
5768 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
5769 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
5770 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
5771 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
5772 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
5773 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
5774 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
5775 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
5776 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
5777 { DT_HP_GST, "HP_GST" },
5778 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
5779 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
5780 { DT_HP_NODELETE, "HP_NODELETE" },
5781 { DT_HP_GROUP, "HP_GROUP" },
5782 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
5784 int first = 1;
5785 size_t cnt;
5786 bfd_vma val = entry->d_un.d_val;
5788 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
5789 if (val & flags[cnt].bit)
5791 if (! first)
5792 putchar (' ');
5793 fputs (flags[cnt].str, stdout);
5794 first = 0;
5795 val ^= flags[cnt].bit;
5798 if (val != 0 || first)
5800 if (! first)
5801 putchar (' ');
5802 print_vma (val, HEX);
5805 break;
5807 default:
5808 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5809 break;
5811 putchar ('\n');
5814 static void
5815 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
5817 switch (entry->d_tag)
5819 case DT_IA_64_PLT_RESERVE:
5820 /* First 3 slots reserved. */
5821 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5822 printf (" -- ");
5823 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
5824 break;
5826 default:
5827 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5828 break;
5830 putchar ('\n');
5833 static int
5834 get_32bit_dynamic_section (FILE * file)
5836 Elf32_External_Dyn * edyn;
5837 Elf32_External_Dyn * ext;
5838 Elf_Internal_Dyn * entry;
5840 edyn = (Elf32_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
5841 dynamic_size, _("dynamic section"));
5842 if (!edyn)
5843 return 0;
5845 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
5846 might not have the luxury of section headers. Look for the DT_NULL
5847 terminator to determine the number of entries. */
5848 for (ext = edyn, dynamic_nent = 0;
5849 (char *) ext < (char *) edyn + dynamic_size;
5850 ext++)
5852 dynamic_nent++;
5853 if (BYTE_GET (ext->d_tag) == DT_NULL)
5854 break;
5857 dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
5858 sizeof (* entry));
5859 if (dynamic_section == NULL)
5861 error (_("Out of memory\n"));
5862 free (edyn);
5863 return 0;
5866 for (ext = edyn, entry = dynamic_section;
5867 entry < dynamic_section + dynamic_nent;
5868 ext++, entry++)
5870 entry->d_tag = BYTE_GET (ext->d_tag);
5871 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
5874 free (edyn);
5876 return 1;
5879 static int
5880 get_64bit_dynamic_section (FILE * file)
5882 Elf64_External_Dyn * edyn;
5883 Elf64_External_Dyn * ext;
5884 Elf_Internal_Dyn * entry;
5886 edyn = (Elf64_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
5887 dynamic_size, _("dynamic section"));
5888 if (!edyn)
5889 return 0;
5891 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
5892 might not have the luxury of section headers. Look for the DT_NULL
5893 terminator to determine the number of entries. */
5894 for (ext = edyn, dynamic_nent = 0;
5895 (char *) ext < (char *) edyn + dynamic_size;
5896 ext++)
5898 dynamic_nent++;
5899 if (BYTE_GET (ext->d_tag) == DT_NULL)
5900 break;
5903 dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
5904 sizeof (* entry));
5905 if (dynamic_section == NULL)
5907 error (_("Out of memory\n"));
5908 free (edyn);
5909 return 0;
5912 for (ext = edyn, entry = dynamic_section;
5913 entry < dynamic_section + dynamic_nent;
5914 ext++, entry++)
5916 entry->d_tag = BYTE_GET (ext->d_tag);
5917 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
5920 free (edyn);
5922 return 1;
5925 static void
5926 print_dynamic_flags (bfd_vma flags)
5928 int first = 1;
5930 while (flags)
5932 bfd_vma flag;
5934 flag = flags & - flags;
5935 flags &= ~ flag;
5937 if (first)
5938 first = 0;
5939 else
5940 putc (' ', stdout);
5942 switch (flag)
5944 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
5945 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
5946 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
5947 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
5948 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
5949 default: fputs ("unknown", stdout); break;
5952 puts ("");
5955 /* Parse and display the contents of the dynamic section. */
5957 static int
5958 process_dynamic_section (FILE * file)
5960 Elf_Internal_Dyn * entry;
5962 if (dynamic_size == 0)
5964 if (do_dynamic)
5965 printf (_("\nThere is no dynamic section in this file.\n"));
5967 return 1;
5970 if (is_32bit_elf)
5972 if (! get_32bit_dynamic_section (file))
5973 return 0;
5975 else if (! get_64bit_dynamic_section (file))
5976 return 0;
5978 /* Find the appropriate symbol table. */
5979 if (dynamic_symbols == NULL)
5981 for (entry = dynamic_section;
5982 entry < dynamic_section + dynamic_nent;
5983 ++entry)
5985 Elf_Internal_Shdr section;
5987 if (entry->d_tag != DT_SYMTAB)
5988 continue;
5990 dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
5992 /* Since we do not know how big the symbol table is,
5993 we default to reading in the entire file (!) and
5994 processing that. This is overkill, I know, but it
5995 should work. */
5996 section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
5998 if (archive_file_offset != 0)
5999 section.sh_size = archive_file_size - section.sh_offset;
6000 else
6002 if (fseek (file, 0, SEEK_END))
6003 error (_("Unable to seek to end of file!\n"));
6005 section.sh_size = ftell (file) - section.sh_offset;
6008 if (is_32bit_elf)
6009 section.sh_entsize = sizeof (Elf32_External_Sym);
6010 else
6011 section.sh_entsize = sizeof (Elf64_External_Sym);
6013 num_dynamic_syms = section.sh_size / section.sh_entsize;
6014 if (num_dynamic_syms < 1)
6016 error (_("Unable to determine the number of symbols to load\n"));
6017 continue;
6020 dynamic_symbols = GET_ELF_SYMBOLS (file, &section);
6024 /* Similarly find a string table. */
6025 if (dynamic_strings == NULL)
6027 for (entry = dynamic_section;
6028 entry < dynamic_section + dynamic_nent;
6029 ++entry)
6031 unsigned long offset;
6032 long str_tab_len;
6034 if (entry->d_tag != DT_STRTAB)
6035 continue;
6037 dynamic_info[DT_STRTAB] = entry->d_un.d_val;
6039 /* Since we do not know how big the string table is,
6040 we default to reading in the entire file (!) and
6041 processing that. This is overkill, I know, but it
6042 should work. */
6044 offset = offset_from_vma (file, entry->d_un.d_val, 0);
6046 if (archive_file_offset != 0)
6047 str_tab_len = archive_file_size - offset;
6048 else
6050 if (fseek (file, 0, SEEK_END))
6051 error (_("Unable to seek to end of file\n"));
6052 str_tab_len = ftell (file) - offset;
6055 if (str_tab_len < 1)
6057 error
6058 (_("Unable to determine the length of the dynamic string table\n"));
6059 continue;
6062 dynamic_strings = (char *) get_data (NULL, file, offset, 1,
6063 str_tab_len,
6064 _("dynamic string table"));
6065 dynamic_strings_length = str_tab_len;
6066 break;
6070 /* And find the syminfo section if available. */
6071 if (dynamic_syminfo == NULL)
6073 unsigned long syminsz = 0;
6075 for (entry = dynamic_section;
6076 entry < dynamic_section + dynamic_nent;
6077 ++entry)
6079 if (entry->d_tag == DT_SYMINENT)
6081 /* Note: these braces are necessary to avoid a syntax
6082 error from the SunOS4 C compiler. */
6083 assert (sizeof (Elf_External_Syminfo) == entry->d_un.d_val);
6085 else if (entry->d_tag == DT_SYMINSZ)
6086 syminsz = entry->d_un.d_val;
6087 else if (entry->d_tag == DT_SYMINFO)
6088 dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
6089 syminsz);
6092 if (dynamic_syminfo_offset != 0 && syminsz != 0)
6094 Elf_External_Syminfo * extsyminfo;
6095 Elf_External_Syminfo * extsym;
6096 Elf_Internal_Syminfo * syminfo;
6098 /* There is a syminfo section. Read the data. */
6099 extsyminfo = (Elf_External_Syminfo *)
6100 get_data (NULL, file, dynamic_syminfo_offset, 1, syminsz,
6101 _("symbol information"));
6102 if (!extsyminfo)
6103 return 0;
6105 dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
6106 if (dynamic_syminfo == NULL)
6108 error (_("Out of memory\n"));
6109 return 0;
6112 dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
6113 for (syminfo = dynamic_syminfo, extsym = extsyminfo;
6114 syminfo < dynamic_syminfo + dynamic_syminfo_nent;
6115 ++syminfo, ++extsym)
6117 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
6118 syminfo->si_flags = BYTE_GET (extsym->si_flags);
6121 free (extsyminfo);
6125 if (do_dynamic && dynamic_addr)
6126 printf (_("\nDynamic section at offset 0x%lx contains %u entries:\n"),
6127 dynamic_addr, dynamic_nent);
6128 if (do_dynamic)
6129 printf (_(" Tag Type Name/Value\n"));
6131 for (entry = dynamic_section;
6132 entry < dynamic_section + dynamic_nent;
6133 entry++)
6135 if (do_dynamic)
6137 const char * dtype;
6139 putchar (' ');
6140 print_vma (entry->d_tag, FULL_HEX);
6141 dtype = get_dynamic_type (entry->d_tag);
6142 printf (" (%s)%*s", dtype,
6143 ((is_32bit_elf ? 27 : 19)
6144 - (int) strlen (dtype)),
6145 " ");
6148 switch (entry->d_tag)
6150 case DT_FLAGS:
6151 if (do_dynamic)
6152 print_dynamic_flags (entry->d_un.d_val);
6153 break;
6155 case DT_AUXILIARY:
6156 case DT_FILTER:
6157 case DT_CONFIG:
6158 case DT_DEPAUDIT:
6159 case DT_AUDIT:
6160 if (do_dynamic)
6162 switch (entry->d_tag)
6164 case DT_AUXILIARY:
6165 printf (_("Auxiliary library"));
6166 break;
6168 case DT_FILTER:
6169 printf (_("Filter library"));
6170 break;
6172 case DT_CONFIG:
6173 printf (_("Configuration file"));
6174 break;
6176 case DT_DEPAUDIT:
6177 printf (_("Dependency audit library"));
6178 break;
6180 case DT_AUDIT:
6181 printf (_("Audit library"));
6182 break;
6185 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
6186 printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
6187 else
6189 printf (": ");
6190 print_vma (entry->d_un.d_val, PREFIX_HEX);
6191 putchar ('\n');
6194 break;
6196 case DT_FEATURE:
6197 if (do_dynamic)
6199 printf (_("Flags:"));
6201 if (entry->d_un.d_val == 0)
6202 printf (_(" None\n"));
6203 else
6205 unsigned long int val = entry->d_un.d_val;
6207 if (val & DTF_1_PARINIT)
6209 printf (" PARINIT");
6210 val ^= DTF_1_PARINIT;
6212 if (val & DTF_1_CONFEXP)
6214 printf (" CONFEXP");
6215 val ^= DTF_1_CONFEXP;
6217 if (val != 0)
6218 printf (" %lx", val);
6219 puts ("");
6222 break;
6224 case DT_POSFLAG_1:
6225 if (do_dynamic)
6227 printf (_("Flags:"));
6229 if (entry->d_un.d_val == 0)
6230 printf (_(" None\n"));
6231 else
6233 unsigned long int val = entry->d_un.d_val;
6235 if (val & DF_P1_LAZYLOAD)
6237 printf (" LAZYLOAD");
6238 val ^= DF_P1_LAZYLOAD;
6240 if (val & DF_P1_GROUPPERM)
6242 printf (" GROUPPERM");
6243 val ^= DF_P1_GROUPPERM;
6245 if (val != 0)
6246 printf (" %lx", val);
6247 puts ("");
6250 break;
6252 case DT_FLAGS_1:
6253 if (do_dynamic)
6255 printf (_("Flags:"));
6256 if (entry->d_un.d_val == 0)
6257 printf (_(" None\n"));
6258 else
6260 unsigned long int val = entry->d_un.d_val;
6262 if (val & DF_1_NOW)
6264 printf (" NOW");
6265 val ^= DF_1_NOW;
6267 if (val & DF_1_GLOBAL)
6269 printf (" GLOBAL");
6270 val ^= DF_1_GLOBAL;
6272 if (val & DF_1_GROUP)
6274 printf (" GROUP");
6275 val ^= DF_1_GROUP;
6277 if (val & DF_1_NODELETE)
6279 printf (" NODELETE");
6280 val ^= DF_1_NODELETE;
6282 if (val & DF_1_LOADFLTR)
6284 printf (" LOADFLTR");
6285 val ^= DF_1_LOADFLTR;
6287 if (val & DF_1_INITFIRST)
6289 printf (" INITFIRST");
6290 val ^= DF_1_INITFIRST;
6292 if (val & DF_1_NOOPEN)
6294 printf (" NOOPEN");
6295 val ^= DF_1_NOOPEN;
6297 if (val & DF_1_ORIGIN)
6299 printf (" ORIGIN");
6300 val ^= DF_1_ORIGIN;
6302 if (val & DF_1_DIRECT)
6304 printf (" DIRECT");
6305 val ^= DF_1_DIRECT;
6307 if (val & DF_1_TRANS)
6309 printf (" TRANS");
6310 val ^= DF_1_TRANS;
6312 if (val & DF_1_INTERPOSE)
6314 printf (" INTERPOSE");
6315 val ^= DF_1_INTERPOSE;
6317 if (val & DF_1_NODEFLIB)
6319 printf (" NODEFLIB");
6320 val ^= DF_1_NODEFLIB;
6322 if (val & DF_1_NODUMP)
6324 printf (" NODUMP");
6325 val ^= DF_1_NODUMP;
6327 if (val & DF_1_CONLFAT)
6329 printf (" CONLFAT");
6330 val ^= DF_1_CONLFAT;
6332 if (val != 0)
6333 printf (" %lx", val);
6334 puts ("");
6337 break;
6339 case DT_PLTREL:
6340 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6341 if (do_dynamic)
6342 puts (get_dynamic_type (entry->d_un.d_val));
6343 break;
6345 case DT_NULL :
6346 case DT_NEEDED :
6347 case DT_PLTGOT :
6348 case DT_HASH :
6349 case DT_STRTAB :
6350 case DT_SYMTAB :
6351 case DT_RELA :
6352 case DT_INIT :
6353 case DT_FINI :
6354 case DT_SONAME :
6355 case DT_RPATH :
6356 case DT_SYMBOLIC:
6357 case DT_REL :
6358 case DT_DEBUG :
6359 case DT_TEXTREL :
6360 case DT_JMPREL :
6361 case DT_RUNPATH :
6362 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6364 if (do_dynamic)
6366 char * name;
6368 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
6369 name = GET_DYNAMIC_NAME (entry->d_un.d_val);
6370 else
6371 name = NULL;
6373 if (name)
6375 switch (entry->d_tag)
6377 case DT_NEEDED:
6378 printf (_("Shared library: [%s]"), name);
6380 if (streq (name, program_interpreter))
6381 printf (_(" program interpreter"));
6382 break;
6384 case DT_SONAME:
6385 printf (_("Library soname: [%s]"), name);
6386 break;
6388 case DT_RPATH:
6389 printf (_("Library rpath: [%s]"), name);
6390 break;
6392 case DT_RUNPATH:
6393 printf (_("Library runpath: [%s]"), name);
6394 break;
6396 default:
6397 print_vma (entry->d_un.d_val, PREFIX_HEX);
6398 break;
6401 else
6402 print_vma (entry->d_un.d_val, PREFIX_HEX);
6404 putchar ('\n');
6406 break;
6408 case DT_PLTRELSZ:
6409 case DT_RELASZ :
6410 case DT_STRSZ :
6411 case DT_RELSZ :
6412 case DT_RELAENT :
6413 case DT_SYMENT :
6414 case DT_RELENT :
6415 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6416 case DT_PLTPADSZ:
6417 case DT_MOVEENT :
6418 case DT_MOVESZ :
6419 case DT_INIT_ARRAYSZ:
6420 case DT_FINI_ARRAYSZ:
6421 case DT_GNU_CONFLICTSZ:
6422 case DT_GNU_LIBLISTSZ:
6423 if (do_dynamic)
6425 print_vma (entry->d_un.d_val, UNSIGNED);
6426 printf (" (bytes)\n");
6428 break;
6430 case DT_VERDEFNUM:
6431 case DT_VERNEEDNUM:
6432 case DT_RELACOUNT:
6433 case DT_RELCOUNT:
6434 if (do_dynamic)
6436 print_vma (entry->d_un.d_val, UNSIGNED);
6437 putchar ('\n');
6439 break;
6441 case DT_SYMINSZ:
6442 case DT_SYMINENT:
6443 case DT_SYMINFO:
6444 case DT_USED:
6445 case DT_INIT_ARRAY:
6446 case DT_FINI_ARRAY:
6447 if (do_dynamic)
6449 if (entry->d_tag == DT_USED
6450 && VALID_DYNAMIC_NAME (entry->d_un.d_val))
6452 char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
6454 if (*name)
6456 printf (_("Not needed object: [%s]\n"), name);
6457 break;
6461 print_vma (entry->d_un.d_val, PREFIX_HEX);
6462 putchar ('\n');
6464 break;
6466 case DT_BIND_NOW:
6467 /* The value of this entry is ignored. */
6468 if (do_dynamic)
6469 putchar ('\n');
6470 break;
6472 case DT_GNU_PRELINKED:
6473 if (do_dynamic)
6475 struct tm * tmp;
6476 time_t time = entry->d_un.d_val;
6478 tmp = gmtime (&time);
6479 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
6480 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
6481 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
6484 break;
6486 case DT_GNU_HASH:
6487 dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
6488 if (do_dynamic)
6490 print_vma (entry->d_un.d_val, PREFIX_HEX);
6491 putchar ('\n');
6493 break;
6495 default:
6496 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
6497 version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
6498 entry->d_un.d_val;
6500 if (do_dynamic)
6502 switch (elf_header.e_machine)
6504 case EM_MIPS:
6505 case EM_MIPS_RS3_LE:
6506 dynamic_section_mips_val (entry);
6507 break;
6508 case EM_PARISC:
6509 dynamic_section_parisc_val (entry);
6510 break;
6511 case EM_IA_64:
6512 dynamic_section_ia64_val (entry);
6513 break;
6514 default:
6515 print_vma (entry->d_un.d_val, PREFIX_HEX);
6516 putchar ('\n');
6519 break;
6523 return 1;
6526 static char *
6527 get_ver_flags (unsigned int flags)
6529 static char buff[32];
6531 buff[0] = 0;
6533 if (flags == 0)
6534 return _("none");
6536 if (flags & VER_FLG_BASE)
6537 strcat (buff, "BASE ");
6539 if (flags & VER_FLG_WEAK)
6541 if (flags & VER_FLG_BASE)
6542 strcat (buff, "| ");
6544 strcat (buff, "WEAK ");
6547 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK))
6548 strcat (buff, "| <unknown>");
6550 return buff;
6553 /* Display the contents of the version sections. */
6555 static int
6556 process_version_sections (FILE * file)
6558 Elf_Internal_Shdr * section;
6559 unsigned i;
6560 int found = 0;
6562 if (! do_version)
6563 return 1;
6565 for (i = 0, section = section_headers;
6566 i < elf_header.e_shnum;
6567 i++, section++)
6569 switch (section->sh_type)
6571 case SHT_GNU_verdef:
6573 Elf_External_Verdef * edefs;
6574 unsigned int idx;
6575 unsigned int cnt;
6576 char * endbuf;
6578 found = 1;
6580 printf
6581 (_("\nVersion definition section '%s' contains %u entries:\n"),
6582 SECTION_NAME (section), section->sh_info);
6584 printf (_(" Addr: 0x"));
6585 printf_vma (section->sh_addr);
6586 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
6587 (unsigned long) section->sh_offset, section->sh_link,
6588 section->sh_link < elf_header.e_shnum
6589 ? SECTION_NAME (section_headers + section->sh_link)
6590 : "<corrupt>");
6592 edefs = (Elf_External_Verdef *)
6593 get_data (NULL, file, section->sh_offset, 1,section->sh_size,
6594 _("version definition section"));
6595 endbuf = (char *) edefs + section->sh_size;
6596 if (!edefs)
6597 break;
6599 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
6601 char * vstart;
6602 Elf_External_Verdef * edef;
6603 Elf_Internal_Verdef ent;
6604 Elf_External_Verdaux * eaux;
6605 Elf_Internal_Verdaux aux;
6606 int j;
6607 int isum;
6609 vstart = ((char *) edefs) + idx;
6610 if (vstart + sizeof (*edef) > endbuf)
6611 break;
6613 edef = (Elf_External_Verdef *) vstart;
6615 ent.vd_version = BYTE_GET (edef->vd_version);
6616 ent.vd_flags = BYTE_GET (edef->vd_flags);
6617 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
6618 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
6619 ent.vd_hash = BYTE_GET (edef->vd_hash);
6620 ent.vd_aux = BYTE_GET (edef->vd_aux);
6621 ent.vd_next = BYTE_GET (edef->vd_next);
6623 printf (_(" %#06x: Rev: %d Flags: %s"),
6624 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
6626 printf (_(" Index: %d Cnt: %d "),
6627 ent.vd_ndx, ent.vd_cnt);
6629 vstart += ent.vd_aux;
6631 eaux = (Elf_External_Verdaux *) vstart;
6633 aux.vda_name = BYTE_GET (eaux->vda_name);
6634 aux.vda_next = BYTE_GET (eaux->vda_next);
6636 if (VALID_DYNAMIC_NAME (aux.vda_name))
6637 printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
6638 else
6639 printf (_("Name index: %ld\n"), aux.vda_name);
6641 isum = idx + ent.vd_aux;
6643 for (j = 1; j < ent.vd_cnt; j++)
6645 isum += aux.vda_next;
6646 vstart += aux.vda_next;
6648 eaux = (Elf_External_Verdaux *) vstart;
6649 if (vstart + sizeof (*eaux) > endbuf)
6650 break;
6652 aux.vda_name = BYTE_GET (eaux->vda_name);
6653 aux.vda_next = BYTE_GET (eaux->vda_next);
6655 if (VALID_DYNAMIC_NAME (aux.vda_name))
6656 printf (_(" %#06x: Parent %d: %s\n"),
6657 isum, j, GET_DYNAMIC_NAME (aux.vda_name));
6658 else
6659 printf (_(" %#06x: Parent %d, name index: %ld\n"),
6660 isum, j, aux.vda_name);
6662 if (j < ent.vd_cnt)
6663 printf (_(" Version def aux past end of section\n"));
6665 idx += ent.vd_next;
6667 if (cnt < section->sh_info)
6668 printf (_(" Version definition past end of section\n"));
6670 free (edefs);
6672 break;
6674 case SHT_GNU_verneed:
6676 Elf_External_Verneed * eneed;
6677 unsigned int idx;
6678 unsigned int cnt;
6679 char * endbuf;
6681 found = 1;
6683 printf (_("\nVersion needs section '%s' contains %u entries:\n"),
6684 SECTION_NAME (section), section->sh_info);
6686 printf (_(" Addr: 0x"));
6687 printf_vma (section->sh_addr);
6688 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
6689 (unsigned long) section->sh_offset, section->sh_link,
6690 section->sh_link < elf_header.e_shnum
6691 ? SECTION_NAME (section_headers + section->sh_link)
6692 : "<corrupt>");
6694 eneed = (Elf_External_Verneed *) get_data (NULL, file,
6695 section->sh_offset, 1,
6696 section->sh_size,
6697 _("version need section"));
6698 endbuf = (char *) eneed + section->sh_size;
6699 if (!eneed)
6700 break;
6702 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
6704 Elf_External_Verneed * entry;
6705 Elf_Internal_Verneed ent;
6706 int j;
6707 int isum;
6708 char * vstart;
6710 vstart = ((char *) eneed) + idx;
6711 if (vstart + sizeof (*entry) > endbuf)
6712 break;
6714 entry = (Elf_External_Verneed *) vstart;
6716 ent.vn_version = BYTE_GET (entry->vn_version);
6717 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
6718 ent.vn_file = BYTE_GET (entry->vn_file);
6719 ent.vn_aux = BYTE_GET (entry->vn_aux);
6720 ent.vn_next = BYTE_GET (entry->vn_next);
6722 printf (_(" %#06x: Version: %d"), idx, ent.vn_version);
6724 if (VALID_DYNAMIC_NAME (ent.vn_file))
6725 printf (_(" File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
6726 else
6727 printf (_(" File: %lx"), ent.vn_file);
6729 printf (_(" Cnt: %d\n"), ent.vn_cnt);
6731 vstart += ent.vn_aux;
6733 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
6735 Elf_External_Vernaux * eaux;
6736 Elf_Internal_Vernaux aux;
6738 if (vstart + sizeof (*eaux) > endbuf)
6739 break;
6740 eaux = (Elf_External_Vernaux *) vstart;
6742 aux.vna_hash = BYTE_GET (eaux->vna_hash);
6743 aux.vna_flags = BYTE_GET (eaux->vna_flags);
6744 aux.vna_other = BYTE_GET (eaux->vna_other);
6745 aux.vna_name = BYTE_GET (eaux->vna_name);
6746 aux.vna_next = BYTE_GET (eaux->vna_next);
6748 if (VALID_DYNAMIC_NAME (aux.vna_name))
6749 printf (_(" %#06x: Name: %s"),
6750 isum, GET_DYNAMIC_NAME (aux.vna_name));
6751 else
6752 printf (_(" %#06x: Name index: %lx"),
6753 isum, aux.vna_name);
6755 printf (_(" Flags: %s Version: %d\n"),
6756 get_ver_flags (aux.vna_flags), aux.vna_other);
6758 isum += aux.vna_next;
6759 vstart += aux.vna_next;
6761 if (j < ent.vn_cnt)
6762 printf (_(" Version need aux past end of section\n"));
6764 idx += ent.vn_next;
6766 if (cnt < section->sh_info)
6767 printf (_(" Version need past end of section\n"));
6769 free (eneed);
6771 break;
6773 case SHT_GNU_versym:
6775 Elf_Internal_Shdr * link_section;
6776 int total;
6777 int cnt;
6778 unsigned char * edata;
6779 unsigned short * data;
6780 char * strtab;
6781 Elf_Internal_Sym * symbols;
6782 Elf_Internal_Shdr * string_sec;
6783 long off;
6785 if (section->sh_link >= elf_header.e_shnum)
6786 break;
6788 link_section = section_headers + section->sh_link;
6789 total = section->sh_size / sizeof (Elf_External_Versym);
6791 if (link_section->sh_link >= elf_header.e_shnum)
6792 break;
6794 found = 1;
6796 symbols = GET_ELF_SYMBOLS (file, link_section);
6798 string_sec = section_headers + link_section->sh_link;
6800 strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
6801 string_sec->sh_size,
6802 _("version string table"));
6803 if (!strtab)
6804 break;
6806 printf (_("\nVersion symbols section '%s' contains %d entries:\n"),
6807 SECTION_NAME (section), total);
6809 printf (_(" Addr: "));
6810 printf_vma (section->sh_addr);
6811 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
6812 (unsigned long) section->sh_offset, section->sh_link,
6813 SECTION_NAME (link_section));
6815 off = offset_from_vma (file,
6816 version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
6817 total * sizeof (short));
6818 edata = (unsigned char *) get_data (NULL, file, off, total,
6819 sizeof (short),
6820 _("version symbol data"));
6821 if (!edata)
6823 free (strtab);
6824 break;
6827 data = (short unsigned int *) cmalloc (total, sizeof (short));
6829 for (cnt = total; cnt --;)
6830 data[cnt] = byte_get (edata + cnt * sizeof (short),
6831 sizeof (short));
6833 free (edata);
6835 for (cnt = 0; cnt < total; cnt += 4)
6837 int j, nn;
6838 int check_def, check_need;
6839 char * name;
6841 printf (" %03x:", cnt);
6843 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
6844 switch (data[cnt + j])
6846 case 0:
6847 fputs (_(" 0 (*local*) "), stdout);
6848 break;
6850 case 1:
6851 fputs (_(" 1 (*global*) "), stdout);
6852 break;
6854 default:
6855 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
6856 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
6858 check_def = 1;
6859 check_need = 1;
6860 if (symbols[cnt + j].st_shndx >= elf_header.e_shnum
6861 || section_headers[symbols[cnt + j].st_shndx].sh_type
6862 != SHT_NOBITS)
6864 if (symbols[cnt + j].st_shndx == SHN_UNDEF)
6865 check_def = 0;
6866 else
6867 check_need = 0;
6870 if (check_need
6871 && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
6873 Elf_Internal_Verneed ivn;
6874 unsigned long offset;
6876 offset = offset_from_vma
6877 (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
6878 sizeof (Elf_External_Verneed));
6882 Elf_Internal_Vernaux ivna;
6883 Elf_External_Verneed evn;
6884 Elf_External_Vernaux evna;
6885 unsigned long a_off;
6887 get_data (&evn, file, offset, sizeof (evn), 1,
6888 _("version need"));
6890 ivn.vn_aux = BYTE_GET (evn.vn_aux);
6891 ivn.vn_next = BYTE_GET (evn.vn_next);
6893 a_off = offset + ivn.vn_aux;
6897 get_data (&evna, file, a_off, sizeof (evna),
6898 1, _("version need aux (2)"));
6900 ivna.vna_next = BYTE_GET (evna.vna_next);
6901 ivna.vna_other = BYTE_GET (evna.vna_other);
6903 a_off += ivna.vna_next;
6905 while (ivna.vna_other != data[cnt + j]
6906 && ivna.vna_next != 0);
6908 if (ivna.vna_other == data[cnt + j])
6910 ivna.vna_name = BYTE_GET (evna.vna_name);
6912 if (ivna.vna_name >= string_sec->sh_size)
6913 name = _("*invalid*");
6914 else
6915 name = strtab + ivna.vna_name;
6916 nn += printf ("(%s%-*s",
6917 name,
6918 12 - (int) strlen (name),
6919 ")");
6920 check_def = 0;
6921 break;
6924 offset += ivn.vn_next;
6926 while (ivn.vn_next);
6929 if (check_def && data[cnt + j] != 0x8001
6930 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
6932 Elf_Internal_Verdef ivd;
6933 Elf_External_Verdef evd;
6934 unsigned long offset;
6936 offset = offset_from_vma
6937 (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
6938 sizeof evd);
6942 get_data (&evd, file, offset, sizeof (evd), 1,
6943 _("version def"));
6945 ivd.vd_next = BYTE_GET (evd.vd_next);
6946 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
6948 offset += ivd.vd_next;
6950 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
6951 && ivd.vd_next != 0);
6953 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
6955 Elf_External_Verdaux evda;
6956 Elf_Internal_Verdaux ivda;
6958 ivd.vd_aux = BYTE_GET (evd.vd_aux);
6960 get_data (&evda, file,
6961 offset - ivd.vd_next + ivd.vd_aux,
6962 sizeof (evda), 1,
6963 _("version def aux"));
6965 ivda.vda_name = BYTE_GET (evda.vda_name);
6967 if (ivda.vda_name >= string_sec->sh_size)
6968 name = _("*invalid*");
6969 else
6970 name = strtab + ivda.vda_name;
6971 nn += printf ("(%s%-*s",
6972 name,
6973 12 - (int) strlen (name),
6974 ")");
6978 if (nn < 18)
6979 printf ("%*c", 18 - nn, ' ');
6982 putchar ('\n');
6985 free (data);
6986 free (strtab);
6987 free (symbols);
6989 break;
6991 default:
6992 break;
6996 if (! found)
6997 printf (_("\nNo version information found in this file.\n"));
6999 return 1;
7002 static const char *
7003 get_symbol_binding (unsigned int binding)
7005 static char buff[32];
7007 switch (binding)
7009 case STB_LOCAL: return "LOCAL";
7010 case STB_GLOBAL: return "GLOBAL";
7011 case STB_WEAK: return "WEAK";
7012 default:
7013 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
7014 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
7015 binding);
7016 else if (binding >= STB_LOOS && binding <= STB_HIOS)
7018 if (binding == STB_GNU_UNIQUE
7019 && (elf_header.e_ident[EI_OSABI] == ELFOSABI_LINUX
7020 /* GNU/Linux is still using the default value 0. */
7021 || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
7022 return "UNIQUE";
7023 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
7025 else
7026 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
7027 return buff;
7031 static const char *
7032 get_symbol_type (unsigned int type)
7034 static char buff[32];
7036 switch (type)
7038 case STT_NOTYPE: return "NOTYPE";
7039 case STT_OBJECT: return "OBJECT";
7040 case STT_FUNC: return "FUNC";
7041 case STT_SECTION: return "SECTION";
7042 case STT_FILE: return "FILE";
7043 case STT_COMMON: return "COMMON";
7044 case STT_TLS: return "TLS";
7045 case STT_RELC: return "RELC";
7046 case STT_SRELC: return "SRELC";
7047 default:
7048 if (type >= STT_LOPROC && type <= STT_HIPROC)
7050 if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
7051 return "THUMB_FUNC";
7053 if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
7054 return "REGISTER";
7056 if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
7057 return "PARISC_MILLI";
7059 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
7061 else if (type >= STT_LOOS && type <= STT_HIOS)
7063 if (elf_header.e_machine == EM_PARISC)
7065 if (type == STT_HP_OPAQUE)
7066 return "HP_OPAQUE";
7067 if (type == STT_HP_STUB)
7068 return "HP_STUB";
7071 if (type == STT_GNU_IFUNC
7072 && (elf_header.e_ident[EI_OSABI] == ELFOSABI_LINUX
7073 /* GNU/Linux is still using the default value 0. */
7074 || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
7075 return "IFUNC";
7077 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
7079 else
7080 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
7081 return buff;
7085 static const char *
7086 get_symbol_visibility (unsigned int visibility)
7088 switch (visibility)
7090 case STV_DEFAULT: return "DEFAULT";
7091 case STV_INTERNAL: return "INTERNAL";
7092 case STV_HIDDEN: return "HIDDEN";
7093 case STV_PROTECTED: return "PROTECTED";
7094 default: abort ();
7098 static const char *
7099 get_mips_symbol_other (unsigned int other)
7101 switch (other)
7103 case STO_OPTIONAL: return "OPTIONAL";
7104 case STO_MIPS16: return "MIPS16";
7105 case STO_MIPS_PLT: return "MIPS PLT";
7106 case STO_MIPS_PIC: return "MIPS PIC";
7107 default: return NULL;
7111 static const char *
7112 get_symbol_other (unsigned int other)
7114 const char * result = NULL;
7115 static char buff [32];
7117 if (other == 0)
7118 return "";
7120 switch (elf_header.e_machine)
7122 case EM_MIPS:
7123 result = get_mips_symbol_other (other);
7124 default:
7125 break;
7128 if (result)
7129 return result;
7131 snprintf (buff, sizeof buff, _("<other>: %x"), other);
7132 return buff;
7135 static const char *
7136 get_symbol_index_type (unsigned int type)
7138 static char buff[32];
7140 switch (type)
7142 case SHN_UNDEF: return "UND";
7143 case SHN_ABS: return "ABS";
7144 case SHN_COMMON: return "COM";
7145 default:
7146 if (type == SHN_IA_64_ANSI_COMMON
7147 && elf_header.e_machine == EM_IA_64
7148 && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
7149 return "ANSI_COM";
7150 else if ((elf_header.e_machine == EM_X86_64
7151 || elf_header.e_machine == EM_L1OM)
7152 && type == SHN_X86_64_LCOMMON)
7153 return "LARGE_COM";
7154 else if (type == SHN_MIPS_SCOMMON
7155 && elf_header.e_machine == EM_MIPS)
7156 return "SCOM";
7157 else if (type == SHN_MIPS_SUNDEFINED
7158 && elf_header.e_machine == EM_MIPS)
7159 return "SUND";
7160 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
7161 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
7162 else if (type >= SHN_LOOS && type <= SHN_HIOS)
7163 sprintf (buff, "OS [0x%04x]", type & 0xffff);
7164 else if (type >= SHN_LORESERVE)
7165 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
7166 else
7167 sprintf (buff, "%3d", type);
7168 break;
7171 return buff;
7174 static bfd_vma *
7175 get_dynamic_data (FILE * file, unsigned int number, unsigned int ent_size)
7177 unsigned char * e_data;
7178 bfd_vma * i_data;
7180 e_data = (unsigned char *) cmalloc (number, ent_size);
7182 if (e_data == NULL)
7184 error (_("Out of memory\n"));
7185 return NULL;
7188 if (fread (e_data, ent_size, number, file) != number)
7190 error (_("Unable to read in dynamic data\n"));
7191 return NULL;
7194 i_data = (bfd_vma *) cmalloc (number, sizeof (*i_data));
7196 if (i_data == NULL)
7198 error (_("Out of memory\n"));
7199 free (e_data);
7200 return NULL;
7203 while (number--)
7204 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
7206 free (e_data);
7208 return i_data;
7211 static void
7212 print_dynamic_symbol (bfd_vma si, unsigned long hn)
7214 Elf_Internal_Sym * psym;
7215 int n;
7217 psym = dynamic_symbols + si;
7219 n = print_vma (si, DEC_5);
7220 if (n < 5)
7221 fputs (" " + n, stdout);
7222 printf (" %3lu: ", hn);
7223 print_vma (psym->st_value, LONG_HEX);
7224 putchar (' ');
7225 print_vma (psym->st_size, DEC_5);
7227 printf (" %6s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
7228 printf (" %6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
7229 printf (" %3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
7230 /* Check to see if any other bits in the st_other field are set.
7231 Note - displaying this information disrupts the layout of the
7232 table being generated, but for the moment this case is very
7233 rare. */
7234 if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
7235 printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
7236 printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
7237 if (VALID_DYNAMIC_NAME (psym->st_name))
7238 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
7239 else
7240 printf (" <corrupt: %14ld>", psym->st_name);
7241 putchar ('\n');
7244 /* Dump the symbol table. */
7245 static int
7246 process_symbol_table (FILE * file)
7248 Elf_Internal_Shdr * section;
7249 bfd_vma nbuckets = 0;
7250 bfd_vma nchains = 0;
7251 bfd_vma * buckets = NULL;
7252 bfd_vma * chains = NULL;
7253 bfd_vma ngnubuckets = 0;
7254 bfd_vma * gnubuckets = NULL;
7255 bfd_vma * gnuchains = NULL;
7256 bfd_vma gnusymidx = 0;
7258 if (! do_syms && !do_histogram)
7259 return 1;
7261 if (dynamic_info[DT_HASH]
7262 && (do_histogram
7263 || (do_using_dynamic && dynamic_strings != NULL)))
7265 unsigned char nb[8];
7266 unsigned char nc[8];
7267 int hash_ent_size = 4;
7269 if ((elf_header.e_machine == EM_ALPHA
7270 || elf_header.e_machine == EM_S390
7271 || elf_header.e_machine == EM_S390_OLD)
7272 && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
7273 hash_ent_size = 8;
7275 if (fseek (file,
7276 (archive_file_offset
7277 + offset_from_vma (file, dynamic_info[DT_HASH],
7278 sizeof nb + sizeof nc)),
7279 SEEK_SET))
7281 error (_("Unable to seek to start of dynamic information\n"));
7282 goto no_hash;
7285 if (fread (nb, hash_ent_size, 1, file) != 1)
7287 error (_("Failed to read in number of buckets\n"));
7288 goto no_hash;
7291 if (fread (nc, hash_ent_size, 1, file) != 1)
7293 error (_("Failed to read in number of chains\n"));
7294 goto no_hash;
7297 nbuckets = byte_get (nb, hash_ent_size);
7298 nchains = byte_get (nc, hash_ent_size);
7300 buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
7301 chains = get_dynamic_data (file, nchains, hash_ent_size);
7303 no_hash:
7304 if (buckets == NULL || chains == NULL)
7306 if (do_using_dynamic)
7307 return 0;
7308 free (buckets);
7309 free (chains);
7310 buckets = NULL;
7311 chains = NULL;
7312 nbuckets = 0;
7313 nchains = 0;
7317 if (dynamic_info_DT_GNU_HASH
7318 && (do_histogram
7319 || (do_using_dynamic && dynamic_strings != NULL)))
7321 unsigned char nb[16];
7322 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
7323 bfd_vma buckets_vma;
7325 if (fseek (file,
7326 (archive_file_offset
7327 + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
7328 sizeof nb)),
7329 SEEK_SET))
7331 error (_("Unable to seek to start of dynamic information\n"));
7332 goto no_gnu_hash;
7335 if (fread (nb, 16, 1, file) != 1)
7337 error (_("Failed to read in number of buckets\n"));
7338 goto no_gnu_hash;
7341 ngnubuckets = byte_get (nb, 4);
7342 gnusymidx = byte_get (nb + 4, 4);
7343 bitmaskwords = byte_get (nb + 8, 4);
7344 buckets_vma = dynamic_info_DT_GNU_HASH + 16;
7345 if (is_32bit_elf)
7346 buckets_vma += bitmaskwords * 4;
7347 else
7348 buckets_vma += bitmaskwords * 8;
7350 if (fseek (file,
7351 (archive_file_offset
7352 + offset_from_vma (file, buckets_vma, 4)),
7353 SEEK_SET))
7355 error (_("Unable to seek to start of dynamic information\n"));
7356 goto no_gnu_hash;
7359 gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
7361 if (gnubuckets == NULL)
7362 goto no_gnu_hash;
7364 for (i = 0; i < ngnubuckets; i++)
7365 if (gnubuckets[i] != 0)
7367 if (gnubuckets[i] < gnusymidx)
7368 return 0;
7370 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
7371 maxchain = gnubuckets[i];
7374 if (maxchain == 0xffffffff)
7375 goto no_gnu_hash;
7377 maxchain -= gnusymidx;
7379 if (fseek (file,
7380 (archive_file_offset
7381 + offset_from_vma (file, buckets_vma
7382 + 4 * (ngnubuckets + maxchain), 4)),
7383 SEEK_SET))
7385 error (_("Unable to seek to start of dynamic information\n"));
7386 goto no_gnu_hash;
7391 if (fread (nb, 4, 1, file) != 1)
7393 error (_("Failed to determine last chain length\n"));
7394 goto no_gnu_hash;
7397 if (maxchain + 1 == 0)
7398 goto no_gnu_hash;
7400 ++maxchain;
7402 while ((byte_get (nb, 4) & 1) == 0);
7404 if (fseek (file,
7405 (archive_file_offset
7406 + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
7407 SEEK_SET))
7409 error (_("Unable to seek to start of dynamic information\n"));
7410 goto no_gnu_hash;
7413 gnuchains = get_dynamic_data (file, maxchain, 4);
7415 no_gnu_hash:
7416 if (gnuchains == NULL)
7418 free (gnubuckets);
7419 gnubuckets = NULL;
7420 ngnubuckets = 0;
7421 if (do_using_dynamic)
7422 return 0;
7426 if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
7427 && do_syms
7428 && do_using_dynamic
7429 && dynamic_strings != NULL)
7431 unsigned long hn;
7433 if (dynamic_info[DT_HASH])
7435 bfd_vma si;
7437 printf (_("\nSymbol table for image:\n"));
7438 if (is_32bit_elf)
7439 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7440 else
7441 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7443 for (hn = 0; hn < nbuckets; hn++)
7445 if (! buckets[hn])
7446 continue;
7448 for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
7449 print_dynamic_symbol (si, hn);
7453 if (dynamic_info_DT_GNU_HASH)
7455 printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
7456 if (is_32bit_elf)
7457 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7458 else
7459 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7461 for (hn = 0; hn < ngnubuckets; ++hn)
7462 if (gnubuckets[hn] != 0)
7464 bfd_vma si = gnubuckets[hn];
7465 bfd_vma off = si - gnusymidx;
7469 print_dynamic_symbol (si, hn);
7470 si++;
7472 while ((gnuchains[off++] & 1) == 0);
7476 else if (do_syms && !do_using_dynamic)
7478 unsigned int i;
7480 for (i = 0, section = section_headers;
7481 i < elf_header.e_shnum;
7482 i++, section++)
7484 unsigned int si;
7485 char * strtab = NULL;
7486 unsigned long int strtab_size = 0;
7487 Elf_Internal_Sym * symtab;
7488 Elf_Internal_Sym * psym;
7490 if ( section->sh_type != SHT_SYMTAB
7491 && section->sh_type != SHT_DYNSYM)
7492 continue;
7494 printf (_("\nSymbol table '%s' contains %lu entries:\n"),
7495 SECTION_NAME (section),
7496 (unsigned long) (section->sh_size / section->sh_entsize));
7497 if (is_32bit_elf)
7498 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
7499 else
7500 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
7502 symtab = GET_ELF_SYMBOLS (file, section);
7503 if (symtab == NULL)
7504 continue;
7506 if (section->sh_link == elf_header.e_shstrndx)
7508 strtab = string_table;
7509 strtab_size = string_table_length;
7511 else if (section->sh_link < elf_header.e_shnum)
7513 Elf_Internal_Shdr * string_sec;
7515 string_sec = section_headers + section->sh_link;
7517 strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
7518 1, string_sec->sh_size,
7519 _("string table"));
7520 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
7523 for (si = 0, psym = symtab;
7524 si < section->sh_size / section->sh_entsize;
7525 si++, psym++)
7527 printf ("%6d: ", si);
7528 print_vma (psym->st_value, LONG_HEX);
7529 putchar (' ');
7530 print_vma (psym->st_size, DEC_5);
7531 printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
7532 printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
7533 printf (" %-3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
7534 /* Check to see if any other bits in the st_other field are set.
7535 Note - displaying this information disrupts the layout of the
7536 table being generated, but for the moment this case is very rare. */
7537 if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
7538 printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
7539 printf (" %4s ", get_symbol_index_type (psym->st_shndx));
7540 print_symbol (25, psym->st_name < strtab_size
7541 ? strtab + psym->st_name : "<corrupt>");
7543 if (section->sh_type == SHT_DYNSYM &&
7544 version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
7546 unsigned char data[2];
7547 unsigned short vers_data;
7548 unsigned long offset;
7549 int is_nobits;
7550 int check_def;
7552 offset = offset_from_vma
7553 (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
7554 sizeof data + si * sizeof (vers_data));
7556 get_data (&data, file, offset + si * sizeof (vers_data),
7557 sizeof (data), 1, _("version data"));
7559 vers_data = byte_get (data, 2);
7561 is_nobits = (psym->st_shndx < elf_header.e_shnum
7562 && section_headers[psym->st_shndx].sh_type
7563 == SHT_NOBITS);
7565 check_def = (psym->st_shndx != SHN_UNDEF);
7567 if ((vers_data & VERSYM_HIDDEN) || vers_data > 1)
7569 if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
7570 && (is_nobits || ! check_def))
7572 Elf_External_Verneed evn;
7573 Elf_Internal_Verneed ivn;
7574 Elf_Internal_Vernaux ivna;
7576 /* We must test both. */
7577 offset = offset_from_vma
7578 (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
7579 sizeof evn);
7583 unsigned long vna_off;
7585 get_data (&evn, file, offset, sizeof (evn), 1,
7586 _("version need"));
7588 ivn.vn_aux = BYTE_GET (evn.vn_aux);
7589 ivn.vn_next = BYTE_GET (evn.vn_next);
7591 vna_off = offset + ivn.vn_aux;
7595 Elf_External_Vernaux evna;
7597 get_data (&evna, file, vna_off,
7598 sizeof (evna), 1,
7599 _("version need aux (3)"));
7601 ivna.vna_other = BYTE_GET (evna.vna_other);
7602 ivna.vna_next = BYTE_GET (evna.vna_next);
7603 ivna.vna_name = BYTE_GET (evna.vna_name);
7605 vna_off += ivna.vna_next;
7607 while (ivna.vna_other != vers_data
7608 && ivna.vna_next != 0);
7610 if (ivna.vna_other == vers_data)
7611 break;
7613 offset += ivn.vn_next;
7615 while (ivn.vn_next != 0);
7617 if (ivna.vna_other == vers_data)
7619 printf ("@%s (%d)",
7620 ivna.vna_name < strtab_size
7621 ? strtab + ivna.vna_name : "<corrupt>",
7622 ivna.vna_other);
7623 check_def = 0;
7625 else if (! is_nobits)
7626 error (_("bad dynamic symbol\n"));
7627 else
7628 check_def = 1;
7631 if (check_def)
7633 if (vers_data != 0x8001
7634 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
7636 Elf_Internal_Verdef ivd;
7637 Elf_Internal_Verdaux ivda;
7638 Elf_External_Verdaux evda;
7639 unsigned long offset;
7641 offset = offset_from_vma
7642 (file,
7643 version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
7644 sizeof (Elf_External_Verdef));
7648 Elf_External_Verdef evd;
7650 get_data (&evd, file, offset, sizeof (evd),
7651 1, _("version def"));
7653 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
7654 ivd.vd_aux = BYTE_GET (evd.vd_aux);
7655 ivd.vd_next = BYTE_GET (evd.vd_next);
7657 offset += ivd.vd_next;
7659 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION)
7660 && ivd.vd_next != 0);
7662 offset -= ivd.vd_next;
7663 offset += ivd.vd_aux;
7665 get_data (&evda, file, offset, sizeof (evda),
7666 1, _("version def aux"));
7668 ivda.vda_name = BYTE_GET (evda.vda_name);
7670 if (psym->st_name != ivda.vda_name)
7671 printf ((vers_data & VERSYM_HIDDEN)
7672 ? "@%s" : "@@%s",
7673 ivda.vda_name < strtab_size
7674 ? strtab + ivda.vda_name : "<corrupt>");
7680 putchar ('\n');
7683 free (symtab);
7684 if (strtab != string_table)
7685 free (strtab);
7688 else if (do_syms)
7689 printf
7690 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
7692 if (do_histogram && buckets != NULL)
7694 unsigned long * lengths;
7695 unsigned long * counts;
7696 unsigned long hn;
7697 bfd_vma si;
7698 unsigned long maxlength = 0;
7699 unsigned long nzero_counts = 0;
7700 unsigned long nsyms = 0;
7702 printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
7703 (unsigned long) nbuckets);
7704 printf (_(" Length Number %% of total Coverage\n"));
7706 lengths = (unsigned long *) calloc (nbuckets, sizeof (*lengths));
7707 if (lengths == NULL)
7709 error (_("Out of memory\n"));
7710 return 0;
7712 for (hn = 0; hn < nbuckets; ++hn)
7714 for (si = buckets[hn]; si > 0 && si < nchains; si = chains[si])
7716 ++nsyms;
7717 if (maxlength < ++lengths[hn])
7718 ++maxlength;
7722 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
7723 if (counts == NULL)
7725 error (_("Out of memory\n"));
7726 return 0;
7729 for (hn = 0; hn < nbuckets; ++hn)
7730 ++counts[lengths[hn]];
7732 if (nbuckets > 0)
7734 unsigned long i;
7735 printf (" 0 %-10lu (%5.1f%%)\n",
7736 counts[0], (counts[0] * 100.0) / nbuckets);
7737 for (i = 1; i <= maxlength; ++i)
7739 nzero_counts += counts[i] * i;
7740 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
7741 i, counts[i], (counts[i] * 100.0) / nbuckets,
7742 (nzero_counts * 100.0) / nsyms);
7746 free (counts);
7747 free (lengths);
7750 if (buckets != NULL)
7752 free (buckets);
7753 free (chains);
7756 if (do_histogram && gnubuckets != NULL)
7758 unsigned long * lengths;
7759 unsigned long * counts;
7760 unsigned long hn;
7761 unsigned long maxlength = 0;
7762 unsigned long nzero_counts = 0;
7763 unsigned long nsyms = 0;
7765 lengths = (unsigned long *) calloc (ngnubuckets, sizeof (*lengths));
7766 if (lengths == NULL)
7768 error (_("Out of memory\n"));
7769 return 0;
7772 printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
7773 (unsigned long) ngnubuckets);
7774 printf (_(" Length Number %% of total Coverage\n"));
7776 for (hn = 0; hn < ngnubuckets; ++hn)
7777 if (gnubuckets[hn] != 0)
7779 bfd_vma off, length = 1;
7781 for (off = gnubuckets[hn] - gnusymidx;
7782 (gnuchains[off] & 1) == 0; ++off)
7783 ++length;
7784 lengths[hn] = length;
7785 if (length > maxlength)
7786 maxlength = length;
7787 nsyms += length;
7790 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
7791 if (counts == NULL)
7793 error (_("Out of memory\n"));
7794 return 0;
7797 for (hn = 0; hn < ngnubuckets; ++hn)
7798 ++counts[lengths[hn]];
7800 if (ngnubuckets > 0)
7802 unsigned long j;
7803 printf (" 0 %-10lu (%5.1f%%)\n",
7804 counts[0], (counts[0] * 100.0) / ngnubuckets);
7805 for (j = 1; j <= maxlength; ++j)
7807 nzero_counts += counts[j] * j;
7808 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
7809 j, counts[j], (counts[j] * 100.0) / ngnubuckets,
7810 (nzero_counts * 100.0) / nsyms);
7814 free (counts);
7815 free (lengths);
7816 free (gnubuckets);
7817 free (gnuchains);
7820 return 1;
7823 static int
7824 process_syminfo (FILE * file ATTRIBUTE_UNUSED)
7826 unsigned int i;
7828 if (dynamic_syminfo == NULL
7829 || !do_dynamic)
7830 /* No syminfo, this is ok. */
7831 return 1;
7833 /* There better should be a dynamic symbol section. */
7834 if (dynamic_symbols == NULL || dynamic_strings == NULL)
7835 return 0;
7837 if (dynamic_addr)
7838 printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
7839 dynamic_syminfo_offset, dynamic_syminfo_nent);
7841 printf (_(" Num: Name BoundTo Flags\n"));
7842 for (i = 0; i < dynamic_syminfo_nent; ++i)
7844 unsigned short int flags = dynamic_syminfo[i].si_flags;
7846 printf ("%4d: ", i);
7847 if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
7848 print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
7849 else
7850 printf ("<corrupt: %19ld>", dynamic_symbols[i].st_name);
7851 putchar (' ');
7853 switch (dynamic_syminfo[i].si_boundto)
7855 case SYMINFO_BT_SELF:
7856 fputs ("SELF ", stdout);
7857 break;
7858 case SYMINFO_BT_PARENT:
7859 fputs ("PARENT ", stdout);
7860 break;
7861 default:
7862 if (dynamic_syminfo[i].si_boundto > 0
7863 && dynamic_syminfo[i].si_boundto < dynamic_nent
7864 && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
7866 print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
7867 putchar (' ' );
7869 else
7870 printf ("%-10d ", dynamic_syminfo[i].si_boundto);
7871 break;
7874 if (flags & SYMINFO_FLG_DIRECT)
7875 printf (" DIRECT");
7876 if (flags & SYMINFO_FLG_PASSTHRU)
7877 printf (" PASSTHRU");
7878 if (flags & SYMINFO_FLG_COPY)
7879 printf (" COPY");
7880 if (flags & SYMINFO_FLG_LAZYLOAD)
7881 printf (" LAZYLOAD");
7883 puts ("");
7886 return 1;
7889 /* Check to see if the given reloc needs to be handled in a target specific
7890 manner. If so then process the reloc and return TRUE otherwise return
7891 FALSE. */
7893 static bfd_boolean
7894 target_specific_reloc_handling (Elf_Internal_Rela * reloc,
7895 unsigned char * start,
7896 Elf_Internal_Sym * symtab)
7898 unsigned int reloc_type = get_reloc_type (reloc->r_info);
7900 switch (elf_header.e_machine)
7902 case EM_MN10300:
7903 case EM_CYGNUS_MN10300:
7905 static Elf_Internal_Sym * saved_sym = NULL;
7907 switch (reloc_type)
7909 case 34: /* R_MN10300_ALIGN */
7910 return TRUE;
7911 case 33: /* R_MN10300_SYM_DIFF */
7912 saved_sym = symtab + get_reloc_symindex (reloc->r_info);
7913 return TRUE;
7914 case 1: /* R_MN10300_32 */
7915 case 2: /* R_MN10300_16 */
7916 if (saved_sym != NULL)
7918 bfd_vma value;
7920 value = reloc->r_addend
7921 + (symtab[get_reloc_symindex (reloc->r_info)].st_value
7922 - saved_sym->st_value);
7924 byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
7926 saved_sym = NULL;
7927 return TRUE;
7929 break;
7930 default:
7931 if (saved_sym != NULL)
7932 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc"));
7933 break;
7935 break;
7939 return FALSE;
7942 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
7943 DWARF debug sections. This is a target specific test. Note - we do not
7944 go through the whole including-target-headers-multiple-times route, (as
7945 we have already done with <elf/h8.h>) because this would become very
7946 messy and even then this function would have to contain target specific
7947 information (the names of the relocs instead of their numeric values).
7948 FIXME: This is not the correct way to solve this problem. The proper way
7949 is to have target specific reloc sizing and typing functions created by
7950 the reloc-macros.h header, in the same way that it already creates the
7951 reloc naming functions. */
7953 static bfd_boolean
7954 is_32bit_abs_reloc (unsigned int reloc_type)
7956 switch (elf_header.e_machine)
7958 case EM_386:
7959 case EM_486:
7960 return reloc_type == 1; /* R_386_32. */
7961 case EM_68K:
7962 return reloc_type == 1; /* R_68K_32. */
7963 case EM_860:
7964 return reloc_type == 1; /* R_860_32. */
7965 case EM_ALPHA:
7966 return reloc_type == 1; /* XXX Is this right ? */
7967 case EM_ARC:
7968 return reloc_type == 1; /* R_ARC_32. */
7969 case EM_ARM:
7970 return reloc_type == 2; /* R_ARM_ABS32 */
7971 case EM_AVR_OLD:
7972 case EM_AVR:
7973 return reloc_type == 1;
7974 case EM_BLACKFIN:
7975 return reloc_type == 0x12; /* R_byte4_data. */
7976 case EM_CRIS:
7977 return reloc_type == 3; /* R_CRIS_32. */
7978 case EM_CR16:
7979 case EM_CR16_OLD:
7980 return reloc_type == 3; /* R_CR16_NUM32. */
7981 case EM_CRX:
7982 return reloc_type == 15; /* R_CRX_NUM32. */
7983 case EM_CYGNUS_FRV:
7984 return reloc_type == 1;
7985 case EM_CYGNUS_D10V:
7986 case EM_D10V:
7987 return reloc_type == 6; /* R_D10V_32. */
7988 case EM_CYGNUS_D30V:
7989 case EM_D30V:
7990 return reloc_type == 12; /* R_D30V_32_NORMAL. */
7991 case EM_DLX:
7992 return reloc_type == 3; /* R_DLX_RELOC_32. */
7993 case EM_CYGNUS_FR30:
7994 case EM_FR30:
7995 return reloc_type == 3; /* R_FR30_32. */
7996 case EM_H8S:
7997 case EM_H8_300:
7998 case EM_H8_300H:
7999 return reloc_type == 1; /* R_H8_DIR32. */
8000 case EM_IA_64:
8001 return reloc_type == 0x65; /* R_IA64_SECREL32LSB. */
8002 case EM_IP2K_OLD:
8003 case EM_IP2K:
8004 return reloc_type == 2; /* R_IP2K_32. */
8005 case EM_IQ2000:
8006 return reloc_type == 2; /* R_IQ2000_32. */
8007 case EM_LATTICEMICO32:
8008 return reloc_type == 3; /* R_LM32_32. */
8009 case EM_M32C_OLD:
8010 case EM_M32C:
8011 return reloc_type == 3; /* R_M32C_32. */
8012 case EM_M32R:
8013 return reloc_type == 34; /* R_M32R_32_RELA. */
8014 case EM_MCORE:
8015 return reloc_type == 1; /* R_MCORE_ADDR32. */
8016 case EM_CYGNUS_MEP:
8017 return reloc_type == 4; /* R_MEP_32. */
8018 case EM_MIPS:
8019 return reloc_type == 2; /* R_MIPS_32. */
8020 case EM_MMIX:
8021 return reloc_type == 4; /* R_MMIX_32. */
8022 case EM_CYGNUS_MN10200:
8023 case EM_MN10200:
8024 return reloc_type == 1; /* R_MN10200_32. */
8025 case EM_CYGNUS_MN10300:
8026 case EM_MN10300:
8027 return reloc_type == 1; /* R_MN10300_32. */
8028 case EM_MSP430_OLD:
8029 case EM_MSP430:
8030 return reloc_type == 1; /* R_MSP43_32. */
8031 case EM_MT:
8032 return reloc_type == 2; /* R_MT_32. */
8033 case EM_ALTERA_NIOS2:
8034 case EM_NIOS32:
8035 return reloc_type == 1; /* R_NIOS_32. */
8036 case EM_OPENRISC:
8037 case EM_OR32:
8038 return reloc_type == 1; /* R_OR32_32. */
8039 case EM_PARISC:
8040 return (reloc_type == 1 /* R_PARISC_DIR32. */
8041 || reloc_type == 41); /* R_PARISC_SECREL32. */
8042 case EM_PJ:
8043 case EM_PJ_OLD:
8044 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
8045 case EM_PPC64:
8046 return reloc_type == 1; /* R_PPC64_ADDR32. */
8047 case EM_PPC:
8048 return reloc_type == 1; /* R_PPC_ADDR32. */
8049 case EM_S370:
8050 return reloc_type == 1; /* R_I370_ADDR31. */
8051 case EM_S390_OLD:
8052 case EM_S390:
8053 return reloc_type == 4; /* R_S390_32. */
8054 case EM_SCORE:
8055 return reloc_type == 8; /* R_SCORE_ABS32. */
8056 case EM_SH:
8057 return reloc_type == 1; /* R_SH_DIR32. */
8058 case EM_SPARC32PLUS:
8059 case EM_SPARCV9:
8060 case EM_SPARC:
8061 return reloc_type == 3 /* R_SPARC_32. */
8062 || reloc_type == 23; /* R_SPARC_UA32. */
8063 case EM_SPU:
8064 return reloc_type == 6; /* R_SPU_ADDR32 */
8065 case EM_CYGNUS_V850:
8066 case EM_V850:
8067 return reloc_type == 6; /* R_V850_ABS32. */
8068 case EM_VAX:
8069 return reloc_type == 1; /* R_VAX_32. */
8070 case EM_X86_64:
8071 case EM_L1OM:
8072 return reloc_type == 10; /* R_X86_64_32. */
8073 case EM_XSTORMY16:
8074 return reloc_type == 1; /* R_XSTROMY16_32. */
8075 case EM_XTENSA_OLD:
8076 case EM_XTENSA:
8077 return reloc_type == 1; /* R_XTENSA_32. */
8079 default:
8080 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
8081 elf_header.e_machine);
8082 abort ();
8086 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8087 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
8089 static bfd_boolean
8090 is_32bit_pcrel_reloc (unsigned int reloc_type)
8092 switch (elf_header.e_machine)
8094 case EM_386:
8095 case EM_486:
8096 return reloc_type == 2; /* R_386_PC32. */
8097 case EM_68K:
8098 return reloc_type == 4; /* R_68K_PC32. */
8099 case EM_ALPHA:
8100 return reloc_type == 10; /* R_ALPHA_SREL32. */
8101 case EM_ARM:
8102 return reloc_type == 3; /* R_ARM_REL32 */
8103 case EM_PARISC:
8104 return reloc_type == 9; /* R_PARISC_PCREL32. */
8105 case EM_PPC:
8106 return reloc_type == 26; /* R_PPC_REL32. */
8107 case EM_PPC64:
8108 return reloc_type == 26; /* R_PPC64_REL32. */
8109 case EM_S390_OLD:
8110 case EM_S390:
8111 return reloc_type == 5; /* R_390_PC32. */
8112 case EM_SH:
8113 return reloc_type == 2; /* R_SH_REL32. */
8114 case EM_SPARC32PLUS:
8115 case EM_SPARCV9:
8116 case EM_SPARC:
8117 return reloc_type == 6; /* R_SPARC_DISP32. */
8118 case EM_SPU:
8119 return reloc_type == 13; /* R_SPU_REL32. */
8120 case EM_X86_64:
8121 case EM_L1OM:
8122 return reloc_type == 2; /* R_X86_64_PC32. */
8123 case EM_XTENSA_OLD:
8124 case EM_XTENSA:
8125 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
8126 default:
8127 /* Do not abort or issue an error message here. Not all targets use
8128 pc-relative 32-bit relocs in their DWARF debug information and we
8129 have already tested for target coverage in is_32bit_abs_reloc. A
8130 more helpful warning message will be generated by apply_relocations
8131 anyway, so just return. */
8132 return FALSE;
8136 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8137 a 64-bit absolute RELA relocation used in DWARF debug sections. */
8139 static bfd_boolean
8140 is_64bit_abs_reloc (unsigned int reloc_type)
8142 switch (elf_header.e_machine)
8144 case EM_ALPHA:
8145 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
8146 case EM_IA_64:
8147 return reloc_type == 0x27; /* R_IA64_DIR64LSB. */
8148 case EM_PARISC:
8149 return reloc_type == 80; /* R_PARISC_DIR64. */
8150 case EM_PPC64:
8151 return reloc_type == 38; /* R_PPC64_ADDR64. */
8152 case EM_SPARC32PLUS:
8153 case EM_SPARCV9:
8154 case EM_SPARC:
8155 return reloc_type == 54; /* R_SPARC_UA64. */
8156 case EM_X86_64:
8157 case EM_L1OM:
8158 return reloc_type == 1; /* R_X86_64_64. */
8159 case EM_S390_OLD:
8160 case EM_S390:
8161 return reloc_type == 22; /* R_S390_64 */
8162 case EM_MIPS:
8163 return reloc_type == 18; /* R_MIPS_64 */
8164 default:
8165 return FALSE;
8169 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
8170 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
8172 static bfd_boolean
8173 is_64bit_pcrel_reloc (unsigned int reloc_type)
8175 switch (elf_header.e_machine)
8177 case EM_ALPHA:
8178 return reloc_type == 11; /* R_ALPHA_SREL64 */
8179 case EM_IA_64:
8180 return reloc_type == 0x4f; /* R_IA64_PCREL64LSB */
8181 case EM_PARISC:
8182 return reloc_type == 72; /* R_PARISC_PCREL64 */
8183 case EM_PPC64:
8184 return reloc_type == 44; /* R_PPC64_REL64 */
8185 case EM_SPARC32PLUS:
8186 case EM_SPARCV9:
8187 case EM_SPARC:
8188 return reloc_type == 46; /* R_SPARC_DISP64 */
8189 case EM_X86_64:
8190 case EM_L1OM:
8191 return reloc_type == 24; /* R_X86_64_PC64 */
8192 case EM_S390_OLD:
8193 case EM_S390:
8194 return reloc_type == 23; /* R_S390_PC64 */
8195 default:
8196 return FALSE;
8200 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8201 a 24-bit absolute RELA relocation used in DWARF debug sections. */
8203 static bfd_boolean
8204 is_24bit_abs_reloc (unsigned int reloc_type)
8206 switch (elf_header.e_machine)
8208 case EM_CYGNUS_MN10200:
8209 case EM_MN10200:
8210 return reloc_type == 4; /* R_MN10200_24. */
8211 default:
8212 return FALSE;
8216 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
8217 a 16-bit absolute RELA relocation used in DWARF debug sections. */
8219 static bfd_boolean
8220 is_16bit_abs_reloc (unsigned int reloc_type)
8222 switch (elf_header.e_machine)
8224 case EM_AVR_OLD:
8225 case EM_AVR:
8226 return reloc_type == 4; /* R_AVR_16. */
8227 case EM_CYGNUS_D10V:
8228 case EM_D10V:
8229 return reloc_type == 3; /* R_D10V_16. */
8230 case EM_H8S:
8231 case EM_H8_300:
8232 case EM_H8_300H:
8233 return reloc_type == R_H8_DIR16;
8234 case EM_IP2K_OLD:
8235 case EM_IP2K:
8236 return reloc_type == 1; /* R_IP2K_16. */
8237 case EM_M32C_OLD:
8238 case EM_M32C:
8239 return reloc_type == 1; /* R_M32C_16 */
8240 case EM_MSP430_OLD:
8241 case EM_MSP430:
8242 return reloc_type == 5; /* R_MSP430_16_BYTE. */
8243 case EM_ALTERA_NIOS2:
8244 case EM_NIOS32:
8245 return reloc_type == 9; /* R_NIOS_16. */
8246 default:
8247 return FALSE;
8251 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
8252 relocation entries (possibly formerly used for SHT_GROUP sections). */
8254 static bfd_boolean
8255 is_none_reloc (unsigned int reloc_type)
8257 switch (elf_header.e_machine)
8259 case EM_68K: /* R_68K_NONE. */
8260 case EM_386: /* R_386_NONE. */
8261 case EM_SPARC32PLUS:
8262 case EM_SPARCV9:
8263 case EM_SPARC: /* R_SPARC_NONE. */
8264 case EM_MIPS: /* R_MIPS_NONE. */
8265 case EM_PARISC: /* R_PARISC_NONE. */
8266 case EM_ALPHA: /* R_ALPHA_NONE. */
8267 case EM_PPC: /* R_PPC_NONE. */
8268 case EM_PPC64: /* R_PPC64_NONE. */
8269 case EM_ARM: /* R_ARM_NONE. */
8270 case EM_IA_64: /* R_IA64_NONE. */
8271 case EM_SH: /* R_SH_NONE. */
8272 case EM_S390_OLD:
8273 case EM_S390: /* R_390_NONE. */
8274 case EM_CRIS: /* R_CRIS_NONE. */
8275 case EM_X86_64: /* R_X86_64_NONE. */
8276 case EM_L1OM: /* R_X86_64_NONE. */
8277 case EM_MN10300: /* R_MN10300_NONE. */
8278 case EM_M32R: /* R_M32R_NONE. */
8279 return reloc_type == 0;
8280 case EM_XTENSA_OLD:
8281 case EM_XTENSA:
8282 return (reloc_type == 0 /* R_XTENSA_NONE. */
8283 || reloc_type == 17 /* R_XTENSA_DIFF8. */
8284 || reloc_type == 18 /* R_XTENSA_DIFF16. */
8285 || reloc_type == 19 /* R_XTENSA_DIFF32. */);
8287 return FALSE;
8290 /* Apply relocations to a section.
8291 Note: So far support has been added only for those relocations
8292 which can be found in debug sections.
8293 FIXME: Add support for more relocations ? */
8295 static void
8296 apply_relocations (void * file,
8297 Elf_Internal_Shdr * section,
8298 unsigned char * start)
8300 Elf_Internal_Shdr * relsec;
8301 unsigned char * end = start + section->sh_size;
8303 if (elf_header.e_type != ET_REL)
8304 return;
8306 /* Find the reloc section associated with the section. */
8307 for (relsec = section_headers;
8308 relsec < section_headers + elf_header.e_shnum;
8309 ++relsec)
8311 bfd_boolean is_rela;
8312 unsigned long num_relocs;
8313 Elf_Internal_Rela * relocs;
8314 Elf_Internal_Rela * rp;
8315 Elf_Internal_Shdr * symsec;
8316 Elf_Internal_Sym * symtab;
8317 Elf_Internal_Sym * sym;
8319 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
8320 || relsec->sh_info >= elf_header.e_shnum
8321 || section_headers + relsec->sh_info != section
8322 || relsec->sh_size == 0
8323 || relsec->sh_link >= elf_header.e_shnum)
8324 continue;
8326 is_rela = relsec->sh_type == SHT_RELA;
8328 if (is_rela)
8330 if (!slurp_rela_relocs ((FILE *) file, relsec->sh_offset,
8331 relsec->sh_size, & relocs, & num_relocs))
8332 return;
8334 else
8336 if (!slurp_rel_relocs ((FILE *) file, relsec->sh_offset,
8337 relsec->sh_size, & relocs, & num_relocs))
8338 return;
8341 /* SH uses RELA but uses in place value instead of the addend field. */
8342 if (elf_header.e_machine == EM_SH)
8343 is_rela = FALSE;
8345 symsec = section_headers + relsec->sh_link;
8346 symtab = GET_ELF_SYMBOLS ((FILE *) file, symsec);
8348 for (rp = relocs; rp < relocs + num_relocs; ++rp)
8350 bfd_vma addend;
8351 unsigned int reloc_type;
8352 unsigned int reloc_size;
8353 unsigned char * loc;
8355 reloc_type = get_reloc_type (rp->r_info);
8357 if (target_specific_reloc_handling (rp, start, symtab))
8358 continue;
8359 else if (is_none_reloc (reloc_type))
8360 continue;
8361 else if (is_32bit_abs_reloc (reloc_type)
8362 || is_32bit_pcrel_reloc (reloc_type))
8363 reloc_size = 4;
8364 else if (is_64bit_abs_reloc (reloc_type)
8365 || is_64bit_pcrel_reloc (reloc_type))
8366 reloc_size = 8;
8367 else if (is_24bit_abs_reloc (reloc_type))
8368 reloc_size = 3;
8369 else if (is_16bit_abs_reloc (reloc_type))
8370 reloc_size = 2;
8371 else
8373 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
8374 reloc_type, SECTION_NAME (section));
8375 continue;
8378 loc = start + rp->r_offset;
8379 if ((loc + reloc_size) > end)
8381 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
8382 (unsigned long) rp->r_offset,
8383 SECTION_NAME (section));
8384 continue;
8387 sym = symtab + get_reloc_symindex (rp->r_info);
8389 /* If the reloc has a symbol associated with it,
8390 make sure that it is of an appropriate type.
8392 Relocations against symbols without type can happen.
8393 Gcc -feliminate-dwarf2-dups may generate symbols
8394 without type for debug info.
8396 Icc generates relocations against function symbols
8397 instead of local labels.
8399 Relocations against object symbols can happen, eg when
8400 referencing a global array. For an example of this see
8401 the _clz.o binary in libgcc.a. */
8402 if (sym != symtab
8403 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
8405 warn (_("skipping unexpected symbol type %s in %ld'th relocation in section %s\n"),
8406 get_symbol_type (ELF_ST_TYPE (sym->st_info)),
8407 (long int)(rp - relocs),
8408 SECTION_NAME (relsec));
8409 continue;
8412 addend = 0;
8413 if (is_rela)
8414 addend += rp->r_addend;
8415 /* R_XTENSA_32 and R_PJ_DATA_DIR32 are partial_inplace. */
8416 if (!is_rela
8417 || (elf_header.e_machine == EM_XTENSA
8418 && reloc_type == 1)
8419 || ((elf_header.e_machine == EM_PJ
8420 || elf_header.e_machine == EM_PJ_OLD)
8421 && reloc_type == 1))
8422 addend += byte_get (loc, reloc_size);
8424 if (is_32bit_pcrel_reloc (reloc_type)
8425 || is_64bit_pcrel_reloc (reloc_type))
8427 /* On HPPA, all pc-relative relocations are biased by 8. */
8428 if (elf_header.e_machine == EM_PARISC)
8429 addend -= 8;
8430 byte_put (loc, (addend + sym->st_value) - rp->r_offset,
8431 reloc_size);
8433 else
8434 byte_put (loc, addend + sym->st_value, reloc_size);
8437 free (symtab);
8438 free (relocs);
8439 break;
8443 #ifdef SUPPORT_DISASSEMBLY
8444 static int
8445 disassemble_section (Elf_Internal_Shdr * section, FILE * file)
8447 printf (_("\nAssembly dump of section %s\n"),
8448 SECTION_NAME (section));
8450 /* XXX -- to be done --- XXX */
8452 return 1;
8454 #endif
8456 /* Reads in the contents of SECTION from FILE, returning a pointer
8457 to a malloc'ed buffer or NULL if something went wrong. */
8459 static char *
8460 get_section_contents (Elf_Internal_Shdr * section, FILE * file)
8462 bfd_size_type num_bytes;
8464 num_bytes = section->sh_size;
8466 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
8468 printf (_("\nSection '%s' has no data to dump.\n"),
8469 SECTION_NAME (section));
8470 return NULL;
8473 return (char *) get_data (NULL, file, section->sh_offset, 1, num_bytes,
8474 _("section contents"));
8478 static void
8479 dump_section_as_strings (Elf_Internal_Shdr * section, FILE * file)
8481 Elf_Internal_Shdr * relsec;
8482 bfd_size_type num_bytes;
8483 bfd_vma addr;
8484 char * data;
8485 char * end;
8486 char * start;
8487 char * name = SECTION_NAME (section);
8488 bfd_boolean some_strings_shown;
8490 start = get_section_contents (section, file);
8491 if (start == NULL)
8492 return;
8494 printf (_("\nString dump of section '%s':\n"), name);
8496 /* If the section being dumped has relocations against it the user might
8497 be expecting these relocations to have been applied. Check for this
8498 case and issue a warning message in order to avoid confusion.
8499 FIXME: Maybe we ought to have an option that dumps a section with
8500 relocs applied ? */
8501 for (relsec = section_headers;
8502 relsec < section_headers + elf_header.e_shnum;
8503 ++relsec)
8505 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
8506 || relsec->sh_info >= elf_header.e_shnum
8507 || section_headers + relsec->sh_info != section
8508 || relsec->sh_size == 0
8509 || relsec->sh_link >= elf_header.e_shnum)
8510 continue;
8512 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
8513 break;
8516 num_bytes = section->sh_size;
8517 addr = section->sh_addr;
8518 data = start;
8519 end = start + num_bytes;
8520 some_strings_shown = FALSE;
8522 while (data < end)
8524 while (!ISPRINT (* data))
8525 if (++ data >= end)
8526 break;
8528 if (data < end)
8530 #ifndef __MSVCRT__
8531 printf (" [%6tx] %s\n", data - start, data);
8532 #else
8533 printf (" [%6Ix] %s\n", (size_t) (data - start), data);
8534 #endif
8535 data += strlen (data);
8536 some_strings_shown = TRUE;
8540 if (! some_strings_shown)
8541 printf (_(" No strings found in this section."));
8543 free (start);
8545 putchar ('\n');
8548 static void
8549 dump_section_as_bytes (Elf_Internal_Shdr * section,
8550 FILE * file,
8551 bfd_boolean relocate)
8553 Elf_Internal_Shdr * relsec;
8554 bfd_size_type bytes;
8555 bfd_vma addr;
8556 unsigned char * data;
8557 unsigned char * start;
8559 start = (unsigned char *) get_section_contents (section, file);
8560 if (start == NULL)
8561 return;
8563 printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section));
8565 if (relocate)
8567 apply_relocations (file, section, start);
8569 else
8571 /* If the section being dumped has relocations against it the user might
8572 be expecting these relocations to have been applied. Check for this
8573 case and issue a warning message in order to avoid confusion.
8574 FIXME: Maybe we ought to have an option that dumps a section with
8575 relocs applied ? */
8576 for (relsec = section_headers;
8577 relsec < section_headers + elf_header.e_shnum;
8578 ++relsec)
8580 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
8581 || relsec->sh_info >= elf_header.e_shnum
8582 || section_headers + relsec->sh_info != section
8583 || relsec->sh_size == 0
8584 || relsec->sh_link >= elf_header.e_shnum)
8585 continue;
8587 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
8588 break;
8592 addr = section->sh_addr;
8593 bytes = section->sh_size;
8594 data = start;
8596 while (bytes)
8598 int j;
8599 int k;
8600 int lbytes;
8602 lbytes = (bytes > 16 ? 16 : bytes);
8604 printf (" 0x%8.8lx ", (unsigned long) addr);
8606 for (j = 0; j < 16; j++)
8608 if (j < lbytes)
8609 printf ("%2.2x", data[j]);
8610 else
8611 printf (" ");
8613 if ((j & 3) == 3)
8614 printf (" ");
8617 for (j = 0; j < lbytes; j++)
8619 k = data[j];
8620 if (k >= ' ' && k < 0x7f)
8621 printf ("%c", k);
8622 else
8623 printf (".");
8626 putchar ('\n');
8628 data += lbytes;
8629 addr += lbytes;
8630 bytes -= lbytes;
8633 free (start);
8635 putchar ('\n');
8638 /* Uncompresses a section that was compressed using zlib, in place.
8639 This is a copy of bfd_uncompress_section_contents, in bfd/compress.c */
8641 static int
8642 uncompress_section_contents (unsigned char ** buffer, dwarf_size_type * size)
8644 #ifndef HAVE_ZLIB_H
8645 /* These are just to quiet gcc. */
8646 buffer = 0;
8647 size = 0;
8648 return FALSE;
8649 #else
8650 dwarf_size_type compressed_size = *size;
8651 unsigned char * compressed_buffer = *buffer;
8652 dwarf_size_type uncompressed_size;
8653 unsigned char * uncompressed_buffer;
8654 z_stream strm;
8655 int rc;
8656 dwarf_size_type header_size = 12;
8658 /* Read the zlib header. In this case, it should be "ZLIB" followed
8659 by the uncompressed section size, 8 bytes in big-endian order. */
8660 if (compressed_size < header_size
8661 || ! streq ((char *) compressed_buffer, "ZLIB"))
8662 return 0;
8664 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
8665 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
8666 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
8667 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
8668 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
8669 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
8670 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
8671 uncompressed_size += compressed_buffer[11];
8673 /* It is possible the section consists of several compressed
8674 buffers concatenated together, so we uncompress in a loop. */
8675 strm.zalloc = NULL;
8676 strm.zfree = NULL;
8677 strm.opaque = NULL;
8678 strm.avail_in = compressed_size - header_size;
8679 strm.next_in = (Bytef *) compressed_buffer + header_size;
8680 strm.avail_out = uncompressed_size;
8681 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
8683 rc = inflateInit (& strm);
8684 while (strm.avail_in > 0)
8686 if (rc != Z_OK)
8687 goto fail;
8688 strm.next_out = ((Bytef *) uncompressed_buffer
8689 + (uncompressed_size - strm.avail_out));
8690 rc = inflate (&strm, Z_FINISH);
8691 if (rc != Z_STREAM_END)
8692 goto fail;
8693 rc = inflateReset (& strm);
8695 rc = inflateEnd (& strm);
8696 if (rc != Z_OK
8697 || strm.avail_out != 0)
8698 goto fail;
8700 free (compressed_buffer);
8701 *buffer = uncompressed_buffer;
8702 *size = uncompressed_size;
8703 return 1;
8705 fail:
8706 free (uncompressed_buffer);
8707 return 0;
8708 #endif /* HAVE_ZLIB_H */
8711 static int
8712 load_specific_debug_section (enum dwarf_section_display_enum debug,
8713 Elf_Internal_Shdr * sec, void * file)
8715 struct dwarf_section * section = &debug_displays [debug].section;
8716 char buf [64];
8717 int section_is_compressed;
8719 /* If it is already loaded, do nothing. */
8720 if (section->start != NULL)
8721 return 1;
8723 section_is_compressed = section->name == section->compressed_name;
8725 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
8726 section->address = sec->sh_addr;
8727 section->size = sec->sh_size;
8728 section->start = (unsigned char *) get_data (NULL, (FILE *) file,
8729 sec->sh_offset, 1,
8730 sec->sh_size, buf);
8731 if (section->start == NULL)
8732 return 0;
8734 if (section_is_compressed)
8735 if (! uncompress_section_contents (&section->start, &section->size))
8736 return 0;
8738 if (debug_displays [debug].relocate)
8739 apply_relocations ((FILE *) file, sec, section->start);
8741 return 1;
8745 load_debug_section (enum dwarf_section_display_enum debug, void * file)
8747 struct dwarf_section * section = &debug_displays [debug].section;
8748 Elf_Internal_Shdr * sec;
8750 /* Locate the debug section. */
8751 sec = find_section (section->uncompressed_name);
8752 if (sec != NULL)
8753 section->name = section->uncompressed_name;
8754 else
8756 sec = find_section (section->compressed_name);
8757 if (sec != NULL)
8758 section->name = section->compressed_name;
8760 if (sec == NULL)
8761 return 0;
8763 return load_specific_debug_section (debug, sec, (FILE *) file);
8766 void
8767 free_debug_section (enum dwarf_section_display_enum debug)
8769 struct dwarf_section * section = &debug_displays [debug].section;
8771 if (section->start == NULL)
8772 return;
8774 free ((char *) section->start);
8775 section->start = NULL;
8776 section->address = 0;
8777 section->size = 0;
8780 static int
8781 display_debug_section (Elf_Internal_Shdr * section, FILE * file)
8783 char * name = SECTION_NAME (section);
8784 bfd_size_type length;
8785 int result = 1;
8786 int i;
8788 length = section->sh_size;
8789 if (length == 0)
8791 printf (_("\nSection '%s' has no debugging data.\n"), name);
8792 return 0;
8794 if (section->sh_type == SHT_NOBITS)
8796 /* There is no point in dumping the contents of a debugging section
8797 which has the NOBITS type - the bits in the file will be random.
8798 This can happen when a file containing a .eh_frame section is
8799 stripped with the --only-keep-debug command line option. */
8800 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"), name);
8801 return 0;
8804 if (const_strneq (name, ".gnu.linkonce.wi."))
8805 name = ".debug_info";
8807 /* See if we know how to display the contents of this section. */
8808 for (i = 0; i < max; i++)
8809 if (streq (debug_displays[i].section.uncompressed_name, name)
8810 || streq (debug_displays[i].section.compressed_name, name))
8812 struct dwarf_section * sec = &debug_displays [i].section;
8813 int secondary = (section != find_section (name));
8815 if (secondary)
8816 free_debug_section ((enum dwarf_section_display_enum) i);
8818 if (streq (debug_displays[i].section.uncompressed_name, name))
8819 sec->name = sec->uncompressed_name;
8820 else
8821 sec->name = sec->compressed_name;
8822 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
8823 section, file))
8825 result &= debug_displays[i].display (sec, file);
8827 if (secondary || (i != info && i != abbrev))
8828 free_debug_section ((enum dwarf_section_display_enum) i);
8831 break;
8834 if (i == max)
8836 printf (_("Unrecognized debug section: %s\n"), name);
8837 result = 0;
8840 return result;
8843 /* Set DUMP_SECTS for all sections where dumps were requested
8844 based on section name. */
8846 static void
8847 initialise_dumps_byname (void)
8849 struct dump_list_entry * cur;
8851 for (cur = dump_sects_byname; cur; cur = cur->next)
8853 unsigned int i;
8854 int any;
8856 for (i = 0, any = 0; i < elf_header.e_shnum; i++)
8857 if (streq (SECTION_NAME (section_headers + i), cur->name))
8859 request_dump_bynumber (i, cur->type);
8860 any = 1;
8863 if (!any)
8864 warn (_("Section '%s' was not dumped because it does not exist!\n"),
8865 cur->name);
8869 static void
8870 process_section_contents (FILE * file)
8872 Elf_Internal_Shdr * section;
8873 unsigned int i;
8875 if (! do_dump)
8876 return;
8878 initialise_dumps_byname ();
8880 for (i = 0, section = section_headers;
8881 i < elf_header.e_shnum && i < num_dump_sects;
8882 i++, section++)
8884 #ifdef SUPPORT_DISASSEMBLY
8885 if (dump_sects[i] & DISASS_DUMP)
8886 disassemble_section (section, file);
8887 #endif
8888 if (dump_sects[i] & HEX_DUMP)
8889 dump_section_as_bytes (section, file, FALSE);
8891 if (dump_sects[i] & RELOC_DUMP)
8892 dump_section_as_bytes (section, file, TRUE);
8894 if (dump_sects[i] & STRING_DUMP)
8895 dump_section_as_strings (section, file);
8897 if (dump_sects[i] & DEBUG_DUMP)
8898 display_debug_section (section, file);
8901 /* Check to see if the user requested a
8902 dump of a section that does not exist. */
8903 while (i++ < num_dump_sects)
8904 if (dump_sects[i])
8905 warn (_("Section %d was not dumped because it does not exist!\n"), i);
8908 static void
8909 process_mips_fpe_exception (int mask)
8911 if (mask)
8913 int first = 1;
8914 if (mask & OEX_FPU_INEX)
8915 fputs ("INEX", stdout), first = 0;
8916 if (mask & OEX_FPU_UFLO)
8917 printf ("%sUFLO", first ? "" : "|"), first = 0;
8918 if (mask & OEX_FPU_OFLO)
8919 printf ("%sOFLO", first ? "" : "|"), first = 0;
8920 if (mask & OEX_FPU_DIV0)
8921 printf ("%sDIV0", first ? "" : "|"), first = 0;
8922 if (mask & OEX_FPU_INVAL)
8923 printf ("%sINVAL", first ? "" : "|");
8925 else
8926 fputs ("0", stdout);
8929 /* ARM EABI attributes section. */
8930 typedef struct
8932 int tag;
8933 const char * name;
8934 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
8935 int type;
8936 const char ** table;
8937 } arm_attr_public_tag;
8939 static const char * arm_attr_tag_CPU_arch[] =
8940 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
8941 "v6K", "v7", "v6-M", "v6S-M"};
8942 static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
8943 static const char * arm_attr_tag_THUMB_ISA_use[] =
8944 {"No", "Thumb-1", "Thumb-2"};
8945 static const char * arm_attr_tag_VFP_arch[] =
8946 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16"};
8947 static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
8948 static const char * arm_attr_tag_Advanced_SIMD_arch[] = {"No", "NEONv1"};
8949 static const char * arm_attr_tag_PCS_config[] =
8950 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
8951 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
8952 static const char * arm_attr_tag_ABI_PCS_R9_use[] =
8953 {"V6", "SB", "TLS", "Unused"};
8954 static const char * arm_attr_tag_ABI_PCS_RW_data[] =
8955 {"Absolute", "PC-relative", "SB-relative", "None"};
8956 static const char * arm_attr_tag_ABI_PCS_RO_data[] =
8957 {"Absolute", "PC-relative", "None"};
8958 static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
8959 {"None", "direct", "GOT-indirect"};
8960 static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
8961 {"None", "??? 1", "2", "??? 3", "4"};
8962 static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
8963 static const char * arm_attr_tag_ABI_FP_denormal[] =
8964 {"Unused", "Needed", "Sign only"};
8965 static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
8966 static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
8967 static const char * arm_attr_tag_ABI_FP_number_model[] =
8968 {"Unused", "Finite", "RTABI", "IEEE 754"};
8969 static const char * arm_attr_tag_ABI_align8_needed[] = {"No", "Yes", "4-byte"};
8970 static const char * arm_attr_tag_ABI_align8_preserved[] =
8971 {"No", "Yes, except leaf SP", "Yes"};
8972 static const char * arm_attr_tag_ABI_enum_size[] =
8973 {"Unused", "small", "int", "forced to int"};
8974 static const char * arm_attr_tag_ABI_HardFP_use[] =
8975 {"As Tag_VFP_arch", "SP only", "DP only", "SP and DP"};
8976 static const char * arm_attr_tag_ABI_VFP_args[] =
8977 {"AAPCS", "VFP registers", "custom"};
8978 static const char * arm_attr_tag_ABI_WMMX_args[] =
8979 {"AAPCS", "WMMX registers", "custom"};
8980 static const char * arm_attr_tag_ABI_optimization_goals[] =
8981 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
8982 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
8983 static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
8984 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
8985 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
8986 static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
8987 static const char * arm_attr_tag_VFP_HP_extension[] =
8988 {"Not Allowed", "Allowed"};
8989 static const char * arm_attr_tag_ABI_FP_16bit_format[] =
8990 {"None", "IEEE 754", "Alternative Format"};
8991 static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
8992 static const char * arm_attr_tag_Virtualization_use[] =
8993 {"Not Allowed", "Allowed"};
8994 static const char * arm_attr_tag_MPextension_use[] = {"Not Allowed", "Allowed"};
8996 #define LOOKUP(id, name) \
8997 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
8998 static arm_attr_public_tag arm_attr_public_tags[] =
9000 {4, "CPU_raw_name", 1, NULL},
9001 {5, "CPU_name", 1, NULL},
9002 LOOKUP(6, CPU_arch),
9003 {7, "CPU_arch_profile", 0, NULL},
9004 LOOKUP(8, ARM_ISA_use),
9005 LOOKUP(9, THUMB_ISA_use),
9006 LOOKUP(10, VFP_arch),
9007 LOOKUP(11, WMMX_arch),
9008 LOOKUP(12, Advanced_SIMD_arch),
9009 LOOKUP(13, PCS_config),
9010 LOOKUP(14, ABI_PCS_R9_use),
9011 LOOKUP(15, ABI_PCS_RW_data),
9012 LOOKUP(16, ABI_PCS_RO_data),
9013 LOOKUP(17, ABI_PCS_GOT_use),
9014 LOOKUP(18, ABI_PCS_wchar_t),
9015 LOOKUP(19, ABI_FP_rounding),
9016 LOOKUP(20, ABI_FP_denormal),
9017 LOOKUP(21, ABI_FP_exceptions),
9018 LOOKUP(22, ABI_FP_user_exceptions),
9019 LOOKUP(23, ABI_FP_number_model),
9020 LOOKUP(24, ABI_align8_needed),
9021 LOOKUP(25, ABI_align8_preserved),
9022 LOOKUP(26, ABI_enum_size),
9023 LOOKUP(27, ABI_HardFP_use),
9024 LOOKUP(28, ABI_VFP_args),
9025 LOOKUP(29, ABI_WMMX_args),
9026 LOOKUP(30, ABI_optimization_goals),
9027 LOOKUP(31, ABI_FP_optimization_goals),
9028 {32, "compatibility", 0, NULL},
9029 LOOKUP(34, CPU_unaligned_access),
9030 LOOKUP(36, VFP_HP_extension),
9031 LOOKUP(38, ABI_FP_16bit_format),
9032 {64, "nodefaults", 0, NULL},
9033 {65, "also_compatible_with", 0, NULL},
9034 LOOKUP(66, T2EE_use),
9035 {67, "conformance", 1, NULL},
9036 LOOKUP(68, Virtualization_use),
9037 LOOKUP(70, MPextension_use)
9039 #undef LOOKUP
9041 /* Read an unsigned LEB128 encoded value from p. Set *PLEN to the number of
9042 bytes read. */
9044 static unsigned int
9045 read_uleb128 (unsigned char * p, unsigned int * plen)
9047 unsigned char c;
9048 unsigned int val;
9049 int shift;
9050 int len;
9052 val = 0;
9053 shift = 0;
9054 len = 0;
9057 c = *(p++);
9058 len++;
9059 val |= ((unsigned int)c & 0x7f) << shift;
9060 shift += 7;
9062 while (c & 0x80);
9064 *plen = len;
9065 return val;
9068 static unsigned char *
9069 display_arm_attribute (unsigned char * p)
9071 int tag;
9072 unsigned int len;
9073 int val;
9074 arm_attr_public_tag * attr;
9075 unsigned i;
9076 int type;
9078 tag = read_uleb128 (p, &len);
9079 p += len;
9080 attr = NULL;
9081 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
9083 if (arm_attr_public_tags[i].tag == tag)
9085 attr = &arm_attr_public_tags[i];
9086 break;
9090 if (attr)
9092 printf (" Tag_%s: ", attr->name);
9093 switch (attr->type)
9095 case 0:
9096 switch (tag)
9098 case 7: /* Tag_CPU_arch_profile. */
9099 val = read_uleb128 (p, &len);
9100 p += len;
9101 switch (val)
9103 case 0: printf ("None\n"); break;
9104 case 'A': printf ("Application\n"); break;
9105 case 'R': printf ("Realtime\n"); break;
9106 case 'M': printf ("Microcontroller\n"); break;
9107 default: printf ("??? (%d)\n", val); break;
9109 break;
9111 case 32: /* Tag_compatibility. */
9112 val = read_uleb128 (p, &len);
9113 p += len;
9114 printf ("flag = %d, vendor = %s\n", val, p);
9115 p += strlen ((char *) p) + 1;
9116 break;
9118 case 64: /* Tag_nodefaults. */
9119 p++;
9120 printf ("True\n");
9121 break;
9123 case 65: /* Tag_also_compatible_with. */
9124 val = read_uleb128 (p, &len);
9125 p += len;
9126 if (val == 6 /* Tag_CPU_arch. */)
9128 val = read_uleb128 (p, &len);
9129 p += len;
9130 if ((unsigned int)val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
9131 printf ("??? (%d)\n", val);
9132 else
9133 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
9135 else
9136 printf ("???\n");
9137 while (*(p++) != '\0' /* NUL terminator. */);
9138 break;
9140 default:
9141 abort ();
9143 return p;
9145 case 1:
9146 case 2:
9147 type = attr->type;
9148 break;
9150 default:
9151 assert (attr->type & 0x80);
9152 val = read_uleb128 (p, &len);
9153 p += len;
9154 type = attr->type & 0x7f;
9155 if (val >= type)
9156 printf ("??? (%d)\n", val);
9157 else
9158 printf ("%s\n", attr->table[val]);
9159 return p;
9162 else
9164 if (tag & 1)
9165 type = 1; /* String. */
9166 else
9167 type = 2; /* uleb128. */
9168 printf (" Tag_unknown_%d: ", tag);
9171 if (type == 1)
9173 printf ("\"%s\"\n", p);
9174 p += strlen ((char *) p) + 1;
9176 else
9178 val = read_uleb128 (p, &len);
9179 p += len;
9180 printf ("%d (0x%x)\n", val, val);
9183 return p;
9186 static unsigned char *
9187 display_gnu_attribute (unsigned char * p,
9188 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
9190 int tag;
9191 unsigned int len;
9192 int val;
9193 int type;
9195 tag = read_uleb128 (p, &len);
9196 p += len;
9198 /* Tag_compatibility is the only generic GNU attribute defined at
9199 present. */
9200 if (tag == 32)
9202 val = read_uleb128 (p, &len);
9203 p += len;
9204 printf ("flag = %d, vendor = %s\n", val, p);
9205 p += strlen ((char *) p) + 1;
9206 return p;
9209 if ((tag & 2) == 0 && display_proc_gnu_attribute)
9210 return display_proc_gnu_attribute (p, tag);
9212 if (tag & 1)
9213 type = 1; /* String. */
9214 else
9215 type = 2; /* uleb128. */
9216 printf (" Tag_unknown_%d: ", tag);
9218 if (type == 1)
9220 printf ("\"%s\"\n", p);
9221 p += strlen ((char *) p) + 1;
9223 else
9225 val = read_uleb128 (p, &len);
9226 p += len;
9227 printf ("%d (0x%x)\n", val, val);
9230 return p;
9233 static unsigned char *
9234 display_power_gnu_attribute (unsigned char * p, int tag)
9236 int type;
9237 unsigned int len;
9238 int val;
9240 if (tag == Tag_GNU_Power_ABI_FP)
9242 val = read_uleb128 (p, &len);
9243 p += len;
9244 printf (" Tag_GNU_Power_ABI_FP: ");
9246 switch (val)
9248 case 0:
9249 printf ("Hard or soft float\n");
9250 break;
9251 case 1:
9252 printf ("Hard float\n");
9253 break;
9254 case 2:
9255 printf ("Soft float\n");
9256 break;
9257 case 3:
9258 printf ("Single-precision hard float\n");
9259 break;
9260 default:
9261 printf ("??? (%d)\n", val);
9262 break;
9264 return p;
9267 if (tag == Tag_GNU_Power_ABI_Vector)
9269 val = read_uleb128 (p, &len);
9270 p += len;
9271 printf (" Tag_GNU_Power_ABI_Vector: ");
9272 switch (val)
9274 case 0:
9275 printf ("Any\n");
9276 break;
9277 case 1:
9278 printf ("Generic\n");
9279 break;
9280 case 2:
9281 printf ("AltiVec\n");
9282 break;
9283 case 3:
9284 printf ("SPE\n");
9285 break;
9286 default:
9287 printf ("??? (%d)\n", val);
9288 break;
9290 return p;
9293 if (tag == Tag_GNU_Power_ABI_Struct_Return)
9295 val = read_uleb128 (p, &len);
9296 p += len;
9297 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
9298 switch (val)
9300 case 0:
9301 printf ("Any\n");
9302 break;
9303 case 1:
9304 printf ("r3/r4\n");
9305 break;
9306 case 2:
9307 printf ("Memory\n");
9308 break;
9309 default:
9310 printf ("??? (%d)\n", val);
9311 break;
9313 return p;
9316 if (tag & 1)
9317 type = 1; /* String. */
9318 else
9319 type = 2; /* uleb128. */
9320 printf (" Tag_unknown_%d: ", tag);
9322 if (type == 1)
9324 printf ("\"%s\"\n", p);
9325 p += strlen ((char *) p) + 1;
9327 else
9329 val = read_uleb128 (p, &len);
9330 p += len;
9331 printf ("%d (0x%x)\n", val, val);
9334 return p;
9337 static unsigned char *
9338 display_mips_gnu_attribute (unsigned char * p, int tag)
9340 int type;
9341 unsigned int len;
9342 int val;
9344 if (tag == Tag_GNU_MIPS_ABI_FP)
9346 val = read_uleb128 (p, &len);
9347 p += len;
9348 printf (" Tag_GNU_MIPS_ABI_FP: ");
9350 switch (val)
9352 case 0:
9353 printf ("Hard or soft float\n");
9354 break;
9355 case 1:
9356 printf ("Hard float (-mdouble-float)\n");
9357 break;
9358 case 2:
9359 printf ("Hard float (-msingle-float)\n");
9360 break;
9361 case 3:
9362 printf ("Soft float\n");
9363 break;
9364 case 4:
9365 printf ("64-bit float (-mips32r2 -mfp64)\n");
9366 break;
9367 default:
9368 printf ("??? (%d)\n", val);
9369 break;
9371 return p;
9374 if (tag & 1)
9375 type = 1; /* String. */
9376 else
9377 type = 2; /* uleb128. */
9378 printf (" Tag_unknown_%d: ", tag);
9380 if (type == 1)
9382 printf ("\"%s\"\n", p);
9383 p += strlen ((char *) p) + 1;
9385 else
9387 val = read_uleb128 (p, &len);
9388 p += len;
9389 printf ("%d (0x%x)\n", val, val);
9392 return p;
9395 static int
9396 process_attributes (FILE * file,
9397 const char * public_name,
9398 unsigned int proc_type,
9399 unsigned char * (* display_pub_attribute) (unsigned char *),
9400 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
9402 Elf_Internal_Shdr * sect;
9403 unsigned char * contents;
9404 unsigned char * p;
9405 unsigned char * end;
9406 bfd_vma section_len;
9407 bfd_vma len;
9408 unsigned i;
9410 /* Find the section header so that we get the size. */
9411 for (i = 0, sect = section_headers;
9412 i < elf_header.e_shnum;
9413 i++, sect++)
9415 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
9416 continue;
9418 contents = (unsigned char *) get_data (NULL, file, sect->sh_offset, 1,
9419 sect->sh_size, _("attributes"));
9420 if (contents == NULL)
9421 continue;
9423 p = contents;
9424 if (*p == 'A')
9426 len = sect->sh_size - 1;
9427 p++;
9429 while (len > 0)
9431 int namelen;
9432 bfd_boolean public_section;
9433 bfd_boolean gnu_section;
9435 section_len = byte_get (p, 4);
9436 p += 4;
9438 if (section_len > len)
9440 printf (_("ERROR: Bad section length (%d > %d)\n"),
9441 (int) section_len, (int) len);
9442 section_len = len;
9445 len -= section_len;
9446 printf ("Attribute Section: %s\n", p);
9448 if (public_name && streq ((char *) p, public_name))
9449 public_section = TRUE;
9450 else
9451 public_section = FALSE;
9453 if (streq ((char *) p, "gnu"))
9454 gnu_section = TRUE;
9455 else
9456 gnu_section = FALSE;
9458 namelen = strlen ((char *) p) + 1;
9459 p += namelen;
9460 section_len -= namelen + 4;
9462 while (section_len > 0)
9464 int tag = *(p++);
9465 int val;
9466 bfd_vma size;
9468 size = byte_get (p, 4);
9469 if (size > section_len)
9471 printf (_("ERROR: Bad subsection length (%d > %d)\n"),
9472 (int) size, (int) section_len);
9473 size = section_len;
9476 section_len -= size;
9477 end = p + size - 1;
9478 p += 4;
9480 switch (tag)
9482 case 1:
9483 printf ("File Attributes\n");
9484 break;
9485 case 2:
9486 printf ("Section Attributes:");
9487 goto do_numlist;
9488 case 3:
9489 printf ("Symbol Attributes:");
9490 do_numlist:
9491 for (;;)
9493 unsigned int i;
9495 val = read_uleb128 (p, &i);
9496 p += i;
9497 if (val == 0)
9498 break;
9499 printf (" %d", val);
9501 printf ("\n");
9502 break;
9503 default:
9504 printf ("Unknown tag: %d\n", tag);
9505 public_section = FALSE;
9506 break;
9509 if (public_section)
9511 while (p < end)
9512 p = display_pub_attribute (p);
9514 else if (gnu_section)
9516 while (p < end)
9517 p = display_gnu_attribute (p,
9518 display_proc_gnu_attribute);
9520 else
9522 /* ??? Do something sensible, like dump hex. */
9523 printf (" Unknown section contexts\n");
9524 p = end;
9529 else
9530 printf (_("Unknown format '%c'\n"), *p);
9532 free (contents);
9534 return 1;
9537 static int
9538 process_arm_specific (FILE * file)
9540 return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
9541 display_arm_attribute, NULL);
9544 static int
9545 process_power_specific (FILE * file)
9547 return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
9548 display_power_gnu_attribute);
9551 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
9552 Print the Address, Access and Initial fields of an entry at VMA ADDR
9553 and return the VMA of the next entry. */
9555 static bfd_vma
9556 print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
9558 printf (" ");
9559 print_vma (addr, LONG_HEX);
9560 printf (" ");
9561 if (addr < pltgot + 0xfff0)
9562 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
9563 else
9564 printf ("%10s", "");
9565 printf (" ");
9566 if (data == NULL)
9567 printf ("%*s", is_32bit_elf ? 8 : 16, "<unknown>");
9568 else
9570 bfd_vma entry;
9572 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
9573 print_vma (entry, LONG_HEX);
9575 return addr + (is_32bit_elf ? 4 : 8);
9578 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
9579 PLTGOT. Print the Address and Initial fields of an entry at VMA
9580 ADDR and return the VMA of the next entry. */
9582 static bfd_vma
9583 print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
9585 printf (" ");
9586 print_vma (addr, LONG_HEX);
9587 printf (" ");
9588 if (data == NULL)
9589 printf ("%*s", is_32bit_elf ? 8 : 16, "<unknown>");
9590 else
9592 bfd_vma entry;
9594 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
9595 print_vma (entry, LONG_HEX);
9597 return addr + (is_32bit_elf ? 4 : 8);
9600 static int
9601 process_mips_specific (FILE * file)
9603 Elf_Internal_Dyn * entry;
9604 size_t liblist_offset = 0;
9605 size_t liblistno = 0;
9606 size_t conflictsno = 0;
9607 size_t options_offset = 0;
9608 size_t conflicts_offset = 0;
9609 size_t pltrelsz = 0;
9610 size_t pltrel = 0;
9611 bfd_vma pltgot = 0;
9612 bfd_vma mips_pltgot = 0;
9613 bfd_vma jmprel = 0;
9614 bfd_vma local_gotno = 0;
9615 bfd_vma gotsym = 0;
9616 bfd_vma symtabno = 0;
9618 process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
9619 display_mips_gnu_attribute);
9621 /* We have a lot of special sections. Thanks SGI! */
9622 if (dynamic_section == NULL)
9623 /* No information available. */
9624 return 0;
9626 for (entry = dynamic_section; entry->d_tag != DT_NULL; ++entry)
9627 switch (entry->d_tag)
9629 case DT_MIPS_LIBLIST:
9630 liblist_offset
9631 = offset_from_vma (file, entry->d_un.d_val,
9632 liblistno * sizeof (Elf32_External_Lib));
9633 break;
9634 case DT_MIPS_LIBLISTNO:
9635 liblistno = entry->d_un.d_val;
9636 break;
9637 case DT_MIPS_OPTIONS:
9638 options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
9639 break;
9640 case DT_MIPS_CONFLICT:
9641 conflicts_offset
9642 = offset_from_vma (file, entry->d_un.d_val,
9643 conflictsno * sizeof (Elf32_External_Conflict));
9644 break;
9645 case DT_MIPS_CONFLICTNO:
9646 conflictsno = entry->d_un.d_val;
9647 break;
9648 case DT_PLTGOT:
9649 pltgot = entry->d_un.d_ptr;
9650 break;
9651 case DT_MIPS_LOCAL_GOTNO:
9652 local_gotno = entry->d_un.d_val;
9653 break;
9654 case DT_MIPS_GOTSYM:
9655 gotsym = entry->d_un.d_val;
9656 break;
9657 case DT_MIPS_SYMTABNO:
9658 symtabno = entry->d_un.d_val;
9659 break;
9660 case DT_MIPS_PLTGOT:
9661 mips_pltgot = entry->d_un.d_ptr;
9662 break;
9663 case DT_PLTREL:
9664 pltrel = entry->d_un.d_val;
9665 break;
9666 case DT_PLTRELSZ:
9667 pltrelsz = entry->d_un.d_val;
9668 break;
9669 case DT_JMPREL:
9670 jmprel = entry->d_un.d_ptr;
9671 break;
9672 default:
9673 break;
9676 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
9678 Elf32_External_Lib * elib;
9679 size_t cnt;
9681 elib = (Elf32_External_Lib *) get_data (NULL, file, liblist_offset,
9682 liblistno,
9683 sizeof (Elf32_External_Lib),
9684 _("liblist"));
9685 if (elib)
9687 printf ("\nSection '.liblist' contains %lu entries:\n",
9688 (unsigned long) liblistno);
9689 fputs (" Library Time Stamp Checksum Version Flags\n",
9690 stdout);
9692 for (cnt = 0; cnt < liblistno; ++cnt)
9694 Elf32_Lib liblist;
9695 time_t time;
9696 char timebuf[20];
9697 struct tm * tmp;
9699 liblist.l_name = BYTE_GET (elib[cnt].l_name);
9700 time = BYTE_GET (elib[cnt].l_time_stamp);
9701 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
9702 liblist.l_version = BYTE_GET (elib[cnt].l_version);
9703 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
9705 tmp = gmtime (&time);
9706 snprintf (timebuf, sizeof (timebuf),
9707 "%04u-%02u-%02uT%02u:%02u:%02u",
9708 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9709 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9711 printf ("%3lu: ", (unsigned long) cnt);
9712 if (VALID_DYNAMIC_NAME (liblist.l_name))
9713 print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
9714 else
9715 printf ("<corrupt: %9ld>", liblist.l_name);
9716 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
9717 liblist.l_version);
9719 if (liblist.l_flags == 0)
9720 puts (" NONE");
9721 else
9723 static const struct
9725 const char * name;
9726 int bit;
9728 l_flags_vals[] =
9730 { " EXACT_MATCH", LL_EXACT_MATCH },
9731 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
9732 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
9733 { " EXPORTS", LL_EXPORTS },
9734 { " DELAY_LOAD", LL_DELAY_LOAD },
9735 { " DELTA", LL_DELTA }
9737 int flags = liblist.l_flags;
9738 size_t fcnt;
9740 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
9741 if ((flags & l_flags_vals[fcnt].bit) != 0)
9743 fputs (l_flags_vals[fcnt].name, stdout);
9744 flags ^= l_flags_vals[fcnt].bit;
9746 if (flags != 0)
9747 printf (" %#x", (unsigned int) flags);
9749 puts ("");
9753 free (elib);
9757 if (options_offset != 0)
9759 Elf_External_Options * eopt;
9760 Elf_Internal_Shdr * sect = section_headers;
9761 Elf_Internal_Options * iopt;
9762 Elf_Internal_Options * option;
9763 size_t offset;
9764 int cnt;
9766 /* Find the section header so that we get the size. */
9767 while (sect->sh_type != SHT_MIPS_OPTIONS)
9768 ++sect;
9770 eopt = (Elf_External_Options *) get_data (NULL, file, options_offset, 1,
9771 sect->sh_size, _("options"));
9772 if (eopt)
9774 iopt = (Elf_Internal_Options *)
9775 cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
9776 if (iopt == NULL)
9778 error (_("Out of memory\n"));
9779 return 0;
9782 offset = cnt = 0;
9783 option = iopt;
9785 while (offset < sect->sh_size)
9787 Elf_External_Options * eoption;
9789 eoption = (Elf_External_Options *) ((char *) eopt + offset);
9791 option->kind = BYTE_GET (eoption->kind);
9792 option->size = BYTE_GET (eoption->size);
9793 option->section = BYTE_GET (eoption->section);
9794 option->info = BYTE_GET (eoption->info);
9796 offset += option->size;
9798 ++option;
9799 ++cnt;
9802 printf (_("\nSection '%s' contains %d entries:\n"),
9803 SECTION_NAME (sect), cnt);
9805 option = iopt;
9807 while (cnt-- > 0)
9809 size_t len;
9811 switch (option->kind)
9813 case ODK_NULL:
9814 /* This shouldn't happen. */
9815 printf (" NULL %d %lx", option->section, option->info);
9816 break;
9817 case ODK_REGINFO:
9818 printf (" REGINFO ");
9819 if (elf_header.e_machine == EM_MIPS)
9821 /* 32bit form. */
9822 Elf32_External_RegInfo * ereg;
9823 Elf32_RegInfo reginfo;
9825 ereg = (Elf32_External_RegInfo *) (option + 1);
9826 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9827 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9828 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9829 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9830 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9831 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
9833 printf ("GPR %08lx GP 0x%lx\n",
9834 reginfo.ri_gprmask,
9835 (unsigned long) reginfo.ri_gp_value);
9836 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
9837 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9838 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9840 else
9842 /* 64 bit form. */
9843 Elf64_External_RegInfo * ereg;
9844 Elf64_Internal_RegInfo reginfo;
9846 ereg = (Elf64_External_RegInfo *) (option + 1);
9847 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9848 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9849 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9850 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9851 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9852 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
9854 printf ("GPR %08lx GP 0x",
9855 reginfo.ri_gprmask);
9856 printf_vma (reginfo.ri_gp_value);
9857 printf ("\n");
9859 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
9860 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9861 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9863 ++option;
9864 continue;
9865 case ODK_EXCEPTIONS:
9866 fputs (" EXCEPTIONS fpe_min(", stdout);
9867 process_mips_fpe_exception (option->info & OEX_FPU_MIN);
9868 fputs (") fpe_max(", stdout);
9869 process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
9870 fputs (")", stdout);
9872 if (option->info & OEX_PAGE0)
9873 fputs (" PAGE0", stdout);
9874 if (option->info & OEX_SMM)
9875 fputs (" SMM", stdout);
9876 if (option->info & OEX_FPDBUG)
9877 fputs (" FPDBUG", stdout);
9878 if (option->info & OEX_DISMISS)
9879 fputs (" DISMISS", stdout);
9880 break;
9881 case ODK_PAD:
9882 fputs (" PAD ", stdout);
9883 if (option->info & OPAD_PREFIX)
9884 fputs (" PREFIX", stdout);
9885 if (option->info & OPAD_POSTFIX)
9886 fputs (" POSTFIX", stdout);
9887 if (option->info & OPAD_SYMBOL)
9888 fputs (" SYMBOL", stdout);
9889 break;
9890 case ODK_HWPATCH:
9891 fputs (" HWPATCH ", stdout);
9892 if (option->info & OHW_R4KEOP)
9893 fputs (" R4KEOP", stdout);
9894 if (option->info & OHW_R8KPFETCH)
9895 fputs (" R8KPFETCH", stdout);
9896 if (option->info & OHW_R5KEOP)
9897 fputs (" R5KEOP", stdout);
9898 if (option->info & OHW_R5KCVTL)
9899 fputs (" R5KCVTL", stdout);
9900 break;
9901 case ODK_FILL:
9902 fputs (" FILL ", stdout);
9903 /* XXX Print content of info word? */
9904 break;
9905 case ODK_TAGS:
9906 fputs (" TAGS ", stdout);
9907 /* XXX Print content of info word? */
9908 break;
9909 case ODK_HWAND:
9910 fputs (" HWAND ", stdout);
9911 if (option->info & OHWA0_R4KEOP_CHECKED)
9912 fputs (" R4KEOP_CHECKED", stdout);
9913 if (option->info & OHWA0_R4KEOP_CLEAN)
9914 fputs (" R4KEOP_CLEAN", stdout);
9915 break;
9916 case ODK_HWOR:
9917 fputs (" HWOR ", stdout);
9918 if (option->info & OHWA0_R4KEOP_CHECKED)
9919 fputs (" R4KEOP_CHECKED", stdout);
9920 if (option->info & OHWA0_R4KEOP_CLEAN)
9921 fputs (" R4KEOP_CLEAN", stdout);
9922 break;
9923 case ODK_GP_GROUP:
9924 printf (" GP_GROUP %#06lx self-contained %#06lx",
9925 option->info & OGP_GROUP,
9926 (option->info & OGP_SELF) >> 16);
9927 break;
9928 case ODK_IDENT:
9929 printf (" IDENT %#06lx self-contained %#06lx",
9930 option->info & OGP_GROUP,
9931 (option->info & OGP_SELF) >> 16);
9932 break;
9933 default:
9934 /* This shouldn't happen. */
9935 printf (" %3d ??? %d %lx",
9936 option->kind, option->section, option->info);
9937 break;
9940 len = sizeof (* eopt);
9941 while (len < option->size)
9942 if (((char *) option)[len] >= ' '
9943 && ((char *) option)[len] < 0x7f)
9944 printf ("%c", ((char *) option)[len++]);
9945 else
9946 printf ("\\%03o", ((char *) option)[len++]);
9948 fputs ("\n", stdout);
9949 ++option;
9952 free (eopt);
9956 if (conflicts_offset != 0 && conflictsno != 0)
9958 Elf32_Conflict * iconf;
9959 size_t cnt;
9961 if (dynamic_symbols == NULL)
9963 error (_("conflict list found without a dynamic symbol table\n"));
9964 return 0;
9967 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
9968 if (iconf == NULL)
9970 error (_("Out of memory\n"));
9971 return 0;
9974 if (is_32bit_elf)
9976 Elf32_External_Conflict * econf32;
9978 econf32 = (Elf32_External_Conflict *)
9979 get_data (NULL, file, conflicts_offset, conflictsno,
9980 sizeof (* econf32), _("conflict"));
9981 if (!econf32)
9982 return 0;
9984 for (cnt = 0; cnt < conflictsno; ++cnt)
9985 iconf[cnt] = BYTE_GET (econf32[cnt]);
9987 free (econf32);
9989 else
9991 Elf64_External_Conflict * econf64;
9993 econf64 = (Elf64_External_Conflict *)
9994 get_data (NULL, file, conflicts_offset, conflictsno,
9995 sizeof (* econf64), _("conflict"));
9996 if (!econf64)
9997 return 0;
9999 for (cnt = 0; cnt < conflictsno; ++cnt)
10000 iconf[cnt] = BYTE_GET (econf64[cnt]);
10002 free (econf64);
10005 printf (_("\nSection '.conflict' contains %lu entries:\n"),
10006 (unsigned long) conflictsno);
10007 puts (_(" Num: Index Value Name"));
10009 for (cnt = 0; cnt < conflictsno; ++cnt)
10011 Elf_Internal_Sym * psym = & dynamic_symbols[iconf[cnt]];
10013 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
10014 print_vma (psym->st_value, FULL_HEX);
10015 putchar (' ');
10016 if (VALID_DYNAMIC_NAME (psym->st_name))
10017 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
10018 else
10019 printf ("<corrupt: %14ld>", psym->st_name);
10020 putchar ('\n');
10023 free (iconf);
10026 if (pltgot != 0 && local_gotno != 0)
10028 bfd_vma entry, local_end, global_end;
10029 size_t i, offset;
10030 unsigned char * data;
10031 int addr_size;
10033 entry = pltgot;
10034 addr_size = (is_32bit_elf ? 4 : 8);
10035 local_end = pltgot + local_gotno * addr_size;
10036 global_end = local_end + (symtabno - gotsym) * addr_size;
10038 offset = offset_from_vma (file, pltgot, global_end - pltgot);
10039 data = (unsigned char *) get_data (NULL, file, offset,
10040 global_end - pltgot, 1, _("GOT"));
10041 printf (_("\nPrimary GOT:\n"));
10042 printf (_(" Canonical gp value: "));
10043 print_vma (pltgot + 0x7ff0, LONG_HEX);
10044 printf ("\n\n");
10046 printf (_(" Reserved entries:\n"));
10047 printf (_(" %*s %10s %*s Purpose\n"),
10048 addr_size * 2, "Address", "Access",
10049 addr_size * 2, "Initial");
10050 entry = print_mips_got_entry (data, pltgot, entry);
10051 printf (" Lazy resolver\n");
10052 if (data
10053 && (byte_get (data + entry - pltgot, addr_size)
10054 >> (addr_size * 8 - 1)) != 0)
10056 entry = print_mips_got_entry (data, pltgot, entry);
10057 printf (" Module pointer (GNU extension)\n");
10059 printf ("\n");
10061 if (entry < local_end)
10063 printf (_(" Local entries:\n"));
10064 printf (_(" %*s %10s %*s\n"),
10065 addr_size * 2, "Address", "Access",
10066 addr_size * 2, "Initial");
10067 while (entry < local_end)
10069 entry = print_mips_got_entry (data, pltgot, entry);
10070 printf ("\n");
10072 printf ("\n");
10075 if (gotsym < symtabno)
10077 int sym_width;
10079 printf (_(" Global entries:\n"));
10080 printf (_(" %*s %10s %*s %*s %-7s %3s %s\n"),
10081 addr_size * 2, "Address", "Access",
10082 addr_size * 2, "Initial",
10083 addr_size * 2, "Sym.Val.", "Type", "Ndx", "Name");
10084 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
10085 for (i = gotsym; i < symtabno; i++)
10087 Elf_Internal_Sym * psym;
10089 psym = dynamic_symbols + i;
10090 entry = print_mips_got_entry (data, pltgot, entry);
10091 printf (" ");
10092 print_vma (psym->st_value, LONG_HEX);
10093 printf (" %-7s %3s ",
10094 get_symbol_type (ELF_ST_TYPE (psym->st_info)),
10095 get_symbol_index_type (psym->st_shndx));
10096 if (VALID_DYNAMIC_NAME (psym->st_name))
10097 print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
10098 else
10099 printf ("<corrupt: %14ld>", psym->st_name);
10100 printf ("\n");
10102 printf ("\n");
10105 if (data)
10106 free (data);
10109 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
10111 bfd_vma entry, end;
10112 size_t offset, rel_offset;
10113 unsigned long count, i;
10114 unsigned char * data;
10115 int addr_size, sym_width;
10116 Elf_Internal_Rela * rels;
10118 rel_offset = offset_from_vma (file, jmprel, pltrelsz);
10119 if (pltrel == DT_RELA)
10121 if (!slurp_rela_relocs (file, rel_offset, pltrelsz, &rels, &count))
10122 return 0;
10124 else
10126 if (!slurp_rel_relocs (file, rel_offset, pltrelsz, &rels, &count))
10127 return 0;
10130 entry = mips_pltgot;
10131 addr_size = (is_32bit_elf ? 4 : 8);
10132 end = mips_pltgot + (2 + count) * addr_size;
10134 offset = offset_from_vma (file, mips_pltgot, end - mips_pltgot);
10135 data = (unsigned char *) get_data (NULL, file, offset, end - mips_pltgot,
10136 1, _("PLT GOT"));
10137 printf (_("\nPLT GOT:\n\n"));
10138 printf (_(" Reserved entries:\n"));
10139 printf (_(" %*s %*s Purpose\n"),
10140 addr_size * 2, "Address", addr_size * 2, "Initial");
10141 entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
10142 printf (" PLT lazy resolver\n");
10143 entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
10144 printf (" Module pointer\n");
10145 printf ("\n");
10147 printf (_(" Entries:\n"));
10148 printf (_(" %*s %*s %*s %-7s %3s %s\n"),
10149 addr_size * 2, "Address",
10150 addr_size * 2, "Initial",
10151 addr_size * 2, "Sym.Val.", "Type", "Ndx", "Name");
10152 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
10153 for (i = 0; i < count; i++)
10155 Elf_Internal_Sym * psym;
10157 psym = dynamic_symbols + get_reloc_symindex (rels[i].r_info);
10158 entry = print_mips_pltgot_entry (data, mips_pltgot, entry);
10159 printf (" ");
10160 print_vma (psym->st_value, LONG_HEX);
10161 printf (" %-7s %3s ",
10162 get_symbol_type (ELF_ST_TYPE (psym->st_info)),
10163 get_symbol_index_type (psym->st_shndx));
10164 if (VALID_DYNAMIC_NAME (psym->st_name))
10165 print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
10166 else
10167 printf ("<corrupt: %14ld>", psym->st_name);
10168 printf ("\n");
10170 printf ("\n");
10172 if (data)
10173 free (data);
10174 free (rels);
10177 return 1;
10180 static int
10181 process_gnu_liblist (FILE * file)
10183 Elf_Internal_Shdr * section;
10184 Elf_Internal_Shdr * string_sec;
10185 Elf32_External_Lib * elib;
10186 char * strtab;
10187 size_t strtab_size;
10188 size_t cnt;
10189 unsigned i;
10191 if (! do_arch)
10192 return 0;
10194 for (i = 0, section = section_headers;
10195 i < elf_header.e_shnum;
10196 i++, section++)
10198 switch (section->sh_type)
10200 case SHT_GNU_LIBLIST:
10201 if (section->sh_link >= elf_header.e_shnum)
10202 break;
10204 elib = (Elf32_External_Lib *)
10205 get_data (NULL, file, section->sh_offset, 1, section->sh_size,
10206 _("liblist"));
10208 if (elib == NULL)
10209 break;
10210 string_sec = section_headers + section->sh_link;
10212 strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
10213 string_sec->sh_size,
10214 _("liblist string table"));
10215 strtab_size = string_sec->sh_size;
10217 if (strtab == NULL
10218 || section->sh_entsize != sizeof (Elf32_External_Lib))
10220 free (elib);
10221 break;
10224 printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
10225 SECTION_NAME (section),
10226 (unsigned long) (section->sh_size / sizeof (Elf32_External_Lib)));
10228 puts (" Library Time Stamp Checksum Version Flags");
10230 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
10231 ++cnt)
10233 Elf32_Lib liblist;
10234 time_t time;
10235 char timebuf[20];
10236 struct tm * tmp;
10238 liblist.l_name = BYTE_GET (elib[cnt].l_name);
10239 time = BYTE_GET (elib[cnt].l_time_stamp);
10240 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
10241 liblist.l_version = BYTE_GET (elib[cnt].l_version);
10242 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
10244 tmp = gmtime (&time);
10245 snprintf (timebuf, sizeof (timebuf),
10246 "%04u-%02u-%02uT%02u:%02u:%02u",
10247 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10248 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10250 printf ("%3lu: ", (unsigned long) cnt);
10251 if (do_wide)
10252 printf ("%-20s", liblist.l_name < strtab_size
10253 ? strtab + liblist.l_name : "<corrupt>");
10254 else
10255 printf ("%-20.20s", liblist.l_name < strtab_size
10256 ? strtab + liblist.l_name : "<corrupt>");
10257 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
10258 liblist.l_version, liblist.l_flags);
10261 free (elib);
10265 return 1;
10268 static const char *
10269 get_note_type (unsigned e_type)
10271 static char buff[64];
10273 if (elf_header.e_type == ET_CORE)
10274 switch (e_type)
10276 case NT_AUXV:
10277 return _("NT_AUXV (auxiliary vector)");
10278 case NT_PRSTATUS:
10279 return _("NT_PRSTATUS (prstatus structure)");
10280 case NT_FPREGSET:
10281 return _("NT_FPREGSET (floating point registers)");
10282 case NT_PRPSINFO:
10283 return _("NT_PRPSINFO (prpsinfo structure)");
10284 case NT_TASKSTRUCT:
10285 return _("NT_TASKSTRUCT (task structure)");
10286 case NT_PRXFPREG:
10287 return _("NT_PRXFPREG (user_xfpregs structure)");
10288 case NT_PPC_VMX:
10289 return _("NT_PPC_VMX (ppc Altivec registers)");
10290 case NT_PPC_VSX:
10291 return _("NT_PPC_VSX (ppc VSX registers)");
10292 case NT_PSTATUS:
10293 return _("NT_PSTATUS (pstatus structure)");
10294 case NT_FPREGS:
10295 return _("NT_FPREGS (floating point registers)");
10296 case NT_PSINFO:
10297 return _("NT_PSINFO (psinfo structure)");
10298 case NT_LWPSTATUS:
10299 return _("NT_LWPSTATUS (lwpstatus_t structure)");
10300 case NT_LWPSINFO:
10301 return _("NT_LWPSINFO (lwpsinfo_t structure)");
10302 case NT_WIN32PSTATUS:
10303 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
10304 default:
10305 break;
10307 else
10308 switch (e_type)
10310 case NT_VERSION:
10311 return _("NT_VERSION (version)");
10312 case NT_ARCH:
10313 return _("NT_ARCH (architecture)");
10314 default:
10315 break;
10318 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10319 return buff;
10322 static const char *
10323 get_gnu_elf_note_type (unsigned e_type)
10325 static char buff[64];
10327 switch (e_type)
10329 case NT_GNU_ABI_TAG:
10330 return _("NT_GNU_ABI_TAG (ABI version tag)");
10331 case NT_GNU_HWCAP:
10332 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
10333 case NT_GNU_BUILD_ID:
10334 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
10335 case NT_GNU_GOLD_VERSION:
10336 return _("NT_GNU_GOLD_VERSION (gold version)");
10337 default:
10338 break;
10341 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10342 return buff;
10345 static const char *
10346 get_netbsd_elfcore_note_type (unsigned e_type)
10348 static char buff[64];
10350 if (e_type == NT_NETBSDCORE_PROCINFO)
10352 /* NetBSD core "procinfo" structure. */
10353 return _("NetBSD procinfo structure");
10356 /* As of Jan 2002 there are no other machine-independent notes
10357 defined for NetBSD core files. If the note type is less
10358 than the start of the machine-dependent note types, we don't
10359 understand it. */
10361 if (e_type < NT_NETBSDCORE_FIRSTMACH)
10363 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
10364 return buff;
10367 switch (elf_header.e_machine)
10369 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
10370 and PT_GETFPREGS == mach+2. */
10372 case EM_OLD_ALPHA:
10373 case EM_ALPHA:
10374 case EM_SPARC:
10375 case EM_SPARC32PLUS:
10376 case EM_SPARCV9:
10377 switch (e_type)
10379 case NT_NETBSDCORE_FIRSTMACH+0:
10380 return _("PT_GETREGS (reg structure)");
10381 case NT_NETBSDCORE_FIRSTMACH+2:
10382 return _("PT_GETFPREGS (fpreg structure)");
10383 default:
10384 break;
10386 break;
10388 /* On all other arch's, PT_GETREGS == mach+1 and
10389 PT_GETFPREGS == mach+3. */
10390 default:
10391 switch (e_type)
10393 case NT_NETBSDCORE_FIRSTMACH+1:
10394 return _("PT_GETREGS (reg structure)");
10395 case NT_NETBSDCORE_FIRSTMACH+3:
10396 return _("PT_GETFPREGS (fpreg structure)");
10397 default:
10398 break;
10402 snprintf (buff, sizeof (buff), _("PT_FIRSTMACH+%d"),
10403 e_type - NT_NETBSDCORE_FIRSTMACH);
10404 return buff;
10407 /* Note that by the ELF standard, the name field is already null byte
10408 terminated, and namesz includes the terminating null byte.
10409 I.E. the value of namesz for the name "FSF" is 4.
10411 If the value of namesz is zero, there is no name present. */
10412 static int
10413 process_note (Elf_Internal_Note * pnote)
10415 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
10416 const char * nt;
10418 if (pnote->namesz == 0)
10419 /* If there is no note name, then use the default set of
10420 note type strings. */
10421 nt = get_note_type (pnote->type);
10423 else if (const_strneq (pnote->namedata, "GNU"))
10424 /* GNU-specific object file notes. */
10425 nt = get_gnu_elf_note_type (pnote->type);
10427 else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
10428 /* NetBSD-specific core file notes. */
10429 nt = get_netbsd_elfcore_note_type (pnote->type);
10431 else if (strneq (pnote->namedata, "SPU/", 4))
10433 /* SPU-specific core file notes. */
10434 nt = pnote->namedata + 4;
10435 name = "SPU";
10438 else
10439 /* Don't recognize this note name; just use the default set of
10440 note type strings. */
10441 nt = get_note_type (pnote->type);
10443 printf (" %s\t\t0x%08lx\t%s\n", name, pnote->descsz, nt);
10444 return 1;
10448 static int
10449 process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
10451 Elf_External_Note * pnotes;
10452 Elf_External_Note * external;
10453 int res = 1;
10455 if (length <= 0)
10456 return 0;
10458 pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
10459 _("notes"));
10460 if (!pnotes)
10461 return 0;
10463 external = pnotes;
10465 printf (_("\nNotes at offset 0x%08lx with length 0x%08lx:\n"),
10466 (unsigned long) offset, (unsigned long) length);
10467 printf (_(" Owner\t\tData size\tDescription\n"));
10469 while (external < (Elf_External_Note *) ((char *) pnotes + length))
10471 Elf_External_Note * next;
10472 Elf_Internal_Note inote;
10473 char * temp = NULL;
10475 inote.type = BYTE_GET (external->type);
10476 inote.namesz = BYTE_GET (external->namesz);
10477 inote.namedata = external->name;
10478 inote.descsz = BYTE_GET (external->descsz);
10479 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
10480 inote.descpos = offset + (inote.descdata - (char *) pnotes);
10482 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
10484 if (((char *) next) > (((char *) pnotes) + length))
10486 warn (_("corrupt note found at offset %lx into core notes\n"),
10487 (unsigned long) ((char *) external - (char *) pnotes));
10488 warn (_(" type: %lx, namesize: %08lx, descsize: %08lx\n"),
10489 inote.type, inote.namesz, inote.descsz);
10490 break;
10493 external = next;
10495 /* Verify that name is null terminated. It appears that at least
10496 one version of Linux (RedHat 6.0) generates corefiles that don't
10497 comply with the ELF spec by failing to include the null byte in
10498 namesz. */
10499 if (inote.namedata[inote.namesz] != '\0')
10501 temp = (char *) malloc (inote.namesz + 1);
10503 if (temp == NULL)
10505 error (_("Out of memory\n"));
10506 res = 0;
10507 break;
10510 strncpy (temp, inote.namedata, inote.namesz);
10511 temp[inote.namesz] = 0;
10513 /* warn (_("'%s' NOTE name not properly null terminated\n"), temp); */
10514 inote.namedata = temp;
10517 res &= process_note (& inote);
10519 if (temp != NULL)
10521 free (temp);
10522 temp = NULL;
10526 free (pnotes);
10528 return res;
10531 static int
10532 process_corefile_note_segments (FILE * file)
10534 Elf_Internal_Phdr * segment;
10535 unsigned int i;
10536 int res = 1;
10538 if (! get_program_headers (file))
10539 return 0;
10541 for (i = 0, segment = program_headers;
10542 i < elf_header.e_phnum;
10543 i++, segment++)
10545 if (segment->p_type == PT_NOTE)
10546 res &= process_corefile_note_segment (file,
10547 (bfd_vma) segment->p_offset,
10548 (bfd_vma) segment->p_filesz);
10551 return res;
10554 static int
10555 process_note_sections (FILE * file)
10557 Elf_Internal_Shdr * section;
10558 unsigned long i;
10559 int res = 1;
10561 for (i = 0, section = section_headers;
10562 i < elf_header.e_shnum;
10563 i++, section++)
10564 if (section->sh_type == SHT_NOTE)
10565 res &= process_corefile_note_segment (file,
10566 (bfd_vma) section->sh_offset,
10567 (bfd_vma) section->sh_size);
10569 return res;
10572 static int
10573 process_notes (FILE * file)
10575 /* If we have not been asked to display the notes then do nothing. */
10576 if (! do_notes)
10577 return 1;
10579 if (elf_header.e_type != ET_CORE)
10580 return process_note_sections (file);
10582 /* No program headers means no NOTE segment. */
10583 if (elf_header.e_phnum > 0)
10584 return process_corefile_note_segments (file);
10586 printf (_("No note segments present in the core file.\n"));
10587 return 1;
10590 static int
10591 process_arch_specific (FILE * file)
10593 if (! do_arch)
10594 return 1;
10596 switch (elf_header.e_machine)
10598 case EM_ARM:
10599 return process_arm_specific (file);
10600 case EM_MIPS:
10601 case EM_MIPS_RS3_LE:
10602 return process_mips_specific (file);
10603 break;
10604 case EM_PPC:
10605 return process_power_specific (file);
10606 break;
10607 default:
10608 break;
10610 return 1;
10613 static int
10614 get_file_header (FILE * file)
10616 /* Read in the identity array. */
10617 if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
10618 return 0;
10620 /* Determine how to read the rest of the header. */
10621 switch (elf_header.e_ident[EI_DATA])
10623 default: /* fall through */
10624 case ELFDATANONE: /* fall through */
10625 case ELFDATA2LSB:
10626 byte_get = byte_get_little_endian;
10627 byte_put = byte_put_little_endian;
10628 break;
10629 case ELFDATA2MSB:
10630 byte_get = byte_get_big_endian;
10631 byte_put = byte_put_big_endian;
10632 break;
10635 /* For now we only support 32 bit and 64 bit ELF files. */
10636 is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
10638 /* Read in the rest of the header. */
10639 if (is_32bit_elf)
10641 Elf32_External_Ehdr ehdr32;
10643 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
10644 return 0;
10646 elf_header.e_type = BYTE_GET (ehdr32.e_type);
10647 elf_header.e_machine = BYTE_GET (ehdr32.e_machine);
10648 elf_header.e_version = BYTE_GET (ehdr32.e_version);
10649 elf_header.e_entry = BYTE_GET (ehdr32.e_entry);
10650 elf_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
10651 elf_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
10652 elf_header.e_flags = BYTE_GET (ehdr32.e_flags);
10653 elf_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
10654 elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
10655 elf_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
10656 elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
10657 elf_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
10658 elf_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
10660 else
10662 Elf64_External_Ehdr ehdr64;
10664 /* If we have been compiled with sizeof (bfd_vma) == 4, then
10665 we will not be able to cope with the 64bit data found in
10666 64 ELF files. Detect this now and abort before we start
10667 overwriting things. */
10668 if (sizeof (bfd_vma) < 8)
10670 error (_("This instance of readelf has been built without support for a\n\
10671 64 bit data type and so it cannot read 64 bit ELF files.\n"));
10672 return 0;
10675 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
10676 return 0;
10678 elf_header.e_type = BYTE_GET (ehdr64.e_type);
10679 elf_header.e_machine = BYTE_GET (ehdr64.e_machine);
10680 elf_header.e_version = BYTE_GET (ehdr64.e_version);
10681 elf_header.e_entry = BYTE_GET (ehdr64.e_entry);
10682 elf_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
10683 elf_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
10684 elf_header.e_flags = BYTE_GET (ehdr64.e_flags);
10685 elf_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
10686 elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
10687 elf_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
10688 elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
10689 elf_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
10690 elf_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
10693 if (elf_header.e_shoff)
10695 /* There may be some extensions in the first section header. Don't
10696 bomb if we can't read it. */
10697 if (is_32bit_elf)
10698 get_32bit_section_headers (file, 1);
10699 else
10700 get_64bit_section_headers (file, 1);
10703 return 1;
10706 /* Process one ELF object file according to the command line options.
10707 This file may actually be stored in an archive. The file is
10708 positioned at the start of the ELF object. */
10710 static int
10711 process_object (char * file_name, FILE * file)
10713 unsigned int i;
10715 if (! get_file_header (file))
10717 error (_("%s: Failed to read file header\n"), file_name);
10718 return 1;
10721 /* Initialise per file variables. */
10722 for (i = ARRAY_SIZE (version_info); i--;)
10723 version_info[i] = 0;
10725 for (i = ARRAY_SIZE (dynamic_info); i--;)
10726 dynamic_info[i] = 0;
10728 /* Process the file. */
10729 if (show_name)
10730 printf (_("\nFile: %s\n"), file_name);
10732 /* Initialise the dump_sects array from the cmdline_dump_sects array.
10733 Note we do this even if cmdline_dump_sects is empty because we
10734 must make sure that the dump_sets array is zeroed out before each
10735 object file is processed. */
10736 if (num_dump_sects > num_cmdline_dump_sects)
10737 memset (dump_sects, 0, num_dump_sects * sizeof (* dump_sects));
10739 if (num_cmdline_dump_sects > 0)
10741 if (num_dump_sects == 0)
10742 /* A sneaky way of allocating the dump_sects array. */
10743 request_dump_bynumber (num_cmdline_dump_sects, 0);
10745 assert (num_dump_sects >= num_cmdline_dump_sects);
10746 memcpy (dump_sects, cmdline_dump_sects,
10747 num_cmdline_dump_sects * sizeof (* dump_sects));
10750 if (! process_file_header ())
10751 return 1;
10753 if (! process_section_headers (file))
10755 /* Without loaded section headers we cannot process lots of
10756 things. */
10757 do_unwind = do_version = do_dump = do_arch = 0;
10759 if (! do_using_dynamic)
10760 do_syms = do_reloc = 0;
10763 if (! process_section_groups (file))
10765 /* Without loaded section groups we cannot process unwind. */
10766 do_unwind = 0;
10769 if (process_program_headers (file))
10770 process_dynamic_section (file);
10772 process_relocs (file);
10774 process_unwind (file);
10776 process_symbol_table (file);
10778 process_syminfo (file);
10780 process_version_sections (file);
10782 process_section_contents (file);
10784 process_notes (file);
10786 process_gnu_liblist (file);
10788 process_arch_specific (file);
10790 if (program_headers)
10792 free (program_headers);
10793 program_headers = NULL;
10796 if (section_headers)
10798 free (section_headers);
10799 section_headers = NULL;
10802 if (string_table)
10804 free (string_table);
10805 string_table = NULL;
10806 string_table_length = 0;
10809 if (dynamic_strings)
10811 free (dynamic_strings);
10812 dynamic_strings = NULL;
10813 dynamic_strings_length = 0;
10816 if (dynamic_symbols)
10818 free (dynamic_symbols);
10819 dynamic_symbols = NULL;
10820 num_dynamic_syms = 0;
10823 if (dynamic_syminfo)
10825 free (dynamic_syminfo);
10826 dynamic_syminfo = NULL;
10829 if (section_headers_groups)
10831 free (section_headers_groups);
10832 section_headers_groups = NULL;
10835 if (section_groups)
10837 struct group_list * g;
10838 struct group_list * next;
10840 for (i = 0; i < group_count; i++)
10842 for (g = section_groups [i].root; g != NULL; g = next)
10844 next = g->next;
10845 free (g);
10849 free (section_groups);
10850 section_groups = NULL;
10853 free_debug_memory ();
10855 return 0;
10858 /* Return the path name for a proxy entry in a thin archive, adjusted relative
10859 to the path name of the thin archive itself if necessary. Always returns
10860 a pointer to malloc'ed memory. */
10862 static char *
10863 adjust_relative_path (char * file_name, char * name, int name_len)
10865 char * member_file_name;
10866 const char * base_name = lbasename (file_name);
10868 /* This is a proxy entry for a thin archive member.
10869 If the extended name table contains an absolute path
10870 name, or if the archive is in the current directory,
10871 use the path name as given. Otherwise, we need to
10872 find the member relative to the directory where the
10873 archive is located. */
10874 if (IS_ABSOLUTE_PATH (name) || base_name == file_name)
10876 member_file_name = (char *) malloc (name_len + 1);
10877 if (member_file_name == NULL)
10879 error (_("Out of memory\n"));
10880 return NULL;
10882 memcpy (member_file_name, name, name_len);
10883 member_file_name[name_len] = '\0';
10885 else
10887 /* Concatenate the path components of the archive file name
10888 to the relative path name from the extended name table. */
10889 size_t prefix_len = base_name - file_name;
10890 member_file_name = (char *) malloc (prefix_len + name_len + 1);
10891 if (member_file_name == NULL)
10893 error (_("Out of memory\n"));
10894 return NULL;
10896 memcpy (member_file_name, file_name, prefix_len);
10897 memcpy (member_file_name + prefix_len, name, name_len);
10898 member_file_name[prefix_len + name_len] = '\0';
10900 return member_file_name;
10903 /* Structure to hold information about an archive file. */
10905 struct archive_info
10907 char * file_name; /* Archive file name. */
10908 FILE * file; /* Open file descriptor. */
10909 unsigned long index_num; /* Number of symbols in table. */
10910 unsigned long * index_array; /* The array of member offsets. */
10911 char * sym_table; /* The symbol table. */
10912 unsigned long sym_size; /* Size of the symbol table. */
10913 char * longnames; /* The long file names table. */
10914 unsigned long longnames_size; /* Size of the long file names table. */
10915 unsigned long nested_member_origin; /* Origin in the nested archive of the current member. */
10916 unsigned long next_arhdr_offset; /* Offset of the next archive header. */
10917 bfd_boolean is_thin_archive; /* TRUE if this is a thin archive. */
10918 struct ar_hdr arhdr; /* Current archive header. */
10921 /* Read the symbol table and long-name table from an archive. */
10923 static int
10924 setup_archive (struct archive_info * arch, char * file_name, FILE * file,
10925 bfd_boolean is_thin_archive, bfd_boolean read_symbols)
10927 size_t got;
10928 unsigned long size;
10930 arch->file_name = strdup (file_name);
10931 arch->file = file;
10932 arch->index_num = 0;
10933 arch->index_array = NULL;
10934 arch->sym_table = NULL;
10935 arch->sym_size = 0;
10936 arch->longnames = NULL;
10937 arch->longnames_size = 0;
10938 arch->nested_member_origin = 0;
10939 arch->is_thin_archive = is_thin_archive;
10940 arch->next_arhdr_offset = SARMAG;
10942 /* Read the first archive member header. */
10943 if (fseek (file, SARMAG, SEEK_SET) != 0)
10945 error (_("%s: failed to seek to first archive header\n"), file_name);
10946 return 1;
10948 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
10949 if (got != sizeof arch->arhdr)
10951 if (got == 0)
10952 return 0;
10954 error (_("%s: failed to read archive header\n"), file_name);
10955 return 1;
10958 /* See if this is the archive symbol table. */
10959 if (const_strneq (arch->arhdr.ar_name, "/ ")
10960 || const_strneq (arch->arhdr.ar_name, "/SYM64/ "))
10962 size = strtoul (arch->arhdr.ar_size, NULL, 10);
10963 size = size + (size & 1);
10965 arch->next_arhdr_offset += sizeof arch->arhdr + size;
10967 if (read_symbols)
10969 unsigned long i;
10970 /* A buffer used to hold numbers read in from an archive index.
10971 These are always 4 bytes long and stored in big-endian format. */
10972 #define SIZEOF_AR_INDEX_NUMBERS 4
10973 unsigned char integer_buffer[SIZEOF_AR_INDEX_NUMBERS];
10974 unsigned char * index_buffer;
10976 /* Check the size of the archive index. */
10977 if (size < SIZEOF_AR_INDEX_NUMBERS)
10979 error (_("%s: the archive index is empty\n"), file_name);
10980 return 1;
10983 /* Read the numer of entries in the archive index. */
10984 got = fread (integer_buffer, 1, sizeof integer_buffer, file);
10985 if (got != sizeof (integer_buffer))
10987 error (_("%s: failed to read archive index\n"), file_name);
10988 return 1;
10990 arch->index_num = byte_get_big_endian (integer_buffer, sizeof integer_buffer);
10991 size -= SIZEOF_AR_INDEX_NUMBERS;
10993 /* Read in the archive index. */
10994 if (size < arch->index_num * SIZEOF_AR_INDEX_NUMBERS)
10996 error (_("%s: the archive index is supposed to have %ld entries, but the size in the header is too small\n"),
10997 file_name, arch->index_num);
10998 return 1;
11000 index_buffer = (unsigned char *)
11001 malloc (arch->index_num * SIZEOF_AR_INDEX_NUMBERS);
11002 if (index_buffer == NULL)
11004 error (_("Out of memory whilst trying to read archive symbol index\n"));
11005 return 1;
11007 got = fread (index_buffer, SIZEOF_AR_INDEX_NUMBERS, arch->index_num, file);
11008 if (got != arch->index_num)
11010 free (index_buffer);
11011 error (_("%s: failed to read archive index\n"), file_name);
11012 return 1;
11014 size -= arch->index_num * SIZEOF_AR_INDEX_NUMBERS;
11016 /* Convert the index numbers into the host's numeric format. */
11017 arch->index_array = (long unsigned int *)
11018 malloc (arch->index_num * sizeof (* arch->index_array));
11019 if (arch->index_array == NULL)
11021 free (index_buffer);
11022 error (_("Out of memory whilst trying to convert the archive symbol index\n"));
11023 return 1;
11026 for (i = 0; i < arch->index_num; i++)
11027 arch->index_array[i] = byte_get_big_endian ((unsigned char *) (index_buffer + (i * SIZEOF_AR_INDEX_NUMBERS)),
11028 SIZEOF_AR_INDEX_NUMBERS);
11029 free (index_buffer);
11031 /* The remaining space in the header is taken up by the symbol table. */
11032 if (size < 1)
11034 error (_("%s: the archive has an index but no symbols\n"), file_name);
11035 return 1;
11037 arch->sym_table = (char *) malloc (size);
11038 arch->sym_size = size;
11039 if (arch->sym_table == NULL)
11041 error (_("Out of memory whilst trying to read archive index symbol table\n"));
11042 return 1;
11044 got = fread (arch->sym_table, 1, size, file);
11045 if (got != size)
11047 error (_("%s: failed to read archive index symbol table\n"), file_name);
11048 return 1;
11051 else
11053 if (fseek (file, size, SEEK_CUR) != 0)
11055 error (_("%s: failed to skip archive symbol table\n"), file_name);
11056 return 1;
11060 /* Read the next archive header. */
11061 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
11062 if (got != sizeof arch->arhdr)
11064 if (got == 0)
11065 return 0;
11066 error (_("%s: failed to read archive header following archive index\n"), file_name);
11067 return 1;
11070 else if (read_symbols)
11071 printf (_("%s has no archive index\n"), file_name);
11073 if (const_strneq (arch->arhdr.ar_name, "// "))
11075 /* This is the archive string table holding long member names. */
11076 arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
11077 arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
11079 arch->longnames = (char *) malloc (arch->longnames_size);
11080 if (arch->longnames == NULL)
11082 error (_("Out of memory reading long symbol names in archive\n"));
11083 return 1;
11086 if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
11088 free (arch->longnames);
11089 arch->longnames = NULL;
11090 error (_("%s: failed to read long symbol name string table\n"), file_name);
11091 return 1;
11094 if ((arch->longnames_size & 1) != 0)
11095 getc (file);
11098 return 0;
11101 /* Release the memory used for the archive information. */
11103 static void
11104 release_archive (struct archive_info * arch)
11106 if (arch->file_name != NULL)
11107 free (arch->file_name);
11108 if (arch->index_array != NULL)
11109 free (arch->index_array);
11110 if (arch->sym_table != NULL)
11111 free (arch->sym_table);
11112 if (arch->longnames != NULL)
11113 free (arch->longnames);
11116 /* Open and setup a nested archive, if not already open. */
11118 static int
11119 setup_nested_archive (struct archive_info * nested_arch, char * member_file_name)
11121 FILE * member_file;
11123 /* Have we already setup this archive? */
11124 if (nested_arch->file_name != NULL
11125 && streq (nested_arch->file_name, member_file_name))
11126 return 0;
11128 /* Close previous file and discard cached information. */
11129 if (nested_arch->file != NULL)
11130 fclose (nested_arch->file);
11131 release_archive (nested_arch);
11133 member_file = fopen (member_file_name, "rb");
11134 if (member_file == NULL)
11135 return 1;
11136 return setup_archive (nested_arch, member_file_name, member_file, FALSE, FALSE);
11139 static char *
11140 get_archive_member_name_at (struct archive_info * arch,
11141 unsigned long offset,
11142 struct archive_info * nested_arch);
11144 /* Get the name of an archive member from the current archive header.
11145 For simple names, this will modify the ar_name field of the current
11146 archive header. For long names, it will return a pointer to the
11147 longnames table. For nested archives, it will open the nested archive
11148 and get the name recursively. NESTED_ARCH is a single-entry cache so
11149 we don't keep rereading the same information from a nested archive. */
11151 static char *
11152 get_archive_member_name (struct archive_info * arch,
11153 struct archive_info * nested_arch)
11155 unsigned long j, k;
11157 if (arch->arhdr.ar_name[0] == '/')
11159 /* We have a long name. */
11160 char * endp;
11161 char * member_file_name;
11162 char * member_name;
11164 arch->nested_member_origin = 0;
11165 k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
11166 if (arch->is_thin_archive && endp != NULL && * endp == ':')
11167 arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
11169 while ((j < arch->longnames_size)
11170 && (arch->longnames[j] != '\n')
11171 && (arch->longnames[j] != '\0'))
11172 j++;
11173 if (arch->longnames[j-1] == '/')
11174 j--;
11175 arch->longnames[j] = '\0';
11177 if (!arch->is_thin_archive || arch->nested_member_origin == 0)
11178 return arch->longnames + k;
11180 /* This is a proxy for a member of a nested archive.
11181 Find the name of the member in that archive. */
11182 member_file_name = adjust_relative_path (arch->file_name, arch->longnames + k, j - k);
11183 if (member_file_name != NULL
11184 && setup_nested_archive (nested_arch, member_file_name) == 0
11185 && (member_name = get_archive_member_name_at (nested_arch, arch->nested_member_origin, NULL)) != NULL)
11187 free (member_file_name);
11188 return member_name;
11190 free (member_file_name);
11192 /* Last resort: just return the name of the nested archive. */
11193 return arch->longnames + k;
11196 /* We have a normal (short) name. */
11197 j = 0;
11198 while ((arch->arhdr.ar_name[j] != '/') && (j < 16))
11199 j++;
11200 arch->arhdr.ar_name[j] = '\0';
11201 return arch->arhdr.ar_name;
11204 /* Get the name of an archive member at a given OFFSET within an archive ARCH. */
11206 static char *
11207 get_archive_member_name_at (struct archive_info * arch,
11208 unsigned long offset,
11209 struct archive_info * nested_arch)
11211 size_t got;
11213 if (fseek (arch->file, offset, SEEK_SET) != 0)
11215 error (_("%s: failed to seek to next file name\n"), arch->file_name);
11216 return NULL;
11218 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
11219 if (got != sizeof arch->arhdr)
11221 error (_("%s: failed to read archive header\n"), arch->file_name);
11222 return NULL;
11224 if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
11226 error (_("%s: did not find a valid archive header\n"), arch->file_name);
11227 return NULL;
11230 return get_archive_member_name (arch, nested_arch);
11233 /* Construct a string showing the name of the archive member, qualified
11234 with the name of the containing archive file. For thin archives, we
11235 use square brackets to denote the indirection. For nested archives,
11236 we show the qualified name of the external member inside the square
11237 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
11239 static char *
11240 make_qualified_name (struct archive_info * arch,
11241 struct archive_info * nested_arch,
11242 char * member_name)
11244 size_t len;
11245 char * name;
11247 len = strlen (arch->file_name) + strlen (member_name) + 3;
11248 if (arch->is_thin_archive && arch->nested_member_origin != 0)
11249 len += strlen (nested_arch->file_name) + 2;
11251 name = (char *) malloc (len);
11252 if (name == NULL)
11254 error (_("Out of memory\n"));
11255 return NULL;
11258 if (arch->is_thin_archive && arch->nested_member_origin != 0)
11259 snprintf (name, len, "%s[%s(%s)]", arch->file_name, nested_arch->file_name, member_name);
11260 else if (arch->is_thin_archive)
11261 snprintf (name, len, "%s[%s]", arch->file_name, member_name);
11262 else
11263 snprintf (name, len, "%s(%s)", arch->file_name, member_name);
11265 return name;
11268 /* Process an ELF archive.
11269 On entry the file is positioned just after the ARMAG string. */
11271 static int
11272 process_archive (char * file_name, FILE * file, bfd_boolean is_thin_archive)
11274 struct archive_info arch;
11275 struct archive_info nested_arch;
11276 size_t got;
11277 size_t file_name_size;
11278 int ret;
11280 show_name = 1;
11282 /* The ARCH structure is used to hold information about this archive. */
11283 arch.file_name = NULL;
11284 arch.file = NULL;
11285 arch.index_array = NULL;
11286 arch.sym_table = NULL;
11287 arch.longnames = NULL;
11289 /* The NESTED_ARCH structure is used as a single-item cache of information
11290 about a nested archive (when members of a thin archive reside within
11291 another regular archive file). */
11292 nested_arch.file_name = NULL;
11293 nested_arch.file = NULL;
11294 nested_arch.index_array = NULL;
11295 nested_arch.sym_table = NULL;
11296 nested_arch.longnames = NULL;
11298 if (setup_archive (&arch, file_name, file, is_thin_archive, do_archive_index) != 0)
11300 ret = 1;
11301 goto out;
11304 if (do_archive_index)
11306 if (arch.sym_table == NULL)
11307 error (_("%s: unable to dump the index as none was found\n"), file_name);
11308 else
11310 unsigned int i, l;
11311 unsigned long current_pos;
11313 printf (_("Index of archive %s: (%ld entries, 0x%lx bytes in the symbol table)\n"),
11314 file_name, arch.index_num, arch.sym_size);
11315 current_pos = ftell (file);
11317 for (i = l = 0; i < arch.index_num; i++)
11319 if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
11321 char * member_name;
11323 member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
11325 if (member_name != NULL)
11327 char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
11329 if (qualified_name != NULL)
11331 printf (_("Binary %s contains:\n"), qualified_name);
11332 free (qualified_name);
11337 if (l >= arch.sym_size)
11339 error (_("%s: end of the symbol table reached before the end of the index\n"),
11340 file_name);
11341 break;
11343 printf ("\t%s\n", arch.sym_table + l);
11344 l += strlen (arch.sym_table + l) + 1;
11347 if (l & 01)
11348 ++l;
11349 if (l < arch.sym_size)
11350 error (_("%s: symbols remain in the index symbol table, but without corresponding entries in the index table\n"),
11351 file_name);
11353 if (fseek (file, current_pos, SEEK_SET) != 0)
11355 error (_("%s: failed to seek back to start of object files in the archive\n"), file_name);
11356 ret = 1;
11357 goto out;
11361 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
11362 && !do_segments && !do_header && !do_dump && !do_version
11363 && !do_histogram && !do_debugging && !do_arch && !do_notes
11364 && !do_section_groups)
11366 ret = 0; /* Archive index only. */
11367 goto out;
11371 file_name_size = strlen (file_name);
11372 ret = 0;
11374 while (1)
11376 char * name;
11377 size_t namelen;
11378 char * qualified_name;
11380 /* Read the next archive header. */
11381 if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
11383 error (_("%s: failed to seek to next archive header\n"), file_name);
11384 return 1;
11386 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
11387 if (got != sizeof arch.arhdr)
11389 if (got == 0)
11390 break;
11391 error (_("%s: failed to read archive header\n"), file_name);
11392 ret = 1;
11393 break;
11395 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
11397 error (_("%s: did not find a valid archive header\n"), arch.file_name);
11398 ret = 1;
11399 break;
11402 arch.next_arhdr_offset += sizeof arch.arhdr;
11404 archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
11405 if (archive_file_size & 01)
11406 ++archive_file_size;
11408 name = get_archive_member_name (&arch, &nested_arch);
11409 if (name == NULL)
11411 error (_("%s: bad archive file name\n"), file_name);
11412 ret = 1;
11413 break;
11415 namelen = strlen (name);
11417 qualified_name = make_qualified_name (&arch, &nested_arch, name);
11418 if (qualified_name == NULL)
11420 error (_("%s: bad archive file name\n"), file_name);
11421 ret = 1;
11422 break;
11425 if (is_thin_archive && arch.nested_member_origin == 0)
11427 /* This is a proxy for an external member of a thin archive. */
11428 FILE * member_file;
11429 char * member_file_name = adjust_relative_path (file_name, name, namelen);
11430 if (member_file_name == NULL)
11432 ret = 1;
11433 break;
11436 member_file = fopen (member_file_name, "rb");
11437 if (member_file == NULL)
11439 error (_("Input file '%s' is not readable.\n"), member_file_name);
11440 free (member_file_name);
11441 ret = 1;
11442 break;
11445 archive_file_offset = arch.nested_member_origin;
11447 ret |= process_object (qualified_name, member_file);
11449 fclose (member_file);
11450 free (member_file_name);
11452 else if (is_thin_archive)
11454 /* This is a proxy for a member of a nested archive. */
11455 archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
11457 /* The nested archive file will have been opened and setup by
11458 get_archive_member_name. */
11459 if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
11461 error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
11462 ret = 1;
11463 break;
11466 ret |= process_object (qualified_name, nested_arch.file);
11468 else
11470 archive_file_offset = arch.next_arhdr_offset;
11471 arch.next_arhdr_offset += archive_file_size;
11473 ret |= process_object (qualified_name, file);
11476 free (qualified_name);
11479 out:
11480 if (nested_arch.file != NULL)
11481 fclose (nested_arch.file);
11482 release_archive (&nested_arch);
11483 release_archive (&arch);
11485 return ret;
11488 static int
11489 process_file (char * file_name)
11491 FILE * file;
11492 struct stat statbuf;
11493 char armag[SARMAG];
11494 int ret;
11496 if (stat (file_name, &statbuf) < 0)
11498 if (errno == ENOENT)
11499 error (_("'%s': No such file\n"), file_name);
11500 else
11501 error (_("Could not locate '%s'. System error message: %s\n"),
11502 file_name, strerror (errno));
11503 return 1;
11506 if (! S_ISREG (statbuf.st_mode))
11508 error (_("'%s' is not an ordinary file\n"), file_name);
11509 return 1;
11512 file = fopen (file_name, "rb");
11513 if (file == NULL)
11515 error (_("Input file '%s' is not readable.\n"), file_name);
11516 return 1;
11519 if (fread (armag, SARMAG, 1, file) != 1)
11521 error (_("%s: Failed to read file's magic number\n"), file_name);
11522 fclose (file);
11523 return 1;
11526 if (memcmp (armag, ARMAG, SARMAG) == 0)
11527 ret = process_archive (file_name, file, FALSE);
11528 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
11529 ret = process_archive (file_name, file, TRUE);
11530 else
11532 if (do_archive_index)
11533 error (_("File %s is not an archive so its index cannot be displayed.\n"),
11534 file_name);
11536 rewind (file);
11537 archive_file_size = archive_file_offset = 0;
11538 ret = process_object (file_name, file);
11541 fclose (file);
11543 return ret;
11546 #ifdef SUPPORT_DISASSEMBLY
11547 /* Needed by the i386 disassembler. For extra credit, someone could
11548 fix this so that we insert symbolic addresses here, esp for GOT/PLT
11549 symbols. */
11551 void
11552 print_address (unsigned int addr, FILE * outfile)
11554 fprintf (outfile,"0x%8.8x", addr);
11557 /* Needed by the i386 disassembler. */
11558 void
11559 db_task_printsym (unsigned int addr)
11561 print_address (addr, stderr);
11563 #endif
11566 main (int argc, char ** argv)
11568 int err;
11570 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
11571 setlocale (LC_MESSAGES, "");
11572 #endif
11573 #if defined (HAVE_SETLOCALE)
11574 setlocale (LC_CTYPE, "");
11575 #endif
11576 bindtextdomain (PACKAGE, LOCALEDIR);
11577 textdomain (PACKAGE);
11579 expandargv (&argc, &argv);
11581 parse_args (argc, argv);
11583 if (num_dump_sects > 0)
11585 /* Make a copy of the dump_sects array. */
11586 cmdline_dump_sects = (dump_type *)
11587 malloc (num_dump_sects * sizeof (* dump_sects));
11588 if (cmdline_dump_sects == NULL)
11589 error (_("Out of memory allocating dump request table.\n"));
11590 else
11592 memcpy (cmdline_dump_sects, dump_sects,
11593 num_dump_sects * sizeof (* dump_sects));
11594 num_cmdline_dump_sects = num_dump_sects;
11598 if (optind < (argc - 1))
11599 show_name = 1;
11601 err = 0;
11602 while (optind < argc)
11603 err |= process_file (argv[optind++]);
11605 if (dump_sects != NULL)
11606 free (dump_sects);
11607 if (cmdline_dump_sects != NULL)
11608 free (cmdline_dump_sects);
11610 return err;