From 76e826dc759b95eed50f6c1b57f14a82b1a355b4 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Thu, 14 Nov 2024 17:46:40 -0700 Subject: [PATCH] feat: Relocation::write_func --- include/REL/Relocation.h | 10 ++++++++++ src/REL/Relocation.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/REL/Relocation.h b/include/REL/Relocation.h index 8f07112bc..7d1bd97c5 100644 --- a/include/REL/Relocation.h +++ b/include/REL/Relocation.h @@ -323,6 +323,16 @@ namespace REL safe_fill(address(), a_value, a_count); } +#ifdef SKSE_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 + template 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 index 4ce831bde..b41629390 100644 --- a/src/REL/Relocation.cpp +++ b/src/REL/Relocation.cpp @@ -32,3 +32,33 @@ namespace REL assert(success); } } + +#ifdef SKSE_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