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