Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Relocation::write_func #31

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CommonLibF4/include/REL/Relocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<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

template <class U = value_type>
std::uintptr_t write_vfunc(std::size_t a_idx, std::uintptr_t a_newFunc) requires(std::same_as<U, std::uintptr_t>)
{
Expand Down
30 changes: 30 additions & 0 deletions CommonLibF4/src/REL/Relocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,33 @@ namespace REL
assert(success);
}
}

#ifdef F4SE_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
Loading