ReactOS 0.4.16-dev-2528-g7139e57
implicit_tls.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Tests for Implicit Thread Local Storage (TLS) support
5 * COPYRIGHT: Copyright 2025 Shane Fournier <shanefournier@yandex.com>
6 */
7
8#define WIN32_NO_STATUS
9#define _INC_WINDOWS
10#define COM_NO_WINDOWS_H
11
12#include <ndk/ntndk.h>
13#include <windows.h>
14
15#if defined(_MSC_VER)
16#define _CRTALLOC(x) __declspec(allocate(x))
17#elif defined(__GNUC__)
18#define _CRTALLOC(x) __attribute__ ((section (x) ))
19#else
20#error Your compiler is not supported.
21#endif
22
23/*
24 * Putting this note here as this is more directly related to TLS:
25 * https://web.archive.org/web/20250204223245/http://www.nynaeve.net/?tag=tls
26 */
27/* Tls magic stolen from sdk/lib/crt/startup/tlssup.c */
28/* ROS is built with the flag /Zc:threadSafeInit- which prevents the compilation
29 * of proper TLS code. Instead, hack up a TLS directory to increment the TLS index
30 * and allow the loader to allocate a sufficient buffer in the TLS vector. */
31
32#if defined(_MSC_VER)
33#pragma section(".rdata$T",long,read)
34#pragma section(".tls",long,read,write)
35#pragma section(".tls$ZZZ",long,read,write)
36#endif
37
38_CRTALLOC(".tls") char _tls_start = 0;
39_CRTALLOC(".tls$ZZZ") char _tls_end = 0;
40
42
44 (ULONG_PTR) &_tls_start, (ULONG_PTR) &_tls_end + 3,
46 (ULONG) 0, (ULONG) 0
47};
48
50DllMain(IN HINSTANCE hDllHandle,
53{
54 PTEB Teb = NtCurrentTeb();
55 PVOID* TlsVector = Teb->ThreadLocalStoragePointer;
56 PULONG_PTR ModuleHandle = (PULONG_PTR)TlsVector[_tls_index];
58 {
59 #if defined(_MSC_VER)
60 #pragma warning( disable : 4311)
61 #endif
62 *ModuleHandle = (ULONG_PTR)GetModuleHandleA(NULL) + 1;
63 }
64 return TRUE;
65}
DWORD dwReason
Definition: misc.cpp:135
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
unsigned char
Definition: typeof.h:29
#define ULONG_PTR
Definition: config.h:101
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define _CRTALLOC(x)
BOOL WINAPI DllMain(IN HINSTANCE hDllHandle, IN DWORD dwReason, IN LPVOID lpvReserved)
Definition: implicit_tls.c:50
const IMAGE_TLS_DIRECTORY _tls_used
#define NtCurrentTeb
static IN DWORD IN LPVOID lpvReserved
ULONG _tls_index
Definition: compat.h:836
PVOID ThreadLocalStoragePointer
Definition: compat.h:841
uint32_t * PULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define WINAPI
Definition: msvc.h:6
#define const
Definition: zconf.h:233