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 | ffd0aaf | 2018-06-22 15:13:10 +0000 | [diff] [blame] | 109 | static void checkFunctionType(const 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 | 65d6380 | 2018-05-14 23:01:16 +0000 | [diff] [blame] | 117 | const WasmSignature *OldSig = ExistingFunction->getFunctionType(); |
| 118 | if (OldSig && NewSig && *NewSig != *OldSig) { |
Sam Clegg | ffd0aaf | 2018-06-22 15:13:10 +0000 | [diff] [blame] | 119 | warn("function signature mismatch: " + Existing->getName() + |
| 120 | "\n>>> defined as " + toString(*OldSig) + " in " + |
| 121 | toString(Existing->getFile()) + "\n>>> defined as " + |
| 122 | toString(*NewSig) + " in " + toString(File)); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 126 | // Check the type of new symbol matches that of the symbol is replacing. |
| 127 | // For functions this can also involve verifying that the signatures match. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 128 | static void checkGlobalType(const Symbol *Existing, const InputFile *File, |
| 129 | const WasmGlobalType *NewType) { |
| 130 | if (!isa<GlobalSymbol>(Existing)) { |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 131 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 135 | const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType(); |
| 136 | if (*NewType != *OldType) { |
| 137 | error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " + |
| 138 | toString(*OldType) + " in " + toString(Existing->getFile()) + |
| 139 | "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 140 | } |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 143 | static void checkDataType(const Symbol *Existing, const InputFile *File) { |
| 144 | if (!isa<DataSymbol>(Existing)) |
Sam Clegg | 3876d89a | 2018-05-14 22:42:33 +0000 | [diff] [blame] | 145 | reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 148 | DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name, |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 149 | uint32_t Flags, |
| 150 | InputFunction *Function) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 151 | LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 152 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 153 | SyntheticFunctions.emplace_back(Function); |
| 154 | return replaceSymbol<DefinedFunction>(insert(Name).first, Name, Flags, |
| 155 | nullptr, Function); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 158 | DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name, |
| 159 | uint32_t Flags) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 160 | LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 161 | assert(!find(Name)); |
| 162 | return replaceSymbol<DefinedData>(insert(Name).first, Name, Flags); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 165 | DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags, |
| 166 | InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 167 | LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global |
| 168 | << "\n"); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 169 | assert(!find(Name)); |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 170 | SyntheticGlobals.emplace_back(Global); |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 171 | return replaceSymbol<DefinedGlobal>(insert(Name).first, Name, Flags, nullptr, |
| 172 | Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 175 | static bool shouldReplace(const Symbol *Existing, InputFile *NewFile, |
| 176 | uint32_t NewFlags) { |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 177 | // If existing symbol is undefined, replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 178 | if (!Existing->isDefined()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 179 | LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " |
| 180 | << Existing->getName() << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | |
| 184 | // Now we have two defined symbols. If the new one is weak, we can ignore it. |
| 185 | if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 186 | LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
| 190 | // If the existing symbol is weak, we should replace it. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 191 | if (Existing->isWeak()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 192 | LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
| 196 | // Neither symbol is week. They conflict. |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 197 | error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " + |
| 198 | toString(Existing->getFile()) + "\n>>> defined in " + |
| 199 | toString(NewFile)); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 200 | return true; |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 204 | InputFile *File, |
| 205 | InputFunction *Function) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 206 | LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 207 | Symbol *S; |
| 208 | bool WasInserted; |
| 209 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 210 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 211 | if (!File || File->kind() == InputFile::ObjectKind) |
| 212 | S->IsUsedInRegularObj = true; |
| 213 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 214 | if (WasInserted || S->isLazy()) { |
| 215 | replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function); |
| 216 | return S; |
| 217 | } |
| 218 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 219 | if (Function) |
| 220 | checkFunctionType(S, File, &Function->Signature); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 221 | |
| 222 | if (shouldReplace(S, File, Flags)) |
| 223 | replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 224 | return S; |
| 225 | } |
| 226 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 227 | Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 228 | InputFile *File, InputSegment *Segment, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 229 | uint32_t Address, uint32_t Size) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 230 | LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address |
| 231 | << "\n"); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 232 | Symbol *S; |
| 233 | bool WasInserted; |
| 234 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 235 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 236 | if (!File || File->kind() == InputFile::ObjectKind) |
| 237 | S->IsUsedInRegularObj = true; |
| 238 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 239 | if (WasInserted || S->isLazy()) { |
| 240 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
| 241 | return S; |
| 242 | } |
| 243 | |
| 244 | checkDataType(S, File); |
| 245 | |
| 246 | if (shouldReplace(S, File, Flags)) |
| 247 | replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 248 | return S; |
| 249 | } |
| 250 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 251 | Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags, |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 252 | InputFile *File, InputGlobal *Global) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 253 | LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 254 | Symbol *S; |
| 255 | bool WasInserted; |
| 256 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 257 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 258 | if (!File || File->kind() == InputFile::ObjectKind) |
| 259 | S->IsUsedInRegularObj = true; |
| 260 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 261 | if (WasInserted || S->isLazy()) { |
| 262 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
| 263 | return S; |
| 264 | } |
| 265 | |
| 266 | checkGlobalType(S, File, &Global->getType()); |
| 267 | |
| 268 | if (shouldReplace(S, File, Flags)) |
| 269 | replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 270 | return S; |
| 271 | } |
| 272 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 273 | Symbol *SymbolTable::addUndefinedFunction(StringRef Name, uint32_t Flags, |
| 274 | InputFile *File, |
| 275 | const WasmSignature *Sig) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 276 | LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 277 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 278 | Symbol *S; |
| 279 | bool WasInserted; |
Sam Clegg | 20db381 | 2018-01-10 00:52:20 +0000 | [diff] [blame] | 280 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 281 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 282 | if (!File || File->kind() == InputFile::ObjectKind) |
| 283 | S->IsUsedInRegularObj = true; |
| 284 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 285 | if (WasInserted) |
| 286 | replaceSymbol<UndefinedFunction>(S, Name, Flags, File, Sig); |
| 287 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 288 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 289 | else if (S->isDefined()) |
| 290 | checkFunctionType(S, File, Sig); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 291 | return S; |
| 292 | } |
| 293 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 294 | Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags, |
| 295 | InputFile *File) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 296 | LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 297 | |
| 298 | Symbol *S; |
| 299 | bool WasInserted; |
| 300 | std::tie(S, WasInserted) = insert(Name); |
| 301 | |
| 302 | if (WasInserted) |
| 303 | replaceSymbol<UndefinedData>(S, Name, Flags, File); |
| 304 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 305 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 306 | else if (S->isDefined()) |
| 307 | checkDataType(S, File); |
| 308 | return S; |
| 309 | } |
| 310 | |
| 311 | Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, uint32_t Flags, |
| 312 | InputFile *File, |
| 313 | const WasmGlobalType *Type) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 314 | LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 315 | |
| 316 | Symbol *S; |
| 317 | bool WasInserted; |
| 318 | std::tie(S, WasInserted) = insert(Name); |
| 319 | |
Sam Clegg | c729c1b | 2018-05-30 18:07:52 +0000 | [diff] [blame] | 320 | if (!File || File->kind() == InputFile::ObjectKind) |
| 321 | S->IsUsedInRegularObj = true; |
| 322 | |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 323 | if (WasInserted) |
| 324 | replaceSymbol<UndefinedGlobal>(S, Name, Flags, File, Type); |
| 325 | else if (auto *Lazy = dyn_cast<LazySymbol>(S)) |
Rui Ueyama | b961abc | 2018-02-28 22:51:51 +0000 | [diff] [blame] | 326 | Lazy->fetch(); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 327 | else if (S->isDefined()) |
| 328 | checkGlobalType(S, File, Type); |
| 329 | return S; |
| 330 | } |
| 331 | |
| 332 | void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 333 | LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 334 | StringRef Name = Sym->getName(); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 335 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 336 | Symbol *S; |
| 337 | bool WasInserted; |
| 338 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 339 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 340 | if (WasInserted) { |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 341 | replaceSymbol<LazySymbol>(S, Name, File, *Sym); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | // If there is an existing undefined symbol, load a new one from the archive. |
| 346 | if (S->isUndefined()) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 347 | LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); |
Rui Ueyama | e3498ec | 2018-02-28 00:09:22 +0000 | [diff] [blame] | 348 | File->addMember(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 351 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 352 | bool SymbolTable::addComdat(StringRef Name) { |
| 353 | return Comdats.insert(CachedHashStringRef(Name)).second; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 354 | } |