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); |
| 40 | } |
| 41 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 42 | // This function is where all the optimizations of link-time |
| 43 | // optimization happens. When LTO is in use, some input files are |
| 44 | // not in native object file format but in the LLVM bitcode format. |
| 45 | // This function compiles bitcode files into a few big native files |
| 46 | // using LLVM functions and replaces bitcode symbols with the results. |
| 47 | // Because all bitcode files that the program consists of are passed |
| 48 | // to the compiler at once, it can do whole-program optimization. |
| 49 | void SymbolTable::addCombinedLTOObject() { |
| 50 | if (BitcodeFiles.empty()) |
| 51 | return; |
| 52 | |
| 53 | // Compile bitcode files and replace bitcode symbols. |
| 54 | LTO.reset(new BitcodeCompiler); |
| 55 | for (BitcodeFile *F : BitcodeFiles) |
| 56 | LTO->add(*F); |
| 57 | |
| 58 | for (StringRef Filename : LTO->compile()) { |
| 59 | auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp")); |
| 60 | Obj->parse(); |
| 61 | ObjectFiles.push_back(Obj); |
| 62 | } |
| 63 | } |
| 64 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 65 | void SymbolTable::reportRemainingUndefines() { |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 66 | for (Symbol *Sym : SymVector) { |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 67 | if (!Sym->isUndefined() || Sym->isWeak()) |
| 68 | continue; |
| 69 | if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0) |
| 70 | continue; |
| 71 | if (!Sym->IsUsedInRegularObj) |
| 72 | continue; |
Sam Clegg | 47e2b6b | 2018-08-04 00:04:06 +0000 | [diff] [blame] | 73 | error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 74 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | Symbol *SymbolTable::find(StringRef Name) { |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 78 | auto It = SymMap.find(CachedHashStringRef(Name)); |
| 79 | if (It == SymMap.end() || It->second == -1) |
| 80 | return nullptr; |
| 81 | return SymVector[It->second]; |
| 82 | } |
| 83 | |
| 84 | std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) { |
| 85 | bool Trace = false; |
| 86 | auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()}); |
| 87 | int &SymIndex = P.first->second; |
| 88 | bool IsNew = P.second; |
| 89 | if (SymIndex == -1) { |
| 90 | SymIndex = SymVector.size(); |
| 91 | Trace = true; |
| 92 | IsNew = true; |
| 93 | } |
| 94 | |
| 95 | if (!IsNew) |
| 96 | return {SymVector[SymIndex], false}; |
| 97 | |
| 98 | Symbol *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); |
| 99 | Sym->IsUsedInRegularObj = false; |
| 100 | Sym->Traced = Trace; |
| 101 | SymVector.emplace_back(Sym); |
| 102 | return {Sym, true}; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame^] | 105 | std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, |
| 106 | const InputFile *File) { |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 107 | Symbol *S; |
| 108 | bool WasInserted; |
| 109 | std::tie(S, WasInserted) = insertName(Name); |
| 110 | |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 111 | if (!File || File->kind() == InputFile::ObjectKind) |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 112 | S->IsUsedInRegularObj = true; |
| 113 | |
| 114 | return {S, WasInserted}; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 117 | static void reportTypeError(const Symbol *Existing, const InputFile *File, |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 118 | llvm::wasm::WasmSymbolType Type) { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 119 | error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " + |
| 120 | toString(Existing->getWasmType()) + " in " + |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 121 | toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) + |
| 122 | " in " + toString(File)); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Heejin Ahn | 6f4286f | 2018-11-19 23:31:28 +0000 | [diff] [blame] | 125 | // Check the type of new symbol matches that of the symbol is replacing. |
| 126 | // For functions this can also involve verifying that the signatures match. |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 127 | static void checkFunctionType(Symbol *Existing, const InputFile *File, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 128 | const WasmSignature *NewSig) { |
Sam Clegg | 65d6380 | 2018-05-14 23:01:16 +0000 | [diff] [blame] | 129 | auto ExistingFunction = dyn_cast<FunctionSymbol>(Existing); |
| 130 | if (!ExistingFunction) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 131 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_FUNCTION); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 135 | if (!NewSig) |
| 136 | return; |
| 137 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 138 | const WasmSignature *OldSig = ExistingFunction->Signature; |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 139 | if (!OldSig) { |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 140 | ExistingFunction->Signature = NewSig; |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | if (*NewSig != *OldSig) |
Sam Clegg | ffd0aaf | 2018-06-22 15:13:10 +0000 | [diff] [blame] | 145 | warn("function signature mismatch: " + Existing->getName() + |
| 146 | "\n>>> defined as " + toString(*OldSig) + " in " + |
| 147 | toString(Existing->getFile()) + "\n>>> defined as " + |
| 148 | toString(*NewSig) + " in " + toString(File)); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 151 | static void checkGlobalType(const Symbol *Existing, const InputFile *File, |
| 152 | const WasmGlobalType *NewType) { |
| 153 | if (!isa<GlobalSymbol>(Existing)) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 154 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 158 | const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType(); |
| 159 | if (*NewType != *OldType) { |
| 160 | error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " + |
| 161 | toString(*OldType) + " in " + toString(Existing->getFile()) + |
| 162 | "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 163 | } |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 166 | static void checkEventType(const Symbol *Existing, const InputFile *File, |
| 167 | const WasmEventType *NewType, |
| 168 | const WasmSignature *NewSig) { |
| 169 | auto ExistingEvent = dyn_cast<EventSymbol>(Existing); |
| 170 | if (!isa<EventSymbol>(Existing)) { |
| 171 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_EVENT); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | const WasmEventType *OldType = cast<EventSymbol>(Existing)->getEventType(); |
| 176 | const WasmSignature *OldSig = ExistingEvent->Signature; |
| 177 | if (NewType->Attribute != OldType->Attribute) |
| 178 | error("Event type mismatch: " + Existing->getName() + "\n>>> defined as " + |
| 179 | toString(*OldType) + " in " + toString(Existing->getFile()) + |
| 180 | "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); |
| 181 | if (*NewSig != *OldSig) |
| 182 | warn("Event signature mismatch: " + Existing->getName() + |
| 183 | "\n>>> defined as " + toString(*OldSig) + " in " + |
| 184 | toString(Existing->getFile()) + "\n>>> defined as " + |
| 185 | toString(*NewSig) + " in " + toString(File)); |
| 186 | } |
| 187 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 188 | static void checkDataType(const Symbol *Existing, const InputFile *File) { |
| 189 | if (!isa<DataSymbol>(Existing)) |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 190 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 193 | DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name, |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 194 | uint32_t Flags, |
| 195 | InputFunction *Function) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 196 | LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 197 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 198 | SyntheticFunctions.emplace_back(Function); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 199 | return replaceSymbol<DefinedFunction>(insert(Name, nullptr).first, Name, |
| 200 | Flags, nullptr, Function); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 203 | DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name, |
| 204 | uint32_t Flags) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 205 | LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 206 | assert(!find(Name)); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 207 | return replaceSymbol<DefinedData>(insert(Name, nullptr).first, Name, Flags); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 210 | DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags, |
| 211 | InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 212 | LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global |
| 213 | << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 214 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 215 | SyntheticGlobals.emplace_back(Global); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 216 | return replaceSymbol<DefinedGlobal>(insert(Name, nullptr).first, Name, Flags, |
| 217 | nullptr, Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 220 | static bool shouldReplace(const Symbol *Existing, InputFile *NewFile, |
| 221 | uint32_t NewFlags) { |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 222 | // If existing symbol is undefined, replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 223 | if (!Existing->isDefined()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 224 | LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " |
| 225 | << Existing->getName() << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
| 229 | // Now we have two defined symbols. If the new one is weak, we can ignore it. |
| 230 | if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 231 | LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | |
| 235 | // If the existing symbol is weak, we should replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 236 | if (Existing->isWeak()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 237 | LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | // Neither symbol is week. They conflict. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 242 | error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " + |
| 243 | toString(Existing->getFile()) + "\n>>> defined in " + |
| 244 | toString(NewFile)); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 245 | return true; |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 249 | InputFile *File, |
| 250 | InputFunction *Function) { |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 251 | LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " [" |
| 252 | << (Function ? toString(Function->Signature) : "none") |
| 253 | << "]\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 254 | Symbol *S; |
| 255 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 256 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 257 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 258 | if (WasInserted || S->isLazy()) { |
| 259 | replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function); |
| 260 | return S; |
| 261 | } |
| 262 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 263 | if (Function) |
| 264 | checkFunctionType(S, File, &Function->Signature); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 265 | |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 266 | if (shouldReplace(S, File, Flags)) { |
| 267 | // If the new defined function doesn't have signture (i.e. bitcode |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 268 | // functions) but the old symbol does then preserve the old signature |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 269 | const WasmSignature *OldSig = nullptr; |
| 270 | if (auto* F = dyn_cast<FunctionSymbol>(S)) |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 271 | OldSig = F->Signature; |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 272 | if (auto *L = dyn_cast<LazySymbol>(S)) |
| 273 | OldSig = L->Signature; |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 274 | auto NewSym = replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function); |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 275 | if (!NewSym->Signature) |
| 276 | NewSym->Signature = OldSig; |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 277 | } |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 278 | return S; |
| 279 | } |
| 280 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 281 | Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 282 | InputFile *File, InputSegment *Segment, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 283 | uint32_t Address, uint32_t Size) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 284 | LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address |
| 285 | << "\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 286 | Symbol *S; |
| 287 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 288 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 289 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 290 | if (WasInserted || S->isLazy()) { |
| 291 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
| 292 | return S; |
| 293 | } |
| 294 | |
| 295 | checkDataType(S, File); |
| 296 | |
| 297 | if (shouldReplace(S, File, Flags)) |
| 298 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 299 | return S; |
| 300 | } |
| 301 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 302 | Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 303 | InputFile *File, InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 304 | LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n"); |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 305 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 306 | Symbol *S; |
| 307 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 308 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 309 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 310 | if (WasInserted || S->isLazy()) { |
| 311 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
| 312 | return S; |
| 313 | } |
| 314 | |
| 315 | checkGlobalType(S, File, &Global->getType()); |
| 316 | |
| 317 | if (shouldReplace(S, File, Flags)) |
| 318 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 319 | return S; |
| 320 | } |
| 321 | |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 +0000 | [diff] [blame] | 322 | Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags, |
| 323 | InputFile *File, InputEvent *Event) { |
| 324 | LLVM_DEBUG(dbgs() << "addDefinedEvent:" << Name << "\n"); |
| 325 | |
| 326 | Symbol *S; |
| 327 | bool WasInserted; |
| 328 | std::tie(S, WasInserted) = insert(Name, File); |
| 329 | |
| 330 | if (WasInserted || S->isLazy()) { |
| 331 | replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event); |
| 332 | return S; |
| 333 | } |
| 334 | |
| 335 | checkEventType(S, File, &Event->getType(), &Event->Signature); |
| 336 | |
| 337 | if (shouldReplace(S, File, Flags)) |
| 338 | replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event); |
| 339 | return S; |
| 340 | } |
| 341 | |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 342 | Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName, |
| 343 | StringRef ImportModule, |
Sam Clegg | 7cc0753 | 2019-02-01 02:29:57 +0000 | [diff] [blame] | 344 | uint32_t Flags, InputFile *File, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 345 | const WasmSignature *Sig) { |
Sam Clegg | 8b0b48f | 2018-09-28 16:50:14 +0000 | [diff] [blame] | 346 | LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << |
| 347 | " [" << (Sig ? toString(*Sig) : "none") << "]\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 348 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 349 | Symbol *S; |
| 350 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 351 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 352 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 353 | if (WasInserted) |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 354 | replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags, |
| 355 | File, Sig); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 356 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 357 | Lazy->fetch(); |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 358 | else |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 359 | checkFunctionType(S, File, Sig); |
Sam Clegg | cefbf9a | 2018-06-28 16:53:53 +0000 | [diff] [blame] | 360 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 361 | return S; |
| 362 | } |
| 363 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 364 | Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags, |
| 365 | InputFile *File) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 366 | LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 367 | |
| 368 | Symbol *S; |
| 369 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 370 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | f989a92 | 2018-07-17 19:15:02 +0000 | [diff] [blame] | 371 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 372 | if (WasInserted) |
| 373 | replaceSymbol<UndefinedData>(S, Name, Flags, File); |
| 374 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 375 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 376 | else if (S->isDefined()) |
| 377 | checkDataType(S, File); |
| 378 | return S; |
| 379 | } |
| 380 | |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 381 | Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName, |
| 382 | StringRef ImportModule, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 383 | InputFile *File, |
| 384 | const WasmGlobalType *Type) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 385 | LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 386 | |
| 387 | Symbol *S; |
| 388 | bool WasInserted; |
Sam Clegg | 4c2cbfe | 2018-08-02 20:39:19 +0000 | [diff] [blame] | 389 | std::tie(S, WasInserted) = insert(Name, File); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 390 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 391 | if (WasInserted) |
Dan Gohman | 9b84eea | 2019-02-07 22:00:48 +0000 | [diff] [blame] | 392 | replaceSymbol<UndefinedGlobal>(S, Name, ImportName, ImportModule, Flags, |
| 393 | File, Type); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 394 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 395 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 396 | else if (S->isDefined()) |
| 397 | checkGlobalType(S, File, Type); |
| 398 | return S; |
| 399 | } |
| 400 | |
| 401 | void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 402 | LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 403 | StringRef Name = Sym->getName(); |
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, nullptr); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 408 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 409 | if (WasInserted) { |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 410 | replaceSymbol<LazySymbol>(S, Name, 0, File, *Sym); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 414 | if (!S->isUndefined()) |
| 415 | return; |
| 416 | |
| 417 | // The existing symbol is undefined, load a new one from the archive, |
| 418 | // unless the the existing symbol is weak in which case replace the undefined |
| 419 | // symbols with a LazySymbol. |
| 420 | if (S->isWeak()) { |
| 421 | const WasmSignature *OldSig = nullptr; |
| 422 | // In the case of an UndefinedFunction we need to preserve the expected |
| 423 | // signature. |
| 424 | if (auto *F = dyn_cast<UndefinedFunction>(S)) |
| 425 | OldSig = F->Signature; |
| 426 | LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n"); |
| 427 | auto NewSym = replaceSymbol<LazySymbol>(S, Name, WASM_SYMBOL_BINDING_WEAK, |
| 428 | File, *Sym); |
| 429 | NewSym->Signature = OldSig; |
| 430 | return; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 431 | } |
Sam Clegg | 37b4ee5 | 2019-01-29 22:26:31 +0000 | [diff] [blame] | 432 | |
| 433 | LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); |
| 434 | File->addMember(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 435 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 436 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 437 | bool SymbolTable::addComdat(StringRef Name) { |
| 438 | return Comdats.insert(CachedHashStringRef(Name)).second; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 439 | } |
Sam Clegg | 1f3f774 | 2019-02-06 02:35:18 +0000 | [diff] [blame] | 440 | |
| 441 | // Set a flag for --trace-symbol so that we can print out a log message |
| 442 | // if a new symbol with the same name is inserted into the symbol table. |
| 443 | void SymbolTable::trace(StringRef Name) { |
| 444 | SymMap.insert({CachedHashStringRef(Name), -1}); |
| 445 | } |
Sam Clegg | 230dc11 | 2019-02-07 22:42:16 +0000 | [diff] [blame^] | 446 | |
| 447 | static const uint8_t UnreachableFn[] = { |
| 448 | 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, |
| 449 | 0x00 /* opcode unreachable */, 0x0b /* opcode end */ |
| 450 | }; |
| 451 | |
| 452 | // Replace the given symbol body with an unreachable function. |
| 453 | // This is used by handleWeakUndefines in order to generate a callable |
| 454 | // equivalent of an undefined function. |
| 455 | InputFunction *SymbolTable::replaceWithUnreachable(Symbol *Sym, |
| 456 | const WasmSignature &Sig, |
| 457 | StringRef DebugName) { |
| 458 | auto *Func = make<SyntheticFunction>(Sig, Sym->getName(), DebugName); |
| 459 | Func->setBody(UnreachableFn); |
| 460 | SyntheticFunctions.emplace_back(Func); |
| 461 | replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Sym->getFlags(), nullptr, Func); |
| 462 | return Func; |
| 463 | } |
| 464 | |
| 465 | // For weak undefined functions, there may be "call" instructions that reference |
| 466 | // the symbol. In this case, we need to synthesise a dummy/stub function that |
| 467 | // will abort at runtime, so that relocations can still provided an operand to |
| 468 | // the call instruction that passes Wasm validation. |
| 469 | void SymbolTable::handleWeakUndefines() { |
| 470 | for (Symbol *Sym : getSymbols()) { |
| 471 | if (!Sym->isUndefWeak()) |
| 472 | continue; |
| 473 | |
| 474 | const WasmSignature *Sig = nullptr; |
| 475 | |
| 476 | if (auto *FuncSym = dyn_cast<FunctionSymbol>(Sym)) { |
| 477 | // It is possible for undefined functions not to have a signature (eg. if |
| 478 | // added via "--undefined"), but weak undefined ones do have a signature. |
| 479 | assert(FuncSym->Signature); |
| 480 | Sig = FuncSym->Signature; |
| 481 | } else if (auto *LazySym = dyn_cast<LazySymbol>(Sym)) { |
| 482 | // Lazy symbols may not be functions and therefore can have a null |
| 483 | // signature. |
| 484 | Sig = LazySym->Signature; |
| 485 | } |
| 486 | |
| 487 | if (!Sig) |
| 488 | continue; |
| 489 | |
| 490 | // Add a synthetic dummy for weak undefined functions. These dummies will |
| 491 | // be GC'd if not used as the target of any "call" instructions. |
| 492 | StringRef DebugName = Saver.save("undefined:" + toString(*Sym)); |
| 493 | InputFunction* Func = replaceWithUnreachable(Sym, *Sig, DebugName); |
| 494 | // Ensure it compares equal to the null pointer, and so that table relocs |
| 495 | // don't pull in the stub body (only call-operand relocs should do that). |
| 496 | Func->setTableIndex(0); |
| 497 | // Hide our dummy to prevent export. |
| 498 | Sym->setHidden(true); |
| 499 | } |
| 500 | } |