From b526acbded4116a19fd5ce59dee878eac4871fc0 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 17 May 2020 13:11:52 +0200 Subject: [PATCH] Allocate everything on the stack instead of the heap --- .gitignore | 2 + Makefile | 2 +- relocator/Makefile | 4 +- relocator/src/DynamicLinkingHelper.cpp | 8 ++-- relocator/src/DynamicLinkingHelper.h | 4 +- relocator/src/ModuleDataPersistence.cpp | 54 ++++++++++-------------- relocator/src/ModuleDataPersistence.h | 4 +- relocator/src/entry.cpp | 30 +++++++------- source/main.cpp | 26 ++++++------ source/module/DynamicLinkingHelper.cpp | 8 ++-- source/module/DynamicLinkingHelper.h | 4 +- source/module/ImportRPLInformation.h | 13 +++--- source/module/ModuleData.cpp | 8 ++-- source/module/ModuleData.h | 30 +++++++------- source/module/ModuleDataFactory.cpp | 41 ++++++++---------- source/module/ModuleDataFactory.h | 4 +- source/module/ModuleDataPersistence.cpp | 55 ++++++++++--------------- source/module/ModuleDataPersistence.h | 4 +- source/module/RelocationData.cpp | 4 +- source/module/RelocationData.h | 22 ++++------ 20 files changed, 146 insertions(+), 181 deletions(-) diff --git a/.gitignore b/.gitignore index 10a67a4..cab6c65 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ relocator.h *.depend *.cscope_file_list *.layout +.idea/ +cmake-build-debug/ diff --git a/Makefile b/Makefile index 6a1b6c7..1f8eced 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ CFLAGS := -g -Wall -O2 -ffunction-sections \ CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__ -CXXFLAGS := $(CFLAGS) +CXXFLAGS := $(CFLAGS) -std=c++17 ASFLAGS := -g $(ARCH) LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) diff --git a/relocator/Makefile b/relocator/Makefile index 29074e7..927bbce 100644 --- a/relocator/Makefile +++ b/relocator/Makefile @@ -43,7 +43,7 @@ INCLUDES := src CFLAGS := -std=gnu11 -mcpu=750 -meabi -mhard-float -ffast-math \ -O2 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE) CXXFLAGS := -std=gnu++11 -mcpu=750 -meabi -mhard-float -ffast-math \ - -O2 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE) + -O2 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing -std=c++17 $(INCLUDE) ASFLAGS := -mregnames LDFLAGS := -nostartfiles -Wl,--gc-sections @@ -164,7 +164,7 @@ $(OUTPUT).elf: $(OFILES) @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) -c $< -o $@ $(ERROR_FILTER) #--------------------------------------------------------------------------------- -%.o: %.S +%.o: %.s @echo $(notdir $<) @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@ $(ERROR_FILTER) diff --git a/relocator/src/DynamicLinkingHelper.cpp b/relocator/src/DynamicLinkingHelper.cpp index 66319a0..d71f695 100644 --- a/relocator/src/DynamicLinkingHelper.cpp +++ b/relocator/src/DynamicLinkingHelper.cpp @@ -64,12 +64,12 @@ dyn_linking_import_t * DynamicLinkingHelper::getOrAddImport(dyn_linking_relocati return result; } -bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t * linking_data, dyn_linking_relocation_entry_t * linking_entries, uint32_t linking_entry_length, 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::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::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, ImportRPLInformation * rplInfo) { - dyn_linking_import_t * importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo->getName().c_str(),rplInfo->isData()); +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) { + dyn_linking_import_t * importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo.getName().c_str(),rplInfo.isData()); if(importInfoGbl == NULL) { DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d rpl files to import reached.\n",DYN_LINK_IMPORT_LIST_LENGTH); return false; diff --git a/relocator/src/DynamicLinkingHelper.h b/relocator/src/DynamicLinkingHelper.h index 7f69345..ef97ccd 100644 --- a/relocator/src/DynamicLinkingHelper.h +++ b/relocator/src/DynamicLinkingHelper.h @@ -46,9 +46,9 @@ 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, RelocationData * relocationData); + 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 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, ImportRPLInformation * rplInfo); + 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); private: diff --git a/relocator/src/ModuleDataPersistence.cpp b/relocator/src/ModuleDataPersistence.cpp index 6bdabf5..85c36c5 100644 --- a/relocator/src/ModuleDataPersistence.cpp +++ b/relocator/src/ModuleDataPersistence.cpp @@ -5,7 +5,7 @@ #include "../../source/module/RelocationData.h" #include -bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformation, ModuleData * module) { +bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformation, const ModuleData& module) { int32_t module_count = moduleInformation->number_used_modules; if(module_count >= MAXIMUM_MODULES) { @@ -15,21 +15,21 @@ bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformat module_information_single_t * module_data = &(moduleInformation->module_data[module_count]); // Relocation - std::vector relocationData = module->getRelocationDataList(); + 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)) { return false; } } - module_data->bssAddr = module->getBSSAddr(); - module_data->bssSize = module->getBSSSize(); - module_data->sbssAddr = module->getSBSSAddr(); - module_data->sbssSize = module->getSBSSSize(); - module_data->endAddress = module->getStartAddress(); - module_data->startAddress = module->getEndAddress(); + module_data->bssAddr = module.getBSSAddr(); + module_data->bssSize = module.getBSSSize(); + module_data->sbssAddr = module.getSBSSAddr(); + module_data->sbssSize = module.getSBSSSize(); + module_data->endAddress = module.getStartAddress(); + module_data->startAddress = module.getEndAddress(); - module_data->entrypoint = module->getEntrypoint(); + module_data->entrypoint = module.getEntrypoint(); moduleInformation->number_used_modules++; @@ -38,8 +38,8 @@ bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformat return true; } -std::vector ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) { - std::vector result; +std::vector ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) { + std::vector result; if(moduleInformation == NULL) { DEBUG_FUNCTION_LINE("moduleInformation == NULL\n"); return result; @@ -57,16 +57,13 @@ std::vector ModuleDataPersistence::loadModuleData(module_informatio for(int32_t i = 0; i < module_count; i++) { // Copy data from struct. module_information_single_t * module_data = &(moduleInformation->module_data[i]); - ModuleData * moduleData = new ModuleData(); - if(moduleData == NULL){ - DEBUG_FUNCTION_LINE("Failed to allocate data for ModuleData object\n"); - continue; - } - moduleData->setBSSLocation(module_data->bssAddr, module_data->bssSize); - moduleData->setSBSSLocation(module_data->sbssAddr, module_data->sbssSize); - moduleData->setEntrypoint(module_data->entrypoint); - moduleData->setStartAddress(module_data->startAddress); - moduleData->setEndAddress(module_data->endAddress); + ModuleData moduleData; + + moduleData.setBSSLocation(module_data->bssAddr, module_data->bssSize); + moduleData.setSBSSLocation(module_data->sbssAddr, module_data->sbssSize); + moduleData.setEntrypoint(module_data->entrypoint); + moduleData.setStartAddress(module_data->startAddress); + moduleData.setEndAddress(module_data->endAddress); for(uint32_t j = 0; j < DYN_LINK_RELOCATION_LIST_LENGTH; j++) { dyn_linking_relocation_entry_t * linking_entry = &(module_data->linking_entries[j]); @@ -92,17 +89,10 @@ std::vector ModuleDataPersistence::loadModuleData(module_informatio DEBUG_FUNCTION_LINE("functionEntry->functionName was NULL, skipping relocation entry\n"); continue; } - ImportRPLInformation * rplInfo = new ImportRPLInformation(importEntry->importName, importEntry->isData); - if(rplInfo == NULL){ - DEBUG_FUNCTION_LINE("Failed to allocate ImportRPLInformation object. Skipping relocation entry.\n"); - continue; - } - RelocationData * reloc = new RelocationData(linking_entry->type, linking_entry->offset, linking_entry->addend, linking_entry->destination, functionEntry->functionName, rplInfo); - if(reloc == NULL){ - DEBUG_FUNCTION_LINE("Failed to allocate RelocationData object. Skipping relocation entry.\n"); - continue; - } - moduleData->addRelocationData(reloc); + ImportRPLInformation rplInfo(importEntry->importName, importEntry->isData); + RelocationData reloc(linking_entry->type, linking_entry->offset, linking_entry->addend, linking_entry->destination, functionEntry->functionName, rplInfo); + + moduleData.addRelocationData(reloc); } result.push_back(moduleData); } diff --git a/relocator/src/ModuleDataPersistence.h b/relocator/src/ModuleDataPersistence.h index 89a27fb..98242bc 100644 --- a/relocator/src/ModuleDataPersistence.h +++ b/relocator/src/ModuleDataPersistence.h @@ -5,6 +5,6 @@ class ModuleDataPersistence { public: - static bool saveModuleData(module_information_t * moduleInformation, ModuleData * module); - static std::vector loadModuleData(module_information_t * moduleInformation); + static bool saveModuleData(module_information_t * moduleInformation, const ModuleData& module); + static std::vector loadModuleData(module_information_t * moduleInformation); }; diff --git a/relocator/src/entry.cpp b/relocator/src/entry.cpp index 0b33a6b..caf5f4d 100644 --- a/relocator/src/entry.cpp +++ b/relocator/src/entry.cpp @@ -25,19 +25,18 @@ extern "C" int _start(int argc, char **argv) { InitFunctionPointers(); socket_lib_init(); log_init(); + doStart(argc,argv); DEBUG_FUNCTION_LINE("Call real one\n"); - 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 doRelocation(std::vector &relocData, relocation_trampolin_entry_t * tramp_data, uint32_t tramp_length) { for (auto const& curReloc : relocData) { - RelocationData * cur = curReloc; - std::string functionName = cur->getName(); - std::string rplName = cur->getImportRPLInformation()->getName(); - int32_t isData = cur->getImportRPLInformation()->isData(); + std::string functionName = curReloc.getName(); + std::string rplName = curReloc.getImportRPLInformation().getName(); + int32_t isData = curReloc.getImportRPLInformation().isData(); OSDynLoad_Module rplHandle = 0; OSDynLoad_Acquire(rplName.c_str(), &rplHandle); @@ -46,7 +45,7 @@ bool doRelocation(std::vector &relocData, relocation_trampolin if(functionAddress == 0) { return false; } - if(!ElfUtils::elfLinkOne(cur->getType(), cur->getOffset(), cur->getAddend(), (uint32_t) cur->getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT)) { + if(!ElfUtils::elfLinkOne(curReloc.getType(), curReloc.getOffset(), curReloc.getAddend(), (uint32_t) curReloc.getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT)) { DEBUG_FUNCTION_LINE("Relocation failed\n"); return false; } @@ -57,26 +56,25 @@ bool doRelocation(std::vector &relocData, relocation_trampolin return true; } bool ResolveRelocations() { - std::vector loadedModules = ModuleDataPersistence::loadModuleData(gModuleData); + std::vector loadedModules = ModuleDataPersistence::loadModuleData(gModuleData); bool wasSuccessful = true; uint32_t count = 0; for (auto const& curModule : loadedModules) { if(wasSuccessful) { - std::vector relocData = curModule->getRelocationDataList(); + std::vector relocData = curModule.getRelocationDataList(); if(!doRelocation(relocData, gModuleData->trampolines,DYN_LINK_TRAMPOLIN_LIST_LENGTH)) { DEBUG_FUNCTION_LINE("FAIL\n"); wasSuccessful = false; } } - if(curModule->getBSSAddr() != 0){ - DEBUG_FUNCTION_LINE("memset .bss %08X (%d)\n", curModule->getBSSAddr(), curModule->getBSSSize()); - memset((void*)curModule->getBSSAddr(), 0, curModule->getBSSSize()); + if(curModule.getBSSAddr() != 0){ + DEBUG_FUNCTION_LINE("memset .bss %08X (%d)\n", curModule.getBSSAddr(), curModule.getBSSSize()); + memset((void*)curModule.getBSSAddr(), 0, curModule.getBSSSize()); } - if(curModule->getSBSSAddr() != 0){ - DEBUG_FUNCTION_LINE("memset .sbss %08X (%d)\n", curModule->getSBSSAddr(), curModule->getSBSSSize()); - memset((void*)curModule->getSBSSAddr(), 0, curModule->getSBSSSize()); + if(curModule.getSBSSAddr() != 0){ + DEBUG_FUNCTION_LINE("memset .sbss %08X (%d)\n", curModule.getSBSSAddr(), curModule.getSBSSSize()); + memset((void*)curModule.getSBSSAddr(), 0, curModule.getSBSSSize()); } - delete curModule; } if(count > 0) { DCFlushRange((void*) 0x00800000, 0x00800000); diff --git a/source/main.cpp b/source/main.cpp index 325b595..e8e9469 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "fs/DirList.h" #include "utils/logger.h" @@ -49,12 +50,11 @@ static_assert(sizeof(module_information_t) <= 0x80000); extern "C" uint32_t textStart(); -bool doRelocation(std::vector &relocData, relocation_trampolin_entry_t * tramp_data, uint32_t tramp_length) { +bool doRelocation(std::vector &relocData, relocation_trampolin_entry_t * tramp_data, uint32_t tramp_length) { for (auto const& curReloc : relocData) { - RelocationData * cur = curReloc; - std::string functionName = cur->getName(); - std::string rplName = cur->getImportRPLInformation()->getName(); - int32_t isData = cur->getImportRPLInformation()->isData(); + std::string functionName = curReloc.getName(); + std::string rplName = curReloc.getImportRPLInformation().getName(); + int32_t isData = curReloc.getImportRPLInformation().isData(); OSDynLoad_Module rplHandle = 0; OSDynLoad_Acquire(rplName.c_str(), &rplHandle); @@ -63,7 +63,7 @@ bool doRelocation(std::vector &relocData, relocation_trampolin if(functionAddress == 0) { return false; } - if(!ElfUtils::elfLinkOne(cur->getType(), cur->getOffset(), cur->getAddend(), (uint32_t) cur->getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT)) { + if(!ElfUtils::elfLinkOne(curReloc.getType(), curReloc.getOffset(), curReloc.getAddend(), (uint32_t) curReloc.getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT)) { DEBUG_FUNCTION_LINE("Relocation failed\n"); return false; } @@ -87,13 +87,13 @@ int main(int argc, char **argv) { for(int i = 0; i < setupModules.GetFilecount(); i++) { memset((void*)gModuleData, 0, sizeof(module_information_t)); DEBUG_FUNCTION_LINE("Trying to run %s",setupModules.GetFilepath(i)); - ModuleData * moduleData = ModuleDataFactory::load(setupModules.GetFilepath(i), 0x00900000, 0x01000000 - textSectionStart, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); - if(moduleData == NULL) { + std::optional moduleData = ModuleDataFactory::load(setupModules.GetFilepath(i), 0x00900000, 0x01000000 - textSectionStart, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); + if(!moduleData) { DEBUG_FUNCTION_LINE("Failed to load %s", setupModules.GetFilepath(i)); continue; } DEBUG_FUNCTION_LINE("Loaded module data"); - std::vector relocData = moduleData->getRelocationDataList(); + std::vector relocData = moduleData->getRelocationDataList(); if(!doRelocation(relocData, gModuleData->trampolines,DYN_LINK_TRAMPOLIN_LIST_LENGTH)) { DEBUG_FUNCTION_LINE("relocations failed\n"); } @@ -110,7 +110,6 @@ int main(int argc, char **argv) { DEBUG_FUNCTION_LINE("Calling %08X", moduleData->getEntrypoint()); ((int (*)(int, char **))moduleData->getEntrypoint())(argc, argv); DEBUG_FUNCTION_LINE("Back from module"); - delete moduleData; } memset((void*)gModuleData, 0, sizeof(module_information_t)); @@ -121,12 +120,11 @@ int main(int argc, char **argv) { for(int i = 0; i < modules.GetFilecount(); i++) { DEBUG_FUNCTION_LINE("Loading module %s",modules.GetFilepath(i)); - ModuleData * moduleData = ModuleDataFactory::load(modules.GetFilepath(i), 0x00900000, 0x01000000 - textSectionStart, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); + std::optional moduleData = ModuleDataFactory::load(modules.GetFilepath(i), 0x00900000, 0x01000000 - textSectionStart, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); - if(moduleData != NULL) { + if(moduleData) { DEBUG_FUNCTION_LINE("Successfully loaded %s", modules.GetFilepath(i)); - ModuleDataPersistence::saveModuleData(gModuleData, moduleData); - delete moduleData; + ModuleDataPersistence::saveModuleData(gModuleData, moduleData.value()); } else { DEBUG_FUNCTION_LINE("Failed to load %s", modules.GetFilepath(i)); } diff --git a/source/module/DynamicLinkingHelper.cpp b/source/module/DynamicLinkingHelper.cpp index eef4960..b98f27b 100644 --- a/source/module/DynamicLinkingHelper.cpp +++ b/source/module/DynamicLinkingHelper.cpp @@ -65,12 +65,12 @@ dyn_linking_import_t * DynamicLinkingHelper::getOrAddImport(dyn_linking_relocati return result; } -bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t * linking_data, dyn_linking_relocation_entry_t * linking_entries, uint32_t linking_entry_length, 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::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::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, ImportRPLInformation * rplInfo) { - dyn_linking_import_t * importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo->getName().c_str(),rplInfo->isData()); +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) { + dyn_linking_import_t * importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo.getName().c_str(),rplInfo.isData()); if(importInfoGbl == NULL) { DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d rpl files to import reached.\n",DYN_LINK_IMPORT_LIST_LENGTH); return false; diff --git a/source/module/DynamicLinkingHelper.h b/source/module/DynamicLinkingHelper.h index 0e30dc5..0ed97ee 100644 --- a/source/module/DynamicLinkingHelper.h +++ b/source/module/DynamicLinkingHelper.h @@ -46,9 +46,9 @@ 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, RelocationData * relocationData); + 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 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, ImportRPLInformation * rplInfo); + 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); private: diff --git a/source/module/ImportRPLInformation.h b/source/module/ImportRPLInformation.h index ded0760..648a732 100644 --- a/source/module/ImportRPLInformation.h +++ b/source/module/ImportRPLInformation.h @@ -18,6 +18,7 @@ #pragma once #include +#include #include "utils/logger.h" class ImportRPLInformation { @@ -31,7 +32,7 @@ public: ~ImportRPLInformation() { } - static ImportRPLInformation * createImportRPLInformation(std::string rawSectionName) { + static std::optional createImportRPLInformation(std::string rawSectionName) { std::string fimport = ".fimport_"; std::string dimport = ".dimport_"; @@ -40,7 +41,7 @@ public: std::string rplName = ""; if(rawSectionName.size() < fimport.size()) { - return NULL; + return std::nullopt; } else if (std::equal(fimport.begin(), fimport.end(), rawSectionName.begin())) { rplName = rawSectionName.substr(fimport.size()); } else if (std::equal(dimport.begin(), dimport.end(), rawSectionName.begin())) { @@ -48,16 +49,16 @@ public: data = true; } else { DEBUG_FUNCTION_LINE("invalid section name\n"); - return NULL; + return std::nullopt; } - return new ImportRPLInformation(rplName, data); + return ImportRPLInformation(rplName, data); } - std::string getName() { + std::string getName() const { return name; } - bool isData() { + bool isData() const { return _isData; } diff --git a/source/module/ModuleData.cpp b/source/module/ModuleData.cpp index 5c41dba..e2f3047 100644 --- a/source/module/ModuleData.cpp +++ b/source/module/ModuleData.cpp @@ -1,12 +1,10 @@ #include "ModuleData.h" #include "utils/StringTools.h" -std::string ModuleData::toString() { +std::string ModuleData::toString() const { 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) { - if(reloc != NULL) { - res += reloc->toString(); - } + for (auto const &reloc : relocation_data_list) { + res += reloc.toString(); } return res; } diff --git a/source/module/ModuleData.h b/source/module/ModuleData.h index eec61a7..00897c6 100644 --- a/source/module/ModuleData.h +++ b/source/module/ModuleData.h @@ -19,7 +19,9 @@ #include #include +#include #include "RelocationData.h" +#include "SectionInfo.h" class ModuleData { public: @@ -27,11 +29,6 @@ public: } ~ModuleData() { - for (auto const& reloc : relocation_data_list) { - if(reloc != NULL) { - delete reloc; - } - } } void setBSSLocation(uint32_t addr, uint32_t size) { @@ -56,45 +53,46 @@ public: this->endAddress = endAddress; } - void addRelocationData(RelocationData * relocation_data) { + void addRelocationData(const RelocationData& relocation_data) { relocation_data_list.push_back(relocation_data); } - std::vector getRelocationDataList() { + const std::vector& getRelocationDataList() const { return relocation_data_list; } - uint32_t getBSSAddr() { + uint32_t getBSSAddr() const { return bssAddr; } - uint32_t getBSSSize() { + uint32_t getBSSSize() const{ return bssSize; } - uint32_t getSBSSAddr() { + uint32_t getSBSSAddr() const{ return sbssAddr; } - uint32_t getSBSSSize() { + uint32_t getSBSSSize() const{ return sbssSize; } - uint32_t getEntrypoint() { + uint32_t getEntrypoint() const{ return entrypoint; } - uint32_t getStartAddress() { + uint32_t getStartAddress() const{ return startAddress; } - uint32_t getEndAddress() { + uint32_t getEndAddress() const{ return endAddress; } - std::string toString(); + std::string toString() const; private: - std::vector relocation_data_list; + std::vector relocation_data_list; + std::map section_info_list; uint32_t bssAddr = 0; uint32_t bssSize = 0; diff --git a/source/module/ModuleDataFactory.cpp b/source/module/ModuleDataFactory.cpp index 48dc34b..45f124e 100644 --- a/source/module/ModuleDataFactory.cpp +++ b/source/module/ModuleDataFactory.cpp @@ -23,21 +23,18 @@ #include "elfio/elfio.hpp" #include "utils/utils.h" #include "ElfUtils.h" +#include "SectionInfo.h" using namespace ELFIO; -ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_address, uint32_t maximum_size, relocation_trampolin_entry_t * trampolin_data, uint32_t trampolin_data_length) { +std::optional ModuleDataFactory::load(std::string path, uint32_t destination_address, uint32_t maximum_size, relocation_trampolin_entry_t * trampolin_data, uint32_t trampolin_data_length) { elfio reader; - ModuleData * moduleData = new ModuleData(); - if(moduleData == NULL) { - return NULL; - } + ModuleData moduleData; // Load ELF data if (!reader.load(path)) { DEBUG_FUNCTION_LINE("Can't find or process %s", path.c_str()); - delete moduleData; - return NULL; + return std::nullopt; } uint32_t sec_num = reader.sections.size(); @@ -82,8 +79,7 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr } else { DEBUG_FUNCTION_LINE("Unhandled case"); free(destinations); - delete moduleData; - return NULL; + return std::nullopt; } const char* p = reader.sections[i]->get_data(); @@ -98,10 +94,10 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr //nextAddress = ROUNDUP(destination + sectionSize,0x100); if(psec->get_name().compare(".bss") == 0) { - moduleData->setBSSLocation(destination, sectionSize); + moduleData.setBSSLocation(destination, sectionSize); DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize); } else if(psec->get_name().compare(".sbss") == 0) { - moduleData->setSBSSLocation(destination, sectionSize); + moduleData.setSBSSLocation(destination, sectionSize); DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize); } totalSize += sectionSize; @@ -122,15 +118,14 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], offset_text, offset_data, trampolin_data, trampolin_data_length)) { DEBUG_FUNCTION_LINE("elfLink failed"); free(destinations); - delete moduleData; - return NULL; + return std::nullopt; } } } - std::vector relocationData = getImportRelocationData(reader, destinations); + std::vector relocationData = getImportRelocationData(reader, destinations); for (auto const& reloc : relocationData) { - moduleData->addRelocationData(reloc); + moduleData.addRelocationData(reloc); } DCFlushRange((void*)destination_address, totalSize); @@ -138,9 +133,9 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr free(destinations); - moduleData->setEntrypoint(entrypoint); - moduleData->setStartAddress(destination_address); - moduleData->setEndAddress(endAddress); + moduleData.setEntrypoint(entrypoint); + moduleData.setStartAddress(destination_address); + moduleData.setEndAddress(endAddress); DEBUG_FUNCTION_LINE("Saved entrypoint as %08X", entrypoint); DEBUG_FUNCTION_LINE("Saved startAddress as %08X", destination_address); DEBUG_FUNCTION_LINE("Saved endAddress as %08X", endAddress); @@ -148,8 +143,8 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr return moduleData; } -std::vector ModuleDataFactory::getImportRelocationData(elfio& reader, uint8_t ** destinations) { - std::vector result; +std::vector ModuleDataFactory::getImportRelocationData(elfio& reader, uint8_t ** destinations) { + std::vector result; std::map infoMap; uint32_t sec_num = reader.sections.size(); @@ -183,8 +178,8 @@ std::vector ModuleDataFactory::getImportRelocationData(elfio& r if(adjusted_sym_value < 0xC0000000) { continue; } - ImportRPLInformation * rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]); - if(rplInfo == NULL) { + std::optional rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]); + if(!rplInfo) { DEBUG_FUNCTION_LINE("Failed to create import information"); break; } @@ -192,7 +187,7 @@ std::vector ModuleDataFactory::getImportRelocationData(elfio& r uint32_t section_index = psec->get_info(); // When these relocations are performed, we don't need the 0xC0000000 offset anymore. - RelocationData * relocationData = new RelocationData(type, offset - 0x02000000, addend, (void*)(destinations[section_index] + 0x02000000), sym_name, rplInfo); + RelocationData relocationData(type, offset - 0x02000000, addend, (void*)(destinations[section_index] + 0x02000000), sym_name, rplInfo.value()); //relocationData->printInformation(); result.push_back(relocationData); } diff --git a/source/module/ModuleDataFactory.h b/source/module/ModuleDataFactory.h index b0a2525..594f66c 100644 --- a/source/module/ModuleDataFactory.h +++ b/source/module/ModuleDataFactory.h @@ -26,7 +26,7 @@ class ModuleDataFactory { public: - static ModuleData * load(std::string path, uint32_t destination_address, uint32_t maximum_size, relocation_trampolin_entry_t * trampolin_data, uint32_t trampolin_data_length); + static std::optional load(std::string path, uint32_t destination_address, 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 std::vector getImportRelocationData(ELFIO::elfio& reader, uint8_t ** destinations); + static std::vector getImportRelocationData(ELFIO::elfio& reader, uint8_t ** destinations); }; diff --git a/source/module/ModuleDataPersistence.cpp b/source/module/ModuleDataPersistence.cpp index 9e29a40..05a04c5 100644 --- a/source/module/ModuleDataPersistence.cpp +++ b/source/module/ModuleDataPersistence.cpp @@ -6,7 +6,7 @@ #include "ModuleData.h" #include "RelocationData.h" -bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformation, ModuleData * module) { +bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformation, const ModuleData& module) { int32_t module_count = moduleInformation->number_used_modules; if(module_count >= MAXIMUM_MODULES) { @@ -16,9 +16,9 @@ bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformat // Copy data to global struct. module_information_single_t * module_data = &(moduleInformation->module_data[module_count]); - 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 - std::vector relocationData = module->getRelocationDataList(); + 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)) { DEBUG_FUNCTION_LINE("Failed to add relocation entry\n"); @@ -26,14 +26,14 @@ bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformat } } - module_data->bssAddr = module->getBSSAddr(); - module_data->bssSize = module->getBSSSize(); - module_data->sbssAddr = module->getSBSSAddr(); - module_data->sbssSize = module->getSBSSSize(); - module_data->startAddress = module->getStartAddress(); - module_data->endAddress = module->getEndAddress(); + module_data->bssAddr = module.getBSSAddr(); + module_data->bssSize = module.getBSSSize(); + module_data->sbssAddr = module.getSBSSAddr(); + module_data->sbssSize = module.getSBSSSize(); + module_data->startAddress = module.getStartAddress(); + module_data->endAddress = module.getEndAddress(); - module_data->entrypoint = module->getEntrypoint(); + module_data->entrypoint = module.getEntrypoint(); moduleInformation->number_used_modules++; @@ -43,8 +43,8 @@ bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformat return true; } -std::vector ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) { - std::vector result; +std::vector ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) { + std::vector result; if(moduleInformation == NULL) { DEBUG_FUNCTION_LINE("moduleInformation == NULL\n"); return result; @@ -60,16 +60,12 @@ std::vector ModuleDataPersistence::loadModuleData(module_informatio for(int32_t i = 0; i < module_count; i++) { // Copy data from struct. module_information_single_t * module_data = &(moduleInformation->module_data[i]); - ModuleData * moduleData = new ModuleData(); - if(moduleData == NULL){ - DEBUG_FUNCTION_LINE("Failed to allocate data for ModuleData object\n"); - continue; - } - moduleData->setBSSLocation(module_data->bssAddr, module_data->bssSize); - moduleData->setSBSSLocation(module_data->sbssAddr, module_data->sbssSize); - moduleData->setEntrypoint(module_data->entrypoint); - moduleData->setStartAddress(module_data->startAddress); - moduleData->setEndAddress(module_data->endAddress); + ModuleData moduleData; + moduleData.setBSSLocation(module_data->bssAddr, module_data->bssSize); + moduleData.setSBSSLocation(module_data->sbssAddr, module_data->sbssSize); + moduleData.setEntrypoint(module_data->entrypoint); + moduleData.setStartAddress(module_data->startAddress); + moduleData.setEndAddress(module_data->endAddress); for(uint32_t j = 0; j < DYN_LINK_RELOCATION_LIST_LENGTH; j++) { dyn_linking_relocation_entry_t * linking_entry = &(module_data->linking_entries[j]); @@ -95,17 +91,10 @@ std::vector ModuleDataPersistence::loadModuleData(module_informatio DEBUG_FUNCTION_LINE("functionEntry->functionName was NULL, skipping relocation entry\n"); continue; } - ImportRPLInformation * rplInfo = new ImportRPLInformation(importEntry->importName, importEntry->isData); - if(rplInfo == NULL){ - DEBUG_FUNCTION_LINE("Failed to allocate ImportRPLInformation object. Skipping relocation entry.\n"); - continue; - } - RelocationData * reloc = new RelocationData(linking_entry->type, linking_entry->offset, linking_entry->addend, linking_entry->destination, functionEntry->functionName, rplInfo); - if(reloc == NULL){ - DEBUG_FUNCTION_LINE("Failed to allocate RelocationData object. Skipping relocation entry.\n"); - continue; - } - moduleData->addRelocationData(reloc); + ImportRPLInformation rplInfo(importEntry->importName, importEntry->isData); + RelocationData reloc(linking_entry->type, linking_entry->offset, linking_entry->addend, linking_entry->destination, functionEntry->functionName, rplInfo); + + moduleData.addRelocationData(reloc); } result.push_back(moduleData); } diff --git a/source/module/ModuleDataPersistence.h b/source/module/ModuleDataPersistence.h index 2c35ff6..08545ba 100644 --- a/source/module/ModuleDataPersistence.h +++ b/source/module/ModuleDataPersistence.h @@ -5,6 +5,6 @@ class ModuleDataPersistence { public: - static bool saveModuleData(module_information_t * moduleInformation, ModuleData * module); - static std::vector loadModuleData(module_information_t * moduleInformation); + static bool saveModuleData(module_information_t * moduleInformation, const ModuleData& module); + static std::vector loadModuleData(module_information_t * moduleInformation); }; diff --git a/source/module/RelocationData.cpp b/source/module/RelocationData.cpp index 8fdcc3a..c8b7efc 100644 --- a/source/module/RelocationData.cpp +++ b/source/module/RelocationData.cpp @@ -1,6 +1,6 @@ #include "RelocationData.h" #include "utils/StringTools.h" -std::string RelocationData::toString(){ - 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() ); +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() ); } diff --git a/source/module/RelocationData.h b/source/module/RelocationData.h index 5e463b3..7a9275d 100644 --- a/source/module/RelocationData.h +++ b/source/module/RelocationData.h @@ -23,51 +23,47 @@ class RelocationData { public: - RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, ImportRPLInformation * rplInfo) { + RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, const ImportRPLInformation& rplInfo): rplInfo(rplInfo) { this->type = type; this->offset = offset; this->addend = addend; this->destination = destination; this->name = name; - this->rplInfo = rplInfo; } ~RelocationData() { - if(rplInfo != NULL) { - delete rplInfo; - } } - char getType() { + char getType() const{ return type; } - size_t getOffset() { + size_t getOffset() const{ return offset; } - int32_t getAddend() { + int32_t getAddend() const{ return addend; } - void * getDestination() { + void * getDestination() const{ return destination; } - std::string getName() { + std::string getName() const{ return name; } - ImportRPLInformation * getImportRPLInformation() { + ImportRPLInformation getImportRPLInformation() const{ return rplInfo; } - std::string toString(); + std::string toString() const; private: char type; size_t offset; int32_t addend; void *destination; std::string name; - ImportRPLInformation * rplInfo; + const ImportRPLInformation rplInfo; };