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 | |
| 32 | if (auto *F = dyn_cast<ObjFile>(File)) |
| 33 | ObjectFiles.push_back(F); |
| 34 | } |
| 35 | |
| 36 | void SymbolTable::reportRemainingUndefines() { |
Rui Ueyama | 7d67dd1 | 2018-02-13 22:30:52 +0000 | [diff] [blame] | 37 | SetVector<Symbol *> Undefs; |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 38 | for (Symbol *Sym : SymVector) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 39 | if (Sym->isUndefined() && !Sym->isWeak() && |
| 40 | Config->AllowUndefinedSymbols.count(Sym->getName()) == 0) { |
| 41 | Undefs.insert(Sym); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (Undefs.empty()) |
| 46 | return; |
| 47 | |
| 48 | for (ObjFile *File : ObjectFiles) |
| 49 | for (Symbol *Sym : File->getSymbols()) |
| 50 | if (Undefs.count(Sym)) |
| 51 | error(toString(File) + ": undefined symbol: " + toString(*Sym)); |
| 52 | |
| 53 | for (Symbol *Sym : Undefs) |
| 54 | if (!Sym->getFile()) |
| 55 | error("undefined symbol: " + toString(*Sym)); |
| 56 | } |
| 57 | |
| 58 | Symbol *SymbolTable::find(StringRef Name) { |
Sam Clegg | a80d94d | 2017-11-27 23:16:06 +0000 | [diff] [blame] | 59 | auto It = SymMap.find(CachedHashStringRef(Name)); |
| 60 | if (It == SymMap.end()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 61 | return nullptr; |
| 62 | return It->second; |
| 63 | } |
| 64 | |
| 65 | std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) { |
Sam Clegg | a80d94d | 2017-11-27 23:16:06 +0000 | [diff] [blame] | 66 | Symbol *&Sym = SymMap[CachedHashStringRef(Name)]; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 67 | if (Sym) |
| 68 | return {Sym, false}; |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 69 | Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); |
Sam Clegg | 74fe0ba | 2017-12-07 01:51:24 +0000 | [diff] [blame] | 70 | SymVector.emplace_back(Sym); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 71 | return {Sym, true}; |
| 72 | } |
| 73 | |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 74 | // Check the type of new symbol matches that of the symbol is replacing. |
| 75 | // For functions this can also involve verifying that the signatures match. |
| 76 | static void checkSymbolTypes(const Symbol &Existing, const InputFile &F, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 77 | WasmSymbolType NewType, |
| 78 | const WasmSignature *NewFunctionSig, |
| 79 | const WasmGlobalType *NewGlobalType) { |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 80 | if (Existing.isLazy()) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 81 | return; |
| 82 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 83 | WasmSymbolType ExistingType = Existing.getWasmType(); |
| 84 | |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 85 | // First check the symbol types match (i.e. either both are function |
| 86 | // symbols or both are data symbols). |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 87 | if (NewType != ExistingType) { |
Sam Clegg | 20db381 | 2018-01-10 00:52:20 +0000 | [diff] [blame] | 88 | error("symbol type mismatch: " + Existing.getName() + "\n>>> defined as " + |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 89 | toString(ExistingType) + " in " + toString(Existing.getFile()) + |
| 90 | "\n>>> defined as " + toString(NewType) + " in " + F.getName()); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 94 | // For function/global symbols, optionally check the type matches too. |
| 95 | if (NewType == WASM_SYMBOL_TYPE_DATA || !Config->CheckSignatures) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 96 | return; |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 97 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 98 | DEBUG(dbgs() << "checkSymbolTypes: " << Existing.getName() << "\n"); |
Sam Clegg | 3f8db98 | 2018-02-16 23:50:23 +0000 | [diff] [blame] | 99 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 100 | auto ReportError = [&](const Twine &Old, const Twine &New) { |
| 101 | error(toString(NewType) + " type mismatch: " + Existing.getName() + |
| 102 | "\n>>> defined as " + Old + " in " + toString(Existing.getFile()) + |
| 103 | "\n>>> defined as " + New + " in " + F.getName()); |
| 104 | }; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 105 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 106 | if (NewType == WASM_SYMBOL_TYPE_FUNCTION) { |
| 107 | // Skip the signature check if the existing function has no signature (e.g. |
| 108 | // if it is an undefined symbol generated by --undefined command line flag). |
| 109 | auto &Sym = cast<FunctionSymbol>(Existing); |
| 110 | const WasmSignature *OldSig = Sym.getFunctionType(); |
| 111 | if (!OldSig) |
| 112 | return; |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 113 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 114 | assert(NewFunctionSig); |
| 115 | if (*NewFunctionSig == *OldSig) |
| 116 | return; |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 117 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 118 | ReportError(toString(*OldSig), toString(*NewFunctionSig)); |
| 119 | } else { |
| 120 | auto &Sym = cast<GlobalSymbol>(Existing); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 121 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 122 | assert(NewGlobalType != nullptr); |
| 123 | const WasmGlobalType *OldType = Sym.getGlobalType(); |
| 124 | if (*NewGlobalType == *OldType) |
| 125 | return; |
| 126 | |
| 127 | ReportError(toString(*OldType), toString(*NewGlobalType)); |
| 128 | } |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 131 | DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name, |
| 132 | const WasmSignature *Type, |
| 133 | uint32_t Flags) { |
Sam Clegg | a189230 | 2018-02-13 20:14:26 +0000 | [diff] [blame] | 134 | DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n"); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 135 | Symbol *S; |
| 136 | bool WasInserted; |
| 137 | std::tie(S, WasInserted) = insert(Name); |
Sam Clegg | a189230 | 2018-02-13 20:14:26 +0000 | [diff] [blame] | 138 | assert(WasInserted); |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 139 | return replaceSymbol<DefinedFunction>(S, Name, Flags, Type); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 142 | DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name, |
| 143 | uint32_t Flags) { |
| 144 | DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 145 | Symbol *S; |
| 146 | bool WasInserted; |
| 147 | std::tie(S, WasInserted) = insert(Name); |
Sam Clegg | a189230 | 2018-02-13 20:14:26 +0000 | [diff] [blame] | 148 | assert(WasInserted); |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 149 | return replaceSymbol<DefinedData>(S, Name, Flags); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 152 | DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags, |
| 153 | InputGlobal *Global) { |
| 154 | DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global << "\n"); |
| 155 | Symbol *S; |
| 156 | bool WasInserted; |
| 157 | std::tie(S, WasInserted) = insert(Name); |
| 158 | assert(WasInserted); |
| 159 | return replaceSymbol<DefinedGlobal>(S, Name, Flags, nullptr, Global); |
| 160 | } |
| 161 | |
Sam Clegg | f60b0d5 | 2018-02-20 18:55:07 +0000 | [diff] [blame] | 162 | static bool shouldReplace(const Symbol &Existing, InputFile *NewFile, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 163 | WasmSymbolType NewType, uint32_t NewFlags, |
| 164 | const WasmSignature *NewFuncType = nullptr, |
| 165 | const WasmGlobalType *NewGlobalType = nullptr) { |
| 166 | |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 167 | // If existing symbol is lazy, replace it without checking types since |
| 168 | // lazy symbols don't have any type information. |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 169 | if (Existing.isLazy()) { |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 170 | DEBUG(dbgs() << "replacing existing lazy symbol: " << Existing.getName() |
| 171 | << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 172 | return true; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 173 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 174 | |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 175 | // Now we have two wasm symbols, and all wasm symbols that have the same |
| 176 | // symbol name must have the same type, even if they are undefined. This |
| 177 | // is different from ELF because symbol types are not that significant |
| 178 | // in ELF, and undefined symbols in ELF don't have type in the first place. |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 179 | checkSymbolTypes(Existing, *NewFile, NewType, NewFuncType, NewGlobalType); |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 180 | |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 181 | // If existing symbol is undefined, replace it. |
| 182 | if (!Existing.isDefined()) { |
| 183 | DEBUG(dbgs() << "resolving existing undefined symbol: " |
| 184 | << Existing.getName() << "\n"); |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | // Now we have two defined symbols. If the new one is weak, we can ignore it. |
| 189 | if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { |
| 190 | DEBUG(dbgs() << "existing symbol takes precedence\n"); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // If the existing symbol is weak, we should replace it. |
| 195 | if (Existing.isWeak()) { |
| 196 | DEBUG(dbgs() << "replacing existing weak symbol\n"); |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | // Neither symbol is week. They conflict. |
| 201 | error("duplicate symbol: " + toString(Existing) + "\n>>> defined in " + |
| 202 | toString(Existing.getFile()) + "\n>>> defined in " + toString(NewFile)); |
| 203 | return true; |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags, |
| 207 | InputFile *F, InputFunction *Function) { |
| 208 | DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n"); |
| 209 | Symbol *S; |
| 210 | bool WasInserted; |
| 211 | std::tie(S, WasInserted) = insert(Name); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 212 | if (WasInserted || shouldReplace(*S, F, WASM_SYMBOL_TYPE_FUNCTION, Flags, |
| 213 | &Function->Signature)) |
Sam Clegg | 93e559b | 2018-02-20 18:55:06 +0000 | [diff] [blame] | 214 | replaceSymbol<DefinedFunction>(S, Name, Flags, F, Function); |
| 215 | return S; |
| 216 | } |
| 217 | |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 218 | Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 219 | InputFile *F, InputSegment *Segment, |
| 220 | uint32_t Address, uint32_t Size) { |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 221 | DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address << "\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); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 225 | if (WasInserted || shouldReplace(*S, F, WASM_SYMBOL_TYPE_DATA, Flags)) |
| 226 | replaceSymbol<DefinedData>(S, Name, Flags, F, Segment, Address, Size); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 227 | return S; |
| 228 | } |
| 229 | |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 230 | Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags, |
| 231 | InputFile *F, InputGlobal *Global) { |
| 232 | DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n"); |
| 233 | Symbol *S; |
| 234 | bool WasInserted; |
| 235 | std::tie(S, WasInserted) = insert(Name); |
| 236 | if (WasInserted || shouldReplace(*S, F, WASM_SYMBOL_TYPE_GLOBAL, Flags, |
| 237 | nullptr, &Global->getType())) |
| 238 | replaceSymbol<DefinedGlobal>(S, Name, Flags, F, Global); |
| 239 | return S; |
| 240 | } |
| 241 | |
| 242 | Symbol *SymbolTable::addUndefined(StringRef Name, WasmSymbolType Type, |
Sam Clegg | 20db381 | 2018-01-10 00:52:20 +0000 | [diff] [blame] | 243 | uint32_t Flags, InputFile *F, |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 244 | const WasmSignature *FunctionType, |
| 245 | const WasmGlobalType *GlobalType) { |
| 246 | DEBUG(dbgs() << "addUndefined type=" << Type << ": " << Name << "\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 247 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 248 | Symbol *S; |
| 249 | bool WasInserted; |
Sam Clegg | 20db381 | 2018-01-10 00:52:20 +0000 | [diff] [blame] | 250 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 251 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 252 | if (WasInserted) { |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 253 | switch (Type) { |
| 254 | case WASM_SYMBOL_TYPE_FUNCTION: |
| 255 | replaceSymbol<UndefinedFunction>(S, Name, Flags, F, FunctionType); |
| 256 | break; |
| 257 | case WASM_SYMBOL_TYPE_GLOBAL: |
| 258 | replaceSymbol<UndefinedGlobal>(S, Name, Flags, F, GlobalType); |
| 259 | break; |
| 260 | case WASM_SYMBOL_TYPE_DATA: |
Sam Clegg | 0024553 | 2018-02-20 23:38:27 +0000 | [diff] [blame] | 261 | replaceSymbol<UndefinedData>(S, Name, Flags, F); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 262 | break; |
| 263 | } |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 264 | return S; |
| 265 | } |
| 266 | |
| 267 | if (auto *Lazy = dyn_cast<LazySymbol>(S)) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 268 | DEBUG(dbgs() << "resolved by existing lazy\n"); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 269 | cast<ArchiveFile>(Lazy->getFile())->addMember(&Lazy->getArchiveSymbol()); |
| 270 | return S; |
| 271 | } |
| 272 | |
| 273 | if (S->isDefined()) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 274 | DEBUG(dbgs() << "resolved by existing\n"); |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 275 | checkSymbolTypes(*S, *F, Type, FunctionType, GlobalType); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 276 | } |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame^] | 277 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 278 | return S; |
| 279 | } |
| 280 | |
| 281 | void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol *Sym) { |
Sam Clegg | a681a11 | 2017-12-06 03:10:39 +0000 | [diff] [blame] | 282 | DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 283 | StringRef Name = Sym->getName(); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 284 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 285 | Symbol *S; |
| 286 | bool WasInserted; |
| 287 | std::tie(S, WasInserted) = insert(Name); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 288 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 289 | if (WasInserted) { |
Sam Clegg | dfb0b2c | 2018-02-14 18:27:59 +0000 | [diff] [blame] | 290 | replaceSymbol<LazySymbol>(S, Name, F, *Sym); |
Rui Ueyama | c03c904 | 2018-02-20 21:08:47 +0000 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | |
| 294 | // If there is an existing undefined symbol, load a new one from the archive. |
| 295 | if (S->isUndefined()) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 296 | DEBUG(dbgs() << "replacing existing undefined\n"); |
| 297 | F->addMember(Sym); |
| 298 | } |
| 299 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 300 | |
| 301 | bool SymbolTable::addComdat(StringRef Name, ObjFile *F) { |
| 302 | DEBUG(dbgs() << "addComdat: " << Name << "\n"); |
| 303 | ObjFile *&File = ComdatMap[CachedHashStringRef(Name)]; |
| 304 | if (File) { |
| 305 | DEBUG(dbgs() << "COMDAT already defined\n"); |
| 306 | return false; |
| 307 | } |
| 308 | File = F; |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | ObjFile *SymbolTable::findComdat(StringRef Name) const { |
| 313 | auto It = ComdatMap.find(CachedHashStringRef(Name)); |
| 314 | return It == ComdatMap.end() ? nullptr : It->second; |
| 315 | } |