From da5cc8466186b15f31db3c7c98c4f0eee3ad7ced Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 16 Feb 2017 04:12:19 +0000 Subject: [PATCH] Add CopyRelSection instances to BSS in the regular way. Previously, space in a BSS section for copy relocations are reserved in a special way. We directly manipulated size of the BSS section. r294577 changed the way of doing it. Now, we create an instance of CopyRelSection (which is a synthetic input section) for each copy relocation. This patch removes the remains of the old way and add CopyRelSections to BSS sections using `addSections` function, which is the usual way to add an input section to an output section. llvm-svn: 295278 --- lld/ELF/Relocations.cpp | 25 ++++++++----------------- lld/ELF/SyntheticSections.h | 3 ++- lld/ELF/Writer.cpp | 14 +++++++++----- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 57ff259141c3..9e0f46291857 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -426,37 +426,28 @@ 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); - OutputSection *CopySec = - IsReadOnly ? Out::BssRelRo : Out::Bss; + OutputSection *OSec = IsReadOnly ? Out::BssRelRo : Out::Bss; - uintX_t Alignment = getAlignment(SS); - uintX_t Off = alignTo(CopySec->Size, Alignment); - CopySec->Size = Off + SymSize; - 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); + // Create a SyntheticSection in Out to hold the .bss and the Copy Reloc. + auto *ISec = + make>(IsReadOnly, getAlignment(SS), 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 (const Elf_Sym &S : SS->file()->getGlobalSymbols()) { - if (S.st_shndx != Shndx || S.st_value != Value) + if (S.st_shndx != SS->Sym.st_shndx || S.st_value != SS->Sym.st_value) continue; auto *Alias = dyn_cast_or_null>( Symtab::X->find(check(S.getName(SS->file()->getStringTable())))); if (!Alias) continue; - Alias->CopySection = CopyISec; + Alias->CopySection = ISec; Alias->NeedsCopyOrPltAddr = true; Alias->symbol()->IsUsedInRegularObj = true; } - In::RelaDyn->addReloc({Target->CopyRel, CopyISec, 0, false, SS, 0}); + In::RelaDyn->addReloc({Target->CopyRel, ISec, 0, false, SS, 0}); } template diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h index 3af25a57353e..a1190d2803f4 100644 --- a/lld/ELF/SyntheticSections.h +++ b/lld/ELF/SyntheticSections.h @@ -107,7 +107,8 @@ private: uint8_t *HashBuf; }; -// SHT_NOBITS section created for a copyReloc +// 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 { typedef typename ELFT::uint uintX_t; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 5ec87dc425cf..9a1741f3a043 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1184,12 +1184,16 @@ template void Writer::finalizeSections() { } template void Writer::addPredefinedSections() { - if (Out::Bss->Size > 0) - OutputSections.push_back(Out::Bss); - if (Out::BssRelRo->Size > 0) - OutputSections.push_back(Out::BssRelRo); + auto Add = [=](OutputSection *Sec) { + if (!Sec->Sections.empty()) { + Sec->assignOffsets(); + OutputSections.push_back(Sec); + } + }; + Add(Out::Bss); + Add(Out::BssRelRo); - auto OS = dyn_cast_or_null>(findSection(".ARM.exidx")); + auto *OS = dyn_cast_or_null>(findSection(".ARM.exidx")); if (OS && !OS->Sections.empty() && !Config->Relocatable) OS->addSection(make>()); }