Add command-line flag to tblgen to turn off generating comments for the new
[oota-llvm.git] / test / Scripts / macho-dump
1 #!/usr/bin/env python
2
3 import struct
4 import sys
5 import StringIO
6
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
15
16       self.string_table = None
17
18    def setLSB(self, isLSB):
19       self.isLSB = bool(isLSB)
20
21    def tell(self):
22       return self.file.tell()
23
24    def seek(self, pos):
25       self.file.seek(pos)
26
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
32
33    def read8(self):
34       return ord(self.read(1))
35
36    def read16(self):
37       return struct.unpack('><'[self.isLSB] + 'H', self.read(2))[0]
38
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)
44
45    def registerStringTable(self, strings):
46       if self.string_table is not None:
47          raise ValueError,"%s: warning: multiple string tables" % sys.argv[0]
48
49       self.string_table = strings
50
51    def getString(self, index):
52       if self.string_table is None:
53          raise ValueError,"%s: warning: no string table registered" % sys.argv[0]
54       
55       end = self.string_table.index('\x00', index)
56       return self.string_table[index:end]
57
58 def dumpmacho(path, opts):
59    f = Reader(path)
60
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
68
69    print "('cputype', %r)" % f.read32()
70    print "('cpusubtype', %r)" % f.read32()
71    filetype = f.read32()
72    print "('filetype', %r)" % filetype
73    
74    numLoadCommands = f.read32()
75    print "('num_load_commands', %r)" % filetype
76
77    loadCommandsSize = f.read32()
78    print "('load_commands_size', %r)" % loadCommandsSize
79
80    print "('flag', %r)" % f.read32()
81
82    start = f.tell()
83
84    print "('load_commands', ["
85    for i in range(numLoadCommands):
86       dumpLoadCommand(f, i, opts)
87    print "])"
88
89    if f.tell() - start != loadCommandsSize:
90       raise ValueError,"%s: warning: invalid load commands size: %r" % (sys.argv[0], loadCommandsSize)
91
92 def dumpLoadCommand(f, i, opts):
93    start = f.tell()
94
95    print "  # Load Command %r" % i
96    cmd = f.read32()
97    print " (('command', %r)" % cmd
98    cmdSize = f.read32()
99    print "  ('size', %r)" % cmdSize
100
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    elif cmd == 27:
108       import uuid
109       print "  ('uuid', %s)" % uuid.UUID(bytes=f.read(16))
110    else:
111       print >>sys.stderr,"%s: warning: unknown load command: %r" % (sys.argv[0], cmd)
112       f.read(cmdSize - 8)
113    print " ),"
114
115    if f.tell() - start != cmdSize:
116       raise ValueError,"%s: warning: invalid load command size: %r" % (sys.argv[0], cmdSize)
117
118 def dumpSegmentLoadCommand32(f, opts):
119    print "  ('segment_name', %r)" % f.read(16) 
120    print "  ('vm_addr', %r)" % f.read32()
121    print "  ('vm_size', %r)" % f.read32()
122    print "  ('file_offset', %r)" % f.read32()
123    print "  ('file_size', %r)" % f.read32()
124    print "  ('maxprot', %r)" % f.read32()
125    print "  ('initprot', %r)" % f.read32()
126    numSections = f.read32()
127    print "  ('num_sections', %r)" % numSections
128    print "  ('flags', %r)" % f.read32()
129
130    print "  ('sections', ["
131    for i in range(numSections):
132       dumpSection32(f, i, opts)
133    print "  ])"
134
135 def dumpSymtabCommand(f, opts):
136    symoff = f.read32()
137    print "  ('symoff', %r)" % symoff
138    nsyms = f.read32()
139    print "  ('nsyms', %r)" % nsyms
140    stroff = f.read32()
141    print "  ('stroff', %r)" % stroff
142    strsize = f.read32()
143    print "  ('strsize', %r)" % strsize
144
145    prev_pos = f.tell()
146
147    f.seek(stroff)
148    string_data = f.read(strsize)
149    print "  ('_string_data', %r)" % string_data
150
151    f.registerStringTable(string_data)
152
153    f.seek(symoff)
154    print "  ('_symbols', ["
155    for i in range(nsyms):
156       dumpNlist32(f, i, opts)
157    print "  ])"
158       
159    f.seek(prev_pos)
160
161 def dumpNlist32(f, i, opts):
162    print "    # Symbol %r" % i
163    n_strx = f.read32()
164    print "   (('n_strx', %r)" % n_strx
165    n_type = f.read8()
166    print "    ('n_type', %#x)" % n_type
167    n_sect = f.read8()
168    print "    ('n_sect', %r)" % n_sect
169    n_desc = f.read16()
170    print "    ('n_desc', %r)" % n_desc
171    n_value = f.read32()
172    print "    ('n_value', %r)" % n_value
173    print "    ('_string', %r)" % f.getString(n_strx)
174    print "   ),"
175
176 def dumpDysymtabCommand(f, opts):   
177    print "  ('ilocalsym', %r)" % f.read32()
178    print "  ('nlocalsym', %r)" % f.read32()
179    print "  ('iextdefsym', %r)" % f.read32()
180    print "  ('nextdefsym', %r)" % f.read32()
181    print "  ('iundefsym', %r)" % f.read32()
182    print "  ('nundefsym', %r)" % f.read32()
183    print "  ('tocoff', %r)" % f.read32()
184    print "  ('ntoc', %r)" % f.read32()
185    print "  ('modtaboff', %r)" % f.read32()
186    print "  ('nmodtab', %r)" % f.read32()
187    print "  ('extrefsymoff', %r)" % f.read32()
188    print "  ('nextrefsyms', %r)" % f.read32()
189    indirectsymoff = f.read32()
190    print "  ('indirectsymoff', %r)" % indirectsymoff
191    nindirectsyms = f.read32()
192    print "  ('nindirectsyms', %r)" % nindirectsyms
193    print "  ('extreloff', %r)" % f.read32()
194    print "  ('nextrel', %r)" % f.read32()
195    print "  ('locreloff', %r)" % f.read32()
196    print "  ('nlocrel', %r)" % f.read32()
197
198    prev_pos = f.tell()
199
200    f.seek(indirectsymoff)
201    print "  ('_indirect_symbols', ["
202    for i in range(nindirectsyms):
203       print "    # Indirect Symbol %r" % i
204       print "    (('symbol_index', %#x),)," % f.read32()
205    print "  ])"
206       
207    f.seek(prev_pos)
208
209 def dumpSection32(f, i, opts):
210    print "    # Section %r" % i
211    print "   (('section_name', %r)" % f.read(16)
212    print "    ('segment_name', %r)" % f.read(16)
213    print "    ('address', %r)" % f.read32()
214    size = f.read32()
215    print "    ('size', %r)" % size
216    offset = f.read32()
217    print "    ('offset', %r)" % offset
218    print "    ('alignment', %r)" % f.read32()   
219    reloc_offset = f.read32()
220    print "    ('reloc_offset', %r)" % reloc_offset
221    num_reloc = f.read32()
222    print "    ('num_reloc', %r)" % num_reloc
223    print "    ('flags', %#x)" % f.read32()
224    print "    ('reserved1', %r)" % f.read32()
225    print "    ('reserved2', %r)" % f.read32()
226    print "   ),"
227
228    prev_pos = f.tell()
229
230    f.seek(reloc_offset)
231    print "  ('_relocations', ["
232    for i in range(num_reloc):
233       print "    # Relocation %r" % i
234       print "    (('word-0', %#x)," % f.read32()
235       print "     ('word-1', %#x))," % f.read32()
236    print "  ])"
237
238    if opts.dumpSectionData:
239       f.seek(offset)
240       print "  ('_section_data', %r)" % f.read(size)
241       
242    f.seek(prev_pos)
243    
244 def main():
245     from optparse import OptionParser, OptionGroup
246     parser = OptionParser("usage: %prog [options] {files}")
247     parser.add_option("", "--dump-section-data", dest="dumpSectionData",
248                       help="Dump the contents of sections",
249                       action="store_true", default=False)    
250     (opts, args) = parser.parse_args()
251
252     if not args:
253        args.append('-')
254
255     for arg in args:
256        dumpmacho(arg, opts)
257
258 if __name__ == '__main__':
259    main()