Add initial qualcomm support.
[official-gcc.git] / libsanitizer / asan / asan_win_dynamic_runtime_thunk.cc
blob69da2a83c47cf8dd4d6b03095190d301762b88f8
1 //===-- asan_win_uar_thunk.cc ---------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // This file defines things that need to be present in the application modules
11 // to interact with the ASan DLL runtime correctly and can't be implemented
12 // using the default "import library" generated when linking the DLL RTL.
14 // This includes:
15 // - forwarding the detect_stack_use_after_return runtime option
16 // - working around deficiencies of the MD runtime
17 // - installing a custom SEH handlerx
19 //===----------------------------------------------------------------------===//
21 // Only compile this code when buidling asan_dynamic_runtime_thunk.lib
22 // Using #ifdef rather than relying on Makefiles etc.
23 // simplifies the build procedure.
24 #ifdef ASAN_DYNAMIC_RUNTIME_THUNK
25 #include <windows.h>
27 // First, declare CRT sections we'll be using in this file
28 #pragma section(".CRT$XID", long, read) // NOLINT
29 #pragma section(".CRT$XIZ", long, read) // NOLINT
30 #pragma section(".CRT$XTW", long, read) // NOLINT
31 #pragma section(".CRT$XTY", long, read) // NOLINT
33 ////////////////////////////////////////////////////////////////////////////////
34 // Define a copy of __asan_option_detect_stack_use_after_return that should be
35 // used when linking an MD runtime with a set of object files on Windows.
37 // The ASan MD runtime dllexports '__asan_option_detect_stack_use_after_return',
38 // so normally we would just dllimport it. Unfortunately, the dllimport
39 // attribute adds __imp_ prefix to the symbol name of a variable.
40 // Since in general we don't know if a given TU is going to be used
41 // with a MT or MD runtime and we don't want to use ugly __imp_ names on Windows
42 // just to work around this issue, let's clone the a variable that is
43 // constant after initialization anyways.
44 extern "C" {
45 __declspec(dllimport) int __asan_should_detect_stack_use_after_return();
46 int __asan_option_detect_stack_use_after_return =
47 __asan_should_detect_stack_use_after_return();
50 ////////////////////////////////////////////////////////////////////////////////
51 // For some reason, the MD CRT doesn't call the C/C++ terminators during on DLL
52 // unload or on exit. ASan relies on LLVM global_dtors to call
53 // __asan_unregister_globals on these events, which unfortunately doesn't work
54 // with the MD runtime, see PR22545 for the details.
55 // To work around this, for each DLL we schedule a call to UnregisterGlobals
56 // using atexit() that calls a small subset of C terminators
57 // where LLVM global_dtors is placed. Fingers crossed, no other C terminators
58 // are there.
59 extern "C" void __cdecl _initterm(void *a, void *b);
61 namespace {
62 __declspec(allocate(".CRT$XTW")) void* before_global_dtors = 0;
63 __declspec(allocate(".CRT$XTY")) void* after_global_dtors = 0;
65 void UnregisterGlobals() {
66 _initterm(&before_global_dtors, &after_global_dtors);
69 int ScheduleUnregisterGlobals() {
70 return atexit(UnregisterGlobals);
73 // We need to call 'atexit(UnregisterGlobals);' as early as possible, but after
74 // atexit() is initialized (.CRT$XIC). As this is executed before C++
75 // initializers (think ctors for globals), UnregisterGlobals gets executed after
76 // dtors for C++ globals.
77 __declspec(allocate(".CRT$XID"))
78 int (*__asan_schedule_unregister_globals)() = ScheduleUnregisterGlobals;
80 } // namespace
82 ////////////////////////////////////////////////////////////////////////////////
83 // ASan SEH handling.
84 // We need to set the ASan-specific SEH handler at the end of CRT initialization
85 // of each module (see also asan_win.cc).
86 extern "C" {
87 __declspec(dllimport) int __asan_set_seh_filter();
88 static int SetSEHFilter() { return __asan_set_seh_filter(); }
90 // Unfortunately, putting a pointer to __asan_set_seh_filter into
91 // __asan_intercept_seh gets optimized out, so we have to use an extra function.
92 __declspec(allocate(".CRT$XIZ")) int (*__asan_seh_interceptor)() = SetSEHFilter;
95 #endif // ASAN_DYNAMIC_RUNTIME_THUNK