Clean up helper classes, sync with WUPS

This commit is contained in:
Maschell 2026-03-05 20:02:22 +01:00
parent bf3210555b
commit b4fbfbe83e
18 changed files with 236 additions and 132 deletions

View File

@ -1,4 +1,5 @@
#include "globals.h"
#include "module/ModuleData.h"
MEMHeapHandle gHeapHandle __attribute__((section(".data"))) = nullptr;
uint8_t gInitCalled __attribute__((section(".data"))) = 0;

View File

@ -1,10 +1,17 @@
#pragma once
#include "module/ModuleData.h"
#include <wums/hooks.h>
#include <coreinit/dynload.h>
#include <coreinit/memheap.h>
#include <cstdint>
#include <memory>
#include <vector>
#include <map>
#include <cstdint>
class ModuleData;
extern uint8_t gInitCalled;
extern MEMHeapHandle gHeapHandle;
@ -24,7 +31,6 @@ extern WUMSRPLAllocatorFreeFn gCustomRPLAllocatorFreeFn;
#define ENVIRONMENT_PATH_LENGTH 0x100 // Length of the EnvironmentPath.
#define MEMORY_REGION_USABLE_MEM_REGION_END_LENGTH 0x04 // sizeof(uint32_t)
#define MEMORY_REGION_ENVIRONMENT_STRING_ADRR (MEMORY_REGION_START + RELOCATOR_SIZE)
#define MEMORY_REGION_USABLE_MEM_REGION_END_VALUE_PTR ((uint32_t *) (MEMORY_REGION_ENVIRONMENT_STRING_ADRR + ENVIRONMENT_PATH_LENGTH))
#define MEMORY_REGION_USABLE_MEM_REGION_END_VALUE (*MEMORY_REGION_USABLE_MEM_REGION_END_VALUE_PTR)

View File

@ -0,0 +1,26 @@
#include "FunctionSymbolData.h"
FunctionSymbolData::FunctionSymbolData(const FunctionSymbolData &o2) = default;
FunctionSymbolData::FunctionSymbolData(std::string_view name, void *address, uint32_t size) : mName(name),
mAddress(address),
mSize(size) {
}
bool FunctionSymbolData::operator<(const FunctionSymbolData &rhs) const {
return reinterpret_cast<uint32_t>(mAddress) < reinterpret_cast<uint32_t>(rhs.mAddress);
}
FunctionSymbolData::~FunctionSymbolData() = default;
[[nodiscard]] const std::string &FunctionSymbolData::getName() const {
return mName;
}
[[nodiscard]] void *FunctionSymbolData::getAddress() const {
return mAddress;
}
[[nodiscard]] uint32_t FunctionSymbolData::getSize() const {
return mSize;
}

View File

@ -18,37 +18,29 @@
#pragma once
#include <string>
#include <string_view>
#include <cstdint>
class FunctionSymbolData {
public:
FunctionSymbolData(const FunctionSymbolData &o2) = default;
FunctionSymbolData(const FunctionSymbolData &o2);
FunctionSymbolData(std::string name, void *address, uint32_t size) : name(std::move(name)),
address(address),
size(size) {
}
FunctionSymbolData(std::string_view name, void *address, uint32_t size);
virtual ~FunctionSymbolData() = default;
bool operator<(const FunctionSymbolData &rhs) const;
bool operator<(const FunctionSymbolData &rhs) const {
return (uint32_t) address < (uint32_t) rhs.address;
}
virtual ~FunctionSymbolData();
[[nodiscard]] const std::string &getName() const {
return name;
}
[[nodiscard]] const std::string &getName() const;
[[nodiscard]] void *getAddress() const {
return address;
}
[[nodiscard]] void *getAddress() const;
[[nodiscard]] uint32_t getSize() const {
return size;
}
[[nodiscard]] uint32_t getSize() const;
private:
std::string name;
void *address;
uint32_t size;
std::string mName;
void *mAddress;
uint32_t mSize;
};

View File

@ -0,0 +1,16 @@
#include "HookData.h"
HookData::HookData(void *functionPointer, const wums_hook_type_t type) {
this->mFunctionPointer = functionPointer;
this->mType = type;
}
HookData::~HookData() = default;
[[nodiscard]] void *HookData::getFunctionPointer() const {
return mFunctionPointer;
}
[[nodiscard]] wums_hook_type_t HookData::getType() const {
return mType;
}

View File

