forked from UsernameFodder/pmdsky-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_symbols_json.py
53 lines (46 loc) · 1.65 KB
/
import_symbols_json.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
# Imports symbols from a JSON file
# @author UsernameFodder
# @category Data
import json
import ghidra.program.model.symbol.SourceType as SourceType
COMMENT_TAG = "=== imported description ===\n"
functionManager = currentProgram.getFunctionManager()
jythonFile = askFile("Select symbol JSON file", "Import")
with open(jythonFile.absolutePath, "r") as f:
symbols = json.load(f)
for s in symbols:
stype = s["type"]
name = s["name"]
address = s["address"]
if type(address) != int:
address = int(address, 0)
address = toAddr(address)
description = s.get("description")
if stype == "function":
func = functionManager.getFunctionAt(address)
if func is None:
func = createFunction(address, name)
print("Created function {} at address {}".format(name, address))
else:
old_name = func.getName()
func.setName(name, SourceType.USER_DEFINED)
print(
"Renamed function {} to {} at address {}".format(
old_name, name, address
)
)
else:
print("Created label {} at address {}".format(name, address))
createLabel(address, name, False)
if description:
comment = getPlateComment(address)
if not comment:
comment = COMMENT_TAG + description
else:
commentParts = comment.split(COMMENT_TAG)
if len(commentParts) == 1:
commentParts[0] += "\n\n"
commentParts.append("")
commentParts[1] = description
comment = COMMENT_TAG.join(commentParts)
setPlateComment(address, comment)