1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker // Windows doesn't support pthread_key_create's destr_function, and in fact
6*6777b538SAndroid Build Coastguard Worker // it's a bit tricky to get code to run when a thread exits. This is
7*6777b538SAndroid Build Coastguard Worker // cargo-cult magic from http://www.codeproject.com/threads/tls.asp.
8*6777b538SAndroid Build Coastguard Worker // We are trying to be compatible with both a LoadLibrary style invocation, as
9*6777b538SAndroid Build Coastguard Worker // well as static linking. This code only needs to be included if we use
10*6777b538SAndroid Build Coastguard Worker // LoadLibrary, but it hooks into the "standard" set of TLS callbacks that are
11*6777b538SAndroid Build Coastguard Worker // provided for static linking.
12*6777b538SAndroid Build Coastguard Worker
13*6777b538SAndroid Build Coastguard Worker // This code is deliberately written to match the style of calls seen in
14*6777b538SAndroid Build Coastguard Worker // base/threading/thread_local_storage_win.cc. Please keep the two in sync if
15*6777b538SAndroid Build Coastguard Worker // coding conventions are changed.
16*6777b538SAndroid Build Coastguard Worker
17*6777b538SAndroid Build Coastguard Worker // WARNING: Do *NOT* try to include this in the construction of the base
18*6777b538SAndroid Build Coastguard Worker // library, even though it potentially drives code in
19*6777b538SAndroid Build Coastguard Worker // base/threading/thread_local_storage_win.cc. If you do, some users will end
20*6777b538SAndroid Build Coastguard Worker // up getting duplicate definition of DllMain() in some of their later links.
21*6777b538SAndroid Build Coastguard Worker
22*6777b538SAndroid Build Coastguard Worker // Force a reference to _tls_used to make the linker create the TLS directory
23*6777b538SAndroid Build Coastguard Worker // if it's not already there (that is, even if __declspec(thread) is not used).
24*6777b538SAndroid Build Coastguard Worker // Force a reference to p_thread_callback_dllmain_typical_entry to prevent whole
25*6777b538SAndroid Build Coastguard Worker // program optimization from discarding the variables.
26*6777b538SAndroid Build Coastguard Worker
27*6777b538SAndroid Build Coastguard Worker #include <windows.h>
28*6777b538SAndroid Build Coastguard Worker
29*6777b538SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
30*6777b538SAndroid Build Coastguard Worker #include "base/win/win_util.h"
31*6777b538SAndroid Build Coastguard Worker
32*6777b538SAndroid Build Coastguard Worker // Indicate if another service is scanning the callbacks. When this becomes
33*6777b538SAndroid Build Coastguard Worker // set to true, then DllMain() will stop supporting the callback service. This
34*6777b538SAndroid Build Coastguard Worker // value is set to true the first time any of our callbacks are called, as that
35*6777b538SAndroid Build Coastguard Worker // shows that some other service is handling callbacks.
36*6777b538SAndroid Build Coastguard Worker static bool linker_notifications_are_active = false;
37*6777b538SAndroid Build Coastguard Worker
38*6777b538SAndroid Build Coastguard Worker // This will be our mostly no-op callback that we'll list. We won't
39*6777b538SAndroid Build Coastguard Worker // deliberately call it, and if it is called, that means we don't need to do any
40*6777b538SAndroid Build Coastguard Worker // of the callbacks anymore. We expect such a call to arrive via a
41*6777b538SAndroid Build Coastguard Worker // THREAD_ATTACH message, long before we'd have to perform our THREAD_DETACH
42*6777b538SAndroid Build Coastguard Worker // callbacks.
43*6777b538SAndroid Build Coastguard Worker static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved);
44*6777b538SAndroid Build Coastguard Worker
45*6777b538SAndroid Build Coastguard Worker #ifdef _WIN64
46*6777b538SAndroid Build Coastguard Worker
47*6777b538SAndroid Build Coastguard Worker #pragma comment(linker, "/INCLUDE:_tls_used")
48*6777b538SAndroid Build Coastguard Worker #pragma comment(linker, "/INCLUDE:p_thread_callback_dllmain_typical_entry")
49*6777b538SAndroid Build Coastguard Worker
50*6777b538SAndroid Build Coastguard Worker #else // _WIN64
51*6777b538SAndroid Build Coastguard Worker
52*6777b538SAndroid Build Coastguard Worker #pragma comment(linker, "/INCLUDE:__tls_used")
53*6777b538SAndroid Build Coastguard Worker #pragma comment(linker, "/INCLUDE:_p_thread_callback_dllmain_typical_entry")
54*6777b538SAndroid Build Coastguard Worker
55*6777b538SAndroid Build Coastguard Worker #endif // _WIN64
56*6777b538SAndroid Build Coastguard Worker
57*6777b538SAndroid Build Coastguard Worker // Explicitly depend on VC\crt\src\tlssup.c variables
58*6777b538SAndroid Build Coastguard Worker // to bracket the list of TLS callbacks.
59*6777b538SAndroid Build Coastguard Worker extern "C" PIMAGE_TLS_CALLBACK __xl_a, __xl_z;
60*6777b538SAndroid Build Coastguard Worker
61*6777b538SAndroid Build Coastguard Worker // extern "C" suppresses C++ name mangling so we know the symbol names for the
62*6777b538SAndroid Build Coastguard Worker // linker /INCLUDE:symbol pragmas above.
63*6777b538SAndroid Build Coastguard Worker extern "C" {
64*6777b538SAndroid Build Coastguard Worker #ifdef _WIN64
65*6777b538SAndroid Build Coastguard Worker
66*6777b538SAndroid Build Coastguard Worker // .CRT section is merged with .rdata on x64 so it must be constant data.
67*6777b538SAndroid Build Coastguard Worker #pragma data_seg(push, old_seg)
68*6777b538SAndroid Build Coastguard Worker // Use a typical possible name in the .CRT$XL? list of segments.
69*6777b538SAndroid Build Coastguard Worker #pragma const_seg(".CRT$XLB")
70*6777b538SAndroid Build Coastguard Worker // When defining a const variable, it must have external linkage to be sure the
71*6777b538SAndroid Build Coastguard Worker // linker doesn't discard it.
72*6777b538SAndroid Build Coastguard Worker extern const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry;
73*6777b538SAndroid Build Coastguard Worker const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry = on_callback;
74*6777b538SAndroid Build Coastguard Worker #pragma data_seg(pop, old_seg)
75*6777b538SAndroid Build Coastguard Worker
76*6777b538SAndroid Build Coastguard Worker #else // _WIN64
77*6777b538SAndroid Build Coastguard Worker
78*6777b538SAndroid Build Coastguard Worker #pragma data_seg(push, old_seg)
79*6777b538SAndroid Build Coastguard Worker // Use a typical possible name in the .CRT$XL? list of segments.
80*6777b538SAndroid Build Coastguard Worker #pragma data_seg(".CRT$XLB")
81*6777b538SAndroid Build Coastguard Worker PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_typical_entry = on_callback;
82*6777b538SAndroid Build Coastguard Worker #pragma data_seg(pop, old_seg)
83*6777b538SAndroid Build Coastguard Worker
84*6777b538SAndroid Build Coastguard Worker #endif // _WIN64
85*6777b538SAndroid Build Coastguard Worker } // extern "C"
86*6777b538SAndroid Build Coastguard Worker
87*6777b538SAndroid Build Coastguard Worker // Custom crash code to get a unique entry in crash reports.
CrashOnProcessDetach()88*6777b538SAndroid Build Coastguard Worker NOINLINE static void CrashOnProcessDetach() {
89*6777b538SAndroid Build Coastguard Worker *static_cast<volatile int*>(nullptr) = 0x356;
90*6777b538SAndroid Build Coastguard Worker }
91*6777b538SAndroid Build Coastguard Worker
92*6777b538SAndroid Build Coastguard Worker // Make DllMain call the listed callbacks. This way any third parties that are
93*6777b538SAndroid Build Coastguard Worker // linked in will also be called.
DllMain(PVOID h,DWORD reason,PVOID reserved)94*6777b538SAndroid Build Coastguard Worker BOOL WINAPI DllMain(PVOID h, DWORD reason, PVOID reserved) {
95*6777b538SAndroid Build Coastguard Worker if (DLL_PROCESS_DETACH == reason && base::win::ShouldCrashOnProcessDetach())
96*6777b538SAndroid Build Coastguard Worker CrashOnProcessDetach();
97*6777b538SAndroid Build Coastguard Worker
98*6777b538SAndroid Build Coastguard Worker if (DLL_THREAD_DETACH != reason && DLL_PROCESS_DETACH != reason)
99*6777b538SAndroid Build Coastguard Worker return true; // We won't service THREAD_ATTACH calls.
100*6777b538SAndroid Build Coastguard Worker
101*6777b538SAndroid Build Coastguard Worker if (linker_notifications_are_active)
102*6777b538SAndroid Build Coastguard Worker return true; // Some other service is doing this work.
103*6777b538SAndroid Build Coastguard Worker
104*6777b538SAndroid Build Coastguard Worker for (PIMAGE_TLS_CALLBACK* it = &__xl_a; it < &__xl_z; ++it) {
105*6777b538SAndroid Build Coastguard Worker if (*it == nullptr || *it == on_callback)
106*6777b538SAndroid Build Coastguard Worker continue; // Don't bother to call our own callback.
107*6777b538SAndroid Build Coastguard Worker (*it)(h, reason, reserved);
108*6777b538SAndroid Build Coastguard Worker }
109*6777b538SAndroid Build Coastguard Worker return true;
110*6777b538SAndroid Build Coastguard Worker }
111*6777b538SAndroid Build Coastguard Worker
on_callback(PVOID h,DWORD reason,PVOID reserved)112*6777b538SAndroid Build Coastguard Worker static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved) {
113*6777b538SAndroid Build Coastguard Worker // Do nothing. We were just a place holder in the list used to test that we
114*6777b538SAndroid Build Coastguard Worker // call all items.
115*6777b538SAndroid Build Coastguard Worker // If we are called, it means that some other system is scanning the callbacks
116*6777b538SAndroid Build Coastguard Worker // and we don't need to do so in DllMain().
117*6777b538SAndroid Build Coastguard Worker linker_notifications_are_active = true;
118*6777b538SAndroid Build Coastguard Worker // Note: If some other routine some how plays this same game... we could both
119*6777b538SAndroid Build Coastguard Worker // decide not to do the scanning <sigh>, but this trick should suppress
120*6777b538SAndroid Build Coastguard Worker // duplicate calls on Vista, where the runtime takes care of the callbacks,
121*6777b538SAndroid Build Coastguard Worker // and allow us to do the callbacks on XP, where we are currently devoid of
122*6777b538SAndroid Build Coastguard Worker // callbacks (due to an explicit LoadLibrary call).
123*6777b538SAndroid Build Coastguard Worker }
124