-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumpPcode1011.java
180 lines (140 loc) · 5.01 KB
/
DumpPcode1011.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
//
//@author
//@category
//@keybinding
//@menupath
//@toolbar
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
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.app.services.GraphDisplayBroker;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.pcode.HighFunction;
import ghidra.program.model.pcode.PcodeBlock;
import ghidra.program.model.pcode.PcodeBlockBasic;
import ghidra.program.model.pcode.PcodeOp;
import ghidra.service.graph.*;
import ghidra.program.model.listing.Function;
import java.util.*;
import ghidra.app.decompiler.*;
import ghidra.program.model.address.*;
import ghidra.program.model.lang.Register;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
import ghidra.program.model.pcode.*;
import ghidra.service.graph.*;
public class DumpPcode1011 extends GhidraScript {
private int nextEdgeID = 1;
private HighFunction high;
private Function func;
private Map<String, String> avMap = new HashMap();
private File f;
private PrintWriter ia;
@Override
protected void run() throws Exception {
try
{
println("haha1");
f = new File("C:\\Users\\chai.peng\\Desktop\\pcode.bin");
ia = new PrintWriter(f);
if(f.exists())
{
f.delete();
}
f.createNewFile();
buildAST();
generateGraph();
//generateAstGraph();
ia.close();
}catch(Exception e)
{
println(e.getMessage());
e.printStackTrace();
}
}
private void generateGraph() {
java.util.ArrayList<PcodeBlockBasic> listPBB = high.getBasicBlocks();
for(int i = 0; i < listPBB.size(); i++)
{
printBasicBlock(listPBB.get(i));
}
}
protected Iterator<PcodeOpAST> getPcodeOpIterator() {
Iterator<PcodeOpAST> opiter = high.getPcodeOps();
return opiter;
}
private void generateAstGraph() {
Map<String, String> addrMap = new HashMap();
Iterator<PcodeOpAST> opiter = getPcodeOpIterator();
while (opiter.hasNext()) {
PcodeOpAST op = opiter.next();
String beginStr = String.valueOf(op.getParent().getStart().getOffset());
if(!addrMap.containsKey(beginStr))
{
ia.println(new String().format("%016x~%016x:", op.getParent().getStart().getOffset(), op.getParent().getStop().getOffset()));
addrMap.put(beginStr, beginStr);
}
ia.println(op.toString());
}
}
private void buildAST() throws DecompileException {
DecompileOptions options = new DecompileOptions();
DecompInterface ifc = new DecompInterface();
ifc.setOptions(options);
if (!ifc.openProgram(this.currentProgram)) {
throw new DecompileException("Decompiler", "Unable to initialize: " + ifc.getLastMessage());
}
//ifc.setSimplificationStyle("normal");
func = this.getFunctionContaining(this.currentAddress);
if (func == null) {
throw new DecompileException( "GraphAST Error", "No Function at current location");
}
DecompileResults res = ifc.decompileFunction(func, 30, null);
high = res.getHighFunction();
}
private void printBasicBlock(PcodeBlockBasic pbb)
{
//println("haha2");
StringBuilder sb = new StringBuilder();
ghidra.program.model.address.Address beginAddress = pbb.getStart();
String beginStr = String.valueOf(beginAddress.getOffset());
//if(avMap.containsKey(beginStr) == false)
//{
sb.append(new String().format("%08x~%08x\n", beginAddress.getOffset(), pbb.getStop().getOffset()));
sb.append("in:");
java.util.Iterator<PcodeOp> itPcode = pbb.getIterator();
//println("haha3");
for (int i = 0; i < pbb.getInSize() ; i++ ) {
long inOffset = pbb.getIn(i).getStart().getOffset();
sb.append(new String().format("%08x,", inOffset));
}
sb.append("\n");
//println("haha4");
while(itPcode.hasNext()) {
PcodeOp po = itPcode.next();
sb.append(po.getSeqnum().toString()+":"+po.toString() + "\n");
}
if(pbb.getOutSize() >= 2)
{
sb.append(new String().format("true out:%08x false out:%08x\n", pbb.getTrueOut().getStart().getOffset(), pbb.getFalseOut().getStart().getOffset()));
}
else if(pbb.getOutSize() == 1)
{
//println("haha5");
sb.append(new String().format("out:%08x\n", pbb.getOut(0).getStart().getOffset()));
}
avMap.put(beginStr, beginStr);
ia.println(sb.toString());
//println(sb.toString());
//return;
//}
return;
}
}