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/ADT/SmallPtrSet.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 namespace llvm { 21 struct LTOCodeGenerator; 22 } 23 24 namespace lld::coff { 25 26 class Chunk; 27 class CommonChunk; 28 class COFFLinkerContext; 29 class Defined; 30 class DefinedAbsolute; 31 class DefinedRegular; 32 class ImportThunkChunk; 33 class LazyArchive; 34 class SectionChunk; 35 class Symbol; 36 37 // SymbolTable is a bucket of all known symbols, including defined, 38 // undefined, or lazy symbols (the last one is symbols in archive 39 // files whose archive members are not yet loaded). 40 // 41 // We put all symbols of all files to a SymbolTable, and the 42 // SymbolTable selects the "best" symbols if there are name 43 // conflicts. For example, obviously, a defined symbol is better than 44 // an undefined symbol. Or, if there's a conflict between a lazy and a 45 // undefined, it'll read an archive member to read a real definition 46 // to replace the lazy symbol. The logic is implemented in the 47 // add*() functions, which are called by input files as they are parsed. 48 // There is one add* function per symbol type. 49 class SymbolTable { 50 public: 51 SymbolTable(COFFLinkerContext &c, 52 llvm::COFF::MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN) 53 : ctx(c), machine(machine) {} 54 55 // Emit errors for symbols that cannot be resolved. 56 void reportUnresolvable(); 57 58 // Try to resolve any undefined symbols and update the symbol table 59 // accordingly, then print an error message for any remaining undefined 60 // symbols and warn about imported local symbols. 61 // Returns whether more files might need to be linked in to resolve lazy 62 // symbols, in which case the caller is expected to call the function again 63 // after linking those files. 64 bool resolveRemainingUndefines(); 65 66 // Load lazy objects that are needed for MinGW automatic import and for 67 // doing stdcall fixups. 68 void loadMinGWSymbols(); 69 bool handleMinGWAutomaticImport(Symbol *sym, StringRef name); 70 71 // Returns a symbol for a given name. Returns a nullptr if not found. 72 Symbol *find(StringRef name) const; 73 Symbol *findUnderscore(StringRef name) const; 74 75 void addUndefinedGlob(StringRef arg); 76 77 // Occasionally we have to resolve an undefined symbol to its 78 // mangled symbol. This function tries to find a mangled name 79 // for U from the symbol table, and if found, set the symbol as 80 // a weak alias for U. 81 Symbol *findMangle(StringRef name); 82 StringRef mangleMaybe(Symbol *s); 83 84 // Symbol names are mangled by prepending "_" on x86. 85 StringRef mangle(StringRef sym); 86 87 // Windows specific -- "main" is not the only main function in Windows. 88 // You can choose one from these four -- {w,}{WinMain,main}. 89 // There are four different entry point functions for them, 90 // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to 91 // choose the right one depending on which "main" function is defined. 92 // This function looks up the symbol table and resolve corresponding 93 // entry point name. 94 StringRef findDefaultEntry(); 95 WindowsSubsystem inferSubsystem(); 96 97 // Build a set of COFF objects representing the combined contents of 98 // BitcodeFiles and add them to the symbol table. Called after all files are 99 // added and before the writer writes results to a file. 100 void compileBitcodeFiles(); 101 102 // Creates an Undefined symbol and marks it as live. 103 Symbol *addGCRoot(StringRef sym, bool aliasEC = false); 104 105 // Creates an Undefined symbol for a given name. 106 Symbol *addUndefined(StringRef name); 107 108 Symbol *addSynthetic(StringRef n, Chunk *c); 109 Symbol *addAbsolute(StringRef n, uint64_t va); 110 111 Symbol *addUndefined(StringRef name, InputFile *f, bool overrideLazy); 112 void addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym); 113 void addLazyObject(InputFile *f, StringRef n); 114 void addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym, StringRef n); 115 Symbol *addAbsolute(StringRef n, COFFSymbolRef s); 116 Symbol *addRegular(InputFile *f, StringRef n, 117 const llvm::object::coff_symbol_generic *s = nullptr, 118 SectionChunk *c = nullptr, uint32_t sectionOffset = 0, 119 bool isWeak = false); 120 std::pair<DefinedRegular *, bool> 121 addComdat(InputFile *f, StringRef n, 122 const llvm::object::coff_symbol_generic *s = nullptr); 123 Symbol *addCommon(InputFile *f, StringRef n, uint64_t size, 124 const llvm::object::coff_symbol_generic *s = nullptr, 125 CommonChunk *c = nullptr); 126 DefinedImportData *addImportData(StringRef n, ImportFile *f, 127 Chunk *&location); 128 Defined *addImportThunk(StringRef name, DefinedImportData *s, 129 ImportThunkChunk *chunk); 130 void addLibcall(StringRef name); 131 void addEntryThunk(Symbol *from, Symbol *to); 132 void addExitThunk(Symbol *from, Symbol *to); 133 void initializeECThunks(); 134 135 void reportDuplicate(Symbol *existing, InputFile *newFile, 136 SectionChunk *newSc = nullptr, 137 uint32_t newSectionOffset = 0); 138 139 COFFLinkerContext &ctx; 140 llvm::COFF::MachineTypes machine; 141 142 bool isEC() const { return machine == ARM64EC; } 143 144 // An entry point symbol. 145 Symbol *entry = nullptr; 146 147 // A list of chunks which to be added to .rdata. 148 std::vector<Chunk *> localImportChunks; 149 150 // A list of EC EXP+ symbols. 151 std::vector<Symbol *> expSymbols; 152 153 // A list of DLL exports. 154 std::vector<Export> exports; 155 llvm::DenseSet<StringRef> directivesExports; 156 bool hadExplicitExports; 157 158 Chunk *edataStart = nullptr; 159 Chunk *edataEnd = nullptr; 160 161 Symbol *delayLoadHelper = nullptr; 162 Chunk *tailMergeUnwindInfoChunk = nullptr; 163 164 void fixupExports(); 165 void assignExportOrdinals(); 166 void parseModuleDefs(StringRef path); 167 168 // Iterates symbols in non-determinstic hash table order. 169 template <typename T> void forEachSymbol(T callback) { 170 for (auto &pair : symMap) 171 callback(pair.second); 172 } 173 174 std::vector<BitcodeFile *> bitcodeFileInstances; 175 176 DefinedRegular *loadConfigSym = nullptr; 177 uint32_t loadConfigSize = 0; 178 void initializeLoadConfig(); 179 180 private: 181 /// Given a name without "__imp_" prefix, returns a defined symbol 182 /// with the "__imp_" prefix, if it exists. 183 Defined *impSymbol(StringRef name); 184 /// Inserts symbol if not already present. 185 std::pair<Symbol *, bool> insert(StringRef name); 186 /// Same as insert(Name), but also sets isUsedInRegularObj. 187 std::pair<Symbol *, bool> insert(StringRef name, InputFile *f); 188 189 bool findUnderscoreMangle(StringRef sym); 190 std::vector<Symbol *> getSymsWithPrefix(StringRef prefix); 191 192 llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap; 193 std::unique_ptr<BitcodeCompiler> lto; 194 std::vector<std::pair<Symbol *, Symbol *>> entryThunks; 195 llvm::DenseMap<Symbol *, Symbol *> exitThunks; 196 197 void 198 reportProblemSymbols(const llvm::SmallPtrSetImpl<Symbol *> &undefs, 199 const llvm::DenseMap<Symbol *, Symbol *> *localImports, 200 bool needBitcodeFiles); 201 }; 202 203 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex); 204 205 StringRef ltrim1(StringRef s, const char *chars); 206 207 } // namespace lld::coff 208 209 #endif 210