//===- SymbolTable.cpp ----------------------------------------------------===// // // The LLVM Linker // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // Symbol table is a bag of all known symbols. We put all symbols of // all input files to the symbol table. The symbol Table is basically // a hash table with the logic to resolve symbol name conflicts using // the symbol types. // //===----------------------------------------------------------------------===// #include "SymbolTable.h" #include "Config.h" #include "Error.h" #include "Symbols.h" using namespace llvm; using namespace llvm::object; using namespace llvm::ELF; using namespace lld; using namespace lld::elf2; template SymbolTable::SymbolTable() {} template static void checkCompatibility(InputFile *FileP) { auto *F = dyn_cast>(FileP); if (!F) return; if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine) return; StringRef A = F->getName(); StringRef B = Config->Emulation; if (B.empty()) B = Config->FirstElf->getName(); error(A + " is incompatible with " + B); } template void SymbolTable::addFile(std::unique_ptr File) { InputFile *FileP = File.get(); checkCompatibility(FileP); // .a file if (auto *F = dyn_cast(FileP)) { ArchiveFiles.emplace_back(cast(File.release())); F->parse(); for (Lazy &Sym : F->getLazySymbols()) addLazy(&Sym); return; } // .so file if (auto *F = dyn_cast>(FileP)) { // DSOs are uniquified not by filename but by soname. F->parseSoName(); if (!IncludedSoNames.insert(F->getSoName()).second) return; SharedFiles.emplace_back(cast>(File.release())); F->parse(); for (SharedSymbol &B : F->getSharedSymbols()) resolve(&B); return; } // .o file auto *F = cast>(FileP); ObjectFiles.emplace_back(cast>(File.release())); F->parse(Comdats); for (SymbolBody *B : F->getSymbols()) resolve(B); } template SymbolBody *SymbolTable::addUndefined(StringRef Name) { auto *Sym = new (Alloc) Undefined(Name, false, STV_DEFAULT, false); resolve(Sym); return Sym; } template SymbolBody *SymbolTable::addUndefinedOpt(StringRef Name) { auto *Sym = new (Alloc) Undefined(Name, false, STV_HIDDEN, true); resolve(Sym); return Sym; } template void SymbolTable::addAbsolute(StringRef Name, typename ELFFile::Elf_Sym &ESym) { resolve(new (Alloc) DefinedAbsolute(Name, ESym)); } template void SymbolTable::addSynthetic(StringRef Name, OutputSectionBase &Section, typename ELFFile::uintX_t Value) { auto *Sym = new (Alloc) DefinedSynthetic(Name, Value, Section); resolve(Sym); } template SymbolBody *SymbolTable::addIgnored(StringRef Name) { auto *Sym = new (Alloc) DefinedAbsolute(Name, DefinedAbsolute::IgnoreUndef); resolve(Sym); return Sym; } template bool SymbolTable::isUndefined(StringRef Name) { if (SymbolBody *Sym = find(Name)) return Sym->isUndefined(); return false; } // Returns a file from which symbol B was created. // If B does not belong to any file in ObjectFiles, returns a nullptr. template ELFFileBase * elf2::findFile(ArrayRef>> ObjectFiles, const SymbolBody *B) { for (const std::unique_ptr> &F : ObjectFiles) { ArrayRef Syms = F->getSymbols(); if (std::find(Syms.begin(), Syms.end(), B) != Syms.end()) return F.get(); } return nullptr; } template std::string SymbolTable::conflictMsg(SymbolBody *Old, SymbolBody *New) { ELFFileBase *OldFile = findFile(ObjectFiles, Old); ELFFileBase *NewFile = findFile(ObjectFiles, New); StringRef Sym = Old->getName(); StringRef F1 = OldFile ? OldFile->getName() : "(internal)"; StringRef F2 = NewFile ? NewFile->getName() : "(internal)"; return (Sym + " in " + F1 + " and " + F2).str(); } // This function resolves conflicts if there's an existing symbol with // the same name. Decisions are made based on symbol type. template void SymbolTable::resolve(SymbolBody *New) { Symbol *Sym = insert(New); if (Sym->Body == New) return; SymbolBody *Existing = Sym->Body; if (Lazy *L = dyn_cast(Existing)) { if (auto *Undef = dyn_cast(New)) { addMemberFile(Undef, L); return; } // Found a definition for something also in an archive. // Ignore the archive definition. Sym->Body = New; return; } if (New->isTls() != Existing->isTls()) error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New)); // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, // equivalent (conflicting), or more preferable, respectively. int comp = Existing->compare(New); if (comp == 0) { std::string S = "duplicate symbol: " + conflictMsg(Existing, New); if (!Config->AllowMultipleDefinition) error(S); warning(S); return; } if (comp < 0) Sym->Body = New; } template Symbol *SymbolTable::insert(SymbolBody *New) { // Find an existing Symbol or create and insert a new one. StringRef Name = New->getName(); Symbol *&Sym = Symtab[Name]; if (!Sym) Sym = new (Alloc) Symbol{New}; New->setBackref(Sym); return Sym; } template SymbolBody *SymbolTable::find(StringRef Name) { auto It = Symtab.find(Name); if (It == Symtab.end()) return nullptr; return It->second->Body; } template void SymbolTable::addLazy(Lazy *L) { Symbol *Sym = insert(L); if (Sym->Body == L) return; if (auto *Undef = dyn_cast(Sym->Body)) { Sym->Body = L; addMemberFile(Undef, L); } } template void SymbolTable::addMemberFile(Undefined *Undef, Lazy *L) { // Weak undefined symbols should not fetch members from archives. // If we were to keep old symbol we would not know that an archive member was // available if a strong undefined symbol shows up afterwards in the link. // If a strong undefined symbol never shows up, this lazy symbol will // get to the end of the link and must be treated as the weak undefined one. // We set UsedInRegularObj in a similar way to what is done with shared // symbols and mark it as weak to reduce how many special cases are needed. if (Undef->isWeak()) { L->setUsedInRegularObj(); L->setWeak(); return; } // Fetch a member file that has the definition for L. // getMember returns nullptr if the member was already read from the library. if (std::unique_ptr File = L->getMember()) addFile(std::move(File)); } // This function takes care of the case in which shared libraries depend on // the user program (not the other way, which is usual). Shared libraries // may have undefined symbols, expecting that the user program provides // the definitions for them. An example is BSD's __progname symbol. // We need to put such symbols to the main program's .dynsym so that // shared libraries can find them. // Except this, we ignore undefined symbols in DSOs. template void SymbolTable::scanShlibUndefined() { for (std::unique_ptr> &File : SharedFiles) for (StringRef U : File->getUndefinedSymbols()) if (SymbolBody *Sym = find(U)) if (Sym->isDefined()) Sym->setUsedInDynamicReloc(); } template class lld::elf2::SymbolTable; template class lld::elf2::SymbolTable; template class lld::elf2::SymbolTable; template class lld::elf2::SymbolTable; template ELFFileBase * lld::elf2::findFile(ArrayRef>>, const SymbolBody *); template ELFFileBase * lld::elf2::findFile(ArrayRef>>, const SymbolBody *); template ELFFileBase * lld::elf2::findFile(ArrayRef>>, const SymbolBody *); template ELFFileBase * lld::elf2::findFile(ArrayRef>>, const SymbolBody *);