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
|
import tempfile
import os
from os import path
import sys
import re
import subprocess
from contextlib import contextmanager
from collections import defaultdict
import colorsys
import cxxfilt
from elftools.elf.elffile import ELFFile
from elftools.elf.descriptions import describe_symbol_type
import libarchive
import matplotlib.cm
@contextmanager
def chdir(newdir):
old_cwd = os.getcwd()
try:
os.chdir(newdir)
yield
finally:
os.chdir(old_cwd)
def trace_source_files(linker, cmdline, trace_sections=[]):
with tempfile.TemporaryDirectory() as tempdir:
out_path = path.join(tempdir, 'output.elf')
output = subprocess.check_output([linker, '-o', out_path, f'-Wl,--cref', *cmdline])
lines = [ line.strip() for line in output.decode().splitlines() ]
# FIXME also find isr vector table references
defs = {}
for line in lines[lines.index('Cross Reference Table')+3:]:
try:
*left, right = line.split()
if left:
defs[' '.join(left)] = right
except:
pass
refs = defaultdict(lambda: set())
syms = {}
for sym, obj in defs.items():
fn, _, member = re.match('^([^()]+)(\((.+)\))?$', obj).groups()
fn = path.abspath(fn)
if member:
subprocess.check_call(['ar', 'x', '--output', tempdir, fn, member])
fn = path.join(tempdir, member)
with open(fn, 'rb') as f:
elf = ELFFile(f)
symtab = elf.get_section_by_name('.symtab')
symtab_demangled = { cxxfilt.demangle(nsym.name).replace(' ', ''): i
for i, nsym in enumerate(symtab.iter_symbols()) }
def lookup_size(name):
name_normalized = name.replace(' ', '')
if name_normalized in symtab_demangled:
entry = symtab.get_symbol(symtab_demangled[name_normalized])
return entry['st_size']
else:
return None
syms[sym] = fn, lookup_size(sym)
s = set()
sec_map = { sec.name: i for i, sec in enumerate(elf.iter_sections()) }
sec_name = f'.rel.text.{sym}'
matches = [ i for name, i in sec_map.items() if re.match(f'\.rel\..*\.{sym}', name) ]
if matches:
sec = elf.get_section(matches[0])
for reloc in sec.iter_relocations():
refsym = symtab.get_symbol(reloc['r_info_sym'])
s.add(refsym.name)
if refsym.name not in defs:
syms[refsym.name] = fn, lookup_size(refsym.name)
refs[sym] = s
for tsec in trace_sections:
matches = [ i for name, i in sec_map.items() if name == f'.rel{tsec}' ]
s = set()
if matches:
sec = elf.get_section(matches[0])
for reloc in sec.iter_relocations():
refsym = symtab.get_symbol(reloc['r_info_sym'])
s.add(refsym.name)
refs[tsec.replace('.', '_')] |= s
syms_out = set()
with open(out_path, 'rb') as f:
elf = ELFFile(f)
symtab = elf.get_section_by_name('.symtab')
for sym in symtab.iter_symbols():
if describe_symbol_type(sym['st_info']['type']) == 'FUNC':
syms_out.add(sym.name)
#for sym in defs:
# entry = symtab.get_symbol_by_name(sym)
# if entry is None:
# syms[sym] = defs[sym], None
# else:
# syms[sym] = defs[sym], entry[0]['st_size']
return syms, refs, syms_out
@contextmanager
def wrap(leader='', print=print, left='{', right='}'):
print(leader, left)
yield lambda *args, **kwargs: print(' ', *args, **kwargs)
print(right)
def mangle(name):
return re.sub('[^a-zA-Z0-9_]', '_', name)
hexcolor = lambda r, g, b: f'#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}'
def vhex(val):
r,g,b,_a = matplotlib.cm.viridis(1.0-val)
fc = hexcolor(r, g, b)
h,s,v = colorsys.rgb_to_hsv(r,g,b)
cc = '#000000' if v > 0.8 else '#ffffff'
return fc, cc
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--trace-sections', type=str, action='append', default=[])
parser.add_argument('--trim-stubs', type=str, action='append', default=[])
parser.add_argument('linker_binary')
parser.add_argument('linker_args', nargs=argparse.REMAINDER)
args = parser.parse_args()
trace_sections = args.trace_sections
trace_sections_mangled = { sec.replace('.', '_') for sec in trace_sections }
syms, refs, syms_out = trace_source_files(args.linker_binary, args.linker_args, trace_sections)
for name, (obj, size) in syms.items():
if path.basename(obj) in args.trim_stubs and size <= 8 and not refs.get(name):
syms_out.discard(name)
clusters = defaultdict(lambda: [])
for sym, (obj, size) in syms.items():
clusters[obj].append((sym, size))
obj_size = defaultdict(lambda: 0)
for name, (obj, size) in syms.items():
if size is not None:
obj_size[obj] += size
max_size = max([ size for _obj, size in syms.values() if size is not None ])
max_osize = max(obj_size.values())
with wrap('digraph G', print) as lvl1print:
print('rankdir=LR;')
print()
for i, (obj, syms) in enumerate(clusters.items()):
with wrap(f'subgraph cluster_{i}', lvl1print) as lvl2print:
fc, cc = vhex(obj_size[obj]/max_osize)
lvl2print(f'label = <<table border="0"><tr><td border="0" cellpadding="5" bgcolor="{fc}"><font face="carlito" color="{cc}"><b>{path.basename(obj)} ({obj_size[obj]}B)</b></font></td></tr></table>>;')
lvl2print()
for sym, size in syms:
if sym in syms_out:
fc, cc = vhex(size/max_size)
lvl2print(f'{mangle(sym)}[label = "{sym} ({size}B)", style="rounded,filled", shape="box", fillcolor="{fc}", fontname="carlito", fontcolor="{cc}" color=None];')
lvl1print()
for start, ends in refs.items():
for end in ends:
if end and (start in syms_out or start in trace_sections_mangled) and end in syms_out:
lvl1print(f'{mangle(start)} -> {mangle(end)} [style="bold", color="#333333"];')
for sec in trace_sections:
lvl1print(f'{sec.replace(".", "_")} [label = "section {sec}", shape="box", style="filled,bold"];')
|