First iteration of the WUPS-Backend as a .rpx/module for the SetupPayload

- Function replacements are not implemented yet
- Serveral features like the config menu are missing

Still WIP
This commit is contained in:
Maschell
2020-04-29 18:02:36 +02:00
parent a83d11379e
commit 2705a91c13
188 changed files with 7644 additions and 27184 deletions

View 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/plugin_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, 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, const void *destination, const 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);
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, const 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 = (void*) destination;
curEntry->functionEntry = functionName;
curEntry->importEntry = importInfo;
return true;
}
return false;
}

View 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, 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, const void *destination, const 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, const void *destination, dyn_linking_function_t * functionName, dyn_linking_import_t * importInfo);
private:
DynamicLinkingHelper() {
}
~DynamicLinkingHelper() {
}
};

View File

@@ -0,0 +1,69 @@
/****************************************************************************
* Copyright (C) 2018-2020 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 <wups.h>
#include <string>
class FunctionData {
public:
FunctionData(void * paddress, void * vaddress, const std::string& name, wups_loader_library_type_t library, void * replaceAddr, void * replaceCall) {
this->paddress = paddress;
this->vaddress = vaddress;
this->name = name;
this->library = library;
this->replaceAddr = replaceAddr;
this->replaceCall = replaceCall;
}
~FunctionData() {
}
const std::string& getName() const{
return this->name;
}
wups_loader_library_type_t getLibrary() const{
return this->library;
}
const void * getPhysicalAddress() const{
return paddress;
}
const void * getVirtualAddress() const{
return vaddress;
}
const void * getReplaceAddress() const{
return replaceAddr;
}
const void * getReplaceCall() const{
return replaceCall;
}
private:
void * paddress = NULL;
void * vaddress = NULL;
std::string name;
wups_loader_library_type_t library;
void * replaceAddr = NULL;
void * replaceCall = NULL;
};

45
source/plugin/HookData.h Normal file
View File

@@ -0,0 +1,45 @@
/****************************************************************************
* Copyright (C) 2018-2020 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 <wups.h>
#include <string>
class HookData {
public:
HookData(void * function_pointer, wups_loader_hook_type_t type) {
this->function_pointer = function_pointer;
this->type = type;
}
~HookData() {
}
void * getFunctionPointer() const{
return function_pointer;
}
wups_loader_hook_type_t getType() const{
return this->type;
}
private:
void * function_pointer;
wups_loader_hook_type_t type;
};

View File

@@ -0,0 +1,45 @@
/****************************************************************************
* 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() {
}
std::string getName() const {
return name;
}
bool isData() const{
return _isData;
}
private:
std::string name;
bool _isData = false;
};

View File

@@ -0,0 +1,56 @@
/****************************************************************************
* Copyright (C) 2020 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 "PluginData.h"
#include "PluginMetaInformation.h"
#include "PluginInformation.h"
class PluginContainer {
public:
PluginContainer(){
}
const PluginMetaInformation& getMetaInformation() const{
return this->metaInformation;
}
void setMetaInformation(PluginMetaInformation& metaInfo){
this->metaInformation = metaInfo;
}
const PluginInformation& getPluginInformation() const{
return pluginInformation;
}
void setPluginInformation(PluginInformation& pluginInformation){
this->pluginInformation = pluginInformation;
}
const PluginData& getPluginData() const{
return pluginData;
}
void setPluginData(PluginData& pluginData){
this->pluginData = pluginData;
}
PluginData pluginData;
PluginMetaInformation metaInformation;
PluginInformation pluginInformation;
};

View File

@@ -0,0 +1,296 @@
#include <coreinit/cache.h>
#include "PluginContainer.h"
#include "PluginInformationFactory.h"
#include "PluginMetaInformationFactory.h"
#include "PluginContainerPersistence.h"
#include "DynamicLinkingHelper.h"
#include "common/plugin_defines.h"
#include "PluginInformation.h"
#include "RelocationData.h"
bool PluginContainerPersistence::savePlugin(plugin_information_t * pluginInformation, PluginContainer& plugin) {
int32_t plugin_count = pluginInformation->number_used_plugins;
auto pluginName = plugin.getMetaInformation().getName();
//auto pluginPath = plugin.getMetaInformation().getPath();
if(plugin_count >= MAXIMUM_PLUGINS - 1) {
DEBUG_FUNCTION_LINE("Maximum of %d plugins reached. %s won't be loaded!\n", MAXIMUM_PLUGINS, pluginName.c_str());
return false;
}
// Copy data to global struct.
plugin_information_single_t * plugin_data = &(pluginInformation->plugin_data[plugin_count]);
DEBUG_FUNCTION_LINE("%08X", plugin_data);
// Make sure everything is reset.
//plugin_data = {};
memset((void*)plugin_data, 0, sizeof(plugin_information_single_t));
auto pluginMetaInfo = plugin.getMetaInformation();
auto plugin_meta_data = &plugin_data->meta;
if(pluginMetaInfo.getName().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
DEBUG_FUNCTION_LINE("Warning: name will be truncated.");
}
strncpy(plugin_meta_data->name, pluginMetaInfo.getName().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH-1);
if(pluginMetaInfo.getAuthor().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
DEBUG_FUNCTION_LINE("Warning: author will be truncated.");
}
strncpy(plugin_meta_data->author, pluginMetaInfo.getAuthor().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH-1);
if(pluginMetaInfo.getVersion().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
DEBUG_FUNCTION_LINE("Warning: version will be truncated.");
}
strncpy(plugin_meta_data->version, pluginMetaInfo.getVersion().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH-1);
if(pluginMetaInfo.getLicense().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
DEBUG_FUNCTION_LINE("Warning: license will be truncated.");
}
strncpy(plugin_meta_data->license, pluginMetaInfo.getLicense().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH-1);
if(pluginMetaInfo.getBuildTimestamp().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
DEBUG_FUNCTION_LINE("Warning: build timestampt will be truncated.");
}
strncpy(plugin_meta_data->buildTimestamp, pluginMetaInfo.getBuildTimestamp().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH-1);
if(pluginMetaInfo.getDescription().size() >= MAXIMUM_PLUGIN_DESCRIPTION_LENGTH) {
DEBUG_FUNCTION_LINE("Warning: description will be truncated.");
DEBUG_FUNCTION_LINE("%s", pluginMetaInfo.getDescription().c_str());
}
strncpy(plugin_meta_data->descripion, pluginMetaInfo.getDescription().c_str(), MAXIMUM_PLUGIN_DESCRIPTION_LENGTH-1);
plugin_meta_data->size = pluginMetaInfo.getSize();
auto pluginInfo = plugin.getPluginInformation();
// Relocation
std::vector<RelocationData> relocationData = pluginInfo.getRelocationDataList();
for (auto & reloc : relocationData) {
if(!DynamicLinkingHelper::addReloationEntry(&(pluginInformation->linking_data), plugin_data->info.linking_entries, DYN_LINK_RELOCATION_LIST_LENGTH, reloc)) {
return false;
}
}
std::vector<FunctionData> function_data_list = pluginInfo.getFunctionDataList();
std::vector<HookData> hook_data_list = pluginInfo.getHookDataList();
if(function_data_list.size() > MAXIMUM_FUNCTION_PER_PLUGIN) {
DEBUG_FUNCTION_LINE("Plugin %s would replace to many function (%d, maximum is %d). It won't be loaded.",pluginName.c_str(), function_data_list.size(), MAXIMUM_FUNCTION_PER_PLUGIN);
return false;
}
if(hook_data_list.size() > MAXIMUM_HOOKS_PER_PLUGIN) {
DEBUG_FUNCTION_LINE("Plugin %s would set too many hooks (%d, maximum is %d). It won't be loaded.", pluginName.c_str(), hook_data_list.size(), MAXIMUM_HOOKS_PER_PLUGIN);
return false;
}
if(pluginName.length() > MAXIMUM_PLUGIN_NAME_LENGTH-1) {
DEBUG_FUNCTION_LINE("Name for plugin %s was too long to be stored.", pluginName.c_str());
return false;
}
/* Store function replacement information */
uint32_t i = 0;
for(auto & curFunction : pluginInfo.getFunctionDataList()) {
replacement_data_function_t * function_data = &plugin_data->info.functions[i];
if(strlen(curFunction.getName().c_str()) > MAXIMUM_FUNCTION_NAME_LENGTH-1) {
DEBUG_FUNCTION_LINE("Could not add function \"%s\" for plugin \"%s\" function name is too long.", curFunction.getName().c_str(), pluginName.c_str());
continue;
}
DEBUG_FUNCTION_LINE("Adding function \"%s\" for plugin \"%s\"",curFunction.getName().c_str(), pluginName.c_str());
strncpy(function_data->function_name, curFunction.getName().c_str(),MAXIMUM_FUNCTION_NAME_LENGTH-1);
function_data->library = curFunction.getLibrary();
function_data->replaceAddr = (uint32_t) curFunction.getReplaceAddress();
function_data->replaceCall = (uint32_t) curFunction.getReplaceCall();
function_data->physicalAddr = (uint32_t) curFunction.getPhysicalAddress();
function_data->virtualAddr = (uint32_t) curFunction.getVirtualAddress();
plugin_data->info.number_used_functions++;
i++;
}
i = 0;
for(auto & curHook : pluginInfo.getHookDataList()) {
replacement_data_hook_t * hook_data = &plugin_data->info.hooks[i];
DEBUG_FUNCTION_LINE("Set hook for plugin \"%s\" of type %08X to target %08X",plugin_data->meta.name, curHook.getType(),(void*) curHook.getFunctionPointer());
hook_data->func_pointer = (void*) curHook.getFunctionPointer();
hook_data->type = curHook.getType();
plugin_data->info.number_used_hooks++;
i++;
}
/* Saving SectionInfos */
for(auto & curSection : pluginInfo.getSectionInfoList()) {
bool foundFreeSlot = false;
uint32_t slot = 0;
for(uint32_t i = 0; i < MAXIMUM_PLUGIN_SECTION_LENGTH; i++) {
plugin_section_info_t * sectionInfo = &(plugin_data->info.sectionInfos[i]);
if(sectionInfo->addr == 0 && sectionInfo->size == 0) {
foundFreeSlot = true;
slot = i;
break;
}
}
if(foundFreeSlot) {
plugin_section_info_t * sectionInfo = &(plugin_data->info.sectionInfos[slot]);
if(strlen(curSection.first.c_str()) > MAXIMUM_PLUGIN_SECTION_NAME_LENGTH-1) {
DEBUG_FUNCTION_LINE("Could not add section info \"%s\" for plugin \"%s\" section name is too long.",curSection.first.c_str(), pluginName.c_str());
break;
}
strncpy(sectionInfo->name, curSection.first.c_str(), MAXIMUM_PLUGIN_SECTION_NAME_LENGTH-1);
sectionInfo->addr = curSection.second.getAddress();
sectionInfo->size = curSection.second.getSize();
} else {
DEBUG_FUNCTION_LINE("Failed to store SectionInfos");
return false;
}
}
plugin_data->info.trampolinId = pluginInfo.getTrampolinId();
/* Copy plugin data */
auto pluginData = plugin.getPluginData();
auto plugin_data_data = &plugin_data->data;
plugin_data_data->buffer = (char*)pluginData.buffer;
plugin_data_data->bufferLength = pluginData.length;
plugin_data_data->memoryType = pluginData.memoryType;
plugin_data_data->heapHandle = (int)pluginData.heapHandle;
pluginInformation->number_used_plugins++;
DCFlushRange((void*)pluginInformation,sizeof(plugin_information_t));
ICInvalidateRange((void*)pluginInformation,sizeof(plugin_information_t));
return true;
}
std::vector<PluginContainer> PluginContainerPersistence::loadPlugins(plugin_information_t * pluginInformation) {
std::vector<PluginContainer> result;
if(pluginInformation == NULL) {
DEBUG_FUNCTION_LINE("pluginInformation == NULL");
return result;
}
DCFlushRange((void*)pluginInformation,sizeof(plugin_information_t));
ICInvalidateRange((void*)pluginInformation,sizeof(plugin_information_t));
int32_t plugin_count = pluginInformation->number_used_plugins;
if(plugin_count > MAXIMUM_PLUGINS) {
DEBUG_FUNCTION_LINE("pluginInformation->plugin_count was bigger then allowed. %d > %d. Limiting to %d",plugin_count, MAXIMUM_PLUGINS, MAXIMUM_PLUGINS);
plugin_count = MAXIMUM_PLUGINS;
}
for(int32_t i = 0; i < plugin_count; i++) {
// Copy data from struct.
plugin_information_single_t * plugin_data = &(pluginInformation->plugin_data[i]);
PluginMetaInformation metaInformation;
plugin_meta_info_t * meta = &(plugin_data->meta);
metaInformation.setAuthor(meta->author);
metaInformation.setVersion(meta->version);
metaInformation.setBuildTimestamp(meta->buildTimestamp);
metaInformation.setLicense(meta->license);
metaInformation.setDescription(meta->descripion);
metaInformation.setSize(meta->size);
metaInformation.setName(meta->name);
PluginData pluginData;
plugin_data_t * data = &(plugin_data->data);
pluginData.buffer = data->buffer;
pluginData.length = data->bufferLength;
pluginData.memoryType = (eMemoryTypes) data->memoryType;
pluginData.heapHandle = (MEMHeapHandle) data->heapHandle;
pluginData.loadReader();
PluginInformation pluginInformation;
pluginInformation.setTrampolinId(plugin_data->info.trampolinId);
for(uint32_t i = 0; i < MAXIMUM_PLUGIN_SECTION_LENGTH; i++) {
plugin_section_info_t * sectionInfo = &(plugin_data->info.sectionInfos[i]);
if(sectionInfo->addr == 0 && sectionInfo->size == 0) {
continue;
}
DEBUG_FUNCTION_LINE("Add SectionInfo %s", sectionInfo->name);
pluginInformation.addSectionInfo(SectionInfo(sectionInfo->name, sectionInfo->addr, sectionInfo->size));
}
/* load hook data */
uint32_t hookCount = plugin_data->info.number_used_hooks;
if(hookCount > MAXIMUM_HOOKS_PER_PLUGIN) {
DEBUG_FUNCTION_LINE("number_used_hooks was bigger then allowed. %d > %d. Limiting to %d",hookCount, MAXIMUM_HOOKS_PER_PLUGIN, MAXIMUM_HOOKS_PER_PLUGIN);
hookCount = MAXIMUM_HOOKS_PER_PLUGIN;
}
for(uint32_t j = 0; j < hookCount; j++) {
replacement_data_hook_t * hook_entry = &(plugin_data->info.hooks[j]);
HookData curHook(hook_entry->func_pointer, hook_entry->type);
pluginInformation.addHookData(curHook);
}
/* load function replacement data */
uint32_t functionReplaceCount = plugin_data->info.number_used_functions;
if(functionReplaceCount > MAXIMUM_FUNCTION_PER_PLUGIN) {
DEBUG_FUNCTION_LINE("number_used_functions was bigger then allowed. %d > %d. Limiting to %d",functionReplaceCount, MAXIMUM_FUNCTION_PER_PLUGIN, MAXIMUM_FUNCTION_PER_PLUGIN);
functionReplaceCount = MAXIMUM_FUNCTION_PER_PLUGIN;
}
for(uint32_t j = 0; j < functionReplaceCount; j++) {
replacement_data_function_t * entry = &(plugin_data->info.functions[j]);
FunctionData func((void *) entry->physicalAddr, (void *) entry->virtualAddr, entry->function_name, entry->library, (void *) entry->replaceAddr, (void *) entry->replaceCall);
pluginInformation.addFunctionData(func);
}
/* load relocation data */
for(uint32_t j = 0; j < DYN_LINK_RELOCATION_LIST_LENGTH; j++) {
dyn_linking_relocation_entry_t * linking_entry = &(plugin_data->info.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");
continue;
}
if(importEntry->importName == NULL) {
DEBUG_FUNCTION_LINE("importEntry->importName was NULL, skipping relocation entry");
continue;
}
dyn_linking_function_t* functionEntry = linking_entry->functionEntry;
if(functionEntry == NULL) {
DEBUG_FUNCTION_LINE("functionEntry was NULL, skipping relocation entry");
continue;
}
if(functionEntry->functionName == NULL) {
DEBUG_FUNCTION_LINE("functionEntry->functionName was NULL, skipping relocation entry");
continue;
}
ImportRPLInformation rplInfo(importEntry->importName, importEntry->isData);
RelocationData reloc(linking_entry->type, linking_entry->offset, linking_entry->addend, linking_entry->destination, functionEntry->functionName, rplInfo);
pluginInformation.addRelocationData(reloc);
}
PluginContainer container;
container.setMetaInformation(metaInformation);
container.setPluginData(pluginData);
container.setPluginInformation(pluginInformation);
result.push_back(container);
}
return result;
}

