1c94d393aSSam Clegg //===- SymbolTable.cpp ----------------------------------------------------===// 2c94d393aSSam Clegg // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6c94d393aSSam Clegg // 7c94d393aSSam Clegg //===----------------------------------------------------------------------===// 8c94d393aSSam Clegg 9c94d393aSSam Clegg #include "SymbolTable.h" 10c94d393aSSam Clegg #include "Config.h" 115fa274beSSam Clegg #include "InputChunks.h" 12e915a71fSHeejin Ahn #include "InputEvent.h" 1393102974SSam Clegg #include "InputGlobal.h" 14b8621596SSam Clegg #include "WriterUtils.h" 15c94d393aSSam Clegg #include "lld/Common/ErrorHandler.h" 162017d52bSRui Ueyama #include "lld/Common/Memory.h" 177d67dd11SRui Ueyama #include "llvm/ADT/SetVector.h" 18c94d393aSSam Clegg 19c94d393aSSam Clegg #define DEBUG_TYPE "lld" 20c94d393aSSam Clegg 21c94d393aSSam Clegg using namespace llvm; 2220db381bSSam Clegg using namespace llvm::wasm; 2345218f4aSSam Clegg using namespace llvm::object; 24c94d393aSSam Clegg using namespace lld; 25c94d393aSSam Clegg using namespace lld::wasm; 26c94d393aSSam Clegg 27c94d393aSSam Clegg SymbolTable *lld::wasm::Symtab; 28c94d393aSSam Clegg 29c94d393aSSam Clegg void SymbolTable::addFile(InputFile *File) { 30c94d393aSSam Clegg log("Processing: " + toString(File)); 311f3f774fSSam Clegg if (Config->Trace) 321f3f774fSSam Clegg message(toString(File)); 33c94d393aSSam Clegg File->parse(); 34c94d393aSSam Clegg 35c729c1b4SSam Clegg // LLVM bitcode file 36c729c1b4SSam Clegg if (auto *F = dyn_cast<BitcodeFile>(File)) 37c729c1b4SSam Clegg BitcodeFiles.push_back(F); 38c729c1b4SSam Clegg else if (auto *F = dyn_cast<ObjFile>(File)) 39c94d393aSSam Clegg ObjectFiles.push_back(F); 40c94d393aSSam Clegg } 41c94d393aSSam Clegg 42c729c1b4SSam Clegg // This function is where all the optimizations of link-time 43c729c1b4SSam Clegg // optimization happens. When LTO is in use, some input files are 44c729c1b4SSam Clegg // not in native object file format but in the LLVM bitcode format. 45c729c1b4SSam Clegg // This function compiles bitcode files into a few big native files 46c729c1b4SSam Clegg // using LLVM functions and replaces bitcode symbols with the results. 47c729c1b4SSam Clegg // Because all bitcode files that the program consists of are passed 48c729c1b4SSam Clegg // to the compiler at once, it can do whole-program optimization. 49c729c1b4SSam Clegg void SymbolTable::addCombinedLTOObject() { 50c729c1b4SSam Clegg if (BitcodeFiles.empty()) 51c729c1b4SSam Clegg return; 52c729c1b4SSam Clegg 53c729c1b4SSam Clegg // Compile bitcode files and replace bitcode symbols. 54c729c1b4SSam Clegg LTO.reset(new BitcodeCompiler); 55c729c1b4SSam Clegg for (BitcodeFile *F : BitcodeFiles) 56c729c1b4SSam Clegg LTO->add(*F); 57c729c1b4SSam Clegg 58c729c1b4SSam Clegg for (StringRef Filename : LTO->compile()) { 59c729c1b4SSam Clegg auto *Obj = make<ObjFile>(MemoryBufferRef(Filename, "lto.tmp")); 60c729c1b4SSam Clegg Obj->parse(); 61c729c1b4SSam Clegg ObjectFiles.push_back(Obj); 62c729c1b4SSam Clegg } 63c729c1b4SSam Clegg } 64c729c1b4SSam Clegg 65c94d393aSSam Clegg void SymbolTable::reportRemainingUndefines() { 6674fe0ba1SSam Clegg for (Symbol *Sym : SymVector) { 67c729c1b4SSam Clegg if (!Sym->isUndefined() || Sym->isWeak()) 68c729c1b4SSam Clegg continue; 69c729c1b4SSam Clegg if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0) 70c729c1b4SSam Clegg continue; 71c729c1b4SSam Clegg if (!Sym->IsUsedInRegularObj) 72c729c1b4SSam Clegg continue; 7347e2b6b2SSam Clegg error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym)); 74c94d393aSSam Clegg } 75c94d393aSSam Clegg } 76c94d393aSSam Clegg 77c94d393aSSam Clegg Symbol *SymbolTable::find(StringRef Name) { 781f3f774fSSam Clegg auto It = SymMap.find(CachedHashStringRef(Name)); 791f3f774fSSam Clegg if (It == SymMap.end() || It->second == -1) 801f3f774fSSam Clegg return nullptr; 811f3f774fSSam Clegg return SymVector[It->second]; 821f3f774fSSam Clegg } 831f3f774fSSam Clegg 846540e570SSam Clegg void SymbolTable::replace(StringRef Name, Symbol* Sym) { 856540e570SSam Clegg auto It = SymMap.find(CachedHashStringRef(Name)); 866540e570SSam Clegg SymVector[It->second] = Sym; 876540e570SSam Clegg } 886540e570SSam Clegg 891f3f774fSSam Clegg std::pair<Symbol *, bool> SymbolTable::insertName(StringRef Name) { 901f3f774fSSam Clegg bool Trace = false; 911f3f774fSSam Clegg auto P = SymMap.insert({CachedHashStringRef(Name), (int)SymVector.size()}); 921f3f774fSSam Clegg int &SymIndex = P.first->second; 931f3f774fSSam Clegg bool IsNew = P.second; 941f3f774fSSam Clegg if (SymIndex == -1) { 951f3f774fSSam Clegg SymIndex = SymVector.size(); 961f3f774fSSam Clegg Trace = true; 971f3f774fSSam Clegg IsNew = true; 981f3f774fSSam Clegg } 991f3f774fSSam Clegg 1001f3f774fSSam Clegg if (!IsNew) 1011f3f774fSSam Clegg return {SymVector[SymIndex], false}; 1021f3f774fSSam Clegg 1031f3f774fSSam Clegg Symbol *Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 1041f3f774fSSam Clegg Sym->IsUsedInRegularObj = false; 1051f3f774fSSam Clegg Sym->Traced = Trace; 1061f3f774fSSam Clegg SymVector.emplace_back(Sym); 1071f3f774fSSam Clegg return {Sym, true}; 108c94d393aSSam Clegg } 109c94d393aSSam Clegg 110230dc11dSSam Clegg std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, 111230dc11dSSam Clegg const InputFile *File) { 1121f3f774fSSam Clegg Symbol *S; 1131f3f774fSSam Clegg bool WasInserted; 1141f3f774fSSam Clegg std::tie(S, WasInserted) = insertName(Name); 1151f3f774fSSam Clegg 1164c2cbfe6SSam Clegg if (!File || File->kind() == InputFile::ObjectKind) 1171f3f774fSSam Clegg S->IsUsedInRegularObj = true; 1181f3f774fSSam Clegg 1191f3f774fSSam Clegg return {S, WasInserted}; 120c94d393aSSam Clegg } 121c94d393aSSam Clegg 122e3498ec5SRui Ueyama static void reportTypeError(const Symbol *Existing, const InputFile *File, 1233876d89aSSam Clegg llvm::wasm::WasmSymbolType Type) { 124e3498ec5SRui Ueyama error("symbol type mismatch: " + toString(*Existing) + "\n>>> defined as " + 125e3498ec5SRui Ueyama toString(Existing->getWasmType()) + " in " + 1263876d89aSSam Clegg toString(Existing->getFile()) + "\n>>> defined as " + toString(Type) + 1273876d89aSSam Clegg " in " + toString(File)); 128e3498ec5SRui Ueyama } 129e3498ec5SRui Ueyama 1306f4286fbSHeejin Ahn // Check the type of new symbol matches that of the symbol is replacing. 1316540e570SSam Clegg // Returns true if the function types match, false is there is a singature 1326540e570SSam Clegg // mismatch. 1336540e570SSam Clegg bool signatureMatches(FunctionSymbol *Existing, const WasmSignature *NewSig) { 134cefbf9acSSam Clegg if (!NewSig) 1356540e570SSam Clegg return true; 136cefbf9acSSam Clegg 1376540e570SSam Clegg const WasmSignature *OldSig = Existing->Signature; 138cefbf9acSSam Clegg if (!OldSig) { 1396540e570SSam Clegg Existing->Signature = NewSig; 1406540e570SSam Clegg return true; 141cefbf9acSSam Clegg } 142cefbf9acSSam Clegg 1436540e570SSam Clegg return *NewSig == *OldSig; 144e3498ec5SRui Ueyama } 145e3498ec5SRui Ueyama 146e3498ec5SRui Ueyama static void checkGlobalType(const Symbol *Existing, const InputFile *File, 147e3498ec5SRui Ueyama const WasmGlobalType *NewType) { 148e3498ec5SRui Ueyama if (!isa<GlobalSymbol>(Existing)) { 1493876d89aSSam Clegg reportTypeError(Existing, File, WASM_SYMBOL_TYPE_GLOBAL); 150b8621596SSam Clegg return; 151b8621596SSam Clegg } 152b8621596SSam Clegg 153e3498ec5SRui Ueyama const WasmGlobalType *OldType = cast<GlobalSymbol>(Existing)->getGlobalType(); 154e3498ec5SRui Ueyama if (*NewType != *OldType) { 155e3498ec5SRui Ueyama error("Global type mismatch: " + Existing->getName() + "\n>>> defined as " + 156e3498ec5SRui Ueyama toString(*OldType) + " in " + toString(Existing->getFile()) + 157e3498ec5SRui Ueyama "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); 158c94d393aSSam Clegg } 15924b3dcd4SSam Clegg } 16024b3dcd4SSam Clegg 161e915a71fSHeejin Ahn static void checkEventType(const Symbol *Existing, const InputFile *File, 162e915a71fSHeejin Ahn const WasmEventType *NewType, 163e915a71fSHeejin Ahn const WasmSignature *NewSig) { 164e915a71fSHeejin Ahn auto ExistingEvent = dyn_cast<EventSymbol>(Existing); 165e915a71fSHeejin Ahn if (!isa<EventSymbol>(Existing)) { 166e915a71fSHeejin Ahn reportTypeError(Existing, File, WASM_SYMBOL_TYPE_EVENT); 167e915a71fSHeejin Ahn return; 168e915a71fSHeejin Ahn } 169e915a71fSHeejin Ahn 170e915a71fSHeejin Ahn const WasmEventType *OldType = cast<EventSymbol>(Existing)->getEventType(); 171e915a71fSHeejin Ahn const WasmSignature *OldSig = ExistingEvent->Signature; 172e915a71fSHeejin Ahn if (NewType->Attribute != OldType->Attribute) 173e915a71fSHeejin Ahn error("Event type mismatch: " + Existing->getName() + "\n>>> defined as " + 174e915a71fSHeejin Ahn toString(*OldType) + " in " + toString(Existing->getFile()) + 175e915a71fSHeejin Ahn "\n>>> defined as " + toString(*NewType) + " in " + toString(File)); 176e915a71fSHeejin Ahn if (*NewSig != *OldSig) 177e915a71fSHeejin Ahn warn("Event signature mismatch: " + Existing->getName() + 178e915a71fSHeejin Ahn "\n>>> defined as " + toString(*OldSig) + " in " + 179e915a71fSHeejin Ahn toString(Existing->getFile()) + "\n>>> defined as " + 180e915a71fSHeejin Ahn toString(*NewSig) + " in " + toString(File)); 181e915a71fSHeejin Ahn } 182e915a71fSHeejin Ahn 183e3498ec5SRui Ueyama static void checkDataType(const Symbol *Existing, const InputFile *File) { 184e3498ec5SRui Ueyama if (!isa<DataSymbol>(Existing)) 1853876d89aSSam Clegg reportTypeError(Existing, File, WASM_SYMBOL_TYPE_DATA); 186e3498ec5SRui Ueyama } 187e3498ec5SRui Ueyama 188dfb0b2c7SSam Clegg DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name, 189ebda41f8SNicholas Wilson uint32_t Flags, 190ebda41f8SNicholas Wilson InputFunction *Function) { 191e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n"); 192b961abc8SRui Ueyama assert(!find(Name)); 193ebda41f8SNicholas Wilson SyntheticFunctions.emplace_back(Function); 1944c2cbfe6SSam Clegg return replaceSymbol<DefinedFunction>(insert(Name, nullptr).first, Name, 1954c2cbfe6SSam Clegg Flags, nullptr, Function); 19650686856SSam Clegg } 19750686856SSam Clegg 19800245539SSam Clegg DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name, 19900245539SSam Clegg uint32_t Flags) { 200e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n"); 201b961abc8SRui Ueyama assert(!find(Name)); 2024c2cbfe6SSam Clegg return replaceSymbol<DefinedData>(insert(Name, nullptr).first, Name, Flags); 203c94d393aSSam Clegg } 204c94d393aSSam Clegg 20593102974SSam Clegg DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags, 20693102974SSam Clegg InputGlobal *Global) { 207e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << Name << " -> " << Global 208e7245b42SNicola Zaghen << "\n"); 209b961abc8SRui Ueyama assert(!find(Name)); 210ebda41f8SNicholas Wilson SyntheticGlobals.emplace_back(Global); 2114c2cbfe6SSam Clegg return replaceSymbol<DefinedGlobal>(insert(Name, nullptr).first, Name, Flags, 2124c2cbfe6SSam Clegg nullptr, Global); 21393102974SSam Clegg } 21493102974SSam Clegg 215e3498ec5SRui Ueyama static bool shouldReplace(const Symbol *Existing, InputFile *NewFile, 216e3498ec5SRui Ueyama uint32_t NewFlags) { 217c03c9040SRui Ueyama // If existing symbol is undefined, replace it. 218e3498ec5SRui Ueyama if (!Existing->isDefined()) { 219e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " 220e3498ec5SRui Ueyama << Existing->getName() << "\n"); 221c03c9040SRui Ueyama return true; 222c03c9040SRui Ueyama } 223c03c9040SRui Ueyama 224c03c9040SRui Ueyama // Now we have two defined symbols. If the new one is weak, we can ignore it. 225c03c9040SRui Ueyama if ((NewFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { 226e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); 227c03c9040SRui Ueyama return false; 228c03c9040SRui Ueyama } 229c03c9040SRui Ueyama 230c03c9040SRui Ueyama // If the existing symbol is weak, we should replace it. 231e3498ec5SRui Ueyama if (Existing->isWeak()) { 232e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); 233c03c9040SRui Ueyama return true; 234c03c9040SRui Ueyama } 235c03c9040SRui Ueyama 236c03c9040SRui Ueyama // Neither symbol is week. They conflict. 237e3498ec5SRui Ueyama error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " + 238e3498ec5SRui Ueyama toString(Existing->getFile()) + "\n>>> defined in " + 239e3498ec5SRui Ueyama toString(NewFile)); 240c03c9040SRui Ueyama return true; 241b8621596SSam Clegg } 24293e559b1SSam Clegg 24393e559b1SSam Clegg Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags, 244e3498ec5SRui Ueyama InputFile *File, 245e3498ec5SRui Ueyama InputFunction *Function) { 2468b0b48f3SSam Clegg LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << " [" 2478b0b48f3SSam Clegg << (Function ? toString(Function->Signature) : "none") 2488b0b48f3SSam Clegg << "]\n"); 24993e559b1SSam Clegg Symbol *S; 25093e559b1SSam Clegg bool WasInserted; 2514c2cbfe6SSam Clegg std::tie(S, WasInserted) = insert(Name, File); 252c729c1b4SSam Clegg 2536540e570SSam Clegg auto Replace = [&](Symbol* Sym) { 2546540e570SSam Clegg // If the new defined function doesn't have signture (i.e. bitcode 2556540e570SSam Clegg // functions) but the old symbol does, then preserve the old signature 2566540e570SSam Clegg const WasmSignature *OldSig = S->getSignature(); 2576540e570SSam Clegg auto* NewSym = replaceSymbol<DefinedFunction>(Sym, Name, Flags, File, Function); 2586540e570SSam Clegg if (!NewSym->Signature) 2596540e570SSam Clegg NewSym->Signature = OldSig; 2606540e570SSam Clegg }; 2616540e570SSam Clegg 262e3498ec5SRui Ueyama if (WasInserted || S->isLazy()) { 2636540e570SSam Clegg Replace(S); 264e3498ec5SRui Ueyama return S; 265e3498ec5SRui Ueyama } 266e3498ec5SRui Ueyama 2676540e570SSam Clegg auto ExistingFunction = dyn_cast<FunctionSymbol>(S); 2686540e570SSam Clegg if (!ExistingFunction) { 2696540e570SSam Clegg reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION); 2706540e570SSam Clegg return S; 2718b0b48f3SSam Clegg } 2726540e570SSam Clegg 2736540e570SSam Clegg if (Function && !signatureMatches(ExistingFunction, &Function->Signature)) { 2746540e570SSam Clegg Symbol* Variant; 2756540e570SSam Clegg if (getFunctionVariant(S, &Function->Signature, File, &Variant)) 2766540e570SSam Clegg // New variant, always replace 2776540e570SSam Clegg Replace(Variant); 2786540e570SSam Clegg else if (shouldReplace(S, File, Flags)) 2796540e570SSam Clegg // Variant already exists, replace it after checking shouldReplace 2806540e570SSam Clegg Replace(Variant); 2816540e570SSam Clegg 2826540e570SSam Clegg // This variant we found take the place in the symbol table as the primary 2836540e570SSam Clegg // variant. 2846540e570SSam Clegg replace(Name, Variant); 2856540e570SSam Clegg return Variant; 2866540e570SSam Clegg } 2876540e570SSam Clegg 2886540e570SSam Clegg // Existing function with matching signature. 2896540e570SSam Clegg if (shouldReplace(S, File, Flags)) 2906540e570SSam Clegg Replace(S); 2916540e570SSam Clegg 29293e559b1SSam Clegg return S; 29393e559b1SSam Clegg } 29493e559b1SSam Clegg 29500245539SSam Clegg Symbol *SymbolTable::addDefinedData(StringRef Name, uint32_t Flags, 296e3498ec5SRui Ueyama InputFile *File, InputSegment *Segment, 29793102974SSam Clegg uint32_t Address, uint32_t Size) { 298e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "addDefinedData:" << Name << " addr:" << Address 299e7245b42SNicola Zaghen << "\n"); 30093e559b1SSam Clegg Symbol *S; 30193e559b1SSam Clegg bool WasInserted; 3024c2cbfe6SSam Clegg std::tie(S, WasInserted) = insert(Name, File); 303c729c1b4SSam Clegg 3046540e570SSam Clegg auto Replace = [&]() { 305e3498ec5SRui Ueyama replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size); 3066540e570SSam Clegg }; 3076540e570SSam Clegg 3086540e570SSam Clegg if (WasInserted || S->isLazy()) { 3096540e570SSam Clegg Replace(); 310e3498ec5SRui Ueyama return S; 311e3498ec5SRui Ueyama } 312e3498ec5SRui Ueyama 313e3498ec5SRui Ueyama checkDataType(S, File); 314e3498ec5SRui Ueyama 315e3498ec5SRui Ueyama if (shouldReplace(S, File, Flags)) 3166540e570SSam Clegg Replace(); 317c94d393aSSam Clegg return S; 318c94d393aSSam Clegg } 319c94d393aSSam Clegg 32093102974SSam Clegg Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags, 321e3498ec5SRui Ueyama InputFile *File, InputGlobal *Global) { 322e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n"); 3234c2cbfe6SSam Clegg 32493102974SSam Clegg Symbol *S; 32593102974SSam Clegg bool WasInserted; 3264c2cbfe6SSam Clegg std::tie(S, WasInserted) = insert(Name, File); 327c729c1b4SSam Clegg 3286540e570SSam Clegg auto Replace = [&]() { 329e3498ec5SRui Ueyama replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global); 3306540e570SSam Clegg }; 3316540e570SSam Clegg 3326540e570SSam Clegg if (WasInserted || S->isLazy()) { 3336540e570SSam Clegg Replace(); 33493102974SSam Clegg return S; 33593102974SSam Clegg } 33693102974SSam Clegg 337e3498ec5SRui Ueyama checkGlobalType(S, File, &Global->getType()); 338e3498ec5SRui Ueyama 339e3498ec5SRui Ueyama if (shouldReplace(S, File, Flags)) 3406540e570SSam Clegg Replace(); 341e3498ec5SRui Ueyama return S; 342e3498ec5SRui Ueyama } 343e3498ec5SRui Ueyama 344e915a71fSHeejin Ahn Symbol *SymbolTable::addDefinedEvent(StringRef Name, uint32_t Flags, 345e915a71fSHeejin Ahn InputFile *File, InputEvent *Event) { 346e915a71fSHeejin Ahn LLVM_DEBUG(dbgs() << "addDefinedEvent:" << Name << "\n"); 347e915a71fSHeejin Ahn 348e915a71fSHeejin Ahn Symbol *S; 349e915a71fSHeejin Ahn bool WasInserted; 350e915a71fSHeejin Ahn std::tie(S, WasInserted) = insert(Name, File); 351e915a71fSHeejin Ahn 3526540e570SSam Clegg auto Replace = [&]() { 353e915a71fSHeejin Ahn replaceSymbol<DefinedEvent>(S, Name, Flags, File, Event); 3546540e570SSam Clegg }; 3556540e570SSam Clegg 3566540e570SSam Clegg if (WasInserted || S->isLazy()) { 3576540e570SSam Clegg Replace(); 358e915a71fSHeejin Ahn return S; 359e915a71fSHeejin Ahn } 360e915a71fSHeejin Ahn 361e915a71fSHeejin Ahn checkEventType(S, File, &Event->getType(), &Event->Signature); 362e915a71fSHeejin Ahn 363e915a71fSHeejin Ahn if (shouldReplace(S, File, Flags)) 3646540e570SSam Clegg Replace(); 365e915a71fSHeejin Ahn return S; 366e915a71fSHeejin Ahn } 367e915a71fSHeejin Ahn 3689b84eeaaSDan Gohman Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName, 3699b84eeaaSDan Gohman StringRef ImportModule, 3707cc07531SSam Clegg uint32_t Flags, InputFile *File, 371e3498ec5SRui Ueyama const WasmSignature *Sig) { 3728b0b48f3SSam Clegg LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << 3738b0b48f3SSam Clegg " [" << (Sig ? toString(*Sig) : "none") << "]\n"); 374c03c9040SRui Ueyama 375c94d393aSSam Clegg Symbol *S; 376c94d393aSSam Clegg bool WasInserted; 3774c2cbfe6SSam Clegg std::tie(S, WasInserted) = insert(Name, File); 378c729c1b4SSam Clegg 3796540e570SSam Clegg auto Replace = [&]() { 3809b84eeaaSDan Gohman replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags, 3819b84eeaaSDan Gohman File, Sig); 3826540e570SSam Clegg }; 3836540e570SSam Clegg 3846540e570SSam Clegg if (WasInserted) 3856540e570SSam Clegg Replace(); 386e3498ec5SRui Ueyama else if (auto *Lazy = dyn_cast<LazySymbol>(S)) 387b961abc8SRui Ueyama Lazy->fetch(); 3886540e570SSam Clegg else { 3896540e570SSam Clegg auto ExistingFunction = dyn_cast<FunctionSymbol>(S); 3906540e570SSam Clegg if (!ExistingFunction) { 3916540e570SSam Clegg reportTypeError(S, File, WASM_SYMBOL_TYPE_FUNCTION); 3926540e570SSam Clegg return S; 3936540e570SSam Clegg } 3946540e570SSam Clegg if (!signatureMatches(ExistingFunction, Sig)) 3956540e570SSam Clegg if (getFunctionVariant(S, Sig, File, &S)) 3966540e570SSam Clegg Replace(); 3976540e570SSam Clegg } 398cefbf9acSSam Clegg 399c03c9040SRui Ueyama return S; 400c03c9040SRui Ueyama } 401c03c9040SRui Ueyama 402e3498ec5SRui Ueyama Symbol *SymbolTable::addUndefinedData(StringRef Name, uint32_t Flags, 403e3498ec5SRui Ueyama InputFile *File) { 404e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "addUndefinedData: " << Name << "\n"); 40593102974SSam Clegg 406e3498ec5SRui Ueyama Symbol *S; 407e3498ec5SRui Ueyama bool WasInserted; 4084c2cbfe6SSam Clegg std::tie(S, WasInserted) = insert(Name, File); 409f989a92dSSam Clegg 410e3498ec5SRui Ueyama if (WasInserted) 411e3498ec5SRui Ueyama replaceSymbol<UndefinedData>(S, Name, Flags, File); 412e3498ec5SRui Ueyama else if (auto *Lazy = dyn_cast<LazySymbol>(S)) 413b961abc8SRui Ueyama Lazy->fetch(); 414e3498ec5SRui Ueyama else if (S->isDefined()) 415e3498ec5SRui Ueyama checkDataType(S, File); 416c94d393aSSam Clegg return S; 417c94d393aSSam Clegg } 418c94d393aSSam Clegg 4199b84eeaaSDan Gohman Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName, 4209b84eeaaSDan Gohman StringRef ImportModule, uint32_t Flags, 421e3498ec5SRui Ueyama InputFile *File, 422e3498ec5SRui Ueyama const WasmGlobalType *Type) { 423e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n"); 424e3498ec5SRui Ueyama 425e3498ec5SRui Ueyama Symbol *S; 426e3498ec5SRui Ueyama bool WasInserted; 4274c2cbfe6SSam Clegg std::tie(S, WasInserted) = insert(Name, File); 428c729c1b4SSam Clegg 429e3498ec5SRui Ueyama if (WasInserted) 4309b84eeaaSDan Gohman replaceSymbol<UndefinedGlobal>(S, Name, ImportName, ImportModule, Flags, 4319b84eeaaSDan Gohman File, Type); 432e3498ec5SRui Ueyama else if (auto *Lazy = dyn_cast<LazySymbol>(S)) 433b961abc8SRui Ueyama Lazy->fetch(); 434e3498ec5SRui Ueyama else if (S->isDefined()) 435e3498ec5SRui Ueyama checkGlobalType(S, File, Type); 436e3498ec5SRui Ueyama return S; 437e3498ec5SRui Ueyama } 438e3498ec5SRui Ueyama 439e3498ec5SRui Ueyama void SymbolTable::addLazy(ArchiveFile *File, const Archive::Symbol *Sym) { 440e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "addLazy: " << Sym->getName() << "\n"); 441c94d393aSSam Clegg StringRef Name = Sym->getName(); 442c03c9040SRui Ueyama 443c94d393aSSam Clegg Symbol *S; 444c94d393aSSam Clegg bool WasInserted; 4454c2cbfe6SSam Clegg std::tie(S, WasInserted) = insert(Name, nullptr); 446c03c9040SRui Ueyama 447c94d393aSSam Clegg if (WasInserted) { 44837b4ee52SSam Clegg replaceSymbol<LazySymbol>(S, Name, 0, File, *Sym); 449c03c9040SRui Ueyama return; 450c03c9040SRui Ueyama } 451c03c9040SRui Ueyama 45237b4ee52SSam Clegg if (!S->isUndefined()) 45337b4ee52SSam Clegg return; 45437b4ee52SSam Clegg 45537b4ee52SSam Clegg // The existing symbol is undefined, load a new one from the archive, 45637b4ee52SSam Clegg // unless the the existing symbol is weak in which case replace the undefined 45737b4ee52SSam Clegg // symbols with a LazySymbol. 45837b4ee52SSam Clegg if (S->isWeak()) { 45937b4ee52SSam Clegg const WasmSignature *OldSig = nullptr; 46037b4ee52SSam Clegg // In the case of an UndefinedFunction we need to preserve the expected 46137b4ee52SSam Clegg // signature. 46237b4ee52SSam Clegg if (auto *F = dyn_cast<UndefinedFunction>(S)) 46337b4ee52SSam Clegg OldSig = F->Signature; 46437b4ee52SSam Clegg LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n"); 46537b4ee52SSam Clegg auto NewSym = replaceSymbol<LazySymbol>(S, Name, WASM_SYMBOL_BINDING_WEAK, 46637b4ee52SSam Clegg File, *Sym); 46737b4ee52SSam Clegg NewSym->Signature = OldSig; 46837b4ee52SSam Clegg return; 46937b4ee52SSam Clegg } 47037b4ee52SSam Clegg 471e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); 472e3498ec5SRui Ueyama File->addMember(Sym); 473c94d393aSSam Clegg } 474e0f6fcd0SSam Clegg 475c4d9aa1bSNicholas Wilson bool SymbolTable::addComdat(StringRef Name) { 476c4d9aa1bSNicholas Wilson return Comdats.insert(CachedHashStringRef(Name)).second; 477e0f6fcd0SSam Clegg } 4781f3f774fSSam Clegg 4796540e570SSam Clegg // The new signature doesn't match. Create a variant to the symbol with the 4806540e570SSam Clegg // signature encoded in the name and return that instead. These symbols are 4816540e570SSam Clegg // then unified later in handleSymbolVariants. 4826540e570SSam Clegg bool SymbolTable::getFunctionVariant(Symbol* Sym, const WasmSignature *Sig, 4836540e570SSam Clegg const InputFile *File, Symbol **Out) { 48414bab09eSRichard Trieu LLVM_DEBUG(dbgs() << "getFunctionVariant: " << Sym->getName() << " -> " 4856540e570SSam Clegg << " " << toString(*Sig) << "\n"); 4866540e570SSam Clegg Symbol *Variant = nullptr; 4876540e570SSam Clegg 4886540e570SSam Clegg // Linear search through symbol variants. Should never be more than two 4896540e570SSam Clegg // or three entries here. 4906540e570SSam Clegg auto &Variants = SymVariants[CachedHashStringRef(Sym->getName())]; 4916540e570SSam Clegg if (Variants.size() == 0) 4926540e570SSam Clegg Variants.push_back(Sym); 4936540e570SSam Clegg 4946540e570SSam Clegg for (Symbol* V : Variants) { 4956540e570SSam Clegg if (*V->getSignature() == *Sig) { 4966540e570SSam Clegg Variant = V; 4976540e570SSam Clegg break; 4986540e570SSam Clegg } 4996540e570SSam Clegg } 5006540e570SSam Clegg 5016540e570SSam Clegg bool WasAdded = !Variant; 5026540e570SSam Clegg if (WasAdded) { 5036540e570SSam Clegg // Create a new variant; 5046540e570SSam Clegg LLVM_DEBUG(dbgs() << "added new variant\n"); 5056540e570SSam Clegg Variant = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 5066540e570SSam Clegg Variants.push_back(Variant); 5076540e570SSam Clegg } else { 5086540e570SSam Clegg LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*Variant) << "\n"); 5096540e570SSam Clegg assert(*Variant->getSignature() == *Sig); 5106540e570SSam Clegg } 5116540e570SSam Clegg 5126540e570SSam Clegg *Out = Variant; 5136540e570SSam Clegg return WasAdded; 5146540e570SSam Clegg } 5156540e570SSam Clegg 5161f3f774fSSam Clegg // Set a flag for --trace-symbol so that we can print out a log message 5171f3f774fSSam Clegg // if a new symbol with the same name is inserted into the symbol table. 5181f3f774fSSam Clegg void SymbolTable::trace(StringRef Name) { 5191f3f774fSSam Clegg SymMap.insert({CachedHashStringRef(Name), -1}); 5201f3f774fSSam Clegg } 521230dc11dSSam Clegg 522230dc11dSSam Clegg static const uint8_t UnreachableFn[] = { 523230dc11dSSam Clegg 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, 524230dc11dSSam Clegg 0x00 /* opcode unreachable */, 0x0b /* opcode end */ 525230dc11dSSam Clegg }; 526230dc11dSSam Clegg 527230dc11dSSam Clegg // Replace the given symbol body with an unreachable function. 528230dc11dSSam Clegg // This is used by handleWeakUndefines in order to generate a callable 5296540e570SSam Clegg // equivalent of an undefined function and also handleSymbolVariants for 5306540e570SSam Clegg // undefined functions that don't match the signature of the definition. 531230dc11dSSam Clegg InputFunction *SymbolTable::replaceWithUnreachable(Symbol *Sym, 532230dc11dSSam Clegg const WasmSignature &Sig, 533230dc11dSSam Clegg StringRef DebugName) { 534230dc11dSSam Clegg auto *Func = make<SyntheticFunction>(Sig, Sym->getName(), DebugName); 535230dc11dSSam Clegg Func->setBody(UnreachableFn); 536230dc11dSSam Clegg SyntheticFunctions.emplace_back(Func); 5376540e570SSam Clegg replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Sym->getFlags(), nullptr, 5386540e570SSam Clegg Func); 539230dc11dSSam Clegg return Func; 540230dc11dSSam Clegg } 541230dc11dSSam Clegg 542230dc11dSSam Clegg // For weak undefined functions, there may be "call" instructions that reference 543230dc11dSSam Clegg // the symbol. In this case, we need to synthesise a dummy/stub function that 544230dc11dSSam Clegg // will abort at runtime, so that relocations can still provided an operand to 545230dc11dSSam Clegg // the call instruction that passes Wasm validation. 546230dc11dSSam Clegg void SymbolTable::handleWeakUndefines() { 547230dc11dSSam Clegg for (Symbol *Sym : getSymbols()) { 548230dc11dSSam Clegg if (!Sym->isUndefWeak()) 549230dc11dSSam Clegg continue; 550230dc11dSSam Clegg 5516540e570SSam Clegg const WasmSignature *Sig = Sym->getSignature(); 5526540e570SSam Clegg if (!Sig) { 553230dc11dSSam Clegg // It is possible for undefined functions not to have a signature (eg. if 554230dc11dSSam Clegg // added via "--undefined"), but weak undefined ones do have a signature. 5556540e570SSam Clegg // Lazy symbols may not be functions and therefore Sig can still be null 5566540e570SSam Clegg // in some circumstantce. 5576540e570SSam Clegg assert(!isa<FunctionSymbol>(Sym)); 558230dc11dSSam Clegg continue; 5596540e570SSam Clegg } 560230dc11dSSam Clegg 561230dc11dSSam Clegg // Add a synthetic dummy for weak undefined functions. These dummies will 562230dc11dSSam Clegg // be GC'd if not used as the target of any "call" instructions. 563230dc11dSSam Clegg StringRef DebugName = Saver.save("undefined:" + toString(*Sym)); 564230dc11dSSam Clegg InputFunction* Func = replaceWithUnreachable(Sym, *Sig, DebugName); 565230dc11dSSam Clegg // Ensure it compares equal to the null pointer, and so that table relocs 566230dc11dSSam Clegg // don't pull in the stub body (only call-operand relocs should do that). 567230dc11dSSam Clegg Func->setTableIndex(0); 568230dc11dSSam Clegg // Hide our dummy to prevent export. 569230dc11dSSam Clegg Sym->setHidden(true); 570230dc11dSSam Clegg } 571230dc11dSSam Clegg } 5726540e570SSam Clegg 5736540e570SSam Clegg static void reportFunctionSignatureMismatch(StringRef SymName, 5746540e570SSam Clegg FunctionSymbol *A, 5756540e570SSam Clegg FunctionSymbol *B, bool Error) { 5766540e570SSam Clegg std::string msg = ("function signature mismatch: " + SymName + 5776540e570SSam Clegg "\n>>> defined as " + toString(*A->Signature) + " in " + 5786540e570SSam Clegg toString(A->getFile()) + "\n>>> defined as " + 5796540e570SSam Clegg toString(*B->Signature) + " in " + toString(B->getFile())) 5806540e570SSam Clegg .str(); 5816540e570SSam Clegg if (Error) 5826540e570SSam Clegg error(msg); 5836540e570SSam Clegg else 5846540e570SSam Clegg warn(msg); 5856540e570SSam Clegg } 5866540e570SSam Clegg 5876540e570SSam Clegg // Remove any variant symbols that were created due to function signature 5886540e570SSam Clegg // mismatches. 5896540e570SSam Clegg void SymbolTable::handleSymbolVariants() { 5906540e570SSam Clegg for (auto Pair : SymVariants) { 5916540e570SSam Clegg // Push the initial symbol onto the list of variants. 5926540e570SSam Clegg StringRef SymName = Pair.first.val(); 5936540e570SSam Clegg std::vector<Symbol *> &Variants = Pair.second; 5946540e570SSam Clegg 5956540e570SSam Clegg #ifndef NDEBUG 596*f8d736f1SSam Clegg LLVM_DEBUG(dbgs() << "symbol with (" << Variants.size() 597*f8d736f1SSam Clegg << ") variants: " << SymName << "\n"); 5986540e570SSam Clegg for (auto *S: Variants) { 5996540e570SSam Clegg auto *F = cast<FunctionSymbol>(S); 600*f8d736f1SSam Clegg LLVM_DEBUG(dbgs() << " variant: " + F->getName() << " " 601*f8d736f1SSam Clegg << toString(*F->Signature) << "\n"); 6026540e570SSam Clegg } 6036540e570SSam Clegg #endif 6046540e570SSam Clegg 6056540e570SSam Clegg // Find the one definition. 6066540e570SSam Clegg DefinedFunction *Defined = nullptr; 6076540e570SSam Clegg for (auto *Symbol : Variants) { 6086540e570SSam Clegg if (auto F = dyn_cast<DefinedFunction>(Symbol)) { 6096540e570SSam Clegg Defined = F; 6106540e570SSam Clegg break; 6116540e570SSam Clegg } 6126540e570SSam Clegg } 6136540e570SSam Clegg 6146540e570SSam Clegg // If there are no definitions, and the undefined symbols disagree on 6156540e570SSam Clegg // the signature, there is not we can do since we don't know which one 6166540e570SSam Clegg // to use as the signature on the import. 6176540e570SSam Clegg if (!Defined) { 6186540e570SSam Clegg reportFunctionSignatureMismatch(SymName, 6196540e570SSam Clegg cast<FunctionSymbol>(Variants[0]), 6206540e570SSam Clegg cast<FunctionSymbol>(Variants[1]), true); 6216540e570SSam Clegg return; 6226540e570SSam Clegg } 6236540e570SSam Clegg 6246540e570SSam Clegg for (auto *Symbol : Variants) { 6256540e570SSam Clegg if (Symbol != Defined) { 6266540e570SSam Clegg auto *F = cast<FunctionSymbol>(Symbol); 6276540e570SSam Clegg reportFunctionSignatureMismatch(SymName, F, Defined, false); 6286540e570SSam Clegg StringRef DebugName = Saver.save("unreachable:" + toString(*F)); 6296540e570SSam Clegg replaceWithUnreachable(F, *F->Signature, DebugName); 6306540e570SSam Clegg } 6316540e570SSam Clegg } 6326540e570SSam Clegg } 6336540e570SSam Clegg } 634