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')
16
self.string_table = None
18
def setLSB(self, isLSB):
19
self.isLSB = bool(isLSB)
22
return self.file.tell()
28
data = self.file.read(N)
30
raise ValueError,"Out of data!"
34
return ord(self.read(1))
37
return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
40
# Force to 32-bit, if possible; otherwise these might be long ints on a
41
# big-endian platform. FIXME: Why???
42
Value = struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0]
45
def registerStringTable(self, strings):
46
if self.string_table is not None:
47
raise ValueError,"%s: warning: multiple string tables" % sys.argv[0]
49
self.string_table = strings
51
def getString(self, index):
52
if self.string_table is None:
53
raise ValueError,"%s: warning: no string table registered" % sys.argv[0]
55
end = self.string_table.index('\x00', index)
56
return self.string_table[index:end]
58
def dumpmacho(path, opts):
62
if magic == '\xFE\xED\xFA\xCE':
64
elif magic == '\xCE\xFA\xED\xFE':
67
raise ValueError,"Not a Mach-O object file: %r (bad magic)" % path
69
print "('cputype', %r)" % f.read32()
70
print "('cpusubtype', %r)" % f.read32()
72
print "('filetype', %r)" % filetype
74
numLoadCommands = f.read32()
75
print "('num_load_commands', %r)" % filetype
77
loadCommandsSize = f.read32()
78
print "('load_commands_size', %r)" % loadCommandsSize
80
print "('flag', %r)" % f.read32()
84
print "('load_commands', ["
85
for i in range(numLoadCommands):
86
dumpLoadCommand(f, i, opts)
89
if f.tell() - start != loadCommandsSize:
90
raise ValueError,"%s: warning: invalid load commands size: %r" % (sys.argv[0], loadCommandsSize)
92
def dumpLoadCommand(f, i, opts):
95
print " # Load Command %r" % i
97
print " (('command', %r)" % cmd
99
print " ('size', %r)" % cmdSize
102
dumpSegmentLoadCommand32(f, opts)
104
dumpSymtabCommand(f, opts)
106
dumpDysymtabCommand(f, opts)
109
print " ('uuid', %s)" % uuid.UUID(bytes=f.read(16))
111
print >>sys.stderr,"%s: warning: unknown load command: %r" % (sys.argv[0], cmd)
115
if f.tell() - start != cmdSize:
116
raise ValueError,"%s: warning: invalid load command size: %r" % (sys.argv[0], cmdSize)
118
def dumpSegmentLoadCommand32(f, opts):
119
print " ('segment_name', %r)" % f.read(16)
120
print " ('vm_addr', %r)" % f.read32()
121
print " ('vm_size', %r)" % f.read32()
122
print " ('file_offset', %r)" % f.read32()
123
print " ('file_size', %r)" % f.read32()
124
print " ('maxprot', %r)" % f.read32()
125
print " ('initprot', %r)" % f.read32()
126
numSections = f.read32()
127
print " ('num_sections', %r)" % numSections
128
print " ('flags', %r)" % f.read32()
130
print " ('sections', ["
131
for i in range(numSections):
132
dumpSection32(f, i, opts)
135
def dumpSymtabCommand(f, opts):
137
print " ('symoff', %r)" % symoff
139
print " ('nsyms', %r)" % nsyms
141
print " ('stroff', %r)" % stroff
143
print " ('strsize', %r)" % strsize
148
string_data = f.read(strsize)
149
print " ('_string_data', %r)" % string_data
151
f.registerStringTable(string_data)
154
print " ('_symbols', ["
155
for i in range(nsyms):
156
dumpNlist32(f, i, opts)
161
def dumpNlist32(f, i, opts):
162
print " # Symbol %r" % i
164
print " (('n_strx', %r)" % n_strx
166
print " ('n_type', %#x)" % n_type
168
print " ('n_sect', %r)" % n_sect
170
print " ('n_desc', %r)" % n_desc
172
print " ('n_value', %r)" % n_value
173
print " ('_string', %r)" % f.getString(n_strx)
176
def dumpDysymtabCommand(f, opts):
177
print " ('ilocalsym', %r)" % f.read32()
178
print " ('nlocalsym', %r)" % f.read32()
179
print " ('iextdefsym', %r)" % f.read32()
180
print " ('nextdefsym', %r)" % f.read32()
181
print " ('iundefsym', %r)" % f.read32()
182
print " ('nundefsym', %r)" % f.read32()
183
print " ('tocoff', %r)" % f.read32()
184
print " ('ntoc', %r)" % f.read32()
185
print " ('modtaboff', %r)" % f.read32()
186
print " ('nmodtab', %r)" % f.read32()
187
print " ('extrefsymoff', %r)" % f.read32()
188
print " ('nextrefsyms', %r)" % f.read32()
189
indirectsymoff = f.read32()
190
print " ('indirectsymoff', %r)" % indirectsymoff
191
nindirectsyms = f.read32()
192
print " ('nindirectsyms', %r)" % nindirectsyms
193
print " ('extreloff', %r)" % f.read32()
194
print " ('nextrel', %r)" % f.read32()
195
print " ('locreloff', %r)" % f.read32()
196
print " ('nlocrel', %r)" % f.read32()
200
f.seek(indirectsymoff)
201
print " ('_indirect_symbols', ["
202
for i in range(nindirectsyms):
203
print " # Indirect Symbol %r" % i
204
print " (('symbol_index', %#x),)," % f.read32()
209
def dumpSection32(f, i, opts):
210
print " # Section %r" % i
211
print " (('section_name', %r)" % f.read(16)
212
print " ('segment_name', %r)" % f.read(16)
213
print " ('address', %r)" % f.read32()
215
print " ('size', %r)" % size
217
print " ('offset', %r)" % offset
218
print " ('alignment', %r)" % f.read32()
219
reloc_offset = f.read32()
220
print " ('reloc_offset', %r)" % reloc_offset
221
num_reloc = f.read32()
222
print " ('num_reloc', %r)" % num_reloc
223
print " ('flags', %#x)" % f.read32()
224
print " ('reserved1', %r)" % f.read32()
225
print " ('reserved2', %r)" % f.read32()
231
print " ('_relocations', ["
232
for i in range(num_reloc):
233
print " # Relocation %r" % i
234
print " (('word-0', %#x)," % f.read32()
235
print " ('word-1', %#x))," % f.read32()
238
if opts.dumpSectionData:
240
print " ('_section_data', %r)" % f.read(size)
245
from optparse import OptionParser, OptionGroup
246
parser = OptionParser("usage: %prog [options] {files}")
247
parser.add_option("", "--dump-section-data", dest="dumpSectionData",
248
help="Dump the contents of sections",
249
action="store_true", default=False)
250
(opts, args) = parser.parse_args()
258
if __name__ == '__main__':