Skip to content

Commit d0c96c1

Browse files
committed
feat: Relocation::write_func
1 parent a835bef commit d0c96c1

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

include/REL/Relocation.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,18 @@ namespace REL
311311
safe_fill(address(), a_value, a_count);
312312
}
313313

314+
#ifdef SFSE_SUPPORT_XBYAK
315+
void write_func(const std::size_t a_count, const std::uintptr_t a_dst)
316+
requires(std::same_as<value_type, std::uintptr_t>);
317+
318+
template <class F>
319+
void write_func(const std::size_t a_count, const F a_dst)
320+
requires(std::same_as<value_type, std::uintptr_t>)
321+
{
322+
write_func(a_count, stl::unrestricted_cast<std::uintptr_t>(a_dst));
323+
}
324+
#endif
325+
314326
constexpr std::uintptr_t write_vfunc(const std::size_t a_idx, const std::uintptr_t a_newFunc)
315327
requires(std::same_as<value_type, std::uintptr_t>)
316328
{

src/REL/Relocation.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "REL/Relocation.h"
2+
3+
#ifdef SFSE_SUPPORT_XBYAK
4+
# include <xbyak/xbyak.h>
5+
6+
namespace REL
7+
{
8+
struct write_func_impl : Xbyak::CodeGenerator
9+
{
10+
write_func_impl(std::uintptr_t a_dst)
11+
{
12+
Xbyak::Label dst;
13+
jmp(ptr[rip + dst]);
14+
L(dst);
15+
dq(a_dst);
16+
}
17+
};
18+
19+
template <class T>
20+
void Relocation<T>::write_func(const std::size_t a_count, const std::uintptr_t a_dst)
21+
requires(std::same_as<value_type, std::uintptr_t>)
22+
{
23+
safe_fill(address(), INT3, a_count);
24+
auto patch = write_func_impl{ a_dst };
25+
patch.ready();
26+
assert(patch.getSize() <= a_count);
27+
safe_write(address(), std::span{ patch.getCode<const std::byte*>(), patch.getSize() });
28+
}
29+
30+
template void Relocation<std::uintptr_t>::write_func(const std::size_t, const std::uintptr_t);
31+
}
32+
#endif

0 commit comments

Comments
 (0)