diff --git a/wumsloader/src/globals.cpp b/wumsloader/src/globals.cpp index 399ab08..a20e829 100644 --- a/wumsloader/src/globals.cpp +++ b/wumsloader/src/globals.cpp @@ -1,4 +1,5 @@ #include "globals.h" +#include "module/ModuleData.h" MEMHeapHandle gHeapHandle __attribute__((section(".data"))) = nullptr; uint8_t gInitCalled __attribute__((section(".data"))) = 0; diff --git a/wumsloader/src/globals.h b/wumsloader/src/globals.h index 1621d18..70e5709 100644 --- a/wumsloader/src/globals.h +++ b/wumsloader/src/globals.h @@ -1,10 +1,17 @@ #pragma once -#include "module/ModuleData.h" +#include + #include #include -#include + #include +#include +#include + +#include + +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) diff --git a/wumsloader/src/module/FunctionSymbolData.cpp b/wumsloader/src/module/FunctionSymbolData.cpp new file mode 100644 index 0000000..1692bf0 --- /dev/null +++ b/wumsloader/src/module/FunctionSymbolData.cpp @@ -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(mAddress) < reinterpret_cast(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; +} \ No newline at end of file diff --git a/wumsloader/src/module/FunctionSymbolData.h b/wumsloader/src/module/FunctionSymbolData.h index 2bb074a..6daa3d9 100644 --- a/wumsloader/src/module/FunctionSymbolData.h +++ b/wumsloader/src/module/FunctionSymbolData.h @@ -18,37 +18,29 @@ #pragma once #include +#include + +#include 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; }; \ No newline at end of file diff --git a/wumsloader/src/module/HookData.cpp b/wumsloader/src/module/HookData.cpp new file mode 100644 index 0000000..8cc83e2 --- /dev/null +++ b/wumsloader/src/module/HookData.cpp @@ -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; +} \ No newline at end of file diff --git a/wumsloader/src/module/HookData.h b/wumsloader/src/module/HookData.h index e2905c8..e590ced 100644 --- a/wumsloader/src/module/HookData.h +++ b/wumsloader/src/module/HookData.h @@ -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 . + ****************************************************************************/ + #pragma once -#include +#include 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; -}; \ No newline at end of file + void *mFunctionPointer; + wums_hook_type_t mType; +}; diff --git a/wumsloader/src/module/ImportRPLInformation.cpp b/wumsloader/src/module/ImportRPLInformation.cpp new file mode 100644 index 0000000..b22fb38 --- /dev/null +++ b/wumsloader/src/module/ImportRPLInformation.cpp @@ -0,0 +1,20 @@ +#include "ImportRPLInformation.h" +#include + +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_"); +} \ No newline at end of file diff --git a/wumsloader/src/module/ImportRPLInformation.h b/wumsloader/src/module/ImportRPLInformation.h index 0c19bb4..14afbf7 100644 --- a/wumsloader/src/module/ImportRPLInformation.h +++ b/wumsloader/src/module/ImportRPLInformation.h @@ -17,37 +17,22 @@ #pragma once -#include "utils/logger.h" -#include -#include -#include #include -#include +#include 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; }; diff --git a/wumsloader/src/module/ModuleData.h b/wumsloader/src/module/ModuleData.h index 7dbbbdc..7f02b90 100644 --- a/wumsloader/src/module/ModuleData.h +++ b/wumsloader/src/module/ModuleData.h @@ -22,7 +22,9 @@ #include "HookData.h" #include "RelocationData.h" #include "SectionInfo.h" + #include +#include #include #include #include @@ -55,7 +57,7 @@ public: } void addRelocationData(std::unique_ptr relocation_data) { - addDependency(relocation_data->getImportRPLInformation()->getRPLName()); + addDependency(relocation_data->getImportRPLInformation().getRPLName()); relocation_data_list.push_back(std::move(relocation_data)); } diff --git a/wumsloader/src/module/ModuleDataFactory.cpp b/wumsloader/src/module/ModuleDataFactory.cpp index 0f21442..6c17fc4 100644 --- a/wumsloader/src/module/ModuleDataFactory.cpp +++ b/wumsloader/src/module/ModuleDataFactory.cpp @@ -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 #include #include +#include #include #include @@ -276,7 +278,7 @@ std::optional> 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(hook->type, hook->target); + auto hookData = make_unique_nothrow(const_cast(hook->target), hook->type); if (!hookData) { DEBUG_FUNCTION_LINE_ERR("Failed to alloc HookData"); return {}; diff --git a/wumsloader/src/module/ModuleDataPersistence.cpp b/wumsloader/src/module/ModuleDataPersistence.cpp index 62b446d..8a3f96a 100644 --- a/wumsloader/src/module/ModuleDataPersistence.cpp +++ b/wumsloader/src/module/ModuleDataPersistence.cpp @@ -1,5 +1,6 @@ #include "ModuleDataPersistence.h" #include "globals.h" +#include "utils/logger.h" #include "utils/utils.h" #include @@ -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 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(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; } } diff --git a/wumsloader/src/module/RelocationData.cpp b/wumsloader/src/module/RelocationData.cpp new file mode 100644 index 0000000..b51b026 --- /dev/null +++ b/wumsloader/src/module/RelocationData.cpp @@ -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 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; +} \ No newline at end of file diff --git a/wumsloader/src/module/RelocationData.h b/wumsloader/src/module/RelocationData.h index 0c66255..8b45db7 100644 --- a/wumsloader/src/module/RelocationData.h +++ b/wumsloader/src/module/RelocationData.h @@ -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 #include -#include + +#include class RelocationData { public: - RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, std::shared_ptr 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 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 &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 rplInfo; + char mType; + size_t mOffset; + int32_t mAddend; + void *mDestination; + std::string mName; + std::shared_ptr mRPLInfo; }; diff --git a/wumsloader/src/module/SectionInfo.cpp b/wumsloader/src/module/SectionInfo.cpp new file mode 100644 index 0000000..f109220 --- /dev/null +++ b/wumsloader/src/module/SectionInfo.cpp @@ -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; +} \ No newline at end of file diff --git a/wumsloader/src/module/SectionInfo.h b/wumsloader/src/module/SectionInfo.h index d96fa54..13fae4f 100644 --- a/wumsloader/src/module/SectionInfo.h +++ b/wumsloader/src/module/SectionInfo.h @@ -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 -#include + +#include 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 = {}; }; diff --git a/wumsloader/src/utils/RelocationUtils.cpp b/wumsloader/src/utils/RelocationUtils.cpp index da65e0c..16f0934 100644 --- a/wumsloader/src/utils/RelocationUtils.cpp +++ b/wumsloader/src/utils/RelocationUtils.cpp @@ -1,6 +1,7 @@ #include "RelocationUtils.h" #include "ElfUtils.h" #include "globals.h" +#include "logger.h" #include "memory.h" #include #include @@ -83,7 +84,7 @@ bool doRelocation(const std::vector> &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> &moduleList, } if (functionAddress == 0) { - int32_t isData = curReloc->getImportRPLInformation()->isData(); + int32_t isData = curReloc->getImportRPLInformation().isData(); OSDynLoad_Module rplHandle = nullptr; if (!usedRPls.contains(rplName)) { diff --git a/wumsloader/src/utils/hooks.cpp b/wumsloader/src/utils/hooks.cpp index 246dda3..6828e67 100644 --- a/wumsloader/src/utils/hooks.cpp +++ b/wumsloader/src/utils/hooks.cpp @@ -3,6 +3,7 @@ #include "module/ModuleData.h" #include "utils/logger.h" #include +#include #include #include @@ -64,7 +65,7 @@ void CallHook(const std::shared_ptr &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; diff --git a/wumsloader/src/utils/utils.h b/wumsloader/src/utils/utils.h index f25451e..11704d7 100644 --- a/wumsloader/src/utils/utils.h +++ b/wumsloader/src/utils/utils.h @@ -28,7 +28,7 @@ bool remove_first_if(std::vector &list, Predicate pred) { list.erase(it); return true; } - it++; + ++it; } return false; } \ No newline at end of file