1 //===-- sanitizer_win_defs.h ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Common definitions for Windows-specific code.
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_WIN_DEFS_H
13 #define SANITIZER_WIN_DEFS_H
15 #include "sanitizer_platform.h"
19 #if defined(_M_IX86) || defined(__i386__)
20 #define WINAPI __stdcall
26 #if defined(_M_IX86) || defined(__i386__)
27 #define WIN_SYM_PREFIX "_"
29 #define WIN_SYM_PREFIX
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.
37 #define WIN_EXPORT_PREFIX
39 #define WIN_EXPORT_PREFIX WIN_SYM_PREFIX
42 // Intermediate macro to ensure the parameter is expanded before stringified.
43 #define STRINGIFY_(A) #A
44 #define STRINGIFY(A) STRINGIFY_(A)
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()
66 #define WIN_WEAK_ALIAS(Name, Default) \
67 __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY(Name) "="\
68 WIN_SYM_PREFIX STRINGIFY(Default)))
70 #define WIN_FORCE_LINK(Name) \
71 __pragma(comment(linker, "/include:" WIN_SYM_PREFIX STRINGIFY(Name)))
73 #define WIN_EXPORT(ExportedName, Name) \
74 __pragma(comment(linker, "/export:" WIN_EXPORT_PREFIX STRINGIFY(ExportedName)\
75 "=" WIN_EXPORT_PREFIX STRINGIFY(Name)))
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
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
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__)
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))
106 // So, for Windows we provide something similar to weak symbols in Linux, with
108 // + A default implementation must always be provided.
110 // + When linking statically it works quite similarly. For example:
113 // WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
116 // // We can use the default implementation from the library:
118 // // Or we can override it:
119 // extern "C" bool compare (int a, int b) { return a >= b; }
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:".
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.
130 // WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }
133 // WIN_WEAK_IMPORT_DEF(compare)
134 // // We can use the default implementation from the library:
136 // // Or we can override it:
137 // extern "C" bool compare (int a, int b) { return a >= b; }
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.
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))
155 // WIN_WEAK_IMPORT_DEF(compare)
156 // // We override and export compare:
157 // extern "C" __declspec(dllexport) bool compare (int a, int b) {
161 #endif // SANITIZER_WINDOWS
162 #endif // SANITIZER_WIN_DEFS_H