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