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 #if !SANITIZER_GO
47 
48 // ----------------- A workaround for the absence of weak symbols --------------
49 // We don't have a direct equivalent of weak symbols when using MSVC, but we can
50 // use the /alternatename directive to tell the linker to default a specific
51 // symbol to a specific value.
52 // Take into account that this is a pragma directive for the linker, so it will
53 // be ignored by the compiler and the function will be marked as UNDEF in the
54 // symbol table of the resulting object file. The linker won't find the default
55 // implementation until it links with that object file.
56 // So, suppose we provide a default implementation "fundef" for "fun", and this
57 // is compiled into the object file "test.obj" including the pragma directive.
58 // If we have some code with references to "fun" and we link that code with
59 // "test.obj", it will work because the linker always link object files.
60 // But, if "test.obj" is included in a static library, like "test.lib", then the
61 // liker will only link to "test.obj" if necessary. If we only included the
62 // definition of "fun", it won't link to "test.obj" (from test.lib) because
63 // "fun" appears as UNDEF, so it doesn't resolve the symbol "fun", and will
64 // result in a link error (the linker doesn't find the pragma directive).
65 // So, a workaround is to force linkage with the modules that include weak
66 // definitions, with the following macro: WIN_FORCE_LINK()
67 
68 #define WIN_WEAK_ALIAS(Name, Default)                                          \
69   __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY(Name) "="\
70                                              WIN_SYM_PREFIX STRINGIFY(Default)))
71 
72 #define WIN_FORCE_LINK(Name)                                                   \
73   __pragma(comment(linker, "/include:" WIN_SYM_PREFIX STRINGIFY(Name)))
74 
75 #define WIN_EXPORT(ExportedName, Name)                                         \
76   __pragma(comment(linker, "/export:" WIN_EXPORT_PREFIX STRINGIFY(ExportedName)\
77                                   "=" WIN_EXPORT_PREFIX STRINGIFY(Name)))
78 
79 // We cannot define weak functions on Windows, but we can use WIN_WEAK_ALIAS()
80 // which defines an alias to a default implementation, and only works when
81 // linking statically.
82 // So, to define a weak function "fun", we define a default implementation with
83 // a different name "fun__def" and we create a "weak alias" fun = fun__def.
84 // Then, users can override it just defining "fun".
85 // We impose "extern "C"" because otherwise WIN_WEAK_ALIAS() will fail because
86 // of name mangling.
87 
88 // Dummy name for default implementation of weak function.
89 # define WEAK_DEFAULT_NAME(Name) Name##__def
90 // Name for exported implementation of weak function.
91 # define WEAK_EXPORT_NAME(Name) Name##__dll
92 
93 // Use this macro when you need to define and export a weak function from a
94 // library. For example:
95 //   WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
96 # define WIN_WEAK_EXPORT_DEF(ReturnType, Name, ...)                            \
97   WIN_WEAK_ALIAS(Name, WEAK_DEFAULT_NAME(Name))                                \
98   WIN_EXPORT(WEAK_EXPORT_NAME(Name), Name)                                     \
99   extern "C" ReturnType Name(__VA_ARGS__);                                     \
100   extern "C" ReturnType WEAK_DEFAULT_NAME(Name)(__VA_ARGS__)
101 
102 // Use this macro when you need to import a weak function from a library. It
103 // defines a weak alias to the imported function from the dll. For example:
104 //   WIN_WEAK_IMPORT_DEF(compare)
105 # define WIN_WEAK_IMPORT_DEF(Name)                                             \
106   WIN_WEAK_ALIAS(Name, WEAK_EXPORT_NAME(Name))
107 
108 // So, for Windows we provide something similar to weak symbols in Linux, with
109 // some differences:
110 // + A default implementation must always be provided.
111 //
112 // + When linking statically it works quite similarly. For example:
113 //
114 //   // libExample.cc
115 //   WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
116 //
117 //   // client.cc
118 //   // We can use the default implementation from the library:
119 //   compare(1, 2);
120 //   // Or we can override it:
121 //   extern "C" bool compare (int a, int b) { return a >= b; }
122 //
123 //  And it will work fine. If we don't override the function, we need to ensure
124 //  that the linker includes the object file with the default implementation.
125 //  We can do so with the linker option "-wholearchive:".
126 //
127 // + When linking dynamically with a library (dll), weak functions are exported
128 //  with "__dll" suffix. Clients can use the macro WIN_WEAK_IMPORT_DEF(fun)
129 //  which defines a "weak alias" fun = fun__dll.
130 //
131 //   // libExample.cc
132 //   WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
133 //
134 //   // client.cc
135 //   WIN_WEAK_IMPORT_DEF(compare)
136 //   // We can use the default implementation from the library:
137 //   compare(1, 2);
138 //   // Or we can override it:
139 //   extern "C" bool compare (int a, int b) { return a >= b; }
140 //
141 //  But if we override the function, the dlls don't have access to it (which
142 //  is different in linux). If that is desired, the strong definition must be
143 //  exported and interception can be used from the rest of the dlls.
144 //
145 //   // libExample.cc
146 //   WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
147 //   // When initialized, check if the main executable defined "compare".
148 //   int libExample_init() {
149 //     uptr fnptr = __interception::InternalGetProcAddress(
150 //         (void *)GetModuleHandleA(0), "compare");
151 //     if (fnptr && !__interception::OverrideFunction((uptr)compare, fnptr, 0))
152 //       abort();
153 //     return 0;
154 //   }
155 //
156 //   // client.cc
157 //   WIN_WEAK_IMPORT_DEF(compare)
158 //   // We override and export compare:
159 //   extern "C" __declspec(dllexport) bool compare (int a, int b) {
160 //     return a >= b;
161 //   }
162 //
163 
164 #else // SANITIZER_GO
165 
166 // Go neither needs nor wants weak references.
167 // The shenanigans above don't work for gcc.
168 # define WIN_WEAK_EXPORT_DEF(ReturnType, Name, ...)                            \
169   extern "C" ReturnType Name(__VA_ARGS__)
170 
171 #endif // SANITIZER_GO
172 
173 #endif // SANITIZER_WINDOWS
174 #endif // SANITIZER_WIN_DEFS_H
175