Skip to content

Commit

Permalink
feat: Relocation::write_func (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
shad0wshayd3 authored Nov 15, 2024
1 parent fe09e4d commit 69eb1f2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/REL/Relocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<value_type, std::uintptr_t>);

template <class F>
void write_func(const std::size_t a_count, const F a_dst)
requires(std::same_as<value_type, std::uintptr_t>)
{
write_func(a_count, stl::unrestricted_cast<std::uintptr_t>(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<value_type, std::uintptr_t>)
{
Expand Down
32 changes: 32 additions & 0 deletions src/REL/Relocation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "REL/Relocation.h"

#ifdef SFSE_SUPPORT_XBYAK
# include <xbyak/xbyak.h>

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 <class T>
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>)
{
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<const std::byte*>(), patch.getSize() });
}

template void Relocation<std::uintptr_t>::write_func(const std::size_t, const std::uintptr_t);
}
#endif

0 comments on commit 69eb1f2

Please sign in to comment.