175 lines · c
1//===-- sanitizer_win_defs.h ------------------------------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// Common definitions for Windows-specific code.10//11//===----------------------------------------------------------------------===//12#ifndef SANITIZER_WIN_DEFS_H13#define SANITIZER_WIN_DEFS_H14 15#include "sanitizer_platform.h"16#if SANITIZER_WINDOWS17 18#ifndef WINAPI19#if defined(_M_IX86) || defined(__i386__)20#define WINAPI __stdcall21#else22#define WINAPI23#endif24#endif25 26#if defined(_M_IX86) || defined(__i386__)27#define WIN_SYM_PREFIX "_"28#else29#define WIN_SYM_PREFIX30#endif31 32// For MinGW, the /export: directives contain undecorated symbols, contrary to33// link/lld-link. The GNU linker doesn't support /alternatename and /include34// though, thus lld-link in MinGW mode interprets them in the same way as35// in the default mode.36#ifdef __MINGW32__37#define WIN_EXPORT_PREFIX38#else39#define WIN_EXPORT_PREFIX WIN_SYM_PREFIX40#endif41 42// Intermediate macro to ensure the parameter is expanded before stringified.43#define STRINGIFY_(A) #A44#define STRINGIFY(A) STRINGIFY_(A)45 46#if !SANITIZER_GO47 48// ----------------- A workaround for the absence of weak symbols --------------49// We don't have a direct equivalent of weak symbols when using MSVC, but we can50// use the /alternatename directive to tell the linker to default a specific51// symbol to a specific value.52// Take into account that this is a pragma directive for the linker, so it will53// be ignored by the compiler and the function will be marked as UNDEF in the54// symbol table of the resulting object file. The linker won't find the default55// implementation until it links with that object file.56// So, suppose we provide a default implementation "fundef" for "fun", and this57// is compiled into the object file "test.obj" including the pragma directive.58// If we have some code with references to "fun" and we link that code with59// "test.obj", it will work because the linker always link object files.60// But, if "test.obj" is included in a static library, like "test.lib", then the61// liker will only link to "test.obj" if necessary. If we only included the62// definition of "fun", it won't link to "test.obj" (from test.lib) because63// "fun" appears as UNDEF, so it doesn't resolve the symbol "fun", and will64// result in a link error (the linker doesn't find the pragma directive).65// So, a workaround is to force linkage with the modules that include weak66// definitions, with the following macro: WIN_FORCE_LINK()67 68#define WIN_WEAK_ALIAS(Name, Default) \69 __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY(Name) "="\70 WIN_SYM_PREFIX STRINGIFY(Default)))71 72#define WIN_FORCE_LINK(Name) \73 __pragma(comment(linker, "/include:" WIN_SYM_PREFIX STRINGIFY(Name)))74 75#define WIN_EXPORT(ExportedName, Name) \76 __pragma(comment(linker, "/export:" WIN_EXPORT_PREFIX STRINGIFY(ExportedName)\77 "=" WIN_EXPORT_PREFIX STRINGIFY(Name)))78 79// We cannot define weak functions on Windows, but we can use WIN_WEAK_ALIAS()80// which defines an alias to a default implementation, and only works when81// linking statically.82// So, to define a weak function "fun", we define a default implementation with83// a different name "fun__def" and we create a "weak alias" fun = fun__def.84// Then, users can override it just defining "fun".85// We impose "extern "C"" because otherwise WIN_WEAK_ALIAS() will fail because86// of name mangling.87 88// Dummy name for default implementation of weak function.89# define WEAK_DEFAULT_NAME(Name) Name##__def90// Name for exported implementation of weak function.91# define WEAK_EXPORT_NAME(Name) Name##__dll92 93// Use this macro when you need to define and export a weak function from a94// library. For example:95// WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }96# define WIN_WEAK_EXPORT_DEF(ReturnType, Name, ...) \97 WIN_WEAK_ALIAS(Name, WEAK_DEFAULT_NAME(Name)) \98 WIN_EXPORT(WEAK_EXPORT_NAME(Name), Name) \99 extern "C" ReturnType Name(__VA_ARGS__); \100 extern "C" ReturnType WEAK_DEFAULT_NAME(Name)(__VA_ARGS__)101 102// Use this macro when you need to import a weak function from a library. It103// defines a weak alias to the imported function from the dll. For example:104// WIN_WEAK_IMPORT_DEF(compare)105# define WIN_WEAK_IMPORT_DEF(Name) \106 WIN_WEAK_ALIAS(Name, WEAK_EXPORT_NAME(Name))107 108// So, for Windows we provide something similar to weak symbols in Linux, with109// some differences:110// + A default implementation must always be provided.111//112// + When linking statically it works quite similarly. For example:113//114// // libExample.cc115// WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }116//117// // client.cc118// // We can use the default implementation from the library:119// compare(1, 2);120// // Or we can override it:121// extern "C" bool compare (int a, int b) { return a >= b; }122//123// And it will work fine. If we don't override the function, we need to ensure124// that the linker includes the object file with the default implementation.125// We can do so with the linker option "-wholearchive:".126//127// + When linking dynamically with a library (dll), weak functions are exported128// with "__dll" suffix. Clients can use the macro WIN_WEAK_IMPORT_DEF(fun)129// which defines a "weak alias" fun = fun__dll.130//131// // libExample.cc132// WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }133//134// // client.cc135// WIN_WEAK_IMPORT_DEF(compare)136// // We can use the default implementation from the library:137// compare(1, 2);138// // Or we can override it:139// extern "C" bool compare (int a, int b) { return a >= b; }140//141// But if we override the function, the dlls don't have access to it (which142// is different in linux). If that is desired, the strong definition must be143// exported and interception can be used from the rest of the dlls.144//145// // libExample.cc146// WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; }147// // When initialized, check if the main executable defined "compare".148// int libExample_init() {149// uptr fnptr = __interception::InternalGetProcAddress(150// (void *)GetModuleHandleA(0), "compare");151// if (fnptr && !__interception::OverrideFunction((uptr)compare, fnptr, 0))152// abort();153// return 0;154// }155//156// // client.cc157// WIN_WEAK_IMPORT_DEF(compare)158// // We override and export compare:159// extern "C" __declspec(dllexport) bool compare (int a, int b) {160// return a >= b;161// }162//163 164#else // SANITIZER_GO165 166// Go neither needs nor wants weak references.167// The shenanigans above don't work for gcc.168# define WIN_WEAK_EXPORT_DEF(ReturnType, Name, ...) \169 extern "C" ReturnType Name(__VA_ARGS__)170 171#endif // SANITIZER_GO172 173#endif // SANITIZER_WINDOWS174#endif // SANITIZER_WIN_DEFS_H175