From 69eb1f210f74982c2b2df0432675ea195b075e35 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Thu, 14 Nov 2024 20:24:33 -0700 Subject: [PATCH] feat: Relocation::write_func (#298) --- 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 4a73f2b8..9babdb4c 100644 --- a/include/REL/Relocation.h +++ b/include/REL/Relocation.h @@ -338,6 +338,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