forked from laanwj/decuda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelfToCubin.py
More file actions
executable file
·453 lines (436 loc) · 16.6 KB
/
elfToCubin.py
File metadata and controls
executable file
·453 lines (436 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/python
#
# Copyright 2010, Imran Haque
# ihaque AT cs DOT stanford DOT edu
# http://cs.stanford.edu/people/ihaque
# This code is placed in the public domain and bears no warranty whatsoever
#
# Uses objdump to dump contents of an nvcc-generated ELF file
# and parses this output to generate an old-format CUBIN file
#
# Alternatively, given the --nouveau option, parses ELF file
# and automatically passes each kernel off to one of the nouveau-project
# disassemblers nv50dis (Tesla) or nvc0dis (Fermi)
#
# Notable shortcomings:
# kernel .info sections are not parsed (presumably contains memory/reg info)
# no attempt is made to preserve names of constant sections
# does not parse size or proper name/offset of constant sections
# emits initializer data for file-constant variables
import sys
import socket
from struct import unpack
from subprocess import Popen,PIPE
from StringIO import StringIO
import string
def ishexdigit(char):
return (char in string.hexdigits)
class cubin:
def __init__(self,codelevel="sm_10"):
self.kernels = []
self.consts = []
self.info = []
self.arch = codelevel
self.abiversion = "1"
self.modname = "cubin"
def output(self,f):
f.write("architecture {%s}\n"%self.arch)
f.write("abiversion {%s}\n"%self.abiversion)
f.write("modname {%s}\n"%self.modname)
for c in self.consts:
c.output(f)
for k in self.kernels:
k.output(f)
class constant:
def __init__(self,name,data=None,depth=0,inkernel=False):
self.name = name
self.segname = "const"
self.segnum = 0
self.offset = 0
self.bytes = 0
self.depth = depth
self.mem = None
self.inkernel = inkernel
if data is not None:
self.mem = mem(data,self.depth+1)
def output(self,f):
prefix= "\t"*(self.depth+1)
shortprefix= "\t"*(self.depth)
if self.inkernel:
f.write("%sconst {\n"%shortprefix)
else:
f.write("%sconsts {\n"%shortprefix)
if self.name is not None:
f.write("%s\t\tname = %s\n"%(prefix,self.name))
f.write("%s\t\tsegname = %s\n"%(prefix,self.segname))
f.write("%s\t\tsegnum = %d\n"%(prefix,self.segnum))
f.write("%s\t\toffset = %d\n"%(prefix,self.offset))
f.write("%s\t\tbytes = %d\n"%(prefix,self.bytes))
if self.mem is not None:
f.write("%smem {\n"%prefix)
self.mem.output(f)
f.write("%s}\n"%prefix)
f.write("%s}\n"%shortprefix)
class kernel:
def __init__(self,name,buf_text=None,buf_const=None,buf_info=None):
self.name = name
self.lmem = 0
self.smem = 0
self.reg = 0
self.bar = 0
self.offset = 0
self.bytes = 0
self.consts = []
self.code = None
if buf_text is not None:
self.code = mem(buf_text)
if buf_const is not None:
self.consts = [constant("%s0"%self.name,buf_const,1,True)]
if buf_info is not None:
self.info = mem(buf_info,1)
def output(self,f):
f.write("code {\n")
f.write("\tname = %s\n"%self.name)
f.write("\tlmem = %d\n"%self.lmem)
f.write("\tsmem = %d\n"%self.smem)
f.write("\treg = %d\n"%self.reg )
f.write("\tbar = %d\n"%self.bar )
for c in self.consts:
c.output(f)
f.write("\tbincode {\n")
self.code.output(f)
f.write("\t}\n")
f.write("}\n")
def hex_output(self,f):
self.code.hex_output(f)
return
class mem:
def __init__(self,data,depth=0,little_endian=True):
self.le = little_endian
self.data = []
if data is not None:
self.append(data)
self.tabs = "\t"*(depth+2)
return
def append(self,data):
for i in data:
#print i
x = int(i,16)
if self.le:
# This sort of assumes that the file was built on the same arch where it's being disasm'd
x = socket.htonl(x)
self.data.append(x)
return
def output(self,f):
for i in range(0,len(self.data),4):
datastrs = ["0x%08x"%x for x in self.data[i:i+4]]
f.write("%s%s\n"%(self.tabs," ".join(datastrs)))
return
def hex_output(self,f):
f.write("%s\n"%" ,".join(["0x%08x"%x for x in self.data]))
return
def parse_objdump(objdump,codelevel,archlevel):
# Parser states
START = 0
INKERNEL = 1
INFILECONST = 2
INKCONST = 3
INKINFO = 4
INFINFO = 5
lines = [x.strip() for x in objdump.split("\n")]
format = lines[1].split("-")[-1]
little_endian = True
if format != "little":
little_endian = False
print "Warning, found a non-little-endian file"
buffer_text = []
buffer_const = []
buffer_info = []
state = START
kname = None
const_id = 0
file = cubin(codelevel)
for line in lines[2:]:
#print "%%:",line
if len(line.strip()) == 0:
continue
if state == START: #{{{
if line.startswith("Contents of section .text"):
kname = line.split(".")[-1].rstrip(":")
state = INKERNEL
continue
elif len(line) == 0:
continue
else:
raise ValueError("Got unexpected line in state START\n%s"%line)
#}}}
elif state == INKERNEL: #{{{
if line.startswith("Contents of section .text"):
# Handle new kernel
# Store old kernel
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
buffer_text = []
buffer_const = []
buffer_info = []
kname = line.split(".")[-1].rstrip(":")
state = INKERNEL
continue
elif line.startswith("Contents of section .nv.constant"):
# Handle const data
if line.endswith("%s:"%kname):
state = INKCONST
continue
else:
# This is a file constant section
# Store old kernel
state = INFILECONST
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
kname = None
buffer_text = []
buffer_const = []
buffer_info = []
continue
elif line.startswith("Contents of section .nv.info:"):
# Entering a file info section, dump existing kernel
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
buffer_text = []
buffer_const = []
buffer_info = []
kname = None
state = INFINFO
elif line.startswith("Contents of section .nv.info"):
# Handle kernel info
state = INKINFO
continue
elif ishexdigit(line[0]):
# Handle new line of kernel binary
# Take hex dump from objdump, remove address and ASCIIization
buffer_text.extend(line[6:42].split())
else:
raise ValueError("Got unexpected line in state INKERNEL\n%s"%line)
#}}}
elif state == INFILECONST: #{{{
if line.startswith("Contents of section .text"):
# Handle new kernel
# Store old kernel
file.consts.append(constant("constant%d"%const_id,buffer_const))
buffer_const = []
const_id = 0
kname = line.split(".")[-1].rstrip(":")
state = INKERNEL
continue
elif line.startswith("Contents of section .nv.constant"):
# Handle const data
# we should not be in a kernel here
# TODO make sure not in kernel
file.consts.append(constant("constant%d"%const_id,buffer_const))
buffer_const = []
const_id = 0
state = INFILECONST
continue
elif line.startswith("Contents of section .nv.info:"):
# Entering a file info section, dump existing section
file.consts.append(constant("constant%d"%const_id,buffer_const))
buffer_const = []
const_id = 0
kname = None
state = INFINFO
elif line.startswith("Contents of section .nv.info"):
# Handle kernel info
raise ValueError("Error, got into an .nv.info section from a file const section")
elif ishexdigit(line[0]):
# Handle new line of kernel binary
# Take hex dump from objdump, remove address and ASCIIization
buffer_const.extend(line[6:42].split())
else:
raise ValueError("Got unexpected line in state INFILECONST\n%s"%line)
#}}}
elif state == INKCONST: #{{{
if line.startswith("Contents of section .text"):
# Handle new kernel
# Store old kernel
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
buffer_text = []
buffer_const = []
buffer_info = []
const_id = 0
kname = line.split(".")[-1].rstrip(":")
state = INKERNEL
continue
elif line.startswith("Contents of section .nv.constant"):
# Handle const data
if line.endswith("%s:"%kname):
#raise ValueError("Can't deal yet with kernels with multiple const sections")
pass
else:
# This is a file constant section
# Store old kernel
state = INFILECONST
file.append(kernel(kname,buffer_text,buffer_const,buffer_info))
kname = None
buffer_text = []
buffer_const = []
buffer_info = []
const_id = 0
continue
elif line.startswith("Contents of section .nv.info:"):
# Entering a file info section, dump existing kernel
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
buffer_text = []
buffer_const = []
buffer_info = []
kname = None
state = INFINFO
elif line.startswith("Contents of section .nv.info"):
# Handle kernel info
state = INKINFO
continue
elif ishexdigit(line[0]):
# Handle new line of kernel binary
# Take hex dump from objdump, remove address and ASCIIization
buffer_const.extend(line[6:42].split())
else:
raise ValueError("Got unexpected line in state INKCONST\n%s"%line)
#}}}
elif state == INKINFO: #{{{
if line.startswith("Contents of section .text"):
# Handle new kernel
# Store old kernel
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
buffer_text = []
buffer_const = []
buffer_info = []
const_id = 0
kname = line.split(".")[-1].rstrip(":")
state = INKERNEL
continue
elif line.startswith("Contents of section .nv.constant"):
# Handle const data
if line.endswith("%s:"%kname):
state = INKCONST
continue
else:
# This is a file constant section
# Store old kernel
state = INFILECONST
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
kname = None
buffer_text = []
buffer_const = []
buffer_info = []
continue
elif line.startswith("Contents of section .nv.info:"):
# Entering a file info section, dump existing kernel
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
buffer_text = []
buffer_const = []
buffer_info = []
kname = None
state = INFINFO
elif line.startswith("Contents of section .nv.info"):
# Handle kernel info
raise ValueError("Can't handle kernels with multiple INFO sections")
state = INKINFO
continue
elif ishexdigit(line[0]):
# Handle new line of kernel binary
# Take hex dump from objdump, remove address and ASCIIization
buffer_info.extend(line[6:42].split())
else:
raise ValueError("Got unexpected line in state INKINFO\n%s"%line)
#}}}
elif state == INFINFO: #{{{
if line.startswith("Contents of section .text"):
# Handle new kernel
# Store old kernel
file.info.append(mem(buffer_info))
buffer_info = []
kname = line.split(".")[-1].rstrip(":")
state = INKERNEL
continue
elif line.startswith("Contents of section .nv.constant"):
# Handle const data
# we should not be in a kernel here
# TODO make sure not in kernel
file.info.append(mem(buffer_info))
buffer_info = []
state = INFILECONST
continue
elif line.startswith("Contents of section .nv.info:"):
# Entering a file info section, dump existing section
file.info.append(mem(buffer_info))
buffer_info = []
state = INFINFO
elif line.startswith("Contents of section .nv.info"):
# Handle kernel info
raise ValueError("Error, got into an .nv.info.KERNEL section from a file info section")
elif ishexdigit(line[0]):
# Handle new line of kernel binary
# Take hex dump from objdump, remove address and ASCIIization
buffer_info.extend(line[6:42].split())
else:
raise ValueError("Got unexpected line in state INFINFO\n%s"%line)
#}}}
else:
raise ValueError("wtf bad state")
if kname is None:
if len(buffer_const) > 0:
file.consts.append(constant("constant%d"%const_id,buffer_const))
else:
file.kernels.append(kernel(kname,buffer_text,buffer_const,buffer_info))
return file
def parseHeader(elfname):
# Parse compute level from elf header
elf = open(elfname,"rb")
elf.seek(4)
elflevel = unpack('B',elf.read(1))[0]
if elflevel == 1:
# ELF32 has flags at 36 bytes in
elf.seek(36)
elif elflevel == 2:
# ELF64 has flags at 48 bytes in
elf.seek(48)
else:
raise ValueError("Invalid elflevel %x"%elflevel)
flags = unpack("I",elf.read(4))[0]
elf.close()
#print "0x%08x"%flags
codeint = flags & 0xFFFF
archint = (flags & 0xFFFF0000) >> 16
code = "sm_%d"%codeint
arch = "sm_%d"%archint
#print "code",code, "arch",arch
return (code,codeint,arch,archint)
if len(sys.argv) < 2:
print "Usage: elfToCubin [--nouveau] [input file]"
sys.exit(1)
nouveau = False
elfname = None
if sys.argv[1] == '--nouveau':
nouveau = True
if len(sys.argv) < 3:
print "Usage: elfToCubin [--nouveau] [input file]"
sys.exit(1)
elfname = sys.argv[2]
else:
elfname = sys.argv[1]
(code,code_int,arch,arch_int) = parseHeader(elfname)
output = "".join(Popen(["objdump", "-s", elfname], stdout=PIPE).communicate()[0])
cubin = parse_objdump(output,code,arch)
if not nouveau:
cubin.output(sys.stdout)
else:
disassembler = None
if code_int < 20:
disassembler = "nv50dis"
else:
disassembler = "nvc0dis"
for kernel in cubin.kernels:
print "--> Disassembling kernel ",kernel.name,"with",disassembler
disasm = Popen(disassembler,stdin=PIPE,stdout=PIPE)
hex = StringIO()
kernel.hex_output(hex)
output = "".join(disasm.communicate(hex.getvalue())[0])
hex.close()
print output
print