dcc.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // A boolean option with multiple names (-f, --force)
  130. //QCommandLineOption forceOption(QStringList() << "f" << "force", "Overwrite existing files.");
  131. // An option with a value
  132. QCommandLineOption targetFileOption(QStringList() << "o" << "output",
  133. QCoreApplication::translate("main", "Place output into <file>."),
  134. QCoreApplication::translate("main", "file"));
  135. parser.addOption(targetFileOption);
  136. parser.addOption(assembly);
  137. //parser.addOption(forceOption);
  138. // Process the actual command line arguments given by the user
  139. parser.addPositionalArgument("source", QCoreApplication::translate("main", "Dos Executable file to decompile."));
  140. parser.process(app);
  141. const QStringList args = parser.positionalArguments();
  142. if(args.empty()) {
  143. parser.showHelp();
  144. }
  145. // source is args.at(0), destination is args.at(1)
  146. option.verbose = parser.isSet(boolOpts[0]);
  147. option.VeryVerbose = parser.isSet(boolOpts[1]);
  148. if(parser.isSet(assembly)) {
  149. option.asm1 = parser.value(assembly).toInt()==1;
  150. option.asm2 = parser.value(assembly).toInt()==2;
  151. }
  152. option.Map = parser.isSet(boolOpts[3]);
  153. option.Stats = parser.isSet(boolOpts[4]);
  154. option.Interact = false;
  155. option.Calls = parser.isSet(boolOpts[2]);
  156. option.filename = args.first();
  157. if(parser.isSet(targetFileOption))
  158. asm1_name = asm2_name = parser.value(targetFileOption);
  159. else if(option.asm1 or option.asm2) {
  160. asm1_name = option.filename+".a1";
  161. asm2_name = option.filename+".a2";
  162. }
  163. }
  164. int main(int argc, char **argv)
  165. {
  166. QCoreApplication app(argc,argv);
  167. QCoreApplication::setApplicationVersion("0.1");
  168. setupOptions(app);
  169. /* Front end reads in EXE or COM file, parses it into I-code while
  170. * building the call graph and attaching appropriate bits of code for
  171. * each procedure.
  172. */
  173. Project::get()->create(option.filename);
  174. DccFrontend fe(&app);
  175. if(!Project::get()->load()) {
  176. return -1;
  177. }
  178. if (option.verbose)
  179. Project::get()->prog.displayLoadInfo();
  180. if(false==fe.FrontEnd ())
  181. return -1;
  182. if(option.asm1)
  183. return 0;
  184. /* In the middle is a so called Universal Decompiling Machine.
  185. * It processes the procedure list and I-code and attaches where it can
  186. * to each procedure an optimised cfg and ud lists
  187. */
  188. udm();
  189. if(option.asm2)
  190. return 0;
  191. /* Back end converts each procedure into C using I-code, interval
  192. * analysis, data flow etc. and outputs it to output file ready for
  193. * re-compilation.
  194. */
  195. BackEnd(Project::get()->callGraph);
  196. Project::get()->callGraph->write();
  197. if (option.Stats)
  198. displayTotalStats();
  199. return 0;
  200. }
  201. static void
  202. displayTotalStats ()
  203. /* Displays final statistics for the complete program */
  204. {
  205. printf ("\nFinal Program Statistics\n");
  206. printf (" Total number of low-level Icodes : %d\n", stats.totalLL);
  207. printf (" Total number of high-level Icodes: %d\n", stats.totalHL);
  208. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  209. (stats.totalHL * 100.0) / stats.totalLL);
  210. }