diff options
author | Tsuda Kageyu <tsuda.kageyu@gmail.com> | 2015-05-25 10:34:15 +0300 |
---|---|---|
committer | Tsuda Kageyu <tsuda.kageyu@gmail.com> | 2015-05-25 10:34:15 +0300 |
commit | 0b51a8f4ffdd304148b96f2a445b03e9675d1d14 (patch) | |
tree | 3fbf61d7013923010d1d84ee381945ef966f0be8 | |
parent | e39251c0c990e5c480ab902e0b7bfee0a8879197 (diff) | |
parent | 20922427295daf22b0ce798dcd891738c6e319cc (diff) |
Merge pull request #17 from ZLOFENIX/master
Support for MinGW, small fixes.
-rw-r--r-- | build/MinGW/make.bat | 1 | ||||
-rw-r--r-- | include/MinHook.h | 3 | ||||
-rw-r--r-- | src/HDE/hde32.c | 4 | ||||
-rw-r--r-- | src/HDE/hde64.c | 4 | ||||
-rw-r--r-- | src/buffer.c | 5 | ||||
-rw-r--r-- | src/hook.c | 40 | ||||
-rw-r--r-- | src/trampoline.c | 13 |
7 files changed, 65 insertions, 5 deletions
diff --git a/build/MinGW/make.bat b/build/MinGW/make.bat new file mode 100644 index 0000000..7671878 --- /dev/null +++ b/build/MinGW/make.bat @@ -0,0 +1 @@ +windres -i ../../dll_resources/MinHook.rc -o MinHook_rc.o && dllwrap --driver-name g++ -o MinHook.dll -masm=intel --def ../../dll_resources/MinHook.def -Wl,-enable-stdcall-fixup -Wall MinHook_rc.o ../../src/*.c ../../src/HDE/*.c -I../../include -I../../src -Werror -std=c++11 -s -static-libgcc -static-libstdc++|| pause
\ No newline at end of file diff --git a/include/MinHook.h b/include/MinHook.h index aa6763b..f389d0f 100644 --- a/include/MinHook.h +++ b/include/MinHook.h @@ -121,8 +121,9 @@ extern "C" { // ppOriginal [out] A pointer to the trampoline function, which will be // used to call the original target function. // This parameter can be NULL. + // autoLoad [in] Will load module if not loaded MH_STATUS WINAPI MH_CreateHookApi( - LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal); + LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal, bool autoLoad = false); // Removes an already created hook. // Parameters: diff --git a/src/HDE/hde32.c b/src/HDE/hde32.c index 55ade81..c3c62e8 100644 --- a/src/HDE/hde32.c +++ b/src/HDE/hde32.c @@ -14,7 +14,11 @@ unsigned int hde32_disasm(const void *code, hde32s *hs) uint8_t *ht = hde32_table, m_mod, m_reg, m_rm, disp_size = 0; // Avoid using memset to reduce the footprint. +#ifndef _MSC_VER + memset((LPBYTE)hs, 0, sizeof(hde32s)); +#else __stosb((LPBYTE)hs, 0, sizeof(hde32s)); +#endif for (x = 16; x; x--) switch (c = *p++) { diff --git a/src/HDE/hde64.c b/src/HDE/hde64.c index dfe5427..72f9517 100644 --- a/src/HDE/hde64.c +++ b/src/HDE/hde64.c @@ -15,7 +15,11 @@ unsigned int hde64_disasm(const void *code, hde64s *hs) uint8_t op64 = 0; // Avoid using memset to reduce the footprint. +#ifndef _MSC_VER + memset((LPBYTE)hs, 0, sizeof(hde64s)); +#else __stosb((LPBYTE)hs, 0, sizeof(hde64s)); +#endif for (x = 16; x; x--) switch (c = *p++) { diff --git a/src/buffer.c b/src/buffer.c index b3d9306..68d2b30 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -29,6 +29,11 @@ #include <Windows.h> #include "buffer.h" +#ifndef max +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#endif + // Size of each memory block. (= page size of VirtualAlloc) #define MEMORY_BLOCK_SIZE 0x1000 @@ -26,8 +26,22 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifndef _MSC_VER +#define _WIN32_WINNT 0x0501 +#define WIN32_LEAN_AND_MEAN +#endif #include <Windows.h> #include <TlHelp32.h> +#ifndef _MSC_VER +#include <x86intrin.h> +#include <limits.h> +#ifndef ARRAYSIZE + #define ARRAYSIZE(x) sizeof(x)/sizeof(*x) +#endif +#else +#include <intrin.h> +#include <xmmintrin.h> +#endif #include "../include/MinHook.h" #include "buffer.h" @@ -433,13 +447,21 @@ static VOID EnterSpinLock(VOID) SIZE_T spinCount = 0; // Wait until the flag is FALSE. +#ifndef _MSC_VER + while (InterlockedCompareExchange(&g_isLocked, TRUE, FALSE) != FALSE) + { +#else while (_InterlockedCompareExchange(&g_isLocked, TRUE, FALSE) != FALSE) { _ReadWriteBarrier(); - +#endif // Prevent the loop from being too busy. if (spinCount < 16) +#ifndef _MSC_VER + Sleep(0); +#else _mm_pause(); +#endif else if (spinCount < 32) Sleep(0); else @@ -452,8 +474,12 @@ static VOID EnterSpinLock(VOID) //------------------------------------------------------------------------- static VOID LeaveSpinLock(VOID) { +#ifndef _MSC_VER + InterlockedExchange(&g_isLocked, FALSE); +#else _ReadWriteBarrier(); _InterlockedExchange(&g_isLocked, FALSE); +#endif } //------------------------------------------------------------------------- @@ -826,16 +852,22 @@ MH_STATUS WINAPI MH_ApplyQueued(VOID) //------------------------------------------------------------------------- MH_STATUS WINAPI MH_CreateHookApi( - LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal) + LPCWSTR pszModule, LPCSTR pszProcName, LPVOID pDetour, LPVOID *ppOriginal, bool autoLoad) { HMODULE hModule; LPVOID pTarget; hModule = GetModuleHandleW(pszModule); - if (hModule == NULL) + if (hModule == NULL && autoLoad) + { + hModule = LoadLibraryW(pszModule); + if (hModule == NULL) + return MH_ERROR_MODULE_NOT_FOUND; + } + else return MH_ERROR_MODULE_NOT_FOUND; - pTarget = GetProcAddress(hModule, pszProcName); + pTarget = (LPVOID)GetProcAddress(hModule, pszProcName); if (pTarget == NULL) return MH_ERROR_FUNCTION_NOT_FOUND; diff --git a/src/trampoline.c b/src/trampoline.c index c70d394..f36655a 100644 --- a/src/trampoline.c +++ b/src/trampoline.c @@ -27,6 +27,11 @@ */ #include <Windows.h> +#ifndef _MSC_VER +#ifndef ARRAYSIZE + #define ARRAYSIZE(x) sizeof(x)/sizeof(*x) +#endif +#endif #ifdef _M_X64 #include "./hde/hde64.h" @@ -144,7 +149,11 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) PUINT32 pRelAddr; // Avoid using memcpy to reduce the footprint. +#ifndef _MSC_VER + memcpy(instBuf, (LPBYTE)pOldInst, copySize); +#else __movsb(instBuf, (LPBYTE)pOldInst, copySize); +#endif pCopySrc = instBuf; // Relative address is stored at (instruction length - immediate value length - 4). @@ -265,7 +274,11 @@ BOOL CreateTrampolineFunction(PTRAMPOLINE ct) ct->nIP++; // Avoid using memcpy to reduce the footprint. +#ifndef _MSC_VER + memcpy((LPBYTE)ct->pTrampoline + newPos, pCopySrc, copySize); +#else __movsb((LPBYTE)ct->pTrampoline + newPos, pCopySrc, copySize); +#endif newPos += copySize; oldPos += hs.len; } |