From 9181ce623fd8189252659da7c48de1982597b79c Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Mon, 15 Aug 2022 11:04:50 -0700 Subject: [Windows] Put init_seg(compiler/lib) in llvm.global_ctors Currently we treat initializers with init_seg(compiler/lib) as similar to any other init_seg, they simply have a global variable in the proper section (".CRT$XCC" for compiler/".CRT$XCL" for lib) and are added to llvm.used. However, this doesn't match with how LLVM sees normal (or init_seg(user)) initializers via llvm.global_ctors. This causes issues like incorrect init_seg(compiler) vs init_seg(user) ordering due to GlobalOpt evaluating constructors, and the ability to remove init_seg(compiler/lib) initializers at all. Currently we use 'A' for priorities less than 200. Use 200 for init_seg(compiler) (".CRT$XCC") and 400 for init_seg(lib) (".CRT$XCL"), which do not append the priority to the section name. Priorities between 200 and 400 use ".CRT$XCC${Priority}". This allows for some wiggle room for people/future extensions that want to add initializers between compiler and lib. Fixes #56922 Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D131910 --- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp') diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 5859937..941e6a2 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1889,11 +1889,24 @@ static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, // 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. + // internally, so we use ".CRT$XCA00001" for them. We have a contract with + // the frontend that "init_seg(compiler)" corresponds to priority 200 and + // "init_seg(lib)" corresponds to priority 400, and those respectively use + // 'C' and 'L' without the priority suffix. Priorities between 200 and 400 + // use 'C' with the priority as a suffix. SmallString<24> Name; + char LastLetter = 'T'; + bool AddPrioritySuffix = Priority != 200 && Priority != 400; + if (Priority < 200) + LastLetter = 'A'; + else if (Priority < 400) + LastLetter = 'C'; + else if (Priority == 400) + LastLetter = 'L'; raw_svector_ostream OS(Name); - OS << ".CRT$X" << (IsCtor ? "C" : "T") << - (Priority < 200 ? 'A' : 'T') << format("%05u", Priority); + OS << ".CRT$X" << (IsCtor ? "C" : "T") << LastLetter; + if (AddPrioritySuffix) + OS << format("%05u", Priority); MCSectionCOFF *Sec = Ctx.getCOFFSection( Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getReadOnly()); -- cgit v1.1