Formatting, fix spelling, clean up

This commit is contained in:
Maschell 2022-01-27 12:47:33 +01:00
parent 84afdb3179
commit 614f58ea8e
25 changed files with 13010 additions and 14975 deletions

View File

@ -1,6 +1,6 @@
FROM wiiuenv/devkitppc:20211229 FROM wiiuenv/devkitppc:20211229
COPY --from=wiiuenv/wiiumodulesystem:20220123 /artifacts $DEVKITPRO COPY --from=wiiuenv/wiiumodulesystem:20220127 /artifacts $DEVKITPRO
COPY --from=wiiuenv/wiiupluginsystem:20220123 /artifacts $DEVKITPRO COPY --from=wiiuenv/wiiupluginsystem:20220123 /artifacts $DEVKITPRO
COPY --from=wiiuenv/libfunctionpatcher:20210924 /artifacts $DEVKITPRO COPY --from=wiiuenv/libfunctionpatcher:20210924 /artifacts $DEVKITPRO
COPY --from=wiiuenv/libmappedmemory:20210924 /artifacts $DEVKITPRO COPY --from=wiiuenv/libmappedmemory:20210924 /artifacts $DEVKITPRO

View File

@ -14,7 +14,7 @@
#include "hooks.h" #include "hooks.h"
#include "globals.h" #include "globals.h"
bool PluginManagement::doRelocation(const std::vector<std::shared_ptr<RelocationData>> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length, uint32_t trampolinID) { bool PluginManagement::doRelocation(const std::vector<std::shared_ptr<RelocationData>> &relocData, relocation_trampoline_entry_t *tramp_data, uint32_t tramp_length, uint32_t trampolineID) {
std::map<std::string, OSDynLoad_Module> moduleHandleCache; std::map<std::string, OSDynLoad_Module> moduleHandleCache;
for (auto const &cur: relocData) { for (auto const &cur: relocData) {
uint32_t functionAddress = 0; uint32_t functionAddress = 0;
@ -53,23 +53,23 @@ bool PluginManagement::doRelocation(const std::vector<std::shared_ptr<Relocation
} else { } else {
//DEBUG_FUNCTION_LINE("Found export for %s %s", rplName.c_str(), functionName.c_str()); //DEBUG_FUNCTION_LINE("Found export for %s %s", rplName.c_str(), functionName.c_str());
} }
if (!ElfUtils::elfLinkOne(cur->getType(), cur->getOffset(), cur->getAddend(), (uint32_t) cur->getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT, trampolinID)) { if (!ElfUtils::elfLinkOne(cur->getType(), cur->getOffset(), cur->getAddend(), (uint32_t) cur->getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT, trampolineID)) {
DEBUG_FUNCTION_LINE("Relocation failed"); DEBUG_FUNCTION_LINE("Relocation failed");
return false; return false;
} }
} }
DCFlushRange(tramp_data, tramp_length * sizeof(relocation_trampolin_entry_t)); DCFlushRange(tramp_data, tramp_length * sizeof(relocation_trampoline_entry_t));
ICInvalidateRange(tramp_data, tramp_length * sizeof(relocation_trampolin_entry_t)); ICInvalidateRange(tramp_data, tramp_length * sizeof(relocation_trampoline_entry_t));
return true; return true;
} }
void PluginManagement::doRelocations(const std::vector<std::shared_ptr<PluginContainer>> &plugins, relocation_trampolin_entry_t *trampData, uint32_t tramp_size) { void PluginManagement::doRelocations(const std::vector<std::shared_ptr<PluginContainer>> &plugins, relocation_trampoline_entry_t *trampData, uint32_t tramp_size) {
for (auto &pluginContainer: plugins) { for (auto &pluginContainer: plugins) {
DEBUG_FUNCTION_LINE_VERBOSE("Doing relocations for plugin: %s", pluginContainer->getMetaInformation()->getName().c_str()); DEBUG_FUNCTION_LINE_VERBOSE("Doing relocations for plugin: %s", pluginContainer->getMetaInformation()->getName().c_str());
if (!PluginManagement::doRelocation(pluginContainer->getPluginInformation()->getRelocationDataList(), trampData, tramp_size, pluginContainer->getPluginInformation()->getTrampolinId())) { if (!PluginManagement::doRelocation(pluginContainer->getPluginInformation()->getRelocationDataList(), trampData, tramp_size, pluginContainer->getPluginInformation()->getTrampolineId())) {
DEBUG_FUNCTION_LINE("Relocation failed"); DEBUG_FUNCTION_LINE("Relocation failed");
} }
} }
@ -145,7 +145,7 @@ void PluginManagement::unloadPlugins(plugin_information_t *pluginInformation, ME
for (uint32_t i = 0; i < gTrampolineDataSize; i++) { for (uint32_t i = 0; i < gTrampolineDataSize; i++) {
auto trampoline = &(gTrampolineData[i]); auto trampoline = &(gTrampolineData[i]);
if (trampoline->id == plugin->info.trampolinId) { if (trampoline->id == plugin->info.trampolineId) {
trampoline->id = 0; trampoline->id = 0;
trampoline->status = RELOC_TRAMP_FREE; trampoline->status = RELOC_TRAMP_FREE;
} }
@ -189,7 +189,7 @@ void PluginManagement::PatchFunctionsAndCallHooks(plugin_information_t *pluginIn
} }
std::vector<std::shared_ptr<PluginContainer>> std::vector<std::shared_ptr<PluginContainer>>
PluginManagement::loadPlugins(const std::vector<std::shared_ptr<PluginData>> &pluginList, MEMHeapHandle heapHandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) { PluginManagement::loadPlugins(const std::vector<std::shared_ptr<PluginData>> &pluginList, MEMHeapHandle heapHandle, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length) {
std::vector<std::shared_ptr<PluginContainer>> plugins; std::vector<std::shared_ptr<PluginContainer>> plugins;
for (auto &pluginData: pluginList) { for (auto &pluginData: pluginList) {
@ -206,7 +206,7 @@ PluginManagement::loadPlugins(const std::vector<std::shared_ptr<PluginData>> &pl
} }
uint32_t trampolineID = 0; uint32_t trampolineID = 0;
for (auto &pluginContainer: plugins) { for (auto &pluginContainer: plugins) {
auto info = PluginInformationFactory::load(pluginContainer->getPluginData(), heapHandle, trampolin_data, trampolin_data_length, trampolineID++); auto info = PluginInformationFactory::load(pluginContainer->getPluginData(), heapHandle, trampoline_data, trampoline_data_length, trampolineID++);
if (!info) { if (!info) {
DEBUG_FUNCTION_LINE("Failed to load Plugin %s", pluginContainer->getMetaInformation()->getName().c_str()); DEBUG_FUNCTION_LINE("Failed to load Plugin %s", pluginContainer->getMetaInformation()->getName().c_str());
continue; continue;

View File

@ -6,7 +6,7 @@
class PluginManagement { class PluginManagement {
public: public:
static void doRelocations(const std::vector<std::shared_ptr<PluginContainer>> &plugins, relocation_trampolin_entry_t *trampData, uint32_t tramp_size); static void doRelocations(const std::vector<std::shared_ptr<PluginContainer>> &plugins, relocation_trampoline_entry_t *trampData, uint32_t tramp_size);
static void memsetBSS(const std::vector<std::shared_ptr<PluginContainer>> &plugins); static void memsetBSS(const std::vector<std::shared_ptr<PluginContainer>> &plugins);
@ -14,11 +14,12 @@ public:
static void PatchFunctionsAndCallHooks(plugin_information_t *gPluginInformation); static void PatchFunctionsAndCallHooks(plugin_information_t *gPluginInformation);
static bool doRelocation(const std::vector<std::shared_ptr<RelocationData>> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length, uint32_t trampolinID); static bool doRelocation(const std::vector<std::shared_ptr<RelocationData>> &relocData, relocation_trampoline_entry_t *tramp_data, uint32_t tramp_length, uint32_t trampolineID);
static void unloadPlugins(plugin_information_t *pluginInformation, MEMHeapHandle pluginHeap, BOOL freePluginData); static void unloadPlugins(plugin_information_t *pluginInformation, MEMHeapHandle pluginHeap, BOOL freePluginData);
static std::vector<std::shared_ptr<PluginContainer>> loadPlugins(const std::vector<std::shared_ptr<PluginData>> &pluginList, MEMHeapHandle pHeader, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length); static std::vector<std::shared_ptr<PluginContainer>>
loadPlugins(const std::vector<std::shared_ptr<PluginData>> &pluginList, MEMHeapHandle pHeader, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length);
static void RestorePatches(plugin_information_t *pluginInformation, BOOL pluginOnly); static void RestorePatches(plugin_information_t *pluginInformation, BOOL pluginOnly);
}; };

View File

@ -78,7 +78,7 @@ struct plugin_info_t {
function_replacement_data_t functions[MAXIMUM_FUNCTION_PER_PLUGIN]{}; // Replacement information for each function. function_replacement_data_t functions[MAXIMUM_FUNCTION_PER_PLUGIN]{}; // Replacement information for each function.
uint32_t number_used_hooks{}; // Number of used hooks. Maximum is MAXIMUM_HOOKS_PER_PLUGIN uint32_t number_used_hooks{}; // Number of used hooks. Maximum is MAXIMUM_HOOKS_PER_PLUGIN
replacement_data_hook_t hooks[MAXIMUM_HOOKS_PER_PLUGIN]; // Replacement information for each function. replacement_data_hook_t hooks[MAXIMUM_HOOKS_PER_PLUGIN]; // Replacement information for each function.
uint8_t trampolinId{}; uint8_t trampolineId{};
plugin_function_symbol_data_t * function_symbol_data = nullptr; plugin_function_symbol_data_t * function_symbol_data = nullptr;
uint32_t number_function_symbol_data = 0; uint32_t number_function_symbol_data = 0;
void * allocatedTextMemoryAddress = nullptr; void * allocatedTextMemoryAddress = nullptr;

View File

@ -5,7 +5,7 @@ MEMHeapHandle gPluginInformationHeap __attribute__((section(".data"))) = nullptr
plugin_information_t *gPluginInformation __attribute__((section(".data"))) = nullptr; plugin_information_t *gPluginInformation __attribute__((section(".data"))) = nullptr;
plugin_information_on_reload_t gLinkOnReload __attribute__((section(".data"))); plugin_information_on_reload_t gLinkOnReload __attribute__((section(".data")));
module_information_t *gModuleData __attribute__((section(".data"))) = nullptr; module_information_t *gModuleData __attribute__((section(".data"))) = nullptr;
relocation_trampolin_entry_t *gTrampolineData __attribute__((section(".data"))) = nullptr; relocation_trampoline_entry_t *gTrampolineData __attribute__((section(".data"))) = nullptr;
uint32_t gPluginDataHeapSize = 0; uint32_t gPluginDataHeapSize = 0;
uint32_t gPluginInformationHeapSize = 0; uint32_t gPluginInformationHeapSize = 0;

View File

@ -13,7 +13,7 @@ extern uint32_t gPluginDataHeapSize;
extern uint32_t gPluginInformationHeapSize; extern uint32_t gPluginInformationHeapSize;
extern plugin_information_on_reload_t gLinkOnReload; extern plugin_information_on_reload_t gLinkOnReload;
extern module_information_t *gModuleData; extern module_information_t *gModuleData;
extern relocation_trampolin_entry_t *gTrampolineData; extern relocation_trampoline_entry_t *gTrampolineData;
extern uint32_t gTrampolineDataSize; extern uint32_t gTrampolineDataSize;
extern StoredBuffer storedTVBuffer; extern StoredBuffer storedTVBuffer;
extern StoredBuffer storedDRCBuffer; extern StoredBuffer storedDRCBuffer;

View File

@ -106,14 +106,14 @@ WUMS_APPLICATION_STARTS() {
memset((void *) gPluginInformation, 0, sizeof(plugin_information_t)); memset((void *) gPluginInformation, 0, sizeof(plugin_information_t));
} }
if (gTrampolineData == nullptr) { if (gTrampolineData == nullptr) {
DEBUG_FUNCTION_LINE_VERBOSE("Allocate gTrampolineData on heap %08X (size: %d bytes)", gPluginDataHeap, sizeof(relocation_trampolin_entry_t) * NUMBER_OF_TRAMPS); DEBUG_FUNCTION_LINE_VERBOSE("Allocate gTrampolineData on heap %08X (size: %d bytes)", gPluginDataHeap, sizeof(relocation_trampoline_entry_t) * NUMBER_OF_TRAMPS);
gTrampolineData = (relocation_trampolin_entry_t *) MEMAllocFromExpHeapEx(gPluginDataHeap, sizeof(relocation_trampolin_entry_t) * NUMBER_OF_TRAMPS, 4); gTrampolineData = (relocation_trampoline_entry_t *) MEMAllocFromExpHeapEx(gPluginDataHeap, sizeof(relocation_trampoline_entry_t) * NUMBER_OF_TRAMPS, 4);
if (gTrampolineData == nullptr) { if (gTrampolineData == nullptr) {
OSFatal("PluginBackend: Failed to allocate gTrampolineData"); OSFatal("PluginBackend: Failed to allocate gTrampolineData");
return; return;
} }
gTrampolineDataSize = NUMBER_OF_TRAMPS; gTrampolineDataSize = NUMBER_OF_TRAMPS;
memset((void *) gTrampolineData, 0, sizeof(relocation_trampolin_entry_t) * NUMBER_OF_TRAMPS); memset((void *) gTrampolineData, 0, sizeof(relocation_trampoline_entry_t) * NUMBER_OF_TRAMPS);
} }
DEBUG_FUNCTION_LINE("Available memory for storing plugins: %d kb", MEMGetAllocatableSizeForExpHeapEx(gPluginDataHeap, 4) / 1024); DEBUG_FUNCTION_LINE("Available memory for storing plugins: %d kb", MEMGetAllocatableSizeForExpHeapEx(gPluginDataHeap, 4) / 1024);
std::vector<std::shared_ptr<PluginData>> pluginList = PluginDataFactory::loadDir("fs:/vol/external01/wiiu/plugins/", gPluginDataHeap); std::vector<std::shared_ptr<PluginData>> pluginList = PluginDataFactory::loadDir("fs:/vol/external01/wiiu/plugins/", gPluginDataHeap);
@ -190,7 +190,7 @@ WUMS_APPLICATION_STARTS() {
if (gPluginDataHeap != nullptr) { if (gPluginDataHeap != nullptr) {
auto plugins = PluginContainerPersistence::loadPlugins(gPluginInformation); auto plugins = PluginContainerPersistence::loadPlugins(gPluginInformation);
PluginManagement::doRelocations(plugins, gTrampolineData, DYN_LINK_TRAMPOLIN_LIST_LENGTH); PluginManagement::doRelocations(plugins, gTrampolineData, DYN_LINK_TRAMPOLINE_LIST_LENGTH);
// PluginManagement::memsetBSS(plugins); // PluginManagement::memsetBSS(plugins);
DCFlushRange((void *) gPluginDataHeap, gPluginDataHeapSize); DCFlushRange((void *) gPluginDataHeap, gPluginDataHeapSize);

View File

@ -210,6 +210,7 @@ DECL_FUNCTION(uint32_t, KiGetAppSymbolName, uint32_t addr, char *buffer, int32_t
return real_KiGetAppSymbolName(addr, buffer, bufSize); return real_KiGetAppSymbolName(addr, buffer, bufSize);
} }
#pragma GCC pop_options #pragma GCC pop_options
function_replacement_data_t method_hooks_hooks_static[] __attribute__((section(".data"))) = { function_replacement_data_t method_hooks_hooks_static[] __attribute__((section(".data"))) = {

View File

@ -47,7 +47,8 @@ public:
**/ **/
static dyn_linking_import_t *getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData); static dyn_linking_import_t *getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData);
static bool addReloationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, const std::shared_ptr<RelocationData> &relocationData); static bool addReloationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length,
const std::shared_ptr<RelocationData> &relocationData);
static bool 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, 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,

View File

@ -18,13 +18,14 @@
#pragma once #pragma once
#include <string> #include <string>
#include <utility>
#include "../utils/logger.h" #include "../utils/logger.h"
class ImportRPLInformation { class ImportRPLInformation {
public: public:
explicit ImportRPLInformation(std::string name, bool isData = false) { explicit ImportRPLInformation(std::string name, bool isData = false) {
this->name = name; this->name = std::move(name);
this->_isData = isData; this->_isData = isData;
} }

View File

@ -157,7 +157,7 @@ bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformat
return false; return false;
} }
} }
plugin_data->info.trampolinId = pluginInfo->getTrampolinId(); plugin_data->info.trampolineId = pluginInfo->getTrampolineId();
plugin_data->info.allocatedTextMemoryAddress = pluginInfo->allocatedTextMemoryAddress; plugin_data->info.allocatedTextMemoryAddress = pluginInfo->allocatedTextMemoryAddress;
plugin_data->info.allocatedDataMemoryAddress = pluginInfo->allocatedDataMemoryAddress; plugin_data->info.allocatedDataMemoryAddress = pluginInfo->allocatedDataMemoryAddress;
@ -253,7 +253,7 @@ std::vector<std::shared_ptr<PluginContainer>> PluginContainerPersistence::loadPl
auto curPluginInformation = std::make_shared<PluginInformation>(); auto curPluginInformation = std::make_shared<PluginInformation>();
curPluginInformation->setTrampolinId(plugin_data->info.trampolinId); curPluginInformation->setTrampolineId(plugin_data->info.trampolineId);
curPluginInformation->allocatedTextMemoryAddress = plugin_data->info.allocatedTextMemoryAddress; curPluginInformation->allocatedTextMemoryAddress = plugin_data->info.allocatedTextMemoryAddress;
curPluginInformation->allocatedDataMemoryAddress = plugin_data->info.allocatedDataMemoryAddress; curPluginInformation->allocatedDataMemoryAddress = plugin_data->info.allocatedDataMemoryAddress;

View File

@ -43,7 +43,7 @@ public:
void *buffer = nullptr; void *buffer = nullptr;
MEMHeapHandle heapHandle{}; MEMHeapHandle heapHandle{};
eMemoryTypes memoryType; eMemoryTypes memoryType{};
size_t length = 0; size_t length = 0;
private: private:

View File

@ -26,7 +26,7 @@
std::vector<std::shared_ptr<PluginData>> PluginDataFactory::loadDir(const std::string &path, MEMHeapHandle heapHandle) { std::vector<std::shared_ptr<PluginData>> PluginDataFactory::loadDir(const std::string &path, MEMHeapHandle heapHandle) {
std::vector<std::shared_ptr<PluginData>> result; std::vector<std::shared_ptr<PluginData>> result;
struct dirent *dp; struct dirent *dp;
DIR *dfd = nullptr; DIR *dfd;
if (path.empty()) { if (path.empty()) {
DEBUG_FUNCTION_LINE("Path was empty"); DEBUG_FUNCTION_LINE("Path was empty");

View File

@ -6,6 +6,7 @@ class PluginDataPersistence {
public: public:
static bool save(plugin_data_t *pluginDataStruct, const std::shared_ptr<PluginData> &plugin); static bool save(plugin_data_t *pluginDataStruct, const std::shared_ptr<PluginData> &plugin);
static bool save(plugin_data_t *pluginDataStruct, PluginData *plugin); static bool save(plugin_data_t *pluginDataStruct, PluginData *plugin);
static std::shared_ptr<PluginData> load(plugin_data_t *pluginDataStruct); static std::shared_ptr<PluginData> load(plugin_data_t *pluginDataStruct);

View File

@ -14,7 +14,7 @@ PluginInformation::PluginInformation(const PluginInformation &other) {
symbol_data_list.insert(i); symbol_data_list.insert(i);
} }
section_info_list = other.section_info_list; section_info_list = other.section_info_list;
trampolinId = other.trampolinId; trampolineId = other.trampolineId;
allocatedTextMemoryAddress = other.allocatedTextMemoryAddress; allocatedTextMemoryAddress = other.allocatedTextMemoryAddress;
allocatedDataMemoryAddress = other.allocatedDataMemoryAddress; allocatedDataMemoryAddress = other.allocatedDataMemoryAddress;
} }

View File

@ -32,8 +32,7 @@
struct FunctionSymbolDataComparator { struct FunctionSymbolDataComparator {
bool operator()(const std::shared_ptr<FunctionSymbolData> &lhs, bool operator()(const std::shared_ptr<FunctionSymbolData> &lhs,
const std::shared_ptr<FunctionSymbolData>& rhs) const const std::shared_ptr<FunctionSymbolData> &rhs) const {
{
return (uint32_t) lhs->getAddress() < (uint32_t) rhs->getAddress(); return (uint32_t) lhs->getAddress() < (uint32_t) rhs->getAddress();
} }
}; };
@ -94,12 +93,12 @@ public:
return std::nullopt; return std::nullopt;
} }
void setTrampolinId(uint8_t _trampolinId) { void setTrampolineId(uint8_t _trampolineId) {
this->trampolinId = _trampolinId; this->trampolineId = _trampolineId;
} }
[[nodiscard]] uint8_t getTrampolinId() const { [[nodiscard]] uint8_t getTrampolineId() const {
return trampolinId; return trampolineId;
} }
private: private:
@ -109,7 +108,7 @@ private:
std::set<std::shared_ptr<FunctionSymbolData>, FunctionSymbolDataComparator> symbol_data_list; std::set<std::shared_ptr<FunctionSymbolData>, FunctionSymbolDataComparator> symbol_data_list;
std::map<std::string, std::shared_ptr<SectionInfo>> section_info_list; std::map<std::string, std::shared_ptr<SectionInfo>> section_info_list;
uint8_t trampolinId = 0; uint8_t trampolineId = 0;
void *allocatedTextMemoryAddress = nullptr; void *allocatedTextMemoryAddress = nullptr;
void *allocatedDataMemoryAddress = nullptr; void *allocatedDataMemoryAddress = nullptr;

View File

@ -30,8 +30,8 @@
using namespace ELFIO; using namespace ELFIO;
std::optional<std::shared_ptr<PluginInformation>> std::optional<std::shared_ptr<PluginInformation>>
PluginInformationFactory::load(const std::shared_ptr<PluginData> &pluginData, MEMHeapHandle heapHandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, PluginInformationFactory::load(const std::shared_ptr<PluginData> &pluginData, MEMHeapHandle heapHandle, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length,
uint8_t trampolinId) { uint8_t trampolineId) {
if (pluginData->buffer == nullptr) { if (pluginData->buffer == nullptr) {
DEBUG_FUNCTION_LINE("Buffer was nullptr"); DEBUG_FUNCTION_LINE("Buffer was nullptr");
return std::nullopt; return std::nullopt;
@ -145,7 +145,8 @@ PluginInformationFactory::load(const std::shared_ptr<PluginData> &pluginData, ME
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) { if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
DEBUG_FUNCTION_LINE_VERBOSE("Linking (%d)... %s at %08X", i, psec->get_name().c_str(), destinations[psec->get_index()]); DEBUG_FUNCTION_LINE_VERBOSE("Linking (%d)... %s at %08X", i, psec->get_name().c_str(), destinations[psec->get_index()]);
if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], (uint32_t) text_data, (uint32_t) data_data, trampolin_data, trampolin_data_length, trampolinId)) { if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], (uint32_t) text_data, (uint32_t) data_data, trampoline_data, trampoline_data_length,
trampolineId)) {
DEBUG_FUNCTION_LINE("elfLink failed"); DEBUG_FUNCTION_LINE("elfLink failed");
free(destinations); free(destinations);
MEMFreeToExpHeap(heapHandle, text_data); MEMFreeToExpHeap(heapHandle, text_data);
@ -167,7 +168,7 @@ PluginInformationFactory::load(const std::shared_ptr<PluginData> &pluginData, ME
free(destinations); free(destinations);
pluginInfo->setTrampolinId(trampolinId); pluginInfo->setTrampolineId(trampolineId);
auto secInfo = pluginInfo->getSectionInfo(".wups.hooks"); auto secInfo = pluginInfo->getSectionInfo(".wups.hooks");
if (secInfo && secInfo.value()->getSize() > 0) { if (secInfo && secInfo.value()->getSize() > 0) {
@ -314,9 +315,9 @@ std::vector<std::shared_ptr<RelocationData>> PluginInformationFactory::getImport
return result; return result;
} }
bool PluginInformationFactory::linkSection(const elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, bool PluginInformationFactory::linkSection(const elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampoline_entry_t *trampoline_data,
uint32_t trampolin_data_length, uint32_t trampoline_data_length,
uint8_t trampolinId) { uint8_t trampolineId) {
uint32_t sec_num = reader.sections.size(); uint32_t sec_num = reader.sections.size();
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
@ -372,7 +373,7 @@ bool PluginInformationFactory::linkSection(const elfio &reader, uint32_t section
} }
// DEBUG_FUNCTION_LINE_VERBOSE("sym_value %08X adjusted_sym_value %08X offset %08X adjusted_offset %08X", (uint32_t) sym_value, adjusted_sym_value, (uint32_t) offset, adjusted_offset); // DEBUG_FUNCTION_LINE_VERBOSE("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)) { if (!ElfUtils::elfLinkOne(type, adjusted_offset, addend, destination, adjusted_sym_value, trampoline_data, trampoline_data_length, RELOC_TYPE_FIXED, trampolineId)) {
DEBUG_FUNCTION_LINE("Link failed"); DEBUG_FUNCTION_LINE("Link failed");
return false; return false;
} }

View File

@ -30,11 +30,12 @@
class PluginInformationFactory { class PluginInformationFactory {
public: public:
static std::optional<std::shared_ptr<PluginInformation>> static std::optional<std::shared_ptr<PluginInformation>>
load(const std::shared_ptr<PluginData> &pluginData, MEMHeapHandle heaphandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId); load(const std::shared_ptr<PluginData> &pluginData, MEMHeapHandle heaphandle, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length, uint8_t trampolineId);
static bool static bool
linkSection(const 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, linkSection(const elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampoline_entry_t *trampoline_data,
uint8_t trampolinId); uint32_t trampoline_data_length,
uint8_t trampolineId);
static std::vector<std::shared_ptr<RelocationData>> getImportRelocationData(const elfio &reader, uint8_t **destinations); static std::vector<std::shared_ptr<RelocationData>> getImportRelocationData(const elfio &reader, uint8_t **destinations);
}; };

View File

@ -5,8 +5,8 @@
#include "ElfUtils.h" #include "ElfUtils.h"
// See https://github.com/decaf-emu/decaf-emu/blob/43366a34e7b55ab9d19b2444aeb0ccd46ac77dea/src/libdecaf/src/cafe/loader/cafe_loader_reloc.cpp#L144 // See https://github.com/decaf-emu/decaf-emu/blob/43366a34e7b55ab9d19b2444aeb0ccd46ac77dea/src/libdecaf/src/cafe/loader/cafe_loader_reloc.cpp#L144
bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length,
RelocationType reloc_type, uint8_t trampolinId) { RelocationType reloc_type, uint8_t trampolineId) {
if (type == R_PPC_NONE) { if (type == R_PPC_NONE) {
return true; return true;
} }
@ -75,44 +75,44 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
// } // }
auto distance = static_cast<int32_t>(value) - static_cast<int32_t>(target); auto distance = static_cast<int32_t>(value) - static_cast<int32_t>(target);
if (distance > 0x1FFFFFC || distance < -0x1FFFFFC) { if (distance > 0x1FFFFFC || distance < -0x1FFFFFC) {
if (trampolin_data == nullptr) { if (trampoline_data == nullptr) {
DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin isn't provided"); DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampoline isn't provided");
DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, distance); DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, distance);
return false; return false;
} else { } else {
relocation_trampolin_entry_t *freeSlot = nullptr; relocation_trampoline_entry_t *freeSlot = nullptr;
for (uint32_t i = 0; i < trampolin_data_length; i++) { for (uint32_t i = 0; i < trampoline_data_length; i++) {
// We want to override "old" relocations of imports // We want to override "old" relocations of imports
// Pending relocations have the status RELOC_TRAMP_IMPORT_IN_PROGRESS. // Pending relocations have the status RELOC_TRAMP_IMPORT_IN_PROGRESS.
// When all relocations are done successfully, they will be turned into RELOC_TRAMP_IMPORT_DONE // When all relocations are done successfully, they will be turned into RELOC_TRAMP_IMPORT_DONE
// so they can be overridden/updated/reused on the next application launch. // so they can be overridden/updated/reused on the next application launch.
// //
// Relocations that won't change will have the status RELOC_TRAMP_FIXED and are set to free when the module is unloaded. // Relocations that won't change will have the status RELOC_TRAMP_FIXED and are set to free when the module is unloaded.
if (trampolin_data[i].status == RELOC_TRAMP_FREE || if (trampoline_data[i].status == RELOC_TRAMP_FREE ||
trampolin_data[i].status == RELOC_TRAMP_IMPORT_DONE) { trampoline_data[i].status == RELOC_TRAMP_IMPORT_DONE) {
freeSlot = &(trampolin_data[i]); freeSlot = &(trampoline_data[i]);
break; break;
} }
} }
if (freeSlot == nullptr) { if (freeSlot == nullptr) {
DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin data list is full"); DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampoline data list is full");
DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, target - (uint32_t) &(freeSlot->trampolin[0])); DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, target - (uint32_t) &(freeSlot->trampoline[0]));
return false; return false;
} }
if (target - (uint32_t) &(freeSlot->trampolin[0]) > 0x1FFFFFC) { if (target - (uint32_t) &(freeSlot->trampoline[0]) > 0x1FFFFFC) {
DEBUG_FUNCTION_LINE("**Cannot link 24-bit jump (too far to tramp buffer)."); DEBUG_FUNCTION_LINE("**Cannot link 24-bit jump (too far to tramp buffer).");
DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, (target - (uint32_t) &(freeSlot->trampolin[0]))); DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, (target - (uint32_t) &(freeSlot->trampoline[0])));
return false; return false;
} }
freeSlot->trampolin[0] = 0x3D600000 | ((((uint32_t) value) >> 16) & 0x0000FFFF); // lis r11, real_addr@h freeSlot->trampoline[0] = 0x3D600000 | ((((uint32_t) value) >> 16) & 0x0000FFFF); // lis r11, real_addr@h
freeSlot->trampolin[1] = 0x616B0000 | (((uint32_t) value) & 0x0000ffff); // ori r11, r11, real_addr@l freeSlot->trampoline[1] = 0x616B0000 | (((uint32_t) value) & 0x0000ffff); // ori r11, r11, real_addr@l
freeSlot->trampolin[2] = 0x7D6903A6; // mtctr r11 freeSlot->trampoline[2] = 0x7D6903A6; // mtctr r11
freeSlot->trampolin[3] = 0x4E800420; // bctr freeSlot->trampoline[3] = 0x4E800420; // bctr
DCFlushRange((void *) freeSlot->trampolin, sizeof(freeSlot->trampolin)); DCFlushRange((void *) freeSlot->trampoline, sizeof(freeSlot->trampoline));
ICInvalidateRange((unsigned char *) freeSlot->trampolin, sizeof(freeSlot->trampolin)); ICInvalidateRange((unsigned char *) freeSlot->trampoline, sizeof(freeSlot->trampoline));
freeSlot->id = trampolinId; freeSlot->id = trampolineId;
DCFlushRange((void *) &freeSlot->id, sizeof(freeSlot->id)); DCFlushRange((void *) &freeSlot->id, sizeof(freeSlot->id));
ICInvalidateRange((unsigned char *) &freeSlot->id, sizeof(freeSlot->id)); ICInvalidateRange((unsigned char *) &freeSlot->id, sizeof(freeSlot->id));
@ -122,7 +122,7 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
// Relocations for the imports may be overridden // Relocations for the imports may be overridden
freeSlot->status = RELOC_TRAMP_IMPORT_DONE; freeSlot->status = RELOC_TRAMP_IMPORT_DONE;
} }
uint32_t symbolValue = (uint32_t) &(freeSlot->trampolin[0]); auto symbolValue = (uint32_t) &(freeSlot->trampoline[0]);
value = symbolValue + addend; value = symbolValue + addend;
distance = static_cast<int32_t>(value) - static_cast<int32_t>(target); distance = static_cast<int32_t>(value) - static_cast<int32_t>(target);
} }

View File

@ -46,6 +46,6 @@ uint32_t load_loader_elf(unsigned char *baseAddress, char *elf_data, uint32_t fi
class ElfUtils { class ElfUtils {
public: public:
static bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, static bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length,
RelocationType reloc_type, uint8_t trampolinId); RelocationType reloc_type, uint8_t trampolineId);
}; };

File diff suppressed because it is too large Load Diff