Forbid arrays of function-type and structures with function-typed fields.
[llvm/avr.git] / test / Scripts / macho-dump
blob12ec26d45896ae5a6c8b46e77cd95a61c22146cb
1 #!/usr/bin/env python
3 import struct
4 import sys
5 import StringIO
7 class Reader:
8 def __init__(self, path):
9 if path == '-':
10 # Snarf all the data so we can seek.
11 self.file = StringIO.StringIO(sys.stdin.read())
12 else:
13 self.file = open(path,'rb')
14 self.isLSB = None
16 self.string_table = None
18 def setLSB(self, isLSB):
19 self.isLSB = bool(isLSB)
21 def tell(self):
22 return self.file.tell()
24 def seek(self, pos):
25 self.file.seek(pos)
27 def read(self, N):
28 data = self.file.read(N)
29 if len(data) != N:
30 raise ValueError,"Out of data!"
31 return data
33 def read8(self):
34 return ord(self.read(1))
36 def read16(self):
37 return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
39 def read32(self):
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]
43 return int(Value)
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):
59 f = Reader(path)
61 magic = f.read(4)
62 if magic == '\xFE\xED\xFA\xCE':
63 f.setLSB(False)
64 elif magic == '\xCE\xFA\xED\xFE':
65 f.setLSB(True)
66 else:
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()
71 filetype = 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()
82 start = f.tell()
84 print "('load_commands', ["
85 for i in range(numLoadCommands):
86 dumpLoadCommand(f, i, opts)
87 print "])"
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):
93 start = f.tell()
95 print " # Load Command %r" % i
96 cmd = f.read32()
97 print " (('command', %r)" % cmd
98 cmdSize = f.read32()
99 print " ('size', %r)" % cmdSize
101 if cmd == 1:
102 dumpSegmentLoadCommand32(f, opts)
103 elif cmd == 2:
104 dumpSymtabCommand(f, opts)
105 elif cmd == 11:
106 dumpDysymtabCommand(f, opts)
107 else:
108 print >>sys.stderr,"%s: warning: unknown load command: %r" % (sys.argv[0], cmd)
109 f.read(cmdSize - 8)
110 print " ),"
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)
130 print " ])"
132 def dumpSymtabCommand(f, opts):
133 symoff = f.read32()
134 print " ('symoff', %r)" % symoff
135 nsyms = f.read32()
136 print " ('nsyms', %r)" % nsyms
137 stroff = f.read32()
138 print " ('stroff', %r)" % stroff
139 strsize = f.read32()
140 print " ('strsize', %r)" % strsize
142 prev_pos = f.tell()
144 f.seek(stroff)
145 string_data = f.read(strsize)
146 print " ('_string_data', %r)" % string_data
148 f.registerStringTable(string_data)
150 f.seek(symoff)
151 print " ('_symbols', ["
152 for i in range(nsyms):
153 dumpNlist32(f, i, opts)
154 print " ])"
156 f.seek(prev_pos)
158 def dumpNlist32(f, i, opts):
159 print " # Symbol %r" % i
160 n_strx = f.read32()
161 print " (('n_strx', %r)" % n_strx
162 n_type = f.read8()
163 print " ('n_type', %#x)" % n_type
164 n_sect = f.read8()
165 print " ('n_sect', %r)" % n_sect
166 n_desc = f.read16()
167 print " ('n_desc', %r)" % n_desc
168 n_value = f.read32()
169 print " ('n_value', %r)" % n_value
170 print " ('_string', %r)" % f.getString(n_strx)
171 print " ),"
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()
195 prev_pos = f.tell()
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()
202 print " ])"
204 f.seek(prev_pos)
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()
211 size = f.read32()
212 print " ('size', %r)" % size
213 offset = f.read32()
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()
223 print " ),"
225 prev_pos = f.tell()
227 f.seek(reloc_offset)
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()
233 print " ])"
235 if opts.dumpSectionData:
236 f.seek(offset)
237 print " ('_section_data', %r)" % f.read(size)
239 f.seek(prev_pos)
241 def main():
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()
249 if not args:
250 args.append('-')
252 for arg in args:
253 dumpmacho(arg, opts)
255 if __name__ == '__main__':
256 main()