sanitizer_win_defs.h 7.2 KB

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