Allocate everything on the stack instead of the heap

This commit is contained in:
Maschell 2020-05-17 13:11:52 +02:00
parent d36ad9bf3d
commit b526acbded
20 changed files with 146 additions and 181 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@ relocator.h
*.depend
*.cscope_file_list
*.layout
.idea/
cmake-build-debug/

View File

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

View File

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

View File

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

View File

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

View File

@ -5,7 +5,7 @@
#include "../../source/module/RelocationData.h"
#include <coreinit/cache.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) {
@ -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 *> relocationData = module->getRelocationDataList();
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)) {
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<ModuleData*> ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) {
std::vector<ModuleData*> result;
std::vector<ModuleData> ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) {
std::vector<ModuleData> result;
if(moduleInformation == NULL) {
DEBUG_FUNCTION_LINE("moduleInformation == NULL\n");
return result;
@ -57,16 +57,13 @@ std::vector<ModuleData*> 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<ModuleData*> 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);
}

View File

@ -5,6 +5,6 @@
class ModuleDataPersistence {
public:
static bool saveModuleData(module_information_t * moduleInformation, ModuleData * module);
static std::vector<ModuleData*> loadModuleData(module_information_t * moduleInformation);
static bool saveModuleData(module_information_t * moduleInformation, const ModuleData& module);
static std::vector<ModuleData> loadModuleData(module_information_t * moduleInformation);
};

View File

@ -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<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) {
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<RelocationData *> &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<RelocationData *> &relocData, relocation_trampolin
return true;
}
bool ResolveRelocations() {
std::vector<ModuleData *> loadedModules = ModuleDataPersistence::loadModuleData(gModuleData);
std::vector<ModuleData> loadedModules = ModuleDataPersistence::loadModuleData(gModuleData);
bool wasSuccessful = true;
uint32_t count = 0;
for (auto const& curModule : loadedModules) {
if(wasSuccessful) {
std::vector<RelocationData *> relocData = curModule->getRelocationDataList();
std::vector<RelocationData> 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);

View File

@ -12,6 +12,7 @@
#include <coreinit/dynload.h>
#include <whb/log.h>
#include <whb/log_udp.h>
#include <vector>
#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<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) {
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<RelocationData *> &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> 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<RelocationData *> relocData = moduleData->getRelocationDataList();
std::vector<RelocationData> 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> 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));
}

View File

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

View File

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

View File

@ -18,6 +18,7 @@
#pragma once
#include <string>
#include <optional>
#include "utils/logger.h"
class ImportRPLInformation {
@ -31,7 +32,7 @@ public:
~ImportRPLInformation() {
}
static ImportRPLInformation * createImportRPLInformation(std::string rawSectionName) {
static std::optional<ImportRPLInformation> 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;
}

View File

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

View File

@ -19,7 +19,9 @@
#include <string>
#include <vector>
#include <map>
#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<RelocationData *> getRelocationDataList() {
const std::vector<RelocationData>& 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<RelocationData *> relocation_data_list;
std::vector<RelocationData> relocation_data_list;
std::map<std::string, SectionInfo> section_info_list;
uint32_t bssAddr = 0;
uint32_t bssSize = 0;

View File

@ -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<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) {
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*> relocationData = getImportRelocationData(reader, destinations);
std::vector<RelocationData> 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<RelocationData*> ModuleDataFactory::getImportRelocationData(elfio& reader, uint8_t ** destinations) {
std::vector<RelocationData*> result;
std::vector<RelocationData> ModuleDataFactory::getImportRelocationData(elfio& reader, uint8_t ** destinations) {
std::vector<RelocationData> result;
std::map<uint32_t,std::string> infoMap;
uint32_t sec_num = reader.sections.size();
@ -183,8 +178,8 @@ std::vector<RelocationData*> ModuleDataFactory::getImportRelocationData(elfio& r
if(adjusted_sym_value < 0xC0000000) {
continue;
}
ImportRPLInformation * rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]);
if(rplInfo == NULL) {
std::optional<ImportRPLInformation> rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]);
if(!rplInfo) {
DEBUG_FUNCTION_LINE("Failed to create import information");
break;
}
@ -192,7 +187,7 @@ std::vector<RelocationData*> 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);
}

View File

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

@ -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 *> relocationData = module->getRelocationDataList();
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)) {
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<ModuleData*> ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) {
std::vector<ModuleData*> result;
std::vector<ModuleData> ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) {
std::vector<ModuleData> result;
if(moduleInformation == NULL) {
DEBUG_FUNCTION_LINE("moduleInformation == NULL\n");
return result;
@ -60,16 +60,12 @@ std::vector<ModuleData*> 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<ModuleData*> 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);
}

View File

@ -5,6 +5,6 @@
class ModuleDataPersistence {
public:
static bool saveModuleData(module_information_t * moduleInformation, ModuleData * module);
static std::vector<ModuleData*> loadModuleData(module_information_t * moduleInformation);
static bool saveModuleData(module_information_t * moduleInformation, const ModuleData& module);
static std::vector<ModuleData> loadModuleData(module_information_t * moduleInformation);
};

View File

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

View File

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