From 98c8de747946fb868418d749c8ea190144f483d6 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 26 Dec 2020 16:01:46 +0100 Subject: [PATCH] Implementation of InitBeforeRelocationDoneHook and SkipEntrypoint module options --- relocator/src/ModuleDataPersistence.cpp | 3 ++- relocator/src/entry.cpp | 23 +++++++++++------------ source/module/ModuleData.h | 18 +++++++++++++----- source/module/ModuleDataFactory.cpp | 11 ++++++++--- source/module/ModuleDataPersistence.cpp | 6 ++++-- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/relocator/src/ModuleDataPersistence.cpp b/relocator/src/ModuleDataPersistence.cpp index 9a4a633..bc901ed 100644 --- a/relocator/src/ModuleDataPersistence.cpp +++ b/relocator/src/ModuleDataPersistence.cpp @@ -31,7 +31,8 @@ std::vector ModuleDataPersistence::loadModuleData(module_information moduleData.setEntrypoint(module_data->entrypoint); moduleData.setStartAddress(module_data->startAddress); moduleData.setEndAddress(module_data->endAddress); - moduleData.setInitBeforeEntrypoint(module_data->initBeforeEntrypoint); + moduleData.setSkipEntrypoint(module_data->skipEntrypoint); + moduleData.setInitBeforeRelocationDoneHook(module_data->initBeforeRelocationDoneHook); moduleData.setExportName(module_data->module_export_name); diff --git a/relocator/src/entry.cpp b/relocator/src/entry.cpp index f8002b1..615eede 100644 --- a/relocator/src/entry.cpp +++ b/relocator/src/entry.cpp @@ -1,19 +1,16 @@ #include #include -#include -#include +#include +#include #include #include #include -#include #include #include #include "../../source/module/RelocationData.h" #include "../../source/module/ModuleData.h" #include "ModuleDataPersistence.h" #include "ElfUtils.h" - -#include "utils/logger.h" #include "utils/dynamic.h" #include "globals.h" #include "hooks.h" @@ -125,24 +122,25 @@ extern "C" void doStart(int argc, char **argv) { DEBUG_FUNCTION_LINE("Resolve relocations without replacing alloc functions\n"); ResolveRelocations(loadedModules); - DEBUG_FUNCTION_LINE("Try to call kernel init\n"); - // Call init hook of kernel for (auto &curModule : loadedModules) { - if (curModule.isInitBeforeEntrypoint()) { + if (curModule.isInitBeforeRelocationDoneHook()) { CallHook(curModule, WUMS_HOOK_INIT); } } + DEBUG_FUNCTION_LINE("Relocations done\n"); CallHook(loadedModules, WUMS_HOOK_RELOCATIONS_DONE); for (int i = 0; i < gModuleData->number_used_modules; i++) { - DEBUG_FUNCTION_LINE("About to call %08X\n", gModuleData->module_data[i].entrypoint); - int ret = ((int (*)(int, char **)) (gModuleData->module_data[i].entrypoint))(argc, argv); - DEBUG_FUNCTION_LINE("return code was %d\n", ret); + if (!gModuleData->module_data[i].skipEntrypoint) { + DEBUG_FUNCTION_LINE("About to call %08X\n", gModuleData->module_data[i].entrypoint); + int ret = ((int (*)(int, char **)) (gModuleData->module_data[i].entrypoint))(argc, argv); + DEBUG_FUNCTION_LINE("return code was %d\n", ret); + } } for (auto &curModule : loadedModules) { - if (!curModule.isInitBeforeEntrypoint()) { + if (!curModule.isInitBeforeRelocationDoneHook()) { CallHook(curModule, WUMS_HOOK_INIT); } } @@ -152,6 +150,7 @@ extern "C" void doStart(int argc, char **argv) { CallHook(loadedModules, WUMS_HOOK_RELOCATIONS_DONE); } + // TODO: Implement Application ends hook // CallHook(loadedModules, WUMS_HOOK_FINI_WUT); // CallHook(loadedModules, WUMS_HOOK_INIT_WUT); diff --git a/source/module/ModuleData.h b/source/module/ModuleData.h index 6d5f8fa..77f2e0e 100644 --- a/source/module/ModuleData.h +++ b/source/module/ModuleData.h @@ -132,12 +132,19 @@ public: return this->export_name; } - bool isInitBeforeEntrypoint() const { - return this->initBeforeEntrypoint; + bool isSkipEntrypoint() const { + return this->skipEntrypoint; } - void setInitBeforeEntrypoint(bool value) { - this->initBeforeEntrypoint = value; + bool isInitBeforeRelocationDoneHook() const { + return this->initBeforeRelocationDoneHook; + } + + void setSkipEntrypoint(bool value) { + this->skipEntrypoint = value; + } + void setInitBeforeRelocationDoneHook(bool value) { + this->initBeforeRelocationDoneHook = value; } private: @@ -155,5 +162,6 @@ private: uint32_t startAddress = 0; uint32_t endAddress = 0; uint32_t entrypoint = 0; - bool initBeforeEntrypoint = false; + bool skipEntrypoint = false; + bool initBeforeRelocationDoneHook = false; }; diff --git a/source/module/ModuleDataFactory.cpp b/source/module/ModuleDataFactory.cpp index 3142077..1cfd094 100644 --- a/source/module/ModuleDataFactory.cpp +++ b/source/module/ModuleDataFactory.cpp @@ -189,10 +189,15 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de if (key.compare("export_name") == 0) { DEBUG_FUNCTION_LINE("export_name = %s", value.c_str()); moduleData.setExportName(value); - } else if (key.compare("initBeforeEntrypoint") == 0) { + } else if (key.compare("skipEntrypoint") == 0) { if (value.compare("true") == 0) { - DEBUG_FUNCTION_LINE("initBeforeEntrypoint = %s", value.c_str()); - moduleData.setInitBeforeEntrypoint(true); + DEBUG_FUNCTION_LINE("skipEntrypoint = %s", value.c_str()); + moduleData.setSkipEntrypoint(true); + } + } else if (key.compare("initBeforeRelocationDoneHook") == 0) { + if (value.compare("true") == 0) { + DEBUG_FUNCTION_LINE("initBeforeRelocationDoneHook = %s", value.c_str()); + moduleData.setInitBeforeRelocationDoneHook(true); } } if (key.compare("wums") == 0) { diff --git a/source/module/ModuleDataPersistence.cpp b/source/module/ModuleDataPersistence.cpp index 68bb6b9..3820b3f 100644 --- a/source/module/ModuleDataPersistence.cpp +++ b/source/module/ModuleDataPersistence.cpp @@ -71,7 +71,8 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati module_data->startAddress = module.getStartAddress(); module_data->endAddress = module.getEndAddress(); module_data->entrypoint = module.getEntrypoint(); - module_data->initBeforeEntrypoint = module.isInitBeforeEntrypoint(); + module_data->skipEntrypoint = module.isSkipEntrypoint(); + module_data->initBeforeRelocationDoneHook = module.isInitBeforeRelocationDoneHook(); moduleInformation->number_used_modules++; @@ -105,7 +106,8 @@ std::vector ModuleDataPersistence::loadModuleData(module_information moduleData.setStartAddress(module_data->startAddress); moduleData.setEndAddress(module_data->endAddress); moduleData.setExportName(module_data->module_export_name); - moduleData.setInitBeforeEntrypoint(module_data->initBeforeEntrypoint); + moduleData.setSkipEntrypoint(module_data->skipEntrypoint); + moduleData.setInitBeforeRelocationDoneHook(module_data->initBeforeRelocationDoneHook); for (uint32_t j = 0; j < EXPORT_ENTRY_LIST_LENGTH; j++) { export_data_t *export_entry = &(module_data->export_entries[j]);