Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- SymbolTable.cpp ----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "SymbolTable.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 11 | #include "Config.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 12 | #include "InputChunks.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 | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 23 | using namespace lld; |
| 24 | using namespace lld::wasm; |
| 25 | |
| 26 | SymbolTable *lld::wasm::Symtab; |
| 27 | |
| 28 | void SymbolTable::addFile(InputFile *File) { |
| 29 | log("Processing: " + toString(File)); |
| 30 | File->parse(); |
| 31 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 32 | // LLVM bitcode file |
| 33 | if (auto *F = dyn_cast<BitcodeFile>(File)) |
| 34 | BitcodeFiles.push_back(F); |
| 35 | else if (auto *F = dyn_cast<ObjFile>(File)) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 36 | ObjectFiles.push_back(F); |
| 37 | } |
| 38 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 39 | // This function is where all the optimizations of link-time |
| 40 | // optimization happens. When LTO is in use, some input files are |
| 41 | // not in native object file format but in the LLVM bitcode format. |
| 42 | // This function compiles bitcode files into a few big native files |
| 43 | // using LLVM functions and replaces bitcode symbols with the results. |
| 44 | // Because all bitcode files that the program consists of are passed |
| 45 | // to the compiler at once, it can do whole-program optimization. |
| 46 | void SymbolTable::addCombinedLTOObject() { |
| 47 | if (BitcodeFiles.empty()) |
| 48 | return; |
| 49 | |
| 50 | // Compile bitcode files and replace bitcode symbols. |
| 51 | LTO.reset(new BitcodeCompiler); |
| 52 | for (BitcodeFile *F : BitcodeFiles) |
| 53 | LTO->add(*F); |
| 54 | |
| 55 | for (StringRef Filename : LTO->compile()) { |
| 56 | auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp")); |
| 57 | Obj->parse(); |
| 58 | ObjectFiles.push_back(Obj); |
| 59 | } |
| 60 | } |
| 61 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 62 | void SymbolTable::reportRemainingUndefines() { |
Rui Ueyama | 7d67dd1 | 2018-02-13 22:30:52 +0000 | [diff] [blame] | 63 | SetVector<Symbol *> Undefs; |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 64 | for (Symbol *Sym : SymVector) { |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 65 | if (!Sym->isUndefined() || Sym->isWeak()) |
| 66 | continue; |
| 67 | if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0) |
| 68 | continue; |
| 69 | if (!Sym->IsUsedInRegularObj) |
| 70 | continue; |
| 71 | Undefs.insert(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (Undefs.empty()) |
| 75 | return; |
| 76 | |
| 77 | for (ObjFile *File : ObjectFiles) |
| 78 | for (Symbol *Sym : File->getSymbols()) |
| 79 | if (Undefs.count(Sym)) |
| 80 | error(toString(File) + ": undefined symbol: " + toString(*Sym)); |
| 81 | |
| 82 | for (Symbol *Sym : Undefs) |
| 83 | if (!Sym->getFile()) |
| 84 | error("undefined symbol: " + toString(*Sym)); |
| 85 | } |
| 86 | |
| 87 | Symbol *SymbolTable::find(StringRef Name) { |
Rui Ueyama | 67d6908 | 2018-02-28 23:03:06 +0000 | [diff] [blame] | 88 | return SymMap.lookup(CachedHashStringRef(Name)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) { |
Sam Clegg | a80d94d | 2017-11-27 23:16:06 +0000 | [diff] [blame] | 92 | Symbol *&Sym = SymMap[CachedHashStringRef(Name)]; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 93 | if (Sym) |
| 94 | return {Sym, false}; |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 95 | Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 96 | Sym->IsUsedInRegularObj = false; |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 97 | SymVector.emplace_back(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 98 | return {Sym, true}; |
| 99 | } |
| 100 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 101 | static void reportTypeError(const Symbol *Existing, const InputFile *File, |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 102 | llvm::wasm::WasmSymbolType Type) { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 103 | error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " + |
| 104 | toString(Existing->getWasmType()) + " in " + |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 105 | toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) + |
| 106 | " in " + toString(File)); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Sam Clegg | 1369dfa | 2018-06-21 00:12:25 +0000 | [diff] [blame] | 109 | static void checkFunctionType(Symbol *Existing, const InputFile *File, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 110 | const WasmSignature *NewSig) { |
Sam Clegg | 65d6380 | 2018-05-14 23:01:16 +0000 | [diff] [blame] | 111 | auto ExistingFunction = dyn_cast<FunctionSymbol>(Existing); |
| 112 | if (!ExistingFunction) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 113 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_FUNCTION); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
Sam Clegg | 1369dfa | 2018-06-21 00:12:25 +0000 | [diff] [blame] | 117 | |
Sam Clegg | 65d6380 | 2018-05-14 23:01:16 +0000 | [diff] [blame] | 118 | const WasmSignature *OldSig = ExistingFunction->getFunctionType(); |
| 119 | if (OldSig && NewSig && *NewSig != *OldSig) { |
Sam Clegg | 1369dfa | 2018-06-21 00:12:25 +0000 | [diff] [blame] | 120 | // Don't generate more than one warning per symbol. |
| 121 | if (Existing->SignatureMismatch) |
| 122 | return; |
| 123 | Existing->SignatureMismatch = true; |
| 124 | |
| 125 | std::string msg = ("function signature mismatch: " + Existing->getName() + |
| 126 | "\n>>> defined as " + toString(*OldSig) + " in " + |
| 127 | toString(Existing->getFile()) + "\n>>> defined as " + |
| 128 | toString(*NewSig) + " in " + toString(File)) |
| 129 | .str(); |
| 130 | // A function signature mismatch is only really problem if the mismatched |
| 131 | // symbol is included in the final output, and gc-sections can remove the |
| 132 | // offending uses. Therefore we delay reporting this as an error when |
| 133 | // section GC is enabled. |
| 134 | if (Config->GcSections) |
| 135 | warn(msg); |
| 136 | else |
| 137 | error(msg); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 141 | // Check the type of new symbol matches that of the symbol is replacing. |
| 142 | // For functions this can also involve verifying that the signatures match. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 143 | static void checkGlobalType(const Symbol *Existing, const InputFile *File, |
| 144 | const WasmGlobalType *NewType) { |
| 145 | if (!isa<GlobalSymbol>(Existing)) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 146 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 150 | const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType(); |
| 151 | if (*NewType != *OldType) { |
| 152 | error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " + |
| 153 | toString(*OldType) + " in " + toString(Existing->getFile()) + |
| 154 | "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 155 | } |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 158 | static void checkDataType(const Symbol *Existing, const InputFile *File) { |
| 159 | if (!isa<DataSymbol>(Existing)) |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 160 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 163 | DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name, |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 164 | uint32_t Flags, |
| 165 | InputFunction *Function) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 166 | LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 167 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 168 | SyntheticFunctions.emplace_back(Function); |
| 169 | return replaceSymbol<DefinedFunction>(insert(Name).first, Name, Flags, |
| 170 | nullptr, Function); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 173 | DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name, |
| 174 | uint32_t Flags) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 175 | LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 176 | assert(!find(Name)); |
| 177 | return replaceSymbol<DefinedData>(insert(Name).first, Name, Flags); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 180 | DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags, |
| 181 | InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 182 | LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global |
| 183 | << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 184 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 185 | SyntheticGlobals.emplace_back(Global); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 186 | return replaceSymbol<DefinedGlobal>(insert(Name).first, Name, Flags, nullptr, |
| 187 | Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 190 | static bool shouldReplace(const Symbol *Existing, InputFile *NewFile, |
| 191 | uint32_t NewFlags) { |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 192 | // If existing symbol is undefined, replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 193 | if (!Existing->isDefined()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 194 | LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " |
| 195 | << Existing->getName() << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | |
| 199 | // Now we have two defined symbols. If the new one is weak, we can ignore it. |
| 200 | if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 201 | LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
| 205 | // If the existing symbol is weak, we should replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 206 | if (Existing->isWeak()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 207 | LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
| 211 | // Neither symbol is week. They conflict. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 212 | error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " + |
| 213 | toString(Existing->getFile()) + "\n>>> defined in " + |
| 214 | toString(NewFile)); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 215 | return true; |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 219 | InputFile *File, |
| 220 | InputFunction *Function) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 221 | LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 222 | Symbol *S; |
| 223 | bool WasInserted; |
| 224 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 225 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 226 | if (!File || File->kind() == InputFile::ObjectKind) |
| 227 | S->IsUsedInRegularObj = true; |
| 228 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 229 | if (WasInserted || S->isLazy()) { |
| 230 | replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function); |
| 231 | return S; |
| 232 | } |
| 233 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 234 | if (Function) |
| 235 | checkFunctionType(S, File, &Function->Signature); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 236 | |
| 237 | if (shouldReplace(S, File, Flags)) |
| 238 | replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 239 | return S; |
| 240 | } |
| 241 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 242 | Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 243 | InputFile *File, InputSegment *Segment, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 244 | uint32_t Address, uint32_t Size) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 245 | LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address |
| 246 | << "\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 247 | Symbol *S; |
| 248 | bool WasInserted; |
| 249 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 250 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 251 | if (!File || File->kind() == InputFile::ObjectKind) |
| 252 | S->IsUsedInRegularObj = true; |
| 253 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 254 | if (WasInserted || S->isLazy()) { |
| 255 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
| 256 | return S; |
| 257 | } |
| 258 | |
| 259 | checkDataType(S, File); |
| 260 | |
| 261 | if (shouldReplace(S, File, Flags)) |
| 262 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 263 | return S; |
| 264 | } |
| 265 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 266 | Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 267 | InputFile *File, InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 268 | LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 269 | Symbol *S; |
| 270 | bool WasInserted; |
| 271 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 272 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 273 | if (!File || File->kind() == InputFile::ObjectKind) |
| 274 | S->IsUsedInRegularObj = true; |
| 275 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 276 | if (WasInserted || S->isLazy()) { |
| 277 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
| 278 | return S; |
| 279 | } |
| 280 | |
| 281 | checkGlobalType(S, File, &Global->getType()); |
| 282 | |
| 283 | if (shouldReplace(S, File, Flags)) |
| 284 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 285 | return S; |
| 286 | } |
| 287 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 288 | Symbol *SymbolTable::addUndefinedFunction(StringRef Name, uint32_t Flags, |
| 289 | InputFile *File, |
| 290 | const WasmSignature *Sig) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 291 | LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 292 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 293 | Symbol *S; |
| 294 | bool WasInserted; |
Sam Clegg | 20db381 | 2018-01-10 00:52:20 +0000 | [diff] [blame] | 295 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 296 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 297 | if (!File || File->kind() == InputFile::ObjectKind) |
| 298 | S->IsUsedInRegularObj = true; |
| 299 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 300 | if (WasInserted) |
| 301 | replaceSymbol<UndefinedFunction>(S, Name, Flags, File, Sig); |
| 302 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 303 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 304 | else if (S->isDefined()) |
| 305 | checkFunctionType(S, File, Sig); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 306 | return S; |
| 307 | } |
| 308 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 309 | Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags, |
| 310 | InputFile *File) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 311 | LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 312 | |
| 313 | Symbol *S; |
| 314 | bool WasInserted; |
| 315 | std::tie(S, WasInserted) = insert(Name); |
| 316 | |
| 317 | if (WasInserted) |
| 318 | replaceSymbol<UndefinedData>(S, Name, Flags, File); |
| 319 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 320 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 321 | else if (S->isDefined()) |
| 322 | checkDataType(S, File); |
| 323 | return S; |
| 324 | } |
| 325 | |
| 326 | Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, uint32_t Flags, |
| 327 | InputFile *File, |
| 328 | const WasmGlobalType *Type) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 329 | LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 330 | |
| 331 | Symbol *S; |
| 332 | bool WasInserted; |
| 333 | std::tie(S, WasInserted) = insert(Name); |
| 334 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 335 | if (!File || File->kind() == InputFile::ObjectKind) |
| 336 | S->IsUsedInRegularObj = true; |
| 337 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 338 | if (WasInserted) |
| 339 | replaceSymbol<UndefinedGlobal>(S, Name, Flags, File, Type); |
| 340 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 341 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 342 | else if (S->isDefined()) |
| 343 | checkGlobalType(S, File, Type); |
| 344 | return S; |
| 345 | } |
| 346 | |
| 347 | void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 348 | LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 349 | StringRef Name = Sym->getName(); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 350 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 351 | Symbol *S; |
| 352 | bool WasInserted; |
| 353 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 354 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 355 | if (WasInserted) { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 356 | replaceSymbol<LazySymbol>(S, Name, File, *Sym); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | |
| 360 | // If there is an existing undefined symbol, load a new one from the archive. |
| 361 | if (S->isUndefined()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 362 | LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 363 | File->addMember(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 366 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 367 | bool SymbolTable::addComdat(StringRef Name) { |
| 368 | return Comdats.insert(CachedHashStringRef(Name)).second; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 369 | } |