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