summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJose Fonseca <jfonseca@vmware.com>2016-09-03 10:17:38 +0100
committerJose Fonseca <jfonseca@vmware.com>2016-09-05 15:57:40 +0100
commitc71a5687e076550fb607f85a9ff041bda7afe4e9 (patch)
tree140f2c82c99d558d600f9a2393d821bc2859c8af
parent6e1b4f89736c93e0069e60afd68852997c5bbd5a (diff)
mhook: Fix MinGW build.
Using https://github.com/SirAnthony/mhook as reference.
-rw-r--r--thirdparty/mhook/disasm-lib/disasm.c2
-rw-r--r--thirdparty/mhook/disasm-lib/disasm.h3
-rw-r--r--thirdparty/mhook/disasm-lib/disasm_x86.c12
-rw-r--r--thirdparty/mhook/disasm-lib/disasm_x86.h22
-rw-r--r--thirdparty/mhook/mhook-lib/mhook.cpp4
-rw-r--r--thirdparty/mhook/mhook-test.cpp30
-rw-r--r--thirdparty/mhook/stdafx.h4
7 files changed, 49 insertions, 28 deletions
diff --git a/thirdparty/mhook/disasm-lib/disasm.c b/thirdparty/mhook/disasm-lib/disasm.c
index 7e72baaa..7dd8fa3b 100644
--- a/thirdparty/mhook/disasm-lib/disasm.c
+++ b/thirdparty/mhook/disasm-lib/disasm.c
@@ -37,7 +37,7 @@ typedef struct _DISASM_ARG_INFO
//////////////////////////////////////////////////////////////////////
BOOL InitInstruction(INSTRUCTION *Instruction, DISASSEMBLER *Disassembler);
-struct _ARCHITECTURE_FORMAT *GetArchitectureFormat(ARCHITECTURE_TYPE Type);
+static struct _ARCHITECTURE_FORMAT *GetArchitectureFormat(ARCHITECTURE_TYPE Type);
//////////////////////////////////////////////////////////////////////
// Disassembler setup
diff --git a/thirdparty/mhook/disasm-lib/disasm.h b/thirdparty/mhook/disasm-lib/disasm.h
index 6e35ab72..4b8be071 100644
--- a/thirdparty/mhook/disasm-lib/disasm.h
+++ b/thirdparty/mhook/disasm-lib/disasm.h
@@ -25,6 +25,9 @@ typedef unsigned long U32;
typedef LONG64 S64;
typedef ULONG64 U64;
+// Forward declarations
+struct _INSTRUCTION;
+
#ifdef SPEEDY
// On Visual Studio 6, making the internal functions inline makes compiling take forever
#define INTERNAL static _inline
diff --git a/thirdparty/mhook/disasm-lib/disasm_x86.c b/thirdparty/mhook/disasm-lib/disasm_x86.c
index 26a5d764..005a3d1a 100644
--- a/thirdparty/mhook/disasm-lib/disasm_x86.c
+++ b/thirdparty/mhook/disasm-lib/disasm_x86.c
@@ -25,8 +25,10 @@
#include "disasm_x86_tables.h"
#ifdef _WIN64
+#ifdef _MSC_VER
#pragma warning(disable:4311 4312)
#endif
+#endif
////////////////////////////////////////////////////////////////////////
// Internal macros
@@ -130,7 +132,7 @@
{ \
if (!Instruction->AnomalyOccurred) \
{ \
- if (!SuppressErrors) printf("[0x%08I64X] ANOMALY: unexpected segment 0x%02X\n", VIRTUAL_ADDRESS, X86Instruction->Selector); \
+ if (!SuppressErrors) printf("[0x%08I64X] ANOMALY: unexpected segment 0x%02lX\n", VIRTUAL_ADDRESS, X86Instruction->Selector); \
Instruction->AnomalyOccurred = TRUE; \
} \
} \
@@ -1915,7 +1917,7 @@ HasSpecialExtension:
Instruction->AnomalyOccurred = TRUE;
break;
default:
- if (!SuppressErrors) printf("[0x%08I64X] ANOMALY: unexpected segment 0x%02X\n", VIRTUAL_ADDRESS, X86Instruction->Selector);
+ if (!SuppressErrors) printf("[0x%08I64X] ANOMALY: unexpected segment 0x%02lX\n", VIRTUAL_ADDRESS, X86Instruction->Selector);
Instruction->AnomalyOccurred = TRUE;
break;
}
@@ -2461,7 +2463,7 @@ HasSpecialExtension:
if (!Instruction->AnomalyOccurred &&
((X86Instruction->OperandSize != 2 && (Instruction->StackChange & 3)) || (Instruction->StackChange & 1)))
{
- if (!SuppressErrors) printf("[0x%08I64X] ANOMALY: \"%s\" has invalid stack change 0x%02X\n", VIRTUAL_ADDRESS, X86Opcode->Mnemonic, Instruction->StackChange);
+ if (!SuppressErrors) printf("[0x%08I64X] ANOMALY: \"%s\" has invalid stack change 0x%02lX\n", VIRTUAL_ADDRESS, X86Opcode->Mnemonic, Instruction->StackChange);
Instruction->AnomalyOccurred = TRUE;
}
}
@@ -3737,7 +3739,7 @@ INTERNAL U8 *SetOperands(INSTRUCTION *Instruction, U8 *Address, U32 Flags)
}
else if (X86Instruction->OperandSize == 2)
{
- if (!SuppressErrors) printf("[0x%08I64X] ERROR: AMODE_PR illegal in 16-bit mode (\"%s\")\n", VIRTUAL_ADDRESS, rex_modrm.rm, X86Instruction->Opcode.Mnemonic);
+ if (!SuppressErrors) printf("[0x%08I64X] ERROR: AMODE_PR illegal in 16-bit mode (\"%s\")\n", VIRTUAL_ADDRESS, X86Instruction->Opcode.Mnemonic);
goto abort;
}
if (!Decode) continue;
@@ -3764,7 +3766,7 @@ INTERNAL U8 *SetOperands(INSTRUCTION *Instruction, U8 *Address, U32 Flags)
}
else if (X86Instruction->OperandSize == 2)
{
- if (!SuppressErrors) printf("[0x%08I64X] ERROR: AMODE_VR illegal in 16-bit mode (\"%s\")\n", VIRTUAL_ADDRESS, rex_modrm.rm, X86Instruction->Opcode.Mnemonic);
+ if (!SuppressErrors) printf("[0x%08I64X] ERROR: AMODE_VR illegal in 16-bit mode (\"%s\")\n", VIRTUAL_ADDRESS, X86Instruction->Opcode.Mnemonic);
goto abort;
}
if (!Decode) continue;
diff --git a/thirdparty/mhook/disasm-lib/disasm_x86.h b/thirdparty/mhook/disasm-lib/disasm_x86.h
index e64a11f2..b8505029 100644
--- a/thirdparty/mhook/disasm-lib/disasm_x86.h
+++ b/thirdparty/mhook/disasm-lib/disasm_x86.h
@@ -94,33 +94,33 @@ extern "C" {
// NOTE: OPTYPES >= 0x80 reserved for registers (OP_REG+XX)
-#define OPTYPE_REG_AL OP_REG+0x01
-#define OPTYPE_REG_CL OP_REG+0x02
-#define OPTYPE_REG_AH OP_REG+0x03
-#define OPTYPE_REG_AX OP_REG+0x04
-#define OPTYPE_REG_DX OP_REG+0x05
-#define OPTYPE_REG_ECX OP_REG+0x06
-#define OPTYPE_REG8 OP_REG+0x07
+#define OPTYPE_REG_AL (OP_REG+0x01)
+#define OPTYPE_REG_CL (OP_REG+0x02)
+#define OPTYPE_REG_AH (OP_REG+0x03)
+#define OPTYPE_REG_AX (OP_REG+0x04)
+#define OPTYPE_REG_DX (OP_REG+0x05)
+#define OPTYPE_REG_ECX (OP_REG+0x06)
+#define OPTYPE_REG8 (OP_REG+0x07)
// If address size is 2, use BP
// If address size is 4, use EBP
// If address size is 8, use RBP
-#define OPTYPE_REG_xBP OP_REG+0x08
+#define OPTYPE_REG_xBP (OP_REG+0x08)
// If address size is 2, use BP
// If address size is 4, use EBP
// If address size is 8, use RBP
-#define OPTYPE_REG_xSP OP_REG+0x09
+#define OPTYPE_REG_xSP (OP_REG+0x09)
// If operand size is 2, take 8-bit register
// If operand size is 4, take 16-bit register
// If operand size is 8, take 32-bit register
-#define OPTYPE_REG_xAX_SMALL OP_REG+0x0a
+#define OPTYPE_REG_xAX_SMALL (OP_REG+0x0a)
// If operand size is 2, take 16-bit register
// If operand size is 4, take 32-bit register
// If operand size is 8, take 64-bit register
-#define OPTYPE_REG_xAX_BIG OP_REG+0x0b
+#define OPTYPE_REG_xAX_BIG (OP_REG+0x0b)
typedef enum _CPU_TYPE
{
diff --git a/thirdparty/mhook/mhook-lib/mhook.cpp b/thirdparty/mhook/mhook-lib/mhook.cpp
index 3380dcea..7249ad5d 100644
--- a/thirdparty/mhook/mhook-lib/mhook.cpp
+++ b/thirdparty/mhook/mhook-lib/mhook.cpp
@@ -24,6 +24,10 @@
#include "mhook.h"
#include "../disasm-lib/disasm.h"
+#ifndef max
+#define max(a,b) (((a) > (b)) ? (a) : (b))
+#endif
+
//=========================================================================
#ifndef cntof
#define cntof(a) (sizeof(a)/sizeof(a[0]))
diff --git a/thirdparty/mhook/mhook-test.cpp b/thirdparty/mhook/mhook-test.cpp
index 79dc767e..c043866e 100644
--- a/thirdparty/mhook/mhook-test.cpp
+++ b/thirdparty/mhook/mhook-test.cpp
@@ -73,7 +73,7 @@ ULONG WINAPI HookNtOpenProcess(OUT PHANDLE ProcessHandle,
IN PVOID ObjectAttributes,
IN PCLIENT_ID ClientId)
{
- printf("***** Call to open process %d\n", ClientId->UniqueProcess);
+ printf("***** Call to open process %ld\n", (DWORD)ClientId->UniqueProcess);
return TrueNtOpenProcess(ProcessHandle, AccessMask,
ObjectAttributes, ClientId);
}
@@ -103,7 +103,12 @@ int WSAAPI Hookgetaddrinfo(const char* nodename, const char* servname, const str
// is in place
//
LPVOID WINAPI HookHeapAlloc(HANDLE a_Handle, DWORD a_Bla, SIZE_T a_Bla2) {
- printf("***** Call to HeapAlloc(0x%p, %u, 0x%p)\n", a_Handle, a_Bla, a_Bla2);
+ static int recurse = 0;
+ if (recurse == 0) {
+ ++recurse;
+ printf("***** Call to HeapAlloc(0x%p, %lu, 0x%p)\n", a_Handle, a_Bla, (LPVOID)a_Bla2);
+ --recurse;
+ }
return TrueHeapAlloc(a_Handle, a_Bla, a_Bla2);
}
@@ -119,12 +124,17 @@ ULONG WINAPI HookNtClose(HANDLE hHandle) {
//=========================================================================
// This is where the work gets done.
//
+
+#ifndef _MSC_VER
+int main(int argc, char* argv[])
+#else
int wmain(int argc, WCHAR* argv[])
+#endif
{
HANDLE hProc = NULL;
// Set the hook
- if (Mhook_SetHook((PVOID*)&TrueNtOpenProcess, HookNtOpenProcess)) {
+ if (Mhook_SetHook((PVOID*)&TrueNtOpenProcess, (PVOID)HookNtOpenProcess)) {
// Now call OpenProcess and observe NtOpenProcess being redirected
// under the hood.
hProc = OpenProcess(PROCESS_ALL_ACCESS,
@@ -133,7 +143,7 @@ int wmain(int argc, WCHAR* argv[])
printf("Successfully opened self: %p\n", hProc);
CloseHandle(hProc);
} else {
- printf("Could not open self: %d\n", GetLastError());
+ printf("Could not open self: %ld\n", GetLastError());
}
// Remove the hook
Mhook_Unhook((PVOID*)&TrueNtOpenProcess);
@@ -146,7 +156,7 @@ int wmain(int argc, WCHAR* argv[])
printf("Successfully opened self: %p\n", hProc);
CloseHandle(hProc);
} else {
- printf("Could not open self: %d\n", GetLastError());
+ printf("Could not open self: %ld\n", GetLastError());
}
// Test another hook, this time in SelectObject
@@ -155,7 +165,7 @@ int wmain(int argc, WCHAR* argv[])
// extra work under the hood to make things work properly. This really
// is more of a test case rather than a demo.)
printf("Testing SelectObject.\n");
- if (Mhook_SetHook((PVOID*)&TrueSelectObject, HookSelectobject)) {
+ if (Mhook_SetHook((PVOID*)&TrueSelectObject, (PVOID)HookSelectobject)) {
// error checking omitted for brevity. doesn't matter much
// in this context anyway.
HDC hdc = GetDC(NULL);
@@ -171,12 +181,12 @@ int wmain(int argc, WCHAR* argv[])
}
printf("Testing getaddrinfo.\n");
- if (Mhook_SetHook((PVOID*)&Truegetaddrinfo, Hookgetaddrinfo)) {
+ if (Mhook_SetHook((PVOID*)&Truegetaddrinfo, (PVOID)Hookgetaddrinfo)) {
// error checking omitted for brevity. doesn't matter much
// in this context anyway.
WSADATA wd = {0};
WSAStartup(MAKEWORD(2, 2), &wd);
- char* ip = "localhost";
+ const char* ip = "localhost";
struct addrinfo aiHints;
struct addrinfo *res = NULL;
memset(&aiHints, 0, sizeof(aiHints));
@@ -198,7 +208,7 @@ int wmain(int argc, WCHAR* argv[])
}
printf("Testing HeapAlloc.\n");
- if (Mhook_SetHook((PVOID*)&TrueHeapAlloc, HookHeapAlloc))
+ if (Mhook_SetHook((PVOID*)&TrueHeapAlloc, (PVOID)HookHeapAlloc))
{
free(malloc(10));
// Remove the hook
@@ -206,7 +216,7 @@ int wmain(int argc, WCHAR* argv[])
}
printf("Testing NtClose.\n");
- if (Mhook_SetHook((PVOID*)&TrueNtClose, HookNtClose))
+ if (Mhook_SetHook((PVOID*)&TrueNtClose, (PVOID)HookNtClose))
{
CloseHandle(NULL);
// Remove the hook
diff --git a/thirdparty/mhook/stdafx.h b/thirdparty/mhook/stdafx.h
index 76cc8f68..c78b5bb7 100644
--- a/thirdparty/mhook/stdafx.h
+++ b/thirdparty/mhook/stdafx.h
@@ -10,8 +10,10 @@
#endif
//for the getaddrinfo test
-#include <WS2tcpip.h>
+#include <ws2tcpip.h>
+#ifdef _MSC_VER
#pragma comment(lib, "ws2_32")
+#endif
#include <windows.h>
#include <stdio.h>