Skip to content

Commit

Permalink
Fix build for Windows with raw MSC
Browse files Browse the repository at this point in the history
  • Loading branch information
navytux authored and kubo committed Jun 4, 2023
1 parent a69e405 commit 1163751
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 20 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ News
* Add `funchook_prepare_with_params()` to support prehook.
* Add `funchook_get_arg()` to get arguments in prehook.

### 1.1.3 (2023-06-04)

* Fix build for Windows with raw MSC ([#45][])
* Fix debug log format warnings by mingw-w64 compiler.

### 1.1.2 (2023-03-12)

* Experimental support for Windows arm64
Expand Down Expand Up @@ -282,4 +287,5 @@ itself is under the GPL.
[#30]: https://github.com/kubo/funchook/pull/30
[#31]: https://github.com/kubo/funchook/pull/31
[#32]: https://github.com/kubo/funchook/pull/32
[#45]: https://github.com/kubo/funchook/pull/45
[`mprotect`]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/mprotect.2.html
2 changes: 1 addition & 1 deletion include/funchook.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extern "C" {
* or libfunchook.so. Others are invisible.
*/
#ifdef FUNCHOOK_EXPORTS
#if defined(WIN32)
#if defined(_WIN32)
#define FUNCHOOK_EXPORT __declspec(dllexport)
#elif defined(__GNUC__)
#define FUNCHOOK_EXPORT __attribute__((visibility("default")))
Expand Down
4 changes: 2 additions & 2 deletions src/arch_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ int funchook_get_arg_offset(const char *arg_types, int pos, uint32_t flags)
} state = ST_STACK;
int offset = INT_MIN;
int next_offset = 1;
#ifdef WIN32
#ifdef _WIN32
if (flags & FUNCHOOK_FLAG_THISCALL) {
state = ST_THISCALL;
}
Expand Down Expand Up @@ -479,7 +479,7 @@ int funchook_get_arg_offset(const char *arg_types, int pos, uint32_t flags)
}
}
return offset;
#elif defined(WIN32)
#elif defined(_WIN32)
size_t num_args = strlen(arg_types);
if (pos > num_args) {
return INT_MIN;
Expand Down
8 changes: 4 additions & 4 deletions src/funchook.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include <stdint.h>
#include <string.h>
#include <limits.h>
#ifdef WIN32
#ifdef _WIN32
#include <windows.h>
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
Expand Down Expand Up @@ -82,7 +82,7 @@ static void flush_instruction_cache(void *addr, size_t size)
{
#if defined __GNUC__
__builtin___clear_cache((char*)addr, (char*)addr + size);
#elif defined WIN32
#elif defined _WIN32
FlushInstructionCache(GetCurrentProcess(), addr, size);
#else
#error unsupported OS or compiler
Expand Down Expand Up @@ -308,12 +308,12 @@ static funchook_t *funchook_create_internal(void)
num_entries_in_page = (page_size - offsetof(funchook_page_t, entries)) / sizeof(funchook_entry_t);
#endif
funchook_log(funchook,
#ifdef WIN32
#ifdef _WIN32
" allocation_unit=%"PRIuPTR"\n"
#endif
" page_size=%"PRIuPTR"\n"
" num_entries_in_page=%"PRIuPTR"\n",
#ifdef WIN32
#ifdef _WIN32
allocation_unit,
#endif
page_size, num_entries_in_page);
Expand Down
4 changes: 2 additions & 2 deletions src/funchook_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#ifndef FUNCHOOK_INTERNAL_H
#define FUNCHOOK_INTERNAL_H 1
#include "funchook.h"
#ifdef WIN32
#ifdef _WIN32
#include <windows.h>
#endif

Expand Down Expand Up @@ -115,7 +115,7 @@ struct funchook_arg_handle {
typedef struct {
void *addr;
size_t size;
#ifdef WIN32
#ifdef _WIN32
DWORD protect;
#endif
} mem_state_t;
Expand Down
2 changes: 1 addition & 1 deletion test/libfunchook_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extern "C" {
#endif

#if defined(WIN32)
#if defined(_WIN32)
#ifdef FUNCHOOK_TEST_EXPORTS
#define TEST_DLL_EXPORT __declspec(dllexport)
#else
Expand Down
2 changes: 1 addition & 1 deletion test/libfunchook_test_noasm.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef WIN32
#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
Expand Down
2 changes: 1 addition & 1 deletion test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define NOINLINE __declspec(noinline)
#endif

#if defined(WIN32)
#if defined(_WIN32)
#define DLLIMPORT __declspec(dllimport)
#else
#define DLLIMPORT
Expand Down
10 changes: 5 additions & 5 deletions test/test_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#if defined(__linux__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#ifdef WIN32
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
Expand All @@ -14,7 +14,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef WIN32
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#define mode_t int
Expand All @@ -33,7 +33,7 @@
#include "test.h"
#include "libfunchook_test.h"

#ifdef WIN32
#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
Expand Down Expand Up @@ -125,7 +125,7 @@ enum load_type {
static void *load_func(const char *module, const char *func)
{
void *addr;
#ifdef WIN32
#ifdef _WIN32
HMODULE hMod = GetModuleHandleA(module);

if (hMod == NULL) {
Expand Down Expand Up @@ -592,7 +592,7 @@ int main()
TEST_FUNCHOOK_EXPECT_ERROR(x86_test_error_jump1, FUNCHOOK_ERROR_CANNOT_FIX_IP_RELATIVE);
TEST_FUNCHOOK_EXPECT_ERROR(x86_test_error_jump2, FUNCHOOK_ERROR_FOUND_BACK_JUMP);

#ifndef WIN32
#ifndef _WIN32
TEST_FUNCHOOK_INT(x86_test_call_get_pc_thunk_ax, LOAD_TYPE_NO_LOAD);
TEST_FUNCHOOK_INT(x86_test_call_get_pc_thunk_bx, LOAD_TYPE_NO_LOAD);
TEST_FUNCHOOK_INT(x86_test_call_get_pc_thunk_cx, LOAD_TYPE_NO_LOAD);
Expand Down
4 changes: 2 additions & 2 deletions test/test_prehook.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <funchook.h>
#include "test.h"
#include "libfunchook_test.h"
#ifdef WIN32
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
Expand Down Expand Up @@ -43,7 +43,7 @@ static void test_dllfunc(void)
int rv;
void *dllfunc_1_user_data = (void*)(size_t)0xdeadbeef;
void *dllfunc_2_user_data = (void*)(size_t)0xcafebabe;
#ifdef WIN32
#ifdef _WIN32
HANDLE hMod = GetModuleHandleA("funchook_test_dll.dll");
if (hMod == NULL) {
hMod = GetModuleHandleA("libfunchook_test.so");
Expand Down
2 changes: 1 addition & 1 deletion test/x86_test.S
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ label_2_1:
#endif
ret

#ifndef WIN32
#ifndef _WIN32
.p2align 4,,15
.globl x86_test_call_get_pc_thunk_ax
x86_test_call_get_pc_thunk_ax:
Expand Down

0 comments on commit 1163751

Please sign in to comment.