Set a custom OSDynLoad Allocator when processing relocations

This commit is contained in:
Maschell 2022-10-03 21:51:12 +02:00
parent 0a254e59f4
commit 2882635f6f
3 changed files with 41 additions and 3 deletions

View File

@ -52,6 +52,7 @@ bool PluginManagement::doRelocation(const std::vector<std::unique_ptr<Relocation
} else {
//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, trampolineID)) {
DEBUG_FUNCTION_LINE_ERR("elfLinkOne failed");
return false;
@ -73,19 +74,28 @@ bool PluginManagement::doRelocation(const std::vector<std::unique_ptr<Relocation
return true;
}
bool PluginManagement::doRelocations(const std::vector<std::unique_ptr<PluginContainer>> &plugins, relocation_trampoline_entry_t *trampData, uint32_t tramp_size) {
for (uint32_t i = 0; i < tramp_size; i++) {
if (trampData[i].status == RELOC_TRAMP_IMPORT_DONE) {
trampData[i].status = RELOC_TRAMP_FREE;
}
}
OSDynLoadAllocFn prevDynLoadAlloc = nullptr;
OSDynLoadFreeFn prevDynLoadFree = nullptr;
OSDynLoad_GetAllocator(&prevDynLoadAlloc, &prevDynLoadFree);
OSDynLoad_SetAllocator(CustomDynLoadAlloc, CustomDynLoadFree);
for (auto &pluginContainer : plugins) {
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()->getTrampolineId())) {
return false;
}
}
OSDynLoad_SetAllocator(prevDynLoadAlloc, prevDynLoadFree);
return true;
}

View File

@ -1,4 +1,5 @@
#include "utils/logger.h"
#include "utils.h"
#include "logger.h"
#include <coreinit/ios.h>
#include <cstring>
#include <malloc.h>
@ -53,3 +54,25 @@ void dumpHex(const void *data, size_t size) {
}
}
}
OSDynLoad_Error CustomDynLoadAlloc(int32_t size, int32_t align, void **outAddr) {
if (!outAddr) {
return OS_DYNLOAD_INVALID_ALLOCATOR_PTR;
}
if (align >= 0 && align < 4) {
align = 4;
} else if (align < 0 && align > -4) {
align = -4;
}
if (!(*outAddr = memalign(align, size))) {
return OS_DYNLOAD_OUT_OF_MEMORY;
}
return OS_DYNLOAD_OK;
}
void CustomDynLoadFree(void *addr) {
free(addr);
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <coreinit/dynload.h>
#include <cstdint>
#include <forward_list>
#include <malloc.h>
@ -73,4 +74,8 @@ bool remove_locked_first_if(std::mutex &mutex, std::forward_list<T, Allocator> &
return false;
}
std::string getPluginPath();
std::string getPluginPath();
OSDynLoad_Error CustomDynLoadAlloc(int32_t size, int32_t align, void **outAddr);
void CustomDynLoadFree(void *addr);