From 47bed8c9f7d4dfcbe2c47d3bfb8e3c7d7d8f85a9 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Thu, 16 Mar 2017 08:44:53 +0000 Subject: [PATCH] Revert r297813 "[ELF] - Make Bss and BssRelRo sections to be synthetic (#3)." I suppose it is the reason of BB fail: http://lab.llvm.org:8011/builders/clang-cmake-aarch64-lld/builds/921 https://bugs.llvm.org/show_bug.cgi?id=32167 llvm-svn: 297933 --- lld/ELF/OutputSections.cpp | 2 ++ lld/ELF/Relocations.cpp | 13 +++++++---- lld/ELF/Symbols.cpp | 5 ++-- lld/ELF/Symbols.h | 5 ++-- lld/ELF/SyntheticSections.cpp | 21 +++++++++-------- lld/ELF/SyntheticSections.h | 18 +++++---------- lld/ELF/Writer.cpp | 43 +++++++++++++++++++++-------------- 7 files changed, 57 insertions(+), 50 deletions(-) diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index b1d6e4e27f65..9b7eda1a0600 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -31,6 +31,8 @@ using namespace lld; using namespace lld::elf; uint8_t Out::First; +OutputSection *Out::Bss; +OutputSection *Out::BssRelRo; OutputSection *Out::Opd; uint8_t *Out::OpdBuf; PhdrEntry *Out::TlsPhdr; diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 200c30aee3a4..7fbcea1a26a2 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -479,20 +479,23 @@ template static void addCopyRelSymbol(SharedSymbol *SS) { // See if this symbol is in a read-only segment. If so, preserve the symbol's // memory protection by reserving space in the .bss.rel.ro section. bool IsReadOnly = isReadOnly(SS); - BssSection *Sec = IsReadOnly ? In::BssRelRo : In::Bss; - uintX_t Off = Sec->reserveSpace(SS->getAlignment(), SymSize); + OutputSection *OSec = IsReadOnly ? Out::BssRelRo : Out::Bss; + + // Create a SyntheticSection in Out to hold the .bss and the Copy Reloc. + auto *ISec = + make>(IsReadOnly, SS->getAlignment(), SymSize); + OSec->addSection(ISec); // Look through the DSO's dynamic symbol table for aliases and create a // dynamic symbol for each one. This causes the copy relocation to correctly // interpose any aliases. for (SharedSymbol *Sym : getSymbolsAt(SS)) { Sym->NeedsCopy = true; - Sym->CopyRelSec = Sec; - Sym->CopyRelSecOff = Off; + Sym->Section = ISec; Sym->symbol()->IsUsedInRegularObj = true; } - In::RelaDyn->addReloc({Target->CopyRel, Sec, Off, false, SS, 0}); + In::RelaDyn->addReloc({Target->CopyRel, ISec, 0, false, SS, 0}); } template diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp index b22270705ee1..56607e9b6c61 100644 --- a/lld/ELF/Symbols.cpp +++ b/lld/ELF/Symbols.cpp @@ -106,8 +106,7 @@ static typename ELFT::uint getSymVA(const SymbolBody &Body, int64_t &Addend) { case SymbolBody::SharedKind: { auto &SS = cast(Body); if (SS.NeedsCopy) - return SS.CopyRelSec->OutSec->Addr + SS.CopyRelSec->OutSecOff + - SS.CopyRelSecOff; + return SS.Section->OutSec->Addr + SS.Section->OutSecOff; if (SS.NeedsPltAddr) return Body.getPltVA(); return 0; @@ -208,7 +207,7 @@ template OutputSection *SymbolBody::getOutputSection() const { if (auto *S = dyn_cast(this)) { if (S->NeedsCopy) - return S->CopyRelSec->OutSec; + return S->Section->OutSec; return nullptr; } diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h index b66b21678edf..b1159963392f 100644 --- a/lld/ELF/Symbols.h +++ b/lld/ELF/Symbols.h @@ -238,9 +238,8 @@ public: // This field is a pointer to the symbol's version definition. const void *Verdef; - // CopyRelSec and CopyRelSecOff are significant only when NeedsCopy is true. - InputSection *CopyRelSec; - size_t CopyRelSecOff; + // Section is significant only when NeedsCopy is true. + InputSection *Section = nullptr; private: template const typename ELFT::Sym &getSym() const { diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index a9e3551214e2..b2221206bde7 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -376,14 +376,12 @@ void BuildIdSection::computeHash( HashFn(HashBuf, Hashes); } -BssSection::BssSection(StringRef Name) - : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 0, Name) {} - -size_t BssSection::reserveSpace(uint32_t Alignment, size_t Size) { - OutSec->updateAlignment(Alignment); - this->Size = alignTo(this->Size, Alignment) + Size; - return this->Size - Size; -} +template +CopyRelSection::CopyRelSection(bool ReadOnly, uint32_t Alignment, + size_t S) + : SyntheticSection(SHF_ALLOC, SHT_NOBITS, Alignment, + ReadOnly ? ".bss.rel.ro" : ".bss"), + Size(S) {} template void BuildIdSection::writeBuildId(ArrayRef Buf) { @@ -2264,8 +2262,6 @@ InputSection *ThunkSection::getTargetInputSection() const { } InputSection *InX::ARMAttributes; -BssSection *InX::Bss; -BssSection *InX::BssRelRo; InputSection *InX::Common; StringTableSection *InX::DynStrTab; InputSection *InX::Interp; @@ -2318,6 +2314,11 @@ template class elf::BuildIdSection; template class elf::BuildIdSection; template class elf::BuildIdSection; +template class elf::CopyRelSection; +template class elf::CopyRelSection; +template class elf::CopyRelSection; +template class elf::CopyRelSection; + template class elf::GotSection; template class elf::GotSection; template class elf::GotSection; diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 8e9cdb6cb942..aabd9c73b7d6 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -153,19 +153,15 @@ private: uint8_t *HashBuf; }; -// BssSection is used to reserve space for copy relocations. We create two -// instances of this class for .bss and .bss.rel.ro. .bss is used for writable -// symbols, and .bss.rel.ro is used for read-only symbols. -class BssSection final : public SyntheticSection { +// For each copy relocation, we create an instance of this class to +// reserve space in .bss or .bss.rel.ro. +template class CopyRelSection final : public SyntheticSection { public: - BssSection(StringRef Name); + CopyRelSection(bool ReadOnly, uint32_t Alignment, size_t Size); void writeTo(uint8_t *) override {} - bool empty() const override { return getSize() == 0; } - size_t reserveSpace(uint32_t Alignment, size_t Size); - size_t getSize() const override { return Size; } -private: - size_t Size = 0; + size_t getSize() const override { return Size; } + size_t Size; }; template class MipsGotSection final : public SyntheticSection { @@ -760,8 +756,6 @@ SymbolBody *addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value, // Linker generated sections which can be used as inputs. struct InX { static InputSection *ARMAttributes; - static BssSection *Bss; - static BssSection *BssRelRo; static InputSection *Common; static StringTableSection *DynStrTab; static InputSection *Interp; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index ccf267fb2e90..aeea54ef6d0c 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -111,8 +111,8 @@ StringRef elf::getOutputSectionName(StringRef Name) { } for (StringRef V : - {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.", - ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", + {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.", + ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", ".gcc_except_table.", ".tdata.", ".ARM.exidx."}) { StringRef Prefix = V.drop_back(); if (Name.startswith(V) || Name == Prefix) @@ -327,6 +327,11 @@ template void Writer::createSyntheticSections() { auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); }; + // Create singleton output sections. + Out::Bss = make(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); + Out::BssRelRo = + make(".bss.rel.ro", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); + In::DynStrTab = make(".dynstr", true); In::Dynamic = make>(); In::RelaDyn = make>( @@ -364,11 +369,6 @@ template void Writer::createSyntheticSections() { Add(Common); } - In::Bss = make(".bss"); - Add(In::Bss); - In::BssRelRo = make(".bss.rel.ro"); - Add(In::BssRelRo); - // Add MIPS-specific sections. bool HasDynSymTab = !Symtab::X->getSharedFiles().empty() || Config->pic() || @@ -618,7 +618,7 @@ template bool elf::isRelroSection(const OutputSection *Sec) { return true; if (In::Got && Sec == In::Got->OutSec) return true; - if (Sec == In::BssRelRo->OutSec) + if (Sec == Out::BssRelRo) return true; StringRef S = Sec->Name; @@ -1168,15 +1168,14 @@ template void Writer::finalizeSections() { // Dynamic section must be the last one in this list and dynamic // symbol table section (DynSymTab) must be the first one. applySynthetic( - {In::DynSymTab, In::Bss, In::BssRelRo, - In::GnuHashTab, In::HashTab, In::SymTab, - In::ShStrTab, In::StrTab, In::VerDef, - In::DynStrTab, In::GdbIndex, In::Got, - In::MipsGot, In::IgotPlt, In::GotPlt, - In::RelaDyn, In::RelaIplt, In::RelaPlt, - In::Plt, In::Iplt, In::Plt, - In::EhFrameHdr, In::VerSym, In::VerNeed, - In::Dynamic}, + {In::DynSymTab, In::GnuHashTab, In::HashTab, + In::SymTab, In::ShStrTab, In::StrTab, + In::VerDef, In::DynStrTab, In::GdbIndex, + In::Got, In::MipsGot, In::IgotPlt, + In::GotPlt, In::RelaDyn, In::RelaIplt, + In::RelaPlt, In::Plt, In::Iplt, + In::Plt, In::EhFrameHdr, In::VerSym, + In::VerNeed, In::Dynamic}, [](SyntheticSection *SS) { SS->finalizeContents(); }); // Some architectures use small displacements for jump instructions. @@ -1205,6 +1204,16 @@ template void Writer::finalizeSections() { } template void Writer::addPredefinedSections() { + // Add BSS sections. + auto Add = [=](OutputSection *Sec) { + if (!Sec->Sections.empty()) { + Sec->assignOffsets(); + OutputSections.push_back(Sec); + } + }; + Add(Out::Bss); + Add(Out::BssRelRo); + // ARM ABI requires .ARM.exidx to be terminated by some piece of data. // We have the terminater synthetic section class. Add that at the end. auto *OS = dyn_cast_or_null(findSection(".ARM.exidx"));