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')
17 self.string_table = None
20 return self.file.tell()
26 data = self.file.read(N)
28 raise ValueError,"Out of data!"
32 return ord(self.read(1))
35 return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
38 # Force to 32-bit, if possible; otherwise these might be long ints on a
39 # big-endian platform. FIXME: Why???
40 Value = struct.unpack('><'[self.isLSB] + 'I', self.read(4))[0]
44 return struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0]
46 def registerStringTable(self, strings):
47 if self.string_table is not None:
48 raise ValueError,"%s: warning: multiple string tables" % sys.argv[0]
50 self.string_table = strings
52 def getString(self, index):
53 if self.string_table is None:
54 raise ValueError,"%s: warning: no string table registered" % sys.argv[0]
56 end = self.string_table.index('\x00', index)
57 return self.string_table[index:end]
59 def dumpmacho(path, opts):
63 if magic == '\xFE\xED\xFA\xCE':
64 f.isLSB, f.is64Bit = False, False
65 elif magic == '\xCE\xFA\xED\xFE':
66 f.isLSB, f.is64Bit = True, False
67 elif magic == '\xFE\xED\xFA\xCF':
68 f.isLSB, f.is64Bit = False, True
69 elif magic == '\xCF\xFA\xED\xFE':
70 f.isLSB, f.is64Bit = True, True
72 raise ValueError,"Not a Mach-O object file: %r (bad magic)" % path
74 print "('cputype', %r)" % f.read32()
75 print "('cpusubtype', %r)" % f.read32()
77 print "('filetype', %r)" % filetype
79 numLoadCommands = f.read32()
80 print "('num_load_commands', %r)" % filetype
82 loadCommandsSize = f.read32()
83 print "('load_commands_size', %r)" % loadCommandsSize
85 print "('flag', %r)" % f.read32()
88 print "('reserved', %r)" % f.read32()
92 print "('load_commands', ["
93 for i in range(numLoadCommands):
94 dumpLoadCommand(f, i, opts)
97 if f.tell() - start != loadCommandsSize:
98 raise ValueError,"%s: warning: invalid load commands size: %r" % (
99 sys.argv[0], loadCommandsSize)
101 def dumpLoadCommand(f, i, opts):
104 print " # Load Command %r" % i
106 print " (('command', %r)" % cmd
108 print " ('size', %r)" % cmdSize
111 dumpSegmentLoadCommand(f, opts, False)
113 dumpSymtabCommand(f, opts)
115 dumpDysymtabCommand(f, opts)
117 dumpSegmentLoadCommand(f, opts, True)
120 print " ('uuid', %s)" % uuid.UUID(bytes=f.read(16))
122 print >>sys.stderr,"%s: warning: unknown load command: %r" % (
127 if f.tell() - start != cmdSize:
128 raise ValueError,"%s: warning: invalid load command size: %r" % (
129 sys.argv[0], cmdSize)
131 def dumpSegmentLoadCommand(f, opts, is64Bit):
132 print " ('segment_name', %r)" % f.read(16)
134 print " ('vm_addr', %r)" % f.read64()
135 print " ('vm_size', %r)" % f.read64()
136 print " ('file_offset', %r)" % f.read64()
137 print " ('file_size', %r)" % f.read64()
139 print " ('vm_addr', %r)" % f.read32()
140 print " ('vm_size', %r)" % f.read32()
141 print " ('file_offset', %r)" % f.read32()
142 print " ('file_size', %r)" % f.read32()
143 print " ('maxprot', %r)" % f.read32()
144 print " ('initprot', %r)" % f.read32()
145 numSections = f.read32()
146 print " ('num_sections', %r)" % numSections
147 print " ('flags', %r)" % f.read32()
149 print " ('sections', ["
150 for i in range(numSections):
151 dumpSection(f, i, opts, is64Bit)
154 def dumpSymtabCommand(f, opts):
156 print " ('symoff', %r)" % symoff
158 print " ('nsyms', %r)" % nsyms
160 print " ('stroff', %r)" % stroff
162 print " ('strsize', %r)" % strsize
167 string_data = f.read(strsize)
168 print " ('_string_data', %r)" % string_data
170 f.registerStringTable(string_data)
173 print " ('_symbols', ["
174 for i in range(nsyms):
175 dumpNlist32(f, i, opts)
180 def dumpNlist32(f, i, opts):
181 print " # Symbol %r" % i
183 print " (('n_strx', %r)" % n_strx
185 print " ('n_type', %#x)" % n_type
187 print " ('n_sect', %r)" % n_sect
189 print " ('n_desc', %r)" % n_desc
191 print " ('n_value', %r)" % n_value
192 print " ('_string', %r)" % f.getString(n_strx)
195 def dumpDysymtabCommand(f, opts):
196 print " ('ilocalsym', %r)" % f.read32()
197 print " ('nlocalsym', %r)" % f.read32()
198 print " ('iextdefsym', %r)" % f.read32()
199 print " ('nextdefsym', %r)" % f.read32()
200 print " ('iundefsym', %r)" % f.read32()
201 print " ('nundefsym', %r)" % f.read32()
202 print " ('tocoff', %r)" % f.read32()
203 print " ('ntoc', %r)" % f.read32()
204 print " ('modtaboff', %r)" % f.read32()
205 print " ('nmodtab', %r)" % f.read32()
206 print " ('extrefsymoff', %r)" % f.read32()
207 print " ('nextrefsyms', %r)" % f.read32()
208 indirectsymoff = f.read32()
209 print " ('indirectsymoff', %r)" % indirectsymoff
210 nindirectsyms = f.read32()
211 print " ('nindirectsyms', %r)" % nindirectsyms
212 print " ('extreloff', %r)" % f.read32()
213 print " ('nextrel', %r)" % f.read32()
214 print " ('locreloff', %r)" % f.read32()
215 print " ('nlocrel', %r)" % f.read32()
219 f.seek(indirectsymoff)
220 print " ('_indirect_symbols', ["
221 for i in range(nindirectsyms):
222 print " # Indirect Symbol %r" % i
223 print " (('symbol_index', %#x),)," % f.read32()
228 def dumpSection(f, i, opts, is64Bit):
229 print " # Section %r" % i
230 print " (('section_name', %r)" % f.read(16)
231 print " ('segment_name', %r)" % f.read(16)
233 print " ('address', %r)" % f.read64()
235 print " ('size', %r)" % size
237 print " ('address', %r)" % f.read32()
239 print " ('size', %r)" % size
241 print " ('offset', %r)" % offset
242 print " ('alignment', %r)" % f.read32()
243 reloc_offset = f.read32()
244 print " ('reloc_offset', %r)" % reloc_offset
245 num_reloc = f.read32()
246 print " ('num_reloc', %r)" % num_reloc
247 print " ('flags', %#x)" % f.read32()
248 print " ('reserved1', %r)" % f.read32()
249 print " ('reserved2', %r)" % f.read32()
251 print " ('reserved3', %r)" % f.read32()
257 print " ('_relocations', ["
258 for i in range(num_reloc):
259 print " # Relocation %r" % i
260 print " (('word-0', %#x)," % f.read32()
261 print " ('word-1', %#x))," % f.read32()
264 if opts.dumpSectionData:
266 print " ('_section_data', %r)" % f.read(size)
271 from optparse import OptionParser, OptionGroup
272 parser = OptionParser("usage: %prog [options] {files}")
273 parser.add_option("", "--dump-section-data", dest="dumpSectionData",
274 help="Dump the contents of sections",
275 action="store_true", default=False)
276 (opts, args) = parser.parse_args()
284 if __name__ == '__main__':