Skip to content

Commit 997ce59

Browse files
committed
refactor: Relocation::replace_func
renamed from write_func, removed xbyak dependency
1 parent d583832 commit 997ce59

2 files changed

Lines changed: 31 additions & 40 deletions

File tree

CommonLibF4/include/REL/Relocation.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,37 @@ namespace REL
278278
return stl::unrestricted_cast<value_type>(_impl);
279279
}
280280

281+
template <std::ptrdiff_t O = 0>
282+
void replace_func(const std::size_t a_count, const std::uintptr_t a_dst) requires(std::same_as<value_type, std::uintptr_t>)
283+
{
284+
#pragma pack(push, 1)
285+
struct Assembly
286+
{
287+
std::uint8_t jmp;
288+
std::uint8_t modrm;
289+
std::int32_t disp;
290+
std::uint64_t addr;
291+
};
292+
static_assert(sizeof(Assembly) == 0xE);
293+
#pragma pack(pop)
294+
295+
Assembly assembly{
296+
.jmp = static_cast<std::uint8_t>(0xFF),
297+
.modrm = static_cast<std::uint8_t>(0x25),
298+
.disp = static_cast<std::int32_t>(0),
299+
.addr = static_cast<std::uint64_t>(a_dst),
300+
};
301+
302+
safe_fill(address() + O, INT3, a_count);
303+
safe_write(address() + O, &assembly, sizeof(assembly));
304+
}
305+
306+
template <std::ptrdiff_t O = 0, class F>
307+
void replace_func(const std::size_t a_count, const F a_dst) requires(std::same_as<value_type, std::uintptr_t>)
308+
{
309+
replace_func<O>(a_count, stl::unrestricted_cast<std::uintptr_t>(a_dst));
310+
}
311+
281312
void write(const void* a_src, std::size_t a_count) requires(std::same_as<value_type, std::uintptr_t>)
282313
{
283314
safe_write(address(), a_src, a_count);
@@ -329,16 +360,6 @@ namespace REL
329360
safe_fill(address(), a_value, a_count);
330361
}
331362

332-
#ifdef F4SE_SUPPORT_XBYAK
333-
void write_func(const std::size_t a_count, const std::uintptr_t a_dst) requires(std::same_as<value_type, std::uintptr_t>);
334-
335-
template <class F>
336-
void write_func(const std::size_t a_count, const F a_dst) requires(std::same_as<value_type, std::uintptr_t>)
337-
{
338-
write_func(a_count, stl::unrestricted_cast<std::uintptr_t>(a_dst));
339-
}
340-
#endif
341-
342363
template <class U = value_type>
343364
std::uintptr_t write_vfunc(std::size_t a_idx, std::uintptr_t a_newFunc) requires(std::same_as<U, std::uintptr_t>)
344365
{

CommonLibF4/src/REL/Relocation.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,3 @@ namespace REL
3232
assert(success);
3333
}
3434
}
35-
36-
#ifdef F4SE_SUPPORT_XBYAK
37-
# include <xbyak/xbyak.h>
38-
39-
namespace REL
40-
{
41-
struct write_func_impl : Xbyak::CodeGenerator
42-
{
43-
write_func_impl(std::uintptr_t a_dst)
44-
{
45-
Xbyak::Label dst;
46-
jmp(ptr[rip + dst]);
47-
L(dst);
48-
dq(a_dst);
49-
}
50-
};
51-
52-
template <class T>
53-
void Relocation<T>::write_func(const std::size_t a_count, const std::uintptr_t a_dst) requires(std::same_as<value_type, std::uintptr_t>)
54-
{
55-
safe_fill(address(), INT3, a_count);
56-
auto patch = write_func_impl{ a_dst };
57-
patch.ready();
58-
assert(patch.getSize() <= a_count);
59-
safe_write(address(), std::span{ patch.getCode<const std::byte*>(), patch.getSize() });
60-
}
61-
62-
template void Relocation<std::uintptr_t>::write_func(const std::size_t, const std::uintptr_t);
63-
}
64-
#endif

0 commit comments

Comments
 (0)