-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportSymbolsWithDataType.java
197 lines (173 loc) · 6.22 KB
/
ImportSymbolsWithDataType.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
Symbols file format:
Function: int (*hello)() = 0xA0000000;
<T> <ADDRESS> <NAME> <TYPE>
F A0000000 hello int hello();
Data: int *hello = 0xA8000000;
<T> <ADDRESS> <NAME> <TYPE>
D A8000000 hello int
Label
<T> <ADDRESS> <NAME>
L A8000000 hello
*/
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Vector;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.app.util.cparser.C.CParser;
import ghidra.app.util.cparser.C.ParseException;
import ghidra.program.flatapi.FlatProgramAPI;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.DataTypeManager;
import ghidra.program.model.symbol.SourceType;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.FunctionManager;
import ghidra.program.model.listing.FunctionSignature;
import ghidra.program.model.listing.Data;
import ghidra.program.model.listing.Listing;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.SymbolTable;
import ghidra.program.model.symbol.SymbolIterator;
import ghidra.util.exception.CancelledException;
import ghidra.util.exception.DuplicateNameException;
import ghidra.util.exception.InvalidInputException;
import ghidra.app.cmd.function.ApplyFunctionSignatureCmd;
import ghidra.program.model.util.CodeUnitInsertionException;
public class ImportSymbolsWithDataType extends GhidraScript {
private DataTypeManager m_dataTypeManager;
private CParser m_parser;
private class ParsedItem {
Address address;
String name;
String type;
DataType dataType;
};
@Override
protected void run() throws CancelledException, DuplicateNameException, InvalidInputException, CodeUnitInsertionException {
m_dataTypeManager = currentProgram.getDataTypeManager();
m_parser = new CParser(m_dataTypeManager);
FunctionManager functionManager = currentProgram.getFunctionManager();
SymbolTable symbolTable = currentProgram.getSymbolTable();
Listing listing = currentProgram.getListing();
File file = askFile("Please select symbols file, for e.g. symbols.txt", "Apply");
// File file = new File("/tmp/symbols.txt");
Vector<ParsedItem> items = parseSymbolsList(file);
println("Parsed " + items.size() + " symbols!");
println("Importing new symbols...");
int transactionID = getCurrentProgram().startTransaction("Import User Symbols");
for (ParsedItem item : items) {
if (item.type.equals("F")) {
// Clear old symbols
for (Symbol symbol : symbolTable.getSymbols(item.address)) {
if (symbol.getSource() == SourceType.USER_DEFINED) {
symbol.delete();
}
}
// Set label for the address
symbolTable.createLabel(item.address, item.name, SourceType.USER_DEFINED);
// Find or create function
Function func = functionManager.getFunctionAt(item.address);
if (func == null) {
// clearListing(item.address);
// disassemble(item.address);
func = createFunction(item.address, item.name);
}
// Update function signature
ApplyFunctionSignatureCmd cmd = new ApplyFunctionSignatureCmd(item.address, (FunctionSignature) item.dataType, SourceType.USER_DEFINED);
if (!cmd.applyTo(currentProgram, monitor)) {
println("ApplyFunctionSignatureCmd failed on: " + item.name);
}
} else if (item.type.equals("D")) {
Address start = item.address;
Address end = item.address.add(item.dataType.getLength() - 1);
if (!isValidAddress(start) || !isValidAddress(end)) {
println("BAD ADDRESS: " + start.toString());
continue;
}
// Clear old symbols
for (Symbol symbol : symbolTable.getSymbols(item.address)) {
if (symbol.getSource() == SourceType.USER_DEFINED) {
symbol.delete();
}
}
// Set label for the address
symbolTable.createLabel(item.address, item.name, SourceType.USER_DEFINED);
// Clear old data
clearListing(new AddressSet(start, end));
// Create new data
listing.createData(item.address, item.dataType);
} else if (item.type.equals("L")) {
// Set label for the address
symbolTable.createLabel(item.address, item.name, SourceType.USER_DEFINED);
}
}
getCurrentProgram().endTransaction(transactionID, true);
}
protected Vector<ParsedItem> parseSymbolsList(File file) {
Vector<ParsedItem> symbols = new Vector<ParsedItem>();
try {
FileInputStream stream = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.trim().split("\t");
ParsedItem symbol = new ParsedItem();
long addr = Long.parseLong(tokens[1], 16);
symbol.address = currentProgram.getAddressFactory().getDefaultAddressSpace().getAddress(addr);
symbol.name = tokens[2];
symbol.type = tokens[0];
if (tokens[0].equals("F")) { // Function
String code = tokens[3] + ";";
DataType functionSignature = parseDataType(code);
if (functionSignature == null) {
println("BAD: " + code);
} else {
symbol.dataType = functionSignature;
symbols.add(symbol);
}
} else if (tokens[0].equals("D")) { // DATA
String code = tokens[3] + " varName;";
DataType dataType = parseDataType(code);
if (dataType == null) {
println("BAD: " + code);
} else {
symbol.dataType = dataType;
symbols.add(symbol);
}
} else if (tokens[0].equals("L")) { // LABEL
symbols.add(symbol);
} else {
println("ERROR: invalid entry type: " + tokens[0]);
}
}
br.close();
stream.close();
} catch (IOException e) {
println("Can't open symbols: " + file.toString());
}
return symbols;
}
protected DataType parseDataType(String code) {
DataType type = null;
try {
type = m_parser.parse(code);
} catch (ParseException e) {
// println("Can't parse `" + code + "`: " + e.toString());
}
return type;
}
private boolean isValidAddress(Address address) {
MemoryBlock block = currentProgram.getMemory().getBlock(address);
if (block == null) {
return false;
}
return block.contains(address);
}
};