From d0c96c1f359bec83cbd5e9c52c6ad12decfa55d3 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Thu, 14 Nov 2024 17:45:07 -0700 Subject: [PATCH] feat: Relocation::write_func --- include/REL/Relocation.h | 12 ++++++++++++ src/REL/Relocation.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/REL/Relocation.cpp diff --git a/include/REL/Relocation.h b/include/REL/Relocation.h index 75353221..77c0f13b 100644 --- a/include/REL/Relocation.h +++ b/include/REL/Relocation.h @@ -311,6 +311,18 @@ namespace REL safe_fill(address(), a_value, a_count); } +#ifdef SFSE_SUPPORT_XBYAK + void write_func(const std::size_t a_count, const std::uintptr_t a_dst) + requires(std::same_as); + + template + void write_func(const std::size_t a_count, const F a_dst) + requires(std::same_as) + { + write_func(a_count, stl::unrestricted_cast(a_dst)); + } +#endif + constexpr std::uintptr_t write_vfunc(const std::size_t a_idx, const std::uintptr_t a_newFunc) requires(std::same_as) { diff --git a/src/REL/Relocation.cpp b/src/REL/Relocation.cpp new file mode 100644 index 00000000..beafb6e5 --- /dev/null +++ b/src/REL/Relocation.cpp @@ -0,0 +1,32 @@ +#include "REL/Relocation.h" + +#ifdef SFSE_SUPPORT_XBYAK +# include + +namespace REL +{ + struct write_func_impl : Xbyak::CodeGenerator + { + write_func_impl(std::uintptr_t a_dst) + { + Xbyak::Label dst; + jmp(ptr[rip + dst]); + L(dst); + dq(a_dst); + } + }; + + template + void Relocation::write_func(const std::size_t a_count, const std::uintptr_t a_dst) + requires(std::same_as) + { + safe_fill(address(), INT3, a_count); + auto patch = write_func_impl{ a_dst }; + patch.ready(); + assert(patch.getSize() <= a_count); + safe_write(address(), std::span{ patch.getCode(), patch.getSize() }); + } + + template void Relocation::write_func(const std::size_t, const std::uintptr_t); +} +#endif