@ -1,23 +1,36 @@
/****************************************************************************
* 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 <wums.h>
#include <wums/hooks.h>
class HookData {
public:
HookData(wums_hook_type_t type, const void *target) {
this->type = type;
this->target = target;
}
HookData(void *functionPointer, wums_hook_type_t type);
[[nodiscard]] wums_hook_type_t getType() const {
return type;
}
~HookData();
[[nodiscard]] const void *getTarget() const {
return target;
}
[[nodiscard]] void *getFunctionPointer() const;
[[nodiscard]] wums_hook_type_t getType() const;
private:
wums_hook_type_t type;
const void *target;
};
void *mFunctionPointer;
wums_hook_type_t mType;
};

View File

@ -0,0 +1,20 @@
#include "ImportRPLInformation.h"
#include <cstring>
ImportRPLInformation::ImportRPLInformation(std::string_view name) {
this->mName = name;
}
ImportRPLInformation::~ImportRPLInformation() = default;
[[nodiscard]] const std::string &ImportRPLInformation::getName() const {
return mName;
}
[[nodiscard]] std::string ImportRPLInformation::getRPLName() const {
return mName.substr(strlen(".dimport_"));
}
[[nodiscard]] bool ImportRPLInformation::isData() const {
return mName.starts_with(".dimport_");
}

View File

@ -17,37 +17,22 @@
#pragma once
#include "utils/logger.h"
#include <coreinit/debug.h>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <string_view>
class ImportRPLInformation {
public:
explicit ImportRPLInformation(std::string rawSectionName) {
this->name = std::move(rawSectionName);
}
explicit ImportRPLInformation(std::string_view name);
~ImportRPLInformation() = default;
~ImportRPLInformation();
[[nodiscard]] const std::string &getName() const {
return name;
}
[[nodiscard]] const std::string &getName() const;
[[nodiscard]] const char *getRPLName() const {
if (name.max_size() < strlen("._import_") + 1) {
OSFatal("Invalid RPLName, is too short to be valid");
}
return name.c_str() + strlen("._import_");
}
[[nodiscard]] std::string getRPLName() const;
[[nodiscard]] bool isData() const {
return name.starts_with(".dimport_");
}
[[nodiscard]] bool isData() const;
private:
std::string name;
std::string mName;
};

View File

@ -22,7 +22,9 @@
#include "HookData.h"
#include "RelocationData.h"
#include "SectionInfo.h"
#include <map>
#include <optional>
#include <set>
#include <string>
#include <vector>
@ -55,7 +57,7 @@ public:
}
void addRelocationData(std::unique_ptr<RelocationData> relocation_data) {
addDependency(relocation_data->getImportRPLInformation()->getRPLName());
addDependency(relocation_data->getImportRPLInformation().getRPLName());
relocation_data_list.push_back(std::move(relocation_data));
}

View File

@ -16,6 +16,7 @@
****************************************************************************/
#include "ModuleDataFactory.h"
#include "HookData.h"
#include "fs/FileUtils.h"
#include "utils/ElfUtils.h"
#include "utils/OnLeavingScope.h"
@ -25,6 +26,7 @@
#include <coreinit/cache.h>
#include <coreinit/memdefaultheap.h>
#include <map>
#include <optional>
#include <string>
#include <wums.h>
@ -276,7 +278,7 @@ std::optional<std::shared_ptr<ModuleData>> ModuleDataFactory::load(const std::st
for (size_t j = 0; j < entries_count; j++) {
wums_hook_t *hook = &hooks[j];
DEBUG_FUNCTION_LINE("Saving hook of type %08X, target: %p", hook->type, hook->target);
auto hookData = make_unique_nothrow<HookData>(hook->type, hook->target);
auto hookData = make_unique_nothrow<HookData>(const_cast<void *>(hook->target), hook->type);
if (!hookData) {
DEBUG_FUNCTION_LINE_ERR("Failed to alloc HookData");
return {};

View File

@ -1,5 +1,6 @@
#include "ModuleDataPersistence.h"
#include "globals.h"
#include "utils/logger.h"
#include "utils/utils.h"
#include <coreinit/cache.h>
@ -139,7 +140,7 @@ bool ModuleDataPersistence::saveHookDataForModule(module_information_single_t &m
}
auto *curHook = &hook_data[hookCount++];
curHook->type = hook->getType();
curHook->target = (uint32_t) hook->getTarget();
curHook->target = (uint32_t) hook->getFunctionPointer();
}
module_data.hook_entries = hook_data.get();
module_data.number_hook_entries = hookCount;
@ -159,7 +160,7 @@ bool ModuleDataPersistence::saveRelocationDataForModule(module_information_singl
// Determine how many dyn_linking_import_t entries we need.
std::set<std::string_view> rplInfoCountSet;
for (auto const &reloc : module->getRelocationDataList()) {
rplInfoCountSet.insert(reloc->getImportRPLInformation()->getName());
rplInfoCountSet.insert(reloc->getImportRPLInformation().getName());
}
uint32_t rplInfoTotalCount = rplInfoCountSet.size();
@ -180,14 +181,14 @@ bool ModuleDataPersistence::saveRelocationDataForModule(module_information_singl
OSFatal("We tried to write more entries than we have space for.");
}
auto *curReloc = &relocation_data[relocationCount++];
curReloc->destination = reloc->getDestination();
curReloc->destination = const_cast<void *>(reloc->getDestination());
curReloc->offset = reloc->getOffset();
curReloc->addend = reloc->getAddend();
curReloc->type = reloc->getType();
curReloc->functionName = reloc->getName().c_str();
auto &rplInfo = reloc->getImportRPLInformation();
auto rplIt = rplInfoMap.find(rplInfo->getName());
auto rplIt = rplInfoMap.find(rplInfo.getName());
if (rplIt != rplInfoMap.end()) {
curReloc->importEntry = rplIt->second;
} else {
@ -196,9 +197,9 @@ bool ModuleDataPersistence::saveRelocationDataForModule(module_information_singl
OSFatal("We tried to write more entries than we have space for.");
}
auto *rplInfoPtr = &rpl_data[rplInfoCount++];
rplInfoPtr->isData = rplInfo->isData();
rplInfoPtr->importName = rplInfo->getName().c_str();
rplInfoMap[rplInfo->getName()] = rplInfoPtr;
rplInfoPtr->isData = rplInfo.isData();
rplInfoPtr->importName = rplInfo.getName().c_str();
rplInfoMap[rplInfo.getName()] = rplInfoPtr;
curReloc->importEntry = rplInfoPtr;
}
}

View File

@ -0,0 +1,42 @@
#include "RelocationData.h"
RelocationData::RelocationData(const char type,
const size_t offset,
const int32_t addend,
void *destination,
std::string name,
std::shared_ptr<ImportRPLInformation> rplInfo) : mType(type),
mOffset(offset),
mAddend(addend),
mDestination(destination),
mName(std::move(name)),
mRPLInfo(std::move(rplInfo)) {
}
RelocationData::RelocationData(const RelocationData &o2) = default;
RelocationData::~RelocationData() = default;
[[nodiscard]] char RelocationData::getType() const {
return mType;
}
[[nodiscard]] size_t RelocationData::getOffset() const {
return mOffset;
}
[[nodiscard]] int32_t RelocationData::getAddend() const {
return mAddend;
}
[[nodiscard]] const void *RelocationData::getDestination() const {
return mDestination;
}
[[nodiscard]] const std::string &RelocationData::getName() const {
return mName;
}
[[nodiscard]] const ImportRPLInformation &RelocationData::getImportRPLInformation() const {
return *mRPLInfo;
}

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2018 Maschell
* 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
@ -18,52 +18,38 @@
#pragma once
#include "ImportRPLInformation.h"
#include <memory>
#include <string>
#include <utility>
#include <cstdint>
class RelocationData {
public:
RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, std::shared_ptr<ImportRPLInformation> rplInfo) : rplInfo(std::move(rplInfo)) {
this->type = type;
this->offset = offset;
this->addend = addend;
this->destination = destination;
this->name = std::move(name);
}
RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, std::shared_ptr<ImportRPLInformation> rplInfo);
~RelocationData() = default;
RelocationData(const RelocationData &o2);
[[nodiscard]] char getType() const {
return type;
}
virtual ~RelocationData();
[[nodiscard]] size_t getOffset() const {
return offset;
}
[[nodiscard]] char getType() const;
[[nodiscard]] int32_t getAddend() const {
return addend;
}
[[nodiscard]] size_t getOffset() const;
[[nodiscard]] void *getDestination() const {
return destination;
}
[[nodiscard]] int32_t getAddend() const;
[[nodiscard]] const std::string &getName() const {
return name;
}
[[nodiscard]] const void *getDestination() const;
[[nodiscard]] const std::shared_ptr<ImportRPLInformation> &getImportRPLInformation() const {
return rplInfo;
}
[[nodiscard]] const std::string &getName() const;
[[nodiscard]] const ImportRPLInformation &getImportRPLInformation() const;
private:
char type;
size_t offset;
int32_t addend;
void *destination;
std::string name;
const std::shared_ptr<ImportRPLInformation> rplInfo;
char mType;
size_t mOffset;
int32_t mAddend;
void *mDestination;
std::string mName;
std::shared_ptr<ImportRPLInformation> mRPLInfo;
};

