15 #define _CRTIMP __declspec(dllexport)
18 #define _CRTIMP __declspec(dllimport)
24 #include <sect_attribs.h>
27 extern void __cdecl
_initterm(_PVFV
*,_PVFV
*);
28 extern void __main ();
29 extern void _pei386_runtime_relocator (void);
30 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a
[];
31 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z
[];
32 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a
[];
33 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z
[];
35 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
;
37 static int __proc_attached
= 0;
39 extern _PVFV
*__onexitbegin
;
40 extern _PVFV
*__onexitend
;
42 extern int mingw_app_type
;
44 extern BOOL WINAPI
DllMain (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
);
46 extern BOOL WINAPI
DllEntryPoint (HANDLE
, DWORD
, LPVOID
);
48 static int pre_c_init (void);
50 _CRTALLOC(".CRT$XIAA") _PIFV pcinit
= pre_c_init
;
57 onexitbegin
= (_PVFV
*) malloc (32 * sizeof (_PVFV
));
58 __onexitend
= __onexitbegin
= (_PVFV
*) _encode_pointer (onexitbegin
);
60 if (onexitbegin
== NULL
)
62 *onexitbegin
= (_PVFV
) NULL
;
66 BOOL WINAPI
_CRT_INIT (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
68 if (dwReason
== DLL_PROCESS_DETACH
)
70 if (__proc_attached
> 0)
75 if (dwReason
== DLL_PROCESS_ATTACH
)
77 void *lock_free
= NULL
;
78 void *fiberid
= ((PNT_TIB
)NtCurrentTeb ())->StackBase
;
81 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
84 if (lock_free
== fiberid
)
91 if (__native_startup_state
!= __uninitialized
)
97 __native_startup_state
= __initializing
;
99 _initterm ((_PVFV
*) (void *) __xi_a
, (_PVFV
*) (void *) __xi_z
);
100 _initterm (__xc_a
,__xc_z
);
101 __native_startup_state
= __initialized
;
105 InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
107 if (__dyn_tls_init_callback
!= NULL
&&
108 _IsNonwritableInCurrentImage ((PBYTE
) &__dyn_tls_init_callback
))
110 __dyn_tls_init_callback (hDllHandle
, DLL_THREAD_ATTACH
, lpreserved
);
114 else if (dwReason
== DLL_PROCESS_DETACH
)
116 void *lock_free
= NULL
;
117 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,(PVOID
) 1, 0)) != 0)
121 if(__native_startup_state
!=__initialized
)
127 _PVFV
* onexitbegin
= (_PVFV
*) _decode_pointer (__onexitbegin
);
130 _PVFV
*onexitend
= (_PVFV
*) _decode_pointer (__onexitend
);
131 while (--onexitend
>= onexitbegin
)
132 if (*onexitend
!= NULL
)
135 __onexitbegin
= __onexitend
= (_PVFV
*) NULL
;
137 __native_startup_state
= __uninitialized
;
138 InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
144 static BOOL
__DllMainCRTStartup (HANDLE
, DWORD
, LPVOID
);
147 DllMainCRTStartup(HANDLE hDllHandle
,DWORD dwReason
,LPVOID lpreserved
)
150 if (dwReason
== DLL_PROCESS_ATTACH
)
152 __security_init_cookie ();
154 return __DllMainCRTStartup (hDllHandle
, dwReason
, lpreserved
);
157 __declspec(noinline
) BOOL
158 __DllMainCRTStartup (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
162 __native_dllmain_reason
= dwReason
;
163 if (dwReason
== DLL_PROCESS_DETACH
&& __proc_attached
== 0)
168 if (dwReason
== DLL_PROCESS_ATTACH
|| dwReason
== DLL_THREAD_ATTACH
)
170 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
172 retcode
= _CRT_INIT (hDllHandle
, dwReason
, lpreserved
);
176 _pei386_runtime_relocator ();
177 if (retcode
&& dwReason
== DLL_PROCESS_ATTACH
)
179 retcode
= DllMain(hDllHandle
,dwReason
,lpreserved
);
180 if ((dwReason
== DLL_PROCESS_ATTACH
) && ! retcode
)
182 DllMain (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
183 _CRT_INIT (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
184 DllEntryPoint (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
186 if (dwReason
== DLL_PROCESS_DETACH
|| dwReason
== DLL_THREAD_DETACH
)
188 if (_CRT_INIT (hDllHandle
, dwReason
, lpreserved
) == FALSE
)
194 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
198 __native_dllmain_reason
= UINT_MAX
;