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