Skip to content

Commit

Permalink
Merge pull request #55 from Vipon/vkonyche_msvc_mode_code_workaround
Browse files Browse the repository at this point in the history
Create MSVC workaround for MOD_CODE.

issue: #52
  • Loading branch information
Vipon authored Feb 24, 2024
2 parents 4650a5b + 94c9632 commit c666876
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
6 changes: 1 addition & 5 deletions cTools/libs/mod/mod_code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,5 @@ add_vipon_library(
INSTALL ON
)

# MSVC compiler wrongly works with inline assembler. MSVC emits a lot of "int3"
# instructions if there is a ".section" directive.
if (NOT MSVC)
add_subdirectory(test)
endif ()
add_subdirectory(test)

19 changes: 19 additions & 0 deletions cTools/libs/mod/mod_code/mod_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@
"r8", "r9", "r10", "r11", \
"r12", "r13", "r14", "r15"

#ifdef _MSC_VER
// Works without __builtin_expect also.
// Code generated more redable with __builtin_expect.
# define MSVC_WORKAROUND(code) \
{ \
volatile static int a = 1; \
if (__builtin_expect(a == 0, 0)) { \
code; \
} \
}
#else /* _MSVC_LANG */
# define MSVC_WORKAROUND(code) code
#endif /* _MSVC_LANG */

#if AARCH64_DEFINED == 1
# define MOD_CODE(code) \
DEF_GUARD( \
Expand Down Expand Up @@ -108,6 +122,10 @@
DEF_GUARD( \
ASM volatile( \
"0:\n" \
::: "memory", CLOBBERS_ALL_X86_64_REGS \
); \
MSVC_WORKAROUND( \
ASM volatile( \
PUSHSECTION" "MC_STRUCT_SEGSECT", "MOD_SECT_FLAGS"\n" \
".align 8\n" \
/* .insert_point */ \
Expand All @@ -131,6 +149,7 @@
POPSECTION"\n" \
::: "memory", CLOBBERS_ALL_X86_64_REGS \
); \
); \
)
#else
# error "Unknown machine type"
Expand Down
17 changes: 6 additions & 11 deletions cTools/libs/mod/mod_code/test/mod_code_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,22 @@
#include "mod_code.h"
#include <inttypes.h>

extern void test_func(void);
extern void test_func(void) __attribute__((noinline));
void test_func(void)
{
MOD_CODE(
printf("hello\n");
);

STDERROR_PRINT("I'm here\n");
}

extern volatile int a;
volatile int a = 0;

int main(int argc, const char *argv[])
{
UNUSED(argc);
UNUSED(argv);

STDERROR_PRINT("main: %p\n", (void*)&main);

MOD_CODE(
do {
printf("hello\n");
} while (a);
);
STDERROR_PRINT("test_func: %p\n", (void*)&test_func);

mod_code_init(argv[0]);
mod_code_dump();
Expand Down

0 comments on commit c666876

Please sign in to comment.