1 //===-- sanitizer_win_defs.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common definitions for Windows-specific code.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_WIN_DEFS_H
14 #define SANITIZER_WIN_DEFS_H
16 #include "sanitizer_platform.h"
21 #define WINAPI __stdcall
28 #define WIN_SYM_PREFIX
30 #define WIN_SYM_PREFIX "_"
33 // Intermediate macro to ensure the parameter is expanded before stringified.
34 #define STRINGIFY_(A) #A
35 #define STRINGIFY(A) STRINGIFY_(A)
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()
57 #define WIN_WEAK_ALIAS(Name, Default) \
58 __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY(Name) "="\
59 WIN_SYM_PREFIX STRINGIFY(Default)))
61 #define WIN_FORCE_LINK(Name) \
62 __pragma(comment(linker, "/include:" WIN_SYM_PREFIX STRINGIFY(Name)))
64 #define WIN_EXPORT(ExportedName, Name) \
65 __pragma(comment(linker, "/export:" WIN_SYM_PREFIX STRINGIFY(ExportedName) \
66 "=" WIN_SYM_PREFIX STRINGIFY(Name)))
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
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
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__)
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))
97 // So, for Windows we provide something similar to weak symbols in Linux, with
99 // + A default implementation must always be provided.
101 // + When linking statically it works quite similarly. For example:
104 // WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
107 // // We can use the default implementation from the library:
109 // // Or we can override it:
110 // extern "C" bool compare (int a, int b) { return a >= b; }
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:".
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.
121 // WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
124 // WIN_WEAK_IMPORT_DEF(compare)
125 // // We can use the default implementation from the library:
127 // // Or we can override it:
128 // extern "C" bool compare (int a, int b) { return a >= b; }
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.
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))
146 // WIN_WEAK_IMPORT_DEF(compare)
147 // // We override and export compare:
148 // extern "C" __declspec(dllexport) bool compare (int a, int b) {
152 #endif // SANITIZER_WINDOWS
153 #endif // SANITIZER_WIN_DEFS_H