-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdep-symbol.py
executable file
·323 lines (245 loc) · 8.86 KB
/
dep-symbol.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
import subprocess
import os.path
import sys
import glob
from optparse import OptionParser
import json
ARCH='x86_64-linux-gnu'
working_dir = ""
EXCLUDES=["libc6", "libgcc1", "gcc-8-base", "<debconf-2.0>", "debconf"]
# Really just for tracking a bit more about a symbol stored in our table
class Symbol:
name = ""
libs = []
metas = []
def __init__(self,name,libs,metas):
self.name = name
self.libs = libs
self.metas = metas
class Meta:
package_name = ""
package_deb = ""
has_symbols = False
shared_libs = []
def __init__(self,package_name,package_deb,has_symbols,shared_libs):
self.package_name = package_name
self.package_deb = package_deb
self.has_symbols = has_symbols
self.shared_libs = shared_libs
def add_lib(self, lib):
if lib not in self.shared_libs:
self.shared_libs.append(lib)
def json_state(self):
return self.__dict__
def as_meta(dct):
return Meta(dct["package_name"],dct["package_deb"],dct["has_symbols"],dct["shared_libs"])
class MetaEncoder(json.JSONEncoder):
def default(self,o):
if isinstance(o, Meta):
return o.json_state()
else:
return json.JSONEncoder.default(self, o)
def read_dependency_list(name):
deps = {}
with open(name, 'r') as f:
for d in f.read().splitlines():
deps[d] = True
return deps
def download_deps(deps,metas):
debs = {}
for d in deps:
if d in metas:
continue
print('fetching ' + d)
try:
out = subprocess.check_output(['apt-get', 'download', d], stderr=subprocess.STDOUT)
deb = glob.glob(d + '*.deb')[0]
debs[d] = deb
os.rename(deb, working_dir + '/' + deb)
except:
print("No package found for " + d)
return debs
def trim_libname(libpath):
return libpath.split("/")[-1].split('.')[0]
def gather_libs(path):
out = subprocess.run(['find',path, '-name', 'lib*.so*'], stdout=subprocess.PIPE)
libs = []
for l in out.stdout.splitlines():
libs.append(l.decode('utf-8'))
return libs
def build_symbols(meta):
added = set()
with open("symbols", "w") as f:
try:
subprocess.check_call(['dpkg', '-x', meta.package_deb, 'tmp'])
libs = gather_libs("tmp")
for l in libs:
n = trim_libname(l)
if n in added:
continue
subprocess.check_call(['dpkg-gensymbols', '-v0', '-p' + meta.package_name, '-e{}'.format(l), '-Osymbols-t'])
with open("symbols-t") as f2:
f.write(f2.read())
added.add(n)
os.remove("symbols-t")
meta.has_symbols = True
except subprocess.CalledProcessError as err:
print(err)
print('failed to build symbols file for ' + meta.package_deb)
def extract_debs(debs,metas):
# Create some metadata about our little repository
home = os.getcwd()
for dep,deb in debs.items():
debhome = os.path.join(working_dir,dep)
if os.path.exists(debhome):
continue
os.mkdir(debhome)
os.rename(os.path.join(working_dir,deb),os.path.join(debhome,deb))
os.chdir(debhome)
print('Extracting ' + deb)
out = subprocess.check_output(['ar', '-xv', deb])
if os.path.exists("control.tar.xz"):
out = subprocess.check_output(['tar', 'xf', 'control.tar.xz'])
elif os.path.exists("control.tar.gz"):
out = subprocess.check_output(['tar', '-xzf', 'control.tar.gz'])
# Test for symbol file
if (os.path.exists('symbols')):
os.remove('symbols')
meta = Meta(dep, deb, False, [])
build_symbols(meta)
metas[deb] = meta
os.chdir(home)
def parse_symbols(meta,symbols):
# We'll point every symbol to its metadata for now
# Build a true repo later
with open(os.path.join(working_dir, meta.package_name, "symbols")) as f:
current_lib = ""
for l in f.readlines():
if l[0] != " " and l[0] != "|" and l[0] != '*':
current_lib = l.split()[0]
meta.add_lib(current_lib)
else:
toks = l.split()
if toks[0] == "|" or toks[0] == "*":
pass
else:
name = toks[0].split("@")[0]
if name in symbols:
# Possible conflict (really only an issue between packages for now)
symbols[name].libs.append(current_lib)
symbols[name].metas.append(meta)
else:
symbols[name] = Symbol(name,[current_lib],[meta])
def load_meta():
metas = {}
if os.path.exists(os.path.join(working_dir, 'meta.txt')):
with open(os.path.join(working_dir, 'meta.txt'), 'r') as f:
for line in f:
m = json.loads(line, object_hook=Meta.as_meta)
metas[m.package_name] = m
return metas
def save_meta(meta):
with open(os.path.join(working_dir,'meta.txt'), 'w') as f:
for k,m in metas.items():
f.write(json.dumps(m, cls=MetaEncoder) + '\n')
def save_packages(meta):
with open(os.path.join(working_dir,'packages.txt'), 'w') as f:
for k,m in metas.items():
for l in m.shared_libs:
f.write("{} {}\n".format(l, m.package_name))
def exclude_symbol(exclude, libs):
for e in exclude:
for l in libs:
if e in l:
return True
return False
def load_symbols(metas):
symbols = {}
for k,m in metas.items():
if m.has_symbols:
parse_symbols(m, symbols)
return symbols
def save_symbols(symbols):
with open(os.path.join(working_dir,'symbols.txt'), 'w') as f:
f.write("{}\n".format(len(symbols)))
for k,s in symbols.items():
if exclude_symbol(["libc.so"], s.libs):
continue
f.write("{} {}\n".format(s.name, s.libs[0]))
#for l in s.libs:
# f.write(" {}".format(l))
#f.write("\n")
def load_trace(name):
calls = []
if os.path.exists(name):
with open(name, 'r') as f:
for line in f:
try:
j = json.loads(line)
calls.append(j)
except json.decoder.JSONDecodeError:
continue
else:
return {}
return calls
def check_deps(metas,deps,symbols,calls):
stats = {}
for d in deps:
stats[d] = None
for c in calls:
if c["indirect"]:
continue
fname = c["fnptr"][1:]
sym = symbols.get(fname)
if sym is not None:
stats[sym.metas[0].package_name] = sym.libs[0]
return stats
def dump_deps(stats,outfile):
# Just for nice output
used = []
notused = []
for d,t in stats.items():
if t is not None:
used.append({"package_name":d, "shared_lib":t})
else:
notused.append(d)
print('Package has ' + str(len(stats)) + ' tracked dependencies')
print('Using ' + str(len(used)) + ':')
for d in used:
print('\t' + d["package_name"] + ' ===> ' + d["shared_lib"])
print('Not using ' + str(len(notused)) + ':')
for d in notused:
print('\t' + d)
if outfile is not None:
j = {}
j["used"] = used
j["notused"] = notused
with open(outfile, 'w') as f:
json.dump(j,f,indent=2)
usage = "usage: %prog [options] dependency-list"
parser = OptionParser(usage=usage)
parser.add_option('-d', '--dir', dest='working_dir', default='symbol-out', help='use DIR as working output directory', metavar='DIR')
parser.add_option('-t', '--trace', dest='trace', help='load trace file DIR', metavar='TRACE')
parser.add_option('-l', '--load', action='store_true', help='jump straight to loading the repository symbols')
parser.add_option('-o', '--outfile', dest='outfile', default=None, help='dump json data to OUTFILE for post-processing', metavar='OUTFILE')
parser.add_option('-s', '--src', action='store_true', default=None, help='download source packages from dep file', metavar='SRC')
(options, args) = parser.parse_args()
working_dir = options.working_dir
metas = load_meta()
if len(args) < 1:
print("error: must supply dependency-list")
parser.print_usage()
sys.exit(1)
if not options.load:
deps=read_dependency_list(args[0])
debs=download_deps(deps,metas)
extract_debs(debs,metas)
symbols = load_symbols(metas)
save_meta(metas)
save_packages(metas)
save_symbols(symbols)
if options.trace is not None:
calls = load_trace(options.trace)
stats = check_deps(metas,deps,symbols,calls)
dump_deps(stats, options.outfile)