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