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 99de3cc..a83349c 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 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 0413d45..19c3b14 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 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 d6650e9..482c863 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![CI-Release](https://github.com/wiiu-env/PatchMemoryRelocationsModule/actions/workflows/ci.yml/badge.svg)](https://github.com/wiiu-env/PatchMemoryRelocationsModule/actions/workflows/ci.yml) + # PatchMemoryRelocationsModule Replaces the usage of `MEMAllocFromDefaultHeap`, `MEMAllocFromDefaultHeapEx` and `MEMFreeToDefaultHeap` with similar function from the MemoryMappingModule in the currently running modules. @@ -22,6 +24,10 @@ docker run -it --rm -v ${PWD}:/project patchmemoryrelocationsmodule-builder make docker run -it --rm -v ${PWD}:/project patchmemoryrelocationsmodule-builder make clean ``` +## Format the code via docker + +`docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source -i` + # Dependencies: - [wut](https://github.com/decaf-emu/wut) - [WUMS](https://github.com/wiiu-env/WiiUModuleSystem) diff --git a/source/logger.c b/source/logger.c index 0411db7..f700806 100644 --- a/source/logger.c +++ b/source/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/logger.h b/source/logger.h index 6ff5ae2..061d52e 100644 --- a/source/logger.h +++ b/source/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/main.cpp b/source/main.cpp index b2a9a55..53b3b32 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,10 +1,10 @@ #include +#include "logger.h" +#include #include #include -#include #include -#include "logger.h" WUMS_MODULE_EXPORT_NAME("homebrew_patchmemoryrelocations"); WUMS_MODULE_INIT_BEFORE_RELOCATION_DONE_HOOK(); @@ -31,7 +31,7 @@ WUMS_RELOCATIONS_DONE(args) { continue; } DEBUG_FUNCTION_LINE_VERBOSE("Patch relocations of %s", gModuleData->module_data[i].module_export_name); - for (auto &curEntry: gModuleData->module_data[i].linking_entries) { + for (auto &curEntry : gModuleData->module_data[i].linking_entries) { if (curEntry.functionEntry == nullptr) { continue; } @@ -57,18 +57,18 @@ WUMS_RELOCATIONS_DONE(args) { deinitLogging(); } -#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_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_GHS_REL16_HA 251 +#define R_PPC_GHS_REL16_HI 252 +#define R_PPC_GHS_REL16_LO 253 // See https://github.com/decaf-emu/decaf-emu/blob/43366a34e7b55ab9d19b2444aeb0ccd46ac77dea/src/libdecaf/src/cafe/loader/cafe_loader_reloc.cpp#L144 bool 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, @@ -77,7 +77,7 @@ bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, return true; } auto target = destination + offset; - auto value = symbol_addr + addend; + auto value = symbol_addr + addend; auto relValue = value - static_cast(target); @@ -164,16 +164,16 @@ bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, distance); 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, distance); 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)); @@ -183,9 +183,9 @@ bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, // Relocations for the imports may be overridden freeSlot->status = RELOC_TRAMP_IMPORT_IN_PROGRESS; } - uint32_t symbolValue = (uint32_t) &(freeSlot->trampoline[0]); - value = symbolValue + addend; - distance = static_cast(value) - static_cast(target); + uint32_t symbolValue = (uint32_t) & (freeSlot->trampoline[0]); + value = symbolValue + addend; + distance = static_cast(value) - static_cast(target); DEBUG_FUNCTION_LINE("Created tramp\n"); } }