diff --git a/Dockerfile b/Dockerfile index a32d9e5..9ec7aa1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM wiiuenv/devkitppc:20211229 -COPY --from=wiiuenv/wiiumodulesystem:20220123 /artifacts $DEVKITPRO +COPY --from=wiiuenv/wiiumodulesystem:20220127 /artifacts $DEVKITPRO WORKDIR project \ No newline at end of file diff --git a/relocator/src/ElfUtils.cpp b/relocator/src/ElfUtils.cpp index 3edf605..5f7ba81 100644 --- a/relocator/src/ElfUtils.cpp +++ b/relocator/src/ElfUtils.cpp @@ -4,7 +4,7 @@ #include "ElfUtils.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_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, +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, RelocationType reloc_type) { if (type == R_PPC_NONE) { return true; @@ -73,43 +73,43 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des // } auto distance = static_cast(value) - static_cast(target); if (distance > 0x1FFFFFC || distance < -0x1FFFFFC) { - if (trampolin_data == nullptr) { - DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin isn't provided\n"); + if (trampoline_data == nullptr) { + DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampoline isn't provided\n"); DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, distance); return false; } else { - relocation_trampolin_entry_t *freeSlot = nullptr; - for (uint32_t i = 0; i < trampolin_data_length; i++) { + relocation_trampoline_entry_t *freeSlot = nullptr; + for (uint32_t i = 0; i < trampoline_data_length; i++) { // We want to override "old" relocations of imports // Pending relocations have the status RELOC_TRAMP_IMPORT_IN_PROGRESS. // When all relocations are done successfully, they will be turned into RELOC_TRAMP_IMPORT_DONE // so they can be overridden/updated/reused on the next application launch. // // Relocations that won't change will have the status RELOC_TRAMP_FIXED and are set to free when the module is unloaded. - if (trampolin_data[i].status == RELOC_TRAMP_FREE || - trampolin_data[i].status == RELOC_TRAMP_IMPORT_DONE) { - freeSlot = &(trampolin_data[i]); + if (trampoline_data[i].status == RELOC_TRAMP_FREE || + trampoline_data[i].status == RELOC_TRAMP_IMPORT_DONE) { + freeSlot = &(trampoline_data[i]); break; } } if (freeSlot == nullptr) { - DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin data list is full\n"); - DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) &(freeSlot->trampolin[0]))); + 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]))); return false; } - if (target - (uint32_t) &(freeSlot->trampolin[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->trampolin[0]))); + DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) &(freeSlot->trampoline[0]))); return false; } - freeSlot->trampolin[0] = 0x3D600000 | ((((uint32_t) value) >> 16) & 0x0000FFFF); // lis r11, real_addr@h - freeSlot->trampolin[1] = 0x616B0000 | (((uint32_t) value) & 0x0000ffff); // ori r11, r11, real_addr@l - freeSlot->trampolin[2] = 0x7D6903A6; // mtctr r11 - freeSlot->trampolin[3] = 0x4E800420; // bctr - DCFlushRange((void *) freeSlot->trampolin, sizeof(freeSlot->trampolin)); - ICInvalidateRange((unsigned char *) freeSlot->trampolin, sizeof(freeSlot->trampolin)); + 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 + DCFlushRange((void *) freeSlot->trampoline, sizeof(freeSlot->trampoline)); + ICInvalidateRange((unsigned char *) freeSlot->trampoline, sizeof(freeSlot->trampoline)); if (reloc_type == RELOC_TYPE_FIXED) { freeSlot->status = RELOC_TRAMP_FIXED; @@ -117,7 +117,7 @@ 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->trampolin[0]); + 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 586cd08..fafcbf9 100644 --- a/relocator/src/ElfUtils.h +++ b/relocator/src/ElfUtils.h @@ -42,6 +42,6 @@ extern "C" { class ElfUtils { public: - static bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, + static 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, RelocationType reloc_type); }; diff --git a/relocator/src/entry.cpp b/relocator/src/entry.cpp index 60f0760..d2861e9 100644 --- a/relocator/src/entry.cpp +++ b/relocator/src/entry.cpp @@ -48,7 +48,7 @@ extern "C" int _start(int argc, char **argv) { return ((int (*)(int, char **)) (*(unsigned int *) 0x1005E040))(argc, argv); } -bool doRelocation(std::vector> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length, bool skipAllocReplacement) { +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) { std::string functionName = curReloc->getName(); @@ -107,8 +107,8 @@ bool doRelocation(std::vector> &relocData, reloc } } - DCFlushRange(tramp_data, tramp_length * sizeof(relocation_trampolin_entry_t)); - ICInvalidateRange(tramp_data, tramp_length * sizeof(relocation_trampolin_entry_t)); + DCFlushRange(tramp_data, tramp_length * sizeof(relocation_trampoline_entry_t)); + ICInvalidateRange(tramp_data, tramp_length * sizeof(relocation_trampoline_entry_t)); return true; } @@ -126,7 +126,7 @@ bool ResolveRelocations(std::vector> &loadedM // once with the default heap. Afterwards we can just rely on the custom heap. bool skipAllocFunction = skipMemoryMappingModule && (curModule->getExportName() == "homebrew_memorymapping" || curModule->getExportName() == "homebrew_functionpatcher"); DEBUG_FUNCTION_LINE_VERBOSE("Skip alloc replace? %d\n", skipAllocFunction); - if (!doRelocation(relocData, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH, skipAllocFunction)) { + if (!doRelocation(relocData, gModuleData->trampolines, DYN_LINK_TRAMPOLINE_LIST_LENGTH, skipAllocFunction)) { DEBUG_FUNCTION_LINE("FAIL\n"); wasSuccessful = false; curModule->relocationsDone = false; diff --git a/source/ElfUtils.cpp b/source/ElfUtils.cpp index 54da647..873c7d2 100644 --- a/source/ElfUtils.cpp +++ b/source/ElfUtils.cpp @@ -108,7 +108,7 @@ uint32_t load_loader_elf(unsigned char *baseAddress, char *elf_data, uint32_t fi } // 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_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, +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, RelocationType reloc_type) { if (type == R_PPC_NONE) { return true; @@ -178,42 +178,42 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des // } auto distance = static_cast(value) - static_cast(target); if (distance > 0x1FFFFFC || distance < -0x1FFFFFC) { - if (trampolin_data == nullptr) { - DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin isn't provided\n"); + if (trampoline_data == nullptr) { + DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampoline isn't provided\n"); DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, distance); return false; } else { - relocation_trampolin_entry_t *freeSlot = nullptr; - for (uint32_t i = 0; i < trampolin_data_length; i++) { + relocation_trampoline_entry_t *freeSlot = nullptr; + for (uint32_t i = 0; i < trampoline_data_length; i++) { // We want to override "old" relocations of imports // Pending relocations have the status RELOC_TRAMP_IMPORT_IN_PROGRESS. // When all relocations are done successfully, they will be turned into RELOC_TRAMP_IMPORT_DONE // so they can be overridden/updated/reused on the next application launch. // // Relocations that won't change will have the status RELOC_TRAMP_FIXED and are set to free when the module is unloaded. - if (trampolin_data[i].status == RELOC_TRAMP_FREE || - trampolin_data[i].status == RELOC_TRAMP_IMPORT_DONE) { - freeSlot = &(trampolin_data[i]); + if (trampoline_data[i].status == RELOC_TRAMP_FREE || + trampoline_data[i].status == RELOC_TRAMP_IMPORT_DONE) { + freeSlot = &(trampoline_data[i]); break; } } if (freeSlot == nullptr) { - DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin data list is full\n"); - DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) &(freeSlot->trampolin[0]))); + 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]))); return false; } - if (target - (uint32_t) &(freeSlot->trampolin[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->trampolin[0]))); + DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, (target - (uint32_t) &(freeSlot->trampoline[0]))); return false; } - freeSlot->trampolin[0] = 0x3D600000 | ((((uint32_t) value) >> 16) & 0x0000FFFF); // lis r11, real_addr@h - freeSlot->trampolin[1] = 0x616B0000 | (((uint32_t) value) & 0x0000ffff); // ori r11, r11, real_addr@l - freeSlot->trampolin[2] = 0x7D6903A6; // mtctr r11 - freeSlot->trampolin[3] = 0x4E800420; // bctr - DCFlushRange((void *) freeSlot->trampolin, sizeof(freeSlot->trampolin)); - ICInvalidateRange((unsigned char *) freeSlot->trampolin, sizeof(freeSlot->trampolin)); + 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 + DCFlushRange((void *) freeSlot->trampoline, sizeof(freeSlot->trampoline)); + ICInvalidateRange((unsigned char *) freeSlot->trampoline, sizeof(freeSlot->trampoline)); if (reloc_type == RELOC_TYPE_FIXED) { freeSlot->status = RELOC_TRAMP_FIXED; @@ -221,7 +221,7 @@ 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->trampolin[0]); + 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 44a3356..abd17ea 100644 --- a/source/ElfUtils.h +++ b/source/ElfUtils.h @@ -47,6 +47,6 @@ uint32_t load_loader_elf(unsigned char *baseAddress, char *elf_data, uint32_t fi class ElfUtils { public: - static bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, + static 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, RelocationType reloc_type); }; diff --git a/source/main.cpp b/source/main.cpp index 82f3bd9..e29735c 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -35,7 +35,7 @@ int main(int argc, char **argv) { for (int i = 0; i < modules.GetFilecount(); i++) { DEBUG_FUNCTION_LINE("Loading module %s", modules.GetFilepath(i)); auto moduleData = ModuleDataFactory::load(modules.GetFilepath(i), &destination_address, textSectionStart - destination_address, gModuleData->trampolines, - DYN_LINK_TRAMPOLIN_LIST_LENGTH); + DYN_LINK_TRAMPOLINE_LIST_LENGTH); if (moduleData) { DEBUG_FUNCTION_LINE("Successfully loaded %s", modules.GetFilepath(i)); ModuleDataPersistence::saveModuleData(gModuleData, moduleData.value()); diff --git a/source/module/DynamicLinkingHelper.h b/source/module/DynamicLinkingHelper.h index 3bd8b4f..f763415 100644 --- a/source/module/DynamicLinkingHelper.h +++ b/source/module/DynamicLinkingHelper.h @@ -47,10 +47,11 @@ public: **/ static dyn_linking_import_t *getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData); - static bool addRelocationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const std::shared_ptr &relocationData); + static bool addRelocationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, + const std::shared_ptr &relocationData); static bool addReloationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, - void *destination, const std::string& name, + void *destination, const std::string &name, const std::shared_ptr &rplInfo); static bool diff --git a/source/module/FunctionSymbolData.h b/source/module/FunctionSymbolData.h index d1b35e7..957bdc0 100644 --- a/source/module/FunctionSymbolData.h +++ b/source/module/FunctionSymbolData.h @@ -24,7 +24,7 @@ class FunctionSymbolData { public: FunctionSymbolData(const FunctionSymbolData &o2) = default; - FunctionSymbolData(const char* name, void *address, uint32_t size) : + FunctionSymbolData(const char *name, void *address, uint32_t size) : name(name), address(address), size(size) { @@ -50,7 +50,7 @@ public: private: - const char* name; + const char *name; void *address; uint32_t size; }; \ No newline at end of file diff --git a/source/module/ModuleDataFactory.cpp b/source/module/ModuleDataFactory.cpp index 964c6c7..2848723 100644 --- a/source/module/ModuleDataFactory.cpp +++ b/source/module/ModuleDataFactory.cpp @@ -28,7 +28,7 @@ using namespace ELFIO; 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) { +ModuleDataFactory::load(const std::string &path, uint32_t *destination_address_ptr, uint32_t maximum_size, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length) { elfio reader; std::shared_ptr moduleData = std::make_shared(); @@ -124,7 +124,7 @@ ModuleDataFactory::load(const std::string &path, uint32_t *destination_address_p section *psec = reader.sections[i]; if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) { DEBUG_FUNCTION_LINE("Linking (%d)... %s", i, psec->get_name().c_str()); - if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], offset_text, offset_data, trampolin_data, trampolin_data_length)) { + if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], offset_text, offset_data, trampoline_data, trampoline_data_length)) { DEBUG_FUNCTION_LINE("elfLink failed"); free(destinations); return std::nullopt; @@ -325,8 +325,8 @@ std::vector> ModuleDataFactory::getImportRelocat return result; } -bool ModuleDataFactory::linkSection(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) { +bool ModuleDataFactory::linkSection(elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampoline_entry_t *trampoline_data, + uint32_t trampoline_data_length) { uint32_t sec_num = reader.sections.size(); for (uint32_t i = 0; i < sec_num; ++i) { @@ -371,7 +371,7 @@ bool ModuleDataFactory::linkSection(elfio &reader, uint32_t section_index, uint3 return false; } - if (!ElfUtils::elfLinkOne(type, offset, addend, destination, adjusted_sym_value, trampolin_data, trampolin_data_length, RELOC_TYPE_FIXED)) { + if (!ElfUtils::elfLinkOne(type, offset, addend, destination, adjusted_sym_value, trampoline_data, trampoline_data_length, RELOC_TYPE_FIXED)) { DEBUG_FUNCTION_LINE("Link failed"); return false; } diff --git a/source/module/ModuleDataFactory.h b/source/module/ModuleDataFactory.h index d0a48ae..0601118 100644 --- a/source/module/ModuleDataFactory.h +++ b/source/module/ModuleDataFactory.h @@ -27,10 +27,10 @@ class ModuleDataFactory { public: 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); + load(const std::string &path, uint32_t *destination_address_ptr, uint32_t maximum_size, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_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); + static bool linkSection(ELFIO::elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampoline_entry_t *trampoline_data, + uint32_t trampoline_data_length); static std::vector> getImportRelocationData(ELFIO::elfio &reader, uint8_t **destinations); };