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" 14*53e3b81fSAndy Wingo #include "InputTable.h" 15b8621596SSam Clegg #include "WriterUtils.h" 16c94d393aSSam Clegg #include "lld/Common/ErrorHandler.h" 172017d52bSRui Ueyama #include "lld/Common/Memory.h" 187d67dd11SRui Ueyama #include "llvm/ADT/SetVector.h" 19c94d393aSSam Clegg 20c94d393aSSam Clegg #define DEBUG_TYPE "lld" 21c94d393aSSam Clegg 22c94d393aSSam Clegg using namespace llvm; 2320db381bSSam Clegg using namespace llvm::wasm; 2445218f4aSSam Clegg using namespace llvm::object; 25c94d393aSSam Clegg 2633c59abfSFangrui Song namespace lld { 2733c59abfSFangrui Song namespace wasm { 2833c59abfSFangrui Song SymbolTable *symtab; 29c94d393aSSam Clegg 30136d27abSRui Ueyama void SymbolTable::addFile(InputFile *file) { 31136d27abSRui Ueyama log("Processing: " + toString(file)); 32a282a61bSSam Clegg 33a282a61bSSam Clegg // .a file 34136d27abSRui Ueyama if (auto *f = dyn_cast<ArchiveFile>(file)) { 35136d27abSRui Ueyama f->parse(); 36a282a61bSSam Clegg return; 37a282a61bSSam Clegg } 38a282a61bSSam Clegg 39a282a61bSSam Clegg // .so file 40136d27abSRui Ueyama if (auto *f = dyn_cast<SharedFile>(file)) { 41136d27abSRui Ueyama sharedFiles.push_back(f); 42a282a61bSSam Clegg return; 43a282a61bSSam Clegg } 44a282a61bSSam Clegg 45136d27abSRui Ueyama if (config->trace) 46136d27abSRui Ueyama message(toString(file)); 47c94d393aSSam Clegg 48c729c1b4SSam Clegg // LLVM bitcode file 49136d27abSRui Ueyama if (auto *f = dyn_cast<BitcodeFile>(file)) { 50136d27abSRui Ueyama f->parse(); 51136d27abSRui Ueyama bitcodeFiles.push_back(f); 52a282a61bSSam Clegg return; 53a282a61bSSam Clegg } 54a282a61bSSam Clegg 55a282a61bSSam Clegg // Regular object file 56136d27abSRui Ueyama auto *f = cast<ObjFile>(file); 57136d27abSRui Ueyama f->parse(false); 58136d27abSRui Ueyama objectFiles.push_back(f); 59c94d393aSSam Clegg } 60c94d393aSSam Clegg 61c729c1b4SSam Clegg // This function is where all the optimizations of link-time 62c729c1b4SSam Clegg // optimization happens. When LTO is in use, some input files are 63c729c1b4SSam Clegg // not in native object file format but in the LLVM bitcode format. 64c729c1b4SSam Clegg // This function compiles bitcode files into a few big native files 65c729c1b4SSam Clegg // using LLVM functions and replaces bitcode symbols with the results. 66c729c1b4SSam Clegg // Because all bitcode files that the program consists of are passed 67c729c1b4SSam Clegg // to the compiler at once, it can do whole-program optimization. 68c729c1b4SSam Clegg void SymbolTable::addCombinedLTOObject() { 69b062fe18SSam Clegg // Prevent further LTO objects being included 70b062fe18SSam Clegg BitcodeFile::doneLTO = true; 71b062fe18SSam Clegg 72136d27abSRui Ueyama if (bitcodeFiles.empty()) 73c729c1b4SSam Clegg return; 74c729c1b4SSam Clegg 75c729c1b4SSam Clegg // Compile bitcode files and replace bitcode symbols. 76136d27abSRui Ueyama lto.reset(new BitcodeCompiler); 77136d27abSRui Ueyama for (BitcodeFile *f : bitcodeFiles) 78136d27abSRui Ueyama lto->add(*f); 79c729c1b4SSam Clegg 80136d27abSRui Ueyama for (StringRef filename : lto->compile()) { 81136d27abSRui Ueyama auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), ""); 82136d27abSRui Ueyama obj->parse(true); 83136d27abSRui Ueyama objectFiles.push_back(obj); 84c729c1b4SSam Clegg } 85c729c1b4SSam Clegg } 86c729c1b4SSam Clegg 87136d27abSRui Ueyama Symbol *SymbolTable::find(StringRef name) { 88136d27abSRui Ueyama auto it = symMap.find(CachedHashStringRef(name)); 89136d27abSRui Ueyama if (it == symMap.end() || it->second == -1) 901f3f774fSSam Clegg return nullptr; 91136d27abSRui Ueyama return symVector[it->second]; 921f3f774fSSam Clegg } 931f3f774fSSam Clegg 94136d27abSRui Ueyama void SymbolTable::replace(StringRef name, Symbol* sym) { 95136d27abSRui Ueyama auto it = symMap.find(CachedHashStringRef(name)); 96136d27abSRui Ueyama symVector[it->second] = sym; 976540e570SSam Clegg } 986540e570SSam Clegg 99136d27abSRui Ueyama std::pair<Symbol *, bool> SymbolTable::insertName(StringRef name) { 100136d27abSRui Ueyama bool trace = false; 101136d27abSRui Ueyama auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()}); 102136d27abSRui Ueyama int &symIndex = p.first->second; 103136d27abSRui Ueyama bool isNew = p.second; 104136d27abSRui Ueyama if (symIndex == -1) { 105136d27abSRui Ueyama symIndex = symVector.size(); 106136d27abSRui Ueyama trace = true; 107136d27abSRui Ueyama isNew = true; 1081f3f774fSSam Clegg } 1091f3f774fSSam Clegg 110136d27abSRui Ueyama if (!isNew) 111136d27abSRui Ueyama return {symVector[symIndex], false}; 1121f3f774fSSam Clegg 113136d27abSRui Ueyama Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 114136d27abSRui Ueyama sym->isUsedInRegularObj = false; 115136d27abSRui Ueyama sym->canInline = true; 116136d27abSRui Ueyama sym->traced = trace; 117064e9907SSam Clegg sym->forceExport = false; 118136d27abSRui Ueyama symVector.emplace_back(sym); 119136d27abSRui Ueyama return {sym, true}; 120c94d393aSSam Clegg } 121c94d393aSSam Clegg 122136d27abSRui Ueyama std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, 123136d27abSRui Ueyama const InputFile *file) { 124136d27abSRui Ueyama Symbol *s; 125136d27abSRui Ueyama bool wasInserted; 126136d27abSRui Ueyama std::tie(s, wasInserted) = insertName(name); 1271f3f774fSSam Clegg 128136d27abSRui Ueyama if (!file || file->kind() == InputFile::ObjectKind) 129136d27abSRui Ueyama s->isUsedInRegularObj = true; 1301f3f774fSSam Clegg 131136d27abSRui Ueyama return {s, wasInserted}; 132c94d393aSSam Clegg } 133c94d393aSSam Clegg 134136d27abSRui Ueyama static void reportTypeError(const Symbol *existing, const InputFile *file, 135136d27abSRui Ueyama llvm::wasm::WasmSymbolType type) { 136136d27abSRui Ueyama error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " + 137136d27abSRui Ueyama toString(existing->getWasmType()) + " in " + 138136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined as " + toString(type) + 139136d27abSRui Ueyama " in " + toString(file)); 140e3498ec5SRui Ueyama } 141e3498ec5SRui Ueyama 1426f4286fbSHeejin Ahn // Check the type of new symbol matches that of the symbol is replacing. 1437c5fcb35SKazuaki Ishizaki // Returns true if the function types match, false is there is a signature 1446540e570SSam Clegg // mismatch. 145136d27abSRui Ueyama static bool signatureMatches(FunctionSymbol *existing, 146136d27abSRui Ueyama const WasmSignature *newSig) { 147136d27abSRui Ueyama const WasmSignature *oldSig = existing->signature; 148d506b0a4SSam Clegg 149d506b0a4SSam Clegg // If either function is missing a signature (this happend for bitcode 150d506b0a4SSam Clegg // symbols) then assume they match. Any mismatch will be reported later 151d506b0a4SSam Clegg // when the LTO objects are added. 152136d27abSRui Ueyama if (!newSig || !oldSig) 1536540e570SSam Clegg return true; 154cefbf9acSSam Clegg 155136d27abSRui Ueyama return *newSig == *oldSig; 156e3498ec5SRui Ueyama } 157e3498ec5SRui Ueyama 158136d27abSRui Ueyama static void checkGlobalType(const Symbol *existing, const InputFile *file, 159136d27abSRui Ueyama const WasmGlobalType *newType) { 160136d27abSRui Ueyama if (!isa<GlobalSymbol>(existing)) { 161136d27abSRui Ueyama reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL); 162b8621596SSam Clegg return; 163b8621596SSam Clegg } 164b8621596SSam Clegg 165136d27abSRui Ueyama const WasmGlobalType *oldType = cast<GlobalSymbol>(existing)->getGlobalType(); 166136d27abSRui Ueyama if (*newType != *oldType) { 167136d27abSRui Ueyama error("Global type mismatch: " + existing->getName() + "\n>>> defined as " + 168136d27abSRui Ueyama toString(*oldType) + " in " + toString(existing->getFile()) + 169136d27abSRui Ueyama "\n>>> defined as " + toString(*newType) + " in " + toString(file)); 170c94d393aSSam Clegg } 17124b3dcd4SSam Clegg } 17224b3dcd4SSam Clegg 173136d27abSRui Ueyama static void checkEventType(const Symbol *existing, const InputFile *file, 174136d27abSRui Ueyama const WasmEventType *newType, 175136d27abSRui Ueyama const WasmSignature *newSig) { 176136d27abSRui Ueyama auto existingEvent = dyn_cast<EventSymbol>(existing); 177136d27abSRui Ueyama if (!isa<EventSymbol>(existing)) { 178136d27abSRui Ueyama reportTypeError(existing, file, WASM_SYMBOL_TYPE_EVENT); 179e915a71fSHeejin Ahn return; 180e915a71fSHeejin Ahn } 181e915a71fSHeejin Ahn 182136d27abSRui Ueyama const WasmEventType *oldType = cast<EventSymbol>(existing)->getEventType(); 183136d27abSRui Ueyama const WasmSignature *oldSig = existingEvent->signature; 184136d27abSRui Ueyama if (newType->Attribute != oldType->Attribute) 185136d27abSRui Ueyama error("Event type mismatch: " + existing->getName() + "\n>>> defined as " + 186136d27abSRui Ueyama toString(*oldType) + " in " + toString(existing->getFile()) + 187136d27abSRui Ueyama "\n>>> defined as " + toString(*newType) + " in " + toString(file)); 188136d27abSRui Ueyama if (*newSig != *oldSig) 189136d27abSRui Ueyama warn("Event signature mismatch: " + existing->getName() + 190136d27abSRui Ueyama "\n>>> defined as " + toString(*oldSig) + " in " + 191136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined as " + 192136d27abSRui Ueyama toString(*newSig) + " in " + toString(file)); 193e915a71fSHeejin Ahn } 194e915a71fSHeejin Ahn 195*53e3b81fSAndy Wingo static void checkTableType(const Symbol *existing, const InputFile *file, 196*53e3b81fSAndy Wingo const WasmTableType *newType) { 197*53e3b81fSAndy Wingo if (!isa<TableSymbol>(existing)) { 198*53e3b81fSAndy Wingo reportTypeError(existing, file, WASM_SYMBOL_TYPE_TABLE); 199*53e3b81fSAndy Wingo return; 200*53e3b81fSAndy Wingo } 201*53e3b81fSAndy Wingo 202*53e3b81fSAndy Wingo const WasmTableType *oldType = cast<TableSymbol>(existing)->getTableType(); 203*53e3b81fSAndy Wingo if (newType->ElemType != oldType->ElemType) { 204*53e3b81fSAndy Wingo error("Table type mismatch: " + existing->getName() + "\n>>> defined as " + 205*53e3b81fSAndy Wingo toString(*oldType) + " in " + toString(existing->getFile()) + 206*53e3b81fSAndy Wingo "\n>>> defined as " + toString(*newType) + " in " + toString(file)); 207*53e3b81fSAndy Wingo } 208*53e3b81fSAndy Wingo // FIXME: No assertions currently on the limits. 209*53e3b81fSAndy Wingo } 210*53e3b81fSAndy Wingo 211136d27abSRui Ueyama static void checkDataType(const Symbol *existing, const InputFile *file) { 212136d27abSRui Ueyama if (!isa<DataSymbol>(existing)) 213136d27abSRui Ueyama reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA); 214e3498ec5SRui Ueyama } 215e3498ec5SRui Ueyama 216136d27abSRui Ueyama DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name, 217136d27abSRui Ueyama uint32_t flags, 218136d27abSRui Ueyama InputFunction *function) { 219136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n"); 220136d27abSRui Ueyama assert(!find(name)); 221136d27abSRui Ueyama syntheticFunctions.emplace_back(function); 222136d27abSRui Ueyama return replaceSymbol<DefinedFunction>(insertName(name).first, name, 223136d27abSRui Ueyama flags, nullptr, function); 22450686856SSam Clegg } 22550686856SSam Clegg 22629a3056bSSam Clegg // Adds an optional, linker generated, data symbol. The symbol will only be 2277ae3d335SKazuaki Ishizaki // added if there is an undefine reference to it, or if it is explicitly 2287ae3d335SKazuaki Ishizaki // exported via the --export flag. Otherwise we don't add the symbol and return 2297ae3d335SKazuaki Ishizaki // nullptr. 230caa0db13SSam Clegg DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name, 231582fd474SWouter van Oortmerssen uint64_t value) { 232136d27abSRui Ueyama Symbol *s = find(name); 233136d27abSRui Ueyama if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0)) 234136d27abSRui Ueyama s = insertName(name).first; 235136d27abSRui Ueyama else if (!s || s->isDefined()) 2364bce63a0SSam Clegg return nullptr; 237136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n"); 238caa0db13SSam Clegg auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN); 239136d27abSRui Ueyama rtn->setVirtualAddress(value); 240136d27abSRui Ueyama rtn->referenced = true; 2414bce63a0SSam Clegg return rtn; 2424bce63a0SSam Clegg } 2434bce63a0SSam Clegg 244136d27abSRui Ueyama DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name, 245136d27abSRui Ueyama uint32_t flags) { 246136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n"); 247136d27abSRui Ueyama assert(!find(name)); 248136d27abSRui Ueyama return replaceSymbol<DefinedData>(insertName(name).first, name, flags); 249c94d393aSSam Clegg } 250c94d393aSSam Clegg 251136d27abSRui Ueyama DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags, 252136d27abSRui Ueyama InputGlobal *global) { 253136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global 254e7245b42SNicola Zaghen << "\n"); 255136d27abSRui Ueyama assert(!find(name)); 256136d27abSRui Ueyama syntheticGlobals.emplace_back(global); 257136d27abSRui Ueyama return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags, 258136d27abSRui Ueyama nullptr, global); 25993102974SSam Clegg } 26093102974SSam Clegg 26129a3056bSSam Clegg DefinedGlobal *SymbolTable::addOptionalGlobalSymbols(StringRef name, 26229a3056bSSam Clegg uint32_t flags, 26329a3056bSSam Clegg InputGlobal *global) { 26429a3056bSSam Clegg LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbols: " << name << " -> " << global 26529a3056bSSam Clegg << "\n"); 26629a3056bSSam Clegg Symbol *s = find(name); 26729a3056bSSam Clegg if (!s || s->isDefined()) 26829a3056bSSam Clegg return nullptr; 26929a3056bSSam Clegg syntheticGlobals.emplace_back(global); 27029a3056bSSam Clegg return replaceSymbol<DefinedGlobal>(s, name, flags, nullptr, global); 27129a3056bSSam Clegg } 27229a3056bSSam Clegg 273136d27abSRui Ueyama static bool shouldReplace(const Symbol *existing, InputFile *newFile, 274136d27abSRui Ueyama uint32_t newFlags) { 275c03c9040SRui Ueyama // If existing symbol is undefined, replace it. 276136d27abSRui Ueyama if (!existing->isDefined()) { 277e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " 278136d27abSRui Ueyama << existing->getName() << "\n"); 279c03c9040SRui Ueyama return true; 280c03c9040SRui Ueyama } 281c03c9040SRui Ueyama 282c03c9040SRui Ueyama // Now we have two defined symbols. If the new one is weak, we can ignore it. 283136d27abSRui Ueyama if ((newFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { 284e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); 285c03c9040SRui Ueyama return false; 286c03c9040SRui Ueyama } 287c03c9040SRui Ueyama 288c03c9040SRui Ueyama // If the existing symbol is weak, we should replace it. 289136d27abSRui Ueyama if (existing->isWeak()) { 290e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); 291c03c9040SRui Ueyama return true; 292c03c9040SRui Ueyama } 293c03c9040SRui Ueyama 294c03c9040SRui Ueyama // Neither symbol is week. They conflict. 295136d27abSRui Ueyama error("duplicate symbol: " + toString(*existing) + "\n>>> defined in " + 296136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined in " + 297136d27abSRui Ueyama toString(newFile)); 298c03c9040SRui Ueyama return true; 299b8621596SSam Clegg } 30093e559b1SSam Clegg 301136d27abSRui Ueyama Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags, 302136d27abSRui Ueyama InputFile *file, 303136d27abSRui Ueyama InputFunction *function) { 304136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " [" 305136d27abSRui Ueyama << (function ? toString(function->signature) : "none") 3068b0b48f3SSam Clegg << "]\n"); 307136d27abSRui Ueyama Symbol *s; 308136d27abSRui Ueyama bool wasInserted; 309136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 310c729c1b4SSam Clegg 311136d27abSRui Ueyama auto replaceSym = [&](Symbol *sym) { 3127c5fcb35SKazuaki Ishizaki // If the new defined function doesn't have signature (i.e. bitcode 3136540e570SSam Clegg // functions) but the old symbol does, then preserve the old signature 314136d27abSRui Ueyama const WasmSignature *oldSig = s->getSignature(); 315136d27abSRui Ueyama auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function); 316136d27abSRui Ueyama if (!newSym->signature) 317136d27abSRui Ueyama newSym->signature = oldSig; 3186540e570SSam Clegg }; 3196540e570SSam Clegg 320136d27abSRui Ueyama if (wasInserted || s->isLazy()) { 321136d27abSRui Ueyama replaceSym(s); 322136d27abSRui Ueyama return s; 323e3498ec5SRui Ueyama } 324e3498ec5SRui Ueyama 325136d27abSRui Ueyama auto existingFunction = dyn_cast<FunctionSymbol>(s); 326136d27abSRui Ueyama if (!existingFunction) { 327136d27abSRui Ueyama reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION); 328136d27abSRui Ueyama return s; 3298b0b48f3SSam Clegg } 3306540e570SSam Clegg 331136d27abSRui Ueyama bool checkSig = true; 332136d27abSRui Ueyama if (auto ud = dyn_cast<UndefinedFunction>(existingFunction)) 333136d27abSRui Ueyama checkSig = ud->isCalledDirectly; 33459f959ffSSam Clegg 335136d27abSRui Ueyama if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) { 336136d27abSRui Ueyama Symbol* variant; 337136d27abSRui Ueyama if (getFunctionVariant(s, &function->signature, file, &variant)) 3386540e570SSam Clegg // New variant, always replace 339136d27abSRui Ueyama replaceSym(variant); 340136d27abSRui Ueyama else if (shouldReplace(s, file, flags)) 3416540e570SSam Clegg // Variant already exists, replace it after checking shouldReplace 342136d27abSRui Ueyama replaceSym(variant); 3436540e570SSam Clegg 3446540e570SSam Clegg // This variant we found take the place in the symbol table as the primary 3456540e570SSam Clegg // variant. 346136d27abSRui Ueyama replace(name, variant); 347136d27abSRui Ueyama return variant; 3486540e570SSam Clegg } 3496540e570SSam Clegg 3506540e570SSam Clegg // Existing function with matching signature. 351136d27abSRui Ueyama if (shouldReplace(s, file, flags)) 352136d27abSRui Ueyama replaceSym(s); 3536540e570SSam Clegg 354136d27abSRui Ueyama return s; 35593e559b1SSam Clegg } 35693e559b1SSam Clegg 357136d27abSRui Ueyama Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags, 358136d27abSRui Ueyama InputFile *file, InputSegment *segment, 3593b29376eSWouter van Oortmerssen uint64_t address, uint64_t size) { 360136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address 361e7245b42SNicola Zaghen << "\n"); 362136d27abSRui Ueyama Symbol *s; 363136d27abSRui Ueyama bool wasInserted; 364136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 365c729c1b4SSam Clegg 366136d27abSRui Ueyama auto replaceSym = [&]() { 367136d27abSRui Ueyama replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size); 3686540e570SSam Clegg }; 3696540e570SSam Clegg 370136d27abSRui Ueyama if (wasInserted || s->isLazy()) { 371136d27abSRui Ueyama replaceSym(); 372136d27abSRui Ueyama return s; 373e3498ec5SRui Ueyama } 374e3498ec5SRui Ueyama 375136d27abSRui Ueyama checkDataType(s, file); 376e3498ec5SRui Ueyama 377136d27abSRui Ueyama if (shouldReplace(s, file, flags)) 378136d27abSRui Ueyama replaceSym(); 379136d27abSRui Ueyama return s; 380c94d393aSSam Clegg } 381c94d393aSSam Clegg 382136d27abSRui Ueyama Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags, 383136d27abSRui Ueyama InputFile *file, InputGlobal *global) { 384136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n"); 3854c2cbfe6SSam Clegg 386136d27abSRui Ueyama Symbol *s; 387136d27abSRui Ueyama bool wasInserted; 388136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 389c729c1b4SSam Clegg 390136d27abSRui Ueyama auto replaceSym = [&]() { 391136d27abSRui Ueyama replaceSymbol<DefinedGlobal>(s, name, flags, file, global); 3926540e570SSam Clegg }; 3936540e570SSam Clegg 394136d27abSRui Ueyama if (wasInserted || s->isLazy()) { 395136d27abSRui Ueyama replaceSym(); 396136d27abSRui Ueyama return s; 39793102974SSam Clegg } 39893102974SSam Clegg 399136d27abSRui Ueyama checkGlobalType(s, file, &global->getType()); 400e3498ec5SRui Ueyama 401136d27abSRui Ueyama if (shouldReplace(s, file, flags)) 402136d27abSRui Ueyama replaceSym(); 403136d27abSRui Ueyama return s; 404e3498ec5SRui Ueyama } 405e3498ec5SRui Ueyama 406136d27abSRui Ueyama Symbol *SymbolTable::addDefinedEvent(StringRef name, uint32_t flags, 407136d27abSRui Ueyama InputFile *file, InputEvent *event) { 408136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedEvent:" << name << "\n"); 409e915a71fSHeejin Ahn 410136d27abSRui Ueyama Symbol *s; 411136d27abSRui Ueyama bool wasInserted; 412136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 413e915a71fSHeejin Ahn 414136d27abSRui Ueyama auto replaceSym = [&]() { 415136d27abSRui Ueyama replaceSymbol<DefinedEvent>(s, name, flags, file, event); 4166540e570SSam Clegg }; 4176540e570SSam Clegg 418136d27abSRui Ueyama if (wasInserted || s->isLazy()) { 419136d27abSRui Ueyama replaceSym(); 420136d27abSRui Ueyama return s; 421e915a71fSHeejin Ahn } 422e915a71fSHeejin Ahn 423136d27abSRui Ueyama checkEventType(s, file, &event->getType(), &event->signature); 424e915a71fSHeejin Ahn 425136d27abSRui Ueyama if (shouldReplace(s, file, flags)) 426136d27abSRui Ueyama replaceSym(); 427136d27abSRui Ueyama return s; 428e915a71fSHeejin Ahn } 429e915a71fSHeejin Ahn 430*53e3b81fSAndy Wingo Symbol *SymbolTable::addDefinedTable(StringRef name, uint32_t flags, 431*53e3b81fSAndy Wingo InputFile *file, InputTable *table) { 432*53e3b81fSAndy Wingo LLVM_DEBUG(dbgs() << "addDefinedTable:" << name << "\n"); 433*53e3b81fSAndy Wingo 434*53e3b81fSAndy Wingo Symbol *s; 435*53e3b81fSAndy Wingo bool wasInserted; 436*53e3b81fSAndy Wingo std::tie(s, wasInserted) = insert(name, file); 437*53e3b81fSAndy Wingo 438*53e3b81fSAndy Wingo auto replaceSym = [&]() { 439*53e3b81fSAndy Wingo replaceSymbol<DefinedTable>(s, name, flags, file, table); 440*53e3b81fSAndy Wingo }; 441*53e3b81fSAndy Wingo 442*53e3b81fSAndy Wingo if (wasInserted || s->isLazy()) { 443*53e3b81fSAndy Wingo replaceSym(); 444*53e3b81fSAndy Wingo return s; 445*53e3b81fSAndy Wingo } 446*53e3b81fSAndy Wingo 447*53e3b81fSAndy Wingo checkTableType(s, file, &table->getType()); 448*53e3b81fSAndy Wingo 449*53e3b81fSAndy Wingo if (shouldReplace(s, file, flags)) 450*53e3b81fSAndy Wingo replaceSym(); 451*53e3b81fSAndy Wingo return s; 452*53e3b81fSAndy Wingo } 453*53e3b81fSAndy Wingo 45467b05584SSam Clegg // This function get called when an undefined symbol is added, and there is 45567b05584SSam Clegg // already an existing one in the symbols table. In this case we check that 45667b05584SSam Clegg // custom 'import-module' and 'import-field' symbol attributes agree. 4577ae3d335SKazuaki Ishizaki // With LTO these attributes are not available when the bitcode is read and only 45867b05584SSam Clegg // become available when the LTO object is read. In this case we silently 45967b05584SSam Clegg // replace the empty attributes with the valid ones. 46067b05584SSam Clegg template <typename T> 461bd481277SSam Clegg static void setImportAttributes(T *existing, Optional<StringRef> importName, 462bd481277SSam Clegg Optional<StringRef> importModule, 463a57f1a54SSam Clegg uint32_t flags, InputFile *file) { 464bd481277SSam Clegg if (importName) { 465bd481277SSam Clegg if (!existing->importName) 46667b05584SSam Clegg existing->importName = importName; 46767b05584SSam Clegg if (existing->importName != importName) 46867b05584SSam Clegg error("import name mismatch for symbol: " + toString(*existing) + 469bd481277SSam Clegg "\n>>> defined as " + *existing->importName + " in " + 470bd481277SSam Clegg toString(existing->getFile()) + "\n>>> defined as " + *importName + 47167b05584SSam Clegg " in " + toString(file)); 47267b05584SSam Clegg } 47367b05584SSam Clegg 474bd481277SSam Clegg if (importModule) { 475bd481277SSam Clegg if (!existing->importModule) 47667b05584SSam Clegg existing->importModule = importModule; 47767b05584SSam Clegg if (existing->importModule != importModule) 47867b05584SSam Clegg error("import module mismatch for symbol: " + toString(*existing) + 479bd481277SSam Clegg "\n>>> defined as " + *existing->importModule + " in " + 480bd481277SSam Clegg toString(existing->getFile()) + "\n>>> defined as " + 481bd481277SSam Clegg *importModule + " in " + toString(file)); 48267b05584SSam Clegg } 483a57f1a54SSam Clegg 484a57f1a54SSam Clegg // Update symbol binding, if the existing symbol is weak 485a57f1a54SSam Clegg uint32_t binding = flags & WASM_SYMBOL_BINDING_MASK; 486a57f1a54SSam Clegg if (existing->isWeak() && binding != WASM_SYMBOL_BINDING_WEAK) { 487a57f1a54SSam Clegg existing->flags = (existing->flags & ~WASM_SYMBOL_BINDING_MASK) | binding; 488a57f1a54SSam Clegg } 48967b05584SSam Clegg } 49067b05584SSam Clegg 491bd481277SSam Clegg Symbol *SymbolTable::addUndefinedFunction(StringRef name, 492bd481277SSam Clegg Optional<StringRef> importName, 493bd481277SSam Clegg Optional<StringRef> importModule, 494136d27abSRui Ueyama uint32_t flags, InputFile *file, 495136d27abSRui Ueyama const WasmSignature *sig, 496136d27abSRui Ueyama bool isCalledDirectly) { 497136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " [" 498136d27abSRui Ueyama << (sig ? toString(*sig) : "none") 499a57f1a54SSam Clegg << "] IsCalledDirectly:" << isCalledDirectly << " flags=0x" 500a57f1a54SSam Clegg << utohexstr(flags) << "\n"); 501f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED); 502c03c9040SRui Ueyama 503136d27abSRui Ueyama Symbol *s; 504136d27abSRui Ueyama bool wasInserted; 505136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 506136d27abSRui Ueyama if (s->traced) 507136d27abSRui Ueyama printTraceSymbolUndefined(name, file); 508c729c1b4SSam Clegg 509136d27abSRui Ueyama auto replaceSym = [&]() { 510136d27abSRui Ueyama replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags, 511136d27abSRui Ueyama file, sig, isCalledDirectly); 5126540e570SSam Clegg }; 5136540e570SSam Clegg 514b34ec596SSam Clegg if (wasInserted) { 515136d27abSRui Ueyama replaceSym(); 516b34ec596SSam Clegg } else if (auto *lazy = dyn_cast<LazySymbol>(s)) { 517b34ec596SSam Clegg if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { 518b34ec596SSam Clegg lazy->setWeak(); 519b34ec596SSam Clegg lazy->signature = sig; 520b34ec596SSam Clegg } else { 521136d27abSRui Ueyama lazy->fetch(); 522b34ec596SSam Clegg } 523b34ec596SSam Clegg } else { 524136d27abSRui Ueyama auto existingFunction = dyn_cast<FunctionSymbol>(s); 525136d27abSRui Ueyama if (!existingFunction) { 526136d27abSRui Ueyama reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION); 527136d27abSRui Ueyama return s; 5286540e570SSam Clegg } 529136d27abSRui Ueyama if (!existingFunction->signature && sig) 530136d27abSRui Ueyama existingFunction->signature = sig; 531a57f1a54SSam Clegg auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction); 532fecfc592SSam Clegg if (isCalledDirectly && !signatureMatches(existingFunction, sig)) { 533a57f1a54SSam Clegg // If the existing undefined functions is not called directly then let 534fecfc592SSam Clegg // this one take precedence. Otherwise the existing function is either 535a57f1a54SSam Clegg // directly called or defined, in which case we need a function variant. 536fecfc592SSam Clegg if (existingUndefined && !existingUndefined->isCalledDirectly) 537136d27abSRui Ueyama replaceSym(); 538fecfc592SSam Clegg else if (getFunctionVariant(s, sig, file, &s)) 539fecfc592SSam Clegg replaceSym(); 540fecfc592SSam Clegg } 54167b05584SSam Clegg if (existingUndefined) 542a57f1a54SSam Clegg setImportAttributes(existingUndefined, importName, importModule, flags, 543a57f1a54SSam Clegg file); 5446540e570SSam Clegg } 545cefbf9acSSam Clegg 546136d27abSRui Ueyama return s; 547c03c9040SRui Ueyama } 548c03c9040SRui Ueyama 549136d27abSRui Ueyama Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags, 550136d27abSRui Ueyama InputFile *file) { 551136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n"); 552f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED); 55393102974SSam Clegg 554136d27abSRui Ueyama Symbol *s; 555136d27abSRui Ueyama bool wasInserted; 556136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 557136d27abSRui Ueyama if (s->traced) 558136d27abSRui Ueyama printTraceSymbolUndefined(name, file); 559f989a92dSSam Clegg 560b34ec596SSam Clegg if (wasInserted) { 561136d27abSRui Ueyama replaceSymbol<UndefinedData>(s, name, flags, file); 562b34ec596SSam Clegg } else if (auto *lazy = dyn_cast<LazySymbol>(s)) { 563b34ec596SSam Clegg if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) 564b34ec596SSam Clegg lazy->setWeak(); 565b34ec596SSam Clegg else 566136d27abSRui Ueyama lazy->fetch(); 567b34ec596SSam Clegg } else if (s->isDefined()) { 568136d27abSRui Ueyama checkDataType(s, file); 569b34ec596SSam Clegg } 570136d27abSRui Ueyama return s; 571c94d393aSSam Clegg } 572c94d393aSSam Clegg 573bd481277SSam Clegg Symbol *SymbolTable::addUndefinedGlobal(StringRef name, 574bd481277SSam Clegg Optional<StringRef> importName, 575bd481277SSam Clegg Optional<StringRef> importModule, 576bd481277SSam Clegg uint32_t flags, InputFile *file, 577136d27abSRui Ueyama const WasmGlobalType *type) { 578136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n"); 579f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED); 580e3498ec5SRui Ueyama 581136d27abSRui Ueyama Symbol *s; 582136d27abSRui Ueyama bool wasInserted; 583136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 584136d27abSRui Ueyama if (s->traced) 585136d27abSRui Ueyama printTraceSymbolUndefined(name, file); 586c729c1b4SSam Clegg 587136d27abSRui Ueyama if (wasInserted) 588136d27abSRui Ueyama replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags, 589136d27abSRui Ueyama file, type); 590136d27abSRui Ueyama else if (auto *lazy = dyn_cast<LazySymbol>(s)) 591136d27abSRui Ueyama lazy->fetch(); 592136d27abSRui Ueyama else if (s->isDefined()) 593136d27abSRui Ueyama checkGlobalType(s, file, type); 594136d27abSRui Ueyama return s; 595e3498ec5SRui Ueyama } 596e3498ec5SRui Ueyama 597*53e3b81fSAndy Wingo Symbol *SymbolTable::addUndefinedTable(StringRef name, 598*53e3b81fSAndy Wingo Optional<StringRef> importName, 599*53e3b81fSAndy Wingo Optional<StringRef> importModule, 600*53e3b81fSAndy Wingo uint32_t flags, InputFile *file, 601*53e3b81fSAndy Wingo const WasmTableType *type) { 602*53e3b81fSAndy Wingo LLVM_DEBUG(dbgs() << "addUndefinedTable: " << name << "\n"); 603*53e3b81fSAndy Wingo assert(flags & WASM_SYMBOL_UNDEFINED); 604*53e3b81fSAndy Wingo 605*53e3b81fSAndy Wingo Symbol *s; 606*53e3b81fSAndy Wingo bool wasInserted; 607*53e3b81fSAndy Wingo std::tie(s, wasInserted) = insert(name, file); 608*53e3b81fSAndy Wingo if (s->traced) 609*53e3b81fSAndy Wingo printTraceSymbolUndefined(name, file); 610*53e3b81fSAndy Wingo 611*53e3b81fSAndy Wingo if (wasInserted) 612*53e3b81fSAndy Wingo replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags, 613*53e3b81fSAndy Wingo file, type); 614*53e3b81fSAndy Wingo else if (auto *lazy = dyn_cast<LazySymbol>(s)) 615*53e3b81fSAndy Wingo lazy->fetch(); 616*53e3b81fSAndy Wingo else if (s->isDefined()) 617*53e3b81fSAndy Wingo checkTableType(s, file, type); 618*53e3b81fSAndy Wingo return s; 619*53e3b81fSAndy Wingo } 620*53e3b81fSAndy Wingo 621136d27abSRui Ueyama void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) { 622136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n"); 623136d27abSRui Ueyama StringRef name = sym->getName(); 624c03c9040SRui Ueyama 625136d27abSRui Ueyama Symbol *s; 626136d27abSRui Ueyama bool wasInserted; 627136d27abSRui Ueyama std::tie(s, wasInserted) = insertName(name); 628c03c9040SRui Ueyama 629136d27abSRui Ueyama if (wasInserted) { 630136d27abSRui Ueyama replaceSymbol<LazySymbol>(s, name, 0, file, *sym); 631c03c9040SRui Ueyama return; 632c03c9040SRui Ueyama } 633c03c9040SRui Ueyama 634136d27abSRui Ueyama if (!s->isUndefined()) 63537b4ee52SSam Clegg return; 63637b4ee52SSam Clegg 63737b4ee52SSam Clegg // The existing symbol is undefined, load a new one from the archive, 6387ae3d335SKazuaki Ishizaki // unless the existing symbol is weak in which case replace the undefined 63937b4ee52SSam Clegg // symbols with a LazySymbol. 640136d27abSRui Ueyama if (s->isWeak()) { 641136d27abSRui Ueyama const WasmSignature *oldSig = nullptr; 64237b4ee52SSam Clegg // In the case of an UndefinedFunction we need to preserve the expected 64337b4ee52SSam Clegg // signature. 644136d27abSRui Ueyama if (auto *f = dyn_cast<UndefinedFunction>(s)) 645136d27abSRui Ueyama oldSig = f->signature; 64637b4ee52SSam Clegg LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n"); 647136d27abSRui Ueyama auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK, 648136d27abSRui Ueyama file, *sym); 649136d27abSRui Ueyama newSym->signature = oldSig; 65037b4ee52SSam Clegg return; 65137b4ee52SSam Clegg } 65237b4ee52SSam Clegg 653e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); 654136d27abSRui Ueyama file->addMember(sym); 655c94d393aSSam Clegg } 656e0f6fcd0SSam Clegg 657136d27abSRui Ueyama bool SymbolTable::addComdat(StringRef name) { 658136d27abSRui Ueyama return comdatGroups.insert(CachedHashStringRef(name)).second; 659e0f6fcd0SSam Clegg } 6601f3f774fSSam Clegg 6616540e570SSam Clegg // The new signature doesn't match. Create a variant to the symbol with the 6626540e570SSam Clegg // signature encoded in the name and return that instead. These symbols are 6636540e570SSam Clegg // then unified later in handleSymbolVariants. 664136d27abSRui Ueyama bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig, 665136d27abSRui Ueyama const InputFile *file, Symbol **out) { 666136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> " 667136d27abSRui Ueyama << " " << toString(*sig) << "\n"); 668136d27abSRui Ueyama Symbol *variant = nullptr; 6696540e570SSam Clegg 6706540e570SSam Clegg // Linear search through symbol variants. Should never be more than two 6716540e570SSam Clegg // or three entries here. 672136d27abSRui Ueyama auto &variants = symVariants[CachedHashStringRef(sym->getName())]; 673136d27abSRui Ueyama if (variants.empty()) 674136d27abSRui Ueyama variants.push_back(sym); 6756540e570SSam Clegg 676136d27abSRui Ueyama for (Symbol* v : variants) { 677136d27abSRui Ueyama if (*v->getSignature() == *sig) { 678136d27abSRui Ueyama variant = v; 6796540e570SSam Clegg break; 6806540e570SSam Clegg } 6816540e570SSam Clegg } 6826540e570SSam Clegg 683136d27abSRui Ueyama bool wasAdded = !variant; 684136d27abSRui Ueyama if (wasAdded) { 6856540e570SSam Clegg // Create a new variant; 6866540e570SSam Clegg LLVM_DEBUG(dbgs() << "added new variant\n"); 687136d27abSRui Ueyama variant = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 688064e9907SSam Clegg variant->isUsedInRegularObj = 689064e9907SSam Clegg !file || file->kind() == InputFile::ObjectKind; 690064e9907SSam Clegg variant->canInline = true; 691064e9907SSam Clegg variant->traced = false; 692064e9907SSam Clegg variant->forceExport = false; 693136d27abSRui Ueyama variants.push_back(variant); 6946540e570SSam Clegg } else { 695136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n"); 696136d27abSRui Ueyama assert(*variant->getSignature() == *sig); 6976540e570SSam Clegg } 6986540e570SSam Clegg 699136d27abSRui Ueyama *out = variant; 700136d27abSRui Ueyama return wasAdded; 7016540e570SSam Clegg } 7026540e570SSam Clegg 7031f3f774fSSam Clegg // Set a flag for --trace-symbol so that we can print out a log message 7041f3f774fSSam Clegg // if a new symbol with the same name is inserted into the symbol table. 705136d27abSRui Ueyama void SymbolTable::trace(StringRef name) { 706136d27abSRui Ueyama symMap.insert({CachedHashStringRef(name), -1}); 7071f3f774fSSam Clegg } 708230dc11dSSam Clegg 709136d27abSRui Ueyama void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) { 710a5ca34e6SSam Clegg // Swap symbols as instructed by -wrap. 711136d27abSRui Ueyama int &origIdx = symMap[CachedHashStringRef(sym->getName())]; 712136d27abSRui Ueyama int &realIdx= symMap[CachedHashStringRef(real->getName())]; 713136d27abSRui Ueyama int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())]; 714136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n"); 715a5ca34e6SSam Clegg 716a5ca34e6SSam Clegg // Anyone looking up __real symbols should get the original 717136d27abSRui Ueyama realIdx = origIdx; 718a5ca34e6SSam Clegg // Anyone looking up the original should get the __wrap symbol 719136d27abSRui Ueyama origIdx = wrapIdx; 720a5ca34e6SSam Clegg } 721a5ca34e6SSam Clegg 722136d27abSRui Ueyama static const uint8_t unreachableFn[] = { 723230dc11dSSam Clegg 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, 724230dc11dSSam Clegg 0x00 /* opcode unreachable */, 0x0b /* opcode end */ 725230dc11dSSam Clegg }; 726230dc11dSSam Clegg 727230dc11dSSam Clegg // Replace the given symbol body with an unreachable function. 728230dc11dSSam Clegg // This is used by handleWeakUndefines in order to generate a callable 7296540e570SSam Clegg // equivalent of an undefined function and also handleSymbolVariants for 7306540e570SSam Clegg // undefined functions that don't match the signature of the definition. 731136d27abSRui Ueyama InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym, 732136d27abSRui Ueyama const WasmSignature &sig, 733136d27abSRui Ueyama StringRef debugName) { 734136d27abSRui Ueyama auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName); 735136d27abSRui Ueyama func->setBody(unreachableFn); 736136d27abSRui Ueyama syntheticFunctions.emplace_back(func); 7374b8e2d8eSSam Clegg // Mark new symbols as local. For relocatable output we don't want them 7384b8e2d8eSSam Clegg // to be exported outside the object file. 7394b8e2d8eSSam Clegg replaceSymbol<DefinedFunction>(sym, debugName, WASM_SYMBOL_BINDING_LOCAL, 7404b8e2d8eSSam Clegg nullptr, func); 74148ddf5e1SSam Clegg // Ensure the stub function doesn't get a table entry. Its address 74248ddf5e1SSam Clegg // should always compare equal to the null pointer. 74348ddf5e1SSam Clegg sym->isStub = true; 744136d27abSRui Ueyama return func; 745230dc11dSSam Clegg } 746230dc11dSSam Clegg 747206884bfSSam Clegg void SymbolTable::replaceWithUndefined(Symbol *sym) { 748206884bfSSam Clegg // Add a synthetic dummy for weak undefined functions. These dummies will 749206884bfSSam Clegg // be GC'd if not used as the target of any "call" instructions. 750206884bfSSam Clegg StringRef debugName = saver.save("undefined_weak:" + toString(*sym)); 751206884bfSSam Clegg replaceWithUnreachable(sym, *sym->getSignature(), debugName); 752206884bfSSam Clegg // Hide our dummy to prevent export. 753206884bfSSam Clegg sym->setHidden(true); 754206884bfSSam Clegg } 755206884bfSSam Clegg 756230dc11dSSam Clegg // For weak undefined functions, there may be "call" instructions that reference 757230dc11dSSam Clegg // the symbol. In this case, we need to synthesise a dummy/stub function that 758230dc11dSSam Clegg // will abort at runtime, so that relocations can still provided an operand to 759230dc11dSSam Clegg // the call instruction that passes Wasm validation. 760230dc11dSSam Clegg void SymbolTable::handleWeakUndefines() { 761136d27abSRui Ueyama for (Symbol *sym : getSymbols()) { 762206884bfSSam Clegg if (sym->isUndefWeak()) { 763206884bfSSam Clegg if (sym->getSignature()) { 764206884bfSSam Clegg replaceWithUndefined(sym); 765206884bfSSam Clegg } else { 766206884bfSSam Clegg // It is possible for undefined functions not to have a signature (eg. 767206884bfSSam Clegg // if added via "--undefined"), but weak undefined ones do have a 768206884bfSSam Clegg // signature. Lazy symbols may not be functions and therefore Sig can 769206884bfSSam Clegg // still be null in some circumstance. 770136d27abSRui Ueyama assert(!isa<FunctionSymbol>(sym)); 771206884bfSSam Clegg } 772206884bfSSam Clegg } 773206884bfSSam Clegg } 7746540e570SSam Clegg } 775230dc11dSSam Clegg 776206884bfSSam Clegg DefinedFunction *SymbolTable::createUndefinedStub(const WasmSignature &sig) { 777206884bfSSam Clegg if (stubFunctions.count(sig)) 778206884bfSSam Clegg return stubFunctions[sig]; 779206884bfSSam Clegg LLVM_DEBUG(dbgs() << "createUndefinedStub: " << toString(sig) << "\n"); 780206884bfSSam Clegg auto *sym = reinterpret_cast<DefinedFunction *>(make<SymbolUnion>()); 781206884bfSSam Clegg sym->isUsedInRegularObj = true; 782206884bfSSam Clegg sym->canInline = true; 783206884bfSSam Clegg sym->traced = false; 784206884bfSSam Clegg sym->forceExport = false; 785206884bfSSam Clegg sym->signature = &sig; 786206884bfSSam Clegg replaceSymbol<DefinedFunction>( 787206884bfSSam Clegg sym, "undefined_stub", WASM_SYMBOL_VISIBILITY_HIDDEN, nullptr, nullptr); 788206884bfSSam Clegg replaceWithUnreachable(sym, sig, "undefined_stub"); 789206884bfSSam Clegg stubFunctions[sig] = sym; 790206884bfSSam Clegg return sym; 791230dc11dSSam Clegg } 7926540e570SSam Clegg 793136d27abSRui Ueyama static void reportFunctionSignatureMismatch(StringRef symName, 794136d27abSRui Ueyama FunctionSymbol *a, 795136d27abSRui Ueyama FunctionSymbol *b, bool isError) { 796136d27abSRui Ueyama std::string msg = ("function signature mismatch: " + symName + 797136d27abSRui Ueyama "\n>>> defined as " + toString(*a->signature) + " in " + 798136d27abSRui Ueyama toString(a->getFile()) + "\n>>> defined as " + 799136d27abSRui Ueyama toString(*b->signature) + " in " + toString(b->getFile())) 8006540e570SSam Clegg .str(); 801136d27abSRui Ueyama if (isError) 8026540e570SSam Clegg error(msg); 8036540e570SSam Clegg else 8046540e570SSam Clegg warn(msg); 8056540e570SSam Clegg } 8066540e570SSam Clegg 8076540e570SSam Clegg // Remove any variant symbols that were created due to function signature 8086540e570SSam Clegg // mismatches. 8096540e570SSam Clegg void SymbolTable::handleSymbolVariants() { 810136d27abSRui Ueyama for (auto pair : symVariants) { 8116540e570SSam Clegg // Push the initial symbol onto the list of variants. 812136d27abSRui Ueyama StringRef symName = pair.first.val(); 813136d27abSRui Ueyama std::vector<Symbol *> &variants = pair.second; 8146540e570SSam Clegg 8156540e570SSam Clegg #ifndef NDEBUG 816136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "symbol with (" << variants.size() 817136d27abSRui Ueyama << ") variants: " << symName << "\n"); 818136d27abSRui Ueyama for (auto *s: variants) { 819136d27abSRui Ueyama auto *f = cast<FunctionSymbol>(s); 820136d27abSRui Ueyama LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " " 821136d27abSRui Ueyama << toString(*f->signature) << "\n"); 8226540e570SSam Clegg } 8236540e570SSam Clegg #endif 8246540e570SSam Clegg 8256540e570SSam Clegg // Find the one definition. 826136d27abSRui Ueyama DefinedFunction *defined = nullptr; 827136d27abSRui Ueyama for (auto *symbol : variants) { 828136d27abSRui Ueyama if (auto f = dyn_cast<DefinedFunction>(symbol)) { 829136d27abSRui Ueyama defined = f; 8306540e570SSam Clegg break; 8316540e570SSam Clegg } 8326540e570SSam Clegg } 8336540e570SSam Clegg 8346540e570SSam Clegg // If there are no definitions, and the undefined symbols disagree on 8356540e570SSam Clegg // the signature, there is not we can do since we don't know which one 8366540e570SSam Clegg // to use as the signature on the import. 837136d27abSRui Ueyama if (!defined) { 838136d27abSRui Ueyama reportFunctionSignatureMismatch(symName, 839136d27abSRui Ueyama cast<FunctionSymbol>(variants[0]), 840136d27abSRui Ueyama cast<FunctionSymbol>(variants[1]), true); 8416540e570SSam Clegg return; 8426540e570SSam Clegg } 8436540e570SSam Clegg 844136d27abSRui Ueyama for (auto *symbol : variants) { 845136d27abSRui Ueyama if (symbol != defined) { 846136d27abSRui Ueyama auto *f = cast<FunctionSymbol>(symbol); 847136d27abSRui Ueyama reportFunctionSignatureMismatch(symName, f, defined, false); 84851b521c0SSam Clegg StringRef debugName = saver.save("signature_mismatch:" + toString(*f)); 849136d27abSRui Ueyama replaceWithUnreachable(f, *f->signature, debugName); 8506540e570SSam Clegg } 8516540e570SSam Clegg } 8526540e570SSam Clegg } 8536540e570SSam Clegg } 85433c59abfSFangrui Song 85533c59abfSFangrui Song } // namespace wasm 85633c59abfSFangrui Song } // namespace lld 857