-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibdeps.py
executable file
·117 lines (90 loc) · 3.31 KB
/
libdeps.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
#!/usr/bin/python3
import subprocess
import sys
import copy
libprovides = dict()
librequires = dict()
matrix = dict()
visited = dict()
def add_provides(lib, function):
global libprovides
if not lib in libprovides.keys():
libprovides[lib] = dict()
libprovides[lib][function] = "Yes"
def add_requires(lib, function):
global librequires
if not lib in librequires.keys():
librequires[lib] = dict()
librequires[lib][function] = "Yes"
def add_from_to_func(req, prov, func):
global matrix
if not req in matrix.keys():
matrix[req] = dict()
if not prov in matrix[req].keys():
matrix[req][prov] = list()
matrix[req][prov].append(func)
def process_library(filename):
pipeout = subprocess.check_output(['nm', '-D', filename]).decode("utf-8")
lines = pipeout.split('\n')
for line in lines:
words = line.split()
if len(words) > 1:
if words[0] == 'U':
add_requires(filename, words[1])
if words[1] == 'T':
add_provides(filename, words[2])
if words[1] == 'W':
add_provides(filename, words[2])
def process_binary(filename):
process_library(filename)
pipeout = subprocess.check_output(['ldd', '-r', filename]).decode("utf-8").replace("\t","")
lines = pipeout.split('\n')
for line in lines:
words = line.split()
if len(words) > 2:
process_library(words[2])
def create_matrix():
for file in librequires.keys():
for symbol in librequires[file].keys():
for file2 in libprovides.keys():
if file2 == '/usr/lib64/ld-linux-x86-64.so.2':
continue
if file2 == '/usr/lib64/libc.so.6':
continue
if file2 == '/usr/lib64/haswell/libc.so.6':
continue
if file2 == '/usr/lib64/libpthread.so.0':
continue
if file != file2 and symbol in libprovides[file2].keys():
add_from_to_func(file, file2, symbol)
def print_matrix(req, level):
if not req in matrix.keys():
return
for prov in matrix[req].keys():
start = ""
for i in range(level):
start += "\t"
reason = "(" + str(len(matrix[req][prov])) + ") " + matrix[req][prov][0]
if len(matrix[req][prov]) > 1:
reason += " " + matrix[req][prov][1]
if len(matrix[req][prov]) > 2:
reason += " " + matrix[req][prov][2]
if len(matrix[req][prov]) > 3:
reason += " " + matrix[req][prov][3]
if len(matrix[req][prov]) > 4:
reason += " " + matrix[req][prov][4]
if len(matrix[req][prov]) > 5:
reason += " " + matrix[req][prov][5]
if len(matrix[req][prov]) > 5:
reason += " ..."
print(start, req, "pulls in", prov, "because of", reason)
if prov in visited:
continue
visited[prov] = "Yes"
print_matrix(prov, level + 1)
def main():
process_binary(sys.argv[1])
create_matrix()
print_matrix(sys.argv[1], 0)
if __name__ == '__main__':
main()