View File

@ -0,0 +1,24 @@
#include "SectionInfo.h"
SectionInfo::SectionInfo(std::string name,
const uint32_t address,
const uint32_t sectionSize) : mName(std::move(name)),
mAddress(address),
mSectionSize(sectionSize) {
}
[[nodiscard]] const std::string &SectionInfo::getName() const {
return mName;
}
[[nodiscard]] uint32_t SectionInfo::getAddress() const {
return mAddress;
}
[[nodiscard]] uint32_t SectionInfo::getSize() const {
return mSectionSize;
}
[[nodiscard]] uint32_t SectionInfo::isInSection(uint32_t addr) const {
return addr >= mAddress && addr < mAddress + mSectionSize;
}

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2022 Maschell
* 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
@ -18,38 +18,24 @@
#pragma once
#include <string>
#include <utility>
#include <cstdint>
class SectionInfo {
public:
SectionInfo(std::string name, uint32_t address, uint32_t sectionSize) : name(std::move(name)),
address(address),
sectionSize(sectionSize) {
}
SectionInfo(std::string name, uint32_t address, uint32_t sectionSize);
SectionInfo() = default;
[[nodiscard]] const std::string &getName() const;
SectionInfo(const SectionInfo &o2) = default;
[[nodiscard]] uint32_t getAddress() const;
SectionInfo &operator=(const SectionInfo &other) = default;
[[nodiscard]] uint32_t getSize() const;
virtual ~SectionInfo() = default;
[[nodiscard]] const std::string &getName() const {
return name;
}
[[nodiscard]] uint32_t getAddress() const {
return address;
}
[[nodiscard]] uint32_t getSize() const {
return sectionSize;
}
[[nodiscard]] uint32_t isInSection(uint32_t addr) const;
private:
std::string name;
uint32_t address{};
uint32_t sectionSize{};
std::string mName;
uint32_t mAddress = {};
uint32_t mSectionSize = {};
};

