dcc.cpp 8.0 KB

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