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 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 Symbol *findMangle(StringRef name); 72 73 // Build a set of COFF objects representing the combined contents of 74 // BitcodeFiles and add them to the symbol table. Called after all files are 75 // added and before the writer writes results to a file. 76 void addCombinedLTOObjects(); 77 std::vector<StringRef> compileBitcodeFiles(); 78 79 // Creates an Undefined symbol for a given name. 80 Symbol *addUndefined(StringRef name); 81 82 Symbol *addSynthetic(StringRef n, Chunk *c); 83 Symbol *addAbsolute(StringRef n, uint64_t va); 84 85 Symbol *addUndefined(StringRef name, InputFile *f, bool isWeakAlias); 86 void addLazy(ArchiveFile *f, const Archive::Symbol &sym); 87 Symbol *addAbsolute(StringRef n, COFFSymbolRef s); 88 Symbol *addRegular(InputFile *f, StringRef n, 89 const llvm::object::coff_symbol_generic *s = nullptr, 90 SectionChunk *c = nullptr); 91 std::pair<DefinedRegular *, bool> 92 addComdat(InputFile *f, StringRef n, 93 const llvm::object::coff_symbol_generic *s = nullptr); 94 Symbol *addCommon(InputFile *f, StringRef n, uint64_t size, 95 const llvm::object::coff_symbol_generic *s = nullptr, 96 CommonChunk *c = nullptr); 97 Symbol *addImportData(StringRef n, ImportFile *f); 98 Symbol *addImportThunk(StringRef name, DefinedImportData *s, 99 uint16_t machine); 100 void addLibcall(StringRef name); 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. 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 119 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix); 120 121 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap; 122 std::unique_ptr<BitcodeCompiler> lto; 123 }; 124 125 extern SymbolTable *symtab; 126 127 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex); 128 129 } // namespace coff 130 } // namespace lld 131 132 #endif 133