Index: lld/trunk/ELF/InputFiles.h =================================================================== --- lld/trunk/ELF/InputFiles.h +++ lld/trunk/ELF/InputFiles.h @@ -87,16 +87,15 @@ // string for creating error messages. StringRef ArchiveName; - // Filename used for logging. It is either in the form of "foo.o" or - // "bar.a(foo.o)". - std::string LogName; - // If this is an architecture-specific file, the following members // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type. ELFKind EKind = ELFNoneKind; uint16_t EMachine = llvm::ELF::EM_NONE; uint8_t OSABI = 0; + // Cache for toString(). Only toString() should use this member. + mutable std::string ToStringCache; + protected: InputFile(Kind K, MemoryBufferRef M); std::vector Sections; Index: lld/trunk/ELF/InputFiles.cpp =================================================================== --- lld/trunk/ELF/InputFiles.cpp +++ lld/trunk/ELF/InputFiles.cpp @@ -38,8 +38,7 @@ TarWriter *elf::Tar; -InputFile::InputFile(Kind K, MemoryBufferRef M) - : MB(M), LogName(M.getBufferIdentifier()), FileKind(K) {} +InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} namespace { // In ELF object file all section addresses are zero. If we have multiple @@ -77,7 +76,7 @@ template void elf::ObjectFile::initializeDwarfLine() { std::unique_ptr Obj = - check(object::ObjectFile::createObjectFile(this->MB), this->LogName); + check(object::ObjectFile::createObjectFile(this->MB), toString(this)); ObjectInfo ObjInfo; DWARFContextInMemory Dwarf(*Obj, &ObjInfo); @@ -127,7 +126,16 @@ // Returns "(internal)", "foo.a(bar.o)" or "baz.o". std::string lld::toString(const InputFile *F) { - return F ? F->LogName : "(internal)"; + if (!F) + return "(internal)"; + + if (F->ToStringCache.empty()) { + if (F->ArchiveName.empty()) + F->ToStringCache = F->getName(); + else + F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str(); + } + return F->ToStringCache; } template static ELFKind getELFKind() { @@ -151,19 +159,19 @@ template uint32_t ELFFileBase::getSectionIndex(const Elf_Sym &Sym) const { return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX), - this->LogName); + toString(this)); } template void ELFFileBase::initSymtab(ArrayRef Sections, const Elf_Shdr *Symtab) { FirstNonLocal = Symtab->sh_info; - Symbols = check(getObj().symbols(Symtab), this->LogName); + Symbols = check(getObj().symbols(Symtab), toString(this)); if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size()) fatal(toString(this) + ": invalid sh_info in symbol table"); - StringTable = - check(getObj().getStringTableForSymtab(*Symtab, Sections), this->LogName); + StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections), + toString(this)); } template @@ -206,10 +214,10 @@ if (this->Symbols.empty()) this->initSymtab( Sections, - check(object::getSection(Sections, Sec.sh_link), this->LogName)); - const Elf_Sym *Sym = - check(object::getSymbol(this->Symbols, Sec.sh_info), this->LogName); - return check(Sym->getName(this->StringTable), this->LogName); + check(object::getSection(Sections, Sec.sh_link), toString(this))); + const Elf_Sym *Sym = check( + object::getSymbol(this->Symbols, Sec.sh_info), toString(this)); + return check(Sym->getName(this->StringTable), toString(this)); } template @@ -217,7 +225,7 @@ elf::ObjectFile::getShtGroupEntries(const Elf_Shdr &Sec) { const ELFFile &Obj = this->getObj(); ArrayRef Entries = check( - Obj.template getSectionContentsAsArray(&Sec), this->LogName); + Obj.template getSectionContentsAsArray(&Sec), toString(this)); if (Entries.empty() || Entries[0] != GRP_COMDAT) fatal(toString(this) + ": unsupported SHT_GROUP format"); return Entries.slice(1); @@ -280,13 +288,13 @@ void elf::ObjectFile::initializeSections( DenseSet &ComdatGroups) { ArrayRef ObjSections = - check(this->getObj().sections(), this->LogName); + check(this->getObj().sections(), toString(this)); const ELFFile &Obj = this->getObj(); uint64_t Size = ObjSections.size(); this->Sections.resize(Size); unsigned I = -1; StringRef SectionStringTable = - check(Obj.getSectionStringTable(ObjSections), this->LogName); + check(Obj.getSectionStringTable(ObjSections), toString(this)); for (const Elf_Shdr &Sec : ObjSections) { ++I; if (this->Sections[I] == &InputSection::Discarded) @@ -320,7 +328,7 @@ break; case SHT_SYMTAB_SHNDX: this->SymtabSHNDX = - check(Obj.getSHNDXTable(Sec, ObjSections), this->LogName); + check(Obj.getSHNDXTable(Sec, ObjSections), toString(this)); break; case SHT_STRTAB: case SHT_NULL: @@ -364,7 +372,7 @@ elf::ObjectFile::createInputSection(const Elf_Shdr &Sec, StringRef SectionStringTable) { StringRef Name = check( - this->getObj().getSectionName(&Sec, SectionStringTable), this->LogName); + this->getObj().getSectionName(&Sec, SectionStringTable), toString(this)); switch (Sec.sh_type) { case SHT_ARM_ATTRIBUTES: @@ -403,12 +411,12 @@ size_t NumRelocations; if (Sec.sh_type == SHT_RELA) { ArrayRef Rels = - check(this->getObj().relas(&Sec), this->LogName); + check(this->getObj().relas(&Sec), toString(this)); Target->FirstRelocation = Rels.begin(); NumRelocations = Rels.size(); Target->AreRelocsRela = true; } else { - ArrayRef Rels = check(this->getObj().rels(&Sec), this->LogName); + ArrayRef Rels = check(this->getObj().rels(&Sec), toString(this)); Target->FirstRelocation = Rels.begin(); NumRelocations = Rels.size(); Target->AreRelocsRela = false; @@ -520,7 +528,7 @@ if (Binding == STB_LOCAL) { if (Sym->getType() == STT_FILE) - SourceFile = check(Sym->getName(this->StringTable), this->LogName); + SourceFile = check(Sym->getName(this->StringTable), toString(this)); if (this->StringTable.size() <= Sym->st_name) fatal(toString(this) + ": invalid symbol name offset"); @@ -533,7 +541,7 @@ Size, Sec, this); } - StringRef Name = check(Sym->getName(this->StringTable), this->LogName); + StringRef Name = check(Sym->getName(this->StringTable), toString(this)); switch (Sym->st_shndx) { case SHN_UNDEF: @@ -584,7 +592,7 @@ std::pair ArchiveFile::getMember(const Archive::Symbol *Sym) { Archive::Child C = - check(Sym->getMember(), this->LogName + + check(Sym->getMember(), toString(this) + ": could not get the member for symbol " + Sym->getName()); @@ -593,12 +601,12 @@ MemoryBufferRef Ret = check(C.getMemoryBufferRef(), - this->LogName + + toString(this) + ": could not get the buffer for the member defining symbol " + Sym->getName()); if (C.getParent()->isThin() && Tar) - Tar->append(relativeToRoot(check(C.getFullName(), this->LogName)), + Tar->append(relativeToRoot(check(C.getFullName(), toString(this))), Ret.getBuffer()); if (C.getParent()->isThin()) return {Ret, 0}; @@ -614,7 +622,7 @@ SharedFile::getSection(const Elf_Sym &Sym) const { return check( this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX), - this->LogName); + toString(this)); } // Partially parse the shared object file so that we can call @@ -623,7 +631,7 @@ const Elf_Shdr *DynamicSec = nullptr; const ELFFile Obj = this->getObj(); - ArrayRef Sections = check(Obj.sections(), this->LogName); + ArrayRef Sections = check(Obj.sections(), toString(this)); for (const Elf_Shdr &Sec : Sections) { switch (Sec.sh_type) { default: @@ -636,7 +644,7 @@ break; case SHT_SYMTAB_SHNDX: this->SymtabSHNDX = - check(Obj.getSHNDXTable(Sec, Sections), this->LogName); + check(Obj.getSHNDXTable(Sec, Sections), toString(this)); break; case SHT_GNU_versym: this->VersymSec = &Sec; @@ -660,7 +668,7 @@ ArrayRef Arr = check(Obj.template getSectionContentsAsArray(DynamicSec), - this->LogName); + toString(this)); for (const Elf_Dyn &Dyn : Arr) { if (Dyn.d_tag == DT_SONAME) { uint64_t Val = Dyn.getVal(); @@ -729,7 +737,7 @@ bool Hidden = VersymIndex & VERSYM_HIDDEN; VersymIndex = VersymIndex & ~VERSYM_HIDDEN; - StringRef Name = check(Sym.getName(this->StringTable), this->LogName); + StringRef Name = check(Sym.getName(this->StringTable), toString(this)); if (Sym.isUndefined()) { Undefs.push_back(Name); continue; @@ -852,7 +860,7 @@ MemoryBufferRef MBRef(MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier() + utostr(OffsetInArchive))); - Obj = check(lto::InputFile::create(MBRef), this->LogName); + Obj = check(lto::InputFile::create(MBRef), toString(this)); std::vector KeptComdats; for (StringRef S : Obj->getComdatTable()) @@ -929,8 +937,6 @@ InputFile *F = isBitcode(MB) ? make(MB, OffsetInArchive) : createELFFile(MB); F->ArchiveName = ArchiveName; - if (!ArchiveName.empty()) - F->LogName = (ArchiveName + "(" + F->LogName + ")").str(); return F; } @@ -956,20 +962,20 @@ typedef typename ELFT::SymRange Elf_Sym_Range; const ELFFile Obj(this->MB.getBuffer()); - ArrayRef Sections = check(Obj.sections(), this->LogName); + ArrayRef Sections = check(Obj.sections(), toString(this)); for (const Elf_Shdr &Sec : Sections) { if (Sec.sh_type != SHT_SYMTAB) continue; - Elf_Sym_Range Syms = check(Obj.symbols(&Sec), this->LogName); + Elf_Sym_Range Syms = check(Obj.symbols(&Sec), toString(this)); uint32_t FirstNonLocal = Sec.sh_info; StringRef StringTable = - check(Obj.getStringTableForSymtab(Sec, Sections), this->LogName); + check(Obj.getStringTableForSymtab(Sec, Sections), toString(this)); std::vector V; for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal)) if (Sym.st_shndx != SHN_UNDEF) - V.push_back(check(Sym.getName(StringTable), this->LogName)); + V.push_back(check(Sym.getName(StringTable), toString(this))); return V; } return {}; @@ -977,7 +983,7 @@ std::vector LazyObjectFile::getBitcodeSymbols() { std::unique_ptr Obj = - check(lto::InputFile::create(this->MB), this->LogName); + check(lto::InputFile::create(this->MB), toString(this)); std::vector V; for (const lto::InputFile::Symbol &Sym : Obj->symbols()) if (!Sym.isUndefined())