/* readelf.c -- display contents of an ELF format file Copyright (C) 1998 Free Software Foundation, Inc. Originally developed by Eric Youngdale Modifications by Nick Clifton This file is part of GNU Binutils. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include "readelf.h" #include "bucomm.h" #include "getopt.h" #ifdef ANSI_PROTOTYPES #include #else #include #endif unsigned int dynamic_addr; unsigned int dynamic_size; char * dynamic_strings; Elf_Sym * dynamic_symbols; char program_interpreter [64]; char * program_name = "readelf"; int dynamic_info [DT_JMPREL + 1]; int version_info [16]; int must_swap = 0; int loadaddr = 0; unsigned int rela_addr; unsigned int rela_size; Elf_Ehdr elf_header; Elf_Shdr * section_headers; char * string_table; Elf_Dyn * dpnt; Elf_Rel * rpnt; int show_name; int do_dynamic; int do_syms; int do_reloc; int do_sections; int do_segments; int do_using_dynamic; int do_header; int do_dump; int do_version; long int expected_endian; #define SECTION_NAME(X) (string_table + (X)->sh_name) #define NUM_DUMP_SECTS 100 char dump_sects [NUM_DUMP_SECTS]; #define HEX_DUMP 1 #define DISASS_DUMP 2 /* Forward declarations for dumb compilers. */ static char * get_dynamic_type PARAMS ((unsigned long)); static char * get_i386_rel_type PARAMS ((bfd_vma rtype)); static char * get_m68k_rel_type PARAMS ((bfd_vma rtype)); static char * get_sparc_rel_type PARAMS ((bfd_vma rtype)); static char * get_m32r_rel_type PARAMS ((bfd_vma rtype)); static char * get_v850_rel_type PARAMS ((bfd_vma rtype)); static char * get_d10v_rel_type PARAMS ((bfd_vma rtype)); /* start-sanitize-d30v */ static char * get_d30v_rel_type PARAMS ((bfd_vma rtype)); /* end-sanitize-d30v */ static char * get_sh_rel_type PARAMS ((bfd_vma rtype)); static char * get_mn10300_rel_type PARAMS ((bfd_vma rtype)); static char * get_mn10200_rel_type PARAMS ((bfd_vma rtype)); static char * get_ppc_rel_type PARAMS ((bfd_vma rtype)); static void dump_relocations PARAMS ((FILE *, unsigned long, unsigned long, Elf_Sym *, char *)); static char * get_file_type PARAMS ((unsigned e_type)); static char * get_machine_name PARAMS ((unsigned e_machine)); static char * get_machine_data PARAMS ((unsigned e_data)); static char * get_machine_flags PARAMS ((unsigned, unsigned e_machine)); static char * get_segment_type PARAMS ((unsigned long p_type)); static char * get_section_type_name PARAMS ((unsigned int sh_type)); static char * get_symbol_binding PARAMS ((unsigned int binding)); static char * get_symbol_type PARAMS ((unsigned int type)); static void usage PARAMS ((void)); static void parse_args PARAMS ((int argc, char ** argv)); static int process_file_header PARAMS ((void)); static void process_program_headers PARAMS ((FILE *)); static void process_section_headers PARAMS ((FILE *)); static void process_dynamic_segment PARAMS ((FILE *)); static void process_symbol_table PARAMS ((FILE *)); static void process_section_contents PARAMS ((FILE *)); static void process_file PARAMS ((char * file_name)); static void process_relocs PARAMS ((FILE *)); static void process_version_sections PARAMS ((FILE *)); static char * get_ver_flags PARAMS ((unsigned short flags)); static char * get_symbol_index_type PARAMS ((unsigned short type)); #define NUM_ELEM(array) (sizeof (array) / sizeof ((array)[0])) #define SWAP2(val) ( (((val) << 8) & (0xff << 8)) \ | (((val) >> 8) & (0xff << 0))) #define SWAP4(val) ( (((val) << 24) & (0xff << 24)) \ | (((val) << 8) & (0xff << 16)) \ | (((val) >> 8) & (0xff << 8)) \ | (((val) >> 24) & (0xff << 0))) /* Warning: This macro assumes 8 bits in a char, and at least 16 bits in a short, and at least 32 bits in a long. */ #define BYTE_SWAP(structure, field) \ if (sizeof ((structure).field) == 2) \ { \ unsigned short val = (structure).field; \ (structure).field = SWAP2 (val); \ } \ else if (sizeof ((structure).field) != 4) \ abort (); \ else \ { \ unsigned long val = (structure).field; \ (structure).field = SWAP4 (val); \ } #define GET_DATA_ALLOC(offset, size, var, type, reason) \ if (fseek (file, offset, SEEK_SET)) \ { \ error (_("Unable to seek to start of %s at %x\n"), reason, offset); \ return; \ } \ \ var = (type) malloc (size); \ \ if (var == NULL) \ { \ error (_("Out of memory allocating %d bytes for %s\n"), size, reason); \ return; \ } \ \ if (fread (var, size, 1, file) != 1) \ { \ error (_("Unable to read in %d bytes of %s\n"), size, reason); \ free (var); \ var = NULL; \ return; \ } #define GET_DATA(offset, var, reason) \ if (fseek (file, offset, SEEK_SET)) \ { \ error (_("Unable to seek to %x for %s\n"), offset, reason); \ return; \ } \ else if (fread (& var, sizeof (var), 1, file) != 1) \ { \ error (_("Unable to read data at %x for %s\n"), offset, reason); \ return; \ } #ifdef ANSI_PROTOTYPES static void error (const char * message, ...) { va_list args; fprintf (stderr, _("%s: Error: "), program_name); va_start (args, message); vfprintf (stderr, message, args); va_end (args); return; } static void warn (const char * message, ...) { va_list args; fprintf (stderr, _("%s: Warning: "), program_name); va_start (args, message); vfprintf (stderr, message, args); va_end (args); return; } #else static void error (va_alist) { char * message; va_list args; fprintf (stderr, _("%s: Error: "), program_name); va_start (args); message = va_arg (args, char *); vfprintf (stderr, message, args); va_end (args); return; } static void warn (va_alist) va_dcl; { char * message; va_list args; fprintf (stderr, _("%s: Warning: "), program_name); va_start (args); message = va_arg (args, char *); vfprintf (stderr, message, args); va_end (args); return; } #endif static char * get_i386_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_386_NONE"; case 1: return "R_386_32"; case 2: return "R_386_PC32"; case 3: return "R_386_GOT32"; case 4: return "R_386_PLT32"; case 5: return "R_386_COPY"; case 6: return "R_386_GLOB_DAT"; case 7: return "R_386_JMP_SLOT"; case 8: return "R_386_RELATIVE"; case 9: return "R_386_GOTOFF"; case 10: return "R_386_GOTPC"; case 20: return "R_386_16"; case 21: return "R_386_PC16"; case 22: return "R_386_PC8"; case 23: return "R_386_max"; default: return _("*INVALID*"); } } static char * get_m68k_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_68K_NONE"; case 1: return "R_68K_32"; case 2: return "R_68K_16"; case 3: return "R_68K_8"; case 4: return "R_68K_PC32"; case 5: return "R_68K_PC16"; case 6: return "R_68K_PC8"; case 7: return "R_68K_GOT32"; case 8: return "R_68K_GOT16"; case 9: return "R_68K_GOT8"; case 10: return "R_68K_GOT32O"; case 11: return "R_68K_GOT16O"; case 12: return "R_68K_GOT8O"; case 13: return "R_68K_PLT32"; case 14: return "R_68K_PLT16"; case 15: return "R_68K_PLT8"; case 16: return "R_68K_PLT32O"; case 17: return "R_68K_PLT16O"; case 18: return "R_68K_PLT8O"; case 19: return "R_68K_COPY"; case 20: return "R_68K_GLOB_DAT"; case 21: return "R_68K_JMP_SLOT"; case 22: return "R_68K_RELATIVE"; default: return _("*INVALID*"); } } static char * get_sparc_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_SPARC_NONE"; case 1: return "R_SPARC_8"; case 2: return "R_SPARC_16"; case 3: return "R_SPARC_32"; case 4: return "R_SPARC_DISP8"; case 5: return "R_SPARC_DISP16"; case 6: return "R_SPARC_DISP32"; case 7: return "R_SPARC_WDISP30"; case 8: return "R_SPARC_WDISP22"; case 9: return "R_SPARC_HI22"; case 10: return "R_SPARC_22"; case 11: return "R_SPARC_13"; case 12: return "R_SPARC_LO10"; case 13: return "R_SPARC_GOT10"; case 14: return "R_SPARC_GOT13"; case 15: return "R_SPARC_GOT22"; case 16: return "R_SPARC_PC10"; case 17: return "R_SPARC_PC22"; case 18: return "R_SPARC_WPLT30"; case 19: return "R_SPARC_COPY"; case 20: return "R_SPARC_GLOB_DAT"; case 21: return "R_SPARC_JMP_SLOT"; case 22: return "R_SPARC_RELATIVE"; case 23: return "R_SPARC_UA32"; case 24: return "R_SPARC_10"; case 25: return "R_SPARC_11"; case 26: return "R_SPARC_64"; case 27: return "R_SPARC_OLO10"; case 28: return "R_SPARC_HH22"; case 29: return "R_SPARC_HM10"; case 30: return "R_SPARC_LM22"; case 31: return "R_SPARC_PC_HH22"; case 32: return "R_SPARC_PC_HM10"; case 33: return "R_SPARC_PC_LM22"; case 34: return "R_SPARC_WDISP16"; case 35: return "R_SPARC_WDISP19"; case 36: return "R_SPARC_UNUSED_42"; case 37: return "R_SPARC_7"; case 38: return "R_SPARC_5"; case 39: return "R_SPARC_6"; case 40: return "R_SPARC_DISP64"; case 41: return "R_SPARC_PLT64"; case 42: return "R_SPARC_HIX22"; case 43: return "R_SPARC_LOX10"; case 44: return "R_SPARC_H44"; case 45: return "R_SPARC_M44"; case 46: return "R_SPARC_L44"; case 47: return "R_SPARC_REGISTER"; case 48: return "R_SPARC_UA64"; case 49: return "R_SPARC_UA16"; case 50: return "R_SPARC_32LE"; default: return _("*INVALID*"); } } static char * get_m32r_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_M32R_NONE"; case 1: return "R_M32R_16"; case 2: return "R_M32R_32"; case 3: return "R_M32R_24"; case 4: return "R_M32R_10_PCREL"; case 5: return "R_M32R_18_PCREL"; case 6: return "R_M32R_26_PCREL"; case 7: return "R_M32R_HI16_ULO"; case 8: return "R_M32R_HI16_SLO"; case 9: return "R_M32R_LO16"; case 10: return "R_M32R_SDA16"; default: return _("*INVALID*"); } } static char * get_v850_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_V850_NONE"; case 1: return "R_V850_9_PCREL"; case 2: return "R_V850_22_PCREL"; case 3: return "R_V850_HI16_S"; case 4: return "R_V850_HI16"; case 5: return "R_V850_LO16"; case 6: return "R_V850_32"; case 7: return "R_V850_16"; case 8: return "R_V850_8"; case 9: return "R_V850_SDA_16_16_OFFSET"; case 10: return "R_V850_SDA_15_16_OFFSET"; case 11: return "R_V850_ZDA_16_16_OFFSET"; case 12: return "R_V850_ZDA_15_16_OFFSET"; case 13: return "R_V850_TDA_6_8_OFFSET"; case 14: return "R_V850_TDA_7_8_OFFSET"; case 15: return "R_V850_TDA_7_7_OFFSET"; case 16: return "R_V850_TDA_16_16_OFFSET"; /* start-sanitize-v850e */ case 17: return "R_V850_TDA_4_5_OFFSET"; case 18: return "R_V850_TDA_4_4_OFFSET"; case 19: return "R_V850_SDA_16_16_SPLIT_OFFSET"; case 20: return "R_V850_ZDA_16_16_SPLIT_OFFSET"; case 21: return "R_V850_CALLT_6_7_OFFSET"; case 22: return "R_V850_CALLT_16_16_OFFSET"; /* end-sanitize-v850e */ default: return _("*INVALID*"); } } static char * get_d10v_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_D10V_NONE"; case 1: return "R_D10V_10_PCREL_R"; case 2: return "R_D10V_10_PCREL_L"; case 3: return "R_D10V_16"; case 4: return "R_D10V_18"; case 5: return "R_D10V_18_PCREL"; case 6: return "R_D10V_32"; default: return _("*INVALID*"); } } /* start-sanitize-d30v */ static char * get_d30v_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_D30V_NONE"; case 1: return "R_D30V_6"; case 2: return "R_D30V_9_PCREL"; case 3: return "R_D30V_9_PCREL_R"; case 4: return "R_D30V_15"; case 5: return "R_D30V_15_PCREL"; case 6: return "R_D30V_15_PCREL_R"; case 7: return "R_D30V_21"; case 8: return "R_D30V_21_PCREL"; case 9: return "R_D30V_21_PCREL_R"; case 10: return "R_D30V_32"; case 11: return "R_D30V_32_PCREL"; case 12: return "R_D30V_32_NORMAL"; default: return _("*INVALID*"); } } /* end-sanitize-d30v */ static char * get_sh_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_SH_NONE"; case 1: return "R_SH_DIR32"; case 2: return "R_SH_REL32"; case 3: return "R_SH_DIR8WPN"; case 4: return "R_SH_IND12W"; case 5: return "R_SH_DIR8WPL"; case 6: return "R_SH_DIR8WPZ"; case 7: return "R_SH_DIR8BP"; case 8: return "R_SH_DIR8W"; case 9: return "R_SH_DIR8L"; case 25: return "R_SH_SWITCH16"; case 26: return "R_SH_SWITCH32"; case 27: return "R_SH_USES"; case 28: return "R_SH_COUNT"; case 29: return "R_SH_ALIGN"; case 30: return "R_SH_CODE"; case 31: return "R_SH_DATA"; case 32: return "R_SH_LABEL"; default: return _("*INVALID*"); } } static char * get_mn10300_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_MN10300_NONE"; case 1: return "R_MN10300_32"; case 2: return "R_MN10300_16"; case 3: return "R_MN10300_8"; case 4: return "R_MN10300_PCREL32"; case 5: return "R_MN10300_PCREL16"; case 6: return "R_MN10300_PCREL8"; default: return _("*INVALID*"); } } static char * get_mn10200_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_MN10200_NONE"; case 1: return "R_MN10200_32"; case 2: return "R_MN10200_16"; case 3: return "R_MN10200_8"; case 4: return "R_MN10200_24"; case 5: return "R_MN10200_PCREL8"; case 6: return "R_MN10200_PCREL16"; case 7: return "R_MN10200_PCREL24"; default: return _("*INVALID*"); } } static char * get_ppc_rel_type (rtype) bfd_vma rtype; { switch (rtype) { case 0: return "R_PPC_NONE,"; case 1: return "R_PPC_ADDR32,"; case 2: return "R_PPC_ADDR24,"; case 3: return "R_PPC_ADDR16,"; case 4: return "R_PPC_ADDR16_LO,"; case 5: return "R_PPC_ADDR16_HI,"; case 6: return "R_PPC_ADDR16_HA,"; case 7: return "R_PPC_ADDR14,"; case 8: return "R_PPC_ADDR14_BRTAKEN,"; case 9: return "R_PPC_ADDR14_BRNTAKEN,"; case 10: return "R_PPC_REL24,"; case 11: return "R_PPC_REL14,"; case 12: return "R_PPC_REL14_BRTAKEN,"; case 13: return "R_PPC_REL14_BRNTAKEN,"; case 14: return "R_PPC_GOT16,"; case 15: return "R_PPC_GOT16_LO,"; case 16: return "R_PPC_GOT16_HI,"; case 17: return "R_PPC_GOT16_HA,"; case 18: return "R_PPC_PLT24,"; case 19: return "R_PPC_COPY,"; case 21: return "R_PPC_JMP_SLOT,"; case 22: return "R_PPC_RELATIVE,"; case 23: return "R_PPC_LOCAL24PC,"; case 24: return "R_PPC_UADDR32,"; case 25: return "R_PPC_UADDR16,"; case 26: return "R_PPC_REL32,"; case 27: return "R_PPC_PLT32,"; case 28: return "R_PPC_PLTREL32,"; case 29: return "R_PPC_PLT16_LO,"; case 30: return "R_PPC_PLT16_HI,"; case 31: return "R_PPC_PLT16_HA,"; case 32: return "R_PPC_SDAREL,"; case 33: return "R_PPC_SECTOFF,"; case 34: return "R_PPC_SECTOFF_LO,"; case 35: return "R_PPC_SECTOFF_HI,"; case 36: return "R_PPC_SECTOFF_HA,"; case 101: return "R_PPC_EMB_NADDR32,"; case 102: return "R_PPC_EMB_NADDR16,"; case 103: return "R_PPC_EMB_NADDR16_LO,"; case 104: return "R_PPC_EMB_NADDR16_HI,"; case 105: return "R_PPC_EMB_NADDR16_HA,"; case 106: return "R_PPC_EMB_SDAI16,"; case 107: return "R_PPC_EMB_SDA2I16,"; case 108: return "R_PPC_EMB_SDA2REL,"; case 109: return "R_PPC_EMB_SDA21,"; case 110: return "R_PPC_EMB_MRKREF,"; case 111: return "R_PPC_EMB_RELSEC16,"; case 112: return "R_PPC_EMB_RELST_LO,"; case 113: return "R_PPC_EMB_RELST_HI,"; case 114: return "R_PPC_EMB_RELST_HA,"; case 115: return "R_PPC_EMB_BIT_FLD,"; case 116: return "R_PPC_EMB_RELSDA,"; default: return _("*INVALID*"); } } /* Display the contents of the relocation data found at the specified offset. */ static void dump_relocations (file, rel_offset, rel_size, symtab, strtab) FILE * file; unsigned long rel_offset; unsigned long rel_size; Elf_Sym * symtab; char * strtab; { unsigned i; int is_rela; Elf_Rel * rels; Elf_Rela * relas; /* Compute number of relocations and read them in. */ switch (elf_header.e_machine) { case EM_386: case EM_486: case EM_CYGNUS_M32R: case EM_CYGNUS_D10V: GET_DATA_ALLOC (rel_offset, rel_size, rels, Elf_Rel *, "relocs"); rel_size = rel_size / sizeof (Elf_Rel); if (must_swap) { for (i = 0; i < rel_size; i++) { BYTE_SWAP (rels[i], r_offset); BYTE_SWAP (rels[i], r_info); } } is_rela = 0; relas = (Elf_Rela *) rels; break; case EM_68K: case EM_SPARC: case EM_PPC: case EM_CYGNUS_V850: /* start-sanitize-d30v */ case EM_CYGNUS_D30V: /* end-sanitize-d30v */ case EM_CYGNUS_MN10200: case EM_CYGNUS_MN10300: case EM_SH: GET_DATA_ALLOC (rel_offset, rel_size, relas, Elf_Rela *, "relocs"); rel_size = rel_size / sizeof (Elf_Rela); if (must_swap) { for (i = 0; i < rel_size; i++) { BYTE_SWAP (relas [i], r_offset); BYTE_SWAP (relas [i], r_info); BYTE_SWAP (relas [i], r_addend); } } is_rela = 1; rels = (Elf_Rel *) relas; break; default: warn (_("Don't know about relocations on this machine architecture\n")); return; } if (is_rela) printf (_(" Offset Value Type Symbol's Value Symbol's Name Addend\n")); else printf (_(" Offset Value Type Symbol's Value Symbol's Name\n")); for (i = 0; i < rel_size; i++) { char * rtype; bfd_vma offset; bfd_vma info; int symtab_index; if (is_rela) { offset = relas [i].r_offset; info = relas [i].r_info; } else { offset = rels [i].r_offset; info = rels [i].r_info; } printf (" %8.8lx %5.5lx ", offset, info); switch (elf_header.e_machine) { default: rtype = ""; break; case EM_CYGNUS_M32R: rtype = get_m32r_rel_type (ELF32_R_TYPE (info)); break; case EM_386: case EM_486: rtype = get_i386_rel_type (ELF32_R_TYPE (info)); break; case EM_68K: rtype = get_m68k_rel_type (ELF32_R_TYPE (info)); break; case EM_SPARC: rtype = get_sparc_rel_type (ELF32_R_TYPE (info)); break; case EM_CYGNUS_V850: rtype = get_v850_rel_type (ELF32_R_TYPE (info)); break; case EM_CYGNUS_D10V: rtype = get_d10v_rel_type (ELF32_R_TYPE (info)); break; /* start-sanitize-d30v */ case EM_CYGNUS_D30V: rtype = get_d30v_rel_type (ELF32_R_TYPE (info)); break; /* end-sanitize-d30v */ case EM_SH: rtype = get_sh_rel_type (ELF32_R_TYPE (info)); break; case EM_CYGNUS_MN10300: rtype = get_mn10300_rel_type (ELF32_R_TYPE (info)); break; case EM_CYGNUS_MN10200: rtype = get_mn10200_rel_type (ELF32_R_TYPE (info)); break; case EM_PPC: rtype = get_ppc_rel_type (ELF32_R_TYPE (info)); break; } printf ("%-21.21s", rtype); symtab_index = ELF32_R_SYM (info); if (symtab_index && symtab != NULL) { Elf_Sym * psym; psym = symtab + symtab_index; printf (" %08lx ", psym->st_value); if (psym->st_name == 0) printf ("%-17.17s", SECTION_NAME (section_headers + psym->st_shndx)); else if (strtab == NULL) printf (_(""), psym->st_name); else printf ("%-17.17s", strtab + psym->st_name); if (is_rela) printf (" + %lx", relas [i].r_addend); } putchar ('\n'); } free (relas); } static char * get_dynamic_type (type) unsigned long type; { static char buff [32]; switch (type) { case DT_NULL: return _("NULL"); case DT_NEEDED: return _("NEEDED"); case DT_PLTRELSZ: return _("PLTRELSZ"); case DT_PLTGOT: return _("PLTGOT"); case DT_HASH: return _("HASH"); case DT_STRTAB: return _("STRTAB"); case DT_SYMTAB: return _("SYMTAB"); case DT_RELA: return _("RELA"); case DT_RELASZ: return _("RELASZ"); case DT_RELAENT: return _("RELAENT"); case DT_STRSZ: return _("STRSZ"); case DT_SYMENT: return _("SYMENT"); case DT_INIT: return _("INIT"); case DT_FINI: return _("FINI"); case DT_SONAME: return _("SONAME"); case DT_RPATH: return _("RPATH"); case DT_SYMBOLIC: return _("SYMBOLIC"); case DT_REL: return _("REL"); case DT_RELSZ: return _("RELSZ"); case DT_RELENT: return _("RELENT"); case DT_PLTREL: return _("PLTREL"); case DT_DEBUG: return _("DEBUG"); case DT_TEXTREL: return _("TEXTREL"); case DT_JMPREL: return _("JMPREL"); case DT_VERDEF: return _("VERDEF"); case DT_VERDEFNUM: return _("VERDEFNUM"); case DT_VERNEED: return _("VERNEED"); case DT_VERNEEDNUM: return _("VERNEEDNUM"); case DT_VERSYM: return _("VERSYN"); case DT_AUXILIARY: return _("AUXILARY"); case DT_FILTER: return _("FILTER"); default: if ((type >= DT_LOPROC) && (type <= DT_HIPROC)) sprintf (buff, _("Processor Specific: (%x)"), type); else sprintf (buff, _(": %x"), type); return buff; } } static char * get_file_type (e_type) unsigned e_type; { static char buff [32]; switch (e_type) { case ET_NONE: return _("None"); case ET_REL: return _("Relocatable file"); case ET_EXEC: return _("Executable file"); case ET_DYN: return _("Shared object file"); case ET_CORE: return _("Core file"); default: if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC)) sprintf (buff, _("Processor Specific: (%x)"), e_type); else sprintf (buff, _(": %x"), e_type); return buff; } } static char * get_machine_name (e_machine) unsigned e_machine; { static char buff [32]; switch (e_machine) { case EM_NONE: return _("None"); case EM_M32: return "WE32100"; case EM_SPARC: return "Sparc"; case EM_386: return "80386"; case EM_68K: return "MC68000"; case EM_88K: return "MC88000"; case EM_486: return "Intel 80486"; case EM_860: return "Intel 80860"; case EM_MIPS: return "MIPS R3000 big-endian"; case EM_S370: return "Amdahl"; case EM_MIPS_RS4_BE: return "MIPS R400 big-endian"; case EM_OLD_SPARCV9: return "Sparc v9 (old)"; case EM_PARISC: return "HPPA"; case EM_PPC_OLD: return "Power PC (old)"; case EM_SPARC32PLUS: return "Sparc v8+" ; case EM_960: return "Intel 90860"; case EM_PPC: return "PowerPC"; case EM_V800: return "NEC V800"; case EM_FR20: return "Fujitsu FR20"; case EM_RH32: return "TRW RH32"; case EM_MMA: return "Fujitsu MMA"; case EM_ARM: return "ARM"; case EM_OLD_ALPHA: return "Digital Alpha (old)"; case EM_SH: return "Hitachi SH"; case EM_SPARCV9: return "Sparc v9"; case EM_ALPHA: return "Alpha"; case EM_CYGNUS_D10V: return "d10v"; /* start-sanitize-d30v */ case EM_CYGNUS_D30V: return "d30v"; /* end-sanitize-d30v */ case EM_CYGNUS_M32R: return "M32r"; case EM_CYGNUS_V850: return "v850"; case EM_CYGNUS_MN10300: return "mn10300"; case EM_CYGNUS_MN10200: return "mn10200"; default: sprintf (buff, _(": %x"), e_machine); return buff; } } static char * get_machine_flags (e_flags, e_machine) unsigned e_flags; unsigned e_machine; { static char buf [1024]; buf[0] = '\0'; if (e_flags) { switch (e_machine) { default: break; case EM_PPC: if (e_flags & EF_PPC_EMB) strcat (buf, ", emb"); if (e_flags & EF_PPC_RELOCATABLE) strcat (buf, ", relocatable"); if (e_flags & EF_PPC_RELOCATABLE_LIB) strcat (buf, ", relocatable-lib"); break; case EM_CYGNUS_M32R: if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH) strcat (buf, ", m32r"); /* start-sanitize-m32rx */ #ifdef E_M32RX_ARCH if ((e_flags & EF_M32R_ARCH) == E_M32RX_ARCH) strcat (buf, ", m32rx"); #endif /* end-sanitize-m32rx */ break; case EM_MIPS: case EM_MIPS_RS4_BE: if (e_flags & EF_MIPS_NOREORDER) strcat (buf, ", noreorder"); if (e_flags & EF_MIPS_PIC) strcat (buf, ", pic"); if (e_flags & EF_MIPS_CPIC) strcat (buf, ", cpic"); if (e_flags & EF_MIPS_ABI2) strcat (buf, ", abi2"); if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1) strcat (buf, ", mips1"); if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2) strcat (buf, ", mips2"); if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3) strcat (buf, ", mips3"); if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4) strcat (buf, ", mips4"); break; } } return buf; } static char * get_machine_data (e_data) unsigned e_data; { static char buff [32]; switch (e_data) { case ELFDATA2LSB: return _("ELFDATA2LSB (little endian)"); case ELFDATA2MSB: return _("ELFDATA2MSB (big endian)"); default: sprintf (buff, _(": %x"), e_data); return buff; } } static char * get_segment_type (p_type) unsigned long p_type; { static char buff [32]; switch (p_type) { case PT_NULL: return _("Unused"); case PT_LOAD: return _("Loadable"); case PT_DYNAMIC: return _("Dynamic link info"); case PT_INTERP: return _("Interpreter"); case PT_NOTE: return _("Auxillary Info"); case PT_SHLIB: return _("Shared Library"); case PT_PHDR: return _("Program Header"); default: if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC)) return _("processor specific"); else { sprintf (buff, _(": %x"), p_type); return buff; } } } static char * get_section_type_name (sh_type) unsigned int sh_type; { static char buff [32]; switch (sh_type) { case SHT_NULL: return _("Unused"); case SHT_PROGBITS: return _("Program data"); case SHT_SYMTAB: return _("Symbol table"); case SHT_STRTAB: return _("String table"); case SHT_RELA: return _("Relocations"); case SHT_HASH: return _("Symbol hashes"); case SHT_DYNAMIC: return _("Dynamic info"); case SHT_NOTE: return _("Notes"); case SHT_NOBITS: return _("Space, no data"); case SHT_REL: return _("Relocations"); case SHT_SHLIB: return _("Shared lib info"); case SHT_DYNSYM: return _("Dynamic symbols"); case SHT_GNU_verdef: return _("Version definition"); case SHT_GNU_verneed: return _("Version needs"); case SHT_GNU_versym: return _("Version symbols"); case 0x6ffffff0: return "VERSYM"; case 0x6ffffffc: return "VERDEF"; case 0x7ffffffd: return "AUXILIARY"; case 0x7fffffff: return "FILTER"; default: if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC)) sprintf (buff, _("cpu defined (%d)"), sh_type - SHT_LOPROC); else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER)) sprintf (buff, _("app defined (%d)"), sh_type - SHT_LOUSER); else sprintf (buff, _(": %x"), sh_type); return buff; } } struct option options [] = { {"all", no_argument, 0, 'a'}, {"file-header", no_argument, 0, 'h'}, {"program-headers", no_argument, 0, 'l'}, {"headers", no_argument, 0, 'e'}, {"segments", no_argument, 0, 'l'}, {"sections", no_argument, 0, 'S'}, {"section-headers", no_argument, 0, 'S'}, {"symbols", no_argument, 0, 's'}, {"relocs", no_argument, 0, 'r'}, {"dynamic", no_argument, 0, 'd'}, {"version-info", no_argument, 0, 'V'}, {"use-dynamic", no_argument, 0, 'D'}, {"hex-dump", required_argument, 0, 'x'}, #ifdef SUPPORT_DISASSEMBLY {"instruction-dump", required_argument, 0, 'i'}, #endif {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'H'}, {0, no_argument, 0, 0} }; static void usage () { fprintf (stdout, _("Usage: readelf {options} elf-file(s)\n")); fprintf (stdout, _(" Options are:\n")); fprintf (stdout, _(" -a or --all Equivalent to: -h -l -S -s -r -d -V\n")); fprintf (stdout, _(" -h or --file-header Display the ELF file header\n")); fprintf (stdout, _(" -l or --program-headers or --segments\n")); fprintf (stdout, _(" Display the program headers\n")); fprintf (stdout, _(" -S or --section-headers or --sections\n")); fprintf (stdout, _(" Display the sections' header\n")); fprintf (stdout, _(" -e or --headers Equivalent to: -h -l -S\n")); fprintf (stdout, _(" -s or --symbols Display the symbol table\n")); fprintf (stdout, _(" -r or --relocs Display the relocations (if present)\n")); fprintf (stdout, _(" -d or --dynamic Display the dynamic segment (if present)\n")); fprintf (stdout, _(" -V or --version-info Display the version sections (if present)\n")); fprintf (stdout, _(" -D or --use-dynamic Use the dynamic section info when displaying symbols\n")); fprintf (stdout, _(" -x or --hex-dump=\n")); fprintf (stdout, _(" Dump the contents of section \n")); #ifdef SUPPORT_DISASSEMBLY fprintf (stdout, _(" -i or --instruction-dump=\n")); fprintf (stdout, _(" Disassemble the contents of section \n")); #endif fprintf (stdout, _(" -v or --version Display the version number of readelf\n")); fprintf (stdout, _(" -H or --help Display this information\n")); fprintf (stdout, _("Report bugs to bug-gnu-utils@gnu.org\n")); exit (0); } static void parse_args (argc, argv) int argc; char ** argv; { int c; if (argc < 2) usage (); while ((c = getopt_long (argc, argv, "ersahldSDx:i:vV", options, NULL)) != EOF) { char * cp; int section; switch (c) { case 'H': usage (); break; case 'a': do_syms ++; do_reloc ++; do_dynamic ++; do_header ++; do_sections ++; do_segments ++; do_version ++; break; case 'e': do_header ++; do_sections ++; do_segments ++; break; case 'D': do_using_dynamic ++; break; case 'r': do_reloc ++; break; case 'h': do_header ++; break; case 'l': do_segments ++; break; case 's': do_syms ++; break; case 'S': do_sections ++; break; case 'd': do_dynamic ++; break; case 'x': do_dump ++; section = strtoul (optarg, & cp, 0); if (! * cp && section >= 0 && section < NUM_DUMP_SECTS) { dump_sects [section] |= HEX_DUMP; break; } goto oops; #ifdef SUPPORT_DISASSEMBLY case 'i': do_dump ++; section = strtoul (optarg, & cp, 0); if (! * cp && section >= 0 && section < NUM_DUMP_SECTS) { dump_sects [section] |= DISASS_DUMP; break; } goto oops; #endif case 'v': print_version (program_name); break; case 'V': do_version ++; break; default: oops: /* xgettext:c-format */ error (_("Invalid option '-%c'\n"), c); /* Drop through. */ case '?': usage (); } } if (!do_dynamic && !do_syms && !do_reloc && !do_sections && !do_segments && !do_header && !do_dump && !do_version) usage (); else if (argc < 3) { warn (_("Nothing to do.\n")); usage(); } } /* Decode the data held in 'elf_header'. */ static int process_file_header () { if ( elf_header.e_ident [EI_MAG0] != ELFMAG0 || elf_header.e_ident [EI_MAG1] != ELFMAG1 || elf_header.e_ident [EI_MAG2] != ELFMAG2 || elf_header.e_ident [EI_MAG3] != ELFMAG3) { error (_("Not an ELF file - it has the wrong magic bytes at the start\n")); return 0; } if (elf_header.e_ident [EI_CLASS] != ELFCLASS32) { error (_("Not a 32 bit ELF file\n")); return 0; } if (elf_header.e_ident [EI_DATA] != expected_endian) { must_swap = 1; BYTE_SWAP (elf_header, e_type); BYTE_SWAP (elf_header, e_machine); BYTE_SWAP (elf_header, e_version); BYTE_SWAP (elf_header, e_entry); BYTE_SWAP (elf_header, e_phoff); BYTE_SWAP (elf_header, e_shoff); BYTE_SWAP (elf_header, e_flags); BYTE_SWAP (elf_header, e_ehsize); BYTE_SWAP (elf_header, e_phentsize); BYTE_SWAP (elf_header, e_phnum); BYTE_SWAP (elf_header, e_shentsize); BYTE_SWAP (elf_header, e_shnum); BYTE_SWAP (elf_header, e_shstrndx); } if (do_header) { int i; printf (_("ELF Header:\n")); printf (_(" Magic: ")); for (i = 0; i < EI_NIDENT; i ++) printf ("%2.2x ", elf_header.e_ident [i]); printf ("\n"); printf (_(" Type: %s\n"), get_file_type (elf_header.e_type)); printf (_(" Machine: %s\n"), get_machine_name (elf_header.e_machine)); printf (_(" Version: 0x%lx\n"), (unsigned long) elf_header.e_version); printf (_(" Data: %s"), get_machine_data (elf_header.e_ident [EI_DATA])); if (must_swap) printf (_(" [byte swapping]")); putchar ('\n'); printf (_(" Entry point address: 0x%lx\n"), (unsigned long) elf_header.e_entry); printf (_(" Start of program headers: %ld (bytes into file)\n"), (long) elf_header.e_phoff); printf (_(" Start of section headers: %ld (bytes into file)\n"), (long) elf_header.e_shoff); printf (_(" Flags: 0x%lx%s\n"), (unsigned long) elf_header.e_flags, get_machine_flags (elf_header.e_flags, elf_header.e_machine)); printf (_(" Size of this header: %ld (bytes)\n"), (long) elf_header.e_ehsize); printf (_(" Size of program headers: %ld (bytes)\n"), (long) elf_header.e_phentsize); printf (_(" Number of program headers: %ld\n"), (long) elf_header.e_phnum); printf (_(" Size of section headers: %ld (bytes)\n"), (long) elf_header.e_shentsize); printf (_(" Number of section headers: %ld\n"), (long) elf_header.e_shnum); printf (_(" Section header string table index: %ld\n"), (long) elf_header.e_shstrndx); } return 1; } static void process_program_headers (file) FILE * file; { Elf_Phdr * program_headers; Elf_Phdr * segment; int i; if (elf_header.e_phnum == 0) { if (do_segments) printf (_("\nThere are no program headers in this file.\n")); return; } if (do_segments && !do_header) { printf (_("\nElf file is %s\n"), get_file_type (elf_header.e_type)); printf (_("Entry point 0x%x\n"), elf_header.e_entry); printf (_("There are %d program headers, starting at offset %x:\n"), elf_header.e_phnum, elf_header.e_phoff); } GET_DATA_ALLOC (elf_header.e_phoff, elf_header.e_phentsize * elf_header.e_phnum, program_headers, Elf_Phdr *, "program headers"); if (must_swap) { for (i = 0; i < elf_header.e_phnum; i++) { BYTE_SWAP (program_headers [i], p_type); BYTE_SWAP (program_headers [i], p_flags); BYTE_SWAP (program_headers [i], p_offset); BYTE_SWAP (program_headers [i], p_vaddr); BYTE_SWAP (program_headers [i], p_paddr); BYTE_SWAP (program_headers [i], p_filesz); BYTE_SWAP (program_headers [i], p_memsz); BYTE_SWAP (program_headers [i], p_align); } } if (do_segments) { printf (_("\nProgram Header%s:\n"), elf_header.e_phnum > 1 ? "s" : ""); printf (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n")); } loadaddr = -1; dynamic_addr = 0; for (i = 0; i < elf_header.e_phnum; i++) { segment = program_headers + i; if (do_segments) { printf (" %-16.16s ", get_segment_type (segment->p_type)); printf ("0x%5.5lx ", segment->p_offset); printf ("0x%8.8lx ", segment->p_vaddr); printf ("0x%8.8lx ", segment->p_paddr); printf ("0x%5.5lx 0x%5.5lx ", segment->p_filesz, segment->p_memsz); printf ("%c%c%c ", (segment->p_flags & PF_R ? 'R' : ' '), (segment->p_flags & PF_W ? 'W' : ' '), (segment->p_flags & PF_X ? 'E' : ' ')); printf ("%#lx", segment->p_align); } switch (segment->p_type) { case PT_LOAD: if (loadaddr == -1) loadaddr = (segment->p_vaddr & 0xfffff000) - (segment->p_offset & 0xfffff000); break; case PT_DYNAMIC: if (dynamic_addr) error (_("more than one dynamic segment\n")); dynamic_addr = segment->p_offset; dynamic_size = segment->p_filesz; break; case PT_INTERP: if (fseek (file, segment->p_offset, SEEK_SET)) error (_("Unable to find program interpreter name\n")); else { program_interpreter[0] = 0; fscanf (file, "%63s", program_interpreter); if (do_segments) printf (_("\n [Requesting program interpreter: %s]"), program_interpreter); } break; } if (do_segments) putc ('\n', stdout); } if (loadaddr == -1) { /* Very strange. */ loadaddr = 0; } if (do_segments && section_headers != NULL) { printf (_("\n Section to Segment mapping:\n")); printf (_(" Segment Sections...\n")); assert (string_table != NULL); for (i = 0; i < elf_header.e_phnum; i++) { int j; Elf_Shdr * section; segment = program_headers + i; section = section_headers; printf (" %2.2d ", i); for (j = 0; j < elf_header.e_shnum; j++, section ++) { if (section->sh_size > 0 /* Compare allocated sections by VMA, unallocated sections by file offset. */ && (section->sh_flags & SHF_ALLOC ? (section->sh_addr >= segment->p_vaddr && section->sh_addr + section->sh_size <= segment->p_vaddr + segment->p_memsz) : (section->sh_offset >= segment->p_offset && (section->sh_offset + section->sh_size <= segment->p_offset + segment->p_filesz)))) printf ("%s ", SECTION_NAME (section)); } putc ('\n',stdout); } } free (program_headers); } static void process_section_headers (file) FILE * file; { Elf_Shdr * section; int i; if (elf_header.e_shnum == 0) { if (do_sections) printf (_("\nThere are no sections in this file.\n")); section_headers = NULL; return; } if (do_sections && !do_header) printf (_("There are %d section headers, starting at offset %x:\n"), elf_header.e_shnum, elf_header.e_shoff); GET_DATA_ALLOC (elf_header.e_shoff, elf_header.e_shentsize * elf_header.e_shnum, section_headers, Elf_Shdr *, "section headers"); if (must_swap) { for (i = 0; i < elf_header.e_shnum; i++) { BYTE_SWAP (section_headers [i], sh_name); BYTE_SWAP (section_headers [i], sh_type); BYTE_SWAP (section_headers [i], sh_flags); BYTE_SWAP (section_headers [i], sh_addr); BYTE_SWAP (section_headers [i], sh_offset); BYTE_SWAP (section_headers [i], sh_size); BYTE_SWAP (section_headers [i], sh_link); BYTE_SWAP (section_headers [i], sh_info); BYTE_SWAP (section_headers [i], sh_addralign); BYTE_SWAP (section_headers [i], sh_entsize); } } /* Read in the string table, so that we have names to display. */ section = section_headers + elf_header.e_shstrndx; if (section->sh_size != 0) { unsigned long string_table_offset; string_table_offset = section->sh_offset; GET_DATA_ALLOC (section->sh_offset, section->sh_size, string_table, char *, "string table"); } /* Scan the sections for the dynamic symbol table and dynamic string table. */ dynamic_symbols = NULL; dynamic_strings = NULL; for (i = 0, section = section_headers; i < elf_header.e_shnum; i ++, section ++) { if (section->sh_type == SHT_DYNSYM) { if (dynamic_symbols != NULL) { error (_("File contains multiple dynamic symbol tables\n")); continue; } GET_DATA_ALLOC (section->sh_offset, section->sh_size, dynamic_symbols, Elf_Sym *, "dynamic symbols"); if (must_swap) { Elf_Sym * psym; for (i = 0, psym = dynamic_symbols; i < section->sh_size / section->sh_entsize; i ++, psym ++) { BYTE_SWAP (* psym, st_name); BYTE_SWAP (* psym, st_value); BYTE_SWAP (* psym, st_size); BYTE_SWAP (* psym, st_shndx); } } } else if (section->sh_type == SHT_STRTAB && strcmp (SECTION_NAME (section), ".dynstr") == 0) { if (dynamic_strings != NULL) { error (_("File contains multiple dynamic string tables\n")); continue; } GET_DATA_ALLOC (section->sh_offset, section->sh_size, dynamic_strings, char *, "dynamic strings"); } } if (! do_sections) return; printf (_("\nSection Header%s:\n"), elf_header.e_shnum > 1 ? "s" : ""); printf (_(" [Nr] Name Type Addr Off Size ES Flg Lk In Al\n")); for (i = 0, section = section_headers; i < elf_header.e_shnum; i ++, section ++) { printf (" [%2d] %-17.17s", i, SECTION_NAME (section)); printf (" %-15.15s ",get_section_type_name (section->sh_type)); printf ( "%8.8lx %6.6lx %6.6lx %2.2lx", section->sh_addr, section->sh_offset, section->sh_size, section->sh_entsize); printf (" %c%c%c %2ld %2lx %ld \n", (section->sh_flags & SHF_WRITE ? 'W' : ' '), (section->sh_flags & SHF_ALLOC ? 'A' : ' '), (section->sh_flags & SHF_EXECINSTR ? 'X' : ' '), section->sh_link, section->sh_info, section->sh_addralign); } } /* Process the reloc section. */ static void process_relocs (file) FILE * file; { unsigned long rel_size; unsigned long rel_offset; if (!do_reloc) return; if (do_using_dynamic) { rel_size = 0; rel_offset = 0; if (dynamic_info [DT_REL]) { rel_offset = dynamic_info [DT_REL]; rel_size = dynamic_info [DT_RELSZ]; } else if (dynamic_info [DT_RELA]) { rel_offset = dynamic_info [DT_RELA]; rel_size = dynamic_info [DT_RELASZ]; } else if (dynamic_info [DT_JMPREL]) { rel_offset = dynamic_info [DT_JMPREL]; rel_size = dynamic_info [DT_PLTRELSZ]; } if (rel_size) { printf (_("\nRelocation section at offset 0x%x contains %d bytes:\n"), rel_offset, rel_size); dump_relocations (file, rel_offset - loadaddr, rel_size, dynamic_symbols, dynamic_strings); } else printf (_("\nThere are no dynamic relocations in this file.\n")); } else { Elf_Shdr * section; unsigned long i; int found = 0; assert (string_table != NULL); for (i = 0, section = section_headers; i < elf_header.e_shnum; i++, section ++) { if ( section->sh_type != SHT_RELA && section->sh_type != SHT_REL) continue; rel_offset = section->sh_offset; rel_size = section->sh_size; if (rel_size) { Elf_Shdr * strsec; Elf_Shdr * symsec; Elf_Sym * symtab; char * strtab; printf (_("\nRelocation section '%s' at offset 0x%x contains %d entries:\n"), SECTION_NAME (section), rel_offset, rel_size / section->sh_entsize); symsec = section_headers + section->sh_link; GET_DATA_ALLOC (symsec->sh_offset, symsec->sh_size, symtab, Elf_Sym *, "symbols"); if (must_swap) { unsigned int si; for (si = 0; si < symsec->sh_size / symsec->sh_entsize; si ++) { BYTE_SWAP (symtab [si], st_name); BYTE_SWAP (symtab [si], st_value); BYTE_SWAP (symtab [si], st_size); BYTE_SWAP (symtab [si], st_shndx); } } strsec = section_headers + symsec->sh_link; GET_DATA_ALLOC (strsec->sh_offset, strsec->sh_size, strtab, char *, "string table"); dump_relocations (file, rel_offset, rel_size, symtab, strtab); free (strtab); free (symtab); found = 1; } } if (! found) printf (_("\nThere are no relocations in this file.\n")); } } /* Parse the dynamic segment */ static void process_dynamic_segment (file) FILE * file; { Elf_Dyn * dynamic_segment; Elf_Dyn * entry; unsigned int i; if (dynamic_size == 0) { if (do_dynamic) printf (_("\nThere is no dynamic segment in this file.\n")); return; } GET_DATA_ALLOC (dynamic_addr, dynamic_size, dynamic_segment, Elf_Dyn *, "dynamic segment"); dynamic_size = dynamic_size / sizeof (Elf_Dyn); if (must_swap) { for (i = 0; i < dynamic_size; i++) { BYTE_SWAP (dynamic_segment [i], d_tag); BYTE_SWAP (dynamic_segment [i], d_un.d_val); } } /* Find the appropriate symbol table. */ if (dynamic_symbols == NULL) { for (i = 0, entry = dynamic_segment; i < dynamic_size; ++i, ++ entry) { unsigned long offset; long num_syms; if (entry->d_tag != DT_SYMTAB) continue; dynamic_info [DT_SYMTAB] = entry->d_un.d_val; /* Since we do not know how big the symbol table is, we default to reading in the entire file (!) and processing that. This is overkill, I know, but it should work. */ offset = entry->d_un.d_val - loadaddr; if (fseek (file, 0, SEEK_END)) error (_("Unable to seek to end of file!")); num_syms = (ftell (file) - offset) / sizeof (Elf_Sym); if (num_syms < 1) { error (_("Unable to determine the number of symbols to load\n")); continue; } GET_DATA_ALLOC (offset, num_syms * sizeof (Elf_Sym), dynamic_symbols, Elf_Sym *, "dynamic symbols"); if (must_swap) { Elf_Sym * psym; for (i = 0, psym = dynamic_symbols; i < num_syms; i ++, psym ++) { BYTE_SWAP (* psym, st_name); BYTE_SWAP (* psym, st_value); BYTE_SWAP (* psym, st_size); BYTE_SWAP (* psym, st_shndx); } } break; } } /* Similarly find a string table. */ if (dynamic_strings == NULL) { for (i = 0, entry = dynamic_segment; i < dynamic_size; ++i, ++ entry) { unsigned long offset; long str_tab_len; if (entry->d_tag != DT_STRTAB) continue; dynamic_info [DT_STRTAB] = entry->d_un.d_val; /* Since we do not know how big the string table is, we default to reading in the entire file (!) and processing that. This is overkill, I know, but it should work. */ offset = entry->d_un.d_val - loadaddr; if (fseek (file, 0, SEEK_END)) error (_("Unable to seek to end of file\n")); str_tab_len = ftell (file) - offset; if (str_tab_len < 1) { error (_("Unable to determine the length of the dynamic string table\n")); continue; } GET_DATA_ALLOC (offset, str_tab_len, dynamic_strings, char *, "dynamic string table"); break; } } if (do_dynamic && dynamic_addr) printf (_("\nDynamic segement at offset 0x%x contains %d entries:\n"), dynamic_addr, dynamic_size); if (do_dynamic) printf (_(" Tag Type Name/Value\n")); for (i = 0, entry = dynamic_segment; i < dynamic_size; i++, entry ++) { if (do_dynamic) printf (_(" 0x%-6.6x (%-11.11s) "), entry->d_tag, get_dynamic_type (entry->d_tag)); switch (entry->d_tag) { case DT_AUXILIARY: case DT_FILTER: if (do_dynamic) { if (entry->d_tag == DT_AUXILIARY) printf (_("Auxiliary library")); else printf (_("Filter library")); if (dynamic_strings) printf (": [%s]\n", dynamic_strings + entry->d_un.d_val); else printf (": %#lx\n", entry->d_un.d_val); } break; case DT_NULL : case DT_NEEDED : case DT_PLTRELSZ: case DT_PLTGOT : case DT_HASH : case DT_STRTAB : case DT_SYMTAB : case DT_RELA : case DT_RELASZ : case DT_RELAENT : case DT_STRSZ : case DT_SYMENT : case DT_INIT : case DT_FINI : case DT_SONAME : case DT_RPATH : case DT_SYMBOLIC: case DT_REL : case DT_RELSZ : case DT_RELENT : case DT_PLTREL : case DT_DEBUG : case DT_TEXTREL : case DT_JMPREL : dynamic_info [entry->d_tag] = entry->d_un.d_val; if (do_dynamic) { char * name; if (dynamic_strings == NULL) name = NULL; else name = dynamic_strings + entry->d_un.d_val; if (name) { switch (entry->d_tag) { case DT_NEEDED: printf (_("Shared library: [%s]"), name); if (strcmp (name, program_interpreter)) printf ("\n"); else printf (_(" program interpreter\n")); break; case DT_SONAME: printf (_("Library soname: [%s]\n"), name); break; case DT_RPATH: printf (_("Library rpath: [%s]\n"), name); break; default: printf ("%#lx\n", entry->d_un.d_val); } } else printf ("%#lx\n", entry->d_un.d_val); } break; default: if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM)) { version_info [DT_VERSIONTAGIDX (entry->d_tag)] = entry->d_un.d_val; if (do_dynamic) printf ("%#lx\n", entry->d_un.d_ptr); } break; } } free (dynamic_segment); } static char * get_ver_flags (unsigned short flags) { static char buff [32]; buff[0] = 0; if (flags == 0) return _("none"); if (flags & VER_FLG_BASE) strcat (buff, "BASE "); if (flags & VER_FLG_WEAK) { if (flags & VER_FLG_BASE) strcat (buff, "| "); strcat (buff, "WEAK "); } if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK)) strcat (buff, "| "); return buff; } /* Display the contents of the version sections. */ static void process_version_sections (file) FILE * file; { Elf_Shdr * section; unsigned i; int found = 0; if (! do_version) return; for (i = 0, section = section_headers; i < elf_header.e_shnum; i++, section ++) { switch (section->sh_type) { case SHT_GNU_verdef: { Elf_Verdef * verdef_section; unsigned int idx; unsigned int cnt; found = 1; printf (_("\nVersion definition section '%s' contains %d entries:\n"), SECTION_NAME (section), section->sh_info); printf (_(" Addr: %#08x Offset: %#08x Link: %x (%s)\n"), section->sh_addr, section->sh_offset, section->sh_link, SECTION_NAME (section_headers + section->sh_link)); GET_DATA_ALLOC (section->sh_offset, section->sh_size, verdef_section, Elf_Verdef *, "version definition section"); for (idx = cnt = 0; cnt < section->sh_info; ++ cnt) { Elf_Verdef * ent; Elf_Verdaux * aux; int j; int isum; ent = (Elf_Verdef *) (((char *) verdef_section) + idx); if (must_swap) { BYTE_SWAP (* ent, vd_version); BYTE_SWAP (* ent, vd_flags); BYTE_SWAP (* ent, vd_ndx); BYTE_SWAP (* ent, vd_cnt); BYTE_SWAP (* ent, vd_hash); BYTE_SWAP (* ent, vd_aux); BYTE_SWAP (* ent, vd_next); } printf (_(" %#06x: Rev: %d Flags: %s"), idx, ent->vd_version, get_ver_flags (ent->vd_flags)); printf (_(" Index: %d Cnt: %d "), ent->vd_ndx, ent->vd_cnt); aux = (Elf_Verdaux *) ((char *) ent + ent->vd_aux); if (must_swap) { BYTE_SWAP (* aux, vda_name); BYTE_SWAP (* aux, vda_next); } if (dynamic_strings) printf (_("Name: %s\n"), dynamic_strings + aux->vda_name); else printf (_("Name index: %d\n"), aux->vda_name); j = 1; isum = idx + ent->vd_aux; while (j < ent->vd_cnt) { isum += aux->vda_next; aux = (Elf_Verdaux *)((char *) aux + aux->vda_next); if (must_swap) { BYTE_SWAP (* aux, vda_name); BYTE_SWAP (* aux, vda_next); } if (dynamic_strings) printf (_(" %#06x: Parent %d: %s\n"), isum, j, dynamic_strings + aux->vda_name); else printf (_(" %#06x: Parent %d, name index: %d\n"), isum, j, aux->vda_name); ++j; } idx += ent->vd_next; } free (verdef_section); } break; case SHT_GNU_verneed: { Elf_Verneed * verneed_section; unsigned int idx; unsigned int cnt; found = 1; printf (_("\nVersion needs section '%s' contains %d entries:\n"), SECTION_NAME (section), section->sh_info); printf (_(" Addr: %#08x Offset: %#08x Link to section: %d (%s)\n"), section->sh_addr, section->sh_offset, section->sh_link, SECTION_NAME (section_headers + section->sh_link)); GET_DATA_ALLOC (section->sh_offset, section->sh_size, verneed_section, Elf_Verneed *, "version need section"); for (idx = cnt = 0; cnt < section->sh_info; ++cnt) { Elf_Verneed * ent; Elf_Vernaux * aux; int j; int isum; ent = verneed_section + idx / sizeof (Elf_Verneed); if (must_swap) { BYTE_SWAP (* ent, vn_version); BYTE_SWAP (* ent, vn_cnt); BYTE_SWAP (* ent, vn_file); BYTE_SWAP (* ent, vn_aux); BYTE_SWAP (* ent, vn_next); } printf (_(" %#06x: Version: %d"), idx, ent->vn_version); if (dynamic_strings) printf (_(" File: %s"), dynamic_strings + ent->vn_file); else printf (_(" File: %x"), ent->vn_file); printf (_(" Cnt: %d\n"), ent->vn_cnt); aux = (Elf_Vernaux *)((char *) ent + ent->vn_aux); for (j = 0, isum = idx + ent->vn_aux; j < ent->vn_cnt; ++j) { if (must_swap) { BYTE_SWAP (* aux, vna_hash); BYTE_SWAP (* aux, vna_flags); BYTE_SWAP (* aux, vna_other); BYTE_SWAP (* aux, vna_name); BYTE_SWAP (* aux, vna_next); } if (dynamic_strings) printf (_(" %#06x: Name: %s"), isum, dynamic_strings + aux->vna_name); else printf (_(" %#06x: Name index: %x"), isum, aux->vna_name); printf (_(" Flags: %s Version: %d\n"), get_ver_flags (aux->vna_flags), aux->vna_other); isum += aux->vna_next; aux = (Elf_Vernaux *)((char *) aux + aux->vna_next); } idx += ent->vn_next; } } break; case SHT_GNU_versym: { Elf_Shdr * link_section = section_headers + section->sh_link; int total = section->sh_size / section->sh_entsize; int cnt; unsigned short * p; char * strtab; Elf_Sym * symbols; Elf_Shdr * string_sec; found = 1; GET_DATA_ALLOC (link_section->sh_offset, link_section->sh_size, symbols, Elf_Sym *, "version symbols section"); if (must_swap) { int k; for (k = link_section->sh_size / link_section->sh_entsize; k --;) { BYTE_SWAP (symbols [k], st_name); BYTE_SWAP (symbols [k], st_value); BYTE_SWAP (symbols [k], st_size); BYTE_SWAP (symbols [k], st_shndx); } } string_sec = section_headers + link_section->sh_link; GET_DATA_ALLOC (string_sec->sh_offset, string_sec->sh_size, strtab, char *, "version string table"); printf (_("\nVersion symbols section '%s' contains %d entries:\n"), SECTION_NAME (section), total); printf (_(" Addr: %#08x Offset: %#08x Link: %x (%s)\n"), section->sh_addr, section->sh_offset, section->sh_link, SECTION_NAME (link_section)); GET_DATA_ALLOC (version_info [DT_VERSIONTAGIDX (DT_VERSYM)] - loadaddr, total * sizeof (unsigned short), p, unsigned short *, "version symbol data"); if (must_swap) { int k; for (k = total; k--;) p [k] = SWAP2 (p [k]); } for (cnt = 0; cnt < total; cnt += 4) { int j, nn; printf (" %03x:", cnt); for (j = 0; (j < 4) && (cnt + j) < total; ++j) switch (p[cnt + j]) { case 0: printf (" 0 (*local*) "); break; case 1: printf (" 1 (*global*) "); break; default: nn = printf ("%4x%c", p[cnt + j] & 0x7fff, p[cnt + j] & 0x8000 ? 'h' : ' '); if (symbols [cnt + j].st_shndx < SHN_LORESERVE && section_headers[symbols [cnt + j].st_shndx].sh_type == SHT_NOBITS) { /* We must test both. */ Elf_Verneed vn; Elf_Vernaux vna; unsigned long offset; offset = version_info [DT_VERSIONTAGIDX (DT_VERNEED)] - loadaddr; do { unsigned long vna_off; GET_DATA (offset, vn, "version need"); if (must_swap) { BYTE_SWAP (vn, vn_aux); BYTE_SWAP (vn, vn_next); } vna_off = offset + vn.vn_aux; do { GET_DATA (vna_off, vna, "version need aux (1)"); if (must_swap) { BYTE_SWAP (vna, vna_next); BYTE_SWAP (vna, vna_other); } vna_off += vna.vna_next; } while (vna.vna_other != p[cnt + j] && vna.vna_next != 0); if (vna.vna_other == p[cnt + j]) { if (must_swap) BYTE_SWAP (vna, vna_name); nn += printf ("(%11.11s)", strtab + vna.vna_name); break; } else if (vn.vn_next == 0) { if (p[cnt + j] != 0x8001) { Elf_Verdef vd; Elf_Verdaux vda; offset = version_info [DT_VERSIONTAGIDX (DT_VERDEF)] - loadaddr; do { GET_DATA (offset, vd, "version definition"); if (must_swap) { BYTE_SWAP (vd, vd_next); BYTE_SWAP (vd, vd_ndx); } offset += vd.vd_next; } while (vd.vd_ndx != (p[cnt + j] & 0x7fff) && vd.vd_next != 0); if (vd.vd_ndx == (p[cnt + j] & 0x7fff)) { if (must_swap) BYTE_SWAP (vd, vd_aux); GET_DATA (offset + vd.vd_aux, vda, "version definition aux"); if (must_swap) BYTE_SWAP (vda, vda_name); nn += printf ("(%11.11s)", strtab + vda.vda_name); } } break; } else offset += vn.vn_next; } while (vn.vn_next); } else if (symbols [cnt + j].st_shndx == SHN_UNDEF) { Elf_Vernaux a; Elf_Verneed v; unsigned long offset; offset = version_info [DT_VERSIONTAGIDX (DT_VERNEED)] - loadaddr; do { unsigned long a_off; GET_DATA (offset, v, "version need"); if (must_swap) { BYTE_SWAP (v, vn_aux); BYTE_SWAP (v, vn_next); } a_off = offset + v.vn_aux; do { GET_DATA (a_off, a, "version need aux (2)"); if (must_swap) { BYTE_SWAP (a, vna_next); BYTE_SWAP (a, vna_other); } a_off += a.vna_next; } while (a.vna_other != p[cnt + j] && a.vna_next != 0); if (a.vna_other == p[cnt + j]) { if (must_swap) BYTE_SWAP (a, vna_name); nn += printf ("(%11.11s)", strtab + a.vna_name); break; } offset += v.vn_next; } while (v.vn_next); } else { if (p[cnt + j] != 0x8001) { Elf_Verdaux a; Elf_Verdef v; unsigned long offset; offset = version_info [DT_VERSIONTAGIDX (DT_VERDEF)] - loadaddr; do { GET_DATA (offset, v, "version def"); if (must_swap) { BYTE_SWAP (v, vd_next); BYTE_SWAP (v, vd_ndx); } offset += v.vd_next; } while (v.vd_ndx != (p[cnt + j] & 0x7fff) && v.vd_next != 0); if (v.vd_ndx == (p[cnt + j] & 0x7fff)) { if (must_swap) BYTE_SWAP (v, vd_aux); GET_DATA (offset - v.vd_next + v.vd_aux, a, "version def aux"); if (must_swap) BYTE_SWAP (a, vda_name); nn += printf ("(%11.11s)", strtab + a.vda_name); } } } if (nn < 18) printf ("%*c", 18 - nn, ' '); } putchar ('\n'); } free (p); free (strtab); free (symbols); } break; default: } } if (! found) printf (_("\nNo version information found in this file.\n")); } static char * get_symbol_binding (binding) unsigned int binding; { static char buff [32]; switch (binding) { case STB_LOCAL: return _("LOCAL"); case STB_GLOBAL: return _("GLOBAL"); case STB_WEAK: return _("WEAK"); default: if (binding >= STB_LOPROC && binding <= STB_HIPROC) sprintf (buff, _(": %d"), binding); else sprintf (buff, _(": %d"), binding); return buff; } } static char * get_symbol_type (type) unsigned int type; { static char buff [32]; switch (type) { case STT_NOTYPE: return _("NOTYPE"); case STT_OBJECT: return _("OBJECT"); case STT_FUNC: return _("FUNC"); case STT_SECTION: return _("SECTION"); case STT_FILE: return _("FILE"); default: if (type >= STT_LOPROC && type <= STT_HIPROC) sprintf (buff, _(": %d"), type); else sprintf (buff, _(": %d"), type); return buff; } } static char * get_symbol_index_type (type) unsigned short type; { switch (type) { case SHN_UNDEF: return "UND"; case SHN_ABS: return "ABS"; case SHN_COMMON: return "COM"; default: if (type >= SHN_LOPROC && type <= SHN_HIPROC) return "PRC"; else if (type >= SHN_LORESERVE && type <= SHN_HIRESERVE) return "RSV"; else { static char buff [32]; sprintf (buff, "%3d", type); return buff; } } } /* Dump the symbol table */ static void process_symbol_table (file) FILE * file; { Elf_Shdr * section; if (! do_syms) return; if (dynamic_info [DT_HASH] && do_using_dynamic && dynamic_strings != NULL) { int nbuckets; int nchains; int * buckets; int * chains; int hn; int si; if (fseek (file, dynamic_info [DT_HASH] - loadaddr, SEEK_SET)) { error (_("Unable to seek to start of dynamic information")); return; } if (fread (& nbuckets, sizeof (nbuckets), 1, file) != 1) { error (_("Failed to read in number of buckets\n")); return; } if (fread (& nchains, sizeof (nchains), 1, file) != 1) { error (_("Failed to read in number of chains\n")); return; } if (must_swap) { nbuckets = SWAP4 (nbuckets); nchains = SWAP4 (nchains); } buckets = (int *) malloc (nbuckets * sizeof (* buckets)); if (buckets == NULL) { error (_("Failed to allocate space for buckets\n")); return; } if (fread (buckets, sizeof (* buckets), nbuckets, file) != nbuckets) { error (_("Unable to read in buckets\n")); return; } chains = (int *) malloc (nchains * sizeof (* chains)); if (chains == NULL) { error (_("Failed to allocate space for chains\n")); free (buckets); return; } if (fread (chains, sizeof (* chains), nchains, file) != nchains) { error (_("Unable to read in chains!\n")); free (buckets); return; } if (must_swap) { for (si = nbuckets; si--;) buckets [si] = SWAP4 (buckets [si]); for (si = nchains; si--;) chains [si] = SWAP4 (chains [si]); } printf (_("\nSymbol table for image:\n")); printf (_(" Num Buc: Value Size Type Bind Ot Ndx Name\n")); for (hn = 0; hn < nbuckets; hn++) { if (! buckets [hn]) continue; for (si = buckets [hn]; si; si = chains [si]) { Elf_Sym * psym; psym = dynamic_symbols + si; printf (" %3d %3d: %8lx %5ld %6s %6s %2d ", si, hn, psym->st_value, psym->st_size, get_symbol_type (ELF_ST_TYPE (psym->st_info)), get_symbol_binding (ELF_ST_BIND (psym->st_info)), psym->st_other); printf ("%3.3s", get_symbol_index_type (psym->st_shndx)); printf (" %s\n", dynamic_strings + psym->st_name); } } free (buckets); free (chains); } else if (!do_using_dynamic) { unsigned int i; for (i = 0, section = section_headers; i < elf_header.e_shnum; i++, section++) { unsigned int si; char * strtab; Elf_Sym * symtab; Elf_Sym * psym; if ( section->sh_type != SHT_SYMTAB && section->sh_type != SHT_DYNSYM) continue; printf (_("\nSymbol table '%s' contains %d entries:\n"), SECTION_NAME (section), section->sh_size / section->sh_entsize); printf (_(" Num: Value Size Type Bind Ot Ndx Name\n")); GET_DATA_ALLOC (section->sh_offset, section->sh_size, symtab, Elf_Sym *, "symbol section"); if (must_swap) { for (si = 0; si < section->sh_size / section->sh_entsize; si ++) { BYTE_SWAP (symtab [si], st_name); BYTE_SWAP (symtab [si], st_value); BYTE_SWAP (symtab [si], st_size); BYTE_SWAP (symtab [si], st_shndx); } } if (section->sh_link == elf_header.e_shstrndx) strtab = string_table; else { Elf_Shdr * string_sec = section_headers + section->sh_link; GET_DATA_ALLOC (string_sec->sh_offset, string_sec->sh_size, strtab, char *, "string table"); } for (si = 0, psym = symtab; si < section->sh_size / section->sh_entsize; si ++, psym ++) { printf (" %3d: %8lx %5ld %-7s %-6s %2d ", si, psym->st_value, psym->st_size, get_symbol_type (ELF_ST_TYPE (psym->st_info)), get_symbol_binding (ELF_ST_BIND (psym->st_info)), psym->st_other); if (psym->st_shndx == 0) printf ("UND"); else if ((psym->st_shndx & 0xffff) == 0xfff1) printf ("ABS"); else if ((psym->st_shndx & 0xffff) == 0xfff2) printf ("COM"); else printf ("%3d", psym->st_shndx); printf (" %s", strtab + psym->st_name); if (section->sh_type == SHT_DYNSYM && version_info [DT_VERSIONTAGIDX (DT_VERSYM)] != 0) { unsigned short vers_data; unsigned long offset; int is_nobits; int check_def; offset = version_info [DT_VERSIONTAGIDX (DT_VERSYM)] - loadaddr; GET_DATA (offset + si * sizeof (vers_data), vers_data, "version data"); if (must_swap) vers_data = SWAP2 (vers_data); is_nobits = psym->st_shndx < SHN_LORESERVE ? (section_headers [psym->st_shndx].sh_type == SHT_NOBITS) : 0; check_def = (psym->st_shndx != SHN_UNDEF); if ((vers_data & 0x8000) || vers_data > 1) { if (is_nobits || ! check_def) { Elf_Verneed vn; Elf_Vernaux vna; /* We must test both. */ offset = version_info [DT_VERSIONTAGIDX (DT_VERNEED)] - loadaddr; GET_DATA (offset, vn, "version need"); if (must_swap) { BYTE_SWAP (vn, vn_aux); BYTE_SWAP (vn, vn_next); } do { unsigned long vna_off; vna_off = offset + vn.vn_aux; do { GET_DATA (vna_off, vna, "version need aux (3)"); if (must_swap) { BYTE_SWAP (vna, vna_other); BYTE_SWAP (vna, vna_next); } vna_off += vna.vna_next; } while (vna.vna_other != vers_data && vna.vna_next != 0); if (vna.vna_other == vers_data) break; offset += vn.vn_next; } while (vn.vn_next != 0); if (vna.vna_other == vers_data) printf ("@%s (%d)", strtab + vna.vna_name, vna.vna_other); else if (! is_nobits) error (_("bad dynamic symbol")); else check_def = 1; } if (check_def) { if (vers_data != 0x8001) { Elf_Verdef vd; Elf_Verdaux vda; unsigned long offset; offset = version_info [DT_VERSIONTAGIDX (DT_VERDEF)] - loadaddr; do { GET_DATA (offset, vd, "version def"); if (must_swap) { BYTE_SWAP (vd, vd_ndx); BYTE_SWAP (vd, vd_aux); BYTE_SWAP (vd, vd_next); } offset += vd.vd_next; } while (vd.vd_ndx != (vers_data & 0x7fff) && vd.vd_next != 0); offset -= vd.vd_next; offset += vd.vd_aux; GET_DATA (offset, vda, "version def aux"); if (must_swap) BYTE_SWAP (vda, vda_name); if (psym->st_name != vda.vda_name) printf ((vers_data & 0x8000) ? "@%s" : "@@%s", strtab + vda.vda_name); } } } } putchar ('\n'); } free (symtab); if (strtab != string_table) free (strtab); } } else printf (_("\nDynamic symbol information is not available for displaying symbols.\n")); } static void process_section_contents (file) FILE * file; { Elf_Shdr * section; unsigned int i; if (! do_dump) return; for (i = 0, section = section_headers; i < elf_header.e_shnum; i ++, section ++) { #ifdef SUPPORT_DISASSEMBLY /* See if we need an assembly dump of this section */ if ((i < NUM_DUMP_SECTS) && (dump_sects[i] & DISASS_DUMP)) { printf (_("\nAssembly dump of section %s\n"), SECTION_NAME (section)); /* XXX -- to be done --- XXX */ } #endif /* See if we need a hex dump of this section. */ if ((i < NUM_DUMP_SECTS) && (dump_sects[i] & HEX_DUMP)) { int bytes; int addr; unsigned char * data; char * start; printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section)); bytes = section->sh_size; addr = section->sh_addr; GET_DATA_ALLOC (section->sh_offset, bytes, start, char *, "section data"); data = start; while (bytes) { int j; int k; int lbytes; lbytes = (bytes > 16 ? 16 : bytes); printf (" 0x%8.8x ", addr); switch (elf_header.e_ident [EI_DATA]) { case ELFDATA2LSB: for (j = 15; j >= 0; j --) { if (j < lbytes) printf ("%2.2x", data [j]); else printf (" "); if (!(j & 0x3)) printf (" "); } break; case ELFDATA2MSB: for (j = 0; j < 16; j++) { if (j < lbytes) printf ("%2.2x", data [j]); else printf (" "); if ((j & 3) == 3) printf (" "); } break; } for (j = 0; j < lbytes; j++) { k = data [j]; if (k >= ' ' && k < 0x80) printf ("%c", k); else printf ("."); } putchar ('\n'); data += lbytes; addr += lbytes; bytes -= lbytes; } free (start); } } } static void process_file (file_name) char * file_name; { FILE * file; struct stat statbuf; unsigned int i; if (stat (file_name, & statbuf) < 0) { error (_("Cannot stat input file %s.\n"), file_name); return; } file = fopen (file_name, "rb"); if (file == NULL) { error (_("Input file %s not found.\n"), file_name); return; } if (fread (& elf_header, sizeof (elf_header), 1, file) != 1) { error (_("%s: Failed to read file header\n"), file_name); fclose (file); return; } /* Initialise per file variables. */ must_swap = 0; for (i = NUM_ELEM (version_info); i--;) version_info [i] = 0; for (i = NUM_ELEM (dynamic_info); i--;) dynamic_info [i] = 0; /* Process the file. */ if (show_name) printf (_("\nFile: %s\n"), file_name); if (! process_file_header ()) { fclose (file); return; } process_section_headers (file); process_program_headers (file); process_dynamic_segment (file); process_relocs (file); process_symbol_table (file); process_version_sections (file); process_section_contents (file); fclose (file); if (section_headers) { free (section_headers); section_headers = NULL; } if (string_table) { free (string_table); string_table = NULL; } if (dynamic_strings) { free (dynamic_strings); dynamic_strings = NULL; } if (dynamic_symbols) { free (dynamic_symbols); dynamic_symbols = NULL; } } #ifdef SUPPORT_DISASSEMBLY /* Needed by the i386 disassembler. For extra credit, someone could fix this so that we insert symbolic addresses here, esp for GOT/PLT symbols */ void print_address (unsigned int addr, FILE * outfile) { fprintf (outfile,"0x%8.8x", addr); } /* Needed by the i386 disassembler. */ void db_task_printsym (unsigned int addr) { print_address (addr, stderr); } #endif int main (argc, argv) int argc; char ** argv; { parse_args (argc, argv); expected_endian = 0x12345678; if (* ((char *) & expected_endian) == 0x12) expected_endian = ELFDATA2MSB; else expected_endian = ELFDATA2LSB; if (optind < (argc - 1)) show_name = 1; while (optind < argc) process_file (argv [optind ++]); return 0; }