View File

@@ -0,0 +1,10 @@
#pragma once
#include "common/plugin_defines.h"
#include "PluginContainer.h"
class PluginContainerPersistence {
public:
static bool savePlugin(plugin_information_t * pluginInformation, PluginContainer& plugin);
static std::vector<PluginContainer> loadPlugins(plugin_information_t * pluginInformation);
};

View File

@@ -0,0 +1,76 @@
#include "PluginData.h"
void PluginData::freeMemory() {
if(buffer == NULL) {
return;
}
switch(memoryType) {
default:
case eMemTypeExpHeap:
MEMFreeToExpHeap(this->heapHandle, buffer);
this->buffer = NULL;
break;
case eMemTypeMEM2:
free(this->buffer);
this->buffer = NULL;
break;
}
}
PluginData::PluginData(std::vector<uint8_t> buffer) : PluginData(buffer, 0, eMemTypeMEM2) {
}
void PluginData::loadReader() {
if(this->buffer == NULL) {
this->reader = std::nullopt;
} else {
elfio * nReader = new elfio;
if(nReader != NULL && nReader->load((char*)this->buffer, length)) {
DEBUG_FUNCTION_LINE("Loading was okay");
this->reader = nReader;
} else {
if(nReader){
delete nReader;
nReader = NULL;
}
DEBUG_FUNCTION_LINE("Loading failed");
this->reader = std::nullopt;
}
}
}
PluginData::PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMemoryTypes memoryType):
heapHandle(heapHandle),
memoryType(memoryType),
length(input.size()) {
void * data_copy = NULL;
switch(memoryType) {
default:
case eMemTypeExpHeap:
data_copy = MEMAllocFromExpHeapEx(heapHandle, length, 4);
if(data_copy == NULL) {
DEBUG_FUNCTION_LINE("Failed to allocate space on exp heap");
} else {
memcpy(data_copy, &input[0], length);
}
this->buffer = data_copy;
DEBUG_FUNCTION_LINE("copied data to exp heap");
break;
case eMemTypeMEM2:
data_copy = memalign(length, 4);
if(data_copy == NULL) {
DEBUG_FUNCTION_LINE("Failed to allocate space on default heap");
} else {
memcpy(data_copy, &input[0], length);
}
this->buffer = data_copy;
break;
}
loadReader();
}
std::optional<PluginData> PluginData::createFromExistingData(const void* buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length) {
return std::nullopt;
}

