diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2017-02-02 21:26:06 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2017-02-02 21:26:06 +0000 |
commit | 13a79bbfe583e1d8cc85d241b580907260065eb8 (patch) | |
tree | 538d692317f9990cae5ae6bb23bc23fd3c5803ef /llvm/lib/MC/MCContext.cpp | |
parent | bb8dcc6aec4071c114ed4205f01c9163dbd1bf00 (diff) | |
download | bcm5719-llvm-13a79bbfe583e1d8cc85d241b580907260065eb8.tar.gz bcm5719-llvm-13a79bbfe583e1d8cc85d241b580907260065eb8.zip |
Change how we handle section symbols on ELF.
On ELF every section can have a corresponding section symbol. When in
an assembly file we have
.quad .text
the '.text' refers to that symbol.
The way we used to handle them is to leave .text an undefined symbol
until the very end when the object writer would map them to the
actual section symbol.
The problem with that is that anything before the end would see an
undefined symbol. This could result in bad diagnostics
(test/MC/AArch64/label-arithmetic-diags-elf.s), or incorrect results
when using the asm streamer (est/MC/Mips/expansion-jal-sym-pic.s).
Fixing this will also allow using the section symbol earlier for
setting sh_link of SHF_METADATA sections.
This patch includes a few hacks to avoid changing our behaviour when
handling conflicts between section symbols and other symbols. I
reported pr31850 to track that.
llvm-svn: 293936
Diffstat (limited to 'llvm/lib/MC/MCContext.cpp')
-rw-r--r-- | llvm/lib/MC/MCContext.cpp | 73 |
1 files changed, 42 insertions, 31 deletions
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp index 4798991ceed..16c2c99be67 100644 --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -80,7 +80,6 @@ void MCContext::reset() { MCSubtargetAllocator.DestroyAll(); UsedNames.clear(); Symbols.clear(); - SectionSymbols.clear(); Allocator.Reset(); Instances.clear(); CompilationDir.clear(); @@ -124,18 +123,6 @@ MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) { return Sym; } -MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) { - MCSymbol *&Sym = SectionSymbols[&Section]; - if (Sym) - return cast<MCSymbolELF>(Sym); - - StringRef Name = Section.getSectionName(); - auto NameIter = UsedNames.insert(std::make_pair(Name, false)).first; - Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false); - - return cast<MCSymbolELF>(Sym); -} - MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName, unsigned Idx) { return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName + @@ -316,6 +303,38 @@ void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) { const_cast<MCSectionELF *>(Section)->setSectionName(CachedName); } +MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type, + unsigned Flags, SectionKind K, + unsigned EntrySize, + const MCSymbolELF *Group, + unsigned UniqueID, + const MCSectionELF *Associated) { + + MCSymbolELF *R; + MCSymbol *&Sym = Symbols[Section]; + if (Sym && Sym->isUndefined()) { + R = cast<MCSymbolELF>(Sym); + } else { + auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first; + R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false); + if (!Sym) + Sym = R; + } + R->setBinding(ELF::STB_LOCAL); + R->setType(ELF::STT_SECTION); + R->setRedefinable(true); + + auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF( + Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated); + + auto *F = new MCDataFragment(); + Ret->getFragmentList().insert(Ret->begin(), F); + F->setParent(Ret); + R->setFragment(F); + + return Ret; +} + MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type, unsigned Flags, unsigned EntrySize, const MCSymbolELF *Group, @@ -325,9 +344,9 @@ MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type, std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name.str(), true)); - return new (ELFAllocator.Allocate()) - MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(), - EntrySize, Group, true, nullptr, Associated); + return createELFSectionImpl(I->getKey(), Type, Flags, + SectionKind::getReadOnly(), EntrySize, Group, + true, Associated); } MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix, @@ -339,21 +358,19 @@ MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix, MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, unsigned Flags, unsigned EntrySize, - const Twine &Group, unsigned UniqueID, - const char *BeginSymName) { + const Twine &Group, unsigned UniqueID) { MCSymbolELF *GroupSym = nullptr; if (!Group.isTriviallyEmpty() && !Group.str().empty()) GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group)); return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID, - BeginSymName, nullptr); + nullptr); } MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, unsigned Flags, unsigned EntrySize, const MCSymbolELF *GroupSym, unsigned UniqueID, - const char *BeginSymName, const MCSectionELF *Associated) { StringRef Group = ""; if (GroupSym) @@ -375,22 +392,16 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, else Kind = SectionKind::getReadOnly(); - MCSymbol *Begin = nullptr; - if (BeginSymName) - Begin = createTempSymbol(BeginSymName, false); - - MCSectionELF *Result = new (ELFAllocator.Allocate()) - MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, - Begin, Associated); + MCSectionELF *Result = createELFSectionImpl( + CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated); Entry.second = Result; return Result; } MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) { - MCSectionELF *Result = new (ELFAllocator.Allocate()) - MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4, - Group, ~0, nullptr, nullptr); - return Result; + return createELFSectionImpl(".group", ELF::SHT_GROUP, 0, + SectionKind::getReadOnly(), 4, Group, ~0, + nullptr); } MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, |