1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLD_ELF_SYMBOL_TABLE_H 11 #define LLD_ELF_SYMBOL_TABLE_H 12 13 #include "InputFiles.h" 14 #include "LTO.h" 15 #include "Strings.h" 16 #include "llvm/ADT/CachedHashString.h" 17 #include "llvm/ADT/DenseMap.h" 18 19 namespace lld { 20 namespace elf { 21 class Lazy; 22 class OutputSectionBase; 23 struct Symbol; 24 25 typedef llvm::CachedHashStringRef SymName; 26 27 // SymbolTable is a bucket of all known symbols, including defined, 28 // undefined, or lazy symbols (the last one is symbols in archive 29 // files whose archive members are not yet loaded). 30 // 31 // We put all symbols of all files to a SymbolTable, and the 32 // SymbolTable selects the "best" symbols if there are name 33 // conflicts. For example, obviously, a defined symbol is better than 34 // an undefined symbol. Or, if there's a conflict between a lazy and a 35 // undefined, it'll read an archive member to read a real definition 36 // to replace the lazy symbol. The logic is implemented in the 37 // add*() functions, which are called by input files as they are parsed. There 38 // is one add* function per symbol type. 39 template <class ELFT> class SymbolTable { 40 typedef typename ELFT::Sym Elf_Sym; 41 typedef typename ELFT::uint uintX_t; 42 43 public: 44 void addFile(InputFile *File); 45 void addCombinedLTOObject(); 46 47 ArrayRef<Symbol *> getSymbols() const { return SymVector; } 48 ArrayRef<ObjectFile<ELFT> *> getObjectFiles() const { return ObjectFiles; } 49 ArrayRef<BinaryFile *> getBinaryFiles() const { return BinaryFiles; } 50 ArrayRef<SharedFile<ELFT> *> getSharedFiles() const { return SharedFiles; } 51 52 DefinedRegular<ELFT> *addAbsolute(StringRef Name, 53 uint8_t Visibility = llvm::ELF::STV_HIDDEN, 54 uint8_t Binding = llvm::ELF::STB_GLOBAL); 55 DefinedRegular<ELFT> *addIgnored(StringRef Name, 56 uint8_t Visibility = llvm::ELF::STV_HIDDEN); 57 58 Symbol *addUndefined(StringRef Name); 59 Symbol *addUndefined(StringRef Name, bool IsLocal, uint8_t Binding, 60 uint8_t StOther, uint8_t Type, bool CanOmitFromDynSym, 61 InputFile *File); 62 63 Symbol *addRegular(StringRef Name, uint8_t StOther, uint8_t Type, 64 uintX_t Value, uintX_t Size, uint8_t Binding, 65 InputSectionBase<ELFT> *Section, InputFile *File); 66 67 Symbol *addSynthetic(StringRef N, const OutputSectionBase *Section, 68 uintX_t Value, uint8_t StOther); 69 70 void addShared(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym, 71 const typename ELFT::Verdef *Verdef); 72 73 void addLazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S); 74 void addLazyObject(StringRef Name, LazyObjectFile &Obj); 75 Symbol *addBitcode(StringRef Name, uint8_t Binding, uint8_t StOther, 76 uint8_t Type, bool CanOmitFromDynSym, BitcodeFile *File); 77 78 Symbol *addCommon(StringRef N, uint64_t Size, uint64_t Alignment, 79 uint8_t Binding, uint8_t StOther, uint8_t Type, 80 InputFile *File); 81 82 void scanUndefinedFlags(); 83 void scanShlibUndefined(); 84 void scanVersionScript(); 85 86 SymbolBody *find(StringRef Name); 87 88 void trace(StringRef Name); 89 void wrap(StringRef Name); 90 91 std::vector<InputSectionBase<ELFT> *> Sections; 92 93 private: 94 std::pair<Symbol *, bool> insert(StringRef Name); 95 std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type, 96 uint8_t Visibility, bool CanOmitFromDynSym, 97 InputFile *File); 98 99 std::vector<SymbolBody *> findByVersion(SymbolVersion Ver); 100 std::vector<SymbolBody *> findAllByVersion(SymbolVersion Ver); 101 102 void initDemangledSyms(); 103 void handleAnonymousVersion(); 104 void assignExactVersion(SymbolVersion Ver, uint16_t VersionId, 105 StringRef VersionName); 106 void assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId); 107 108 struct SymIndex { 109 SymIndex(int Idx, bool Traced) : Idx(Idx), Traced(Traced) {} 110 int Idx : 31; 111 unsigned Traced : 1; 112 }; 113 114 // The order the global symbols are in is not defined. We can use an arbitrary 115 // order, but it has to be reproducible. That is true even when cross linking. 116 // The default hashing of StringRef produces different results on 32 and 64 117 // bit systems so we use a map to a vector. That is arbitrary, deterministic 118 // but a bit inefficient. 119 // FIXME: Experiment with passing in a custom hashing or sorting the symbols 120 // once symbol resolution is finished. 121 llvm::DenseMap<SymName, SymIndex> Symtab; 122 std::vector<Symbol *> SymVector; 123 124 // Comdat groups define "link once" sections. If two comdat groups have the 125 // same name, only one of them is linked, and the other is ignored. This set 126 // is used to uniquify them. 127 llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups; 128 129 std::vector<ObjectFile<ELFT> *> ObjectFiles; 130 std::vector<SharedFile<ELFT> *> SharedFiles; 131 std::vector<BitcodeFile *> BitcodeFiles; 132 std::vector<BinaryFile *> BinaryFiles; 133 134 // Set of .so files to not link the same shared object file more than once. 135 llvm::DenseSet<StringRef> SoNames; 136 137 // A map from demangled symbol names to their symbol objects. 138 // This mapping is 1:N because two symbols with different versions 139 // can have the same name. We use this map to handle "extern C++ {}" 140 // directive in version scripts. 141 llvm::Optional<llvm::StringMap<std::vector<SymbolBody *>>> DemangledSyms; 142 143 // For LTO. 144 std::unique_ptr<BitcodeCompiler> LTO; 145 }; 146 147 template <class ELFT> struct Symtab { static SymbolTable<ELFT> *X; }; 148 template <class ELFT> SymbolTable<ELFT> *Symtab<ELFT>::X; 149 150 } // namespace elf 151 } // namespace lld 152 153 #endif 154