View File

@ -1,6 +1,7 @@
#include "RelocationUtils.h"
#include "ElfUtils.h"
#include "globals.h"
#include "logger.h"
#include "memory.h"
#include <algorithm>
#include <coreinit/cache.h>
@ -83,7 +84,7 @@ bool doRelocation(const std::vector<std::shared_ptr<ModuleData>> &moduleList,
const bool skipUnloadedRpl) {
for (auto const &curReloc : relocData) {
auto &functionName = curReloc->getName();
std::string rplName = curReloc->getImportRPLInformation()->getRPLName();
std::string rplName = curReloc->getImportRPLInformation().getRPLName();
uint32_t functionAddress = 0;
for (auto &module : moduleList) {
@ -111,7 +112,7 @@ bool doRelocation(const std::vector<std::shared_ptr<ModuleData>> &moduleList,
}
if (functionAddress == 0) {
int32_t isData = curReloc->getImportRPLInformation()->isData();
int32_t isData = curReloc->getImportRPLInformation().isData();
OSDynLoad_Module rplHandle = nullptr;
if (!usedRPls.contains(rplName)) {

View File

@ -3,6 +3,7 @@
#include "module/ModuleData.h"
#include "utils/logger.h"
#include <coreinit/memexpheap.h>
#include <optional>
#include <memory>
#include <wums.h>
@ -64,7 +65,7 @@ void CallHook(const std::shared_ptr<ModuleData> &module, wums_hook_type_t type)
bool foundHook = false;
#endif
for (auto &curHook : module->getHookDataList()) {
auto func_ptr = (uint32_t) curHook->getTarget();
auto func_ptr = (uint32_t) curHook->getFunctionPointer();
if (func_ptr == 0) {
DEBUG_FUNCTION_LINE_ERR("Module %s: hook ptr was NULL", module->getExportName().c_str());
break;

View File

@ -28,7 +28,7 @@ bool remove_first_if(std::vector<T, Allocator> &list, Predicate pred) {
list.erase(it);
return true;
}
it++;
++it;
}
return false;
}