win32_before_main update to holding the symbol with linker include option replacing dllexport

main
Allen Webster 2024-04-24 16:28:39 -07:00
parent 0b6adfde1e
commit 9ddc06fc0a
3 changed files with 14 additions and 11 deletions

View File

@ -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:<symbol-name> and we can add this command to the linker via a pragma
comment:
__pragma(comment(linker,"/include:<symbol-name>"))
From what I've seen in testing, this works as desired, even with the /GL option.

View File

@ -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){

View File

@ -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