Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 98f320d

Browse files
committed
init.c: Re-Add msvc compatible global construct/destruct.
1 parent a2b338a commit 98f320d

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/init.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,28 @@
3232
#include <winsock2.h>
3333
#endif
3434

35+
#ifdef _MSC_VER
36+
#define CONSTRUCTOR_ATTRIBUTE(_func) static void _func(void); \
37+
static int _func ## _wrapper(void) { _func(); return 0; } \
38+
__pragma(section(".CRT$XCU",read)) \
39+
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
40+
#define DESTRUCTOR_ATTRIBUTE(_func) static void _func(void); \
41+
static int _func ## _constructor(void) { atexit (_func); return 0; } \
42+
__pragma(section(".CRT$XCU",read)) \
43+
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
44+
#else
3545
#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
36-
#define CONSTRUCTOR_ATTRIBUTE __attribute__((constructor))
46+
#define CONSTRUCTOR_ATTRIBUTE(_func) void _func(void) __attribute__((constructor))
3747
#else
38-
#define CONSTRUCTOR_ATTRIBUTE
48+
#define CONSTRUCTOR_ATTRIBUTE(_func)
3949
#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
4050

4151
#ifdef HAVE_DESTRUCTOR_ATTRIBUTE
42-
#define DESTRUCTOR_ATTRIBUTE __attribute__((destructor))
52+
#define DESTRUCTOR_ATTRIBUTE(_func) void _func(void) __attribute__((destructor))
4353
#else
44-
#define DESTRUCTOR_ATTRIBUTE
54+
#define DESTRUCTOR_ATTRIBUTE(_func)
4555
#endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
56+
#endif
4657

4758
/* Declare static mutex */
4859
static SSH_MUTEX ssh_init_mutex = SSH_MUTEX_STATIC_INIT;

0 commit comments

Comments
 (0)