2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
23 #define _CRTIMP __declspec(dllexport)
26 #define _CRTIMP __declspec(dllimport)
32 #include <sect_attribs.h>
35 extern void __cdecl
_initterm(_PVFV
*,_PVFV
*);
38 extern void __main (void);
39 extern void _pei386_runtime_relocator (void);
42 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a
[];
43 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z
[];
44 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a
[];
45 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z
[];
47 /* TLS initialization hook. */
48 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
;
50 static int __proc_attached
= 0;
52 extern _PVFV
*__onexitbegin
;
53 extern _PVFV
*__onexitend
;
55 extern int mingw_app_type
;
57 extern WINBOOL WINAPI
DllMain (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
);
59 extern WINBOOL WINAPI
DllEntryPoint (HANDLE
, DWORD
, LPVOID
);
61 static int pre_c_init (void);
63 _CRTALLOC(".CRT$XIAA") _PIFV pcinit
= pre_c_init
;
70 onexitbegin
= (_PVFV
*) malloc (32 * sizeof (_PVFV
));
71 __onexitend
= __onexitbegin
= (_PVFV
*) _encode_pointer (onexitbegin
);
73 if (onexitbegin
== NULL
)
75 *onexitbegin
= (_PVFV
) NULL
;
79 WINBOOL WINAPI
_CRT_INIT (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
81 if (dwReason
== DLL_PROCESS_DETACH
)
83 if (__proc_attached
> 0)
88 if (dwReason
== DLL_PROCESS_ATTACH
)
90 void *lock_free
= NULL
;
91 void *fiberid
= ((PNT_TIB
)NtCurrentTeb ())->StackBase
;
94 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
97 if (lock_free
== fiberid
)
104 if (__native_startup_state
!= __uninitialized
)
110 __native_startup_state
= __initializing
;
112 _initterm ((_PVFV
*) (void *) __xi_a
, (_PVFV
*) (void *) __xi_z
);
113 _initterm (__xc_a
,__xc_z
);
114 __native_startup_state
= __initialized
;
118 (void) InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
120 if (__dyn_tls_init_callback
!= NULL
)
122 __dyn_tls_init_callback (hDllHandle
, DLL_THREAD_ATTACH
, lpreserved
);
126 else if (dwReason
== DLL_PROCESS_DETACH
)
128 void *lock_free
= NULL
;
129 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,(PVOID
) 1, 0)) != 0)
133 if(__native_startup_state
!=__initialized
)
139 _PVFV
* onexitbegin
= (_PVFV
*) _decode_pointer (__onexitbegin
);
142 _PVFV
*onexitend
= (_PVFV
*) _decode_pointer (__onexitend
);
143 while (--onexitend
>= onexitbegin
)
144 if (*onexitend
!= NULL
)
147 __onexitbegin
= __onexitend
= (_PVFV
*) NULL
;
149 __native_startup_state
= __uninitialized
;
150 (void) InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
156 static WINBOOL
__DllMainCRTStartup (HANDLE
, DWORD
, LPVOID
);
158 WINBOOL WINAPI
DllMainCRTStartup (HANDLE
, DWORD
, LPVOID
);
159 int __mingw_init_ehandler (void);
162 DllMainCRTStartup (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
165 if (dwReason
== DLL_PROCESS_ATTACH
)
167 __security_init_cookie ();
169 __mingw_init_ehandler ();
172 return __DllMainCRTStartup (hDllHandle
, dwReason
, lpreserved
);
175 __declspec(noinline
) WINBOOL
176 __DllMainCRTStartup (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
178 WINBOOL retcode
= TRUE
;
180 __native_dllmain_reason
= dwReason
;
181 if (dwReason
== DLL_PROCESS_DETACH
&& __proc_attached
== 0)
186 #if defined(__GNUC__)
187 _pei386_runtime_relocator ();
189 if (dwReason
== DLL_PROCESS_ATTACH
|| dwReason
== DLL_THREAD_ATTACH
)
191 retcode
= _CRT_INIT (hDllHandle
, dwReason
, lpreserved
);
194 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
197 if (dwReason
== DLL_PROCESS_ATTACH
)
198 _CRT_INIT (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
202 #if defined(__GNUC__)
203 if (dwReason
== DLL_PROCESS_ATTACH
)
206 retcode
= DllMain(hDllHandle
,dwReason
,lpreserved
);
207 if (dwReason
== DLL_PROCESS_ATTACH
&& ! retcode
)
209 DllMain (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
210 DllEntryPoint (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
211 _CRT_INIT (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
213 if (dwReason
== DLL_PROCESS_DETACH
|| dwReason
== DLL_THREAD_DETACH
)
215 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
216 if (_CRT_INIT (hDllHandle
, dwReason
, lpreserved
) == FALSE
)
220 __native_dllmain_reason
= UINT_MAX
;