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