[llvm] r319150 - [COFF] Implement constructor priorities

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 28 00:07:18 PST 2017


Author: mstorsjo
Date: Tue Nov 28 00:07:18 2017
New Revision: 319150

URL: http://llvm.org/viewvc/llvm-project?rev=319150&view=rev
Log:
[COFF] Implement constructor priorities

The priorities in the section name suffixes are zero padded,
allowing the linker to just do a lexical sort.

Add zero padding for .ctors sections in ELF as well.

Differential Revision: https://reviews.llvm.org/D40407

Modified:
    llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    llvm/trunk/test/CodeGen/X86/constructor.ll

Modified: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=319150&r1=319149&r2=319150&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (original)
+++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Tue Nov 28 00:07:18 2017
@@ -52,6 +52,7 @@
 #include "llvm/ProfileData/InstrProf.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CodeGen.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
@@ -530,10 +531,8 @@ static MCSectionELF *getStaticStructorSe
       Name = ".ctors";
     else
       Name = ".dtors";
-    if (Priority != 65535) {
-      Name += '.';
-      Name += utostr(65535 - Priority);
-    }
+    if (Priority != 65535)
+      raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
     Type = ELF::SHT_PROGBITS;
   }
 
@@ -1212,16 +1211,38 @@ void TargetLoweringObjectFileCOFF::Initi
   }
 }
 
+static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
+                                                   const Triple &T, bool IsCtor,
+                                                   unsigned Priority,
+                                                   const MCSymbol *KeySym,
+                                                   MCSectionCOFF *Default) {
+  if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment())
+    return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
+
+  std::string Name = IsCtor ? ".ctors" : ".dtors";
+  if (Priority != 65535)
+    raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
+
+  return Ctx.getAssociativeCOFFSection(
+      Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
+                                   COFF::IMAGE_SCN_MEM_READ |
+                                   COFF::IMAGE_SCN_MEM_WRITE,
+                         SectionKind::getData()),
+      KeySym, 0);
+}
+
 MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
     unsigned Priority, const MCSymbol *KeySym) const {
-  return getContext().getAssociativeCOFFSection(
-      cast<MCSectionCOFF>(StaticCtorSection), KeySym, 0);
+  return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
+                                      Priority, KeySym,
+                                      cast<MCSectionCOFF>(StaticCtorSection));
 }
 
 MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
     unsigned Priority, const MCSymbol *KeySym) const {
-  return getContext().getAssociativeCOFFSection(
-      cast<MCSectionCOFF>(StaticDtorSection), KeySym, 0);
+  return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
+                                      Priority, KeySym,
+                                      cast<MCSectionCOFF>(StaticDtorSection));
 }
 
 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(

Modified: llvm/trunk/test/CodeGen/X86/constructor.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/constructor.ll?rev=319150&r1=319149&r2=319150&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/constructor.ll (original)
+++ llvm/trunk/test/CodeGen/X86/constructor.ll Tue Nov 28 00:07:18 2017
@@ -7,7 +7,8 @@
 ; RUN: llc -mtriple x86_64-unknown-nacl < %s | FileCheck --check-prefix=NACL %s
 ; RUN: llc -mtriple i586-intel-elfiamcu -use-ctors < %s | FileCheck %s --check-prefix=MCU-CTORS
 ; RUN: llc -mtriple i586-intel-elfiamcu < %s | FileCheck %s --check-prefix=MCU-INIT-ARRAY
- at llvm.global_ctors = appending global [2 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @f, i8* null}, { i32, void ()*, i8* } { i32 15, void ()* @g, i8* @v }]
+; RUN: llc -mtriple x86_64-win32-gnu < %s | FileCheck --check-prefix=COFF-CTOR %s
+ at llvm.global_ctors = appending global [3 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @f, i8* null}, { i32, void ()*, i8* } { i32 15, void ()* @g, i8* @v }, { i32, void ()*, i8* } { i32 55555, void ()* @h, i8* @v }]
 
 @v = weak_odr global i8 0
 
@@ -21,9 +22,17 @@ entry:
   ret void
 }
 
+define void @h() {
+entry:
+  ret void
+}
+
 ; CTOR:		.section	.ctors.65520,"aGw", at progbits,v,comdat
 ; CTOR-NEXT:	.p2align	3
 ; CTOR-NEXT:	.quad	g
+; CTOR-NEXT:	.section	.ctors.09980,"aGw", at progbits,v,comdat
+; CTOR-NEXT:	.p2align	3
+; CTOR-NEXT:	.quad	h
 ; CTOR-NEXT:	.section	.ctors,"aw", at progbits
 ; CTOR-NEXT:	.p2align	3
 ; CTOR-NEXT:	.quad	f
@@ -31,6 +40,9 @@ entry:
 ; INIT-ARRAY:		.section	.init_array.15,"aGw", at init_array,v,comdat
 ; INIT-ARRAY-NEXT:	.p2align	3
 ; INIT-ARRAY-NEXT:	.quad	g
+; INIT-ARRAY-NEXT:	.section	.init_array.55555,"aGw", at init_array,v,comdat
+; INIT-ARRAY-NEXT:	.p2align	3
+; INIT-ARRAY-NEXT:	.quad	h
 ; INIT-ARRAY-NEXT:	.section	.init_array,"aw", at init_array
 ; INIT-ARRAY-NEXT:	.p2align	3
 ; INIT-ARRAY-NEXT:	.quad	f
@@ -38,9 +50,22 @@ entry:
 ; NACL:		.section	.init_array.15,"aGw", at init_array,v,comdat
 ; NACL-NEXT:	.p2align	2
 ; NACL-NEXT:	.long	g
+; NACL-NEXT:	.section	.init_array.55555,"aGw", at init_array,v,comdat
+; NACL-NEXT:	.p2align	2
+; NACL-NEXT:	.long	h
 ; NACL-NEXT:	.section	.init_array,"aw", at init_array
 ; NACL-NEXT:	.p2align	2
 ; NACL-NEXT:	.long	f
 
 ; MCU-CTORS:         .section        .ctors,"aw", at progbits
 ; MCU-INIT-ARRAY:    .section        .init_array,"aw", at init_array
+
+; COFF-CTOR:		.section	.ctors.65520,"dw",associative,v
+; COFF-CTOR-NEXT:	.p2align	3
+; COFF-CTOR-NEXT:	.quad	g
+; COFF-CTOR-NEXT:	.section	.ctors.09980,"dw",associative,v
+; COFF-CTOR-NEXT:	.p2align	3
+; COFF-CTOR-NEXT:	.quad	h
+; COFF-CTOR-NEXT:	.section	.ctors,"dw"
+; COFF-CTOR-NEXT:	.p2align	3
+; COFF-CTOR-NEXT:	.quad	f




More information about the llvm-commits mailing list