diff --git a/lld/ELF/Arch/MipsArchTree.cpp b/lld/ELF/Arch/MipsArchTree.cpp index edb3f68c995b7..ff3aa9d40b350 100644 --- a/lld/ELF/Arch/MipsArchTree.cpp +++ b/lld/ELF/Arch/MipsArchTree.cpp @@ -283,8 +283,9 @@ static uint32_t getArchFlags(ArrayRef Files) { template uint32_t elf::getMipsEFlags() { std::vector V; - for (ObjFile *F : ObjFile::Instances) - V.push_back({F->getName(), F->getObj().getHeader()->e_flags}); + for (InputFile *F : ObjectFiles) + V.push_back( + {F->getName(), cast>(F)->getObj().getHeader()->e_flags}); if (V.empty()) return 0; checkFlags(V); diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 853ca9513e26c..ca3b8fae1b90d 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1015,8 +1015,8 @@ template void LinkerDriver::link(opt::InputArgList &Args) { // producing a shared library. // We also need one if any shared libraries are used and for pie executables // (probably because the dynamic linker needs it). - Config->HasDynSymTab = !SharedFile::Instances.empty() || Config->Pic || - Config->ExportDynamic; + Config->HasDynSymTab = + !SharedFiles.empty() || Config->Pic || Config->ExportDynamic; // Some symbols (such as __ehdr_start) are defined lazily only when there // are undefined symbols for them, so we add these to trigger that logic. @@ -1064,11 +1064,11 @@ template void LinkerDriver::link(opt::InputArgList &Args) { // Now that we have a complete list of input files. // Beyond this point, no new files are added. // Aggregate all input sections into one place. - for (ObjFile *F : ObjFile::Instances) + for (InputFile *F : ObjectFiles) for (InputSectionBase *S : F->getSections()) if (S && S != &InputSection::Discarded) InputSections.push_back(S); - for (BinaryFile *F : BinaryFile::Instances) + for (BinaryFile *F : BinaryFiles) for (InputSectionBase *S : F->getSections()) InputSections.push_back(cast(S)); diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 7f81f14f90c9a..80ef567c0124b 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -35,6 +35,11 @@ using namespace llvm::sys::fs; using namespace lld; using namespace lld::elf; +std::vector elf::BinaryFiles; +std::vector elf::BitcodeFiles; +std::vector elf::ObjectFiles; +std::vector elf::SharedFiles; + TarWriter *elf::Tar; InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} @@ -820,8 +825,6 @@ static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) { } } -std::vector BitcodeFile::Instances; - BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName, uint64_t OffsetInArchive) : InputFile(BitcodeKind, MB) { @@ -917,8 +920,6 @@ static ELFKind getELFKind(MemoryBufferRef MB) { return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind; } -std::vector BinaryFile::Instances; - template void BinaryFile::parse() { ArrayRef Data = toArrayRef(MB.getBuffer()); auto *Section = diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index a6720e609a8ea..5a1f6d6a6df4f 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -162,8 +162,6 @@ template class ObjFile : public ELFFileBase { public: static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; } - static std::vector *> Instances; - ArrayRef getLocalSymbols(); ObjFile(MemoryBufferRef M, StringRef ArchiveName); @@ -221,8 +219,6 @@ template class ObjFile : public ELFFileBase { llvm::once_flag InitDwarfLine; }; -template std::vector *> ObjFile::Instances; - // LazyObjFile is analogous to ArchiveFile in the sense that // the file contains lazy symbols. The difference is that // LazyObjFile wraps a single file instead of multiple files. @@ -279,7 +275,6 @@ class BitcodeFile : public InputFile { template void parse(llvm::DenseSet &ComdatGroups); std::unique_ptr Obj; - static std::vector Instances; }; // .so file. @@ -299,8 +294,6 @@ template class SharedFile : public ELFFileBase { public: std::string SoName; - static std::vector *> Instances; - const Elf_Shdr *getSection(const Elf_Sym &Sym) const; llvm::ArrayRef getUndefinedSymbols() { return Undefs; } @@ -332,21 +325,22 @@ template class SharedFile : public ELFFileBase { bool isNeeded() const { return !AsNeeded || IsUsed; } }; -template -std::vector *> SharedFile::Instances; - class BinaryFile : public InputFile { public: explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {} static bool classof(const InputFile *F) { return F->kind() == BinaryKind; } template void parse(); - static std::vector Instances; }; InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "", uint64_t OffsetInArchive = 0); InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName); +extern std::vector BinaryFiles; +extern std::vector BitcodeFiles; +extern std::vector ObjectFiles; +extern std::vector SharedFiles; + } // namespace elf } // namespace lld diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 7183df80c67ad..25b9957eb2624 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -44,7 +44,7 @@ std::string lld::toString(const InputSectionBase *Sec) { return (toString(Sec->File) + ":(" + Sec->Name + ")").str(); } -template DenseMap elf::buildSectionOrder() { +DenseMap elf::buildSectionOrder() { // Build a map from symbols to their priorities. Symbols that didn't // appear in the symbol ordering file have the lowest priority 0. // All explicitly mentioned symbols have negative (higher) priorities. @@ -55,7 +55,7 @@ template DenseMap elf::buildSectionOrder() { // Build a map from sections to their priorities. DenseMap SectionOrder; - for (ObjFile *File : ObjFile::Instances) { + for (InputFile *File : ObjectFiles) { for (SymbolBody *Body : File->getSymbols()) { auto *D = dyn_cast(Body); if (!D || !D->Section) @@ -1001,11 +1001,6 @@ uint64_t MergeInputSection::getOffset(uint64_t Offset) const { return Piece.OutputOff + Addend; } -template DenseMap elf::buildSectionOrder(); -template DenseMap elf::buildSectionOrder(); -template DenseMap elf::buildSectionOrder(); -template DenseMap elf::buildSectionOrder(); - template InputSection::InputSection(ObjFile *, const ELF32LE::Shdr *, StringRef); template InputSection::InputSection(ObjFile *, const ELF32BE::Shdr *, diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index 11382a7123d5d..15436dfd37be5 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -338,7 +338,7 @@ class InputSection : public InputSectionBase { extern std::vector InputSections; // Builds section order for handling --symbol-ordering-file. -template llvm::DenseMap buildSectionOrder(); +llvm::DenseMap buildSectionOrder(); } // namespace elf diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index a033cfc900788..908ebe504be07 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -241,25 +241,10 @@ static void sortSections(InputSection **Begin, InputSection **End, std::stable_sort(Begin, End, getComparator(K)); } -static llvm::DenseMap getSectionOrder() { - switch (Config->EKind) { - case ELF32LEKind: - return buildSectionOrder(); - case ELF32BEKind: - return buildSectionOrder(); - case ELF64LEKind: - return buildSectionOrder(); - case ELF64BEKind: - return buildSectionOrder(); - default: - llvm_unreachable("unknown ELF type"); - } -} - static void sortBySymbolOrder(InputSection **Begin, InputSection **End) { if (Config->SymbolOrderingFile.empty()) return; - static llvm::DenseMap Order = getSectionOrder(); + static llvm::DenseMap Order = buildSectionOrder(); MutableArrayRef In(Begin, End - Begin); sortByOrder(In, [&](InputSectionBase *S) { return Order.lookup(S); }); } diff --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp index d20edb2d48ec7..d744a82f2c751 100644 --- a/lld/ELF/MapFile.cpp +++ b/lld/ELF/MapFile.cpp @@ -50,7 +50,7 @@ static std::string indent(int Depth) { return std::string(Depth * 8, ' '); } // Returns a list of all symbols that we want to print out. template static std::vector getSymbols() { std::vector V; - for (ObjFile *File : ObjFile::Instances) { + for (InputFile *File : ObjectFiles) { for (SymbolBody *B : File->getSymbols()) { if (auto *DR = dyn_cast(B)) { if (DR->getFile() == File && !DR->isSection() && DR->Section && diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp index af60af616d4f4..e1fcba63f2a0d 100644 --- a/lld/ELF/SymbolTable.cpp +++ b/lld/ELF/SymbolTable.cpp @@ -62,7 +62,7 @@ template void SymbolTable::addFile(InputFile *File) { // Binary file if (auto *F = dyn_cast(File)) { - BinaryFile::Instances.push_back(F); + BinaryFiles.push_back(F); F->parse(); return; } @@ -88,22 +88,21 @@ template void SymbolTable::addFile(InputFile *File) { F->parseSoName(); if (ErrorCount || !SoNames.insert(F->SoName).second) return; - SharedFile::Instances.push_back(F); + SharedFiles.push_back(F); F->parseRest(); return; } // LLVM bitcode file if (auto *F = dyn_cast(File)) { - BitcodeFile::Instances.push_back(F); + BitcodeFiles.push_back(F); F->parse(ComdatGroups); return; } // Regular object file - auto *F = cast>(File); - ObjFile::Instances.push_back(F); - F->parse(ComdatGroups); + ObjectFiles.push_back(File); + cast>(File)->parse(ComdatGroups); } // This function is where all the optimizations of link-time @@ -114,19 +113,18 @@ template void SymbolTable::addFile(InputFile *File) { // Because all bitcode files that consist of a program are passed // to the compiler at once, it can do whole-program optimization. template void SymbolTable::addCombinedLTOObject() { - if (BitcodeFile::Instances.empty()) + if (BitcodeFiles.empty()) return; // Compile bitcode files and replace bitcode symbols. LTO.reset(new BitcodeCompiler); - for (BitcodeFile *F : BitcodeFile::Instances) + for (BitcodeFile *F : BitcodeFiles) LTO->add(*F); for (InputFile *File : LTO->compile()) { - ObjFile *Obj = cast>(File); DenseSet DummyGroups; - Obj->parse(DummyGroups); - ObjFile::Instances.push_back(Obj); + cast>(File)->parse(DummyGroups); + ObjectFiles.push_back(File); } } @@ -593,8 +591,8 @@ template void SymbolTable::scanUndefinedFlags() { // shared libraries can find them. // Except this, we ignore undefined symbols in DSOs. template void SymbolTable::scanShlibUndefined() { - for (SharedFile *File : SharedFile::Instances) { - for (StringRef U : File->getUndefinedSymbols()) { + for (InputFile *F : SharedFiles) { + for (StringRef U : cast>(F)->getUndefinedSymbols()) { SymbolBody *Sym = find(U); if (!Sym || !Sym->isDefined()) continue; diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 901a43d4dff95..e1b14d6199691 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1028,9 +1028,11 @@ template void DynamicSection::addEntries() { if (!Config->Rpath.empty()) add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, InX::DynStrTab->addString(Config->Rpath)}); - for (SharedFile *F : SharedFile::Instances) + for (InputFile *File : SharedFiles) { + SharedFile *F = cast>(File); if (F->isNeeded()) add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)}); + } if (!Config->SoName.empty()) add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)}); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 4f5a4500ecfe9..9f1cea5501ad8 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -115,9 +115,9 @@ StringRef elf::getOutputSectionName(StringRef Name) { return Name; } -template static bool needsInterpSection() { - return !SharedFile::Instances.empty() && - !Config->DynamicLinker.empty() && !Script->ignoreInterpSection(); +static bool needsInterpSection() { + return !SharedFiles.empty() && !Config->DynamicLinker.empty() && + !Script->ignoreInterpSection(); } template void elf::writeResult() { Writer().run(); } @@ -273,7 +273,7 @@ template void Writer::createSyntheticSections() { Out::ProgramHeaders = make("", 0, SHF_ALLOC); Out::ProgramHeaders->updateAlignment(Config->Wordsize); - if (needsInterpSection()) { + if (needsInterpSection()) { InX::Interp = createInterpSection(); Add(InX::Interp); } else { @@ -454,7 +454,8 @@ static bool includeInSymtab(const SymbolBody &B) { template void Writer::copyLocalSymbols() { if (!InX::SymTab) return; - for (ObjFile *F : ObjFile::Instances) { + for (InputFile *File : ObjectFiles) { + ObjFile *F = cast>(File); for (SymbolBody *B : F->getLocalSymbols()) { if (!B->IsLocal) fatal(toString(F) + @@ -862,12 +863,12 @@ static void sortCtorsDtors(OutputSection *Cmd) { } // Sort input sections using the list provided by --symbol-ordering-file. -template static void sortBySymbolsOrder() { +static void sortBySymbolsOrder() { if (Config->SymbolOrderingFile.empty()) return; // Sort sections by priority. - DenseMap SectionOrder = buildSectionOrder(); + DenseMap SectionOrder = buildSectionOrder(); for (BaseCommand *Base : Script->Opt.Commands) if (auto *Sec = dyn_cast(Base)) Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); }); @@ -897,7 +898,7 @@ template void Writer::createSections() { Old.end()); Script->fabricateDefaultCommands(); - sortBySymbolsOrder(); + sortBySymbolsOrder(); sortInitFini(findSection(".init_array")); sortInitFini(findSection(".fini_array")); sortCtorsDtors(findSection(".ctors"));