mirror of
https://github.com/wiiu-env/WUMSLoader.git
synced 2026-09-14 14:27:21 -05:00
first commit
This commit is contained in:
104
source/module/DynamicLinkingHelper.cpp
Normal file
104
source/module/DynamicLinkingHelper.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include "DynamicLinkingHelper.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
#include <coreinit/dynload.h>
|
||||
#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) {
|
||||
return 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) {
|
||||
DEBUG_FUNCTION_LINE("Failed to add function name, it's too long.\n");
|
||||
return NULL;
|
||||
}
|
||||
strncpy(curEntry->functionName,functionName,DYN_LINK_FUNCTION_NAME_LENGTH);
|
||||
result = curEntry;
|
||||
break;
|
||||
}
|
||||
if(strncmp(curEntry->functionName,functionName,DYN_LINK_FUNCTION_NAME_LENGTH) == 0) {
|
||||
result = curEntry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
DEBUG_FUNCTION_LINE("Failed to add Import, it's too long.\n");
|
||||
return NULL;
|
||||
}
|
||||
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)) {
|
||||
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, 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());
|
||||
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);
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
curEntry->type = type;
|
||||
curEntry->offset = offset;
|
||||
curEntry->addend = addend;
|
||||
curEntry->destination = destination;
|
||||
curEntry->functionEntry = functionName;
|
||||
curEntry->importEntry = importInfo;
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
60
source/module/DynamicLinkingHelper.h
Normal file
60
source/module/DynamicLinkingHelper.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
#include "common/dynamic_linking_defines.h"
|
||||
#include "utils/logger.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "RelocationData.h"
|
||||
|
||||
class DynamicLinkingHelper {
|
||||
public:
|
||||
/**
|
||||
Gets the function entry for a given function name. If the function name is not present in the list, it will be added.
|
||||
|
||||
\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);
|
||||
|
||||
/**
|
||||
Gets the function import entry for a given function name. If the import is not present in the list, it will be added.
|
||||
This will return entries for _function_ imports.
|
||||
|
||||
\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);
|
||||
|
||||
|
||||
/**
|
||||
Gets the data import entry for a given data name. If the import is not present in the list, it will be added.
|
||||
This will return entries for _data_ imports.
|
||||
|
||||
\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);
|
||||
|
||||
|
||||
/**
|
||||
Gets the import entry for a given data name and type. If the import is not present in the list, it will be added.
|
||||
This will return entries for _data_ and _function_ imports, depending on the isData parameter.
|
||||
|
||||
\param importName Name of the data
|
||||
\param isData Set this to true to return a data import
|
||||
|
||||
\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 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, char type, size_t offset, int32_t addend, void *destination, std::string name, 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:
|
||||
DynamicLinkingHelper() {
|
||||
}
|
||||
|
||||
~DynamicLinkingHelper() {
|
||||
}
|
||||
};
|
||||
67
source/module/ImportRPLInformation.h
Normal file
67
source/module/ImportRPLInformation.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/****************************************************************************
|
||||
* Copyright (C) 2018 Maschell
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "utils/logger.h"
|
||||
|
||||
class ImportRPLInformation {
|
||||
|
||||
public:
|
||||
ImportRPLInformation(std::string name, bool isData = false) {
|
||||
this->name = name;
|
||||
this->_isData = isData;
|
||||
}
|
||||
|
||||
~ImportRPLInformation() {
|
||||
}
|
||||
|
||||
static ImportRPLInformation * createImportRPLInformation(std::string rawSectionName) {
|
||||
std::string fimport = ".fimport_";
|
||||
std::string dimport = ".dimport_";
|
||||
|
||||
bool data = false;
|
||||
|
||||
std::string rplName = "";
|
||||
|
||||
if(rawSectionName.size() < fimport.size()) {
|
||||
return NULL;
|
||||
} 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())) {
|
||||
rplName = rawSectionName.substr(dimport.size());
|
||||
data = true;
|
||||
} else {
|
||||
DEBUG_FUNCTION_LINE("invalid section name\n");
|
||||
return NULL;
|
||||
}
|
||||
return new ImportRPLInformation(rplName, data);
|
||||
}
|
||||
|
||||
std::string getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
bool isData() {
|
||||
return _isData;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
bool _isData = false;
|
||||
};
|
||||
12
source/module/ModuleData.cpp
Normal file
12
source/module/ModuleData.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "ModuleData.h"
|
||||
#include "utils/StringTools.h"
|
||||
|
||||
std::string ModuleData::toString() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
88
source/module/ModuleData.h
Normal file
88
source/module/ModuleData.h
Normal file
@@ -0,0 +1,88 @@
|
||||
/****************************************************************************
|
||||
* Copyright (C) 2018 Maschell
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "RelocationData.h"
|
||||
|
||||
class ModuleData {
|
||||
public:
|
||||
ModuleData() {
|
||||
}
|
||||
|
||||
~ModuleData() {
|
||||
for (auto const& reloc : relocation_data_list) {
|
||||
if(reloc != NULL) {
|
||||
delete reloc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setBSSLocation(uint32_t addr, uint32_t size) {
|
||||
this->bssAddr = addr;
|
||||
this->bssSize = size;
|
||||
}
|
||||
|
||||
void setSBSSLocation(uint32_t addr, uint32_t size) {
|
||||
this->sbssAddr = addr;
|
||||
this->sbssSize = size;
|
||||
}
|
||||
|
||||
void setEntrypoint(uint32_t addr) {
|
||||
this->entrypoint = addr;
|
||||
}
|
||||
|
||||
void addRelocationData(RelocationData * relocation_data) {
|
||||
relocation_data_list.push_back(relocation_data);
|
||||
}
|
||||
|
||||
std::vector<RelocationData *> getRelocationDataList() {
|
||||
return relocation_data_list;
|
||||
}
|
||||
|
||||
uint32_t getBSSAddr() {
|
||||
return bssAddr;
|
||||
}
|
||||
|
||||
uint32_t getBSSSize() {
|
||||
return bssSize;
|
||||
}
|
||||
|
||||
uint32_t getSBSSAddr() {
|
||||
return sbssAddr;
|
||||
}
|
||||
|
||||
uint32_t getSBSSSize() {
|
||||
return sbssSize;
|
||||
}
|
||||
|
||||
uint32_t getEntrypoint() {
|
||||
return entrypoint;
|
||||
}
|
||||
|
||||
std::string toString();
|
||||
private:
|
||||
std::vector<RelocationData *> relocation_data_list;
|
||||
|
||||
uint32_t bssAddr = 0;
|
||||
uint32_t bssSize = 0;
|
||||
uint32_t sbssAddr = 0;
|
||||
uint32_t sbssSize = 0;
|
||||
uint32_t entrypoint = 0;
|
||||
};
|
||||
248
source/module/ModuleDataFactory.cpp
Normal file
248
source/module/ModuleDataFactory.cpp
Normal file
@@ -0,0 +1,248 @@
|
||||
/****************************************************************************
|
||||
* Copyright (C) 2018 Maschell
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
****************************************************************************/
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <coreinit/cache.h>
|
||||
#include "ModuleDataFactory.h"
|
||||
#include "elfio/elfio.hpp"
|
||||
#include "utils/utils.h"
|
||||
#include "ElfUtils.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) {
|
||||
elfio reader;
|
||||
ModuleData * moduleData = new ModuleData();
|
||||
if(moduleData == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Load ELF data
|
||||
if (!reader.load(path)) {
|
||||
DEBUG_FUNCTION_LINE("Can't find or process %s", path.c_str());
|
||||
delete moduleData;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t sec_num = reader.sections.size();
|
||||
|
||||
uint8_t **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num);
|
||||
|
||||
uint32_t baseOffset = destination_address;
|
||||
|
||||
uint32_t offset_text = baseOffset;
|
||||
uint32_t offset_data = offset_text;
|
||||
|
||||
uint32_t entrypoint = offset_text + (uint32_t) reader.get_entry() - 0x02000000;
|
||||
|
||||
uint32_t totalSize = 0;
|
||||
|
||||
for(uint32_t i = 0; i < sec_num; ++i ) {
|
||||
section* psec = reader.sections[i];
|
||||
if (psec->get_type() == 0x80000002) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
|
||||
uint32_t sectionSize = psec->get_size();
|
||||
uint32_t address = (uint32_t) psec->get_address();
|
||||
|
||||
destinations[psec->get_index()] = (uint8_t *) baseOffset;
|
||||
|
||||
uint32_t destination = baseOffset + address;
|
||||
if((address >= 0x02000000) && address < 0x10000000) {
|
||||
destination -= 0x02000000;
|
||||
destinations[psec->get_index()] -= 0x02000000;
|
||||
baseOffset += sectionSize;
|
||||
offset_data += sectionSize;
|
||||
} else if((address >= 0x10000000) && address < 0xC0000000) {
|
||||
destination -= 0x10000000;
|
||||
destinations[psec->get_index()] -= 0x10000000;
|
||||
} else if(address >= 0xC0000000) {
|
||||
destination -= 0xC0000000;
|
||||
destinations[psec->get_index()] -= 0xC0000000;
|
||||
} else {
|
||||
DEBUG_FUNCTION_LINE("Unhandled case");
|
||||
free(destinations);
|
||||
delete moduleData;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* p = reader.sections[i]->get_data();
|
||||
|
||||
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) {
|
||||
DEBUG_FUNCTION_LINE("Copy section %s %08X -> %08X (%d bytes)", psec->get_name().c_str(), p, destination, sectionSize);
|
||||
memcpy((void*) destination, p, sectionSize);
|
||||
}
|
||||
|
||||
//nextAddress = ROUNDUP(destination + sectionSize,0x100);
|
||||
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) {
|
||||
moduleData->setSBSSLocation(destination, sectionSize);
|
||||
DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize);
|
||||
}
|
||||
totalSize += sectionSize;
|
||||
|
||||
DCFlushRange((void*)destination, sectionSize);
|
||||
ICInvalidateRange((void*)destination, sectionSize);
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<RelocationData*> relocationData = getImportRelocationData(reader, destinations);
|
||||
|
||||
for (auto const& reloc : relocationData) {
|
||||
moduleData->addRelocationData(reloc);
|
||||
}
|
||||
|
||||
DCFlushRange((void*)destination_address, totalSize);
|
||||
ICInvalidateRange((void*)destination_address, totalSize);
|
||||
|
||||
free(destinations);
|
||||
|
||||
moduleData->setEntrypoint(entrypoint);
|
||||
DEBUG_FUNCTION_LINE("Saved entrypoint as %08X", entrypoint);
|
||||
|
||||
return moduleData;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
|
||||
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());
|
||||
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;
|
||||
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
ImportRPLInformation * rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]);
|
||||
if(rplInfo == NULL) {
|
||||
DEBUG_FUNCTION_LINE("Failed to create import information");
|
||||
break;
|
||||
}
|
||||
|
||||
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->printInformation();
|
||||
result.push_back(relocationData);
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
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());
|
||||
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;
|
||||
|
||||
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) {
|
||||
adjusted_sym_value -= 0x02000000;
|
||||
adjusted_sym_value += base_text;
|
||||
} 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) {
|
||||
// Skip imports
|
||||
continue;
|
||||
} else if(adjusted_sym_value == 0x0) {
|
||||
//
|
||||
} else {
|
||||
DEBUG_FUNCTION_LINE("Unhandled case %08X",adjusted_sym_value);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(sym_section_index == SHN_ABS) {
|
||||
//
|
||||
} 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)) {
|
||||
DEBUG_FUNCTION_LINE("Link failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
DEBUG_FUNCTION_LINE("done");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
32
source/module/ModuleDataFactory.h
Normal file
32
source/module/ModuleDataFactory.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/****************************************************************************
|
||||
* Copyright (C) 2019 Maschell
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "common/relocation_defines.h"
|
||||
#include "ModuleData.h"
|
||||
#include "elfio/elfio.hpp"
|
||||
|
||||
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 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);
|
||||
};
|
||||
106
source/module/ModuleDataPersistence.cpp
Normal file
106
source/module/ModuleDataPersistence.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <coreinit/cache.h>
|
||||
|
||||
#include "ModuleDataPersistence.h"
|
||||
#include "DynamicLinkingHelper.h"
|
||||
#include "common/module_defines.h"
|
||||
#include "ModuleData.h"
|
||||
#include "RelocationData.h"
|
||||
|
||||
bool ModuleDataPersistence::saveModuleData(module_information_t * moduleInformation, ModuleData * module) {
|
||||
int32_t module_count = moduleInformation->number_used_modules;
|
||||
|
||||
if(module_count >= MAXIMUM_MODULES) {
|
||||
return false;
|
||||
}
|
||||
// Copy data to global struct.
|
||||
module_information_single_t * module_data = &(moduleInformation->module_data[module_count]);
|
||||
|
||||
// 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)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module_data->bssAddr = module->getBSSAddr();
|
||||
module_data->bssSize = module->getBSSSize();
|
||||
module_data->sbssAddr = module->getSBSSAddr();
|
||||
module_data->sbssSize = module->getSBSSSize();
|
||||
|
||||
module_data->entrypoint = module->getEntrypoint();
|
||||
|
||||
moduleInformation->number_used_modules++;
|
||||
|
||||
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*> result;
|
||||
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));
|
||||
|
||||
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);
|
||||
module_count = MAXIMUM_MODULES;
|
||||
}
|
||||
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);
|
||||
|
||||
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){
|
||||
DEBUG_FUNCTION_LINE("importEntry was NULL, skipping relocation entry\n");
|
||||
continue;
|
||||
}
|
||||
if(importEntry->importName == NULL){
|
||||
DEBUG_FUNCTION_LINE("importEntry->importName was NULL, skipping relocation entry\n");
|
||||
continue;
|
||||
}
|
||||
dyn_linking_function_t* functionEntry = linking_entry->functionEntry;
|
||||
|
||||
if(functionEntry == NULL){
|
||||
DEBUG_FUNCTION_LINE("functionEntry was NULL, skipping relocation entry\n");
|
||||
continue;
|
||||
}
|
||||
if(functionEntry->functionName == NULL){
|
||||
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);
|
||||
}
|
||||
result.push_back(moduleData);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
10
source/module/ModuleDataPersistence.h
Normal file
10
source/module/ModuleDataPersistence.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/module_defines.h"
|
||||
#include "ModuleData.h"
|
||||
|
||||
class ModuleDataPersistence {
|
||||
public:
|
||||
static bool saveModuleData(module_information_t * moduleInformation, ModuleData * module);
|
||||
static std::vector<ModuleData*> loadModuleData(module_information_t * moduleInformation);
|
||||
};
|
||||
6
source/module/RelocationData.cpp
Normal file
6
source/module/RelocationData.cpp
Normal file
@@ -0,0 +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() );
|
||||
}
|
||||
73
source/module/RelocationData.h
Normal file
73
source/module/RelocationData.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/****************************************************************************
|
||||
* Copyright (C) 2018 Maschell
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "ImportRPLInformation.h"
|
||||
|
||||
class RelocationData {
|
||||
|
||||
public:
|
||||
RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, ImportRPLInformation * 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() {
|
||||
return type;
|
||||
}
|
||||
|
||||
size_t getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
int32_t getAddend() {
|
||||
return addend;
|
||||
}
|
||||
|
||||
void * getDestination() {
|
||||
return destination;
|
||||
}
|
||||
|
||||
std::string getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
ImportRPLInformation * getImportRPLInformation() {
|
||||
return rplInfo;
|
||||
}
|
||||
|
||||
std::string toString();
|
||||
private:
|
||||
char type;
|
||||
size_t offset;
|
||||
int32_t addend;
|
||||
void *destination;
|
||||
std::string name;
|
||||
ImportRPLInformation * rplInfo;
|
||||
};
|
||||
Reference in New Issue
Block a user