[ASan] Add __asan_init to the list of C dynamic initializers to support /MD on Windows
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@151059 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_rtl.cc b/lib/asan/asan_rtl.cc
index e0a9207..48d26e2 100644
--- a/lib/asan/asan_rtl.cc
+++ b/lib/asan/asan_rtl.cc
@@ -508,9 +508,15 @@
}
#if defined(ASAN_USE_PREINIT_ARRAY)
-// On Linux, we force __asan_init to be called before anyone else
-// by placing it into .preinit_array section.
-// FIXME: do we have anything like this on Mac?
-__attribute__((section(".preinit_array")))
- typeof(__asan_init) *__asan_preinit =__asan_init;
+ // On Linux, we force __asan_init to be called before anyone else
+ // by placing it into .preinit_array section.
+ // FIXME: do we have anything like this on Mac?
+ __attribute__((section(".preinit_array")))
+ typeof(__asan_init) *__asan_preinit =__asan_init;
+#elif defined(_WIN32) && defined(_DLL)
+ // On Windows, when using dynamic CRT (/MD), we can put a pointer
+ // to __asan_init into the global list of C initializers.
+ // See crt0dat.c in the CRT sources for the details.
+ #pragma section(".CRT$XIB",long,read)
+ __declspec(allocate(".CRT$XIB")) void (*__asan_preinit)() = __asan_init;
#endif