[llvm-commits] [PATCH] Global dtors should be in .CRT$ section for MS C Runtime

Kai kai at redstar.de
Tue Sep 11 13:25:11 PDT 2012


Hi!

Global dtors should be placed in the .CRT$XT* COFF section if targeting 
the MS C Runtime. Then they are automatically executed by the runtime. 
This only works with the statically linked runtime (switches /MT or 
/MTd). Test case is included.

Regards
Kai
-------------- next part --------------
diff --git a/lib/MC/MCObjectFileInfo.cpp b/lib/MC/MCObjectFileInfo.cpp
index 29b4a94..8053624 100644
--- a/lib/MC/MCObjectFileInfo.cpp
+++ b/lib/MC/MCObjectFileInfo.cpp
@@ -430,12 +430,20 @@ void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
   }
 
 
-  StaticDtorSection =
-    Ctx->getCOFFSection(".dtors",
-                        COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
-                        COFF::IMAGE_SCN_MEM_READ |
-                        COFF::IMAGE_SCN_MEM_WRITE,
-                        SectionKind::getDataRel());
+  if (T.getOS() == Triple::Win32) {
+    StaticDtorSection =
+      Ctx->getCOFFSection(".CRT$XTX",
+                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
+                          COFF::IMAGE_SCN_MEM_READ,
+                          SectionKind::getReadOnly());
+  } else {
+    StaticDtorSection =
+      Ctx->getCOFFSection(".dtors",
+                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
+                          COFF::IMAGE_SCN_MEM_READ |
+                          COFF::IMAGE_SCN_MEM_WRITE,
+                          SectionKind::getDataRel());
+  }
 
   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
   // though it contains relocatable pointers.  In PIC mode, this is probably a
diff --git a/test/MC/COFF/global_dtors.ll b/test/MC/COFF/global_dtors.ll
new file mode 100644
index 0000000..0679c09
--- /dev/null
+++ b/test/MC/COFF/global_dtors.ll
@@ -0,0 +1,28 @@
+; Test that global dtors are emitted into the proper COFF section for the
+; target. Mingw uses .dtors, whereas MSVC uses .CRT$XT*.
+; RUN: llc < %s -mtriple i686-pc-win32 | FileCheck %s --check-prefix WIN32
+; RUN: llc < %s -mtriple x86_64-pc-win32 | FileCheck %s --check-prefix WIN32
+; RUN: llc < %s -mtriple i686-pc-mingw32 | FileCheck %s --check-prefix MINGW32
+; RUN: llc < %s -mtriple x86_64-pc-mingw32 | FileCheck %s --check-prefix MINGW32
+
+ at .str = private unnamed_addr constant [13 x i8] c"constructing\00", align 1
+ at .str2 = private unnamed_addr constant [5 x i8] c"main\00", align 1
+
+ at llvm.global_dtors = appending global [1 x { i32, void ()* }] [{ i32, void ()* } { i32 65535, void ()* @a_global_dtor }]
+
+declare i32 @puts(i8*)
+
+define void @a_global_dtor() nounwind {
+  %1 = call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @.str, i32 0, i32 0))
+  ret void
+}
+
+define i32 @main() nounwind {
+  %1 = call i32 @puts(i8* getelementptr inbounds ([5 x i8]* @.str2, i32 0, i32 0))
+  ret i32 0
+}
+
+; WIN32: .section .CRT$XTX,"r"
+; WIN32: a_global_dtor
+; MINGW32: .section .dtors,"w"
+; MINGW32: a_global_dtor


More information about the llvm-commits mailing list