Index: lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1212,16 +1212,44 @@ } } +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; + if (IsCtor) + Name = ".ctors"; + else + Name = ".dtors"; + if (Priority != 65535) { + Name += '.'; + Name += utostr(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(StaticCtorSection), KeySym, 0); + return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true, + Priority, KeySym, + cast(StaticCtorSection)); } MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( unsigned Priority, const MCSymbol *KeySym) const { - return getContext().getAssociativeCOFFSection( - cast(StaticDtorSection), KeySym, 0); + return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false, + Priority, KeySym, + cast(StaticDtorSection)); } void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( Index: test/CodeGen/X86/constructor.ll =================================================================== --- test/CodeGen/X86/constructor.ll +++ test/CodeGen/X86/constructor.ll @@ -7,6 +7,7 @@ ; 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 +; RUN: llc -mtriple x86_64-win32-gnu < %s | FileCheck --check-prefix=COFF-CTOR %s @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 }] @v = weak_odr global i8 0 @@ -44,3 +45,10 @@ ; MCU-CTORS: .section .ctors,"aw",@progbits ; MCU-INIT-ARRAY: .section .init_array,"aw",@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,"dw" +; COFF-CTOR-NEXT: .p2align 3 +; COFF-CTOR-NEXT: .quad f