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