10 def __init__(self, path):
12 # Snarf all the data so we can seek.
13 self.file = StringIO.StringIO(sys.stdin.read())
15 self.file = open(path, "rb")
23 data = self.file.read(N)
25 raise ValueError, "Out of data!"
29 return ord(self.read(1))
32 return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
35 return struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0]
38 return struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0]
47 def __init__(self, strings):
48 self.string_table = strings
50 def __getitem__(self, index):
51 end = self.string_table.index('\x00', index)
52 return self.string_table[index:end]
55 def __init__(self, f):
56 self.sh_name = f.read32()
57 self.sh_type = f.read32()
58 self.sh_flags = f.readWord()
59 self.sh_addr = f.readWord()
60 self.sh_offset = f.readWord()
61 self.sh_size = f.readWord()
62 self.sh_link = f.read32()
63 self.sh_info = f.read32()
64 self.sh_addralign = f.readWord()
65 self.sh_entsize = f.readWord()
67 def dump(self, shstrtab, f, strtab, dumpdata):
68 print " (('sh_name', %s)" % common_dump.HexDump(self.sh_name), "# %r" % shstrtab[self.sh_name]
69 print " ('sh_type', %s)" % common_dump.HexDump(self.sh_type)
70 print " ('sh_flags', %s)" % common_dump.HexDump(self.sh_flags)
71 print " ('sh_addr', %s)" % common_dump.HexDump(self.sh_addr)
72 print " ('sh_offset', %s)" % common_dump.HexDump(self.sh_offset)
73 print " ('sh_size', %s)" % common_dump.HexDump(self.sh_size)
74 print " ('sh_link', %s)" % common_dump.HexDump(self.sh_link)
75 print " ('sh_info', %s)" % common_dump.HexDump(self.sh_info)
76 print " ('sh_addralign', %s)" % common_dump.HexDump(self.sh_addralign)
77 print " ('sh_entsize', %s)" % common_dump.HexDump(self.sh_entsize)
78 if self.sh_type == 2: # SHT_SYMTAB
79 print " ('_symbols', ["
80 dumpSymtab(f, self, strtab)
82 elif self.sh_type == 4 or self.sh_type == 9: # SHT_RELA / SHT_REL
83 print " ('_relocations', ["
84 dumpRel(f, self, self.sh_type == 4)
87 f.seek(self.sh_offset)
88 if self.sh_type != 8: # != SHT_NOBITS
89 data = f.read(self.sh_size)
90 print " ('_section_data', '%s')" % common_dump.dataToHex(data)
92 print " ('_section_data', '')"
95 def dumpSymtab(f, section, strtab):
96 entries = section.sh_size // section.sh_entsize
98 for index in range(entries):
99 f.seek(section.sh_offset + index * section.sh_entsize)
100 print " # Symbol %s" % index
102 print " (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name]
104 print " ('st_value', %s)" % common_dump.HexDump(f.read32())
105 print " ('st_size', %s)" % common_dump.HexDump(f.read32())
107 print " ('st_bind', %s)" % common_dump.HexDump((st_info >> 4))
108 print " ('st_type', %s)" % common_dump.HexDump((st_info & 0xf))
109 print " ('st_other', %s)" % common_dump.HexDump(f.read8())
110 print " ('st_shndx', %s)" % common_dump.HexDump(f.read16())
112 print " ('st_value', %s)" % common_dump.HexDump(f.read64(), 64)
113 print " ('st_size', %s)" % common_dump.HexDump(f.read64(), 64)
116 def dumpRel(f, section, dumprela = False):
117 entries = section.sh_size // section.sh_entsize
119 for index in range(entries):
120 f.seek(section.sh_offset + index * section.sh_entsize)
121 print " # Relocation %s" % index
122 print " (('r_offset', %s)" % common_dump.HexDump(f.readWord())
123 r_info = f.readWord()
125 print " ('r_sym', %s)" % common_dump.HexDump((r_info >> 32))
126 print " ('r_type', %s)" % common_dump.HexDump((r_info & 0xffffffff))
128 print " ('r_sym', %s)" % common_dump.HexDump((r_info >> 8))
129 print " ('r_type', %s)" % common_dump.HexDump((r_info & 0xff))
136 print " ('r_addend', %s)" % common_dump.HexDump(val, numBits)
139 def dumpELF(path, opts):
143 assert magic == '\x7FELF'
145 fileclass = f.read8()
146 if fileclass == 1: # ELFCLASS32
148 elif fileclass == 2: # ELFCLASS64
151 raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass)
152 print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass)
154 byteordering = f.read8()
155 if byteordering == 1: # ELFDATA2LSB
157 elif byteordering == 2: # ELFDATA2MSB
160 raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering)
161 print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering)
163 print "('e_indent[EI_VERSION]', %s)" % common_dump.HexDump(f.read8())
164 print "('e_indent[EI_OSABI]', %s)" % common_dump.HexDump(f.read8())
165 print "('e_indent[EI_ABIVERSION]', %s)" % common_dump.HexDump(f.read8())
167 f.seek(16) # Seek to end of e_ident.
169 print "('e_type', %s)" % common_dump.HexDump(f.read16())
170 print "('e_machine', %s)" % common_dump.HexDump(f.read16())
171 print "('e_version', %s)" % common_dump.HexDump(f.read32())
172 print "('e_entry', %s)" % common_dump.HexDump(f.readWord())
173 print "('e_phoff', %s)" % common_dump.HexDump(f.readWord())
174 e_shoff = f.readWord()
175 print "('e_shoff', %s)" % common_dump.HexDump(e_shoff)
176 print "('e_flags', %s)" % common_dump.HexDump(f.read32())
177 print "('e_ehsize', %s)" % common_dump.HexDump(f.read16())
178 print "('e_phentsize', %s)" % common_dump.HexDump(f.read16())
179 print "('e_phnum', %s)" % common_dump.HexDump(f.read16())
180 e_shentsize = f.read16()
181 print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize)
183 print "('e_shnum', %s)" % common_dump.HexDump(e_shnum)
184 e_shstrndx = f.read16()
185 print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx)
187 # Read all section headers
189 for index in range(e_shnum):
190 f.seek(e_shoff + index * e_shentsize)
194 # Read .shstrtab so we can resolve section names
195 f.seek(sections[e_shstrndx].sh_offset)
196 shstrtab = StringTable(f.read(sections[e_shstrndx].sh_size))
198 # Get the symbol string table
200 for section in sections:
201 if shstrtab[section.sh_name] == ".strtab":
202 f.seek(section.sh_offset)
203 strtab = StringTable(f.read(section.sh_size))
206 print "('_sections', ["
207 for index in range(e_shnum):
208 print " # Section %s" % index
209 sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
212 if __name__ == "__main__":
213 from optparse import OptionParser, OptionGroup
214 parser = OptionParser("usage: %prog [options] {files}")
215 parser.add_option("", "--dump-section-data", dest="dumpSectionData",
216 help="Dump the contents of sections",
217 action="store_true", default=False)
218 (opts, args) = parser.parse_args()