remove GenericVectorTests from rsp (#17366)
[mono-project.git] / mono / mini / mini-windows-tls-callback.c
blobb2438670dd8becb27394dbff85a4c075a5507483
1 /**
2 * Copyright 2019 Microsoft Corporation
3 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
4 */
6 #include "mini-runtime.h"
8 #if defined(HOST_WIN32)
9 #include "mini-windows.h"
10 #include <windows.h>
12 /* Only reset by initial callback from OS loader with reason DLL_PROCESS_ATTACH */
13 /* both for DLL and LIB callbacks, so no need to protect this variable. */
14 static MonoWin32TLSCallbackType mono_win32_tls_callback_type = MONO_WIN32_TLS_CALLBACK_TYPE_NONE;
16 /* NOTE, this function needs to be in this source file to make sure linker */
17 /* resolve this symbol from mini-windows.c, picking up callback and */
18 /* included it in TLS Directory PE header. Function makes sure we only activate */
19 /* one of the supported mono callback types at runtime (if multiple one has been used) */
20 gboolean
21 mono_win32_handle_tls_callback_type (MonoWin32TLSCallbackType callback_type)
23 /* Makes sure our tls callback doesn't get optimized away. */
24 extern const PIMAGE_TLS_CALLBACK __mono_win32_tls_callback;
25 const volatile PIMAGE_TLS_CALLBACK __tls_callback = __mono_win32_tls_callback;
27 if (mono_win32_tls_callback_type == MONO_WIN32_TLS_CALLBACK_TYPE_NONE)
28 mono_win32_tls_callback_type = callback_type;
30 if (callback_type != mono_win32_tls_callback_type)
31 return FALSE;
33 return TRUE;
36 VOID NTAPI mono_win32_tls_callback (PVOID module_handle, DWORD reason, PVOID reserved);
38 VOID NTAPI mono_win32_tls_callback (PVOID module_handle, DWORD reason, PVOID reserved)
40 mono_win32_runtime_tls_callback ((HMODULE)module_handle, reason, reserved, MONO_WIN32_TLS_CALLBACK_TYPE_LIB);
43 /* If we are building a static library that won't have access to DllMain we can't */
44 /* correctly detach a thread before it terminates. The MSVC linker + runtime (also applies to MINGW) */
45 /* uses a set of predefined segments/sections where callbacks can be stored and called */
46 /* by OS loader (part of TLS Directory PE header), regardless if runtime is used */
47 /* as a static or dynamic library. */
48 #if (_MSC_VER >= 1400)
49 #pragma const_seg (".CRT$XLX")
50 extern const PIMAGE_TLS_CALLBACK __mono_win32_tls_callback = mono_win32_tls_callback;
51 #pragma const_seg ()
52 #elif defined(__MINGW64__) || (__MINGW32__)
53 extern const PIMAGE_TLS_CALLBACK __mono_win32_tls_callback __attribute__ ((section (".CRT$XLX"))) = mono_win32_tls_callback;
54 #else
55 #pragma message ("TLS callback support not included in .CRT$XLX segment. Static linked runtime won't add callback into PE header.")
56 #endif
57 #endif