dcc.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*****************************************************************************
  2. * dcc decompiler
  3. * Reads the command line switches and then executes each major section in turn
  4. * (C) Cristina Cifuentes
  5. ****************************************************************************/
  6. #include "dcc.h"
  7. #include "msvc_fixes.h"
  8. #include "project.h"
  9. #include "CallGraph.h"
  10. #include "DccFrontend.h"
  11. #include <cstring>
  12. #include <iostream>
  13. #include <QtCore/QCoreApplication>
  14. #include <QCommandLineParser>
  15. #ifdef LLVM_EXPERIMENTAL
  16. #include <llvm/Support/raw_os_ostream.h>
  17. #include <llvm/Support/CommandLine.h>
  18. #include <llvm/Support/TargetSelect.h>
  19. #include <llvm/Support/TargetRegistry.h>
  20. #include <llvm/Support/PrettyStackTrace.h>
  21. #include <llvm/Support/Signals.h>
  22. #include <llvm/Support/Host.h>
  23. #include <llvm/Target/TargetMachine.h>
  24. #include <llvm/Target/TargetInstrInfo.h>
  25. #include <llvm/MC/MCAsmInfo.h>
  26. #include <llvm/CodeGen/MachineInstrBuilder.h>
  27. #include <llvm/TableGen/Main.h>
  28. #include <llvm/TableGen/TableGenBackend.h>
  29. #include <llvm/TableGen/Record.h>
  30. #endif
  31. #include <QtCore/QFile>
  32. /* Global variables - extern to other modules */
  33. extern QString asm1_name, asm2_name; /* Assembler output filenames */
  34. extern SYMTAB symtab; /* Global symbol table */
  35. extern STATS stats; /* cfg statistics */
  36. extern OPTION option; /* Command line options */
  37. static char *initargs(int argc, char *argv[]);
  38. static void displayTotalStats(void);
  39. /****************************************************************************
  40. * main
  41. ***************************************************************************/
  42. #ifdef LLVM_EXPERIMENTAL
  43. using namespace llvm;
  44. bool TVisitor(raw_ostream &OS, RecordKeeper &Records)
  45. {
  46. Record *rec = Records.getDef("ADD8i8");
  47. if(rec)
  48. {
  49. if(not rec->getTemplateArgs().empty())
  50. std::cout << "Has template args\n";
  51. auto classes(rec->getSuperClasses());
  52. for(auto val : rec->getSuperClasses())
  53. std::cout << "Super "<<val->getName()<<"\n";
  54. // DagInit * in = rec->getValueAsDag(val.getName());
  55. // in->dump();
  56. for(const RecordVal &val : rec->getValues())
  57. {
  58. // val.dump();
  59. }
  60. rec->dump();
  61. }
  62. // rec = Records.getDef("CCR");
  63. // if(rec)
  64. // rec->dump();
  65. // for(auto val : Records.getDefs())
  66. // {
  67. // //std::cout<< "Def "<<val.first<<"\n";
  68. // }
  69. return false;
  70. }
  71. int testTblGen(int argc, char **argv)
  72. {
  73. // using namespace llvm;
  74. // sys::PrintStackTraceOnErrorSignal();
  75. // PrettyStackTraceProgram(argc,argv);
  76. // cl::ParseCommandLineOptions(argc,argv);
  77. // return llvm::TableGenMain(argv[0],TVisitor);
  78. // InitializeNativeTarget();
  79. // Triple TheTriple;
  80. // std::string def = sys::getDefaultTargetTriple();
  81. // std::string MCPU="i386";
  82. // std::string MARCH="x86";
  83. // InitializeAllTargetInfos();
  84. // InitializeAllTargetMCs();
  85. // InitializeAllAsmPrinters();
  86. // InitializeAllAsmParsers();
  87. // InitializeAllDisassemblers();
  88. // std::string TargetTriple("i386-pc-linux-gnu");
  89. // TheTriple = Triple(Triple::normalize(TargetTriple));
  90. // MCOperand op=llvm::MCOperand::CreateImm(11);
  91. // MCAsmInfo info;
  92. // raw_os_ostream wrap(std::cerr);
  93. // op.print(wrap,&info);
  94. // wrap.flush();
  95. // std::cerr<<"\n";
  96. // std::string lookuperr;
  97. // TargetRegistry::printRegisteredTargetsForVersion();
  98. // const Target *t = TargetRegistry::lookupTarget(MARCH,TheTriple,lookuperr);
  99. // TargetOptions opts;
  100. // std::string Features;
  101. // opts.PrintMachineCode=1;
  102. // TargetMachine *tm = t->createTargetMachine(TheTriple.getTriple(),MCPU,Features,opts);
  103. // std::cerr<<tm->getInstrInfo()->getName(97)<<"\n";
  104. // const MCInstrDesc &ds(tm->getInstrInfo()->get(97));
  105. // const MCOperandInfo *op1=ds.OpInfo;
  106. // uint16_t impl_def = ds.getImplicitDefs()[0];
  107. // std::cerr<<lookuperr<<"\n";
  108. // exit(0);
  109. }
  110. #endif
  111. void setupOptions(QCoreApplication &app) {
  112. //[-a1a2cmsi]
  113. QCommandLineParser parser;
  114. parser.setApplicationDescription("dcc");
  115. parser.addHelpOption();
  116. //parser.addVersionOption();
  117. //QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy"));
  118. QCommandLineOption boolOpts[] {
  119. QCommandLineOption {"v", QCoreApplication::translate("main", "verbose")},
  120. QCommandLineOption {"V", QCoreApplication::translate("main", "very verbose")},
  121. QCommandLineOption {"c", QCoreApplication::translate("main", "Follow register indirect calls")},
  122. QCommandLineOption {"m", QCoreApplication::translate("main", "Print memory maps of program")},
  123. QCommandLineOption {"s", QCoreApplication::translate("main", "Print stats")}
  124. };
  125. for(QCommandLineOption &o : boolOpts) {
  126. parser.addOption(o);
  127. }
  128. QCommandLineOption assembly("a", QCoreApplication::translate("main", "Produce assembly"),"assembly_level");
  129. QCommandLineOption targetFileOption(QStringList() << "o" << "output",
  130. QCoreApplication::translate("main", "Place output into <file>."),
  131. QCoreApplication::translate("main", "file"));
  132. QCommandLineOption entryPointOption(QStringList() << "E",
  133. QCoreApplication::translate("main", "Custom entry point as hex"),
  134. QCoreApplication::translate("main", "offset"),
  135. "0"
  136. );
  137. parser.addOption(targetFileOption);
  138. parser.addOption(assembly);
  139. parser.addOption(entryPointOption);
  140. //parser.addOption(forceOption);
  141. // Process the actual command line arguments given by the user
  142. parser.addPositionalArgument("source", QCoreApplication::translate("main", "Dos Executable file to decompile."));
  143. parser.process(app);
  144. const QStringList args = parser.positionalArguments();
  145. if(args.empty()) {
  146. parser.showHelp();
  147. }
  148. // source is args.at(0), destination is args.at(1)
  149. option.verbose = parser.isSet(boolOpts[0]);
  150. option.VeryVerbose = parser.isSet(boolOpts[1]);
  151. if(parser.isSet(assembly)) {
  152. option.asm1 = parser.value(assembly).toInt()==1;
  153. option.asm2 = parser.value(assembly).toInt()==2;
  154. }
  155. option.Map = parser.isSet(boolOpts[3]);
  156. option.Stats = parser.isSet(boolOpts[4]);
  157. option.Interact = false;
  158. option.Calls = parser.isSet(boolOpts[2]);
  159. option.filename = args.first();
  160. option.CustomEntryPoint = parser.value(entryPointOption).toUInt(0,16);
  161. if(parser.isSet(targetFileOption))
  162. asm1_name = asm2_name = parser.value(targetFileOption);
  163. else if(option.asm1 or option.asm2) {
  164. asm1_name = option.filename+".a1";
  165. asm2_name = option.filename+".a2";
  166. }
  167. }
  168. int main(int argc, char **argv)
  169. {
  170. QCoreApplication app(argc,argv);
  171. QCoreApplication::setApplicationVersion("0.1");
  172. setupOptions(app);
  173. /* Front end reads in EXE or COM file, parses it into I-code while
  174. * building the call graph and attaching appropriate bits of code for
  175. * each procedure.
  176. */
  177. Project::get()->create(option.filename);
  178. DccFrontend fe(&app);
  179. if(not Project::get()->load()) {
  180. return -1;
  181. }
  182. if (option.verbose)
  183. Project::get()->prog.displayLoadInfo();
  184. if(false==fe.FrontEnd ())
  185. return -1;
  186. if(option.asm1)
  187. return 0;
  188. /* In the middle is a so called Universal Decompiling Machine.
  189. * It processes the procedure list and I-code and attaches where it can
  190. * to each procedure an optimised cfg and ud lists
  191. */
  192. udm();
  193. if(option.asm2)
  194. return 0;
  195. /* Back end converts each procedure into C using I-code, interval
  196. * analysis, data flow etc. and outputs it to output file ready for
  197. * re-compilation.
  198. */
  199. BackEnd(Project::get()->callGraph);
  200. Project::get()->callGraph->write();
  201. if (option.Stats)
  202. displayTotalStats();
  203. return 0;
  204. }
  205. static void
  206. displayTotalStats ()
  207. /* Displays final statistics for the complete program */
  208. {
  209. printf ("\nFinal Program Statistics\n");
  210. printf (" Total number of low-level Icodes : %d\n", stats.totalLL);
  211. printf (" Total number of high-level Icodes: %d\n", stats.totalHL);
  212. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  213. (stats.totalHL * 100.0) / stats.totalLL);
  214. }