dcc.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <QtCore/QCoreApplication>
  7. #include <QCommandLineParser>
  8. #include <cstring>
  9. #include "dcc.h"
  10. #include "project.h"
  11. #include "CallGraph.h"
  12. /* Global variables - extern to other modules */
  13. extern std::string asm1_name, asm2_name; /* Assembler output filenames */
  14. extern SYMTAB symtab; /* Global symbol table */
  15. extern STATS stats; /* cfg statistics */
  16. //PROG prog; /* programs fields */
  17. extern OPTION option; /* Command line options */
  18. //Function * pProcList; /* List of procedures, topologically sort */
  19. //Function * pLastProc; /* Pointer to last node in procedure list */
  20. //FunctionListType pProcList;
  21. //CALL_GRAPH *callGraph; /* Call graph of the program */
  22. static char *initargs(int argc, char *argv[]);
  23. static void displayTotalStats(void);
  24. #include <llvm/Support/raw_os_ostream.h>
  25. #include <llvm/Support/CommandLine.h>
  26. #include <llvm/Support/TargetSelect.h>
  27. #include <llvm/Support/TargetRegistry.h>
  28. #include <llvm/Support/PrettyStackTrace.h>
  29. #include <llvm/Support/Signals.h>
  30. #include <llvm/Support/Host.h>
  31. #include <llvm/Target/TargetMachine.h>
  32. #include <llvm/Target/TargetInstrInfo.h>
  33. #include <llvm/MC/MCAsmInfo.h>
  34. #include <llvm/CodeGen/MachineInstrBuilder.h>
  35. #include <llvm/TableGen/Main.h>
  36. #include <llvm/TableGen/TableGenBackend.h>
  37. #include <llvm/TableGen/Record.h>
  38. /****************************************************************************
  39. * main
  40. ***************************************************************************/
  41. #include <QtCore/QFile>
  42. #include <iostream>
  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. 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().toStdString();
  156. if(parser.isSet(targetFileOption))
  157. asm1_name = asm2_name = parser.value(targetFileOption).toStdString();
  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. DccFrontend fe(option.filename);
  173. if(false==fe.FrontEnd ())
  174. return -1;
  175. if(option.asm1)
  176. return 0;
  177. /* In the middle is a so called Universal Decompiling Machine.
  178. * It processes the procedure list and I-code and attaches where it can
  179. * to each procedure an optimised cfg and ud lists
  180. */
  181. udm();
  182. if(option.asm2)
  183. return 0;
  184. /* Back end converts each procedure into C using I-code, interval
  185. * analysis, data flow etc. and outputs it to output file ready for
  186. * re-compilation.
  187. */
  188. BackEnd(!asm1_name.empty() ? asm1_name:option.filename, Project::get()->callGraph);
  189. Project::get()->callGraph->write();
  190. if (option.Stats)
  191. displayTotalStats();
  192. return 0;
  193. }
  194. static void
  195. displayTotalStats ()
  196. /* Displays final statistics for the complete program */
  197. {
  198. printf ("\nFinal Program Statistics\n");
  199. printf (" Total number of low-level Icodes : %d\n", stats.totalLL);
  200. printf (" Total number of high-level Icodes: %d\n", stats.totalHL);
  201. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  202. (stats.totalHL * 100.0) / stats.totalLL);
  203. }