diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2024-07-27 23:34:35 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2024-12-01 12:32:12 +0000 |
commit | fbc266465ed3585efdbd8e9ebf71e97ce7e8b464 (patch) | |
tree | 7560c2cbec09e542e5f2e2100ffc16ca742b1075 /contrib/llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | |
parent | ac8517f04c0fe31968ed43e36608ad02d72d3597 (diff) |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp')
-rw-r--r-- | contrib/llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 178 |
1 files changed, 105 insertions, 73 deletions
diff --git a/contrib/llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/contrib/llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index a69b71451736..0d3e4ba5662e 100644 --- a/contrib/llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/contrib/llvm-project/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -43,6 +43,7 @@ #include "llvm/IR/PseudoProbe.h" #include "llvm/IR/Type.h" #include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCAsmInfoDarwin.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCSectionCOFF.h" @@ -212,13 +213,11 @@ void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, // identify N64 from just a triple. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; - // We don't support PC-relative LSDA references in GAS so we use the default - // DW_EH_PE_absptr for those. // FreeBSD must be explicit about the data size and using pcrel since it's // assembler/linker won't do the automatic conversion that the Linux tools // do. - if (TgtM.getTargetTriple().isOSFreeBSD()) { + if (isPositionIndependent() || TgtM.getTargetTriple().isOSFreeBSD()) { PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; } @@ -479,7 +478,7 @@ static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { Name == ".llvmbc" || Name == ".llvmcmd") return SectionKind::getMetadata(); - if (Name.empty() || Name[0] != '.') return K; + if (!Name.starts_with(".")) return K; // Default implementation based on some magic section names. if (Name == ".bss" || Name.starts_with(".bss.") || @@ -525,6 +524,8 @@ static unsigned getELFSectionType(StringRef Name, SectionKind K) { if (hasPrefix(Name, ".llvm.offloading")) return ELF::SHT_LLVM_OFFLOADING; + if (Name == ".llvm.lto") + return ELF::SHT_LLVM_LTO; if (K.isBSS() || K.isThreadBSS()) return ELF::SHT_NOBITS; @@ -635,21 +636,22 @@ static SmallString<128> getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind, Mangler &Mang, const TargetMachine &TM, unsigned EntrySize, bool UniqueSectionName) { - SmallString<128> Name; + SmallString<128> Name = + getSectionPrefixForGlobal(Kind, TM.isLargeGlobalValue(GO)); if (Kind.isMergeableCString()) { // We also need alignment here. // FIXME: this is getting the alignment of the character, not the // alignment of the global! - Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( + Align Alignment = GO->getDataLayout().getPreferredAlign( cast<GlobalVariable>(GO)); - std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; - Name = SizeSpec + utostr(Alignment.value()); + Name += ".str"; + Name += utostr(EntrySize); + Name += "."; + Name += utostr(Alignment.value()); } else if (Kind.isMergeableConst()) { - Name = ".rodata.cst"; + Name += ".cst"; Name += utostr(EntrySize); - } else { - Name = getSectionPrefixForGlobal(Kind, TM.isLargeGlobalValue(GO)); } bool HasPrefix = false; @@ -732,15 +734,20 @@ calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName, Ctx.isELFGenericMergeableSection(SectionName); // If this is the first ocurrence of this section name, treat it as the // generic section - if (!SymbolMergeable && !SeenSectionNameBefore) - return MCContext::GenericSectionID; + if (!SymbolMergeable && !SeenSectionNameBefore) { + if (TM.getSeparateNamedSections()) + return NextUniqueID++; + else + return MCContext::GenericSectionID; + } // Symbols must be placed into sections with compatible entry sizes. Generate // unique sections for symbols that have not been assigned to compatible // sections. const auto PreviousID = Ctx.getELFUniqueIDForEntsize(SectionName, Flags, EntrySize); - if (PreviousID) + if (PreviousID && (!TM.getSeparateNamedSections() || + *PreviousID == MCContext::GenericSectionID)) return *PreviousID; // If the user has specified the same section name as would be created @@ -796,10 +803,6 @@ static MCSection *selectExplicitSectionGlobal( SectionName = Attrs.getAttribute("data-section").getValueAsString(); } } - const Function *F = dyn_cast<Function>(GO); - if (F && F->hasFnAttribute("implicit-section-name")) { - SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); - } // Infer section flags from the section name if we can. Kind = getELFKindForNamedSection(SectionName, Kind); @@ -933,7 +936,7 @@ MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction( unsigned Flags = getELFSectionFlags(Kind); // If the function's section names is pre-determined via pragma or a // section attribute, call selectExplicitSectionGlobal. - if (F.hasSection() || F.hasFnAttribute("implicit-section-name")) + if (F.hasSection()) return selectExplicitSectionGlobal( &F, Kind, TM, getContext(), getMangler(), NextUniqueID, Used.count(&F), /* ForceUnique = */true); @@ -1034,7 +1037,7 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock( // name, or a unique ID for the section. SmallString<128> Name; StringRef FunctionSectionName = MBB.getParent()->getSection()->getName(); - if (FunctionSectionName.equals(".text") || + if (FunctionSectionName == ".text" || FunctionSectionName.starts_with(".text.")) { // Function is in a regular .text section. StringRef FunctionName = MBB.getParent()->getName(); @@ -1297,11 +1300,6 @@ MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( } } - const Function *F = dyn_cast<Function>(GO); - if (F && F->hasFnAttribute("implicit-section-name")) { - SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); - } - // Parse the section specifier and create it if valid. StringRef Segment, Section; unsigned TAA = 0, StubSize = 0; @@ -1362,7 +1360,7 @@ MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( // FIXME: Alignment check should be handled by section classifier. if (Kind.isMergeable1ByteCString() && - GO->getParent()->getDataLayout().getPreferredAlign( + GO->getDataLayout().getPreferredAlign( cast<GlobalVariable>(GO)) < Align(32)) return CStringSection; @@ -1370,7 +1368,7 @@ MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( // externally visible label, this runs into issues with certain linker // versions. if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && - GO->getParent()->getDataLayout().getPreferredAlign( + GO->getDataLayout().getPreferredAlign( cast<GlobalVariable>(GO)) < Align(32)) return UStringSection; @@ -1558,7 +1556,7 @@ const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, const MCSection &Section) { - if (!AsmInfo.isSectionAtomizableBySymbols(Section)) + if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(Section)) return true; // FIXME: we should be able to use private labels for sections that can't be @@ -1699,7 +1697,7 @@ MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( } } - return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, + return getContext().getCOFFSection(Name, Characteristics, COMDATSymName, Selection); } @@ -1758,12 +1756,12 @@ MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( if (getContext().getTargetTriple().isWindowsGNUEnvironment()) raw_svector_ostream(Name) << '$' << ComdatGV->getName(); - return getContext().getCOFFSection(Name, Characteristics, Kind, - COMDATSymName, Selection, UniqueID); + return getContext().getCOFFSection(Name, Characteristics, COMDATSymName, + Selection, UniqueID); } else { SmallString<256> TmpData; getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); - return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, + return getContext().getCOFFSection(Name, Characteristics, TmpData, Selection, UniqueID); } } @@ -1820,9 +1818,9 @@ MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; unsigned UniqueID = NextUniqueID++; - return getContext().getCOFFSection( - SecName, Characteristics, Kind, COMDATSymName, - COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); + return getContext().getCOFFSection(SecName, Characteristics, COMDATSymName, + COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, + UniqueID); } bool TargetLoweringObjectFileCOFF::shouldPutJumpTableInFunctionSection( @@ -1849,10 +1847,8 @@ void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, GetObjCImageInfo(M, Version, Flags, Section); if (!Section.empty()) { auto &C = getContext(); - auto *S = C.getCOFFSection(Section, - COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getReadOnly()); + auto *S = C.getCOFFSection(Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ); Streamer.switchSection(S); Streamer.emitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); Streamer.emitInt32(Version); @@ -1932,21 +1928,17 @@ void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { StaticCtorSection = Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getReadOnly()); + COFF::IMAGE_SCN_MEM_READ); StaticDtorSection = Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | - COFF::IMAGE_SCN_MEM_READ, - SectionKind::getReadOnly()); + COFF::IMAGE_SCN_MEM_READ); } else { StaticCtorSection = Ctx.getCOFFSection( ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | - COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getData()); + COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE); StaticDtorSection = Ctx.getCOFFSection( ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | - COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getData()); + COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE); } } @@ -1984,8 +1976,7 @@ static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, if (AddPrioritySuffix) OS << format("%05u", Priority); MCSectionCOFF *Sec = Ctx.getCOFFSection( - Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, - SectionKind::getReadOnly()); + Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ); return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); } @@ -1996,8 +1987,7 @@ static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, return Ctx.getAssociativeCOFFSection( Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | - COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getData()), + COFF::IMAGE_SCN_MEM_WRITE), KeySym, 0); } @@ -2115,7 +2105,7 @@ MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( } if (!COMDATSymName.empty()) - return getContext().getCOFFSection(".rdata", Characteristics, Kind, + return getContext().getCOFFSection(".rdata", Characteristics, COMDATSymName, COFF::IMAGE_COMDAT_SELECT_ANY); } @@ -2141,7 +2131,7 @@ static const Comdat *getWasmComdat(const GlobalValue *GV) { return C; } -static unsigned getWasmSectionFlags(SectionKind K) { +static unsigned getWasmSectionFlags(SectionKind K, bool Retain) { unsigned Flags = 0; if (K.isThreadLocal()) @@ -2150,11 +2140,22 @@ static unsigned getWasmSectionFlags(SectionKind K) { if (K.isMergeableCString()) Flags |= wasm::WASM_SEG_FLAG_STRINGS; + if (Retain) + Flags |= wasm::WASM_SEG_FLAG_RETAIN; + // TODO(sbc): Add suport for K.isMergeableConst() return Flags; } +void TargetLoweringObjectFileWasm::getModuleMetadata(Module &M) { + SmallVector<GlobalValue *, 4> Vec; + collectUsedGlobalVariables(M, Vec, false); + for (GlobalValue *GV : Vec) + if (auto *GO = dyn_cast<GlobalObject>(GV)) + Used.insert(GO); +} + MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { // We don't support explict section names for functions in the wasm object @@ -2178,16 +2179,18 @@ MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( Group = C->getName(); } - unsigned Flags = getWasmSectionFlags(Kind); + unsigned Flags = getWasmSectionFlags(Kind, Used.count(GO)); MCSectionWasm *Section = getContext().getWasmSection( Name, Kind, Flags, Group, MCContext::GenericSectionID); return Section; } -static MCSectionWasm *selectWasmSectionForGlobal( - MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, - const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { +static MCSectionWasm * +selectWasmSectionForGlobal(MCContext &Ctx, const GlobalObject *GO, + SectionKind Kind, Mangler &Mang, + const TargetMachine &TM, bool EmitUniqueSection, + unsigned *NextUniqueID, bool Retain) { StringRef Group = ""; if (const Comdat *C = getWasmComdat(GO)) { Group = C->getName(); @@ -2212,7 +2215,7 @@ static MCSectionWasm *selectWasmSectionForGlobal( (*NextUniqueID)++; } - unsigned Flags = getWasmSectionFlags(Kind); + unsigned Flags = getWasmSectionFlags(Kind, Retain); return Ctx.getWasmSection(Name, Kind, Flags, Group, UniqueID); } @@ -2230,9 +2233,11 @@ MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( else EmitUniqueSection = TM.getDataSections(); EmitUniqueSection |= GO->hasComdat(); + bool Retain = Used.count(GO); + EmitUniqueSection |= Retain; return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, - EmitUniqueSection, &NextUniqueID); + EmitUniqueSection, &NextUniqueID, Retain); } bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( @@ -2318,7 +2323,7 @@ bool TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB( MCSymbol * TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(const MachineFunction *MF) { - MCSymbol *EHInfoSym = MF->getMMI().getContext().getOrCreateSymbol( + MCSymbol *EHInfoSym = MF->getContext().getOrCreateSymbol( "__ehinfo." + Twine(MF->getFunctionNumber())); cast<MCSymbolXCOFF>(EHInfoSym)->setEHInfo(); return EHInfoSym; @@ -2402,6 +2407,15 @@ MCSection *TargetLoweringObjectFileXCOFF::getSectionForExternalReference( SmallString<128> Name; getNameWithPrefix(Name, GO, TM); + // AIX TLS local-dynamic does not need the external reference for the + // "_$TLSML" symbol. + if (GO->getThreadLocalMode() == GlobalVariable::LocalDynamicTLSModel && + GO->hasName() && GO->getName() == "_$TLSML") { + return getContext().getXCOFFSection( + Name, SectionKind::getData(), + XCOFF::CsectProperties(XCOFF::XMC_TC, XCOFF::XTY_SD)); + } + XCOFF::StorageMappingClass SMC = isa<Function>(GO) ? XCOFF::XMC_DS : XCOFF::XMC_UA; if (GO->isThreadLocal()) @@ -2424,8 +2438,10 @@ MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal( if (GVar->hasAttribute("toc-data")) { SmallString<128> Name; getNameWithPrefix(Name, GO, TM); + XCOFF::SymbolType symType = + GO->hasCommonLinkage() ? XCOFF::XTY_CM : XCOFF::XTY_SD; return getContext().getXCOFFSection( - Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TD, XCOFF::XTY_SD), + Name, Kind, XCOFF::CsectProperties(XCOFF::XMC_TD, symType), /* MultiSymbolsAllowed*/ true); } @@ -2653,17 +2669,34 @@ MCSection *TargetLoweringObjectFileXCOFF::getSectionForFunctionDescriptor( MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry( const MCSymbol *Sym, const TargetMachine &TM) const { - // Use TE storage-mapping class when large code model is enabled so that - // the chance of needing -bbigtoc is decreased. Also, the toc-entry for - // EH info is never referenced directly using instructions so it can be - // allocated with TE storage-mapping class. + const XCOFF::StorageMappingClass SMC = [](const MCSymbol *Sym, + const TargetMachine &TM) { + const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(Sym); + + // The "_$TLSML" symbol for TLS local-dynamic mode requires XMC_TC, + // otherwise the AIX assembler will complain. + if (XSym->getSymbolTableName() == "_$TLSML") + return XCOFF::XMC_TC; + + // Use large code model toc entries for ehinfo symbols as they are + // never referenced directly. The runtime loads their TOC entry + // addresses from the trace-back table. + if (XSym->isEHInfo()) + return XCOFF::XMC_TE; + + // If the symbol does not have a code model specified use the module value. + if (!XSym->hasPerSymbolCodeModel()) + return TM.getCodeModel() == CodeModel::Large ? XCOFF::XMC_TE + : XCOFF::XMC_TC; + + return XSym->getPerSymbolCodeModel() == MCSymbolXCOFF::CM_Large + ? XCOFF::XMC_TE + : XCOFF::XMC_TC; + }(Sym, TM); + return getContext().getXCOFFSection( cast<MCSymbolXCOFF>(Sym)->getSymbolTableName(), SectionKind::getData(), - XCOFF::CsectProperties((TM.getCodeModel() == CodeModel::Large || - cast<MCSymbolXCOFF>(Sym)->isEHInfo()) - ? XCOFF::XMC_TE - : XCOFF::XMC_TC, - XCOFF::XTY_SD)); + XCOFF::CsectProperties(SMC, XCOFF::XTY_SD)); } MCSection *TargetLoweringObjectFileXCOFF::getSectionForLSDA( @@ -2693,8 +2726,7 @@ MCSection *TargetLoweringObjectFileGOFF::getExplicitSectionGlobal( MCSection *TargetLoweringObjectFileGOFF::getSectionForLSDA( const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const { std::string Name = ".gcc_exception_table." + F.getName().str(); - return getContext().getGOFFSection(Name, SectionKind::getData(), nullptr, - nullptr); + return getContext().getGOFFSection(Name, SectionKind::getData(), nullptr, 0); } MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal( @@ -2702,7 +2734,7 @@ MCSection *TargetLoweringObjectFileGOFF::SelectSectionForGlobal( auto *Symbol = TM.getSymbol(GO); if (Kind.isBSS()) return getContext().getGOFFSection(Symbol->getName(), SectionKind::getBSS(), - nullptr, nullptr); + nullptr, 0); return getContext().getObjectFileInfo()->getTextSection(); } |