From 81ab0a3bbdf131dc214b3e11a26b167963252d85 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Wed, 24 Apr 2024 06:31:59 -0700 Subject: [PATCH] win32 before main example --- win32_before_main/build.bat | 15 +++++++++++++++ win32_before_main/win32_before_main.c | 24 ++++++++++++++++++++++++ xlist/base.h | 8 +++++--- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 win32_before_main/build.bat create mode 100644 win32_before_main/win32_before_main.c diff --git a/win32_before_main/build.bat b/win32_before_main/build.bat new file mode 100644 index 0000000..c64fe26 --- /dev/null +++ b/win32_before_main/build.bat @@ -0,0 +1,15 @@ +@echo off + +REM: Path setup + +SET src=%cd% +cd .. +if not exist "build\" mkdir build +cd build + + + +REM: Build +cl /nologo /GL /O2 %src%\win32_before_main.c + + diff --git a/win32_before_main/win32_before_main.c b/win32_before_main/win32_before_main.c new file mode 100644 index 0000000..481afbd --- /dev/null +++ b/win32_before_main/win32_before_main.c @@ -0,0 +1,24 @@ +// this specific section contains before-main function pointers +// this pragma ensures the compilation unit will generate this section +#pragma section(".CRT$XCU", read) + +// declare the "before main" function +static void run_before_main_func(void); + +// set the before-main execution function pointer +__declspec(allocate(".CRT$XCU")) +__pragma(comment(linker, "/INCLUDE:run_before_main_ptr")) +void (*run_before_main_ptr)(void) = run_before_main_func; + +// define the "before main" function +int x = 0; +static void run_before_main_func(void){ + x = 100; +} + +// main +#include +int main(){ + printf("x = %d\n", x); + return(0); +} \ No newline at end of file diff --git a/xlist/base.h b/xlist/base.h index 801e6d2..064656c 100644 --- a/xlist/base.h +++ b/xlist/base.h @@ -31,9 +31,11 @@ # pragma section(".CRT$XCU", read) -# define BEFORE_MAIN(n) extern void n(void); \ -__declspec(allocate(".CRT$XCU")) void (*n##__)(void) = n; \ -__pragma(comment(linker, "/include:" #n)) extern void n(void) +# define BEFORE_MAIN(n) static void n(void); \ +__declspec(allocate(".CRT$XCU")) \ +__pragma(comment(linker, "/INCLUDE:" #n "__")) \ +void (*n##__)(void) = n; \ +static void n(void) #elif OS_LINUX