From e0d02e748c23fa83f8a441ba5a318bb35bb64613 Mon Sep 17 00:00:00 2001 From: Ratin Gao Date: Sun, 28 Jul 2024 11:33:42 +0800 Subject: [PATCH] [SLIMDETOURS] Support GCC and can be built with ReactOS --- README.md | 6 ++-- README.zh-CN.md | 6 ++-- Source/SlimDetours/Disassembler.c | 50 +++++++++++++++---------------- Source/SlimDetours/Instruction.c | 10 +++---- Source/SlimDetours/SlimDetours.h | 8 ++--- Source/SlimDetours/Trampoline.c | 2 +- Source/SlimDetours/Transaction.c | 4 +++ 7 files changed, 43 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 3270d6e..716b262 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/README.zh-CN.md b/README.zh-CN.md index eb38aaf..73fab07 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -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阶段,应小心使用。 diff --git a/Source/SlimDetours/Disassembler.c b/Source/SlimDetours/Disassembler.c index a423b85..18df882 100644 --- a/Source/SlimDetours/Disassembler.c +++ b/Source/SlimDetours/Disassembler.c @@ -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. // @@ -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. // diff --git a/Source/SlimDetours/Instruction.c b/Source/SlimDetours/Instruction.c index 287540a..8fa9f17 100644 --- a/Source/SlimDetours/Instruction.c +++ b/Source/SlimDetours/Instruction.c @@ -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_ @@ -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_ diff --git a/Source/SlimDetours/SlimDetours.h b/Source/SlimDetours/SlimDetours.h index 02bd39c..602798d 100644 --- a/Source/SlimDetours/SlimDetours.h +++ b/Source/SlimDetours/SlimDetours.h @@ -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 @@ -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 @@ -165,5 +163,3 @@ SlimDetoursDelayAttach( #endif /* (NTDDI_VERSION >= NTDDI_WIN6) */ #endif // __cplusplus >= 201103L || _MSVC_LANG >= 201103L - -#pragma endregion diff --git a/Source/SlimDetours/Trampoline.c b/Source/SlimDetours/Trampoline.c index f3e7b07..aad5de0 100644 --- a/Source/SlimDetours/Trampoline.c +++ b/Source/SlimDetours/Trampoline.c @@ -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; diff --git a/Source/SlimDetours/Transaction.c b/Source/SlimDetours/Transaction.c index ecb2c1c..2ad668b 100644 --- a/Source/SlimDetours/Transaction.c +++ b/Source/SlimDetours/Transaction.c @@ -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); }