Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- SymbolTable.cpp ----------------------------------------------------===// |
| 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 | #include "SymbolTable.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 11 | #include "Config.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 12 | #include "InputChunks.h" |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 13 | #include "InputGlobal.h" |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 14 | #include "WriterUtils.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 15 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 +0000 | [diff] [blame] | 16 | #include "lld/Common/Memory.h" |
Rui Ueyama | 7d67dd1 | 2018-02-13 22:30:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SetVector.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 18 | |
| 19 | #define DEBUG_TYPE "lld" |
| 20 | |
| 21 | using namespace llvm; |
Sam Clegg | 20db381 | 2018-01-10 00:52:20 +0000 | [diff] [blame] | 22 | using namespace llvm::wasm; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 23 | using namespace lld; |
| 24 | using namespace lld::wasm; |
| 25 | |
| 26 | SymbolTable *lld::wasm::Symtab; |
| 27 | |
| 28 | void SymbolTable::addFile(InputFile *File) { |
| 29 | log("Processing: " + toString(File)); |
| 30 | File->parse(); |
| 31 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 32 | // LLVM bitcode file |
| 33 | if (auto *F = dyn_cast<BitcodeFile>(File)) |
| 34 | BitcodeFiles.push_back(F); |
| 35 | else if (auto *F = dyn_cast<ObjFile>(File)) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 36 | ObjectFiles.push_back(F); |
| 37 | } |
| 38 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 39 | // This function is where all the optimizations of link-time |
| 40 | // optimization happens. When LTO is in use, some input files are |
| 41 | // not in native object file format but in the LLVM bitcode format. |
| 42 | // This function compiles bitcode files into a few big native files |
| 43 | // using LLVM functions and replaces bitcode symbols with the results. |
| 44 | // Because all bitcode files that the program consists of are passed |
| 45 | // to the compiler at once, it can do whole-program optimization. |
| 46 | void SymbolTable::addCombinedLTOObject() { |
| 47 | if (BitcodeFiles.empty()) |
| 48 | return; |
| 49 | |
| 50 | // Compile bitcode files and replace bitcode symbols. |
| 51 | LTO.reset(new BitcodeCompiler); |
| 52 | for (BitcodeFile *F : BitcodeFiles) |
| 53 | LTO->add(*F); |
| 54 | |
| 55 | for (StringRef Filename : LTO->compile()) { |
| 56 | auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp")); |
| 57 | Obj->parse(); |
| 58 | ObjectFiles.push_back(Obj); |
| 59 | } |
| 60 | } |
| 61 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 62 | void SymbolTable::reportRemainingUndefines() { |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 63 | for (Symbol *Sym : SymVector) { |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 64 | if (!Sym->isUndefined() || Sym->isWeak()) |
| 65 | continue; |
| 66 | if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0) |
| 67 | continue; |
| 68 | if (!Sym->IsUsedInRegularObj) |
| 69 | continue; |
Sam Clegg | 47e2b6b | 2018-08-04 00:04:06 +0000 | [diff] [blame] | 70 | error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 71 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | Symbol *SymbolTable::find(StringRef Name) { |
Rui Ueyama | 67d6908 | 2018-02-28 23:03:06 +0000 | [diff] [blame] | 75 | return SymMap.lookup(CachedHashStringRef(Name)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 78 | std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) { |
| 79 | bool Inserted = false; |
Sam Clegg | a80d94d | 2017-11-27 23:16:06 +0000 | [diff] [blame] | 80 | Symbol *&Sym = SymMap[CachedHashStringRef(Name)]; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 81 | if (!Sym) { |
| 82 | Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); |
| 83 | Sym->IsUsedInRegularObj = false; |
| 84 | SymVector.emplace_back(Sym); |
| 85 | Inserted = true; |
| 86 | } |
| 87 | if (!File || File->kind() == InputFile::ObjectKind) |
| 88 | Sym->IsUsedInRegularObj = true; |
| 89 | return {Sym, Inserted}; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 92 | static void reportTypeError(const Symbol *Existing, const InputFile *File, |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 93 | llvm::wasm::WasmSymbolType Type) { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 94 | error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " + |
| 95 | toString(Existing->getWasmType()) + " in " + |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 96 | toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) + |
| 97 | " in " + toString(File)); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 100 | static void checkFunctionType(Symbol *Existing, const InputFile *File, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 101 | const WasmSignature *NewSig) { |
Sam Clegg | 65d6380 | 2018-05-14 23:01:16 +0000 | [diff] [blame] | 102 | auto ExistingFunction = dyn_cast<FunctionSymbol>(Existing); |
| 103 | if (!ExistingFunction) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 104 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_FUNCTION); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 108 | if (!NewSig) |
| 109 | return; |
| 110 | |
| 111 | const WasmSignature *OldSig = ExistingFunction->FunctionType; |
| 112 | if (!OldSig) { |
| 113 | ExistingFunction->FunctionType = NewSig; |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | if (*NewSig != *OldSig) |
Sam Clegg | ffd0aaf | 2018-06-22 15:13:10 +0000 | [diff] [blame] | 118 | warn("function signature mismatch: " + Existing->getName() + |
| 119 | "\n>>> defined as " + toString(*OldSig) + " in " + |
| 120 | toString(Existing->getFile()) + "\n>>> defined as " + |
| 121 | toString(*NewSig) + " in " + toString(File)); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 124 | // Check the type of new symbol matches that of the symbol is replacing. |
| 125 | // For functions this can also involve verifying that the signatures match. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 126 | static void checkGlobalType(const Symbol *Existing, const InputFile *File, |
| 127 | const WasmGlobalType *NewType) { |
| 128 | if (!isa<GlobalSymbol>(Existing)) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 129 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 133 | const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType(); |
| 134 | if (*NewType != *OldType) { |
| 135 | error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " + |
| 136 | toString(*OldType) + " in " + toString(Existing->getFile()) + |
| 137 | "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 138 | } |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 141 | static void checkDataType(const Symbol *Existing, const InputFile *File) { |
| 142 | if (!isa<DataSymbol>(Existing)) |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 143 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 146 | DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name, |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 147 | uint32_t Flags, |
| 148 | InputFunction *Function) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 149 | LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 150 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 151 | SyntheticFunctions.emplace_back(Function); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 152 | return replaceSymbol<DefinedFunction>(insert(Name, nullptr).first, Name, |
| 153 | Flags, nullptr, Function); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 156 | DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name, |
| 157 | uint32_t Flags) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 158 | LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 159 | assert(!find(Name)); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 160 | return replaceSymbol<DefinedData>(insert(Name, nullptr).first, Name, Flags); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 163 | DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags, |
| 164 | InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 165 | LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global |
| 166 | << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 167 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 168 | SyntheticGlobals.emplace_back(Global); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 169 | return replaceSymbol<DefinedGlobal>(insert(Name, nullptr).first, Name, Flags, |
| 170 | nullptr, Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 173 | static bool shouldReplace(const Symbol *Existing, InputFile *NewFile, |
| 174 | uint32_t NewFlags) { |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 175 | // If existing symbol is undefined, replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 176 | if (!Existing->isDefined()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 177 | LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " |
| 178 | << Existing->getName() << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
| 182 | // Now we have two defined symbols. If the new one is weak, we can ignore it. |
| 183 | if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 184 | LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
| 188 | // If the existing symbol is weak, we should replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 189 | if (Existing->isWeak()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 190 | LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
| 194 | // Neither symbol is week. They conflict. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 195 | error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " + |
| 196 | toString(Existing->getFile()) + "\n>>> defined in " + |
| 197 | toString(NewFile)); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 198 | return true; |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 202 | InputFile *File, |
| 203 | InputFunction *Function) { |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 204 | LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " [" |
| 205 | << (Function ? toString(Function->Signature) : "none") |
| 206 | << "]\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 207 | Symbol *S; |
| 208 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 209 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 210 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 211 | if (WasInserted || S->isLazy()) { |
| 212 | replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function); |
| 213 | return S; |
| 214 | } |
| 215 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 216 | if (Function) |
| 217 | checkFunctionType(S, File, &Function->Signature); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 218 | |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 219 | if (shouldReplace(S, File, Flags)) { |
| 220 | // If the new defined function doesn't have signture (i.e. bitcode |
| 221 | // functions) but the old symbols does then preserve the old signature |
| 222 | const WasmSignature *OldSig = nullptr; |
| 223 | if (auto* F = dyn_cast<FunctionSymbol>(S)) |
| 224 | OldSig = F->FunctionType; |
| 225 | auto NewSym = replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function); |
| 226 | if (!NewSym->FunctionType) |
| 227 | NewSym->FunctionType = OldSig; |
| 228 | } |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 229 | return S; |
| 230 | } |
| 231 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 232 | Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 233 | InputFile *File, InputSegment *Segment, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 234 | uint32_t Address, uint32_t Size) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 235 | LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address |
| 236 | << "\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 237 | Symbol *S; |
| 238 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 239 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 240 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 241 | if (WasInserted || S->isLazy()) { |
| 242 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
| 243 | return S; |
| 244 | } |
| 245 | |
| 246 | checkDataType(S, File); |
| 247 | |
| 248 | if (shouldReplace(S, File, Flags)) |
| 249 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 250 | return S; |
| 251 | } |
| 252 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 253 | Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 254 | InputFile *File, InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 255 | LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n"); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 256 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 257 | Symbol *S; |
| 258 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 259 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 260 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 261 | if (WasInserted || S->isLazy()) { |
| 262 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
| 263 | return S; |
| 264 | } |
| 265 | |
| 266 | checkGlobalType(S, File, &Global->getType()); |
| 267 | |
| 268 | if (shouldReplace(S, File, Flags)) |
| 269 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 270 | return S; |
| 271 | } |
| 272 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 273 | Symbol *SymbolTable::addUndefinedFunction(StringRef Name, uint32_t Flags, |
| 274 | InputFile *File, |
| 275 | const WasmSignature *Sig) { |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 276 | LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << |
| 277 | " [" << (Sig ? toString(*Sig) : "none") << "]\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 278 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 279 | Symbol *S; |
| 280 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 281 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 282 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 283 | if (WasInserted) |
| 284 | replaceSymbol<UndefinedFunction>(S, Name, Flags, File, Sig); |
| 285 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 286 | Lazy->fetch(); |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 287 | else |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 288 | checkFunctionType(S, File, Sig); |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 289 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 290 | return S; |
| 291 | } |
| 292 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 293 | Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags, |
| 294 | InputFile *File) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 295 | LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 296 | |
| 297 | Symbol *S; |
| 298 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 299 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | f989a92 | 2018-07-17 19:15:02 +0000 | [diff] [blame] | 300 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 301 | if (WasInserted) |
| 302 | replaceSymbol<UndefinedData>(S, Name, Flags, File); |
| 303 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 304 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 305 | else if (S->isDefined()) |
| 306 | checkDataType(S, File); |
| 307 | return S; |
| 308 | } |
| 309 | |
| 310 | Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, uint32_t Flags, |
| 311 | InputFile *File, |
| 312 | const WasmGlobalType *Type) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 313 | LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 314 | |
| 315 | Symbol *S; |
| 316 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 317 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 318 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 319 | if (WasInserted) |
| 320 | replaceSymbol<UndefinedGlobal>(S, Name, Flags, File, Type); |
| 321 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 322 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 323 | else if (S->isDefined()) |
| 324 | checkGlobalType(S, File, Type); |
| 325 | return S; |
| 326 | } |
| 327 | |
| 328 | void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 329 | LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 330 | StringRef Name = Sym->getName(); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 331 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 332 | Symbol *S; |
| 333 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 334 | std::tie(S, WasInserted) = insert(Name, nullptr); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 335 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 336 | if (WasInserted) { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 337 | replaceSymbol<LazySymbol>(S, Name, File, *Sym); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | |
| 341 | // If there is an existing undefined symbol, load a new one from the archive. |
| 342 | if (S->isUndefined()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 343 | LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 344 | File->addMember(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 347 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 348 | bool SymbolTable::addComdat(StringRef Name) { |
| 349 | return Comdats.insert(CachedHashStringRef(Name)).second; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 350 | } |