Remove the global trampoline table and add a small trampoline table next to each .text section instead

This commit is contained in:
Maschell
2025-01-19 10:08:14 +01:00
parent c884b17695
commit aa7eb11013
13 changed files with 252 additions and 155 deletions

View File

@@ -1,14 +1,16 @@
#include <coreinit/cache.h>
#include <cstdlib>
#include <cstring>
#include <vector>
#include "ElfUtils.h"
#include "utils/logger.h"
#include <coreinit/cache.h>
#include <span>
#include <cstdlib>
#include <cstring>
// See https://github.com/decaf-emu/decaf-emu/blob/43366a34e7b55ab9d19b2444aeb0ccd46ac77dea/src/libdecaf/src/cafe/loader/cafe_loader_reloc.cpp#L144
bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr,
std::vector<relocation_trampoline_entry_t> &trampolineData, RelocationType reloc_type, uint8_t trampolineId) {
std::span<relocation_trampoline_entry_t> trampolineData, RelocationType reloc_type) {
if (type == R_PPC_NONE) {
return true;
}
@@ -85,11 +87,10 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
relocation_trampoline_entry_t *freeSlot = nullptr;
for (auto &cur : trampolineData) {
// We want to override "old" relocations of imports
// Pending relocations have the status RELOC_TRAMP_IMPORT_IN_PROGRESS.
// When all relocations are done successfully, they will be turned into RELOC_TRAMP_IMPORT_DONE
// so they can be overridden/updated/reused on the next application launch.
//
// Relocations that won't change will have the status RELOC_TRAMP_FIXED and are set to free when the module is unloaded.
// Relocations that won't change will have the status RELOC_TRAMP_FIXED and are set to free when the plugin is unloaded.
if (cur.status == RELOC_TRAMP_FREE) {
freeSlot = &cur;
break;
@@ -119,9 +120,6 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
freeSlot->trampoline[3] = 0x4E800420; // bctr
ICInvalidateRange((unsigned char *) freeSlot->trampoline, sizeof(freeSlot->trampoline));
freeSlot->id = trampolineId;
ICInvalidateRange((unsigned char *) &freeSlot->id, sizeof(freeSlot->id));
if (reloc_type == RELOC_TYPE_FIXED) {
freeSlot->status = RELOC_TRAMP_FIXED;
} else {

View File

@@ -2,6 +2,8 @@
#include <wums/defines/relocation_defines.h>
#include <span>
#include <cstdint>
#ifdef __cplusplus
@@ -47,6 +49,6 @@ uint32_t load_loader_elf(unsigned char *baseAddress, char *elf_data, uint32_t fi
class ElfUtils {
public:
static bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, std::vector<relocation_trampoline_entry_t> &trampolineData,
RelocationType reloc_type, uint8_t trampolineId);
static bool elfLinkOne(char type, size_t offset, int32_t addend, uint32_t destination, uint32_t symbol_addr, std::span<relocation_trampoline_entry_t> trampolineData,
RelocationType reloc_type);
};

View File

@@ -1,33 +1,69 @@
#include "HeapMemoryFixedSize.h"
#include "logger.h"
#include "utils.h"
HeapMemoryFixedSize::HeapMemoryFixedSize() = default;
HeapMemoryFixedSizePool::MemorySegmentInfo::MemorySegmentInfo(void *data, const size_t size) : mData(data),
mSize(size) {}
HeapMemoryFixedSize::HeapMemoryFixedSize(const std::size_t size) : mData(make_unique_nothrow<uint8_t[]>(size)), mSize(mData ? size : 0) {}
HeapMemoryFixedSize::HeapMemoryFixedSize(HeapMemoryFixedSize &&other) noexcept
: mData(std::move(other.mData)), mSize(other.mSize) {
other.mSize = 0;
void *HeapMemoryFixedSizePool::MemorySegmentInfo::data() const {
return mData;
}
HeapMemoryFixedSize &HeapMemoryFixedSize::operator=(HeapMemoryFixedSize &&other) noexcept {
size_t HeapMemoryFixedSizePool::MemorySegmentInfo::size() const {
return mSize;
}
HeapMemoryFixedSizePool::HeapMemoryFixedSizePool() = default;
HeapMemoryFixedSizePool::HeapMemoryFixedSizePool(const std::initializer_list<size_t> segmentSizes) : HeapMemoryFixedSizePool(std::span(segmentSizes.begin(), segmentSizes.size())) {}
HeapMemoryFixedSizePool::HeapMemoryFixedSizePool(std::span<const std::size_t> segmentSizes) {
assert(!segmentSizes.empty());
size_t totalSize = 0;
for (const auto size : segmentSizes) {
totalSize += size + 0x40; // add 0x40 bytes overhead for each entry to ensure padding to 0x40
}
mData = make_unique_nothrow<uint8_t[]>(totalSize);
mTotalSize = (mData ? totalSize : 0);
if (mData) {
auto address = reinterpret_cast<uint32_t>(mData.get());
for (const auto size : segmentSizes) {
address = ROUNDUP(address, 0x40);
assert(address >= reinterpret_cast<uint32_t>(mData.get()) && address < reinterpret_cast<uint32_t>(mData.get()) + totalSize);
mSegmentInfos.emplace_back(reinterpret_cast<void *>(address), size);
address += size;
}
}
}
HeapMemoryFixedSizePool::HeapMemoryFixedSizePool(HeapMemoryFixedSizePool &&other) noexcept
: mData(std::move(other.mData)), mTotalSize(other.mTotalSize), mSegmentInfos(std::move(other.mSegmentInfos)) {
other.mTotalSize = 0;
}
HeapMemoryFixedSizePool &HeapMemoryFixedSizePool::operator=(HeapMemoryFixedSizePool &&other) noexcept {
if (this != &other) {
mData = std::move(other.mData);
mSize = other.mSize;
other.mSize = 0;
mData = std::move(other.mData);
mTotalSize = other.mTotalSize;
mSegmentInfos = std::move(other.mSegmentInfos);
other.mTotalSize = 0;
}
return *this;
}
HeapMemoryFixedSize::operator bool() const {
return mData != nullptr;
uint32_t HeapMemoryFixedSizePool::numberOfSegments() const {
return mSegmentInfos.size();
}
[[nodiscard]] const void *HeapMemoryFixedSize::data() const {
return mData.get();
HeapMemoryFixedSizePool::operator bool() const {
return mData != nullptr && mTotalSize > 0;
}
[[nodiscard]] std::size_t HeapMemoryFixedSize::size() const {
return mSize;
HeapMemoryFixedSizePool::MemorySegmentInfo HeapMemoryFixedSizePool::operator[](const int idx) const {
if (idx < 0 || idx >= static_cast<int>(mSegmentInfos.size())) {
DEBUG_FUNCTION_LINE_ERR("Out of bounce access (tried to access index %d; size is", idx, mSegmentInfos.size());
}
return mSegmentInfos[idx];
}

View File

@@ -1,30 +1,45 @@
#pragma once
#include <memory>
#include <span>
#include <vector>
#include <cstdint>
class HeapMemoryFixedSize {
class HeapMemoryFixedSizePool {
public:
HeapMemoryFixedSize();
class MemorySegmentInfo {
public:
MemorySegmentInfo(void *data, size_t size);
explicit HeapMemoryFixedSize(std::size_t size);
[[nodiscard]] void *data() const;
[[nodiscard]] size_t size() const;
private:
void *mData;
size_t mSize;
};
HeapMemoryFixedSizePool();
HeapMemoryFixedSizePool(std::initializer_list<size_t> segmentSizes);
HeapMemoryFixedSizePool(std::span<const std::size_t> segmentSizes);
// Delete the copy constructor and copy assignment operator
HeapMemoryFixedSize(const HeapMemoryFixedSize &) = delete;
HeapMemoryFixedSize &operator=(const HeapMemoryFixedSize &) = delete;
HeapMemoryFixedSizePool(const HeapMemoryFixedSizePool &) = delete;
HeapMemoryFixedSizePool &operator=(const HeapMemoryFixedSizePool &) = delete;
HeapMemoryFixedSize(HeapMemoryFixedSize &&other) noexcept;
HeapMemoryFixedSizePool(HeapMemoryFixedSizePool &&other) noexcept;
HeapMemoryFixedSize &operator=(HeapMemoryFixedSize &&other) noexcept;
HeapMemoryFixedSizePool &operator=(HeapMemoryFixedSizePool &&other) noexcept;
[[nodiscard]] uint32_t numberOfSegments() const;
explicit operator bool() const;
[[nodiscard]] const void *data() const;
[[nodiscard]] std::size_t size() const;
MemorySegmentInfo operator[](int idx) const;
private:
std::unique_ptr<uint8_t[]> mData{};
std::size_t mSize{};
};
std::size_t mTotalSize{};
std::vector<MemorySegmentInfo> mSegmentInfos;
};