forked from yegord/snowman
-
Notifications
You must be signed in to change notification settings - Fork 15
/
IdaFrontend.cpp
262 lines (215 loc) · 8.38 KB
/
IdaFrontend.cpp
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
/* The file is part of Snowman decompiler. */
/* See doc/licenses.asciidoc for the licensing information. */
//
// SmartDec decompiler - SmartDec is a native code to C/C++ decompiler
// Copyright (C) 2015 Alexander Chernov, Katerina Troshina, Yegor Derevenets,
// Alexander Fokin, Sergey Levin, Leonid Tsvetkov
//
// This file is part of SmartDec decompiler.
//
// SmartDec decompiler 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 3 of the License, or
// (at your option) any later version.
//
// SmartDec decompiler 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 SmartDec decompiler. If not, see <http://www.gnu.org/licenses/>.
//
#include "IdaFrontend.h"
#include "IdaWorkaroundStart.h"
#include <ida.hpp>
#include <segment.hpp>
#include <bytes.hpp>
#include <ua.hpp>
#include <funcs.hpp> /* For get_func_name. */
#include <demangle.hpp>
#include <idp.hpp> /* Required by intel.hpp. */
#include <intel.hpp>
#include <nalt.hpp> /* For import_node. */
#include "IdaWorkaroundEnd.h"
#include <nc/common/CheckedCast.h>
#include <nc/common/make_unique.h>
#include <nc/core/image/Image.h>
#include <nc/core/image/Section.h>
#include "IdaByteSource.h"
namespace nc { namespace ida {
bool IdaFrontend::jumpToAddress(ByteAddr address) {
return ::jumpto(checked_cast<ea_t>(address));
}
QString IdaFrontend::functionName(ByteAddr address) {
char buffer[MAXSTR];
if(::get_func_name(checked_cast<ea_t>(address), buffer, sizeof(buffer)) == NULL)
return QString();
return QLatin1String(buffer);
}
QString IdaFrontend::addressName(ByteAddr address) {
char buffer[MAXSTR];
if(::get_true_name(BADADDR, checked_cast<ea_t>(address), buffer, sizeof(buffer)) == NULL)
return QString();
return QLatin1String(buffer);
}
std::vector<std::pair<ByteAddr, QString> > IdaFrontend::importedFunctions() {
std::vector<std::pair<ByteAddr, QString> > result;
for(int i = 0, n = import_node.altval(-1); i < n; i++) {
netnode module(import_node.altval(i));
for(ea_t address = module.sup1st(); address != BADNODE; address = module.supnxt(address)) {
char functionName[MAXSTR];
ssize_t size = module.supval(address, functionName, sizeof(functionName));
if(size != -1)
result.push_back(std::make_pair(address, QLatin1String(functionName)));
}
}
return result;
}
QString IdaFrontend::demangle(const QString &mangled) {
QString preprocessed = mangled;
if(preprocessed.startsWith("j_?"))
preprocessed = preprocessed.mid(2); /* Handle thunks. */
char buffer[MAXSTR];
if(::demangle(buffer, sizeof(buffer), preprocessed.toLatin1().data(), 0) > 0)
return QLatin1String(buffer);
return preprocessed;
}
std::vector<ByteAddr> IdaFrontend::functionStarts() {
std::vector<ByteAddr> result;
for(std::size_t i = 0, n = get_func_qty(); i < n; i++) {
func_t *func = getn_func(i);
if(func != NULL)
result.push_back(func->startEA);
}
return result;
}
void IdaFrontend::createSections(core::image::Image *image) {
for (int i = 0; i < get_segm_qty(); i++) {
segment_t *idaSegment = getnseg(i);
assert(idaSegment != NULL);
char segName[MAXSTR];
ssize_t segNameSize = get_segm_name(idaSegment, segName, sizeof(segName) - 1);
if(segNameSize < 0) {
segName[0] = '\0';
} else if(segNameSize > 0 && segName[0] == '_') {
segName[0] = '.';
}
auto section = std::make_unique<core::image::Section>(
segName,
checked_cast<ByteAddr>(idaSegment->startEA),
checked_cast<ByteSize>(idaSegment->size())
);
section->setReadable(idaSegment->perm & SEGPERM_READ);
section->setWritable(idaSegment->perm & SEGPERM_WRITE);
section->setExecutable(idaSegment->perm & SEGPERM_EXEC);
section->setCode(idaSegment->type == SEG_CODE);
section->setData(idaSegment->type == SEG_DATA);
section->setBss(idaSegment->type == SEG_BSS);
section->setAllocated(section->isCode() || section->isData() || section->isBss());
section->setExternalByteSource(std::make_unique<IdaByteSource>());
image->addSection(std::move(section));
}
}
ByteOrder IdaFrontend::byteOrder() {
return inf.mf ? ByteOrder::BigEndian : ByteOrder::LittleEndian;
}
QString IdaFrontend::architecture() {
reg_info_t regsize;
if (inf.procName == QLatin1String("ARM")) {
return QLatin1String(byteOrder() == ByteOrder::LittleEndian ? "arm-le" : "arm-be");
} else if (inf.procName == QLatin1String("ARMB")) {
return QLatin1String("arm-be");
} else if (inf.procName == QLatin1String("psp")) {
return QLatin1String("allegrex");
} else if (inf.procName == QLatin1String("mipsl")) {
if(parse_reg_name("$zero", ®size)){
switch (regsize.size){
case 4: return QLatin1String("mips-le");
case 8: return QLatin1String("mips64-le");
}
} else {
return QLatin1String("mips-le");
}
} else if (inf.procName == QLatin1String("mipsb")) {
if(parse_reg_name("$zero", ®size)){
switch (regsize.size){
case 4: return QLatin1String("mips-be");
case 8: return QLatin1String("mips64-be");
}
} else {
return QLatin1String("mips-be");
}
} else {
/* Assume x86 by default. */
if (segment_t *segment = get_segm_by_name(".text")) {
switch (segment->bitness) {
case 0: return QLatin1String("8086");
case 1: return QLatin1String("i386");
case 2: return QLatin1String("x86-64");
}
}
return QLatin1String("i386");
}
}
core::image::Platform::OperatingSystem IdaFrontend::operatingSystem() {
if (inf.filetype == f_WIN || inf.filetype == f_PE) {
return core::image::Platform::Windows;
}
return core::image::Platform::UnknownOS;
}
std::vector<AddressRange> IdaFrontend::functionAddresses(ByteAddr address) {
std::vector<AddressRange> result;
ea_t func_ea = checked_cast<ea_t>(address);
func_t *function = ::get_func(func_ea);
if (!function) {
return result;
}
auto startAddr = checked_cast<ByteAddr>(function->startEA);
auto endAddr = checked_cast<ByteAddr>(function->endEA);
result.push_back(AddressRange(startAddr, endAddr));
for (int i = 0; i < function->tailqty; ++i) {
auto chunkStartAddr = checked_cast<ByteAddr>(function->tails[i].startEA);
auto chunkEndAddr = checked_cast<ByteAddr>(function->tails[i].endEA);
result.push_back(AddressRange(chunkStartAddr, chunkEndAddr));
}
return result;
}
ByteAddr IdaFrontend::screenAddress() {
return checked_cast<ByteAddr>(get_screen_ea());
}
namespace {
/* Stdcall -> cdecl adaptor. */
bool idaapi callbackThunk(void *realCallback) {
return reinterpret_cast<bool (*)()>(realCallback)();
}
} // anonymous namespace
void IdaFrontend::addMenuItem(const QString &menuItem, const QString &name, const QString &hotkey, bool (*callback)()) {
::add_menu_item(
menuItem.toLocal8Bit().constData(),
name.toLocal8Bit().constData(),
hotkey.toLocal8Bit().constData(),
SETMENU_APP,
callbackThunk,
reinterpret_cast<void *>(callback));
}
void IdaFrontend::deleteMenuItem(const QString &menuItem) {
::del_menu_item(menuItem.toLocal8Bit().constData());
}
void IdaFrontend::print(const QString &message) {
::msg(message.toLocal8Bit());
}
QWidget *IdaFrontend::createWidget(const QString &caption) {
HWND hwnd;
TForm *form = create_tform(caption.toLocal8Bit().constData(), &hwnd);
open_tform(form, /*FORM_MDI |*/ FORM_TAB | FORM_MENU | FORM_QWIDGET);
return reinterpret_cast<QWidget *>(form);
}
void IdaFrontend::activateWidget(QWidget *widget) {
::switchto_tform(reinterpret_cast<TForm *>(widget), true);
}
void IdaFrontend::closeWidget(QWidget *widget) {
::close_tform(reinterpret_cast<TForm *>(widget), 0);
}
}} // namespace nc::ida
/* vim:set et sts=4 sw=4: */