Formatting

This commit is contained in:
Maschell
2020-05-17 19:05:51 +02:00
parent 373579cd6f
commit 2ee3011ae8
39 changed files with 881 additions and 810 deletions

View File

@@ -6,26 +6,26 @@
#include "utils/logger.h"
#include "common/module_defines.h"
dyn_linking_function_t * DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_linking_relocation_data_t * data, const char* functionName) {
if(data == NULL) {
dyn_linking_function_t *DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_linking_relocation_data_t *data, const char *functionName) {
if (data == NULL) {
return NULL;
}
if(functionName == NULL) {
if (functionName == NULL) {
return NULL;
}
dyn_linking_function_t * result = NULL;
for(int32_t i = 0; i < DYN_LINK_FUNCTION_LIST_LENGTH; i++) {
dyn_linking_function_t * curEntry = &(data->functions[i]);
if(strlen(curEntry->functionName) == 0) {
if(strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) {
dyn_linking_function_t *result = NULL;
for (int32_t i = 0; i < DYN_LINK_FUNCTION_LIST_LENGTH; i++) {
dyn_linking_function_t *curEntry = &(data->functions[i]);
if (strlen(curEntry->functionName) == 0) {
if (strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) {
DEBUG_FUNCTION_LINE("Failed to add function name, it's too long.\n");
return NULL;
}
strncpy(curEntry->functionName,functionName,DYN_LINK_FUNCTION_NAME_LENGTH);
strncpy(curEntry->functionName, functionName, DYN_LINK_FUNCTION_NAME_LENGTH);
result = curEntry;
break;
}
if(strncmp(curEntry->functionName,functionName,DYN_LINK_FUNCTION_NAME_LENGTH) == 0) {
if (strncmp(curEntry->functionName, functionName, DYN_LINK_FUNCTION_NAME_LENGTH) == 0) {
result = curEntry;
break;
}
@@ -33,62 +33,65 @@ dyn_linking_function_t * DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_l
return result;
}
dyn_linking_import_t * DynamicLinkingHelper::getOrAddFunctionImportByName(dyn_linking_relocation_data_t * data, const char* importName) {
dyn_linking_import_t *DynamicLinkingHelper::getOrAddFunctionImportByName(dyn_linking_relocation_data_t *data, const char *importName) {
return getOrAddImport(data, importName, false);
}
dyn_linking_import_t * DynamicLinkingHelper::getOrAddDataImportByName(dyn_linking_relocation_data_t * data, const char* importName) {
dyn_linking_import_t *DynamicLinkingHelper::getOrAddDataImportByName(dyn_linking_relocation_data_t *data, const char *importName) {
return getOrAddImport(data, importName, true);
}
dyn_linking_import_t * DynamicLinkingHelper::getOrAddImport(dyn_linking_relocation_data_t * data, const char* importName, bool isData) {
if(importName == NULL || data == NULL) {
dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData) {
if (importName == NULL || data == NULL) {
return NULL;
}
dyn_linking_import_t * result = NULL;
for(int32_t i = 0; i < DYN_LINK_IMPORT_LIST_LENGTH; i++) {
dyn_linking_import_t * curEntry = &(data->imports[i]);
if(strlen(curEntry->importName) == 0) {
if(strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) {
dyn_linking_import_t *result = NULL;
for (int32_t i = 0; i < DYN_LINK_IMPORT_LIST_LENGTH; i++) {
dyn_linking_import_t *curEntry = &(data->imports[i]);
if (strlen(curEntry->importName) == 0) {
if (strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) {
DEBUG_FUNCTION_LINE("Failed to add Import, it's too long.\n");
return NULL;
}
strncpy(curEntry->importName,importName,DYN_LINK_IMPORT_NAME_LENGTH);
strncpy(curEntry->importName, importName, DYN_LINK_IMPORT_NAME_LENGTH);
curEntry->isData = isData;
result = curEntry;
break;
}
if(strncmp(curEntry->importName,importName,DYN_LINK_IMPORT_NAME_LENGTH) == 0 && (curEntry->isData == isData)) {
if (strncmp(curEntry->importName, importName, DYN_LINK_IMPORT_NAME_LENGTH) == 0 && (curEntry->isData == isData)) {
return curEntry;
}
}
return result;
}
bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t * linking_data, dyn_linking_relocation_entry_t * linking_entries, uint32_t linking_entry_length, const RelocationData& relocationData) {
return addReloationEntry(linking_data, linking_entries, linking_entry_length, relocationData.getType(), relocationData.getOffset(), relocationData.getAddend(), relocationData.getDestination(), relocationData.getName(), relocationData.getImportRPLInformation());
bool DynamicLinkingHelper::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, 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);
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;
}
dyn_linking_function_t * functionInfo = DynamicLinkingHelper::getOrAddFunctionEntryByName(linking_data, name.c_str());
if(functionInfo == NULL) {
DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d function to be relocated reached.\n",DYN_LINK_FUNCTION_LIST_LENGTH);
dyn_linking_function_t *functionInfo = DynamicLinkingHelper::getOrAddFunctionEntryByName(linking_data, name.c_str());
if (functionInfo == NULL) {
DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d function to be relocated reached.\n", DYN_LINK_FUNCTION_LIST_LENGTH);
return false;
}
return addReloationEntry(linking_entries, linking_entry_length, type, offset, addend, destination, functionInfo, importInfoGbl);
}
bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_entry_t * linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t * functionName, dyn_linking_import_t * importInfo) {
for(uint32_t i = 0; i < linking_entry_length; i++) {
dyn_linking_relocation_entry_t * curEntry = &(linking_entries[i]);
if(curEntry->functionEntry != NULL) {
bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, dyn_linking_function_t *functionName,
dyn_linking_import_t *importInfo) {
for (uint32_t i = 0; i < linking_entry_length; i++) {
dyn_linking_relocation_entry_t *curEntry = &(linking_entries[i]);
if (curEntry->functionEntry != NULL) {
continue;
}
curEntry->type = type;

View File

@@ -1,4 +1,5 @@
#pragma once
#include "common/dynamic_linking_defines.h"
#include "utils/logger.h"
#include <string>
@@ -13,7 +14,7 @@ public:
\param functionName Name of the function
\return Returns a pointer to the entry which contains the functionName. Null on error or if the list full.
**/
static dyn_linking_function_t * getOrAddFunctionEntryByName(dyn_linking_relocation_data_t * data, const char * functionName);
static dyn_linking_function_t *getOrAddFunctionEntryByName(dyn_linking_relocation_data_t *data, const char *functionName);
/**
Gets the function import entry for a given function name. If the import is not present in the list, it will be added.
@@ -22,7 +23,7 @@ public:
\param importName Name of the function
\return Returns a pointer to the function import entry which contains the importName. Null on error or if the list full.
**/
static dyn_linking_import_t * getOrAddFunctionImportByName(dyn_linking_relocation_data_t * data, const char * importName);
static dyn_linking_import_t *getOrAddFunctionImportByName(dyn_linking_relocation_data_t *data, const char *importName);
/**
@@ -32,7 +33,7 @@ public:
\param importName Name of the data
\return Returns a pointer to the data import entry which contains the importName. Null on error or if the list full.
**/
static dyn_linking_import_t * getOrAddDataImportByName(dyn_linking_relocation_data_t * data, const char * importName);
static dyn_linking_import_t *getOrAddDataImportByName(dyn_linking_relocation_data_t *data, const char *importName);
/**
@@ -44,13 +45,16 @@ public:
\return Returns a pointer to the data import entry which contains the importName. Null on error or if the list full.
**/
static dyn_linking_import_t * getOrAddImport(dyn_linking_relocation_data_t * data, const char * importName, bool isData);
static dyn_linking_import_t *getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData);
static bool addReloationEntry(dyn_linking_relocation_data_t * linking_data, dyn_linking_relocation_entry_t * linking_entries, uint32_t linking_entry_length, const RelocationData& relocationData);
static bool 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, const 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);
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:
DynamicLinkingHelper() {
}

View File

@@ -40,7 +40,7 @@ public:
std::string rplName = "";
if(rawSectionName.size() < fimport.size()) {
if (rawSectionName.size() < fimport.size()) {
return std::nullopt;
} else if (std::equal(fimport.begin(), fimport.end(), rawSectionName.begin())) {
rplName = rawSectionName.substr(fimport.size());

View File

@@ -53,11 +53,11 @@ public:
this->endAddress = endAddress;
}
void addRelocationData(const RelocationData& relocation_data) {
void addRelocationData(const RelocationData &relocation_data) {
relocation_data_list.push_back(relocation_data);
}
const std::vector<RelocationData>& getRelocationDataList() const {
const std::vector<RelocationData> &getRelocationDataList() const {
return relocation_data_list;
}
@@ -80,31 +80,32 @@ public:
return bssAddr;
}
uint32_t getBSSSize() const{
uint32_t getBSSSize() const {
return bssSize;
}
uint32_t getSBSSAddr() const{
uint32_t getSBSSAddr() const {
return sbssAddr;
}
uint32_t getSBSSSize() const{
uint32_t getSBSSSize() const {
return sbssSize;
}
uint32_t getEntrypoint() const{
uint32_t getEntrypoint() const {
return entrypoint;
}
uint32_t getStartAddress() const{
uint32_t getStartAddress() const {
return startAddress;
}
uint32_t getEndAddress() const{
uint32_t getEndAddress() const {
return endAddress;
}
std::string toString() const;
private:
std::vector<RelocationData> relocation_data_list;
std::map<std::string, SectionInfo> section_info_list;

View File

@@ -27,7 +27,7 @@
using namespace ELFIO;
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) {
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;
@@ -52,8 +52,8 @@ std::optional<ModuleData> ModuleDataFactory::load(std::string path, uint32_t des
uint32_t endAddress = 0;
for(uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader.sections[i];
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i];
if (psec->get_type() == 0x80000002) {
continue;
}
@@ -65,15 +65,15 @@ std::optional<ModuleData> ModuleDataFactory::load(std::string path, uint32_t des
destinations[psec->get_index()] = (uint8_t *) baseOffset;
uint32_t destination = baseOffset + address;
if((address >= 0x02000000) && address < 0x10000000) {
if ((address >= 0x02000000) && address < 0x10000000) {
destination -= 0x02000000;
destinations[psec->get_index()] -= 0x02000000;
baseOffset += sectionSize;
offset_data += sectionSize;
} else if((address >= 0x10000000) && address < 0xC0000000) {
} else if ((address >= 0x10000000) && address < 0xC0000000) {
destination -= 0x10000000;
destinations[psec->get_index()] -= 0x10000000;
} else if(address >= 0xC0000000) {
} else if (address >= 0xC0000000) {
destination -= 0xC0000000;
destinations[psec->get_index()] -= 0xC0000000;
} else {
@@ -82,21 +82,21 @@ std::optional<ModuleData> ModuleDataFactory::load(std::string path, uint32_t des
return std::nullopt;
}
const char* p = reader.sections[i]->get_data();
const char *p = reader.sections[i]->get_data();
if(psec->get_type() == SHT_NOBITS) {
if (psec->get_type() == SHT_NOBITS) {
DEBUG_FUNCTION_LINE("memset section %s %08X to 0 (%d bytes)", psec->get_name().c_str(), destination, sectionSize);
memset((void*) destination, 0, sectionSize);
} else if(psec->get_type() == SHT_PROGBITS) {
memset((void *) destination, 0, sectionSize);
} else if (psec->get_type() == SHT_PROGBITS) {
DEBUG_FUNCTION_LINE("Copy section %s %08X -> %08X (%d bytes)", psec->get_name().c_str(), p, destination, sectionSize);
memcpy((void*) destination, p, sectionSize);
memcpy((void *) destination, p, sectionSize);
}
//nextAddress = ROUNDUP(destination + sectionSize,0x100);
if(psec->get_name().compare(".bss") == 0) {
if (psec->get_name().compare(".bss") == 0) {
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) {
} else if (psec->get_name().compare(".sbss") == 0) {
moduleData.setSBSSLocation(destination, sectionSize);
DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize);
}
@@ -107,19 +107,19 @@ std::optional<ModuleData> ModuleDataFactory::load(std::string path, uint32_t des
totalSize += sectionSize;
if(endAddress < destination + sectionSize){
if (endAddress < destination + sectionSize) {
endAddress = destination + sectionSize;
}
DCFlushRange((void*)destination, sectionSize);
ICInvalidateRange((void*)destination, sectionSize);
DCFlushRange((void *) destination, sectionSize);
ICInvalidateRange((void *) destination, sectionSize);
}
}
for(uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader.sections[i];
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i];
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
DEBUG_FUNCTION_LINE("Linking (%d)... %s",i,psec->get_name().c_str());
DEBUG_FUNCTION_LINE("Linking (%d)... %s", i, psec->get_name().c_str());
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);
@@ -129,12 +129,12 @@ std::optional<ModuleData> ModuleDataFactory::load(std::string path, uint32_t des
}
std::vector<RelocationData> relocationData = getImportRelocationData(reader, destinations);
for (auto const& reloc : relocationData) {
for (auto const &reloc : relocationData) {
moduleData.addRelocationData(reloc);
}
DCFlushRange((void*)destination_address, totalSize);
ICInvalidateRange((void*)destination_address, totalSize);
DCFlushRange((void *) destination_address, totalSize);
ICInvalidateRange((void *) destination_address, totalSize);
free(destinations);
@@ -148,43 +148,43 @@ std::optional<ModuleData> ModuleDataFactory::load(std::string path, uint32_t des
return moduleData;
}
std::vector<RelocationData> ModuleDataFactory::getImportRelocationData(elfio& reader, uint8_t ** destinations) {
std::vector<RelocationData> ModuleDataFactory::getImportRelocationData(elfio &reader, uint8_t **destinations) {
std::vector<RelocationData> result;
std::map<uint32_t,std::string> infoMap;
std::map<uint32_t, std::string> infoMap;
uint32_t sec_num = reader.sections.size();
for ( uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader.sections[i];
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i];
if (psec->get_type() == 0x80000002) {
infoMap[i] = psec->get_name();
}
}
for (uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader.sections[i];
if(psec->get_type() == SHT_RELA || psec->get_type() == SHT_REL) {
DEBUG_FUNCTION_LINE("Found relocation section %s",psec->get_name().c_str());
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i];
if (psec->get_type() == SHT_RELA || psec->get_type() == SHT_REL) {
DEBUG_FUNCTION_LINE("Found relocation section %s", psec->get_name().c_str());
relocation_section_accessor rel(reader, psec);
for ( uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j ) {
Elf64_Addr offset;
Elf_Word type;
Elf_Sxword addend;
std::string sym_name;
Elf64_Addr sym_value;
Elf_Half sym_section_index;
for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) {
Elf64_Addr offset;
Elf_Word type;
Elf_Sxword addend;
std::string sym_name;
Elf64_Addr sym_value;
Elf_Half sym_section_index;
if(!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
if (!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
DEBUG_FUNCTION_LINE("Failed to get relocation");
break;
}
uint32_t adjusted_sym_value = (uint32_t) sym_value;
if(adjusted_sym_value < 0xC0000000) {
if (adjusted_sym_value < 0xC0000000) {
continue;
}
std::optional<ImportRPLInformation> rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]);
if(!rplInfo) {
if (!rplInfo) {
DEBUG_FUNCTION_LINE("Failed to create import information");
break;
}
@@ -192,7 +192,7 @@ std::vector<RelocationData> ModuleDataFactory::getImportRelocationData(elfio& re
uint32_t section_index = psec->get_info();
// When these relocations are performed, we don't need the 0xC0000000 offset anymore.
RelocationData relocationData(type, offset - 0x02000000, addend, (void*)(destinations[section_index] + 0x02000000), sym_name, rplInfo.value());
RelocationData relocationData(type, offset - 0x02000000, addend, (void *) (destinations[section_index] + 0x02000000), sym_name, rplInfo.value());
//relocationData->printInformation();
result.push_back(relocationData);
}
@@ -200,52 +200,53 @@ std::vector<RelocationData> ModuleDataFactory::getImportRelocationData(elfio& re
}
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();
for (uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader.sections[i];
if(psec->get_info() == section_index) {
DEBUG_FUNCTION_LINE("Found relocation section %s",psec->get_name().c_str());
for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i];
if (psec->get_info() == section_index) {
DEBUG_FUNCTION_LINE("Found relocation section %s", psec->get_name().c_str());
relocation_section_accessor rel(reader, psec);
for ( uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j ) {
Elf64_Addr offset;
Elf_Word type;
Elf_Sxword addend;
std::string sym_name;
Elf64_Addr sym_value;
Elf_Half sym_section_index;
for (uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j) {
Elf64_Addr offset;
Elf_Word type;
Elf_Sxword addend;
std::string sym_name;
Elf64_Addr sym_value;
Elf_Half sym_section_index;
if(!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
if (!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
DEBUG_FUNCTION_LINE("Failed to get relocation");
break;
}
uint32_t adjusted_sym_value = (uint32_t) sym_value;
if((adjusted_sym_value >= 0x02000000) && adjusted_sym_value < 0x10000000) {
if ((adjusted_sym_value >= 0x02000000) && adjusted_sym_value < 0x10000000) {
adjusted_sym_value -= 0x02000000;
adjusted_sym_value += base_text;
} else if((adjusted_sym_value >= 0x10000000) && adjusted_sym_value < 0xC0000000) {
} else if ((adjusted_sym_value >= 0x10000000) && adjusted_sym_value < 0xC0000000) {
adjusted_sym_value -= 0x10000000;
adjusted_sym_value += base_data;
} else if(adjusted_sym_value >= 0xC0000000) {
} else if (adjusted_sym_value >= 0xC0000000) {
// Skip imports
continue;
} else if(adjusted_sym_value == 0x0) {
} else if (adjusted_sym_value == 0x0) {
//
} else {
DEBUG_FUNCTION_LINE("Unhandled case %08X",adjusted_sym_value);
DEBUG_FUNCTION_LINE("Unhandled case %08X", adjusted_sym_value);
return false;
}
if(sym_section_index == SHN_ABS) {
if (sym_section_index == SHN_ABS) {
//
} else if(sym_section_index > SHN_LORESERVE) {
} else if (sym_section_index > SHN_LORESERVE) {
DEBUG_FUNCTION_LINE("NOT IMPLEMENTED: %04X", sym_section_index);
return false;
}
if(!ElfUtils::elfLinkOne(type, offset, addend, destination, adjusted_sym_value, trampolin_data, trampolin_data_length, RELOC_TYPE_FIXED)) {
if (!ElfUtils::elfLinkOne(type, offset, addend, destination, adjusted_sym_value, trampolin_data, trampolin_data_length, RELOC_TYPE_FIXED)) {
DEBUG_FUNCTION_LINE("Link failed");
return false;
}

View File

@@ -26,7 +26,9 @@
class ModuleDataFactory {
public:
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::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);
};

View File

@@ -6,21 +6,21 @@
#include "ModuleData.h"
#include "RelocationData.h"
bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformation, const 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) {
if (module_count >= MAXIMUM_MODULES) {
DEBUG_FUNCTION_LINE("Reached maximum module count of %d", MAXIMUM_MODULES);
return false;
}
// Copy data to global struct.
module_information_single_t * module_data = &(moduleInformation->module_data[module_count]);
module_information_single_t *module_data = &(moduleInformation->module_data[module_count]);
DEBUG_FUNCTION_LINE("Saving reloation data for module at %08X", module.getEntrypoint());
// Relocation
std::vector<RelocationData> relocationData = module.getRelocationDataList();
for (auto const& reloc : relocationData) {
if(!DynamicLinkingHelper::addReloationEntry(&(moduleInformation->linking_data), module_data->linking_entries, DYN_LINK_RELOCATION_LIST_LENGTH, reloc)) {
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");
return false;
}
@@ -37,29 +37,29 @@ bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformat
moduleInformation->number_used_modules++;
DCFlushRange((void*)moduleInformation,sizeof(module_information_t));
ICInvalidateRange((void*)moduleInformation,sizeof(module_information_t));
DCFlushRange((void *) moduleInformation, sizeof(module_information_t));
ICInvalidateRange((void *) moduleInformation, sizeof(module_information_t));
return true;
}
std::vector<ModuleData> ModuleDataPersistence::loadModuleData(module_information_t * moduleInformation) {
std::vector<ModuleData> ModuleDataPersistence::loadModuleData(module_information_t *moduleInformation) {
std::vector<ModuleData> result;
if(moduleInformation == NULL) {
if (moduleInformation == NULL) {
DEBUG_FUNCTION_LINE("moduleInformation == NULL\n");
return result;
}
DCFlushRange((void*)moduleInformation,sizeof(module_information_t));
ICInvalidateRange((void*)moduleInformation,sizeof(module_information_t));
DCFlushRange((void *) moduleInformation, sizeof(module_information_t));
ICInvalidateRange((void *) moduleInformation, sizeof(module_information_t));
int32_t module_count = moduleInformation->number_used_modules;
if(module_count > MAXIMUM_MODULES) {
DEBUG_FUNCTION_LINE("moduleInformation->module_count was bigger then allowed. %d > %d. Limiting to %d\n",module_count, MAXIMUM_MODULES, MAXIMUM_MODULES);
if (module_count > MAXIMUM_MODULES) {
DEBUG_FUNCTION_LINE("moduleInformation->module_count was bigger then allowed. %d > %d. Limiting to %d\n", module_count, MAXIMUM_MODULES, MAXIMUM_MODULES);
module_count = MAXIMUM_MODULES;
}
for(int32_t i = 0; i < module_count; i++) {
for (int32_t i = 0; i < module_count; i++) {
// Copy data from struct.
module_information_single_t * module_data = &(moduleInformation->module_data[i]);
module_information_single_t *module_data = &(moduleInformation->module_data[i]);
ModuleData moduleData;
moduleData.setBSSLocation(module_data->bssAddr, module_data->bssSize);
moduleData.setSBSSLocation(module_data->sbssAddr, module_data->sbssSize);
@@ -67,27 +67,27 @@ std::vector<ModuleData> ModuleDataPersistence::loadModuleData(module_information
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]);
if(linking_entry->destination == NULL){
for (uint32_t j = 0; j < DYN_LINK_RELOCATION_LIST_LENGTH; j++) {
dyn_linking_relocation_entry_t *linking_entry = &(module_data->linking_entries[j]);
if (linking_entry->destination == NULL) {
break;
}
dyn_linking_import_t* importEntry = linking_entry->importEntry;
if(importEntry == NULL){
dyn_linking_import_t *importEntry = linking_entry->importEntry;
if (importEntry == NULL) {
DEBUG_FUNCTION_LINE("importEntry was NULL, skipping relocation entry\n");
continue;
}
if(importEntry->importName == NULL){
if (importEntry->importName == NULL) {
DEBUG_FUNCTION_LINE("importEntry->importName was NULL, skipping relocation entry\n");
continue;
}
dyn_linking_function_t* functionEntry = linking_entry->functionEntry;
dyn_linking_function_t *functionEntry = linking_entry->functionEntry;
if(functionEntry == NULL){
if (functionEntry == NULL) {
DEBUG_FUNCTION_LINE("functionEntry was NULL, skipping relocation entry\n");
continue;
}
if(functionEntry->functionName == NULL){
if (functionEntry->functionName == NULL) {
DEBUG_FUNCTION_LINE("functionEntry->functionName was NULL, skipping relocation entry\n");
continue;
}

View File

@@ -5,6 +5,7 @@
class ModuleDataPersistence {
public:
static bool saveModuleData(module_information_t * moduleInformation, const 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() 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() );
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,7 +23,7 @@
class RelocationData {
public:
RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, const ImportRPLInformation& rplInfo): rplInfo(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;
@@ -34,31 +34,32 @@ public:
~RelocationData() {
}
char getType() const{
char getType() const {
return type;
}
size_t getOffset() const{
size_t getOffset() const {
return offset;
}
int32_t getAddend() const{
int32_t getAddend() const {
return addend;
}
void * getDestination() const{
void *getDestination() const {
return destination;
}
std::string getName() const{
std::string getName() const {
return name;
}
ImportRPLInformation getImportRPLInformation() const{
ImportRPLInformation getImportRPLInformation() const {
return rplInfo;
}
std::string toString() const;
private:
char type;
size_t offset;