10b57cec5SDimitry Andric //===- SymbolTable.h --------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef LLD_ELF_SYMBOL_TABLE_H 100b57cec5SDimitry Andric #define LLD_ELF_SYMBOL_TABLE_H 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "Symbols.h" 130b57cec5SDimitry Andric #include "llvm/ADT/CachedHashString.h" 140b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 15*bdd1243dSDimitry Andric #include "llvm/Support/Compiler.h" 160b57cec5SDimitry Andric 17*bdd1243dSDimitry Andric namespace lld::elf { 180b57cec5SDimitry Andric 1981ad6265SDimitry Andric class InputFile; 2081ad6265SDimitry Andric class SharedFile; 2181ad6265SDimitry Andric 220b57cec5SDimitry Andric // SymbolTable is a bucket of all known symbols, including defined, 230b57cec5SDimitry Andric // undefined, or lazy symbols (the last one is symbols in archive 240b57cec5SDimitry Andric // files whose archive members are not yet loaded). 250b57cec5SDimitry Andric // 260b57cec5SDimitry Andric // We put all symbols of all files to a SymbolTable, and the 270b57cec5SDimitry Andric // SymbolTable selects the "best" symbols if there are name 280b57cec5SDimitry Andric // conflicts. For example, obviously, a defined symbol is better than 290b57cec5SDimitry Andric // an undefined symbol. Or, if there's a conflict between a lazy and a 300b57cec5SDimitry Andric // undefined, it'll read an archive member to read a real definition 310b57cec5SDimitry Andric // to replace the lazy symbol. The logic is implemented in the 320b57cec5SDimitry Andric // add*() functions, which are called by input files as they are parsed. There 330b57cec5SDimitry Andric // is one add* function per symbol type. 340b57cec5SDimitry Andric class SymbolTable { 35480093f4SDimitry Andric public: 36*bdd1243dSDimitry Andric ArrayRef<Symbol *> getSymbols() const { return symVector; } 370b57cec5SDimitry Andric 38480093f4SDimitry Andric void wrap(Symbol *sym, Symbol *real, Symbol *wrap); 39480093f4SDimitry Andric 400b57cec5SDimitry Andric Symbol *insert(StringRef name); 410b57cec5SDimitry Andric 42*bdd1243dSDimitry Andric template <typename T> Symbol *addSymbol(const T &newSym) { 43*bdd1243dSDimitry Andric Symbol *sym = insert(newSym.getName()); 44*bdd1243dSDimitry Andric sym->resolve(newSym); 45*bdd1243dSDimitry Andric return sym; 46*bdd1243dSDimitry Andric } 4781ad6265SDimitry Andric Symbol *addAndCheckDuplicate(const Defined &newSym); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric void scanVersionScript(); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric Symbol *find(StringRef name); 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric void handleDynamicList(); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric // Set of .so files to not link the same shared object file more than once. 5604eeddc0SDimitry Andric llvm::DenseMap<llvm::CachedHashStringRef, SharedFile *> soNames; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric // Comdat groups define "link once" sections. If two comdat groups have the 590b57cec5SDimitry Andric // same name, only one of them is linked, and the other is ignored. This map 600b57cec5SDimitry Andric // is used to uniquify them. 610b57cec5SDimitry Andric llvm::DenseMap<llvm::CachedHashStringRef, const InputFile *> comdatGroups; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric private: 640eae32dcSDimitry Andric SmallVector<Symbol *, 0> findByVersion(SymbolVersion ver); 650eae32dcSDimitry Andric SmallVector<Symbol *, 0> findAllByVersion(SymbolVersion ver, 666e75b2fbSDimitry Andric bool includeNonDefault); 670b57cec5SDimitry Andric 680eae32dcSDimitry Andric llvm::StringMap<SmallVector<Symbol *, 0>> &getDemangledSyms(); 696e75b2fbSDimitry Andric bool assignExactVersion(SymbolVersion ver, uint16_t versionId, 706e75b2fbSDimitry Andric StringRef versionName, bool includeNonDefault); 716e75b2fbSDimitry Andric void assignWildcardVersion(SymbolVersion ver, uint16_t versionId, 726e75b2fbSDimitry Andric bool includeNonDefault); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // The order the global symbols are in is not defined. We can use an arbitrary 750b57cec5SDimitry Andric // order, but it has to be reproducible. That is true even when cross linking. 760b57cec5SDimitry Andric // The default hashing of StringRef produces different results on 32 and 64 770b57cec5SDimitry Andric // bit systems so we use a map to a vector. That is arbitrary, deterministic 780b57cec5SDimitry Andric // but a bit inefficient. 790b57cec5SDimitry Andric // FIXME: Experiment with passing in a custom hashing or sorting the symbols 800b57cec5SDimitry Andric // once symbol resolution is finished. 810b57cec5SDimitry Andric llvm::DenseMap<llvm::CachedHashStringRef, int> symMap; 820eae32dcSDimitry Andric SmallVector<Symbol *, 0> symVector; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric // A map from demangled symbol names to their symbol objects. 850b57cec5SDimitry Andric // This mapping is 1:N because two symbols with different versions 860b57cec5SDimitry Andric // can have the same name. We use this map to handle "extern C++ {}" 870b57cec5SDimitry Andric // directive in version scripts. 88*bdd1243dSDimitry Andric std::optional<llvm::StringMap<SmallVector<Symbol *, 0>>> demangledSyms; 890b57cec5SDimitry Andric }; 900b57cec5SDimitry Andric 91*bdd1243dSDimitry Andric LLVM_LIBRARY_VISIBILITY extern SymbolTable symtab; 920b57cec5SDimitry Andric 93*bdd1243dSDimitry Andric } // namespace lld::elf 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric #endif 96