-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathconfig_generator.cpp
684 lines (609 loc) · 14.2 KB
/
config_generator.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/**
* @file idaplugin/config_generator.cpp
* @brief Module contains classes/methods dealing with information export
* from IDA Pro to Retargetable Decompiler config database.
* @copyright (c) 2017 Avast Software, licensed under the MIT license
*/
#include <algorithm>
#include <iostream>
#include <sstream>
#include "config_generator.h"
namespace idaplugin {
/**
* Initialize config with empty content.
*/
ConfigGenerator::ConfigGenerator(RdGlobalInfo& gi) :
decompInfo(gi),
config(gi.configDB)
{
config = retdec::config::Config();
}
/**
* Generate decompiler config file.
* @return Name of generated config file.
*/
std::string ConfigGenerator::generate()
{
DBG_MSG("Configuration Generator:\n");
structIdSet.clear();
generateHeader();
generateFunctions();
generateSegmentsAndGlobals();
return config.generateJsonFile();
}
/**
* Generate general information about analysed file.
*/
void ConfigGenerator::generateHeader()
{
config.setInputFile(decompInfo.workIdb);
#ifdef __X64__
config.setEntryPoint(inf_start_ea);
#else
config.setEntryPoint(inf.beginEA);
#endif
config.setIsIda(true);
}
/**
* Convert IDA's object location (address, register, etc.) into
* retdec-config representation.
*
* @param loc Location.
* @param locType Location type.
* @return False if ok, true otherwise.
*/
retdec::config::Storage ConfigGenerator::generateObjectLocation(
const argloc_t &loc,
const tinfo_t &locType)
{
if (loc.is_reg()) // is_reg1() || is_reg2()
{
qstring buff;
if (get_reg_name(&buff, loc.reg1(), locType.get_size()) <= 0)
{
return retdec::config::Storage::undefined();
}
return retdec::config::Storage::inRegister(buff.c_str());
}
else if (loc.is_stkoff())
{
return retdec::config::Storage::onStack(loc.stkoff());
}
else if (loc.is_ea())
{
return retdec::config::Storage::inMemory(loc.get_ea());
}
else if (loc.is_rrel())
{
return retdec::config::Storage::undefined();
}
else if (loc.is_scattered())
{
return retdec::config::Storage::undefined();
}
else if (loc.is_fragmented())
{
return retdec::config::Storage::undefined();
}
else if (loc.is_custom())
{
return retdec::config::Storage::undefined();
}
else if (loc.is_badloc())
{
return retdec::config::Storage::undefined();
}
else
{
return retdec::config::Storage::undefined();
}
}
/**
* Convert IDA's calling convention into retdec-config representation.
* @param idaCC IDA calling convention.
* @param[out] configCC retdec-config calling convention.
*/
void ConfigGenerator::generateCallingConvention(
const cm_t &idaCC,
retdec::config::CallingConvention &configCC)
{
switch (idaCC)
{
case CM_CC_VOIDARG: configCC.setIsVoidarg(); break;
case CM_CC_CDECL: configCC.setIsCdecl(); break;
case CM_CC_ELLIPSIS: configCC.setIsEllipsis(); break;
case CM_CC_STDCALL: configCC.setIsStdcall(); break;
case CM_CC_PASCAL: configCC.setIsPascal(); break;
case CM_CC_FASTCALL: configCC.setIsFastcall(); break;
case CM_CC_THISCALL: configCC.setIsThiscall(); break;
case CM_CC_MANUAL: configCC.setIsManual(); break;
case CM_CC_SPOILED: configCC.setIsSpoiled(); break;
case CM_CC_SPECIALE: configCC.setIsSpecialE(); break;
case CM_CC_SPECIALP: configCC.setIsSpecialP(); break;
case CM_CC_SPECIAL: configCC.setIsSpecial(); break;
#if IDA_SDK_VERSION >= 770
case CM_CC_GOLANG: //configCC.setIsGoLang(); break;
#endif
case CM_CC_INVALID:
case CM_CC_UNKNOWN:
#if IDA_SDK_VERSION < 770
case CM_CC_RESERVE4:
#endif
case CM_CC_RESERVE3:
default: configCC.setIsUnknown(); break;
}
}
/**
* Convert IDA's function type into retdec-config representation.
* @param fncType IDA's function type.
* @param ccFnc retdec-config function type.
*/
void ConfigGenerator::generateFunctionType(
const tinfo_t &fncType,
retdec::config::Function &ccFnc)
{
// Generate arguments and return from function type.
//
func_type_data_t fncInfo;
if (fncType.get_func_details(&fncInfo))
{
// Return info.
//
ccFnc.returnType.setLlvmIr(type2string(fncInfo.rettype));
ccFnc.returnStorage = generateObjectLocation(
fncInfo.retloc,
fncInfo.rettype);
// Argument info.
//
unsigned cntr = 1;
for (auto const& a : fncInfo)
{
std::string name = a.name.c_str();
if (name.empty())
{
name = "a" + std::to_string(cntr);
}
auto s = generateObjectLocation(a.argloc, a.type);
retdec::config::Object arg(name, s);
arg.type.setLlvmIr( type2string(a.type) );
ccFnc.parameters.insert(arg);
++cntr;
}
// Calling convention.
//
generateCallingConvention(fncType.get_cc(), ccFnc.callingConvention);
}
else
{
// TODO: ???
}
}
/**
* @return @c True if provided function is linked.
* TODO: Do we really want to do this? What is the point?
*/
bool isLinkedFunction(func_t *fnc)
{
// Either there is no code in function = no instructions,
// or only instructions have "retn" mnemonics.
//
for (ea_t addr = fnc->start_ea; addr < fnc->end_ea; ++addr)
{
flags_t flags = get_flags(addr);
if (is_code(flags))
{
qstring mnem;
print_insn_mnem(&mnem, addr);
if (mnem != "retn")
{
return false;
}
}
}
return true;
}
/**
* Generate function information from the analysed file.
*/
void ConfigGenerator::generateFunctions()
{
for (unsigned i = 0; i < get_func_qty(); ++i)
{
func_t *fnc = getn_func(i);
qstring qFncName;
get_func_name(&qFncName, fnc->start_ea);
std::string fncName = qFncName.c_str();
std::replace(fncName.begin(), fncName.end(), '.', '_');
DBG_MSG("\t" << fncName << " @ " << std::hex << fnc->start_ea
<< ", #args = " << std::dec << fnc->regargqty << "\n");
retdec::config::Function ccFnc(fncName);
ccFnc.setStart(fnc->start_ea);
ccFnc.setEnd(fnc->end_ea);
// TODO: return type is always set to default: ugly, make it better.
ccFnc.returnType.setLlvmIr("i32");
qstring qCmt;
if (get_func_cmt(&qCmt, fnc, false) > 0)
{
ccFnc.setComment(qCmt.c_str());
}
qstring qDemangled;
if (demangle_name(&qDemangled, fncName.c_str(), MNG_SHORT_FORM) > 0)
{
ccFnc.setDemangledName(qDemangled.c_str());
}
if (fnc->flags & FUNC_STATICDEF)
{
ccFnc.setIsStaticallyLinked();
}
else if (fnc->flags & FUNC_LIB)
{
ccFnc.setIsDynamicallyLinked();
}
if (isLinkedFunction(fnc))
{
ccFnc.setIsDynamicallyLinked();
}
// For IDA 6.x (don't know about IDA 7.x):
// get_tinfo2() is preferred before guess_func_tinfo2()
// for unknown reason, guess_func_tinfo2() sometimes mix up the
// arguments (vawtrak sub_10021A76).
//
tinfo_t fncType;
get_tinfo(&fncType, fnc->start_ea);
if (!fncType.is_func())
{
// Guess type from first instruction address.
//
if (guess_tinfo(&fncType, fnc->start_ea) != GUESS_FUNC_OK)
{
// problem
}
}
if (fncType.is_func())
{
generateFunctionType(fncType, ccFnc);
}
config.functions.insert( ccFnc );
}
}
/**
* Generate segments, and generate all global data from segments.
*/
void ConfigGenerator::generateSegmentsAndGlobals()
{
qstring buff;
int segNum = get_segm_qty();
for (int i = 0; i < segNum; ++i)
{
segment_t* seg = getnseg(i);
if (seg == nullptr)
{
continue;
}
if (get_visible_segm_name(&buff, seg) <= 0)
{
continue;
}
retdec::config::Segment segment(retdec::utils::Address(seg->start_ea));
segment.setName(buff.c_str());
segment.setEnd(seg->end_ea);
config.segments.insert(segment);
ea_t head = seg->start_ea - 1;
while ( (head = next_head(head, seg->end_ea)) != BADADDR)
{
flags_t f = get_full_flags(head);
if (f == 0)
{
continue;
}
// Argument 1 should not be present for data.
// Some object do have argument 0 (off_X), some dont (strings).
//
if (!is_data(f) || !is_head(f) || /*!is_defarg0(f) ||*/ is_defarg1(f))
{
continue;
}
if (!has_any_name(f)) // usually alignment.
{
continue;
}
if (get_name(&buff, head) <= 0)
{
continue;
}
auto s = retdec::config::Storage::inMemory(
retdec::utils::Address(head));
retdec::config::Object global(buff.c_str(), s);
// Get type.
//
tinfo_t getType;
get_tinfo(&getType, head);
if (!getType.empty() && getType.present() && getType.is_func())
{
if (config.functions.getFunctionByStartAddress(head) != nullptr)
{
continue;
}
std::string fncName = buff.c_str();
std::replace(fncName.begin(), fncName.end(), '.', '_');
retdec::config::Function ccFnc(fncName);
ccFnc.setStart(head);
ccFnc.setEnd(head);
ccFnc.setIsDynamicallyLinked();
generateFunctionType(getType, ccFnc);
qstring qDemangled;
if (demangle_name(&qDemangled, fncName.c_str(), MNG_SHORT_FORM) > 0)
{
ccFnc.setDemangledName(qDemangled.c_str());
}
config.functions.insert(ccFnc);
continue;
}
// Continue creating global variable.
//
if (!getType.empty() && getType.present())
{
global.type.setLlvmIr(type2string(getType));
}
else
{
global.type.setLlvmIr(addrType2string(head));
}
config.globals.insert( global );
}
}
}
/**
* @brief Get LLVM IR representation of item type on provided address.
* @return LLVM IR type string.
*/
std::string ConfigGenerator::addrType2string(ea_t addr)
{
flags_t f = get_full_flags(addr);
if (f == 0)
{
return defaultTypeString();
}
asize_t itemSize = get_item_size(addr);
asize_t elemSize = get_data_elsize(addr, f);
asize_t arraySize = 0;
if (itemSize > elemSize)
{
arraySize = itemSize / elemSize;
}
std::string item = defaultTypeString();
if (is_byte(f))
{
item = "i8";
}
else if (is_word(f))
{
item = "i16";
}
else if (is_dword(f))
{
item = "i32";
}
else if (is_qword(f))
{
item = "i64";
}
else if (is_oword(f))
{
item = "i128";
}
else if (is_yword(f))
{
item = "i256";
}
else if (is_tbyte(f))
{
item = "i80";
}
else if (is_float(f))
{
item = "float";
}
else if (is_double(f))
{
item = "double";
}
else if (is_pack_real(f))
{
item = "x86_fp80"; // TODO: ??? maybe 12B = 96b.
}
else if (is_strlit(f))
{
item = "i8";
}
else if (is_struct(f))
{
item = defaultTypeString(); // TODO: not supported right now.
}
else if (is_align(f))
{
item = "i" + std::to_string(elemSize);
}
else if (is_custom(f))
{
item = defaultTypeString(); // TODO: not supported right now.
}
else
{
item = defaultTypeString();
}
std::string ret = defaultTypeString();
if (arraySize)
{
ret = "[" + std::to_string(arraySize) + " x " + item + "]";
}
else
{
ret = item;
}
return ret;
}
/**
* Get LLVM IR representation of the provided IDA Pro data type.
* @param type IDA Pro type.
* @return LLVM IR data type.
*
* TODO - recursive structure types?
*/
std::string ConfigGenerator::type2string(const tinfo_t &type)
{
std::string ret = defaultTypeString();
if (type.empty())
return ret;
if (type.is_char() || type.is_uchar()) ret = "i8";
else if (type.is_int16() || type.is_uint16()) ret = "i16";
else if (type.is_int32() || type.is_uint() || type.is_uint32()) ret = "i32";
else if (type.is_int64() || type.is_uint64()) ret = "i64";
else if (type.is_int128()) ret = "i128";
else if (type.is_ldouble()) ret = "f80";
else if (type.is_double()) ret = "double";
else if (type.is_float()) ret = "float";
else if (type.is_bool()) ret = "i1";
else if (type.is_void()) ret = "void";
else if (type.is_unknown()) ret = "i32";
else if (type.is_ptr())
{
tinfo_t base = type.get_pointed_object();
ret = type2string(base) + "*";
}
else if (type.is_func())
{
func_type_data_t fncType;
if (type.get_func_details(&fncType))
{
ret = type2string( fncType.rettype );
ret += "(";
bool first = true;
for (auto const &a : fncType)
{
if (first)
{
first = false;
}
else
{
ret += ", ";
}
ret += type2string(a.type);
}
ret += ")";
}
else
{
ERROR_MSG("ConfigGenerator::type2string() -- function type failed\n");
ret = "i32*";
}
}
else if (type.is_array())
{
tinfo_t base = type.get_array_element();
std::string baseType = type2string(base);
int arraySize = type.get_array_nelems();
if (arraySize > 0)
{
ret = "[" + std::to_string(arraySize) + " x " + baseType + "]";
}
else
{
ret = baseType + "*";
}
}
else if (type.is_struct())
{
auto it = structIdSet.find(type);
std::string strName = "%";
// This structure have already been generated.
//
if (it != structIdSet.end())
{
return it->second;
}
else
{
qstring idaStrName = ""; // make sure it is empty.
if (type.get_final_type_name(&idaStrName) && !idaStrName.empty())
{
strName += idaStrName.c_str();
}
else
{
strName += "struct_" + std::to_string(config.structures.size());
}
structIdSet[type] = strName;
}
std::string body;
int elemCnt = type.get_udt_nmembers();
if (elemCnt > 0)
{
body = "{ ";
bool first = true;
for (int i=0; i<elemCnt; ++i)
{
udt_member_t mem;
mem.offset = i;
std::string memType = defaultTypeString();
if (type.find_udt_member(&mem, STRMEM_INDEX) >= 0)
{
memType = type2string( mem.type );
}
if (first)
{
first = false;
}
else
{
body += ", ";
}
body += memType;
}
body += " }";
}
else
{
body = "{ " + defaultTypeString() + " }";
}
ret = strName; // only structure name is returned.
retdec::config::Type ccType( strName + " = type " + body );
config.structures.insert( ccType );
}
else if (type.is_union())
{
ERROR_MSG("ConfigGenerator::type2string() -- union type not supported\n");
ret = defaultTypeString();
}
else if (type.is_enum())
{
ERROR_MSG("ConfigGenerator::type2string() -- enum type not supported\n");
ret = defaultTypeString();
}
else if (type.is_sue())
{
ERROR_MSG("ConfigGenerator::type2string() -- SUE type not supported\n");
ret = defaultTypeString();
}
else if (type.is_bitfield()) // http://en.cppreference.com/w/cpp/language/bit_field
{
ERROR_MSG("ConfigGenerator::type2string() -- bitfield type not supported\n");
ret = defaultTypeString();
}
else
{
ERROR_MSG("ConfigGenerator::type2string() -- some unknown type\n");
ret = defaultTypeString();
}
return ret;
}
/**
* Get LLVM IR representation of the default data type.
* @return LLVM IR data type.
*/
std::string ConfigGenerator::defaultTypeString()
{
return "i32";
}
} // namespace idaplugin