-
Notifications
You must be signed in to change notification settings - Fork 2
/
remap_symbols.py
72 lines (53 loc) · 2.61 KB
/
remap_symbols.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
import argparse
import yaml
import sqlite3
def read_yaml(yaml_file: str) -> dict:
yaml_data = yaml.load(open(yaml_file, 'r'),
Loader=yaml.FullLoader)
return yaml_data
def remap_symbols(database_path, yaml_map: dict):
"""
Remaps symbols in the database. This is very useful for situations where juicer does not
accurately capture the intent of the source code. An example is a typedef'd voi* type by a macro.
Since the "void*" type does not exist in the database(as of DWARF Version4), one may remap the macro to the word size
of the machine which may represented by uint16, uint32 or uint64 depending on the architecture of course.
:param database_path: The path to the sqlite database.
:param yaml_map:A dictionary of the form {old_symbol1:new_symbol1, old_symbol2:new_symbol2} which has all
of the remaps.
:return:
NOTE: This function commits the database transactions; so there is no need for the caller to commit anything to the
database.
"""
db_handle = sqlite3.connect(database_path)
db_cursor = db_handle.cursor()
for old_symbol, new_symbol in yaml_map.items():
old_symbol_id = db_cursor.execute('SELECT id FROM symbols where name=?',
(old_symbol,)).fetchone()[0]
new_symbol_id = db_cursor.execute('SELECT id FROM symbols where name=?',
(new_symbol,)).fetchone()[0]
field_ids = db_cursor.execute('SELECT id FROM fields where type=?',
(old_symbol_id,)).fetchall()
for field_id in field_ids:
db_handle.execute("UPDATE fields SET type = ? WHERE id = ?",
(new_symbol_id, field_id[0]))
db_handle.commit()
def parse_cli() -> argparse.Namespace:
"""
Parses cli arguments.
:return: The namespace that has all of the arguments that have been parsed.
"""
parser = argparse.ArgumentParser(description='Takes in path to sqlite database.')
parser.add_argument('--database', type=str, required=True, help='The path to the SQLITE database..')
parser.add_argument('--yaml_path', type=str, required=True,
help='The yaml config file that has the symbol remappings.')
return parser.parse_args()
def main():
args = parse_cli()
yaml = read_yaml(args.yaml_path)
if 'remaps' in yaml:
yaml_remaps = read_yaml(args.yaml_path)['remaps']
else:
yaml_remaps = read_yaml(args.yaml_path)['type_remaps']
remap_symbols(args.database, yaml_remaps)
if __name__ == '__main__':
main()