Formatting

This commit is contained in:
Maschell 2021-09-23 18:38:29 +02:00
parent 30d7e3b7bd
commit 4542bc4d1a
21 changed files with 99 additions and 73 deletions

View File

@ -9,7 +9,7 @@ dyn_linking_function_t *DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_li
return nullptr; return nullptr;
} }
dyn_linking_function_t *result = nullptr; dyn_linking_function_t *result = nullptr;
for (auto & function : data->functions) { for (auto &function: data->functions) {
dyn_linking_function_t *curEntry = &function; dyn_linking_function_t *curEntry = &function;
if (strlen(curEntry->functionName) == 0) { if (strlen(curEntry->functionName) == 0) {
if (strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) { if (strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) {
@ -41,7 +41,7 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio
return nullptr; return nullptr;
} }
dyn_linking_import_t *result = nullptr; dyn_linking_import_t *result = nullptr;
for (auto & import : data->imports) { for (auto &import: data->imports) {
dyn_linking_import_t *curEntry = &import; dyn_linking_import_t *curEntry = &import;
if (strlen(curEntry->importName) == 0) { if (strlen(curEntry->importName) == 0) {
if (strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) { if (strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) {
@ -60,15 +60,17 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio
return result; return result;
} }
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) { 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(), return addRelocationEntry(linking_data, linking_entries, linking_entry_length, relocationData.getType(),
relocationData.getOffset(), relocationData.getAddend(), relocationData.getDestination(), relocationData.getOffset(), relocationData.getAddend(), relocationData.getDestination(),
relocationData.getName(), relocationData.getName(),
relocationData.getImportRPLInformation()); relocationData.getImportRPLInformation());
} }
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, 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,
const std::string& name, const ImportRPLInformation &rplInfo) { 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()); dyn_linking_import_t *importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo.getName().c_str(), rplInfo.isData());
if (importInfoGbl == nullptr) { 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); DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d rpl files to import reached.\n", DYN_LINK_IMPORT_LIST_LENGTH);
@ -85,7 +87,8 @@ bool DynamicLinkingHelper::addRelocationEntry(dyn_linking_relocation_data_t *lin
importInfoGbl); importInfoGbl);
} }
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, 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) { dyn_linking_import_t *importInfo) {
for (uint32_t i = 0; i < linking_entry_length; i++) { for (uint32_t i = 0; i < linking_entry_length; i++) {
dyn_linking_relocation_entry_t *curEntry = &(linking_entries[i]); dyn_linking_relocation_entry_t *curEntry = &(linking_entries[i]);

View File

@ -49,11 +49,14 @@ public:
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 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 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, 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); const ImportRPLInformation &rplInfo);
static bool static bool
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); 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: private:
DynamicLinkingHelper() = default; DynamicLinkingHelper() = default;

View File

@ -4,7 +4,8 @@
#include "ElfUtils.h" #include "ElfUtils.h"
// See https://github.com/decaf-emu/decaf-emu/blob/43366a34e7b55ab9d19b2444aeb0ccd46ac77dea/src/libdecaf/src/cafe/loader/cafe_loader_reloc.cpp#L144 // 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, RelocationType reloc_type) { 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,
RelocationType reloc_type) {
if (type == R_PPC_NONE) { if (type == R_PPC_NONE) {
return true; return true;
} }

View File

@ -42,5 +42,6 @@ extern "C" {
class ElfUtils { class ElfUtils {
public: 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, RelocationType reloc_type); 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,
RelocationType reloc_type);
}; };

View File

