2020-05-03 10:21:05 +02:00
|
|
|
#include <coreinit/cache.h>
|
2020-06-13 16:57:53 +02:00
|
|
|
#include <coreinit/dynload.h>
|
2020-12-26 14:17:50 +01:00
|
|
|
#include <coreinit/memdefaultheap.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/PluginMetaInformationFactory.h"
|
|
|
|
#include "plugin/PluginInformationFactory.h"
|
|
|
|
|
|
|
|
#include "utils/ElfUtils.h"
|
2020-05-03 10:21:05 +02:00
|
|
|
#include "PluginManagement.h"
|
|
|
|
#include "hooks.h"
|
2021-04-01 00:37:22 +02:00
|
|
|
#include "globals.h"
|
2020-05-03 10:21:05 +02:00
|
|
|
|
2020-05-03 12:25:38 +02:00
|
|
|
bool PluginManagement::doRelocation(const std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length, uint32_t trampolinID) {
|
|
|
|
std::map<std::string, OSDynLoad_Module> moduleHandleCache;
|
2021-09-25 14:26:18 +02:00
|
|
|
for (auto const &cur: relocData) {
|
2020-06-03 18:22:44 +02:00
|
|
|
uint32_t functionAddress = 0;
|
2020-12-26 14:17:50 +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) {
|
|
|
|
std::string rplName = cur.getImportRPLInformation().getName();
|
|
|
|
int32_t isData = cur.getImportRPLInformation().isData();
|
2020-12-26 14:17:50 +01:00
|
|
|
OSDynLoad_Module rplHandle = nullptr;
|
2020-06-03 18:22:44 +02:00
|
|
|
if (moduleHandleCache.count(rplName) > 0) {
|
|
|
|
rplHandle = moduleHandleCache[rplName];
|
|
|
|
} else {
|
|
|
|
OSDynLoad_Acquire(rplName.c_str(), &rplHandle);
|
|
|
|
moduleHandleCache[rplName] = rplHandle;
|
|
|
|
}
|
|
|
|
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) {
|
2020-05-03 10:21:05 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Failed to find export for %s", functionName.c_str());
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
//DEBUG_FUNCTION_LINE("Found export for %s %s", rplName.c_str(), functionName.c_str());
|
|
|
|
}
|
2020-05-03 12:25:38 +02:00
|
|
|
if (!ElfUtils::elfLinkOne(cur.getType(), cur.getOffset(), cur.getAddend(), (uint32_t) cur.getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT, trampolinID)) {
|
2020-12-28 14:40:53 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Relocation failed");
|
2020-05-03 10:21:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DCFlushRange(tramp_data, tramp_length * sizeof(relocation_trampolin_entry_t));
|
|
|
|
ICInvalidateRange(tramp_data, tramp_length * sizeof(relocation_trampolin_entry_t));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-03 12:25:38 +02:00
|
|
|
void PluginManagement::doRelocations(const std::vector<PluginContainer> &plugins, relocation_trampolin_entry_t *trampData, uint32_t tramp_size) {
|
2021-09-25 14:26:18 +02:00
|
|
|
for (auto &pluginContainer: plugins) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Doing relocations for plugin: %s", pluginContainer.getMetaInformation().getName().c_str());
|
2020-05-03 10:21:05 +02:00
|
|
|
|
2020-05-03 12:25:38 +02:00
|
|
|
if (!PluginManagement::doRelocation(pluginContainer.getPluginInformation().getRelocationDataList(), trampData, tramp_size, pluginContainer.getPluginInformation().getTrampolinId())) {
|
2020-05-03 10:21:05 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Relocation failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:25:38 +02:00
|
|
|
void PluginManagement::memsetBSS(const std::vector<PluginContainer> &plugins) {
|
2021-09-25 14:26:18 +02:00
|
|
|
for (auto &pluginContainer: plugins) {
|
2020-05-03 10:21:05 +02:00
|
|
|
auto sbssSection = pluginContainer.getPluginInformation().getSectionInfo(".sbss");
|
2020-05-03 12:25:38 +02:00
|
|
|
if (sbssSection) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("memset .sbss %08X (%d)", sbssSection->getAddress(), sbssSection->getSize());
|
2020-05-03 12:25:38 +02:00
|
|
|
memset((void *) sbssSection->getAddress(), 0, sbssSection->getSize());
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
|
|
|
auto bssSection = pluginContainer.getPluginInformation().getSectionInfo(".bss");
|
2020-05-03 12:25:38 +02:00
|
|
|
if (bssSection) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("memset .bss %08X (%d)", bssSection->getAddress(), bssSection->getSize());
|
2020-05-03 12:25:38 +02:00
|
|
|
memset((void *) bssSection->getAddress(), 0, bssSection->getSize());
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 21:04:16 +02:00
|
|
|
void PluginManagement::RestorePatches(plugin_information_t *pluginInformation, BOOL pluginOnly) {
|
|
|
|
for (int32_t plugin_index = pluginInformation->number_used_plugins - 1; plugin_index >= 0; plugin_index--) {
|
2020-06-13 16:57:53 +02:00
|
|
|
FunctionPatcherRestoreFunctions(pluginInformation->plugin_data[plugin_index].info.functions, pluginInformation->plugin_data[plugin_index].info.number_used_functions);
|
2020-05-17 21:04:16 +02:00
|
|
|
}
|
|
|
|
if (!pluginOnly) {
|
2020-06-12 20:51:21 +02:00
|
|
|
FunctionPatcherRestoreFunctions(method_hooks_hooks_static, method_hooks_size_hooks_static);
|
2020-05-17 21:04:16 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 14:16:11 +02:00
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
void PluginManagement::unloadPlugins(plugin_information_t *pluginInformation, MEMHeapHandle pluginHeap, BOOL freePluginData) {
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_MALLOC);
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_NEWLIB);
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_STDCPP);
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB);
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_SOCKETS);
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_DEINIT_PLUGIN);
|
|
|
|
// CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_SOCKETS); To keep network alive we skip this.
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_DEVOPTAB);
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_STDCPP);
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_NEWLIB);
|
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_MALLOC);
|
|
|
|
|
|
|
|
RestorePatches(pluginInformation, true);
|
|
|
|
for (int32_t plugin_index = 0; plugin_index < pluginInformation->number_used_plugins; plugin_index++) {
|
|
|
|
plugin_information_single_t *plugin = &(pluginInformation->plugin_data[plugin_index]);
|
2020-05-17 21:06:11 +02:00
|
|
|
if (freePluginData) {
|
|
|
|
if (plugin->data.buffer != nullptr) {
|
|
|
|
if (plugin->data.memoryType == eMemTypeMEM2) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Free plugin data buffer for %s [%08X]", plugin->meta.name, plugin->data.buffer);
|
2020-05-17 21:06:11 +02:00
|
|
|
free(plugin->data.buffer);
|
|
|
|
} else if (plugin->data.memoryType == eMemTypeExpHeap) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Free plugin data buffer for %s [%08X on heap %08X]", plugin->meta.name, plugin->data.buffer, plugin->data.heapHandle);
|
2020-05-17 21:06:11 +02:00
|
|
|
MEMFreeToExpHeap((MEMHeapHandle) plugin->data.heapHandle, plugin->data.buffer);
|
|
|
|
} else {
|
|
|
|
DEBUG_FUNCTION_LINE("########################");
|
|
|
|
DEBUG_FUNCTION_LINE("Failed to free memory from plugin");
|
|
|
|
DEBUG_FUNCTION_LINE("########################");
|
|
|
|
}
|
2021-01-10 13:17:47 +01:00
|
|
|
plugin->data.buffer = nullptr;
|
2020-05-17 21:06:11 +02:00
|
|
|
plugin->data.bufferLength = 0;
|
2020-05-03 14:16:11 +02:00
|
|
|
} else {
|
2021-01-10 13:17:47 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Plugin has no copy of elf saved in memory, can't free it");
|
2020-05-03 14:16:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (plugin->info.allocatedTextMemoryAddress != nullptr) {
|
|
|
|
MEMFreeToExpHeap((MEMHeapHandle) pluginHeap, plugin->info.allocatedTextMemoryAddress);
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Deleted allocated .text section for plugin %s [%08X]", plugin->meta.name, plugin->info.allocatedTextMemoryAddress);
|
2020-05-03 14:16:11 +02:00
|
|
|
}
|
|
|
|
if (plugin->info.allocatedDataMemoryAddress != nullptr) {
|
|
|
|
MEMFreeToExpHeap((MEMHeapHandle) pluginHeap, plugin->info.allocatedDataMemoryAddress);
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Deleted allocated .data section for plugin %s [%08X]", plugin->meta.name, plugin->info.allocatedDataMemoryAddress);
|
2020-05-03 14:16:11 +02:00
|
|
|
}
|
2020-05-17 20:41:11 +02:00
|
|
|
|
2021-04-01 00:37:22 +02:00
|
|
|
for (uint32_t i = 0; i < gTrampolineDataSize; i++) {
|
|
|
|
auto trampoline = &(gTrampolineData[i]);
|
|
|
|
if (trampoline->id == plugin->info.trampolinId) {
|
|
|
|
trampoline->id = 0;
|
|
|
|
trampoline->status = RELOC_TRAMP_FREE;
|
2020-05-17 20:41:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 14:16:11 +02:00
|
|
|
}
|
2020-05-17 21:06:11 +02:00
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
memset((void *) pluginInformation, 0, sizeof(plugin_information_t));
|
2020-05-03 14:16:11 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 10:21:05 +02:00
|
|
|
void PluginManagement::callInitHooks(plugin_information_t *pluginInformation) {
|
2021-04-07 00:23:23 +02:00
|
|
|
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_STORAGE);
|
2020-05-03 10:21:05 +02:00
|
|
|
CallHook(pluginInformation, 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
|
|
|
}
|
|
|
|
|
2020-12-26 15:59:06 +01:00
|
|
|
void module_callback(OSDynLoad_Module module,
|
|
|
|
void *userContext,
|
|
|
|
OSDynLoad_NotifyReason reason,
|
|
|
|
OSDynLoad_NotifyData *infos) {
|
|
|
|
if (reason == OS_DYNLOAD_NOTIFY_LOADED) {
|
2021-09-25 14:26:18 +02:00
|
|
|
auto *pluginInformation = (plugin_information_t *) userContext;
|
|
|
|
for (int32_t plugin_index = 0; plugin_index < pluginInformation->number_used_plugins; plugin_index++) {
|
|
|
|
FunctionPatcherPatchFunction(pluginInformation->plugin_data[plugin_index].info.functions, pluginInformation->plugin_data[plugin_index].info.number_used_functions);
|
2020-12-26 15:59:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
void PluginManagement::PatchFunctionsAndCallHooks(plugin_information_t *pluginInformation) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Patching functions");
|
2020-06-12 20:51:21 +02:00
|
|
|
FunctionPatcherPatchFunction(method_hooks_hooks_static, method_hooks_size_hooks_static);
|
2020-05-03 10:21:05 +02:00
|
|
|
|
2020-05-29 19:27:55 +02:00
|
|
|
DCFlushRange((void *) 0x00800000, 0x00800000);
|
|
|
|
ICInvalidateRange((void *) 0x00800000, 0x00800000);
|
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
for (int32_t plugin_index = 0; plugin_index < pluginInformation->number_used_plugins; plugin_index++) {
|
|
|
|
CallHookEx(pluginInformation, WUPS_LOADER_HOOK_APPLICATION_STARTS, plugin_index);
|
|
|
|
FunctionPatcherPatchFunction(pluginInformation->plugin_data[plugin_index].info.functions, pluginInformation->plugin_data[plugin_index].info.number_used_functions);
|
|
|
|
CallHookEx(pluginInformation, WUPS_LOADER_HOOK_FUNCTIONS_PATCHED, plugin_index);
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
2021-09-25 14:26:18 +02:00
|
|
|
OSDynLoad_AddNotifyCallback(module_callback, pluginInformation);
|
2020-05-03 10:21:05 +02:00
|
|
|
}
|
2020-05-03 14:20:00 +02:00
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
std::vector<PluginContainer>
|
|
|
|
PluginManagement::loadPlugins(const std::vector<PluginData> &pluginList, MEMHeapHandle heapHandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) {
|
2020-05-03 14:20:00 +02:00
|
|
|
std::vector<PluginContainer> plugins;
|
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
for (auto &pluginData: pluginList) {
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Load meta information");
|
2020-05-03 14:20:00 +02:00
|
|
|
auto metaInfo = PluginMetaInformationFactory::loadPlugin(pluginData);
|
|
|
|
if (metaInfo) {
|
|
|
|
PluginContainer container;
|
|
|
|
container.setMetaInformation(metaInfo.value());
|
2020-06-03 18:30:05 +02:00
|
|
|
container.setPluginData(pluginData);
|
2020-05-03 14:20:00 +02:00
|
|
|
plugins.push_back(container);
|
|
|
|
} else {
|
|
|
|
DEBUG_FUNCTION_LINE("Failed to get meta information");
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 18:23:23 +02:00
|
|
|
uint32_t trampolineID = 0;
|
2021-09-25 14:26:18 +02:00
|
|
|
for (auto &pluginContainer: plugins) {
|
2020-06-03 18:23:23 +02:00
|
|
|
std::optional<PluginInformation> info = PluginInformationFactory::load(pluginContainer.getPluginData(), heapHandle, trampolin_data, trampolin_data_length, trampolineID++);
|
2020-05-03 14:20:00 +02:00
|
|
|
if (!info) {
|
|
|
|
DEBUG_FUNCTION_LINE("Failed to load Plugin %s", pluginContainer.getMetaInformation().getName().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pluginContainer.setPluginInformation(info.value());
|
|
|
|
}
|
|
|
|
return plugins;
|
|
|
|
}
|
|
|
|
|