dcc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include <QtCore/QFile>
  16. /* Global variables - extern to other modules */
  17. extern QString asm1_name, asm2_name; /* Assembler output filenames */
  18. extern SYMTAB symtab; /* Global symbol table */
  19. extern STATS stats; /* cfg statistics */
  20. extern OPTION option; /* Command line options */
  21. static char *initargs(int argc, char *argv[]);
  22. static void displayTotalStats(void);
  23. /****************************************************************************
  24. * main
  25. ***************************************************************************/
  26. void setupOptions(QCoreApplication &app) {
  27. //[-a1a2cmsi]
  28. QCommandLineParser parser;
  29. parser.setApplicationDescription("dcc");
  30. parser.addHelpOption();
  31. //parser.addVersionOption();
  32. //QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy"));
  33. QCommandLineOption boolOpts[] {
  34. QCommandLineOption {"v", QCoreApplication::translate("main", "verbose")},
  35. QCommandLineOption {"V", QCoreApplication::translate("main", "very verbose")},
  36. QCommandLineOption {"c", QCoreApplication::translate("main", "Follow register indirect calls")},
  37. QCommandLineOption {"m", QCoreApplication::translate("main", "Print memory maps of program")},
  38. QCommandLineOption {"s", QCoreApplication::translate("main", "Print stats")}
  39. };
  40. for(QCommandLineOption &o : boolOpts) {
  41. parser.addOption(o);
  42. }
  43. QCommandLineOption assembly("a", QCoreApplication::translate("main", "Produce assembly"),"assembly_level");
  44. QCommandLineOption targetFileOption(QStringList() << "o" << "output",
  45. QCoreApplication::translate("main", "Place output into <file>."),
  46. QCoreApplication::translate("main", "file"));
  47. QCommandLineOption entryPointOption(QStringList() << "E",
  48. QCoreApplication::translate("main", "Custom entry point as hex"),
  49. QCoreApplication::translate("main", "offset"),
  50. "0"
  51. );
  52. parser.addOption(targetFileOption);
  53. parser.addOption(assembly);
  54. parser.addOption(entryPointOption);
  55. //parser.addOption(forceOption);
  56. // Process the actual command line arguments given by the user
  57. parser.addPositionalArgument("source", QCoreApplication::translate("main", "Dos Executable file to decompile."));
  58. parser.process(app);
  59. const QStringList args = parser.positionalArguments();
  60. if(args.empty()) {
  61. parser.showHelp();
  62. }
  63. // source is args.at(0), destination is args.at(1)
  64. option.verbose = parser.isSet(boolOpts[0]);
  65. option.VeryVerbose = parser.isSet(boolOpts[1]);
  66. if(parser.isSet(assembly)) {
  67. option.asm1 = parser.value(assembly).toInt()==1;
  68. option.asm2 = parser.value(assembly).toInt()==2;
  69. }
  70. option.Map = parser.isSet(boolOpts[3]);
  71. option.Stats = parser.isSet(boolOpts[4]);
  72. option.Interact = false;
  73. option.Calls = parser.isSet(boolOpts[2]);
  74. option.filename = args.first();
  75. option.CustomEntryPoint = parser.value(entryPointOption).toUInt(0,16);
  76. if(parser.isSet(targetFileOption))
  77. asm1_name = asm2_name = parser.value(targetFileOption);
  78. else if(option.asm1 or option.asm2) {
  79. asm1_name = option.filename+".a1";
  80. asm2_name = option.filename+".a2";
  81. }
  82. }
  83. int main(int argc, char **argv)
  84. {
  85. QCoreApplication app(argc,argv);
  86. QCoreApplication::setApplicationVersion("0.1");
  87. setupOptions(app);
  88. /* Front end reads in EXE or COM file, parses it into I-code while
  89. * building the call graph and attaching appropriate bits of code for
  90. * each procedure.
  91. */
  92. Project::get()->create(option.filename);
  93. DccFrontend fe(&app);
  94. if(not Project::get()->load()) {
  95. return -1;
  96. }
  97. if (option.verbose)
  98. Project::get()->prog.displayLoadInfo();
  99. if(false==fe.FrontEnd ())
  100. return -1;
  101. if(option.asm1)
  102. return 0;
  103. /* In the middle is a so called Universal Decompiling Machine.
  104. * It processes the procedure list and I-code and attaches where it can
  105. * to each procedure an optimised cfg and ud lists
  106. */
  107. udm();
  108. if(option.asm2)
  109. return 0;
  110. /* Back end converts each procedure into C using I-code, interval
  111. * analysis, data flow etc. and outputs it to output file ready for
  112. * re-compilation.
  113. */
  114. BackEnd(Project::get()->callGraph);
  115. Project::get()->callGraph->write();
  116. if (option.Stats)
  117. displayTotalStats();
  118. return 0;
  119. }
  120. static void
  121. displayTotalStats ()
  122. /* Displays final statistics for the complete program */
  123. {
  124. printf ("\nFinal Program Statistics\n");
  125. printf (" Total number of low-level Icodes : %d\n", stats.totalLL);
  126. printf (" Total number of high-level Icodes: %d\n", stats.totalHL);
  127. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  128. (stats.totalHL * 100.0) / stats.totalLL);
  129. }