Ticket #12730: tss_pe.cpp

File tss_pe.cpp, 10.8 KB (added by z7z7z@…, 6 years ago)

Fixed tss_pe.cpp

Line 
1// $Id$
2// (C) Copyright Aaron W. LaFramboise, Roland Schwarz, Michael Glassford 2004.
3// (C) Copyright 2007 Roland Schwarz
4// (C) Copyright 2007 Anthony Williams
5// (C) Copyright 2007 David Deakins
6// Use, modification and distribution are subject to the
7// Boost Software License, Version 1.0. (See accompanying file
8// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10#include <boost/detail/winapi/config.hpp>
11#include <boost/thread/detail/config.hpp>
12
13#if defined(BOOST_HAS_WINTHREADS) && defined(BOOST_THREAD_BUILD_LIB)
14
15#if (defined(__MINGW32__) && !defined(_WIN64)) || defined(__MINGW64__) || (__MINGW64_VERSION_MAJOR)
16
17#include <boost/thread/detail/tss_hooks.hpp>
18
19#include <windows.h>
20
21#include <cstdlib>
22
23namespace boost
24{
25 void tss_cleanup_implemented() {}
26}
27
28namespace {
29 void NTAPI on_tls_callback(void* , DWORD dwReason, PVOID )
30 {
31 switch (dwReason)
32 {
33 case DLL_THREAD_DETACH:
34 {
35 boost::on_thread_exit();
36 break;
37 }
38 }
39 }
40}
41
42#if defined(__MINGW64__) || (__MINGW64_VERSION_MAJOR) || (__MINGW32_MAJOR_VERSION >3) || \
43 ((__MINGW32_MAJOR_VERSION==3) && (__MINGW32_MINOR_VERSION>=18))
44extern "C"
45{
46 PIMAGE_TLS_CALLBACK __crt_xl_tls_callback__ __attribute__ ((section(".CRT$XLB"))) = on_tls_callback;
47}
48#else
49extern "C" {
50
51 void (* after_ctors )() __attribute__((section(".ctors"))) = boost::on_process_enter;
52 void (* before_dtors)() __attribute__((section(".dtors"))) = boost::on_thread_exit;
53 void (* after_dtors )() __attribute__((section(".dtors.zzz"))) = boost::on_process_exit;
54
55 ULONG __tls_index__ = 0;
56 char __tls_end__ __attribute__((section(".tls$zzz"))) = 0;
57 char __tls_start__ __attribute__((section(".tls"))) = 0;
58
59
60 PIMAGE_TLS_CALLBACK __crt_xl_start__ __attribute__ ((section(".CRT$XLA"))) = 0;
61 PIMAGE_TLS_CALLBACK __crt_xl_end__ __attribute__ ((section(".CRT$XLZ"))) = 0;
62}
63extern "C" const IMAGE_TLS_DIRECTORY32 _tls_used __attribute__ ((section(".rdata$T"))) =
64{
65 (DWORD) &__tls_start__,
66 (DWORD) &__tls_end__,
67 (DWORD) &__tls_index__,
68 (DWORD) (&__crt_xl_start__+1),
69 (DWORD) 0,
70 (DWORD) 0
71};
72#endif
73
74
75#elif defined(_MSC_VER) && !defined(UNDER_CE)
76
77 #include <boost/thread/detail/tss_hooks.hpp>
78
79 #include <stdlib.h>
80
81 #include <windows.h>
82
83
84// _pRawDllMainOrig can be defined by including boost/thread/win32/mfc_thread_init.hpp
85// into your dll; it ensures that MFC-Dll-initialization will be done properly
86// The following code is adapted from the MFC-Dll-init code
87/*
88 * _pRawDllMainOrig MUST be an extern const variable, which will be aliased to
89 * _pDefaultRawDllMainOrig if no real user definition is present, thanks to the
90 * alternatename directive.
91 */
92
93// work at least with _MSC_VER 1500 (MSVC++ 9.0, VS 2008)
94#if (_MSC_VER >= 1500)
95
96extern "C" {
97extern BOOL (WINAPI * const _pRawDllMainOrig)(HANDLE, DWORD, LPVOID);
98extern BOOL (WINAPI * const _pDefaultRawDllMainOrig)(HANDLE, DWORD, LPVOID) = NULL;
99#if defined (_M_IX86)
100#pragma comment(linker, "/alternatename:__pRawDllMainOrig=__pDefaultRawDllMainOrig")
101#elif defined (_M_X64) || defined (_M_ARM)
102#pragma comment(linker, "/alternatename:_pRawDllMainOrig=_pDefaultRawDllMainOrig")
103#else /* defined (_M_X64) || defined (_M_ARM) */
104#error Unsupported platform
105#endif /* defined (_M_X64) || defined (_M_ARM) */
106}
107
108#endif
109
110
111
112
113 //Definitions required by implementation
114
115 #if (_MSC_VER < 1300) || ((_MSC_VER > 1900) && (_MSC_VER < 1910)) // 1300 == VC++ 7.0, 1900 == VC++ 14.0, 1910 == VC++ 2017
116 typedef void ( __cdecl *_PVFV_ )();
117 typedef void ( __cdecl *_PIFV_ )();
118 #define INIRETSUCCESS_V
119 #define INIRETSUCCESS_I
120 #define PVAPI_V void __cdecl
121 #define PVAPI_I void __cdecl
122 #elif (_MSC_VER >= 1910)
123 typedef void ( __cdecl *_PVFV_ )();
124 typedef int ( __cdecl *_PIFV_ )();
125 #define INIRETSUCCESS_V
126 #define INIRETSUCCESS_I 0
127 #define PVAPI_V void __cdecl
128 #define PVAPI_I int __cdecl
129 #else
130 typedef int ( __cdecl *_PVFV_ )();
131 typedef int ( __cdecl *_PIFV_ )();
132 #define INIRETSUCCESS_V 0
133 #define INIRETSUCCESS_I 0
134 #define PVAPI_V int __cdecl
135 #define PVAPI_I int __cdecl
136 #endif
137
138 typedef void (NTAPI* _TLSCB)(HINSTANCE, DWORD, PVOID);
139
140 //Symbols for connection to the runtime environment
141
142 extern "C"
143 {
144 extern DWORD _tls_used; //the tls directory (located in .rdata segment)
145 extern _TLSCB __xl_a[], __xl_z[]; //tls initializers */
146 }
147
148 namespace
149 {
150 //Forward declarations
151
152 static PVAPI_I on_tls_prepare();
153 static PVAPI_V on_process_init();
154 static PVAPI_V on_process_term();
155 static void NTAPI on_tls_callback(HINSTANCE, DWORD, PVOID);
156
157 //The .CRT$Xxx information is taken from Codeguru:
158 //http://www.codeguru.com/Cpp/misc/misc/threadsprocesses/article.php/c6945__2/
159
160#if (_MSC_VER >= 1400)
161#pragma section(".CRT$XIU",long,read)
162#pragma section(".CRT$XCU",long,read)
163#pragma section(".CRT$XTU",long,read)
164#pragma section(".CRT$XLC",long,read)
165 __declspec(allocate(".CRT$XLC")) _TLSCB __xl_ca=on_tls_callback;
166 __declspec(allocate(".CRT$XIU"))_PIFV_ p_tls_prepare = on_tls_prepare;
167 __declspec(allocate(".CRT$XCU"))_PVFV_ p_process_init = on_process_init;
168 __declspec(allocate(".CRT$XTU"))_PVFV_ p_process_term = on_process_term;
169#else
170 #if (_MSC_VER >= 1300) // 1300 == VC++ 7.0
171 # pragma data_seg(push, old_seg)
172 #endif
173 //Callback to run tls glue code first.
174 //I don't think it is necessary to run it
175 //at .CRT$XIB level, since we are only
176 //interested in thread detachement. But
177 //this could be changed easily if required.
178
179 #pragma data_seg(".CRT$XIU")
180 static _PIFV_ p_tls_prepare = on_tls_prepare;
181 #pragma data_seg()
182
183 //Callback after all global ctors.
184
185 #pragma data_seg(".CRT$XCU")
186 static _PVFV_ p_process_init = on_process_init;
187 #pragma data_seg()
188
189 //Callback for tls notifications.
190
191 #pragma data_seg(".CRT$XLB")
192 _TLSCB p_thread_callback = on_tls_callback;
193 #pragma data_seg()
194 //Callback for termination.
195
196 #pragma data_seg(".CRT$XTU")
197 static _PVFV_ p_process_term = on_process_term;
198 #pragma data_seg()
199 #if (_MSC_VER >= 1300) // 1300 == VC++ 7.0
200 # pragma data_seg(pop, old_seg)
201 #endif
202#endif
203
204#ifdef BOOST_MSVC
205#pragma warning(push)
206#pragma warning(disable:4189)
207#endif
208
209 PVAPI_I on_tls_prepare()
210 {
211 //The following line has an important side effect:
212 //if the TLS directory is not already there, it will
213 //be created by the linker. In other words, it forces a tls
214 //directory to be generated by the linker even when static tls
215 //(i.e. __declspec(thread)) is not used.
216 //The volatile should prevent the optimizer
217 //from removing the reference.
218
219 DWORD volatile dw = _tls_used;
220
221 #if (_MSC_VER < 1300) // 1300 == VC++ 7.0
222 _TLSCB* pfbegin = __xl_a;
223 _TLSCB* pfend = __xl_z;
224 _TLSCB* pfdst = pfbegin;
225 //pfdst = (_TLSCB*)_tls_used.AddressOfCallBacks;
226
227 //The following loop will merge the address pointers
228 //into a contiguous area, since the tlssup code seems
229 //to require this (at least on MSVC 6)
230
231 while (pfbegin < pfend)
232 {
233 if (*pfbegin != 0)
234 {
235 *pfdst = *pfbegin;
236 ++pfdst;
237 }
238 ++pfbegin;
239 }
240
241 *pfdst = 0;
242 #endif
243
244 return INIRETSUCCESS_I;
245 }
246#ifdef BOOST_MSVC
247#pragma warning(pop)
248#endif
249
250 PVAPI_V on_process_init()
251 {
252 //Schedule on_thread_exit() to be called for the main
253 //thread before destructors of global objects have been
254 //called.
255
256 //It will not be run when 'quick' exiting the
257 //library; however, this is the standard behaviour
258 //for destructors of global objects, so that
259 //shouldn't be a problem.
260
261 atexit(boost::on_thread_exit);
262
263 //Call Boost process entry callback here
264
265 boost::on_process_enter();
266
267 return INIRETSUCCESS_V;
268 }
269
270 PVAPI_V on_process_term()
271 {
272 boost::on_process_exit();
273 return INIRETSUCCESS_V;
274 }
275
276 void NTAPI on_tls_callback(HINSTANCE /*h*/, DWORD dwReason, PVOID /*pv*/)
277 {
278 switch (dwReason)
279 {
280 case DLL_THREAD_DETACH:
281 boost::on_thread_exit();
282 break;
283 }
284 }
285
286#if (_MSC_VER >= 1500)
287 BOOL WINAPI dll_callback(HANDLE hInstance, DWORD dwReason, LPVOID lpReserved)
288#else
289 BOOL WINAPI dll_callback(HANDLE, DWORD dwReason, LPVOID)
290#endif
291 {
292 switch (dwReason)
293 {
294 case DLL_THREAD_DETACH:
295 boost::on_thread_exit();
296 break;
297 case DLL_PROCESS_DETACH:
298 boost::on_process_exit();
299 break;
300 }
301
302#if (_MSC_VER >= 1500)
303 if( _pRawDllMainOrig )
304 {
305 return _pRawDllMainOrig(hInstance, dwReason, lpReserved);
306 }
307#endif
308 return true;
309 }
310 } //namespace
311
312extern "C"
313{
314 extern BOOL (WINAPI * const _pRawDllMain)(HANDLE, DWORD, LPVOID)=&dll_callback;
315}
316namespace boost
317{
318 void tss_cleanup_implemented()
319 {
320 /*
321 This function's sole purpose is to cause a link error in cases where
322 automatic tss cleanup is not implemented by Boost.Threads as a
323 reminder that user code is responsible for calling the necessary
324 functions at the appropriate times (and for implementing an a
325 tss_cleanup_implemented() function to eliminate the linker's
326 missing symbol error).
327
328 If Boost.Threads later implements automatic tss cleanup in cases
329 where it currently doesn't (which is the plan), the duplicate
330 symbol error will warn the user that their custom solution is no
331 longer needed and can be removed.
332 */
333 }
334}
335
336#endif //defined(_MSC_VER) && !defined(UNDER_CE)
337
338#endif //defined(BOOST_HAS_WINTHREADS) && defined(BOOST_THREAD_BUILD_LIB)