diff options
author | Reid Kleckner <rnk@google.com> | 2018-09-07 23:07:55 +0000 |
---|---|---|
committer | Reid Kleckner <rnk@google.com> | 2018-09-07 23:07:55 +0000 |
commit | f803b23879d9e1d9415ec1875713534dcc203df5 (patch) | |
tree | 7694ed09cc37eb69d3dc59206ef755fe3580633d /llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | |
parent | c30dfb2dfc3dbe707a86c8aaf3b1cae79f3eb838 (diff) | |
download | bcm5719-llvm-f803b23879d9e1d9415ec1875713534dcc203df5.tar.gz bcm5719-llvm-f803b23879d9e1d9415ec1875713534dcc203df5.zip |
[COFF] Implement llvm.global_ctors priorities for MSVC COFF targets
Summary:
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.
Implements PR38552.
Reviewers: majnemer, mstorsjo
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D51820
llvm-svn: 341727
Diffstat (limited to 'llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp')
-rw-r--r-- | llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 7a5fd2985c7..f6882c40531 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1456,8 +1456,25 @@ static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, 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) |