dcc.cpp 5.5 KB

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