Skip to content

Commit

Permalink
[SLIMDETOURS] Support GCC and can be built with ReactOS
Browse files Browse the repository at this point in the history
  • Loading branch information
RatinCN committed Jul 28, 2024
1 parent 848098f commit 2701c78
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 44 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ Demo: [DelayHook.c](https://github.com/KNSoft/KNSoft.SlimDetours/blob/main/Sourc
## Compatibility
Project building: only support for the latest MSVC generation tools and SDKs is considered, but it is generally more widely backward compatible.
Project building: support for the latest MSVC generation tools and SDKs is mainly considered, and it is generally more widely backward compatible. GCC compatible and can be built with [ReactOS](https://github.com/reactos/reactos).
Artifact integration: it is widely backward compatible with MSVC generation tools (support for VS2015 is known), and different compilation configurations (e.g., `/MD`, `/MT`).
Artifact integration: widely compatible with MSVC generation tools (support for VS2015 is known), and different compilation configurations (e.g., `/MD`, `/MT`).
Runtime environment: NT6 or above OS, x86/x64/ARM64 platform.
Runtime environment: NT5 or above OS, x86/x64/ARM64 platform.
> [!CAUTION]
> In beta stage, should be used with caution.
Expand Down
6 changes: 3 additions & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ SlimDetoursDelayAttach((PVOID*)&g_pfnFuncXxx,
## 兼容性
项目构建:仅考虑对最新MSVC生成工具及SDK的支持,但一般也能较广泛地向下兼容
项目构建:主要考虑对最新MSVC生成工具及SDK的支持,同时一般也能较广泛地向下兼容。兼容GCC并可随[ReactOS](https://github.com/reactos/reactos)一同构建
制品集成:能较广泛地向下兼容MSVC生成工具(已知支持VS2015),以及不同编译配置(如`/MD`、`/MT`)。
制品集成:广泛地兼容MSVC生成工具(已知支持VS2015),以及不同编译配置(如`/MD`、`/MT`)。
运行环境:NT6及以上操作系统,x86/x64/ARM64平台。
运行环境:NT5及以上操作系统,x86/x64/ARM64平台。
> [!CAUTION]
> 处于beta阶段,应小心使用。
Expand Down
50 changes: 25 additions & 25 deletions Source/SlimDetours/Disassembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,27 @@ typedef struct _COPYENTRY
COPYFUNC pfCopy; // Function pointer.
} COPYENTRY, *PCOPYENTRY;

PBYTE CopyBytes(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyBytesPrefix(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyBytesSegment(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyBytesRax(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyBytesJump(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE Invalid(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE Copy0F(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE Copy0F78(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE Copy0F00(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE Copy0FB8(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE Copy66(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE Copy67(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyF2(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyF3(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyF6(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyF7(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyFF(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyVex3(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyVex2(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyEvex(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
PBYTE CopyXop(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyBytes(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyBytesPrefix(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyBytesSegment(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyBytesRax(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyBytesJump(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE Invalid(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE Copy0F(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE Copy0F78(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE Copy0F00(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE Copy0FB8(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE Copy66(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE Copy67(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyF2(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyF3(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyF6(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyF7(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyFF(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyVex3(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyVex2(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyEvex(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);
static PBYTE CopyXop(_In_ PDETOUR_DISASM pDisasm, _In_opt_ REFCOPYENTRY pEntry, _In_ PBYTE pbDst, _In_ PBYTE pbSrc);

///////////////////////////////////////////////////////// Disassembler Tables.
//
Expand Down Expand Up @@ -954,10 +954,10 @@ static const BYTE g_rceCopyTable0F[] =
/* FF */ eENTRY_Invalid, // _FF
};

_STATIC_ASSERT(ARRAYSIZE(g_rbModRm) == 256 &&
ARRAYSIZE(g_rceCopyMap) == eENTRY_Invalid + 1 &&
ARRAYSIZE(g_rceCopyTable) == 256 &&
ARRAYSIZE(g_rceCopyTable0F) == 256);
_STATIC_ASSERT(_countof(g_rbModRm) == 256 &&
_countof(g_rceCopyMap) == eENTRY_Invalid + 1 &&
_countof(g_rceCopyTable) == 256 &&
_countof(g_rceCopyTable0F) == 256);

/////////////////////////////////////////////////////////// Disassembler Code.
//
Expand Down
10 changes: 5 additions & 5 deletions Source/SlimDetours/Instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ detour_gen_jmp_immediate(
{
PBYTE pbJmpSrc = pbCode + 5;
*pbCode++ = 0xe9; // jmp +imm32
*((INT32*)pbCode)++ = (INT32)(pbJmpVal - pbJmpSrc);
return pbCode;
*((INT32*)pbCode) = (INT32)(pbJmpVal - pbJmpSrc);
return pbCode + sizeof(INT32);
}

_Ret_notnull_
Expand All @@ -133,11 +133,11 @@ detour_gen_jmp_indirect(
*pbCode++ = 0xff; // jmp [+imm32]
*pbCode++ = 0x25;
#if defined(_AMD64_)
*((INT32*)pbCode)++ = (INT32)((PBYTE)ppbJmpVal - pbJmpSrc);
*((INT32*)pbCode) = (INT32)((PBYTE)ppbJmpVal - pbJmpSrc);
#else
*((INT32*)pbCode)++ = (INT32)((PBYTE)ppbJmpVal);
*((INT32*)pbCode) = (INT32)((PBYTE)ppbJmpVal);
#endif
return pbCode;
return pbCode + sizeof(INT32);
}

_Ret_notnull_
Expand Down
42 changes: 42 additions & 0 deletions Source/SlimDetours/SlimDetours.NDK.Addendum.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Addendum to ReactOS NDK and helper macros on KNSoft.NDK */

#pragma once

#if defined(_X86_)
#define CONTEXT_PC Eip
#elif defined(_AMD64_)
#define CONTEXT_PC Rip
#elif defined(_ARM64_)
#define CONTEXT_PC Pc
#endif

#define Add2Ptr(P,I) ((PVOID)((PUCHAR)(P) + (I)))
#define PtrOffset(B,O) ((ULONG)((ULONG_PTR)(O) - (ULONG_PTR)(B)))

#define KB_TO_BYTES(x) ((x) * 1024UL)
#define MB_TO_KB(x) ((x) * 1024UL)
#define MB_TO_BYTES(x) (KB_TO_BYTES(MB_TO_KB(x)))
#define GB_TO_MB(x) ((x) * 1024UL)
#define GB_TO_BYTES(x) (MB_TO_BYTES(GB_TO_MB(x)))

#define MM_LOWEST_USER_ADDRESS ((PVOID)0x10000)

#if defined(_WIN64)

/* [0x00007FF7FFFF0000 ... 0x00007FFFFFFF0000], 32G */

#define MI_ASLR_BITMAP_SIZE 0x10000
#define MI_ASLR_HIGHEST_SYSTEM_RANGE_ADDRESS ((PVOID)0x00007FFFFFFF0000ULL)

#else

/* [0x50000000 ... 0x78000000], 640M */

#define MI_ASLR_BITMAP_SIZE 0x500
#define MI_ASLR_HIGHEST_SYSTEM_RANGE_ADDRESS ((PVOID)0x78000000UL)

#endif

#define NtGetCurrentProcessId() (NtCurrentTeb()->ClientId.UniqueProcess)
#define NtGetCurrentThreadId() (NtCurrentTeb()->ClientId.UniqueThread)
#define NtGetProcessHeap() (NtCurrentPeb()->ProcessHeap)
8 changes: 2 additions & 6 deletions Source/SlimDetours/SlimDetours.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
#define DETOUR_INSTRUCTION_TARGET_NONE ((PVOID)0)
#define DETOUR_INSTRUCTION_TARGET_DYNAMIC ((PVOID)(LONG_PTR)-1)

#pragma region APIs
/* APIs */

HRESULT
NTAPI
Expand Down Expand Up @@ -107,9 +107,7 @@ SlimDetoursCopyInstruction(
}
#endif

#pragma endregion

#pragma region Type - safe overloads for C++
/* Type - safe overloads for C++ */

#if __cplusplus >= 201103L || _MSVC_LANG >= 201103L
#include <type_traits>
Expand Down Expand Up @@ -165,5 +163,3 @@ SlimDetoursDelayAttach(
#endif /* (NTDDI_VERSION >= NTDDI_WIN6) */

#endif // __cplusplus >= 201103L || _MSVC_LANG >= 201103L

#pragma endregion
2 changes: 1 addition & 1 deletion Source/SlimDetours/SlimDetours.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/* Use ReactOS NDK with addendum on ReactOS */

#include "NDK_Addendum.h"
#include "SlimDetours.NDK.Addendum.inl"

#define WIN32_NO_STATUS
#include <windef.h>
Expand Down
2 changes: 1 addition & 1 deletion Source/SlimDetours/Trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ detour_alloc_trampoline(
PDETOUR_TRAMPOLINE pLo;
PDETOUR_TRAMPOLINE pHi;

detour_find_jmp_bounds(pbTarget, &pLo, &pHi);
detour_find_jmp_bounds(pbTarget, (PVOID*)&pLo, (PVOID*)&pHi);

PDETOUR_TRAMPOLINE pTrampoline = NULL;

Expand Down
4 changes: 4 additions & 0 deletions Source/SlimDetours/Transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ SlimDetoursTransactionBegin(VOID)
return HRESULT_FROM_NT(STATUS_SUCCESS);

fail:
#ifdef _MSC_VER
#pragma warning(disable: __WARNING_INTERLOCKED_ACCESS)
#endif
s_nPendingThreadId = 0;
#ifdef _MSC_VER
#pragma warning(default: __WARNING_INTERLOCKED_ACCESS)
#endif
return HRESULT_FROM_NT(Status);
}

Expand Down

0 comments on commit 2701c78

Please sign in to comment.