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 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 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
;
45 static int __proc_attached
= 0;
47 extern _PVFV
*__onexitbegin
;
48 extern _PVFV
*__onexitend
;
50 extern int mingw_app_type
;
52 extern BOOL WINAPI
DllMain (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
);
54 extern BOOL WINAPI
DllEntryPoint (HANDLE
, DWORD
, LPVOID
);
56 static int pre_c_init (void);
58 _CRTALLOC(".CRT$XIAA") _PIFV pcinit
= pre_c_init
;
65 onexitbegin
= (_PVFV
*) malloc (32 * sizeof (_PVFV
));
66 __onexitend
= __onexitbegin
= (_PVFV
*) _encode_pointer (onexitbegin
);
68 if (onexitbegin
== NULL
)
70 *onexitbegin
= (_PVFV
) NULL
;
74 BOOL WINAPI
_CRT_INIT (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
76 if (dwReason
== DLL_PROCESS_DETACH
)
78 if (__proc_attached
> 0)
83 if (dwReason
== DLL_PROCESS_ATTACH
)
85 void *lock_free
= NULL
;
86 void *fiberid
= ((PNT_TIB
)NtCurrentTeb ())->StackBase
;
89 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
92 if (lock_free
== fiberid
)
99 if (__native_startup_state
!= __uninitialized
)
105 __native_startup_state
= __initializing
;
107 _initterm ((_PVFV
*) (void *) __xi_a
, (_PVFV
*) (void *) __xi_z
);
108 _initterm (__xc_a
,__xc_z
);
109 __native_startup_state
= __initialized
;
113 (void) InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
115 if (__dyn_tls_init_callback
!= NULL
)
117 __dyn_tls_init_callback (hDllHandle
, DLL_THREAD_ATTACH
, lpreserved
);
121 else if (dwReason
== DLL_PROCESS_DETACH
)
123 void *lock_free
= NULL
;
124 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,(PVOID
) 1, 0)) != 0)
128 if(__native_startup_state
!=__initialized
)
134 _PVFV
* onexitbegin
= (_PVFV
*) _decode_pointer (__onexitbegin
);
137 _PVFV
*onexitend
= (_PVFV
*) _decode_pointer (__onexitend
);
138 while (--onexitend
>= onexitbegin
)
139 if (*onexitend
!= NULL
)
142 __onexitbegin
= __onexitend
= (_PVFV
*) NULL
;
144 __native_startup_state
= __uninitialized
;
145 (void) InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
151 static BOOL
__DllMainCRTStartup (HANDLE
, DWORD
, LPVOID
);
153 BOOL WINAPI
DllMainCRTStartup (HANDLE
, DWORD
, LPVOID
);
154 int __mingw_init_ehandler (void);
157 DllMainCRTStartup (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
160 if (dwReason
== DLL_PROCESS_ATTACH
)
162 __security_init_cookie ();
164 __mingw_init_ehandler ();
167 return __DllMainCRTStartup (hDllHandle
, dwReason
, lpreserved
);
170 __declspec(noinline
) BOOL
171 __DllMainCRTStartup (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
175 __native_dllmain_reason
= dwReason
;
176 if (dwReason
== DLL_PROCESS_DETACH
&& __proc_attached
== 0)
181 if (dwReason
== DLL_PROCESS_ATTACH
|| dwReason
== DLL_THREAD_ATTACH
)
183 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
185 retcode
= _CRT_INIT (hDllHandle
, dwReason
, lpreserved
);
189 _pei386_runtime_relocator ();
190 if (retcode
&& dwReason
== DLL_PROCESS_ATTACH
)
192 retcode
= DllMain(hDllHandle
,dwReason
,lpreserved
);
193 if ((dwReason
== DLL_PROCESS_ATTACH
) && ! retcode
)
195 DllMain (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
196 _CRT_INIT (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
197 DllEntryPoint (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
199 if (dwReason
== DLL_PROCESS_DETACH
|| dwReason
== DLL_THREAD_DETACH
)
201 if (_CRT_INIT (hDllHandle
, dwReason
, lpreserved
) == FALSE
)
207 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
211 __native_dllmain_reason
= UINT_MAX
;