View File

@@ -0,0 +1,68 @@
/****************************************************************************
* Copyright (C) 2020 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 <optional>
#include <vector>
#include <malloc.h>
#include <coreinit/memexpheap.h>
#include "elfio/elfio.hpp"
using namespace ELFIO;
enum eMemoryTypes {
eMemTypeMEM2,
eMemTypeExpHeap
};
class PluginData {
public:
const std::optional<elfio*>& getReader() const {
return reader;
}
~PluginData() {
if(nReader != NULL) {
delete nReader;
nReader = NULL;
}
}
void freeMemory();
private:
PluginData() {
}
PluginData(std::vector<uint8_t> buffer);
PluginData(std::vector<uint8_t> input, MEMHeapHandle heapHandle, eMemoryTypes memoryType);
void loadReader();
static std::optional<PluginData> createFromExistingData(const void* buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length);
std::optional<elfio*> reader;
elfio* nReader = NULL;
void* buffer;
MEMHeapHandle heapHandle;
eMemoryTypes memoryType;
size_t length;
friend class PluginDataFactory;
friend class PluginContainer;
friend class PluginContainerPersistence;
};

View File

@@ -0,0 +1,112 @@
/****************************************************************************
* 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 <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "PluginDataFactory.h"
#include "utils/logger.h"
#include "utils/StringTools.h"
std::vector<PluginData> PluginDataFactory::loadDir(const std::string & path, MEMHeapHandle heapHandle) {
std::vector<PluginData> result;
struct dirent *dp;
DIR *dfd = NULL;
if(path.empty()) {
DEBUG_FUNCTION_LINE("Path was empty\n");
return result;
}
if ((dfd = opendir(path.c_str())) == NULL) {
DEBUG_FUNCTION_LINE("Couldn't open dir %s\n",path.c_str());
return result;
}
while ((dp = readdir(dfd)) != NULL) {
struct stat stbuf ;
std::string full_file_path = StringTools::strfmt("%s/%s",path.c_str(),dp->d_name);
StringTools::RemoveDoubleSlashs(full_file_path);
if( stat(full_file_path.c_str(),&stbuf ) == -1 ) {
DEBUG_FUNCTION_LINE("Unable to stat file: %s\n",full_file_path.c_str()) ;
continue;
}
if ( ( stbuf.st_mode & S_IFMT ) == S_IFDIR ) { // Skip directories
continue;
} else {
DEBUG_FUNCTION_LINE("Found file: %s\n",full_file_path.c_str()) ;
auto pluginData = load(full_file_path, heapHandle);
if(pluginData) {
result.push_back(pluginData.value());
}
}
}
if(dfd != NULL) {
closedir(dfd);
}
return result;
}
std::optional<PluginData> PluginDataFactory::load(const std::string & filename, MEMHeapHandle heapHandle) {
// open the file:
DEBUG_FUNCTION_LINE();
std::ifstream file(filename, std::ios::binary);
DEBUG_FUNCTION_LINE();
if(!file.is_open()){
DEBUG_FUNCTION_LINE("Failed to open %s", filename.c_str());
return std::nullopt;
}
// Stop eating new lines in binary mode!!!
file.unsetf(std::ios::skipws);
DEBUG_FUNCTION_LINE();
// get its size:
std::streampos fileSize;
DEBUG_FUNCTION_LINE();
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
DEBUG_FUNCTION_LINE();
std::vector<uint8_t> vBuffer;
vBuffer.reserve(fileSize);
DEBUG_FUNCTION_LINE();
// read the data:
vBuffer.insert(vBuffer.begin(),
std::istream_iterator<uint8_t>(file),
std::istream_iterator<uint8_t>());
DEBUG_FUNCTION_LINE("Loaded file");
return load(vBuffer, heapHandle);
}
std::optional<PluginData> PluginDataFactory::load(std::vector<uint8_t>& buffer, MEMHeapHandle heapHandle) {
if(buffer.empty()){
return std::nullopt;
}
PluginData pluginData(buffer, heapHandle, eMemoryTypes::eMemTypeExpHeap);
return pluginData;
}

View File

@@ -0,0 +1,31 @@
/****************************************************************************
* Copyright (C) 2020 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 <optional>
#include <string>
#include <vector>
#include <coreinit/memexpheap.h>
#include "PluginData.h"
class PluginDataFactory {
public:
static std::vector<PluginData> loadDir(const std::string & path, MEMHeapHandle heapHandle);
static std::optional<PluginData> load(const std::string & path, MEMHeapHandle heapHandle);
static std::optional<PluginData> load(std::vector<uint8_t>& buffer, MEMHeapHandle heapHandle);
};

View File

@@ -0,0 +1,95 @@
/****************************************************************************
* 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 <map>
#include <optional>
#include <string>
#include <vector>
#include "PluginMetaInformation.h"
#include "RelocationData.h"
#include "HookData.h"
#include "FunctionData.h"
#include "SectionInfo.h"
class PluginInformation {
public:
PluginInformation(){
}
virtual ~PluginInformation() {
}
void addHookData(const HookData& hook_data) {
hook_data_list.push_back(hook_data);
}
const std::vector<HookData>& getHookDataList() const {
return hook_data_list;
}
void addFunctionData(const FunctionData &function_data) {
function_data_list.push_back(function_data);
}
const std::vector<FunctionData>& getFunctionDataList() const {
return function_data_list;
}
void addRelocationData(const RelocationData &relocation_data) {
relocation_data_list.push_back(relocation_data);
}
const std::vector<RelocationData>& getRelocationDataList() const {
return relocation_data_list;
}
void addSectionInfo(const SectionInfo& sectionInfo) {
section_info_list[sectionInfo.getName()] = sectionInfo;
}
const std::map<std::string, SectionInfo>& getSectionInfoList() const {
return section_info_list;
}
std::optional<SectionInfo> getSectionInfo(const std::string& sectionName) const {
if(getSectionInfoList().count(sectionName) > 0) {
return section_info_list.at(sectionName);
}
return std::nullopt;
}
void setTrampolinId(uint8_t trampolinId) {
this->trampolinId = trampolinId;
}
uint8_t getTrampolinId() const {
return trampolinId;
}
private:
std::vector<HookData> hook_data_list;
std::vector<FunctionData> function_data_list;
std::vector<RelocationData> relocation_data_list;
std::map<std::string, SectionInfo> section_info_list;
uint8_t trampolinId = 0;
friend class PluginInformationFactory;
friend class PluginInformationPersistence;
};

View File

@@ -0,0 +1,349 @@
/****************************************************************************
* 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 <coreinit/memexpheap.h>
#include <wups.h>
#include <whb/file.h>
#include "PluginData.h"
#include "PluginInformationFactory.h"
#include "HookData.h"
#include "SectionInfo.h"
#include "elfio/elfio.hpp"
#include "utils/utils.h"
#include "utils/ElfUtils.h"
#include "utils/StringTools.h"
using namespace ELFIO;
std::optional<PluginInformation> PluginInformationFactory::load(const PluginData & pluginData, MEMHeapHandle heapHandle, relocation_trampolin_entry_t * trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId) {
auto readerOpt = pluginData.getReader();
if(!readerOpt) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file");
return std::nullopt;
}
auto reader = readerOpt.value();
PluginInformation pluginInfo;
uint32_t sec_num = reader->sections.size();
uint8_t **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num);
uint32_t totalSize = 0;
uint32_t text_size = 0;
uint32_t data_size = 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();
if((address >= 0x02000000) && address < 0x10000000) {
text_size += sectionSize;
} else if((address >= 0x10000000) && address < 0xC0000000) {
data_size += sectionSize;
}
}
}
void * text_data = MEMAllocFromExpHeapEx(heapHandle, text_size, 0x1000);
if(text_data == NULL) {
DEBUG_FUNCTION_LINE("Failed to alloc memory for the .text section (%d bytes)\n", text_size);
return std::nullopt;
}
DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", text_size/1024);
void * data_data = MEMAllocFromExpHeapEx(heapHandle, data_size, 0x1000);
if(data_data == NULL) {
DEBUG_FUNCTION_LINE("Failed to alloc memory for the .data section (%d bytes)\n", data_size);
MEMFreeToExpHeap(heapHandle, text_data);
return std::nullopt;
}
DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", data_size/1024);
uint32_t entrypoint = (uint32_t)text_data + (uint32_t) reader->get_entry() - 0x02000000;
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();
uint32_t destination = address;
if((address >= 0x02000000) && address < 0x10000000) {
destination += (uint32_t) text_data;
destination -= 0x02000000;
destinations[psec->get_index()] = (uint8_t *) text_data;
} else if((address >= 0x10000000) && address < 0xC0000000) {
destination += (uint32_t) data_data;
destination -= 0x10000000;
destinations[psec->get_index()] = (uint8_t *) data_data;
} else if(address >= 0xC0000000) {
destination += (uint32_t) data_data;
destination -= 0xC0000000;
//destinations[psec->get_index()] = (uint8_t *) data_data;
//destinations[psec->get_index()] -= 0xC0000000;
} else {
DEBUG_FUNCTION_LINE("Unhandled case");
free(destinations);
MEMFreeToExpHeap(heapHandle, text_data);
MEMFreeToExpHeap(heapHandle, data_data);
return std::nullopt;
}
const char* p = psec->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);
}
pluginInfo.addSectionInfo(SectionInfo(psec->get_name(), 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 at %08X",i,psec->get_name().c_str(), destinations[psec->get_index()]);
if (!linkSection(pluginData, psec->get_index(), (uint32_t) destinations[psec->get_index()], (uint32_t) text_data, (uint32_t) data_data, trampolin_data, trampolin_data_length, trampolinId)) {
DEBUG_FUNCTION_LINE("elfLink failed");
free(destinations);
MEMFreeToExpHeap(heapHandle, text_data);
MEMFreeToExpHeap(heapHandle, data_data);
return std::nullopt;
}
}
}
std::vector<RelocationData> relocationData = getImportRelocationData(pluginData, destinations);
for (auto const& reloc : relocationData) {
pluginInfo.addRelocationData(reloc);
}
DCFlushRange((void*)text_data, text_size);
ICInvalidateRange((void*)text_data, text_size);
DCFlushRange((void*)data_data, data_size);
ICInvalidateRange((void*)data_data, data_size);
free(destinations);
pluginInfo.setTrampolinId(trampolinId);
DEBUG_FUNCTION_LINE("Saved entrypoint as %08X", entrypoint);
std::optional<SectionInfo> secInfo = pluginInfo.getSectionInfo(".wups.hooks");
if(secInfo && secInfo->getSize() > 0) {
size_t entries_count = secInfo->getSize() / sizeof(wups_loader_hook_t);
wups_loader_hook_t * entries = (wups_loader_hook_t *) secInfo->getAddress();
if(entries != NULL) {
for(size_t j=0; j<entries_count; j++) {
wups_loader_hook_t * hook = &entries[j];
DEBUG_FUNCTION_LINE("Saving hook of plugin Type: %08X, target: %08X"/*,pluginData.getPluginInformation()->getName().c_str()*/,hook->type,(void*) hook->target);
HookData hook_data((void *) hook->target,hook->type);
pluginInfo.addHookData(hook_data);
}
}
}
secInfo = pluginInfo.getSectionInfo(".wups.load");
if(secInfo && secInfo->getSize() > 0) {
size_t entries_count = secInfo->getSize() / sizeof(wups_loader_entry_t);
wups_loader_entry_t * entries = (wups_loader_entry_t *) secInfo->getAddress();
if(entries != NULL) {
for(size_t j=0; j<entries_count; j++) {
wups_loader_entry_t * cur_function = &entries[j];
DEBUG_FUNCTION_LINE("Saving function \"%s\" of plugin . PA:%08X VA:%08X Library: %08X, target: %08X, call_addr: %08X",cur_function->_function.name/*,pluginData.getPluginInformation()->getName().c_str()*/,cur_function->_function.physical_address,cur_function->_function.virtual_address, cur_function->_function.library,cur_function->_function.target, (void *) cur_function->_function.call_addr);
FunctionData function_data((void *) cur_function->_function.physical_address,(void *) cur_function->_function.virtual_address, cur_function->_function.name, cur_function->_function.library, (void *) cur_function->_function.target, (void *) cur_function->_function.call_addr);
pluginInfo.addFunctionData(function_data);
}
}
}
return pluginInfo;
}
std::vector<RelocationData> PluginInformationFactory::getImportRelocationData(const PluginData &pluginData, uint8_t ** destinations) {
auto readerOpt = pluginData.getReader();
std::vector<RelocationData> result;
if(!readerOpt) {
return result;
}
auto reader = readerOpt.value();
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;
}
std::string fimport = ".fimport_";
std::string dimport = ".dimport_";
bool isData = false;
std::string rplName = "";
std::string rawSectionName = infoMap[sym_section_index];
if(rawSectionName.size() < fimport.size()) {
DEBUG_FUNCTION_LINE("Section name was shorter than expected, skipping this relocation");
continue;
} 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());
isData = true;
} else {
DEBUG_FUNCTION_LINE("invalid section name\n");
continue;
}
ImportRPLInformation rplInfo(rplName, isData);
uint32_t section_index = psec->get_info();
result.push_back(RelocationData(type, offset - 0x02000000, addend, (void*)(destinations[section_index]), sym_name, rplInfo));
}
}
}
return result;
}
bool PluginInformationFactory::linkSection(const PluginData& pluginData, 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, uint8_t trampolinId) {
auto readerOpt = pluginData.getReader();
if(!readerOpt) {
return false;
}
auto reader = readerOpt.value();
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) {
//DEBUG_FUNCTION_LINE("Skip imports");
// Skip imports
continue;
} else if(adjusted_sym_value == 0x0) {
//
} else {
DEBUG_FUNCTION_LINE("Unhandled case %08X",adjusted_sym_value);
return false;
}
uint32_t adjusted_offset = (uint32_t) offset;
if((offset >= 0x02000000) && offset < 0x10000000) {
adjusted_offset -= 0x02000000;
} else if((adjusted_offset >= 0x10000000) && adjusted_offset < 0xC0000000) {
adjusted_offset -= 0x10000000;
} else if(adjusted_offset >= 0xC0000000) {
adjusted_offset -= 0xC0000000;
}
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(false) {
DEBUG_FUNCTION_LINE("sym_value %08X adjusted_sym_value %08X offset %08X adjusted_offset %08X", (uint32_t) sym_value, adjusted_sym_value, (uint32_t) offset, adjusted_offset);
}
if(!ElfUtils::elfLinkOne(type, adjusted_offset, addend, destination, adjusted_sym_value, trampolin_data, trampolin_data_length, RELOC_TYPE_FIXED, trampolinId)) {
DEBUG_FUNCTION_LINE("Link failed");
return false;
}
}
DEBUG_FUNCTION_LINE("done");
return true;
}
}
DEBUG_FUNCTION_LINE("Failed to find relocation section");
return true;
}

