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.
21 #define _CRTIMP __declspec(dllexport)
24 #define _CRTIMP __declspec(dllimport)
30 #include <sect_attribs.h>
33 extern void __cdecl
_initterm(_PVFV
*,_PVFV
*);
34 extern void __main ();
35 extern void _pei386_runtime_relocator (void);
36 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a
[];
37 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z
[];
38 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a
[];
39 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z
[];
41 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
;
43 static int __proc_attached
= 0;
45 extern _PVFV
*__onexitbegin
;
46 extern _PVFV
*__onexitend
;
48 extern int mingw_app_type
;
50 extern BOOL WINAPI
DllMain (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
);
52 extern BOOL WINAPI
DllEntryPoint (HANDLE
, DWORD
, LPVOID
);
54 static int pre_c_init (void);
56 _CRTALLOC(".CRT$XIAA") _PIFV pcinit
= pre_c_init
;
63 onexitbegin
= (_PVFV
*) malloc (32 * sizeof (_PVFV
));
64 __onexitend
= __onexitbegin
= (_PVFV
*) _encode_pointer (onexitbegin
);
66 if (onexitbegin
== NULL
)
68 *onexitbegin
= (_PVFV
) NULL
;
72 BOOL WINAPI
_CRT_INIT (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
74 if (dwReason
== DLL_PROCESS_DETACH
)
76 if (__proc_attached
> 0)
81 if (dwReason
== DLL_PROCESS_ATTACH
)
83 void *lock_free
= NULL
;
84 void *fiberid
= ((PNT_TIB
)NtCurrentTeb ())->StackBase
;
87 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
90 if (lock_free
== fiberid
)
97 if (__native_startup_state
!= __uninitialized
)
103 __native_startup_state
= __initializing
;
105 _initterm ((_PVFV
*) (void *) __xi_a
, (_PVFV
*) (void *) __xi_z
);
106 _initterm (__xc_a
,__xc_z
);
107 __native_startup_state
= __initialized
;
111 InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
113 if (__dyn_tls_init_callback
!= NULL
&&
114 _IsNonwritableInCurrentImage ((PBYTE
) &__dyn_tls_init_callback
))
116 __dyn_tls_init_callback (hDllHandle
, DLL_THREAD_ATTACH
, lpreserved
);
120 else if (dwReason
== DLL_PROCESS_DETACH
)
122 void *lock_free
= NULL
;
123 while ((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,(PVOID
) 1, 0)) != 0)
127 if(__native_startup_state
!=__initialized
)
133 _PVFV
* onexitbegin
= (_PVFV
*) _decode_pointer (__onexitbegin
);
136 _PVFV
*onexitend
= (_PVFV
*) _decode_pointer (__onexitend
);
137 while (--onexitend
>= onexitbegin
)
138 if (*onexitend
!= NULL
)
141 __onexitbegin
= __onexitend
= (_PVFV
*) NULL
;
143 __native_startup_state
= __uninitialized
;
144 InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
150 static BOOL
__DllMainCRTStartup (HANDLE
, DWORD
, LPVOID
);
153 DllMainCRTStartup(HANDLE hDllHandle
,DWORD dwReason
,LPVOID lpreserved
)
156 if (dwReason
== DLL_PROCESS_ATTACH
)
158 __security_init_cookie ();
160 return __DllMainCRTStartup (hDllHandle
, dwReason
, lpreserved
);
163 __declspec(noinline
) BOOL
164 __DllMainCRTStartup (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
168 __native_dllmain_reason
= dwReason
;
169 if (dwReason
== DLL_PROCESS_DETACH
&& __proc_attached
== 0)
174 if (dwReason
== DLL_PROCESS_ATTACH
|| dwReason
== DLL_THREAD_ATTACH
)
176 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
178 retcode
= _CRT_INIT (hDllHandle
, dwReason
, lpreserved
);
182 _pei386_runtime_relocator ();
183 if (retcode
&& dwReason
== DLL_PROCESS_ATTACH
)
185 retcode
= DllMain(hDllHandle
,dwReason
,lpreserved
);
186 if ((dwReason
== DLL_PROCESS_ATTACH
) && ! retcode
)
188 DllMain (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
189 _CRT_INIT (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
190 DllEntryPoint (hDllHandle
, DLL_PROCESS_DETACH
, lpreserved
);
192 if (dwReason
== DLL_PROCESS_DETACH
|| dwReason
== DLL_THREAD_DETACH
)
194 if (_CRT_INIT (hDllHandle
, dwReason
, lpreserved
) == FALSE
)
200 retcode
= DllEntryPoint (hDllHandle
, dwReason
, lpreserved
);
204 __native_dllmain_reason
= UINT_MAX
;