diff --git a/win32_before_main/DETAILS.txt b/win32_before_main/DETAILS.txt index 2f6b79e..7cb8659 100644 --- a/win32_before_main/DETAILS.txt +++ b/win32_before_main/DETAILS.txt @@ -23,8 +23,11 @@ anything. However, in this example I show how we can still make it work. We have to make sure the linker won't eliminate the global variable that we are trying to -place into the ".CRT$XCU" section. I achieve this by marking it as an export -symbol. Export symbols can't be eliminated even if they aren't used locally. +place into the ".CRT$XCU" section. We can tell the linker to keep a symbol with +/include: and we can add this command to the linker via a pragma +comment: + +__pragma(comment(linker,"/include:")) From what I've seen in testing, this works as desired, even with the /GL option. diff --git a/win32_before_main/win32_before_main.c b/win32_before_main/win32_before_main.c index e6d0903..3e90d23 100644 --- a/win32_before_main/win32_before_main.c +++ b/win32_before_main/win32_before_main.c @@ -7,7 +7,7 @@ static void run_before_main_func(void); // set the before-main execution function pointer __declspec(allocate(".CRT$XCU")) -__declspec(dllexport) +__pragma(comment(linker,"/include:run_before_main_ptr")) void (*run_before_main_ptr)(void) = run_before_main_func; // define the "before main" function @@ -27,10 +27,10 @@ int main(){ #if 0 // wrapped in a macro: -#define BEFORE_MAIN(n) static void n(void); \ -__declspec(allocate(".CRT$XCU")) \ -__declspec(dllexport) \ -void (*n##__)(void) = n; \ +#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) BEFORE_MAIN(before_main_rule){ diff --git a/xlist/base.h b/xlist/base.h index ebeaf63..1ab6274 100644 --- a/xlist/base.h +++ b/xlist/base.h @@ -33,10 +33,10 @@ # pragma section(".CRT$XCU", read) -# define BEFORE_MAIN(n) static void n(void); \ -__declspec(allocate(".CRT$XCU")) \ -__declspec(dllexport) \ -void (*n##__)(void) = n; \ +#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