-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextract_func_opcodes_from_objdump.py
53 lines (43 loc) · 1.32 KB
/
extract_func_opcodes_from_objdump.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
# -*- coding: utf-8 -*-
import re
import sys
import os
func_name_re = re.compile('<[_a-zA-Z].*>:')
hex_re = re.compile('{[0-9a-fA-F], 2}:')
def is_end_of_func(line):
return line == '\n'
def instrs_to_opcodes(instrs):
opcodes = ''
for instr in instrs:
opcode = instr.split('\t')
if len(opcode) < 3:
continue
opcode = opcode[-1].split()[0]
if opcode == '(bad)' or opcode == '...':
continue
opcodes += opcode + ' '
return opcodes
if __name__ == '__main__':
file_name = sys.argv[1]
outdir = sys.argv[2]
if not os.path.isdir(outdir):
os.mkdir(outdir)
with open(file_name, 'r') as f:
lines = f.readlines()
in_func = False
func_name = None
func_instrs = []
for l in lines:
search = func_name_re.search(l)
if not in_func and search:
in_func = True
func_name = search[0][:-1]
func_instrs = []
elif in_func:
if is_end_of_func(l):
in_func = False
opcodes = instrs_to_opcodes(func_instrs)
with open(os.path.join(outdir, func_name), 'w') as f:
f.write(opcodes)
else:
func_instrs.append(l)