From 32c349d30ed6b08f9c277ca22593479361791494 Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Thu, 30 Jan 2025 10:11:38 +0200 Subject: [PATCH] [VCRUNTIME] Add TLS callback support --- sdk/lib/vcruntime/CMakeLists.txt | 1 + sdk/lib/vcruntime/inc/internal_shared.h | 16 +++++++++++ sdk/lib/vcruntime/tlssup.c | 38 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 sdk/lib/vcruntime/tlssup.c diff --git a/sdk/lib/vcruntime/CMakeLists.txt b/sdk/lib/vcruntime/CMakeLists.txt index 21630765497..633a1b7a466 100644 --- a/sdk/lib/vcruntime/CMakeLists.txt +++ b/sdk/lib/vcruntime/CMakeLists.txt @@ -24,6 +24,7 @@ list(APPEND VCRT_COMMON_SOURCES _fltused.c initializers.cpp isa_available.cpp + tlssup.c ) list(APPEND VCRT_RUNTIME_SOURCES diff --git a/sdk/lib/vcruntime/inc/internal_shared.h b/sdk/lib/vcruntime/inc/internal_shared.h index 892799639fc..968f5ecb498 100644 --- a/sdk/lib/vcruntime/inc/internal_shared.h +++ b/sdk/lib/vcruntime/inc/internal_shared.h @@ -38,6 +38,20 @@ extern "C" { #pragma section(".CRT$XPZ", long, read) #pragma section(".CRT$XTA", long, read) #pragma section(".CRT$XTZ", long, read) + + +#pragma section(".CRT$XDA",long,read) +#pragma section(".CRT$XDC",long,read) +#pragma section(".CRT$XDZ",long,read) + +#pragma section(".CRT$XLA",long,read) // TLS callback start +#pragma section(".CRT$XLC",long,read) // TLS constructors +#pragma section(".CRT$XLD",long,read) // TLS destructors +#pragma section(".CRT$XLZ",long,read) // TLS callback end + +#pragma section(".tls",long,read,write) +#pragma section(".tls$AAA",long,read,write) +#pragma section(".tls$ZZZ",long,read,write) #endif extern _PIFV __xi_a[]; @@ -51,6 +65,8 @@ extern _PVFV __xt_z[]; extern char __ImageBase; +extern const IMAGE_TLS_DIRECTORY _tls_used; + #define CRT_WARNING_DISABLE_PUSH(wn, message) \ __pragma(warning(push)) \ __pragma(warning(disable: wn)) diff --git a/sdk/lib/vcruntime/tlssup.c b/sdk/lib/vcruntime/tlssup.c new file mode 100644 index 00000000000..ba3f97ba1ce --- /dev/null +++ b/sdk/lib/vcruntime/tlssup.c @@ -0,0 +1,38 @@ +// +// tlssup.c +// +// Copyright (c) 2025 Timo Kreuzer +// +// TLS callback support +// +// SPDX-License-Identifier: MIT +// + +#include + +// Dummy TLS undex +unsigned int _tls_index; + +// The TLS data +_CRTALLOC(".tls") char *_tls_start = NULL; +_CRTALLOC(".tls$ZZZ") char *_tls_end = NULL; + +// Describes the range of TLS callbacks. +_CRTALLOC(".CRT$XLA") PIMAGE_TLS_CALLBACK __xl_a = 0; +_CRTALLOC(".CRT$XLZ") PIMAGE_TLS_CALLBACK __xl_z = 0; + +// +// The TLS directory. +// The linker will point the executables TLS data directory entry to this. +// Must be force-included with "#pragma comment(linker, "/INCLUDE:_tls_used")" +// or by referencing it from another compilation unit. +// +const IMAGE_TLS_DIRECTORY _tls_used = +{ + (ULONG_PTR)&_tls_start, + (ULONG_PTR)&_tls_end, + (ULONG_PTR)&_tls_index, + (ULONG_PTR)(&__xl_a + 1), + (ULONG)0, + (ULONG)0 +};