From 3f0b8e85d76c0d7cf317be1a52122155594b2646 Mon Sep 17 00:00:00 2001 From: keisawaakira <1351005284@qq.com> Date: Wed, 11 Nov 2026 11:16:04 +0900 Subject: [PATCH] Support the PE TLS callback in Windows ARM64X images An ARM64X image contains native ARM64 and ARM64EC code and can be loaded by both ARM64 and x64 processes. It has a single TLS directory whose callback array the linker builds from the native .CRT$XL* entries, so the callback registered by tss_pe.cpp (a function with internal linkage) is the native ARM64 one in both views: x64 processes loading an ARM64X image linked with the static library call native ARM64 code from the loader's TLS callback dispatch. In practice this breaks e.g. attaching a debugger to such x64 processes. When BOOST_THREAD_ARM64X is defined, register an extern "C" forwarder instead and mark it /ARM64XSAMEADDRESS from the ARM64EC object file, as the MSVC CRT does for __dyn_tls_init. The linker then emits a thunk that dispatches to the ARM64 forwarder in the native view and to the ARM64EC forwarder in the x64 view. The directive makes the ARM64EC object reference the ARM64 symbol, so the ARM64EC library has to be linked together with the ARM64 one (e.g. as an ARM64X library containing both, like the MSVC CRT libraries), also for ARM64EC-only images. This is why the behaviour is opt-in; without the macro the generated code is unchanged on all targets. --- doc/configuration.qbk | 18 ++++++++++++++++++ src/win32/tss_pe.cpp | 25 ++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/doc/configuration.qbk b/doc/configuration.qbk index c94f2d434..4f80fd91c 100644 --- a/doc/configuration.qbk +++ b/doc/configuration.qbk @@ -375,6 +375,24 @@ The user can request the version 4 by defining `BOOST_THREAD_VERSION` to 4. In t [endsect] +[section:arm64x Windows ARM64X images] + +An ARM64X image (DLL or EXE linked with `/MACHINE:ARM64X`) contains both native ARM64 code and ARM64EC code, +and can be loaded by both ARM64 and x64 processes. Its TLS callbacks are taken from the native ARM64 objects, +so unless configured as described below, x64 processes that load an ARM64X image linked with the static +Boost.Thread library call the native ARM64 TLS callback of the library, which does not work. + +`BOOST_THREAD_ARM64X` is a user-definable macro that is not defined by default. Users who link Boost.Thread +into ARM64X images must define it when compiling both the ARM64 and the ARM64EC variants of the static +library. Boost.Thread then registers its TLS callback so that the linker dispatches to the ARM64EC callback in +x64 processes. As a consequence, the ARM64EC variant references the ARM64 one: it must be linked together with +the ARM64 variant (for example, both variants merged into an ARM64X library with `lib /machine:arm64x`), also +when linking ARM64EC-only images. + +The macro has no effect on other targets. + +[endsect] + [endsect] [section:limitations Limitations] diff --git a/src/win32/tss_pe.cpp b/src/win32/tss_pe.cpp index 5a4c7649f..2e37a7223 100644 --- a/src/win32/tss_pe.cpp +++ b/src/win32/tss_pe.cpp @@ -154,6 +154,29 @@ extern BOOL (WINAPI * const _pDefaultRawDllMainOrig)(HINSTANCE, DWORD, LPVOID) = static void NTAPI on_tls_callback(HINSTANCE, DWORD, PVOID); } +#if defined(BOOST_THREAD_ARM64X) && (defined(_M_ARM64) || defined(_M_ARM64EC)) + // An ARM64X image has a single TLS directory, shared by its native ARM64 and its + // ARM64EC code; the linker builds the callback array from the native .CRT$XL* + // entries. A callback registered there must therefore resolve to the native + // function in the native view and to the ARM64EC function in the x64/ARM64EC view, + // otherwise x64 processes loading the image call native ARM64 code. Like the CRT + // does for __dyn_tls_init, /ARM64XSAMEADDRESS makes the linker emit a thunk that + // dispatches to the right function in each view. It needs a function with external + // linkage that is defined by both the ARM64 and the ARM64EC object files, so the + // ARM64EC library must be linked together with the ARM64 one (as an ARM64X library + // containing both, like the MSVC CRT libraries). + extern "C" void NTAPI boost_thread_arm64x_on_tls_callback(HINSTANCE h, DWORD dwReason, PVOID pv) + { + on_tls_callback(h, dwReason, pv); + } +#if defined(_M_ARM64EC) +#pragma comment(linker, "/arm64xsameaddress:boost_thread_arm64x_on_tls_callback") +#endif +#define BOOST_THREAD_DETAIL_TLS_CALLBACK boost_thread_arm64x_on_tls_callback +#else +#define BOOST_THREAD_DETAIL_TLS_CALLBACK on_tls_callback +#endif + namespace boost { //The .CRT$Xxx information is taken from Codeguru: @@ -167,7 +190,7 @@ extern BOOL (WINAPI * const _pDefaultRawDllMainOrig)(HINSTANCE, DWORD, LPVOID) = #pragma section(".CRT$XCU",long,read) #pragma section(".CRT$XTU",long,read) #pragma section(".CRT$XLC",long,read) - extern const __declspec(allocate(".CRT$XLC")) _TLSCB p_tls_callback = on_tls_callback; + extern const __declspec(allocate(".CRT$XLC")) _TLSCB p_tls_callback = BOOST_THREAD_DETAIL_TLS_CALLBACK; extern const __declspec(allocate(".CRT$XIU")) _PIFV_ p_tls_prepare = on_tls_prepare; extern const __declspec(allocate(".CRT$XCU")) _PVFV_ p_process_init = on_process_init; extern const __declspec(allocate(".CRT$XTU")) _PVFV_ p_process_term = on_process_term;