Code cleanup

This commit is contained in:
Maschell 2021-09-18 11:55:01 +02:00
parent 74f7b8a662
commit f5c8df9416
16 changed files with 79 additions and 83 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ relocator.h
cmake-build-debug/
*.txt
build1/
cmake-build-default/

View File

@ -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) {

View File

@ -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;
};

View File

@ -72,12 +72,12 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
// }
auto distance = static_cast<int32_t>(value) - static_cast<int32_t>(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<int32_t>(value) - static_cast<int32_t>(target);
DEBUG_FUNCTION_LINE("Created tramp\n");

View File

@ -254,7 +254,7 @@ std::vector<ModuleData> OrderModulesByDependencies(const std::vector<ModuleData>
canBreak = false;
DEBUG_FUNCTION_LINE_VERBOSE("Check if we can load %s\n", curModule.getExportName().c_str());
std::vector<std::string> 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()) {

View File

@ -1,6 +1,3 @@
#include <stdio.h>
#include <string.h>
#include <coreinit/debug.h>
#include <coreinit/cache.h>
#include <coreinit/memdefaultheap.h>
@ -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<int32_t>(value) - static_cast<int32_t>(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<int32_t>(value) - static_cast<int32_t>(target);
DEBUG_FUNCTION_LINE("Created tramp\n");

View File

@ -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);

View File

@ -40,6 +40,7 @@ bool CheckRunning() {
extern "C" uint32_t textStart();
extern "C" void _SYSLaunchMenuWithCheckingAccount(nn::act::SlotNo slot);
bool doRelocation(std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length) {
for (auto const &curReloc : relocData) {
std::string functionName = curReloc.getName();

View File

@ -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) {

View File

@ -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;
};

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<ImportRPLInformation> 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;
}

View File

@ -19,7 +19,8 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati
// Relocation
std::vector<RelocationData> 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;
}

View File

@ -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;

View File

@ -18,18 +18,18 @@
#pragma once
#include <string>
#include <utility>
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{};
};