From dd664ad8681d32989866f60e5abeb8419704820a Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Thu, 14 Nov 2024 17:24:41 -0700 Subject: [PATCH 1/2] feat: Relocation::write_func requires xbyak --- CommonLibF4/include/REL/Relocation.h | 10 +++++++++ CommonLibF4/src/REL/Relocation.cpp | 31 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/CommonLibF4/include/REL/Relocation.h b/CommonLibF4/include/REL/Relocation.h index b58f124a..cac01367 100644 --- a/CommonLibF4/include/REL/Relocation.h +++ b/CommonLibF4/include/REL/Relocation.h @@ -329,6 +329,16 @@ namespace REL safe_fill(address(), a_value, a_count); } +#ifdef F4SE_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(std::size_t a_idx, std::uintptr_t a_newFunc) requires(std::same_as) { diff --git a/CommonLibF4/src/REL/Relocation.cpp b/CommonLibF4/src/REL/Relocation.cpp index 4ce831bd..3b8b39c6 100644 --- a/CommonLibF4/src/REL/Relocation.cpp +++ b/CommonLibF4/src/REL/Relocation.cpp @@ -32,3 +32,34 @@ namespace REL assert(success); } } + +#ifdef F4SE_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 From 327cd150fa19013d808e1d4ccb3fe044ae6d6628 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Fri, 15 Nov 2024 00:25:10 +0000 Subject: [PATCH 2/2] maintenance --- CommonLibF4/src/REL/Relocation.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CommonLibF4/src/REL/Relocation.cpp b/CommonLibF4/src/REL/Relocation.cpp index 3b8b39c6..2266b261 100644 --- a/CommonLibF4/src/REL/Relocation.cpp +++ b/CommonLibF4/src/REL/Relocation.cpp @@ -50,8 +50,7 @@ namespace REL }; template - void Relocation::write_func(const std::size_t a_count, const std::uintptr_t a_dst) - requires(std::same_as) + 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 };