[compiler-rt] ef8d9d1 - [compiler-rt] Retain sanitizer callbacks with MinGW GCC (#222711)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 14:13:00 PDT 2026


Author: oltolm
Date: 2026-09-24T14:12:54-07:00
New Revision: ef8d9d19fb232076b7a2070860666a65a2cf563e

URL: https://github.com/llvm/llvm-project/commit/ef8d9d19fb232076b7a2070860666a65a2cf563e
DIFF: https://github.com/llvm/llvm-project/commit/ef8d9d19fb232076b7a2070860666a65a2cf563e.diff

LOG: [compiler-rt] Retain sanitizer callbacks with MinGW GCC (#222711)

MSVC and clang-cl use /include directives emitted by WIN_FORCE_LINK. GNU
tools need an ordinary used relocation instead. Keep the sanitizer
initialization callbacks and ASan DSO registration object reachable when
linking with MinGW GCC.

---------

Co-authored-by: Hannes Domani <ssbssa at yahoo.de>

Added: 
    

Modified: 
    compiler-rt/lib/asan/asan_globals_win.cpp
    compiler-rt/lib/asan/asan_win.cpp
    compiler-rt/lib/asan/asan_win_common_runtime_thunk.h
    compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
    compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/asan_globals_win.cpp b/compiler-rt/lib/asan/asan_globals_win.cpp
index 40c47aea372e9..581e3a77c3d4f 100644
--- a/compiler-rt/lib/asan/asan_globals_win.cpp
+++ b/compiler-rt/lib/asan/asan_globals_win.cpp
@@ -12,6 +12,7 @@
 
 #include "asan_interface_internal.h"
 #if SANITIZER_WINDOWS
+#  include "asan_win_common_runtime_thunk.h"
 #  include "sanitizer_common/sanitizer_win_defs.h"
 
 #  if defined(__GNUC__) && !defined(__clang__)

diff  --git a/compiler-rt/lib/asan/asan_win.cpp b/compiler-rt/lib/asan/asan_win.cpp
index 46e3b57aa3668..776ab49de565a 100644
--- a/compiler-rt/lib/asan/asan_win.cpp
+++ b/compiler-rt/lib/asan/asan_win.cpp
@@ -24,6 +24,7 @@
 #  include "asan_report.h"
 #  include "asan_stack.h"
 #  include "asan_thread.h"
+#  include "asan_win_common_runtime_thunk.h"
 #  include "sanitizer_common/sanitizer_libc.h"
 #  include "sanitizer_common/sanitizer_mutex.h"
 #  include "sanitizer_common/sanitizer_win.h"

diff  --git a/compiler-rt/lib/asan/asan_win_common_runtime_thunk.h b/compiler-rt/lib/asan/asan_win_common_runtime_thunk.h
index 8cba5dabe3481..5ed4e56a618c6 100644
--- a/compiler-rt/lib/asan/asan_win_common_runtime_thunk.h
+++ b/compiler-rt/lib/asan/asan_win_common_runtime_thunk.h
@@ -14,6 +14,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+extern "C" void (*const __asan_dso_reg_hook)();
+
 #if defined(SANITIZER_STATIC_RUNTIME_THUNK) || \
     defined(SANITIZER_DYNAMIC_RUNTIME_THUNK)
 #  include "sanitizer_common/sanitizer_win_defs.h"

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
index 047a7286b1ad0..ec5fdfb6b6152 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
@@ -75,12 +75,18 @@
   __pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY(Name) "="\
                                              WIN_SYM_PREFIX STRINGIFY(Default)))
 
-#define WIN_FORCE_LINK(Name)                                                   \
-  __pragma(comment(linker, "/include:" WIN_SYM_PREFIX STRINGIFY(Name)))
-
 #define WIN_EXPORT(ExportedName, Name)                                         \
   __pragma(comment(linker, "/export:" WIN_EXPORT_PREFIX STRINGIFY(ExportedName)\
                                   "=" WIN_EXPORT_PREFIX STRINGIFY(Name)))
+#    if !defined(__GNUC__) || defined(__clang__)
+#      define WIN_FORCE_LINK(Name) \
+        __pragma(comment(linker, "/include:" WIN_SYM_PREFIX STRINGIFY(Name)))
+#    else
+#      define WIN_FORCE_LINK(Name)                                           \
+        extern "C" __typeof__(Name) Name;                                    \
+        static __attribute__((used)) __typeof__(&Name) __force_link_##Name = \
+            &Name;
+#    endif
 
 // We cannot define weak functions on Windows, but we can use WIN_WEAK_ALIAS()
 // which defines an alias to a default implementation, and only works when

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.cpp
index d9cf6ac0a76af..434b21d98e064 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_win_thunk_interception.cpp
@@ -91,6 +91,9 @@ extern "C" int __sanitizer_thunk_init() {
 #  pragma section(".CRT$XIB", long, read)
 extern "C" IN_SECTION(".CRT$XIB") int (*__sanitizer_thunk_init_ptr)() =
     __sanitizer_thunk_init;
+#  if defined(__GNUC__) && !defined(__clang__)
+WIN_FORCE_LINK(__sanitizer_thunk_init_ptr)
+#  endif
 
 static void WINAPI sanitizer_thunk_thread_init(void *mod, unsigned long reason,
                                                void *reserved) {
@@ -102,5 +105,8 @@ static void WINAPI sanitizer_thunk_thread_init(void *mod, unsigned long reason,
 extern "C" IN_SECTION(".CRT$XLAB") void(
     WINAPI* __sanitizer_thunk_thread_init_ptr)(void*, unsigned long, void*) =
     sanitizer_thunk_thread_init;
+#  if defined(__GNUC__) && !defined(__clang__)
+WIN_FORCE_LINK(__sanitizer_thunk_thread_init_ptr)
+#  endif
 #endif  // defined(SANITIZER_STATIC_RUNTIME_THUNK) ||
         // defined(SANITIZER_DYNAMIC_RUNTIME_THUNK)


        


More information about the llvm-commits mailing list