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_WASM_SYMBOL_TABLE_H 10 #define LLD_WASM_SYMBOL_TABLE_H 11 12 #include "InputFiles.h" 13 #include "LTO.h" 14 #include "Symbols.h" 15 #include "lld/Common/LLVM.h" 16 #include "llvm/ADT/CachedHashString.h" 17 #include "llvm/ADT/DenseSet.h" 18 #include "llvm/ADT/Optional.h" 19 #include "llvm/BinaryFormat/WasmTraits.h" 20 21 namespace lld { 22 namespace wasm { 23 24 class InputSegment; 25 26 // SymbolTable is a bucket of all known symbols, including defined, 27 // undefined, or lazy symbols (the last one is symbols in archive 28 // files whose archive members are not yet loaded). 29 // 30 // We put all symbols of all files to a SymbolTable, and the 31 // SymbolTable selects the "best" symbols if there are name 32 // conflicts. For example, obviously, a defined symbol is better than 33 // an undefined symbol. Or, if there's a conflict between a lazy and a 34 // undefined, it'll read an archive member to read a real definition 35 // to replace the lazy symbol. The logic is implemented in the 36 // add*() functions, which are called by input files as they are parsed. 37 // There is one add* function per symbol type. 38 class SymbolTable { 39 public: 40 void wrap(Symbol *sym, Symbol *real, Symbol *wrap); 41 42 void addFile(InputFile *file); 43 44 void compileBitcodeFiles(); 45 getSymbols()46 ArrayRef<Symbol *> getSymbols() const { return symVector; } 47 48 Symbol *find(StringRef name); 49 50 void replace(StringRef name, Symbol* sym); 51 52 void trace(StringRef name); 53 54 Symbol *addDefinedFunction(StringRef name, uint32_t flags, InputFile *file, 55 InputFunction *function); 56 Symbol *addDefinedData(StringRef name, uint32_t flags, InputFile *file, 57 InputChunk *segment, uint64_t address, uint64_t size); 58 Symbol *addDefinedGlobal(StringRef name, uint32_t flags, InputFile *file, 59 InputGlobal *g); 60 Symbol *addDefinedTag(StringRef name, uint32_t flags, InputFile *file, 61 InputTag *t); 62 Symbol *addDefinedTable(StringRef name, uint32_t flags, InputFile *file, 63 InputTable *t); 64 65 Symbol *addUndefinedFunction(StringRef name, 66 llvm::Optional<StringRef> importName, 67 llvm::Optional<StringRef> importModule, 68 uint32_t flags, InputFile *file, 69 const WasmSignature *signature, 70 bool isCalledDirectly); 71 Symbol *addUndefinedData(StringRef name, uint32_t flags, InputFile *file); 72 Symbol *addUndefinedGlobal(StringRef name, 73 llvm::Optional<StringRef> importName, 74 llvm::Optional<StringRef> importModule, 75 uint32_t flags, InputFile *file, 76 const WasmGlobalType *type); 77 Symbol *addUndefinedTable(StringRef name, 78 llvm::Optional<StringRef> importName, 79 llvm::Optional<StringRef> importModule, 80 uint32_t flags, InputFile *file, 81 const WasmTableType *type); 82 Symbol *addUndefinedTag(StringRef name, llvm::Optional<StringRef> importName, 83 llvm::Optional<StringRef> importModule, 84 uint32_t flags, InputFile *file, 85 const WasmSignature *sig); 86 87 TableSymbol *resolveIndirectFunctionTable(bool required); 88 89 void addLazy(ArchiveFile *f, const llvm::object::Archive::Symbol *sym); 90 91 bool addComdat(StringRef name); 92 93 DefinedData *addSyntheticDataSymbol(StringRef name, uint32_t flags); 94 DefinedGlobal *addSyntheticGlobal(StringRef name, uint32_t flags, 95 InputGlobal *global); 96 DefinedFunction *addSyntheticFunction(StringRef name, uint32_t flags, 97 InputFunction *function); 98 DefinedData *addOptionalDataSymbol(StringRef name, uint64_t value = 0); 99 DefinedGlobal *addOptionalGlobalSymbol(StringRef name, InputGlobal *global); 100 DefinedTable *addSyntheticTable(StringRef name, uint32_t flags, 101 InputTable *global); 102 103 void handleSymbolVariants(); 104 void handleWeakUndefines(); 105 DefinedFunction *createUndefinedStub(const WasmSignature &sig); 106 107 std::vector<ObjFile *> objectFiles; 108 std::vector<SharedFile *> sharedFiles; 109 std::vector<BitcodeFile *> bitcodeFiles; 110 std::vector<InputFunction *> syntheticFunctions; 111 std::vector<InputGlobal *> syntheticGlobals; 112 std::vector<InputTable *> syntheticTables; 113 114 private: 115 std::pair<Symbol *, bool> insert(StringRef name, const InputFile *file); 116 std::pair<Symbol *, bool> insertName(StringRef name); 117 118 bool getFunctionVariant(Symbol* sym, const WasmSignature *sig, 119 const InputFile *file, Symbol **out); 120 InputFunction *replaceWithUnreachable(Symbol *sym, const WasmSignature &sig, 121 StringRef debugName); 122 void replaceWithUndefined(Symbol *sym); 123 124 TableSymbol *createDefinedIndirectFunctionTable(StringRef name); 125 TableSymbol *createUndefinedIndirectFunctionTable(StringRef name); 126 127 // Maps symbol names to index into the symVector. -1 means that symbols 128 // is to not yet in the vector but it should have tracing enabled if it is 129 // ever added. 130 llvm::DenseMap<llvm::CachedHashStringRef, int> symMap; 131 std::vector<Symbol *> symVector; 132 133 // For certain symbols types, e.g. function symbols, we allow for multiple 134 // variants of the same symbol with different signatures. 135 llvm::DenseMap<llvm::CachedHashStringRef, std::vector<Symbol *>> symVariants; 136 llvm::DenseMap<WasmSignature, DefinedFunction *> stubFunctions; 137 138 // Comdat groups define "link once" sections. If two comdat groups have the 139 // same name, only one of them is linked, and the other is ignored. This set 140 // is used to uniquify them. 141 llvm::DenseSet<llvm::CachedHashStringRef> comdatGroups; 142 143 // For LTO. 144 std::unique_ptr<BitcodeCompiler> lto; 145 }; 146 147 extern SymbolTable *symtab; 148 149 } // namespace wasm 150 } // namespace lld 151 152 #endif 153