Index: lld/trunk/ELF/Relocations.cpp =================================================================== --- lld/trunk/ELF/Relocations.cpp +++ lld/trunk/ELF/Relocations.cpp @@ -434,6 +434,13 @@ CopySec->updateAlignment(Alignment); uintX_t Shndx = SS->Sym.st_shndx; uintX_t Value = SS->Sym.st_value; + + // Create a SyntheticSection in CopySec to hold the .bss and the Copy Reloc + auto *CopyISec = make>(IsReadOnly, Alignment, SymSize); + CopyISec->OutSecOff = Off; + CopyISec->OutSec = CopySec; + CopySec->Sections.push_back(CopyISec); + // 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. @@ -444,12 +451,11 @@ Symtab::X->find(check(S.getName(SS->file()->getStringTable())))); if (!Alias) continue; - Alias->CopyIsInBssRelRo = IsReadOnly; - Alias->CopyOffset = Off; + Alias->CopySection = CopyISec; Alias->NeedsCopyOrPltAddr = true; Alias->symbol()->IsUsedInRegularObj = true; } - In::RelaDyn->addReloc({Target->CopyRel, CopySec, Off, false, SS, 0}); + In::RelaDyn->addReloc({Target->CopyRel, CopyISec, 0, false, SS, 0}); } template Index: lld/trunk/ELF/Symbols.h =================================================================== --- lld/trunk/ELF/Symbols.h +++ lld/trunk/ELF/Symbols.h @@ -122,11 +122,6 @@ // True if this symbol is in the Igot sub-section of the .got.plt or .got. unsigned IsInIgot : 1; - // True if this is a shared symbol in a read-only segment which requires a - // copy relocation. This causes space for the symbol to be allocated in the - // .bss.rel.ro section. - unsigned CopyIsInBssRelRo : 1; - // The following fields have the same meaning as the ELF symbol attributes. uint8_t Type; // symbol type uint8_t StOther; // st_other field value @@ -276,12 +271,11 @@ // This field is a pointer to the symbol's version definition. const Elf_Verdef *Verdef; - // CopyOffset is significant only when needsCopy() is true. - uintX_t CopyOffset = 0; - + // CopySection is significant only when needsCopy() is true. + InputSection *CopySection = nullptr; bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); } - OutputSection *getBssSectionForCopy() const; + InputSection *getBssSectionForCopy() const; }; // This class represents a symbol defined in an archive file. It is Index: lld/trunk/ELF/Symbols.cpp =================================================================== --- lld/trunk/ELF/Symbols.cpp +++ lld/trunk/ELF/Symbols.cpp @@ -84,7 +84,8 @@ return 0; if (SS.isFunc()) return Body.getPltVA(); - return SS.getBssSectionForCopy()->Addr + SS.CopyOffset; + InputSection *CopyISec = SS.getBssSectionForCopy(); + return CopyISec->OutSec->Addr + CopyISec->OutSecOff; } case SymbolBody::UndefinedKind: return 0; @@ -100,7 +101,7 @@ uint8_t Type) : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(IsLocal), IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false), - IsInIgot(false), CopyIsInBssRelRo(false), Type(Type), StOther(StOther), + IsInIgot(false), Type(Type), StOther(StOther), Name(Name) {} // Returns true if a symbol can be replaced at load-time by a symbol @@ -231,9 +232,10 @@ } template -OutputSection *SharedSymbol::getBssSectionForCopy() const { +InputSection *SharedSymbol::getBssSectionForCopy() const { assert(needsCopy()); - return CopyIsInBssRelRo ? Out::BssRelRo : Out::Bss; + assert(CopySection); + return CopySection; } DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint64_t Alignment, Index: lld/trunk/ELF/SyntheticSections.h =================================================================== --- lld/trunk/ELF/SyntheticSections.h +++ lld/trunk/ELF/SyntheticSections.h @@ -107,6 +107,18 @@ uint8_t *HashBuf; }; +// SHT_NOBITS section created for a copyReloc +template +class CopyRelSection final : public SyntheticSection { + typedef typename ELFT::uint uintX_t; + +public: + CopyRelSection(bool ReadOnly, uintX_t AddrAlign, size_t Size); + void writeTo(uint8_t *) override {} + size_t getSize() const override { return Size; } + size_t Size; +}; + template class MipsGotSection final : public SyntheticSection { typedef typename ELFT::uint uintX_t; @@ -271,16 +283,9 @@ : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {} - DynamicReloc(uint32_t Type, const OutputSectionBase *OutputSec, - uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym, - uintX_t Addend) - : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec), - UseSymVA(UseSymVA), Addend(Addend) {} - uintX_t getOffset() const; uintX_t getAddend() const; uint32_t getSymIndex() const; - const OutputSectionBase *getOutputSec() const { return OutputSec; } const InputSectionBase *getInputSec() const { return InputSec; } uint32_t Type; @@ -288,7 +293,6 @@ private: SymbolBody *Sym; const InputSectionBase *InputSec = nullptr; - const OutputSectionBase *OutputSec = nullptr; uintX_t OffsetInSec; bool UseSymVA; uintX_t Addend; Index: lld/trunk/ELF/SyntheticSections.cpp =================================================================== --- lld/trunk/ELF/SyntheticSections.cpp +++ lld/trunk/ELF/SyntheticSections.cpp @@ -362,6 +362,15 @@ } template +CopyRelSection::CopyRelSection(bool ReadOnly, uintX_t AddrAlign, size_t S) + : SyntheticSection(SHF_ALLOC, SHT_NOBITS, AddrAlign, + ReadOnly ? ".bss.rel.ro" : ".bss"), + Size(S) { + if (!ReadOnly) + this->Flags |= SHF_WRITE; +} + +template void BuildIdSection::writeBuildId(ArrayRef Buf) { switch (Config->BuildId) { case BuildIdKind::Fast: @@ -965,8 +974,6 @@ template typename ELFT::uint DynamicReloc::getOffset() const { - if (OutputSec) - return OutputSec->Addr + OffsetInSec; return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec); } @@ -1228,7 +1235,7 @@ case SymbolBody::SharedKind: { auto &SS = cast>(*Sym); if (SS.needsCopy()) - return SS.getBssSectionForCopy(); + return SS.getBssSectionForCopy()->OutSec; break; } case SymbolBody::UndefinedKind: @@ -2051,6 +2058,11 @@ 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;