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
*);
36 extern void __main ();
37 extern void _pei386_runtime_relocator (void);
38 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a
[];
39 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z
[];
40 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a
[];
41 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z
[];
43 /* TLS initialization hook. */
44 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
;
46 static int __proc_attached
= 0;
48 extern _PVFV
*__onexitbegin
;
49 extern _PVFV
*__onexitend
;
51 extern int mingw_app_type
;
53 extern WINBOOL WINAPI
DllMain (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
);
55 extern WINBOOL WINAPI
DllEntryPoint (HANDLE
, DWORD
, LPVOID
);
57 static int pre_c_init (void);
59 _CRTALLOC(".CRT$XIAA") _PIFV pcinit
= pre_c_init
;
66 onexitbegin
= (_PVFV
*) malloc (32 * sizeof (_PVFV
));
67 __onexitend
= __onexitbegin
= (_PVFV
*) _encode_pointer (onexitbegin
);
69 if (onexitbegin
== NULL
)
71 *onexitbegin
= (_PVFV
) NULL
;
75 WINBOOL WINAPI
_CRT_INIT (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
77 if (dwReason
== DLL_PROCESS_DETACH
)
79 if (__proc_attached
> 0)
84 if (dwReason
== DLL_PROCESS_ATTACH
)
86 void *lock_free
= NULL
;
87 void *fiberid
= ((PNT_TIB
)NtCurrentTeb ())->StackBase
;
90 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
93 if (lock_free
== fiberid
)
100 if (__native_startup_state
!= __uninitialized
)
106 __native_startup_state
= __initializing
;
108 _initterm ((_PVFV
*) (void *) __xi_a
, (_PVFV
*) (void *) __xi_z
);
109 _initterm (__xc_a
,__xc_z
);
110 __native_startup_state
= __initialized
;
114 (void) InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
116 if (__dyn_tls_init_callback
!= NULL
)
118 __dyn_tls_init_callback (hDllHandle
, DLL_THREAD_ATTACH
, lpreserved
);
122 else if (dwReason
== DLL_PROCESS_DETACH
)
124 void *lock_free
= NULL
;
125 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,(PVOID
) 1, 0)) != 0)
129 if(__native_startup_state
!=__initialized
)
135 _PVFV
* onexitbegin
= (_PVFV
*) _decode_pointer (__onexitbegin
);
138 _PVFV
*onexitend
= (_PVFV
*) _decode_pointer (__onexitend
);
139 while (--onexitend
>= onexitbegin
)
140 if (*onexitend
!= NULL
)
143 __onexitbegin
= __onexitend
= (_PVFV
*) NULL
;
145 __native_startup_state
= __uninitialized
;
146 (void) InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
152 static WINBOOL
__DllMainCRTStartup (HANDLE
, DWORD
, LPVOID
);
154 WINBOOL WINAPI
DllMainCRTStartup (HANDLE
, DWORD
, LPVOID
);
155 int __mingw_init_ehandler (void);
158 DllMainCRTStartup (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
161 if (dwReason
== DLL_PROCESS_ATTACH
)
163 __security_init_cookie ();
165 __mingw_init_ehandler ();
168 return __DllMainCRTStartup (hDllHandle
, dwReason
, lpreserved
);
171 __declspec(noinline
) WINBOOL
172 __DllMainCRTStartup (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
174 WINBOOL retcode
= TRUE
;
176 __native_dllmain_reason
= dwReason
;
177 if (dwReason
== DLL_PROCESS_DETACH
&& __proc_attached
== 0)
182 if (dwReason
== DLL_PROCESS_ATTACH
|| dwReason
== DLL_THREAD_ATTACH
)
184 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
186 retcode
= _CRT_INIT (hDllHandle
, dwReason
, lpreserved
);
190 _pei386_runtime_relocator ();
191 if (retcode
&& dwReason
== DLL_PROCESS_ATTACH
)
193 retcode
= DllMain(hDllHandle
,dwReason
,lpreserved
);
194 if ((dwReason
== DLL_PROCESS_ATTACH
) && ! retcode
)
196 DllMain (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
197 _CRT_INIT (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
198 DllEntryPoint (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
200 if (dwReason
== DLL_PROCESS_DETACH
|| dwReason
== DLL_THREAD_DETACH
)
202 if (_CRT_INIT (hDllHandle
, dwReason
, lpreserved
) == FALSE
)
208 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
212 __native_dllmain_reason
= UINT_MAX
;