8 def __init__(self, path):
10 # Snarf all the data so we can seek.
11 self.file = StringIO.StringIO(sys.stdin.read())
13 self.file = open(path, "rb")
21 data = self.file.read(N)
23 raise ValueError, "Out of data!"
27 return ord(self.read(1))
30 return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
33 return struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0]
36 return struct.unpack('><'[self.isLSB] + 'i', self.read(4))[0]
39 return struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0]
42 return struct.unpack('><'[self.isLSB] + 'q', self.read(8))[0]
57 def __init__(self, strings):
58 self.string_table = strings
60 def __getitem__(self, index):
61 end = self.string_table.index('\x00', index)
62 return self.string_table[index:end]
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()
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)
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)
97 f.seek(self.sh_offset)
98 print " ('_section_data', %r)" % f.read(self.sh_size)
101 def dumpSymtab(f, section, strtab):
102 entries = section.sh_size // section.sh_entsize
104 for index in range(entries):
105 f.seek(section.sh_offset + index * section.sh_entsize)
106 print " # Symbol %d" % index
108 print " (('st_name', %d) # %r" % (name, strtab[name])
110 print " ('st_value', %d)" % f.read32()
111 print " ('st_size', %d)" % f.read32()
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()
118 print " ('st_value', %d)" % f.read64()
119 print " ('st_size', %d)" % f.read64()
122 def dumpRel(f, section, dumprela = False):
123 entries = section.sh_size // section.sh_entsize
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()
131 print " ('r_sym', %d)" % (r_info >> 32)
132 print " ('r_type', %d)" % (r_info & 0xffffffff)
134 print " ('r_sym', %d)" % (r_info >> 8)
135 print " ('r_type', %d)" % (r_info & 0xff)
137 print " ('r_addend', %d)" % f.readWordS()
140 def dumpELF(path, opts):
144 assert magic == '\x7FELF'
146 fileclass = f.read8()
147 if fileclass == 1: # ELFCLASS32
149 elif fileclass == 2: # ELFCLASS64
152 raise ValueError, "Unknown file class %d" % fileclass
153 print "('e_indent[EI_CLASS]', %d)" % fileclass
155 byteordering = f.read8()
156 if byteordering == 1: # ELFDATA2LSB
158 elif byteordering == 2: # ELFDATA2MSB
161 raise ValueError, "Unknown byte ordering %d" % byteordering
162 print "('e_indent[EI_DATA]', %d)" % byteordering
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()
168 f.seek(16) # Seek to end of e_ident.
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
184 print "('e_shnum', %d)" % e_shnum
185 e_shstrndx = f.read16()
186 print "('e_shstrndx', %d)" % e_shstrndx
188 # Read all section headers
190 for index in range(e_shnum):
191 f.seek(e_shoff + index * e_shentsize)
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))
199 # Get the symbol string table
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))
207 print "('_sections', ["
208 for index in range(e_shnum):
209 print " # Section %d" % index
210 sections[index].dump(shstrtab, f, strtab, opts.dumpSectionData)
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()