-
Notifications
You must be signed in to change notification settings - Fork 1
/
sym_graph.py
executable file
·157 lines (114 loc) · 4 KB
/
sym_graph.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
#! /usr/bin/env python3
import argparse
import os.path
import sys
from elftools.elf.elffile import ELFFile
from elftools.elf.sections import SymbolTableSection
import dataclasses
VERBOSITY = 0
def dprint(*args, **kwargs):
if VERBOSITY > 0:
print(*args, **kwargs)
def eprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
@dataclasses.dataclass
class Symbol:
def_file: str
ref_files: list[str]
type: str
@dataclasses.dataclass
class File:
name: str
def_syms: list[str]
ref_syms: list[str]
FILES: dict[str, File] = {}
SYMBOLS: dict[str, Symbol] = {}
def read_file(filepath: str):
filename = os.path.basename(filepath)
with open(filepath, "rb") as f:
elffile = ELFFile(f)
symtab_name = ".symtab"
symtab = elffile.get_section_by_name(symtab_name)
if not isinstance(symtab, SymbolTableSection):
eprint("No symtab in this file! Exiting.")
dprint(f"{filepath}")
dprint(f"sym_name : defined, sym_type")
FILES[filename] = File(filename, [], [])
for sym in symtab.iter_symbols():
if sym.name and sym.entry["st_info"]["bind"] != "STB_LOCAL":
sym_name = sym.name
sym_shndx = sym.entry["st_shndx"]
defined = sym_shndx != "SHN_UNDEF"
sym_type = sym.entry["st_info"]["type"]
sym_bind = sym.entry["st_info"]["bind"]
# if sym_bind == "STB_WEAK":
# print(f"{sym_name} : {sym.entry}")
dprint(f"{sym_name} : {sym.entry}")
# dprint(f"{sym_name} : {defined}, {sym_type}")
if defined:
if sym_name in SYMBOLS:
if SYMBOLS[sym_name].def_file:
eprint(
f"Error while parsing {filepath}: symbol {sym.name} is already defined by file {SYMBOLS[sym_name].def_file}"
)
else:
SYMBOLS[sym_name].def_file = filename
else:
SYMBOLS[sym_name] = Symbol(filename, [], sym_type)
FILES[filename].def_syms.append(sym_name)
else:
if sym_name in SYMBOLS:
SYMBOLS[sym_name].ref_files.append(filename)
else:
SYMBOLS[sym_name] = Symbol("", [filename], sym_type)
FILES[filename].ref_syms.append(sym_name)
import graphviz
@dataclasses.dataclass
class Graph:
nodes: list[str]
edges: list[(str, str, str)]
GRAPH = Graph([], [])
def make_graph(graph: Graph):
for file in FILES:
graph.nodes.append(file)
for symbol in SYMBOLS:
for file in SYMBOLS[symbol].ref_files:
graph.edges.append(
(file, symbol, SYMBOLS[symbol].def_file, SYMBOLS[symbol].type)
)
def make_viz(graph: Graph):
dot = graphviz.Digraph(format="png")
# # Show unconnected files too
# for n in graph.nodes:
# dot.node(n)
for e in graph.edges:
type = e[3]
if type == "STT_FUNC":
color = "red"
elif type == "STT_OBJECT":
color = "blue"
dot.edge(e[0], e[2], label=e[1], color=color)
dprint(dot)
dot.unflatten(stagger=100, fanout=True)
dot.render(directory="graphs")
def main():
description = ""
epilog = ""
parser = argparse.ArgumentParser(
description=description,
epilog=epilog,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("files", nargs="+", help="elf files to read.")
args = parser.parse_args()
for elf in args.files:
read_file(elf)
# for name in SYMBOLS:
# print(f"{name} : {SYMBOLS[name]}")
# for file in FILES:
# print(f"{file} : \n {FILES[file].def_syms}\n {FILES[file].ref_syms}")
make_graph(GRAPH)
# print(GRAPH)
make_viz(GRAPH)
if __name__ == "__main__":
main()