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