diff --git a/.gitignore b/.gitignore index 7aa1c01..698dbae 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ relocator.h cmake-build-debug/ *.txt build1/ +cmake-build-default/ diff --git a/relocator/src/DynamicLinkingHelper.cpp b/relocator/src/DynamicLinkingHelper.cpp index 0a9aec8..1018c38 100644 --- a/relocator/src/DynamicLinkingHelper.cpp +++ b/relocator/src/DynamicLinkingHelper.cpp @@ -5,16 +5,16 @@ dyn_linking_function_t *DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_li if (data == nullptr) { return nullptr; } - if (functionName == NULL) { - return NULL; + if (functionName == nullptr) { + return nullptr; } - dyn_linking_function_t *result = NULL; + dyn_linking_function_t *result = nullptr; for (auto & function : data->functions) { dyn_linking_function_t *curEntry = &function; if (strlen(curEntry->functionName) == 0) { if (strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) { DEBUG_FUNCTION_LINE("Failed to add function name, it's too long.\n"); - return NULL; + return nullptr; } strncpy(curEntry->functionName, functionName, DYN_LINK_FUNCTION_NAME_LENGTH); result = curEntry; @@ -37,16 +37,16 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddDataImportByName(dyn_linking } dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData) { - if (importName == NULL || data == NULL) { - return NULL; + if (importName == nullptr || data == nullptr) { + return nullptr; } - dyn_linking_import_t *result = NULL; + dyn_linking_import_t *result = nullptr; for (auto & import : data->imports) { dyn_linking_import_t *curEntry = &import; if (strlen(curEntry->importName) == 0) { if (strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) { DEBUG_FUNCTION_LINE("Failed to add Import, it's too long.\n"); - return NULL; + return nullptr; } strncpy(curEntry->importName, importName, DYN_LINK_IMPORT_NAME_LENGTH); curEntry->isData = isData; @@ -60,30 +60,33 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio return result; } -bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const RelocationData &relocationData) { - return addReloationEntry(linking_data, linking_entries, linking_entry_length, relocationData.getType(), relocationData.getOffset(), relocationData.getAddend(), relocationData.getDestination(), relocationData.getName(), - relocationData.getImportRPLInformation()); +bool DynamicLinkingHelper::addRelocationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const RelocationData &relocationData) { + return addRelocationEntry(linking_data, linking_entries, linking_entry_length, relocationData.getType(), + relocationData.getOffset(), relocationData.getAddend(), relocationData.getDestination(), + relocationData.getName(), + relocationData.getImportRPLInformation()); } -bool DynamicLinkingHelper::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, const ImportRPLInformation &rplInfo) { +bool DynamicLinkingHelper::addRelocationEntry(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, const ImportRPLInformation &rplInfo) { dyn_linking_import_t *importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo.getName().c_str(), rplInfo.isData()); - if (importInfoGbl == NULL) { + if (importInfoGbl == nullptr) { DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d rpl files to import reached.\n", DYN_LINK_IMPORT_LIST_LENGTH); return false; } dyn_linking_function_t *functionInfo = DynamicLinkingHelper::getOrAddFunctionEntryByName(linking_data, name.c_str()); - if (functionInfo == NULL) { + if (functionInfo == nullptr) { DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d function to be relocated reached.\n", DYN_LINK_FUNCTION_LIST_LENGTH); return false; } - return addReloationEntry(linking_entries, linking_entry_length, type, offset, addend, destination, functionInfo, importInfoGbl); + return addRelocationEntry(linking_entries, linking_entry_length, type, offset, addend, destination, functionInfo, + importInfoGbl); } -bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName, - dyn_linking_import_t *importInfo) { +bool DynamicLinkingHelper::addRelocationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName, + dyn_linking_import_t *importInfo) { for (uint32_t i = 0; i < linking_entry_length; i++) { dyn_linking_relocation_entry_t *curEntry = &(linking_entries[i]); if (curEntry->functionEntry != nullptr) { diff --git a/relocator/src/DynamicLinkingHelper.h b/relocator/src/DynamicLinkingHelper.h index e8d2cc6..3faf713 100644 --- a/relocator/src/DynamicLinkingHelper.h +++ b/relocator/src/DynamicLinkingHelper.h @@ -47,18 +47,16 @@ public: **/ static dyn_linking_import_t *getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData); - static bool addReloationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const RelocationData &relocationData); + static bool addRelocationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const RelocationData &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, - const ImportRPLInformation &rplInfo); + static bool addRelocationEntry(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, + const ImportRPLInformation &rplInfo); static bool - addReloationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName, dyn_linking_import_t *importInfo); + addRelocationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName, dyn_linking_import_t *importInfo); private: - DynamicLinkingHelper() { - } + DynamicLinkingHelper() = default; - ~DynamicLinkingHelper() { - } + ~DynamicLinkingHelper() = default; }; diff --git a/relocator/src/ElfUtils.cpp b/relocator/src/ElfUtils.cpp index 1e1a108..b77565c 100644 --- a/relocator/src/ElfUtils.cpp +++ b/relocator/src/ElfUtils.cpp @@ -72,12 +72,12 @@ 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 == NULL) { + if (trampolin_data == nullptr) { DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin 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 = NULL; + relocation_trampolin_entry_t *freeSlot = nullptr; for (uint32_t i = 0; i < trampolin_data_length; i++) { // We want to override "old" relocations of imports // Pending relocations have the status RELOC_TRAMP_IMPORT_IN_PROGRESS. @@ -115,7 +115,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_IN_PROGRESS; } - uint32_t symbolValue = (uint32_t) &(freeSlot->trampolin[0]); + auto symbolValue = (uint32_t) &(freeSlot->trampolin[0]); value = symbolValue + addend; distance = static_cast(value) - static_cast(target); DEBUG_FUNCTION_LINE("Created tramp\n"); diff --git a/relocator/src/entry.cpp b/relocator/src/entry.cpp index a0ef110..604192d 100644 --- a/relocator/src/entry.cpp +++ b/relocator/src/entry.cpp @@ -254,7 +254,7 @@ std::vector OrderModulesByDependencies(const std::vector canBreak = false; DEBUG_FUNCTION_LINE_VERBOSE("Check if we can load %s\n", curModule.getExportName().c_str()); std::vector importsFromOtherModules; - for (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()) { diff --git a/source/ElfUtils.cpp b/source/ElfUtils.cpp index 7531189..015f262 100644 --- a/source/ElfUtils.cpp +++ b/source/ElfUtils.cpp @@ -1,6 +1,3 @@ -#include -#include - #include #include #include @@ -15,7 +12,7 @@ int32_t LoadFileToMem(const char *relativefilepath, char **fileOut, uint32_t *sizeOut) { char path[256]; int result = 0; - char *sdRootPath = NULL; + char *sdRootPath = nullptr; if (!WHBMountSdCard()) { DEBUG_FUNCTION_LINE("Failed to mount SD Card..."); result = -1; @@ -40,7 +37,7 @@ int32_t LoadFileToMem(const char *relativefilepath, char **fileOut, uint32_t *si } uint32_t load_loader_elf_from_sd(unsigned char *baseAddress, const char *relativePath) { - char *elf_data = NULL; + 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."); @@ -95,7 +92,7 @@ uint32_t load_loader_elf(unsigned char *baseAddress, char *elf_data, uint32_t fi } //! clear BSS - ELFIO::Elf32_Shdr *shdr = (ELFIO::Elf32_Shdr *) (elf_data + ehdr->e_shoff); + auto *shdr = (ELFIO::Elf32_Shdr *) (elf_data + ehdr->e_shoff); for (i = 0; i < ehdr->e_shnum; i++) { const char *section_name = ((const char *) elf_data) + shdr[ehdr->e_shstrndx].sh_offset + shdr[i].sh_name; if (section_name[0] == '.' && section_name[1] == 'b' && section_name[2] == 's' && section_name[3] == 's') { @@ -180,12 +177,12 @@ 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 == NULL) { + if (trampolin_data == nullptr) { DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin 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 = NULL; + relocation_trampolin_entry_t *freeSlot = nullptr; for (uint32_t i = 0; i < trampolin_data_length; i++) { // We want to override "old" relocations of imports // Pending relocations have the status RELOC_TRAMP_IMPORT_IN_PROGRESS. @@ -223,7 +220,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_IN_PROGRESS; } - uint32_t symbolValue = (uint32_t) &(freeSlot->trampolin[0]); + auto symbolValue = (uint32_t) &(freeSlot->trampolin[0]); value = symbolValue + addend; distance = static_cast(value) - static_cast(target); DEBUG_FUNCTION_LINE("Created tramp\n"); diff --git a/source/kernel.cpp b/source/kernel.cpp index e35f42a..7eb5b3a 100644 --- a/source/kernel.cpp +++ b/source/kernel.cpp @@ -27,8 +27,8 @@ void KernelWriteU32(uint32_t addr, uint32_t value) { ICInvalidateRange(&value, 4); DCFlushRange(&value, 4); - uint32_t dst = (uint32_t) OSEffectiveToPhysical(addr); - uint32_t src = (uint32_t) OSEffectiveToPhysical((uint32_t) &value); + auto dst = (uint32_t) OSEffectiveToPhysical(addr); + auto src = (uint32_t) OSEffectiveToPhysical((uint32_t) &value); SC_KernelCopyData(dst, src, 4); diff --git a/source/main.cpp b/source/main.cpp index 9c93cf9..f34bfd1 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -40,6 +40,7 @@ bool CheckRunning() { extern "C" uint32_t textStart(); extern "C" void _SYSLaunchMenuWithCheckingAccount(nn::act::SlotNo slot); + bool doRelocation(std::vector &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length) { for (auto const &curReloc : relocData) { std::string functionName = curReloc.getName(); diff --git a/source/module/DynamicLinkingHelper.cpp b/source/module/DynamicLinkingHelper.cpp index 1cdb4a2..da1e336 100644 --- a/source/module/DynamicLinkingHelper.cpp +++ b/source/module/DynamicLinkingHelper.cpp @@ -64,7 +64,7 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio return result; } -bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const RelocationData &relocationData) { +bool DynamicLinkingHelper::addRelocationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const RelocationData &relocationData) { return addReloationEntry(linking_data, linking_entries, linking_entry_length, relocationData.getType(), relocationData.getOffset(), relocationData.getAddend(), relocationData.getDestination(), relocationData.getName(), relocationData.getImportRPLInformation()); } @@ -83,11 +83,12 @@ bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t *link return false; } - return addReloationEntry(linking_entries, linking_entry_length, type, offset, addend, destination, functionInfo, importInfoGbl); + return addRelocationEntry(linking_entries, linking_entry_length, type, offset, addend, destination, functionInfo, + importInfoGbl); } -bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName, - dyn_linking_import_t *importInfo) { +bool DynamicLinkingHelper::addRelocationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName, + dyn_linking_import_t *importInfo) { for (uint32_t i = 0; i < linking_entry_length; i++) { dyn_linking_relocation_entry_t *curEntry = &(linking_entries[i]); if (curEntry->functionEntry != NULL) { diff --git a/source/module/DynamicLinkingHelper.h b/source/module/DynamicLinkingHelper.h index 016682e..023e306 100644 --- a/source/module/DynamicLinkingHelper.h +++ b/source/module/DynamicLinkingHelper.h @@ -47,18 +47,16 @@ public: **/ static dyn_linking_import_t *getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData); - static bool addReloationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const RelocationData &relocationData); + static bool addRelocationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const RelocationData &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, std::string name, const ImportRPLInformation &rplInfo); static bool - addReloationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName, dyn_linking_import_t *importInfo); + addRelocationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName, dyn_linking_import_t *importInfo); private: - DynamicLinkingHelper() { - } + DynamicLinkingHelper() = default; - ~DynamicLinkingHelper() { - } + ~DynamicLinkingHelper() = default; }; diff --git a/source/module/ExportData.h b/source/module/ExportData.h index 4c86306..f187c6b 100644 --- a/source/module/ExportData.h +++ b/source/module/ExportData.h @@ -11,15 +11,15 @@ public: this->address = address; } - wums_entry_type_t getType() const { + [[nodiscard]] wums_entry_type_t getType() const { return type; } - const void *getAddress() const { + [[nodiscard]] const void *getAddress() const { return address; } - const std::string getName() const { + [[nodiscard]] const std::string getName() const { return name; } diff --git a/source/module/HookData.h b/source/module/HookData.h index 1320f35..f89de74 100644 --- a/source/module/HookData.h +++ b/source/module/HookData.h @@ -9,11 +9,11 @@ public: this->target = target; } - wums_hook_type_t getType() const { + [[nodiscard]] wums_hook_type_t getType() const { return type; } - const void *getTarget() const { + [[nodiscard]] const void *getTarget() const { return target; } diff --git a/source/module/ImportRPLInformation.h b/source/module/ImportRPLInformation.h index 1628c7f..b75ce4f 100644 --- a/source/module/ImportRPLInformation.h +++ b/source/module/ImportRPLInformation.h @@ -24,13 +24,12 @@ class ImportRPLInformation { public: - ImportRPLInformation(std::string name, bool isData = false) { + explicit ImportRPLInformation(std::string name, bool isData = false) { this->name = name; this->_isData = isData; } - ~ImportRPLInformation() { - } + ~ImportRPLInformation() = default; static std::optional createImportRPLInformation(std::string rawSectionName) { std::string fimport = ".fimport_"; @@ -38,7 +37,7 @@ public: bool data = false; - std::string rplName = ""; + std::string rplName; if (rawSectionName.size() < fimport.size()) { return std::nullopt; @@ -54,11 +53,11 @@ public: return ImportRPLInformation(rplName, data); } - std::string getName() const { + [[nodiscard]] std::string getName() const { return name; } - bool isData() const { + [[nodiscard]] bool isData() const { return _isData; } diff --git a/source/module/ModuleDataPersistence.cpp b/source/module/ModuleDataPersistence.cpp index acc0537..cdc5ecb 100644 --- a/source/module/ModuleDataPersistence.cpp +++ b/source/module/ModuleDataPersistence.cpp @@ -19,7 +19,8 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati // Relocation std::vector relocationData = module.getRelocationDataList(); for (auto const &reloc : relocationData) { - if (!DynamicLinkingHelper::addReloationEntry(&(moduleInformation->linking_data), module_data->linking_entries, DYN_LINK_RELOCATION_LIST_LENGTH, reloc)) { + 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"); return false; } diff --git a/source/module/RelocationData.h b/source/module/RelocationData.h index ef18ba2..d062e2a 100644 --- a/source/module/RelocationData.h +++ b/source/module/RelocationData.h @@ -31,34 +31,33 @@ public: this->name = name; } - ~RelocationData() { - } + ~RelocationData() = default; - char getType() const { + [[nodiscard]] char getType() const { return type; } - size_t getOffset() const { + [[nodiscard]] size_t getOffset() const { return offset; } - int32_t getAddend() const { + [[nodiscard]] int32_t getAddend() const { return addend; } - void *getDestination() const { + [[nodiscard]] void *getDestination() const { return destination; } - std::string getName() const { + [[nodiscard]] std::string getName() const { return name; } - ImportRPLInformation getImportRPLInformation() const { + [[nodiscard]] ImportRPLInformation getImportRPLInformation() const { return rplInfo; } - std::string toString() const; + [[nodiscard]] std::string toString() const; private: char type; diff --git a/source/module/SectionInfo.h b/source/module/SectionInfo.h index e05037d..ad1f032 100644 --- a/source/module/SectionInfo.h +++ b/source/module/SectionInfo.h @@ -18,18 +18,18 @@ #pragma once #include +#include class SectionInfo { public: SectionInfo(std::string name, uint32_t address, uint32_t sectionSize) : - name(name), + name(std::move(name)), address(address), sectionSize(sectionSize) { } - SectionInfo() { - } + SectionInfo() = default; SectionInfo(const SectionInfo &o2) : name(o2.name), @@ -40,24 +40,22 @@ public: SectionInfo& operator=(const SectionInfo& other) = default; - virtual ~SectionInfo() { + virtual ~SectionInfo() = default; - } - - const std::string &getName() const { + [[nodiscard]] const std::string &getName() const { return name; } - uint32_t getAddress() const { + [[nodiscard]] uint32_t getAddress() const { return address; } - uint32_t getSize() const { + [[nodiscard]] uint32_t getSize() const { return sectionSize; } private: std::string name; - uint32_t address; - uint32_t sectionSize; + uint32_t address{}; + uint32_t sectionSize{}; };