View File

@@ -0,0 +1,35 @@
/****************************************************************************
* 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 <optional>
#include <string>
#include <vector>
#include <map>
#include <coreinit/memheap.h>
#include "common/relocation_defines.h"
#include "PluginInformation.h"
#include "PluginContainer.h"
#include "elfio/elfio.hpp"
class PluginInformationFactory {
public:
static std::optional<PluginInformation> load(const PluginData & pluginData, MEMHeapHandle heaphandle, relocation_trampolin_entry_t * trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId);
static bool linkSection(const PluginData & pluginData, 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, uint8_t trampolinId);
static std::vector<RelocationData> getImportRelocationData(const PluginData & pluginData, uint8_t ** destinations);
};

View File

@@ -0,0 +1,96 @@
/****************************************************************************
* Copyright (C) 2019,2020 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>
class PluginMetaInformation {
public:
const std::string getName() const {
return name;
}
const std::string getAuthor() const {
return this->author;
}
const std::string getVersion() const {
return this->version;
}
const std::string getLicense() const {
return this->license;
}
const std::string getBuildTimestamp() const {
return this->buildtimestamp;
}
const std::string getDescription() const {
return this->description;
}
const size_t getSize() const {
return this->size;
}
private:
PluginMetaInformation() {
}
void setName(const std::string& name) {
this->name = name;
}
void setAuthor(const std::string& author) {
this->author = author;
}
void setVersion(const std::string& version) {
this->version = version;
}
void setLicense(const std::string& license) {
this->license = license;
}
void setBuildTimestamp(const std::string& buildtimestamp) {
this->buildtimestamp = buildtimestamp;
}
void setDescription(const std::string& description) {
this->description = description;
}
void setSize(size_t size) {
this->size = size;
}
std::string name;
std::string author;
std::string version;
std::string license;
std::string buildtimestamp;
std::string description;
size_t size;
friend class PluginMetaInformationFactory;
friend class PluginContainerPersistence;
friend class PluginContainer;
};

View File

@@ -0,0 +1,108 @@
/****************************************************************************
* Copyright (C) 2018-2020 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 <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <whb/file.h>
#include "utils/StringTools.h"
#include "PluginMetaInformationFactory.h"
#include "PluginMetaInformation.h"
#include "elfio/elfio.hpp"
using namespace ELFIO;
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const PluginData& pluginData) {
auto readerOpt = pluginData.getReader();
// Load ELF data
if (!readerOpt) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file");
return std::nullopt;
}
auto reader = readerOpt.value();
DEBUG_FUNCTION_LINE("Found elfio reader");
size_t pluginSize = 0;
PluginMetaInformation pluginInfo;
uint32_t sec_num = reader->sections.size();
DEBUG_FUNCTION_LINE("%d number of sections", sec_num);
for(uint32_t i = 0; i < sec_num; ++i ) {
section* psec = reader->sections[i];
// Calculate total size:
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();
if((address >= 0x02000000) && address < 0x10000000) {
pluginSize += sectionSize;
} else if((address >= 0x10000000) && address < 0xC0000000) {
pluginSize += sectionSize;
}
}
// Get meta information and check WUPS version:
if (psec->get_name().compare(".wups.meta") == 0) {
const void * sectionData = psec->get_data();
uint32_t sectionSize = psec->get_size();
char * curEntry = (char *) sectionData;
while((uint32_t) curEntry < (uint32_t) sectionData + sectionSize) {
if (*curEntry == '\0') {
curEntry++;
continue;
}
auto firstFound = std::string(curEntry).find_first_of("=");
if(firstFound != std::string::npos) {
curEntry[firstFound] = '\0';
std::string key(curEntry);
std::string value(curEntry + firstFound + 1);
if(key.compare("name") == 0) {
DEBUG_FUNCTION_LINE("Name = %s", value.c_str());
pluginInfo.setName(value);
} else if(key.compare("author") == 0) {
pluginInfo.setAuthor(value);
} else if(key.compare("version") == 0) {
pluginInfo.setVersion(value);
} else if(key.compare("license") == 0) {
pluginInfo.setLicense(value);
} else if(key.compare("buildtimestamp") == 0) {
pluginInfo.setBuildTimestamp(value);
} else if(key.compare("description") == 0) {
pluginInfo.setDescription(value);
} else if(key.compare("wups") == 0) {
if(value.compare("0.2") != 0) {
DEBUG_FUNCTION_LINE("Warning: Ignoring plugin - Unsupported WUPS version: %s.\n", value);
return std::nullopt;
}
}
}
curEntry += strlen(curEntry) + 1;
}
}
}
pluginInfo.setSize(pluginSize);
return pluginInfo;
}

View File

@@ -0,0 +1,29 @@
/****************************************************************************
* Copyright (C) 2019,2020 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 <optional>
#include <string>
#include <vector>
#include "PluginMetaInformation.h"
#include "PluginData.h"
class PluginMetaInformationFactory {
public:
static std::optional<PluginMetaInformation> loadPlugin(const PluginData& pluginData);
};

View File

@@ -0,0 +1,78 @@
/****************************************************************************
* 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(const char type, size_t offset, int32_t addend, void *destination, std::string name, ImportRPLInformation rplInfo):
type(type),
offset(offset),
addend(addend),
destination(destination),
name(name),
rplInfo(rplInfo) {
}
RelocationData(const RelocationData &o2):
type(o2.type),
offset(o2.offset),
addend(o2.addend),
destination(o2.destination),
name(o2.name),
rplInfo(o2.rplInfo) {
}
virtual ~RelocationData() {
}
const char getType() const{
return type;
}
const size_t getOffset() const{
return offset;
}
const int32_t getAddend() const{
return addend;
}
const void * getDestination() const{
return destination;
}
const std::string& getName() const{
return name;
}
const ImportRPLInformation& getImportRPLInformation() const{
return rplInfo;
}
private:
char type;
size_t offset;
int32_t addend;
void *destination;
std::string name;
ImportRPLInformation rplInfo;
};

View File

@@ -0,0 +1,61 @@
/****************************************************************************
* 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>
class SectionInfo {
public:
SectionInfo(std::string name, uint32_t address, uint32_t sectionSize):
name(name),
address(address),
sectionSize(sectionSize) {
}
SectionInfo(){
}
SectionInfo(const SectionInfo &o2):
name(o2.name),
address(o2.address),
sectionSize(o2.sectionSize) {
}
virtual ~SectionInfo() {
}
const std::string& getName() const {
return name;
}
const uint32_t getAddress() const {
return address;
}
const uint32_t getSize() const {
return sectionSize;
}
private:
std::string name;
uint32_t address;
uint32_t sectionSize;
};