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" 11 12 #include "Config.h" 13 #include "InputChunks.h" 14 #include "WriterUtils.h" 15 #include "lld/Common/ErrorHandler.h" 16 #include "lld/Common/Memory.h" 17 18 #include <unordered_set> 19 20 #define DEBUG_TYPE "lld" 21 22 using namespace llvm; 23 using namespace llvm::wasm; 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)); 31 File->parse(); 32 33 if (auto *F = dyn_cast<ObjFile>(File)) 34 ObjectFiles.push_back(F); 35 } 36 37 void SymbolTable::reportRemainingUndefines() { 38 std::unordered_set<Symbol *> Undefs; 39 for (Symbol *Sym : SymVector) { 40 if (Sym->isUndefined() && !Sym->isWeak() && 41 Config->AllowUndefinedSymbols.count(Sym->getName()) == 0) { 42 Undefs.insert(Sym); 43 } 44 } 45 46 if (Undefs.empty()) 47 return; 48 49 for (ObjFile *File : ObjectFiles) 50 for (Symbol *Sym : File->getSymbols()) 51 if (Undefs.count(Sym)) 52 error(toString(File) + ": undefined symbol: " + toString(*Sym)); 53 54 for (Symbol *Sym : Undefs) 55 if (!Sym->getFile()) 56 error("undefined symbol: " + toString(*Sym)); 57 } 58 59 Symbol *SymbolTable::find(StringRef Name) { 60 auto It = SymMap.find(CachedHashStringRef(Name)); 61 if (It == SymMap.end()) 62 return nullptr; 63 return It->second; 64 } 65 66 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) { 67 Symbol *&Sym = SymMap[CachedHashStringRef(Name)]; 68 if (Sym) 69 return {Sym, false}; 70 Sym = make<Symbol>(Name, false); 71 SymVector.emplace_back(Sym); 72 return {Sym, true}; 73 } 74 75 void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) { 76 error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " + 77 toString(Existing->getFile()) + "\n>>> defined in " + 78 toString(NewFile)); 79 } 80 81 // Check the type of new symbol matches that of the symbol is replacing. 82 // For functions this can also involve verifying that the signatures match. 83 static void checkSymbolTypes(const Symbol &Existing, const InputFile &F, 84 Symbol::Kind Kind, const WasmSignature *NewSig) { 85 if (Existing.isLazy()) 86 return; 87 88 bool NewIsFunction = Kind == Symbol::Kind::UndefinedFunctionKind || 89 Kind == Symbol::Kind::DefinedFunctionKind; 90 91 // First check the symbol types match (i.e. either both are function 92 // symbols or both are data symbols). 93 if (Existing.isFunction() != NewIsFunction) { 94 error("symbol type mismatch: " + Existing.getName() + "\n>>> defined as " + 95 (Existing.isFunction() ? "Function" : "Global") + " in " + 96 toString(Existing.getFile()) + "\n>>> defined as " + 97 (NewIsFunction ? "Function" : "Global") + " in " + F.getName()); 98 return; 99 } 100 101 // For function symbols, optionally check the function signature matches too. 102 if (!NewIsFunction || !Config->CheckSignatures) 103 return; 104 // Skip the signature check if the existing function has no signature (e.g. 105 // if it is an undefined symbol generated by --undefined command line flag). 106 if (!Existing.hasFunctionType()) 107 return; 108 109 DEBUG(dbgs() << "checkSymbolTypes: " << Existing.getName() << "\n"); 110 assert(NewSig); 111 112 const WasmSignature &OldSig = Existing.getFunctionType(); 113 if (*NewSig == OldSig) 114 return; 115 116 error("function signature mismatch: " + Existing.getName() + 117 "\n>>> defined as " + toString(OldSig) + " in " + 118 toString(Existing.getFile()) + "\n>>> defined as " + toString(*NewSig) + 119 " in " + F.getName()); 120 } 121 122 Symbol *SymbolTable::addDefinedFunction(StringRef Name, 123 const WasmSignature *Type, 124 uint32_t Flags) { 125 DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n"); 126 Symbol *S; 127 bool WasInserted; 128 std::tie(S, WasInserted) = insert(Name); 129 if (WasInserted) { 130 S->update(Symbol::DefinedFunctionKind, nullptr, Flags); 131 S->setFunctionType(Type); 132 } else if (!S->isFunction()) { 133 error("symbol type mismatch: " + Name); 134 } 135 return S; 136 } 137 138 Symbol *SymbolTable::addDefinedGlobal(StringRef Name) { 139 DEBUG(dbgs() << "addDefinedGlobal: " << Name << "\n"); 140 Symbol *S; 141 bool WasInserted; 142 std::tie(S, WasInserted) = insert(Name); 143 if (WasInserted) 144 S->update(Symbol::DefinedGlobalKind); 145 else if (!S->isGlobal()) 146 error("symbol type mismatch: " + Name); 147 return S; 148 } 149 150 Symbol *SymbolTable::addDefined(StringRef Name, Symbol::Kind Kind, 151 uint32_t Flags, InputFile *F, 152 const InputSegment *Segment, 153 InputFunction *Function, uint32_t Address) { 154 DEBUG(dbgs() << "addDefined: " << Name << " addr:" << Address << "\n"); 155 Symbol *S; 156 bool WasInserted; 157 158 std::tie(S, WasInserted) = insert(Name); 159 if (WasInserted) { 160 S->update(Kind, F, Flags, Segment, Function, Address); 161 } else if (S->isLazy()) { 162 // The existing symbol is lazy. Replace it without checking types since 163 // lazy symbols don't have any type information. 164 DEBUG(dbgs() << "replacing existing lazy symbol: " << Name << "\n"); 165 S->update(Kind, F, Flags, Segment, Function, Address); 166 } else if (!S->isDefined()) { 167 // The existing symbol table entry is undefined. The new symbol replaces 168 // it, after checking the type matches 169 DEBUG(dbgs() << "resolving existing undefined symbol: " << Name << "\n"); 170 checkSymbolTypes(*S, *F, Kind, Function ? &Function->Signature : nullptr); 171 S->update(Kind, F, Flags, Segment, Function, Address); 172 } else if ((Flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { 173 // the new symbol is weak we can ignore it 174 DEBUG(dbgs() << "existing symbol takes precedence\n"); 175 } else if (S->isWeak()) { 176 // the new symbol is not weak and the existing symbol is, so we replace 177 // it 178 DEBUG(dbgs() << "replacing existing weak symbol\n"); 179 checkSymbolTypes(*S, *F, Kind, Function ? &Function->Signature : nullptr); 180 S->update(Kind, F, Flags, Segment, Function, Address); 181 } else { 182 // neither symbol is week. They conflict. 183 reportDuplicate(S, F); 184 } 185 return S; 186 } 187 188 Symbol *SymbolTable::addUndefinedFunction(StringRef Name, 189 const WasmSignature *Type) { 190 Symbol *S; 191 bool WasInserted; 192 std::tie(S, WasInserted) = insert(Name); 193 if (WasInserted) { 194 S->update(Symbol::UndefinedFunctionKind); 195 S->setFunctionType(Type); 196 } else if (!S->isFunction()) { 197 error("symbol type mismatch: " + Name); 198 } 199 return S; 200 } 201 202 Symbol *SymbolTable::addUndefined(StringRef Name, Symbol::Kind Kind, 203 uint32_t Flags, InputFile *F, 204 const WasmSignature *Type) { 205 DEBUG(dbgs() << "addUndefined: " << Name << "\n"); 206 Symbol *S; 207 bool WasInserted; 208 std::tie(S, WasInserted) = insert(Name); 209 if (WasInserted) { 210 S->update(Kind, F, Flags); 211 if (Type) 212 S->setFunctionType(Type); 213 } else if (S->isLazy()) { 214 DEBUG(dbgs() << "resolved by existing lazy\n"); 215 auto *AF = cast<ArchiveFile>(S->getFile()); 216 AF->addMember(&S->getArchiveSymbol()); 217 } else if (S->isDefined()) { 218 DEBUG(dbgs() << "resolved by existing\n"); 219 checkSymbolTypes(*S, *F, Kind, Type); 220 } 221 return S; 222 } 223 224 void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol *Sym) { 225 DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); 226 StringRef Name = Sym->getName(); 227 Symbol *S; 228 bool WasInserted; 229 std::tie(S, WasInserted) = insert(Name); 230 if (WasInserted) { 231 S->update(Symbol::LazyKind, F); 232 S->setArchiveSymbol(*Sym); 233 } else if (S->isUndefined()) { 234 // There is an existing undefined symbol. The can load from the 235 // archive. 236 DEBUG(dbgs() << "replacing existing undefined\n"); 237 F->addMember(Sym); 238 } 239 } 240 241 bool SymbolTable::addComdat(StringRef Name, ObjFile *F) { 242 DEBUG(dbgs() << "addComdat: " << Name << "\n"); 243 ObjFile *&File = ComdatMap[CachedHashStringRef(Name)]; 244 if (File) { 245 DEBUG(dbgs() << "COMDAT already defined\n"); 246 return false; 247 } 248 File = F; 249 return true; 250 } 251 252 ObjFile *SymbolTable::findComdat(StringRef Name) const { 253 auto It = ComdatMap.find(CachedHashStringRef(Name)); 254 return It == ComdatMap.end() ? nullptr : It->second; 255 } 256