diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..56cc685 --- /dev/null +++ b/.clang-format @@ -0,0 +1,67 @@ +# Generated from CLion C/C++ Code Style settings +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: Consecutive +AlignConsecutiveMacros: AcrossEmptyLinesAndComments +AlignOperands: Align +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Always +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterReturnType: None +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: true +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +ColumnLimit: 0 +CompactNamespaces: false +ContinuationIndentWidth: 8 +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 4 +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: All +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PointerAlignment: Right +ReflowComments: false +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Never diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a171590..deeabd5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,8 +6,16 @@ on: - master jobs: + clang-format: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - name: clang-format + run: | + docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./relocator/src build-binary: runs-on: ubuntu-18.04 + needs: clang-format steps: - uses: actions/checkout@v2 - name: build binary @@ -26,7 +34,7 @@ jobs: id: get_repository_name run: | echo REPOSITORY_NAME=$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//") >> $GITHUB_ENV - echo DATETIME=$(echo $(date '+%Y%m%d-%H%M%S')) >> $GITHUB_ENV + echo DATETIME=$(echo $(date '+%Y%m%d-%H%M%S')) >> $GITHUB_ENV - uses: actions/download-artifact@master with: name: binary diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index f907f7e..d08a892 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -3,8 +3,16 @@ name: CI-PR on: [pull_request] jobs: + clang-format: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - name: clang-format + run: | + docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./relocator/src build-binary: runs-on: ubuntu-18.04 + needs: clang-format steps: - uses: actions/checkout@v2 - name: build binary diff --git a/README.md b/README.md index 137216c..8d48641 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![CI-Release](https://github.com/wiiu-env/WUMSLoader/actions/workflows/ci.yml/badge.svg)](https://github.com/wiiu-env/WUMSLoader/actions/workflows/ci.yml) + # Wii U Module System Loader This is a payload that should be run with [EnvironmentLoader](https://github.com/wiiu-env/EnvironmentLoader). @@ -29,6 +31,9 @@ docker run -it --rm -v ${PWD}:/project wumsloader-builder make docker run -it --rm -v ${PWD}:/project wumsloader-builder make clean ``` +## Format the code via docker + +`docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./relocator/src -i` ## Credits - maschell diff --git a/relocator/src/DynamicLinkingHelper.cpp b/relocator/src/DynamicLinkingHelper.cpp index 4daad55..4ad24c0 100644 --- a/relocator/src/DynamicLinkingHelper.cpp +++ b/relocator/src/DynamicLinkingHelper.cpp @@ -9,7 +9,7 @@ dyn_linking_function_t *DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_li return nullptr; } dyn_linking_function_t *result = nullptr; - for (auto &function: data->functions) { + for (auto &function : data->functions) { dyn_linking_function_t *curEntry = &function; if (strlen(curEntry->functionName) == 0) { if (strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) { @@ -41,7 +41,7 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio return nullptr; } dyn_linking_import_t *result = nullptr; - for (auto &import: data->imports) { + for (auto &import : data->imports) { dyn_linking_import_t *curEntry = &import; if (strlen(curEntry->importName) == 0) { if (strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) { @@ -50,7 +50,7 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio } strncpy(curEntry->importName, importName, DYN_LINK_IMPORT_NAME_LENGTH); curEntry->isData = isData; - result = curEntry; + result = curEntry; break; } if (strncmp(curEntry->importName, importName, DYN_LINK_IMPORT_NAME_LENGTH) == 0 && (curEntry->isData == isData)) { @@ -95,12 +95,12 @@ bool DynamicLinkingHelper::addRelocationEntry(dyn_linking_relocation_entry_t *li if (curEntry->functionEntry != nullptr) { continue; } - curEntry->type = type; - curEntry->offset = offset; - curEntry->addend = addend; - curEntry->destination = destination; + curEntry->type = type; + curEntry->offset = offset; + curEntry->addend = addend; + curEntry->destination = destination; curEntry->functionEntry = functionName; - curEntry->importEntry = importInfo; + curEntry->importEntry = importInfo; return true; } diff --git a/relocator/src/DynamicLinkingHelper.h b/relocator/src/DynamicLinkingHelper.h index d03969f..ea02c95 100644 --- a/relocator/src/DynamicLinkingHelper.h +++ b/relocator/src/DynamicLinkingHelper.h @@ -1,10 +1,10 @@ #pragma once -#include +#include "../../source/module/RelocationData.h" #include "utils/logger.h" #include #include -#include "../../source/module/RelocationData.h" +#include class DynamicLinkingHelper { public: diff --git a/relocator/src/ElfUtils.cpp b/relocator/src/ElfUtils.cpp index 5f7ba81..b8ff7a3 100644 --- a/relocator/src/ElfUtils.cpp +++ b/relocator/src/ElfUtils.cpp @@ -1,7 +1,7 @@ #include -#include "utils/logger.h" #include "ElfUtils.h" +#include "utils/logger.h" // See https://github.com/decaf-emu/decaf-emu/blob/43366a34e7b55ab9d19b2444aeb0ccd46ac77dea/src/libdecaf/src/cafe/loader/cafe_loader_reloc.cpp#L144 bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length, @@ -10,7 +10,7 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des return true; } auto target = destination + offset; - auto value = symbol_addr + addend; + auto value = symbol_addr + addend; auto relValue = value - static_cast(target); @@ -94,20 +94,20 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des } if (freeSlot == nullptr) { DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampoline data list is full\n"); - DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) &(freeSlot->trampoline[0]))); + DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) & (freeSlot->trampoline[0]))); return false; } - if (target - (uint32_t) &(freeSlot->trampoline[0]) > 0x1FFFFFC) { + if (target - (uint32_t) & (freeSlot->trampoline[0]) > 0x1FFFFFC) { DEBUG_FUNCTION_LINE("**Cannot link 24-bit jump (too far to tramp buffer)."); - DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) &(freeSlot->trampoline[0]))); + DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) & (freeSlot->trampoline[0]))); return false; } freeSlot->trampoline[0] = 0x3D600000 | ((((uint32_t) value) >> 16) & 0x0000FFFF); // lis r11, real_addr@h - freeSlot->trampoline[1] = 0x616B0000 | (((uint32_t) value) & 0x0000ffff); // ori r11, r11, real_addr@l - freeSlot->trampoline[2] = 0x7D6903A6; // mtctr r11 - freeSlot->trampoline[3] = 0x4E800420; // bctr + freeSlot->trampoline[1] = 0x616B0000 | (((uint32_t) value) & 0x0000ffff); // ori r11, r11, real_addr@l + freeSlot->trampoline[2] = 0x7D6903A6; // mtctr r11 + freeSlot->trampoline[3] = 0x4E800420; // bctr DCFlushRange((void *) freeSlot->trampoline, sizeof(freeSlot->trampoline)); ICInvalidateRange((unsigned char *) freeSlot->trampoline, sizeof(freeSlot->trampoline)); @@ -117,9 +117,9 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des // Relocations for the imports may be overridden freeSlot->status = RELOC_TRAMP_IMPORT_DONE; } - auto symbolValue = (uint32_t) &(freeSlot->trampoline[0]); - value = symbolValue + addend; - distance = static_cast(value) - static_cast(target); + auto symbolValue = (uint32_t) & (freeSlot->trampoline[0]); + value = symbolValue + addend; + distance = static_cast(value) - static_cast(target); } } diff --git a/relocator/src/ElfUtils.h b/relocator/src/ElfUtils.h index fafcbf9..9d196f3 100644 --- a/relocator/src/ElfUtils.h +++ b/relocator/src/ElfUtils.h @@ -7,33 +7,33 @@ extern "C" { #endif -#define R_PPC_NONE 0 -#define R_PPC_ADDR32 1 -#define R_PPC_ADDR16_LO 4 -#define R_PPC_ADDR16_HI 5 -#define R_PPC_ADDR16_HA 6 -#define R_PPC_REL24 10 -#define R_PPC_REL14 11 -#define R_PPC_DTPMOD32 68 -#define R_PPC_DTPREL32 78 -#define R_PPC_EMB_SDA21 109 -#define R_PPC_EMB_RELSDA 116 -#define R_PPC_DIAB_SDA21_LO 180 -#define R_PPC_DIAB_SDA21_HI 181 -#define R_PPC_DIAB_SDA21_HA 182 -#define R_PPC_DIAB_RELSDA_LO 183 -#define R_PPC_DIAB_RELSDA_HI 184 -#define R_PPC_DIAB_RELSDA_HA 185 -#define R_PPC_GHS_REL16_HA 251 -#define R_PPC_GHS_REL16_HI 252 -#define R_PPC_GHS_REL16_LO 253 +#define R_PPC_NONE 0 +#define R_PPC_ADDR32 1 +#define R_PPC_ADDR16_LO 4 +#define R_PPC_ADDR16_HI 5 +#define R_PPC_ADDR16_HA 6 +#define R_PPC_REL24 10 +#define R_PPC_REL14 11 +#define R_PPC_DTPMOD32 68 +#define R_PPC_DTPREL32 78 +#define R_PPC_EMB_SDA21 109 +#define R_PPC_EMB_RELSDA 116 +#define R_PPC_DIAB_SDA21_LO 180 +#define R_PPC_DIAB_SDA21_HI 181 +#define R_PPC_DIAB_SDA21_HA 182 +#define R_PPC_DIAB_RELSDA_LO 183 +#define R_PPC_DIAB_RELSDA_HI 184 +#define R_PPC_DIAB_RELSDA_HA 185 +#define R_PPC_GHS_REL16_HA 251 +#define R_PPC_GHS_REL16_HI 252 +#define R_PPC_GHS_REL16_LO 253 // Masks for manipulating Power PC relocation targets -#define PPC_WORD32 0xFFFFFFFF -#define PPC_WORD30 0xFFFFFFFC -#define PPC_LOW24 0x03FFFFFC -#define PPC_LOW14 0x0020FFFC -#define PPC_HALF16 0xFFFF +#define PPC_WORD32 0xFFFFFFFF +#define PPC_WORD30 0xFFFFFFFC +#define PPC_LOW24 0x03FFFFFC +#define PPC_LOW14 0x0020FFFC +#define PPC_HALF16 0xFFFF #ifdef __cplusplus } diff --git a/relocator/src/ModuleDataMinimal.h b/relocator/src/ModuleDataMinimal.h index 6e04205..464d2e1 100644 --- a/relocator/src/ModuleDataMinimal.h +++ b/relocator/src/ModuleDataMinimal.h @@ -15,8 +15,9 @@ * along with this program. If not, see . ****************************************************************************/ -#include "../../source/module/RelocationData.h" #include "../../source/module/HookData.h" +#include "../../source/module/RelocationData.h" +#include #pragma once @@ -75,11 +76,12 @@ public: } bool relocationsDone = false; + private: std::vector> relocation_data_list; std::vector> hook_data_list; std::string export_name; - uint32_t entrypoint = 0; + uint32_t entrypoint = 0; bool initBeforeRelocationDoneHook = false; - bool skipInitFini = false; + bool skipInitFini = false; }; diff --git a/relocator/src/ModuleDataPersistence.cpp b/relocator/src/ModuleDataPersistence.cpp index 2c570b5..136a83f 100644 --- a/relocator/src/ModuleDataPersistence.cpp +++ b/relocator/src/ModuleDataPersistence.cpp @@ -22,21 +22,21 @@ std::vector> ModuleDataPersistence::loadModul for (int32_t i = 0; i < module_count; i++) { // Copy data from struct. module_information_single_t *module_data = &(moduleInformation->module_data[i]); - auto moduleData = std::make_shared(); + auto moduleData = std::make_shared(); moduleData->setEntrypoint(module_data->entrypoint); moduleData->setInitBeforeRelocationDoneHook(module_data->initBeforeRelocationDoneHook); moduleData->setSkipInitFini(module_data->skipInitFini); moduleData->setExportName(module_data->module_export_name); - for (auto &hook_entry: module_data->hook_entries) { + for (auto &hook_entry : module_data->hook_entries) { if (hook_entry.target == 0) { continue; } moduleData->addHookData(std::make_shared(static_cast(hook_entry.type), reinterpret_cast(hook_entry.target))); } - for (auto &linking_entry: module_data->linking_entries) { + for (auto &linking_entry : module_data->linking_entries) { if (linking_entry.destination == nullptr) { break; } @@ -60,7 +60,7 @@ std::vector> ModuleDataPersistence::loadModul continue; } auto rplInfo = std::make_shared(importEntry->importName, importEntry->isData); - auto reloc = std::make_shared(linking_entry.type, linking_entry.offset, linking_entry.addend, linking_entry.destination, functionEntry->functionName, rplInfo); + auto reloc = std::make_shared(linking_entry.type, linking_entry.offset, linking_entry.addend, linking_entry.destination, functionEntry->functionName, rplInfo); moduleData->addRelocationData(reloc); } diff --git a/relocator/src/ModuleDataPersistence.h b/relocator/src/ModuleDataPersistence.h index 624bf45..7b4a9fd 100644 --- a/relocator/src/ModuleDataPersistence.h +++ b/relocator/src/ModuleDataPersistence.h @@ -1,8 +1,8 @@ #pragma once -#include -#include #include "ModuleDataMinimal.h" +#include +#include class ModuleDataPersistence { public: diff --git a/relocator/src/entry.cpp b/relocator/src/entry.cpp index d2861e9..c7ad4fc 100644 --- a/relocator/src/entry.cpp +++ b/relocator/src/entry.cpp @@ -1,23 +1,23 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "utils/logger.h" -#include "utils/memory.h" #include "../../source/module/RelocationData.h" -#include "ModuleDataPersistence.h" #include "ElfUtils.h" -#include "utils/dynamic.h" +#include "ModuleDataPersistence.h" #include "globals.h" #include "hooks.h" +#include "utils/dynamic.h" +#include "utils/logger.h" +#include "utils/memory.h" +#include +#include +#include +#include +#include +#include +#include +#include MEMHeapHandle gHeapHandle __attribute__((section(".data"))) = nullptr; uint8_t gFunctionsPatched __attribute__((section(".data"))) = 0; -uint8_t gInitCalled __attribute__((section(".data"))) = 0; +uint8_t gInitCalled __attribute__((section(".data"))) = 0; extern "C" void socket_lib_init(); @@ -33,7 +33,7 @@ extern "C" int _start(int argc, char **argv) { static uint8_t ucSetupRequired = 1; if (ucSetupRequired) { - gHeapHandle = MEMCreateExpHeapEx((void *) (MEMORY_REGION_USABLE_HEAP_START), MEMORY_REGION_USABLE_HEAP_END - MEMORY_REGION_USABLE_HEAP_START, 1); + gHeapHandle = MEMCreateExpHeapEx((void *) (MEMORY_REGION_USABLE_HEAP_START), MEMORY_REGION_USABLE_HEAP_END - MEMORY_REGION_USABLE_HEAP_START, 1); ucSetupRequired = 0; } @@ -45,14 +45,14 @@ extern "C" int _start(int argc, char **argv) { DEBUG_FUNCTION_LINE_VERBOSE("Call real one\n"); log_deinit(); - return ((int (*)(int, char **)) (*(unsigned int *) 0x1005E040))(argc, argv); + return ((int (*)(int, char **))(*(unsigned int *) 0x1005E040))(argc, argv); } bool doRelocation(std::vector> &relocData, relocation_trampoline_entry_t *tramp_data, uint32_t tramp_length, bool skipAllocReplacement) { std::map moduleCache; - for (auto const &curReloc: relocData) { + for (auto const &curReloc : relocData) { std::string functionName = curReloc->getName(); - std::string rplName = curReloc->getImportRPLInformation()->getName(); + std::string rplName = curReloc->getImportRPLInformation()->getName(); uint32_t functionAddress = 0; for (uint32_t i = 0; i < MAXIMUM_MODULES; i++) { @@ -77,7 +77,7 @@ bool doRelocation(std::vector> &relocData, reloc } if (functionAddress == 0) { - int32_t isData = curReloc->getImportRPLInformation()->isData(); + int32_t isData = curReloc->getImportRPLInformation()->isData(); OSDynLoad_Module rplHandle = nullptr; if (moduleCache.count(rplName) == 0) { OSDynLoad_Error err = OSDynLoad_IsModuleLoaded(rplName.c_str(), &rplHandle); @@ -115,7 +115,7 @@ bool doRelocation(std::vector> &relocData, reloc bool ResolveRelocations(std::vector> &loadedModules, bool skipMemoryMappingModule) { bool wasSuccessful = true; - for (auto &curModule: loadedModules) { + for (auto &curModule : loadedModules) { DEBUG_FUNCTION_LINE_VERBOSE("Let's do the relocations for %s\n", curModule->getExportName().c_str()); if (wasSuccessful) { auto relocData = curModule->getRelocationDataList(); @@ -128,11 +128,10 @@ bool ResolveRelocations(std::vector> &loadedM DEBUG_FUNCTION_LINE_VERBOSE("Skip alloc replace? %d\n", skipAllocFunction); if (!doRelocation(relocData, gModuleData->trampolines, DYN_LINK_TRAMPOLINE_LIST_LENGTH, skipAllocFunction)) { DEBUG_FUNCTION_LINE("FAIL\n"); - wasSuccessful = false; + wasSuccessful = false; curModule->relocationsDone = false; } curModule->relocationsDone = true; - } } DCFlushRange((void *) MEMORY_REGION_START, MEMORY_REGION_SIZE); @@ -146,10 +145,10 @@ extern "C" void doStart(int argc, char **argv) { } DEBUG_FUNCTION_LINE("Loading module data\n"); auto loadedModulesUnordered = ModuleDataPersistence::loadModuleData(gModuleData); - auto loadedModules = OrderModulesByDependencies(loadedModulesUnordered); + auto loadedModules = OrderModulesByDependencies(loadedModulesUnordered); bool applicationEndHookLoaded = false; - for (auto &curModule: loadedModules) { + for (auto &curModule : loadedModules) { if (curModule->getExportName() == "homebrew_applicationendshook") { DEBUG_FUNCTION_LINE_VERBOSE("We have ApplicationEndsHook Module!\n"); applicationEndHookLoaded = true; @@ -158,8 +157,8 @@ 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()) { + for (auto &curModule : loadedModules) { + for (auto &curHook : curModule->getHookDataList()) { 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()); @@ -175,7 +174,7 @@ extern "C" void doStart(int argc, char **argv) { DEBUG_FUNCTION_LINE_VERBOSE("Resolve relocations without replacing alloc functions\n"); ResolveRelocations(loadedModules, true); - for (auto &curModule: loadedModules) { + for (auto &curModule : loadedModules) { if (curModule->isInitBeforeRelocationDoneHook()) { CallInitHooksForModule(curModule); } @@ -184,7 +183,7 @@ extern "C" void doStart(int argc, char **argv) { DEBUG_FUNCTION_LINE_VERBOSE("Call Relocations done hook\n"); CallHook(loadedModules, WUMS_HOOK_RELOCATIONS_DONE); - for (auto &curModule: loadedModules) { + for (auto &curModule : loadedModules) { if (!curModule->isInitBeforeRelocationDoneHook()) { CallInitHooksForModule(curModule); } @@ -200,7 +199,7 @@ extern "C" void doStart(int argc, char **argv) { CallHook(loadedModules, WUMS_HOOK_INIT_WUT_STDCPP); CallHook(loadedModules, WUMS_HOOK_INIT_WUT_DEVOPTAB); CallHook(loadedModules, WUMS_HOOK_INIT_WUT_SOCKETS); - for (auto &curModule: loadedModules) { + for (auto &curModule : loadedModules) { CallHook(curModule, WUMS_HOOK_INIT_WRAPPER, !curModule->isSkipInitFini()); } CallHook(loadedModules, WUMS_HOOK_APPLICATION_STARTS); @@ -228,9 +227,9 @@ std::vector> OrderModulesByDependencies(const std::vector loadedModulesEntrypoints; while (true) { - bool canBreak = true; + bool canBreak = true; bool weDidSomething = false; - for (auto const &curModule: loadedModules) { + for (auto const &curModule : loadedModules) { if (std::find(loadedModulesEntrypoints.begin(), loadedModulesEntrypoints.end(), curModule->getEntrypoint()) != loadedModulesEntrypoints.end()) { // DEBUG_FUNCTION_LINE("%s [%08X] is already loaded\n", curModule->getExportName().c_str(), curModule->getEntrypoint()); continue; @@ -238,7 +237,7 @@ std::vector> OrderModulesByDependencies(const canBreak = false; DEBUG_FUNCTION_LINE_VERBOSE("Check if we can load %s\n", curModule->getExportName().c_str()); std::vector importsFromOtherModules; - for (const auto &curReloc: curModule->getRelocationDataList()) { + for (const auto &curReloc : curModule->getRelocationDataList()) { std::string curRPL = curReloc->getImportRPLInformation()->getName(); if (curRPL.rfind("homebrew", 0) == 0) { if (std::find(importsFromOtherModules.begin(), importsFromOtherModules.end(), curRPL) != importsFromOtherModules.end()) { @@ -250,7 +249,7 @@ std::vector> OrderModulesByDependencies(const } } bool canLoad = true; - for (auto &curImportRPL: importsFromOtherModules) { + for (auto &curImportRPL : importsFromOtherModules) { if (std::find(loadedModulesExportNames.begin(), loadedModulesExportNames.end(), curImportRPL) != loadedModulesExportNames.end()) { } else { diff --git a/relocator/src/globals.h b/relocator/src/globals.h index 5e97379..92cb3f7 100644 --- a/relocator/src/globals.h +++ b/relocator/src/globals.h @@ -1,7 +1,7 @@ #pragma once -#include #include "../../source/globals.h" +#include extern uint32_t MemoryMappingEffectiveToPhysicalPTR; extern uint32_t MemoryMappingPhysicalToEffectivePTR; \ No newline at end of file diff --git a/relocator/src/hooks.cpp b/relocator/src/hooks.cpp index d600396..e690af6 100644 --- a/relocator/src/hooks.cpp +++ b/relocator/src/hooks.cpp @@ -1,8 +1,8 @@ -#include #include "hooks.h" #include "globals.h" +#include -static const char **hook_names = (const char *[]) { +static const char **hook_names = (const char *[]){ "WUMS_HOOK_INIT_WUT_MALLOC", "WUMS_HOOK_FINI_WUT_MALLOC", "WUMS_HOOK_INIT_WUT_NEWLIB", @@ -21,8 +21,7 @@ static const char **hook_names = (const char *[]) { "WUMS_HOOK_APPLICATION_STARTS", "WUMS_HOOK_APPLICATION_ENDS", "WUMS_HOOK_RELOCATIONS_DONE", - "WUMS_HOOK_APPLICATION_REQUESTS_EXIT" -}; + "WUMS_HOOK_APPLICATION_REQUESTS_EXIT"}; void CallHook(const std::vector> &modules, wums_hook_type_t type, bool condition) { if (condition) { @@ -32,7 +31,7 @@ void CallHook(const std::vector> &modules, wu 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) { + for (auto &curModule : modules) { CallHook(curModule, type); } } @@ -49,7 +48,7 @@ void CallHook(const std::shared_ptr &module, wums_hook_type_t return; } - for (auto &curHook: module->getHookDataList()) { + for (auto &curHook : module->getHookDataList()) { auto func_ptr = (uint32_t) curHook->getTarget(); if (func_ptr == 0) { DEBUG_FUNCTION_LINE("Hook ptr was NULL\n"); @@ -70,17 +69,16 @@ void CallHook(const std::shared_ptr &module, wums_hook_type_t type == WUMS_HOOK_INIT_WUT_SOCKETS || type == WUMS_HOOK_FINI_WUT_SOCKETS || type == WUMS_HOOK_INIT_WRAPPER || - type == WUMS_HOOK_FINI_WRAPPER - )) { + type == WUMS_HOOK_FINI_WRAPPER)) { 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))(); + ((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: %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); + ((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); diff --git a/relocator/src/hooks.h b/relocator/src/hooks.h index f965c00..341aae7 100644 --- a/relocator/src/hooks.h +++ b/relocator/src/hooks.h @@ -1,8 +1,8 @@ #pragma once -#include -#include #include "ModuleDataMinimal.h" +#include +#include void CallHook(const std::vector> &modules, wums_hook_type_t type, bool condition); diff --git a/relocator/src/utils/dynamic.c b/relocator/src/utils/dynamic.c index 8857c7c..16e85b5 100644 --- a/relocator/src/utils/dynamic.c +++ b/relocator/src/utils/dynamic.c @@ -1,7 +1,7 @@ -#include #include +#include -#define IMPORT(name) void* addr_##name +#define IMPORT(name) void *addr_##name #define IMPORT_BEGIN(lib) #define IMPORT_END() @@ -11,17 +11,22 @@ #undef IMPORT_BEGIN #undef IMPORT_END -#define IMPORT(name) do{if(OSDynLoad_FindExport(handle, 0, #name, &addr_##name) < 0)OSFatal("Function " # name " is NULL");} while(0) -#define IMPORT_BEGIN(lib) do{if(OSDynLoad_IsModuleLoaded(#lib ".rpl", &handle) != OS_DYNLOAD_OK) OSFatal(#lib ".rpl is not loaded");} while(0) +#define IMPORT(name) \ + do { \ + if (OSDynLoad_FindExport(handle, 0, #name, &addr_##name) < 0) OSFatal("Function " #name " is NULL"); \ + } while (0) +#define IMPORT_BEGIN(lib) \ + do { \ + if (OSDynLoad_IsModuleLoaded(#lib ".rpl", &handle) != OS_DYNLOAD_OK) OSFatal(#lib ".rpl is not loaded"); \ + } while (0) #define IMPORT_END() void InitFunctionPointers(void) { OSDynLoad_Module handle; - addr_OSDynLoad_Acquire = (void *) 0x0102A3B4; // 0x0200dfb4 - 0xFE3C00 - addr_OSDynLoad_FindExport = (void *) 0x0102B828; // 0200f428 - 0xFE3C00 + addr_OSDynLoad_Acquire = (void *) 0x0102A3B4; // 0x0200dfb4 - 0xFE3C00 + addr_OSDynLoad_FindExport = (void *) 0x0102B828; // 0200f428 - 0xFE3C00 addr_OSDynLoad_IsModuleLoaded = (void *) 0x0102A59C; // 0200e19c - 0xFE3C00 #include "imports.h" - } diff --git a/relocator/src/utils/logger.c b/relocator/src/utils/logger.c index ad8f213..fa73f31 100644 --- a/relocator/src/utils/logger.c +++ b/relocator/src/utils/logger.c @@ -1,37 +1,37 @@ -#include -#include -#include -#include -#include #include "logger.h" #include #include #include +#include +#include +#include +#include +#include static int log_socket __attribute__((section(".data"))) = -1; static struct sockaddr_in connect_addr __attribute__((section(".data"))); static volatile int log_lock __attribute__((section(".data"))) = 0; -#define SOL_SOCKET -1 +#define SOL_SOCKET -1 -#define SOCK_DGRAM 2 +#define SOCK_DGRAM 2 #define INADDR_ANY 0 #define INADDR_BROADCAST 0xFFFFFFFF -#define PF_UNSPEC 0 -#define PF_INET 2 -#define PF_INET6 23 +#define PF_UNSPEC 0 +#define PF_INET 2 +#define PF_INET6 23 -#define AF_UNSPEC PF_UNSPEC -#define AF_INET PF_INET -#define AF_INET6 PF_INET6 +#define AF_UNSPEC PF_UNSPEC +#define AF_INET PF_INET +#define AF_INET6 PF_INET6 -#define IPPROTO_UDP 17 +#define IPPROTO_UDP 17 -#define SO_BROADCAST 0x0020 // broadcast +#define SO_BROADCAST 0x0020 // broadcast typedef uint16_t sa_family_t; @@ -65,15 +65,15 @@ extern uint32_t htonl(uint32_t val); void log_init() { int broadcastEnable = 1; - log_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + log_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (log_socket < 0) return; setsockopt(log_socket, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)); memset(&connect_addr, 0, sizeof(struct sockaddr_in)); - connect_addr.sin_family = AF_INET; - connect_addr.sin_port = 4405; + connect_addr.sin_family = AF_INET; + connect_addr.sin_port = 4405; connect_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); } @@ -102,7 +102,7 @@ void log_print(const char *str) { int ret; while (len > 0) { int block = len < 1400 ? len : 1400; // take max 1400 bytes per UDP packet - ret = sendto(log_socket, str, block, 0, (struct sockaddr *) &connect_addr, sizeof(struct sockaddr_in)); + ret = sendto(log_socket, str, block, 0, (struct sockaddr *) &connect_addr, sizeof(struct sockaddr_in)); if (ret < 0) break; @@ -139,4 +139,3 @@ void log_printf(const char *format, ...) { } va_end(va); } - diff --git a/relocator/src/utils/logger.h b/relocator/src/utils/logger.h index 74f239d..cd5c631 100644 --- a/relocator/src/utils/logger.h +++ b/relocator/src/utils/logger.h @@ -18,18 +18,21 @@ void log_printf(const char *format, ...); void OSFatal_printf(const char *format, ...); #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) +#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) -#define OSFATAL_FUNCTION_LINE(FMT, ARGS...)do { \ - OSFatal_printf("[%s]%s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ +#define OSFATAL_FUNCTION_LINE(FMT, ARGS...) \ + do { \ + OSFatal_printf("[%s]%s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ } while (0) -#define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ - log_printf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) \ + do { \ + log_printf("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ } while (0) -#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...)do { \ - log_printf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ +#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/relocator/src/utils/memory.cpp b/relocator/src/utils/memory.cpp index 13efea3..2df306f 100644 --- a/relocator/src/utils/memory.cpp +++ b/relocator/src/utils/memory.cpp @@ -14,21 +14,21 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . ****************************************************************************/ -#include -#include -#include +#include "logger.h" #include +#include +#include +#include +#include #include #include -#include -#include "logger.h" extern MEMHeapHandle gHeapHandle; void *MEMAllocSafe(uint32_t size, uint32_t align) { - void *res = nullptr; + void *res = nullptr; MEMHeapHandle heapHandle = gHeapHandle; - res = MEMAllocFromExpHeapEx(heapHandle, size, align); + res = MEMAllocFromExpHeapEx(heapHandle, size, align); return res; } @@ -57,9 +57,9 @@ void MemoryFree(void *ptr) { } } -uint32_t MEMAlloc __attribute__((__section__ (".data"))) = (uint32_t) MemoryAlloc; -uint32_t MEMAllocEx __attribute__((__section__ (".data"))) = (uint32_t) MemoryAllocEx; -uint32_t MEMFree __attribute__((__section__ (".data"))) = (uint32_t) MemoryFree; +uint32_t MEMAlloc __attribute__((__section__(".data"))) = (uint32_t) MemoryAlloc; +uint32_t MEMAllocEx __attribute__((__section__(".data"))) = (uint32_t) MemoryAllocEx; +uint32_t MEMFree __attribute__((__section__(".data"))) = (uint32_t) MemoryFree; //!------------------------------------------------------------------------------------------- //! reent versions @@ -113,12 +113,10 @@ struct mallinfo _mallinfo_r(struct _reent *r) { return info; } -void -_malloc_stats_r(struct _reent *r) { +void _malloc_stats_r(struct _reent *r) { } -int -_mallopt_r(struct _reent *r, int param, int value) { +int _mallopt_r(struct _reent *r, int param, int value) { return 0; } @@ -137,7 +135,6 @@ _pvalloc_r(struct _reent *r, size_t size) { return MemoryAllocEx((size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1), OS_PAGE_SIZE); } -int -_malloc_trim_r(struct _reent *r, size_t pad) { +int _malloc_trim_r(struct _reent *r, size_t pad) { return 0; } diff --git a/source/ElfUtils.cpp b/source/ElfUtils.cpp index 873c7d2..4c8692c 100644 --- a/source/ElfUtils.cpp +++ b/source/ElfUtils.cpp @@ -1,17 +1,17 @@ -#include +#include "utils/logger.h" #include +#include #include -#include #include #include -#include "utils/logger.h" +#include -#include "elfio/elfio.hpp" #include "ElfUtils.h" +#include "elfio/elfio.hpp" int32_t LoadFileToMem(const char *relativefilepath, char **fileOut, uint32_t *sizeOut) { char path[256]; - int result = 0; + int result = 0; char *sdRootPath = nullptr; if (!WHBMountSdCard()) { DEBUG_FUNCTION_LINE("Failed to mount SD Card..."); @@ -31,13 +31,13 @@ int32_t LoadFileToMem(const char *relativefilepath, char **fileOut, uint32_t *si goto exit; } - exit: +exit: WHBUnmountSdCard(); return result; } uint32_t load_loader_elf_from_sd(unsigned char *baseAddress, const char *relativePath) { - char *elf_data = nullptr; + char *elf_data = nullptr; uint32_t fileSize = 0; if (LoadFileToMem(relativePath, &elf_data, &fileSize) != 0) { OSFatal("Failed to load hook_payload.elf from the SD Card."); @@ -81,7 +81,7 @@ uint32_t load_loader_elf(unsigned char *baseAddress, char *elf_data, uint32_t fi } uint32_t p_paddr = phdrs[i].p_paddr + (uint32_t) baseAddress; - image = (uint8_t *) (elf_data + phdrs[i].p_offset); + image = (uint8_t *) (elf_data + phdrs[i].p_offset); memcpy((void *) p_paddr, image, phdrs[i].p_filesz); DCFlushRange((void *) p_paddr, phdrs[i].p_filesz); @@ -114,7 +114,7 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des return true; } auto target = destination + offset; - auto value = symbol_addr + addend; + auto value = symbol_addr + addend; auto relValue = value - static_cast(target); @@ -199,19 +199,19 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des } if (freeSlot == nullptr) { DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampoline data list is full\n"); - DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) &(freeSlot->trampoline[0]))); + DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) & (freeSlot->trampoline[0]))); return false; } - if (target - (uint32_t) &(freeSlot->trampoline[0]) > 0x1FFFFFC) { + if (target - (uint32_t) & (freeSlot->trampoline[0]) > 0x1FFFFFC) { DEBUG_FUNCTION_LINE("**Cannot link 24-bit jump (too far to tramp buffer)."); - DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) &(freeSlot->trampoline[0]))); + DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) & (freeSlot->trampoline[0]))); return false; } freeSlot->trampoline[0] = 0x3D600000 | ((((uint32_t) value) >> 16) & 0x0000FFFF); // lis r11, real_addr@h - freeSlot->trampoline[1] = 0x616B0000 | (((uint32_t) value) & 0x0000ffff); // ori r11, r11, real_addr@l - freeSlot->trampoline[2] = 0x7D6903A6; // mtctr r11 - freeSlot->trampoline[3] = 0x4E800420; // bctr + freeSlot->trampoline[1] = 0x616B0000 | (((uint32_t) value) & 0x0000ffff); // ori r11, r11, real_addr@l + freeSlot->trampoline[2] = 0x7D6903A6; // mtctr r11 + freeSlot->trampoline[3] = 0x4E800420; // bctr DCFlushRange((void *) freeSlot->trampoline, sizeof(freeSlot->trampoline)); ICInvalidateRange((unsigned char *) freeSlot->trampoline, sizeof(freeSlot->trampoline)); @@ -221,9 +221,9 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des // Relocations for the imports may be overridden freeSlot->status = RELOC_TRAMP_IMPORT_DONE; } - auto symbolValue = (uint32_t) &(freeSlot->trampoline[0]); - value = symbolValue + addend; - distance = static_cast(value) - static_cast(target); + auto symbolValue = (uint32_t) & (freeSlot->trampoline[0]); + value = symbolValue + addend; + distance = static_cast(value) - static_cast(target); } } diff --git a/source/ElfUtils.h b/source/ElfUtils.h index abd17ea..f66ed8e 100644 --- a/source/ElfUtils.h +++ b/source/ElfUtils.h @@ -12,33 +12,33 @@ int32_t LoadFileToMem(const char *relativefilepath, char **fileOut, uint32_t *si uint32_t load_loader_elf_from_sd(unsigned char *baseAddress, const char *relativePath); uint32_t load_loader_elf(unsigned char *baseAddress, char *elf_data, uint32_t fileSize); -#define R_PPC_NONE 0 -#define R_PPC_ADDR32 1 -#define R_PPC_ADDR16_LO 4 -#define R_PPC_ADDR16_HI 5 -#define R_PPC_ADDR16_HA 6 -#define R_PPC_REL24 10 -#define R_PPC_REL14 11 -#define R_PPC_DTPMOD32 68 -#define R_PPC_DTPREL32 78 -#define R_PPC_EMB_SDA21 109 -#define R_PPC_EMB_RELSDA 116 -#define R_PPC_DIAB_SDA21_LO 180 -#define R_PPC_DIAB_SDA21_HI 181 -#define R_PPC_DIAB_SDA21_HA 182 -#define R_PPC_DIAB_RELSDA_LO 183 -#define R_PPC_DIAB_RELSDA_HI 184 -#define R_PPC_DIAB_RELSDA_HA 185 -#define R_PPC_GHS_REL16_HA 251 -#define R_PPC_GHS_REL16_HI 252 -#define R_PPC_GHS_REL16_LO 253 +#define R_PPC_NONE 0 +#define R_PPC_ADDR32 1 +#define R_PPC_ADDR16_LO 4 +#define R_PPC_ADDR16_HI 5 +#define R_PPC_ADDR16_HA 6 +#define R_PPC_REL24 10 +#define R_PPC_REL14 11 +#define R_PPC_DTPMOD32 68 +#define R_PPC_DTPREL32 78 +#define R_PPC_EMB_SDA21 109 +#define R_PPC_EMB_RELSDA 116 +#define R_PPC_DIAB_SDA21_LO 180 +#define R_PPC_DIAB_SDA21_HI 181 +#define R_PPC_DIAB_SDA21_HA 182 +#define R_PPC_DIAB_RELSDA_LO 183 +#define R_PPC_DIAB_RELSDA_HI 184 +#define R_PPC_DIAB_RELSDA_HA 185 +#define R_PPC_GHS_REL16_HA 251 +#define R_PPC_GHS_REL16_HI 252 +#define R_PPC_GHS_REL16_LO 253 // Masks for manipulating Power PC relocation targets -#define PPC_WORD32 0xFFFFFFFF -#define PPC_WORD30 0xFFFFFFFC -#define PPC_LOW24 0x03FFFFFC -#define PPC_LOW14 0x0020FFFC -#define PPC_HALF16 0xFFFF +#define PPC_WORD32 0xFFFFFFFF +#define PPC_WORD30 0xFFFFFFFC +#define PPC_LOW24 0x03FFFFFC +#define PPC_LOW14 0x0020FFFC +#define PPC_HALF16 0xFFFF #ifdef __cplusplus } diff --git a/source/elfio/elf_types.hpp b/source/elfio/elf_types.hpp index 63d025a..9a20040 100644 --- a/source/elfio/elf_types.hpp +++ b/source/elfio/elf_types.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio.hpp b/source/elfio/elfio.hpp index 2a0174e..1d691b1 100644 --- a/source/elfio/elfio.hpp +++ b/source/elfio/elfio.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_dump.hpp b/source/elfio/elfio_dump.hpp index 4ace665..015cff2 100644 --- a/source/elfio/elfio_dump.hpp +++ b/source/elfio/elfio_dump.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_dynamic.hpp b/source/elfio/elfio_dynamic.hpp index 42f2680..36de384 100644 --- a/source/elfio/elfio_dynamic.hpp +++ b/source/elfio/elfio_dynamic.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_header.hpp b/source/elfio/elfio_header.hpp index 6f8da02..e7b9a24 100644 --- a/source/elfio/elfio_header.hpp +++ b/source/elfio/elfio_header.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_note.hpp b/source/elfio/elfio_note.hpp index 8619c73..fbfc51c 100644 --- a/source/elfio/elfio_note.hpp +++ b/source/elfio/elfio_note.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_relocation.hpp b/source/elfio/elfio_relocation.hpp index 4a3fab0..641031a 100644 --- a/source/elfio/elfio_relocation.hpp +++ b/source/elfio/elfio_relocation.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_section.hpp b/source/elfio/elfio_section.hpp index ed3b908..52d3102 100644 --- a/source/elfio/elfio_section.hpp +++ b/source/elfio/elfio_section.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_segment.hpp b/source/elfio/elfio_segment.hpp index e906f3e..fa370fb 100644 --- a/source/elfio/elfio_segment.hpp +++ b/source/elfio/elfio_segment.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_strings.hpp b/source/elfio/elfio_strings.hpp index 552f000..5fc9bb1 100644 --- a/source/elfio/elfio_strings.hpp +++ b/source/elfio/elfio_strings.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_symbols.hpp b/source/elfio/elfio_symbols.hpp index d18756a..6205bd2 100644 --- a/source/elfio/elfio_symbols.hpp +++ b/source/elfio/elfio_symbols.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/elfio/elfio_utils.hpp b/source/elfio/elfio_utils.hpp index 2baf5a7..e567d41 100644 --- a/source/elfio/elfio_utils.hpp +++ b/source/elfio/elfio_utils.hpp @@ -1,3 +1,4 @@ +// clang-format off /* Copyright (C) 2001-2015 by Serge Lamikhov-Center diff --git a/source/fs/CFile.cpp b/source/fs/CFile.cpp index c8e78d2..eb63a80 100644 --- a/source/fs/CFile.cpp +++ b/source/fs/CFile.cpp @@ -1,15 +1,15 @@ -#include -#include -#include -#include #include +#include +#include +#include +#include CFile::CFile() { - iFd = -1; + iFd = -1; mem_file = NULL; filesize = 0; - pos = 0; + pos = 0; } CFile::CFile(const std::string &filepath, eOpenTypes mode) { @@ -35,7 +35,7 @@ int32_t CFile::open(const std::string &filepath, eOpenTypes mode) { switch (mode) { default: - case ReadOnly: // file must exist + case ReadOnly: // file must exist openMode = O_RDONLY; break; case WriteOnly: // file will be created / zerod @@ -77,10 +77,10 @@ void CFile::close() { if (iFd >= 0) ::close(iFd); - iFd = -1; + iFd = -1; mem_file = NULL; filesize = 0; - pos = 0; + pos = 0; } int32_t CFile::read(uint8_t *ptr, size_t size) { @@ -127,7 +127,7 @@ int32_t CFile::write(const uint8_t *ptr, size_t size) { } int32_t CFile::seek(long int offset, int32_t origin) { - int32_t ret = 0; + int32_t ret = 0; int64_t newPos = pos; if (origin == SEEK_SET) { @@ -158,7 +158,7 @@ int32_t CFile::seek(long int offset, int32_t origin) { int32_t CFile::fwrite(const char *format, ...) { char tmp[512]; - tmp[0] = 0; + tmp[0] = 0; int32_t result = -1; va_list va; @@ -171,5 +171,3 @@ int32_t CFile::fwrite(const char *format, ...) { return result; } - - diff --git a/source/fs/CFile.hpp b/source/fs/CFile.hpp index 6c0421b..2bc9dfc 100644 --- a/source/fs/CFile.hpp +++ b/source/fs/CFile.hpp @@ -1,10 +1,10 @@ #ifndef CFILE_HPP_ #define CFILE_HPP_ -#include -#include -#include #include +#include +#include +#include #include #include diff --git a/source/fs/DirList.cpp b/source/fs/DirList.cpp index ab2fea4..3198efb 100644 --- a/source/fs/DirList.cpp +++ b/source/fs/DirList.cpp @@ -24,22 +24,22 @@ * DirList Class * for WiiXplorer 2010 ***************************************************************************/ +#include #include #include #include #include #include -#include -#include #include +#include #include #include DirList::DirList() { - Flags = 0; + Flags = 0; Filter = 0; - Depth = 0; + Depth = 0; } DirList::DirList(const std::string &path, const char *filter, uint32_t flags, uint32_t maxDepth) { @@ -55,9 +55,9 @@ BOOL DirList::LoadPath(const std::string &folder, const char *filter, uint32_t f if (folder.empty()) return false; - Flags = flags; + Flags = flags; Filter = filter; - Depth = maxDepth; + Depth = maxDepth; std::string folderpath(folder); uint32_t length = folderpath.size(); @@ -82,14 +82,14 @@ BOOL DirList::InternalLoadPath(std::string &folderpath) { return false; struct dirent *dirent = NULL; - DIR *dir = NULL; + DIR *dir = NULL; dir = opendir(folderpath.c_str()); if (dir == NULL) return false; while ((dirent = readdir(dir)) != 0) { - BOOL isDir = dirent->d_type & DT_DIR; + BOOL isDir = dirent->d_type & DT_DIR; const char *filename = dirent->d_name; if (isDir) { diff --git a/source/fs/DirList.h b/source/fs/DirList.h index 08b3fbc..bd1cd90 100644 --- a/source/fs/DirList.h +++ b/source/fs/DirList.h @@ -27,8 +27,8 @@ #ifndef ___DIRLIST_H_ #define ___DIRLIST_H_ -#include #include +#include #include typedef struct { @@ -93,10 +93,11 @@ public: //! Enum for search/filter flags enum { - Files = 0x01, - Dirs = 0x02, + Files = 0x01, + Dirs = 0x02, CheckSubfolders = 0x08, }; + protected: // Internal parser BOOL InternalLoadPath(std::string &path); diff --git a/source/globals.h b/source/globals.h index 24f5e8d..17bd5c5 100644 --- a/source/globals.h +++ b/source/globals.h @@ -3,13 +3,13 @@ #include #include -#define MEMORY_REGION_START 0x00800000 -#define MEMORY_REGION_SIZE 0x00800000 +#define MEMORY_REGION_START 0x00800000 +#define MEMORY_REGION_SIZE 0x00800000 -#define MEMORY_REGION_USABLE_HEAP_START (MEMORY_REGION_START + 0x00080000) // We don't want to override the relocator -#define MEMORY_REGION_USABLE_HEAP_END (MEMORY_REGION_USABLE_HEAP_START + 0x00080000) // heap size is 512 KiB for now +#define MEMORY_REGION_USABLE_HEAP_START (MEMORY_REGION_START + 0x00080000) // We don't want to override the relocator +#define MEMORY_REGION_USABLE_HEAP_END (MEMORY_REGION_USABLE_HEAP_START + 0x00080000) // heap size is 512 KiB for now -#define MEMORY_REGION_USABLE_START MEMORY_REGION_USABLE_HEAP_END -#define MEMORY_REGION_USABLE_END 0x00FFF000 // The last 0x1000 bytes are reserved kernel hook +#define MEMORY_REGION_USABLE_START MEMORY_REGION_USABLE_HEAP_END +#define MEMORY_REGION_USABLE_END 0x00FFF000 // The last 0x1000 bytes are reserved kernel hook -#define gModuleData ((module_information_t *) (MEMORY_REGION_USABLE_START)) \ No newline at end of file +#define gModuleData ((module_information_t *) (MEMORY_REGION_USABLE_START)) \ No newline at end of file diff --git a/source/kernel.cpp b/source/kernel.cpp index 7eb5b3a..85e7800 100644 --- a/source/kernel.cpp +++ b/source/kernel.cpp @@ -1,6 +1,6 @@ #include "kernel.h" -#include "relocator_elf.h" #include "ElfUtils.h" +#include "relocator_elf.h" #include #include @@ -37,23 +37,22 @@ void KernelWriteU32(uint32_t addr, uint32_t value) { } /* Write a 32-bit word with kernel permissions */ -void __attribute__ ((noinline)) kern_write(void *addr, uint32_t value) { - asm volatile ( - "li 3,1\n" - "li 4,0\n" - "mr 5,%1\n" - "li 6,0\n" - "li 7,0\n" - "lis 8,1\n" - "mr 9,%0\n" - "mr %1,1\n" - "li 0,0x3500\n" - "sc\n" - "nop\n" - "mr 1,%1\n" - : - : "r"(addr), "r"(value) - : "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10", - "11", "12" - ); +void __attribute__((noinline)) kern_write(void *addr, uint32_t value) { + asm volatile( + "li 3,1\n" + "li 4,0\n" + "mr 5,%1\n" + "li 6,0\n" + "li 7,0\n" + "lis 8,1\n" + "mr 9,%0\n" + "mr %1,1\n" + "li 0,0x3500\n" + "sc\n" + "nop\n" + "mr 1,%1\n" + : + : "r"(addr), "r"(value) + : "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10", + "11", "12"); } diff --git a/source/kernel.h b/source/kernel.h index d5e0478..c6314df 100644 --- a/source/kernel.h +++ b/source/kernel.h @@ -2,16 +2,16 @@ #include -#define ADDRESS_main_entry_hook 0x0101C56C +#define ADDRESS_main_entry_hook 0x0101C56C -#define KERN_SYSCALL_TBL_1 0xFFE84C70 // unknown -#define KERN_SYSCALL_TBL_2 0xFFE85070 // works with games -#define KERN_SYSCALL_TBL_3 0xFFE85470 // works with loader -#define KERN_SYSCALL_TBL_4 0xFFEAAA60 // works with home menu -#define KERN_SYSCALL_TBL_5 0xFFEAAE60 // works with browser (previously KERN_SYSCALL_TBL) +#define KERN_SYSCALL_TBL_1 0xFFE84C70 // unknown +#define KERN_SYSCALL_TBL_2 0xFFE85070 // works with games +#define KERN_SYSCALL_TBL_3 0xFFE85470 // works with loader +#define KERN_SYSCALL_TBL_4 0xFFEAAA60 // works with home menu +#define KERN_SYSCALL_TBL_5 0xFFEAAE60 // works with browser (previously KERN_SYSCALL_TBL) void SetupRelocator(); void KernelWriteU32(uint32_t addr, uint32_t value); -void __attribute__ ((noinline)) kern_write(void *addr, uint32_t value); +void __attribute__((noinline)) kern_write(void *addr, uint32_t value); diff --git a/source/main.cpp b/source/main.cpp index e29735c..1bd8996 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,15 +1,15 @@ #include #include -#include #include +#include -#include "fs/DirList.h" -#include "module/ModuleDataPersistence.h" -#include "module/ModuleDataFactory.h" #include "ElfUtils.h" -#include "kernel.h" +#include "fs/DirList.h" #include "globals.h" +#include "kernel.h" +#include "module/ModuleDataFactory.h" +#include "module/ModuleDataPersistence.h" extern "C" uint32_t textStart(); extern "C" void __fini(); @@ -48,7 +48,7 @@ int main(int argc, char **argv) { SetupRelocator(); nn::act::Initialize(); - nn::act::SlotNo slot = nn::act::GetSlotNo(); + nn::act::SlotNo slot = nn::act::GetSlotNo(); nn::act::SlotNo defaultSlot = nn::act::GetDefaultAccount(); nn::act::Finalize(); diff --git a/source/module/DynamicLinkingHelper.cpp b/source/module/DynamicLinkingHelper.cpp index 26b26ac..8b25dfc 100644 --- a/source/module/DynamicLinkingHelper.cpp +++ b/source/module/DynamicLinkingHelper.cpp @@ -1,9 +1,9 @@ #include "DynamicLinkingHelper.h" +#include "utils/logger.h" +#include #include #include #include -#include -#include "utils/logger.h" dyn_linking_function_t *DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_linking_relocation_data_t *data, const char *functionName) { if (data == nullptr) { @@ -13,7 +13,7 @@ dyn_linking_function_t *DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_li return nullptr; } dyn_linking_function_t *result = nullptr; - for (auto &function: data->functions) { + for (auto &function : data->functions) { dyn_linking_function_t *curEntry = &function; if (strlen(curEntry->functionName) == 0) { if (strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) { @@ -45,7 +45,7 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio return nullptr; } dyn_linking_import_t *result = nullptr; - for (auto &import: data->imports) { + for (auto &import : data->imports) { dyn_linking_import_t *curEntry = &import; if (strlen(curEntry->importName) == 0) { if (strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) { @@ -54,7 +54,7 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio } strncpy(curEntry->importName, importName, DYN_LINK_IMPORT_NAME_LENGTH); curEntry->isData = isData; - result = curEntry; + result = curEntry; break; } if (strncmp(curEntry->importName, importName, DYN_LINK_IMPORT_NAME_LENGTH) == 0 && (curEntry->isData == isData)) { @@ -98,12 +98,12 @@ bool DynamicLinkingHelper::addRelocationEntry(dyn_linking_relocation_entry_t *li if (curEntry->functionEntry != nullptr) { continue; } - curEntry->type = type; - curEntry->offset = offset; - curEntry->addend = addend; - curEntry->destination = destination; + curEntry->type = type; + curEntry->offset = offset; + curEntry->addend = addend; + curEntry->destination = destination; curEntry->functionEntry = functionName; - curEntry->importEntry = importInfo; + curEntry->importEntry = importInfo; return true; } diff --git a/source/module/DynamicLinkingHelper.h b/source/module/DynamicLinkingHelper.h index f763415..a00d493 100644 --- a/source/module/DynamicLinkingHelper.h +++ b/source/module/DynamicLinkingHelper.h @@ -1,10 +1,10 @@ #pragma once -#include +#include "RelocationData.h" #include "utils/logger.h" #include #include -#include "RelocationData.h" +#include class DynamicLinkingHelper { public: diff --git a/source/module/ExportData.h b/source/module/ExportData.h index f187c6b..af322b2 100644 --- a/source/module/ExportData.h +++ b/source/module/ExportData.h @@ -1,13 +1,14 @@ -#pragma once +#pragma once +#include #include class ExportData { public: ExportData(wums_entry_type_t type, const std::string &name, const void *address) { - this->type = type; - this->name = name; + this->type = type; + this->name = name; this->address = address; } diff --git a/source/module/FunctionSymbolData.h b/source/module/FunctionSymbolData.h index 957bdc0..c873f75 100644 --- a/source/module/FunctionSymbolData.h +++ b/source/module/FunctionSymbolData.h @@ -24,16 +24,15 @@ class FunctionSymbolData { public: FunctionSymbolData(const FunctionSymbolData &o2) = default; - FunctionSymbolData(const char *name, void *address, uint32_t size) : - name(name), - address(address), - size(size) { + FunctionSymbolData(const char *name, void *address, uint32_t size) : name(name), + address(address), + size(size) { } virtual ~FunctionSymbolData() = default; bool operator<(const FunctionSymbolData &rhs) const { - return (uint32_t) address < (uint32_t) rhs.address; //assume that you compare the record based on a + return (uint32_t) address < (uint32_t) rhs.address; //assume that you compare the record based on a } [[nodiscard]] const char *getName() const { diff --git a/source/module/HookData.h b/source/module/HookData.h index f89de74..e2905c8 100644 --- a/source/module/HookData.h +++ b/source/module/HookData.h @@ -5,7 +5,7 @@ class HookData { public: HookData(wums_hook_type_t type, const void *target) { - this->type = type; + this->type = type; this->target = target; } diff --git a/source/module/ImportRPLInformation.h b/source/module/ImportRPLInformation.h index 49d1d54..4e3c42f 100644 --- a/source/module/ImportRPLInformation.h +++ b/source/module/ImportRPLInformation.h @@ -17,16 +17,17 @@ #pragma once -#include -#include -#include #include "utils/logger.h" +#include +#include +#include +#include class ImportRPLInformation { public: explicit ImportRPLInformation(std::string name, bool isData = false) { - this->name = std::move(name); + this->name = std::move(name); this->_isData = isData; } @@ -46,7 +47,7 @@ public: rplName = rawSectionName.substr(fimport.size()); } else if (std::equal(dimport.begin(), dimport.end(), rawSectionName.begin())) { rplName = rawSectionName.substr(dimport.size()); - data = true; + data = true; } else { DEBUG_FUNCTION_LINE("invalid section name\n"); return std::nullopt; diff --git a/source/module/ModuleData.cpp b/source/module/ModuleData.cpp index 11f252b..5e50930 100644 --- a/source/module/ModuleData.cpp +++ b/source/module/ModuleData.cpp @@ -3,7 +3,7 @@ std::string ModuleData::toString() const { std::string res = StringTools::strfmt("Entrypoint %08X, bss: %08X (%d), bss: %08X (%d)\n", getEntrypoint(), getBSSAddr(), getBSSSize(), getSBSSAddr(), getSBSSSize()); - for (auto const &reloc: relocation_data_list) { + for (auto const &reloc : relocation_data_list) { res += reloc->toString(); } return res; diff --git a/source/module/ModuleData.h b/source/module/ModuleData.h index 7253235..94b9a99 100644 --- a/source/module/ModuleData.h +++ b/source/module/ModuleData.h @@ -17,15 +17,15 @@ #pragma once -#include -#include -#include -#include +#include "ExportData.h" +#include "FunctionSymbolData.h" +#include "HookData.h" #include "RelocationData.h" #include "SectionInfo.h" -#include "ExportData.h" -#include "HookData.h" -#include "FunctionSymbolData.h" +#include +#include +#include +#include struct FunctionSymbolDataComparator { bool operator()(const std::shared_ptr &lhs, @@ -164,6 +164,7 @@ public: } bool relocationsDone = false; + private: std::vector> relocation_data_list; std::vector> export_data_list; @@ -173,13 +174,13 @@ private: std::string export_name; - uint32_t bssAddr = 0; - uint32_t bssSize = 0; - uint32_t sbssAddr = 0; - uint32_t sbssSize = 0; - uint32_t startAddress = 0; - uint32_t endAddress = 0; - uint32_t entrypoint = 0; + uint32_t bssAddr = 0; + uint32_t bssSize = 0; + uint32_t sbssAddr = 0; + uint32_t sbssSize = 0; + uint32_t startAddress = 0; + uint32_t endAddress = 0; + uint32_t entrypoint = 0; bool initBeforeRelocationDoneHook = false; - bool skipInitFini = false; + bool skipInitFini = false; }; diff --git a/source/module/ModuleDataFactory.cpp b/source/module/ModuleDataFactory.cpp index 2848723..a6d7371 100644 --- a/source/module/ModuleDataFactory.cpp +++ b/source/module/ModuleDataFactory.cpp @@ -15,15 +15,15 @@ * along with this program. If not, see . ****************************************************************************/ -#include -#include -#include -#include -#include #include "ModuleDataFactory.h" -#include "utils/utils.h" #include "ElfUtils.h" #include "FunctionSymbolData.h" +#include "utils/utils.h" +#include +#include +#include +#include +#include using namespace ELFIO; @@ -49,7 +49,7 @@ ModuleDataFactory::load(const std::string &path, uint32_t *destination_address_p uint32_t entrypoint = offset_text + (uint32_t) reader.get_entry() - 0x02000000; - uint32_t totalSize = 0; + uint32_t totalSize = 0; uint32_t endAddress = 0; for (uint32_t i = 0; i < sec_num; ++i) { @@ -133,18 +133,18 @@ ModuleDataFactory::load(const std::string &path, uint32_t *destination_address_p } auto relocationData = getImportRelocationData(reader, destinations); - for (auto const &reloc: relocationData) { + for (auto const &reloc : relocationData) { moduleData->addRelocationData(reloc); } auto secInfo = moduleData->getSectionInfo(".wums.exports"); if (secInfo && secInfo.value()->getSize() > 0) { size_t entries_count = secInfo.value()->getSize() / sizeof(wums_entry_t); - auto *entries = (wums_entry_t *) secInfo.value()->getAddress(); + auto *entries = (wums_entry_t *) secInfo.value()->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); + DEBUG_FUNCTION_LINE("Saving export of type %08X, name %s, target: %08X" /*,pluginData.getPluginInformation()->getName().c_str()*/, exp->type, exp->name, (void *) exp->address); auto exportData = std::make_shared(exp->type, exp->name, exp->address); moduleData->addExportData(exportData); } @@ -154,11 +154,11 @@ ModuleDataFactory::load(const std::string &path, uint32_t *destination_address_p secInfo = moduleData->getSectionInfo(".wums.hooks"); if (secInfo && secInfo.value()->getSize() > 0) { size_t entries_count = secInfo.value()->getSize() / sizeof(wums_hook_t); - auto *hooks = (wums_hook_t *) secInfo.value()->getAddress(); + auto *hooks = (wums_hook_t *) secInfo.value()->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); + DEBUG_FUNCTION_LINE("Saving hook of type %08X, target: %08X" /*,pluginData.getPluginInformation()->getName().c_str()*/, hook->type, hook->target); auto hookData = std::make_shared(hook->type, hook->target); moduleData->addHookData(hookData); } @@ -212,7 +212,7 @@ ModuleDataFactory::load(const std::string &path, uint32_t *destination_address_p } } - char *strTable = (char *) endAddress; + char *strTable = (char *) endAddress; uint32_t strOffset = 0; // Get the symbol for functions. @@ -225,16 +225,16 @@ ModuleDataFactory::load(const std::string &path, uint32_t *destination_address_p if (sym_no > 0) { for (Elf_Half j = 0; j < sym_no; ++j) { std::string name; - Elf64_Addr value = 0; - Elf_Xword size = 0; - unsigned char bind = 0; - unsigned char type = 0; - Elf_Half section = 0; + Elf64_Addr value = 0; + Elf_Xword size = 0; + unsigned char bind = 0; + unsigned char type = 0; + Elf_Half section = 0; unsigned char other = 0; if (symbols.get_symbol(j, name, value, size, bind, type, section, other)) { if (type == STT_FUNC) { // We only care about functions. auto sectionVal = reader.sections[section]; - auto offsetVal = value - sectionVal->get_address(); + auto offsetVal = value - sectionVal->get_address(); auto sectionOpt = moduleData->getSectionInfo(sectionVal->get_name()); if (!sectionOpt.has_value()) { continue; diff --git a/source/module/ModuleDataFactory.h b/source/module/ModuleDataFactory.h index 0601118..0fba3a9 100644 --- a/source/module/ModuleDataFactory.h +++ b/source/module/ModuleDataFactory.h @@ -17,12 +17,12 @@ #pragma once -#include -#include -#include -#include #include "ModuleData.h" #include "elfio/elfio.hpp" +#include +#include +#include +#include class ModuleDataFactory { public: diff --git a/source/module/ModuleDataPersistence.cpp b/source/module/ModuleDataPersistence.cpp index 667e49f..3923e3b 100644 --- a/source/module/ModuleDataPersistence.cpp +++ b/source/module/ModuleDataPersistence.cpp @@ -1,6 +1,6 @@ -#include #include "ModuleDataPersistence.h" #include "DynamicLinkingHelper.h" +#include bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformation, const std::shared_ptr &module) { int32_t module_count = moduleInformation->number_used_modules; @@ -15,7 +15,7 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati DEBUG_FUNCTION_LINE("Saving relocation data for module at %08X", module->getEntrypoint()); // Relocation auto relocationData = module->getRelocationDataList(); - for (auto const &reloc: relocationData) { + for (auto const &reloc : relocationData) { if (!DynamicLinkingHelper::addRelocationEntry(&(moduleInformation->linking_data), module_data->linking_entries, DYN_LINK_RELOCATION_LIST_LENGTH, reloc)) { DEBUG_FUNCTION_LINE("Failed to add relocation entry\n"); @@ -24,15 +24,15 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati } auto exportData = module->getExportDataList(); - for (auto const &curExport: exportData) { + for (auto const &curExport : exportData) { bool found = false; - for (auto &export_entry: module_data->export_entries) { + for (auto &export_entry : module_data->export_entries) { if (export_entry.address == 0) { - export_entry.type = curExport->getType(); + 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; + found = true; break; } } @@ -43,13 +43,13 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati } auto hookData = module->getHookDataList(); - for (auto const &curHook: hookData) { + for (auto const &curHook : hookData) { bool found = false; - for (auto &hook_entry: module_data->hook_entries) { + for (auto &hook_entry : module_data->hook_entries) { if (hook_entry.target == 0) { - hook_entry.type = curHook->getType(); + hook_entry.type = curHook->getType(); hook_entry.target = (uint32_t) curHook->getTarget(); - found = true; + found = true; break; } } @@ -63,36 +63,36 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati uint32_t entryCount = module->getFunctionSymbolDataList().size(); if (entryCount > 0) { - auto ptr = &moduleInformation->function_symbols[moduleInformation->number_used_function_symbols]; + auto ptr = &moduleInformation->function_symbols[moduleInformation->number_used_function_symbols]; module_data->function_symbol_entries = ptr; uint32_t sym_offset = 0; - for (auto &curFuncSym: module->getFunctionSymbolDataList()) { + for (auto &curFuncSym : module->getFunctionSymbolDataList()) { if (moduleInformation->number_used_function_symbols >= FUNCTION_SYMBOL_LIST_LENGTH) { DEBUG_FUNCTION_LINE("Function symbol list is full"); break; } module_data->function_symbol_entries[sym_offset].address = curFuncSym->getAddress(); - module_data->function_symbol_entries[sym_offset].name = (char *) curFuncSym->getName(); - module_data->function_symbol_entries[sym_offset].size = curFuncSym->getSize(); + module_data->function_symbol_entries[sym_offset].name = (char *) curFuncSym->getName(); + module_data->function_symbol_entries[sym_offset].size = curFuncSym->getSize(); sym_offset++; moduleInformation->number_used_function_symbols++; } module_data->number_used_function_symbols = sym_offset; } else { - module_data->function_symbol_entries = nullptr; + module_data->function_symbol_entries = nullptr; module_data->number_used_function_symbols = 0; } - module_data->bssAddr = module->getBSSAddr(); - module_data->bssSize = module->getBSSSize(); - module_data->sbssAddr = module->getSBSSAddr(); - module_data->sbssSize = module->getSBSSSize(); - module_data->startAddress = module->getStartAddress(); - module_data->endAddress = module->getEndAddress(); - module_data->entrypoint = module->getEntrypoint(); - module_data->skipInitFini = module->isSkipInitFini(); + module_data->bssAddr = module->getBSSAddr(); + module_data->bssSize = module->getBSSSize(); + module_data->sbssAddr = module->getSBSSAddr(); + module_data->sbssSize = module->getSBSSSize(); + module_data->startAddress = module->getStartAddress(); + module_data->endAddress = module->getEndAddress(); + module_data->entrypoint = module->getEntrypoint(); + module_data->skipInitFini = module->isSkipInitFini(); module_data->initBeforeRelocationDoneHook = module->isInitBeforeRelocationDoneHook(); moduleInformation->number_used_modules++; @@ -120,7 +120,7 @@ std::vector> ModuleDataPersistence::loadModuleData(m for (int32_t i = 0; i < module_count; i++) { // Copy data from struct. module_information_single_t *module_data = &(moduleInformation->module_data[i]); - auto moduleData = std::make_shared(); + auto moduleData = std::make_shared(); moduleData->setBSSLocation(module_data->bssAddr, module_data->bssSize); moduleData->setSBSSLocation(module_data->sbssAddr, module_data->sbssSize); moduleData->setEntrypoint(module_data->entrypoint); @@ -130,7 +130,7 @@ std::vector> ModuleDataPersistence::loadModuleData(m moduleData->setSkipInitFini(module_data->skipInitFini); moduleData->setInitBeforeRelocationDoneHook(module_data->initBeforeRelocationDoneHook); - for (auto &export_entrie: module_data->export_entries) { + for (auto &export_entrie : module_data->export_entries) { export_data_t *export_entry = &export_entrie; if (export_entry->address == 0) { continue; @@ -139,7 +139,7 @@ std::vector> ModuleDataPersistence::loadModuleData(m moduleData->addExportData(exportData); } - for (auto &hook_entry: module_data->hook_entries) { + for (auto &hook_entry : module_data->hook_entries) { if (hook_entry.target == 0) { continue; } @@ -147,7 +147,7 @@ std::vector> ModuleDataPersistence::loadModuleData(m moduleData->addHookData(hookData); } - for (auto &linking_entry: module_data->linking_entries) { + for (auto &linking_entry : module_data->linking_entries) { if (linking_entry.destination == nullptr) { break; } @@ -163,14 +163,14 @@ std::vector> ModuleDataPersistence::loadModuleData(m continue; } auto rplInfo = std::make_shared(importEntry->importName, importEntry->isData); - auto reloc = std::make_shared(linking_entry.type, linking_entry.offset, linking_entry.addend, linking_entry.destination, functionEntry->functionName, rplInfo); + auto reloc = std::make_shared(linking_entry.type, linking_entry.offset, linking_entry.addend, linking_entry.destination, functionEntry->functionName, rplInfo); moduleData->addRelocationData(reloc); } if (module_data->function_symbol_entries != nullptr && module_data->number_used_function_symbols > 0) { for (uint32_t j = 0; j < module_data->number_used_function_symbols; j++) { - auto symbol = &module_data->function_symbol_entries[j]; + auto symbol = &module_data->function_symbol_entries[j]; auto functionSymbolData = std::make_shared(symbol->name, symbol->address, symbol->size); moduleData->addFunctionSymbolData(functionSymbolData); } diff --git a/source/module/ModuleDataPersistence.h b/source/module/ModuleDataPersistence.h index ef830d8..2bf4515 100644 --- a/source/module/ModuleDataPersistence.h +++ b/source/module/ModuleDataPersistence.h @@ -1,7 +1,7 @@ #pragma once -#include #include "ModuleData.h" +#include class ModuleDataPersistence { public: diff --git a/source/module/RelocationData.h b/source/module/RelocationData.h index cbf4b8c..06cf5c2 100644 --- a/source/module/RelocationData.h +++ b/source/module/RelocationData.h @@ -17,20 +17,20 @@ #pragma once -#include -#include -#include #include "ImportRPLInformation.h" +#include +#include +#include class RelocationData { public: RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, std::shared_ptr rplInfo) : rplInfo(std::move(rplInfo)) { - this->type = type; - this->offset = offset; - this->addend = addend; + this->type = type; + this->offset = offset; + this->addend = addend; this->destination = destination; - this->name = std::move(name); + this->name = std::move(name); } ~RelocationData() = default; diff --git a/source/module/SectionInfo.h b/source/module/SectionInfo.h index 8c7d30a..0ca09e9 100644 --- a/source/module/SectionInfo.h +++ b/source/module/SectionInfo.h @@ -23,18 +23,16 @@ class SectionInfo { public: - SectionInfo(std::string name, uint32_t address, uint32_t sectionSize) : - name(std::move(name)), - address(address), - sectionSize(sectionSize) { + SectionInfo(std::string name, uint32_t address, uint32_t sectionSize) : name(std::move(name)), + address(address), + sectionSize(sectionSize) { } SectionInfo() = default; - SectionInfo(const SectionInfo &o2) : - name(o2.name), - address(o2.address), - sectionSize(o2.sectionSize) { + SectionInfo(const SectionInfo &o2) : name(o2.name), + address(o2.address), + sectionSize(o2.sectionSize) { } SectionInfo &operator=(const SectionInfo &other) = default; diff --git a/source/utils/StringTools.cpp b/source/utils/StringTools.cpp index a97e3b9..726264c 100644 --- a/source/utils/StringTools.cpp +++ b/source/utils/StringTools.cpp @@ -23,17 +23,16 @@ * * for WiiXplorer 2010 ***************************************************************************/ -#include -#include -#include #include +#include #include -#include -#include +#include +#include #include -#include -#include #include +#include +#include +#include BOOL StringTools::EndsWith(const std::string &a, const std::string &b) { @@ -85,7 +84,7 @@ const wchar_t *StringTools::wfmt(const char *format, ...) { static char tmp[512]; static wchar_t strWChar[512]; strWChar[0] = 0; - tmp[0] = 0; + tmp[0] = 0; if (!format) return (const wchar_t *) strWChar; @@ -98,7 +97,7 @@ const wchar_t *StringTools::wfmt(const char *format, ...) { if ((vsprintf(tmp, format, va) >= 0)) { int bt; int32_t strlength = strlen(tmp); - bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512); + bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512); if (bt > 0) { strWChar[bt] = 0; @@ -112,13 +111,13 @@ const wchar_t *StringTools::wfmt(const char *format, ...) { int32_t StringTools::strprintf(std::string &str, const char *format, ...) { static char tmp[512]; - tmp[0] = 0; + tmp[0] = 0; int32_t result = 0; va_list va; va_start(va, format); if ((vsprintf(tmp, format, va) >= 0)) { - str = tmp; + str = tmp; result = str.size(); } va_end(va); @@ -215,7 +214,7 @@ const char *StringTools::FullpathToFilename(const char *path) { if (!path) return path; - const char *ptr = path; + const char *ptr = path; const char *Filename = ptr; while (*ptr != '\0') { @@ -244,13 +243,13 @@ void StringTools::RemoveDoubleSlashs(std::string &str) { // You must free the result if result is non-NULL. char *StringTools::str_replace(char *orig, char *rep, char *with) { - char *result; // the return string - char *ins; // the next insert point - char *tmp; // varies - int len_rep; // length of rep (the string to remove) - int len_with; // length of with (the string to replace rep with) + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + int len_rep; // length of rep (the string to remove) + int len_with; // length of with (the string to replace rep with) int len_front; // distance between rep and end of last rep - int count; // number of replacements + int count; // number of replacements // sanity checks and initialization if (!orig || !rep) @@ -279,10 +278,10 @@ char *StringTools::str_replace(char *orig, char *rep, char *with) { // ins points to the next occurrence of rep in orig // orig points to the remainder of orig after "end of rep" while (count--) { - ins = strstr(orig, rep); + ins = strstr(orig, rep); len_front = ins - orig; - tmp = strncpy(tmp, orig, len_front) + len_front; - tmp = strcpy(tmp, with) + len_with; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; orig += len_front + len_rep; // move to next "end of rep" } strcpy(tmp, orig); diff --git a/source/utils/StringTools.h b/source/utils/StringTools.h index 851aa73..d1a1570 100644 --- a/source/utils/StringTools.h +++ b/source/utils/StringTools.h @@ -26,8 +26,8 @@ #ifndef __STRING_TOOLS_H #define __STRING_TOOLS_H -#include #include +#include #include class StringTools { @@ -62,4 +62,3 @@ public: }; #endif /* __STRING_TOOLS_H */ - diff --git a/source/utils/logger.c b/source/utils/logger.c index 0411db7..f700806 100644 --- a/source/utils/logger.c +++ b/source/utils/logger.c @@ -1,19 +1,19 @@ #ifdef DEBUG #include -#include #include #include +#include uint32_t moduleLogInit = false; -uint32_t cafeLogInit = false; -uint32_t udpLogInit = false; +uint32_t cafeLogInit = false; +uint32_t udpLogInit = false; #endif // DEBUG void initLogging() { #ifdef DEBUG if (!(moduleLogInit = WHBLogModuleInit())) { cafeLogInit = WHBLogCafeInit(); - udpLogInit = WHBLogUdpInit(); + udpLogInit = WHBLogUdpInit(); } #endif // DEBUG } diff --git a/source/utils/logger.h b/source/utils/logger.h index 6ff5ae2..061d52e 100644 --- a/source/utils/logger.h +++ b/source/utils/logger.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #ifdef __cplusplus extern "C" { @@ -9,26 +9,28 @@ extern "C" { #ifdef DEBUG -#define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) +#define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) +#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) #define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) -#define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ - WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) \ + do { \ + 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); \ +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) \ + do { \ + WHBLogWritef("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ } while (0) #else #define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) -#define DEBUG_FUNCTION_LINE(FMT, ARGS...) while (0) +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) while (0) -#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) while (0) +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) while (0) #endif @@ -39,4 +41,3 @@ void deinitLogging(); #ifdef __cplusplus } #endif - diff --git a/source/utils/utils.c b/source/utils/utils.c index 29ff0a1..e5a7dcd 100644 --- a/source/utils/utils.c +++ b/source/utils/utils.c @@ -1,7 +1,7 @@ -#include -#include -#include #include "utils/logger.h" +#include +#include +#include // https://gist.github.com/ccbrown/9722406 void dumpHex(const void *data, size_t size) { diff --git a/source/utils/utils.h b/source/utils/utils.h index a502522..2890779 100644 --- a/source/utils/utils.h +++ b/source/utils/utils.h @@ -7,28 +7,29 @@ extern "C" { #endif -#define LIMIT(x, min, max) \ - ({ \ - typeof( x ) _x = x; \ - typeof( min ) _min = min; \ - typeof( max ) _max = max; \ - ( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \ -}) +#define LIMIT(x, min, max) \ + ({ \ + typeof(x) _x = x; \ + typeof(min) _min = min; \ + typeof(max) _max = max; \ + (((_x) < (_min)) ? (_min) : ((_x) > (_max)) ? (_max) \ + : (_x)); \ + }) -#define DegToRad(a) ( (a) * 0.01745329252f ) -#define RadToDeg(a) ( (a) * 57.29577951f ) +#define DegToRad(a) ((a) *0.01745329252f) +#define RadToDeg(a) ((a) *57.29577951f) -#define ALIGN4(x) (((x) + 3) & ~3) -#define ALIGN32(x) (((x) + 31) & ~31) +#define ALIGN4(x) (((x) + 3) & ~3) +#define ALIGN32(x) (((x) + 31) & ~31) // those work only in powers of 2 -#define ROUNDDOWN(val, align) ((val) & ~(align-1)) -#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align-1)), align) +#define ROUNDDOWN(val, align) ((val) & ~(align - 1)) +#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align) -#define le16(i) ((((uint16_t) ((i) & 0xFF)) << 8) | ((uint16_t) (((i) & 0xFF00) >> 8))) -#define le32(i) ((((uint32_t)le16((i) & 0xFFFF)) << 16) | ((uint32_t)le16(((i) & 0xFFFF0000) >> 16))) -#define le64(i) ((((uint64_t)le32((i) & 0xFFFFFFFFLL)) << 32) | ((uint64_t)le32(((i) & 0xFFFFFFFF00000000LL) >> 32))) +#define le16(i) ((((uint16_t) ((i) &0xFF)) << 8) | ((uint16_t) (((i) &0xFF00) >> 8))) +#define le32(i) ((((uint32_t) le16((i) &0xFFFF)) << 16) | ((uint32_t) le16(((i) &0xFFFF0000) >> 16))) +#define le64(i) ((((uint64_t) le32((i) &0xFFFFFFFFLL)) << 32) | ((uint64_t) le32(((i) &0xFFFFFFFF00000000LL) >> 32))) //Needs to have log_init() called beforehand. void dumpHex(const void *data, size_t size);