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
)
108 print >>sys
.stderr
,"%s: warning: unknown load command: %r" % (sys
.argv
[0], cmd
)
112 if f
.tell() - start
!= cmdSize
:
113 raise ValueError,"%s: warning: invalid load command size: %r" % (sys
.argv
[0], cmdSize
)
115 def dumpSegmentLoadCommand32(f
, opts
):
116 print " ('segment_name', %r)" % f
.read(16)
117 print " ('vm_addr', %r)" % f
.read32()
118 print " ('vm_size', %r)" % f
.read32()
119 print " ('file_offset', %r)" % f
.read32()
120 print " ('file_size', %r)" % f
.read32()
121 print " ('maxprot', %r)" % f
.read32()
122 print " ('initprot', %r)" % f
.read32()
123 numSections
= f
.read32()
124 print " ('num_sections', %r)" % numSections
125 print " ('flags', %r)" % f
.read32()
127 print " ('sections', ["
128 for i
in range(numSections
):
129 dumpSection32(f
, i
, opts
)
132 def dumpSymtabCommand(f
, opts
):
134 print " ('symoff', %r)" % symoff
136 print " ('nsyms', %r)" % nsyms
138 print " ('stroff', %r)" % stroff
140 print " ('strsize', %r)" % strsize
145 string_data
= f
.read(strsize
)
146 print " ('_string_data', %r)" % string_data
148 f
.registerStringTable(string_data
)
151 print " ('_symbols', ["
152 for i
in range(nsyms
):
153 dumpNlist32(f
, i
, opts
)
158 def dumpNlist32(f
, i
, opts
):
159 print " # Symbol %r" % i
161 print " (('n_strx', %r)" % n_strx
163 print " ('n_type', %#x)" % n_type
165 print " ('n_sect', %r)" % n_sect
167 print " ('n_desc', %r)" % n_desc
169 print " ('n_value', %r)" % n_value
170 print " ('_string', %r)" % f
.getString(n_strx
)
173 def dumpDysymtabCommand(f
, opts
):
174 print " ('ilocalsym', %r)" % f
.read32()
175 print " ('nlocalsym', %r)" % f
.read32()
176 print " ('iextdefsym', %r)" % f
.read32()
177 print " ('nextdefsym', %r)" % f
.read32()
178 print " ('iundefsym', %r)" % f
.read32()
179 print " ('nundefsym', %r)" % f
.read32()
180 print " ('tocoff', %r)" % f
.read32()
181 print " ('ntoc', %r)" % f
.read32()
182 print " ('modtaboff', %r)" % f
.read32()
183 print " ('nmodtab', %r)" % f
.read32()
184 print " ('extrefsymoff', %r)" % f
.read32()
185 print " ('nextrefsyms', %r)" % f
.read32()
186 indirectsymoff
= f
.read32()
187 print " ('indirectsymoff', %r)" % indirectsymoff
188 nindirectsyms
= f
.read32()
189 print " ('nindirectsyms', %r)" % nindirectsyms
190 print " ('extreloff', %r)" % f
.read32()
191 print " ('nextrel', %r)" % f
.read32()
192 print " ('locreloff', %r)" % f
.read32()
193 print " ('nlocrel', %r)" % f
.read32()
197 f
.seek(indirectsymoff
)
198 print " ('_indirect_symbols', ["
199 for i
in range(nindirectsyms
):
200 print " # Indirect Symbol %r" % i
201 print " (('symbol_index', %#x),)," % f
.read32()
206 def dumpSection32(f
, i
, opts
):
207 print " # Section %r" % i
208 print " (('section_name', %r)" % f
.read(16)
209 print " ('segment_name', %r)" % f
.read(16)
210 print " ('address', %r)" % f
.read32()
212 print " ('size', %r)" % size
214 print " ('offset', %r)" % offset
215 print " ('alignment', %r)" % f
.read32()
216 reloc_offset
= f
.read32()
217 print " ('reloc_offset', %r)" % reloc_offset
218 num_reloc
= f
.read32()
219 print " ('num_reloc', %r)" % num_reloc
220 print " ('flags', %#x)" % f
.read32()
221 print " ('reserved1', %r)" % f
.read32()
222 print " ('reserved2', %r)" % f
.read32()
228 print " ('_relocations', ["
229 for i
in range(num_reloc
):
230 print " # Relocation %r" % i
231 print " (('word-0', %#x)," % f
.read32()
232 print " ('word-1', %#x))," % f
.read32()
235 if opts
.dumpSectionData
:
237 print " ('_section_data', %r)" % f
.read(size
)
242 from optparse
import OptionParser
, OptionGroup
243 parser
= OptionParser("usage: %prog [options] {files}")
244 parser
.add_option("", "--dump-section-data", dest
="dumpSectionData",
245 help="Dump the contents of sections",
246 action
="store_true", default
=False)
247 (opts
, args
) = parser
.parse_args()
255 if __name__
== '__main__':