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')
19 self.string_table = None
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]
46 return struct.unpack('><'[self.isLSB] + 'Q', self.read(8))[0]
48 def registerStringTable(self, strings):
49 if self.string_table is not None:
50 raise ValueError,"%s: warning: multiple string tables" % sys.argv[0]
52 self.string_table = strings
54 def getString(self, index):
55 if self.string_table is None:
56 raise ValueError,"%s: warning: no string table registered" % sys.argv[0]
58 end = self.string_table.index('\x00', index)
59 return self.string_table[index:end]
61 def dumpmacho(path, opts):
65 if magic == '\xFE\xED\xFA\xCE':
66 f.isLSB, f.is64Bit = False, False
67 elif magic == '\xCE\xFA\xED\xFE':
68 f.isLSB, f.is64Bit = True, False
69 elif magic == '\xFE\xED\xFA\xCF':
70 f.isLSB, f.is64Bit = False, True
71 elif magic == '\xCF\xFA\xED\xFE':
72 f.isLSB, f.is64Bit = True, True
74 raise ValueError,"Not a Mach-O object file: %r (bad magic)" % path
76 print "('cputype', %r)" % f.read32()
77 print "('cpusubtype', %r)" % f.read32()
79 print "('filetype', %r)" % filetype
81 numLoadCommands = f.read32()
82 print "('num_load_commands', %r)" % filetype
84 loadCommandsSize = f.read32()
85 print "('load_commands_size', %r)" % loadCommandsSize
87 print "('flag', %r)" % f.read32()
90 print "('reserved', %r)" % f.read32()
94 print "('load_commands', ["
95 for i in range(numLoadCommands):
96 dumpLoadCommand(f, i, opts)
99 if f.tell() - start != loadCommandsSize:
100 raise ValueError,"%s: warning: invalid load commands size: %r" % (
101 sys.argv[0], loadCommandsSize)
103 def dumpLoadCommand(f, i, opts):
106 print " # Load Command %r" % i
108 print " (('command', %r)" % cmd
110 print " ('size', %r)" % cmdSize
113 dumpSegmentLoadCommand(f, opts, False)
115 dumpSymtabCommand(f, opts)
117 dumpDysymtabCommand(f, opts)
119 dumpSegmentLoadCommand(f, opts, True)
122 print " ('uuid', %s)" % uuid.UUID(bytes=f.read(16))
124 print >>sys.stderr,"%s: warning: unknown load command: %r" % (
129 if f.tell() - start != cmdSize:
130 raise ValueError,"%s: warning: invalid load command size: %r" % (
131 sys.argv[0], cmdSize)
133 def dumpSegmentLoadCommand(f, opts, is64Bit):
134 print " ('segment_name', %r)" % f.read(16)
136 print " ('vm_addr', %r)" % f.read64()
137 print " ('vm_size', %r)" % f.read64()
138 print " ('file_offset', %r)" % f.read64()
139 print " ('file_size', %r)" % f.read64()
141 print " ('vm_addr', %r)" % f.read32()
142 print " ('vm_size', %r)" % f.read32()
143 print " ('file_offset', %r)" % f.read32()
144 print " ('file_size', %r)" % f.read32()
145 print " ('maxprot', %r)" % f.read32()
146 print " ('initprot', %r)" % f.read32()
147 numSections = f.read32()
148 print " ('num_sections', %r)" % numSections
149 print " ('flags', %r)" % f.read32()
151 print " ('sections', ["
152 for i in range(numSections):
153 dumpSection(f, i, opts, is64Bit)
156 def dumpSymtabCommand(f, opts):
158 print " ('symoff', %r)" % symoff
160 print " ('nsyms', %r)" % nsyms
162 print " ('stroff', %r)" % stroff
164 print " ('strsize', %r)" % strsize
169 string_data = f.read(strsize)
170 print " ('_string_data', %r)" % string_data
172 f.registerStringTable(string_data)
175 print " ('_symbols', ["
176 for i in range(nsyms):
177 dumpNlist32(f, i, opts)
182 def dumpNlist32(f, i, opts):
183 print " # Symbol %r" % i
185 print " (('n_strx', %r)" % n_strx
187 print " ('n_type', %#x)" % n_type
189 print " ('n_sect', %r)" % n_sect
191 print " ('n_desc', %r)" % n_desc
194 print " ('n_value', %r)" % n_value
197 print " ('n_value', %r)" % n_value
198 print " ('_string', %r)" % f.getString(n_strx)
201 def dumpDysymtabCommand(f, opts):
202 print " ('ilocalsym', %r)" % f.read32()
203 print " ('nlocalsym', %r)" % f.read32()
204 print " ('iextdefsym', %r)" % f.read32()
205 print " ('nextdefsym', %r)" % f.read32()
206 print " ('iundefsym', %r)" % f.read32()
207 print " ('nundefsym', %r)" % f.read32()
208 print " ('tocoff', %r)" % f.read32()
209 print " ('ntoc', %r)" % f.read32()
210 print " ('modtaboff', %r)" % f.read32()
211 print " ('nmodtab', %r)" % f.read32()
212 print " ('extrefsymoff', %r)" % f.read32()
213 print " ('nextrefsyms', %r)" % f.read32()
214 indirectsymoff = f.read32()
215 print " ('indirectsymoff', %r)" % indirectsymoff
216 nindirectsyms = f.read32()
217 print " ('nindirectsyms', %r)" % nindirectsyms
218 print " ('extreloff', %r)" % f.read32()
219 print " ('nextrel', %r)" % f.read32()
220 print " ('locreloff', %r)" % f.read32()
221 print " ('nlocrel', %r)" % f.read32()
225 f.seek(indirectsymoff)
226 print " ('_indirect_symbols', ["
227 for i in range(nindirectsyms):
228 print " # Indirect Symbol %r" % i
229 print " (('symbol_index', %#x),)," % f.read32()
234 def dumpSection(f, i, opts, is64Bit):
235 print " # Section %r" % i
236 print " (('section_name', %r)" % f.read(16)
237 print " ('segment_name', %r)" % f.read(16)
239 print " ('address', %r)" % f.read64()
241 print " ('size', %r)" % size
243 print " ('address', %r)" % f.read32()
245 print " ('size', %r)" % size
247 print " ('offset', %r)" % offset
248 print " ('alignment', %r)" % f.read32()
249 reloc_offset = f.read32()
250 print " ('reloc_offset', %r)" % reloc_offset
251 num_reloc = f.read32()
252 print " ('num_reloc', %r)" % num_reloc
253 print " ('flags', %#x)" % f.read32()
254 print " ('reserved1', %r)" % f.read32()
255 print " ('reserved2', %r)" % f.read32()
257 print " ('reserved3', %r)" % f.read32()
263 print " ('_relocations', ["
264 for i in range(num_reloc):
265 print " # Relocation %r" % i
266 print " (('word-0', %#x)," % f.read32()
267 print " ('word-1', %#x))," % f.read32()
270 if opts.dumpSectionData:
272 print " ('_section_data', '%s')" % common_dump.dataToHex(f.read(size))
277 from optparse import OptionParser, OptionGroup
278 parser = OptionParser("usage: %prog [options] {files}")
279 parser.add_option("", "--dump-section-data", dest="dumpSectionData",
280 help="Dump the contents of sections",
281 action="store_true", default=False)
282 (opts, args) = parser.parse_args()
290 if __name__ == '__main__':