feat(dhooks): add Linux x86_64 support (#9)

Co-authored-by: rtldg <[email protected]>
Co-authored-by: WareZ57 <[email protected]>
(cherry picked from commit 14dc7f93095105a773a6f5b59ddd2d4fe77c697a)
This commit is contained in:
Cmer
2026-11-12 22:22:45 +01:00
committed by Rushaway
parent 292273002b
commit da3223bf41
13 changed files with 936 additions and 34 deletions
+40 -6
View File
@@ -296,6 +296,11 @@ void CHook::CreateBridge()
{
auto& jit = m_bridge;
// Generate the post-hook code first. Both buffers come from the same page allocator,
// and a GenBuffer that grows marks its old block (so the whole page) read/execute:
// growing one while the other is half written makes the next write fault.
CreatePostCallback();
//jit.breakpoint();
PrintRegisters(jit);
@@ -364,6 +369,8 @@ void CHook::Write_ModifyReturnAddress(x64JitWriter& jit)
// Shadow space 32 bytes + 8 bytes to keep it aligned on 16 bytes
MSVC_ONLY(jit.sub(rsp, 40));
// We need to keep it aligned to 16 bytes on Linux too...
GCC_ONLY(jit.sub(rsp, 8));
// 1st param (this)
GCC_ONLY(jit.mov(rdi, reinterpret_cast<std::uint64_t>(this)));
@@ -381,11 +388,13 @@ void CHook::Write_ModifyReturnAddress(x64JitWriter& jit)
jit.mov(rax, func.address);
jit.call(rax);
// Free Linux stack alignemnt
GCC_ONLY(jit.add(rsp, 8));
// Free shadow space
MSVC_ONLY(jit.add(rsp, 40));
// Override the return address. This is a redirect to our post-hook code
CreatePostCallback();
// (generated by CreateBridge beforehand).
jit.mov(rax, reinterpret_cast<std::uint64_t>(&m_pNewRetAddr));
jit.mov(rax, rax());
jit.mov(rsp(), rax);
@@ -410,6 +419,17 @@ void CHook::CreatePostCallback()
// Restore the previously saved registers, so any changes will be applied
Write_RestoreRegisters(jit, HOOKTYPE_POST);
// The return value (rax, rdx, xmm0, xmm1) must reach the caller untouched: GetReturnAddress is a
// C call that overwrites them, and rax then held the return address, so every non-void detoured
// function returned it (a "true" bool) to its caller. Kept on the stack around the call (48 bytes:
// the key passed to GetReturnAddress, rsp as SetReturnAddress stored it, is now rsp + 48), and the
// jump back goes through r11 (caller-saved, never a return register).
jit.push(rax);
jit.push(rdx);
jit.sub(rsp, 32);
jit.movsd(rsp(), xmm0);
jit.movsd(rsp(16), xmm1);
// Get return address
union
{
@@ -420,25 +440,35 @@ void CHook::CreatePostCallback()
// Shadow space 32 bytes + 8 bytes to keep it aligned on 16 bytes
MSVC_ONLY(jit.sub(rsp, 40));
// We need to keep it aligned to 16 bytes on Linux too...
GCC_ONLY(jit.sub(rsp, 8));
// 1st param (this)
GCC_ONLY(jit.mov(rdi, reinterpret_cast<std::uint64_t>(this)));
MSVC_ONLY(jit.mov(rcx, reinterpret_cast<std::uint64_t>(this)));
// 2n parameter (rsp)
GCC_ONLY(jit.lea(rsi, rsp()));
MSVC_ONLY(jit.lea(rdx, rsp(40)));
// 2nd parameter (rsp)
GCC_ONLY(jit.lea(rsi, rsp(48)));
MSVC_ONLY(jit.lea(rdx, rsp(40 + 48)));
// Call GetReturnAddress
jit.mov(rax, func.address);
jit.call(rax);
// Free Linux stack alignemnt
GCC_ONLY(jit.add(rsp, 8));
// Free shadow space
MSVC_ONLY(jit.add(rsp, 40));
// Jump to the original return address
// Back the return value, then jump to the original return address
jit.mov(r11, rax);
jit.movsd(xmm0, rsp());
jit.movsd(xmm1, rsp(16));
jit.add(rsp, 32);
jit.pop(rdx);
jit.pop(rax);
jit.add(rsp, 8);
jit.jump(rax);
jit.jump(r11);
}
void CHook::Write_CallHandler(x64JitWriter& jit, HookType_t type)
@@ -453,6 +483,8 @@ void CHook::Write_CallHandler(x64JitWriter& jit, HookType_t type)
// Shadow space 32 bytes + 8 bytes to keep it aligned on 16 bytes
MSVC_ONLY(jit.sub(rsp, 40));
// We need to keep it aligned to 16 bytes on Linux too...
GCC_ONLY(jit.sub(rsp, 8));
// Call the global hook handler
@@ -467,6 +499,8 @@ void CHook::Write_CallHandler(x64JitWriter& jit, HookType_t type)
jit.mov(rax, func.address);
jit.call(rax);
// Free Linux stack alignemnt
GCC_ONLY(jit.add(rsp, 8));
// Free shadow space
MSVC_ONLY(jit.add(rsp, 40));
}