forked from cseagle/blc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.cc
388 lines (320 loc) · 10.8 KB
/
run.cc
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
/*
Source for the blc IdaPro plugin
Copyright (c) 2019 Chris Eagle
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <stdint.h>
#include <stdlib.h>
using std::iostream;
using std::ifstream;
using std::ostringstream;
using std::istringstream;
using std::stringstream;
using std::map;
#include "libdecomp.hh"
#include "capability.hh"
#include "sleigh_arch.hh"
#include "xml_tree.hh"
#include "plugin.hh"
#include "ida_minimal.hh"
#include "ida_arch.hh"
#include "ast.hh"
//#define DEBUG 1
using ghidra::List;
using ghidra::TrackedSet;
using ghidra::AddrSpace;
using ghidra::TrackedContext;
using ghidra::DecoderError;
using ghidra::LowlevelError;
using ghidra::PcodeOpTree;
using ghidra::Funcdata;
using ghidra::SeqNum;
using ghidra::PcodeOp;
stringstream *err_stream;
static string sleigh_id;
ida_arch *arch; // in lieu of Architecture *IfaceDecompData::conf
void escape_value(const string &value, string &res) {
const char *content = value.c_str();
while (*content) {
if (*content == '&') {
res += "&";
}
else if (*content == '>') {
res += ">";
}
else if (*content == '<') {
res += "<";
}
else if (*content == '"') {
res += """;
}
else if (*content == '\'') {
res += "'";
}
else {
res += *content;
}
content++;
}
}
void dump_el(const Element *el, int indent, string &res) {
const List &children = el->getChildren();
int clen = el->getContent().length();
int nattr = el->getNumAttributes();
res.append(indent, ' ');
res.push_back('<');
res += el->getName();
for (int i = 0; i < nattr; i++) {
res.push_back(' ');
res += el->getAttributeName(i);
res += "=\"";
escape_value(el->getAttributeValue(i).c_str(), res);
res += "\"";
}
int nchildren = 0;
for (List::const_iterator it = children.begin(); it != children.end(); it++) {
const Element *child = *it;
nchildren++;
if (nchildren == 1) {
res += ">\n";
}
dump_el(child, indent + 3, res);
}
if (nchildren) {
if (el->getContent().length() > 0) {
res += "NON-ZERO content in element with children\n";
}
res.append(indent, ' ');
res += "</";
res += el->getName();
res += ">\n";
}
else {
if (clen) {
res += ">";
escape_value(el->getContent().c_str(), res);
res += "</";
res += el->getName();
res += ">\n";
}
else {
res += "/>\n";
}
}
}
static const string empty_string("");
const string &getAttributeValue(const Element *el, const char *attr) {
int nattr = el->getNumAttributes();
for (int i = 0; i < nattr; i++) {
if (el->getAttributeName(i) == attr) {
return el->getAttributeValue(i);
}
}
return empty_string;
}
void check_err_stream() {
if (err_stream->tellp()) {
msg("%s\n", err_stream->str().c_str());
err_stream->str("");
}
}
TrackedSet &get_tracked_set(uint64_t start, uint64_t end) {
//need to add a TrackedSet to arch->context(which is a ContextInternal for us)->trackbase
//if we are tracking any registers. In particular, if any registers are fixed on entry
//we should add them to the TrackedSet for ea. This is probabaly more useful for some archs
//than others.
AddrSpace *as = arch->getSpaceByName("ram");
Address func_begin(as, start);
Address func_end(as, end);
return arch->context->createSet(func_begin, func_end);
}
void add_tracked_reg(TrackedSet ®s, uint64_t offset, uint64_t value, uint32_t size) {
regs.push_back(TrackedContext());
TrackedContext ® = regs.back();
reg.loc.space = arch->getSpaceByName("register");
reg.loc.offset = offset;
reg.loc.size = size;
reg.val = value;
}
void mips_setup(uint64_t start, uint64_t end) {
TrackedSet ®s = get_tracked_set(start, end);
//this is very n64 specific
// this is $t9 - need to do this better
add_tracked_reg(regs, 0xc8, start, 8);
add_tracked_reg(regs, 0xcc, start & 0xffffffffll, 4);
add_tracked_reg(regs, 0xc8, start >> 32, 4);
}
bool ghidra_init(void) {
ghidra::startDecompilerLibrary(ghidra_dir.c_str());
err_stream = new stringstream();
// IfaceCapability::registerAllCommands(term); // Register commands for decompiler and all modules
string filename;
get_input_file_path(filename);
if (!get_saved_sleigh_id(sleigh_id)) {
if (!get_sleigh_id(sleigh_id)) {
return false;
}
}
//implement most of IfcLoadFile::execute here since file is
//already loaded in IDA
arch = new ida_arch(filename, sleigh_id, err_stream);
DocumentStorage store; // temporary storage for xml docs
string errmsg;
bool iserror = false;
try {
arch->init(store);
//at this point we have arch->context (a ContextInternal) available
// we can do things like:
// context->setVariableDefault("addrsize",1); // Address size is 32-bits
// context->setVariableDefault("opsize",1); // Operand size is 32-bits
// that make sense for our architecture
} catch(DecoderError &err) {
errmsg = err.explain;
iserror = true;
} catch(LowlevelError &err) {
errmsg = err.explain;
iserror = true;
}
if (iserror) {
msg("%s\n", errmsg.c_str());
msg("Could not create architecture\n");
delete arch;
arch = NULL;
return false;
}
check_err_stream();
set_saved_sleigh_id(sleigh_id);
msg("Ghidra architecture successfully created\n");
return true;
}
void ghidra_term(void) {
ghidra::shutdownDecompilerLibrary();
// GhidraCapability::shutDown();
delete err_stream;
err_stream = NULL;
}
void do_pcode(const Funcdata *fd) {
//typedef map<SeqNum,PcodeOp *> PcodeOpTree
/// \brief Start of all (alive) PcodeOp objects sorted by sequence number
PcodeOpTree::const_iterator iter;
int i = 0;
for (iter = fd->beginOpAll(); iter != fd->endOpAll(); iter++) {
i++;
const SeqNum &sn = iter->first;
const PcodeOp *pcode = iter->second;
ostringstream os;
pcode->printRaw(os);
#ifdef DEBUG
// msg("%p: %u, (%s / %s): %s\n", (void*)sn.getAddr().getOffset(), sn.getOrder(), pcode->getOpcode()->getName().c_str(), get_opname(pcode->code()), os.str().c_str());
#endif
}
// msg("Found %d PcodeOpTree\n", i);
/*
/// \brief Start of all (alive) PcodeOp objects attached to a specific Address
PcodeOpTree::const_iterator beginOp(const Address &addr) const { return obank.begin(addr); }
/// \brief End of all (alive) PcodeOp objects attached to a specific Address
PcodeOpTree::const_iterator endOp(const Address &addr) const { return obank.end(addr); }
*/
}
// Extract the info that the decompiler needs to instantiate its address space manager
// This also builds the internal register map while it walks the sleigh spec.
// see IfcDecompile::execute
int do_decompile(uint64_t start_ea, uint64_t end_ea, Function **result) {
Scope *global = arch->symboltab->getGlobalScope();
Address addr(arch->getDefaultCodeSpace(), start_ea);
Funcdata *fd = global->findFunction(addr);
*result = NULL;
if (strncmp("ARM", sleigh_id.c_str(), 3) == 0) {
//if ARM check for and set thumb ranges
if (is_thumb_mode(start_ea)) {
arch->context->setVariable("TMode", addr, 1);
}
}
int4 res = -1;
if (fd) {
string xml;
string c_code;
string func_name;
get_func_name(func_name, start_ea);
if (func_name != fd->getName()) {
// Function name has changed since the last decompile
// We need to flush the old symbol name from Ghidra's database to
// force it to request the new name from IDA
global->removeSymbolMappings(fd->getSymbol());
fd = global->findFunction(addr);
}
// msg("Decompiling %s\n", func_name.c_str());
arch->clearAnalysis(fd); // Clear any old analysis
arch_map_t::iterator setup = arch_map.find(get_proc_id());
if (setup != arch_map.end()) {
(*setup->second)(start_ea, end_ea);
}
arch->allacts.getCurrent()->reset(*fd);
res = arch->allacts.getCurrent()->perform(*fd);
if (res < 0) {
ostringstream os;
msg("Break at ");
arch->allacts.getCurrent()->printState(os);
msg("%s\n", os.str().c_str());
}
else {
// msg("Decompilation complete\n");
if (res == 0) {
msg(" (no change)\n");
}
// msg("do_pcode start\n");
do_pcode(fd);
// msg("do_pcode end\n");
stringstream ss;
arch->print->setIndentIncrement(3);
arch->print->setOutputStream(&ss);
//print as C
arch->print->docFunction(fd);
c_code = ss.str();
#ifdef DEBUG
msg("Printing C str\n");
msg("%s\n", c_code.c_str());
msg("Done printing C str\n");
#endif
ss.str("");
arch->print->setMarkup(true);
arch->print->docFunction(fd);
arch->print->setMarkup(false);
string pbuf = ss.str();
const uint8_t* begin = (const uint8_t*)pbuf.c_str();
const uint8_t* end = (const uint8_t*)(pbuf.c_str() + pbuf.size());
//we need to work with raw bytes
vector<uint8_t> packed(begin, end);
XmlElement *root = build_from_packed(packed);
#ifdef DEBUG
dump_tree(root);
FILE* ff = fopen("packed.bin", "wb");
fwrite(pbuf.c_str(), 1, pbuf.size(), ff);
fclose(ff);
#endif
*result = func_from_tree(root, start_ea);
delete root;
}
check_err_stream();
}
else {
#ifdef DEBUG
msg("Error, no Funcdata at 0x%llx\n", (uint64_t)start_ea);
#endif
}
return res;
}