[PATCH] D51820: [COFF] Implement llvm.global_ctors priorities for MSVC COFF targets

Reid Kleckner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 7 15:32:38 PDT 2018


rnk created this revision.
rnk added reviewers: majnemer, mstorsjo.
Herald added a subscriber: hiraditya.

MSVC and LLD sort sections ASCII-betically, so we need to use section
names that sort between .CRT$XCA (the start) and .CRT$XCU (the default
priority).

In the general case, use .CRT$XCT12345 as the section name, and let the
linker sort the zero-padded digits.

Users with low priorities typically want to initialize as early as
possible, so use .CRT$XCA00199 for prioties less than 200. This number
is arbitrary.


https://reviews.llvm.org/D51820

Files:
  llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
  llvm/test/CodeGen/X86/ctor-priority-coff.ll


Index: llvm/test/CodeGen/X86/ctor-priority-coff.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/ctor-priority-coff.ll
@@ -0,0 +1,46 @@
+; RUN: llc < %s | FileCheck %s
+
+; Check that we come up with appropriate section names that link.exe sorts
+; well.
+
+; CHECK: .section        .CRT$XCA00042,"dr"
+; CHECK: .p2align        3
+; CHECK: .quad   f
+; CHECK: .section        .CRT$XCT12345,"dr"
+; CHECK: .p2align        3
+; CHECK: .quad   g
+
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc19.14.26433"
+
+ at str0 = private dso_local unnamed_addr constant [6 x i8] c"later\00", align 1
+ at str1 = private dso_local unnamed_addr constant [6 x i8] c"first\00", align 1
+ at str2 = private dso_local unnamed_addr constant [5 x i8] c"main\00", align 1
+ at llvm.global_ctors = appending global [2 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 12345, void ()* @g, i8* null }, { i32, void ()*, i8* } { i32 42, void ()* @f, i8* null }]
+
+; Function Attrs: nounwind uwtable
+define dso_local void @g() #0 {
+entry:
+  %call = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @str0, i64 0, i64 0))
+  ret void
+}
+
+; Function Attrs: nounwind
+declare dso_local i32 @puts(i8* nocapture readonly) local_unnamed_addr #1
+
+; Function Attrs: nounwind uwtable
+define dso_local void @f() #0 {
+entry:
+  %call = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @str1, i64 0, i64 0))
+  ret void
+}
+
+; Function Attrs: nounwind uwtable
+define dso_local i32 @main() local_unnamed_addr #0 {
+entry:
+  %call = tail call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @str2, i64 0, i64 0))
+  ret i32 0
+}
+
+attributes #0 = { nounwind uwtable }
+attributes #1 = { nounwind }
Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
===================================================================
--- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1456,8 +1456,25 @@
                                                    unsigned Priority,
                                                    const MCSymbol *KeySym,
                                                    MCSectionCOFF *Default) {
-  if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment())
-    return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
+  if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
+    // If the priority is the default, use .CRT$XCU, possibly associative.
+    if (Priority == 65535)
+      return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
+
+    // Otherwise, we need to compute a new section name. Low priorities should
+    // run earlier. The linker will sort sections ASCII-betically, and we need a
+    // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
+    // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
+    // low priorities need to sort before 'L', since the CRT uses that
+    // internally, so we use ".CRT$XCA00001" for them.
+    SmallString<24> Name;
+    raw_svector_ostream OS(Name);
+    OS << ".CRT$XC" << (Priority < 200 ? 'A' : 'T') << format("%05u", Priority);
+    MCSectionCOFF *Sec = Ctx.getCOFFSection(
+        Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
+        SectionKind::getReadOnly());
+    return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0);
+  }
 
   std::string Name = IsCtor ? ".ctors" : ".dtors";
   if (Priority != 65535)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51820.164530.patch
Type: text/x-patch
Size: 3605 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180907/578d63aa/attachment.bin>


More information about the llvm-commits mailing list