-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjdump2html.py
executable file
·166 lines (123 loc) · 4.49 KB
/
objdump2html.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
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
import sys
import re
import xml.sax.saxutils as _saxutils
import pprint
class ISA:
def is_func_def(self, line: str):
raise NotImplemented
def get_func_def_name(self, line: str):
raise NotImplemented
def is_func_call(self, line: str):
raise NotImplemented
def get_func_call_name(self, line: str):
raise NotImplemented
def is_section_start(self, line: str):
raise NotImplemented
def get_section_name(self, line: str):
raise NotImplemented
RE_FUNC = "<([a-zA-Z_]+)>"
RE_FUNC_DEF = "[0-9a-z]+ <([a-zA-Z_]+)>:"
RE_FUNC_CALL = "bl +[0-9a-z]+ <([a-zA-Z_]+)>"
S_SECTION_START = "Disassembly of section"
RE_SECTION_NAME = "Disassembly of section .([a-z\._A-Z0-9]+)"
class ARM(ISA):
def __init__(self):
self.re_func_def = re.compile(RE_FUNC_DEF)
self.re_func_call = re.compile(RE_FUNC)
self.re_sec_name = re.compile(RE_SECTION_NAME)
def is_func_def(self, line):
res_func_def = self.re_func_def.findall(line)
return res_func_def != []
def get_func_def_name(self, line: str):
return self.re_func_def.findall(line)[0]
def is_func_call(self, line: str):
split_line = line.split('\t')
if len(split_line) > 1 and split_line[-2] == 'bl':
return self.re_func_call.findall(split_line[-1].split(' ')[-1]) != []
return False
def get_func_call_name(self, line: str):
split_line = line.split('\t')
if len(split_line) > 1 and split_line[-2] == 'bl':
return self.re_func_call.findall(split_line[-1].split(' ')[-1])[0]
return False
def is_section_start(self, line: str):
return line.startswith(S_SECTION_START)
def get_section_name(self, line: str):
return self.re_sec_name.findall(line)[0]
def get_escaped_html(line: str):
return _saxutils.escape(line,
{
' ': ' ',
'\t': ' '
})
re_func_def = re.compile(RE_FUNC_DEF)
all = []
current_section = []
funcs = []
func_defs = {}
func_calls = {}
section_starts = {}
rev_func_calls = {}
within_func = ""
isa_model = ARM()
line_no = 0
def get_func_def_map(func_def: str):
if func_def not in func_defs:
func_defs[func_def] = \
{
'line_no':0
}
return func_defs[func_def]
def get_func_call_map(func_name: str):
if func_name not in func_calls:
func_calls[func_name] = \
{
'line_no':[],
'call_from':[]
}
return func_calls[func_name]
for line in sys.stdin.readlines():
if isa_model.is_func_def(line):
func_def_name = isa_model.get_func_def_name(line)
get_func_def_map(func_def_name)['line_no'] = line_no
within_func = func_def_name # scope
elif isa_model.is_func_call(line):
func_name = isa_model.get_func_call_name(line)
get_func_call_map(func_name)['call_from'].append(within_func)
get_func_call_map(func_name)['line_no'].append(line_no)
rev_func_calls[line_no] = func_name
elif isa_model.is_section_start(line):
section_starts[isa_model.get_section_name(line)] = line_no
all.append(line)
line_no +=1
rev_funcs = {}
func_calls_from_line_nos = {}
def get_rev_func(func_line_no: int) -> list:
if func_line_no not in rev_funcs:
rev_funcs[func_line_no] = []
return rev_funcs[func_line_no]
for call_line_no, func_name in rev_func_calls.items():
func_line_no = get_func_def_map(func_name)['line_no']
get_rev_func(func_line_no).append(call_line_no)
func_calls_from_line_nos[call_line_no] = func_line_no
line_no = 0
print("<!DOCTYPE HTML>")
print("<html><title>objdump output</title>")
print("<body>")
for line in all:
if line_no not in func_calls_from_line_nos:
if line_no in rev_funcs:
print("<code>Refs: ")
for ref in rev_funcs[line_no]:
print("<a href=\"#%s\">%s</a>" % (ref, ref))
print("</code>")
print("</br>")
if line_no in rev_funcs:
print("<span style=\"color:red\"><code id=\"%s\">%s</code></span>" % (line_no, get_escaped_html(line)))
else:
print("<code id=\"%s\">%s</code>" % (line_no, get_escaped_html(line)))
else:
print("<code id=\"%s\"><a href=\"#%s\">%s</a></code>" % (line_no,func_calls_from_line_nos[line_no], get_escaped_html(line)))
print("<br/>")
line_no += 1
print("</body></html>")