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 Value
= struct
.unpack('><'[self
.isLSB
] + 'Q', self
.read(8))[0]
47 if Value
== int(Value
):
51 def registerStringTable(self
, strings
):
52 if self
.string_table
is not None:
53 raise ValueError,"%s: warning: multiple string tables" % sys
.argv
[0]
55 self
.string_table
= strings
57 def getString(self
, index
):
58 if self
.string_table
is None:
59 raise ValueError,"%s: warning: no string table registered" % sys
.argv
[0]
61 end
= self
.string_table
.index('\x00', index
)
62 return self
.string_table
[index
:end
]
64 def dumpmacho(path
, opts
):
68 if magic
== '\xFE\xED\xFA\xCE':
69 f
.isLSB
, f
.is64Bit
= False, False
70 elif magic
== '\xCE\xFA\xED\xFE':
71 f
.isLSB
, f
.is64Bit
= True, False
72 elif magic
== '\xFE\xED\xFA\xCF':
73 f
.isLSB
, f
.is64Bit
= False, True
74 elif magic
== '\xCF\xFA\xED\xFE':
75 f
.isLSB
, f
.is64Bit
= True, True
77 raise ValueError,"Not a Mach-O object file: %r (bad magic)" % path
79 print "('cputype', %r)" % f
.read32()
80 print "('cpusubtype', %r)" % f
.read32()
82 print "('filetype', %r)" % filetype
84 numLoadCommands
= f
.read32()
85 print "('num_load_commands', %r)" % filetype
87 loadCommandsSize
= f
.read32()
88 print "('load_commands_size', %r)" % loadCommandsSize
90 print "('flag', %r)" % f
.read32()
93 print "('reserved', %r)" % f
.read32()
97 print "('load_commands', ["
98 for i
in range(numLoadCommands
):
99 dumpLoadCommand(f
, i
, opts
)
102 if f
.tell() - start
!= loadCommandsSize
:
103 raise ValueError,"%s: warning: invalid load commands size: %r" % (
104 sys
.argv
[0], loadCommandsSize
)
106 def dumpLoadCommand(f
, i
, opts
):
109 print " # Load Command %r" % i
111 print " (('command', %r)" % cmd
113 print " ('size', %r)" % cmdSize
116 dumpSegmentLoadCommand(f
, opts
, False)
118 dumpSymtabCommand(f
, opts
)
120 dumpDysymtabCommand(f
, opts
)
122 dumpSegmentLoadCommand(f
, opts
, True)
125 print " ('uuid', %s)" % uuid
.UUID(bytes
=f
.read(16))
127 print >>sys
.stderr
,"%s: warning: unknown load command: %r" % (
132 if f
.tell() - start
!= cmdSize
:
133 raise ValueError,"%s: warning: invalid load command size: %r" % (
134 sys
.argv
[0], cmdSize
)
136 def dumpSegmentLoadCommand(f
, opts
, is64Bit
):
137 print " ('segment_name', %r)" % f
.read(16)
139 print " ('vm_addr', %r)" % f
.read64()
140 print " ('vm_size', %r)" % f
.read64()
141 print " ('file_offset', %r)" % f
.read64()
142 print " ('file_size', %r)" % f
.read64()
144 print " ('vm_addr', %r)" % f
.read32()
145 print " ('vm_size', %r)" % f
.read32()
146 print " ('file_offset', %r)" % f
.read32()
147 print " ('file_size', %r)" % f
.read32()
148 print " ('maxprot', %r)" % f
.read32()
149 print " ('initprot', %r)" % f
.read32()
150 numSections
= f
.read32()
151 print " ('num_sections', %r)" % numSections
152 print " ('flags', %r)" % f
.read32()
154 print " ('sections', ["
155 for i
in range(numSections
):
156 dumpSection(f
, i
, opts
, is64Bit
)
159 def dumpSymtabCommand(f
, opts
):
161 print " ('symoff', %r)" % symoff
163 print " ('nsyms', %r)" % nsyms
165 print " ('stroff', %r)" % stroff
167 print " ('strsize', %r)" % strsize
172 string_data
= f
.read(strsize
)
173 print " ('_string_data', %r)" % string_data
175 f
.registerStringTable(string_data
)
178 print " ('_symbols', ["
179 for i
in range(nsyms
):
180 dumpNlist32(f
, i
, opts
)
185 def dumpNlist32(f
, i
, opts
):
186 print " # Symbol %r" % i
188 print " (('n_strx', %r)" % n_strx
190 print " ('n_type', %#x)" % n_type
192 print " ('n_sect', %r)" % n_sect
194 print " ('n_desc', %r)" % n_desc
197 print " ('n_value', %r)" % n_value
200 print " ('n_value', %r)" % n_value
201 print " ('_string', %r)" % f
.getString(n_strx
)
204 def dumpDysymtabCommand(f
, opts
):
205 print " ('ilocalsym', %r)" % f
.read32()
206 print " ('nlocalsym', %r)" % f
.read32()
207 print " ('iextdefsym', %r)" % f
.read32()
208 print " ('nextdefsym', %r)" % f
.read32()
209 print " ('iundefsym', %r)" % f
.read32()
210 print " ('nundefsym', %r)" % f
.read32()
211 print " ('tocoff', %r)" % f
.read32()
212 print " ('ntoc', %r)" % f
.read32()
213 print " ('modtaboff', %r)" % f
.read32()
214 print " ('nmodtab', %r)" % f
.read32()
215 print " ('extrefsymoff', %r)" % f
.read32()
216 print " ('nextrefsyms', %r)" % f
.read32()
217 indirectsymoff
= f
.read32()
218 print " ('indirectsymoff', %r)" % indirectsymoff
219 nindirectsyms
= f
.read32()
220 print " ('nindirectsyms', %r)" % nindirectsyms
221 print " ('extreloff', %r)" % f
.read32()
222 print " ('nextrel', %r)" % f
.read32()
223 print " ('locreloff', %r)" % f
.read32()
224 print " ('nlocrel', %r)" % f
.read32()
228 f
.seek(indirectsymoff
)
229 print " ('_indirect_symbols', ["
230 for i
in range(nindirectsyms
):
231 print " # Indirect Symbol %r" % i
232 print " (('symbol_index', %#x),)," % f
.read32()
237 def dumpSection(f
, i
, opts
, is64Bit
):
238 print " # Section %r" % i
239 print " (('section_name', %r)" % f
.read(16)
240 print " ('segment_name', %r)" % f
.read(16)
242 print " ('address', %r)" % f
.read64()
244 print " ('size', %r)" % size
246 print " ('address', %r)" % f
.read32()
248 print " ('size', %r)" % size
250 print " ('offset', %r)" % offset
251 print " ('alignment', %r)" % f
.read32()
252 reloc_offset
= f
.read32()
253 print " ('reloc_offset', %r)" % reloc_offset
254 num_reloc
= f
.read32()
255 print " ('num_reloc', %r)" % num_reloc
256 print " ('flags', %#x)" % f
.read32()
257 print " ('reserved1', %r)" % f
.read32()
258 print " ('reserved2', %r)" % f
.read32()
260 print " ('reserved3', %r)" % f
.read32()
266 print " ('_relocations', ["
267 for i
in range(num_reloc
):
268 print " # Relocation %r" % i
269 print " (('word-0', %#x)," % f
.read32()
270 print " ('word-1', %#x))," % f
.read32()
273 if opts
.dumpSectionData
:
275 print " ('_section_data', '%s')" % common_dump
.dataToHex(f
.read(size
))
280 from optparse
import OptionParser
, OptionGroup
281 parser
= OptionParser("usage: %prog [options] {files}")
282 parser
.add_option("", "--dump-section-data", dest
="dumpSectionData",
283 help="Dump the contents of sections",
284 action
="store_true", default
=False)
285 (opts
, args
) = parser
.parse_args()
293 if __name__
== '__main__':