From 7f76c9f8c8f72bedfc889c23edaec5def5bb2411 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 21 Jan 2022 19:14:57 +0100 Subject: [PATCH] Rewrite InitHook calling, add functions to call hooks only when a condition is true --- relocator/src/entry.cpp | 40 ++++++++++++++++++---------------------- relocator/src/hooks.cpp | 12 ++++++++++++ relocator/src/hooks.h | 7 +++++-- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/relocator/src/entry.cpp b/relocator/src/entry.cpp index 80edde2..f5800f8 100644 --- a/relocator/src/entry.cpp +++ b/relocator/src/entry.cpp @@ -23,6 +23,8 @@ extern "C" void socket_lib_init(); std::vector> OrderModulesByDependencies(const std::vector> &loadedModules); +void CallInitHooksForModule(const std::shared_ptr &curModule); + extern "C" void doStart(int argc, char **argv); // We need to wrap it to make sure the main function is called AFTER our code. // The compiler tries to optimize this otherwise and calling the main function earlier @@ -175,17 +177,7 @@ extern "C" void doStart(int argc, char **argv) { for (auto &curModule: loadedModules) { if (curModule->isInitBeforeRelocationDoneHook()) { - CallHook(curModule, WUMS_HOOK_INIT_WUT_MALLOC); - CallHook(curModule, WUMS_HOOK_INIT_WUT_NEWLIB); - CallHook(curModule, WUMS_HOOK_INIT_WUT_STDCPP); - CallHook(curModule, WUMS_HOOK_INIT_WUT_DEVOPTAB); - CallHook(curModule, WUMS_HOOK_INIT_WUT_SOCKETS); - CallHook(curModule, WUMS_HOOK_INIT); - CallHook(curModule, WUMS_HOOK_FINI_WUT_SOCKETS); - CallHook(curModule, WUMS_HOOK_FINI_WUT_DEVOPTAB); - CallHook(curModule, WUMS_HOOK_FINI_WUT_STDCPP); - CallHook(curModule, WUMS_HOOK_FINI_WUT_NEWLIB); - CallHook(curModule, WUMS_HOOK_FINI_WUT_MALLOC); + CallInitHooksForModule(curModule); } } @@ -202,17 +194,7 @@ extern "C" void doStart(int argc, char **argv) { for (auto &curModule: loadedModules) { if (!curModule->isInitBeforeRelocationDoneHook()) { - CallHook(curModule, WUMS_HOOK_INIT_WUT_MALLOC); - CallHook(curModule, WUMS_HOOK_INIT_WUT_NEWLIB); - CallHook(curModule, WUMS_HOOK_INIT_WUT_STDCPP); - CallHook(curModule, WUMS_HOOK_INIT_WUT_DEVOPTAB); - CallHook(curModule, WUMS_HOOK_INIT_WUT_SOCKETS); - CallHook(curModule, WUMS_HOOK_INIT); - CallHook(curModule, WUMS_HOOK_FINI_WUT_SOCKETS); - CallHook(curModule, WUMS_HOOK_FINI_WUT_DEVOPTAB); - CallHook(curModule, WUMS_HOOK_FINI_WUT_STDCPP); - CallHook(curModule, WUMS_HOOK_FINI_WUT_NEWLIB); - CallHook(curModule, WUMS_HOOK_FINI_WUT_MALLOC); + CallInitHooksForModule(curModule); } } } else { @@ -230,6 +212,20 @@ extern "C" void doStart(int argc, char **argv) { //CallHook(loadedModules, WUMS_HOOK_FINI_WUT); } +void CallInitHooksForModule(const std::shared_ptr &curModule) { + CallHook(curModule, WUMS_HOOK_INIT_WUT_MALLOC); + CallHook(curModule, WUMS_HOOK_INIT_WUT_NEWLIB); + CallHook(curModule, WUMS_HOOK_INIT_WUT_STDCPP); + CallHook(curModule, WUMS_HOOK_INIT_WUT_DEVOPTAB); + CallHook(curModule, WUMS_HOOK_INIT_WUT_SOCKETS); + CallHook(curModule, WUMS_HOOK_INIT); + CallHook(curModule, WUMS_HOOK_FINI_WUT_SOCKETS); + CallHook(curModule, WUMS_HOOK_FINI_WUT_DEVOPTAB); + CallHook(curModule, WUMS_HOOK_FINI_WUT_STDCPP); + CallHook(curModule, WUMS_HOOK_FINI_WUT_NEWLIB); + CallHook(curModule, WUMS_HOOK_FINI_WUT_MALLOC); +} + std::vector> OrderModulesByDependencies(const std::vector> &loadedModules) { std::vector> finalOrder; std::vector loadedModulesExportNames; diff --git a/relocator/src/hooks.cpp b/relocator/src/hooks.cpp index becf6c3..0fc673d 100644 --- a/relocator/src/hooks.cpp +++ b/relocator/src/hooks.cpp @@ -21,6 +21,12 @@ static const char **hook_names = (const char *[]) { "WUMS_HOOK_APPLICATION_REQUESTS_EXIT" }; +void CallHook(const std::vector> &modules, wums_hook_type_t type, bool condition) { + if (condition) { + CallHook(modules, type); + } +} + void CallHook(const std::vector> &modules, wums_hook_type_t type) { DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s [%d] for all modules\n", hook_names[type], type); for (auto &curModule: modules) { @@ -28,6 +34,12 @@ void CallHook(const std::vector> &modules, wu } } +void CallHook(const std::shared_ptr &module, wums_hook_type_t type, bool condition) { + if (condition) { + CallHook(module, type); + } +} + void CallHook(const std::shared_ptr &module, wums_hook_type_t type) { if (!module->relocationsDone) { DEBUG_FUNCTION_LINE("Hook not called because the relocations failed\n"); diff --git a/relocator/src/hooks.h b/relocator/src/hooks.h index 40a04f3..f965c00 100644 --- a/relocator/src/hooks.h +++ b/relocator/src/hooks.h @@ -1,10 +1,13 @@ #pragma once #include - #include #include "ModuleDataMinimal.h" +void CallHook(const std::vector> &modules, wums_hook_type_t type, bool condition); + void CallHook(const std::vector> &modules, wums_hook_type_t type); -void CallHook(const std::shared_ptr &module, wums_hook_type_t type); \ No newline at end of file +void CallHook(const std::shared_ptr &module, wums_hook_type_t type, bool condition); + +void CallHook(const std::shared_ptr &module, wums_hook_type_t type);