-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjdump-cfg
executable file
·288 lines (250 loc) · 9.01 KB
/
objdump-cfg
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
#!/usr/bin/env pypy3
# -*- coding: utf-8 -*-
# Infer control flow graph via objdump's output.
# FIXME: A more reliable way to get this task done should be implementing the dumper in llvm-objdump.
import os
import sys
import re
import subprocess
import argparse
import shutil
import bisect
import logging
import io
import shlex
FUNCTION_BEGIN_LINE = re.compile(r'^([0-9a-f]+)\s+<(.+)>:$')
INSTRUCTION_LINE = re.compile(r'^\s*([0-9a-f]+):\s*(.*)$')
INSTRUCTION = re.compile(r'([^<#]+)(<([^\+]+)(\+([0-9a-fx]+))?>)?')
UNCONDITIONAL_BRANCHES = [
re.compile(x) for x in [
r'\bb\b',
r'\bjmp\b',
r'\bjmpq\b',
]
]
LONG_BRANCHES = [
re.compile(x) for x in [
r'\bblr\b',
r'\bretq\b',
r'\bret\b',
r'\bbx\b',
]
]
def IsUncondBr(s):
for r in UNCONDITIONAL_BRANCHES:
if r.search(s):
return True
return False
def IsLongBranch(s):
for r in LONG_BRANCHES:
if r.search(s):
return True
return False
def LowerBound(a, x, lo=0, hi=None, key=lambda x: x):
l = lo
r = hi if hi else len(a)
mid = l + (r - l) // 2
while l < r:
if key(a[mid]) >= x:
r = mid
else:
l = mid + 1
mid = l + (r - l) // 2
return r
def UpperBound(a, x, lo=0, hi=None, key=lambda x: x):
l = lo
r = hi if hi else len(a)
mid = l + (r - l) // 2
while l < r:
if key(a[mid]) > x:
r = mid
else:
l = mid + 1
mid = l + (r - l) // 2
return r
class Function(object):
def __init__(self, name):
self.name = name
self.address = -1
self.instructions = []
def Append(self, address, instruction):
self.instructions.append((address, instruction))
def IsEmpty(self):
return len(self.instructions) == 0
class CFGAnalyzer(object):
def __init__(self, function, branch_analyzer):
self.function = function
self.branch_analyzer = branch_analyzer
self.branch_targets = set()
# List of (start_index_of_block, block_length).
self.block_intervals = []
self.preds = {}
self.succs = {}
def Analyze(self):
if len(self.function.instructions) == 0:
return
# 1. Every branch target should start a new block.
# 2. A block ends by either a branch or encounter a branch target.
for b in self.branch_analyzer.branches:
self.branch_targets.update(self.branch_analyzer.branches[b])
# Add entry block to branch target for convenience.
self.branch_targets.add(0)
for t in self.branch_targets:
self.preds[t] = set()
self.succs[t] = set()
i = -1
for j in range(len(self.function.instructions)):
if j in self.branch_targets:
if i >= 0:
self.block_intervals.append((i, j - i))
# This should be fallthrough.
self.preds[j].add(i)
self.succs[i].add(j)
i = j
if j in self.branch_analyzer.branches:
if i >= 0:
self.block_intervals.append((i, j - i + 1))
assert (i in self.branch_targets)
for branch in self.branch_analyzer.branches[j]:
assert (branch in self.branch_targets)
self.preds[branch].add(i)
self.succs[i].add(branch)
i = -1
class GraphvizPainter(object):
def __init__(self, function, cfg_analyzer):
self.function = function
self.cfg_analyzer = cfg_analyzer
def Dot(self, out_stream, name='foo'):
out_stream.write('digraph {} '.format(name))
out_stream.write('{\n')
out_stream.write(' node [shape="box", fontname="monospace"];\n')
for bb in self.cfg_analyzer.block_intervals:
bb_name = 'bb%d' % bb[0]
instructions = '\\l'.join([
'%x: %s' % (x[0], x[1])
for x in self.function.instructions[bb[0]:bb[0] + bb[1]]
]) + '\\l'
out_stream.write(' %s [label="%s"];\n' % (bb_name, instructions))
for src in self.cfg_analyzer.succs:
src_name = 'bb%d' % src
for tgt in self.cfg_analyzer.succs[src]:
tgt_name = 'bb%d' % tgt
out_stream.write(' %s -> %s;\n' % (src_name, tgt_name))
out_stream.write('}\n')
class BranchAnalyzer(object):
def __init__(self, context, function):
self.context = context
self.function = function
self.branches = {}
def Analyze(self):
logging.debug('Analyzing {}'.format(self.function.name))
for i in range(len(self.function.instructions)):
t = self.function.instructions[i]
inst = t[1]
m = INSTRUCTION.match(inst)
if not m:
# Like <unknown>. What matters is branch instruction.
continue
mg = m.groups()
assert (len(mg) >= 1)
inst_main = mg[0]
if IsLongBranch(inst_main):
self.branches[i] = []
elif len(mg) >= 5 and mg[4]:
# We might have encountered a branch. There is chance we get FP of branching.
label = mg[2]
offset = int(mg[4], 16)
label_address = self.context.FindAddress(label)
if label_address < 0:
continue
targets = []
index_of_address = self.findIndexOfAddress(label_address +
offset)
if index_of_address >= 0:
targets.append(index_of_address)
if not IsUncondBr(inst_main) and (i + 1) < len(
self.function.instructions):
# Fallthrough.
targets.append(i + 1)
if not targets:
logging.debug(
'{} is branching to external function'.format(inst))
self.branches[i] = targets
def findIndexOfAddress(self, address):
i = LowerBound(self.function.instructions, address, key=lambda t: t[0])
if i == len(self.function.instructions
) or self.function.instructions[i][0] != address:
return -1
return i
class ParseContext(object):
def __init__(self, in_stream):
self.current_function = ''
self.functions = {}
self.in_stream = in_stream
def FindAddress(self, label):
if label in self.functions:
return self.functions[label].address
return -1
def Parse(self):
for l in self.in_stream:
self.parseLine(l)
def parseLine(self, l):
if not self.current_function:
m = FUNCTION_BEGIN_LINE.match(l)
if m:
logging.debug('Found: {}'.format(m.group(2)))
self.current_function = Function(m.group(2))
self.current_function.address = int(m.group(1), 16)
self.functions[
self.current_function.name] = self.current_function
else:
m = INSTRUCTION_LINE.match(l)
if not m:
# Current function ends.
self.current_function = None
else:
address = int(m.group(1), 16)
instruction = m.group(2)
self.current_function.Append(address, instruction)
def main():
parser = argparse.ArgumentParser(
description='Output CFG dot file via objdump.')
parser.add_argument('--objdump', default=shutil.which('objdump'))
parser.add_argument('--debug', default=False, action='store_true')
parser.add_argument('--func', required=True)
parser.add_argument('-o', dest='out')
parser.add_argument('obj', nargs=1)
config = parser.parse_args()
if config.debug:
logging.basicConfig(level=logging.DEBUG)
cmd = [
config.objdump,
'-d',
'--no-show-raw-insn',
config.obj[0],
]
cp = subprocess.run(cmd, capture_output=True)
if cp.returncode != 0:
logging.error('Failed to run {}'.format(cmd))
return cp.returncode
context = ParseContext(io.StringIO(cp.stdout.decode('utf-8')))
context.Parse()
if config.func not in context.functions:
logging.error("Can't find {} in the object file".format(config.func))
return 1
function = context.functions[config.func]
BA = BranchAnalyzer(context, function)
BA.Analyze()
logging.debug("{}'s branches: {}".format(function.name, BA.branches))
CA = CFGAnalyzer(function, BA)
CA.Analyze()
GVP = GraphvizPainter(function, CA)
logging.debug("{}'s basic block layout: {}".format(function.name,
CA.block_intervals))
if not config.out:
GVP.Dot(sys.stdout)
else:
with open(config.out, 'w') as out:
GVP.Dot(out)
if __name__ == '__main__':
sys.exit(main())