-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPCodeDump.java
217 lines (181 loc) · 6.8 KB
/
PCodeDump.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//Dumps the pcode into a txt file.
//@category PCode
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.io.InputStream;
import ghidra.util.Msg;
import java.util.ArrayList;
import java.lang.Byte;
import ghidra.program.model.address.*;
import ghidra.program.model.pcode.*;
import ghidra.program.model.lang.Register;
import ghidra.program.model.mem.*;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileException;
import ghidra.app.decompiler.DecompileOptions;
import ghidra.app.decompiler.DecompileResults;
import ghidra.app.script.GhidraScript;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.listing.Function;
public class PCodeDump extends GhidraScript {
// P-Code Dump Settings
// Print Registers using string identifiers
boolean registersAsString = false;
boolean normalizeDecompilation = false;
boolean fixCBranch = true;
private Function func;
protected HighFunction high;
@Override
protected void run() throws Exception {
PluginTool tool = state.getTool();
if (tool == null) {
println("Script is not running in GUI");
}
File f = askFile("Enter file name, including extension for P-Code dump", "OK");
try {
println("going to dump into file " + f);
if(f.createNewFile()){ println("File created!");} else {println("File exists already, contents will be overwritten.");}
FileWriter file = new FileWriter(f,false);
DecompileOptions options = new DecompileOptions();
DecompInterface ifc = new DecompInterface();
ifc.setOptions(options);
func = getFirstFunction();
while(true){
if (monitor.isCancelled()) { break;}
if (func == null) {break;}
if (!ifc.openProgram(this.currentProgram)) {
throw new DecompileException("Decompiler", "Unable to initialize: " + ifc.getLastMessage());
}
if (normalizeDecompilation) {ifc.setSimplificationStyle("normalize");}
DecompileResults res = ifc.decompileFunction(func, 30, null);
high = res.getHighFunction();
// In some cases, Ghidra is unable to perform disassembly or decompilation, and high will be null
// In those cases, we skip this function and move on to the next
if (high == null) {func = getFunctionAfter(func); continue;}
// test if function is external
boolean ext = func.isThunk();
if (ext) {
// If so, add "EXT_" to function name
file.write("EXT_");
}
// Print function header information
file.write(func.getName() + "\n");
//iterate over the basic blocks in the function
ArrayList<PcodeBlockBasic> blocks = high.getBasicBlocks();
// test if function is external
if (ext) {
file.write(blocks.get(0).getStart().toString() + "\n");
file.write(" --- EXTCALL " + func.getName()+ "\n");
}
else{
for (int i = 0; i < blocks.size(); i++) {
Address blockAddr = blocks.get(i).getStart();
Iterator<PcodeOp> block = blocks.get(i).getIterator();
if(block.hasNext()){file.write(blockAddr.toString() + "\n");}
while(block.hasNext()){
PcodeOp op = block.next();
// Printing instruction
// Determine if it is an assignment
if (op.isAssignment()){
// if so, print output varnode
file.write(printVarnode(op.getOutput()));
}
else {
// if not, print " --- "
file.write(" --- ");
}
// Then, print opcode
int opcode = op.getOpcode();
file.write(" " + op.getMnemonic() + " ");
// Then, print all inputs, if any
Varnode[] inputs = op.getInputs();
// begin loop
for (int j = 0; j < inputs.length; j++) {
// print varnode
//check if we are dealing with a conditional branch
if (opcode == 5 && j==0 && fixCBranch) {
// if so, we actually need the false out address
file.write(printAddressAsVarnode(blocks.get(i).getTrueOut().getStart())+" , ");//"(ram, 0x" + blocks.get(i).getTrueOut().getStart().toString() + ", 1) , ");
file.write(printAddressAsVarnode(blocks.get(i).getFalseOut().getStart()));
}
else if (opcode == 60) {
// we are dealing with a multiequal. Try to get all addresses that point to this block
file.write(printVarnode(op.getInput(j)) + " , "+ op.getInput(j).getPCAddress().toString());
}
else {file.write(printVarnode(op.getInput(j)));}
// if next, print " , "
if (inputs.length > (j+1)) {file.write(" , ");}
}
file.write("\n");
// Done!
}
// At this point, we wanna check if we need to insert an explicit fall through.
// First, we determine if we will perform fall though (last instruction is not CBRANCH, BRANCH or BRANCHIND)
Iterator<PcodeOp> instrs = blocks.get(i).getIterator();
int lastInstr = -1;
while (instrs.hasNext()) lastInstr = instrs.next().getOpcode();
if (lastInstr != 4 && lastInstr != 6 && lastInstr != 5) {
// This includes safety checks: Is there a known exit path? Is there only one?
if (blocks.get(i).getOutSize() == 1) {
// If so, what address does the Ghidra API think we will jump to?
Address apiNext = blocks.get(i).getOut(0).getStart();
file.write(" --- BRANCH (ram, 0x" + apiNext + ", 1)\n");
}
}
}
}
func = getFunctionAfter(func);
}
// keyword indicating that memory section starts
file.write("MEMORY\n");
Iterator<AddressRange> mem1 = currentProgram.getMemory().getAllInitializedAddressSet().iterator();
while(mem1.hasNext()){
Iterator<Address> range = mem1.next().iterator();
while (range.hasNext()){
Address a = range.next();
if(a.getAddressSpace().getName().equals("ram")){
file.write(a.toString() + " ");
byte b = currentProgram.getMemory().getByte(a);
file.write(Byte.toUnsignedInt(b) + "\n");
}
}
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected String printVarnode(Varnode vn) {
String result = "";
if(vn.isRegister() && registersAsString){
result += "(register, ";
Register reg = func.getProgram().getRegister(vn.getAddress(), vn.getSize());
if (reg != null) { // do we need this condition?
result += reg.getName();
result += ", ";
result += vn.getSize();
result += ")";
}
} else {result += vn.toString();}
return result;
}
protected String printAddressAsVarnode(Address a) {
String result = "(";
String pA = a.toString();
//checks to see if we are dealing with HARVARD architecture binary
if((pA.length() > 4) && (pA.substring(0,4).equals("CODE"))){
result += "CODE, 0x";
result += pA.substring(5,pA.length());
} else { result += "ram, 0x" + pA;}
result += ", 1)";
return result;
}
}