dcc.cpp 8.1 KB

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