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