NaCl changes
[nacl-binutils.git] / binutils / readelf.c
blobda8946383fba023d5a829b12df03f2ab42cd4b69
1 /* readelf.c -- display contents of an ELF format file
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 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 "sysdep.h"
45 #include <assert.h>
46 #include <sys/stat.h>
47 #include <time.h>
49 /* for PATH_MAX */
50 #ifdef HAVE_LIMITS_H
51 #include <limits.h>
52 #endif
54 #ifndef PATH_MAX
55 /* for MAXPATHLEN */
56 # ifdef HAVE_SYS_PARAM_H
57 # include <sys/param.h>
58 # endif
59 # ifndef PATH_MAX
60 # ifdef MAXPATHLEN
61 # define PATH_MAX MAXPATHLEN
62 # else
63 # define PATH_MAX 1024
64 # endif
65 # endif
66 #endif
68 #if __GNUC__ >= 2
69 /* Define BFD64 here, even if our default architecture is 32 bit ELF
70 as this will allow us to read in and parse 64bit and 32bit ELF files.
71 Only do this if we believe that the compiler can support a 64 bit
72 data type. For now we only rely on GCC being able to do this. */
73 #define BFD64
74 #endif
76 #include "bfd.h"
77 #include "bucomm.h"
78 #include "dwarf.h"
80 #include "elf/common.h"
81 #include "elf/external.h"
82 #include "elf/internal.h"
85 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
86 we can obtain the H8 reloc numbers. We need these for the
87 get_reloc_size() function. We include h8.h again after defining
88 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
90 #include "elf/h8.h"
91 #undef _ELF_H8_H
93 /* Undo the effects of #including reloc-macros.h. */
95 #undef START_RELOC_NUMBERS
96 #undef RELOC_NUMBER
97 #undef FAKE_RELOC
98 #undef EMPTY_RELOC
99 #undef END_RELOC_NUMBERS
100 #undef _RELOC_MACROS_H
102 /* The following headers use the elf/reloc-macros.h file to
103 automatically generate relocation recognition functions
104 such as elf_mips_reloc_type() */
106 #define RELOC_MACROS_GEN_FUNC
108 #include "elf/alpha.h"
109 #include "elf/arc.h"
110 #include "elf/arm.h"
111 #include "elf/avr.h"
112 #include "elf/bfin.h"
113 #include "elf/cr16.h"
114 #include "elf/cris.h"
115 #include "elf/crx.h"
116 #include "elf/d10v.h"
117 #include "elf/d30v.h"
118 #include "elf/dlx.h"
119 #include "elf/fr30.h"
120 #include "elf/frv.h"
121 #include "elf/h8.h"
122 #include "elf/hppa.h"
123 #include "elf/i386.h"
124 #include "elf/i370.h"
125 #include "elf/i860.h"
126 #include "elf/i960.h"
127 #include "elf/ia64.h"
128 #include "elf/ip2k.h"
129 #include "elf/iq2000.h"
130 #include "elf/m32c.h"
131 #include "elf/m32r.h"
132 #include "elf/m68k.h"
133 #include "elf/m68hc11.h"
134 #include "elf/mcore.h"
135 #include "elf/mep.h"
136 #include "elf/mips.h"
137 #include "elf/mmix.h"
138 #include "elf/mn10200.h"
139 #include "elf/mn10300.h"
140 #include "elf/mt.h"
141 #include "elf/msp430.h"
142 #include "elf/or32.h"
143 #include "elf/pj.h"
144 #include "elf/ppc.h"
145 #include "elf/ppc64.h"
146 #include "elf/s390.h"
147 #include "elf/score.h"
148 #include "elf/sh.h"
149 #include "elf/sparc.h"
150 #include "elf/spu.h"
151 #include "elf/v850.h"
152 #include "elf/vax.h"
153 #include "elf/x86-64.h"
154 #include "elf/xstormy16.h"
155 #include "elf/xtensa.h"
157 #include "aout/ar.h"
159 #include "getopt.h"
160 #include "libiberty.h"
162 char *program_name = "readelf";
163 static long archive_file_offset;
164 static unsigned long archive_file_size;
165 static unsigned long dynamic_addr;
166 static bfd_size_type dynamic_size;
167 static unsigned int dynamic_nent;
168 static char *dynamic_strings;
169 static unsigned long dynamic_strings_length;
170 static char *string_table;
171 static unsigned long string_table_length;
172 static unsigned long num_dynamic_syms;
173 static Elf_Internal_Sym *dynamic_symbols;
174 static Elf_Internal_Syminfo *dynamic_syminfo;
175 static unsigned long dynamic_syminfo_offset;
176 static unsigned int dynamic_syminfo_nent;
177 static char program_interpreter[PATH_MAX];
178 static bfd_vma dynamic_info[DT_JMPREL + 1];
179 static bfd_vma dynamic_info_DT_GNU_HASH;
180 static bfd_vma version_info[16];
181 static Elf_Internal_Ehdr elf_header;
182 static Elf_Internal_Shdr *section_headers;
183 static Elf_Internal_Phdr *program_headers;
184 static Elf_Internal_Dyn *dynamic_section;
185 static Elf_Internal_Shdr *symtab_shndx_hdr;
186 static int show_name;
187 static int do_dynamic;
188 static int do_syms;
189 static int do_reloc;
190 static int do_sections;
191 static int do_section_groups;
192 static int do_section_details;
193 static int do_segments;
194 static int do_unwind;
195 static int do_using_dynamic;
196 static int do_header;
197 static int do_dump;
198 static int do_version;
199 static int do_wide;
200 static int do_histogram;
201 static int do_debugging;
202 static int do_arch;
203 static int do_notes;
204 static int is_32bit_elf;
206 struct group_list
208 struct group_list *next;
209 unsigned int section_index;
212 struct group
214 struct group_list *root;
215 unsigned int group_index;
218 static size_t group_count;
219 static struct group *section_groups;
220 static struct group **section_headers_groups;
222 /* A linked list of the section names for which dumps were requested
223 by name. */
224 struct dump_list_entry
226 char *name;
227 int type;
228 struct dump_list_entry *next;
230 static struct dump_list_entry *dump_sects_byname;
232 /* A dynamic array of flags indicating for which sections a hex dump
233 has been requested (via the -x switch) and/or a disassembly dump
234 (via the -i switch). */
235 char *cmdline_dump_sects = NULL;
236 unsigned num_cmdline_dump_sects = 0;
238 /* A dynamic array of flags indicating for which sections a dump of
239 some kind has been requested. It is reset on a per-object file
240 basis and then initialised from the cmdline_dump_sects array,
241 the results of interpreting the -w switch, and the
242 dump_sects_byname list. */
243 char *dump_sects = NULL;
244 unsigned int num_dump_sects = 0;
246 #define HEX_DUMP (1 << 0)
247 #define DISASS_DUMP (1 << 1)
248 #define DEBUG_DUMP (1 << 2)
250 /* How to print a vma value. */
251 typedef enum print_mode
253 HEX,
254 DEC,
255 DEC_5,
256 UNSIGNED,
257 PREFIX_HEX,
258 FULL_HEX,
259 LONG_HEX
261 print_mode;
263 static void (*byte_put) (unsigned char *, bfd_vma, int);
265 #define UNKNOWN -1
267 #define SECTION_NAME(X) \
268 ((X) == NULL ? "<none>" \
269 : string_table == NULL ? "<no-name>" \
270 : ((X)->sh_name >= string_table_length ? "<corrupt>" \
271 : string_table + (X)->sh_name))
273 /* Given st_shndx I, map to section_headers index. */
274 #define SECTION_HEADER_INDEX(I) \
275 ((I) < SHN_LORESERVE \
276 ? (I) \
277 : ((I) <= SHN_HIRESERVE \
278 ? 0 \
279 : (I) - (SHN_HIRESERVE + 1 - SHN_LORESERVE)))
281 /* Reverse of the above. */
282 #define SECTION_HEADER_NUM(N) \
283 ((N) < SHN_LORESERVE \
284 ? (N) \
285 : (N) + (SHN_HIRESERVE + 1 - SHN_LORESERVE))
287 #define SECTION_HEADER(I) (section_headers + SECTION_HEADER_INDEX (I))
289 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
291 #define BYTE_GET(field) byte_get (field, sizeof (field))
293 #define GET_ELF_SYMBOLS(file, section) \
294 (is_32bit_elf ? get_32bit_elf_symbols (file, section) \
295 : get_64bit_elf_symbols (file, section))
297 #define VALID_DYNAMIC_NAME(offset) ((dynamic_strings != NULL) && (offset < dynamic_strings_length))
298 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
299 already been called and verified that the string exists. */
300 #define GET_DYNAMIC_NAME(offset) (dynamic_strings + offset)
302 /* This is just a bit of syntatic sugar. */
303 #define streq(a,b) (strcmp ((a), (b)) == 0)
304 #define strneq(a,b,n) (strncmp ((a), (b), (n)) == 0)
305 #define const_strneq(a,b) (strncmp ((a), (b), sizeof (b) - 1) == 0)
307 static void *
308 get_data (void *var, FILE *file, long offset, size_t size, size_t nmemb,
309 const char *reason)
311 void *mvar;
313 if (size == 0 || nmemb == 0)
314 return NULL;
316 if (fseek (file, archive_file_offset + offset, SEEK_SET))
318 error (_("Unable to seek to 0x%lx for %s\n"),
319 archive_file_offset + offset, reason);
320 return NULL;
323 mvar = var;
324 if (mvar == NULL)
326 /* Check for overflow. */
327 if (nmemb < (~(size_t) 0 - 1) / size)
328 /* + 1 so that we can '\0' terminate invalid string table sections. */
329 mvar = malloc (size * nmemb + 1);
331 if (mvar == NULL)
333 error (_("Out of memory allocating 0x%lx bytes for %s\n"),
334 (unsigned long)(size * nmemb), reason);
335 return NULL;
338 ((char *) mvar)[size * nmemb] = '\0';
341 if (fread (mvar, size, nmemb, file) != nmemb)
343 error (_("Unable to read in 0x%lx bytes of %s\n"),
344 (unsigned long)(size * nmemb), reason);
345 if (mvar != var)
346 free (mvar);
347 return NULL;
350 return mvar;
353 static void
354 byte_put_little_endian (unsigned char *field, bfd_vma value, int size)
356 switch (size)
358 case 8:
359 field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
360 field[6] = ((value >> 24) >> 24) & 0xff;
361 field[5] = ((value >> 24) >> 16) & 0xff;
362 field[4] = ((value >> 24) >> 8) & 0xff;
363 /* Fall through. */
364 case 4:
365 field[3] = (value >> 24) & 0xff;
366 field[2] = (value >> 16) & 0xff;
367 /* Fall through. */
368 case 2:
369 field[1] = (value >> 8) & 0xff;
370 /* Fall through. */
371 case 1:
372 field[0] = value & 0xff;
373 break;
375 default:
376 error (_("Unhandled data length: %d\n"), size);
377 abort ();
381 #if defined BFD64 && !BFD_HOST_64BIT_LONG
382 static int
383 print_dec_vma (bfd_vma vma, int is_signed)
385 char buf[40];
386 char *bufp = buf;
387 int nc = 0;
389 if (is_signed && (bfd_signed_vma) vma < 0)
391 vma = -vma;
392 putchar ('-');
393 nc = 1;
398 *bufp++ = '0' + vma % 10;
399 vma /= 10;
401 while (vma != 0);
402 nc += bufp - buf;
404 while (bufp > buf)
405 putchar (*--bufp);
406 return nc;
409 static int
410 print_hex_vma (bfd_vma vma)
412 char buf[32];
413 char *bufp = buf;
414 int nc;
418 char digit = '0' + (vma & 0x0f);
419 if (digit > '9')
420 digit += 'a' - '0' - 10;
421 *bufp++ = digit;
422 vma >>= 4;
424 while (vma != 0);
425 nc = bufp - buf;
427 while (bufp > buf)
428 putchar (*--bufp);
429 return nc;
431 #endif
433 /* Print a VMA value. */
434 static int
435 print_vma (bfd_vma vma, print_mode mode)
437 #ifdef BFD64
438 if (is_32bit_elf)
439 #endif
441 switch (mode)
443 case FULL_HEX:
444 return printf ("0x%8.8lx", (unsigned long) vma);
446 case LONG_HEX:
447 return printf ("%8.8lx", (unsigned long) vma);
449 case DEC_5:
450 if (vma <= 99999)
451 return printf ("%5ld", (long) vma);
452 /* Drop through. */
454 case PREFIX_HEX:
455 return printf ("0x%lx", (unsigned long) vma);
457 case HEX:
458 return printf ("%lx", (unsigned long) vma);
460 case DEC:
461 return printf ("%ld", (unsigned long) vma);
463 case UNSIGNED:
464 return printf ("%lu", (unsigned long) vma);
467 #ifdef BFD64
468 else
470 int nc = 0;
472 switch (mode)
474 case FULL_HEX:
475 nc = printf ("0x");
476 /* Drop through. */
478 case LONG_HEX:
479 printf_vma (vma);
480 return nc + 16;
482 case PREFIX_HEX:
483 nc = printf ("0x");
484 /* Drop through. */
486 case HEX:
487 #if BFD_HOST_64BIT_LONG
488 return nc + printf ("%lx", vma);
489 #else
490 return nc + print_hex_vma (vma);
491 #endif
493 case DEC:
494 #if BFD_HOST_64BIT_LONG
495 return printf ("%ld", vma);
496 #else
497 return print_dec_vma (vma, 1);
498 #endif
500 case DEC_5:
501 #if BFD_HOST_64BIT_LONG
502 if (vma <= 99999)
503 return printf ("%5ld", vma);
504 else
505 return printf ("%#lx", vma);
506 #else
507 if (vma <= 99999)
508 return printf ("%5ld", _bfd_int64_low (vma));
509 else
510 return print_hex_vma (vma);
511 #endif
513 case UNSIGNED:
514 #if BFD_HOST_64BIT_LONG
515 return printf ("%lu", vma);
516 #else
517 return print_dec_vma (vma, 0);
518 #endif
521 #endif
522 return 0;
525 /* Display a symbol on stdout. If do_wide is not true then
526 format the symbol to be at most WIDTH characters,
527 truncating as necessary. If WIDTH is negative then
528 format the string to be exactly - WIDTH characters,
529 truncating or padding as necessary. */
531 static void
532 print_symbol (int width, const char *symbol)
534 if (do_wide)
535 printf ("%s", symbol);
536 else if (width < 0)
537 printf ("%-*.*s", width, width, symbol);
538 else
539 printf ("%-.*s", width, symbol);
542 static void
543 byte_put_big_endian (unsigned char *field, bfd_vma value, int size)
545 switch (size)
547 case 8:
548 field[7] = value & 0xff;
549 field[6] = (value >> 8) & 0xff;
550 field[5] = (value >> 16) & 0xff;
551 field[4] = (value >> 24) & 0xff;
552 value >>= 16;
553 value >>= 16;
554 /* Fall through. */
555 case 4:
556 field[3] = value & 0xff;
557 field[2] = (value >> 8) & 0xff;
558 value >>= 16;
559 /* Fall through. */
560 case 2:
561 field[1] = value & 0xff;
562 value >>= 8;
563 /* Fall through. */
564 case 1:
565 field[0] = value & 0xff;
566 break;
568 default:
569 error (_("Unhandled data length: %d\n"), size);
570 abort ();
574 /* Return a pointer to section NAME, or NULL if no such section exists. */
576 static Elf_Internal_Shdr *
577 find_section (const char *name)
579 unsigned int i;
581 for (i = 0; i < elf_header.e_shnum; i++)
582 if (streq (SECTION_NAME (section_headers + i), name))
583 return section_headers + i;
585 return NULL;
588 /* Guess the relocation size commonly used by the specific machines. */
590 static int
591 guess_is_rela (unsigned long e_machine)
593 switch (e_machine)
595 /* Targets that use REL relocations. */
596 case EM_386:
597 case EM_486:
598 case EM_960:
599 case EM_ARM:
600 case EM_D10V:
601 case EM_CYGNUS_D10V:
602 case EM_DLX:
603 case EM_MIPS:
604 case EM_MIPS_RS3_LE:
605 case EM_CYGNUS_M32R:
606 case EM_OPENRISC:
607 case EM_OR32:
608 case EM_SCORE:
609 return FALSE;
611 /* Targets that use RELA relocations. */
612 case EM_68K:
613 case EM_860:
614 case EM_ALPHA:
615 case EM_ALTERA_NIOS2:
616 case EM_AVR:
617 case EM_AVR_OLD:
618 case EM_BLACKFIN:
619 case EM_CR16:
620 case EM_CRIS:
621 case EM_CRX:
622 case EM_D30V:
623 case EM_CYGNUS_D30V:
624 case EM_FR30:
625 case EM_CYGNUS_FR30:
626 case EM_CYGNUS_FRV:
627 case EM_H8S:
628 case EM_H8_300:
629 case EM_H8_300H:
630 case EM_IA_64:
631 case EM_IP2K:
632 case EM_IP2K_OLD:
633 case EM_IQ2000:
634 case EM_M32C:
635 case EM_M32R:
636 case EM_MCORE:
637 case EM_CYGNUS_MEP:
638 case EM_MMIX:
639 case EM_MN10200:
640 case EM_CYGNUS_MN10200:
641 case EM_MN10300:
642 case EM_CYGNUS_MN10300:
643 case EM_MSP430:
644 case EM_MSP430_OLD:
645 case EM_MT:
646 case EM_NIOS32:
647 case EM_PPC64:
648 case EM_PPC:
649 case EM_S390:
650 case EM_S390_OLD:
651 case EM_SH:
652 case EM_SPARC:
653 case EM_SPARC32PLUS:
654 case EM_SPARCV9:
655 case EM_SPU:
656 case EM_V850:
657 case EM_CYGNUS_V850:
658 case EM_VAX:
659 case EM_X86_64:
660 case EM_XSTORMY16:
661 case EM_XTENSA:
662 case EM_XTENSA_OLD:
663 return TRUE;
665 case EM_68HC05:
666 case EM_68HC08:
667 case EM_68HC11:
668 case EM_68HC16:
669 case EM_FX66:
670 case EM_ME16:
671 case EM_MMA:
672 case EM_NCPU:
673 case EM_NDR1:
674 case EM_PCP:
675 case EM_ST100:
676 case EM_ST19:
677 case EM_ST7:
678 case EM_ST9PLUS:
679 case EM_STARCORE:
680 case EM_SVX:
681 case EM_TINYJ:
682 default:
683 warn (_("Don't know about relocations on this machine architecture\n"));
684 return FALSE;
688 static int
689 slurp_rela_relocs (FILE *file,
690 unsigned long rel_offset,
691 unsigned long rel_size,
692 Elf_Internal_Rela **relasp,
693 unsigned long *nrelasp)
695 Elf_Internal_Rela *relas;
696 unsigned long nrelas;
697 unsigned int i;
699 if (is_32bit_elf)
701 Elf32_External_Rela *erelas;
703 erelas = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
704 if (!erelas)
705 return 0;
707 nrelas = rel_size / sizeof (Elf32_External_Rela);
709 relas = cmalloc (nrelas, sizeof (Elf_Internal_Rela));
711 if (relas == NULL)
713 free (erelas);
714 error (_("out of memory parsing relocs\n"));
715 return 0;
718 for (i = 0; i < nrelas; i++)
720 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
721 relas[i].r_info = BYTE_GET (erelas[i].r_info);
722 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
725 free (erelas);
727 else
729 Elf64_External_Rela *erelas;
731 erelas = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
732 if (!erelas)
733 return 0;
735 nrelas = rel_size / sizeof (Elf64_External_Rela);
737 relas = cmalloc (nrelas, sizeof (Elf_Internal_Rela));
739 if (relas == NULL)
741 free (erelas);
742 error (_("out of memory parsing relocs\n"));
743 return 0;
746 for (i = 0; i < nrelas; i++)
748 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
749 relas[i].r_info = BYTE_GET (erelas[i].r_info);
750 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
753 free (erelas);
755 *relasp = relas;
756 *nrelasp = nrelas;
757 return 1;
760 static int
761 slurp_rel_relocs (FILE *file,
762 unsigned long rel_offset,
763 unsigned long rel_size,
764 Elf_Internal_Rela **relsp,
765 unsigned long *nrelsp)
767 Elf_Internal_Rela *rels;
768 unsigned long nrels;
769 unsigned int i;
771 if (is_32bit_elf)
773 Elf32_External_Rel *erels;
775 erels = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
776 if (!erels)
777 return 0;
779 nrels = rel_size / sizeof (Elf32_External_Rel);
781 rels = cmalloc (nrels, sizeof (Elf_Internal_Rela));
783 if (rels == NULL)
785 free (erels);
786 error (_("out of memory parsing relocs\n"));
787 return 0;
790 for (i = 0; i < nrels; i++)
792 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
793 rels[i].r_info = BYTE_GET (erels[i].r_info);
794 rels[i].r_addend = 0;
797 free (erels);
799 else
801 Elf64_External_Rel *erels;
803 erels = get_data (NULL, file, rel_offset, 1, rel_size, _("relocs"));
804 if (!erels)
805 return 0;
807 nrels = rel_size / sizeof (Elf64_External_Rel);
809 rels = cmalloc (nrels, sizeof (Elf_Internal_Rela));
811 if (rels == NULL)
813 free (erels);
814 error (_("out of memory parsing relocs\n"));
815 return 0;
818 for (i = 0; i < nrels; i++)
820 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
821 rels[i].r_info = BYTE_GET (erels[i].r_info);
822 rels[i].r_addend = 0;
825 free (erels);
827 *relsp = rels;
828 *nrelsp = nrels;
829 return 1;
832 /* Display the contents of the relocation data found at the specified
833 offset. */
835 static int
836 dump_relocations (FILE *file,
837 unsigned long rel_offset,
838 unsigned long rel_size,
839 Elf_Internal_Sym *symtab,
840 unsigned long nsyms,
841 char *strtab,
842 unsigned long strtablen,
843 int is_rela)
845 unsigned int i;
846 Elf_Internal_Rela *rels;
849 if (is_rela == UNKNOWN)
850 is_rela = guess_is_rela (elf_header.e_machine);
852 if (is_rela)
854 if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
855 return 0;
857 else
859 if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
860 return 0;
863 if (is_32bit_elf)
865 if (is_rela)
867 if (do_wide)
868 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
869 else
870 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
872 else
874 if (do_wide)
875 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
876 else
877 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
880 else
882 if (is_rela)
884 if (do_wide)
885 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
886 else
887 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
889 else
891 if (do_wide)
892 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
893 else
894 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
898 for (i = 0; i < rel_size; i++)
900 const char *rtype;
901 const char *rtype2 = NULL;
902 const char *rtype3 = NULL;
903 bfd_vma offset;
904 bfd_vma info;
905 bfd_vma symtab_index;
906 bfd_vma type;
907 bfd_vma type2 = 0;
908 bfd_vma type3 = 0;
910 offset = rels[i].r_offset;
911 info = rels[i].r_info;
913 if (is_32bit_elf)
915 type = ELF32_R_TYPE (info);
916 symtab_index = ELF32_R_SYM (info);
918 else
920 /* The #ifdef BFD64 below is to prevent a compile time warning.
921 We know that if we do not have a 64 bit data type that we
922 will never execute this code anyway. */
923 #ifdef BFD64
924 if (elf_header.e_machine == EM_MIPS)
926 /* In little-endian objects, r_info isn't really a 64-bit
927 little-endian value: it has a 32-bit little-endian
928 symbol index followed by four individual byte fields.
929 Reorder INFO accordingly. */
930 if (elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
931 info = (((info & 0xffffffff) << 32)
932 | ((info >> 56) & 0xff)
933 | ((info >> 40) & 0xff00)
934 | ((info >> 24) & 0xff0000)
935 | ((info >> 8) & 0xff000000));
936 type = ELF64_MIPS_R_TYPE (info);
937 type2 = ELF64_MIPS_R_TYPE2 (info);
938 type3 = ELF64_MIPS_R_TYPE3 (info);
940 else if (elf_header.e_machine == EM_SPARCV9)
941 type = ELF64_R_TYPE_ID (info);
942 else
943 type = ELF64_R_TYPE (info);
945 symtab_index = ELF64_R_SYM (info);
946 #endif
949 if (is_32bit_elf)
951 #ifdef _bfd_int64_low
952 printf ("%8.8lx %8.8lx ", _bfd_int64_low (offset), _bfd_int64_low (info));
953 #else
954 printf ("%8.8lx %8.8lx ", offset, info);
955 #endif
957 else
959 #ifdef _bfd_int64_low
960 printf (do_wide
961 ? "%8.8lx%8.8lx %8.8lx%8.8lx "
962 : "%4.4lx%8.8lx %4.4lx%8.8lx ",
963 _bfd_int64_high (offset),
964 _bfd_int64_low (offset),
965 _bfd_int64_high (info),
966 _bfd_int64_low (info));
967 #else
968 printf (do_wide
969 ? "%16.16lx %16.16lx "
970 : "%12.12lx %12.12lx ",
971 offset, info);
972 #endif
975 switch (elf_header.e_machine)
977 default:
978 rtype = NULL;
979 break;
981 case EM_M32R:
982 case EM_CYGNUS_M32R:
983 rtype = elf_m32r_reloc_type (type);
984 break;
986 case EM_386:
987 case EM_486:
988 rtype = elf_i386_reloc_type (type);
989 break;
991 case EM_68HC11:
992 case EM_68HC12:
993 rtype = elf_m68hc11_reloc_type (type);
994 break;
996 case EM_68K:
997 rtype = elf_m68k_reloc_type (type);
998 break;
1000 case EM_960:
1001 rtype = elf_i960_reloc_type (type);
1002 break;
1004 case EM_AVR:
1005 case EM_AVR_OLD:
1006 rtype = elf_avr_reloc_type (type);
1007 break;
1009 case EM_OLD_SPARCV9:
1010 case EM_SPARC32PLUS:
1011 case EM_SPARCV9:
1012 case EM_SPARC:
1013 rtype = elf_sparc_reloc_type (type);
1014 break;
1016 case EM_SPU:
1017 rtype = elf_spu_reloc_type (type);
1018 break;
1020 case EM_V850:
1021 case EM_CYGNUS_V850:
1022 rtype = v850_reloc_type (type);
1023 break;
1025 case EM_D10V:
1026 case EM_CYGNUS_D10V:
1027 rtype = elf_d10v_reloc_type (type);
1028 break;
1030 case EM_D30V:
1031 case EM_CYGNUS_D30V:
1032 rtype = elf_d30v_reloc_type (type);
1033 break;
1035 case EM_DLX:
1036 rtype = elf_dlx_reloc_type (type);
1037 break;
1039 case EM_SH:
1040 rtype = elf_sh_reloc_type (type);
1041 break;
1043 case EM_MN10300:
1044 case EM_CYGNUS_MN10300:
1045 rtype = elf_mn10300_reloc_type (type);
1046 break;
1048 case EM_MN10200:
1049 case EM_CYGNUS_MN10200:
1050 rtype = elf_mn10200_reloc_type (type);
1051 break;
1053 case EM_FR30:
1054 case EM_CYGNUS_FR30:
1055 rtype = elf_fr30_reloc_type (type);
1056 break;
1058 case EM_CYGNUS_FRV:
1059 rtype = elf_frv_reloc_type (type);
1060 break;
1062 case EM_MCORE:
1063 rtype = elf_mcore_reloc_type (type);
1064 break;
1066 case EM_MMIX:
1067 rtype = elf_mmix_reloc_type (type);
1068 break;
1070 case EM_MSP430:
1071 case EM_MSP430_OLD:
1072 rtype = elf_msp430_reloc_type (type);
1073 break;
1075 case EM_PPC:
1076 rtype = elf_ppc_reloc_type (type);
1077 break;
1079 case EM_PPC64:
1080 rtype = elf_ppc64_reloc_type (type);
1081 break;
1083 case EM_MIPS:
1084 case EM_MIPS_RS3_LE:
1085 rtype = elf_mips_reloc_type (type);
1086 if (!is_32bit_elf)
1088 rtype2 = elf_mips_reloc_type (type2);
1089 rtype3 = elf_mips_reloc_type (type3);
1091 break;
1093 case EM_ALPHA:
1094 rtype = elf_alpha_reloc_type (type);
1095 break;
1097 case EM_ARM:
1098 rtype = elf_arm_reloc_type (type);
1099 break;
1101 case EM_ARC:
1102 rtype = elf_arc_reloc_type (type);
1103 break;
1105 case EM_PARISC:
1106 rtype = elf_hppa_reloc_type (type);
1107 break;
1109 case EM_H8_300:
1110 case EM_H8_300H:
1111 case EM_H8S:
1112 rtype = elf_h8_reloc_type (type);
1113 break;
1115 case EM_OPENRISC:
1116 case EM_OR32:
1117 rtype = elf_or32_reloc_type (type);
1118 break;
1120 case EM_PJ:
1121 case EM_PJ_OLD:
1122 rtype = elf_pj_reloc_type (type);
1123 break;
1124 case EM_IA_64:
1125 rtype = elf_ia64_reloc_type (type);
1126 break;
1128 case EM_CRIS:
1129 rtype = elf_cris_reloc_type (type);
1130 break;
1132 case EM_860:
1133 rtype = elf_i860_reloc_type (type);
1134 break;
1136 case EM_X86_64:
1137 rtype = elf_x86_64_reloc_type (type);
1138 break;
1140 case EM_S370:
1141 rtype = i370_reloc_type (type);
1142 break;
1144 case EM_S390_OLD:
1145 case EM_S390:
1146 rtype = elf_s390_reloc_type (type);
1147 break;
1149 case EM_SCORE:
1150 rtype = elf_score_reloc_type (type);
1151 break;
1153 case EM_XSTORMY16:
1154 rtype = elf_xstormy16_reloc_type (type);
1155 break;
1157 case EM_CRX:
1158 rtype = elf_crx_reloc_type (type);
1159 break;
1161 case EM_VAX:
1162 rtype = elf_vax_reloc_type (type);
1163 break;
1165 case EM_IP2K:
1166 case EM_IP2K_OLD:
1167 rtype = elf_ip2k_reloc_type (type);
1168 break;
1170 case EM_IQ2000:
1171 rtype = elf_iq2000_reloc_type (type);
1172 break;
1174 case EM_XTENSA_OLD:
1175 case EM_XTENSA:
1176 rtype = elf_xtensa_reloc_type (type);
1177 break;
1179 case EM_M32C:
1180 rtype = elf_m32c_reloc_type (type);
1181 break;
1183 case EM_MT:
1184 rtype = elf_mt_reloc_type (type);
1185 break;
1187 case EM_BLACKFIN:
1188 rtype = elf_bfin_reloc_type (type);
1189 break;
1191 case EM_CYGNUS_MEP:
1192 rtype = elf_mep_reloc_type (type);
1193 break;
1195 case EM_CR16:
1196 rtype = elf_cr16_reloc_type (type);
1197 break;
1200 if (rtype == NULL)
1201 #ifdef _bfd_int64_low
1202 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type));
1203 #else
1204 printf (_("unrecognized: %-7lx"), type);
1205 #endif
1206 else
1207 printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1209 if (elf_header.e_machine == EM_ALPHA
1210 && rtype != NULL
1211 && streq (rtype, "R_ALPHA_LITUSE")
1212 && is_rela)
1214 switch (rels[i].r_addend)
1216 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1217 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1218 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1219 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1220 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1221 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1222 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1223 default: rtype = NULL;
1225 if (rtype)
1226 printf (" (%s)", rtype);
1227 else
1229 putchar (' ');
1230 printf (_("<unknown addend: %lx>"),
1231 (unsigned long) rels[i].r_addend);
1234 else if (symtab_index)
1236 if (symtab == NULL || symtab_index >= nsyms)
1237 printf (" bad symbol index: %08lx", (unsigned long) symtab_index);
1238 else
1240 Elf_Internal_Sym *psym;
1242 psym = symtab + symtab_index;
1244 printf (" ");
1245 print_vma (psym->st_value, LONG_HEX);
1246 printf (is_32bit_elf ? " " : " ");
1248 if (psym->st_name == 0)
1250 const char *sec_name = "<null>";
1251 char name_buf[40];
1253 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1255 bfd_vma sec_index = (bfd_vma) -1;
1257 if (psym->st_shndx < SHN_LORESERVE)
1258 sec_index = psym->st_shndx;
1259 else if (psym->st_shndx > SHN_HIRESERVE)
1260 sec_index = psym->st_shndx - (SHN_HIRESERVE + 1
1261 - SHN_LORESERVE);
1263 if (sec_index != (bfd_vma) -1)
1264 sec_name = SECTION_NAME (section_headers + sec_index);
1265 else if (psym->st_shndx == SHN_ABS)
1266 sec_name = "ABS";
1267 else if (psym->st_shndx == SHN_COMMON)
1268 sec_name = "COMMON";
1269 else if (elf_header.e_machine == EM_MIPS
1270 && psym->st_shndx == SHN_MIPS_SCOMMON)
1271 sec_name = "SCOMMON";
1272 else if (elf_header.e_machine == EM_MIPS
1273 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1274 sec_name = "SUNDEF";
1275 else if (elf_header.e_machine == EM_X86_64
1276 && psym->st_shndx == SHN_X86_64_LCOMMON)
1277 sec_name = "LARGE_COMMON";
1278 else if (elf_header.e_machine == EM_IA_64
1279 && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1280 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1281 sec_name = "ANSI_COM";
1282 else
1284 sprintf (name_buf, "<section 0x%x>",
1285 (unsigned int) psym->st_shndx);
1286 sec_name = name_buf;
1289 print_symbol (22, sec_name);
1291 else if (strtab == NULL)
1292 printf (_("<string table index: %3ld>"), psym->st_name);
1293 else if (psym->st_name >= strtablen)
1294 printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1295 else
1296 print_symbol (22, strtab + psym->st_name);
1298 if (is_rela)
1299 printf (" + %lx", (unsigned long) rels[i].r_addend);
1302 else if (is_rela)
1304 printf ("%*c", is_32bit_elf ?
1305 (do_wide ? 34 : 28) : (do_wide ? 26 : 20), ' ');
1306 print_vma (rels[i].r_addend, LONG_HEX);
1309 if (elf_header.e_machine == EM_SPARCV9
1310 && rtype != NULL
1311 && streq (rtype, "R_SPARC_OLO10"))
1312 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (info));
1314 putchar ('\n');
1316 if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1318 printf (" Type2: ");
1320 if (rtype2 == NULL)
1321 #ifdef _bfd_int64_low
1322 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type2));
1323 #else
1324 printf (_("unrecognized: %-7lx"), type2);
1325 #endif
1326 else
1327 printf ("%-17.17s", rtype2);
1329 printf ("\n Type3: ");
1331 if (rtype3 == NULL)
1332 #ifdef _bfd_int64_low
1333 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type3));
1334 #else
1335 printf (_("unrecognized: %-7lx"), type3);
1336 #endif
1337 else
1338 printf ("%-17.17s", rtype3);
1340 putchar ('\n');
1344 free (rels);
1346 return 1;
1349 static const char *
1350 get_mips_dynamic_type (unsigned long type)
1352 switch (type)
1354 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1355 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1356 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1357 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1358 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1359 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1360 case DT_MIPS_MSYM: return "MIPS_MSYM";
1361 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1362 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1363 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1364 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1365 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1366 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1367 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1368 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1369 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1370 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1371 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1372 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1373 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1374 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1375 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1376 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1377 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1378 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1379 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1380 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1381 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1382 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1383 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1384 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1385 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1386 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1387 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1388 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1389 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1390 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1391 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1392 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1393 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1394 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1395 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1396 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1397 default:
1398 return NULL;
1402 static const char *
1403 get_sparc64_dynamic_type (unsigned long type)
1405 switch (type)
1407 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1408 default:
1409 return NULL;
1413 static const char *
1414 get_ppc_dynamic_type (unsigned long type)
1416 switch (type)
1418 case DT_PPC_GOT: return "PPC_GOT";
1419 default:
1420 return NULL;
1424 static const char *
1425 get_ppc64_dynamic_type (unsigned long type)
1427 switch (type)
1429 case DT_PPC64_GLINK: return "PPC64_GLINK";
1430 case DT_PPC64_OPD: return "PPC64_OPD";
1431 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1432 default:
1433 return NULL;
1437 static const char *
1438 get_parisc_dynamic_type (unsigned long type)
1440 switch (type)
1442 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1443 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1444 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1445 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1446 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1447 case DT_HP_PREINIT: return "HP_PREINIT";
1448 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1449 case DT_HP_NEEDED: return "HP_NEEDED";
1450 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1451 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1452 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1453 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1454 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
1455 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
1456 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
1457 case DT_HP_FILTERED: return "HP_FILTERED";
1458 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
1459 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1460 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
1461 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
1462 case DT_PLT: return "PLT";
1463 case DT_PLT_SIZE: return "PLT_SIZE";
1464 case DT_DLT: return "DLT";
1465 case DT_DLT_SIZE: return "DLT_SIZE";
1466 default:
1467 return NULL;
1471 static const char *
1472 get_ia64_dynamic_type (unsigned long type)
1474 switch (type)
1476 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
1477 default:
1478 return NULL;
1482 static const char *
1483 get_alpha_dynamic_type (unsigned long type)
1485 switch (type)
1487 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1488 default:
1489 return NULL;
1493 static const char *
1494 get_score_dynamic_type (unsigned long type)
1496 switch (type)
1498 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1499 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
1500 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
1501 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
1502 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
1503 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
1504 default:
1505 return NULL;
1510 static const char *
1511 get_dynamic_type (unsigned long type)
1513 static char buff[64];
1515 switch (type)
1517 case DT_NULL: return "NULL";
1518 case DT_NEEDED: return "NEEDED";
1519 case DT_PLTRELSZ: return "PLTRELSZ";
1520 case DT_PLTGOT: return "PLTGOT";
1521 case DT_HASH: return "HASH";
1522 case DT_STRTAB: return "STRTAB";
1523 case DT_SYMTAB: return "SYMTAB";
1524 case DT_RELA: return "RELA";
1525 case DT_RELASZ: return "RELASZ";
1526 case DT_RELAENT: return "RELAENT";
1527 case DT_STRSZ: return "STRSZ";
1528 case DT_SYMENT: return "SYMENT";
1529 case DT_INIT: return "INIT";
1530 case DT_FINI: return "FINI";
1531 case DT_SONAME: return "SONAME";
1532 case DT_RPATH: return "RPATH";
1533 case DT_SYMBOLIC: return "SYMBOLIC";
1534 case DT_REL: return "REL";
1535 case DT_RELSZ: return "RELSZ";
1536 case DT_RELENT: return "RELENT";
1537 case DT_PLTREL: return "PLTREL";
1538 case DT_DEBUG: return "DEBUG";
1539 case DT_TEXTREL: return "TEXTREL";
1540 case DT_JMPREL: return "JMPREL";
1541 case DT_BIND_NOW: return "BIND_NOW";
1542 case DT_INIT_ARRAY: return "INIT_ARRAY";
1543 case DT_FINI_ARRAY: return "FINI_ARRAY";
1544 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1545 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1546 case DT_RUNPATH: return "RUNPATH";
1547 case DT_FLAGS: return "FLAGS";
1549 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1550 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1552 case DT_CHECKSUM: return "CHECKSUM";
1553 case DT_PLTPADSZ: return "PLTPADSZ";
1554 case DT_MOVEENT: return "MOVEENT";
1555 case DT_MOVESZ: return "MOVESZ";
1556 case DT_FEATURE: return "FEATURE";
1557 case DT_POSFLAG_1: return "POSFLAG_1";
1558 case DT_SYMINSZ: return "SYMINSZ";
1559 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
1561 case DT_ADDRRNGLO: return "ADDRRNGLO";
1562 case DT_CONFIG: return "CONFIG";
1563 case DT_DEPAUDIT: return "DEPAUDIT";
1564 case DT_AUDIT: return "AUDIT";
1565 case DT_PLTPAD: return "PLTPAD";
1566 case DT_MOVETAB: return "MOVETAB";
1567 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
1569 case DT_VERSYM: return "VERSYM";
1571 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
1572 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
1573 case DT_RELACOUNT: return "RELACOUNT";
1574 case DT_RELCOUNT: return "RELCOUNT";
1575 case DT_FLAGS_1: return "FLAGS_1";
1576 case DT_VERDEF: return "VERDEF";
1577 case DT_VERDEFNUM: return "VERDEFNUM";
1578 case DT_VERNEED: return "VERNEED";
1579 case DT_VERNEEDNUM: return "VERNEEDNUM";
1581 case DT_AUXILIARY: return "AUXILIARY";
1582 case DT_USED: return "USED";
1583 case DT_FILTER: return "FILTER";
1585 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1586 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1587 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1588 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1589 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1590 case DT_GNU_HASH: return "GNU_HASH";
1592 default:
1593 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1595 const char *result;
1597 switch (elf_header.e_machine)
1599 case EM_MIPS:
1600 case EM_MIPS_RS3_LE:
1601 result = get_mips_dynamic_type (type);
1602 break;
1603 case EM_SPARCV9:
1604 result = get_sparc64_dynamic_type (type);
1605 break;
1606 case EM_PPC:
1607 result = get_ppc_dynamic_type (type);
1608 break;
1609 case EM_PPC64:
1610 result = get_ppc64_dynamic_type (type);
1611 break;
1612 case EM_IA_64:
1613 result = get_ia64_dynamic_type (type);
1614 break;
1615 case EM_ALPHA:
1616 result = get_alpha_dynamic_type (type);
1617 break;
1618 case EM_SCORE:
1619 result = get_score_dynamic_type (type);
1620 break;
1621 default:
1622 result = NULL;
1623 break;
1626 if (result != NULL)
1627 return result;
1629 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
1631 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
1632 || (elf_header.e_machine == EM_PARISC
1633 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
1635 const char *result;
1637 switch (elf_header.e_machine)
1639 case EM_PARISC:
1640 result = get_parisc_dynamic_type (type);
1641 break;
1642 default:
1643 result = NULL;
1644 break;
1647 if (result != NULL)
1648 return result;
1650 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
1651 type);
1653 else
1654 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
1656 return buff;
1660 static char *
1661 get_file_type (unsigned e_type)
1663 static char buff[32];
1665 switch (e_type)
1667 case ET_NONE: return _("NONE (None)");
1668 case ET_REL: return _("REL (Relocatable file)");
1669 case ET_EXEC: return _("EXEC (Executable file)");
1670 case ET_DYN: return _("DYN (Shared object file)");
1671 case ET_CORE: return _("CORE (Core file)");
1673 default:
1674 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
1675 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
1676 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
1677 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
1678 else
1679 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
1680 return buff;
1684 static char *
1685 get_machine_name (unsigned e_machine)
1687 static char buff[64]; /* XXX */
1689 switch (e_machine)
1691 case EM_NONE: return _("None");
1692 case EM_M32: return "WE32100";
1693 case EM_SPARC: return "Sparc";
1694 case EM_SPU: return "SPU";
1695 case EM_386: return "Intel 80386";
1696 case EM_68K: return "MC68000";
1697 case EM_88K: return "MC88000";
1698 case EM_486: return "Intel 80486";
1699 case EM_860: return "Intel 80860";
1700 case EM_MIPS: return "MIPS R3000";
1701 case EM_S370: return "IBM System/370";
1702 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
1703 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
1704 case EM_PARISC: return "HPPA";
1705 case EM_PPC_OLD: return "Power PC (old)";
1706 case EM_SPARC32PLUS: return "Sparc v8+" ;
1707 case EM_960: return "Intel 90860";
1708 case EM_PPC: return "PowerPC";
1709 case EM_PPC64: return "PowerPC64";
1710 case EM_V800: return "NEC V800";
1711 case EM_FR20: return "Fujitsu FR20";
1712 case EM_RH32: return "TRW RH32";
1713 case EM_MCORE: return "MCORE";
1714 case EM_ARM: return "ARM";
1715 case EM_OLD_ALPHA: return "Digital Alpha (old)";
1716 case EM_SH: return "Renesas / SuperH SH";
1717 case EM_SPARCV9: return "Sparc v9";
1718 case EM_TRICORE: return "Siemens Tricore";
1719 case EM_ARC: return "ARC";
1720 case EM_H8_300: return "Renesas H8/300";
1721 case EM_H8_300H: return "Renesas H8/300H";
1722 case EM_H8S: return "Renesas H8S";
1723 case EM_H8_500: return "Renesas H8/500";
1724 case EM_IA_64: return "Intel IA-64";
1725 case EM_MIPS_X: return "Stanford MIPS-X";
1726 case EM_COLDFIRE: return "Motorola Coldfire";
1727 case EM_68HC12: return "Motorola M68HC12";
1728 case EM_ALPHA: return "Alpha";
1729 case EM_CYGNUS_D10V:
1730 case EM_D10V: return "d10v";
1731 case EM_CYGNUS_D30V:
1732 case EM_D30V: return "d30v";
1733 case EM_CYGNUS_M32R:
1734 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
1735 case EM_CYGNUS_V850:
1736 case EM_V850: return "NEC v850";
1737 case EM_CYGNUS_MN10300:
1738 case EM_MN10300: return "mn10300";
1739 case EM_CYGNUS_MN10200:
1740 case EM_MN10200: return "mn10200";
1741 case EM_CYGNUS_FR30:
1742 case EM_FR30: return "Fujitsu FR30";
1743 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
1744 case EM_PJ_OLD:
1745 case EM_PJ: return "picoJava";
1746 case EM_MMA: return "Fujitsu Multimedia Accelerator";
1747 case EM_PCP: return "Siemens PCP";
1748 case EM_NCPU: return "Sony nCPU embedded RISC processor";
1749 case EM_NDR1: return "Denso NDR1 microprocesspr";
1750 case EM_STARCORE: return "Motorola Star*Core processor";
1751 case EM_ME16: return "Toyota ME16 processor";
1752 case EM_ST100: return "STMicroelectronics ST100 processor";
1753 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
1754 case EM_FX66: return "Siemens FX66 microcontroller";
1755 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
1756 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
1757 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
1758 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
1759 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
1760 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
1761 case EM_SVX: return "Silicon Graphics SVx";
1762 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
1763 case EM_VAX: return "Digital VAX";
1764 case EM_AVR_OLD:
1765 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
1766 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
1767 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
1768 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
1769 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
1770 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
1771 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
1772 case EM_PRISM: return "Vitesse Prism";
1773 case EM_X86_64: return "Advanced Micro Devices X86-64";
1774 case EM_S390_OLD:
1775 case EM_S390: return "IBM S/390";
1776 case EM_SCORE: return "SUNPLUS S+Core";
1777 case EM_XSTORMY16: return "Sanyo Xstormy16 CPU core";
1778 case EM_OPENRISC:
1779 case EM_OR32: return "OpenRISC";
1780 case EM_CRX: return "National Semiconductor CRX microprocessor";
1781 case EM_DLX: return "OpenDLX";
1782 case EM_IP2K_OLD:
1783 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
1784 case EM_IQ2000: return "Vitesse IQ2000";
1785 case EM_XTENSA_OLD:
1786 case EM_XTENSA: return "Tensilica Xtensa Processor";
1787 case EM_M32C: return "Renesas M32c";
1788 case EM_MT: return "Morpho Techologies MT processor";
1789 case EM_BLACKFIN: return "Analog Devices Blackfin";
1790 case EM_NIOS32: return "Altera Nios";
1791 case EM_ALTERA_NIOS2: return "Altera Nios II";
1792 case EM_XC16X: return "Infineon Technologies xc16x";
1793 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
1794 case EM_CR16: return "National Semiconductor's CR16";
1795 default:
1796 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
1797 return buff;
1801 static void
1802 decode_ARM_machine_flags (unsigned e_flags, char buf[])
1804 unsigned eabi;
1805 int unknown = 0;
1807 eabi = EF_ARM_EABI_VERSION (e_flags);
1808 e_flags &= ~ EF_ARM_EABIMASK;
1810 /* Handle "generic" ARM flags. */
1811 if (e_flags & EF_ARM_RELEXEC)
1813 strcat (buf, ", relocatable executable");
1814 e_flags &= ~ EF_ARM_RELEXEC;
1817 if (e_flags & EF_ARM_HASENTRY)
1819 strcat (buf, ", has entry point");
1820 e_flags &= ~ EF_ARM_HASENTRY;
1823 /* Now handle EABI specific flags. */
1824 switch (eabi)
1826 default:
1827 strcat (buf, ", <unrecognized EABI>");
1828 if (e_flags)
1829 unknown = 1;
1830 break;
1832 case EF_ARM_EABI_VER1:
1833 strcat (buf, ", Version1 EABI");
1834 while (e_flags)
1836 unsigned flag;
1838 /* Process flags one bit at a time. */
1839 flag = e_flags & - e_flags;
1840 e_flags &= ~ flag;
1842 switch (flag)
1844 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1845 strcat (buf, ", sorted symbol tables");
1846 break;
1848 default:
1849 unknown = 1;
1850 break;
1853 break;
1855 case EF_ARM_EABI_VER2:
1856 strcat (buf, ", Version2 EABI");
1857 while (e_flags)
1859 unsigned flag;
1861 /* Process flags one bit at a time. */
1862 flag = e_flags & - e_flags;
1863 e_flags &= ~ flag;
1865 switch (flag)
1867 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1868 strcat (buf, ", sorted symbol tables");
1869 break;
1871 case EF_ARM_DYNSYMSUSESEGIDX:
1872 strcat (buf, ", dynamic symbols use segment index");
1873 break;
1875 case EF_ARM_MAPSYMSFIRST:
1876 strcat (buf, ", mapping symbols precede others");
1877 break;
1879 default:
1880 unknown = 1;
1881 break;
1884 break;
1886 case EF_ARM_EABI_VER3:
1887 strcat (buf, ", Version3 EABI");
1888 break;
1890 case EF_ARM_EABI_VER4:
1891 strcat (buf, ", Version4 EABI");
1892 goto eabi;
1894 case EF_ARM_EABI_VER5:
1895 strcat (buf, ", Version5 EABI");
1896 eabi:
1897 while (e_flags)
1899 unsigned flag;
1901 /* Process flags one bit at a time. */
1902 flag = e_flags & - e_flags;
1903 e_flags &= ~ flag;
1905 switch (flag)
1907 case EF_ARM_BE8:
1908 strcat (buf, ", BE8");
1909 break;
1911 case EF_ARM_LE8:
1912 strcat (buf, ", LE8");
1913 break;
1915 default:
1916 unknown = 1;
1917 break;
1920 break;
1922 case EF_ARM_EABI_UNKNOWN:
1923 strcat (buf, ", GNU EABI");
1924 while (e_flags)
1926 unsigned flag;
1928 /* Process flags one bit at a time. */
1929 flag = e_flags & - e_flags;
1930 e_flags &= ~ flag;
1932 switch (flag)
1934 case EF_ARM_INTERWORK:
1935 strcat (buf, ", interworking enabled");
1936 break;
1938 case EF_ARM_APCS_26:
1939 strcat (buf, ", uses APCS/26");
1940 break;
1942 case EF_ARM_APCS_FLOAT:
1943 strcat (buf, ", uses APCS/float");
1944 break;
1946 case EF_ARM_PIC:
1947 strcat (buf, ", position independent");
1948 break;
1950 case EF_ARM_ALIGN8:
1951 strcat (buf, ", 8 bit structure alignment");
1952 break;
1954 case EF_ARM_NEW_ABI:
1955 strcat (buf, ", uses new ABI");
1956 break;
1958 case EF_ARM_OLD_ABI:
1959 strcat (buf, ", uses old ABI");
1960 break;
1962 case EF_ARM_SOFT_FLOAT:
1963 strcat (buf, ", software FP");
1964 break;
1966 case EF_ARM_VFP_FLOAT:
1967 strcat (buf, ", VFP");
1968 break;
1970 case EF_ARM_MAVERICK_FLOAT:
1971 strcat (buf, ", Maverick FP");
1972 break;
1974 default:
1975 unknown = 1;
1976 break;
1981 if (unknown)
1982 strcat (buf,", <unknown>");
1985 static char *
1986 get_machine_flags (unsigned e_flags, unsigned e_machine)
1988 static char buf[1024];
1990 buf[0] = '\0';
1992 if (e_flags)
1994 switch (e_machine)
1996 default:
1997 break;
1999 case EM_ARM:
2000 decode_ARM_machine_flags (e_flags, buf);
2001 break;
2003 case EM_CYGNUS_FRV:
2004 switch (e_flags & EF_FRV_CPU_MASK)
2006 case EF_FRV_CPU_GENERIC:
2007 break;
2009 default:
2010 strcat (buf, ", fr???");
2011 break;
2013 case EF_FRV_CPU_FR300:
2014 strcat (buf, ", fr300");
2015 break;
2017 case EF_FRV_CPU_FR400:
2018 strcat (buf, ", fr400");
2019 break;
2020 case EF_FRV_CPU_FR405:
2021 strcat (buf, ", fr405");
2022 break;
2024 case EF_FRV_CPU_FR450:
2025 strcat (buf, ", fr450");
2026 break;
2028 case EF_FRV_CPU_FR500:
2029 strcat (buf, ", fr500");
2030 break;
2031 case EF_FRV_CPU_FR550:
2032 strcat (buf, ", fr550");
2033 break;
2035 case EF_FRV_CPU_SIMPLE:
2036 strcat (buf, ", simple");
2037 break;
2038 case EF_FRV_CPU_TOMCAT:
2039 strcat (buf, ", tomcat");
2040 break;
2042 break;
2044 case EM_68K:
2045 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
2046 strcat (buf, ", m68000");
2047 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
2048 strcat (buf, ", cpu32");
2049 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
2050 strcat (buf, ", fido_a");
2051 else
2053 char const *isa = _("unknown");
2054 char const *mac = _("unknown mac");
2055 char const *additional = NULL;
2057 switch (e_flags & EF_M68K_CF_ISA_MASK)
2059 case EF_M68K_CF_ISA_A_NODIV:
2060 isa = "A";
2061 additional = ", nodiv";
2062 break;
2063 case EF_M68K_CF_ISA_A:
2064 isa = "A";
2065 break;
2066 case EF_M68K_CF_ISA_A_PLUS:
2067 isa = "A+";
2068 break;
2069 case EF_M68K_CF_ISA_B_NOUSP:
2070 isa = "B";
2071 additional = ", nousp";
2072 break;
2073 case EF_M68K_CF_ISA_B:
2074 isa = "B";
2075 break;
2077 strcat (buf, ", cf, isa ");
2078 strcat (buf, isa);
2079 if (additional)
2080 strcat (buf, additional);
2081 if (e_flags & EF_M68K_CF_FLOAT)
2082 strcat (buf, ", float");
2083 switch (e_flags & EF_M68K_CF_MAC_MASK)
2085 case 0:
2086 mac = NULL;
2087 break;
2088 case EF_M68K_CF_MAC:
2089 mac = "mac";
2090 break;
2091 case EF_M68K_CF_EMAC:
2092 mac = "emac";
2093 break;
2095 if (mac)
2097 strcat (buf, ", ");
2098 strcat (buf, mac);
2101 break;
2103 case EM_PPC:
2104 if (e_flags & EF_PPC_EMB)
2105 strcat (buf, ", emb");
2107 if (e_flags & EF_PPC_RELOCATABLE)
2108 strcat (buf, ", relocatable");
2110 if (e_flags & EF_PPC_RELOCATABLE_LIB)
2111 strcat (buf, ", relocatable-lib");
2112 break;
2114 case EM_V850:
2115 case EM_CYGNUS_V850:
2116 switch (e_flags & EF_V850_ARCH)
2118 case E_V850E1_ARCH:
2119 strcat (buf, ", v850e1");
2120 break;
2121 case E_V850E_ARCH:
2122 strcat (buf, ", v850e");
2123 break;
2124 case E_V850_ARCH:
2125 strcat (buf, ", v850");
2126 break;
2127 default:
2128 strcat (buf, ", unknown v850 architecture variant");
2129 break;
2131 break;
2133 case EM_M32R:
2134 case EM_CYGNUS_M32R:
2135 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
2136 strcat (buf, ", m32r");
2137 break;
2139 case EM_MIPS:
2140 case EM_MIPS_RS3_LE:
2141 if (e_flags & EF_MIPS_NOREORDER)
2142 strcat (buf, ", noreorder");
2144 if (e_flags & EF_MIPS_PIC)
2145 strcat (buf, ", pic");
2147 if (e_flags & EF_MIPS_CPIC)
2148 strcat (buf, ", cpic");
2150 if (e_flags & EF_MIPS_UCODE)
2151 strcat (buf, ", ugen_reserved");
2153 if (e_flags & EF_MIPS_ABI2)
2154 strcat (buf, ", abi2");
2156 if (e_flags & EF_MIPS_OPTIONS_FIRST)
2157 strcat (buf, ", odk first");
2159 if (e_flags & EF_MIPS_32BITMODE)
2160 strcat (buf, ", 32bitmode");
2162 switch ((e_flags & EF_MIPS_MACH))
2164 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
2165 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
2166 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
2167 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
2168 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
2169 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
2170 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
2171 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
2172 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
2173 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
2174 case 0:
2175 /* We simply ignore the field in this case to avoid confusion:
2176 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
2177 extension. */
2178 break;
2179 default: strcat (buf, ", unknown CPU"); break;
2182 switch ((e_flags & EF_MIPS_ABI))
2184 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
2185 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
2186 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
2187 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
2188 case 0:
2189 /* We simply ignore the field in this case to avoid confusion:
2190 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
2191 This means it is likely to be an o32 file, but not for
2192 sure. */
2193 break;
2194 default: strcat (buf, ", unknown ABI"); break;
2197 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
2198 strcat (buf, ", mdmx");
2200 if (e_flags & EF_MIPS_ARCH_ASE_M16)
2201 strcat (buf, ", mips16");
2203 switch ((e_flags & EF_MIPS_ARCH))
2205 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
2206 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
2207 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
2208 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
2209 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
2210 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
2211 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
2212 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
2213 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
2214 default: strcat (buf, ", unknown ISA"); break;
2217 break;
2219 case EM_SH:
2220 switch ((e_flags & EF_SH_MACH_MASK))
2222 case EF_SH1: strcat (buf, ", sh1"); break;
2223 case EF_SH2: strcat (buf, ", sh2"); break;
2224 case EF_SH3: strcat (buf, ", sh3"); break;
2225 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
2226 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
2227 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
2228 case EF_SH3E: strcat (buf, ", sh3e"); break;
2229 case EF_SH4: strcat (buf, ", sh4"); break;
2230 case EF_SH5: strcat (buf, ", sh5"); break;
2231 case EF_SH2E: strcat (buf, ", sh2e"); break;
2232 case EF_SH4A: strcat (buf, ", sh4a"); break;
2233 case EF_SH2A: strcat (buf, ", sh2a"); break;
2234 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
2235 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
2236 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
2237 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
2238 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
2239 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
2240 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
2241 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
2242 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
2243 default: strcat (buf, ", unknown ISA"); break;
2246 break;
2248 case EM_SPARCV9:
2249 if (e_flags & EF_SPARC_32PLUS)
2250 strcat (buf, ", v8+");
2252 if (e_flags & EF_SPARC_SUN_US1)
2253 strcat (buf, ", ultrasparcI");
2255 if (e_flags & EF_SPARC_SUN_US3)
2256 strcat (buf, ", ultrasparcIII");
2258 if (e_flags & EF_SPARC_HAL_R1)
2259 strcat (buf, ", halr1");
2261 if (e_flags & EF_SPARC_LEDATA)
2262 strcat (buf, ", ledata");
2264 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
2265 strcat (buf, ", tso");
2267 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
2268 strcat (buf, ", pso");
2270 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
2271 strcat (buf, ", rmo");
2272 break;
2274 case EM_PARISC:
2275 switch (e_flags & EF_PARISC_ARCH)
2277 case EFA_PARISC_1_0:
2278 strcpy (buf, ", PA-RISC 1.0");
2279 break;
2280 case EFA_PARISC_1_1:
2281 strcpy (buf, ", PA-RISC 1.1");
2282 break;
2283 case EFA_PARISC_2_0:
2284 strcpy (buf, ", PA-RISC 2.0");
2285 break;
2286 default:
2287 break;
2289 if (e_flags & EF_PARISC_TRAPNIL)
2290 strcat (buf, ", trapnil");
2291 if (e_flags & EF_PARISC_EXT)
2292 strcat (buf, ", ext");
2293 if (e_flags & EF_PARISC_LSB)
2294 strcat (buf, ", lsb");
2295 if (e_flags & EF_PARISC_WIDE)
2296 strcat (buf, ", wide");
2297 if (e_flags & EF_PARISC_NO_KABP)
2298 strcat (buf, ", no kabp");
2299 if (e_flags & EF_PARISC_LAZYSWAP)
2300 strcat (buf, ", lazyswap");
2301 break;
2303 case EM_PJ:
2304 case EM_PJ_OLD:
2305 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
2306 strcat (buf, ", new calling convention");
2308 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
2309 strcat (buf, ", gnu calling convention");
2310 break;
2312 case EM_IA_64:
2313 if ((e_flags & EF_IA_64_ABI64))
2314 strcat (buf, ", 64-bit");
2315 else
2316 strcat (buf, ", 32-bit");
2317 if ((e_flags & EF_IA_64_REDUCEDFP))
2318 strcat (buf, ", reduced fp model");
2319 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
2320 strcat (buf, ", no function descriptors, constant gp");
2321 else if ((e_flags & EF_IA_64_CONS_GP))
2322 strcat (buf, ", constant gp");
2323 if ((e_flags & EF_IA_64_ABSOLUTE))
2324 strcat (buf, ", absolute");
2325 break;
2327 case EM_VAX:
2328 if ((e_flags & EF_VAX_NONPIC))
2329 strcat (buf, ", non-PIC");
2330 if ((e_flags & EF_VAX_DFLOAT))
2331 strcat (buf, ", D-Float");
2332 if ((e_flags & EF_VAX_GFLOAT))
2333 strcat (buf, ", G-Float");
2334 break;
2338 return buf;
2341 static const char *
2342 get_osabi_name (unsigned int osabi)
2344 static char buff[32];
2346 switch (osabi)
2348 case ELFOSABI_NONE: return "UNIX - System V";
2349 case ELFOSABI_HPUX: return "UNIX - HP-UX";
2350 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
2351 case ELFOSABI_LINUX: return "UNIX - Linux";
2352 case ELFOSABI_HURD: return "GNU/Hurd";
2353 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
2354 case ELFOSABI_AIX: return "UNIX - AIX";
2355 case ELFOSABI_IRIX: return "UNIX - IRIX";
2356 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
2357 case ELFOSABI_TRU64: return "UNIX - TRU64";
2358 case ELFOSABI_MODESTO: return "Novell - Modesto";
2359 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
2360 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
2361 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
2362 case ELFOSABI_AROS: return "Amiga Research OS";
2363 case ELFOSABI_STANDALONE: return _("Standalone App");
2364 case ELFOSABI_ARM: return "ARM";
2365 case ELFOSABI_NACL: return "NativeClient";
2366 default:
2367 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
2368 return buff;
2372 static const char *
2373 get_arm_segment_type (unsigned long type)
2375 switch (type)
2377 case PT_ARM_EXIDX:
2378 return "EXIDX";
2379 default:
2380 break;
2383 return NULL;
2386 static const char *
2387 get_mips_segment_type (unsigned long type)
2389 switch (type)
2391 case PT_MIPS_REGINFO:
2392 return "REGINFO";
2393 case PT_MIPS_RTPROC:
2394 return "RTPROC";
2395 case PT_MIPS_OPTIONS:
2396 return "OPTIONS";
2397 default:
2398 break;
2401 return NULL;
2404 static const char *
2405 get_parisc_segment_type (unsigned long type)
2407 switch (type)
2409 case PT_HP_TLS: return "HP_TLS";
2410 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
2411 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
2412 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
2413 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
2414 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
2415 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
2416 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
2417 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
2418 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
2419 case PT_HP_PARALLEL: return "HP_PARALLEL";
2420 case PT_HP_FASTBIND: return "HP_FASTBIND";
2421 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
2422 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
2423 case PT_HP_STACK: return "HP_STACK";
2424 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
2425 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
2426 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
2427 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
2428 default:
2429 break;
2432 return NULL;
2435 static const char *
2436 get_ia64_segment_type (unsigned long type)
2438 switch (type)
2440 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
2441 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
2442 case PT_HP_TLS: return "HP_TLS";
2443 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
2444 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
2445 case PT_IA_64_HP_STACK: return "HP_STACK";
2446 default:
2447 break;
2450 return NULL;
2453 static const char *
2454 get_segment_type (unsigned long p_type)
2456 static char buff[32];
2458 switch (p_type)
2460 case PT_NULL: return "NULL";
2461 case PT_LOAD: return "LOAD";
2462 case PT_DYNAMIC: return "DYNAMIC";
2463 case PT_INTERP: return "INTERP";
2464 case PT_NOTE: return "NOTE";
2465 case PT_SHLIB: return "SHLIB";
2466 case PT_PHDR: return "PHDR";
2467 case PT_TLS: return "TLS";
2469 case PT_GNU_EH_FRAME:
2470 return "GNU_EH_FRAME";
2471 case PT_GNU_STACK: return "GNU_STACK";
2472 case PT_GNU_RELRO: return "GNU_RELRO";
2474 default:
2475 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
2477 const char *result;
2479 switch (elf_header.e_machine)
2481 case EM_ARM:
2482 result = get_arm_segment_type (p_type);
2483 break;
2484 case EM_MIPS:
2485 case EM_MIPS_RS3_LE:
2486 result = get_mips_segment_type (p_type);
2487 break;
2488 case EM_PARISC:
2489 result = get_parisc_segment_type (p_type);
2490 break;
2491 case EM_IA_64:
2492 result = get_ia64_segment_type (p_type);
2493 break;
2494 default:
2495 result = NULL;
2496 break;
2499 if (result != NULL)
2500 return result;
2502 sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
2504 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
2506 const char *result;
2508 switch (elf_header.e_machine)
2510 case EM_PARISC:
2511 result = get_parisc_segment_type (p_type);
2512 break;
2513 case EM_IA_64:
2514 result = get_ia64_segment_type (p_type);
2515 break;
2516 default:
2517 result = NULL;
2518 break;
2521 if (result != NULL)
2522 return result;
2524 sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
2526 else
2527 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
2529 return buff;
2533 static const char *
2534 get_mips_section_type_name (unsigned int sh_type)
2536 switch (sh_type)
2538 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2539 case SHT_MIPS_MSYM: return "MIPS_MSYM";
2540 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2541 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
2542 case SHT_MIPS_UCODE: return "MIPS_UCODE";
2543 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
2544 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
2545 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
2546 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
2547 case SHT_MIPS_RELD: return "MIPS_RELD";
2548 case SHT_MIPS_IFACE: return "MIPS_IFACE";
2549 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
2550 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2551 case SHT_MIPS_SHDR: return "MIPS_SHDR";
2552 case SHT_MIPS_FDESC: return "MIPS_FDESC";
2553 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
2554 case SHT_MIPS_DENSE: return "MIPS_DENSE";
2555 case SHT_MIPS_PDESC: return "MIPS_PDESC";
2556 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
2557 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
2558 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
2559 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
2560 case SHT_MIPS_LINE: return "MIPS_LINE";
2561 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
2562 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
2563 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
2564 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
2565 case SHT_MIPS_DWARF: return "MIPS_DWARF";
2566 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
2567 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2568 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
2569 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
2570 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
2571 case SHT_MIPS_XLATE: return "MIPS_XLATE";
2572 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
2573 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
2574 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
2575 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
2576 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
2577 default:
2578 break;
2580 return NULL;
2583 static const char *
2584 get_parisc_section_type_name (unsigned int sh_type)
2586 switch (sh_type)
2588 case SHT_PARISC_EXT: return "PARISC_EXT";
2589 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
2590 case SHT_PARISC_DOC: return "PARISC_DOC";
2591 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
2592 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
2593 case SHT_PARISC_STUBS: return "PARISC_STUBS";
2594 case SHT_PARISC_DLKM: return "PARISC_DLKM";
2595 default:
2596 break;
2598 return NULL;
2601 static const char *
2602 get_ia64_section_type_name (unsigned int sh_type)
2604 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
2605 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
2606 return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
2608 switch (sh_type)
2610 case SHT_IA_64_EXT: return "IA_64_EXT";
2611 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
2612 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
2613 default:
2614 break;
2616 return NULL;
2619 static const char *
2620 get_x86_64_section_type_name (unsigned int sh_type)
2622 switch (sh_type)
2624 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
2625 default:
2626 break;
2628 return NULL;
2631 static const char *
2632 get_arm_section_type_name (unsigned int sh_type)
2634 switch (sh_type)
2636 case SHT_ARM_EXIDX:
2637 return "ARM_EXIDX";
2638 case SHT_ARM_PREEMPTMAP:
2639 return "ARM_PREEMPTMAP";
2640 case SHT_ARM_ATTRIBUTES:
2641 return "ARM_ATTRIBUTES";
2642 default:
2643 break;
2645 return NULL;
2648 static const char *
2649 get_section_type_name (unsigned int sh_type)
2651 static char buff[32];
2653 switch (sh_type)
2655 case SHT_NULL: return "NULL";
2656 case SHT_PROGBITS: return "PROGBITS";
2657 case SHT_SYMTAB: return "SYMTAB";
2658 case SHT_STRTAB: return "STRTAB";
2659 case SHT_RELA: return "RELA";
2660 case SHT_HASH: return "HASH";
2661 case SHT_DYNAMIC: return "DYNAMIC";
2662 case SHT_NOTE: return "NOTE";
2663 case SHT_NOBITS: return "NOBITS";
2664 case SHT_REL: return "REL";
2665 case SHT_SHLIB: return "SHLIB";
2666 case SHT_DYNSYM: return "DYNSYM";
2667 case SHT_INIT_ARRAY: return "INIT_ARRAY";
2668 case SHT_FINI_ARRAY: return "FINI_ARRAY";
2669 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2670 case SHT_GNU_HASH: return "GNU_HASH";
2671 case SHT_GROUP: return "GROUP";
2672 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICIES";
2673 case SHT_GNU_verdef: return "VERDEF";
2674 case SHT_GNU_verneed: return "VERNEED";
2675 case SHT_GNU_versym: return "VERSYM";
2676 case 0x6ffffff0: return "VERSYM";
2677 case 0x6ffffffc: return "VERDEF";
2678 case 0x7ffffffd: return "AUXILIARY";
2679 case 0x7fffffff: return "FILTER";
2680 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
2682 default:
2683 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
2685 const char *result;
2687 switch (elf_header.e_machine)
2689 case EM_MIPS:
2690 case EM_MIPS_RS3_LE:
2691 result = get_mips_section_type_name (sh_type);
2692 break;
2693 case EM_PARISC:
2694 result = get_parisc_section_type_name (sh_type);
2695 break;
2696 case EM_IA_64:
2697 result = get_ia64_section_type_name (sh_type);
2698 break;
2699 case EM_X86_64:
2700 result = get_x86_64_section_type_name (sh_type);
2701 break;
2702 case EM_ARM:
2703 result = get_arm_section_type_name (sh_type);
2704 break;
2705 default:
2706 result = NULL;
2707 break;
2710 if (result != NULL)
2711 return result;
2713 sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
2715 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
2716 sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
2717 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
2718 sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
2719 else
2720 snprintf (buff, sizeof (buff), _("<unknown>: %x"), sh_type);
2722 return buff;
2726 #define OPTION_DEBUG_DUMP 512
2728 static struct option options[] =
2730 {"all", no_argument, 0, 'a'},
2731 {"file-header", no_argument, 0, 'h'},
2732 {"program-headers", no_argument, 0, 'l'},
2733 {"headers", no_argument, 0, 'e'},
2734 {"histogram", no_argument, 0, 'I'},
2735 {"segments", no_argument, 0, 'l'},
2736 {"sections", no_argument, 0, 'S'},
2737 {"section-headers", no_argument, 0, 'S'},
2738 {"section-groups", no_argument, 0, 'g'},
2739 {"section-details", no_argument, 0, 't'},
2740 {"full-section-name",no_argument, 0, 'N'},
2741 {"symbols", no_argument, 0, 's'},
2742 {"syms", no_argument, 0, 's'},
2743 {"relocs", no_argument, 0, 'r'},
2744 {"notes", no_argument, 0, 'n'},
2745 {"dynamic", no_argument, 0, 'd'},
2746 {"arch-specific", no_argument, 0, 'A'},
2747 {"version-info", no_argument, 0, 'V'},
2748 {"use-dynamic", no_argument, 0, 'D'},
2749 {"hex-dump", required_argument, 0, 'x'},
2750 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
2751 {"unwind", no_argument, 0, 'u'},
2752 #ifdef SUPPORT_DISASSEMBLY
2753 {"instruction-dump", required_argument, 0, 'i'},
2754 #endif
2756 {"version", no_argument, 0, 'v'},
2757 {"wide", no_argument, 0, 'W'},
2758 {"help", no_argument, 0, 'H'},
2759 {0, no_argument, 0, 0}
2762 static void
2763 usage (FILE *stream)
2765 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
2766 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
2767 fprintf (stream, _(" Options are:\n\
2768 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
2769 -h --file-header Display the ELF file header\n\
2770 -l --program-headers Display the program headers\n\
2771 --segments An alias for --program-headers\n\
2772 -S --section-headers Display the sections' header\n\
2773 --sections An alias for --section-headers\n\
2774 -g --section-groups Display the section groups\n\
2775 -t --section-details Display the section details\n\
2776 -e --headers Equivalent to: -h -l -S\n\
2777 -s --syms Display the symbol table\n\
2778 --symbols An alias for --syms\n\
2779 -n --notes Display the core notes (if present)\n\
2780 -r --relocs Display the relocations (if present)\n\
2781 -u --unwind Display the unwind info (if present)\n\
2782 -d --dynamic Display the dynamic section (if present)\n\
2783 -V --version-info Display the version sections (if present)\n\
2784 -A --arch-specific Display architecture specific information (if any).\n\
2785 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
2786 -x --hex-dump=<number> Dump the contents of section <number>\n\
2787 -w[liaprmfFsoR] or\n\
2788 --debug-dump[=line,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=str,=loc,=Ranges]\n\
2789 Display the contents of DWARF2 debug sections\n"));
2790 #ifdef SUPPORT_DISASSEMBLY
2791 fprintf (stream, _("\
2792 -i --instruction-dump=<number>\n\
2793 Disassemble the contents of section <number>\n"));
2794 #endif
2795 fprintf (stream, _("\
2796 -I --histogram Display histogram of bucket list lengths\n\
2797 -W --wide Allow output width to exceed 80 characters\n\
2798 @<file> Read options from <file>\n\
2799 -H --help Display this information\n\
2800 -v --version Display the version number of readelf\n"));
2802 if (REPORT_BUGS_TO[0] && stream == stdout)
2803 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
2805 exit (stream == stdout ? 0 : 1);
2808 /* Record the fact that the user wants the contents of section number
2809 SECTION to be displayed using the method(s) encoded as flags bits
2810 in TYPE. Note, TYPE can be zero if we are creating the array for
2811 the first time. */
2813 static void
2814 request_dump (unsigned int section, int type)
2816 if (section >= num_dump_sects)
2818 char *new_dump_sects;
2820 new_dump_sects = calloc (section + 1, 1);
2822 if (new_dump_sects == NULL)
2823 error (_("Out of memory allocating dump request table.\n"));
2824 else
2826 /* Copy current flag settings. */
2827 memcpy (new_dump_sects, dump_sects, num_dump_sects);
2829 free (dump_sects);
2831 dump_sects = new_dump_sects;
2832 num_dump_sects = section + 1;
2836 if (dump_sects)
2837 dump_sects[section] |= type;
2839 return;
2842 /* Request a dump by section name. */
2844 static void
2845 request_dump_byname (const char *section, int type)
2847 struct dump_list_entry *new_request;
2849 new_request = malloc (sizeof (struct dump_list_entry));
2850 if (!new_request)
2851 error (_("Out of memory allocating dump request table.\n"));
2853 new_request->name = strdup (section);
2854 if (!new_request->name)
2855 error (_("Out of memory allocating dump request table.\n"));
2857 new_request->type = type;
2859 new_request->next = dump_sects_byname;
2860 dump_sects_byname = new_request;
2863 static void
2864 parse_args (int argc, char **argv)
2866 int c;
2868 if (argc < 2)
2869 usage (stderr);
2871 while ((c = getopt_long
2872 (argc, argv, "ersuahnldSDAINtgw::x:i:vVWH", options, NULL)) != EOF)
2874 char *cp;
2875 int section;
2877 switch (c)
2879 case 0:
2880 /* Long options. */
2881 break;
2882 case 'H':
2883 usage (stdout);
2884 break;
2886 case 'a':
2887 do_syms++;
2888 do_reloc++;
2889 do_unwind++;
2890 do_dynamic++;
2891 do_header++;
2892 do_sections++;
2893 do_section_groups++;
2894 do_segments++;
2895 do_version++;
2896 do_histogram++;
2897 do_arch++;
2898 do_notes++;
2899 break;
2900 case 'g':
2901 do_section_groups++;
2902 break;
2903 case 't':
2904 case 'N':
2905 do_sections++;
2906 do_section_details++;
2907 break;
2908 case 'e':
2909 do_header++;
2910 do_sections++;
2911 do_segments++;
2912 break;
2913 case 'A':
2914 do_arch++;
2915 break;
2916 case 'D':
2917 do_using_dynamic++;
2918 break;
2919 case 'r':
2920 do_reloc++;
2921 break;
2922 case 'u':
2923 do_unwind++;
2924 break;
2925 case 'h':
2926 do_header++;
2927 break;
2928 case 'l':
2929 do_segments++;
2930 break;
2931 case 's':
2932 do_syms++;
2933 break;
2934 case 'S':
2935 do_sections++;
2936 break;
2937 case 'd':
2938 do_dynamic++;
2939 break;
2940 case 'I':
2941 do_histogram++;
2942 break;
2943 case 'n':
2944 do_notes++;
2945 break;
2946 case 'x':
2947 do_dump++;
2948 section = strtoul (optarg, & cp, 0);
2949 if (! *cp && section >= 0)
2950 request_dump (section, HEX_DUMP);
2951 else
2952 request_dump_byname (optarg, HEX_DUMP);
2953 break;
2954 case 'w':
2955 do_dump++;
2956 if (optarg == 0)
2957 do_debugging = 1;
2958 else
2960 unsigned int index = 0;
2962 do_debugging = 0;
2964 while (optarg[index])
2965 switch (optarg[index++])
2967 case 'i':
2968 case 'I':
2969 do_debug_info = 1;
2970 break;
2972 case 'a':
2973 case 'A':
2974 do_debug_abbrevs = 1;
2975 break;
2977 case 'l':
2978 case 'L':
2979 do_debug_lines = 1;
2980 break;
2982 case 'p':
2983 case 'P':
2984 do_debug_pubnames = 1;
2985 break;
2987 case 'r':
2988 do_debug_aranges = 1;
2989 break;
2991 case 'R':
2992 do_debug_ranges = 1;
2993 break;
2995 case 'F':
2996 do_debug_frames_interp = 1;
2997 case 'f':
2998 do_debug_frames = 1;
2999 break;
3001 case 'm':
3002 case 'M':
3003 do_debug_macinfo = 1;
3004 break;
3006 case 's':
3007 case 'S':
3008 do_debug_str = 1;
3009 break;
3011 case 'o':
3012 case 'O':
3013 do_debug_loc = 1;
3014 break;
3016 default:
3017 warn (_("Unrecognized debug option '%s'\n"), optarg);
3018 break;
3021 break;
3022 case OPTION_DEBUG_DUMP:
3023 do_dump++;
3024 if (optarg == 0)
3025 do_debugging = 1;
3026 else
3028 typedef struct
3030 const char * option;
3031 int * variable;
3033 debug_dump_long_opts;
3035 debug_dump_long_opts opts_table [] =
3037 /* Please keep this table alpha- sorted. */
3038 { "Ranges", & do_debug_ranges },
3039 { "abbrev", & do_debug_abbrevs },
3040 { "aranges", & do_debug_aranges },
3041 { "frames", & do_debug_frames },
3042 { "frames-interp", & do_debug_frames_interp },
3043 { "info", & do_debug_info },
3044 { "line", & do_debug_lines },
3045 { "loc", & do_debug_loc },
3046 { "macro", & do_debug_macinfo },
3047 { "pubnames", & do_debug_pubnames },
3048 /* This entry is for compatability
3049 with earlier versions of readelf. */
3050 { "ranges", & do_debug_aranges },
3051 { "str", & do_debug_str },
3052 { NULL, NULL }
3055 const char *p;
3057 do_debugging = 0;
3059 p = optarg;
3060 while (*p)
3062 debug_dump_long_opts * entry;
3064 for (entry = opts_table; entry->option; entry++)
3066 size_t len = strlen (entry->option);
3068 if (strneq (p, entry->option, len)
3069 && (p[len] == ',' || p[len] == '\0'))
3071 * entry->variable = 1;
3073 /* The --debug-dump=frames-interp option also
3074 enables the --debug-dump=frames option. */
3075 if (do_debug_frames_interp)
3076 do_debug_frames = 1;
3078 p += len;
3079 break;
3083 if (entry->option == NULL)
3085 warn (_("Unrecognized debug option '%s'\n"), p);
3086 p = strchr (p, ',');
3087 if (p == NULL)
3088 break;
3091 if (*p == ',')
3092 p++;
3095 break;
3096 #ifdef SUPPORT_DISASSEMBLY
3097 case 'i':
3098 do_dump++;
3099 section = strtoul (optarg, & cp, 0);
3100 if (! *cp && section >= 0)
3102 request_dump (section, DISASS_DUMP);
3103 break;
3105 goto oops;
3106 #endif
3107 case 'v':
3108 print_version (program_name);
3109 break;
3110 case 'V':
3111 do_version++;
3112 break;
3113 case 'W':
3114 do_wide++;
3115 break;
3116 default:
3117 #ifdef SUPPORT_DISASSEMBLY
3118 oops:
3119 #endif
3120 /* xgettext:c-format */
3121 error (_("Invalid option '-%c'\n"), c);
3122 /* Drop through. */
3123 case '?':
3124 usage (stderr);
3128 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
3129 && !do_segments && !do_header && !do_dump && !do_version
3130 && !do_histogram && !do_debugging && !do_arch && !do_notes
3131 && !do_section_groups)
3132 usage (stderr);
3133 else if (argc < 3)
3135 warn (_("Nothing to do.\n"));
3136 usage (stderr);
3140 static const char *
3141 get_elf_class (unsigned int elf_class)
3143 static char buff[32];
3145 switch (elf_class)
3147 case ELFCLASSNONE: return _("none");
3148 case ELFCLASS32: return "ELF32";
3149 case ELFCLASS64: return "ELF64";
3150 default:
3151 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
3152 return buff;
3156 static const char *
3157 get_data_encoding (unsigned int encoding)
3159 static char buff[32];
3161 switch (encoding)
3163 case ELFDATANONE: return _("none");
3164 case ELFDATA2LSB: return _("2's complement, little endian");
3165 case ELFDATA2MSB: return _("2's complement, big endian");
3166 default:
3167 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
3168 return buff;
3172 /* Decode the data held in 'elf_header'. */
3174 static int
3175 process_file_header (void)
3177 if ( elf_header.e_ident[EI_MAG0] != ELFMAG0
3178 || elf_header.e_ident[EI_MAG1] != ELFMAG1
3179 || elf_header.e_ident[EI_MAG2] != ELFMAG2
3180 || elf_header.e_ident[EI_MAG3] != ELFMAG3)
3182 error
3183 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
3184 return 0;
3187 if (do_header)
3189 int i;
3191 printf (_("ELF Header:\n"));
3192 printf (_(" Magic: "));
3193 for (i = 0; i < EI_NIDENT; i++)
3194 printf ("%2.2x ", elf_header.e_ident[i]);
3195 printf ("\n");
3196 printf (_(" Class: %s\n"),
3197 get_elf_class (elf_header.e_ident[EI_CLASS]));
3198 printf (_(" Data: %s\n"),
3199 get_data_encoding (elf_header.e_ident[EI_DATA]));
3200 printf (_(" Version: %d %s\n"),
3201 elf_header.e_ident[EI_VERSION],
3202 (elf_header.e_ident[EI_VERSION] == EV_CURRENT
3203 ? "(current)"
3204 : (elf_header.e_ident[EI_VERSION] != EV_NONE
3205 ? "<unknown: %lx>"
3206 : "")));
3207 printf (_(" OS/ABI: %s\n"),
3208 get_osabi_name (elf_header.e_ident[EI_OSABI]));
3209 printf (_(" ABI Version: %d\n"),
3210 elf_header.e_ident[EI_ABIVERSION]);
3211 printf (_(" Type: %s\n"),
3212 get_file_type (elf_header.e_type));
3213 printf (_(" Machine: %s\n"),
3214 get_machine_name (elf_header.e_machine));
3215 printf (_(" Version: 0x%lx\n"),
3216 (unsigned long) elf_header.e_version);
3218 printf (_(" Entry point address: "));
3219 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3220 printf (_("\n Start of program headers: "));
3221 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3222 printf (_(" (bytes into file)\n Start of section headers: "));
3223 print_vma ((bfd_vma) elf_header.e_shoff, DEC);
3224 printf (_(" (bytes into file)\n"));
3226 printf (_(" Flags: 0x%lx%s\n"),
3227 (unsigned long) elf_header.e_flags,
3228 get_machine_flags (elf_header.e_flags, elf_header.e_machine));
3229 printf (_(" Size of this header: %ld (bytes)\n"),
3230 (long) elf_header.e_ehsize);
3231 printf (_(" Size of program headers: %ld (bytes)\n"),
3232 (long) elf_header.e_phentsize);
3233 printf (_(" Number of program headers: %ld\n"),
3234 (long) elf_header.e_phnum);
3235 printf (_(" Size of section headers: %ld (bytes)\n"),
3236 (long) elf_header.e_shentsize);
3237 printf (_(" Number of section headers: %ld"),
3238 (long) elf_header.e_shnum);
3239 if (section_headers != NULL && elf_header.e_shnum == 0)
3240 printf (" (%ld)", (long) section_headers[0].sh_size);
3241 putc ('\n', stdout);
3242 printf (_(" Section header string table index: %ld"),
3243 (long) elf_header.e_shstrndx);
3244 if (section_headers != NULL && elf_header.e_shstrndx == SHN_XINDEX)
3245 printf (" (%ld)", (long) section_headers[0].sh_link);
3246 else if (elf_header.e_shstrndx != SHN_UNDEF
3247 && (elf_header.e_shstrndx >= elf_header.e_shnum
3248 || (elf_header.e_shstrndx >= SHN_LORESERVE
3249 && elf_header.e_shstrndx <= SHN_HIRESERVE)))
3250 printf (" <corrupt: out of range>");
3251 putc ('\n', stdout);
3254 if (section_headers != NULL)
3256 if (elf_header.e_shnum == 0)
3257 elf_header.e_shnum = section_headers[0].sh_size;
3258 if (elf_header.e_shstrndx == SHN_XINDEX)
3259 elf_header.e_shstrndx = section_headers[0].sh_link;
3260 else if (elf_header.e_shstrndx != SHN_UNDEF
3261 && (elf_header.e_shstrndx >= elf_header.e_shnum
3262 || (elf_header.e_shstrndx >= SHN_LORESERVE
3263 && elf_header.e_shstrndx <= SHN_HIRESERVE)))
3264 elf_header.e_shstrndx = SHN_UNDEF;
3265 free (section_headers);
3266 section_headers = NULL;
3269 return 1;
3273 static int
3274 get_32bit_program_headers (FILE *file, Elf_Internal_Phdr *program_headers)
3276 Elf32_External_Phdr *phdrs;
3277 Elf32_External_Phdr *external;
3278 Elf_Internal_Phdr *internal;
3279 unsigned int i;
3281 phdrs = get_data (NULL, file, elf_header.e_phoff,
3282 elf_header.e_phentsize, elf_header.e_phnum,
3283 _("program headers"));
3284 if (!phdrs)
3285 return 0;
3287 for (i = 0, internal = program_headers, external = phdrs;
3288 i < elf_header.e_phnum;
3289 i++, internal++, external++)
3291 internal->p_type = BYTE_GET (external->p_type);
3292 internal->p_offset = BYTE_GET (external->p_offset);
3293 internal->p_vaddr = BYTE_GET (external->p_vaddr);
3294 internal->p_paddr = BYTE_GET (external->p_paddr);
3295 internal->p_filesz = BYTE_GET (external->p_filesz);
3296 internal->p_memsz = BYTE_GET (external->p_memsz);
3297 internal->p_flags = BYTE_GET (external->p_flags);
3298 internal->p_align = BYTE_GET (external->p_align);
3301 free (phdrs);
3303 return 1;
3306 static int
3307 get_64bit_program_headers (FILE *file, Elf_Internal_Phdr *program_headers)
3309 Elf64_External_Phdr *phdrs;
3310 Elf64_External_Phdr *external;
3311 Elf_Internal_Phdr *internal;
3312 unsigned int i;
3314 phdrs = get_data (NULL, file, elf_header.e_phoff,
3315 elf_header.e_phentsize, elf_header.e_phnum,
3316 _("program headers"));
3317 if (!phdrs)
3318 return 0;
3320 for (i = 0, internal = program_headers, external = phdrs;
3321 i < elf_header.e_phnum;
3322 i++, internal++, external++)
3324 internal->p_type = BYTE_GET (external->p_type);
3325 internal->p_flags = BYTE_GET (external->p_flags);
3326 internal->p_offset = BYTE_GET (external->p_offset);
3327 internal->p_vaddr = BYTE_GET (external->p_vaddr);
3328 internal->p_paddr = BYTE_GET (external->p_paddr);
3329 internal->p_filesz = BYTE_GET (external->p_filesz);
3330 internal->p_memsz = BYTE_GET (external->p_memsz);
3331 internal->p_align = BYTE_GET (external->p_align);
3334 free (phdrs);
3336 return 1;
3339 /* Returns 1 if the program headers were read into `program_headers'. */
3341 static int
3342 get_program_headers (FILE *file)
3344 Elf_Internal_Phdr *phdrs;
3346 /* Check cache of prior read. */
3347 if (program_headers != NULL)
3348 return 1;
3350 phdrs = cmalloc (elf_header.e_phnum, sizeof (Elf_Internal_Phdr));
3352 if (phdrs == NULL)
3354 error (_("Out of memory\n"));
3355 return 0;
3358 if (is_32bit_elf
3359 ? get_32bit_program_headers (file, phdrs)
3360 : get_64bit_program_headers (file, phdrs))
3362 program_headers = phdrs;
3363 return 1;
3366 free (phdrs);
3367 return 0;
3370 /* Returns 1 if the program headers were loaded. */
3372 static int
3373 process_program_headers (FILE *file)
3375 Elf_Internal_Phdr *segment;
3376 unsigned int i;
3378 if (elf_header.e_phnum == 0)
3380 if (do_segments)
3381 printf (_("\nThere are no program headers in this file.\n"));
3382 return 0;
3385 if (do_segments && !do_header)
3387 printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
3388 printf (_("Entry point "));
3389 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3390 printf (_("\nThere are %d program headers, starting at offset "),
3391 elf_header.e_phnum);
3392 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3393 printf ("\n");
3396 if (! get_program_headers (file))
3397 return 0;
3399 if (do_segments)
3401 if (elf_header.e_phnum > 1)
3402 printf (_("\nProgram Headers:\n"));
3403 else
3404 printf (_("\nProgram Headers:\n"));
3406 if (is_32bit_elf)
3407 printf
3408 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
3409 else if (do_wide)
3410 printf
3411 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
3412 else
3414 printf
3415 (_(" Type Offset VirtAddr PhysAddr\n"));
3416 printf
3417 (_(" FileSiz MemSiz Flags Align\n"));
3421 dynamic_addr = 0;
3422 dynamic_size = 0;
3424 for (i = 0, segment = program_headers;
3425 i < elf_header.e_phnum;
3426 i++, segment++)
3428 if (do_segments)
3430 printf (" %-14.14s ", get_segment_type (segment->p_type));
3432 if (is_32bit_elf)
3434 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3435 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
3436 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
3437 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
3438 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
3439 printf ("%c%c%c ",
3440 (segment->p_flags & PF_R ? 'R' : ' '),
3441 (segment->p_flags & PF_W ? 'W' : ' '),
3442 (segment->p_flags & PF_X ? 'E' : ' '));
3443 printf ("%#lx", (unsigned long) segment->p_align);
3445 else if (do_wide)
3447 if ((unsigned long) segment->p_offset == segment->p_offset)
3448 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3449 else
3451 print_vma (segment->p_offset, FULL_HEX);
3452 putchar (' ');
3455 print_vma (segment->p_vaddr, FULL_HEX);
3456 putchar (' ');
3457 print_vma (segment->p_paddr, FULL_HEX);
3458 putchar (' ');
3460 if ((unsigned long) segment->p_filesz == segment->p_filesz)
3461 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
3462 else
3464 print_vma (segment->p_filesz, FULL_HEX);
3465 putchar (' ');
3468 if ((unsigned long) segment->p_memsz == segment->p_memsz)
3469 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
3470 else
3472 print_vma (segment->p_offset, FULL_HEX);
3475 printf (" %c%c%c ",
3476 (segment->p_flags & PF_R ? 'R' : ' '),
3477 (segment->p_flags & PF_W ? 'W' : ' '),
3478 (segment->p_flags & PF_X ? 'E' : ' '));
3480 if ((unsigned long) segment->p_align == segment->p_align)
3481 printf ("%#lx", (unsigned long) segment->p_align);
3482 else
3484 print_vma (segment->p_align, PREFIX_HEX);
3487 else
3489 print_vma (segment->p_offset, FULL_HEX);
3490 putchar (' ');
3491 print_vma (segment->p_vaddr, FULL_HEX);
3492 putchar (' ');
3493 print_vma (segment->p_paddr, FULL_HEX);
3494 printf ("\n ");
3495 print_vma (segment->p_filesz, FULL_HEX);
3496 putchar (' ');
3497 print_vma (segment->p_memsz, FULL_HEX);
3498 printf (" %c%c%c ",
3499 (segment->p_flags & PF_R ? 'R' : ' '),
3500 (segment->p_flags & PF_W ? 'W' : ' '),
3501 (segment->p_flags & PF_X ? 'E' : ' '));
3502 print_vma (segment->p_align, HEX);
3506 switch (segment->p_type)
3508 case PT_DYNAMIC:
3509 if (dynamic_addr)
3510 error (_("more than one dynamic segment\n"));
3512 /* Try to locate the .dynamic section. If there is
3513 a section header table, we can easily locate it. */
3514 if (section_headers != NULL)
3516 Elf_Internal_Shdr *sec;
3518 sec = find_section (".dynamic");
3519 if (sec == NULL || sec->sh_size == 0)
3521 error (_("no .dynamic section in the dynamic segment\n"));
3522 break;
3525 if (sec->sh_type == SHT_NOBITS)
3526 break;
3528 dynamic_addr = sec->sh_offset;
3529 dynamic_size = sec->sh_size;
3531 if (dynamic_addr < segment->p_offset
3532 || dynamic_addr > segment->p_offset + segment->p_filesz)
3533 warn (_("the .dynamic section is not contained within the dynamic segment\n"));
3534 else if (dynamic_addr > segment->p_offset)
3535 warn (_("the .dynamic section is not the first section in the dynamic segment.\n"));
3537 else
3539 /* Otherwise, we can only assume that the .dynamic
3540 section is the first section in the DYNAMIC segment. */
3541 dynamic_addr = segment->p_offset;
3542 dynamic_size = segment->p_filesz;
3544 break;
3546 case PT_INTERP:
3547 if (fseek (file, archive_file_offset + (long) segment->p_offset,
3548 SEEK_SET))
3549 error (_("Unable to find program interpreter name\n"));
3550 else
3552 char fmt [32];
3553 int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX);
3555 if (ret >= (int) sizeof (fmt) || ret < 0)
3556 error (_("Internal error: failed to create format string to display program interpreter\n"));
3558 program_interpreter[0] = 0;
3559 if (fscanf (file, fmt, program_interpreter) <= 0)
3560 error (_("Unable to read program interpreter name\n"));
3562 if (do_segments)
3563 printf (_("\n [Requesting program interpreter: %s]"),
3564 program_interpreter);
3566 break;
3569 if (do_segments)
3570 putc ('\n', stdout);
3573 if (do_segments && section_headers != NULL && string_table != NULL)
3575 printf (_("\n Section to Segment mapping:\n"));
3576 printf (_(" Segment Sections...\n"));
3578 for (i = 0; i < elf_header.e_phnum; i++)
3580 unsigned int j;
3581 Elf_Internal_Shdr *section;
3583 segment = program_headers + i;
3584 section = section_headers;
3586 printf (" %2.2d ", i);
3588 for (j = 1; j < elf_header.e_shnum; j++, section++)
3590 if (ELF_IS_SECTION_IN_SEGMENT_MEMORY(section, segment))
3591 printf ("%s ", SECTION_NAME (section));
3594 putc ('\n',stdout);
3598 return 1;
3602 /* Find the file offset corresponding to VMA by using the program headers. */
3604 static long
3605 offset_from_vma (FILE *file, bfd_vma vma, bfd_size_type size)
3607 Elf_Internal_Phdr *seg;
3609 if (! get_program_headers (file))
3611 warn (_("Cannot interpret virtual addresses without program headers.\n"));
3612 return (long) vma;
3615 for (seg = program_headers;
3616 seg < program_headers + elf_header.e_phnum;
3617 ++seg)
3619 if (seg->p_type != PT_LOAD)
3620 continue;
3622 if (vma >= (seg->p_vaddr & -seg->p_align)
3623 && vma + size <= seg->p_vaddr + seg->p_filesz)
3624 return vma - seg->p_vaddr + seg->p_offset;
3627 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
3628 (long) vma);
3629 return (long) vma;
3633 static int
3634 get_32bit_section_headers (FILE *file, unsigned int num)
3636 Elf32_External_Shdr *shdrs;
3637 Elf_Internal_Shdr *internal;
3638 unsigned int i;
3640 shdrs = get_data (NULL, file, elf_header.e_shoff,
3641 elf_header.e_shentsize, num, _("section headers"));
3642 if (!shdrs)
3643 return 0;
3645 section_headers = cmalloc (num, sizeof (Elf_Internal_Shdr));
3647 if (section_headers == NULL)
3649 error (_("Out of memory\n"));
3650 return 0;
3653 for (i = 0, internal = section_headers;
3654 i < num;
3655 i++, internal++)
3657 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3658 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3659 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
3660 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
3661 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3662 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
3663 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3664 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3665 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3666 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
3669 free (shdrs);
3671 return 1;
3674 static int
3675 get_64bit_section_headers (FILE *file, unsigned int num)
3677 Elf64_External_Shdr *shdrs;
3678 Elf_Internal_Shdr *internal;
3679 unsigned int i;
3681 shdrs = get_data (NULL, file, elf_header.e_shoff,
3682 elf_header.e_shentsize, num, _("section headers"));
3683 if (!shdrs)
3684 return 0;
3686 section_headers = cmalloc (num, sizeof (Elf_Internal_Shdr));
3688 if (section_headers == NULL)
3690 error (_("Out of memory\n"));
3691 return 0;
3694 for (i = 0, internal = section_headers;
3695 i < num;
3696 i++, internal++)
3698 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3699 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3700 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
3701 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
3702 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
3703 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
3704 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3705 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3706 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3707 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3710 free (shdrs);
3712 return 1;
3715 static Elf_Internal_Sym *
3716 get_32bit_elf_symbols (FILE *file, Elf_Internal_Shdr *section)
3718 unsigned long number;
3719 Elf32_External_Sym *esyms;
3720 Elf_External_Sym_Shndx *shndx;
3721 Elf_Internal_Sym *isyms;
3722 Elf_Internal_Sym *psym;
3723 unsigned int j;
3725 esyms = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
3726 _("symbols"));
3727 if (!esyms)
3728 return NULL;
3730 shndx = NULL;
3731 if (symtab_shndx_hdr != NULL
3732 && (symtab_shndx_hdr->sh_link
3733 == (unsigned long) SECTION_HEADER_NUM (section - section_headers)))
3735 shndx = get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3736 1, symtab_shndx_hdr->sh_size, _("symtab shndx"));
3737 if (!shndx)
3739 free (esyms);
3740 return NULL;
3744 number = section->sh_size / section->sh_entsize;
3745 isyms = cmalloc (number, sizeof (Elf_Internal_Sym));
3747 if (isyms == NULL)
3749 error (_("Out of memory\n"));
3750 if (shndx)
3751 free (shndx);
3752 free (esyms);
3753 return NULL;
3756 for (j = 0, psym = isyms;
3757 j < number;
3758 j++, psym++)
3760 psym->st_name = BYTE_GET (esyms[j].st_name);
3761 psym->st_value = BYTE_GET (esyms[j].st_value);
3762 psym->st_size = BYTE_GET (esyms[j].st_size);
3763 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3764 if (psym->st_shndx == SHN_XINDEX && shndx != NULL)
3765 psym->st_shndx
3766 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3767 psym->st_info = BYTE_GET (esyms[j].st_info);
3768 psym->st_other = BYTE_GET (esyms[j].st_other);
3771 if (shndx)
3772 free (shndx);
3773 free (esyms);
3775 return isyms;
3778 static Elf_Internal_Sym *
3779 get_64bit_elf_symbols (FILE *file, Elf_Internal_Shdr *section)
3781 unsigned long number;
3782 Elf64_External_Sym *esyms;
3783 Elf_External_Sym_Shndx *shndx;
3784 Elf_Internal_Sym *isyms;
3785 Elf_Internal_Sym *psym;
3786 unsigned int j;
3788 esyms = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
3789 _("symbols"));
3790 if (!esyms)
3791 return NULL;
3793 shndx = NULL;
3794 if (symtab_shndx_hdr != NULL
3795 && (symtab_shndx_hdr->sh_link
3796 == (unsigned long) SECTION_HEADER_NUM (section - section_headers)))
3798 shndx = get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3799 1, symtab_shndx_hdr->sh_size, _("symtab shndx"));
3800 if (!shndx)
3802 free (esyms);
3803 return NULL;
3807 number = section->sh_size / section->sh_entsize;
3808 isyms = cmalloc (number, sizeof (Elf_Internal_Sym));
3810 if (isyms == NULL)
3812 error (_("Out of memory\n"));
3813 if (shndx)
3814 free (shndx);
3815 free (esyms);
3816 return NULL;
3819 for (j = 0, psym = isyms;
3820 j < number;
3821 j++, psym++)
3823 psym->st_name = BYTE_GET (esyms[j].st_name);
3824 psym->st_info = BYTE_GET (esyms[j].st_info);
3825 psym->st_other = BYTE_GET (esyms[j].st_other);
3826 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3827 if (psym->st_shndx == SHN_XINDEX && shndx != NULL)
3828 psym->st_shndx
3829 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3830 psym->st_value = BYTE_GET (esyms[j].st_value);
3831 psym->st_size = BYTE_GET (esyms[j].st_size);
3834 if (shndx)
3835 free (shndx);
3836 free (esyms);
3838 return isyms;
3841 static const char *
3842 get_elf_section_flags (bfd_vma sh_flags)
3844 static char buff[1024];
3845 char *p = buff;
3846 int field_size = is_32bit_elf ? 8 : 16;
3847 int index, size = sizeof (buff) - (field_size + 4 + 1);
3848 bfd_vma os_flags = 0;
3849 bfd_vma proc_flags = 0;
3850 bfd_vma unknown_flags = 0;
3851 const struct
3853 const char *str;
3854 int len;
3856 flags [] =
3858 { "WRITE", 5 },
3859 { "ALLOC", 5 },
3860 { "EXEC", 4 },
3861 { "MERGE", 5 },
3862 { "STRINGS", 7 },
3863 { "INFO LINK", 9 },
3864 { "LINK ORDER", 10 },
3865 { "OS NONCONF", 10 },
3866 { "GROUP", 5 },
3867 { "TLS", 3 }
3870 if (do_section_details)
3872 sprintf (buff, "[%*.*lx]: ",
3873 field_size, field_size, (unsigned long) sh_flags);
3874 p += field_size + 4;
3877 while (sh_flags)
3879 bfd_vma flag;
3881 flag = sh_flags & - sh_flags;
3882 sh_flags &= ~ flag;
3884 if (do_section_details)
3886 switch (flag)
3888 case SHF_WRITE: index = 0; break;
3889 case SHF_ALLOC: index = 1; break;
3890 case SHF_EXECINSTR: index = 2; break;
3891 case SHF_MERGE: index = 3; break;
3892 case SHF_STRINGS: index = 4; break;
3893 case SHF_INFO_LINK: index = 5; break;
3894 case SHF_LINK_ORDER: index = 6; break;
3895 case SHF_OS_NONCONFORMING: index = 7; break;
3896 case SHF_GROUP: index = 8; break;
3897 case SHF_TLS: index = 9; break;
3899 default:
3900 index = -1;
3901 break;
3904 if (index != -1)
3906 if (p != buff + field_size + 4)
3908 if (size < (10 + 2))
3909 abort ();
3910 size -= 2;
3911 *p++ = ',';
3912 *p++ = ' ';
3915 size -= flags [index].len;
3916 p = stpcpy (p, flags [index].str);
3918 else if (flag & SHF_MASKOS)
3919 os_flags |= flag;
3920 else if (flag & SHF_MASKPROC)
3921 proc_flags |= flag;
3922 else
3923 unknown_flags |= flag;
3925 else
3927 switch (flag)
3929 case SHF_WRITE: *p = 'W'; break;
3930 case SHF_ALLOC: *p = 'A'; break;
3931 case SHF_EXECINSTR: *p = 'X'; break;
3932 case SHF_MERGE: *p = 'M'; break;
3933 case SHF_STRINGS: *p = 'S'; break;
3934 case SHF_INFO_LINK: *p = 'I'; break;
3935 case SHF_LINK_ORDER: *p = 'L'; break;
3936 case SHF_OS_NONCONFORMING: *p = 'O'; break;
3937 case SHF_GROUP: *p = 'G'; break;
3938 case SHF_TLS: *p = 'T'; break;
3940 default:
3941 if (elf_header.e_machine == EM_X86_64
3942 && flag == SHF_X86_64_LARGE)
3943 *p = 'l';
3944 else if (flag & SHF_MASKOS)
3946 *p = 'o';
3947 sh_flags &= ~ SHF_MASKOS;
3949 else if (flag & SHF_MASKPROC)
3951 *p = 'p';
3952 sh_flags &= ~ SHF_MASKPROC;
3954 else
3955 *p = 'x';
3956 break;
3958 p++;
3962 if (do_section_details)
3964 if (os_flags)
3966 size -= 5 + field_size;
3967 if (p != buff + field_size + 4)
3969 if (size < (2 + 1))
3970 abort ();
3971 size -= 2;
3972 *p++ = ',';
3973 *p++ = ' ';
3975 sprintf (p, "OS (%*.*lx)", field_size, field_size,
3976 (unsigned long) os_flags);
3977 p += 5 + field_size;
3979 if (proc_flags)
3981 size -= 7 + field_size;
3982 if (p != buff + field_size + 4)
3984 if (size < (2 + 1))
3985 abort ();
3986 size -= 2;
3987 *p++ = ',';
3988 *p++ = ' ';
3990 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
3991 (unsigned long) proc_flags);
3992 p += 7 + field_size;
3994 if (unknown_flags)
3996 size -= 10 + field_size;
3997 if (p != buff + field_size + 4)
3999 if (size < (2 + 1))
4000 abort ();
4001 size -= 2;
4002 *p++ = ',';
4003 *p++ = ' ';
4005 sprintf (p, "UNKNOWN (%*.*lx)", field_size, field_size,
4006 (unsigned long) unknown_flags);
4007 p += 10 + field_size;
4011 *p = '\0';
4012 return buff;
4015 static int
4016 process_section_headers (FILE *file)
4018 Elf_Internal_Shdr *section;
4019 unsigned int i;
4021 section_headers = NULL;
4023 if (elf_header.e_shnum == 0)
4025 if (do_sections)
4026 printf (_("\nThere are no sections in this file.\n"));
4028 return 1;
4031 if (do_sections && !do_header)
4032 printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
4033 elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
4035 if (is_32bit_elf)
4037 if (! get_32bit_section_headers (file, elf_header.e_shnum))
4038 return 0;
4040 else if (! get_64bit_section_headers (file, elf_header.e_shnum))
4041 return 0;
4043 /* Read in the string table, so that we have names to display. */
4044 if (elf_header.e_shstrndx != SHN_UNDEF
4045 && SECTION_HEADER_INDEX (elf_header.e_shstrndx) < elf_header.e_shnum)
4047 section = SECTION_HEADER (elf_header.e_shstrndx);
4049 if (section->sh_size != 0)
4051 string_table = get_data (NULL, file, section->sh_offset,
4052 1, section->sh_size, _("string table"));
4054 string_table_length = string_table != NULL ? section->sh_size : 0;
4058 /* Scan the sections for the dynamic symbol table
4059 and dynamic string table and debug sections. */
4060 dynamic_symbols = NULL;
4061 dynamic_strings = NULL;
4062 dynamic_syminfo = NULL;
4063 symtab_shndx_hdr = NULL;
4065 eh_addr_size = is_32bit_elf ? 4 : 8;
4066 switch (elf_header.e_machine)
4068 case EM_MIPS:
4069 case EM_MIPS_RS3_LE:
4070 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
4071 FDE addresses. However, the ABI also has a semi-official ILP32
4072 variant for which the normal FDE address size rules apply.
4074 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
4075 section, where XX is the size of longs in bits. Unfortunately,
4076 earlier compilers provided no way of distinguishing ILP32 objects
4077 from LP64 objects, so if there's any doubt, we should assume that
4078 the official LP64 form is being used. */
4079 if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
4080 && find_section (".gcc_compiled_long32") == NULL)
4081 eh_addr_size = 8;
4082 break;
4084 case EM_H8_300:
4085 case EM_H8_300H:
4086 switch (elf_header.e_flags & EF_H8_MACH)
4088 case E_H8_MACH_H8300:
4089 case E_H8_MACH_H8300HN:
4090 case E_H8_MACH_H8300SN:
4091 case E_H8_MACH_H8300SXN:
4092 eh_addr_size = 2;
4093 break;
4094 case E_H8_MACH_H8300H:
4095 case E_H8_MACH_H8300S:
4096 case E_H8_MACH_H8300SX:
4097 eh_addr_size = 4;
4098 break;
4102 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
4103 do \
4105 size_t expected_entsize \
4106 = is_32bit_elf ? size32 : size64; \
4107 if (section->sh_entsize != expected_entsize) \
4108 error (_("Section %d has invalid sh_entsize %lx (expected %lx)\n"), \
4109 i, (unsigned long int) section->sh_entsize, \
4110 (unsigned long int) expected_entsize); \
4111 section->sh_entsize = expected_entsize; \
4113 while (0)
4114 #define CHECK_ENTSIZE(section, i, type) \
4115 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
4116 sizeof (Elf64_External_##type))
4118 for (i = 0, section = section_headers;
4119 i < elf_header.e_shnum;
4120 i++, section++)
4122 char *name = SECTION_NAME (section);
4124 if (section->sh_type == SHT_DYNSYM)
4126 if (dynamic_symbols != NULL)
4128 error (_("File contains multiple dynamic symbol tables\n"));
4129 continue;
4132 CHECK_ENTSIZE (section, i, Sym);
4133 num_dynamic_syms = section->sh_size / section->sh_entsize;
4134 dynamic_symbols = GET_ELF_SYMBOLS (file, section);
4136 else if (section->sh_type == SHT_STRTAB
4137 && streq (name, ".dynstr"))
4139 if (dynamic_strings != NULL)
4141 error (_("File contains multiple dynamic string tables\n"));
4142 continue;
4145 dynamic_strings = get_data (NULL, file, section->sh_offset,
4146 1, section->sh_size, _("dynamic strings"));
4147 dynamic_strings_length = section->sh_size;
4149 else if (section->sh_type == SHT_SYMTAB_SHNDX)
4151 if (symtab_shndx_hdr != NULL)
4153 error (_("File contains multiple symtab shndx tables\n"));
4154 continue;
4156 symtab_shndx_hdr = section;
4158 else if (section->sh_type == SHT_SYMTAB)
4159 CHECK_ENTSIZE (section, i, Sym);
4160 else if (section->sh_type == SHT_GROUP)
4161 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
4162 else if (section->sh_type == SHT_REL)
4163 CHECK_ENTSIZE (section, i, Rel);
4164 else if (section->sh_type == SHT_RELA)
4165 CHECK_ENTSIZE (section, i, Rela);
4166 else if ((do_debugging || do_debug_info || do_debug_abbrevs
4167 || do_debug_lines || do_debug_pubnames || do_debug_aranges
4168 || do_debug_frames || do_debug_macinfo || do_debug_str
4169 || do_debug_loc || do_debug_ranges)
4170 && const_strneq (name, ".debug_"))
4172 name += 7;
4174 if (do_debugging
4175 || (do_debug_info && streq (name, "info"))
4176 || (do_debug_abbrevs && streq (name, "abbrev"))
4177 || (do_debug_lines && streq (name, "line"))
4178 || (do_debug_pubnames && streq (name, "pubnames"))
4179 || (do_debug_aranges && streq (name, "aranges"))
4180 || (do_debug_ranges && streq (name, "ranges"))
4181 || (do_debug_frames && streq (name, "frame"))
4182 || (do_debug_macinfo && streq (name, "macinfo"))
4183 || (do_debug_str && streq (name, "str"))
4184 || (do_debug_loc && streq (name, "loc"))
4186 request_dump (i, DEBUG_DUMP);
4188 /* linkonce section to be combined with .debug_info at link time. */
4189 else if ((do_debugging || do_debug_info)
4190 && const_strneq (name, ".gnu.linkonce.wi."))
4191 request_dump (i, DEBUG_DUMP);
4192 else if (do_debug_frames && streq (name, ".eh_frame"))
4193 request_dump (i, DEBUG_DUMP);
4196 if (! do_sections)
4197 return 1;
4199 if (elf_header.e_shnum > 1)
4200 printf (_("\nSection Headers:\n"));
4201 else
4202 printf (_("\nSection Header:\n"));
4204 if (is_32bit_elf)
4206 if (do_section_details)
4208 printf (_(" [Nr] Name\n"));
4209 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
4211 else
4212 printf
4213 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
4215 else if (do_wide)
4217 if (do_section_details)
4219 printf (_(" [Nr] Name\n"));
4220 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
4222 else
4223 printf
4224 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
4226 else
4228 if (do_section_details)
4230 printf (_(" [Nr] Name\n"));
4231 printf (_(" Type Address Offset Link\n"));
4232 printf (_(" Size EntSize Info Align\n"));
4234 else
4236 printf (_(" [Nr] Name Type Address Offset\n"));
4237 printf (_(" Size EntSize Flags Link Info Align\n"));
4241 if (do_section_details)
4242 printf (_(" Flags\n"));
4244 for (i = 0, section = section_headers;
4245 i < elf_header.e_shnum;
4246 i++, section++)
4248 if (do_section_details)
4250 printf (" [%2u] %s\n",
4251 SECTION_HEADER_NUM (i),
4252 SECTION_NAME (section));
4253 if (is_32bit_elf || do_wide)
4254 printf (" %-15.15s ",
4255 get_section_type_name (section->sh_type));
4257 else
4258 printf (" [%2u] %-17.17s %-15.15s ",
4259 SECTION_HEADER_NUM (i),
4260 SECTION_NAME (section),
4261 get_section_type_name (section->sh_type));
4263 if (is_32bit_elf)
4265 print_vma (section->sh_addr, LONG_HEX);
4267 printf ( " %6.6lx %6.6lx %2.2lx",
4268 (unsigned long) section->sh_offset,
4269 (unsigned long) section->sh_size,
4270 (unsigned long) section->sh_entsize);
4272 if (do_section_details)
4273 fputs (" ", stdout);
4274 else
4275 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4277 printf ("%2ld %3lu %2ld\n",
4278 (unsigned long) section->sh_link,
4279 (unsigned long) section->sh_info,
4280 (unsigned long) section->sh_addralign);
4282 else if (do_wide)
4284 print_vma (section->sh_addr, LONG_HEX);
4286 if ((long) section->sh_offset == section->sh_offset)
4287 printf (" %6.6lx", (unsigned long) section->sh_offset);
4288 else
4290 putchar (' ');
4291 print_vma (section->sh_offset, LONG_HEX);
4294 if ((unsigned long) section->sh_size == section->sh_size)
4295 printf (" %6.6lx", (unsigned long) section->sh_size);
4296 else
4298 putchar (' ');
4299 print_vma (section->sh_size, LONG_HEX);
4302 if ((unsigned long) section->sh_entsize == section->sh_entsize)
4303 printf (" %2.2lx", (unsigned long) section->sh_entsize);
4304 else
4306 putchar (' ');
4307 print_vma (section->sh_entsize, LONG_HEX);
4310 if (do_section_details)
4311 fputs (" ", stdout);
4312 else
4313 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4315 printf ("%2ld %3lu ",
4316 (unsigned long) section->sh_link,
4317 (unsigned long) section->sh_info);
4319 if ((unsigned long) section->sh_addralign == section->sh_addralign)
4320 printf ("%2ld\n", (unsigned long) section->sh_addralign);
4321 else
4323 print_vma (section->sh_addralign, DEC);
4324 putchar ('\n');
4327 else if (do_section_details)
4329 printf (" %-15.15s ",
4330 get_section_type_name (section->sh_type));
4331 print_vma (section->sh_addr, LONG_HEX);
4332 if ((long) section->sh_offset == section->sh_offset)
4333 printf (" %16.16lx", (unsigned long) section->sh_offset);
4334 else
4336 printf (" ");
4337 print_vma (section->sh_offset, LONG_HEX);
4339 printf (" %ld\n ", (unsigned long) section->sh_link);
4340 print_vma (section->sh_size, LONG_HEX);
4341 putchar (' ');
4342 print_vma (section->sh_entsize, LONG_HEX);
4344 printf (" %-16lu %ld\n",
4345 (unsigned long) section->sh_info,
4346 (unsigned long) section->sh_addralign);
4348 else
4350 putchar (' ');
4351 print_vma (section->sh_addr, LONG_HEX);
4352 if ((long) section->sh_offset == section->sh_offset)
4353 printf (" %8.8lx", (unsigned long) section->sh_offset);
4354 else
4356 printf (" ");
4357 print_vma (section->sh_offset, LONG_HEX);
4359 printf ("\n ");
4360 print_vma (section->sh_size, LONG_HEX);
4361 printf (" ");
4362 print_vma (section->sh_entsize, LONG_HEX);
4364 printf (" %3s ", get_elf_section_flags (section->sh_flags));
4366 printf (" %2ld %3lu %ld\n",
4367 (unsigned long) section->sh_link,
4368 (unsigned long) section->sh_info,
4369 (unsigned long) section->sh_addralign);
4372 if (do_section_details)
4373 printf (" %s\n", get_elf_section_flags (section->sh_flags));
4376 if (!do_section_details)
4377 printf (_("Key to Flags:\n\
4378 W (write), A (alloc), X (execute), M (merge), S (strings)\n\
4379 I (info), L (link order), G (group), x (unknown)\n\
4380 O (extra OS processing required) o (OS specific), p (processor specific)\n"));
4382 return 1;
4385 static const char *
4386 get_group_flags (unsigned int flags)
4388 static char buff[32];
4389 switch (flags)
4391 case GRP_COMDAT:
4392 return "COMDAT";
4394 default:
4395 snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x]"), flags);
4396 break;
4398 return buff;
4401 static int
4402 process_section_groups (FILE *file)
4404 Elf_Internal_Shdr *section;
4405 unsigned int i;
4406 struct group *group;
4407 Elf_Internal_Shdr *symtab_sec, *strtab_sec;
4408 Elf_Internal_Sym *symtab;
4409 char *strtab;
4410 size_t strtab_size;
4412 /* Don't process section groups unless needed. */
4413 if (!do_unwind && !do_section_groups)
4414 return 1;
4416 if (elf_header.e_shnum == 0)
4418 if (do_section_groups)
4419 printf (_("\nThere are no sections in this file.\n"));
4421 return 1;
4424 if (section_headers == NULL)
4426 error (_("Section headers are not available!\n"));
4427 abort ();
4430 section_headers_groups = calloc (elf_header.e_shnum,
4431 sizeof (struct group *));
4433 if (section_headers_groups == NULL)
4435 error (_("Out of memory\n"));
4436 return 0;
4439 /* Scan the sections for the group section. */
4440 group_count = 0;
4441 for (i = 0, section = section_headers;
4442 i < elf_header.e_shnum;
4443 i++, section++)
4444 if (section->sh_type == SHT_GROUP)
4445 group_count++;
4447 if (group_count == 0)
4449 if (do_section_groups)
4450 printf (_("\nThere are no section groups in this file.\n"));
4452 return 1;
4455 section_groups = calloc (group_count, sizeof (struct group));
4457 if (section_groups == NULL)
4459 error (_("Out of memory\n"));
4460 return 0;
4463 symtab_sec = NULL;
4464 strtab_sec = NULL;
4465 symtab = NULL;
4466 strtab = NULL;
4467 strtab_size = 0;
4468 for (i = 0, section = section_headers, group = section_groups;
4469 i < elf_header.e_shnum;
4470 i++, section++)
4472 if (section->sh_type == SHT_GROUP)
4474 char *name = SECTION_NAME (section);
4475 char *group_name;
4476 unsigned char *start, *indices;
4477 unsigned int entry, j, size;
4478 Elf_Internal_Shdr *sec;
4479 Elf_Internal_Sym *sym;
4481 /* Get the symbol table. */
4482 if (SECTION_HEADER_INDEX (section->sh_link) >= elf_header.e_shnum
4483 || ((sec = SECTION_HEADER (section->sh_link))->sh_type
4484 != SHT_SYMTAB))
4486 error (_("Bad sh_link in group section `%s'\n"), name);
4487 continue;
4490 if (symtab_sec != sec)
4492 symtab_sec = sec;
4493 if (symtab)
4494 free (symtab);
4495 symtab = GET_ELF_SYMBOLS (file, symtab_sec);
4498 sym = symtab + section->sh_info;
4500 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4502 bfd_vma sec_index = SECTION_HEADER_INDEX (sym->st_shndx);
4503 if (sec_index == 0)
4505 error (_("Bad sh_info in group section `%s'\n"), name);
4506 continue;
4509 group_name = SECTION_NAME (section_headers + sec_index);
4510 strtab_sec = NULL;
4511 if (strtab)
4512 free (strtab);
4513 strtab = NULL;
4514 strtab_size = 0;
4516 else
4518 /* Get the string table. */
4519 if (SECTION_HEADER_INDEX (symtab_sec->sh_link)
4520 >= elf_header.e_shnum)
4522 strtab_sec = NULL;
4523 if (strtab)
4524 free (strtab);
4525 strtab = NULL;
4526 strtab_size = 0;
4528 else if (strtab_sec
4529 != (sec = SECTION_HEADER (symtab_sec->sh_link)))
4531 strtab_sec = sec;
4532 if (strtab)
4533 free (strtab);
4534 strtab = get_data (NULL, file, strtab_sec->sh_offset,
4535 1, strtab_sec->sh_size,
4536 _("string table"));
4537 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
4539 group_name = sym->st_name < strtab_size
4540 ? strtab + sym->st_name : "<corrupt>";
4543 start = get_data (NULL, file, section->sh_offset,
4544 1, section->sh_size, _("section data"));
4546 indices = start;
4547 size = (section->sh_size / section->sh_entsize) - 1;
4548 entry = byte_get (indices, 4);
4549 indices += 4;
4551 if (do_section_groups)
4553 printf ("\n%s group section [%5u] `%s' [%s] contains %u sections:\n",
4554 get_group_flags (entry), i, name, group_name, size);
4556 printf (_(" [Index] Name\n"));
4559 group->group_index = i;
4561 for (j = 0; j < size; j++)
4563 struct group_list *g;
4565 entry = byte_get (indices, 4);
4566 indices += 4;
4568 if (SECTION_HEADER_INDEX (entry) >= elf_header.e_shnum)
4570 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
4571 entry, i, elf_header.e_shnum - 1);
4572 continue;
4574 else if (entry >= SHN_LORESERVE && entry <= SHN_HIRESERVE)
4576 error (_("invalid section [%5u] in group section [%5u]\n"),
4577 entry, i);
4578 continue;
4581 if (section_headers_groups [SECTION_HEADER_INDEX (entry)]
4582 != NULL)
4584 if (entry)
4586 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
4587 entry, i,
4588 section_headers_groups [SECTION_HEADER_INDEX (entry)]->group_index);
4589 continue;
4591 else
4593 /* Intel C/C++ compiler may put section 0 in a
4594 section group. We just warn it the first time
4595 and ignore it afterwards. */
4596 static int warned = 0;
4597 if (!warned)
4599 error (_("section 0 in group section [%5u]\n"),
4600 section_headers_groups [SECTION_HEADER_INDEX (entry)]->group_index);
4601 warned++;
4606 section_headers_groups [SECTION_HEADER_INDEX (entry)]
4607 = group;
4609 if (do_section_groups)
4611 sec = SECTION_HEADER (entry);
4612 printf (" [%5u] %s\n", entry, SECTION_NAME (sec));
4615 g = xmalloc (sizeof (struct group_list));
4616 g->section_index = entry;
4617 g->next = group->root;
4618 group->root = g;
4621 if (start)
4622 free (start);
4624 group++;
4628 if (symtab)
4629 free (symtab);
4630 if (strtab)
4631 free (strtab);
4632 return 1;
4635 static struct
4637 const char *name;
4638 int reloc;
4639 int size;
4640 int rela;
4641 } dynamic_relocations [] =
4643 { "REL", DT_REL, DT_RELSZ, FALSE },
4644 { "RELA", DT_RELA, DT_RELASZ, TRUE },
4645 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
4648 /* Process the reloc section. */
4650 static int
4651 process_relocs (FILE *file)
4653 unsigned long rel_size;
4654 unsigned long rel_offset;
4657 if (!do_reloc)
4658 return 1;
4660 if (do_using_dynamic)
4662 int is_rela;
4663 const char *name;
4664 int has_dynamic_reloc;
4665 unsigned int i;
4667 has_dynamic_reloc = 0;
4669 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
4671 is_rela = dynamic_relocations [i].rela;
4672 name = dynamic_relocations [i].name;
4673 rel_size = dynamic_info [dynamic_relocations [i].size];
4674 rel_offset = dynamic_info [dynamic_relocations [i].reloc];
4676 has_dynamic_reloc |= rel_size;
4678 if (is_rela == UNKNOWN)
4680 if (dynamic_relocations [i].reloc == DT_JMPREL)
4681 switch (dynamic_info[DT_PLTREL])
4683 case DT_REL:
4684 is_rela = FALSE;
4685 break;
4686 case DT_RELA:
4687 is_rela = TRUE;
4688 break;
4692 if (rel_size)
4694 printf
4695 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
4696 name, rel_offset, rel_size);
4698 dump_relocations (file,
4699 offset_from_vma (file, rel_offset, rel_size),
4700 rel_size,
4701 dynamic_symbols, num_dynamic_syms,
4702 dynamic_strings, dynamic_strings_length, is_rela);
4706 if (! has_dynamic_reloc)
4707 printf (_("\nThere are no dynamic relocations in this file.\n"));
4709 else
4711 Elf_Internal_Shdr *section;
4712 unsigned long i;
4713 int found = 0;
4715 for (i = 0, section = section_headers;
4716 i < elf_header.e_shnum;
4717 i++, section++)
4719 if ( section->sh_type != SHT_RELA
4720 && section->sh_type != SHT_REL)
4721 continue;
4723 rel_offset = section->sh_offset;
4724 rel_size = section->sh_size;
4726 if (rel_size)
4728 Elf_Internal_Shdr *strsec;
4729 int is_rela;
4731 printf (_("\nRelocation section "));
4733 if (string_table == NULL)
4734 printf ("%d", section->sh_name);
4735 else
4736 printf (_("'%s'"), SECTION_NAME (section));
4738 printf (_(" at offset 0x%lx contains %lu entries:\n"),
4739 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
4741 is_rela = section->sh_type == SHT_RELA;
4743 if (section->sh_link
4744 && SECTION_HEADER_INDEX (section->sh_link)
4745 < elf_header.e_shnum)
4747 Elf_Internal_Shdr *symsec;
4748 Elf_Internal_Sym *symtab;
4749 unsigned long nsyms;
4750 unsigned long strtablen = 0;
4751 char *strtab = NULL;
4753 symsec = SECTION_HEADER (section->sh_link);
4754 if (symsec->sh_type != SHT_SYMTAB
4755 && symsec->sh_type != SHT_DYNSYM)
4756 continue;
4758 nsyms = symsec->sh_size / symsec->sh_entsize;
4759 symtab = GET_ELF_SYMBOLS (file, symsec);
4761 if (symtab == NULL)
4762 continue;
4764 if (SECTION_HEADER_INDEX (symsec->sh_link)
4765 < elf_header.e_shnum)
4767 strsec = SECTION_HEADER (symsec->sh_link);
4769 strtab = get_data (NULL, file, strsec->sh_offset,
4770 1, strsec->sh_size,
4771 _("string table"));
4772 strtablen = strtab == NULL ? 0 : strsec->sh_size;
4775 dump_relocations (file, rel_offset, rel_size,
4776 symtab, nsyms, strtab, strtablen, is_rela);
4777 if (strtab)
4778 free (strtab);
4779 free (symtab);
4781 else
4782 dump_relocations (file, rel_offset, rel_size,
4783 NULL, 0, NULL, 0, is_rela);
4785 found = 1;
4789 if (! found)
4790 printf (_("\nThere are no relocations in this file.\n"));
4793 return 1;
4796 /* Process the unwind section. */
4798 #include "unwind-ia64.h"
4800 /* An absolute address consists of a section and an offset. If the
4801 section is NULL, the offset itself is the address, otherwise, the
4802 address equals to LOAD_ADDRESS(section) + offset. */
4804 struct absaddr
4806 unsigned short section;
4807 bfd_vma offset;
4810 #define ABSADDR(a) \
4811 ((a).section \
4812 ? section_headers [(a).section].sh_addr + (a).offset \
4813 : (a).offset)
4815 struct ia64_unw_aux_info
4817 struct ia64_unw_table_entry
4819 struct absaddr start;
4820 struct absaddr end;
4821 struct absaddr info;
4823 *table; /* Unwind table. */
4824 unsigned long table_len; /* Length of unwind table. */
4825 unsigned char *info; /* Unwind info. */
4826 unsigned long info_size; /* Size of unwind info. */
4827 bfd_vma info_addr; /* starting address of unwind info. */
4828 bfd_vma seg_base; /* Starting address of segment. */
4829 Elf_Internal_Sym *symtab; /* The symbol table. */
4830 unsigned long nsyms; /* Number of symbols. */
4831 char *strtab; /* The string table. */
4832 unsigned long strtab_size; /* Size of string table. */
4835 static void
4836 find_symbol_for_address (Elf_Internal_Sym *symtab,
4837 unsigned long nsyms,
4838 const char *strtab,
4839 unsigned long strtab_size,
4840 struct absaddr addr,
4841 const char **symname,
4842 bfd_vma *offset)
4844 bfd_vma dist = 0x100000;
4845 Elf_Internal_Sym *sym, *best = NULL;
4846 unsigned long i;
4848 for (i = 0, sym = symtab; i < nsyms; ++i, ++sym)
4850 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
4851 && sym->st_name != 0
4852 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
4853 && addr.offset >= sym->st_value
4854 && addr.offset - sym->st_value < dist)
4856 best = sym;
4857 dist = addr.offset - sym->st_value;
4858 if (!dist)
4859 break;
4862 if (best)
4864 *symname = (best->st_name >= strtab_size
4865 ? "<corrupt>" : strtab + best->st_name);
4866 *offset = dist;
4867 return;
4869 *symname = NULL;
4870 *offset = addr.offset;
4873 static void
4874 dump_ia64_unwind (struct ia64_unw_aux_info *aux)
4876 struct ia64_unw_table_entry *tp;
4877 int in_body;
4879 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
4881 bfd_vma stamp;
4882 bfd_vma offset;
4883 const unsigned char *dp;
4884 const unsigned char *head;
4885 const char *procname;
4887 find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
4888 aux->strtab_size, tp->start, &procname, &offset);
4890 fputs ("\n<", stdout);
4892 if (procname)
4894 fputs (procname, stdout);
4896 if (offset)
4897 printf ("+%lx", (unsigned long) offset);
4900 fputs (">: [", stdout);
4901 print_vma (tp->start.offset, PREFIX_HEX);
4902 fputc ('-', stdout);
4903 print_vma (tp->end.offset, PREFIX_HEX);
4904 printf ("], info at +0x%lx\n",
4905 (unsigned long) (tp->info.offset - aux->seg_base));
4907 head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
4908 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
4910 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
4911 (unsigned) UNW_VER (stamp),
4912 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
4913 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
4914 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
4915 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
4917 if (UNW_VER (stamp) != 1)
4919 printf ("\tUnknown version.\n");
4920 continue;
4923 in_body = 0;
4924 for (dp = head + 8; dp < head + 8 + eh_addr_size * UNW_LENGTH (stamp);)
4925 dp = unw_decode (dp, in_body, & in_body);
4929 static int
4930 slurp_ia64_unwind_table (FILE *file,
4931 struct ia64_unw_aux_info *aux,
4932 Elf_Internal_Shdr *sec)
4934 unsigned long size, nrelas, i;
4935 Elf_Internal_Phdr *seg;
4936 struct ia64_unw_table_entry *tep;
4937 Elf_Internal_Shdr *relsec;
4938 Elf_Internal_Rela *rela, *rp;
4939 unsigned char *table, *tp;
4940 Elf_Internal_Sym *sym;
4941 const char *relname;
4943 /* First, find the starting address of the segment that includes
4944 this section: */
4946 if (elf_header.e_phnum)
4948 if (! get_program_headers (file))
4949 return 0;
4951 for (seg = program_headers;
4952 seg < program_headers + elf_header.e_phnum;
4953 ++seg)
4955 if (seg->p_type != PT_LOAD)
4956 continue;
4958 if (sec->sh_addr >= seg->p_vaddr
4959 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
4961 aux->seg_base = seg->p_vaddr;
4962 break;
4967 /* Second, build the unwind table from the contents of the unwind section: */
4968 size = sec->sh_size;
4969 table = get_data (NULL, file, sec->sh_offset, 1, size, _("unwind table"));
4970 if (!table)
4971 return 0;
4973 aux->table = xcmalloc (size / (3 * eh_addr_size), sizeof (aux->table[0]));
4974 tep = aux->table;
4975 for (tp = table; tp < table + size; tp += 3 * eh_addr_size, ++tep)
4977 tep->start.section = SHN_UNDEF;
4978 tep->end.section = SHN_UNDEF;
4979 tep->info.section = SHN_UNDEF;
4980 if (is_32bit_elf)
4982 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
4983 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
4984 tep->info.offset = byte_get ((unsigned char *) tp + 8, 4);
4986 else
4988 tep->start.offset = BYTE_GET ((unsigned char *) tp + 0);
4989 tep->end.offset = BYTE_GET ((unsigned char *) tp + 8);
4990 tep->info.offset = BYTE_GET ((unsigned char *) tp + 16);
4992 tep->start.offset += aux->seg_base;
4993 tep->end.offset += aux->seg_base;
4994 tep->info.offset += aux->seg_base;
4996 free (table);
4998 /* Third, apply any relocations to the unwind table: */
5000 for (relsec = section_headers;
5001 relsec < section_headers + elf_header.e_shnum;
5002 ++relsec)
5004 if (relsec->sh_type != SHT_RELA
5005 || SECTION_HEADER_INDEX (relsec->sh_info) >= elf_header.e_shnum
5006 || SECTION_HEADER (relsec->sh_info) != sec)
5007 continue;
5009 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5010 & rela, & nrelas))
5011 return 0;
5013 for (rp = rela; rp < rela + nrelas; ++rp)
5015 if (is_32bit_elf)
5017 relname = elf_ia64_reloc_type (ELF32_R_TYPE (rp->r_info));
5018 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
5020 else
5022 relname = elf_ia64_reloc_type (ELF64_R_TYPE (rp->r_info));
5023 sym = aux->symtab + ELF64_R_SYM (rp->r_info);
5026 if (! const_strneq (relname, "R_IA64_SEGREL"))
5028 warn (_("Skipping unexpected relocation type %s\n"), relname);
5029 continue;
5032 i = rp->r_offset / (3 * eh_addr_size);
5034 switch (rp->r_offset/eh_addr_size % 3)
5036 case 0:
5037 aux->table[i].start.section = sym->st_shndx;
5038 aux->table[i].start.offset += rp->r_addend + sym->st_value;
5039 break;
5040 case 1:
5041 aux->table[i].end.section = sym->st_shndx;
5042 aux->table[i].end.offset += rp->r_addend + sym->st_value;
5043 break;
5044 case 2:
5045 aux->table[i].info.section = sym->st_shndx;
5046 aux->table[i].info.offset += rp->r_addend + sym->st_value;
5047 break;
5048 default:
5049 break;
5053 free (rela);
5056 aux->table_len = size / (3 * eh_addr_size);
5057 return 1;
5060 static int
5061 ia64_process_unwind (FILE *file)
5063 Elf_Internal_Shdr *sec, *unwsec = NULL, *strsec;
5064 unsigned long i, unwcount = 0, unwstart = 0;
5065 struct ia64_unw_aux_info aux;
5067 memset (& aux, 0, sizeof (aux));
5069 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5071 if (sec->sh_type == SHT_SYMTAB
5072 && SECTION_HEADER_INDEX (sec->sh_link) < elf_header.e_shnum)
5074 aux.nsyms = sec->sh_size / sec->sh_entsize;
5075 aux.symtab = GET_ELF_SYMBOLS (file, sec);
5077 strsec = SECTION_HEADER (sec->sh_link);
5078 aux.strtab = get_data (NULL, file, strsec->sh_offset,
5079 1, strsec->sh_size, _("string table"));
5080 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5082 else if (sec->sh_type == SHT_IA_64_UNWIND)
5083 unwcount++;
5086 if (!unwcount)
5087 printf (_("\nThere are no unwind sections in this file.\n"));
5089 while (unwcount-- > 0)
5091 char *suffix;
5092 size_t len, len2;
5094 for (i = unwstart, sec = section_headers + unwstart;
5095 i < elf_header.e_shnum; ++i, ++sec)
5096 if (sec->sh_type == SHT_IA_64_UNWIND)
5098 unwsec = sec;
5099 break;
5102 unwstart = i + 1;
5103 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
5105 if ((unwsec->sh_flags & SHF_GROUP) != 0)
5107 /* We need to find which section group it is in. */
5108 struct group_list *g = section_headers_groups [i]->root;
5110 for (; g != NULL; g = g->next)
5112 sec = SECTION_HEADER (g->section_index);
5114 if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
5115 break;
5118 if (g == NULL)
5119 i = elf_header.e_shnum;
5121 else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
5123 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
5124 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
5125 suffix = SECTION_NAME (unwsec) + len;
5126 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5127 ++i, ++sec)
5128 if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
5129 && streq (SECTION_NAME (sec) + len2, suffix))
5130 break;
5132 else
5134 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
5135 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
5136 len = sizeof (ELF_STRING_ia64_unwind) - 1;
5137 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
5138 suffix = "";
5139 if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
5140 suffix = SECTION_NAME (unwsec) + len;
5141 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5142 ++i, ++sec)
5143 if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
5144 && streq (SECTION_NAME (sec) + len2, suffix))
5145 break;
5148 if (i == elf_header.e_shnum)
5150 printf (_("\nCould not find unwind info section for "));
5152 if (string_table == NULL)
5153 printf ("%d", unwsec->sh_name);
5154 else
5155 printf (_("'%s'"), SECTION_NAME (unwsec));
5157 else
5159 aux.info_size = sec->sh_size;
5160 aux.info_addr = sec->sh_addr;
5161 aux.info = get_data (NULL, file, sec->sh_offset, 1, aux.info_size,
5162 _("unwind info"));
5164 printf (_("\nUnwind section "));
5166 if (string_table == NULL)
5167 printf ("%d", unwsec->sh_name);
5168 else
5169 printf (_("'%s'"), SECTION_NAME (unwsec));
5171 printf (_(" at offset 0x%lx contains %lu entries:\n"),
5172 (unsigned long) unwsec->sh_offset,
5173 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
5175 (void) slurp_ia64_unwind_table (file, & aux, unwsec);
5177 if (aux.table_len > 0)
5178 dump_ia64_unwind (& aux);
5180 if (aux.table)
5181 free ((char *) aux.table);
5182 if (aux.info)
5183 free ((char *) aux.info);
5184 aux.table = NULL;
5185 aux.info = NULL;
5189 if (aux.symtab)
5190 free (aux.symtab);
5191 if (aux.strtab)
5192 free ((char *) aux.strtab);
5194 return 1;
5197 struct hppa_unw_aux_info
5199 struct hppa_unw_table_entry
5201 struct absaddr start;
5202 struct absaddr end;
5203 unsigned int Cannot_unwind:1; /* 0 */
5204 unsigned int Millicode:1; /* 1 */
5205 unsigned int Millicode_save_sr0:1; /* 2 */
5206 unsigned int Region_description:2; /* 3..4 */
5207 unsigned int reserved1:1; /* 5 */
5208 unsigned int Entry_SR:1; /* 6 */
5209 unsigned int Entry_FR:4; /* number saved */ /* 7..10 */
5210 unsigned int Entry_GR:5; /* number saved */ /* 11..15 */
5211 unsigned int Args_stored:1; /* 16 */
5212 unsigned int Variable_Frame:1; /* 17 */
5213 unsigned int Separate_Package_Body:1; /* 18 */
5214 unsigned int Frame_Extension_Millicode:1; /* 19 */
5215 unsigned int Stack_Overflow_Check:1; /* 20 */
5216 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
5217 unsigned int Ada_Region:1; /* 22 */
5218 unsigned int cxx_info:1; /* 23 */
5219 unsigned int cxx_try_catch:1; /* 24 */
5220 unsigned int sched_entry_seq:1; /* 25 */
5221 unsigned int reserved2:1; /* 26 */
5222 unsigned int Save_SP:1; /* 27 */
5223 unsigned int Save_RP:1; /* 28 */
5224 unsigned int Save_MRP_in_frame:1; /* 29 */
5225 unsigned int extn_ptr_defined:1; /* 30 */
5226 unsigned int Cleanup_defined:1; /* 31 */
5228 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
5229 unsigned int HP_UX_interrupt_marker:1; /* 1 */
5230 unsigned int Large_frame:1; /* 2 */
5231 unsigned int Pseudo_SP_Set:1; /* 3 */
5232 unsigned int reserved4:1; /* 4 */
5233 unsigned int Total_frame_size:27; /* 5..31 */
5235 *table; /* Unwind table. */
5236 unsigned long table_len; /* Length of unwind table. */
5237 bfd_vma seg_base; /* Starting address of segment. */
5238 Elf_Internal_Sym *symtab; /* The symbol table. */
5239 unsigned long nsyms; /* Number of symbols. */
5240 char *strtab; /* The string table. */
5241 unsigned long strtab_size; /* Size of string table. */
5244 static void
5245 dump_hppa_unwind (struct hppa_unw_aux_info *aux)
5247 struct hppa_unw_table_entry *tp;
5249 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
5251 bfd_vma offset;
5252 const char *procname;
5254 find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
5255 aux->strtab_size, tp->start, &procname,
5256 &offset);
5258 fputs ("\n<", stdout);
5260 if (procname)
5262 fputs (procname, stdout);
5264 if (offset)
5265 printf ("+%lx", (unsigned long) offset);
5268 fputs (">: [", stdout);
5269 print_vma (tp->start.offset, PREFIX_HEX);
5270 fputc ('-', stdout);
5271 print_vma (tp->end.offset, PREFIX_HEX);
5272 printf ("]\n\t");
5274 #define PF(_m) if (tp->_m) printf (#_m " ");
5275 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
5276 PF(Cannot_unwind);
5277 PF(Millicode);
5278 PF(Millicode_save_sr0);
5279 /* PV(Region_description); */
5280 PF(Entry_SR);
5281 PV(Entry_FR);
5282 PV(Entry_GR);
5283 PF(Args_stored);
5284 PF(Variable_Frame);
5285 PF(Separate_Package_Body);
5286 PF(Frame_Extension_Millicode);
5287 PF(Stack_Overflow_Check);
5288 PF(Two_Instruction_SP_Increment);
5289 PF(Ada_Region);
5290 PF(cxx_info);
5291 PF(cxx_try_catch);
5292 PF(sched_entry_seq);
5293 PF(Save_SP);
5294 PF(Save_RP);
5295 PF(Save_MRP_in_frame);
5296 PF(extn_ptr_defined);
5297 PF(Cleanup_defined);
5298 PF(MPE_XL_interrupt_marker);
5299 PF(HP_UX_interrupt_marker);
5300 PF(Large_frame);
5301 PF(Pseudo_SP_Set);
5302 PV(Total_frame_size);
5303 #undef PF
5304 #undef PV
5307 printf ("\n");
5310 static int
5311 slurp_hppa_unwind_table (FILE *file,
5312 struct hppa_unw_aux_info *aux,
5313 Elf_Internal_Shdr *sec)
5315 unsigned long size, unw_ent_size, nentries, nrelas, i;
5316 Elf_Internal_Phdr *seg;
5317 struct hppa_unw_table_entry *tep;
5318 Elf_Internal_Shdr *relsec;
5319 Elf_Internal_Rela *rela, *rp;
5320 unsigned char *table, *tp;
5321 Elf_Internal_Sym *sym;
5322 const char *relname;
5324 /* First, find the starting address of the segment that includes
5325 this section. */
5327 if (elf_header.e_phnum)
5329 if (! get_program_headers (file))
5330 return 0;
5332 for (seg = program_headers;
5333 seg < program_headers + elf_header.e_phnum;
5334 ++seg)
5336 if (seg->p_type != PT_LOAD)
5337 continue;
5339 if (sec->sh_addr >= seg->p_vaddr
5340 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
5342 aux->seg_base = seg->p_vaddr;
5343 break;
5348 /* Second, build the unwind table from the contents of the unwind
5349 section. */
5350 size = sec->sh_size;
5351 table = get_data (NULL, file, sec->sh_offset, 1, size, _("unwind table"));
5352 if (!table)
5353 return 0;
5355 unw_ent_size = 16;
5356 nentries = size / unw_ent_size;
5357 size = unw_ent_size * nentries;
5359 tep = aux->table = xcmalloc (nentries, sizeof (aux->table[0]));
5361 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
5363 unsigned int tmp1, tmp2;
5365 tep->start.section = SHN_UNDEF;
5366 tep->end.section = SHN_UNDEF;
5368 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
5369 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
5370 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
5371 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
5373 tep->start.offset += aux->seg_base;
5374 tep->end.offset += aux->seg_base;
5376 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
5377 tep->Millicode = (tmp1 >> 30) & 0x1;
5378 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
5379 tep->Region_description = (tmp1 >> 27) & 0x3;
5380 tep->reserved1 = (tmp1 >> 26) & 0x1;
5381 tep->Entry_SR = (tmp1 >> 25) & 0x1;
5382 tep->Entry_FR = (tmp1 >> 21) & 0xf;
5383 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
5384 tep->Args_stored = (tmp1 >> 15) & 0x1;
5385 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
5386 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
5387 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
5388 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
5389 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
5390 tep->Ada_Region = (tmp1 >> 9) & 0x1;
5391 tep->cxx_info = (tmp1 >> 8) & 0x1;
5392 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
5393 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
5394 tep->reserved2 = (tmp1 >> 5) & 0x1;
5395 tep->Save_SP = (tmp1 >> 4) & 0x1;
5396 tep->Save_RP = (tmp1 >> 3) & 0x1;
5397 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
5398 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
5399 tep->Cleanup_defined = tmp1 & 0x1;
5401 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
5402 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
5403 tep->Large_frame = (tmp2 >> 29) & 0x1;
5404 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
5405 tep->reserved4 = (tmp2 >> 27) & 0x1;
5406 tep->Total_frame_size = tmp2 & 0x7ffffff;
5408 free (table);
5410 /* Third, apply any relocations to the unwind table. */
5412 for (relsec = section_headers;
5413 relsec < section_headers + elf_header.e_shnum;
5414 ++relsec)
5416 if (relsec->sh_type != SHT_RELA
5417 || SECTION_HEADER_INDEX (relsec->sh_info) >= elf_header.e_shnum
5418 || SECTION_HEADER (relsec->sh_info) != sec)
5419 continue;
5421 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5422 & rela, & nrelas))
5423 return 0;
5425 for (rp = rela; rp < rela + nrelas; ++rp)
5427 if (is_32bit_elf)
5429 relname = elf_hppa_reloc_type (ELF32_R_TYPE (rp->r_info));
5430 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
5432 else
5434 relname = elf_hppa_reloc_type (ELF64_R_TYPE (rp->r_info));
5435 sym = aux->symtab + ELF64_R_SYM (rp->r_info);
5438 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
5439 if (! const_strneq (relname, "R_PARISC_SEGREL"))
5441 warn (_("Skipping unexpected relocation type %s\n"), relname);
5442 continue;
5445 i = rp->r_offset / unw_ent_size;
5447 switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
5449 case 0:
5450 aux->table[i].start.section = sym->st_shndx;
5451 aux->table[i].start.offset += sym->st_value + rp->r_addend;
5452 break;
5453 case 1:
5454 aux->table[i].end.section = sym->st_shndx;
5455 aux->table[i].end.offset += sym->st_value + rp->r_addend;
5456 break;
5457 default:
5458 break;
5462 free (rela);
5465 aux->table_len = nentries;
5467 return 1;
5470 static int
5471 hppa_process_unwind (FILE *file)
5473 struct hppa_unw_aux_info aux;
5474 Elf_Internal_Shdr *unwsec = NULL;
5475 Elf_Internal_Shdr *strsec;
5476 Elf_Internal_Shdr *sec;
5477 unsigned long i;
5479 memset (& aux, 0, sizeof (aux));
5481 if (string_table == NULL)
5482 return 1;
5484 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5486 if (sec->sh_type == SHT_SYMTAB
5487 && SECTION_HEADER_INDEX (sec->sh_link) < elf_header.e_shnum)
5489 aux.nsyms = sec->sh_size / sec->sh_entsize;
5490 aux.symtab = GET_ELF_SYMBOLS (file, sec);
5492 strsec = SECTION_HEADER (sec->sh_link);
5493 aux.strtab = get_data (NULL, file, strsec->sh_offset,
5494 1, strsec->sh_size, _("string table"));
5495 aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5497 else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
5498 unwsec = sec;
5501 if (!unwsec)
5502 printf (_("\nThere are no unwind sections in this file.\n"));
5504 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5506 if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
5508 printf (_("\nUnwind section "));
5509 printf (_("'%s'"), SECTION_NAME (sec));
5511 printf (_(" at offset 0x%lx contains %lu entries:\n"),
5512 (unsigned long) sec->sh_offset,
5513 (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
5515 slurp_hppa_unwind_table (file, &aux, sec);
5516 if (aux.table_len > 0)
5517 dump_hppa_unwind (&aux);
5519 if (aux.table)
5520 free ((char *) aux.table);
5521 aux.table = NULL;
5525 if (aux.symtab)
5526 free (aux.symtab);
5527 if (aux.strtab)
5528 free ((char *) aux.strtab);
5530 return 1;
5533 static int
5534 process_unwind (FILE *file)
5536 struct unwind_handler {
5537 int machtype;
5538 int (*handler)(FILE *file);
5539 } handlers[] = {
5540 { EM_IA_64, ia64_process_unwind },
5541 { EM_PARISC, hppa_process_unwind },
5542 { 0, 0 }
5544 int i;
5546 if (!do_unwind)
5547 return 1;
5549 for (i = 0; handlers[i].handler != NULL; i++)
5550 if (elf_header.e_machine == handlers[i].machtype)
5551 return handlers[i].handler (file);
5553 printf (_("\nThere are no unwind sections in this file.\n"));
5554 return 1;
5557 static void
5558 dynamic_section_mips_val (Elf_Internal_Dyn *entry)
5560 switch (entry->d_tag)
5562 case DT_MIPS_FLAGS:
5563 if (entry->d_un.d_val == 0)
5564 printf ("NONE\n");
5565 else
5567 static const char * opts[] =
5569 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
5570 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
5571 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
5572 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
5573 "RLD_ORDER_SAFE"
5575 unsigned int cnt;
5576 int first = 1;
5577 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
5578 if (entry->d_un.d_val & (1 << cnt))
5580 printf ("%s%s", first ? "" : " ", opts[cnt]);
5581 first = 0;
5583 puts ("");
5585 break;
5587 case DT_MIPS_IVERSION:
5588 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
5589 printf ("Interface Version: %s\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
5590 else
5591 printf ("<corrupt: %ld>\n", (long) entry->d_un.d_ptr);
5592 break;
5594 case DT_MIPS_TIME_STAMP:
5596 char timebuf[20];
5597 struct tm *tmp;
5599 time_t time = entry->d_un.d_val;
5600 tmp = gmtime (&time);
5601 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
5602 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
5603 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
5604 printf ("Time Stamp: %s\n", timebuf);
5606 break;
5608 case DT_MIPS_RLD_VERSION:
5609 case DT_MIPS_LOCAL_GOTNO:
5610 case DT_MIPS_CONFLICTNO:
5611 case DT_MIPS_LIBLISTNO:
5612 case DT_MIPS_SYMTABNO:
5613 case DT_MIPS_UNREFEXTNO:
5614 case DT_MIPS_HIPAGENO:
5615 case DT_MIPS_DELTA_CLASS_NO:
5616 case DT_MIPS_DELTA_INSTANCE_NO:
5617 case DT_MIPS_DELTA_RELOC_NO:
5618 case DT_MIPS_DELTA_SYM_NO:
5619 case DT_MIPS_DELTA_CLASSSYM_NO:
5620 case DT_MIPS_COMPACT_SIZE:
5621 printf ("%ld\n", (long) entry->d_un.d_ptr);
5622 break;
5624 default:
5625 printf ("%#lx\n", (long) entry->d_un.d_ptr);
5630 static void
5631 dynamic_section_parisc_val (Elf_Internal_Dyn *entry)
5633 switch (entry->d_tag)
5635 case DT_HP_DLD_FLAGS:
5637 static struct
5639 long int bit;
5640 const char *str;
5642 flags[] =
5644 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
5645 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
5646 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
5647 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
5648 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
5649 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
5650 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
5651 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
5652 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
5653 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
5654 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
5655 { DT_HP_GST, "HP_GST" },
5656 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
5657 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
5658 { DT_HP_NODELETE, "HP_NODELETE" },
5659 { DT_HP_GROUP, "HP_GROUP" },
5660 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
5662 int first = 1;
5663 size_t cnt;
5664 bfd_vma val = entry->d_un.d_val;
5666 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
5667 if (val & flags[cnt].bit)
5669 if (! first)
5670 putchar (' ');
5671 fputs (flags[cnt].str, stdout);
5672 first = 0;
5673 val ^= flags[cnt].bit;
5676 if (val != 0 || first)
5678 if (! first)
5679 putchar (' ');
5680 print_vma (val, HEX);
5683 break;
5685 default:
5686 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5687 break;
5689 putchar ('\n');
5692 static void
5693 dynamic_section_ia64_val (Elf_Internal_Dyn *entry)
5695 switch (entry->d_tag)
5697 case DT_IA_64_PLT_RESERVE:
5698 /* First 3 slots reserved. */
5699 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5700 printf (" -- ");
5701 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
5702 break;
5704 default:
5705 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
5706 break;
5708 putchar ('\n');
5711 static int
5712 get_32bit_dynamic_section (FILE *file)
5714 Elf32_External_Dyn *edyn, *ext;
5715 Elf_Internal_Dyn *entry;
5717 edyn = get_data (NULL, file, dynamic_addr, 1, dynamic_size,
5718 _("dynamic section"));
5719 if (!edyn)
5720 return 0;
5722 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
5723 might not have the luxury of section headers. Look for the DT_NULL
5724 terminator to determine the number of entries. */
5725 for (ext = edyn, dynamic_nent = 0;
5726 (char *) ext < (char *) edyn + dynamic_size;
5727 ext++)
5729 dynamic_nent++;
5730 if (BYTE_GET (ext->d_tag) == DT_NULL)
5731 break;
5734 dynamic_section = cmalloc (dynamic_nent, sizeof (*entry));
5735 if (dynamic_section == NULL)
5737 error (_("Out of memory\n"));
5738 free (edyn);
5739 return 0;
5742 for (ext = edyn, entry = dynamic_section;
5743 entry < dynamic_section + dynamic_nent;
5744 ext++, entry++)
5746 entry->d_tag = BYTE_GET (ext->d_tag);
5747 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
5750 free (edyn);
5752 return 1;
5755 static int
5756 get_64bit_dynamic_section (FILE *file)
5758 Elf64_External_Dyn *edyn, *ext;
5759 Elf_Internal_Dyn *entry;
5761 edyn = get_data (NULL, file, dynamic_addr, 1, dynamic_size,
5762 _("dynamic section"));
5763 if (!edyn)
5764 return 0;
5766 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
5767 might not have the luxury of section headers. Look for the DT_NULL
5768 terminator to determine the number of entries. */
5769 for (ext = edyn, dynamic_nent = 0;
5770 (char *) ext < (char *) edyn + dynamic_size;
5771 ext++)
5773 dynamic_nent++;
5774 if (BYTE_GET (ext->d_tag) == DT_NULL)
5775 break;
5778 dynamic_section = cmalloc (dynamic_nent, sizeof (*entry));
5779 if (dynamic_section == NULL)
5781 error (_("Out of memory\n"));
5782 free (edyn);
5783 return 0;
5786 for (ext = edyn, entry = dynamic_section;
5787 entry < dynamic_section + dynamic_nent;
5788 ext++, entry++)
5790 entry->d_tag = BYTE_GET (ext->d_tag);
5791 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
5794 free (edyn);
5796 return 1;
5799 static void
5800 print_dynamic_flags (bfd_vma flags)
5802 int first = 1;
5804 while (flags)
5806 bfd_vma flag;
5808 flag = flags & - flags;
5809 flags &= ~ flag;
5811 if (first)
5812 first = 0;
5813 else
5814 putc (' ', stdout);
5816 switch (flag)
5818 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
5819 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
5820 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
5821 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
5822 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
5823 default: fputs ("unknown", stdout); break;
5826 puts ("");
5829 /* Parse and display the contents of the dynamic section. */
5831 static int
5832 process_dynamic_section (FILE *file)
5834 Elf_Internal_Dyn *entry;
5836 if (dynamic_size == 0)
5838 if (do_dynamic)
5839 printf (_("\nThere is no dynamic section in this file.\n"));
5841 return 1;
5844 if (is_32bit_elf)
5846 if (! get_32bit_dynamic_section (file))
5847 return 0;
5849 else if (! get_64bit_dynamic_section (file))
5850 return 0;
5852 /* Find the appropriate symbol table. */
5853 if (dynamic_symbols == NULL)
5855 for (entry = dynamic_section;
5856 entry < dynamic_section + dynamic_nent;
5857 ++entry)
5859 Elf_Internal_Shdr section;
5861 if (entry->d_tag != DT_SYMTAB)
5862 continue;
5864 dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
5866 /* Since we do not know how big the symbol table is,
5867 we default to reading in the entire file (!) and
5868 processing that. This is overkill, I know, but it
5869 should work. */
5870 section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
5872 if (archive_file_offset != 0)
5873 section.sh_size = archive_file_size - section.sh_offset;
5874 else
5876 if (fseek (file, 0, SEEK_END))
5877 error (_("Unable to seek to end of file!\n"));
5879 section.sh_size = ftell (file) - section.sh_offset;
5882 if (is_32bit_elf)
5883 section.sh_entsize = sizeof (Elf32_External_Sym);
5884 else
5885 section.sh_entsize = sizeof (Elf64_External_Sym);
5887 num_dynamic_syms = section.sh_size / section.sh_entsize;
5888 if (num_dynamic_syms < 1)
5890 error (_("Unable to determine the number of symbols to load\n"));
5891 continue;
5894 dynamic_symbols = GET_ELF_SYMBOLS (file, &section);
5898 /* Similarly find a string table. */
5899 if (dynamic_strings == NULL)
5901 for (entry = dynamic_section;
5902 entry < dynamic_section + dynamic_nent;
5903 ++entry)
5905 unsigned long offset;
5906 long str_tab_len;
5908 if (entry->d_tag != DT_STRTAB)
5909 continue;
5911 dynamic_info[DT_STRTAB] = entry->d_un.d_val;
5913 /* Since we do not know how big the string table is,
5914 we default to reading in the entire file (!) and
5915 processing that. This is overkill, I know, but it
5916 should work. */
5918 offset = offset_from_vma (file, entry->d_un.d_val, 0);
5920 if (archive_file_offset != 0)
5921 str_tab_len = archive_file_size - offset;
5922 else
5924 if (fseek (file, 0, SEEK_END))
5925 error (_("Unable to seek to end of file\n"));
5926 str_tab_len = ftell (file) - offset;
5929 if (str_tab_len < 1)
5931 error
5932 (_("Unable to determine the length of the dynamic string table\n"));
5933 continue;
5936 dynamic_strings = get_data (NULL, file, offset, 1, str_tab_len,
5937 _("dynamic string table"));
5938 dynamic_strings_length = str_tab_len;
5939 break;
5943 /* And find the syminfo section if available. */
5944 if (dynamic_syminfo == NULL)
5946 unsigned long syminsz = 0;
5948 for (entry = dynamic_section;
5949 entry < dynamic_section + dynamic_nent;
5950 ++entry)
5952 if (entry->d_tag == DT_SYMINENT)
5954 /* Note: these braces are necessary to avoid a syntax
5955 error from the SunOS4 C compiler. */
5956 assert (sizeof (Elf_External_Syminfo) == entry->d_un.d_val);
5958 else if (entry->d_tag == DT_SYMINSZ)
5959 syminsz = entry->d_un.d_val;
5960 else if (entry->d_tag == DT_SYMINFO)
5961 dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
5962 syminsz);
5965 if (dynamic_syminfo_offset != 0 && syminsz != 0)
5967 Elf_External_Syminfo *extsyminfo, *extsym;
5968 Elf_Internal_Syminfo *syminfo;
5970 /* There is a syminfo section. Read the data. */
5971 extsyminfo = get_data (NULL, file, dynamic_syminfo_offset, 1,
5972 syminsz, _("symbol information"));
5973 if (!extsyminfo)
5974 return 0;
5976 dynamic_syminfo = malloc (syminsz);
5977 if (dynamic_syminfo == NULL)
5979 error (_("Out of memory\n"));
5980 return 0;
5983 dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
5984 for (syminfo = dynamic_syminfo, extsym = extsyminfo;
5985 syminfo < dynamic_syminfo + dynamic_syminfo_nent;
5986 ++syminfo, ++extsym)
5988 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
5989 syminfo->si_flags = BYTE_GET (extsym->si_flags);
5992 free (extsyminfo);
5996 if (do_dynamic && dynamic_addr)
5997 printf (_("\nDynamic section at offset 0x%lx contains %u entries:\n"),
5998 dynamic_addr, dynamic_nent);
5999 if (do_dynamic)
6000 printf (_(" Tag Type Name/Value\n"));
6002 for (entry = dynamic_section;
6003 entry < dynamic_section + dynamic_nent;
6004 entry++)
6006 if (do_dynamic)
6008 const char *dtype;
6010 putchar (' ');
6011 print_vma (entry->d_tag, FULL_HEX);
6012 dtype = get_dynamic_type (entry->d_tag);
6013 printf (" (%s)%*s", dtype,
6014 ((is_32bit_elf ? 27 : 19)
6015 - (int) strlen (dtype)),
6016 " ");
6019 switch (entry->d_tag)
6021 case DT_FLAGS:
6022 if (do_dynamic)
6023 print_dynamic_flags (entry->d_un.d_val);
6024 break;
6026 case DT_AUXILIARY:
6027 case DT_FILTER:
6028 case DT_CONFIG:
6029 case DT_DEPAUDIT:
6030 case DT_AUDIT:
6031 if (do_dynamic)
6033 switch (entry->d_tag)
6035 case DT_AUXILIARY:
6036 printf (_("Auxiliary library"));
6037 break;
6039 case DT_FILTER:
6040 printf (_("Filter library"));
6041 break;
6043 case DT_CONFIG:
6044 printf (_("Configuration file"));
6045 break;
6047 case DT_DEPAUDIT:
6048 printf (_("Dependency audit library"));
6049 break;
6051 case DT_AUDIT:
6052 printf (_("Audit library"));
6053 break;
6056 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
6057 printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
6058 else
6060 printf (": ");
6061 print_vma (entry->d_un.d_val, PREFIX_HEX);
6062 putchar ('\n');
6065 break;
6067 case DT_FEATURE:
6068 if (do_dynamic)
6070 printf (_("Flags:"));
6072 if (entry->d_un.d_val == 0)
6073 printf (_(" None\n"));
6074 else
6076 unsigned long int val = entry->d_un.d_val;
6078 if (val & DTF_1_PARINIT)
6080 printf (" PARINIT");
6081 val ^= DTF_1_PARINIT;
6083 if (val & DTF_1_CONFEXP)
6085 printf (" CONFEXP");
6086 val ^= DTF_1_CONFEXP;
6088 if (val != 0)
6089 printf (" %lx", val);
6090 puts ("");
6093 break;
6095 case DT_POSFLAG_1:
6096 if (do_dynamic)
6098 printf (_("Flags:"));
6100 if (entry->d_un.d_val == 0)
6101 printf (_(" None\n"));
6102 else
6104 unsigned long int val = entry->d_un.d_val;
6106 if (val & DF_P1_LAZYLOAD)
6108 printf (" LAZYLOAD");
6109 val ^= DF_P1_LAZYLOAD;
6111 if (val & DF_P1_GROUPPERM)
6113 printf (" GROUPPERM");
6114 val ^= DF_P1_GROUPPERM;
6116 if (val != 0)
6117 printf (" %lx", val);
6118 puts ("");
6121 break;
6123 case DT_FLAGS_1:
6124 if (do_dynamic)
6126 printf (_("Flags:"));
6127 if (entry->d_un.d_val == 0)
6128 printf (_(" None\n"));
6129 else
6131 unsigned long int val = entry->d_un.d_val;
6133 if (val & DF_1_NOW)
6135 printf (" NOW");
6136 val ^= DF_1_NOW;
6138 if (val & DF_1_GLOBAL)
6140 printf (" GLOBAL");
6141 val ^= DF_1_GLOBAL;
6143 if (val & DF_1_GROUP)
6145 printf (" GROUP");
6146 val ^= DF_1_GROUP;
6148 if (val & DF_1_NODELETE)
6150 printf (" NODELETE");
6151 val ^= DF_1_NODELETE;
6153 if (val & DF_1_LOADFLTR)
6155 printf (" LOADFLTR");
6156 val ^= DF_1_LOADFLTR;
6158 if (val & DF_1_INITFIRST)
6160 printf (" INITFIRST");
6161 val ^= DF_1_INITFIRST;
6163 if (val & DF_1_NOOPEN)
6165 printf (" NOOPEN");
6166 val ^= DF_1_NOOPEN;
6168 if (val & DF_1_ORIGIN)
6170 printf (" ORIGIN");
6171 val ^= DF_1_ORIGIN;
6173 if (val & DF_1_DIRECT)
6175 printf (" DIRECT");
6176 val ^= DF_1_DIRECT;
6178 if (val & DF_1_TRANS)
6180 printf (" TRANS");
6181 val ^= DF_1_TRANS;
6183 if (val & DF_1_INTERPOSE)
6185 printf (" INTERPOSE");
6186 val ^= DF_1_INTERPOSE;
6188 if (val & DF_1_NODEFLIB)
6190 printf (" NODEFLIB");
6191 val ^= DF_1_NODEFLIB;
6193 if (val & DF_1_NODUMP)
6195 printf (" NODUMP");
6196 val ^= DF_1_NODUMP;
6198 if (val & DF_1_CONLFAT)
6200 printf (" CONLFAT");
6201 val ^= DF_1_CONLFAT;
6203 if (val != 0)
6204 printf (" %lx", val);
6205 puts ("");
6208 break;
6210 case DT_PLTREL:
6211 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6212 if (do_dynamic)
6213 puts (get_dynamic_type (entry->d_un.d_val));
6214 break;
6216 case DT_NULL :
6217 case DT_NEEDED :
6218 case DT_PLTGOT :
6219 case DT_HASH :
6220 case DT_STRTAB :
6221 case DT_SYMTAB :
6222 case DT_RELA :
6223 case DT_INIT :
6224 case DT_FINI :
6225 case DT_SONAME :
6226 case DT_RPATH :
6227 case DT_SYMBOLIC:
6228 case DT_REL :
6229 case DT_DEBUG :
6230 case DT_TEXTREL :
6231 case DT_JMPREL :
6232 case DT_RUNPATH :
6233 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6235 if (do_dynamic)
6237 char *name;
6239 if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
6240 name = GET_DYNAMIC_NAME (entry->d_un.d_val);
6241 else
6242 name = NULL;
6244 if (name)
6246 switch (entry->d_tag)
6248 case DT_NEEDED:
6249 printf (_("Shared library: [%s]"), name);
6251 if (streq (name, program_interpreter))
6252 printf (_(" program interpreter"));
6253 break;
6255 case DT_SONAME:
6256 printf (_("Library soname: [%s]"), name);
6257 break;
6259 case DT_RPATH:
6260 printf (_("Library rpath: [%s]"), name);
6261 break;
6263 case DT_RUNPATH:
6264 printf (_("Library runpath: [%s]"), name);
6265 break;
6267 default:
6268 print_vma (entry->d_un.d_val, PREFIX_HEX);
6269 break;
6272 else
6273 print_vma (entry->d_un.d_val, PREFIX_HEX);
6275 putchar ('\n');
6277 break;
6279 case DT_PLTRELSZ:
6280 case DT_RELASZ :
6281 case DT_STRSZ :
6282 case DT_RELSZ :
6283 case DT_RELAENT :
6284 case DT_SYMENT :
6285 case DT_RELENT :
6286 dynamic_info[entry->d_tag] = entry->d_un.d_val;
6287 case DT_PLTPADSZ:
6288 case DT_MOVEENT :
6289 case DT_MOVESZ :
6290 case DT_INIT_ARRAYSZ:
6291 case DT_FINI_ARRAYSZ:
6292 case DT_GNU_CONFLICTSZ:
6293 case DT_GNU_LIBLISTSZ:
6294 if (do_dynamic)
6296 print_vma (entry->d_un.d_val, UNSIGNED);
6297 printf (" (bytes)\n");
6299 break;
6301 case DT_VERDEFNUM:
6302 case DT_VERNEEDNUM:
6303 case DT_RELACOUNT:
6304 case DT_RELCOUNT:
6305 if (do_dynamic)
6307 print_vma (entry->d_un.d_val, UNSIGNED);
6308 putchar ('\n');
6310 break;
6312 case DT_SYMINSZ:
6313 case DT_SYMINENT:
6314 case DT_SYMINFO:
6315 case DT_USED:
6316 case DT_INIT_ARRAY:
6317 case DT_FINI_ARRAY:
6318 if (do_dynamic)
6320 if (entry->d_tag == DT_USED
6321 && VALID_DYNAMIC_NAME (entry->d_un.d_val))
6323 char *name = GET_DYNAMIC_NAME (entry->d_un.d_val);
6325 if (*name)
6327 printf (_("Not needed object: [%s]\n"), name);
6328 break;
6332 print_vma (entry->d_un.d_val, PREFIX_HEX);
6333 putchar ('\n');
6335 break;
6337 case DT_BIND_NOW:
6338 /* The value of this entry is ignored. */
6339 if (do_dynamic)
6340 putchar ('\n');
6341 break;
6343 case DT_GNU_PRELINKED:
6344 if (do_dynamic)
6346 struct tm *tmp;
6347 time_t time = entry->d_un.d_val;
6349 tmp = gmtime (&time);
6350 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
6351 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
6352 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
6355 break;
6357 case DT_GNU_HASH:
6358 dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
6359 if (do_dynamic)
6361 print_vma (entry->d_un.d_val, PREFIX_HEX);
6362 putchar ('\n');
6364 break;
6366 default:
6367 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
6368 version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
6369 entry->d_un.d_val;
6371 if (do_dynamic)
6373 switch (elf_header.e_machine)
6375 case EM_MIPS:
6376 case EM_MIPS_RS3_LE:
6377 dynamic_section_mips_val (entry);
6378 break;
6379 case EM_PARISC:
6380 dynamic_section_parisc_val (entry);
6381 break;
6382 case EM_IA_64:
6383 dynamic_section_ia64_val (entry);
6384 break;
6385 default:
6386 print_vma (entry->d_un.d_val, PREFIX_HEX);
6387 putchar ('\n');
6390 break;
6394 return 1;
6397 static char *
6398 get_ver_flags (unsigned int flags)
6400 static char buff[32];
6402 buff[0] = 0;
6404 if (flags == 0)
6405 return _("none");
6407 if (flags & VER_FLG_BASE)
6408 strcat (buff, "BASE ");
6410 if (flags & VER_FLG_WEAK)
6412 if (flags & VER_FLG_BASE)
6413 strcat (buff, "| ");
6415 strcat (buff, "WEAK ");
6418 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK))
6419 strcat (buff, "| <unknown>");
6421 return buff;
6424 /* Display the contents of the version sections. */
6425 static int
6426 process_version_sections (FILE *file)
6428 Elf_Internal_Shdr *section;
6429 unsigned i;
6430 int found = 0;
6432 if (! do_version)
6433 return 1;
6435 for (i = 0, section = section_headers;
6436 i < elf_header.e_shnum;
6437 i++, section++)
6439 switch (section->sh_type)
6441 case SHT_GNU_verdef:
6443 Elf_External_Verdef *edefs;
6444 unsigned int idx;
6445 unsigned int cnt;
6447 found = 1;
6449 printf
6450 (_("\nVersion definition section '%s' contains %ld entries:\n"),
6451 SECTION_NAME (section), section->sh_info);
6453 printf (_(" Addr: 0x"));
6454 printf_vma (section->sh_addr);
6455 printf (_(" Offset: %#08lx Link: %lx (%s)\n"),
6456 (unsigned long) section->sh_offset, section->sh_link,
6457 SECTION_HEADER_INDEX (section->sh_link)
6458 < elf_header.e_shnum
6459 ? SECTION_NAME (SECTION_HEADER (section->sh_link))
6460 : "<corrupt>");
6462 edefs = get_data (NULL, file, section->sh_offset, 1,
6463 section->sh_size,
6464 _("version definition section"));
6465 if (!edefs)
6466 break;
6468 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
6470 char *vstart;
6471 Elf_External_Verdef *edef;
6472 Elf_Internal_Verdef ent;
6473 Elf_External_Verdaux *eaux;
6474 Elf_Internal_Verdaux aux;
6475 int j;
6476 int isum;
6478 vstart = ((char *) edefs) + idx;
6480 edef = (Elf_External_Verdef *) vstart;
6482 ent.vd_version = BYTE_GET (edef->vd_version);
6483 ent.vd_flags = BYTE_GET (edef->vd_flags);
6484 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
6485 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
6486 ent.vd_hash = BYTE_GET (edef->vd_hash);
6487 ent.vd_aux = BYTE_GET (edef->vd_aux);
6488 ent.vd_next = BYTE_GET (edef->vd_next);
6490 printf (_(" %#06x: Rev: %d Flags: %s"),
6491 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
6493 printf (_(" Index: %d Cnt: %d "),
6494 ent.vd_ndx, ent.vd_cnt);
6496 vstart += ent.vd_aux;
6498 eaux = (Elf_External_Verdaux *) vstart;
6500 aux.vda_name = BYTE_GET (eaux->vda_name);
6501 aux.vda_next = BYTE_GET (eaux->vda_next);
6503 if (VALID_DYNAMIC_NAME (aux.vda_name))
6504 printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
6505 else
6506 printf (_("Name index: %ld\n"), aux.vda_name);
6508 isum = idx + ent.vd_aux;
6510 for (j = 1; j < ent.vd_cnt; j++)
6512 isum += aux.vda_next;
6513 vstart += aux.vda_next;
6515 eaux = (Elf_External_Verdaux *) vstart;
6517 aux.vda_name = BYTE_GET (eaux->vda_name);
6518 aux.vda_next = BYTE_GET (eaux->vda_next);
6520 if (VALID_DYNAMIC_NAME (aux.vda_name))
6521 printf (_(" %#06x: Parent %d: %s\n"),
6522 isum, j, GET_DYNAMIC_NAME (aux.vda_name));
6523 else
6524 printf (_(" %#06x: Parent %d, name index: %ld\n"),
6525 isum, j, aux.vda_name);
6528 idx += ent.vd_next;
6531 free (edefs);
6533 break;
6535 case SHT_GNU_verneed:
6537 Elf_External_Verneed *eneed;
6538 unsigned int idx;
6539 unsigned int cnt;
6541 found = 1;
6543 printf (_("\nVersion needs section '%s' contains %ld entries:\n"),
6544 SECTION_NAME (section), section->sh_info);
6546 printf (_(" Addr: 0x"));
6547 printf_vma (section->sh_addr);
6548 printf (_(" Offset: %#08lx Link to section: %ld (%s)\n"),
6549 (unsigned long) section->sh_offset, section->sh_link,
6550 SECTION_HEADER_INDEX (section->sh_link)
6551 < elf_header.e_shnum
6552 ? SECTION_NAME (SECTION_HEADER (section->sh_link))
6553 : "<corrupt>");
6555 eneed = get_data (NULL, file, section->sh_offset, 1,
6556 section->sh_size,
6557 _("version need section"));
6558 if (!eneed)
6559 break;
6561 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
6563 Elf_External_Verneed *entry;
6564 Elf_Internal_Verneed ent;
6565 int j;
6566 int isum;
6567 char *vstart;
6569 vstart = ((char *) eneed) + idx;
6571 entry = (Elf_External_Verneed *) vstart;
6573 ent.vn_version = BYTE_GET (entry->vn_version);
6574 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
6575 ent.vn_file = BYTE_GET (entry->vn_file);
6576 ent.vn_aux = BYTE_GET (entry->vn_aux);
6577 ent.vn_next = BYTE_GET (entry->vn_next);
6579 printf (_(" %#06x: Version: %d"), idx, ent.vn_version);
6581 if (VALID_DYNAMIC_NAME (ent.vn_file))
6582 printf (_(" File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
6583 else
6584 printf (_(" File: %lx"), ent.vn_file);
6586 printf (_(" Cnt: %d\n"), ent.vn_cnt);
6588 vstart += ent.vn_aux;
6590 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
6592 Elf_External_Vernaux *eaux;
6593 Elf_Internal_Vernaux aux;
6595 eaux = (Elf_External_Vernaux *) vstart;
6597 aux.vna_hash = BYTE_GET (eaux->vna_hash);
6598 aux.vna_flags = BYTE_GET (eaux->vna_flags);
6599 aux.vna_other = BYTE_GET (eaux->vna_other);
6600 aux.vna_name = BYTE_GET (eaux->vna_name);
6601 aux.vna_next = BYTE_GET (eaux->vna_next);
6603 if (VALID_DYNAMIC_NAME (aux.vna_name))
6604 printf (_(" %#06x: Name: %s"),
6605 isum, GET_DYNAMIC_NAME (aux.vna_name));
6606 else
6607 printf (_(" %#06x: Name index: %lx"),
6608 isum, aux.vna_name);
6610 printf (_(" Flags: %s Version: %d\n"),
6611 get_ver_flags (aux.vna_flags), aux.vna_other);
6613 isum += aux.vna_next;
6614 vstart += aux.vna_next;
6617 idx += ent.vn_next;
6620 free (eneed);
6622 break;
6624 case SHT_GNU_versym:
6626 Elf_Internal_Shdr *link_section;
6627 int total;
6628 int cnt;
6629 unsigned char *edata;
6630 unsigned short *data;
6631 char *strtab;
6632 Elf_Internal_Sym *symbols;
6633 Elf_Internal_Shdr *string_sec;
6634 long off;
6636 if (SECTION_HEADER_INDEX (section->sh_link) >= elf_header.e_shnum)
6637 break;
6639 link_section = SECTION_HEADER (section->sh_link);
6640 total = section->sh_size / sizeof (Elf_External_Versym);
6642 if (SECTION_HEADER_INDEX (link_section->sh_link)
6643 >= elf_header.e_shnum)
6644 break;
6646 found = 1;
6648 symbols = GET_ELF_SYMBOLS (file, link_section);
6650 string_sec = SECTION_HEADER (link_section->sh_link);
6652 strtab = get_data (NULL, file, string_sec->sh_offset, 1,
6653 string_sec->sh_size, _("version string table"));
6654 if (!strtab)
6655 break;
6657 printf (_("\nVersion symbols section '%s' contains %d entries:\n"),
6658 SECTION_NAME (section), total);
6660 printf (_(" Addr: "));
6661 printf_vma (section->sh_addr);
6662 printf (_(" Offset: %#08lx Link: %lx (%s)\n"),
6663 (unsigned long) section->sh_offset, section->sh_link,
6664 SECTION_NAME (link_section));
6666 off = offset_from_vma (file,
6667 version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
6668 total * sizeof (short));
6669 edata = get_data (NULL, file, off, total, sizeof (short),
6670 _("version symbol data"));
6671 if (!edata)
6673 free (strtab);
6674 break;
6677 data = cmalloc (total, sizeof (short));
6679 for (cnt = total; cnt --;)
6680 data[cnt] = byte_get (edata + cnt * sizeof (short),
6681 sizeof (short));
6683 free (edata);
6685 for (cnt = 0; cnt < total; cnt += 4)
6687 int j, nn;
6688 int check_def, check_need;
6689 char *name;
6691 printf (" %03x:", cnt);
6693 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
6694 switch (data[cnt + j])
6696 case 0:
6697 fputs (_(" 0 (*local*) "), stdout);
6698 break;
6700 case 1:
6701 fputs (_(" 1 (*global*) "), stdout);
6702 break;
6704 default:
6705 nn = printf ("%4x%c", data[cnt + j] & 0x7fff,
6706 data[cnt + j] & 0x8000 ? 'h' : ' ');
6708 check_def = 1;
6709 check_need = 1;
6710 if (SECTION_HEADER_INDEX (symbols[cnt + j].st_shndx)
6711 >= elf_header.e_shnum
6712 || SECTION_HEADER (symbols[cnt + j].st_shndx)->sh_type
6713 != SHT_NOBITS)
6715 if (symbols[cnt + j].st_shndx == SHN_UNDEF)
6716 check_def = 0;
6717 else
6718 check_need = 0;
6721 if (check_need
6722 && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
6724 Elf_Internal_Verneed ivn;
6725 unsigned long offset;
6727 offset = offset_from_vma
6728 (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
6729 sizeof (Elf_External_Verneed));
6733 Elf_Internal_Vernaux ivna;
6734 Elf_External_Verneed evn;
6735 Elf_External_Vernaux evna;
6736 unsigned long a_off;
6738 get_data (&evn, file, offset, sizeof (evn), 1,
6739 _("version need"));
6741 ivn.vn_aux = BYTE_GET (evn.vn_aux);
6742 ivn.vn_next = BYTE_GET (evn.vn_next);
6744 a_off = offset + ivn.vn_aux;
6748 get_data (&evna, file, a_off, sizeof (evna),
6749 1, _("version need aux (2)"));
6751 ivna.vna_next = BYTE_GET (evna.vna_next);
6752 ivna.vna_other = BYTE_GET (evna.vna_other);
6754 a_off += ivna.vna_next;
6756 while (ivna.vna_other != data[cnt + j]
6757 && ivna.vna_next != 0);
6759 if (ivna.vna_other == data[cnt + j])
6761 ivna.vna_name = BYTE_GET (evna.vna_name);
6763 name = strtab + ivna.vna_name;
6764 nn += printf ("(%s%-*s",
6765 name,
6766 12 - (int) strlen (name),
6767 ")");
6768 check_def = 0;
6769 break;
6772 offset += ivn.vn_next;
6774 while (ivn.vn_next);
6777 if (check_def && data[cnt + j] != 0x8001
6778 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
6780 Elf_Internal_Verdef ivd;
6781 Elf_External_Verdef evd;
6782 unsigned long offset;
6784 offset = offset_from_vma
6785 (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
6786 sizeof evd);
6790 get_data (&evd, file, offset, sizeof (evd), 1,
6791 _("version def"));
6793 ivd.vd_next = BYTE_GET (evd.vd_next);
6794 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
6796 offset += ivd.vd_next;
6798 while (ivd.vd_ndx != (data[cnt + j] & 0x7fff)
6799 && ivd.vd_next != 0);
6801 if (ivd.vd_ndx == (data[cnt + j] & 0x7fff))
6803 Elf_External_Verdaux evda;
6804 Elf_Internal_Verdaux ivda;
6806 ivd.vd_aux = BYTE_GET (evd.vd_aux);
6808 get_data (&evda, file,
6809 offset - ivd.vd_next + ivd.vd_aux,
6810 sizeof (evda), 1,
6811 _("version def aux"));
6813 ivda.vda_name = BYTE_GET (evda.vda_name);
6815 name = strtab + ivda.vda_name;
6816 nn += printf ("(%s%-*s",
6817 name,
6818 12 - (int) strlen (name),
6819 ")");
6823 if (nn < 18)
6824 printf ("%*c", 18 - nn, ' ');
6827 putchar ('\n');
6830 free (data);
6831 free (strtab);
6832 free (symbols);
6834 break;
6836 default:
6837 break;
6841 if (! found)
6842 printf (_("\nNo version information found in this file.\n"));
6844 return 1;
6847 static const char *
6848 get_symbol_binding (unsigned int binding)
6850 static char buff[32];
6852 switch (binding)
6854 case STB_LOCAL: return "LOCAL";
6855 case STB_GLOBAL: return "GLOBAL";
6856 case STB_WEAK: return "WEAK";
6857 default:
6858 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
6859 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
6860 binding);
6861 else if (binding >= STB_LOOS && binding <= STB_HIOS)
6862 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
6863 else
6864 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
6865 return buff;
6869 static const char *
6870 get_symbol_type (unsigned int type)
6872 static char buff[32];
6874 switch (type)
6876 case STT_NOTYPE: return "NOTYPE";
6877 case STT_OBJECT: return "OBJECT";
6878 case STT_FUNC: return "FUNC";
6879 case STT_SECTION: return "SECTION";
6880 case STT_FILE: return "FILE";
6881 case STT_COMMON: return "COMMON";
6882 case STT_TLS: return "TLS";
6883 case STT_RELC: return "RELC";
6884 case STT_SRELC: return "SRELC";
6885 default:
6886 if (type >= STT_LOPROC && type <= STT_HIPROC)
6888 if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
6889 return "THUMB_FUNC";
6891 if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
6892 return "REGISTER";
6894 if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
6895 return "PARISC_MILLI";
6897 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
6899 else if (type >= STT_LOOS && type <= STT_HIOS)
6901 if (elf_header.e_machine == EM_PARISC)
6903 if (type == STT_HP_OPAQUE)
6904 return "HP_OPAQUE";
6905 if (type == STT_HP_STUB)
6906 return "HP_STUB";
6909 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
6911 else
6912 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
6913 return buff;
6917 static const char *
6918 get_symbol_visibility (unsigned int visibility)
6920 switch (visibility)
6922 case STV_DEFAULT: return "DEFAULT";
6923 case STV_INTERNAL: return "INTERNAL";
6924 case STV_HIDDEN: return "HIDDEN";
6925 case STV_PROTECTED: return "PROTECTED";
6926 default: abort ();
6930 static const char *
6931 get_mips_symbol_other (unsigned int other)
6933 switch (other)
6935 case STO_OPTIONAL: return "OPTIONAL";
6936 case STO_MIPS16: return "MIPS16";
6937 default: return NULL;
6941 static const char *
6942 get_symbol_other (unsigned int other)
6944 const char * result = NULL;
6945 static char buff [32];
6947 if (other == 0)
6948 return "";
6950 switch (elf_header.e_machine)
6952 case EM_MIPS:
6953 result = get_mips_symbol_other (other);
6954 default:
6955 break;
6958 if (result)
6959 return result;
6961 snprintf (buff, sizeof buff, _("<other>: %x"), other);
6962 return buff;
6965 static const char *
6966 get_symbol_index_type (unsigned int type)
6968 static char buff[32];
6970 switch (type)
6972 case SHN_UNDEF: return "UND";
6973 case SHN_ABS: return "ABS";
6974 case SHN_COMMON: return "COM";
6975 default:
6976 if (type == SHN_IA_64_ANSI_COMMON
6977 && elf_header.e_machine == EM_IA_64
6978 && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
6979 return "ANSI_COM";
6980 else if (elf_header.e_machine == EM_X86_64
6981 && type == SHN_X86_64_LCOMMON)
6982 return "LARGE_COM";
6983 else if (type == SHN_MIPS_SCOMMON
6984 && elf_header.e_machine == EM_MIPS)
6985 return "SCOM";
6986 else if (type == SHN_MIPS_SUNDEFINED
6987 && elf_header.e_machine == EM_MIPS)
6988 return "SUND";
6989 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
6990 sprintf (buff, "PRC[0x%04x]", type);
6991 else if (type >= SHN_LOOS && type <= SHN_HIOS)
6992 sprintf (buff, "OS [0x%04x]", type);
6993 else if (type >= SHN_LORESERVE && type <= SHN_HIRESERVE)
6994 sprintf (buff, "RSV[0x%04x]", type);
6995 else
6996 sprintf (buff, "%3d", type);
6997 break;
7000 return buff;
7003 static bfd_vma *
7004 get_dynamic_data (FILE *file, unsigned int number, unsigned int ent_size)
7006 unsigned char *e_data;
7007 bfd_vma *i_data;
7009 e_data = cmalloc (number, ent_size);
7011 if (e_data == NULL)
7013 error (_("Out of memory\n"));
7014 return NULL;
7017 if (fread (e_data, ent_size, number, file) != number)
7019 error (_("Unable to read in dynamic data\n"));
7020 return NULL;
7023 i_data = cmalloc (number, sizeof (*i_data));
7025 if (i_data == NULL)
7027 error (_("Out of memory\n"));
7028 free (e_data);
7029 return NULL;
7032 while (number--)
7033 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
7035 free (e_data);
7037 return i_data;
7040 /* Dump the symbol table. */
7041 static int
7042 process_symbol_table (FILE *file)
7044 Elf_Internal_Shdr *section;
7045 bfd_vma nbuckets = 0;
7046 bfd_vma nchains = 0;
7047 bfd_vma *buckets = NULL;
7048 bfd_vma *chains = NULL;
7049 bfd_vma ngnubuckets = 0;
7050 bfd_vma *gnubuckets = NULL;
7051 bfd_vma *gnuchains = NULL;
7053 if (! do_syms && !do_histogram)
7054 return 1;
7056 if (dynamic_info[DT_HASH] && ((do_using_dynamic && dynamic_strings != NULL)
7057 || do_histogram))
7059 unsigned char nb[8];
7060 unsigned char nc[8];
7061 int hash_ent_size = 4;
7063 if ((elf_header.e_machine == EM_ALPHA
7064 || elf_header.e_machine == EM_S390
7065 || elf_header.e_machine == EM_S390_OLD)
7066 && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
7067 hash_ent_size = 8;
7069 if (fseek (file,
7070 (archive_file_offset
7071 + offset_from_vma (file, dynamic_info[DT_HASH],
7072 sizeof nb + sizeof nc)),
7073 SEEK_SET))
7075 error (_("Unable to seek to start of dynamic information\n"));
7076 return 0;
7079 if (fread (nb, hash_ent_size, 1, file) != 1)
7081 error (_("Failed to read in number of buckets\n"));
7082 return 0;
7085 if (fread (nc, hash_ent_size, 1, file) != 1)
7087 error (_("Failed to read in number of chains\n"));
7088 return 0;
7091 nbuckets = byte_get (nb, hash_ent_size);
7092 nchains = byte_get (nc, hash_ent_size);
7094 buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
7095 chains = get_dynamic_data (file, nchains, hash_ent_size);
7097 if (buckets == NULL || chains == NULL)
7098 return 0;
7101 if (do_syms
7102 && dynamic_info[DT_HASH] && do_using_dynamic && dynamic_strings != NULL)
7104 unsigned long hn;
7105 bfd_vma si;
7107 printf (_("\nSymbol table for image:\n"));
7108 if (is_32bit_elf)
7109 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7110 else
7111 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
7113 for (hn = 0; hn < nbuckets; hn++)
7115 if (! buckets[hn])
7116 continue;
7118 for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
7120 Elf_Internal_Sym *psym;
7121 int n;
7123 psym = dynamic_symbols + si;
7125 n = print_vma (si, DEC_5);
7126 if (n < 5)
7127 fputs (" " + n, stdout);
7128 printf (" %3lu: ", hn);
7129 print_vma (psym->st_value, LONG_HEX);
7130 putchar (' ');
7131 print_vma (psym->st_size, DEC_5);
7133 printf (" %6s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
7134 printf (" %6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
7135 printf (" %3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
7136 /* Check to see if any other bits in the st_other field are set.
7137 Note - displaying this information disrupts the layout of the
7138 table being generated, but for the moment this case is very rare. */
7139 if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
7140 printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
7141 printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
7142 if (VALID_DYNAMIC_NAME (psym->st_name))
7143 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
7144 else
7145 printf (" <corrupt: %14ld>", psym->st_name);
7146 putchar ('\n');
7150 else if (do_syms && !do_using_dynamic)
7152 unsigned int i;
7154 for (i = 0, section = section_headers;
7155 i < elf_header.e_shnum;
7156 i++, section++)
7158 unsigned int si;
7159 char *strtab = NULL;
7160 unsigned long int strtab_size = 0;
7161 Elf_Internal_Sym *symtab;
7162 Elf_Internal_Sym *psym;
7165 if ( section->sh_type != SHT_SYMTAB
7166 && section->sh_type != SHT_DYNSYM)
7167 continue;
7169 printf (_("\nSymbol table '%s' contains %lu entries:\n"),
7170 SECTION_NAME (section),
7171 (unsigned long) (section->sh_size / section->sh_entsize));
7172 if (is_32bit_elf)
7173 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
7174 else
7175 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
7177 symtab = GET_ELF_SYMBOLS (file, section);
7178 if (symtab == NULL)
7179 continue;
7181 if (section->sh_link == elf_header.e_shstrndx)
7183 strtab = string_table;
7184 strtab_size = string_table_length;
7186 else if (SECTION_HEADER_INDEX (section->sh_link) < elf_header.e_shnum)
7188 Elf_Internal_Shdr *string_sec;
7190 string_sec = SECTION_HEADER (section->sh_link);
7192 strtab = get_data (NULL, file, string_sec->sh_offset,
7193 1, string_sec->sh_size, _("string table"));
7194 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
7197 for (si = 0, psym = symtab;
7198 si < section->sh_size / section->sh_entsize;
7199 si++, psym++)
7201 printf ("%6d: ", si);
7202 print_vma (psym->st_value, LONG_HEX);
7203 putchar (' ');
7204 print_vma (psym->st_size, DEC_5);
7205 printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
7206 printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
7207 printf (" %-3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
7208 /* Check to see if any other bits in the st_other field are set.
7209 Note - displaying this information disrupts the layout of the
7210 table being generated, but for the moment this case is very rare. */
7211 if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
7212 printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
7213 printf (" %4s ", get_symbol_index_type (psym->st_shndx));
7214 print_symbol (25, psym->st_name < strtab_size
7215 ? strtab + psym->st_name : "<corrupt>");
7217 if (section->sh_type == SHT_DYNSYM &&
7218 version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
7220 unsigned char data[2];
7221 unsigned short vers_data;
7222 unsigned long offset;
7223 int is_nobits;
7224 int check_def;
7226 offset = offset_from_vma
7227 (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
7228 sizeof data + si * sizeof (vers_data));
7230 get_data (&data, file, offset + si * sizeof (vers_data),
7231 sizeof (data), 1, _("version data"));
7233 vers_data = byte_get (data, 2);
7235 is_nobits = (SECTION_HEADER_INDEX (psym->st_shndx)
7236 < elf_header.e_shnum
7237 && SECTION_HEADER (psym->st_shndx)->sh_type
7238 == SHT_NOBITS);
7240 check_def = (psym->st_shndx != SHN_UNDEF);
7242 if ((vers_data & 0x8000) || vers_data > 1)
7244 if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
7245 && (is_nobits || ! check_def))
7247 Elf_External_Verneed evn;
7248 Elf_Internal_Verneed ivn;
7249 Elf_Internal_Vernaux ivna;
7251 /* We must test both. */
7252 offset = offset_from_vma
7253 (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
7254 sizeof evn);
7258 unsigned long vna_off;
7260 get_data (&evn, file, offset, sizeof (evn), 1,
7261 _("version need"));
7263 ivn.vn_aux = BYTE_GET (evn.vn_aux);
7264 ivn.vn_next = BYTE_GET (evn.vn_next);
7266 vna_off = offset + ivn.vn_aux;
7270 Elf_External_Vernaux evna;
7272 get_data (&evna, file, vna_off,
7273 sizeof (evna), 1,
7274 _("version need aux (3)"));
7276 ivna.vna_other = BYTE_GET (evna.vna_other);
7277 ivna.vna_next = BYTE_GET (evna.vna_next);
7278 ivna.vna_name = BYTE_GET (evna.vna_name);
7280 vna_off += ivna.vna_next;
7282 while (ivna.vna_other != vers_data
7283 && ivna.vna_next != 0);
7285 if (ivna.vna_other == vers_data)
7286 break;
7288 offset += ivn.vn_next;
7290 while (ivn.vn_next != 0);
7292 if (ivna.vna_other == vers_data)
7294 printf ("@%s (%d)",
7295 ivna.vna_name < strtab_size
7296 ? strtab + ivna.vna_name : "<corrupt>",
7297 ivna.vna_other);
7298 check_def = 0;
7300 else if (! is_nobits)
7301 error (_("bad dynamic symbol\n"));
7302 else
7303 check_def = 1;
7306 if (check_def)
7308 if (vers_data != 0x8001
7309 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
7311 Elf_Internal_Verdef ivd;
7312 Elf_Internal_Verdaux ivda;
7313 Elf_External_Verdaux evda;
7314 unsigned long offset;
7316 offset = offset_from_vma
7317 (file,
7318 version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
7319 sizeof (Elf_External_Verdef));
7323 Elf_External_Verdef evd;
7325 get_data (&evd, file, offset, sizeof (evd),
7326 1, _("version def"));
7328 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
7329 ivd.vd_aux = BYTE_GET (evd.vd_aux);
7330 ivd.vd_next = BYTE_GET (evd.vd_next);
7332 offset += ivd.vd_next;
7334 while (ivd.vd_ndx != (vers_data & 0x7fff)
7335 && ivd.vd_next != 0);
7337 offset -= ivd.vd_next;
7338 offset += ivd.vd_aux;
7340 get_data (&evda, file, offset, sizeof (evda),
7341 1, _("version def aux"));
7343 ivda.vda_name = BYTE_GET (evda.vda_name);
7345 if (psym->st_name != ivda.vda_name)
7346 printf ((vers_data & 0x8000)
7347 ? "@%s" : "@@%s",
7348 ivda.vda_name < strtab_size
7349 ? strtab + ivda.vda_name : "<corrupt>");
7355 putchar ('\n');
7358 free (symtab);
7359 if (strtab != string_table)
7360 free (strtab);
7363 else if (do_syms)
7364 printf
7365 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
7367 if (do_histogram && buckets != NULL)
7369 unsigned long *lengths;
7370 unsigned long *counts;
7371 unsigned long hn;
7372 bfd_vma si;
7373 unsigned long maxlength = 0;
7374 unsigned long nzero_counts = 0;
7375 unsigned long nsyms = 0;
7377 printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
7378 (unsigned long) nbuckets);
7379 printf (_(" Length Number %% of total Coverage\n"));
7381 lengths = calloc (nbuckets, sizeof (*lengths));
7382 if (lengths == NULL)
7384 error (_("Out of memory\n"));
7385 return 0;
7387 for (hn = 0; hn < nbuckets; ++hn)
7389 for (si = buckets[hn]; si > 0 && si < nchains; si = chains[si])
7391 ++nsyms;
7392 if (maxlength < ++lengths[hn])
7393 ++maxlength;
7397 counts = calloc (maxlength + 1, sizeof (*counts));
7398 if (counts == NULL)
7400 error (_("Out of memory\n"));
7401 return 0;
7404 for (hn = 0; hn < nbuckets; ++hn)
7405 ++counts[lengths[hn]];
7407 if (nbuckets > 0)
7409 unsigned long i;
7410 printf (" 0 %-10lu (%5.1f%%)\n",
7411 counts[0], (counts[0] * 100.0) / nbuckets);
7412 for (i = 1; i <= maxlength; ++i)
7414 nzero_counts += counts[i] * i;
7415 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
7416 i, counts[i], (counts[i] * 100.0) / nbuckets,
7417 (nzero_counts * 100.0) / nsyms);
7421 free (counts);
7422 free (lengths);
7425 if (buckets != NULL)
7427 free (buckets);
7428 free (chains);
7431 if (do_histogram && dynamic_info_DT_GNU_HASH)
7433 unsigned char nb[16];
7434 bfd_vma i, maxchain = 0xffffffff, symidx, bitmaskwords;
7435 unsigned long *lengths;
7436 unsigned long *counts;
7437 unsigned long hn;
7438 unsigned long maxlength = 0;
7439 unsigned long nzero_counts = 0;
7440 unsigned long nsyms = 0;
7441 bfd_vma buckets_vma;
7443 if (fseek (file,
7444 (archive_file_offset
7445 + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
7446 sizeof nb)),
7447 SEEK_SET))
7449 error (_("Unable to seek to start of dynamic information\n"));
7450 return 0;
7453 if (fread (nb, 16, 1, file) != 1)
7455 error (_("Failed to read in number of buckets\n"));
7456 return 0;
7459 ngnubuckets = byte_get (nb, 4);
7460 symidx = byte_get (nb + 4, 4);
7461 bitmaskwords = byte_get (nb + 8, 4);
7462 buckets_vma = dynamic_info_DT_GNU_HASH + 16;
7463 if (is_32bit_elf)
7464 buckets_vma += bitmaskwords * 4;
7465 else
7466 buckets_vma += bitmaskwords * 8;
7468 if (fseek (file,
7469 (archive_file_offset
7470 + offset_from_vma (file, buckets_vma, 4)),
7471 SEEK_SET))
7473 error (_("Unable to seek to start of dynamic information\n"));
7474 return 0;
7477 gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
7479 if (gnubuckets == NULL)
7480 return 0;
7482 for (i = 0; i < ngnubuckets; i++)
7483 if (gnubuckets[i] != 0)
7485 if (gnubuckets[i] < symidx)
7486 return 0;
7488 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
7489 maxchain = gnubuckets[i];
7492 if (maxchain == 0xffffffff)
7493 return 0;
7495 maxchain -= symidx;
7497 if (fseek (file,
7498 (archive_file_offset
7499 + offset_from_vma (file, buckets_vma
7500 + 4 * (ngnubuckets + maxchain), 4)),
7501 SEEK_SET))
7503 error (_("Unable to seek to start of dynamic information\n"));
7504 return 0;
7509 if (fread (nb, 4, 1, file) != 1)
7511 error (_("Failed to determine last chain length\n"));
7512 return 0;
7515 if (maxchain + 1 == 0)
7516 return 0;
7518 ++maxchain;
7520 while ((byte_get (nb, 4) & 1) == 0);
7522 if (fseek (file,
7523 (archive_file_offset
7524 + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
7525 SEEK_SET))
7527 error (_("Unable to seek to start of dynamic information\n"));
7528 return 0;
7531 gnuchains = get_dynamic_data (file, maxchain, 4);
7533 if (gnuchains == NULL)
7534 return 0;
7536 lengths = calloc (ngnubuckets, sizeof (*lengths));
7537 if (lengths == NULL)
7539 error (_("Out of memory\n"));
7540 return 0;
7543 printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
7544 (unsigned long) ngnubuckets);
7545 printf (_(" Length Number %% of total Coverage\n"));
7547 for (hn = 0; hn < ngnubuckets; ++hn)
7548 if (gnubuckets[hn] != 0)
7550 bfd_vma off, length = 1;
7552 for (off = gnubuckets[hn] - symidx;
7553 (gnuchains[off] & 1) == 0; ++off)
7554 ++length;
7555 lengths[hn] = length;
7556 if (length > maxlength)
7557 maxlength = length;
7558 nsyms += length;
7561 counts = calloc (maxlength + 1, sizeof (*counts));
7562 if (counts == NULL)
7564 error (_("Out of memory\n"));
7565 return 0;
7568 for (hn = 0; hn < ngnubuckets; ++hn)
7569 ++counts[lengths[hn]];
7571 if (ngnubuckets > 0)
7573 unsigned long j;
7574 printf (" 0 %-10lu (%5.1f%%)\n",
7575 counts[0], (counts[0] * 100.0) / ngnubuckets);
7576 for (j = 1; j <= maxlength; ++j)
7578 nzero_counts += counts[j] * j;
7579 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
7580 j, counts[j], (counts[j] * 100.0) / ngnubuckets,
7581 (nzero_counts * 100.0) / nsyms);
7585 free (counts);
7586 free (lengths);
7587 free (gnubuckets);
7588 free (gnuchains);
7591 return 1;
7594 static int
7595 process_syminfo (FILE *file ATTRIBUTE_UNUSED)
7597 unsigned int i;
7599 if (dynamic_syminfo == NULL
7600 || !do_dynamic)
7601 /* No syminfo, this is ok. */
7602 return 1;
7604 /* There better should be a dynamic symbol section. */
7605 if (dynamic_symbols == NULL || dynamic_strings == NULL)
7606 return 0;
7608 if (dynamic_addr)
7609 printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
7610 dynamic_syminfo_offset, dynamic_syminfo_nent);
7612 printf (_(" Num: Name BoundTo Flags\n"));
7613 for (i = 0; i < dynamic_syminfo_nent; ++i)
7615 unsigned short int flags = dynamic_syminfo[i].si_flags;
7617 printf ("%4d: ", i);
7618 if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
7619 print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
7620 else
7621 printf ("<corrupt: %19ld>", dynamic_symbols[i].st_name);
7622 putchar (' ');
7624 switch (dynamic_syminfo[i].si_boundto)
7626 case SYMINFO_BT_SELF:
7627 fputs ("SELF ", stdout);
7628 break;
7629 case SYMINFO_BT_PARENT:
7630 fputs ("PARENT ", stdout);
7631 break;
7632 default:
7633 if (dynamic_syminfo[i].si_boundto > 0
7634 && dynamic_syminfo[i].si_boundto < dynamic_nent
7635 && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
7637 print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
7638 putchar (' ' );
7640 else
7641 printf ("%-10d ", dynamic_syminfo[i].si_boundto);
7642 break;
7645 if (flags & SYMINFO_FLG_DIRECT)
7646 printf (" DIRECT");
7647 if (flags & SYMINFO_FLG_PASSTHRU)
7648 printf (" PASSTHRU");
7649 if (flags & SYMINFO_FLG_COPY)
7650 printf (" COPY");
7651 if (flags & SYMINFO_FLG_LAZYLOAD)
7652 printf (" LAZYLOAD");
7654 puts ("");
7657 return 1;
7660 #ifdef SUPPORT_DISASSEMBLY
7661 static int
7662 disassemble_section (Elf_Internal_Shdr *section, FILE *file)
7664 printf (_("\nAssembly dump of section %s\n"),
7665 SECTION_NAME (section));
7667 /* XXX -- to be done --- XXX */
7669 return 1;
7671 #endif
7673 static int
7674 dump_section (Elf_Internal_Shdr *section, FILE *file)
7676 Elf_Internal_Shdr *relsec;
7677 bfd_size_type bytes;
7678 bfd_vma addr;
7679 unsigned char *data;
7680 unsigned char *start;
7682 bytes = section->sh_size;
7684 if (bytes == 0 || section->sh_type == SHT_NOBITS)
7686 printf (_("\nSection '%s' has no data to dump.\n"),
7687 SECTION_NAME (section));
7688 return 0;
7690 else
7691 printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section));
7693 addr = section->sh_addr;
7695 start = get_data (NULL, file, section->sh_offset, 1, bytes,
7696 _("section data"));
7697 if (!start)
7698 return 0;
7700 /* If the section being dumped has relocations against it the user might
7701 be expecting these relocations to have been applied. Check for this
7702 case and issue a warning message in order to avoid confusion.
7703 FIXME: Maybe we ought to have an option that dumps a section with
7704 relocs applied ? */
7705 for (relsec = section_headers;
7706 relsec < section_headers + elf_header.e_shnum;
7707 ++relsec)
7709 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
7710 || SECTION_HEADER_INDEX (relsec->sh_info) >= elf_header.e_shnum
7711 || SECTION_HEADER (relsec->sh_info) != section
7712 || relsec->sh_size == 0
7713 || SECTION_HEADER_INDEX (relsec->sh_link) >= elf_header.e_shnum)
7714 continue;
7716 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
7717 break;
7720 data = start;
7722 while (bytes)
7724 int j;
7725 int k;
7726 int lbytes;
7728 lbytes = (bytes > 16 ? 16 : bytes);
7730 printf (" 0x%8.8lx ", (unsigned long) addr);
7732 for (j = 0; j < 16; j++)
7734 if (j < lbytes)
7735 printf ("%2.2x", data[j]);
7736 else
7737 printf (" ");
7739 if ((j & 3) == 3)
7740 printf (" ");
7743 for (j = 0; j < lbytes; j++)
7745 k = data[j];
7746 if (k >= ' ' && k < 0x7f)
7747 printf ("%c", k);
7748 else
7749 printf (".");
7752 putchar ('\n');
7754 data += lbytes;
7755 addr += lbytes;
7756 bytes -= lbytes;
7759 free (start);
7761 putchar ('\n');
7762 return 1;
7765 /* Return the number of bytes affected by a given reloc.
7766 This information is architecture and reloc dependent.
7767 Returns 4 by default, although this is not always correct.
7768 It should return 0 if a decision cannot be made.
7769 FIXME: This is not the correct way to solve this problem.
7770 The proper way is to have target specific reloc sizing functions
7771 created by the reloc-macros.h header, in the same way that it
7772 already creates the reloc naming functions. */
7774 static unsigned int
7775 get_reloc_size (Elf_Internal_Rela * reloc)
7777 switch (elf_header.e_machine)
7779 case EM_H8S:
7780 case EM_H8_300:
7781 case EM_H8_300H:
7782 case EM_H8_500:
7783 switch (ELF32_R_TYPE (reloc->r_info))
7785 /* PR gas/3800 - without this information we do not correctly
7786 decode the debug information generated by the h8300 assembler. */
7787 case R_H8_DIR16:
7788 return 2;
7789 default:
7790 return 4;
7792 default:
7793 /* FIXME: We need to extend this switch statement to cope with other
7794 architecture's relocs. (When those relocs are used against debug
7795 sections, and when their size is not 4). But see the multiple
7796 inclusions of <elf/h8.h> for an example of the hoops that we need
7797 to jump through in order to obtain the reloc numbers. */
7798 return 4;
7802 /* Apply addends of RELA relocations. */
7804 static int
7805 debug_apply_rela_addends (void *file,
7806 Elf_Internal_Shdr *section,
7807 unsigned char *start)
7809 Elf_Internal_Shdr *relsec;
7810 unsigned char *end = start + section->sh_size;
7812 if (!is_relocatable)
7813 return 1;
7815 /* SH uses RELA but uses in place value instead of the addend field. */
7816 if (elf_header.e_machine == EM_SH)
7817 return 0;
7819 for (relsec = section_headers;
7820 relsec < section_headers + elf_header.e_shnum;
7821 ++relsec)
7823 unsigned long nrelas;
7824 Elf_Internal_Rela *rela, *rp;
7825 Elf_Internal_Shdr *symsec;
7826 Elf_Internal_Sym *symtab;
7827 Elf_Internal_Sym *sym;
7829 if (relsec->sh_type != SHT_RELA
7830 || SECTION_HEADER_INDEX (relsec->sh_info) >= elf_header.e_shnum
7831 || SECTION_HEADER (relsec->sh_info) != section
7832 || relsec->sh_size == 0
7833 || SECTION_HEADER_INDEX (relsec->sh_link) >= elf_header.e_shnum)
7834 continue;
7836 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
7837 &rela, &nrelas))
7838 return 0;
7840 symsec = SECTION_HEADER (relsec->sh_link);
7841 symtab = GET_ELF_SYMBOLS (file, symsec);
7843 for (rp = rela; rp < rela + nrelas; ++rp)
7845 unsigned char *loc;
7846 unsigned int reloc_size;
7848 reloc_size = get_reloc_size (rp);
7849 if (reloc_size == 0)
7851 warn (_("skipping relocation of unknown size against offset 0x%lx in section %s\n"),
7852 (unsigned long) rp->r_offset,
7853 SECTION_NAME (section));
7854 continue;
7857 loc = start + rp->r_offset;
7858 if ((loc + reloc_size) > end)
7860 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
7861 (unsigned long) rp->r_offset,
7862 SECTION_NAME (section));
7863 continue;
7866 if (is_32bit_elf)
7868 sym = symtab + ELF32_R_SYM (rp->r_info);
7870 if (ELF32_R_SYM (rp->r_info) != 0
7871 && ELF32_ST_TYPE (sym->st_info) != STT_SECTION
7872 /* Relocations against symbols without type can happen.
7873 Gcc -feliminate-dwarf2-dups may generate symbols
7874 without type for debug info. */
7875 && ELF32_ST_TYPE (sym->st_info) != STT_NOTYPE
7876 /* Relocations against object symbols can happen,
7877 eg when referencing a global array. For an
7878 example of this see the _clz.o binary in libgcc.a. */
7879 && ELF32_ST_TYPE (sym->st_info) != STT_OBJECT)
7881 warn (_("skipping unexpected symbol type %s in relocation in section .rela%s\n"),
7882 get_symbol_type (ELF32_ST_TYPE (sym->st_info)),
7883 SECTION_NAME (section));
7884 continue;
7887 else
7889 /* In MIPS little-endian objects, r_info isn't really a
7890 64-bit little-endian value: it has a 32-bit little-endian
7891 symbol index followed by four individual byte fields.
7892 Reorder INFO accordingly. */
7893 if (elf_header.e_machine == EM_MIPS
7894 && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
7895 rp->r_info = (((rp->r_info & 0xffffffff) << 32)
7896 | ((rp->r_info >> 56) & 0xff)
7897 | ((rp->r_info >> 40) & 0xff00)
7898 | ((rp->r_info >> 24) & 0xff0000)
7899 | ((rp->r_info >> 8) & 0xff000000));
7901 sym = symtab + ELF64_R_SYM (rp->r_info);
7903 if (ELF64_R_SYM (rp->r_info) != 0
7904 && ELF64_ST_TYPE (sym->st_info) != STT_SECTION
7905 && ELF64_ST_TYPE (sym->st_info) != STT_NOTYPE
7906 && ELF64_ST_TYPE (sym->st_info) != STT_OBJECT)
7908 warn (_("skipping unexpected symbol type %s in relocation in section .rela.%s\n"),
7909 get_symbol_type (ELF64_ST_TYPE (sym->st_info)),
7910 SECTION_NAME (section));
7911 continue;
7915 byte_put (loc, rp->r_addend, reloc_size);
7918 free (symtab);
7919 free (rela);
7920 break;
7922 return 1;
7926 load_debug_section (enum dwarf_section_display_enum debug, void *file)
7928 struct dwarf_section *section = &debug_displays [debug].section;
7929 Elf_Internal_Shdr *sec;
7930 char buf [64];
7932 /* If it is already loaded, do nothing. */
7933 if (section->start != NULL)
7934 return 1;
7936 /* Locate the debug section. */
7937 sec = find_section (section->name);
7938 if (sec == NULL)
7939 return 0;
7941 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
7942 section->address = sec->sh_addr;
7943 section->size = sec->sh_size;
7944 section->start = get_data (NULL, file, sec->sh_offset, 1,
7945 sec->sh_size, buf);
7947 if (debug_displays [debug].relocate)
7948 debug_apply_rela_addends (file, sec, section->start);
7950 return section->start != NULL;
7953 void
7954 free_debug_section (enum dwarf_section_display_enum debug)
7956 struct dwarf_section *section = &debug_displays [debug].section;
7958 if (section->start == NULL)
7959 return;
7961 free ((char *) section->start);
7962 section->start = NULL;
7963 section->address = 0;
7964 section->size = 0;
7967 static int
7968 display_debug_section (Elf_Internal_Shdr *section, FILE *file)
7970 char *name = SECTION_NAME (section);
7971 bfd_size_type length;
7972 int result = 1;
7973 enum dwarf_section_display_enum i;
7975 length = section->sh_size;
7976 if (length == 0)
7978 printf (_("\nSection '%s' has no debugging data.\n"), name);
7979 return 0;
7982 if (const_strneq (name, ".gnu.linkonce.wi."))
7983 name = ".debug_info";
7985 /* See if we know how to display the contents of this section. */
7986 for (i = 0; i < max; i++)
7987 if (streq (debug_displays[i].section.name, name))
7989 struct dwarf_section *sec = &debug_displays [i].section;
7991 if (load_debug_section (i, file))
7993 result &= debug_displays[i].display (sec, file);
7995 if (i != info && i != abbrev)
7996 free_debug_section (i);
7999 break;
8002 if (i == max)
8004 printf (_("Unrecognized debug section: %s\n"), name);
8005 result = 0;
8008 return result;
8011 /* Set DUMP_SECTS for all sections where dumps were requested
8012 based on section name. */
8014 static void
8015 initialise_dumps_byname (void)
8017 struct dump_list_entry *cur;
8019 for (cur = dump_sects_byname; cur; cur = cur->next)
8021 unsigned int i;
8022 int any;
8024 for (i = 0, any = 0; i < elf_header.e_shnum; i++)
8025 if (streq (SECTION_NAME (section_headers + i), cur->name))
8027 request_dump (i, cur->type);
8028 any = 1;
8031 if (!any)
8032 warn (_("Section '%s' was not dumped because it does not exist!\n"),
8033 cur->name);
8037 static void
8038 process_section_contents (FILE *file)
8040 Elf_Internal_Shdr *section;
8041 unsigned int i;
8043 if (! do_dump)
8044 return;
8046 initialise_dumps_byname ();
8048 for (i = 0, section = section_headers;
8049 i < elf_header.e_shnum && i < num_dump_sects;
8050 i++, section++)
8052 #ifdef SUPPORT_DISASSEMBLY
8053 if (dump_sects[i] & DISASS_DUMP)
8054 disassemble_section (section, file);
8055 #endif
8056 if (dump_sects[i] & HEX_DUMP)
8057 dump_section (section, file);
8059 if (dump_sects[i] & DEBUG_DUMP)
8060 display_debug_section (section, file);
8063 /* Check to see if the user requested a
8064 dump of a section that does not exist. */
8065 while (i++ < num_dump_sects)
8066 if (dump_sects[i])
8067 warn (_("Section %d was not dumped because it does not exist!\n"), i);
8070 static void
8071 process_mips_fpe_exception (int mask)
8073 if (mask)
8075 int first = 1;
8076 if (mask & OEX_FPU_INEX)
8077 fputs ("INEX", stdout), first = 0;
8078 if (mask & OEX_FPU_UFLO)
8079 printf ("%sUFLO", first ? "" : "|"), first = 0;
8080 if (mask & OEX_FPU_OFLO)
8081 printf ("%sOFLO", first ? "" : "|"), first = 0;
8082 if (mask & OEX_FPU_DIV0)
8083 printf ("%sDIV0", first ? "" : "|"), first = 0;
8084 if (mask & OEX_FPU_INVAL)
8085 printf ("%sINVAL", first ? "" : "|");
8087 else
8088 fputs ("0", stdout);
8091 /* ARM EABI attributes section. */
8092 typedef struct
8094 int tag;
8095 const char *name;
8096 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
8097 int type;
8098 const char **table;
8099 } arm_attr_public_tag;
8101 static const char *arm_attr_tag_CPU_arch[] =
8102 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
8103 "v6K", "v7"};
8104 static const char *arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
8105 static const char *arm_attr_tag_THUMB_ISA_use[] =
8106 {"No", "Thumb-1", "Thumb-2"};
8107 /* FIXME: VFPv3 encoding was extrapolated! */
8108 static const char *arm_attr_tag_VFP_arch[] = {"No", "VFPv1", "VFPv2", "VFPv3"};
8109 static const char *arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1"};
8110 static const char *arm_attr_tag_NEON_arch[] = {"No", "NEONv1"};
8111 static const char *arm_attr_tag_ABI_PCS_config[] =
8112 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
8113 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
8114 static const char *arm_attr_tag_ABI_PCS_R9_use[] =
8115 {"V6", "SB", "TLS", "Unused"};
8116 static const char *arm_attr_tag_ABI_PCS_RW_data[] =
8117 {"Absolute", "PC-relative", "SB-relative", "None"};
8118 static const char *arm_attr_tag_ABI_PCS_RO_DATA[] =
8119 {"Absolute", "PC-relative", "None"};
8120 static const char *arm_attr_tag_ABI_PCS_GOT_use[] =
8121 {"None", "direct", "GOT-indirect"};
8122 static const char *arm_attr_tag_ABI_PCS_wchar_t[] =
8123 {"None", "??? 1", "2", "??? 3", "4"};
8124 static const char *arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
8125 static const char *arm_attr_tag_ABI_FP_denormal[] = {"Unused", "Needed"};
8126 static const char *arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
8127 static const char *arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
8128 static const char *arm_attr_tag_ABI_FP_number_model[] =
8129 {"Unused", "Finite", "RTABI", "IEEE 754"};
8130 static const char *arm_attr_tag_ABI_align8_needed[] = {"No", "Yes", "4-byte"};
8131 static const char *arm_attr_tag_ABI_align8_preserved[] =
8132 {"No", "Yes, except leaf SP", "Yes"};
8133 static const char *arm_attr_tag_ABI_enum_size[] =
8134 {"Unused", "small", "int", "forced to int"};
8135 static const char *arm_attr_tag_ABI_HardFP_use[] =
8136 {"As Tag_VFP_arch", "SP only", "DP only", "SP and DP"};
8137 static const char *arm_attr_tag_ABI_VFP_args[] =
8138 {"AAPCS", "VFP registers", "custom"};
8139 static const char *arm_attr_tag_ABI_WMMX_args[] =
8140 {"AAPCS", "WMMX registers", "custom"};
8141 static const char *arm_attr_tag_ABI_optimization_goals[] =
8142 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
8143 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
8144 static const char *arm_attr_tag_ABI_FP_optimization_goals[] =
8145 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
8146 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
8148 #define LOOKUP(id, name) \
8149 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
8150 static arm_attr_public_tag arm_attr_public_tags[] =
8152 {4, "CPU_raw_name", 1, NULL},
8153 {5, "CPU_name", 1, NULL},
8154 LOOKUP(6, CPU_arch),
8155 {7, "CPU_arch_profile", 0, NULL},
8156 LOOKUP(8, ARM_ISA_use),
8157 LOOKUP(9, THUMB_ISA_use),
8158 LOOKUP(10, VFP_arch),
8159 LOOKUP(11, WMMX_arch),
8160 LOOKUP(12, NEON_arch),
8161 LOOKUP(13, ABI_PCS_config),
8162 LOOKUP(14, ABI_PCS_R9_use),
8163 LOOKUP(15, ABI_PCS_RW_data),
8164 LOOKUP(16, ABI_PCS_RO_DATA),
8165 LOOKUP(17, ABI_PCS_GOT_use),
8166 LOOKUP(18, ABI_PCS_wchar_t),
8167 LOOKUP(19, ABI_FP_rounding),
8168 LOOKUP(20, ABI_FP_denormal),
8169 LOOKUP(21, ABI_FP_exceptions),
8170 LOOKUP(22, ABI_FP_user_exceptions),
8171 LOOKUP(23, ABI_FP_number_model),
8172 LOOKUP(24, ABI_align8_needed),
8173 LOOKUP(25, ABI_align8_preserved),
8174 LOOKUP(26, ABI_enum_size),
8175 LOOKUP(27, ABI_HardFP_use),
8176 LOOKUP(28, ABI_VFP_args),
8177 LOOKUP(29, ABI_WMMX_args),
8178 LOOKUP(30, ABI_optimization_goals),
8179 LOOKUP(31, ABI_FP_optimization_goals),
8180 {32, "compatibility", 0, NULL}
8182 #undef LOOKUP
8184 /* Read an unsigned LEB128 encoded value from p. Set *PLEN to the number of
8185 bytes read. */
8186 static unsigned int
8187 read_uleb128 (unsigned char *p, unsigned int *plen)
8189 unsigned char c;
8190 unsigned int val;
8191 int shift;
8192 int len;
8194 val = 0;
8195 shift = 0;
8196 len = 0;
8199 c = *(p++);
8200 len++;
8201 val |= ((unsigned int)c & 0x7f) << shift;
8202 shift += 7;
8204 while (c & 0x80);
8206 *plen = len;
8207 return val;
8210 static unsigned char *
8211 display_arm_attribute (unsigned char *p)
8213 int tag;
8214 unsigned int len;
8215 int val;
8216 arm_attr_public_tag *attr;
8217 unsigned i;
8218 int type;
8220 tag = read_uleb128 (p, &len);
8221 p += len;
8222 attr = NULL;
8223 for (i = 0; i < ARRAY_SIZE(arm_attr_public_tags); i++)
8225 if (arm_attr_public_tags[i].tag == tag)
8227 attr = &arm_attr_public_tags[i];
8228 break;
8232 if (attr)
8234 printf (" Tag_%s: ", attr->name);
8235 switch (attr->type)
8237 case 0:
8238 switch (tag)
8240 case 7: /* Tag_CPU_arch_profile. */
8241 val = read_uleb128 (p, &len);
8242 p += len;
8243 switch (val)
8245 case 0: printf ("None\n"); break;
8246 case 'A': printf ("Application\n"); break;
8247 case 'R': printf ("Realtime\n"); break;
8248 case 'M': printf ("Microcontroller\n"); break;
8249 default: printf ("??? (%d)\n", val); break;
8251 break;
8253 case 32: /* Tag_compatibility. */
8254 val = read_uleb128 (p, &len);
8255 p += len;
8256 printf ("flag = %d, vendor = %s\n", val, p);
8257 p += strlen((char *)p) + 1;
8258 break;
8260 default:
8261 abort();
8263 return p;
8265 case 1:
8266 case 2:
8267 type = attr->type;
8268 break;
8270 default:
8271 assert (attr->type & 0x80);
8272 val = read_uleb128 (p, &len);
8273 p += len;
8274 type = attr->type & 0x7f;
8275 if (val >= type)
8276 printf ("??? (%d)\n", val);
8277 else
8278 printf ("%s\n", attr->table[val]);
8279 return p;
8282 else
8284 if (tag & 1)
8285 type = 1; /* String. */
8286 else
8287 type = 2; /* uleb128. */
8288 printf (" Tag_unknown_%d: ", tag);
8291 if (type == 1)
8293 printf ("\"%s\"\n", p);
8294 p += strlen((char *)p) + 1;
8296 else
8298 val = read_uleb128 (p, &len);
8299 p += len;
8300 printf ("%d (0x%x)\n", val, val);
8303 return p;
8306 static unsigned char *
8307 display_gnu_attribute (unsigned char * p,
8308 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
8310 int tag;
8311 unsigned int len;
8312 int val;
8313 int type;
8315 tag = read_uleb128 (p, &len);
8316 p += len;
8318 /* Tag_compatibility is the only generic GNU attribute defined at
8319 present. */
8320 if (tag == 32)
8322 val = read_uleb128 (p, &len);
8323 p += len;
8324 printf ("flag = %d, vendor = %s\n", val, p);
8325 p += strlen ((char *) p) + 1;
8326 return p;
8329 if ((tag & 2) == 0 && display_proc_gnu_attribute)
8330 return display_proc_gnu_attribute (p, tag);
8332 if (tag & 1)
8333 type = 1; /* String. */
8334 else
8335 type = 2; /* uleb128. */
8336 printf (" Tag_unknown_%d: ", tag);
8338 if (type == 1)
8340 printf ("\"%s\"\n", p);
8341 p += strlen ((char *) p) + 1;
8343 else
8345 val = read_uleb128 (p, &len);
8346 p += len;
8347 printf ("%d (0x%x)\n", val, val);
8350 return p;
8353 static unsigned char *
8354 display_power_gnu_attribute (unsigned char *p, int tag)
8356 int type;
8357 unsigned int len;
8358 int val;
8360 if (tag == Tag_GNU_Power_ABI_FP)
8362 val = read_uleb128 (p, &len);
8363 p += len;
8364 printf (" Tag_GNU_Power_ABI_FP: ");
8366 switch (val)
8368 case 0:
8369 printf ("Hard or soft float\n");
8370 break;
8371 case 1:
8372 printf ("Hard float\n");
8373 break;
8374 case 2:
8375 printf ("Soft float\n");
8376 break;
8377 default:
8378 printf ("??? (%d)\n", val);
8379 break;
8381 return p;
8384 if (tag & 1)
8385 type = 1; /* String. */
8386 else
8387 type = 2; /* uleb128. */
8388 printf (" Tag_unknown_%d: ", tag);
8390 if (type == 1)
8392 printf ("\"%s\"\n", p);
8393 p += strlen ((char *) p) + 1;
8395 else
8397 val = read_uleb128 (p, &len);
8398 p += len;
8399 printf ("%d (0x%x)\n", val, val);
8402 return p;
8405 static unsigned char *
8406 display_mips_gnu_attribute (unsigned char *p, int tag)
8408 int type;
8409 unsigned int len;
8410 int val;
8412 if (tag == Tag_GNU_MIPS_ABI_FP)
8414 val = read_uleb128 (p, &len);
8415 p += len;
8416 printf (" Tag_GNU_MIPS_ABI_FP: ");
8418 switch (val)
8420 case 0:
8421 printf ("Hard or soft float\n");
8422 break;
8423 case 1:
8424 printf ("Hard float (-mdouble-float)\n");
8425 break;
8426 case 2:
8427 printf ("Hard float (-msingle-float)\n");
8428 break;
8429 case 3:
8430 printf ("Soft float\n");
8431 break;
8432 default:
8433 printf ("??? (%d)\n", val);
8434 break;
8436 return p;
8439 if (tag & 1)
8440 type = 1; /* String. */
8441 else
8442 type = 2; /* uleb128. */
8443 printf (" Tag_unknown_%d: ", tag);
8445 if (type == 1)
8447 printf ("\"%s\"\n", p);
8448 p += strlen ((char *) p) + 1;
8450 else
8452 val = read_uleb128 (p, &len);
8453 p += len;
8454 printf ("%d (0x%x)\n", val, val);
8457 return p;
8460 static int
8461 process_attributes (FILE * file,
8462 const char * public_name,
8463 unsigned int proc_type,
8464 unsigned char * (* display_pub_attribute) (unsigned char *),
8465 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
8467 Elf_Internal_Shdr *sect;
8468 unsigned char *contents;
8469 unsigned char *p;
8470 unsigned char *end;
8471 bfd_vma section_len;
8472 bfd_vma len;
8473 unsigned i;
8475 /* Find the section header so that we get the size. */
8476 for (i = 0, sect = section_headers;
8477 i < elf_header.e_shnum;
8478 i++, sect++)
8480 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
8481 continue;
8483 contents = get_data (NULL, file, sect->sh_offset, 1, sect->sh_size,
8484 _("attributes"));
8485 if (contents == NULL)
8486 continue;
8488 p = contents;
8489 if (*p == 'A')
8491 len = sect->sh_size - 1;
8492 p++;
8494 while (len > 0)
8496 int namelen;
8497 bfd_boolean public_section;
8498 bfd_boolean gnu_section;
8500 section_len = byte_get (p, 4);
8501 p += 4;
8503 if (section_len > len)
8505 printf (_("ERROR: Bad section length (%d > %d)\n"),
8506 (int) section_len, (int) len);
8507 section_len = len;
8510 len -= section_len;
8511 printf ("Attribute Section: %s\n", p);
8513 if (public_name && streq ((char *) p, public_name))
8514 public_section = TRUE;
8515 else
8516 public_section = FALSE;
8518 if (streq ((char *) p, "gnu"))
8519 gnu_section = TRUE;
8520 else
8521 gnu_section = FALSE;
8523 namelen = strlen ((char *) p) + 1;
8524 p += namelen;
8525 section_len -= namelen + 4;
8527 while (section_len > 0)
8529 int tag = *(p++);
8530 int val;
8531 bfd_vma size;
8533 size = byte_get (p, 4);
8534 if (size > section_len)
8536 printf (_("ERROR: Bad subsection length (%d > %d)\n"),
8537 (int) size, (int) section_len);
8538 size = section_len;
8541 section_len -= size;
8542 end = p + size - 1;
8543 p += 4;
8545 switch (tag)
8547 case 1:
8548 printf ("File Attributes\n");
8549 break;
8550 case 2:
8551 printf ("Section Attributes:");
8552 goto do_numlist;
8553 case 3:
8554 printf ("Symbol Attributes:");
8555 do_numlist:
8556 for (;;)
8558 unsigned int i;
8560 val = read_uleb128 (p, &i);
8561 p += i;
8562 if (val == 0)
8563 break;
8564 printf (" %d", val);
8566 printf ("\n");
8567 break;
8568 default:
8569 printf ("Unknown tag: %d\n", tag);
8570 public_section = FALSE;
8571 break;
8574 if (public_section)
8576 while (p < end)
8577 p = display_pub_attribute (p);
8579 else if (gnu_section)
8581 while (p < end)
8582 p = display_gnu_attribute (p,
8583 display_proc_gnu_attribute);
8585 else
8587 /* ??? Do something sensible, like dump hex. */
8588 printf (" Unknown section contexts\n");
8589 p = end;
8594 else
8595 printf (_("Unknown format '%c'\n"), *p);
8597 free (contents);
8599 return 1;
8602 static int
8603 process_arm_specific (FILE *file)
8605 return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
8606 display_arm_attribute, NULL);
8609 static int
8610 process_power_specific (FILE *file)
8612 return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
8613 display_power_gnu_attribute);
8616 static int
8617 process_mips_specific (FILE *file)
8619 Elf_Internal_Dyn *entry;
8620 size_t liblist_offset = 0;
8621 size_t liblistno = 0;
8622 size_t conflictsno = 0;
8623 size_t options_offset = 0;
8624 size_t conflicts_offset = 0;
8626 process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
8627 display_mips_gnu_attribute);
8629 /* We have a lot of special sections. Thanks SGI! */
8630 if (dynamic_section == NULL)
8631 /* No information available. */
8632 return 0;
8634 for (entry = dynamic_section; entry->d_tag != DT_NULL; ++entry)
8635 switch (entry->d_tag)
8637 case DT_MIPS_LIBLIST:
8638 liblist_offset
8639 = offset_from_vma (file, entry->d_un.d_val,
8640 liblistno * sizeof (Elf32_External_Lib));
8641 break;
8642 case DT_MIPS_LIBLISTNO:
8643 liblistno = entry->d_un.d_val;
8644 break;
8645 case DT_MIPS_OPTIONS:
8646 options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
8647 break;
8648 case DT_MIPS_CONFLICT:
8649 conflicts_offset
8650 = offset_from_vma (file, entry->d_un.d_val,
8651 conflictsno * sizeof (Elf32_External_Conflict));
8652 break;
8653 case DT_MIPS_CONFLICTNO:
8654 conflictsno = entry->d_un.d_val;
8655 break;
8656 default:
8657 break;
8660 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
8662 Elf32_External_Lib *elib;
8663 size_t cnt;
8665 elib = get_data (NULL, file, liblist_offset,
8666 liblistno, sizeof (Elf32_External_Lib),
8667 _("liblist"));
8668 if (elib)
8670 printf ("\nSection '.liblist' contains %lu entries:\n",
8671 (unsigned long) liblistno);
8672 fputs (" Library Time Stamp Checksum Version Flags\n",
8673 stdout);
8675 for (cnt = 0; cnt < liblistno; ++cnt)
8677 Elf32_Lib liblist;
8678 time_t time;
8679 char timebuf[20];
8680 struct tm *tmp;
8682 liblist.l_name = BYTE_GET (elib[cnt].l_name);
8683 time = BYTE_GET (elib[cnt].l_time_stamp);
8684 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
8685 liblist.l_version = BYTE_GET (elib[cnt].l_version);
8686 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
8688 tmp = gmtime (&time);
8689 snprintf (timebuf, sizeof (timebuf),
8690 "%04u-%02u-%02uT%02u:%02u:%02u",
8691 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
8692 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
8694 printf ("%3lu: ", (unsigned long) cnt);
8695 if (VALID_DYNAMIC_NAME (liblist.l_name))
8696 print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
8697 else
8698 printf ("<corrupt: %9ld>", liblist.l_name);
8699 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
8700 liblist.l_version);
8702 if (liblist.l_flags == 0)
8703 puts (" NONE");
8704 else
8706 static const struct
8708 const char *name;
8709 int bit;
8711 l_flags_vals[] =
8713 { " EXACT_MATCH", LL_EXACT_MATCH },
8714 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
8715 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
8716 { " EXPORTS", LL_EXPORTS },
8717 { " DELAY_LOAD", LL_DELAY_LOAD },
8718 { " DELTA", LL_DELTA }
8720 int flags = liblist.l_flags;
8721 size_t fcnt;
8723 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
8724 if ((flags & l_flags_vals[fcnt].bit) != 0)
8726 fputs (l_flags_vals[fcnt].name, stdout);
8727 flags ^= l_flags_vals[fcnt].bit;
8729 if (flags != 0)
8730 printf (" %#x", (unsigned int) flags);
8732 puts ("");
8736 free (elib);
8740 if (options_offset != 0)
8742 Elf_External_Options *eopt;
8743 Elf_Internal_Shdr *sect = section_headers;
8744 Elf_Internal_Options *iopt;
8745 Elf_Internal_Options *option;
8746 size_t offset;
8747 int cnt;
8749 /* Find the section header so that we get the size. */
8750 while (sect->sh_type != SHT_MIPS_OPTIONS)
8751 ++sect;
8753 eopt = get_data (NULL, file, options_offset, 1, sect->sh_size,
8754 _("options"));
8755 if (eopt)
8757 iopt = cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (*iopt));
8758 if (iopt == NULL)
8760 error (_("Out of memory\n"));
8761 return 0;
8764 offset = cnt = 0;
8765 option = iopt;
8767 while (offset < sect->sh_size)
8769 Elf_External_Options *eoption;
8771 eoption = (Elf_External_Options *) ((char *) eopt + offset);
8773 option->kind = BYTE_GET (eoption->kind);
8774 option->size = BYTE_GET (eoption->size);
8775 option->section = BYTE_GET (eoption->section);
8776 option->info = BYTE_GET (eoption->info);
8778 offset += option->size;
8780 ++option;
8781 ++cnt;
8784 printf (_("\nSection '%s' contains %d entries:\n"),
8785 SECTION_NAME (sect), cnt);
8787 option = iopt;
8789 while (cnt-- > 0)
8791 size_t len;
8793 switch (option->kind)
8795 case ODK_NULL:
8796 /* This shouldn't happen. */
8797 printf (" NULL %d %lx", option->section, option->info);
8798 break;
8799 case ODK_REGINFO:
8800 printf (" REGINFO ");
8801 if (elf_header.e_machine == EM_MIPS)
8803 /* 32bit form. */
8804 Elf32_External_RegInfo *ereg;
8805 Elf32_RegInfo reginfo;
8807 ereg = (Elf32_External_RegInfo *) (option + 1);
8808 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
8809 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
8810 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
8811 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
8812 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
8813 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
8815 printf ("GPR %08lx GP 0x%lx\n",
8816 reginfo.ri_gprmask,
8817 (unsigned long) reginfo.ri_gp_value);
8818 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
8819 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
8820 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
8822 else
8824 /* 64 bit form. */
8825 Elf64_External_RegInfo *ereg;
8826 Elf64_Internal_RegInfo reginfo;
8828 ereg = (Elf64_External_RegInfo *) (option + 1);
8829 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
8830 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
8831 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
8832 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
8833 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
8834 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
8836 printf ("GPR %08lx GP 0x",
8837 reginfo.ri_gprmask);
8838 printf_vma (reginfo.ri_gp_value);
8839 printf ("\n");
8841 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
8842 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
8843 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
8845 ++option;
8846 continue;
8847 case ODK_EXCEPTIONS:
8848 fputs (" EXCEPTIONS fpe_min(", stdout);
8849 process_mips_fpe_exception (option->info & OEX_FPU_MIN);
8850 fputs (") fpe_max(", stdout);
8851 process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
8852 fputs (")", stdout);
8854 if (option->info & OEX_PAGE0)
8855 fputs (" PAGE0", stdout);
8856 if (option->info & OEX_SMM)
8857 fputs (" SMM", stdout);
8858 if (option->info & OEX_FPDBUG)
8859 fputs (" FPDBUG", stdout);
8860 if (option->info & OEX_DISMISS)
8861 fputs (" DISMISS", stdout);
8862 break;
8863 case ODK_PAD:
8864 fputs (" PAD ", stdout);
8865 if (option->info & OPAD_PREFIX)
8866 fputs (" PREFIX", stdout);
8867 if (option->info & OPAD_POSTFIX)
8868 fputs (" POSTFIX", stdout);
8869 if (option->info & OPAD_SYMBOL)
8870 fputs (" SYMBOL", stdout);
8871 break;
8872 case ODK_HWPATCH:
8873 fputs (" HWPATCH ", stdout);
8874 if (option->info & OHW_R4KEOP)
8875 fputs (" R4KEOP", stdout);
8876 if (option->info & OHW_R8KPFETCH)
8877 fputs (" R8KPFETCH", stdout);
8878 if (option->info & OHW_R5KEOP)
8879 fputs (" R5KEOP", stdout);
8880 if (option->info & OHW_R5KCVTL)
8881 fputs (" R5KCVTL", stdout);
8882 break;
8883 case ODK_FILL:
8884 fputs (" FILL ", stdout);
8885 /* XXX Print content of info word? */
8886 break;
8887 case ODK_TAGS:
8888 fputs (" TAGS ", stdout);
8889 /* XXX Print content of info word? */
8890 break;
8891 case ODK_HWAND:
8892 fputs (" HWAND ", stdout);
8893 if (option->info & OHWA0_R4KEOP_CHECKED)
8894 fputs (" R4KEOP_CHECKED", stdout);
8895 if (option->info & OHWA0_R4KEOP_CLEAN)
8896 fputs (" R4KEOP_CLEAN", stdout);
8897 break;
8898 case ODK_HWOR:
8899 fputs (" HWOR ", stdout);
8900 if (option->info & OHWA0_R4KEOP_CHECKED)
8901 fputs (" R4KEOP_CHECKED", stdout);
8902 if (option->info & OHWA0_R4KEOP_CLEAN)
8903 fputs (" R4KEOP_CLEAN", stdout);
8904 break;
8905 case ODK_GP_GROUP:
8906 printf (" GP_GROUP %#06lx self-contained %#06lx",
8907 option->info & OGP_GROUP,
8908 (option->info & OGP_SELF) >> 16);
8909 break;
8910 case ODK_IDENT:
8911 printf (" IDENT %#06lx self-contained %#06lx",
8912 option->info & OGP_GROUP,
8913 (option->info & OGP_SELF) >> 16);
8914 break;
8915 default:
8916 /* This shouldn't happen. */
8917 printf (" %3d ??? %d %lx",
8918 option->kind, option->section, option->info);
8919 break;
8922 len = sizeof (*eopt);
8923 while (len < option->size)
8924 if (((char *) option)[len] >= ' '
8925 && ((char *) option)[len] < 0x7f)
8926 printf ("%c", ((char *) option)[len++]);
8927 else
8928 printf ("\\%03o", ((char *) option)[len++]);
8930 fputs ("\n", stdout);
8931 ++option;
8934 free (eopt);
8938 if (conflicts_offset != 0 && conflictsno != 0)
8940 Elf32_Conflict *iconf;
8941 size_t cnt;
8943 if (dynamic_symbols == NULL)
8945 error (_("conflict list found without a dynamic symbol table\n"));
8946 return 0;
8949 iconf = cmalloc (conflictsno, sizeof (*iconf));
8950 if (iconf == NULL)
8952 error (_("Out of memory\n"));
8953 return 0;
8956 if (is_32bit_elf)
8958 Elf32_External_Conflict *econf32;
8960 econf32 = get_data (NULL, file, conflicts_offset,
8961 conflictsno, sizeof (*econf32), _("conflict"));
8962 if (!econf32)
8963 return 0;
8965 for (cnt = 0; cnt < conflictsno; ++cnt)
8966 iconf[cnt] = BYTE_GET (econf32[cnt]);
8968 free (econf32);
8970 else
8972 Elf64_External_Conflict *econf64;
8974 econf64 = get_data (NULL, file, conflicts_offset,
8975 conflictsno, sizeof (*econf64), _("conflict"));
8976 if (!econf64)
8977 return 0;
8979 for (cnt = 0; cnt < conflictsno; ++cnt)
8980 iconf[cnt] = BYTE_GET (econf64[cnt]);
8982 free (econf64);
8985 printf (_("\nSection '.conflict' contains %lu entries:\n"),
8986 (unsigned long) conflictsno);
8987 puts (_(" Num: Index Value Name"));
8989 for (cnt = 0; cnt < conflictsno; ++cnt)
8991 Elf_Internal_Sym *psym = & dynamic_symbols[iconf[cnt]];
8993 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
8994 print_vma (psym->st_value, FULL_HEX);
8995 putchar (' ');
8996 if (VALID_DYNAMIC_NAME (psym->st_name))
8997 print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
8998 else
8999 printf ("<corrupt: %14ld>", psym->st_name);
9000 putchar ('\n');
9003 free (iconf);
9006 return 1;
9009 static int
9010 process_gnu_liblist (FILE *file)
9012 Elf_Internal_Shdr *section, *string_sec;
9013 Elf32_External_Lib *elib;
9014 char *strtab;
9015 size_t strtab_size;
9016 size_t cnt;
9017 unsigned i;
9019 if (! do_arch)
9020 return 0;
9022 for (i = 0, section = section_headers;
9023 i < elf_header.e_shnum;
9024 i++, section++)
9026 switch (section->sh_type)
9028 case SHT_GNU_LIBLIST:
9029 if (SECTION_HEADER_INDEX (section->sh_link) >= elf_header.e_shnum)
9030 break;
9032 elib = get_data (NULL, file, section->sh_offset, 1, section->sh_size,
9033 _("liblist"));
9035 if (elib == NULL)
9036 break;
9037 string_sec = SECTION_HEADER (section->sh_link);
9039 strtab = get_data (NULL, file, string_sec->sh_offset, 1,
9040 string_sec->sh_size, _("liblist string table"));
9041 strtab_size = string_sec->sh_size;
9043 if (strtab == NULL
9044 || section->sh_entsize != sizeof (Elf32_External_Lib))
9046 free (elib);
9047 break;
9050 printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
9051 SECTION_NAME (section),
9052 (long) (section->sh_size / sizeof (Elf32_External_Lib)));
9054 puts (" Library Time Stamp Checksum Version Flags");
9056 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
9057 ++cnt)
9059 Elf32_Lib liblist;
9060 time_t time;
9061 char timebuf[20];
9062 struct tm *tmp;
9064 liblist.l_name = BYTE_GET (elib[cnt].l_name);
9065 time = BYTE_GET (elib[cnt].l_time_stamp);
9066 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
9067 liblist.l_version = BYTE_GET (elib[cnt].l_version);
9068 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
9070 tmp = gmtime (&time);
9071 snprintf (timebuf, sizeof (timebuf),
9072 "%04u-%02u-%02uT%02u:%02u:%02u",
9073 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9074 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9076 printf ("%3lu: ", (unsigned long) cnt);
9077 if (do_wide)
9078 printf ("%-20s", liblist.l_name < strtab_size
9079 ? strtab + liblist.l_name : "<corrupt>");
9080 else
9081 printf ("%-20.20s", liblist.l_name < strtab_size
9082 ? strtab + liblist.l_name : "<corrupt>");
9083 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
9084 liblist.l_version, liblist.l_flags);
9087 free (elib);
9091 return 1;
9094 static const char *
9095 get_note_type (unsigned e_type)
9097 static char buff[64];
9099 if (elf_header.e_type == ET_CORE)
9100 switch (e_type)
9102 case NT_AUXV:
9103 return _("NT_AUXV (auxiliary vector)");
9104 case NT_PRSTATUS:
9105 return _("NT_PRSTATUS (prstatus structure)");
9106 case NT_FPREGSET:
9107 return _("NT_FPREGSET (floating point registers)");
9108 case NT_PRPSINFO:
9109 return _("NT_PRPSINFO (prpsinfo structure)");
9110 case NT_TASKSTRUCT:
9111 return _("NT_TASKSTRUCT (task structure)");
9112 case NT_PRXFPREG:
9113 return _("NT_PRXFPREG (user_xfpregs structure)");
9114 case NT_PSTATUS:
9115 return _("NT_PSTATUS (pstatus structure)");
9116 case NT_FPREGS:
9117 return _("NT_FPREGS (floating point registers)");
9118 case NT_PSINFO:
9119 return _("NT_PSINFO (psinfo structure)");
9120 case NT_LWPSTATUS:
9121 return _("NT_LWPSTATUS (lwpstatus_t structure)");
9122 case NT_LWPSINFO:
9123 return _("NT_LWPSINFO (lwpsinfo_t structure)");
9124 case NT_WIN32PSTATUS:
9125 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
9126 default:
9127 break;
9129 else
9130 switch (e_type)
9132 case NT_VERSION:
9133 return _("NT_VERSION (version)");
9134 case NT_ARCH:
9135 return _("NT_ARCH (architecture)");
9136 default:
9137 break;
9140 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
9141 return buff;
9144 static const char *
9145 get_gnu_elf_note_type (unsigned e_type)
9147 static char buff[64];
9149 switch (e_type)
9151 case NT_GNU_ABI_TAG:
9152 return _("NT_GNU_ABI_TAG (ABI version tag)");
9153 case NT_GNU_HWCAP:
9154 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
9155 case NT_GNU_BUILD_ID:
9156 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
9157 default:
9158 break;
9161 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
9162 return buff;
9165 static const char *
9166 get_netbsd_elfcore_note_type (unsigned e_type)
9168 static char buff[64];
9170 if (e_type == NT_NETBSDCORE_PROCINFO)
9172 /* NetBSD core "procinfo" structure. */
9173 return _("NetBSD procinfo structure");
9176 /* As of Jan 2002 there are no other machine-independent notes
9177 defined for NetBSD core files. If the note type is less
9178 than the start of the machine-dependent note types, we don't
9179 understand it. */
9181 if (e_type < NT_NETBSDCORE_FIRSTMACH)
9183 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
9184 return buff;
9187 switch (elf_header.e_machine)
9189 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
9190 and PT_GETFPREGS == mach+2. */
9192 case EM_OLD_ALPHA:
9193 case EM_ALPHA:
9194 case EM_SPARC:
9195 case EM_SPARC32PLUS:
9196 case EM_SPARCV9:
9197 switch (e_type)
9199 case NT_NETBSDCORE_FIRSTMACH+0:
9200 return _("PT_GETREGS (reg structure)");
9201 case NT_NETBSDCORE_FIRSTMACH+2:
9202 return _("PT_GETFPREGS (fpreg structure)");
9203 default:
9204 break;
9206 break;
9208 /* On all other arch's, PT_GETREGS == mach+1 and
9209 PT_GETFPREGS == mach+3. */
9210 default:
9211 switch (e_type)
9213 case NT_NETBSDCORE_FIRSTMACH+1:
9214 return _("PT_GETREGS (reg structure)");
9215 case NT_NETBSDCORE_FIRSTMACH+3:
9216 return _("PT_GETFPREGS (fpreg structure)");
9217 default:
9218 break;
9222 snprintf (buff, sizeof (buff), _("PT_FIRSTMACH+%d"),
9223 e_type - NT_NETBSDCORE_FIRSTMACH);
9224 return buff;
9227 /* Note that by the ELF standard, the name field is already null byte
9228 terminated, and namesz includes the terminating null byte.
9229 I.E. the value of namesz for the name "FSF" is 4.
9231 If the value of namesz is zero, there is no name present. */
9232 static int
9233 process_note (Elf_Internal_Note *pnote)
9235 const char *name = pnote->namesz ? pnote->namedata : "(NONE)";
9236 const char *nt;
9238 if (pnote->namesz == 0)
9239 /* If there is no note name, then use the default set of
9240 note type strings. */
9241 nt = get_note_type (pnote->type);
9243 else if (const_strneq (pnote->namedata, "GNU"))
9244 /* GNU-specific object file notes. */
9245 nt = get_gnu_elf_note_type (pnote->type);
9247 else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
9248 /* NetBSD-specific core file notes. */
9249 nt = get_netbsd_elfcore_note_type (pnote->type);
9251 else if (strneq (pnote->namedata, "SPU/", 4))
9253 /* SPU-specific core file notes. */
9254 nt = pnote->namedata + 4;
9255 name = "SPU";
9258 else
9259 /* Don't recognize this note name; just use the default set of
9260 note type strings. */
9261 nt = get_note_type (pnote->type);
9263 printf (" %s\t\t0x%08lx\t%s\n", name, pnote->descsz, nt);
9264 return 1;
9268 static int
9269 process_corefile_note_segment (FILE *file, bfd_vma offset, bfd_vma length)
9271 Elf_External_Note *pnotes;
9272 Elf_External_Note *external;
9273 int res = 1;
9275 if (length <= 0)
9276 return 0;
9278 pnotes = get_data (NULL, file, offset, 1, length, _("notes"));
9279 if (!pnotes)
9280 return 0;
9282 external = pnotes;
9284 printf (_("\nNotes at offset 0x%08lx with length 0x%08lx:\n"),
9285 (unsigned long) offset, (unsigned long) length);
9286 printf (_(" Owner\t\tData size\tDescription\n"));
9288 while (external < (Elf_External_Note *)((char *) pnotes + length))
9290 Elf_External_Note *next;
9291 Elf_Internal_Note inote;
9292 char *temp = NULL;
9294 inote.type = BYTE_GET (external->type);
9295 inote.namesz = BYTE_GET (external->namesz);
9296 inote.namedata = external->name;
9297 inote.descsz = BYTE_GET (external->descsz);
9298 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
9299 inote.descpos = offset + (inote.descdata - (char *) pnotes);
9301 next = (Elf_External_Note *)(inote.descdata + align_power (inote.descsz, 2));
9303 if (((char *) next) > (((char *) pnotes) + length))
9305 warn (_("corrupt note found at offset %lx into core notes\n"),
9306 (long)((char *)external - (char *)pnotes));
9307 warn (_(" type: %lx, namesize: %08lx, descsize: %08lx\n"),
9308 inote.type, inote.namesz, inote.descsz);
9309 break;
9312 external = next;
9314 /* Verify that name is null terminated. It appears that at least
9315 one version of Linux (RedHat 6.0) generates corefiles that don't
9316 comply with the ELF spec by failing to include the null byte in
9317 namesz. */
9318 if (inote.namedata[inote.namesz] != '\0')
9320 temp = malloc (inote.namesz + 1);
9322 if (temp == NULL)
9324 error (_("Out of memory\n"));
9325 res = 0;
9326 break;
9329 strncpy (temp, inote.namedata, inote.namesz);
9330 temp[inote.namesz] = 0;
9332 /* warn (_("'%s' NOTE name not properly null terminated\n"), temp); */
9333 inote.namedata = temp;
9336 res &= process_note (& inote);
9338 if (temp != NULL)
9340 free (temp);
9341 temp = NULL;
9345 free (pnotes);
9347 return res;
9350 static int
9351 process_corefile_note_segments (FILE *file)
9353 Elf_Internal_Phdr *segment;
9354 unsigned int i;
9355 int res = 1;
9357 if (! get_program_headers (file))
9358 return 0;
9360 for (i = 0, segment = program_headers;
9361 i < elf_header.e_phnum;
9362 i++, segment++)
9364 if (segment->p_type == PT_NOTE)
9365 res &= process_corefile_note_segment (file,
9366 (bfd_vma) segment->p_offset,
9367 (bfd_vma) segment->p_filesz);
9370 return res;
9373 static int
9374 process_note_sections (FILE *file)
9376 Elf_Internal_Shdr *section;
9377 unsigned long i;
9378 int res = 1;
9380 for (i = 0, section = section_headers;
9381 i < elf_header.e_shnum;
9382 i++, section++)
9383 if (section->sh_type == SHT_NOTE)
9384 res &= process_corefile_note_segment (file,
9385 (bfd_vma) section->sh_offset,
9386 (bfd_vma) section->sh_size);
9388 return res;
9391 static int
9392 process_notes (FILE *file)
9394 /* If we have not been asked to display the notes then do nothing. */
9395 if (! do_notes)
9396 return 1;
9398 if (elf_header.e_type != ET_CORE)
9399 return process_note_sections (file);
9401 /* No program headers means no NOTE segment. */
9402 if (elf_header.e_phnum > 0)
9403 return process_corefile_note_segments (file);
9405 printf (_("No note segments present in the core file.\n"));
9406 return 1;
9409 static int
9410 process_arch_specific (FILE *file)
9412 if (! do_arch)
9413 return 1;
9415 switch (elf_header.e_machine)
9417 case EM_ARM:
9418 return process_arm_specific (file);
9419 case EM_MIPS:
9420 case EM_MIPS_RS3_LE:
9421 return process_mips_specific (file);
9422 break;
9423 case EM_PPC:
9424 return process_power_specific (file);
9425 break;
9426 default:
9427 break;
9429 return 1;
9432 static int
9433 get_file_header (FILE *file)
9435 /* Read in the identity array. */
9436 if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
9437 return 0;
9439 /* Determine how to read the rest of the header. */
9440 switch (elf_header.e_ident[EI_DATA])
9442 default: /* fall through */
9443 case ELFDATANONE: /* fall through */
9444 case ELFDATA2LSB:
9445 byte_get = byte_get_little_endian;
9446 byte_put = byte_put_little_endian;
9447 break;
9448 case ELFDATA2MSB:
9449 byte_get = byte_get_big_endian;
9450 byte_put = byte_put_big_endian;
9451 break;
9454 /* For now we only support 32 bit and 64 bit ELF files. */
9455 is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
9457 /* Read in the rest of the header. */
9458 if (is_32bit_elf)
9460 Elf32_External_Ehdr ehdr32;
9462 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
9463 return 0;
9465 elf_header.e_type = BYTE_GET (ehdr32.e_type);
9466 elf_header.e_machine = BYTE_GET (ehdr32.e_machine);
9467 elf_header.e_version = BYTE_GET (ehdr32.e_version);
9468 elf_header.e_entry = BYTE_GET (ehdr32.e_entry);
9469 elf_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
9470 elf_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
9471 elf_header.e_flags = BYTE_GET (ehdr32.e_flags);
9472 elf_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
9473 elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
9474 elf_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
9475 elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
9476 elf_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
9477 elf_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
9479 else
9481 Elf64_External_Ehdr ehdr64;
9483 /* If we have been compiled with sizeof (bfd_vma) == 4, then
9484 we will not be able to cope with the 64bit data found in
9485 64 ELF files. Detect this now and abort before we start
9486 overwriting things. */
9487 if (sizeof (bfd_vma) < 8)
9489 error (_("This instance of readelf has been built without support for a\n\
9490 64 bit data type and so it cannot read 64 bit ELF files.\n"));
9491 return 0;
9494 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
9495 return 0;
9497 elf_header.e_type = BYTE_GET (ehdr64.e_type);
9498 elf_header.e_machine = BYTE_GET (ehdr64.e_machine);
9499 elf_header.e_version = BYTE_GET (ehdr64.e_version);
9500 elf_header.e_entry = BYTE_GET (ehdr64.e_entry);
9501 elf_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
9502 elf_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
9503 elf_header.e_flags = BYTE_GET (ehdr64.e_flags);
9504 elf_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
9505 elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
9506 elf_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
9507 elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
9508 elf_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
9509 elf_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
9512 if (elf_header.e_shoff)
9514 /* There may be some extensions in the first section header. Don't
9515 bomb if we can't read it. */
9516 if (is_32bit_elf)
9517 get_32bit_section_headers (file, 1);
9518 else
9519 get_64bit_section_headers (file, 1);
9522 is_relocatable = elf_header.e_type == ET_REL;
9524 return 1;
9527 /* Process one ELF object file according to the command line options.
9528 This file may actually be stored in an archive. The file is
9529 positioned at the start of the ELF object. */
9531 static int
9532 process_object (char *file_name, FILE *file)
9534 unsigned int i;
9536 if (! get_file_header (file))
9538 error (_("%s: Failed to read file header\n"), file_name);
9539 return 1;
9542 /* Initialise per file variables. */
9543 for (i = ARRAY_SIZE (version_info); i--;)
9544 version_info[i] = 0;
9546 for (i = ARRAY_SIZE (dynamic_info); i--;)
9547 dynamic_info[i] = 0;
9549 /* Process the file. */
9550 if (show_name)
9551 printf (_("\nFile: %s\n"), file_name);
9553 /* Initialise the dump_sects array from the cmdline_dump_sects array.
9554 Note we do this even if cmdline_dump_sects is empty because we
9555 must make sure that the dump_sets array is zeroed out before each
9556 object file is processed. */
9557 if (num_dump_sects > num_cmdline_dump_sects)
9558 memset (dump_sects, 0, num_dump_sects);
9560 if (num_cmdline_dump_sects > 0)
9562 if (num_dump_sects == 0)
9563 /* A sneaky way of allocating the dump_sects array. */
9564 request_dump (num_cmdline_dump_sects, 0);
9566 assert (num_dump_sects >= num_cmdline_dump_sects);
9567 memcpy (dump_sects, cmdline_dump_sects, num_cmdline_dump_sects);
9570 if (! process_file_header ())
9571 return 1;
9573 if (! process_section_headers (file))
9575 /* Without loaded section headers we cannot process lots of
9576 things. */
9577 do_unwind = do_version = do_dump = do_arch = 0;
9579 if (! do_using_dynamic)
9580 do_syms = do_reloc = 0;
9583 if (! process_section_groups (file))
9585 /* Without loaded section groups we cannot process unwind. */
9586 do_unwind = 0;
9589 if (process_program_headers (file))
9590 process_dynamic_section (file);
9592 process_relocs (file);
9594 process_unwind (file);
9596 process_symbol_table (file);
9598 process_syminfo (file);
9600 process_version_sections (file);
9602 process_section_contents (file);
9604 process_notes (file);
9606 process_gnu_liblist (file);
9608 process_arch_specific (file);
9610 if (program_headers)
9612 free (program_headers);
9613 program_headers = NULL;
9616 if (section_headers)
9618 free (section_headers);
9619 section_headers = NULL;
9622 if (string_table)
9624 free (string_table);
9625 string_table = NULL;
9626 string_table_length = 0;
9629 if (dynamic_strings)
9631 free (dynamic_strings);
9632 dynamic_strings = NULL;
9633 dynamic_strings_length = 0;
9636 if (dynamic_symbols)
9638 free (dynamic_symbols);
9639 dynamic_symbols = NULL;
9640 num_dynamic_syms = 0;
9643 if (dynamic_syminfo)
9645 free (dynamic_syminfo);
9646 dynamic_syminfo = NULL;
9649 if (section_headers_groups)
9651 free (section_headers_groups);
9652 section_headers_groups = NULL;
9655 if (section_groups)
9657 struct group_list *g, *next;
9659 for (i = 0; i < group_count; i++)
9661 for (g = section_groups [i].root; g != NULL; g = next)
9663 next = g->next;
9664 free (g);
9668 free (section_groups);
9669 section_groups = NULL;
9672 free_debug_memory ();
9674 return 0;
9677 /* Process an ELF archive. The file is positioned just after the
9678 ARMAG string. */
9680 static int
9681 process_archive (char *file_name, FILE *file)
9683 struct ar_hdr arhdr;
9684 size_t got;
9685 unsigned long size;
9686 char *longnames = NULL;
9687 unsigned long longnames_size = 0;
9688 size_t file_name_size;
9689 int ret;
9691 show_name = 1;
9693 got = fread (&arhdr, 1, sizeof arhdr, file);
9694 if (got != sizeof arhdr)
9696 if (got == 0)
9697 return 0;
9699 error (_("%s: failed to read archive header\n"), file_name);
9700 return 1;
9703 if (const_strneq (arhdr.ar_name, "/ ")
9704 || const_strneq (arhdr.ar_name, "/SYM64/ "))
9706 /* This is the archive symbol table. Skip it.
9707 FIXME: We should have an option to dump it. */
9708 size = strtoul (arhdr.ar_size, NULL, 10);
9709 if (fseek (file, size + (size & 1), SEEK_CUR) != 0)
9711 error (_("%s: failed to skip archive symbol table\n"), file_name);
9712 return 1;
9715 got = fread (&arhdr, 1, sizeof arhdr, file);
9716 if (got != sizeof arhdr)
9718 if (got == 0)
9719 return 0;
9721 error (_("%s: failed to read archive header\n"), file_name);
9722 return 1;
9726 if (const_strneq (arhdr.ar_name, "// "))
9728 /* This is the archive string table holding long member
9729 names. */
9731 longnames_size = strtoul (arhdr.ar_size, NULL, 10);
9733 longnames = malloc (longnames_size);
9734 if (longnames == NULL)
9736 error (_("Out of memory\n"));
9737 return 1;
9740 if (fread (longnames, longnames_size, 1, file) != 1)
9742 free (longnames);
9743 error (_("%s: failed to read string table\n"), file_name);
9744 return 1;
9747 if ((longnames_size & 1) != 0)
9748 getc (file);
9750 got = fread (&arhdr, 1, sizeof arhdr, file);
9751 if (got != sizeof arhdr)
9753 free (longnames);
9755 if (got == 0)
9756 return 0;
9758 error (_("%s: failed to read archive header\n"), file_name);
9759 return 1;
9763 file_name_size = strlen (file_name);
9764 ret = 0;
9766 while (1)
9768 char *name;
9769 char *nameend;
9770 char *namealc;
9772 if (arhdr.ar_name[0] == '/')
9774 unsigned long off;
9776 off = strtoul (arhdr.ar_name + 1, NULL, 10);
9777 if (off >= longnames_size)
9779 error (_("%s: invalid archive string table offset %lu\n"), file_name, off);
9780 ret = 1;
9781 break;
9784 name = longnames + off;
9785 nameend = memchr (name, '/', longnames_size - off);
9787 else
9789 name = arhdr.ar_name;
9790 nameend = memchr (name, '/', 16);
9793 if (nameend == NULL)
9795 error (_("%s: bad archive file name\n"), file_name);
9796 ret = 1;
9797 break;
9800 namealc = malloc (file_name_size + (nameend - name) + 3);
9801 if (namealc == NULL)
9803 error (_("Out of memory\n"));
9804 ret = 1;
9805 break;
9808 memcpy (namealc, file_name, file_name_size);
9809 namealc[file_name_size] = '(';
9810 memcpy (namealc + file_name_size + 1, name, nameend - name);
9811 namealc[file_name_size + 1 + (nameend - name)] = ')';
9812 namealc[file_name_size + 2 + (nameend - name)] = '\0';
9814 archive_file_offset = ftell (file);
9815 archive_file_size = strtoul (arhdr.ar_size, NULL, 10);
9817 ret |= process_object (namealc, file);
9819 free (namealc);
9821 if (fseek (file,
9822 (archive_file_offset
9823 + archive_file_size
9824 + (archive_file_size & 1)),
9825 SEEK_SET) != 0)
9827 error (_("%s: failed to seek to next archive header\n"), file_name);
9828 ret = 1;
9829 break;
9832 got = fread (&arhdr, 1, sizeof arhdr, file);
9833 if (got != sizeof arhdr)
9835 if (got == 0)
9836 break;
9838 error (_("%s: failed to read archive header\n"), file_name);
9839 ret = 1;
9840 break;
9844 if (longnames != 0)
9845 free (longnames);
9847 return ret;
9850 static int
9851 process_file (char *file_name)
9853 FILE *file;
9854 struct stat statbuf;
9855 char armag[SARMAG];
9856 int ret;
9858 if (stat (file_name, &statbuf) < 0)
9860 if (errno == ENOENT)
9861 error (_("'%s': No such file\n"), file_name);
9862 else
9863 error (_("Could not locate '%s'. System error message: %s\n"),
9864 file_name, strerror (errno));
9865 return 1;
9868 if (! S_ISREG (statbuf.st_mode))
9870 error (_("'%s' is not an ordinary file\n"), file_name);
9871 return 1;
9874 file = fopen (file_name, "rb");
9875 if (file == NULL)
9877 error (_("Input file '%s' is not readable.\n"), file_name);
9878 return 1;
9881 if (fread (armag, SARMAG, 1, file) != 1)
9883 error (_("%s: Failed to read file header\n"), file_name);
9884 fclose (file);
9885 return 1;
9888 if (memcmp (armag, ARMAG, SARMAG) == 0)
9889 ret = process_archive (file_name, file);
9890 else
9892 rewind (file);
9893 archive_file_size = archive_file_offset = 0;
9894 ret = process_object (file_name, file);
9897 fclose (file);
9899 return ret;
9902 #ifdef SUPPORT_DISASSEMBLY
9903 /* Needed by the i386 disassembler. For extra credit, someone could
9904 fix this so that we insert symbolic addresses here, esp for GOT/PLT
9905 symbols. */
9907 void
9908 print_address (unsigned int addr, FILE *outfile)
9910 fprintf (outfile,"0x%8.8x", addr);
9913 /* Needed by the i386 disassembler. */
9914 void
9915 db_task_printsym (unsigned int addr)
9917 print_address (addr, stderr);
9919 #endif
9922 main (int argc, char **argv)
9924 int err;
9926 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
9927 setlocale (LC_MESSAGES, "");
9928 #endif
9929 #if defined (HAVE_SETLOCALE)
9930 setlocale (LC_CTYPE, "");
9931 #endif
9932 bindtextdomain (PACKAGE, LOCALEDIR);
9933 textdomain (PACKAGE);
9935 expandargv (&argc, &argv);
9937 parse_args (argc, argv);
9939 if (num_dump_sects > 0)
9941 /* Make a copy of the dump_sects array. */
9942 cmdline_dump_sects = malloc (num_dump_sects);
9943 if (cmdline_dump_sects == NULL)
9944 error (_("Out of memory allocating dump request table.\n"));
9945 else
9947 memcpy (cmdline_dump_sects, dump_sects, num_dump_sects);
9948 num_cmdline_dump_sects = num_dump_sects;
9952 if (optind < (argc - 1))
9953 show_name = 1;
9955 err = 0;
9956 while (optind < argc)
9957 err |= process_file (argv[optind++]);
9959 if (dump_sects != NULL)
9960 free (dump_sects);
9961 if (cmdline_dump_sects != NULL)
9962 free (cmdline_dump_sects);
9964 return err;