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 // To make sure we really traverse the whole list, we put down two elements, and |
| 25 // then check that we called both functions when we scanned the list. |
| 26 // Force a reference to p_thread_callback_dllmain_first_entry and |
| 27 // p_thread_callback_dllmain_first_entry to prevent whole program optimization |
| 28 // from discarding the variables. |
| 29 |
| 30 #include <windows.h> |
| 31 |
| 32 #include "base/logging.h" |
| 33 |
| 34 // Indicate if another service is scanning the callbacks. When this becomes |
| 35 // set to true, then DllMain() will stop supporting the callback service. This |
| 36 // value is set to true the first time any of our callbacks are called, as that |
| 37 // shows that some other service is handling callbacks. |
| 38 static bool linker_notifications_are_active = false; |
| 39 |
| 40 // This will be our mostly no-op callback that we'll list (twice). We won't |
| 41 // deliberately call it, and if it is called, that means we don't need to do any |
| 42 // of the callbacks anymore. We expect such a call to arrive via a |
| 43 // THREAD_ATTACH message, long before we'd have to perform our THREAD_DETACH |
| 44 // callbacks. |
| 45 static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved); |
| 46 |
| 47 #ifdef _WIN64 |
| 48 |
| 49 #pragma comment(linker, "/INCLUDE:_tls_used") |
| 50 #pragma comment(linker, "/INCLUDE:p_thread_callback_dllmain_first_entry") |
| 51 #pragma comment(linker, "/INCLUDE:p_thread_callback_dllmain_later_entry") |
| 52 |
| 53 #else // _WIN64 |
| 54 |
| 55 #pragma comment(linker, "/INCLUDE:__tls_used") |
| 56 #pragma comment(linker, "/INCLUDE:_p_thread_callback_dllmain_first_entry") |
| 57 #pragma comment(linker, "/INCLUDE:_p_thread_callback_dllmain_later_entry") |
| 58 |
| 59 #endif // _WIN64 |
| 60 |
| 61 // extern "C" suppresses C++ name mangling so we know the symbol names for the |
| 62 // linker /INCLUDE:symbol pragmas above. |
| 63 extern "C" { |
| 64 #ifdef _WIN64 |
| 65 |
| 66 // .CRT section is merged with .rdata on x64 so it must be constant data. |
| 67 #pragma data_seg(push, old_seg) |
| 68 // Use earliest possible name in the .CRT$XL? list of segments. |
| 69 #pragma const_seg(".CRT$XLA") |
| 70 // When defining a const variable, it must have external linkage to be sure the |
| 71 // linker doesn't discard it. |
| 72 extern const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_first_entry; |
| 73 const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_first_entry = on_callback; |
| 74 // The latest possible name in the .CRT$XL? (other than *Z, which has a NULL). |
| 75 #pragma const_seg(".CRT$XLY") |
| 76 extern const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_later_entry; |
| 77 const PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_later_entry = on_callback; |
| 78 #pragma data_seg(pop, old_seg) |
| 79 |
| 80 #else // _WIN64 |
| 81 |
| 82 #pragma data_seg(push, old_seg) |
| 83 // Use earliest possible name in the .CRT$XL? list of segments. |
| 84 #pragma data_seg(".CRT$XLA") |
| 85 PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_first_entry = on_callback; |
| 86 // The latest possible name in the .CRT$XL? (other than *Z, which has a NULL). |
| 87 #pragma data_seg(".CRT$XLY") |
| 88 PIMAGE_TLS_CALLBACK p_thread_callback_dllmain_later_entry = on_callback; |
| 89 #pragma data_seg(pop, old_seg) |
| 90 |
| 91 #endif // _WIN64 |
| 92 } // extern "C" |
| 93 |
| 94 |
| 95 // Make DllMain call the listed callbacks. This way any third parties that are |
| 96 // linked in will also be called. |
| 97 BOOL WINAPI DllMain(PVOID h, DWORD reason, PVOID reserved) { |
| 98 if (DLL_THREAD_DETACH != reason && DLL_PROCESS_DETACH != reason) |
| 99 return true; // We won't service THREAD_ATTACH calls. |
| 100 |
| 101 if (linker_notifications_are_active) |
| 102 return true; // Some other service is doing this work. |
| 103 |
| 104 // Use our first entry as a starting point, but don't call it. |
| 105 PIMAGE_TLS_CALLBACK* it = &p_thread_callback_dllmain_first_entry; |
| 106 while (*++it != NULL || it < &p_thread_callback_dllmain_later_entry) { |
| 107 if (*it == NULL || *it == on_callback) |
| 108 continue; // Don't bother to call our own callback. |
| 109 (*it)(h, reason, reserved); |
| 110 } |
| 111 return true; |
| 112 } |
| 113 |
| 114 static void NTAPI on_callback(PVOID h, DWORD reason, PVOID reserved) { |
| 115 // Do nothing. We were just a place holder in the list used to test that we |
| 116 // call all items. |
| 117 // If we are called, it means that some other system is scanning the callbacks |
| 118 // and we don't need to do so in DllMain(). |
| 119 linker_notifications_are_active = true; |
| 120 // Note: If some other routine some how plays this same game... we could both |
| 121 // decide not to do the scanning <sigh>, but this trick should suppress |
| 122 // duplicate calls on Vista, where the runtime takes care of the callbacks, |
| 123 // and allow us to do the callbacks on XP, where we are currently devoid of |
| 124 // callbacks (due to an explicit LoadLibrary call). |
| 125 } |
OLD | NEW |