llvm-mc: Fix tests for python variations in int printing, sigh.
[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    else:
108       print >>sys.stderr,"%s: warning: unknown load command: %r" % (sys.argv[0], cmd)
109       f.read(cmdSize - 8)
110    print " ),"
111
112    if f.tell() - start != cmdSize:
113       raise ValueError,"%s: warning: invalid load command size: %r" % (sys.argv[0], cmdSize)
114
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()
126
127    print "  ('sections', ["
128    for i in range(numSections):
129       dumpSection32(f, i, opts)
130    print "  ])"
131
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
141
142    prev_pos = f.tell()
143
144    f.seek(stroff)
145    string_data = f.read(strsize)
146    print "  ('_string_data', %r)" % string_data
147
148    f.registerStringTable(string_data)
149
150    f.seek(symoff)
151    print "  ('_symbols', ["
152    for i in range(nsyms):
153       dumpNlist32(f, i, opts)
154    print "  ])"
155       
156    f.seek(prev_pos)
157
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 "   ),"
172
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()
194
195    prev_pos = f.tell()
196
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 "  ])"
203       
204    f.seek(prev_pos)
205
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    print "    ('size', %r)" % f.read32()
212    print "    ('offset', %r)" % f.read32()
213    print "    ('alignment', %r)" % f.read32()
214    print "    ('reloc_offset', %r)" % f.read32()
215    print "    ('num_reloc', %r)" % f.read32()
216    print "    ('flags', %#x)" % f.read32()
217    print "    ('reserved1', %r)" % f.read32()
218    print "    ('reserved2', %r)" % f.read32()
219    print "   ),"
220    
221 def main():
222     from optparse import OptionParser, OptionGroup
223     parser = OptionParser("usage: %prog [options] {files}")
224
225     (opts, args) = parser.parse_args()
226
227     if not args:
228        args.append('-')
229
230     for arg in args:
231        dumpmacho(arg, opts)
232
233 if __name__ == '__main__':
234    main()