Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import struct |
| 4 | import sys |
| 5 | import StringIO |
| 6 | |
Rafael Espindola | 228290c | 2010-09-11 15:25:58 +0000 | [diff] [blame] | 7 | import common_dump |
| 8 | |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 9 | class Reader: |
| 10 | def __init__(self, path): |
| 11 | if path == "-": |
| 12 | # Snarf all the data so we can seek. |
| 13 | self.file = StringIO.StringIO(sys.stdin.read()) |
| 14 | else: |
| 15 | self.file = open(path, "rb") |
| 16 | self.isLSB = None |
| 17 | self.is64Bit = None |
Jack Carter | 0140e55 | 2012-06-27 22:48:25 +0000 | [diff] [blame^] | 18 | self.isN64 = False |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 19 | |
| 20 | def seek(self, pos): |
| 21 | self.file.seek(pos) |
| 22 | |
| 23 | def read(self, N): |
| 24 | data = self.file.read(N) |
| 25 | if len(data) != N: |
| 26 | raise ValueError, "Out of data!" |
| 27 | return data |
| 28 | |
| 29 | def read8(self): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 30 | return (ord(self.read(1)), 8) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 31 | |
| 32 | def read16(self): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 33 | return (struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0], 16) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 34 | |
| 35 | def read32(self): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 36 | return (struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0], 32) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 37 | |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 38 | def read64(self): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 39 | return (struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0], 64) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 40 | |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 41 | def readWord(self): |
| 42 | if self.is64Bit: |
| 43 | return self.read64() |
| 44 | else: |
| 45 | return self.read32() |
| 46 | |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 47 | class StringTable: |
| 48 | def __init__(self, strings): |
| 49 | self.string_table = strings |
| 50 | |
| 51 | def __getitem__(self, index): |
| 52 | end = self.string_table.index('\x00', index) |
| 53 | return self.string_table[index:end] |
| 54 | |
| 55 | class Section: |
| 56 | def __init__(self, f): |
| 57 | self.sh_name = f.read32() |
| 58 | self.sh_type = f.read32() |
| 59 | self.sh_flags = f.readWord() |
| 60 | self.sh_addr = f.readWord() |
| 61 | self.sh_offset = f.readWord() |
| 62 | self.sh_size = f.readWord() |
| 63 | self.sh_link = f.read32() |
| 64 | self.sh_info = f.read32() |
| 65 | self.sh_addralign = f.readWord() |
| 66 | self.sh_entsize = f.readWord() |
| 67 | |
| 68 | def dump(self, shstrtab, f, strtab, dumpdata): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 69 | print " (('sh_name', %s)" % common_dump.HexDump(self.sh_name), "# %r" % shstrtab[self.sh_name[0]] |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 70 | print " ('sh_type', %s)" % common_dump.HexDump(self.sh_type) |
| 71 | print " ('sh_flags', %s)" % common_dump.HexDump(self.sh_flags) |
| 72 | print " ('sh_addr', %s)" % common_dump.HexDump(self.sh_addr) |
| 73 | print " ('sh_offset', %s)" % common_dump.HexDump(self.sh_offset) |
| 74 | print " ('sh_size', %s)" % common_dump.HexDump(self.sh_size) |
| 75 | print " ('sh_link', %s)" % common_dump.HexDump(self.sh_link) |
| 76 | print " ('sh_info', %s)" % common_dump.HexDump(self.sh_info) |
| 77 | print " ('sh_addralign', %s)" % common_dump.HexDump(self.sh_addralign) |
| 78 | print " ('sh_entsize', %s)" % common_dump.HexDump(self.sh_entsize) |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 79 | if self.sh_type[0] == 2: # SHT_SYMTAB |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 80 | print " ('_symbols', [" |
| 81 | dumpSymtab(f, self, strtab) |
| 82 | print " ])" |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 83 | elif self.sh_type[0] == 4 or self.sh_type[0] == 9: # SHT_RELA / SHT_REL |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 84 | print " ('_relocations', [" |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 85 | dumpRel(f, self, self.sh_type[0] == 4) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 86 | print " ])" |
| 87 | elif dumpdata: |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 88 | f.seek(self.sh_offset[0]) |
Jason W Kim | 6147bda | 2010-12-16 00:15:10 +0000 | [diff] [blame] | 89 | if self.sh_type != 8: # != SHT_NOBITS |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 90 | data = f.read(self.sh_size[0]) |
Jason W Kim | 6147bda | 2010-12-16 00:15:10 +0000 | [diff] [blame] | 91 | print " ('_section_data', '%s')" % common_dump.dataToHex(data) |
| 92 | else: |
| 93 | print " ('_section_data', '')" |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 94 | print " )," |
| 95 | |
| 96 | def dumpSymtab(f, section, strtab): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 97 | entries = section.sh_size[0] // section.sh_entsize[0] |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 98 | |
| 99 | for index in range(entries): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 100 | f.seek(section.sh_offset[0] + index * section.sh_entsize[0]) |
Rafael Espindola | 65ad8dc | 2011-08-04 14:27:46 +0000 | [diff] [blame] | 101 | print " # Symbol %s" % index |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 102 | name = f.read32() |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 103 | print " (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name[0]] |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 104 | if not f.is64Bit: |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 105 | print " ('st_value', %s)" % common_dump.HexDump(f.read32()) |
| 106 | print " ('st_size', %s)" % common_dump.HexDump(f.read32()) |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 107 | st_info = f.read8()[0] |
Rafael Espindola | d7c2783 | 2011-08-04 15:10:35 +0000 | [diff] [blame] | 108 | st_bind = (st_info >> 4, 4) |
| 109 | st_type = (st_info & 0xf, 4) |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 110 | print " ('st_bind', %s)" % common_dump.HexDump(st_bind) |
| 111 | print " ('st_type', %s)" % common_dump.HexDump(st_type) |
| 112 | print " ('st_other', %s)" % common_dump.HexDump(f.read8()) |
| 113 | print " ('st_shndx', %s)" % common_dump.HexDump(f.read16()) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 114 | if f.is64Bit: |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 115 | print " ('st_value', %s)" % common_dump.HexDump(f.read64()) |
| 116 | print " ('st_size', %s)" % common_dump.HexDump(f.read64()) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 117 | print " )," |
| 118 | |
| 119 | def dumpRel(f, section, dumprela = False): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 120 | entries = section.sh_size[0] // section.sh_entsize[0] |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 121 | |
| 122 | for index in range(entries): |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 123 | f.seek(section.sh_offset[0] + index * section.sh_entsize[0]) |
Rafael Espindola | f7179de | 2011-08-04 14:01:03 +0000 | [diff] [blame] | 124 | print " # Relocation %s" % index |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 125 | print " (('r_offset', %s)" % common_dump.HexDump(f.readWord()) |
Jack Carter | 0140e55 | 2012-06-27 22:48:25 +0000 | [diff] [blame^] | 126 | |
| 127 | if f.isN64: |
| 128 | r_sym = f.read32() |
| 129 | r_ssym = f.read8() |
| 130 | r_type3 = f.read8() |
| 131 | r_type2 = f.read8() |
| 132 | r_type = f.read8() |
| 133 | print " ('r_sym', %s)" % common_dump.HexDump(r_sym) |
| 134 | print " ('r_ssym', %s)" % common_dump.HexDump(r_ssym) |
| 135 | print " ('r_type3', %s)" % common_dump.HexDump(r_type3) |
| 136 | print " ('r_type2', %s)" % common_dump.HexDump(r_type2) |
| 137 | print " ('r_type', %s)" % common_dump.HexDump(r_type) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 138 | else: |
Jack Carter | 0140e55 | 2012-06-27 22:48:25 +0000 | [diff] [blame^] | 139 | r_info = f.readWord()[0] |
| 140 | if f.is64Bit: |
| 141 | r_sym = (r_info >> 32, 32) |
| 142 | r_type = (r_info & 0xffffffff, 32) |
| 143 | else: |
| 144 | r_sym = (r_info >> 8, 24) |
| 145 | r_type = (r_info & 0xff, 8) |
| 146 | print " ('r_sym', %s)" % common_dump.HexDump(r_sym) |
| 147 | print " ('r_type', %s)" % common_dump.HexDump(r_type) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 148 | if dumprela: |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 149 | print " ('r_addend', %s)" % common_dump.HexDump(f.readWord()) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 150 | print " )," |
| 151 | |
| 152 | def dumpELF(path, opts): |
| 153 | f = Reader(path) |
| 154 | |
| 155 | magic = f.read(4) |
| 156 | assert magic == '\x7FELF' |
| 157 | |
| 158 | fileclass = f.read8() |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 159 | if fileclass[0] == 1: # ELFCLASS32 |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 160 | f.is64Bit = False |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 161 | elif fileclass[0] == 2: # ELFCLASS64 |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 162 | f.is64Bit = True |
| 163 | else: |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 164 | raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass) |
| 165 | print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 166 | |
| 167 | byteordering = f.read8() |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 168 | if byteordering[0] == 1: # ELFDATA2LSB |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 169 | f.isLSB = True |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 170 | elif byteordering[0] == 2: # ELFDATA2MSB |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 171 | f.isLSB = False |
| 172 | else: |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 173 | raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering) |
| 174 | print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 175 | |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 176 | print "('e_indent[EI_VERSION]', %s)" % common_dump.HexDump(f.read8()) |
| 177 | print "('e_indent[EI_OSABI]', %s)" % common_dump.HexDump(f.read8()) |
| 178 | print "('e_indent[EI_ABIVERSION]', %s)" % common_dump.HexDump(f.read8()) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 179 | |
| 180 | f.seek(16) # Seek to end of e_ident. |
| 181 | |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 182 | print "('e_type', %s)" % common_dump.HexDump(f.read16()) |
Jack Carter | 0140e55 | 2012-06-27 22:48:25 +0000 | [diff] [blame^] | 183 | |
| 184 | # Does any other architecture use N64? |
| 185 | e_machine = f.read16() |
| 186 | if e_machine[0] == 0x0008 and f.is64Bit: # EM_MIPS && 64 bit |
| 187 | f.isN64 = True |
| 188 | |
| 189 | print "('e_machine', %s)" % common_dump.HexDump(e_machine) |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 190 | print "('e_version', %s)" % common_dump.HexDump(f.read32()) |
| 191 | print "('e_entry', %s)" % common_dump.HexDump(f.readWord()) |
| 192 | print "('e_phoff', %s)" % common_dump.HexDump(f.readWord()) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 193 | e_shoff = f.readWord() |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 194 | print "('e_shoff', %s)" % common_dump.HexDump(e_shoff) |
| 195 | print "('e_flags', %s)" % common_dump.HexDump(f.read32()) |
| 196 | print "('e_ehsize', %s)" % common_dump.HexDump(f.read16()) |
| 197 | print "('e_phentsize', %s)" % common_dump.HexDump(f.read16()) |
| 198 | print "('e_phnum', %s)" % common_dump.HexDump(f.read16()) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 199 | e_shentsize = f.read16() |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 200 | print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 201 | e_shnum = f.read16() |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 202 | print "('e_shnum', %s)" % common_dump.HexDump(e_shnum) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 203 | e_shstrndx = f.read16() |
Jason W Kim | f7d5278 | 2010-10-19 17:39:10 +0000 | [diff] [blame] | 204 | print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 205 | |
| 206 | # Read all section headers |
| 207 | sections = [] |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 208 | for index in range(e_shnum[0]): |
| 209 | f.seek(e_shoff[0] + index * e_shentsize[0]) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 210 | s = Section(f) |
| 211 | sections.append(s) |
| 212 | |
| 213 | # Read .shstrtab so we can resolve section names |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 214 | f.seek(sections[e_shstrndx[0]].sh_offset[0]) |
| 215 | shstrtab = StringTable(f.read(sections[e_shstrndx[0]].sh_size[0])) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 216 | |
| 217 | # Get the symbol string table |
| 218 | strtab = None |
| 219 | for section in sections: |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 220 | if shstrtab[section.sh_name[0]] == ".strtab": |
| 221 | f.seek(section.sh_offset[0]) |
| 222 | strtab = StringTable(f.read(section.sh_size[0])) |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 223 | break |
| 224 | |
| 225 | print "('_sections', [" |
Rafael Espindola | 5c4e52e | 2011-08-04 17:00:11 +0000 | [diff] [blame] | 226 | for index in range(e_shnum[0]): |
Rafael Espindola | 014180d | 2011-08-04 13:39:15 +0000 | [diff] [blame] | 227 | print " # Section %s" % index |
Benjamin Kramer | a754be4 | 2010-09-09 15:00:41 +0000 | [diff] [blame] | 228 | sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData) |
| 229 | print "])" |
| 230 | |
| 231 | if __name__ == "__main__": |
| 232 | from optparse import OptionParser, OptionGroup |
| 233 | parser = OptionParser("usage: %prog [options] {files}") |
| 234 | parser.add_option("", "--dump-section-data", dest="dumpSectionData", |
| 235 | help="Dump the contents of sections", |
| 236 | action="store_true", default=False) |
| 237 | (opts, args) = parser.parse_args() |
| 238 | |
| 239 | if not args: |
| 240 | args.append('-') |
| 241 | |
| 242 | for arg in args: |
| 243 | dumpELF(arg, opts) |