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