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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package internal;
/**
* Most of the code below was inspired by Ghidra's source code at
* Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/BasicCompilerSpec.java,
* Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/processors/sleigh/SleighLanguageProvider.java
*
* It extracts unaffected, killed by call and return registers for the given CPU architecture and adds them to the json output.
*
* For the identification of the correct .cspec file, the processor name is extracted from Ghidra to find the processor's
* corresponding .ldefs file. In the .ldefs file the language id and and compilerSpec id is used to determine the correct .cspec file.
*
* If the correct .cpsec file was found, it iterates over the XML DOM to extract the above mentioned registers.
*
*/
import ghidra.xml.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.xml.sax.*;
import generic.jar.ResourceFile;
import ghidra.app.plugin.processors.sleigh.SleighException;
import ghidra.framework.Application;
import ghidra.program.model.lang.CompilerSpecID;
import ghidra.program.model.lang.CompilerSpecNotFoundException;
import ghidra.program.model.lang.LanguageID;
import ghidra.program.model.listing.Program;
import ghidra.util.Msg;
public class ParseCspecContent {
private static Exception parseException;
private static LanguageID languageId;
private static CompilerSpecID compilerSpecId;
private static Program program;
/**
*
* @param ghidraProgram
* @param conventions
* @throws FileNotFoundException
*
* Set the important parameters and handle the file extraction and parsing
*/
public static void parseSpecs(Program ghidraProgram, HashMap<String, RegisterConvention> conventions) throws FileNotFoundException {
program = ghidraProgram;
languageId = program.getLanguageID();
compilerSpecId = program.getCompilerSpec().getCompilerSpecID();
ResourceFile ldefFile = getLdefFile();
if(ldefFile != null) {
XmlPullParser ldefParser = null;
try {
ldefParser = parseXmlFile(ldefFile);
}
catch (CompilerSpecNotFoundException e) {
System.out.println(e);
}
String cspecFileName = parseLdefFile(ldefParser);
ResourceFile cspecFile = getCspecFile(cspecFileName);
if(cspecFile != null) {
XmlPullParser cspecParser = null;
try {
cspecParser = parseXmlFile(cspecFile);
}
catch (CompilerSpecNotFoundException e) {
System.out.println(e);
}
parseCspecFile(cspecParser, conventions);
} else {
throw new FileNotFoundException("Could not find .cspec file.");
}
} else {
throw new FileNotFoundException("Could not find .ldef file.");
}
}
/**
*
* @param file: File in Ghidra directory
* @return: XML parser for input file
* @throws CompilerSpecNotFoundException
*
* Handles all of the XML error handling and creates the Parser from the XMLPullParserFactory
*/
public static XmlPullParser parseXmlFile(ResourceFile file) throws CompilerSpecNotFoundException {
ErrorHandler errHandler = new ErrorHandler() {
@Override
public void error(SAXParseException exception) throws SAXException {
parseException = exception;
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
parseException = exception;
}
@Override
public void warning(SAXParseException exception) throws SAXException {
Msg.warn(this, "Warning parsing '" + file + "'", exception);
}
};
try {
XmlPullParser parser = XmlPullParserFactory.create(file, errHandler, false);
return parser;
}
catch (SleighException e) {
parseException = e;
Throwable cause = e.getCause();
if (cause != null) {
if (cause instanceof SAXException || cause instanceof IOException) {
parseException = (Exception) cause;
}
}
}
catch (FileNotFoundException e) {
parseException = e;
}
catch (IOException e) {
parseException = e;
}
catch (SAXException e) {
parseException = e;
}
if (parseException != null) {
throw new CompilerSpecNotFoundException(
languageId,
compilerSpecId,
file.getName(),
parseException
);
}
return null;
}
/**
*
* @return: resource file i.e. .ldef
*
* Searches the Ghidra's directories for .ldef extension and returns the corresponding
* file based on the processor name. In some cases the processor name has to be parsed
* for correct matching.
*/
public static ResourceFile getLdefFile() {
String processorDef = String.format("%s.ldefs", program.getLanguage().getLanguageDescription().getProcessor().toString());
if(processorDef.startsWith("MIPS")) {
processorDef = processorDef.toLowerCase();
}
if(processorDef.startsWith("PowerPC")) {
processorDef = "ppc.ldefs";
}
for(ResourceFile file : Application.findFilesByExtensionInApplication(".ldefs")) {
if(file.getName().equals(processorDef)) {
return file;
}
}
return null;
}
/**
*
* @param parser: parser for .ldef file
* @return: filename of .cspec file
*
* Parses the .cspec filename from the .ldef file by
* matching the language id. e.g. id = ARM:LE:32:v8
* to analyse the correct language.
*/
public static String parseLdefFile(XmlPullParser parser) {
String cspecName = null;
parser.start("language_definitions");
while(parser.peek().isStart()) {
XmlElement languageEnter = parser.peek();
if(languageEnter.getAttribute("id").equals(languageId.getIdAsString())) {
cspecName = getCompilerName(parser);
} else {
discardSubTree(parser);
}
}
parser.end();
return cspecName;
}
/**
*
* @param parser
* @return
*
* Parses the compiler fields of the language and extracts the correct
* compiler using the compilerSpec Id e.g. id = default
*
* <compiler name="default" spec="ARM.cspec" id="default"/>
*/
public static String getCompilerName(XmlPullParser parser) {
String cspec = null;
parser.start("language");
while(parser.peek().isStart()) {
XmlElement langProperty = parser.peek();
if(langProperty.getName().equals("compiler")) {
if(langProperty.getAttribute("id").equals(compilerSpecId.getIdAsString())) {
parser.start();
cspec = langProperty.getAttribute("spec");
parser.end();
} else {
discardSubTree(parser);
}
} else {
discardSubTree(parser);
}
}
parser.end();
return cspec;
}
/**
*
* @param parser: parser for .cspec file
* @param conventions
*
* Searches the .cspec file for default_proto or prototype wrapper
*/
public static void parseCspecFile(XmlPullParser parser, HashMap<String, RegisterConvention> conventions) {
parser.start("compiler_spec");
while(parser.peek().isStart()) {
String field = parser.peek().getName();
if(field.equals("default_proto") || field.equals("prototype")) {
parsePrototype(parser, field, conventions);
} else {
discardSubTree(parser);
}
}
parser.end();
}
/**
*
* @param parser: parser for .cspec file
* @param name: name of the wrapper
* @param conventions
*
* Gets registers based on wrapper name. The default_proto wrapper
* is an additional wrapper around the default prototype. Therefore,
* the function has to go one level deeper.
*/
public static void parsePrototype(XmlPullParser parser, String name, HashMap<String, RegisterConvention> conventions) {
RegisterConvention convention = new RegisterConvention();
if(name.equals("default_proto")) {
parser.start();
getUnaffectedKilledByCallAndOutput(parser, convention);
parser.end();
} else if(name.equals("prototype")) {
getUnaffectedKilledByCallAndOutput(parser, convention);
}
// Using the hashmap this way will simplify the addition of parameter registers which are not parsed here
// as they are calling convention specific
conventions.put(convention.getCconv(), convention);
}
/**
*
* @param parser: parser for .cspec file
* @param convention: convention object for later serialization
*
* Sets the convention's unaffected, killed by call and return registers as well as the calling convention
*/
public static void getUnaffectedKilledByCallAndOutput(XmlPullParser parser, RegisterConvention convention) {
XmlElement protoElement = parser.start();
String cconv = protoElement.getAttribute("name");
convention.setCconv(cconv);
while(parser.peek().isStart()) {
XmlElement registers = parser.peek();
if(registers.getName().equals("unaffected")) {
convention.setUnaffected(getRegisters(parser));
} else if (registers.getName().equals("killedbycall")) {
convention.setKilledByCall(getRegisters(parser));
} else if (registers.getName().equals("output")) {
convention.setReturn(parseOutput(parser));
} else {
discardSubTree(parser);
}
}
parser.end(protoElement);
}
/**
*
* @param parser: parser for .cspec file
* @return: list of return registers
*
* Parses the output and pentry wrapper to access the return register fields
*/
public static ArrayList<String> parseOutput(XmlPullParser parser) {
ArrayList<String> registers = new ArrayList<String>();
parser.start("output");
while(parser.peek().isStart()) {
parser.start("pentry");
XmlElement entry = parser.peek();
if(entry.getName().equals("register")) {
parser.start("register");
registers.add(entry.getAttribute("name"));
parser.end();
} else {
discardSubTree(parser);
}
parser.end();
}
parser.end();
return registers;
}
/**
*
* @param parser: parser for .cspec file
* @return: either killed by call or unaffected registers
*
* Parses killed by call or unaffected registers and ingnores varnode types
*/
public static ArrayList<String> getRegisters(XmlPullParser parser) {
ArrayList<String> registers = new ArrayList<String>();
parser.start();
while(parser.peek().isStart()) {
XmlElement type = parser.peek();
if(type.getName().equals("register")) {
parser.start();
registers.add(type.getAttribute("name"));
parser.end();
} else if(type.getName().equals("varnode")) {
discardSubTree(parser);
}
}
parser.end();
return registers;
}
/**
*
* @param filename: .cspec filename
* @return: .cspec file
*
* Return the .cspec file for a given filename
*/
public static ResourceFile getCspecFile(String filename) {
for(ResourceFile file : Application.findFilesByExtensionInApplication(".cspec")) {
if(file.getName().equals(filename)) {
return file;
}
}
return null;
}
/**
*
* @param parser: xml parser
*
* discards XML subtrees if they do not contain necessary information.
* Simply used as a shortcut.
*/
public static void discardSubTree(XmlPullParser parser) {
XmlElement el = parser.start();
parser.discardSubTree(el);
}
}