aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/thread/src/win32/tss_pe.cpp
blob: 5a4c7649f5ba42f49d9381e0009c61360acd0edb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// $Id$
// (C) Copyright Aaron W. LaFramboise, Roland Schwarz, Michael Glassford 2004.
// (C) Copyright 2007 Roland Schwarz
// (C) Copyright 2007 Anthony Williams
// (C) Copyright 2007 David Deakins
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/winapi/config.hpp>
#include <boost/thread/detail/config.hpp>

#if defined(BOOST_THREAD_WIN32) && defined(BOOST_THREAD_BUILD_LIB)

#if (defined(__MINGW32__) && !defined(_WIN64)) || defined(__MINGW64__) || (__MINGW64_VERSION_MAJOR)

#include <boost/thread/detail/tss_hooks.hpp>

#include <windows.h>

#include <cstdlib>

namespace boost
{
    void tss_cleanup_implemented() {}
}

namespace {
    void NTAPI on_tls_callback(void* , DWORD dwReason, PVOID )
    {
        switch (dwReason)
        {
        case DLL_THREAD_DETACH:
        {
            boost::on_thread_exit();
            break;
        }
        }
    }
}

#if defined(__MINGW64__) || (__MINGW64_VERSION_MAJOR) || (__MINGW32__) || (__MINGW32_MAJOR_VERSION >3) ||   \
    ((__MINGW32_MAJOR_VERSION==3) && (__MINGW32_MINOR_VERSION>=18))
extern "C"
{
    PIMAGE_TLS_CALLBACK __crt_xl_tls_callback__ __attribute__ ((section(".CRT$XLB"))) = on_tls_callback;
}
#else
extern "C" {

    void (* after_ctors )() __attribute__((section(".ctors")))     = boost::on_process_enter;
    void (* before_dtors)() __attribute__((section(".dtors")))     = boost::on_thread_exit;
    void (* after_dtors )() __attribute__((section(".dtors.zzz"))) = boost::on_process_exit;

    ULONG __tls_index__ = 0;
    char __tls_end__ __attribute__((section(".tls$zzz"))) = 0;
    char __tls_start__ __attribute__((section(".tls"))) = 0;


    PIMAGE_TLS_CALLBACK __crt_xl_start__ __attribute__ ((section(".CRT$XLA"))) = 0;
    PIMAGE_TLS_CALLBACK __crt_xl_end__ __attribute__ ((section(".CRT$XLZ"))) = 0;
}
extern "C" const IMAGE_TLS_DIRECTORY32 _tls_used __attribute__ ((section(".rdata$T"))) =
{
        (DWORD) &__tls_start__,
        (DWORD) &__tls_end__,
        (DWORD) &__tls_index__,
        (DWORD) (&__crt_xl_start__+1),
        (DWORD) 0,
        (DWORD) 0
};
#endif


#elif  defined(_MSC_VER) && !defined(UNDER_CE)

    #include <boost/thread/detail/tss_hooks.hpp>

    #include <stdlib.h>

    #include <windows.h>


// _pRawDllMainOrig can be defined by including boost/thread/win32/mfc_thread_init.hpp
// into your dll; it ensures that MFC-Dll-initialization will be done properly
// The following code is adapted from the MFC-Dll-init code
/*
 * _pRawDllMainOrig MUST be an extern const variable, which will be aliased to
 * _pDefaultRawDllMainOrig if no real user definition is present, thanks to the
 * alternatename directive.
 */

// work at least with _MSC_VER 1500 (MSVC++ 9.0, VS 2008)
#if (_MSC_VER >= 1500)

extern "C" {
extern BOOL (WINAPI * const _pRawDllMainOrig)(HINSTANCE, DWORD, LPVOID);
extern BOOL (WINAPI * const _pDefaultRawDllMainOrig)(HINSTANCE, DWORD, LPVOID) = NULL;
#if defined (_M_IX86)
#pragma comment(linker, "/alternatename:__pRawDllMainOrig=__pDefaultRawDllMainOrig")
#elif defined (_M_X64) || defined (_M_ARM) || defined (_M_ARM64)
#pragma comment(linker, "/alternatename:_pRawDllMainOrig=_pDefaultRawDllMainOrig")
#else  /* unknown Windows target (not x86, x64, ARM, ARM64) */
#error Unsupported platform
#endif  /* defined (_M_X64) || defined (_M_ARM) || defined (_M_ARM64) */
}

