2 #===-- coff-dump.py - COFF object file dump utility-------------------------===#
4 # The LLVM Compiler Infrastructure
6 # This file is distributed under the University of Illinois Open Source
7 # License. See LICENSE.TXT for details.
9 #===------------------------------------------------------------------------===#
12 # COFF File Definition
15 def string_table_entry (offset):
16 return ('ptr', '+ + PointerToSymbolTable * NumberOfSymbols 18 %s' % offset, ('scalar', 'cstr', '%s'))
20 return string_table_entry (value [1:].rstrip('\0'))
25 parts = struct.unpack("<2L", value)
27 return string_table_entry (parts [1])
32 ('MachineType', ('enum', '<H', '0x%X', {
33 0x0: 'IMAGE_FILE_MACHINE_UNKNOWN',
34 0x1d3: 'IMAGE_FILE_MACHINE_AM33',
35 0x8664: 'IMAGE_FILE_MACHINE_AMD64',
36 0x1c0: 'IMAGE_FILE_MACHINE_ARM',
37 0xebc: 'IMAGE_FILE_MACHINE_EBC',
38 0x14c: 'IMAGE_FILE_MACHINE_I386',
39 0x200: 'IMAGE_FILE_MACHINE_IA64',
40 0x904: 'IMAGE_FILE_MACHINE_M32R',
41 0x266: 'IMAGE_FILE_MACHINE_MIPS16',
42 0x366: 'IMAGE_FILE_MACHINE_MIPSFPU',
43 0x466: 'IMAGE_FILE_MACHINE_MIPSFPU16',
44 0x1f0: 'IMAGE_FILE_MACHINE_POWERPC',
45 0x1f1: 'IMAGE_FILE_MACHINE_POWERPCFP',
46 0x166: 'IMAGE_FILE_MACHINE_R4000',
47 0x1a2: 'IMAGE_FILE_MACHINE_SH3',
48 0x1a3: 'IMAGE_FILE_MACHINE_SH3DSP',
49 0x1a6: 'IMAGE_FILE_MACHINE_SH4',
50 0x1a8: 'IMAGE_FILE_MACHINE_SH5',
51 0x1c2: 'IMAGE_FILE_MACHINE_THUMB',
52 0x169: 'IMAGE_FILE_MACHINE_WCEMIPSV2',
54 ('NumberOfSections', ('scalar', '<H', '%d')),
55 ('TimeDateStamp', ('scalar', '<L', '%d')),
56 ('PointerToSymbolTable', ('scalar', '<L', '0x%0X')),
57 ('NumberOfSymbols', ('scalar', '<L', '%d')),
58 ('SizeOfOptionalHeader', ('scalar', '<H', '%d')),
59 ('Characteristics', ('flags', '<H', '0x%x', [
60 (0x0001, 'IMAGE_FILE_RELOCS_STRIPPED', ),
61 (0x0002, 'IMAGE_FILE_EXECUTABLE_IMAGE', ),
62 (0x0004, 'IMAGE_FILE_LINE_NUMS_STRIPPED', ),
63 (0x0008, 'IMAGE_FILE_LOCAL_SYMS_STRIPPED', ),
64 (0x0010, 'IMAGE_FILE_AGGRESSIVE_WS_TRIM', ),
65 (0x0020, 'IMAGE_FILE_LARGE_ADDRESS_AWARE', ),
66 (0x0080, 'IMAGE_FILE_BYTES_REVERSED_LO', ),
67 (0x0100, 'IMAGE_FILE_32BIT_MACHINE', ),
68 (0x0200, 'IMAGE_FILE_DEBUG_STRIPPED', ),
69 (0x0400, 'IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP', ),
70 (0x0800, 'IMAGE_FILE_NET_RUN_FROM_SWAP', ),
71 (0x1000, 'IMAGE_FILE_SYSTEM', ),
72 (0x2000, 'IMAGE_FILE_DLL', ),
73 (0x4000, 'IMAGE_FILE_UP_SYSTEM_ONLY', ),
74 (0x8000, 'IMAGE_FILE_BYTES_REVERSED_HI', ),
76 ('Sections', ('array', 'NumberOfSections', ('struct', [
77 ('Name', ('scalar', '<8s', secname)),
78 ('VirtualSize', ('scalar', '<L', '%d' )),
79 ('VirtualAddress', ('scalar', '<L', '%d' )),
80 ('SizeOfRawData', ('scalar', '<L', '%d' )),
81 ('PointerToRawData', ('scalar', '<L', '0x%X' )),
82 ('PointerToRelocations', ('scalar', '<L', '0x%X' )),
83 ('PointerToLineNumbers', ('scalar', '<L', '0x%X' )),
84 ('NumberOfRelocations', ('scalar', '<H', '%d' )),
85 ('NumberOfLineNumbers', ('scalar', '<H', '%d' )),
86 ('Charateristics', ('flags', '<L', '0x%X', [
87 (0x00000008, 'IMAGE_SCN_TYPE_NO_PAD'),
88 (0x00000020, 'IMAGE_SCN_CNT_CODE'),
89 (0x00000040, 'IMAGE_SCN_CNT_INITIALIZED_DATA'),
90 (0x00000080, 'IMAGE_SCN_CNT_UNINITIALIZED_DATA'),
91 (0x00000100, 'IMAGE_SCN_LNK_OTHER'),
92 (0x00000200, 'IMAGE_SCN_LNK_INFO'),
93 (0x00000800, 'IMAGE_SCN_LNK_REMOVE'),
94 (0x00001000, 'IMAGE_SCN_LNK_COMDAT'),
95 (0x00008000, 'IMAGE_SCN_GPREL'),
96 (0x00020000, 'IMAGE_SCN_MEM_PURGEABLE'),
97 (0x00020000, 'IMAGE_SCN_MEM_16BIT'),
98 (0x00040000, 'IMAGE_SCN_MEM_LOCKED'),
99 (0x00080000, 'IMAGE_SCN_MEM_PRELOAD'),
100 (0x00F00000, 'IMAGE_SCN_ALIGN', {
101 0x00100000: 'IMAGE_SCN_ALIGN_1BYTES',
102 0x00200000: 'IMAGE_SCN_ALIGN_2BYTES',
103 0x00300000: 'IMAGE_SCN_ALIGN_4BYTES',
104 0x00400000: 'IMAGE_SCN_ALIGN_8BYTES',
105 0x00500000: 'IMAGE_SCN_ALIGN_16BYTES',
106 0x00600000: 'IMAGE_SCN_ALIGN_32BYTES',
107 0x00700000: 'IMAGE_SCN_ALIGN_64BYTES',
108 0x00800000: 'IMAGE_SCN_ALIGN_128BYTES',
109 0x00900000: 'IMAGE_SCN_ALIGN_256BYTES',
110 0x00A00000: 'IMAGE_SCN_ALIGN_512BYTES',
111 0x00B00000: 'IMAGE_SCN_ALIGN_1024BYTES',
112 0x00C00000: 'IMAGE_SCN_ALIGN_2048BYTES',
113 0x00D00000: 'IMAGE_SCN_ALIGN_4096BYTES',
114 0x00E00000: 'IMAGE_SCN_ALIGN_8192BYTES',
116 (0x01000000, 'IMAGE_SCN_LNK_NRELOC_OVFL'),
117 (0x02000000, 'IMAGE_SCN_MEM_DISCARDABLE'),
118 (0x04000000, 'IMAGE_SCN_MEM_NOT_CACHED'),
119 (0x08000000, 'IMAGE_SCN_MEM_NOT_PAGED'),
120 (0x10000000, 'IMAGE_SCN_MEM_SHARED'),
121 (0x20000000, 'IMAGE_SCN_MEM_EXECUTE'),
122 (0x40000000, 'IMAGE_SCN_MEM_READ'),
123 (0x80000000, 'IMAGE_SCN_MEM_WRITE'),
125 ('SectionData', ('ptr', 'PointerToRawData', ('blob', 'SizeOfRawData'))),
126 ('Relocations', ('ptr', 'PointerToRelocations', ('array', 'NumberOfRelocations', ('struct', [
127 ('VirtualAddress', ('scalar', '<L', '0x%X')),
128 ('SymbolTableIndex', ('scalar', '<L', '%d' )),
129 ('Type', ('enum', '<H', '%d', ('MachineType', {
131 0x0000: 'IMAGE_REL_I386_ABSOLUTE',
132 0x0001: 'IMAGE_REL_I386_DIR16',
133 0x0002: 'IMAGE_REL_I386_REL16',
134 0x0006: 'IMAGE_REL_I386_DIR32',
135 0x0007: 'IMAGE_REL_I386_DIR32NB',
136 0x0009: 'IMAGE_REL_I386_SEG12',
137 0x000A: 'IMAGE_REL_I386_SECTION',
138 0x000B: 'IMAGE_REL_I386_SECREL',
139 0x000C: 'IMAGE_REL_I386_TOKEN',
140 0x000D: 'IMAGE_REL_I386_SECREL7',
141 0x0014: 'IMAGE_REL_I386_REL32',
144 0x0000: 'IMAGE_REL_AMD64_ABSOLUTE',
145 0x0001: 'IMAGE_REL_AMD64_ADDR64',
146 0x0002: 'IMAGE_REL_AMD64_ADDR32',
147 0x0003: 'IMAGE_REL_AMD64_ADDR32NB',
148 0x0004: 'IMAGE_REL_AMD64_REL32',
149 0x0005: 'IMAGE_REL_AMD64_REL32_1',
150 0x0006: 'IMAGE_REL_AMD64_REL32_2',
151 0x0007: 'IMAGE_REL_AMD64_REL32_3',
152 0x0008: 'IMAGE_REL_AMD64_REL32_4',
153 0x0009: 'IMAGE_REL_AMD64_REL32_5',
154 0x000A: 'IMAGE_REL_AMD64_SECTION',
155 0x000B: 'IMAGE_REL_AMD64_SECREL',
156 0x000C: 'IMAGE_REL_AMD64_SECREL7',
157 0x000D: 'IMAGE_REL_AMD64_TOKEN',
158 0x000E: 'IMAGE_REL_AMD64_SREL32',
159 0x000F: 'IMAGE_REL_AMD64_PAIR',
160 0x0010: 'IMAGE_REL_AMD64_SSPAN32',
163 ('SymbolName', ('ptr', '+ PointerToSymbolTable * - SymbolTableIndex 1 18', ('scalar', '<8s', symname)))
166 ('Symbols', ('ptr', 'PointerToSymbolTable', ('byte-array', '* NumberOfSymbols 18', ('struct', [
167 ('Name', ('scalar', '<8s', symname)),
168 ('Value', ('scalar', '<L', '%d' )),
169 ('SectionNumber', ('scalar', '<H', '%d' )),
170 ('SimpleType', ('enum', '<B', '%d', {
171 0: 'IMAGE_SYM_TYPE_NULL',
172 1: 'IMAGE_SYM_TYPE_VOID',
173 2: 'IMAGE_SYM_TYPE_CHAR',
174 3: 'IMAGE_SYM_TYPE_SHORT',
175 4: 'IMAGE_SYM_TYPE_INT',
176 5: 'IMAGE_SYM_TYPE_LONG',
177 6: 'IMAGE_SYM_TYPE_FLOAT',
178 7: 'IMAGE_SYM_TYPE_DOUBLE',
179 8: 'IMAGE_SYM_TYPE_STRUCT',
180 9: 'IMAGE_SYM_TYPE_UNION',
181 10: 'IMAGE_SYM_TYPE_ENUM',
182 11: 'IMAGE_SYM_TYPE_MOE',
183 12: 'IMAGE_SYM_TYPE_BYTE',
184 13: 'IMAGE_SYM_TYPE_WORD',
185 14: 'IMAGE_SYM_TYPE_UINT',
186 15: 'IMAGE_SYM_TYPE_DWORD',
188 ('ComplexType', ('enum', '<B', '%d', {
189 0: 'IMAGE_SYM_DTYPE_NULL',
190 1: 'IMAGE_SYM_DTYPE_POINTER',
191 2: 'IMAGE_SYM_DTYPE_FUNCTION',
192 3: 'IMAGE_SYM_DTYPE_ARRAY',
194 ('StorageClass', ('enum', '<B', '%d', {
195 -1: 'IMAGE_SYM_CLASS_END_OF_FUNCTION',
196 0: 'IMAGE_SYM_CLASS_NULL',
197 1: 'IMAGE_SYM_CLASS_AUTOMATIC',
198 2: 'IMAGE_SYM_CLASS_EXTERNAL',
199 3: 'IMAGE_SYM_CLASS_STATIC',
200 4: 'IMAGE_SYM_CLASS_REGISTER',
201 5: 'IMAGE_SYM_CLASS_EXTERNAL_DEF',
202 6: 'IMAGE_SYM_CLASS_LABEL',
203 7: 'IMAGE_SYM_CLASS_UNDEFINED_LABEL',
204 8: 'IMAGE_SYM_CLASS_MEMBER_OF_STRUCT',
205 9: 'IMAGE_SYM_CLASS_ARGUMENT',
206 10: 'IMAGE_SYM_CLASS_STRUCT_TAG',
207 11: 'IMAGE_SYM_CLASS_MEMBER_OF_UNION',
208 12: 'IMAGE_SYM_CLASS_UNION_TAG',
209 13: 'IMAGE_SYM_CLASS_TYPE_DEFINITION',
210 14: 'IMAGE_SYM_CLASS_UNDEFINED_STATIC',
211 15: 'IMAGE_SYM_CLASS_ENUM_TAG',
212 16: 'IMAGE_SYM_CLASS_MEMBER_OF_ENUM',
213 17: 'IMAGE_SYM_CLASS_REGISTER_PARAM',
214 18: 'IMAGE_SYM_CLASS_BIT_FIELD',
215 100: 'IMAGE_SYM_CLASS_BLOCK',
216 101: 'IMAGE_SYM_CLASS_FUNCTION',
217 102: 'IMAGE_SYM_CLASS_END_OF_STRUCT',
218 103: 'IMAGE_SYM_CLASS_FILE',
219 104: 'IMAGE_SYM_CLASS_SECTION',
220 105: 'IMAGE_SYM_CLASS_WEAK_EXTERNAL',
221 107: 'IMAGE_SYM_CLASS_CLR_TOKEN',
223 ('NumberOfAuxSymbols', ('scalar', '<B', '%d' )),
224 ('AuxillaryData', ('blob', '* NumberOfAuxSymbols 18')),
229 # Definition Interpreter
232 import sys, types, struct, re
256 output += Indent * ' '
264 sys.stdout.write (output)
267 return struct.unpack (format, Input.read(struct.calcsize(format)))
272 char = Input.read (1)
274 raise RuntimeError ("EOF while reading cstr")
280 def push_pos(seek_to = None):
281 Stack [0:0] = [Input.tell ()]
286 assert(len (Stack) > 0)
287 Input.seek (Stack [0])
290 def print_binary_data(size):
294 data = Input.read(16)
297 data = Input.read(size)
302 for index in xrange (16):
303 if index < len (data):
306 ch = ord (data [index])
307 bytes += "%02X " % ch
308 if ch >= 0x20 and ch <= 0x7F:
317 write ("%s|%s|\n" % (bytes, text))
320 idlit = re.compile ("[a-zA-Z][a-zA-Z0-9_-]*")
321 numlit = re.compile ("[0-9]+")
323 def read_value(expr):
325 input = iter (expr.split ())
329 token = input.next ()
338 if len (token) > 1 and token [0] in ('=', '@', '<', '!', '>'):
340 assert (len (val) == 1)
344 return eval () + eval ()
346 return eval () - eval ()
348 return eval () * eval ()
350 return eval () / eval ()
352 if idlit.match (token):
353 return Fields [token]
354 if numlit.match (token):
357 raise RuntimeError ("unexpected token %s" % repr(token))
363 except StopIteration:
365 raise RuntimeError("unexpected input at end of expression")
367 def write_value(format,value):
368 format_type = type (format)
369 if format_type is types.StringType:
371 elif format_type is types.FunctionType:
372 write_value (format (value), value)
373 elif format_type is types.TupleType:
374 Fields ['this'] = value
375 handle_element (format)
377 raise RuntimeError("unexpected type: %s" % repr(format_type))
379 def handle_scalar(entry):
383 value = read_value (iformat)
385 write_value (oformat, value)
389 def handle_enum(entry):
392 definitions = entry [3]
394 value = read_value (iformat)
396 if type (definitions) is types.TupleType:
397 selector = read_value (definitions [0])
398 definitions = definitions [1] [selector]
400 description = definitions[value] if value in definitions else "unknown"
402 write ("%s (" % description)
403 write_value (oformat, value)
408 def handle_flags(entry):
411 definitions = entry [3]
413 value = read_value (iformat)
415 write_value (oformat, value)
418 for entry in definitions:
423 selection = value & mask
425 write("\n%s" % map[selection])
427 write("\n%s <%d>" % (name, selection))
428 elif len (entry) == 2:
429 if value & mask != 0:
435 def handle_struct(entry):
444 for member in members:
448 write("%s = "%name.ljust(24))
450 value = handle_element(type)
454 Fields [name] = value
455 newFields [name] = value
462 def handle_array(entry):
471 value = read_value (length)
473 for index in xrange (value):
474 write ("%d = "%index)
475 value = handle_element(element)
477 newItems.append (value)
484 def handle_byte_array(entry):
493 value = read_value (length)
494 end_of_array = Input.tell () + value
497 while Input.tell () < end_of_array:
498 write ("%d = "%index)
499 value = handle_element(element)
501 newItems.append (value)
509 def handle_ptr(entry):
514 offset = read_value (offset)
520 value = handle_element (element)
529 def handle_blob(entry):
535 value = print_binary_data (read_value (length))
541 def handle_element(entry):
543 'struct': handle_struct,
544 'scalar': handle_scalar,
546 'flags': handle_flags,
549 'array': handle_array,
550 'byte-array': handle_byte_array,
553 if not entry [0] in handlers:
554 raise RuntimeError ("unexpected type '%s'" % str (entry[0]))
556 return handlers [entry [0]] (entry)
558 Input = open (sys.argv [1], "rb")
560 handle_element (file)