diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp index 153d4bbb7..8aa8ea4bf 100644 --- a/FEXCore/Source/Interface/Core/CodeCache.cpp +++ b/FEXCore/Source/Interface/Core/CodeCache.cpp @@ -2,6 +2,7 @@ #include "Utils/SpinWaitLock.h" #include +#include #include @@ -12,6 +13,10 @@ #include +#include + +#include + namespace FEXCore { ExecutableFileInfo::~ExecutableFileInfo() = default; @@ -224,4 +229,43 @@ bool CodeCache::SaveData(Core::InternalThreadState& Thread, int fd, const Execut return true; } +bool CodeCache::ApplyCodeRelocations(uint64_t GuestEntry, std::span Code, std::span EntryRelocations) { + CPU::Arm64Emitter Emitter(&CTX, Code.data(), Code.size_bytes()); + for (size_t j = 0; j < EntryRelocations.size(); ++j) { + const FEXCore::CPU::Relocation& Reloc = EntryRelocations[j]; + Emitter.SetCursorOffset(Reloc.Header.Offset); + + switch (Reloc.Header.Type) { + case FEXCore::CPU::RelocationTypes::RELOC_NAMED_SYMBOL_LITERAL: { + // Generate a literal so we can place it + uint64_t Pointer = GetNamedSymbolLiteral(CTX, Reloc.NamedSymbolLiteral.Symbol); + Emitter.dc64(Pointer); + break; + } + case FEXCore::CPU::RelocationTypes::RELOC_NAMED_THUNK_MOVE: { + uint64_t Pointer = reinterpret_cast(CTX.ThunkHandler->LookupThunk(Reloc.NamedThunkMove.Symbol)); + if (Pointer == ~0ULL) { + return false; + } + + Emitter.LoadConstant(ARMEmitter::Size::i64Bit, ARMEmitter::Register(Reloc.NamedThunkMove.RegisterIndex), Pointer, true); + break; + } + case FEXCore::CPU::RelocationTypes::RELOC_GUEST_RIP_LITERAL: { + Emitter.dc64(GuestEntry + Reloc.GuestRIP.GuestRIP); + break; + } + case FEXCore::CPU::RelocationTypes::RELOC_GUEST_RIP_MOVE: { + uint64_t Pointer = Reloc.GuestRIP.GuestRIP + GuestEntry; + Emitter.LoadConstant(ARMEmitter::Size::i64Bit, ARMEmitter::Register(Reloc.GuestRIP.RegisterIndex), Pointer, true); + break; + } + + default: ERROR_AND_DIE_FMT("Unknown relocation type {}", ToUnderlying(Reloc.Header.Type)); + } + } + + return true; +} + } // namespace FEXCore::Context diff --git a/FEXCore/Source/Interface/Core/JIT/Arm64Relocations.cpp b/FEXCore/Source/Interface/Core/JIT/Arm64Relocations.cpp index 97429c823..916bf00ce 100644 --- a/FEXCore/Source/Interface/Core/JIT/Arm64Relocations.cpp +++ b/FEXCore/Source/Interface/Core/JIT/Arm64Relocations.cpp @@ -10,8 +10,8 @@ $end_info$ #include -namespace FEXCore::Context { -static uint64_t GetNamedSymbolLiteral(ContextImpl& CTX, FEXCore::CPU::RelocNamedSymbolLiteral::NamedSymbol Op) { +namespace FEXCore::CPU { +uint64_t GetNamedSymbolLiteral(FEXCore::Context::ContextImpl& CTX, FEXCore::CPU::RelocNamedSymbolLiteral::NamedSymbol Op) { switch (Op) { case FEXCore::CPU::RelocNamedSymbolLiteral::NamedSymbol::SYMBOL_LITERAL_EXITFUNCTION_LINKER: return CTX.Dispatcher->GetExitFunctionLinkerAddress(); @@ -19,9 +19,7 @@ static uint64_t GetNamedSymbolLiteral(ContextImpl& CTX, FEXCore::CPU::RelocNamed default: ERROR_AND_DIE_FMT("Unknown named symbol literal: {}", static_cast(Op)); } } -} // namespace FEXCore::Context -namespace FEXCore::CPU { void Arm64JITCore::InsertNamedThunkRelocation(ARMEmitter::Register Reg, const IR::SHA256Sum& Sum) { Relocation MoveABI {}; MoveABI.NamedThunkMove.Header = {.Offset = GetCursorOffset(), .Type = FEXCore::CPU::RelocationTypes::RELOC_NAMED_THUNK_MOVE}; @@ -97,51 +95,6 @@ void Arm64JITCore::InsertGuestRIPMove(ARMEmitter::Register Reg, uint64_t Constan LoadConstant(ARMEmitter::Size::i64Bit, Reg, Constant, false); Relocations.emplace_back(MoveABI); } -} // namespace FEXCore::CPU - -namespace FEXCore::Context { -bool CodeCache::ApplyCodeRelocations(uint64_t GuestEntry, std::span Code, - std::span EntryRelocations) { - CPU::Arm64Emitter Emitter(&CTX, Code.data(), Code.size_bytes()); - for (size_t j = 0; j < EntryRelocations.size(); ++j) { - const FEXCore::CPU::Relocation& Reloc = EntryRelocations[j]; - Emitter.SetCursorOffset(Reloc.Header.Offset); - - switch (Reloc.Header.Type) { - case FEXCore::CPU::RelocationTypes::RELOC_NAMED_SYMBOL_LITERAL: { - // Generate a literal so we can place it - uint64_t Pointer = GetNamedSymbolLiteral(CTX, Reloc.NamedSymbolLiteral.Symbol); - Emitter.dc64(Pointer); - break; - } - case FEXCore::CPU::RelocationTypes::RELOC_NAMED_THUNK_MOVE: { - uint64_t Pointer = reinterpret_cast(CTX.ThunkHandler->LookupThunk(Reloc.NamedThunkMove.Symbol)); - if (Pointer == ~0ULL) { - return false; - } - - Emitter.LoadConstant(ARMEmitter::Size::i64Bit, ARMEmitter::Register(Reloc.NamedThunkMove.RegisterIndex), Pointer, true); - break; - } - case FEXCore::CPU::RelocationTypes::RELOC_GUEST_RIP_LITERAL: { - Emitter.dc64(GuestEntry + Reloc.GuestRIP.GuestRIP); - break; - } - case FEXCore::CPU::RelocationTypes::RELOC_GUEST_RIP_MOVE: { - uint64_t Pointer = Reloc.GuestRIP.GuestRIP + GuestEntry; - Emitter.LoadConstant(ARMEmitter::Size::i64Bit, ARMEmitter::Register(Reloc.GuestRIP.RegisterIndex), Pointer, true); - break; - } - - default: ERROR_AND_DIE_FMT("Unknown relocation type {}", ToUnderlying(Reloc.Header.Type)); - } - } - - return true; -} -} // namespace FEXCore::Context - -namespace FEXCore::CPU { fextl::vector Arm64JITCore::TakeRelocations(uint64_t GuestBaseAddress) { // Rebase relocations to library base address diff --git a/FEXCore/Source/Interface/Core/JIT/Relocations.h b/FEXCore/Source/Interface/Core/JIT/Relocations.h index b6f1e9eb4..734a5908e 100644 --- a/FEXCore/Source/Interface/Core/JIT/Relocations.h +++ b/FEXCore/Source/Interface/Core/JIT/Relocations.h @@ -3,6 +3,10 @@ #include #include +namespace FEXCore::Context { +class ContextImpl; +} + namespace FEXCore::CPU { enum class RelocationTypes : uint32_t { // 8 byte literal in memory for symbol @@ -79,4 +83,7 @@ union Relocation { RelocGuestRIP GuestRIP; }; + +uint64_t GetNamedSymbolLiteral(FEXCore::Context::ContextImpl&, RelocNamedSymbolLiteral::NamedSymbol); + } // namespace FEXCore::CPU