Index: lld/trunk/ELF/InputSection.h =================================================================== --- lld/trunk/ELF/InputSection.h +++ lld/trunk/ELF/InputSection.h @@ -21,6 +21,8 @@ namespace lld { namespace elf { +template bool isDiscarded(InputSectionBase *S); + class SymbolBody; template class ICF; Index: lld/trunk/ELF/InputSection.cpp =================================================================== --- lld/trunk/ELF/InputSection.cpp +++ lld/trunk/ELF/InputSection.cpp @@ -12,6 +12,7 @@ #include "EhFrame.h" #include "Error.h" #include "InputFiles.h" +#include "LinkerScript.h" #include "OutputSections.h" #include "Target.h" #include "Thunks.h" @@ -27,6 +28,11 @@ using namespace lld; using namespace lld::elf; +template bool elf::isDiscarded(InputSectionBase *S) { + return !S || S == &InputSection::Discarded || !S->Live || + Script::X->isDiscarded(S); +} + template InputSectionBase::InputSectionBase(elf::ObjectFile *File, const Elf_Shdr *Header, @@ -644,6 +650,11 @@ return S->SectionKind == InputSectionBase::MipsOptions; } +template bool elf::isDiscarded(InputSectionBase *); +template bool elf::isDiscarded(InputSectionBase *); +template bool elf::isDiscarded(InputSectionBase *); +template bool elf::isDiscarded(InputSectionBase *); + template class elf::InputSectionBase; template class elf::InputSectionBase; template class elf::InputSectionBase; Index: lld/trunk/ELF/Writer.h =================================================================== --- lld/trunk/ELF/Writer.h +++ lld/trunk/ELF/Writer.h @@ -10,14 +10,28 @@ #ifndef LLD_ELF_WRITER_H #define LLD_ELF_WRITER_H +#include + +namespace llvm { + class StringRef; +} + namespace lld { namespace elf { - +template class InputSectionBase; +template class ObjectFile; template class SymbolTable; template void writeResult(SymbolTable *Symtab); template void markLive(); + +template +llvm::StringRef getOutputSectionName(InputSectionBase *S); + +template +void reportDiscarded(InputSectionBase *IS, + const std::unique_ptr> &File); } } Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -74,8 +74,6 @@ void writeHeader(); void writeSections(); void writeBuildId(); - bool isDiscarded(InputSectionBase *IS) const; - StringRef getOutputSectionName(InputSectionBase *S) const; bool needsInterpSection() const { return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty(); } @@ -103,6 +101,30 @@ }; } // anonymous namespace +template +StringRef elf::getOutputSectionName(InputSectionBase *S) { + StringRef Dest = Script::X->getOutputSection(S); + if (!Dest.empty()) + return Dest; + + StringRef Name = S->getSectionName(); + for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.", + ".init_array.", ".fini_array.", ".ctors.", ".dtors.", + ".tbss.", ".gcc_except_table.", ".tdata."}) + if (Name.startswith(V)) + return V.drop_back(); + return Name; +} + +template +void elf::reportDiscarded(InputSectionBase *IS, + const std::unique_ptr> &File) { + if (!Config->PrintGcSections || !IS || IS->Live) + return; + errs() << "removing unused section from '" << IS->getSectionName() + << "' in file '" << File->getName() << "'\n"; +} + template void elf::writeResult(SymbolTable *Symtab) { typedef typename ELFT::uint uintX_t; typedef typename ELFT::Ehdr Elf_Ehdr; @@ -474,36 +496,6 @@ } template -StringRef Writer::getOutputSectionName(InputSectionBase *S) const { - StringRef Dest = Script::X->getOutputSection(S); - if (!Dest.empty()) - return Dest; - - StringRef Name = S->getSectionName(); - for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.", - ".init_array.", ".fini_array.", ".ctors.", ".dtors.", - ".tbss.", ".gcc_except_table.", ".tdata."}) - if (Name.startswith(V)) - return V.drop_back(); - return Name; -} - -template -void reportDiscarded(InputSectionBase *IS, - const std::unique_ptr> &File) { - if (!Config->PrintGcSections || !IS || IS->Live) - return; - llvm::errs() << "removing unused section from '" << IS->getSectionName() - << "' in file '" << File->getName() << "'\n"; -} - -template -bool Writer::isDiscarded(InputSectionBase *S) const { - return !S || S == &InputSection::Discarded || !S->Live || - Script::X->isDiscarded(S); -} - -template static Symbol *addOptionalSynthetic(SymbolTable &Table, StringRef Name, OutputSectionBase *Sec, typename ELFT::uint Val) {