#endif




    //Definitions required by implementation
    #if (_MSC_VER < 1300) || ((_MSC_VER > 1900) && (_MSC_VER < 1910)) // 1300 == VC++ 7.0, 1900 == VC++ 14.0, 1910 == VC++ 2017
        typedef void ( __cdecl *_PVFV_ )();
        typedef void ( __cdecl *_PIFV_ )();
        #define INIRETSUCCESS_V
        #define INIRETSUCCESS_I
        #define PVAPI_V void __cdecl
        #define PVAPI_I void __cdecl
    #elif (_MSC_VER >= 1910)
        typedef void ( __cdecl *_PVFV_ )();
        typedef int ( __cdecl *_PIFV_ )();
        #define INIRETSUCCESS_V
        #define INIRETSUCCESS_I 0
        #define PVAPI_V void __cdecl
        #define PVAPI_I int __cdecl
    #else
        typedef int ( __cdecl *_PVFV_ )();
        typedef int ( __cdecl *_PIFV_ )();
        #define INIRETSUCCESS_V 0
        #define INIRETSUCCESS_I 0
        #define PVAPI_V int __cdecl
        #define PVAPI_I int __cdecl
    #endif

    typedef void (NTAPI* _TLSCB)(HINSTANCE, DWORD, PVOID);

    //Symbols for connection to the runtime environment

    extern "C"
    {
        extern DWORD _tls_used; //the tls directory (located in .rdata segment)
        extern _TLSCB __xl_a[], __xl_z[];    //tls initializers */
    }

    namespace
    {
        //Forward declarations

        static PVAPI_I on_tls_prepare();
        static PVAPI_V on_process_init();
        static PVAPI_V on_process_term();
        static void NTAPI on_tls_callback(HINSTANCE, DWORD, PVOID);
    }

    namespace boost
    {
        //The .CRT$Xxx information is taken from Codeguru:
        //http://www.codeguru.com/Cpp/misc/misc/threadsprocesses/article.php/c6945__2/

        // Variables below are not referenced anywhere and
        // to not be optimized away has to have external linkage

#if (_MSC_VER >= 1400)
#pragma section(".CRT$XIU",long,read)
#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$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;
#else
        #if (_MSC_VER >= 1300) // 1300 == VC++ 7.0
        #   pragma data_seg(push, old_seg)
        #endif
            //Callback to run tls glue code first.
            //I don't think it is necessary to run it
            //at .CRT$XIB level, since we are only
            //interested in thread detachement. But
            //this could be changed easily if required.

            #pragma data_seg(".CRT$XIU")
            extern const _PIFV_ p_tls_prepare = on_tls_prepare;
            #pragma data_seg()

            //Callback after all global ctors.

            #pragma data_seg(".CRT$XCU")
            extern const _PVFV_ p_process_init = on_process_init;
            #pragma data_seg()

            //Callback for tls notifications.

            #pragma data_seg(".CRT$XLB")
            extern const _TLSCB p_thread_callback = on_tls_callback;
            #pragma data_seg()
            //Callback for termination.

            #pragma data_seg(".CRT$XTU")
            extern const _PVFV_ p_process_term = on_process_term;
            #pragma data_seg()
        #if (_MSC_VER >= 1300) // 1300 == VC++ 7.0
        #   pragma data_seg(pop, old_seg)
        #endif
#endif
    } // namespace boost

    namespace
    {
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4189)
#endif

        PVAPI_I on_tls_prepare()
        {
            //The following line has an important side effect:
            //if the TLS directory is not already there, it will
            //be created by the linker. In other words, it forces a tls
            //directory to be generated by the linker even when static tls
            //(i.e. __declspec(thread)) is not used.
            //The volatile should prevent the optimizer
            //from removing the reference.

            DWORD volatile dw = _tls_used;

            #if (_MSC_VER < 1300) // 1300 == VC++ 7.0
                _TLSCB* pfbegin = __xl_a;
                _TLSCB* pfend = __xl_z;
                _TLSCB* pfdst = pfbegin;
                //pfdst = (_TLSCB*)_tls_used.AddressOfCallBacks;

                //The following loop will merge the address pointers
                //into a contiguous area, since the tlssup code seems
                //to require this (at least on MSVC 6)

                while (pfbegin < pfend)
                {
                    if (*pfbegin != 0)
                    {
                        *pfdst = *pfbegin;
                        ++pfdst;
                    }
                    ++pfbegin;
                }

                *pfdst = 0;
            #endif

            return INIRETSUCCESS_I;
        }
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif

        PVAPI_V on_process_init()
        {
            //Schedule on_thread_exit() to be called for the main
            //thread before destructors of global objects have been
            //called.

            //It will not be run when 'quick' exiting the
            //library; however, this is the standard behaviour
            //for destructors of global objects, so that
            //shouldn't be a problem.

            atexit(boost::on_thread_exit);

            //Call Boost process entry callback here

            boost::on_process_enter();

            return INIRETSUCCESS_V;
        }

        PVAPI_V on_process_term()
        {
            boost::on_process_exit();
            return INIRETSUCCESS_V;
        }

        void NTAPI on_tls_callback(HINSTANCE /*h*/, DWORD dwReason, PVOID /*pv*/)
        {
            switch (dwReason)
            {
            case DLL_THREAD_DETACH:
                boost::on_thread_exit();
                break;
            }
        }

#if (_MSC_VER >= 1500)
        BOOL WINAPI dll_callback(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
#else
        BOOL WINAPI dll_callback(HINSTANCE, DWORD dwReason, LPVOID)
#endif
        {
            switch (dwReason)
            {
            case DLL_THREAD_DETACH:
                boost::on_thread_exit();
                break;
            case DLL_PROCESS_DETACH:
                boost::on_process_exit();
                break;
            }

#if (_MSC_VER >= 1500)
            if( _pRawDllMainOrig )
            {
                return _pRawDllMainOrig(hInstance, dwReason, lpReserved);
            }
#endif
            return true;
        }
    } //namespace

extern "C"
{
    extern BOOL (WINAPI * const _pRawDllMain)(HINSTANCE, DWORD, LPVOID)=&dll_callback;
}
namespace boost
{
    void tss_cleanup_implemented()
    {
        /*
        This function's sole purpose is to cause a link error in cases where
        automatic tss cleanup is not implemented by Boost.Threads as a
        reminder that user code is responsible for calling the necessary
        functions at the appropriate times (and for implementing an a
        tss_cleanup_implemented() function to eliminate the linker's
        missing symbol error).

        If Boost.Threads later implements automatic tss cleanup in cases
        where it currently doesn't (which is the plan), the duplicate
        symbol error will warn the user that their custom solution is no
        longer needed and can be removed.
        */
    }
}

#endif //defined(_MSC_VER) && !defined(UNDER_CE)

#endif //defined(BOOST_THREAD_WIN32) && defined(BOOST_THREAD_BUILD_LIB)