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