From 2812aa82d0cd9d626e4da81b5cf90dd3068aa3cc Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Wed, 23 Sep 2015 16:57:31 +0000 Subject: [PATCH] [elf2] Pass BSSSec to the relocation handling code differently. Don't store it in the symbol. llvm-svn: 248393 --- lld/ELF/InputSection.cpp | 15 ++++++++++----- lld/ELF/InputSection.h | 5 +++-- lld/ELF/OutputSections.cpp | 2 +- lld/ELF/OutputSections.h | 2 ++ lld/ELF/Symbols.h | 2 -- lld/ELF/Writer.cpp | 6 +++++- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index dbdaa1d425ae..e8fa4a68356d 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -29,7 +29,8 @@ template void InputSection::relocate( uint8_t *Buf, iterator_range *> Rels, const ObjectFile &File, uintX_t BaseAddr, - const PltSection &PltSec, const GotSection &GotSec) { + const OutputSection &BssSec, const PltSection &PltSec, + const GotSection &GotSec) { typedef Elf_Rel_Impl RelType; bool IsMips64EL = File.getObj()->isMips64EL(); for (const RelType &RI : Rels) { @@ -58,7 +59,7 @@ void InputSection::relocate( break; case SymbolBody::DefinedCommonKind: { auto *DC = cast>(Body); - SymVA = DC->OutputSec->getVA() + DC->OffsetInBSS; + SymVA = BssSec.getVA() + DC->OffsetInBSS; break; } case SymbolBody::SharedKind: @@ -87,7 +88,9 @@ void InputSection::relocate( } template -void InputSection::writeTo(uint8_t *Buf, const PltSection &PltSec, +void InputSection::writeTo(uint8_t *Buf, + const OutputSection &BssSec, + const PltSection &PltSec, const GotSection &GotSec) { if (Header->sh_type == SHT_NOBITS) return; @@ -102,9 +105,11 @@ void InputSection::writeTo(uint8_t *Buf, const PltSection &PltSec, // Iterate over all relocation sections that apply to this section. for (const Elf_Shdr *RelSec : RelocSections) { if (RelSec->sh_type == SHT_RELA) - relocate(Base, EObj->relas(RelSec), *File, BaseAddr, PltSec, GotSec); + relocate(Base, EObj->relas(RelSec), *File, BaseAddr, BssSec, PltSec, + GotSec); else - relocate(Base, EObj->rels(RelSec), *File, BaseAddr, PltSec, GotSec); + relocate(Base, EObj->rels(RelSec), *File, BaseAddr, BssSec, PltSec, + GotSec); } } diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index b8a7b7531a7f..f0de99ec0848 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -37,8 +37,8 @@ public: // Write this section to a mmap'ed file, assuming Buf is pointing to // beginning of the output section. - void writeTo(uint8_t *Buf, const PltSection &PltSec, - const GotSection &GotSec); + void writeTo(uint8_t *Buf, const OutputSection &BssSec, + const PltSection &PltSec, const GotSection &GotSec); StringRef getSectionName() const; const Elf_Shdr *getSectionHdr() const { return Header; } @@ -65,6 +65,7 @@ private: llvm::iterator_range< const llvm::object::Elf_Rel_Impl *> Rels, const ObjectFile &File, uintX_t BaseAddr, + const OutputSection &BssSec, const PltSection &PltSec, const GotSection &GotSec); // The offset from beginning of the output sections this section was assigned diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 7b2533846e2e..c152fe3d9858 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -252,7 +252,7 @@ lld::elf2::getLocalSymVA(const typename ELFFile::Elf_Sym *Sym, template void OutputSection::writeTo(uint8_t *Buf) { for (InputSection *C : Sections) - C->writeTo(Buf, PltSec, GotSec); + C->writeTo(Buf, *BssSec, PltSec, GotSec); } template diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index 38706b9361f5..6ece9adf4d2e 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -242,11 +242,13 @@ public: void addSection(InputSection *C); void writeTo(uint8_t *Buf) override; + void setBssSec(const OutputSection *BS) { BssSec = BS; } private: std::vector *> Sections; const PltSection &PltSec; const GotSection &GotSec; + const OutputSection *BssSec = nullptr; }; template diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h index d9f89255d37a..f6cd077751c1 100644 --- a/lld/ELF/Symbols.h +++ b/lld/ELF/Symbols.h @@ -189,8 +189,6 @@ public: // The maximum alignment we have seen for this symbol. uintX_t MaxAlignment; - - OutputSection *OutputSec = nullptr; }; // Regular defined symbols read from object file symbol tables. diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 57180df89933..58ad5ac81ee7 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -365,6 +365,11 @@ template void Writer::createSections() { } BSSSec = getSection(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); + + // The only type in OutputSections is currently OutputSection. + for (OutputSectionBase *OSB : OutputSections) + static_cast *>(OSB)->setBssSec(BSSSec); + SymTabSec.setBssSec(BSSSec); DynSymSec.setBssSec(BSSSec); @@ -376,7 +381,6 @@ template void Writer::createSections() { uintX_t Align = C->MaxAlignment; Off = RoundUpToAlignment(Off, Align); C->OffsetInBSS = Off; - C->OutputSec = BSSSec; Off += Sym.st_size; }