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); |
Sam Clegg | a688a42 | 2019-03-13 21:29:20 +0000 | [diff] [blame] | 40 | else if (auto *F = dyn_cast<SharedFile>(File)) |
| 41 | SharedFiles.push_back(F); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 44 | // This function is where all the optimizations of link-time |
| 45 | // optimization happens. When LTO is in use, some input files are |
| 46 | // not in native object file format but in the LLVM bitcode format. |
| 47 | // This function compiles bitcode files into a few big native files |
| 48 | // using LLVM functions and replaces bitcode symbols with the results. |
| 49 | // Because all bitcode files that the program consists of are passed |
| 50 | // to the compiler at once, it can do whole-program optimization. |
| 51 | void SymbolTable::addCombinedLTOObject() { |
| 52 | if (BitcodeFiles.empty()) |
| 53 | return; |
| 54 | |
| 55 | // Compile bitcode files and replace bitcode symbols. |
| 56 | LTO.reset(new BitcodeCompiler); |
| 57 | for (BitcodeFile *F : BitcodeFiles) |
| 58 | LTO->add(*F); |
| 59 | |
| 60 | for (StringRef Filename : LTO->compile()) { |
Sam Clegg | 0cfaa24 | 2019-04-09 05:41:52 +0000 | [diff] [blame] | 61 | auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp"), ""); |
Sam Clegg | 697f214 | 2019-05-15 16:03:28 +0000 | [diff] [blame] | 62 | Obj->parse(true); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 63 | ObjectFiles.push_back(Obj); |
| 64 | } |
| 65 | } |
| 66 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 67 | void SymbolTable::reportRemainingUndefines() { |
Sam Clegg | a5ca34e | 2019-05-24 14:14:25 +0000 | [diff] [blame^] | 68 | for (const auto& Pair : SymMap) { |
| 69 | const Symbol *Sym = SymVector[Pair.second]; |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 70 | if (!Sym->isUndefined() || Sym->isWeak()) |
| 71 | continue; |
| 72 | if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0) |
| 73 | continue; |
| 74 | if (!Sym->IsUsedInRegularObj) |
| 75 | continue; |
Sam Clegg | 47e2b6b | 2018-08-04 00:04:06 +0000 | [diff] [blame] | 76 | error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 77 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | Symbol *SymbolTable::find(StringRef Name) { |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 81 | auto It = SymMap.find(CachedHashStringRef(Name)); |
| 82 | if (It == SymMap.end() || It->second == -1) |
| 83 | return nullptr; |
| 84 | return SymVector[It->second]; |
| 85 | } |
| 86 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 87 | void SymbolTable::replace(StringRef Name, Symbol* Sym) { |
| 88 | auto It = SymMap.find(CachedHashStringRef(Name)); |
| 89 | SymVector[It->second] = Sym; |
| 90 | } |
| 91 | |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 92 | std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) { |
| 93 | bool Trace = false; |
| 94 | auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()}); |
| 95 | int &SymIndex = P.first->second; |
| 96 | bool IsNew = P.second; |
| 97 | if (SymIndex == -1) { |
| 98 | SymIndex = SymVector.size(); |
| 99 | Trace = true; |
| 100 | IsNew = true; |
| 101 | } |
| 102 | |
| 103 | if (!IsNew) |
| 104 | return {SymVector[SymIndex], false}; |
| 105 | |
| 106 | Symbol *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); |
| 107 | Sym->IsUsedInRegularObj = false; |
Sam Clegg | a5ca34e | 2019-05-24 14:14:25 +0000 | [diff] [blame^] | 108 | Sym->CanInline = true; |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 109 | Sym->Traced = Trace; |
| 110 | SymVector.emplace_back(Sym); |
| 111 | return {Sym, true}; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 114 | std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, |
| 115 | const InputFile *File) { |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 116 | Symbol *S; |
| 117 | bool WasInserted; |
| 118 | std::tie(S, WasInserted) = insertName(Name); |
| 119 | |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 120 | if (!File || File->kind() == InputFile::ObjectKind) |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 121 | S->IsUsedInRegularObj = true; |
| 122 | |
| 123 | return {S, WasInserted}; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 126 | static void reportTypeError(const Symbol *Existing, const InputFile *File, |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 127 | llvm::wasm::WasmSymbolType Type) { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 128 | error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " + |
| 129 | toString(Existing->getWasmType()) + " in " + |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 130 | toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) + |
| 131 | " in " + toString(File)); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Heejin Ahn | 6f4286f | 2018-11-19 23:31:28 +0000 | [diff] [blame] | 134 | // Check the type of new symbol matches that of the symbol is replacing. |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 135 | // Returns true if the function types match, false is there is a singature |
| 136 | // mismatch. |
Benjamin Kramer | ba2ea93 | 2019-03-28 17:18:42 +0000 | [diff] [blame] | 137 | static bool signatureMatches(FunctionSymbol *Existing, |
| 138 | const WasmSignature *NewSig) { |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 139 | if (!NewSig) |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 140 | return true; |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 141 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 142 | const WasmSignature *OldSig = Existing->Signature; |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 143 | if (!OldSig) { |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 144 | Existing->Signature = NewSig; |
| 145 | return true; |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 148 | return *NewSig == *OldSig; |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 151 | static void checkGlobalType(const Symbol *Existing, const InputFile *File, |
| 152 | const WasmGlobalType *NewType) { |
| 153 | if (!isa<GlobalSymbol>(Existing)) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 154 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 158 | const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType(); |
| 159 | if (*NewType != *OldType) { |
| 160 | error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " + |
| 161 | toString(*OldType) + " in " + toString(Existing->getFile()) + |
| 162 | "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 163 | } |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 166 | static void checkEventType(const Symbol *Existing, const InputFile *File, |
| 167 | const WasmEventType *NewType, |
| 168 | const WasmSignature *NewSig) { |
| 169 | auto ExistingEvent = dyn_cast<EventSymbol>(Existing); |
| 170 | if (!isa<EventSymbol>(Existing)) { |
| 171 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_EVENT); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | const WasmEventType *OldType = cast<EventSymbol>(Existing)->getEventType(); |
| 176 | const WasmSignature *OldSig = ExistingEvent->Signature; |
| 177 | if (NewType->Attribute != OldType->Attribute) |
| 178 | error("Event type mismatch: " + Existing->getName() + "\n>>> defined as " + |
| 179 | toString(*OldType) + " in " + toString(Existing->getFile()) + |
| 180 | "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); |
| 181 | if (*NewSig != *OldSig) |
| 182 | warn("Event signature mismatch: " + Existing->getName() + |
| 183 | "\n>>> defined as " + toString(*OldSig) + " in " + |
| 184 | toString(Existing->getFile()) + "\n>>> defined as " + |
| 185 | toString(*NewSig) + " in " + toString(File)); |
| 186 | } |
| 187 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 188 | static void checkDataType(const Symbol *Existing, const InputFile *File) { |
| 189 | if (!isa<DataSymbol>(Existing)) |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 190 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 193 | DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name, |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 194 | uint32_t Flags, |
| 195 | InputFunction *Function) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 196 | LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 197 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 198 | SyntheticFunctions.emplace_back(Function); |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 199 | return replaceSymbol<DefinedFunction>(insertName(Name).first, Name, |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 200 | Flags, nullptr, Function); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 203 | DefinedData *SymbolTable::addOptionalDataSymbol(StringRef Name, uint32_t Value, |
| 204 | uint32_t Flags) { |
| 205 | Symbol *S = find(Name); |
| 206 | if (!S || S->isDefined()) |
| 207 | return nullptr; |
| 208 | LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << Name << "\n"); |
| 209 | auto *rtn = replaceSymbol<DefinedData>(S, Name, Flags); |
| 210 | rtn->setVirtualAddress(Value); |
| 211 | return rtn; |
| 212 | } |
| 213 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 214 | DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name, |
| 215 | uint32_t Flags) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 216 | LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 217 | assert(!find(Name)); |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 218 | return replaceSymbol<DefinedData>(insertName(Name).first, Name, Flags); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 221 | DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags, |
| 222 | InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 223 | LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global |
| 224 | << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 225 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 226 | SyntheticGlobals.emplace_back(Global); |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 227 | return replaceSymbol<DefinedGlobal>(insertName(Name).first, Name, Flags, |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 228 | nullptr, Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 231 | static bool shouldReplace(const Symbol *Existing, InputFile *NewFile, |
| 232 | uint32_t NewFlags) { |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 233 | // If existing symbol is undefined, replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 234 | if (!Existing->isDefined()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 235 | LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " |
| 236 | << Existing->getName() << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | |
| 240 | // Now we have two defined symbols. If the new one is weak, we can ignore it. |
| 241 | if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 242 | LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | |
| 246 | // If the existing symbol is weak, we should replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 247 | if (Existing->isWeak()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 248 | LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
| 252 | // Neither symbol is week. They conflict. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 253 | error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " + |
| 254 | toString(Existing->getFile()) + "\n>>> defined in " + |
| 255 | toString(NewFile)); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 256 | return true; |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 260 | InputFile *File, |
| 261 | InputFunction *Function) { |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 262 | LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " [" |
| 263 | << (Function ? toString(Function->Signature) : "none") |
| 264 | << "]\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 265 | Symbol *S; |
| 266 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 267 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 268 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 269 | auto Replace = [&](Symbol* Sym) { |
| 270 | // If the new defined function doesn't have signture (i.e. bitcode |
| 271 | // functions) but the old symbol does, then preserve the old signature |
| 272 | const WasmSignature *OldSig = S->getSignature(); |
| 273 | auto* NewSym = replaceSymbol<DefinedFunction>(Sym, Name, Flags, File, Function); |
| 274 | if (!NewSym->Signature) |
| 275 | NewSym->Signature = OldSig; |
| 276 | }; |
| 277 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 278 | if (WasInserted || S->isLazy()) { |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 279 | Replace(S); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 280 | return S; |
| 281 | } |
| 282 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 283 | auto ExistingFunction = dyn_cast<FunctionSymbol>(S); |
| 284 | if (!ExistingFunction) { |
| 285 | reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION); |
| 286 | return S; |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 287 | } |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 288 | |
| 289 | if (Function && !signatureMatches(ExistingFunction, &Function->Signature)) { |
| 290 | Symbol* Variant; |
| 291 | if (getFunctionVariant(S, &Function->Signature, File, &Variant)) |
| 292 | // New variant, always replace |
| 293 | Replace(Variant); |
| 294 | else if (shouldReplace(S, File, Flags)) |
| 295 | // Variant already exists, replace it after checking shouldReplace |
| 296 | Replace(Variant); |
| 297 | |
| 298 | // This variant we found take the place in the symbol table as the primary |
| 299 | // variant. |
| 300 | replace(Name, Variant); |
| 301 | return Variant; |
| 302 | } |
| 303 | |
| 304 | // Existing function with matching signature. |
| 305 | if (shouldReplace(S, File, Flags)) |
| 306 | Replace(S); |
| 307 | |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 308 | return S; |
| 309 | } |
| 310 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 311 | Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 312 | InputFile *File, InputSegment *Segment, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 313 | uint32_t Address, uint32_t Size) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 314 | LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address |
| 315 | << "\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 316 | Symbol *S; |
| 317 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 318 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 319 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 320 | auto Replace = [&]() { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 321 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | if (WasInserted || S->isLazy()) { |
| 325 | Replace(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 326 | return S; |
| 327 | } |
| 328 | |
| 329 | checkDataType(S, File); |
| 330 | |
| 331 | if (shouldReplace(S, File, Flags)) |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 332 | Replace(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 333 | return S; |
| 334 | } |
| 335 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 336 | Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 337 | InputFile *File, InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 338 | LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n"); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 339 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 340 | Symbol *S; |
| 341 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 342 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 343 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 344 | auto Replace = [&]() { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 345 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | if (WasInserted || S->isLazy()) { |
| 349 | Replace(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 350 | return S; |
| 351 | } |
| 352 | |
| 353 | checkGlobalType(S, File, &Global->getType()); |
| 354 | |
| 355 | if (shouldReplace(S, File, Flags)) |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 356 | Replace(); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 357 | return S; |
| 358 | } |
| 359 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 360 | Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags, |
| 361 | InputFile *File, InputEvent *Event) { |
| 362 | LLVM_DEBUG(dbgs() << "addDefinedEvent:" << Name << "\n"); |
| 363 | |
| 364 | Symbol *S; |
| 365 | bool WasInserted; |
| 366 | std::tie(S, WasInserted) = insert(Name, File); |
| 367 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 368 | auto Replace = [&]() { |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 369 | replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | if (WasInserted || S->isLazy()) { |
| 373 | Replace(); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 374 | return S; |
| 375 | } |
| 376 | |
| 377 | checkEventType(S, File, &Event->getType(), &Event->Signature); |
| 378 | |
| 379 | if (shouldReplace(S, File, Flags)) |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 380 | Replace(); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 381 | return S; |
| 382 | } |
| 383 | |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 384 | Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName, |
| 385 | StringRef ImportModule, |
Sam Clegg | 7cc0753 | 2019-02-01 02:29:57 +0000 | [diff] [blame] | 386 | uint32_t Flags, InputFile *File, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 387 | const WasmSignature *Sig) { |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 388 | LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << |
| 389 | " [" << (Sig ? toString(*Sig) : "none") << "]\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 390 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 391 | Symbol *S; |
| 392 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 393 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 +0000 | [diff] [blame] | 394 | if (S->Traced) |
| 395 | printTraceSymbolUndefined(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 396 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 397 | auto Replace = [&]() { |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 398 | replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags, |
| 399 | File, Sig); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | if (WasInserted) |
| 403 | Replace(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 404 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 405 | Lazy->fetch(); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 406 | else { |
| 407 | auto ExistingFunction = dyn_cast<FunctionSymbol>(S); |
| 408 | if (!ExistingFunction) { |
| 409 | reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION); |
| 410 | return S; |
| 411 | } |
| 412 | if (!signatureMatches(ExistingFunction, Sig)) |
| 413 | if (getFunctionVariant(S, Sig, File, &S)) |
| 414 | Replace(); |
| 415 | } |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 416 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 417 | return S; |
| 418 | } |
| 419 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 420 | Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags, |
| 421 | InputFile *File) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 422 | LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 423 | |
| 424 | Symbol *S; |
| 425 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 426 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 +0000 | [diff] [blame] | 427 | if (S->Traced) |
| 428 | printTraceSymbolUndefined(Name, File); |
Sam Clegg | f989a92 | 2018-07-17 19:15:02 +0000 | [diff] [blame] | 429 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 430 | if (WasInserted) |
| 431 | replaceSymbol<UndefinedData>(S, Name, Flags, File); |
| 432 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 433 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 434 | else if (S->isDefined()) |
| 435 | checkDataType(S, File); |
| 436 | return S; |
| 437 | } |
| 438 | |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 439 | Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName, |
| 440 | StringRef ImportModule, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 441 | InputFile *File, |
| 442 | const WasmGlobalType *Type) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 443 | LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 444 | |
| 445 | Symbol *S; |
| 446 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 447 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 +0000 | [diff] [blame] | 448 | if (S->Traced) |
| 449 | printTraceSymbolUndefined(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 450 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 451 | if (WasInserted) |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 452 | replaceSymbol<UndefinedGlobal>(S, Name, ImportName, ImportModule, Flags, |
| 453 | File, Type); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 454 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 455 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 456 | else if (S->isDefined()) |
| 457 | checkGlobalType(S, File, Type); |
| 458 | return S; |
| 459 | } |
| 460 | |
| 461 | void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 462 | LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 463 | StringRef Name = Sym->getName(); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 464 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 465 | Symbol *S; |
| 466 | bool WasInserted; |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 467 | std::tie(S, WasInserted) = insertName(Name); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 468 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 469 | if (WasInserted) { |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 470 | replaceSymbol<LazySymbol>(S, Name, 0, File, *Sym); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 471 | return; |
| 472 | } |
| 473 | |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 474 | if (!S->isUndefined()) |
| 475 | return; |
| 476 | |
| 477 | // The existing symbol is undefined, load a new one from the archive, |
| 478 | // unless the the existing symbol is weak in which case replace the undefined |
| 479 | // symbols with a LazySymbol. |
| 480 | if (S->isWeak()) { |
| 481 | const WasmSignature *OldSig = nullptr; |
| 482 | // In the case of an UndefinedFunction we need to preserve the expected |
| 483 | // signature. |
| 484 | if (auto *F = dyn_cast<UndefinedFunction>(S)) |
| 485 | OldSig = F->Signature; |
| 486 | LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n"); |
| 487 | auto NewSym = replaceSymbol<LazySymbol>(S, Name, WASM_SYMBOL_BINDING_WEAK, |
| 488 | File, *Sym); |
| 489 | NewSym->Signature = OldSig; |
| 490 | return; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 491 | } |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 492 | |
| 493 | LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); |
| 494 | File->addMember(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 495 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 496 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 497 | bool SymbolTable::addComdat(StringRef Name) { |
Sam Clegg | 697f214 | 2019-05-15 16:03:28 +0000 | [diff] [blame] | 498 | return ComdatGroups.insert(CachedHashStringRef(Name)).second; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 499 | } |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 500 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 501 | // The new signature doesn't match. Create a variant to the symbol with the |
| 502 | // signature encoded in the name and return that instead. These symbols are |
| 503 | // then unified later in handleSymbolVariants. |
| 504 | bool SymbolTable::getFunctionVariant(Symbol* Sym, const WasmSignature *Sig, |
| 505 | const InputFile *File, Symbol **Out) { |
Richard Trieu | 14bab09 | 2019-02-21 00:36:14 +0000 | [diff] [blame] | 506 | LLVM_DEBUG(dbgs() << "getFunctionVariant: " << Sym->getName() << " -> " |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 507 | << " " << toString(*Sig) << "\n"); |
| 508 | Symbol *Variant = nullptr; |
| 509 | |
| 510 | // Linear search through symbol variants. Should never be more than two |
| 511 | // or three entries here. |
| 512 | auto &Variants = SymVariants[CachedHashStringRef(Sym->getName())]; |
Fangrui Song | 196a440 | 2019-04-18 13:33:29 +0000 | [diff] [blame] | 513 | if (Variants.empty()) |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 514 | Variants.push_back(Sym); |
| 515 | |
| 516 | for (Symbol* V : Variants) { |
| 517 | if (*V->getSignature() == *Sig) { |
| 518 | Variant = V; |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | bool WasAdded = !Variant; |
| 524 | if (WasAdded) { |
| 525 | // Create a new variant; |
| 526 | LLVM_DEBUG(dbgs() << "added new variant\n"); |
| 527 | Variant = reinterpret_cast<Symbol *>(make<SymbolUnion>()); |
| 528 | Variants.push_back(Variant); |
| 529 | } else { |
| 530 | LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*Variant) << "\n"); |
| 531 | assert(*Variant->getSignature() == *Sig); |
| 532 | } |
| 533 | |
| 534 | *Out = Variant; |
| 535 | return WasAdded; |
| 536 | } |
| 537 | |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 538 | // Set a flag for --trace-symbol so that we can print out a log message |
| 539 | // if a new symbol with the same name is inserted into the symbol table. |
| 540 | void SymbolTable::trace(StringRef Name) { |
| 541 | SymMap.insert({CachedHashStringRef(Name), -1}); |
| 542 | } |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 543 | |
Sam Clegg | a5ca34e | 2019-05-24 14:14:25 +0000 | [diff] [blame^] | 544 | void SymbolTable::wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap) { |
| 545 | // Swap symbols as instructed by -wrap. |
| 546 | int &OrigIdx = SymMap[CachedHashStringRef(Sym->getName())]; |
| 547 | int &RealIdx= SymMap[CachedHashStringRef(Real->getName())]; |
| 548 | int &WrapIdx = SymMap[CachedHashStringRef(Wrap->getName())]; |
| 549 | LLVM_DEBUG(dbgs() << "wrap: " << Sym->getName() << "\n"); |
| 550 | |
| 551 | // Anyone looking up __real symbols should get the original |
| 552 | RealIdx = OrigIdx; |
| 553 | // Anyone looking up the original should get the __wrap symbol |
| 554 | OrigIdx = WrapIdx; |
| 555 | } |
| 556 | |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 557 | static const uint8_t UnreachableFn[] = { |
| 558 | 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, |
| 559 | 0x00 /* opcode unreachable */, 0x0b /* opcode end */ |
| 560 | }; |
| 561 | |
| 562 | // Replace the given symbol body with an unreachable function. |
| 563 | // This is used by handleWeakUndefines in order to generate a callable |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 564 | // equivalent of an undefined function and also handleSymbolVariants for |
| 565 | // undefined functions that don't match the signature of the definition. |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 566 | InputFunction *SymbolTable::replaceWithUnreachable(Symbol *Sym, |
| 567 | const WasmSignature &Sig, |
| 568 | StringRef DebugName) { |
| 569 | auto *Func = make<SyntheticFunction>(Sig, Sym->getName(), DebugName); |
| 570 | Func->setBody(UnreachableFn); |
| 571 | SyntheticFunctions.emplace_back(Func); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 572 | replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Sym->getFlags(), nullptr, |
| 573 | Func); |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 574 | return Func; |
| 575 | } |
| 576 | |
| 577 | // For weak undefined functions, there may be "call" instructions that reference |
| 578 | // the symbol. In this case, we need to synthesise a dummy/stub function that |
| 579 | // will abort at runtime, so that relocations can still provided an operand to |
| 580 | // the call instruction that passes Wasm validation. |
| 581 | void SymbolTable::handleWeakUndefines() { |
| 582 | for (Symbol *Sym : getSymbols()) { |
| 583 | if (!Sym->isUndefWeak()) |
| 584 | continue; |
| 585 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 586 | const WasmSignature *Sig = Sym->getSignature(); |
| 587 | if (!Sig) { |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 588 | // It is possible for undefined functions not to have a signature (eg. if |
| 589 | // added via "--undefined"), but weak undefined ones do have a signature. |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 590 | // Lazy symbols may not be functions and therefore Sig can still be null |
| 591 | // in some circumstantce. |
| 592 | assert(!isa<FunctionSymbol>(Sym)); |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 593 | continue; |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 594 | } |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 595 | |
| 596 | // Add a synthetic dummy for weak undefined functions. These dummies will |
| 597 | // be GC'd if not used as the target of any "call" instructions. |
| 598 | StringRef DebugName = Saver.save("undefined:" + toString(*Sym)); |
| 599 | InputFunction* Func = replaceWithUnreachable(Sym, *Sig, DebugName); |
| 600 | // Ensure it compares equal to the null pointer, and so that table relocs |
| 601 | // don't pull in the stub body (only call-operand relocs should do that). |
| 602 | Func->setTableIndex(0); |
| 603 | // Hide our dummy to prevent export. |
| 604 | Sym->setHidden(true); |
| 605 | } |
| 606 | } |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 607 | |
| 608 | static void reportFunctionSignatureMismatch(StringRef SymName, |
| 609 | FunctionSymbol *A, |
| 610 | FunctionSymbol *B, bool Error) { |
| 611 | std::string msg = ("function signature mismatch: " + SymName + |
| 612 | "\n>>> defined as " + toString(*A->Signature) + " in " + |
| 613 | toString(A->getFile()) + "\n>>> defined as " + |
| 614 | toString(*B->Signature) + " in " + toString(B->getFile())) |
| 615 | .str(); |
| 616 | if (Error) |
| 617 | error(msg); |
| 618 | else |
| 619 | warn(msg); |
| 620 | } |
| 621 | |
| 622 | // Remove any variant symbols that were created due to function signature |
| 623 | // mismatches. |
| 624 | void SymbolTable::handleSymbolVariants() { |
| 625 | for (auto Pair : SymVariants) { |
| 626 | // Push the initial symbol onto the list of variants. |
| 627 | StringRef SymName = Pair.first.val(); |
| 628 | std::vector<Symbol *> &Variants = Pair.second; |
| 629 | |
| 630 | #ifndef NDEBUG |
Sam Clegg | f8d736f | 2019-02-21 01:33:26 +0000 | [diff] [blame] | 631 | LLVM_DEBUG(dbgs() << "symbol with (" << Variants.size() |
| 632 | << ") variants: " << SymName << "\n"); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 633 | for (auto *S: Variants) { |
| 634 | auto *F = cast<FunctionSymbol>(S); |
Sam Clegg | f8d736f | 2019-02-21 01:33:26 +0000 | [diff] [blame] | 635 | LLVM_DEBUG(dbgs() << " variant: " + F->getName() << " " |
| 636 | << toString(*F->Signature) << "\n"); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 637 | } |
| 638 | #endif |
| 639 | |
| 640 | // Find the one definition. |
| 641 | DefinedFunction *Defined = nullptr; |
| 642 | for (auto *Symbol : Variants) { |
| 643 | if (auto F = dyn_cast<DefinedFunction>(Symbol)) { |
| 644 | Defined = F; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | // If there are no definitions, and the undefined symbols disagree on |
| 650 | // the signature, there is not we can do since we don't know which one |
| 651 | // to use as the signature on the import. |
| 652 | if (!Defined) { |
| 653 | reportFunctionSignatureMismatch(SymName, |
| 654 | cast<FunctionSymbol>(Variants[0]), |
| 655 | cast<FunctionSymbol>(Variants[1]), true); |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | for (auto *Symbol : Variants) { |
| 660 | if (Symbol != Defined) { |
| 661 | auto *F = cast<FunctionSymbol>(Symbol); |
| 662 | reportFunctionSignatureMismatch(SymName, F, Defined, false); |
| 663 | StringRef DebugName = Saver.save("unreachable:" + toString(*F)); |
| 664 | replaceWithUnreachable(F, *F->Signature, DebugName); |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | } |