]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_win_defs.h
1 //===-- sanitizer_win_defs.h ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common definitions for Windows-specific code.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_WIN_DEFS_H
13 #define SANITIZER_WIN_DEFS_H
14
15 #include "sanitizer_platform.h"
16 #if SANITIZER_WINDOWS
17
18 #ifndef WINAPI
19 #if defined(_M_IX86) || defined(__i386__)
20 #define WINAPI __stdcall
21 #else
22 #define WINAPI
23 #endif
24 #endif
25
26 #if defined(_M_IX86) || defined(__i386__)
27 #define WIN_SYM_PREFIX "_"
28 #else
29 #define WIN_SYM_PREFIX
30 #endif
31
32 // For MinGW, the /export: directives contain undecorated symbols, contrary to
33 // link/lld-link. The GNU linker doesn't support /alternatename and /include
34 // though, thus lld-link in MinGW mode interprets them in the same way as
35 // in the default mode.
36 #ifdef __MINGW32__
37 #define WIN_EXPORT_PREFIX
38 #else
39 #define WIN_EXPORT_PREFIX WIN_SYM_PREFIX
40 #endif
41
42 // Intermediate macro to ensure the parameter is expanded before stringified.
43 #define STRINGIFY_(A) #A
44 #define STRINGIFY(A) STRINGIFY_(A)
45
46 // ----------------- A workaround for the absence of weak symbols --------------
47 // We don't have a direct equivalent of weak symbols when using MSVC, but we can
48 // use the /alternatename directive to tell the linker to default a specific
49 // symbol to a specific value.
50 // Take into account that this is a pragma directive for the linker, so it will
51 // be ignored by the compiler and the function will be marked as UNDEF in the
52 // symbol table of the resulting object file. The linker won't find the default
53 // implementation until it links with that object file.
54 // So, suppose we provide a default implementation "fundef" for "fun", and this
55 // is compiled into the object file "test.obj" including the pragma directive.
56 // If we have some code with references to "fun" and we link that code with
57 // "test.obj", it will work because the linker always link object files.
58 // But, if "test.obj" is included in a static library, like "test.lib", then the
59 // liker will only link to "test.obj" if necessary. If we only included the
60 // definition of "fun", it won't link to "test.obj" (from test.lib) because
61 // "fun" appears as UNDEF, so it doesn't resolve the symbol "fun", and will
62 // result in a link error (the linker doesn't find the pragma directive).
63 // So, a workaround is to force linkage with the modules that include weak
64 // definitions, with the following macro: WIN_FORCE_LINK()
65
66 #define WIN_WEAK_ALIAS(Name, Default)                                          \
67   __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY(Name) "="\
68                                              WIN_SYM_PREFIX STRINGIFY(Default)))
69
70 #define WIN_FORCE_LINK(Name)                                                   \
71   __pragma(comment(linker, "/include:" WIN_SYM_PREFIX STRINGIFY(Name)))
72
73 #define WIN_EXPORT(ExportedName, Name)                                         \
74   __pragma(comment(linker, "/export:" WIN_EXPORT_PREFIX STRINGIFY(ExportedName)\
75                                   "=" WIN_EXPORT_PREFIX STRINGIFY(Name)))
76
77 // We cannot define weak functions on Windows, but we can use WIN_WEAK_ALIAS()
78 // which defines an alias to a default implementation, and only works when
79 // linking statically.
80 // So, to define a weak function "fun", we define a default implementation with
81 // a different name "fun__def" and we create a "weak alias" fun = fun__def.
82 // Then, users can override it just defining "fun".
83 // We impose "extern "C"" because otherwise WIN_WEAK_ALIAS() will fail because
84 // of name mangling.
85
86 // Dummy name for default implementation of weak function.
87 # define WEAK_DEFAULT_NAME(Name) Name##__def
88 // Name for exported implementation of weak function.
89 # define WEAK_EXPORT_NAME(Name) Name##__dll
90
91 // Use this macro when you need to define and export a weak function from a
92 // library. For example:
93 //   WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
94 # define WIN_WEAK_EXPORT_DEF(ReturnType, Name, ...)                            \
95   WIN_WEAK_ALIAS(Name, WEAK_DEFAULT_NAME(Name))                                \
96   WIN_EXPORT(WEAK_EXPORT_NAME(Name), Name)                                     \
97   extern "C" ReturnType Name(__VA_ARGS__);                                     \
98   extern "C" ReturnType WEAK_DEFAULT_NAME(Name)(__VA_ARGS__)
99
100 // Use this macro when you need to import a weak function from a library. It
101 // defines a weak alias to the imported function from the dll. For example:
102 //   WIN_WEAK_IMPORT_DEF(compare)
103 # define WIN_WEAK_IMPORT_DEF(Name)                                             \
104   WIN_WEAK_ALIAS(Name, WEAK_EXPORT_NAME(Name))
105
106 // So, for Windows we provide something similar to weak symbols in Linux, with
107 // some differences:
108 // + A default implementation must always be provided.
109 //
110 // + When linking statically it works quite similarly. For example:
111 //
112 //   // libExample.cc
113 //   WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
114 //
115 //   // client.cc
116 //   // We can use the default implementation from the library:
117 //   compare(1, 2);
118 //   // Or we can override it:
119 //   extern "C" bool compare (int a, int b) { return a >= b; }
120 //
121 //  And it will work fine. If we don't override the function, we need to ensure
122 //  that the linker includes the object file with the default implementation.
123 //  We can do so with the linker option "-wholearchive:".
124 //
125 // + When linking dynamically with a library (dll), weak functions are exported
126 //  with "__dll" suffix. Clients can use the macro WIN_WEAK_IMPORT_DEF(fun)
127 //  which defines a "weak alias" fun = fun__dll.
128 //
129 //   // libExample.cc
130 //   WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
131 //
132 //   // client.cc
133 //   WIN_WEAK_IMPORT_DEF(compare)
134 //   // We can use the default implementation from the library:
135 //   compare(1, 2);
136 //   // Or we can override it:
137 //   extern "C" bool compare (int a, int b) { return a >= b; }
138 //
139 //  But if we override the function, the dlls don't have access to it (which
140 //  is different in linux). If that is desired, the strong definition must be
141 //  exported and interception can be used from the rest of the dlls.
142 //
143 //   // libExample.cc
144 //   WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
145 //   // When initialized, check if the main executable defined "compare".
146 //   int libExample_init() {
147 //     uptr fnptr = __interception::InternalGetProcAddress(
148 //         (void *)GetModuleHandleA(0), "compare");
149 //     if (fnptr && !__interception::OverrideFunction((uptr)compare, fnptr, 0))
150 //       abort();
151 //     return 0;
152 //   }
153 //
154 //   // client.cc
155 //   WIN_WEAK_IMPORT_DEF(compare)
156 //   // We override and export compare:
157 //   extern "C" __declspec(dllexport) bool compare (int a, int b) {
158 //     return a >= b;
159 //   }
160 //
161 #endif // SANITIZER_WINDOWS
162 #endif // SANITIZER_WIN_DEFS_H