[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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341727 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Kleckner
2018-09-07 23:07:55 +00:00
parent 72cb007a52
commit 443a8ce3c6
2 changed files with 78 additions and 2 deletions
+19 -2
View File
@@ -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)