Rewrite InitHook calling, add functions to call hooks only when a condition is true

This commit is contained in:
Maschell 2022-01-21 19:14:57 +01:00
parent 8eeb8d95dc
commit 7f76c9f8c8
3 changed files with 35 additions and 24 deletions

View File

@ -23,6 +23,8 @@ extern "C" void socket_lib_init();
std::vector<std::shared_ptr<ModuleDataMinimal>> OrderModulesByDependencies(const std::vector<std::shared_ptr<ModuleDataMinimal>> &loadedModules);
void CallInitHooksForModule(const std::shared_ptr<ModuleDataMinimal> &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<ModuleDataMinimal> &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<std::shared_ptr<ModuleDataMinimal>> OrderModulesByDependencies(const std::vector<std::shared_ptr<ModuleDataMinimal>> &loadedModules) {
std::vector<std::shared_ptr<ModuleDataMinimal>> finalOrder;
std::vector<std::string> loadedModulesExportNames;

View File

@ -21,6 +21,12 @@ static const char **hook_names = (const char *[]) {
"WUMS_HOOK_APPLICATION_REQUESTS_EXIT"
};
void CallHook(const std::vector<std::shared_ptr<ModuleDataMinimal>> &modules, wums_hook_type_t type, bool condition) {
if (condition) {
CallHook(modules, type);
}
}
void CallHook(const std::vector<std::shared_ptr<ModuleDataMinimal>> &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<std::shared_ptr<ModuleDataMinimal>> &modules, wu
}
}
void CallHook(const std::shared_ptr<ModuleDataMinimal> &module, wums_hook_type_t type, bool condition) {
if (condition) {
CallHook(module, type);
}
}
void CallHook(const std::shared_ptr<ModuleDataMinimal> &module, wums_hook_type_t type) {
if (!module->relocationsDone) {
DEBUG_FUNCTION_LINE("Hook not called because the relocations failed\n");

View File

@ -1,10 +1,13 @@
#pragma once
#include <wums.h>
#include <vector>
#include "ModuleDataMinimal.h"
void CallHook(const std::vector<std::shared_ptr<ModuleDataMinimal>> &modules, wums_hook_type_t type, bool condition);
void CallHook(const std::vector<std::shared_ptr<ModuleDataMinimal>> &modules, wums_hook_type_t type);
void CallHook(const std::shared_ptr<ModuleDataMinimal> &module, wums_hook_type_t type);
void CallHook(const std::shared_ptr<ModuleDataMinimal> &module, wums_hook_type_t type, bool condition);
void CallHook(const std::shared_ptr<ModuleDataMinimal> &module, wums_hook_type_t type);