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_COFF_SYMBOL_TABLE_H 11 #define LLD_COFF_SYMBOL_TABLE_H 12 13 #include "InputFiles.h" 14 #include "LTO.h" 15 #include "llvm/ADT/CachedHashString.h" 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/ADT/DenseMapInfo.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 namespace llvm { 21 struct LTOCodeGenerator; 22 } 23 24 namespace lld { 25 namespace coff { 26 27 class Chunk; 28 class CommonChunk; 29 class Defined; 30 class DefinedAbsolute; 31 class DefinedRelative; 32 class Lazy; 33 class SectionChunk; 34 class Symbol; 35 36 // SymbolTable is a bucket of all known symbols, including defined, 37 // undefined, or lazy symbols (the last one is symbols in archive 38 // files whose archive members are not yet loaded). 39 // 40 // We put all symbols of all files to a SymbolTable, and the 41 // SymbolTable selects the "best" symbols if there are name 42 // conflicts. For example, obviously, a defined symbol is better than 43 // an undefined symbol. Or, if there's a conflict between a lazy and a 44 // undefined, it'll read an archive member to read a real definition 45 // to replace the lazy symbol. The logic is implemented in the 46 // add*() functions, which are called by input files as they are parsed. 47 // There is one add* function per symbol type. 48 class SymbolTable { 49 public: 50 void addFile(InputFile *File); 51 52 // Try to resolve any undefined symbols and update the symbol table 53 // accordingly, then print an error message for any remaining undefined 54 // symbols. 55 void reportRemainingUndefines(); 56 57 void loadMinGWAutomaticImports(); 58 bool handleMinGWAutomaticImport(Symbol *Sym, StringRef Name); 59 60 // Returns a list of chunks of selected symbols. 61 std::vector<Chunk *> getChunks(); 62 63 // Returns a symbol for a given name. Returns a nullptr if not found. 64 Symbol *find(StringRef Name); 65 Symbol *findUnderscore(StringRef Name); 66 67 // Occasionally we have to resolve an undefined symbol to its 68 // mangled symbol. This function tries to find a mangled name 69 // for U from the symbol table, and if found, set the symbol as 70 // a weak alias for U. 71 void mangleMaybe(Symbol *B); 72 StringRef findMangle(StringRef Name); 73 74 // Build a set of COFF objects representing the combined contents of 75 // BitcodeFiles and add them to the symbol table. Called after all files are 76 // added and before the writer writes results to a file. 77 void addCombinedLTOObjects(); 78 std::vector<StringRef> compileBitcodeFiles(); 79 80 // Creates an Undefined symbol for a given name. 81 Symbol *addUndefined(StringRef Name); 82 83 Symbol *addSynthetic(StringRef N, Chunk *C); 84 Symbol *addAbsolute(StringRef N, uint64_t VA); 85 86 Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias); 87 void addLazy(ArchiveFile *F, const Archive::Symbol Sym); 88 Symbol *addAbsolute(StringRef N, COFFSymbolRef S); 89 Symbol *addRegular(InputFile *F, StringRef N, 90 const llvm::object::coff_symbol_generic *S = nullptr, 91 SectionChunk *C = nullptr); 92 std::pair<Symbol *, bool> 93 addComdat(InputFile *F, StringRef N, 94 const llvm::object::coff_symbol_generic *S = nullptr); 95 Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size, 96 const llvm::object::coff_symbol_generic *S = nullptr, 97 CommonChunk *C = nullptr); 98 Symbol *addImportData(StringRef N, ImportFile *F); 99 Symbol *addImportThunk(StringRef Name, DefinedImportData *S, 100 uint16_t Machine); 101 102 void reportDuplicate(Symbol *Existing, InputFile *NewFile); 103 104 // A list of chunks which to be added to .rdata. 105 std::vector<Chunk *> LocalImportChunks; 106 107 // Iterates symbols in non-determinstic hash table order. forEachSymbol(T Callback)108 template <typename T> void forEachSymbol(T Callback) { 109 for (auto &Pair : SymMap) 110 Callback(Pair.second); 111 } 112 113 private: 114 /// Inserts symbol if not already present. 115 std::pair<Symbol *, bool> insert(StringRef Name); 116 /// Same as insert(Name), but also sets IsUsedInRegularObj. 117 std::pair<Symbol *, bool> insert(StringRef Name, InputFile *F); 118 StringRef findByPrefix(StringRef Prefix); 119 120 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap; 121 std::unique_ptr<BitcodeCompiler> LTO; 122 }; 123 124 extern SymbolTable *Symtab; 125 126 std::string getSymbolLocations(ObjFile *File, uint32_t SymIndex); 127 128 } // namespace coff 129 } // namespace lld 130 131 #endif 132