From 74f7b8a66290cd4bc4b46370a083686b784fbbcf Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 17 Sep 2021 16:22:54 +0200 Subject: [PATCH] Implement support for WUMS 0.2 --- Dockerfile | 4 +- relocator/src/ModuleDataPersistence.cpp | 8 ++-- relocator/src/entry.cpp | 32 +++++++++++--- relocator/src/hooks.cpp | 42 +++++++++++++----- relocator/src/utils/logger.h | 4 +- source/module/ModuleData.h | 9 ---- source/module/ModuleDataFactory.cpp | 47 ++++++++------------ source/module/ModuleDataFactory.h | 2 +- source/module/ModuleDataPersistence.cpp | 58 ++++++++++--------------- source/utils/logger.h | 4 ++ source/utils/utils.c | 21 ++++----- 11 files changed, 118 insertions(+), 113 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5e00827..4a51634 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ -FROM wiiuenv/devkitppc:20210414 +FROM wiiuenv/devkitppc:20210917 -COPY --from=wiiuenv/wiiumodulesystem:20210219 /artifacts $DEVKITPRO +COPY --from=wiiuenv/wiiumodulesystem:20210917 /artifacts $DEVKITPRO WORKDIR project \ No newline at end of file diff --git a/relocator/src/ModuleDataPersistence.cpp b/relocator/src/ModuleDataPersistence.cpp index 7d818fa..f28539f 100644 --- a/relocator/src/ModuleDataPersistence.cpp +++ b/relocator/src/ModuleDataPersistence.cpp @@ -30,7 +30,6 @@ std::vector ModuleDataPersistence::loadModuleData(module_information moduleData.setStartAddress(module_data->startAddress); moduleData.setEndAddress(module_data->endAddress); moduleData.setSkipEntrypoint(module_data->skipEntrypoint); - moduleData.setSkipWUTInit(module_data->skipWUTInit); moduleData.setInitBeforeRelocationDoneHook(module_data->initBeforeRelocationDoneHook); moduleData.setExportName(module_data->module_export_name); @@ -42,12 +41,11 @@ std::vector ModuleDataPersistence::loadModuleData(module_information moduleData.addExportData(ExportData(static_cast(export_entry.type), export_entry.name, reinterpret_cast(export_entry.address))); } - for (auto & hook_entrie : module_data->hook_entries) { - hook_data_t *hook_entry = &hook_entrie; - if (hook_entry->target == 0) { + for (auto & hook_entry : module_data->hook_entries) { + if (hook_entry.target == 0) { continue; } - moduleData.addHookData(HookData(static_cast(hook_entry->type), reinterpret_cast(hook_entry->target))); + moduleData.addHookData(HookData(static_cast(hook_entry.type), reinterpret_cast(hook_entry.target))); } for (auto & linking_entry : module_data->linking_entries) { diff --git a/relocator/src/entry.cpp b/relocator/src/entry.cpp index b7d5cb8..a0ef110 100644 --- a/relocator/src/entry.cpp +++ b/relocator/src/entry.cpp @@ -166,7 +166,7 @@ extern "C" void doStart(int argc, char **argv) { // Make sure WUMS_HOOK_APPLICATION_ENDS and WUMS_HOOK_FINI_WUT are called for (auto &curModule : loadedModules) { for (auto &curHook : curModule.getHookDataList()) { - if (curHook.getType() == WUMS_HOOK_APPLICATION_ENDS || curHook.getType() == WUMS_HOOK_FINI_WUT) { + if (curHook.getType() == WUMS_HOOK_APPLICATION_ENDS || curHook.getType() == WUMS_HOOK_FINI_WUT_DEVOPTAB) { if (!applicationEndHookLoaded) { OSFatal_printf("%s requires module homebrew_applicationendshook", curModule.getExportName().c_str()); } @@ -183,9 +183,17 @@ extern "C" void doStart(int argc, char **argv) { for (auto &curModule : loadedModules) { if (curModule.isInitBeforeRelocationDoneHook()) { - CallHook(loadedModules, WUMS_HOOK_INIT_WUT); + 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(loadedModules, WUMS_HOOK_FINI_WUT); + 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); } } @@ -203,9 +211,17 @@ extern "C" void doStart(int argc, char **argv) { for (auto &curModule : loadedModules) { if (!curModule.isInitBeforeRelocationDoneHook()) { - CallHook(loadedModules, WUMS_HOOK_INIT_WUT); + 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(loadedModules, WUMS_HOOK_FINI_WUT); + 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); } } } else { @@ -213,7 +229,11 @@ extern "C" void doStart(int argc, char **argv) { ResolveRelocations(loadedModules, false); CallHook(loadedModules, WUMS_HOOK_RELOCATIONS_DONE); } - CallHook(loadedModules, WUMS_HOOK_INIT_WUT); + CallHook(loadedModules, WUMS_HOOK_INIT_WUT_MALLOC); + CallHook(loadedModules, WUMS_HOOK_INIT_WUT_NEWLIB); + CallHook(loadedModules, WUMS_HOOK_INIT_WUT_STDCPP); + CallHook(loadedModules, WUMS_HOOK_INIT_WUT_DEVOPTAB); + CallHook(loadedModules, WUMS_HOOK_INIT_WUT_SOCKETS); CallHook(loadedModules, WUMS_HOOK_APPLICATION_STARTS); //CallHook(loadedModules, WUMS_HOOK_FINI_WUT); } diff --git a/relocator/src/hooks.cpp b/relocator/src/hooks.cpp index 1e5f163..db42c1c 100644 --- a/relocator/src/hooks.cpp +++ b/relocator/src/hooks.cpp @@ -3,12 +3,22 @@ #include "globals.h" static const char **hook_names = (const char *[]) { + "WUMS_HOOK_INIT_WUT_MALLOC", + "WUMS_HOOK_FINI_WUT_MALLOC", + "WUMS_HOOK_INIT_WUT_NEWLIB", + "WUMS_HOOK_FINI_WUT_NEWLIB", + "WUMS_HOOK_INIT_WUT_STDCPP", + "WUMS_HOOK_FINI_WUT_STDCPP", + "WUMS_HOOK_INIT_WUT_DEVOPTAB", + "WUMS_HOOK_FINI_WUT_DEVOPTAB", + "WUMS_HOOK_INIT_WUT_SOCKETS", + "WUMS_HOOK_FINI_WUT_SOCKETS", + "WUMS_HOOK_INIT", "WUMS_HOOK_APPLICATION_STARTS", "WUMS_HOOK_APPLICATION_ENDS", - "WUMS_HOOK_INIT_WUT", - "WUMS_HOOK_FINI_WUT", - "WUMS_HOOK_RELOCATIONS_DONE"}; + "WUMS_HOOK_RELOCATIONS_DONE", + "WUMS_HOOK_APPLICATION_REQUESTS_EXI"}; 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); @@ -23,11 +33,6 @@ void CallHook(const ModuleData &module, wums_hook_type_t type) { return; } - if ((type == WUMS_HOOK_INIT_WUT || type == WUMS_HOOK_FINI_WUT) && module.isSkipWUTInit()) { - DEBUG_FUNCTION_LINE_VERBOSE("Skip WUMS_HOOK_INIT_WUT/WUMS_HOOK_FINI_WUT for %s\n", module.getExportName().c_str()); - return; - } - for (auto &curHook : module.getHookDataList()) { auto func_ptr = (uint32_t) curHook.getTarget(); if (func_ptr == 0) { @@ -38,17 +43,30 @@ void CallHook(const ModuleData &module, wums_hook_type_t type) { if (type == curHook.getType()) { if ((type == WUMS_HOOK_APPLICATION_STARTS || type == WUMS_HOOK_APPLICATION_ENDS || - type == WUMS_HOOK_INIT_WUT || - type == WUMS_HOOK_FINI_WUT)) { - DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s [%d] %d for %s \n", hook_names[type], type, curHook.getType(), module.getExportName().c_str()); + type == WUMS_HOOK_INIT_WUT_MALLOC || + type == WUMS_HOOK_FINI_WUT_MALLOC || + type == WUMS_HOOK_INIT_WUT_NEWLIB || + type == WUMS_HOOK_FINI_WUT_NEWLIB || + type == WUMS_HOOK_INIT_WUT_STDCPP || + type == WUMS_HOOK_FINI_WUT_STDCPP || + type == WUMS_HOOK_INIT_WUT_DEVOPTAB || + type == WUMS_HOOK_FINI_WUT_DEVOPTAB || + type == WUMS_HOOK_INIT_WUT_SOCKETS || + type == WUMS_HOOK_FINI_WUT_SOCKETS + )) { + DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s [%d] %d for %s: %08X\n", hook_names[type], type, curHook.getType(), module.getExportName().c_str(), curHook.getTarget()); ((void (*)()) ((uint32_t *) func_ptr))(); break; } else if (type == WUMS_HOOK_INIT || type == WUMS_HOOK_RELOCATIONS_DONE) { - DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s [%d] %d for %s\n", hook_names[type], type, curHook.getType(), module.getExportName().c_str(), gModuleData); + DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s [%d] %d for %s: %08X\n", hook_names[type], type, curHook.getType(), module.getExportName().c_str(), curHook.getTarget()); wums_app_init_args_t args; args.module_information = gModuleData; ((void (*)(wums_app_init_args_t *)) ((uint32_t *) func_ptr))(&args); + } else { + DEBUG_FUNCTION_LINE("#########################################\n"); + DEBUG_FUNCTION_LINE("#########HOOK NOT IMPLEMENTED %d#########\n", type); + DEBUG_FUNCTION_LINE("#########################################\n"); } break; } diff --git a/relocator/src/utils/logger.h b/relocator/src/utils/logger.h index 6d44221..74f239d 100644 --- a/relocator/src/utils/logger.h +++ b/relocator/src/utils/logger.h @@ -28,7 +28,9 @@ void OSFatal_printf(const char *format, ...); log_printf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ } while (0) -#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while(0) +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...)do { \ + log_printf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ + } while (0) #ifdef __cplusplus } diff --git a/source/module/ModuleData.h b/source/module/ModuleData.h index 5c36839..3a7f31d 100644 --- a/source/module/ModuleData.h +++ b/source/module/ModuleData.h @@ -138,10 +138,6 @@ public: return this->initBeforeRelocationDoneHook; } - [[nodiscard]] bool isSkipWUTInit() const { - return this->skipWUTInit; - } - void setSkipEntrypoint(bool value) { this->skipEntrypoint = value; } @@ -149,10 +145,6 @@ public: this->initBeforeRelocationDoneHook = value; } - void setSkipWUTInit(bool value) { - this->skipWUTInit = value; - } - bool relocationsDone = false; private: std::vector relocation_data_list; @@ -171,5 +163,4 @@ private: uint32_t entrypoint = 0; bool skipEntrypoint = false; bool initBeforeRelocationDoneHook = false; - bool skipWUTInit = false; }; diff --git a/source/module/ModuleDataFactory.cpp b/source/module/ModuleDataFactory.cpp index dcf3e70..312ce54 100644 --- a/source/module/ModuleDataFactory.cpp +++ b/source/module/ModuleDataFactory.cpp @@ -21,16 +21,12 @@ #include #include #include "ModuleDataFactory.h" -#include "elfio/elfio.hpp" #include "utils/utils.h" #include "ElfUtils.h" -#include "SectionInfo.h" -#include "ExportData.h" -#include "HookData.h" using namespace ELFIO; -std::optional ModuleDataFactory::load(std::string path, uint32_t *destination_address_ptr, uint32_t maximum_size, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) { +std::optional ModuleDataFactory::load(const std::string& path, uint32_t *destination_address_ptr, uint32_t maximum_size, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) { elfio reader; ModuleData moduleData; @@ -42,7 +38,7 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de uint32_t sec_num = reader.sections.size(); - uint8_t **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num); + auto **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num); uint32_t baseOffset = *destination_address_ptr; @@ -69,7 +65,7 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de return {}; } - uint32_t address = (uint32_t) psec->get_address(); + auto address = (uint32_t) psec->get_address(); destinations[psec->get_index()] = (uint8_t *) baseOffset; @@ -102,18 +98,17 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de } //nextAddress = ROUNDUP(destination + sectionSize,0x100); - if (psec->get_name().compare(".bss") == 0) { + if (psec->get_name() == ".bss") { moduleData.setBSSLocation(destination, sectionSize); - DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize); - } else if (psec->get_name().compare(".sbss") == 0) { + memset(reinterpret_cast(destination), 0, sectionSize); + } else if (psec->get_name() == ".sbss") { moduleData.setSBSSLocation(destination, sectionSize); - DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize); + memset(reinterpret_cast(destination), 0, sectionSize); } moduleData.addSectionInfo(SectionInfo(psec->get_name(), destination, sectionSize)); DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize); - if (endAddress < destination + sectionSize) { endAddress = destination + sectionSize; } @@ -143,8 +138,8 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de std::optional secInfo = moduleData.getSectionInfo(".wums.exports"); if (secInfo && secInfo->getSize() > 0) { size_t entries_count = secInfo->getSize() / sizeof(wums_entry_t); - wums_entry_t *entries = (wums_entry_t *) secInfo->getAddress(); - if (entries != NULL) { + auto *entries = (wums_entry_t *) secInfo->getAddress(); + if (entries != nullptr) { for (size_t j = 0; j < entries_count; j++) { wums_entry_t *exp = &entries[j]; DEBUG_FUNCTION_LINE("Saving export of type %08X, name %s, target: %08X"/*,pluginData.getPluginInformation()->getName().c_str()*/, exp->type, exp->name, (void *) exp->address); @@ -157,8 +152,8 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de secInfo = moduleData.getSectionInfo(".wums.hooks"); if (secInfo && secInfo->getSize() > 0) { size_t entries_count = secInfo->getSize() / sizeof(wums_hook_t); - wums_hook_t *hooks = (wums_hook_t *) secInfo->getAddress(); - if (hooks != NULL) { + auto *hooks = (wums_hook_t *) secInfo->getAddress(); + if (hooks != nullptr) { for (size_t j = 0; j < entries_count; j++) { wums_hook_t *hook = &hooks[j]; DEBUG_FUNCTION_LINE("Saving hook of type %08X, target: %08X"/*,pluginData.getPluginInformation()->getName().c_str()*/, hook->type, hook->target); @@ -170,8 +165,8 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de secInfo = moduleData.getSectionInfo(".wums.meta"); if (secInfo && secInfo->getSize() > 0) { - wums_entry_t *entries = (wums_entry_t *) secInfo->getAddress(); - if (entries != NULL) { + auto *entries = (wums_entry_t *) secInfo->getAddress(); + if (entries != nullptr) { char *curEntry = (char *) secInfo->getAddress(); while ((uint32_t) curEntry < (uint32_t) secInfo->getAddress() + secInfo->getSize()) { @@ -180,7 +175,7 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de continue; } - auto firstFound = std::string(curEntry).find_first_of("="); + auto firstFound = std::string(curEntry).find_first_of('='); if (firstFound != std::string::npos) { curEntry[firstFound] = '\0'; std::string key(curEntry); @@ -203,16 +198,8 @@ std::optional ModuleDataFactory::load(std::string path, uint32_t *de } else { moduleData.setInitBeforeRelocationDoneHook(false); } - } else if (key == "skipwutInit") { - if (value == "true") { - DEBUG_FUNCTION_LINE("skipwutInit = %s", value.c_str()); - moduleData.setSkipWUTInit(true); - } else { - moduleData.setSkipWUTInit(false); - } - } - if (key == "wums") { - if (value != "0.1") { + } else if (key == "wums") { + if (value != "0.2") { DEBUG_FUNCTION_LINE("Warning: Ignoring module - Unsupported WUMS version: %s.\n", value.c_str()); return std::nullopt; } @@ -314,7 +301,7 @@ bool ModuleDataFactory::linkSection(elfio &reader, uint32_t section_index, uint3 break; } - uint32_t adjusted_sym_value = (uint32_t) sym_value; + auto adjusted_sym_value = (uint32_t) sym_value; if ((adjusted_sym_value >= 0x02000000) && adjusted_sym_value < 0x10000000) { adjusted_sym_value -= 0x02000000; adjusted_sym_value += base_text; diff --git a/source/module/ModuleDataFactory.h b/source/module/ModuleDataFactory.h index 2306ad7..9f15faf 100644 --- a/source/module/ModuleDataFactory.h +++ b/source/module/ModuleDataFactory.h @@ -26,7 +26,7 @@ class ModuleDataFactory { public: - static std::optional load(std::string path, uint32_t *destination_address_ptr, uint32_t maximum_size, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length); + static std::optional load(const std::string& path, uint32_t *destination_address_ptr, uint32_t maximum_size, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length); static bool linkSection(ELFIO::elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length); diff --git a/source/module/ModuleDataPersistence.cpp b/source/module/ModuleDataPersistence.cpp index d715daa..acc0537 100644 --- a/source/module/ModuleDataPersistence.cpp +++ b/source/module/ModuleDataPersistence.cpp @@ -28,12 +28,12 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati std::vector exportData = module.getExportDataList(); for (auto const &curExport : exportData) { bool found = false; - for (uint32_t j = 0; j < EXPORT_ENTRY_LIST_LENGTH; j++) { - export_data_t *export_entry = &(module_data->export_entries[j]); - if (export_entry->address == 0) { - export_entry->type = curExport.getType(); - strncpy(export_entry->name, curExport.getName().c_str(), EXPORT_MAXIMUM_NAME_LENGTH); - export_entry->address = (uint32_t) curExport.getAddress(); + for (auto &export_entry : module_data->export_entries) { + if (export_entry.address == 0) { + export_entry.type = curExport.getType(); + export_entry.name[0] = '\0'; + strncat(export_entry.name, curExport.getName().c_str(), sizeof(export_entry.name) - 1); + export_entry.address = (uint32_t) curExport.getAddress(); found = true; break; } @@ -47,11 +47,10 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati std::vector hookData = module.getHookDataList(); for (auto const &curHook : hookData) { bool found = false; - for (uint32_t j = 0; j < HOOK_ENTRY_LIST_LENGTH; j++) { - hook_data_t *hook_entry = &(module_data->hook_entries[j]); - if (hook_entry->target == 0) { - hook_entry->type = curHook.getType(); - hook_entry->target = (uint32_t) curHook.getTarget(); + for (auto &hook_entry : module_data->hook_entries) { + if (hook_entry.target == 0) { + hook_entry.type = curHook.getType(); + hook_entry.target = (uint32_t) curHook.getTarget(); found = true; break; } @@ -73,7 +72,6 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati module_data->entrypoint = module.getEntrypoint(); module_data->skipEntrypoint = module.isSkipEntrypoint(); module_data->initBeforeRelocationDoneHook = module.isInitBeforeRelocationDoneHook(); - module_data->skipWUTInit = module.isSkipWUTInit(); moduleInformation->number_used_modules++; @@ -85,7 +83,7 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati std::vector ModuleDataPersistence::loadModuleData(module_information_t *moduleInformation) { std::vector result; - if (moduleInformation == NULL) { + if (moduleInformation == nullptr) { DEBUG_FUNCTION_LINE("moduleInformation == NULL\n"); return result; } @@ -110,48 +108,38 @@ std::vector ModuleDataPersistence::loadModuleData(module_information 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]); + for (auto &export_entrie : module_data->export_entries) { + export_data_t *export_entry = &export_entrie; if (export_entry->address == 0) { continue; } moduleData.addExportData(ExportData(static_cast(export_entry->type), export_entry->name, reinterpret_cast(export_entry->address))); } - for (uint32_t j = 0; j < HOOK_ENTRY_LIST_LENGTH; j++) { - hook_data_t *hook_entry = &(module_data->hook_entries[j]); - if (hook_entry->target == 0) { + for (auto &hook_entry : module_data->hook_entries) { + if (hook_entry.target == 0) { continue; } - moduleData.addHookData(HookData(static_cast(hook_entry->type), reinterpret_cast(hook_entry->target))); + moduleData.addHookData(HookData(static_cast(hook_entry.type), reinterpret_cast(hook_entry.target))); } - for (uint32_t j = 0; j < DYN_LINK_RELOCATION_LIST_LENGTH; j++) { - dyn_linking_relocation_entry_t *linking_entry = &(module_data->linking_entries[j]); - if (linking_entry->destination == 0) { + for (auto &linking_entry : module_data->linking_entries) { + if (linking_entry.destination == nullptr) { break; } - dyn_linking_import_t *importEntry = linking_entry->importEntry; - if (importEntry == NULL) { + dyn_linking_import_t *importEntry = linking_entry.importEntry; + if (importEntry == nullptr) { DEBUG_FUNCTION_LINE("importEntry was NULL, skipping relocation entry\n"); continue; } - if (importEntry->importName == NULL) { - DEBUG_FUNCTION_LINE("importEntry->importName was NULL, skipping relocation entry\n"); - continue; - } - dyn_linking_function_t *functionEntry = linking_entry->functionEntry; + dyn_linking_function_t *functionEntry = linking_entry.functionEntry; - if (functionEntry == NULL) { + if (functionEntry == nullptr) { DEBUG_FUNCTION_LINE("functionEntry was NULL, skipping relocation entry\n"); continue; } - if (functionEntry->functionName == NULL) { - DEBUG_FUNCTION_LINE("functionEntry->functionName was NULL, skipping relocation entry\n"); - continue; - } ImportRPLInformation rplInfo(importEntry->importName, importEntry->isData); - RelocationData reloc(linking_entry->type, linking_entry->offset, linking_entry->addend, linking_entry->destination, functionEntry->functionName, rplInfo); + RelocationData reloc(linking_entry.type, linking_entry.offset, linking_entry.addend, linking_entry.destination, functionEntry->functionName, rplInfo); moduleData.addRelocationData(reloc); } diff --git a/source/utils/logger.h b/source/utils/logger.h index 9b5195f..efeda1b 100644 --- a/source/utils/logger.h +++ b/source/utils/logger.h @@ -14,6 +14,10 @@ extern "C" { WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ } while (0) +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ + WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ + } while (0) + #ifdef __cplusplus } #endif diff --git a/source/utils/utils.c b/source/utils/utils.c index 17c597b..29ff0a1 100644 --- a/source/utils/utils.c +++ b/source/utils/utils.c @@ -1,9 +1,6 @@ #include -#include -#include #include -#include -#include +#include #include "utils/logger.h" // https://gist.github.com/ccbrown/9722406 @@ -11,30 +8,30 @@ void dumpHex(const void *data, size_t size) { char ascii[17]; size_t i, j; ascii[16] = '\0'; - DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data); + DEBUG_FUNCTION_LINE_WRITE("0x%08X (0x0000): ", data); for (i = 0; i < size; ++i) { - WHBLogPrintf("%02X ", ((unsigned char *) data)[i]); + WHBLogWritef("%02X ", ((unsigned char *) data)[i]); if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') { ascii[i % 16] = ((unsigned char *) data)[i]; } else { ascii[i % 16] = '.'; } if ((i + 1) % 8 == 0 || i + 1 == size) { - WHBLogPrintf(" "); + WHBLogWritef(" "); if ((i + 1) % 16 == 0) { - WHBLogPrintf("| %s \n", ascii); + WHBLogPrintf("| %s ", ascii); if (i + 1 < size) { - DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1, i + 1); + DEBUG_FUNCTION_LINE_WRITE("0x%08X (0x%04X); ", data + i + 1, i + 1); } } else if (i + 1 == size) { ascii[(i + 1) % 16] = '\0'; if ((i + 1) % 16 <= 8) { - WHBLogPrintf(" "); + WHBLogWritef(" "); } for (j = (i + 1) % 16; j < 16; ++j) { - WHBLogPrintf(" "); + WHBLogWritef(" "); } - WHBLogPrintf("| %s \n", ascii); + WHBLogPrintf("| %s ", ascii); } } }