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