-
Notifications
You must be signed in to change notification settings - Fork 29
/
idaplugin.cpp
928 lines (845 loc) · 24.8 KB
/
idaplugin.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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/**
* @file idaplugin/idaplugin.cpp
* @brief Plugin entry point - definition of plugin's intarface.
* @copyright (c) 2017 Avast Software, licensed under the MIT license
*/
//Build Dependencies:
//IDA Pro 7.2 SDK
//RetDec v3.3: https://github.com/avast/retdec config and utils libraries
//RetDec IDA Plugin v0.9: https://github.com/avast/retdec-idaplugin
//jsoncpp library v1.8.4: https://github.com/open-source-parsers/jsoncpp
//whereami library: https://github.com/gpakosz/whereami
//Runtime Dependencies: Ghidra 9.0.4 path specified (JDK v11+ not necessary for decompile.exe/sleigh.exe in Ghidra/Features/Decompiler/os/*/decompile[.exe], sleigh modules in Ghidra/Processors)
//Plugin copied to IDA Pro 7.2 plugins folder
//conversion.h in x86 needs a fix for adding __cdecl to the ios_base return pointer format as in std::ios_base &(__cdecl* format)(std::ios_base &) = std::dec
#include <fstream>
#include <iostream>
#if !defined(OS_WINDOWS) // Linux || macOS
#include <signal.h>
#endif
#include "retdec/utils/file_io.h"
#include "retdec/utils/filesystem_path.h"
#include "code_viewer.h"
#include "config_generator.h"
#include "decompiler.h"
#include "defs.h"
#include "plugin_config.h"
#include "sleighinterface.h"
#include "idaplugin.h"
#if IDA_SDK_VERSION < 750
class idaplugin::GhidraDec;
idaplugin::GhidraDec* pm;
#endif
namespace idaplugin {
GhidraDec::GhidraDec()
{
#if IDA_SDK_VERSION >= 750
init();
#endif
}
GhidraDec::~GhidraDec()
{
term();
}
/**
* Kill old thread if still running.
*/
void GhidraDec::killDecompilation(bool bTerminate)
{
if (decompInfo->decompRunning)
{
INFO_MSG("Unfinished decompilation was KILLED !!! Only one decompilation can run at a time.\n");
{ //must be a locked unit otherwise race condition can occur
qmutex_locker_t lock(decompInfo->qm);
decompInfo->exiting = true;
if (decompInfo->uiExecutingTask != -1) {
if (cancel_exec_request(decompInfo->uiExecutingTask)) {
qsem_post(decompInfo->termSem);
}
}
}
//obviously killing a thread is a last case resort which risks a large amount of side effects
stopDecompilation(decompInfo, false, false, false); //first kill only the decompilation process
//qsleep(3000); //wait 3 seconds since no way to poll thread status
//qthread_kill(decompInfo->decompThread);
qthread_join(decompInfo->decompThread);
qthread_free(decompInfo->decompThread);
decompInfo->decompThread = nullptr;
stopDecompilation(decompInfo, false, false, true);
decompInfo->uiExecutingTask = -1;
decompInfo->exiting = false;
/*
if (decompInfo->decompPid)
{
//term_process()...
#if defined(OS_WINDOWS) //OpenProcess/WaitForSingleObject/KillProcess...or better yet keep the process handle, the only clean way in Windows
std::string cmd = "taskkill /F /T /PID " + std::to_string(decompInfo->decompPid);
std::system(cmd.c_str());
#else // Linux || macOS
kill(decompInfo->decompPid, SIGTERM);
#endif
decompInfo->decompPid = 0;
}
if (decompInfo->rdHandle != -1) {
_close(decompInfo->rdHandle); decompInfo->rdHandle = -1;
}
if (decompInfo->wrHandle != -1) {
_close(decompInfo->wrHandle); decompInfo->wrHandle = -1;
}*/
decompInfo->decompRunning = false;
} else {
if (decompInfo->decompThread != nullptr) {
qthread_join(decompInfo->decompThread);
qthread_free(decompInfo->decompThread);
decompInfo->decompThread = nullptr;
}
if (bTerminate) stopDecompilation(decompInfo, true, false, true);
else stopDecompilation(decompInfo, false, true, true); //make sure did not exit before starting again
}
}
/**
* Save IDA database before decompilation to protect it if something goes wrong.
* @param inSitu If true, DB is saved with the same name as IDA default database.
* @param suffix If @p inSitu is false, use this suffix to distinguish DBs.
*/
void GhidraDec::saveIdaDatabase(bool inSitu, const std::string &suffix)
{
INFO_MSG("Saving IDA database ...\n");
std::string workIdb = decompInfo->workIdb;
auto dotPos = workIdb.find_last_of(".");
if (dotPos != std::string::npos)
{
workIdb.erase(dotPos, std::string::npos);
}
if (!inSitu)
{
workIdb += suffix;
}
workIdb += std::string(".") + IDB_EXT;
save_database(workIdb.c_str(), DBFL_COMP);
INFO_MSG("IDA database saved into : " << workIdb << "\n");
}
/**
* Generate retargetable decompiler database from IDA database.
*/
void GhidraDec::generatePluginDatabase()
{
INFO_MSG("Generating retargetable decompilation DB ...\n");
ConfigGenerator jg(*decompInfo);
decompInfo->dbFile = jg.generate();
}
/**
* Find out if input file is relocatable -- object file.
* @return @c True if file relocatable, @c false otherwise.
*/
bool GhidraDec::isRelocatable()
{
#ifdef __X64__
if (inf_filetype == f_COFF && inf_start_ea == BADADDR)
#else
if (inf_filetype == f_COFF && inf.beginEA == BADADDR)
#endif
{
return true;
}
else if (inf_filetype == f_ELF)
{
std::ifstream infile(decompInfo->inputPath, std::ios::binary);
if (infile.good())
{
std::size_t e_type_offset = 0x10;
infile.seekg(e_type_offset, std::ios::beg);
// relocatable -- constant 0x1 at <0x10-0x11>
// little endian -- 0x01 0x00
// big endian -- 0x00 0x01
char b1 = 0;
char b2 = 0;
if (infile.get(b1))
{
if (infile.get(b2))
{
if (std::size_t(b1) + std::size_t(b2) == 1)
{
return true;
}
}
}
}
}
// f_BIN || f_PE || f_HEX || other
return false;
}
/**
* Decompile only provided function or if nothing provided then the current function under focus.
* @param fnc2decomp Function to decompile.
* @param force If @c true, decompilation is always performed.
*/
void GhidraDec::runSelectiveDecompilation(func_t *fnc2decomp, bool force)
{
if (decompInfo->decompThread != nullptr) {
if (!decompInfo->decompRunning) killDecompilation(false);
else if (ask_buttons("Yes", "No", nullptr, ASKBTN_YES, "A prior decompilation is still running, would you like to terminate it to process the new request?") == ASKBTN_YES) {
killDecompilation(true);
} else return;
}
if (isRelocatable() && inf_min_ea != 0)
{
WARNING_GUI(decompInfo->pluginName << " version " << decompInfo->pluginVersion
<< " can selectively decompile only relocatable objects loaded at 0x0.\n"
"Rebase the program to 0x0 or use full decompilation or our web interface at: "
<< decompInfo->pluginURL);
return;
}
// Decompilation triggered by double click.
//
if (fnc2decomp)
{
decompInfo->navigationList.erase(
++decompInfo->navigationActual,
decompInfo->navigationList.end());
decompInfo->navigationList.push_back(fnc2decomp);
decompInfo->navigationActual = decompInfo->navigationList.end();
decompInfo->navigationActual--;
// Show existing function.
//
auto fit = decompInfo->fnc2code.find(fnc2decomp);
if (!force && fit != decompInfo->fnc2code.end())
{
decompInfo->decompiledFunction = fnc2decomp;
qstring fncName;
get_func_name(&fncName, fnc2decomp->start_ea);
INFO_MSG("Show already decompiled function: " << fncName.c_str()
<< " @ " << std::hex << fnc2decomp->start_ea << "\n");
//decompInfo->thdProcing = true;
//decompInfo->curUIthread = qthread_create(showDecompiledCode, static_cast<void*>(&decompInfo));
ShowOutput show(decompInfo);
show.execute();
return;
}
// Decompile new function.
//
else
{
createRangesFromSelectedFunction(*decompInfo, fnc2decomp);
}
}
// Decompilation run from our viewer.
//
else if (get_current_viewer() == decompInfo->custViewer
|| get_current_viewer() == decompInfo->codeViewer)
{
// Re-decompile current function.
//
if (decompInfo->decompiledFunction)
{
createRangesFromSelectedFunction(
*decompInfo,
decompInfo->decompiledFunction);
decompInfo->navigationList.erase(
decompInfo->navigationActual,
decompInfo->navigationList.end());
decompInfo->navigationList.push_back(decompInfo->decompiledFunction);
decompInfo->navigationActual = decompInfo->navigationList.end();
decompInfo->navigationActual--;
}
// No current function -> something went wrong.
//
else
{
return;
}
}
// Decompilation run from some other window.
//
else
{
ea_t addr = get_screen_ea();
func_t *fnc = get_func(addr);
// Decompilation run from IDA disasm window (or some other window that allows it).
//
if (fnc)
{
createRangesFromSelectedFunction(*decompInfo, fnc);
decompInfo->decompiledFunction = fnc;
decompInfo->navigationList.clear();
decompInfo->navigationList.push_back( decompInfo->decompiledFunction );
decompInfo->navigationActual = decompInfo->navigationList.end();
decompInfo->navigationActual--;
}
// Bad window or bad position in disasm code.
//
else
{
WARNING_GUI("Function must be selected by the cursor.\n");
return;
}
}
INFO_MSG("Running Ghidra decompiler plugin:\n");
saveIdaDatabase();
generatePluginDatabase();
decompileInput(*decompInfo);
}
/**
* Decompile everything, but do not show it in viewer, instead dump it into file.
*/
void GhidraDec::runAllDecompilation()
{
std::string defaultOut = decompInfo->inputPath + ".c";
char* tmp;
int iChoice;
do {
tmp = ask_file( ///< Returns: file name
true, ///< bool for_saving
defaultOut.data(), ///< const char *default_answer
"%s", ///< const char *format
"Save decompiled file"
);
if (tmp == nullptr) ///< canceled
{
return;
}
struct stat st;
if (stat(tmp, &st) == 0) {
iChoice = ask_yn(ASKBTN_YES, "Overwrite existing file '%s'?", tmp);
if (iChoice == ASKBTN_CANCEL) return;
} else iChoice = ASKBTN_YES;
} while (iChoice == ASKBTN_NO);
decompInfo->outputFile = tmp;
decompInfo->ranges.clear();
decompInfo->decompiledFunction = nullptr;
INFO_MSG("Selected file: " << decompInfo->outputFile << "\n");
saveIdaDatabase();
generatePluginDatabase();
decompileInput(*decompInfo);
}
/**
*
*/
bool GhidraDec::setInputPath()
{
char buff[MAXSTR];
get_root_filename(buff, sizeof(buff));
std::string inName = buff;
get_input_file_path(buff, sizeof(buff));
std::string inPath = buff;
std::string idb = get_path(PATH_TYPE_IDB);
std::string id0 = get_path(PATH_TYPE_ID0);
std::string workDir;
std::string workIdb;
if (!idb.empty())
{
retdec::utils::FilesystemPath fsIdb(idb);
workDir = fsIdb.getParentPath();
workIdb = idb;
}
else if (!id0.empty())
{
retdec::utils::FilesystemPath fsId0(id0);
workDir = fsId0.getParentPath();
workIdb = id0;
}
if (workIdb.empty() || workDir.empty())
{
WARNING_GUI("Cannot decompile this input file, IDB and ID0 are not set.\n");
return false;
}
if (!retdec::utils::FilesystemPath(inPath).exists())
{
INFO_MSG("Input \"" << inPath << "\" does not exist, trying to recover ...\n");
retdec::utils::FilesystemPath fsWork(workDir);
fsWork.append(inName);
workDir = fsWork.getPath();
inPath = workDir;
if (!retdec::utils::FilesystemPath(inPath).exists())
{
INFO_MSG("Input \"" << inPath << "\" does not exist, asking user to "
"specify the input file ...\n");
char *tmp = ask_file( ///< Returns: file name
false, ///< bool for_saving
nullptr, ///< const char *default_answer
"%s", ///< const char *format
"Input binary to decompile"
);
if (!tmp)
{
return false;
}
else if (!retdec::utils::FilesystemPath(std::string(tmp)).exists())
{
WARNING_GUI("Cannot decompile this input file, there is no such "
"file: " << tmp << "\n");
return false;
}
inPath = tmp;
INFO_MSG("Successfully recovered, using user selected "
"file \"" << inPath << "\".\n");
}
else
{
INFO_MSG("Successfully recovered, using input file \"" << inPath << "\".\n");
}
}
else
{
INFO_MSG("Working on input file \"" << inPath << "\".\n");
}
decompInfo->inputName = inName;
decompInfo->inputPath = inPath;
decompInfo->workDir = workDir;
decompInfo->workIdb = workIdb;
DBG_MSG("Input Path : " << decompInfo->inputPath << "\n");
DBG_MSG("Input Name : " << decompInfo->inputName << "\n");
DBG_MSG("Work dir : " << decompInfo->workDir << "\n");
DBG_MSG("Work IDB : " << decompInfo->workIdb << "\n");
return true;
}
/**
* Perform startup check that determines, if plugin can decompile IDA's input file.
* @return True if plugin can decompile IDA's input, false otherwise.
* TODO: do some more checking (architecture, ...).
*/
bool GhidraDec::canDecompileInput()
{
if (!setInputPath())
{
return false;
}
decompInfo->li.clear();
decompInfo->toolMap.clear();
DecompInterface::getLangFiles(decompInfo->ghidraPath + "/Ghidra/Processors", "IDA-PRO", decompInfo->toolMap, decompInfo->li);
//metapc is reported for 16-bit but not reported in Ghidra
for (size_t i = 0; i < decompInfo->li.size(); i++) {
if (decompInfo->li[i].processor == "x86" && decompInfo->li[i].size == 16) {
decompInfo->toolMap["metapc"].push_back((int)i); break;
}
}
std::string pname = inf_procname;
std::transform(pname.begin(), pname.end(), pname.begin(), [](unsigned char c) { return std::tolower(c); });
return decompInfo->toolMap.find(pname) != decompInfo->toolMap.end();
/*// 32-bit binary -> is_32bit() == 1 && is_64bit() == 0.
// 64-bit binary -> is_32bit() == 1 && is_64bit() == 1.
// Allow 64-bit x86.
if ((!inf.is_32bit() || inf.is_64bit()) && !isX86())
{
WARNING_GUI(decompInfo->pluginName << " version " << decompInfo->pluginVersion
<< " can decompile only 32-bit input files.\n"
<< "PROCNAME = " << inf.procname
);
return false;
}
if (!(inf.filetype == f_BIN
|| inf.filetype == f_EXE_old
|| inf.filetype == f_COM_old
|| inf.filetype == f_LX
|| inf.filetype == f_LE
|| inf.filetype == f_WIN
|| inf.filetype == f_EXE
|| inf.filetype == f_COM
|| inf.filetype == f_PE
|| inf.filetype == f_ELF
|| inf.filetype == f_COFF
|| inf.filetype == f_MACHO
|| inf.filetype == f_HEX))
{
if (inf.filetype == f_LOADER)
{
WARNING_GUI("Custom IDA loader plugin was used.\n"
"Decompilation will be attempted, but:\n"
"1. GhidraDec idaplugin can not check if the input can be "
"decompiled. Decompilation may fail.\n"
"2. If the custom loader behaves differently than the GhidraDec "
"loader, decompilation may fail or produce nonsensical result.");
}
else
{
WARNING_GUI(decompInfo->pluginName << " version " << decompInfo->pluginVersion
<< " cannot decompile this input file (file type = "
<< inf.filetype << ").\n");
return false;
}
}
decompInfo->mode.clear();
decompInfo->architecture.clear();
decompInfo->endian.clear();
decompInfo->rawEntryPoint = retdec::utils::Address();
decompInfo->rawSectionVma = retdec::utils::Address();
// Check Intel HEX.
//
if (inf.filetype == f_HEX)
{
std::string procName = inf.procname;
if (procName == "mipsr" || procName == "mipsb")
{
decompInfo->architecture = "mips";
decompInfo->endian = "big";
}
else if (procName == "mipsrl"
|| procName == "mipsl"
|| procName == "psp")
{
decompInfo->architecture = "mips";
decompInfo->endian = "little";
}
else
{
WARNING_GUI("Intel HEX input file can be decompiled only for one of "
"these {mipsr, mipsb, mipsrl, mipsl, psp} processors, "
"not \"" << procName << "\".\n");
return false;
}
}
// Check BIN (RAW).
//
if (inf.filetype == f_BIN)
{
decompInfo->mode = "raw";
// Section VMA.
//
decompInfo->rawSectionVma = inf.min_ea;
// Entry point.
//
if (inf.start_ea != BADADDR)
{
decompInfo->rawEntryPoint = inf.start_ea;
}
else
{
decompInfo->rawEntryPoint = decompInfo->rawSectionVma;
}
// Architecture + endian.
//
std::string procName = inf.procname;
if (procName == "mipsr" || procName == "mipsb")
{
decompInfo->architecture = "mips";
decompInfo->endian = "big";
}
else if (procName == "mipsrl" || procName == "mipsl" || procName == "psp")
{
decompInfo->architecture = "mips";
decompInfo->endian = "little";
}
else if (procName == "ARM")
{
decompInfo->architecture = "arm";
decompInfo->endian = "little";
}
else if (procName == "ARMB")
{
decompInfo->architecture = "arm";
decompInfo->endian = "big";
}
else if (procName == "PPCL")
{
decompInfo->architecture = "powerpc";
decompInfo->endian = "little";
}
else if (procName == "PPC")
{
decompInfo->architecture = "powerpc";
decompInfo->endian = "big";
}
else if (isX86())
{
decompInfo->architecture = inf.is_64bit() ? "x86-64" : "x86";
decompInfo->endian = "little";
}
else
{
WARNING_GUI("Binary input file can be decompiled only for one of these "
"{mipsr, mipsb, mipsrl, mipsl, psp, ARM, ARMB, PPCL, PPC, 80386p, "
"80386r, 80486p, 80486r, 80586p, 80586r, 80686p, p2, p3, p4} "
"processors, not \"" << procName << "\".\n");
return false;
}
}
return true;*/
}
/**
* Plugin run function.
* The plugin can be passed an integer argument from plugins.cfg file.
* This can be useful when we want the one plugin to do something
* different depending on the hot-key pressed or menu item selected.
* IDA is searching for this function.
* @param arg Argument set to value according plugins.cfg based on invocation hotkey.
*/
#ifdef __X64__
bool idaapi GhidraDec::run(size_t arg)
#else
void idaapi GhidraDec::run(int arg)
#endif
{
#if IDA_SDK_VERSION < 750
RdGlobalInfo* decompInfo = pm->decompInfo;
#endif
bool bRet = true;
if (!auto_is_ok())
{
INFO_MSG("GhidraDec plugin cannot run because the initial autoanalysis has not been finished.\n");
bRet = false;
}
if (decompInfo->decompThread != nullptr) {
if (!decompInfo->decompRunning) decompInfo->pm->killDecompilation(false);
else if (ask_buttons(nullptr, nullptr, nullptr, ASKBTN_YES, "HIDECANCEL\nA prior decompilation is still running, would you like to terminate it to process the new request?") == ASKBTN_YES) {
decompInfo->pm->killDecompilation(true);
}
else bRet = false;
}
if (bRet && decompInfo->configureDecompilation())
{
bRet = false;
}
if (bRet && !decompInfo->pm->canDecompileInput()) //needs configuration first
{
bRet = false;
}
// ordinary selective decompilation
//
if (!bRet) {}
else if (arg == 0)
{
decompInfo->pm->runSelectiveDecompilation();
}
// ordinary full decompilation
//
else if (arg == 1)
{
decompInfo->pm->runAllDecompilation();
}
// only plugin configuration
//
else if (arg == 2)
{
if (!pluginConfigurationMenu(*decompInfo)) {
decompInfo->pm->killDecompilation(true); //settings do not take effect without restarting process
}
}
// only run database generation
//
else if (arg == 3)
{
decompInfo->pm->generatePluginDatabase();
}
// selective decompilation used in plugin's regression tests
// forced local decompilation + disabled threads
// function to decompile is selected by "<ghidradec_select>" string in function's comment
//
else if (arg == 4)
{
for (unsigned i = 0; i < get_func_qty(); ++i)
{
qstring qCmt;
func_t* fnc = getn_func(i);
if (get_func_cmt(&qCmt, fnc, false) <= 0)
{
continue;
}
std::string cmt = qCmt.c_str();;
if (cmt.find("<ghidradec_select>") != std::string::npos)
{
decompInfo->outputFile = decompInfo->inputPath + ".c";
decompInfo->setIsUseThreads(false);
decompInfo->pm->runSelectiveDecompilation(fnc);
break;
}
}
}
// full decompilation used in plugin's regression tests
// forced local decompilation + disabled threads
//
else if (arg == 5)
{
decompInfo->setIsUseThreads(false);
decompInfo->pm->runAllDecompilation();
}
else
{
WARNING_GUI(decompInfo->pluginName << " version " << decompInfo->pluginVersion
<< " cannot handle argument '" << arg << "'.\n");
bRet = false;
}
#ifdef __X64__
return bRet;
#endif
}
#if IDA_SDK_VERSION >= 750
ssize_t idaapi GhidraDec::on_event(ssize_t notification_code, va_list va)
{
#else
ssize_t idaapi GhidraDec::view_callback(void* ud, int notification_code, va_list va)
{
RdGlobalInfo* decompInfo = static_cast<RdGlobalInfo*>(ud);
#endif
switch (notification_code) {
case view_close:
TWidget* view = va_arg(va, TWidget*);
if (view == nullptr) return 0;
if (decompInfo->graphWidget == view) {
decompInfo->graphWidget = nullptr;
delete_mutable_graph(decompInfo->mg);
decompInfo->graphText.clear();
netnode nn("GhidraGraph", 0, true);
nn.kill();
} else if (decompInfo->graphViewer == view) { //only this not for graphWidget
decompInfo->graphViewer = nullptr;
if (decompInfo->graphWidget) {
close_widget(decompInfo->graphWidget, 0);
decompInfo->graphWidget = nullptr;
delete_mutable_graph(decompInfo->mg);
decompInfo->graphText.clear();
netnode nn("GhidraGraph", 0, true);
nn.kill();
}
} else if (decompInfo->codeViewer == view) {
decompInfo->codeViewer = nullptr;
} else if (decompInfo->custViewer == view) { //only this not for codeViewer
decompInfo->custViewer = nullptr;
if (decompInfo->codeViewer) {
close_widget(decompInfo->codeViewer, 0);
decompInfo->codeViewer = nullptr;
}
}
break;
}
return 0;
}
/**
* Plugin initialization function.
* IDA is searching for this function.
*/
int idaapi GhidraDec::init()
{
static bool inited = false;
if (inited)
{
return PLUGIN_KEEP;
}
decompInfo = new RdGlobalInfo(this);
decompInfo->pluginRegNumber = register_addon(&decompInfo->pluginInfo);
if (decompInfo->pluginRegNumber < 0)
{
delete decompInfo;
WARNING_GUI(decompInfo->pluginName << " version " << decompInfo->pluginVersion
<< " failed to register.\n");
return PLUGIN_SKIP;
}
else
{
INFO_MSG(decompInfo->pluginName << " version "
<< decompInfo->pluginVersion << " registered OK\n");
}
readConfigFile(*decompInfo);
if (is_idaq() && addConfigurationMenuOption(*decompInfo))
{
delete decompInfo;
return PLUGIN_SKIP;
}
INFO_MSG(decompInfo->pluginName << " version " << decompInfo->pluginVersion
<< " loaded OK\n");
decompInfo->qm = qmutex_create();
decompInfo->termSem = qsem_create(nullptr, 0);
hook_to_notification_point(HT_UI, ui_callback, decompInfo);
#if IDA_SDK_VERSION < 750
hook_to_notification_point(HT_VIEW, view_callback, decompInfo);
#endif
registerPermanentActions();
inited = true;
return PLUGIN_KEEP;
}
/**
* Plugin termination function.
* IDA is searching for this function.
*/
void idaapi GhidraDec::term()
{
killDecompilation(true);
if (decompInfo->idacb != nullptr) {
delete decompInfo->idacb;
decompInfo->idacb = nullptr;
}
if (decompInfo->graphViewer != nullptr) {
if (find_widget((decompInfo->viewerName + "_Graph").c_str()) != nullptr)
close_widget(decompInfo->graphViewer, 0);
decompInfo->graphViewer = nullptr;
}
if (decompInfo->graphWidget != nullptr) {
if (find_widget((decompInfo->viewerName + " Graph").c_str()) != nullptr)
close_widget(decompInfo->graphWidget, 0);
decompInfo->graphWidget = nullptr;
delete_mutable_graph(decompInfo->mg);
decompInfo->graphText.clear();
netnode nn("GhidraGraph", 0, true);
nn.kill();
}
if (decompInfo->custViewer) {
close_widget(decompInfo->custViewer, 0);
decompInfo->custViewer = nullptr;
}
if (decompInfo->codeViewer) {
close_widget(decompInfo->codeViewer, 0);
decompInfo->codeViewer = nullptr;
}
unregister_action("ghidradec:ActionJumpToAsm");
unregister_action("ghidradec:ActionChangeFncGlobName");
unregister_action("ghidradec:ActionOpenXrefs");
unregister_action("ghidradec:ActionOpenCalls");
unregister_action("ghidradec:ActionChangeFncType");
unregister_action("ghidradec:ActionChangeFncComment");
unregister_action("ghidradec:ActionMoveForward");
unregister_action("ghidradec:ActionMoveBackward");
if (is_idaqt) {
const char optionsActionName[] = "ghidradec:ShowOptions";
unregister_action(optionsActionName);
}
#if IDA_SDK_VERSION < 750
unhook_from_notification_point(HT_VIEW, view_callback);
#endif
unhook_from_notification_point(HT_UI, ui_callback);
qsem_free(decompInfo->termSem);
qmutex_free(decompInfo->qm);
delete decompInfo;
}
} // namespace idaplugin
using namespace idaplugin;
/**
* Plugin initialization function.
* IDA is searching for this function.
*/
#if IDA_SDK_VERSION >= 750
plugmod_t* idaapi init()
{
GhidraDec* pm = new GhidraDec();
return pm->decompInfo->pluginRegNumber < 0 ? nullptr : pm;
}
#else
int idaapi init()
{
pm = new GhidraDec();
return pm->init();
}
void idaapi term()
{
delete pm;
}
#endif
/**
* Plugin interface definition.
* IDA is searching for this structure.
*/
plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION, // Constant version.
PLUGIN_MULTI, // Plugin flags.
init, // Initialize.
#if IDA_SDK_VERSION >= 750
nullptr,
nullptr,
#else
term, // Terminate. this pointer may be nullptr.
GhidraDec::run, // Invoke plugin.
#endif
PLUGIN_COPYRIGHT, // Long comment about the plugin.
PLUGIN_URL, // Multiline help about the plugin.
PLUGIN_NAME, // The preferred short name of the plugin.
PLUGIN_HOTKEY // The preferred hotkey to run the plugin.
};