@ -34,21 +34,21 @@ std::vector<ModuleData> ModuleDataPersistence::loadModuleData(module_information
moduleData.setExportName(module_data->module_export_name); moduleData.setExportName(module_data->module_export_name);
for (auto & export_entry : module_data->export_entries) { for (auto &export_entry: module_data->export_entries) {
if (export_entry.address == 0) { if (export_entry.address == 0) {
continue; continue;
} }
moduleData.addExportData(ExportData(static_cast<wums_entry_type_t>(export_entry.type), export_entry.name, reinterpret_cast<const void *>(export_entry.address))); moduleData.addExportData(ExportData(static_cast<wums_entry_type_t>(export_entry.type), export_entry.name, reinterpret_cast<const void *>(export_entry.address)));
} }
for (auto & hook_entry : module_data->hook_entries) { for (auto &hook_entry: module_data->hook_entries) {
if (hook_entry.target == 0) { if (hook_entry.target == 0) {
continue; continue;
} }
moduleData.addHookData(HookData(static_cast<wums_hook_type_t>(hook_entry.type), reinterpret_cast<const void *>(hook_entry.target))); moduleData.addHookData(HookData(static_cast<wums_hook_type_t>(hook_entry.type), reinterpret_cast<const void *>(hook_entry.target)));
} }
for (auto & linking_entry : module_data->linking_entries) { for (auto &linking_entry: module_data->linking_entries) {
if (linking_entry.destination == nullptr) { if (linking_entry.destination == nullptr) {
break; break;
} }

View File

@ -49,7 +49,7 @@ extern "C" int _start(int argc, char **argv) {
bool doRelocation(std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length, bool skipAllocReplacement) { bool doRelocation(std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length, bool skipAllocReplacement) {
std::map<std::string, OSDynLoad_Module> moduleCache; std::map<std::string, OSDynLoad_Module> moduleCache;
for (auto const &curReloc : relocData) { for (auto const &curReloc: relocData) {
std::string functionName = curReloc.getName(); std::string functionName = curReloc.getName();
std::string rplName = curReloc.getImportRPLInformation().getName(); std::string rplName = curReloc.getImportRPLInformation().getName();
uint32_t functionAddress = 0; uint32_t functionAddress = 0;
@ -113,7 +113,7 @@ bool doRelocation(std::vector<RelocationData> &relocData, relocation_trampolin_e
bool ResolveRelocations(std::vector<ModuleData> &loadedModules, bool skipMemoryMappingModule) { bool ResolveRelocations(std::vector<ModuleData> &loadedModules, bool skipMemoryMappingModule) {
bool wasSuccessful = true; 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()); DEBUG_FUNCTION_LINE_VERBOSE("Let's do the relocations for %s\n", curModule.getExportName().c_str());
if (wasSuccessful) { if (wasSuccessful) {
std::vector<RelocationData> relocData = curModule.getRelocationDataList(); std::vector<RelocationData> relocData = curModule.getRelocationDataList();
@ -155,7 +155,7 @@ extern "C" void doStart(int argc, char **argv) {
std::vector<ModuleData> loadedModules = OrderModulesByDependencies(loadedModulesUnordered); std::vector<ModuleData> loadedModules = OrderModulesByDependencies(loadedModulesUnordered);
bool applicationEndHookLoaded = false; bool applicationEndHookLoaded = false;
for (auto &curModule : loadedModules) { for (auto &curModule: loadedModules) {
if (curModule.getExportName() == "homebrew_applicationendshook") { if (curModule.getExportName() == "homebrew_applicationendshook") {
DEBUG_FUNCTION_LINE_VERBOSE("We have ApplicationEndsHook Module!\n"); DEBUG_FUNCTION_LINE_VERBOSE("We have ApplicationEndsHook Module!\n");
applicationEndHookLoaded = true; applicationEndHookLoaded = true;
@ -164,8 +164,8 @@ extern "C" void doStart(int argc, char **argv) {
} }
// Make sure WUMS_HOOK_APPLICATION_ENDS and WUMS_HOOK_FINI_WUT are called // Make sure WUMS_HOOK_APPLICATION_ENDS and WUMS_HOOK_FINI_WUT are called
for (auto &curModule : loadedModules) { for (auto &curModule: loadedModules) {
for (auto &curHook : curModule.getHookDataList()) { for (auto &curHook: curModule.getHookDataList()) {
if (curHook.getType() == WUMS_HOOK_APPLICATION_ENDS || curHook.getType() == WUMS_HOOK_FINI_WUT_DEVOPTAB) { if (curHook.getType() == WUMS_HOOK_APPLICATION_ENDS || curHook.getType() == WUMS_HOOK_FINI_WUT_DEVOPTAB) {
if (!applicationEndHookLoaded) { if (!applicationEndHookLoaded) {
OSFatal_printf("%s requires module homebrew_applicationendshook", curModule.getExportName().c_str()); OSFatal_printf("%s requires module homebrew_applicationendshook", curModule.getExportName().c_str());
@ -181,7 +181,7 @@ extern "C" void doStart(int argc, char **argv) {
DEBUG_FUNCTION_LINE_VERBOSE("Resolve relocations without replacing alloc functions\n"); DEBUG_FUNCTION_LINE_VERBOSE("Resolve relocations without replacing alloc functions\n");
ResolveRelocations(loadedModules, true); ResolveRelocations(loadedModules, true);
for (auto &curModule : loadedModules) { for (auto &curModule: loadedModules) {
if (curModule.isInitBeforeRelocationDoneHook()) { if (curModule.isInitBeforeRelocationDoneHook()) {
CallHook(curModule, WUMS_HOOK_INIT_WUT_MALLOC); CallHook(curModule, WUMS_HOOK_INIT_WUT_MALLOC);
CallHook(curModule, WUMS_HOOK_INIT_WUT_NEWLIB); CallHook(curModule, WUMS_HOOK_INIT_WUT_NEWLIB);
@ -209,7 +209,7 @@ extern "C" void doStart(int argc, char **argv) {
} }
} }
for (auto &curModule : loadedModules) { for (auto &curModule: loadedModules) {
if (!curModule.isInitBeforeRelocationDoneHook()) { if (!curModule.isInitBeforeRelocationDoneHook()) {
CallHook(curModule, WUMS_HOOK_INIT_WUT_MALLOC); CallHook(curModule, WUMS_HOOK_INIT_WUT_MALLOC);
CallHook(curModule, WUMS_HOOK_INIT_WUT_NEWLIB); CallHook(curModule, WUMS_HOOK_INIT_WUT_NEWLIB);
@ -246,7 +246,7 @@ std::vector<ModuleData> OrderModulesByDependencies(const std::vector<ModuleData>
while (true) { while (true) {
bool canBreak = true; bool canBreak = true;
bool weDidSomething = false; bool weDidSomething = false;
for (auto const &curModule : loadedModules) { for (auto const &curModule: loadedModules) {
if (std::find(loadedModulesEntrypoints.begin(), loadedModulesEntrypoints.end(), curModule.getEntrypoint()) != loadedModulesEntrypoints.end()) { 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()); // DEBUG_FUNCTION_LINE("%s [%08X] is already loaded\n", curModule.getExportName().c_str(), curModule.getEntrypoint());
continue; continue;
@ -254,7 +254,7 @@ std::vector<ModuleData> OrderModulesByDependencies(const std::vector<ModuleData>
canBreak = false; canBreak = false;
DEBUG_FUNCTION_LINE_VERBOSE("Check if we can load %s\n", curModule.getExportName().c_str()); DEBUG_FUNCTION_LINE_VERBOSE("Check if we can load %s\n", curModule.getExportName().c_str());
std::vector<std::string> importsFromOtherModules; std::vector<std::string> importsFromOtherModules;
for (const auto& curReloc: curModule.getRelocationDataList()) { for (const auto &curReloc: curModule.getRelocationDataList()) {
std::string curRPL = curReloc.getImportRPLInformation().getName(); std::string curRPL = curReloc.getImportRPLInformation().getName();
if (curRPL.rfind("homebrew", 0) == 0) { if (curRPL.rfind("homebrew", 0) == 0) {
if (std::find(importsFromOtherModules.begin(), importsFromOtherModules.end(), curRPL) != importsFromOtherModules.end()) { if (std::find(importsFromOtherModules.begin(), importsFromOtherModules.end(), curRPL) != importsFromOtherModules.end()) {
@ -266,7 +266,7 @@ std::vector<ModuleData> OrderModulesByDependencies(const std::vector<ModuleData>
} }
} }
bool canLoad = true; bool canLoad = true;
for (auto &curImportRPL : importsFromOtherModules) { for (auto &curImportRPL: importsFromOtherModules) {
if (std::find(loadedModulesExportNames.begin(), loadedModulesExportNames.end(), curImportRPL) != loadedModulesExportNames.end()) { if (std::find(loadedModulesExportNames.begin(), loadedModulesExportNames.end(), curImportRPL) != loadedModulesExportNames.end()) {
} else { } else {

View File

@ -28,12 +28,12 @@ void CallHook(const std::vector<ModuleData> &modules, wums_hook_type_t type) {
} }
void CallHook(const ModuleData &module, wums_hook_type_t type) { void CallHook(const ModuleData &module, wums_hook_type_t type) {
if(!module.relocationsDone){ if (!module.relocationsDone) {
DEBUG_FUNCTION_LINE("Hook not called because the relocations failed\n"); DEBUG_FUNCTION_LINE("Hook not called because the relocations failed\n");
return; return;
} }
for (auto &curHook : module.getHookDataList()) { for (auto &curHook: module.getHookDataList()) {
auto func_ptr = (uint32_t) curHook.getTarget(); auto func_ptr = (uint32_t) curHook.getTarget();
if (func_ptr == 0) { if (func_ptr == 0) {
DEBUG_FUNCTION_LINE("Hook ptr was NULL\n"); DEBUG_FUNCTION_LINE("Hook ptr was NULL\n");

View File

@ -35,19 +35,16 @@ static volatile int log_lock __attribute__((section(".data"))) = 0;
typedef uint16_t sa_family_t; typedef uint16_t sa_family_t;
struct in_addr struct in_addr {
{
unsigned int s_addr; unsigned int s_addr;
}; };
struct sockaddr struct sockaddr {
{
sa_family_t sa_family; sa_family_t sa_family;
char sa_data[]; char sa_data[];
}; };
struct sockaddr_in struct sockaddr_in {
{
unsigned short sin_family; unsigned short sin_family;
unsigned short sin_port; unsigned short sin_port;
struct in_addr sin_addr; struct in_addr sin_addr;
@ -57,9 +54,13 @@ struct sockaddr_in
typedef uint32_t socklen_t; typedef uint32_t socklen_t;
extern int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); extern int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
extern int socket(int domain, int type, int protocol); extern int socket(int domain, int type, int protocol);
extern int socketclose(int sockfd); extern int socketclose(int sockfd);
extern int sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); extern int sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
extern uint32_t htonl(uint32_t val); extern uint32_t htonl(uint32_t val);
void log_init() { void log_init() {
@ -77,10 +78,10 @@ void log_init() {
} }
void log_deinit() { void log_deinit() {
if (log_socket < 0){ if (log_socket < 0) {
return; return;
} }
if(socketclose(log_socket) != 0) { if (socketclose(log_socket) != 0) {
return; return;
} }
} }

View File

@ -108,7 +108,8 @@ 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 // 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, RelocationType reloc_type) { 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,
RelocationType reloc_type) {
if (type == R_PPC_NONE) { if (type == R_PPC_NONE) {
return true; return true;
} }

View File

@ -47,5 +47,6 @@ uint32_t load_loader_elf(unsigned char *baseAddress, char *elf_data, uint32_t fi
class ElfUtils { class ElfUtils {
public: 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, RelocationType reloc_type); 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,
RelocationType reloc_type);
}; };

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <wums/defines/module_defines.h> #include <wums/defines/module_defines.h>

View File

@ -42,13 +42,13 @@ extern "C" uint32_t textStart();
extern "C" void _SYSLaunchMenuWithCheckingAccount(nn::act::SlotNo slot); extern "C" void _SYSLaunchMenuWithCheckingAccount(nn::act::SlotNo slot);
bool doRelocation(std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length) { bool doRelocation(std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length) {
for (auto const &curReloc : relocData) { for (auto const &curReloc: relocData) {
std::string functionName = curReloc.getName(); std::string functionName = curReloc.getName();
std::string rplName = curReloc.getImportRPLInformation().getName(); std::string rplName = curReloc.getImportRPLInformation().getName();
int32_t isData = curReloc.getImportRPLInformation().isData(); int32_t isData = curReloc.getImportRPLInformation().isData();
OSDynLoad_Module rplHandle = nullptr; OSDynLoad_Module rplHandle = nullptr;
if(OSDynLoad_IsModuleLoaded(rplName.c_str(), &rplHandle) != OS_DYNLOAD_OK) { if (OSDynLoad_IsModuleLoaded(rplName.c_str(), &rplHandle) != OS_DYNLOAD_OK) {
// only acquire if not already loaded. // only acquire if not already loaded.
OSDynLoad_Acquire(rplName.c_str(), &rplHandle); OSDynLoad_Acquire(rplName.c_str(), &rplHandle);
} }
@ -83,7 +83,8 @@ int main(int argc, char **argv) {
uint32_t destination_address = ((uint32_t) gModuleData + (sizeof(module_information_t) + 0x0000FFFF)) & 0xFFFF0000; uint32_t destination_address = ((uint32_t) gModuleData + (sizeof(module_information_t) + 0x0000FFFF)) & 0xFFFF0000;
memset((void *) gModuleData, 0, sizeof(module_information_t)); memset((void *) gModuleData, 0, sizeof(module_information_t));
DEBUG_FUNCTION_LINE("Trying to run %s", setupModules.GetFilepath(i)); DEBUG_FUNCTION_LINE("Trying to run %s", setupModules.GetFilepath(i));
std::optional<ModuleData> moduleData = ModuleDataFactory::load(setupModules.GetFilepath(i), &destination_address, textSectionStart - destination_address, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); std::optional<ModuleData> moduleData = ModuleDataFactory::load(setupModules.GetFilepath(i), &destination_address, textSectionStart - destination_address, gModuleData->trampolines,
DYN_LINK_TRAMPOLIN_LIST_LENGTH);
if (!moduleData) { if (!moduleData) {
DEBUG_FUNCTION_LINE("Failed to load %s", setupModules.GetFilepath(i)); DEBUG_FUNCTION_LINE("Failed to load %s", setupModules.GetFilepath(i));
continue; continue;
@ -117,7 +118,8 @@ int main(int argc, char **argv) {
uint32_t destination_address = ((uint32_t) gModuleData + (sizeof(module_information_t) + 0x0000FFFF)) & 0xFFFF0000; uint32_t destination_address = ((uint32_t) gModuleData + (sizeof(module_information_t) + 0x0000FFFF)) & 0xFFFF0000;
for (int i = 0; i < modules.GetFilecount(); i++) { for (int i = 0; i < modules.GetFilecount(); i++) {
DEBUG_FUNCTION_LINE("Loading module %s", modules.GetFilepath(i)); DEBUG_FUNCTION_LINE("Loading module %s", modules.GetFilepath(i));
std::optional<ModuleData> moduleData = ModuleDataFactory::load(modules.GetFilepath(i), &destination_address, MEMORY_REGION_USABLE_END - destination_address, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); std::optional<ModuleData> moduleData = ModuleDataFactory::load(modules.GetFilepath(i), &destination_address, MEMORY_REGION_USABLE_END - destination_address, gModuleData->trampolines,
DYN_LINK_TRAMPOLIN_LIST_LENGTH);
if (moduleData) { if (moduleData) {
DEBUG_FUNCTION_LINE("Successfully loaded %s", modules.GetFilepath(i)); DEBUG_FUNCTION_LINE("Successfully loaded %s", modules.GetFilepath(i));

View File

@ -64,12 +64,15 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocatio
return result; return result;
} }
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) { bool DynamicLinkingHelper::addRelocationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length,
return addReloationEntry(linking_data, linking_entries, linking_entry_length, relocationData.getType(), relocationData.getOffset(), relocationData.getAddend(), relocationData.getDestination(), relocationData.getName(), const RelocationData &relocationData) {
return addReloationEntry(linking_data, linking_entries, linking_entry_length, relocationData.getType(), relocationData.getOffset(), relocationData.getAddend(), relocationData.getDestination(),
relocationData.getName(),
relocationData.getImportRPLInformation()); 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, 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,
std::string name, const ImportRPLInformation &rplInfo) { std::string name, const ImportRPLInformation &rplInfo) {
dyn_linking_import_t *importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo.getName().c_str(), rplInfo.isData()); dyn_linking_import_t *importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo.getName().c_str(), rplInfo.isData());
if (importInfoGbl == NULL) { if (importInfoGbl == NULL) {
@ -87,7 +90,8 @@ bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t *link
importInfoGbl); importInfoGbl);
} }
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, 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) { dyn_linking_import_t *importInfo) {
for (uint32_t i = 0; i < linking_entry_length; i++) { for (uint32_t i = 0; i < linking_entry_length; i++) {
dyn_linking_relocation_entry_t *curEntry = &(linking_entries[i]); dyn_linking_relocation_entry_t *curEntry = &(linking_entries[i]);

View File

@ -49,11 +49,13 @@ public:
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 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, 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); const ImportRPLInformation &rplInfo);
static bool static bool
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); 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: private:
DynamicLinkingHelper() = default; DynamicLinkingHelper() = default;

View File

@ -3,7 +3,7 @@
std::string ModuleData::toString() const { std::string ModuleData::toString() const {
std::string res = StringTools::strfmt("Entrypoint %08X, bss: %08X (%d), bss: %08X (%d)\n", getEntrypoint(), getBSSAddr(), getBSSSize(), getSBSSAddr(), getSBSSSize()); 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(); res += reloc.toString();
} }
return res; return res;

View File

@ -141,6 +141,7 @@ public:
void setSkipEntrypoint(bool value) { void setSkipEntrypoint(bool value) {
this->skipEntrypoint = value; this->skipEntrypoint = value;
} }
void setInitBeforeRelocationDoneHook(bool value) { void setInitBeforeRelocationDoneHook(bool value) {
this->initBeforeRelocationDoneHook = value; this->initBeforeRelocationDoneHook = value;
} }

View File

@ -26,7 +26,8 @@
using namespace ELFIO; using namespace ELFIO;
std::optional<ModuleData> 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) { std::optional<ModuleData>
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) {
elfio reader; elfio reader;
ModuleData moduleData; ModuleData moduleData;
@ -60,7 +61,7 @@ std::optional<ModuleData> ModuleDataFactory::load(const std::string& path, uint3
uint32_t sectionSize = psec->get_size(); uint32_t sectionSize = psec->get_size();
totalSize += sectionSize; totalSize += sectionSize;
if(totalSize > maximum_size){ if (totalSize > maximum_size) {
DEBUG_FUNCTION_LINE("Couldn't load setup module because it's too big."); DEBUG_FUNCTION_LINE("Couldn't load setup module because it's too big.");
return {}; return {};
} }
@ -131,7 +132,7 @@ std::optional<ModuleData> ModuleDataFactory::load(const std::string& path, uint3
} }
std::vector<RelocationData> relocationData = getImportRelocationData(reader, destinations); std::vector<RelocationData> relocationData = getImportRelocationData(reader, destinations);
for (auto const &reloc : relocationData) { for (auto const &reloc: relocationData) {
moduleData.addRelocationData(reloc); moduleData.addRelocationData(reloc);
} }
@ -280,7 +281,8 @@ std::vector<RelocationData> ModuleDataFactory::getImportRelocationData(elfio &re
return result; 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_trampolin_entry_t *trampolin_data,
uint32_t trampolin_data_length) {
uint32_t sec_num = reader.sections.size(); uint32_t sec_num = reader.sections.size();
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {

View File

@ -26,9 +26,11 @@
class ModuleDataFactory { class ModuleDataFactory {
public: public:
static std::optional<ModuleData> 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); static std::optional<ModuleData>
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);
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_trampolin_entry_t *trampolin_data,
uint32_t trampolin_data_length);
static std::vector<RelocationData> getImportRelocationData(ELFIO::elfio &reader, uint8_t **destinations); static std::vector<RelocationData> getImportRelocationData(ELFIO::elfio &reader, uint8_t **destinations);
}; };

View File

@ -18,7 +18,7 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati
DEBUG_FUNCTION_LINE("Saving reloation data for module at %08X", module.getEntrypoint()); DEBUG_FUNCTION_LINE("Saving reloation data for module at %08X", module.getEntrypoint());
// Relocation // Relocation
std::vector<RelocationData> relocationData = module.getRelocationDataList(); std::vector<RelocationData> relocationData = module.getRelocationDataList();
for (auto const &reloc : relocationData) { for (auto const &reloc: relocationData) {
if (!DynamicLinkingHelper::addRelocationEntry(&(moduleInformation->linking_data), module_data->linking_entries, if (!DynamicLinkingHelper::addRelocationEntry(&(moduleInformation->linking_data), module_data->linking_entries,
DYN_LINK_RELOCATION_LIST_LENGTH, reloc)) { DYN_LINK_RELOCATION_LIST_LENGTH, reloc)) {
DEBUG_FUNCTION_LINE("Failed to add relocation entry\n"); DEBUG_FUNCTION_LINE("Failed to add relocation entry\n");
@ -27,9 +27,9 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati
} }
std::vector<ExportData> exportData = module.getExportDataList(); std::vector<ExportData> exportData = module.getExportDataList();
for (auto const &curExport : exportData) { for (auto const &curExport: exportData) {
bool found = false; bool found = false;
for (auto &export_entry : module_data->export_entries) { for (auto &export_entry: module_data->export_entries) {
if (export_entry.address == 0) { if (export_entry.address == 0) {
export_entry.type = curExport.getType(); export_entry.type = curExport.getType();
export_entry.name[0] = '\0'; export_entry.name[0] = '\0';
@ -46,9 +46,9 @@ bool ModuleDataPersistence::saveModuleData(module_information_t *moduleInformati
} }
std::vector<HookData> hookData = module.getHookDataList(); std::vector<HookData> hookData = module.getHookDataList();
for (auto const &curHook : hookData) { for (auto const &curHook: hookData) {
bool found = false; bool found = false;
for (auto &hook_entry : module_data->hook_entries) { for (auto &hook_entry: module_data->hook_entries) {
if (hook_entry.target == 0) { if (hook_entry.target == 0) {
hook_entry.type = curHook.getType(); hook_entry.type = curHook.getType();
hook_entry.target = (uint32_t) curHook.getTarget(); hook_entry.target = (uint32_t) curHook.getTarget();
@ -109,7 +109,7 @@ std::vector<ModuleData> ModuleDataPersistence::loadModuleData(module_information
moduleData.setSkipEntrypoint(module_data->skipEntrypoint); moduleData.setSkipEntrypoint(module_data->skipEntrypoint);
moduleData.setInitBeforeRelocationDoneHook(module_data->initBeforeRelocationDoneHook); 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; export_data_t *export_entry = &export_entrie;
if (export_entry->address == 0) { if (export_entry->address == 0) {
continue; continue;
@ -117,14 +117,14 @@ std::vector<ModuleData> ModuleDataPersistence::loadModuleData(module_information
moduleData.addExportData(ExportData(static_cast<wums_entry_type_t>(export_entry->type), export_entry->name, reinterpret_cast<const void *>(export_entry->address))); moduleData.addExportData(ExportData(static_cast<wums_entry_type_t>(export_entry->type), export_entry->name, reinterpret_cast<const void *>(export_entry->address)));
} }
for (auto &hook_entry : module_data->hook_entries) { for (auto &hook_entry: module_data->hook_entries) {
if (hook_entry.target == 0) { if (hook_entry.target == 0) {
continue; continue;
} }
moduleData.addHookData(HookData(static_cast<wums_hook_type_t>(hook_entry.type), reinterpret_cast<const void *>(hook_entry.target))); moduleData.addHookData(HookData(static_cast<wums_hook_type_t>(hook_entry.type), reinterpret_cast<const void *>(hook_entry.target)));
} }
for (auto &linking_entry : module_data->linking_entries) { for (auto &linking_entry: module_data->linking_entries) {
if (linking_entry.destination == nullptr) { if (linking_entry.destination == nullptr) {
break; break;
} }

View File

@ -2,5 +2,6 @@
#include "utils/StringTools.h" #include "utils/StringTools.h"
std::string RelocationData::toString() const { std::string RelocationData::toString() const {
return StringTools::strfmt("%s destination: %08X offset: %08X type: %02X addend: %d rplName: %s isData: %d \n", name.c_str(), destination, offset, type, addend, rplInfo.getName().c_str(), rplInfo.isData()); return StringTools::strfmt("%s destination: %08X offset: %08X type: %02X addend: %d rplName: %s isData: %d \n", name.c_str(), destination, offset, type, addend, rplInfo.getName().c_str(),
rplInfo.isData());
} }

View File

@ -37,7 +37,7 @@ public:
sectionSize(o2.sectionSize) { sectionSize(o2.sectionSize) {
} }
SectionInfo& operator=(const SectionInfo& other) = default; SectionInfo &operator=(const SectionInfo &other) = default;
virtual ~SectionInfo() = default; virtual ~SectionInfo() = default;