-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisasm.py
126 lines (118 loc) · 5.47 KB
/
disasm.py
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
import os
import sys
from typing import List
from cprinter import CPrinter
from fprinter import FPrinter
from smsymer import utils
from smsymer.evm import ByteCode
def process(args):
c_printer = CPrinter()
if args.result is not None:
if not os.path.exists(args.result):
c_printer.error("Result output directory '{0}' does not exist".format(args.result))
sys.exit(-1)
if not os.path.isdir(args.result):
c_printer.error("'{0}' is not a directory".format(args.result))
sys.exit(-1)
if args.file:
if args.source and args.extension is None:
args.extension = 'sol'
elif args.bytecode and args.extension is None:
args.extension = 'hex'
c_printer.info("processing files with extension '{0}'".format(args.extension))
for s in args.input:
if os.path.splitext(s)[-1][1:] != args.extension:
c_printer.warn("file '{0}' extension mismatch".format(s))
c_printer.warn("skipping '{0}'".format(s))
continue
if not os.path.exists(s):
c_printer.error("file '{0}' does not exist".format(s))
c_printer.warn("skipping '{0}'".format(s))
elif not os.path.isfile(s):
c_printer.error("'{0}' is not a file".format(s))
c_printer.warn("skipping '{0}'".format(s))
else:
with open(s) as file:
c_printer.info("start disassembling {0}".format(s))
try:
if args.source:
_, bytecode = utils.compile_sol(s)[0]
else:
bytecode = ''.join(file.readlines())
if args.result is not None:
filename = os.path.splitext(s)[0] + ".asm"
f_printer = FPrinter(filename=os.path.join(args.result, filename))
f_printer.print(disasm(bytecode))
else:
c_printer.print(disasm(bytecode))
except AttributeError as e:
c_printer.error(str(e))
c_printer.info("fail to disassemble {0}".format(s))
else:
c_printer.info("finish disassembling {0}".format(s))
elif args.dir:
if args.source and args.extension is None:
args.extension = 'sol'
elif args.bytecode and args.extension is None:
args.extension = 'hex'
c_printer.info("processing files with extension '{0}'".format(args.extension))
for s in args.input:
if not os.path.exists(s):
c_printer.error("directory '{0}' does not exist")
c_printer.warn("skipping '{0}'")
elif not os.path.isdir(s):
c_printer.error("'{0}' is not a directory")
c_printer.warn("skipping '{0}'")
else:
process_dir(s, args)
else:
for i, s in enumerate(args.input):
bytecode = s
c_printer.info("start disassembling {0}".format(s))
try:
if args.result is not None:
filename = str(i) + ".asm"
f_printer = FPrinter(filename=os.path.join(args.result, filename))
f_printer.print(disasm(bytecode))
else:
c_printer.print(disasm(bytecode))
except AttributeError as e:
c_printer.error(str(e))
c_printer.info("fail to disassemble {0}".format(s))
else:
c_printer.info("finish disassembling {0}".format(s))
def process_dir(directory: str, args):
c_printer = CPrinter()
for item in os.listdir(directory):
if os.path.isdir(item):
if args.recursively:
process_dir(item, args)
else:
c_printer.warn("not specify -R option, skipping subdirectory '{}'".format(item))
else:
if os.path.splitext(item)[-1][1:] != args.extension:
c_printer.warn("file '{0}' extension mismatch".format(item))
c_printer.warn("skipping '{0}'".format(item))
continue
with open(os.path.join(directory, item)) as file:
c_printer.info("start disassembling {0}".format(os.path.join(directory, item)))
try:
if args.source:
_, bytecode = utils.compile_sol(os.path.join(directory, item))[0]
else:
bytecode = ''.join(file.readlines())
if args.result is not None:
filename = os.path.join(args.result, os.path.split(directory)[0], item + ".asm")
f_printer = FPrinter(filename=filename)
f_printer.print(disasm(bytecode))
else:
c_printer.print(disasm(bytecode))
except AttributeError as e:
c_printer.error(str(e))
c_printer.info("fail to disassemble {0}".format(os.path.join(directory, item)))
else:
c_printer.info("finish disassembling {0}".format(os.path.join(directory, item)))
def disasm(bytecode: str) -> List[str]:
c_printer = CPrinter()
instructions = ByteCode.disasm(bytecode, c_printer)
return list(map(lambda ins: str(ins), instructions))