[lld] r313619 - [ELF] - Introduce std::vector<InputFile *> global arrays.
Rafael Avila de Espindola via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 19 11:11:30 PDT 2017
Thanks!
George Rimar via llvm-commits <llvm-commits at lists.llvm.org> writes:
> Author: grimar
> Date: Tue Sep 19 02:20:54 2017
> New Revision: 313619
>
> URL: http://llvm.org/viewvc/llvm-project?rev=313619&view=rev
> Log:
> [ELF] - Introduce std::vector<InputFile *> global arrays.
>
> This patch removes lot of static Instances arrays from different input file
> classes and introduces global arrays for access instead. Similar to arrays we
> have for InputSections/OutputSectionCommands.
>
> It allows to iterate over input files in a non-templated code.
>
> Differential revision: https://reviews.llvm.org/D35987
>
> Modified:
> lld/trunk/ELF/Arch/MipsArchTree.cpp
> lld/trunk/ELF/Driver.cpp
> lld/trunk/ELF/InputFiles.cpp
> lld/trunk/ELF/InputFiles.h
> lld/trunk/ELF/InputSection.cpp
> lld/trunk/ELF/InputSection.h
> lld/trunk/ELF/LinkerScript.cpp
> lld/trunk/ELF/MapFile.cpp
> lld/trunk/ELF/SymbolTable.cpp
> lld/trunk/ELF/SyntheticSections.cpp
> lld/trunk/ELF/Writer.cpp
>
> Modified: lld/trunk/ELF/Arch/MipsArchTree.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Arch/MipsArchTree.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Arch/MipsArchTree.cpp (original)
> +++ lld/trunk/ELF/Arch/MipsArchTree.cpp Tue Sep 19 02:20:54 2017
> @@ -283,8 +283,9 @@ static uint32_t getArchFlags(ArrayRef<Fi
>
> template <class ELFT> uint32_t elf::getMipsEFlags() {
> std::vector<FileFlags> V;
> - for (ObjFile<ELFT> *F : ObjFile<ELFT>::Instances)
> - V.push_back({F->getName(), F->getObj().getHeader()->e_flags});
> + for (InputFile *F : ObjectFiles)
> + V.push_back(
> + {F->getName(), cast<ObjFile<ELFT>>(F)->getObj().getHeader()->e_flags});
> if (V.empty())
> return 0;
> checkFlags(V);
>
> Modified: lld/trunk/ELF/Driver.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Driver.cpp (original)
> +++ lld/trunk/ELF/Driver.cpp Tue Sep 19 02:20:54 2017
> @@ -1015,8 +1015,8 @@ template <class ELFT> void LinkerDriver:
> // 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<ELFT>::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 <class ELFT> void LinkerDriver:
> // 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<ELFT> *F : ObjFile<ELFT>::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<InputSection>(S));
>
>
> Modified: lld/trunk/ELF/InputFiles.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/InputFiles.cpp (original)
> +++ lld/trunk/ELF/InputFiles.cpp Tue Sep 19 02:20:54 2017
> @@ -35,6 +35,11 @@ using namespace llvm::sys::fs;
> using namespace lld;
> using namespace lld::elf;
>
> +std::vector<BinaryFile *> elf::BinaryFiles;
> +std::vector<BitcodeFile *> elf::BitcodeFiles;
> +std::vector<InputFile *> elf::ObjectFiles;
> +std::vector<InputFile *> elf::SharedFiles;
> +
> TarWriter *elf::Tar;
>
> InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
> @@ -820,8 +825,6 @@ static uint8_t getBitcodeMachineKind(Str
> }
> }
>
> -std::vector<BitcodeFile *> BitcodeFile::Instances;
> -
> BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
> uint64_t OffsetInArchive)
> : InputFile(BitcodeKind, MB) {
> @@ -917,8 +920,6 @@ static ELFKind getELFKind(MemoryBufferRe
> return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
> }
>
> -std::vector<BinaryFile *> BinaryFile::Instances;
> -
> template <class ELFT> void BinaryFile::parse() {
> ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer());
> auto *Section =
>
> Modified: lld/trunk/ELF/InputFiles.h
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/InputFiles.h (original)
> +++ lld/trunk/ELF/InputFiles.h Tue Sep 19 02:20:54 2017
> @@ -162,8 +162,6 @@ template <class ELFT> class ObjFile : pu
> public:
> static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; }
>
> - static std::vector<ObjFile<ELFT> *> Instances;
> -
> ArrayRef<SymbolBody *> getLocalSymbols();
>
> ObjFile(MemoryBufferRef M, StringRef ArchiveName);
> @@ -221,8 +219,6 @@ private:
> llvm::once_flag InitDwarfLine;
> };
>
> -template <class ELFT> std::vector<ObjFile<ELFT> *> ObjFile<ELFT>::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 @@ public:
> template <class ELFT>
> void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
> std::unique_ptr<llvm::lto::InputFile> Obj;
> - static std::vector<BitcodeFile *> Instances;
> };
>
> // .so file.
> @@ -299,8 +294,6 @@ template <class ELFT> class SharedFile :
> public:
> std::string SoName;
>
> - static std::vector<SharedFile<ELFT> *> Instances;
> -
> const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
> llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
>
> @@ -332,21 +325,22 @@ public:
> bool isNeeded() const { return !AsNeeded || IsUsed; }
> };
>
> -template <class ELFT>
> -std::vector<SharedFile<ELFT> *> SharedFile<ELFT>::Instances;
> -
> class BinaryFile : public InputFile {
> public:
> explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
> static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
> template <class ELFT> void parse();
> - static std::vector<BinaryFile *> Instances;
> };
>
> InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
> uint64_t OffsetInArchive = 0);
> InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
>
> +extern std::vector<BinaryFile *> BinaryFiles;
> +extern std::vector<BitcodeFile *> BitcodeFiles;
> +extern std::vector<InputFile *> ObjectFiles;
> +extern std::vector<InputFile *> SharedFiles;
> +
> } // namespace elf
> } // namespace lld
>
>
> Modified: lld/trunk/ELF/InputSection.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/InputSection.cpp (original)
> +++ lld/trunk/ELF/InputSection.cpp Tue Sep 19 02:20:54 2017
> @@ -44,7 +44,7 @@ std::string lld::toString(const InputSec
> return (toString(Sec->File) + ":(" + Sec->Name + ")").str();
> }
>
> -template <class ELFT> DenseMap<SectionBase *, int> elf::buildSectionOrder() {
> +DenseMap<SectionBase *, int> 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 <class ELFT> DenseMap<SectionBa
>
> // Build a map from sections to their priorities.
> DenseMap<SectionBase *, int> SectionOrder;
> - for (ObjFile<ELFT> *File : ObjFile<ELFT>::Instances) {
> + for (InputFile *File : ObjectFiles) {
> for (SymbolBody *Body : File->getSymbols()) {
> auto *D = dyn_cast<DefinedRegular>(Body);
> if (!D || !D->Section)
> @@ -1001,11 +1001,6 @@ uint64_t MergeInputSection::getOffset(ui
> return Piece.OutputOff + Addend;
> }
>
> -template DenseMap<SectionBase *, int> elf::buildSectionOrder<ELF32LE>();
> -template DenseMap<SectionBase *, int> elf::buildSectionOrder<ELF32BE>();
> -template DenseMap<SectionBase *, int> elf::buildSectionOrder<ELF64LE>();
> -template DenseMap<SectionBase *, int> elf::buildSectionOrder<ELF64BE>();
> -
> template InputSection::InputSection(ObjFile<ELF32LE> *, const ELF32LE::Shdr *,
> StringRef);
> template InputSection::InputSection(ObjFile<ELF32BE> *, const ELF32BE::Shdr *,
>
> Modified: lld/trunk/ELF/InputSection.h
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/InputSection.h (original)
> +++ lld/trunk/ELF/InputSection.h Tue Sep 19 02:20:54 2017
> @@ -338,7 +338,7 @@ private:
> extern std::vector<InputSectionBase *> InputSections;
>
> // Builds section order for handling --symbol-ordering-file.
> -template <class ELFT> llvm::DenseMap<SectionBase *, int> buildSectionOrder();
> +llvm::DenseMap<SectionBase *, int> buildSectionOrder();
>
> } // namespace elf
>
>
> Modified: lld/trunk/ELF/LinkerScript.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/LinkerScript.cpp (original)
> +++ lld/trunk/ELF/LinkerScript.cpp Tue Sep 19 02:20:54 2017
> @@ -241,25 +241,10 @@ static void sortSections(InputSection **
> std::stable_sort(Begin, End, getComparator(K));
> }
>
> -static llvm::DenseMap<SectionBase *, int> getSectionOrder() {
> - switch (Config->EKind) {
> - case ELF32LEKind:
> - return buildSectionOrder<ELF32LE>();
> - case ELF32BEKind:
> - return buildSectionOrder<ELF32BE>();
> - case ELF64LEKind:
> - return buildSectionOrder<ELF64LE>();
> - case ELF64BEKind:
> - return buildSectionOrder<ELF64BE>();
> - default:
> - llvm_unreachable("unknown ELF type");
> - }
> -}
> -
> static void sortBySymbolOrder(InputSection **Begin, InputSection **End) {
> if (Config->SymbolOrderingFile.empty())
> return;
> - static llvm::DenseMap<SectionBase *, int> Order = getSectionOrder();
> + static llvm::DenseMap<SectionBase *, int> Order = buildSectionOrder();
> MutableArrayRef<InputSection *> In(Begin, End - Begin);
> sortByOrder(In, [&](InputSectionBase *S) { return Order.lookup(S); });
> }
>
> Modified: lld/trunk/ELF/MapFile.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MapFile.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/MapFile.cpp (original)
> +++ lld/trunk/ELF/MapFile.cpp Tue Sep 19 02:20:54 2017
> @@ -50,7 +50,7 @@ static std::string indent(int Depth) { r
> // Returns a list of all symbols that we want to print out.
> template <class ELFT> static std::vector<Defined *> getSymbols() {
> std::vector<Defined *> V;
> - for (ObjFile<ELFT> *File : ObjFile<ELFT>::Instances) {
> + for (InputFile *File : ObjectFiles) {
> for (SymbolBody *B : File->getSymbols()) {
> if (auto *DR = dyn_cast<DefinedRegular>(B)) {
> if (DR->getFile() == File && !DR->isSection() && DR->Section &&
>
> Modified: lld/trunk/ELF/SymbolTable.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/SymbolTable.cpp (original)
> +++ lld/trunk/ELF/SymbolTable.cpp Tue Sep 19 02:20:54 2017
> @@ -62,7 +62,7 @@ template <class ELFT> void SymbolTable::
>
> // Binary file
> if (auto *F = dyn_cast<BinaryFile>(File)) {
> - BinaryFile::Instances.push_back(F);
> + BinaryFiles.push_back(F);
> F->parse<ELFT>();
> return;
> }
> @@ -88,22 +88,21 @@ template <class ELFT> void SymbolTable::
> F->parseSoName();
> if (ErrorCount || !SoNames.insert(F->SoName).second)
> return;
> - SharedFile<ELFT>::Instances.push_back(F);
> + SharedFiles.push_back(F);
> F->parseRest();
> return;
> }
>
> // LLVM bitcode file
> if (auto *F = dyn_cast<BitcodeFile>(File)) {
> - BitcodeFile::Instances.push_back(F);
> + BitcodeFiles.push_back(F);
> F->parse<ELFT>(ComdatGroups);
> return;
> }
>
> // Regular object file
> - auto *F = cast<ObjFile<ELFT>>(File);
> - ObjFile<ELFT>::Instances.push_back(F);
> - F->parse(ComdatGroups);
> + ObjectFiles.push_back(File);
> + cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
> }
>
> // This function is where all the optimizations of link-time
> @@ -114,19 +113,18 @@ template <class ELFT> void SymbolTable::
> // Because all bitcode files that consist of a program are passed
> // to the compiler at once, it can do whole-program optimization.
> template <class ELFT> 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<ELFT> *Obj = cast<ObjFile<ELFT>>(File);
> DenseSet<CachedHashStringRef> DummyGroups;
> - Obj->parse(DummyGroups);
> - ObjFile<ELFT>::Instances.push_back(Obj);
> + cast<ObjFile<ELFT>>(File)->parse(DummyGroups);
> + ObjectFiles.push_back(File);
> }
> }
>
> @@ -593,8 +591,8 @@ template <class ELFT> void SymbolTable::
> // shared libraries can find them.
> // Except this, we ignore undefined symbols in DSOs.
> template <class ELFT> void SymbolTable::scanShlibUndefined() {
> - for (SharedFile<ELFT> *File : SharedFile<ELFT>::Instances) {
> - for (StringRef U : File->getUndefinedSymbols()) {
> + for (InputFile *F : SharedFiles) {
> + for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
> SymbolBody *Sym = find(U);
> if (!Sym || !Sym->isDefined())
> continue;
>
> Modified: lld/trunk/ELF/SyntheticSections.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/SyntheticSections.cpp (original)
> +++ lld/trunk/ELF/SyntheticSections.cpp Tue Sep 19 02:20:54 2017
> @@ -1028,9 +1028,11 @@ template <class ELFT> void DynamicSectio
> if (!Config->Rpath.empty())
> add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
> InX::DynStrTab->addString(Config->Rpath)});
> - for (SharedFile<ELFT> *F : SharedFile<ELFT>::Instances)
> + for (InputFile *File : SharedFiles) {
> + SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File);
> if (F->isNeeded())
> add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
> + }
> if (!Config->SoName.empty())
> add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
>
>
> Modified: lld/trunk/ELF/Writer.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=313619&r1=313618&r2=313619&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Writer.cpp (original)
> +++ lld/trunk/ELF/Writer.cpp Tue Sep 19 02:20:54 2017
> @@ -115,9 +115,9 @@ StringRef elf::getOutputSectionName(Stri
> return Name;
> }
>
> -template <class ELFT> static bool needsInterpSection() {
> - return !SharedFile<ELFT>::Instances.empty() &&
> - !Config->DynamicLinker.empty() && !Script->ignoreInterpSection();
> +static bool needsInterpSection() {
> + return !SharedFiles.empty() && !Config->DynamicLinker.empty() &&
> + !Script->ignoreInterpSection();
> }
>
> template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
> @@ -273,7 +273,7 @@ template <class ELFT> void Writer<ELFT>:
> Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
> Out::ProgramHeaders->updateAlignment(Config->Wordsize);
>
> - if (needsInterpSection<ELFT>()) {
> + if (needsInterpSection()) {
> InX::Interp = createInterpSection();
> Add(InX::Interp);
> } else {
> @@ -454,7 +454,8 @@ static bool includeInSymtab(const Symbol
> template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
> if (!InX::SymTab)
> return;
> - for (ObjFile<ELFT> *F : ObjFile<ELFT>::Instances) {
> + for (InputFile *File : ObjectFiles) {
> + ObjFile<ELFT> *F = cast<ObjFile<ELFT>>(File);
> for (SymbolBody *B : F->getLocalSymbols()) {
> if (!B->IsLocal)
> fatal(toString(F) +
> @@ -862,12 +863,12 @@ static void sortCtorsDtors(OutputSection
> }
>
> // Sort input sections using the list provided by --symbol-ordering-file.
> -template <class ELFT> static void sortBySymbolsOrder() {
> +static void sortBySymbolsOrder() {
> if (Config->SymbolOrderingFile.empty())
> return;
>
> // Sort sections by priority.
> - DenseMap<SectionBase *, int> SectionOrder = buildSectionOrder<ELFT>();
> + DenseMap<SectionBase *, int> SectionOrder = buildSectionOrder();
> for (BaseCommand *Base : Script->Opt.Commands)
> if (auto *Sec = dyn_cast<OutputSection>(Base))
> Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); });
> @@ -897,7 +898,7 @@ template <class ELFT> void Writer<ELFT>:
> Old.end());
>
> Script->fabricateDefaultCommands();
> - sortBySymbolsOrder<ELFT>();
> + sortBySymbolsOrder();
> sortInitFini(findSection(".init_array"));
> sortInitFini(findSection(".fini_array"));
> sortCtorsDtors(findSection(".ctors"));
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list