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