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