Skip to content

Commit e0eb52f

Browse files
KooShnooem-eight
authored andcommitted
Add script to apply mapped symbol from objdiff
1 parent 57f4538 commit e0eb52f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tools/apply_symbol_mappings.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
copies mapped symbols from objdiff.json to symbols.txt
3+
4+
objdiff allows one to map symbols from the programmer's decompiled binary to the original target binary.
5+
this script will find those mapped symbols, and write the programmer's updated names to `symbols.txt`.
6+
7+
example: say you have changed the signature/name of a function from foo(short) -> bar(long).
8+
now, the symbol from your new object is `bar__Fl`, but symbols.txt still says `foo__Fs`.
9+
objdiff allows you to map `bar__Fl` in your new object to `foo__Fs` in the target object.
10+
this script will automatically replace `foo__Fs` with `bar__Fl` in `symbols.txt`.
11+
"""
12+
13+
import json
14+
15+
with open("./objdiff.json") as f:
16+
objdiff_config = json.load(f)
17+
18+
units = objdiff_config["units"]
19+
20+
symbol_mappings = {}
21+
for unit in units:
22+
symbol_mapping = unit.get("symbol_mappings")
23+
if symbol_mapping is None:
24+
continue
25+
symbol_mappings.update(symbol_mapping)
26+
del unit['symbol_mappings']
27+
28+
29+
def process_symbols_txt(symbols_txt_path):
30+
with open(symbols_txt_path) as f:
31+
symbols = f.readlines()
32+
33+
34+
35+
for i, line in enumerate(symbols):
36+
tokens = line.split()
37+
old_symbol = tokens[0]
38+
39+
new_symbol = symbol_mappings.get(old_symbol)
40+
41+
if new_symbol is None:
42+
continue
43+
44+
tokens[0] = new_symbol
45+
symbols[i] = " ".join(tokens) + "\n"
46+
47+
with open(symbols_txt_path, "w") as f:
48+
f.writelines(symbols)
49+
50+
process_symbols_txt("./config/RMCP01/module/symbols.txt")
51+
process_symbols_txt("./config/RMCP01/symbols.txt")
52+
53+
54+
55+
with open("./objdiff.json", "w") as f:
56+
json.dump(objdiff_config, f)

0 commit comments

Comments
 (0)