2022-05-14 14:00:20 +02:00
|
|
|
#include "PluginManagement.h"
|
|
|
|
#include "hooks.h"
|
2020-05-03 10:21:05 +02:00
|
|
|
#include "patcher/hooks_patcher_static.h"
|
2020-12-26 14:17:50 +01:00
|
|
|
#include "plugin/PluginContainer.h"
|
|
|
|
#include "plugin/PluginInformationFactory.h"
|
2022-02-04 16:25:44 +01:00
|
|
|
#include "plugin/PluginMetaInformationFactory.h"
|
|
|
|
#include "utils/ElfUtils.h"
|
2022-05-14 14:00:20 +02:00
|
|
|
#include "utils/utils.h"
|
|
|
|
#include <coreinit/cache.h>
|
|
|
|
#include <coreinit/dynload.h>
|
|
|
|
#include <forward_list>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <memory>
|
|
|
|
#include <ranges>
|
2020-05-03 10:21:05 +02:00
|
|
|
|
2022-10-05 20:08:49 +02:00
|
|
|
bool PluginManagement::doRelocation(const std::vector<std::unique_ptr<RelocationData>> &relocData,
|
|
|
|
relocation_trampoline_entry_t *tramp_data,
|
|
|
|
uint32_t tramp_length,
|
|
|
|
uint32_t trampolineID,
|
|
|
|
std::map<std::string, OSDynLoad_Module> &usedRPls) {
|
2022-02-04 16:25:44 +01:00
|
|
|
for (auto const &cur : relocData) {
|
|
|
|
uint32_t functionAddress = 0;
|
2021-12-15 17:09:30 +01:00
|
|
|
const std::string &functionName = cur->getName();
|
2020-06-03 18:22:44 +02:00
|
|
|
|
2020-12-26 14:17:50 +01:00
|
|
|
if (functionName == "MEMAllocFromDefaultHeap") {
|
2020-06-03 18:22:44 +02:00
|
|
|
OSDynLoad_Module rplHandle;
|
|
|
|
OSDynLoad_Acquire("homebrew_memorymapping", &rplHandle);
|
|
|
|
OSDynLoad_FindExport(rplHandle, 1, "MEMAllocFromMappedMemory", (void **) &functionAddress);
|
2020-12-26 14:17:50 +01:00
|
|
|
} else if (functionName == "MEMAllocFromDefaultHeapEx") {
|
2020-06-03 18:22:44 +02:00
|
|
|
OSDynLoad_Module rplHandle;
|
|
|
|
OSDynLoad_Acquire("homebrew_memorymapping", &rplHandle);
|
|
|
|
OSDynLoad_FindExport(rplHandle, 1, "MEMAllocFromMappedMemoryEx", (void **) &functionAddress);
|
2020-12-26 14:17:50 +01:00
|
|
|
} else if (functionName == "MEMFreeToDefaultHeap") {
|
2020-06-03 18:22:44 +02:00
|
|
|
OSDynLoad_Module rplHandle;
|
|
|
|
OSDynLoad_Acquire("homebrew_memorymapping", &rplHandle);
|
|
|
|
OSDynLoad_FindExport(rplHandle, 1, "MEMFreeToMappedMemory", (void **) &functionAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functionAddress == 0) {
|
2022-05-14 14:00:20 +02:00
|
|
|
auto rplName = cur->getImportRPLInformation()->getRPLName();
|
2022-02-04 16:25:44 +01:00
|
|
|
int32_t isData = cur->getImportRPLInformation()->isData();
|
2020-12-26 14:17:50 +01:00
|
|
|
OSDynLoad_Module rplHandle = nullptr;
|
2022-10-05 20:08:49 +02:00
|
|
|
|
|
|
|
if (!usedRPls.contains(rplName)) {
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Acquire %s", rplName.c_str());
|
|
|
|
// Always acquire to increase refcount and make sure it won't get unloaded while we're using it.
|
|
|
|
OSDynLoad_Error err = OSDynLoad_Acquire(rplName.c_str(), &rplHandle);
|
|
|
|
if (err != OS_DYNLOAD_OK) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to acquire %s", rplName.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Keep track RPLs we are using.
|
|
|
|
// They will be released on exit
|
|
|
|
usedRPls[rplName] = rplHandle;
|
2020-06-03 18:22:44 +02:00
|
|
|
} else {
|
2022-10-05 20:08:49 +02:00
|
|
|
rplHandle = usedRPls[rplName];
|
2020-06-03 18:22:44 +02:00
|
|
|
}
|
2022-10-05 20:08:49 +02:00
|
|
|
|
2020-06-03 18:22:44 +02:00
|
|
|
OSDynLoad_FindExport(rplHandle, isData, functionName.c_str(), (void **) &functionAddress);
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 12:25:38 +02:00
|
|
|
if (functionAddress == 0) {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to find export for %s", functionName.c_str());
|
2020-05-03 10:21:05 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
//DEBUG_FUNCTION_LINE("Found export for %s %s", rplName.c_str(), functionName.c_str());
|
|
|
|
}
|
2022-10-03 21:51:12 +02:00
|
|
|
|
2022-01-27 12:47:33 +01:00
|
|
|
if (!ElfUtils::elfLinkOne(cur->getType(), cur->getOffset(), cur->getAddend(), (uint32_t) cur->getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT, trampolineID)) {
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("elfLinkOne failed");
|
2020-05-03 10:21:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
// Unloading RPLs which you want to use is stupid.
|
|
|
|
// Never uncomment this again.
|
|
|
|
/* for (auto &cur : moduleHandleCache) {
|
|
|
|
// Release handle if they are not from DynLoadPatchModule
|
|
|
|
if (((uint32_t) cur.second & 0xFFFF0000) != 0x13370000) {
|
|
|
|
OSDynLoad_Release(cur.second);
|
|
|
|
}
|
|
|
|
} */
|
|
|
|
|
2022-01-27 12:47:33 +01:00
|
|
|
DCFlushRange(tramp_data, tramp_length * sizeof(relocation_trampoline_entry_t));
|
|
|
|
ICInvalidateRange(tramp_data, tramp_length * sizeof(relocation_trampoline_entry_t));
|
2022-05-14 14:00:20 +02:00
|
|
|
OSMemoryBarrier();
|
2020-05-03 10:21:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-05 20:08:49 +02:00
|
|
|
bool PluginManagement::doRelocations(const std::vector<std::unique_ptr<PluginContainer>> &plugins,
|
|
|
|
relocation_trampoline_entry_t *trampData, uint32_t tramp_size,
|
|
|
|
std::map<std::string, OSDynLoad_Module> &usedRPls) {
|
2022-10-03 21:58:11 +02:00
|
|
|
for (uint32_t i = 0; i < tramp_size; i++) {
|
|
|
|
if (trampData[i].status == RELOC_TRAMP_IMPORT_DONE) {
|
|
|
|
trampData[i].status = RELOC_TRAMP_FREE;
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 21:51:12 +02:00
|
|
|
|
|
|
|
OSDynLoadAllocFn prevDynLoadAlloc = nullptr;
|
|
|
|
OSDynLoadFreeFn prevDynLoadFree = nullptr;
|
|
|
|
|
|
|
|
OSDynLoad_GetAllocator(&prevDynLoadAlloc, &prevDynLoadFree);
|
|
|
|
OSDynLoad_SetAllocator(CustomDynLoadAlloc, CustomDynLoadFree);
|
|
|
|
|
2022-02-04 16:25:44 +01:00
|
|
|
for (auto &pluginContainer : plugins) {
|
2021-12-15 17:09:30 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Doing relocations for plugin: %s", pluginContainer->getMetaInformation()->getName().c_str());
|
2022-10-05 20:08:49 +02:00
|
|
|
if (!PluginManagement::doRelocation(pluginContainer->getPluginInformation()->getRelocationDataList(),
|
|
|
|
trampData,
|
|
|
|
tramp_size,
|
|
|
|
pluginContainer->getPluginInformation()->getTrampolineId(),
|
|
|
|
usedRPls)) {
|
2022-05-14 14:00:20 +02:00
|
|
|
return false;
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-03 21:51:12 +02:00
|
|
|
|
|
|
|
OSDynLoad_SetAllocator(prevDynLoadAlloc, prevDynLoadFree);
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
return true;
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
bool PluginManagement::RestoreFunctionPatches(const std::vector<std::unique_ptr<PluginContainer>> &plugins) {
|
|
|
|
for (const auto &cur : std::ranges::reverse_view(plugins)) {
|
|
|
|
for (const auto &curFunction : std::ranges::reverse_view(cur->getPluginInformation()->getFunctionDataList())) {
|
|
|
|
if (!curFunction->restore()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
return true;
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
bool PluginManagement::DoFunctionPatches(const std::vector<std::unique_ptr<PluginContainer>> &plugins) {
|
|
|
|
for (const auto &cur : plugins) {
|
|
|
|
for (const auto &curFunction : cur->getPluginInformation()->getFunctionDataList()) {
|
|
|
|
if (!curFunction->patch()) {
|
|
|
|
return false;
|
2020-05-17 20:41:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 14:16:11 +02:00
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
return true;
|
2020-05-03 14:16:11 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
void PluginManagement::callInitHooks(const std::vector<std::unique_ptr<PluginContainer>> &plugins) {
|
|
|
|
CallHook(plugins, WUPS_LOADER_HOOK_INIT_STORAGE);
|
|
|
|
CallHook(plugins, WUPS_LOADER_HOOK_INIT_PLUGIN);
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Done calling init hooks");
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
std::vector<std::unique_ptr<PluginContainer>>
|
|
|
|
PluginManagement::loadPlugins(const std::forward_list<std::shared_ptr<PluginData>> &pluginList, relocation_trampoline_entry_t *trampoline_data, uint32_t trampoline_data_length) {
|
|
|
|
std::vector<std::unique_ptr<PluginContainer>> plugins;
|
2020-05-03 14:20:00 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
uint32_t trampolineID = 0;
|
2022-02-04 16:25:44 +01:00
|
|
|
for (auto &pluginData : pluginList) {
|
2020-05-03 14:20:00 +02:00
|
|
|
auto metaInfo = PluginMetaInformationFactory::loadPlugin(pluginData);
|
|
|
|
if (metaInfo) {
|
2022-05-14 14:00:20 +02:00
|
|
|
auto info = PluginInformationFactory::load(pluginData, trampoline_data, trampoline_data_length, trampolineID++);
|
|
|
|
if (!info) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to load Plugin %s", metaInfo.value()->getName().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto container = make_unique_nothrow<PluginContainer>(std::move(metaInfo.value()), std::move(info.value()), pluginData);
|
|
|
|
if (!container) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to create PluginContainer, skipping %s", metaInfo.value()->getName().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
plugins.push_back(std::move(container));
|
2020-05-03 14:20:00 +02:00
|
|
|
} else {
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get meta information");
|
2020-05-03 14:20:00 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
|
|
|
|
if (!PluginManagement::DoFunctionPatches(plugins)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to patch functions");
|
|
|
|
OSFatal("Failed to patch functions");
|
2020-05-03 14:20:00 +02:00
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
|
2020-05-03 14:20:00 +02:00
|
|
|
return plugins;
|
2022-05-14 14:00:20 +02:00
|
|
|
}
|