diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h --- a/llvm/include/llvm/MC/MCContext.h +++ b/llvm/include/llvm/MC/MCContext.h @@ -296,7 +296,7 @@ unsigned EntrySize, const MCSymbolELF *Group, unsigned UniqueID, - const MCSymbolELF *Associated); + const MCSymbolELF *LinkedToSym); /// Map of currently defined macros. StringMap MacroMap; @@ -442,12 +442,12 @@ MCSectionELF *getELFSection(const Twine &Section, unsigned Type, unsigned Flags, unsigned EntrySize, const Twine &Group, unsigned UniqueID, - const MCSymbolELF *Associated); + const MCSymbolELF *LinkedToSym); MCSectionELF *getELFSection(const Twine &Section, unsigned Type, unsigned Flags, unsigned EntrySize, const MCSymbolELF *Group, unsigned UniqueID, - const MCSymbolELF *Associated); + const MCSymbolELF *LinkedToSym); /// Get a section with the provided group identifier. This section is /// named by concatenating \p Prefix with '.' then \p Suffix. The \p Type diff --git a/llvm/include/llvm/MC/MCSectionELF.h b/llvm/include/llvm/MC/MCSectionELF.h --- a/llvm/include/llvm/MC/MCSectionELF.h +++ b/llvm/include/llvm/MC/MCSectionELF.h @@ -44,18 +44,19 @@ const MCSymbolELF *Group; - /// sh_info for SHF_LINK_ORDER (can be null). - const MCSymbol *AssociatedSymbol; + /// Used by SHF_LINK_ORDER. If non-null, the sh_link field will be set to the + /// section header index of the section where LinkedToSym is defined. + const MCSymbol *LinkedToSym; private: friend class MCContext; MCSectionELF(StringRef Section, unsigned type, unsigned flags, SectionKind K, unsigned entrySize, const MCSymbolELF *group, unsigned UniqueID, - MCSymbol *Begin, const MCSymbolELF *AssociatedSymbol) + MCSymbol *Begin, const MCSymbolELF *LinkedToSym) : MCSection(SV_ELF, K, Begin), SectionName(Section), Type(type), Flags(flags), UniqueID(UniqueID), EntrySize(entrySize), Group(group), - AssociatedSymbol(AssociatedSymbol) { + LinkedToSym(LinkedToSym) { if (Group) Group->setIsSignature(); } @@ -83,8 +84,10 @@ bool isUnique() const { return UniqueID != ~0U; } unsigned getUniqueID() const { return UniqueID; } - const MCSection *getAssociatedSection() const { return &AssociatedSymbol->getSection(); } - const MCSymbol *getAssociatedSymbol() const { return AssociatedSymbol; } + const MCSection *getLinkedToSection() const { + return &LinkedToSym->getSection(); + } + const MCSymbol *getLinkedToSymbol() const { return LinkedToSym; } static bool classof(const MCSection *S) { return S->getVariant() == SV_ELF; diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -3180,8 +3180,8 @@ MCSection *InstMap = nullptr; MCSection *FnSledIndex = nullptr; if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) { - auto Associated = dyn_cast(CurrentFnSym); - assert(Associated != nullptr); + auto LinkedToSym = dyn_cast(CurrentFnSym); + assert(LinkedToSym != nullptr); auto Flags = ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER; std::string GroupName; if (F.hasComdat()) { @@ -3192,10 +3192,10 @@ auto UniqueID = ++XRayFnUniqueID; InstMap = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS, Flags, 0, - GroupName, UniqueID, Associated); + GroupName, UniqueID, LinkedToSym); FnSledIndex = OutContext.getELFSection("xray_fn_idx", ELF::SHT_PROGBITS, Flags, 0, - GroupName, UniqueID, Associated); + GroupName, UniqueID, LinkedToSym); } else if (MF->getSubtarget().getTargetTriple().isOSBinFormatMachO()) { InstMap = OutContext.getMachOSection("__DATA", "xray_instr_map", 0, SectionKind::getReadOnlyWithRel()); diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -512,8 +512,8 @@ return C; } -static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO, - const TargetMachine &TM) { +static const MCSymbolELF *getLinkedToSymbol(const GlobalObject *GO, + const TargetMachine &TM) { MDNode *MD = GO->getMetadata(LLVMContext::MD_associated); if (!MD) return nullptr; @@ -592,18 +592,18 @@ // A section can have at most one associated section. Put each global with // MD_associated in a unique section. unsigned UniqueID = MCContext::GenericSectionID; - const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); - if (AssociatedSymbol) { + const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); + if (LinkedToSym) { UniqueID = NextUniqueID++; Flags |= ELF::SHF_LINK_ORDER; } MCSectionELF *Section = getContext().getELFSection( SectionName, getELFSectionType(SectionName, Kind), Flags, - getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol); + getEntrySizeForKind(Kind), Group, UniqueID, LinkedToSym); // Make sure that we did not get some other section with incompatible sh_link. // This should not be possible due to UniqueID code above. - assert(Section->getAssociatedSymbol() == AssociatedSymbol && + assert(Section->getLinkedToSymbol() == LinkedToSym && "Associated symbol mismatch between sections"); return Section; } @@ -696,16 +696,16 @@ } EmitUniqueSection |= GO->hasComdat(); - const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); - if (AssociatedSymbol) { + const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM); + if (LinkedToSym) { EmitUniqueSection = true; Flags |= ELF::SHF_LINK_ORDER; } MCSectionELF *Section = selectELFSectionForGlobal( getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags, - &NextUniqueID, AssociatedSymbol); - assert(Section->getAssociatedSymbol() == AssociatedSymbol); + &NextUniqueID, LinkedToSym); + assert(Section->getLinkedToSymbol() == LinkedToSym); return Section; } diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -1001,7 +1001,7 @@ case ELF::SHT_RELA: { sh_link = SymbolTableIndex; assert(sh_link && ".symtab not found"); - const MCSection *InfoSection = Section.getAssociatedSection(); + const MCSection *InfoSection = Section.getLinkedToSection(); sh_info = SectionIndexMap.lookup(cast(InfoSection)); break; } @@ -1024,7 +1024,7 @@ } if (Section.getFlags() & ELF::SHF_LINK_ORDER) { - const MCSymbol *Sym = Section.getAssociatedSymbol(); + const MCSymbol *Sym = Section.getLinkedToSymbol(); const MCSectionELF *Sec = cast(&Sym->getSection()); sh_link = SectionIndexMap.lookup(Sec); } @@ -1180,7 +1180,7 @@ uint64_t SecStart = W.OS.tell(); writeRelocations(Asm, - cast(*RelSection->getAssociatedSection())); + cast(*RelSection->getLinkedToSection())); uint64_t SecEnd = W.OS.tell(); SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd); diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -332,7 +332,7 @@ unsigned EntrySize, const MCSymbolELF *Group, unsigned UniqueID, - const MCSymbolELF *Associated) { + const MCSymbolELF *LinkedToSym) { MCSymbolELF *R; MCSymbol *&Sym = Symbols[Section]; // A section symbol can not redefine regular symbols. There may be multiple @@ -352,7 +352,7 @@ R->setType(ELF::STT_SECTION); auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF( - Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated); + Section, Type, Flags, K, EntrySize, Group, UniqueID, R, LinkedToSym); auto *F = new MCDataFragment(); Ret->getFragmentList().insert(Ret->begin(), F); @@ -386,20 +386,20 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, unsigned Flags, unsigned EntrySize, const Twine &Group, unsigned UniqueID, - const MCSymbolELF *Associated) { + const MCSymbolELF *LinkedToSym) { MCSymbolELF *GroupSym = nullptr; if (!Group.isTriviallyEmpty() && !Group.str().empty()) GroupSym = cast(getOrCreateSymbol(Group)); return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID, - Associated); + LinkedToSym); } MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, unsigned Flags, unsigned EntrySize, const MCSymbolELF *GroupSym, unsigned UniqueID, - const MCSymbolELF *Associated) { + const MCSymbolELF *LinkedToSym) { StringRef Group = ""; if (GroupSym) Group = GroupSym->getName(); @@ -420,8 +420,9 @@ else Kind = SectionKind::getReadOnly(); - MCSectionELF *Result = createELFSectionImpl( - CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated); + MCSectionELF *Result = + createELFSectionImpl(CachedName, Type, Flags, Kind, EntrySize, GroupSym, + UniqueID, LinkedToSym); Entry.second = Result; return Result; } diff --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp --- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp +++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp @@ -158,7 +158,7 @@ bool maybeParseSectionType(StringRef &TypeName); bool parseMergeSize(int64_t &Size); bool parseGroup(StringRef &GroupName); - bool parseMetadataSym(MCSymbolELF *&Associated); + bool parseLinkedToSym(MCSymbolELF *&LinkedToSym); bool maybeParseUniqueID(int64_t &UniqueID); }; @@ -443,17 +443,18 @@ return false; } -bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) { +bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) { MCAsmLexer &L = getLexer(); if (L.isNot(AsmToken::Comma)) - return TokError("expected metadata symbol"); + return TokError("expected linked-to symbol"); Lex(); StringRef Name; + SMLoc StartLoc = L.getLoc(); if (getParser().parseIdentifier(Name)) - return TokError("invalid metadata symbol"); - Associated = dyn_cast_or_null(getContext().lookupSymbol(Name)); - if (!Associated || !Associated->isInSection()) - return TokError("symbol is not in a section: " + Name); + return TokError("invalid linked-to symbol"); + LinkedToSym = dyn_cast_or_null(getContext().lookupSymbol(Name)); + if (!LinkedToSym || !LinkedToSym->isInSection()) + return Error(StartLoc, "linked-to symbol is not in a section: " + Name); return false; } @@ -495,7 +496,7 @@ unsigned Flags = 0; const MCExpr *Subsection = nullptr; bool UseLastGroup = false; - MCSymbolELF *Associated = nullptr; + MCSymbolELF *LinkedToSym = nullptr; int64_t UniqueID = ~0; // Set the defaults first. @@ -568,7 +569,7 @@ if (parseGroup(GroupName)) return true; if (Flags & ELF::SHF_LINK_ORDER) - if (parseMetadataSym(Associated)) + if (parseLinkedToSym(LinkedToSym)) return true; if (maybeParseUniqueID(UniqueID)) return true; @@ -633,9 +634,8 @@ } } - MCSection *ELFSection = - getContext().getELFSection(SectionName, Type, Flags, Size, GroupName, - UniqueID, Associated); + MCSection *ELFSection = getContext().getELFSection( + SectionName, Type, Flags, Size, GroupName, UniqueID, LinkedToSym); getStreamer().SwitchSection(ELFSection, Subsection); if (getContext().getGenDwarfForAssembly()) { diff --git a/llvm/lib/MC/MCSectionELF.cpp b/llvm/lib/MC/MCSectionELF.cpp --- a/llvm/lib/MC/MCSectionELF.cpp +++ b/llvm/lib/MC/MCSectionELF.cpp @@ -172,9 +172,9 @@ } if (Flags & ELF::SHF_LINK_ORDER) { - assert(AssociatedSymbol); + assert(LinkedToSym); OS << ","; - printName(OS, AssociatedSymbol->getName()); + printName(OS, LinkedToSym->getName()); } if (isUnique()) diff --git a/llvm/test/MC/ELF/metadata-declaration-errors.s b/llvm/test/MC/ELF/metadata-declaration-errors.s deleted file mode 100644 --- a/llvm/test/MC/ELF/metadata-declaration-errors.s +++ /dev/null @@ -1,10 +0,0 @@ -// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s \ -// RUN: -filetype=obj -o %t.o 2>&1 | FileCheck %s - -// Check we do not silently ignore invalid metadata symbol (123). -// CHECK: error: invalid metadata symbol - -.section .foo,"a" -.quad 0 - -.section bar,"ao",@progbits,123 diff --git a/llvm/test/MC/ELF/section-linkorder-error.s b/llvm/test/MC/ELF/section-linkorder-error.s new file mode 100644 --- /dev/null +++ b/llvm/test/MC/ELF/section-linkorder-error.s @@ -0,0 +1,18 @@ +# RUN: not llvm-mc -triple x86_64 %s -o /dev/null 2>&1 | FileCheck %s + +# CHECK: {{.*}}.s:[[# @LINE+1]]:30: error: expected linked-to symbol +.section .link,"ao",@progbits + +# CHECK: {{.*}}.s:[[# @LINE+1]]:31: error: invalid linked-to symbol +.section .link,"ao",@progbits,123 + +# CHECK: {{.*}}.s:[[# @LINE+1]]:31: error: linked-to symbol is not in a section: foo +.section .link,"ao",@progbits,foo + +# CHECK: {{.*}}.s:[[# @LINE+2]]:31: error: linked-to symbol is not in a section: bar +bar = 42 +.section .link,"ao",@progbits,bar + +# CHECK: {{.*}}.s:[[# @LINE+2]]:31: error: linked-to symbol is not in a section: baz +.quad baz +.section .link,"ao",@progbits,baz diff --git a/llvm/test/MC/ELF/section-metadata-err1.s b/llvm/test/MC/ELF/section-metadata-err1.s deleted file mode 100644 --- a/llvm/test/MC/ELF/section-metadata-err1.s +++ /dev/null @@ -1,5 +0,0 @@ -// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s - -// CHECK: error: symbol is not in a section: foo - - .section .shf_metadata,"ao",@progbits,foo diff --git a/llvm/test/MC/ELF/section-metadata-err2.s b/llvm/test/MC/ELF/section-metadata-err2.s deleted file mode 100644 --- a/llvm/test/MC/ELF/section-metadata-err2.s +++ /dev/null @@ -1,6 +0,0 @@ -// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s - -// CHECK: error: symbol is not in a section: foo - - .quad foo - .section .shf_metadata,"ao",@progbits,foo diff --git a/llvm/test/MC/ELF/section-metadata-err3.s b/llvm/test/MC/ELF/section-metadata-err3.s deleted file mode 100644 --- a/llvm/test/MC/ELF/section-metadata-err3.s +++ /dev/null @@ -1,6 +0,0 @@ -// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s - -// CHECK: error: symbol is not in a section: foo - - foo = 42 - .section .shf_metadata,"ao",@progbits,foo diff --git a/llvm/test/MC/ELF/section-metadata-err4.s b/llvm/test/MC/ELF/section-metadata-err4.s deleted file mode 100644 --- a/llvm/test/MC/ELF/section-metadata-err4.s +++ /dev/null @@ -1,5 +0,0 @@ -// RUN: not llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1 | FileCheck %s - -// CHECK: error: expected metadata symbol - - .section .shf_metadata,"ao",@progbits