1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLD_COFF_SYMBOL_TABLE_H 10 #define LLD_COFF_SYMBOL_TABLE_H 11 12 #include "InputFiles.h" 13 #include "LTO.h" 14 #include "llvm/ADT/CachedHashString.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/DenseMapInfo.h" 17 #include "llvm/Support/raw_ostream.h" 18 19 namespace llvm { 20 struct LTOCodeGenerator; 21 } 22 23 namespace lld { 24 namespace coff { 25 26 class Chunk; 27 class CommonChunk; 28 class Defined; 29 class DefinedAbsolute; 30 class DefinedRegular; 31 class DefinedRelative; 32 class LazyArchive; 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 // Emit errors for symbols that cannot be resolved. 53 void reportUnresolvable(); 54 55 // Try to resolve any undefined symbols and update the symbol table 56 // accordingly, then print an error message for any remaining undefined 57 // symbols and warn about imported local symbols. 58 void resolveRemainingUndefines(); 59 60 void loadMinGWAutomaticImports(); 61 bool handleMinGWAutomaticImport(Symbol *sym, StringRef name); 62 63 // Returns a list of chunks of selected symbols. 64 std::vector<Chunk *> getChunks(); 65 66 // Returns a symbol for a given name. Returns a nullptr if not found. 67 Symbol *find(StringRef name); 68 Symbol *findUnderscore(StringRef name); 69 70 // Occasionally we have to resolve an undefined symbol to its 71 // mangled symbol. This function tries to find a mangled name 72 // for U from the symbol table, and if found, set the symbol as 73 // a weak alias for U. 74 Symbol *findMangle(StringRef name); 75 76 // Build a set of COFF objects representing the combined contents of 77 // BitcodeFiles and add them to the symbol table. Called after all files are 78 // added and before the writer writes results to a file. 79 void addCombinedLTOObjects(); 80 81 // Creates an Undefined symbol for a given name. 82 Symbol *addUndefined(StringRef name); 83 84 Symbol *addSynthetic(StringRef n, Chunk *c); 85 Symbol *addAbsolute(StringRef n, uint64_t va); 86 87 Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias); 88 void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym); 89 void addLazyObject(LazyObjFile *f, StringRef n); 90 Symbol *addAbsolute(StringRef n, COFFSymbolRef s); 91 Symbol *addRegular(InputFile *f, StringRef n, 92 const llvm::object::coff_symbol_generic *s = nullptr, 93 SectionChunk *c = nullptr, uint32_t sectionOffset = 0); 94 std::pair<DefinedRegular *, bool> 95 addComdat(InputFile *f, StringRef n, 96 const llvm::object::coff_symbol_generic *s = nullptr); 97 Symbol *addCommon(InputFile *f, StringRef n, uint64_t size, 98 const llvm::object::coff_symbol_generic *s = nullptr, 99 CommonChunk *c = nullptr); 100 Symbol *addImportData(StringRef n, ImportFile *f); 101 Symbol *addImportThunk(StringRef name, DefinedImportData *s, 102 uint16_t machine); 103 void addLibcall(StringRef name); 104 105 void reportDuplicate(Symbol *existing, InputFile *newFile, 106 SectionChunk *newSc = nullptr, 107 uint32_t newSectionOffset = 0); 108 109 // A list of chunks which to be added to .rdata. 110 std::vector<Chunk *> localImportChunks; 111 112 // Iterates symbols in non-determinstic hash table order. forEachSymbol(T callback)113 template <typename T> void forEachSymbol(T callback) { 114 for (auto &pair : symMap) 115 callback(pair.second); 116 } 117 118 private: 119 /// Given a name without "__imp_" prefix, returns a defined symbol 120 /// with the "__imp_" prefix, if it exists. 121 Defined *impSymbol(StringRef name); 122 /// Inserts symbol if not already present. 123 std::pair<Symbol *, bool> insert(StringRef name); 124 /// Same as insert(Name), but also sets isUsedInRegularObj. 125 std::pair<Symbol *, bool> insert(StringRef name, InputFile *f); 126 127 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix); 128 129 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap; 130 std::unique_ptr<BitcodeCompiler> lto; 131 }; 132 133 extern SymbolTable *symtab; 134 135 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex); 136 137 } // namespace coff 138 } // namespace lld 139 140 #endif 141