From 30634d9b4f9a296a11da7c706a5608a9e80ceb02 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 9 Aug 2016 01:35:38 +0000 Subject: [PATCH] Reduce dependency to OutputSectionFactory. The Factory class is too object-oriented-ish and easy to be abused. This patch reduces dependency to that class. Eventually we want to remove the dependency to that class from LinkerScript. llvm-svn: 278084 --- lld/ELF/Writer.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 9f0304a..8f316f7 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -77,6 +77,7 @@ private: void addRelIpltSymbols(); void addStartEndSymbols(); void addStartStopSymbols(OutputSectionBase *Sec); + OutputSectionBase *findSection(StringRef Name); SymbolTable &Symtab; std::vector Phdrs; @@ -664,18 +665,15 @@ template void Writer::finalizeSections() { // Create output sections for input object file sections. std::vector *> RegularSections = OutputSections; - Out::Dynamic->PreInitArraySec = Factory.lookup( - ".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC); - Out::Dynamic->InitArraySec = - Factory.lookup(".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC); - Out::Dynamic->FiniArraySec = - Factory.lookup(".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC); + Out::Dynamic->PreInitArraySec = findSection(".preinit_array"); + Out::Dynamic->InitArraySec = findSection(".init_array"); + Out::Dynamic->FiniArraySec = findSection(".fini_array"); // Sort section contents for __attribute__((init_priority(N)). sortInitFini(Out::Dynamic->InitArraySec); sortInitFini(Out::Dynamic->FiniArraySec); - sortCtorsDtors(Factory.lookup(".ctors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC)); - sortCtorsDtors(Factory.lookup(".dtors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC)); + sortCtorsDtors(findSection(".ctors")); + sortCtorsDtors(findSection(".dtors")); // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop // symbols for sections, so that the runtime can get the start and end @@ -905,6 +903,14 @@ void Writer::addStartStopSymbols(OutputSectionBase *Sec) { Symtab.addSynthetic(Stop, Sec, DefinedSynthetic::SectionEnd); } +template +OutputSectionBase *Writer::findSection(StringRef Name) { + for (OutputSectionBase *Sec : OutputSections) + if (Sec->getName() == Name) + return Sec; + return nullptr; +} + template static bool needsPtLoad(OutputSectionBase *Sec) { if (!(Sec->getFlags() & SHF_ALLOC)) return false; @@ -1245,7 +1251,7 @@ template void Writer::writeSections() { // PPC64 needs to process relocations in the .opd section // before processing relocations in code-containing sections. - Out::Opd = Factory.lookup(".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC); + Out::Opd = findSection(".opd"); if (Out::Opd) { Out::OpdBuf = Buf + Out::Opd->getFileOff(); Out::Opd->writeTo(Buf + Out::Opd->getFileOff()); -- 2.7.4