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 | a282a61 | 2019-06-05 17:50:45 +0000 | [diff] [blame] | 31 | |
| 32 | // .a file |
| 33 | if (auto *F = dyn_cast<ArchiveFile>(File)) { |
| 34 | F->parse(); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | // .so file |
| 39 | if (auto *F = dyn_cast<SharedFile>(File)) { |
| 40 | SharedFiles.push_back(F); |
| 41 | return; |
| 42 | } |
| 43 | |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +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 |
Sam Clegg | a282a61 | 2019-06-05 17:50:45 +0000 | [diff] [blame] | 48 | if (auto *F = dyn_cast<BitcodeFile>(File)) { |
| 49 | F->parse(); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 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 |
| 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() { |
| 68 | if (BitcodeFiles.empty()) |
| 69 | return; |
| 70 | |
| 71 | // Compile bitcode files and replace bitcode symbols. |
| 72 | LTO.reset(new BitcodeCompiler); |
| 73 | for (BitcodeFile *F : BitcodeFiles) |
| 74 | LTO->add(*F); |
| 75 | |
| 76 | for (StringRef Filename : LTO->compile()) { |
Sam Clegg | 0cfaa24 | 2019-04-09 05:41:52 +0000 | [diff] [blame] | 77 | auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp"), ""); |
Sam Clegg | 697f214 | 2019-05-15 16:03:28 +0000 | [diff] [blame] | 78 | Obj->parse(true); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 79 | ObjectFiles.push_back(Obj); |
| 80 | } |
| 81 | } |
| 82 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 83 | Symbol *SymbolTable::find(StringRef Name) { |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 84 | auto It = SymMap.find(CachedHashStringRef(Name)); |
| 85 | if (It == SymMap.end() || It->second == -1) |
| 86 | return nullptr; |
| 87 | return SymVector[It->second]; |
| 88 | } |
| 89 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 90 | void SymbolTable::replace(StringRef Name, Symbol* Sym) { |
| 91 | auto It = SymMap.find(CachedHashStringRef(Name)); |
| 92 | SymVector[It->second] = Sym; |
| 93 | } |
| 94 | |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +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; |
| 104 | } |
| 105 | |
| 106 | if (!IsNew) |
| 107 | return {SymVector[SymIndex], false}; |
| 108 | |
| 109 | Symbol *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); |
| 110 | Sym->IsUsedInRegularObj = false; |
Sam Clegg | a5ca34e | 2019-05-24 14:14:25 +0000 | [diff] [blame] | 111 | Sym->CanInline = true; |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 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 | |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 117 | std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, |
| 118 | const InputFile *File) { |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 119 | Symbol *S; |
| 120 | bool WasInserted; |
| 121 | std::tie(S, WasInserted) = insertName(Name); |
| 122 | |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 123 | if (!File || File->kind() == InputFile::ObjectKind) |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 124 | S->IsUsedInRegularObj = true; |
| 125 | |
| 126 | return {S, WasInserted}; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 129 | static void reportTypeError(const Symbol *Existing, const InputFile *File, |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 130 | llvm::wasm::WasmSymbolType Type) { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 131 | error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " + |
| 132 | toString(Existing->getWasmType()) + " in " + |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 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. |
Benjamin Kramer | ba2ea93 | 2019-03-28 17:18:42 +0000 | [diff] [blame] | 140 | static bool signatureMatches(FunctionSymbol *Existing, |
| 141 | const WasmSignature *NewSig) { |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 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. |
| 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 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 150 | return *NewSig == *OldSig; |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 153 | static void checkGlobalType(const Symbol *Existing, const InputFile *File, |
| 154 | const WasmGlobalType *NewType) { |
| 155 | if (!isa<GlobalSymbol>(Existing)) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 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 | e3498ec | 2018-02-28 00:09:22 +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 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +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); |
| 174 | return; |
| 175 | } |
| 176 | |
| 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)); |
| 188 | } |
| 189 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 190 | static void checkDataType(const Symbol *Existing, const InputFile *File) { |
| 191 | if (!isa<DataSymbol>(Existing)) |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 192 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 195 | DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name, |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 196 | uint32_t Flags, |
| 197 | InputFunction *Function) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 198 | LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 199 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 200 | SyntheticFunctions.emplace_back(Function); |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 201 | return replaceSymbol<DefinedFunction>(insertName(Name).first, Name, |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 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 | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 208 | DefinedData *SymbolTable::addOptionalDataSymbol(StringRef Name, uint32_t Value, |
| 209 | uint32_t Flags) { |
| 210 | Symbol *S = find(Name); |
Sam Clegg | 7d4ec5a | 2019-05-31 22:51:59 +0000 | [diff] [blame] | 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; |
| 215 | LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << Name << "\n"); |
| 216 | auto *rtn = replaceSymbol<DefinedData>(S, Name, Flags); |
| 217 | rtn->setVirtualAddress(Value); |
Sam Clegg | 1500646 | 2019-07-08 10:35:08 +0000 | [diff] [blame] | 218 | rtn->Referenced = true; |
Sam Clegg | 4bce63a | 2019-05-23 10:06:03 +0000 | [diff] [blame] | 219 | return rtn; |
| 220 | } |
| 221 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 222 | DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name, |
| 223 | uint32_t Flags) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 224 | LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 225 | assert(!find(Name)); |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 226 | return replaceSymbol<DefinedData>(insertName(Name).first, Name, Flags); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 229 | DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags, |
| 230 | InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 231 | LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global |
| 232 | << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 233 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 234 | SyntheticGlobals.emplace_back(Global); |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 235 | return replaceSymbol<DefinedGlobal>(insertName(Name).first, Name, Flags, |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 236 | nullptr, Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +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 | e3498ec | 2018-02-28 00:09:22 +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: " |
| 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. |
| 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 | e3498ec | 2018-02-28 00:09:22 +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 | e3498ec | 2018-02-28 00:09:22 +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 | |
| 267 | Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 268 | InputFile *File, |
| 269 | InputFunction *Function) { |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 270 | LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " [" |
| 271 | << (Function ? toString(Function->Signature) : "none") |
| 272 | << "]\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 273 | Symbol *S; |
| 274 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 275 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 276 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +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 |
| 280 | const WasmSignature *OldSig = S->getSignature(); |
| 281 | auto* NewSym = replaceSymbol<DefinedFunction>(Sym, Name, Flags, File, Function); |
| 282 | if (!NewSym->Signature) |
| 283 | NewSym->Signature = OldSig; |
| 284 | }; |
| 285 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 286 | if (WasInserted || S->isLazy()) { |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 287 | ReplaceSym(S); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 288 | return S; |
| 289 | } |
| 290 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +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 | |
Sam Clegg | 59f959f | 2019-05-24 22:45:08 +0000 | [diff] [blame] | 297 | bool CheckSig = true; |
| 298 | if (auto UD = dyn_cast<UndefinedFunction>(ExistingFunction)) |
| 299 | CheckSig = UD->IsCalledDirectly; |
| 300 | |
| 301 | if (CheckSig && Function && !signatureMatches(ExistingFunction, &Function->Signature)) { |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 302 | Symbol* Variant; |
| 303 | if (getFunctionVariant(S, &Function->Signature, File, &Variant)) |
| 304 | // New variant, always replace |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 305 | ReplaceSym(Variant); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 306 | else if (shouldReplace(S, File, Flags)) |
| 307 | // Variant already exists, replace it after checking shouldReplace |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +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. |
| 312 | replace(Name, Variant); |
| 313 | return Variant; |
| 314 | } |
| 315 | |
| 316 | // Existing function with matching signature. |
| 317 | if (shouldReplace(S, File, Flags)) |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 318 | ReplaceSym(S); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 319 | |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 320 | return S; |
| 321 | } |
| 322 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 323 | Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 324 | InputFile *File, InputSegment *Segment, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 325 | uint32_t Address, uint32_t Size) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 326 | LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address |
| 327 | << "\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 328 | Symbol *S; |
| 329 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 330 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 331 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 332 | auto ReplaceSym = [&]() { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 333 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | if (WasInserted || S->isLazy()) { |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 337 | ReplaceSym(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 338 | return S; |
| 339 | } |
| 340 | |
| 341 | checkDataType(S, File); |
| 342 | |
| 343 | if (shouldReplace(S, File, Flags)) |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 344 | ReplaceSym(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 345 | return S; |
| 346 | } |
| 347 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 348 | Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 349 | InputFile *File, InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 350 | LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n"); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 351 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 352 | Symbol *S; |
| 353 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 354 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 355 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 356 | auto ReplaceSym = [&]() { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 357 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
| 360 | if (WasInserted || S->isLazy()) { |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 361 | ReplaceSym(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 362 | return S; |
| 363 | } |
| 364 | |
| 365 | checkGlobalType(S, File, &Global->getType()); |
| 366 | |
| 367 | if (shouldReplace(S, File, Flags)) |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 368 | ReplaceSym(); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 369 | return S; |
| 370 | } |
| 371 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 372 | Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags, |
| 373 | InputFile *File, InputEvent *Event) { |
| 374 | LLVM_DEBUG(dbgs() << "addDefinedEvent:" << Name << "\n"); |
| 375 | |
| 376 | Symbol *S; |
| 377 | bool WasInserted; |
| 378 | std::tie(S, WasInserted) = insert(Name, File); |
| 379 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 380 | auto ReplaceSym = [&]() { |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 381 | replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | if (WasInserted || S->isLazy()) { |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 385 | ReplaceSym(); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 386 | return S; |
| 387 | } |
| 388 | |
| 389 | checkEventType(S, File, &Event->getType(), &Event->Signature); |
| 390 | |
| 391 | if (shouldReplace(S, File, Flags)) |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 392 | ReplaceSym(); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 393 | return S; |
| 394 | } |
| 395 | |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 396 | Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName, |
| 397 | StringRef ImportModule, |
Sam Clegg | 7cc0753 | 2019-02-01 02:29:57 +0000 | [diff] [blame] | 398 | uint32_t Flags, InputFile *File, |
Sam Clegg | 59f959f | 2019-05-24 22:45:08 +0000 | [diff] [blame] | 399 | const WasmSignature *Sig, |
| 400 | bool IsCalledDirectly) { |
Sam Clegg | d506b0a | 2019-05-29 15:36:42 +0000 | [diff] [blame] | 401 | LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << " [" |
| 402 | << (Sig ? toString(*Sig) : "none") |
| 403 | << "] IsCalledDirectly:" << IsCalledDirectly << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 404 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 405 | Symbol *S; |
| 406 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 407 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 +0000 | [diff] [blame] | 408 | if (S->Traced) |
| 409 | printTraceSymbolUndefined(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 410 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 411 | auto ReplaceSym = [&]() { |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 412 | replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags, |
Sam Clegg | 59f959f | 2019-05-24 22:45:08 +0000 | [diff] [blame] | 413 | File, Sig, IsCalledDirectly); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 414 | }; |
| 415 | |
| 416 | if (WasInserted) |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 417 | ReplaceSym(); |
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 | } |
Sam Clegg | d506b0a | 2019-05-29 15:36:42 +0000 | [diff] [blame] | 426 | if (!ExistingFunction->Signature && Sig) |
| 427 | ExistingFunction->Signature = Sig; |
Sam Clegg | 59f959f | 2019-05-24 22:45:08 +0000 | [diff] [blame] | 428 | if (IsCalledDirectly && !signatureMatches(ExistingFunction, Sig)) |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 429 | if (getFunctionVariant(S, Sig, File, &S)) |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 430 | ReplaceSym(); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 431 | } |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 432 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 433 | return S; |
| 434 | } |
| 435 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 436 | Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags, |
| 437 | InputFile *File) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 438 | LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 439 | |
| 440 | Symbol *S; |
| 441 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 442 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 +0000 | [diff] [blame] | 443 | if (S->Traced) |
| 444 | printTraceSymbolUndefined(Name, File); |
Sam Clegg | f989a92 | 2018-07-17 19:15:02 +0000 | [diff] [blame] | 445 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 446 | if (WasInserted) |
| 447 | replaceSymbol<UndefinedData>(S, Name, Flags, File); |
| 448 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 449 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 450 | else if (S->isDefined()) |
| 451 | checkDataType(S, File); |
| 452 | return S; |
| 453 | } |
| 454 | |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 455 | Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName, |
| 456 | StringRef ImportModule, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 457 | InputFile *File, |
| 458 | const WasmGlobalType *Type) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 459 | LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 460 | |
| 461 | Symbol *S; |
| 462 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 463 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | 7991b68 | 2019-05-24 13:29:17 +0000 | [diff] [blame] | 464 | if (S->Traced) |
| 465 | printTraceSymbolUndefined(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 466 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 467 | if (WasInserted) |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 468 | replaceSymbol<UndefinedGlobal>(S, Name, ImportName, ImportModule, Flags, |
| 469 | File, Type); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 470 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 471 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 472 | else if (S->isDefined()) |
| 473 | checkGlobalType(S, File, Type); |
| 474 | return S; |
| 475 | } |
| 476 | |
| 477 | void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 478 | LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 479 | StringRef Name = Sym->getName(); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 480 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 481 | Symbol *S; |
| 482 | bool WasInserted; |
Sam Clegg | d15a4154 | 2019-03-08 21:10:48 +0000 | [diff] [blame] | 483 | std::tie(S, WasInserted) = insertName(Name); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 484 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 485 | if (WasInserted) { |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 486 | replaceSymbol<LazySymbol>(S, Name, 0, File, *Sym); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 487 | return; |
| 488 | } |
| 489 | |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 490 | if (!S->isUndefined()) |
| 491 | return; |
| 492 | |
| 493 | // The existing symbol is undefined, load a new one from the archive, |
| 494 | // unless the the existing symbol is weak in which case replace the undefined |
| 495 | // symbols with a LazySymbol. |
| 496 | if (S->isWeak()) { |
| 497 | const WasmSignature *OldSig = nullptr; |
| 498 | // In the case of an UndefinedFunction we need to preserve the expected |
| 499 | // signature. |
| 500 | if (auto *F = dyn_cast<UndefinedFunction>(S)) |
| 501 | OldSig = F->Signature; |
| 502 | LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n"); |
| 503 | auto NewSym = replaceSymbol<LazySymbol>(S, Name, WASM_SYMBOL_BINDING_WEAK, |
| 504 | File, *Sym); |
| 505 | NewSym->Signature = OldSig; |
| 506 | return; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 507 | } |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 508 | |
| 509 | LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); |
| 510 | File->addMember(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 511 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 512 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 513 | bool SymbolTable::addComdat(StringRef Name) { |
Sam Clegg | 697f214 | 2019-05-15 16:03:28 +0000 | [diff] [blame] | 514 | return ComdatGroups.insert(CachedHashStringRef(Name)).second; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 515 | } |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 516 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 517 | // The new signature doesn't match. Create a variant to the symbol with the |
| 518 | // signature encoded in the name and return that instead. These symbols are |
| 519 | // then unified later in handleSymbolVariants. |
| 520 | bool SymbolTable::getFunctionVariant(Symbol* Sym, const WasmSignature *Sig, |
| 521 | const InputFile *File, Symbol **Out) { |
Richard Trieu | 14bab09 | 2019-02-21 00:36:14 +0000 | [diff] [blame] | 522 | LLVM_DEBUG(dbgs() << "getFunctionVariant: " << Sym->getName() << " -> " |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 523 | << " " << toString(*Sig) << "\n"); |
| 524 | Symbol *Variant = nullptr; |
| 525 | |
| 526 | // Linear search through symbol variants. Should never be more than two |
| 527 | // or three entries here. |
| 528 | auto &Variants = SymVariants[CachedHashStringRef(Sym->getName())]; |
Fangrui Song | 196a440 | 2019-04-18 13:33:29 +0000 | [diff] [blame] | 529 | if (Variants.empty()) |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 530 | Variants.push_back(Sym); |
| 531 | |
| 532 | for (Symbol* V : Variants) { |
| 533 | if (*V->getSignature() == *Sig) { |
| 534 | Variant = V; |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | bool WasAdded = !Variant; |
| 540 | if (WasAdded) { |
| 541 | // Create a new variant; |
| 542 | LLVM_DEBUG(dbgs() << "added new variant\n"); |
| 543 | Variant = reinterpret_cast<Symbol *>(make<SymbolUnion>()); |
| 544 | Variants.push_back(Variant); |
| 545 | } else { |
| 546 | LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*Variant) << "\n"); |
| 547 | assert(*Variant->getSignature() == *Sig); |
| 548 | } |
| 549 | |
| 550 | *Out = Variant; |
| 551 | return WasAdded; |
| 552 | } |
| 553 | |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 554 | // Set a flag for --trace-symbol so that we can print out a log message |
| 555 | // if a new symbol with the same name is inserted into the symbol table. |
| 556 | void SymbolTable::trace(StringRef Name) { |
| 557 | SymMap.insert({CachedHashStringRef(Name), -1}); |
| 558 | } |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 559 | |
Sam Clegg | a5ca34e | 2019-05-24 14:14:25 +0000 | [diff] [blame] | 560 | void SymbolTable::wrap(Symbol *Sym, Symbol *Real, Symbol *Wrap) { |
| 561 | // Swap symbols as instructed by -wrap. |
| 562 | int &OrigIdx = SymMap[CachedHashStringRef(Sym->getName())]; |
| 563 | int &RealIdx= SymMap[CachedHashStringRef(Real->getName())]; |
| 564 | int &WrapIdx = SymMap[CachedHashStringRef(Wrap->getName())]; |
| 565 | LLVM_DEBUG(dbgs() << "wrap: " << Sym->getName() << "\n"); |
| 566 | |
| 567 | // Anyone looking up __real symbols should get the original |
| 568 | RealIdx = OrigIdx; |
| 569 | // Anyone looking up the original should get the __wrap symbol |
| 570 | OrigIdx = WrapIdx; |
| 571 | } |
| 572 | |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 573 | static const uint8_t UnreachableFn[] = { |
| 574 | 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, |
| 575 | 0x00 /* opcode unreachable */, 0x0b /* opcode end */ |
| 576 | }; |
| 577 | |
| 578 | // Replace the given symbol body with an unreachable function. |
| 579 | // This is used by handleWeakUndefines in order to generate a callable |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 580 | // equivalent of an undefined function and also handleSymbolVariants for |
| 581 | // undefined functions that don't match the signature of the definition. |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 582 | InputFunction *SymbolTable::replaceWithUnreachable(Symbol *Sym, |
| 583 | const WasmSignature &Sig, |
| 584 | StringRef DebugName) { |
| 585 | auto *Func = make<SyntheticFunction>(Sig, Sym->getName(), DebugName); |
| 586 | Func->setBody(UnreachableFn); |
| 587 | SyntheticFunctions.emplace_back(Func); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 588 | replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Sym->getFlags(), nullptr, |
| 589 | Func); |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 590 | return Func; |
| 591 | } |
| 592 | |
| 593 | // For weak undefined functions, there may be "call" instructions that reference |
| 594 | // the symbol. In this case, we need to synthesise a dummy/stub function that |
| 595 | // will abort at runtime, so that relocations can still provided an operand to |
| 596 | // the call instruction that passes Wasm validation. |
| 597 | void SymbolTable::handleWeakUndefines() { |
| 598 | for (Symbol *Sym : getSymbols()) { |
| 599 | if (!Sym->isUndefWeak()) |
| 600 | continue; |
| 601 | |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 602 | const WasmSignature *Sig = Sym->getSignature(); |
| 603 | if (!Sig) { |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 604 | // It is possible for undefined functions not to have a signature (eg. if |
| 605 | // added via "--undefined"), but weak undefined ones do have a signature. |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 606 | // Lazy symbols may not be functions and therefore Sig can still be null |
| 607 | // in some circumstantce. |
| 608 | assert(!isa<FunctionSymbol>(Sym)); |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 609 | continue; |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 610 | } |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame] | 611 | |
| 612 | // Add a synthetic dummy for weak undefined functions. These dummies will |
| 613 | // be GC'd if not used as the target of any "call" instructions. |
| 614 | StringRef DebugName = Saver.save("undefined:" + toString(*Sym)); |
| 615 | InputFunction* Func = replaceWithUnreachable(Sym, *Sig, DebugName); |
| 616 | // Ensure it compares equal to the null pointer, and so that table relocs |
| 617 | // don't pull in the stub body (only call-operand relocs should do that). |
| 618 | Func->setTableIndex(0); |
| 619 | // Hide our dummy to prevent export. |
| 620 | Sym->setHidden(true); |
| 621 | } |
| 622 | } |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 623 | |
| 624 | static void reportFunctionSignatureMismatch(StringRef SymName, |
| 625 | FunctionSymbol *A, |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 626 | FunctionSymbol *B, bool IsError) { |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 627 | std::string msg = ("function signature mismatch: " + SymName + |
| 628 | "\n>>> defined as " + toString(*A->Signature) + " in " + |
| 629 | toString(A->getFile()) + "\n>>> defined as " + |
| 630 | toString(*B->Signature) + " in " + toString(B->getFile())) |
| 631 | .str(); |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 632 | if (IsError) |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 633 | error(msg); |
| 634 | else |
| 635 | warn(msg); |
| 636 | } |
| 637 | |
| 638 | // Remove any variant symbols that were created due to function signature |
| 639 | // mismatches. |
| 640 | void SymbolTable::handleSymbolVariants() { |
| 641 | for (auto Pair : SymVariants) { |
| 642 | // Push the initial symbol onto the list of variants. |
| 643 | StringRef SymName = Pair.first.val(); |
| 644 | std::vector<Symbol *> &Variants = Pair.second; |
| 645 | |
| 646 | #ifndef NDEBUG |
Sam Clegg | f8d736f | 2019-02-21 01:33:26 +0000 | [diff] [blame] | 647 | LLVM_DEBUG(dbgs() << "symbol with (" << Variants.size() |
| 648 | << ") variants: " << SymName << "\n"); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 649 | for (auto *S: Variants) { |
| 650 | auto *F = cast<FunctionSymbol>(S); |
Sam Clegg | f8d736f | 2019-02-21 01:33:26 +0000 | [diff] [blame] | 651 | LLVM_DEBUG(dbgs() << " variant: " + F->getName() << " " |
| 652 | << toString(*F->Signature) << "\n"); |
Sam Clegg | 6540e57 | 2019-02-20 23:19:31 +0000 | [diff] [blame] | 653 | } |
| 654 | #endif |
| 655 | |
| 656 | // Find the one definition. |
| 657 | DefinedFunction *Defined = nullptr; |
| 658 | for (auto *Symbol : Variants) { |
| 659 | if (auto F = dyn_cast<DefinedFunction>(Symbol)) { |
| 660 | Defined = F; |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // If there are no definitions, and the undefined symbols disagree on |
| 666 | // the signature, there is not we can do since we don't know which one |
| 667 | // to use as the signature on the import. |
| 668 | if (!Defined) { |
| 669 | reportFunctionSignatureMismatch(SymName, |
| 670 | cast<FunctionSymbol>(Variants[0]), |
| 671 | cast<FunctionSymbol>(Variants[1]), true); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | for (auto *Symbol : Variants) { |
| 676 | if (Symbol != Defined) { |
| 677 | auto *F = cast<FunctionSymbol>(Symbol); |
| 678 | reportFunctionSignatureMismatch(SymName, F, Defined, false); |
| 679 | StringRef DebugName = Saver.save("unreachable:" + toString(*F)); |
| 680 | replaceWithUnreachable(F, *F->Signature, DebugName); |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | } |