12 def __init__(self, path):
14 # Snarf all the data so we can seek.
15 self.file = StringIO.StringIO(sys.stdin.read())
17 self.file = open(path, "rb")
25 data = self.file.read(N)
27 raise ValueError, "Out of data!"
31 return ord(self.read(1))
34 return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
37 return struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0]
40 return struct.unpack('><'[self.isLSB] + 'i', self.read(4))[0]
43 return struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0]
46 return struct.unpack('><'[self.isLSB] + 'q', self.read(8))[0]
61 def __init__(self, strings):
62 self.string_table = strings
64 def __getitem__(self, index):
65 end = self.string_table.index('\x00', index)
66 return self.string_table[index:end]
69 def __init__(self, f):
70 self.sh_name = f.read32()
71 self.sh_type = f.read32()
72 self.sh_flags = f.readWord()
73 self.sh_addr = f.readWord()
74 self.sh_offset = f.readWord()
75 self.sh_size = f.readWord()
76 self.sh_link = f.read32()
77 self.sh_info = f.read32()
78 self.sh_addralign = f.readWord()
79 self.sh_entsize = f.readWord()
81 def dump(self, shstrtab, f, strtab, dumpdata):
82 print " (('sh_name', %s)" % common_dump.HexDump(self.sh_name), "# %r" % shstrtab[self.sh_name]
83 print " ('sh_type', %s)" % common_dump.HexDump(self.sh_type)
84 print " ('sh_flags', %s)" % common_dump.HexDump(self.sh_flags)
85 print " ('sh_addr', %s)" % common_dump.HexDump(self.sh_addr)
86 print " ('sh_offset', %s)" % common_dump.HexDump(self.sh_offset)
87 print " ('sh_size', %s)" % common_dump.HexDump(self.sh_size)
88 print " ('sh_link', %s)" % common_dump.HexDump(self.sh_link)
89 print " ('sh_info', %s)" % common_dump.HexDump(self.sh_info)
90 print " ('sh_addralign', %s)" % common_dump.HexDump(self.sh_addralign)
91 print " ('sh_entsize', %s)" % common_dump.HexDump(self.sh_entsize)
92 if self.sh_type == 2: # SHT_SYMTAB
93 print " ('_symbols', ["
94 dumpSymtab(f, self, strtab)
96 elif self.sh_type == 4 or self.sh_type == 9: # SHT_RELA / SHT_REL
97 print " ('_relocations', ["
98 dumpRel(f, self, self.sh_type == 4)
101 f.seek(self.sh_offset)
102 data = f.read(self.sh_size)
103 print " ('_section_data', '%s')" % common_dump.dataToHex(data)
106 def dumpSymtab(f, section, strtab):
107 entries = section.sh_size // section.sh_entsize
109 for index in range(entries):
110 f.seek(section.sh_offset + index * section.sh_entsize)
111 print " # Symbol %s" % common_dump.HexDump(index)
113 print " (('st_name', %s)" % common_dump.HexDump(name), "# %r" % strtab[name]
115 print " ('st_value', %s)" % common_dump.HexDump(f.read32())
116 print " ('st_size', %s)" % common_dump.HexDump(f.read32())
118 print " ('st_bind', %s)" % common_dump.HexDump((st_info >> 4))
119 print " ('st_type', %s)" % common_dump.HexDump((st_info & 0xf))
120 print " ('st_other', %s)" % common_dump.HexDump(f.read8())
121 print " ('st_shndx', %s)" % common_dump.HexDump(f.read16())
123 print " ('st_value', %s)" % common_dump.HexDump(f.read64())
124 print " ('st_size', %s)" % common_dump.HexDump(f.read64())
127 def dumpRel(f, section, dumprela = False):
128 entries = section.sh_size // section.sh_entsize
130 for index in range(entries):
131 f.seek(section.sh_offset + index * section.sh_entsize)
132 print " # Relocation %s" % common_dump.HexDump(index)
133 print " (('r_offset', %s)" % common_dump.HexDump(f.readWord())
134 r_info = f.readWord()
136 print " ('r_sym', %s)" % common_dump.HexDump((r_info >> 32))
137 print " ('r_type', %s)" % common_dump.HexDump((r_info & 0xffffffff))
139 print " ('r_sym', %s)" % common_dump.HexDump((r_info >> 8))
140 print " ('r_type', %s)" % common_dump.HexDump((r_info & 0xff))
142 print " ('r_addend', %s)" % common_dump.HexDump(f.readWordS())
145 def dumpELF(path, opts):
149 assert magic == '\x7FELF'
151 fileclass = f.read8()
152 if fileclass == 1: # ELFCLASS32
154 elif fileclass == 2: # ELFCLASS64
157 raise ValueError, "Unknown file class %s" % common_dump.HexDump(fileclass)
158 print "('e_indent[EI_CLASS]', %s)" % common_dump.HexDump(fileclass)
160 byteordering = f.read8()
161 if byteordering == 1: # ELFDATA2LSB
163 elif byteordering == 2: # ELFDATA2MSB
166 raise ValueError, "Unknown byte ordering %s" % common_dump.HexDump(byteordering)
167 print "('e_indent[EI_DATA]', %s)" % common_dump.HexDump(byteordering)
169 print "('e_indent[EI_VERSION]', %s)" % common_dump.HexDump(f.read8())
170 print "('e_indent[EI_OSABI]', %s)" % common_dump.HexDump(f.read8())
171 print "('e_indent[EI_ABIVERSION]', %s)" % common_dump.HexDump(f.read8())
173 f.seek(16) # Seek to end of e_ident.
175 print "('e_type', %s)" % common_dump.HexDump(f.read16())
176 print "('e_machine', %s)" % common_dump.HexDump(f.read16())
177 print "('e_version', %s)" % common_dump.HexDump(f.read32())
178 print "('e_entry', %s)" % common_dump.HexDump(f.readWord())
179 print "('e_phoff', %s)" % common_dump.HexDump(f.readWord())
180 e_shoff = f.readWord()
181 print "('e_shoff', %s)" % common_dump.HexDump(e_shoff)
182 print "('e_flags', %s)" % common_dump.HexDump(f.read32())
183 print "('e_ehsize', %s)" % common_dump.HexDump(f.read16())
184 print "('e_phentsize', %s)" % common_dump.HexDump(f.read16())
185 print "('e_phnum', %s)" % common_dump.HexDump(f.read16())
186 e_shentsize = f.read16()
187 print "('e_shentsize', %s)" % common_dump.HexDump(e_shentsize)
189 print "('e_shnum', %s)" % common_dump.HexDump(e_shnum)
190 e_shstrndx = f.read16()
191 print "('e_shstrndx', %s)" % common_dump.HexDump(e_shstrndx)
193 # Read all section headers
195 for index in range(e_shnum):
196 f.seek(e_shoff + index * e_shentsize)
200 # Read .shstrtab so we can resolve section names
201 f.seek(sections[e_shstrndx].sh_offset)
202 shstrtab = StringTable(f.read(sections[e_shstrndx].sh_size))
204 # Get the symbol string table
206 for section in sections:
207 if shstrtab[section.sh_name] == ".strtab":
208 f.seek(section.sh_offset)
209 strtab = StringTable(f.read(section.sh_size))
212 print "('_sections', ["
213 for index in range(e_shnum):
214 print " # Section %s" % common_dump.HexDump(index)
215 sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
218 if __name__ == "__main__":
219 from optparse import OptionParser, OptionGroup
220 parser = OptionParser("usage: %prog [options] {files}")
221 parser.add_option("", "--dump-section-data", dest="dumpSectionData",
222 help="Dump the contents of sections",
223 action="store_true", default=False)
224 (opts, args) = parser.parse_args()