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" 12a56e5749SAndy Wingo #include "InputElement.h" 13b8621596SSam Clegg #include "WriterUtils.h" 14c94d393aSSam Clegg #include "lld/Common/ErrorHandler.h" 152017d52bSRui Ueyama #include "lld/Common/Memory.h" 167d67dd11SRui Ueyama #include "llvm/ADT/SetVector.h" 17c94d393aSSam Clegg 18c94d393aSSam Clegg #define DEBUG_TYPE "lld" 19c94d393aSSam Clegg 20c94d393aSSam Clegg using namespace llvm; 2120db381bSSam Clegg using namespace llvm::wasm; 2245218f4aSSam Clegg using namespace llvm::object; 23c94d393aSSam Clegg 2433c59abfSFangrui Song namespace lld { 2533c59abfSFangrui Song namespace wasm { 2633c59abfSFangrui Song SymbolTable *symtab; 27c94d393aSSam Clegg 28136d27abSRui Ueyama void SymbolTable::addFile(InputFile *file) { 29136d27abSRui Ueyama log("Processing: " + toString(file)); 30a282a61bSSam Clegg 31a282a61bSSam Clegg // .a file 32136d27abSRui Ueyama if (auto *f = dyn_cast<ArchiveFile>(file)) { 33136d27abSRui Ueyama f->parse(); 34a282a61bSSam Clegg return; 35a282a61bSSam Clegg } 36a282a61bSSam Clegg 37a282a61bSSam Clegg // .so file 38136d27abSRui Ueyama if (auto *f = dyn_cast<SharedFile>(file)) { 39136d27abSRui Ueyama sharedFiles.push_back(f); 40a282a61bSSam Clegg return; 41a282a61bSSam Clegg } 42a282a61bSSam Clegg 43136d27abSRui Ueyama if (config->trace) 44136d27abSRui Ueyama message(toString(file)); 45c94d393aSSam Clegg 46c729c1b4SSam Clegg // LLVM bitcode file 47136d27abSRui Ueyama if (auto *f = dyn_cast<BitcodeFile>(file)) { 48136d27abSRui Ueyama f->parse(); 49136d27abSRui Ueyama bitcodeFiles.push_back(f); 50a282a61bSSam Clegg return; 51a282a61bSSam Clegg } 52a282a61bSSam Clegg 53a282a61bSSam Clegg // Regular object file 54136d27abSRui Ueyama auto *f = cast<ObjFile>(file); 55136d27abSRui Ueyama f->parse(false); 56136d27abSRui Ueyama objectFiles.push_back(f); 57c94d393aSSam Clegg } 58c94d393aSSam Clegg 59c729c1b4SSam Clegg // This function is where all the optimizations of link-time 60c729c1b4SSam Clegg // optimization happens. When LTO is in use, some input files are 61c729c1b4SSam Clegg // not in native object file format but in the LLVM bitcode format. 62c729c1b4SSam Clegg // This function compiles bitcode files into a few big native files 63c729c1b4SSam Clegg // using LLVM functions and replaces bitcode symbols with the results. 64c729c1b4SSam Clegg // Because all bitcode files that the program consists of are passed 65c729c1b4SSam Clegg // to the compiler at once, it can do whole-program optimization. 66c729c1b4SSam Clegg void SymbolTable::addCombinedLTOObject() { 67b062fe18SSam Clegg // Prevent further LTO objects being included 68b062fe18SSam Clegg BitcodeFile::doneLTO = true; 69b062fe18SSam Clegg 70136d27abSRui Ueyama if (bitcodeFiles.empty()) 71c729c1b4SSam Clegg return; 72c729c1b4SSam Clegg 73c729c1b4SSam Clegg // Compile bitcode files and replace bitcode symbols. 74136d27abSRui Ueyama lto.reset(new BitcodeCompiler); 75136d27abSRui Ueyama for (BitcodeFile *f : bitcodeFiles) 76136d27abSRui Ueyama lto->add(*f); 77c729c1b4SSam Clegg 78136d27abSRui Ueyama for (StringRef filename : lto->compile()) { 79136d27abSRui Ueyama auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), ""); 80136d27abSRui Ueyama obj->parse(true); 81136d27abSRui Ueyama objectFiles.push_back(obj); 82c729c1b4SSam Clegg } 83c729c1b4SSam Clegg } 84c729c1b4SSam Clegg 85136d27abSRui Ueyama Symbol *SymbolTable::find(StringRef name) { 86136d27abSRui Ueyama auto it = symMap.find(CachedHashStringRef(name)); 87136d27abSRui Ueyama if (it == symMap.end() || it->second == -1) 881f3f774fSSam Clegg return nullptr; 89136d27abSRui Ueyama return symVector[it->second]; 901f3f774fSSam Clegg } 911f3f774fSSam Clegg 92136d27abSRui Ueyama void SymbolTable::replace(StringRef name, Symbol* sym) { 93136d27abSRui Ueyama auto it = symMap.find(CachedHashStringRef(name)); 94136d27abSRui Ueyama symVector[it->second] = sym; 956540e570SSam Clegg } 966540e570SSam Clegg 97136d27abSRui Ueyama std::pair<Symbol *, bool> SymbolTable::insertName(StringRef name) { 98136d27abSRui Ueyama bool trace = false; 99136d27abSRui Ueyama auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()}); 100136d27abSRui Ueyama int &symIndex = p.first->second; 101136d27abSRui Ueyama bool isNew = p.second; 102136d27abSRui Ueyama if (symIndex == -1) { 103136d27abSRui Ueyama symIndex = symVector.size(); 104136d27abSRui Ueyama trace = true; 105136d27abSRui Ueyama isNew = true; 1061f3f774fSSam Clegg } 1071f3f774fSSam Clegg 108136d27abSRui Ueyama if (!isNew) 109136d27abSRui Ueyama return {symVector[symIndex], false}; 1101f3f774fSSam Clegg 111136d27abSRui Ueyama Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 112136d27abSRui Ueyama sym->isUsedInRegularObj = false; 113136d27abSRui Ueyama sym->canInline = true; 114136d27abSRui Ueyama sym->traced = trace; 115064e9907SSam Clegg sym->forceExport = false; 116136d27abSRui Ueyama symVector.emplace_back(sym); 117136d27abSRui Ueyama return {sym, true}; 118c94d393aSSam Clegg } 119c94d393aSSam Clegg 120136d27abSRui Ueyama std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, 121136d27abSRui Ueyama const InputFile *file) { 122136d27abSRui Ueyama Symbol *s; 123136d27abSRui Ueyama bool wasInserted; 124136d27abSRui Ueyama std::tie(s, wasInserted) = insertName(name); 1251f3f774fSSam Clegg 126136d27abSRui Ueyama if (!file || file->kind() == InputFile::ObjectKind) 127136d27abSRui Ueyama s->isUsedInRegularObj = true; 1281f3f774fSSam Clegg 129136d27abSRui Ueyama return {s, wasInserted}; 130c94d393aSSam Clegg } 131c94d393aSSam Clegg 132136d27abSRui Ueyama static void reportTypeError(const Symbol *existing, const InputFile *file, 133136d27abSRui Ueyama llvm::wasm::WasmSymbolType type) { 134136d27abSRui Ueyama error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " + 135136d27abSRui Ueyama toString(existing->getWasmType()) + " in " + 136136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined as " + toString(type) + 137136d27abSRui Ueyama " in " + toString(file)); 138e3498ec5SRui Ueyama } 139e3498ec5SRui Ueyama 1406f4286fbSHeejin Ahn // Check the type of new symbol matches that of the symbol is replacing. 1417c5fcb35SKazuaki Ishizaki // Returns true if the function types match, false is there is a signature 1426540e570SSam Clegg // mismatch. 143136d27abSRui Ueyama static bool signatureMatches(FunctionSymbol *existing, 144136d27abSRui Ueyama const WasmSignature *newSig) { 145136d27abSRui Ueyama const WasmSignature *oldSig = existing->signature; 146d506b0a4SSam Clegg 147d506b0a4SSam Clegg // If either function is missing a signature (this happend for bitcode 148d506b0a4SSam Clegg // symbols) then assume they match. Any mismatch will be reported later 149d506b0a4SSam Clegg // when the LTO objects are added. 150136d27abSRui Ueyama if (!newSig || !oldSig) 1516540e570SSam Clegg return true; 152cefbf9acSSam Clegg 153136d27abSRui Ueyama return *newSig == *oldSig; 154e3498ec5SRui Ueyama } 155e3498ec5SRui Ueyama 156136d27abSRui Ueyama static void checkGlobalType(const Symbol *existing, const InputFile *file, 157136d27abSRui Ueyama const WasmGlobalType *newType) { 158136d27abSRui Ueyama if (!isa<GlobalSymbol>(existing)) { 159136d27abSRui Ueyama reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL); 160b8621596SSam Clegg return; 161b8621596SSam Clegg } 162b8621596SSam Clegg 163136d27abSRui Ueyama const WasmGlobalType *oldType = cast<GlobalSymbol>(existing)->getGlobalType(); 164136d27abSRui Ueyama if (*newType != *oldType) { 165136d27abSRui Ueyama error("Global type mismatch: " + existing->getName() + "\n>>> defined as " + 166136d27abSRui Ueyama toString(*oldType) + " in " + toString(existing->getFile()) + 167136d27abSRui Ueyama "\n>>> defined as " + toString(*newType) + " in " + toString(file)); 168c94d393aSSam Clegg } 16924b3dcd4SSam Clegg } 17024b3dcd4SSam Clegg 1711d891d44SHeejin Ahn static void checkTagType(const Symbol *existing, const InputFile *file, 172136d27abSRui Ueyama const WasmSignature *newSig) { 1731d891d44SHeejin Ahn const auto *existingTag = dyn_cast<TagSymbol>(existing); 1741d891d44SHeejin Ahn if (!isa<TagSymbol>(existing)) { 1751d891d44SHeejin Ahn reportTypeError(existing, file, WASM_SYMBOL_TYPE_TAG); 176e915a71fSHeejin Ahn return; 177e915a71fSHeejin Ahn } 178e915a71fSHeejin Ahn 1791d891d44SHeejin Ahn const WasmSignature *oldSig = existingTag->signature; 180136d27abSRui Ueyama if (*newSig != *oldSig) 1811d891d44SHeejin Ahn warn("Tag signature mismatch: " + existing->getName() + 182136d27abSRui Ueyama "\n>>> defined as " + toString(*oldSig) + " in " + 183136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined as " + 184136d27abSRui Ueyama toString(*newSig) + " in " + toString(file)); 185e915a71fSHeejin Ahn } 186e915a71fSHeejin Ahn 18753e3b81fSAndy Wingo static void checkTableType(const Symbol *existing, const InputFile *file, 18853e3b81fSAndy Wingo const WasmTableType *newType) { 18953e3b81fSAndy Wingo if (!isa<TableSymbol>(existing)) { 19053e3b81fSAndy Wingo reportTypeError(existing, file, WASM_SYMBOL_TYPE_TABLE); 19153e3b81fSAndy Wingo return; 19253e3b81fSAndy Wingo } 19353e3b81fSAndy Wingo 19453e3b81fSAndy Wingo const WasmTableType *oldType = cast<TableSymbol>(existing)->getTableType(); 19553e3b81fSAndy Wingo if (newType->ElemType != oldType->ElemType) { 19653e3b81fSAndy Wingo error("Table type mismatch: " + existing->getName() + "\n>>> defined as " + 19753e3b81fSAndy Wingo toString(*oldType) + " in " + toString(existing->getFile()) + 19853e3b81fSAndy Wingo "\n>>> defined as " + toString(*newType) + " in " + toString(file)); 19953e3b81fSAndy Wingo } 20053e3b81fSAndy Wingo // FIXME: No assertions currently on the limits. 20153e3b81fSAndy Wingo } 20253e3b81fSAndy Wingo 203136d27abSRui Ueyama static void checkDataType(const Symbol *existing, const InputFile *file) { 204136d27abSRui Ueyama if (!isa<DataSymbol>(existing)) 205136d27abSRui Ueyama reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA); 206e3498ec5SRui Ueyama } 207e3498ec5SRui Ueyama 208136d27abSRui Ueyama DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name, 209136d27abSRui Ueyama uint32_t flags, 210136d27abSRui Ueyama InputFunction *function) { 211136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n"); 212136d27abSRui Ueyama assert(!find(name)); 213136d27abSRui Ueyama syntheticFunctions.emplace_back(function); 214136d27abSRui Ueyama return replaceSymbol<DefinedFunction>(insertName(name).first, name, 215136d27abSRui Ueyama flags, nullptr, function); 21650686856SSam Clegg } 21750686856SSam Clegg 21829a3056bSSam Clegg // Adds an optional, linker generated, data symbol. The symbol will only be 2197ae3d335SKazuaki Ishizaki // added if there is an undefine reference to it, or if it is explicitly 2207ae3d335SKazuaki Ishizaki // exported via the --export flag. Otherwise we don't add the symbol and return 2217ae3d335SKazuaki Ishizaki // nullptr. 222caa0db13SSam Clegg DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name, 223582fd474SWouter van Oortmerssen uint64_t value) { 224136d27abSRui Ueyama Symbol *s = find(name); 225136d27abSRui Ueyama if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0)) 226136d27abSRui Ueyama s = insertName(name).first; 227136d27abSRui Ueyama else if (!s || s->isDefined()) 2284bce63a0SSam Clegg return nullptr; 229136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n"); 230caa0db13SSam Clegg auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN); 23114ffbb84SSam Clegg rtn->setVA(value); 232136d27abSRui Ueyama rtn->referenced = true; 2334bce63a0SSam Clegg return rtn; 2344bce63a0SSam Clegg } 2354bce63a0SSam Clegg 236136d27abSRui Ueyama DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name, 237136d27abSRui Ueyama uint32_t flags) { 238136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n"); 239136d27abSRui Ueyama assert(!find(name)); 240136d27abSRui Ueyama return replaceSymbol<DefinedData>(insertName(name).first, name, flags); 241c94d393aSSam Clegg } 242c94d393aSSam Clegg 243136d27abSRui Ueyama DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags, 244136d27abSRui Ueyama InputGlobal *global) { 245136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global 246e7245b42SNicola Zaghen << "\n"); 247136d27abSRui Ueyama assert(!find(name)); 248136d27abSRui Ueyama syntheticGlobals.emplace_back(global); 249136d27abSRui Ueyama return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags, 250136d27abSRui Ueyama nullptr, global); 25193102974SSam Clegg } 25293102974SSam Clegg 25388e4056bSSam Clegg DefinedGlobal *SymbolTable::addOptionalGlobalSymbol(StringRef name, 25429a3056bSSam Clegg InputGlobal *global) { 25529a3056bSSam Clegg Symbol *s = find(name); 25629a3056bSSam Clegg if (!s || s->isDefined()) 25729a3056bSSam Clegg return nullptr; 2588fe12847SSam Clegg LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name << " -> " << global 2598fe12847SSam Clegg << "\n"); 26029a3056bSSam Clegg syntheticGlobals.emplace_back(global); 26188e4056bSSam Clegg return replaceSymbol<DefinedGlobal>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN, 26288e4056bSSam Clegg nullptr, global); 26329a3056bSSam Clegg } 26429a3056bSSam Clegg 26563393828SAndy Wingo DefinedTable *SymbolTable::addSyntheticTable(StringRef name, uint32_t flags, 26663393828SAndy Wingo InputTable *table) { 26763393828SAndy Wingo LLVM_DEBUG(dbgs() << "addSyntheticTable: " << name << " -> " << table 26863393828SAndy Wingo << "\n"); 26963393828SAndy Wingo Symbol *s = find(name); 27063393828SAndy Wingo assert(!s || s->isUndefined()); 27163393828SAndy Wingo if (!s) 27263393828SAndy Wingo s = insertName(name).first; 27363393828SAndy Wingo syntheticTables.emplace_back(table); 27463393828SAndy Wingo return replaceSymbol<DefinedTable>(s, name, flags, nullptr, table); 27563393828SAndy Wingo } 27663393828SAndy Wingo 277136d27abSRui Ueyama static bool shouldReplace(const Symbol *existing, InputFile *newFile, 278136d27abSRui Ueyama uint32_t newFlags) { 279c03c9040SRui Ueyama // If existing symbol is undefined, replace it. 280136d27abSRui Ueyama if (!existing->isDefined()) { 281e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " 282136d27abSRui Ueyama << existing->getName() << "\n"); 283c03c9040SRui Ueyama return true; 284c03c9040SRui Ueyama } 285c03c9040SRui Ueyama 286c03c9040SRui Ueyama // Now we have two defined symbols. If the new one is weak, we can ignore it. 287136d27abSRui Ueyama if ((newFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { 288e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); 289c03c9040SRui Ueyama return false; 290c03c9040SRui Ueyama } 291c03c9040SRui Ueyama 292c03c9040SRui Ueyama // If the existing symbol is weak, we should replace it. 293136d27abSRui Ueyama if (existing->isWeak()) { 294e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); 295c03c9040SRui Ueyama return true; 296c03c9040SRui Ueyama } 297c03c9040SRui Ueyama 298c03c9040SRui Ueyama // Neither symbol is week. They conflict. 299136d27abSRui Ueyama error("duplicate symbol: " + toString(*existing) + "\n>>> defined in " + 300136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined in " + 301136d27abSRui Ueyama toString(newFile)); 302c03c9040SRui Ueyama return true; 303b8621596SSam Clegg } 30493e559b1SSam Clegg 305136d27abSRui Ueyama Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags, 306136d27abSRui Ueyama InputFile *file, 307136d27abSRui Ueyama InputFunction *function) { 308136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " [" 309136d27abSRui Ueyama << (function ? toString(function->signature) : "none") 3108b0b48f3SSam Clegg << "]\n"); 311136d27abSRui Ueyama Symbol *s; 312136d27abSRui Ueyama bool wasInserted; 313136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 314c729c1b4SSam Clegg 315136d27abSRui Ueyama auto replaceSym = [&](Symbol *sym) { 3167c5fcb35SKazuaki Ishizaki // If the new defined function doesn't have signature (i.e. bitcode 3176540e570SSam Clegg // functions) but the old symbol does, then preserve the old signature 318136d27abSRui Ueyama const WasmSignature *oldSig = s->getSignature(); 319136d27abSRui Ueyama auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function); 320136d27abSRui Ueyama if (!newSym->signature) 321136d27abSRui Ueyama newSym->signature = oldSig; 3226540e570SSam Clegg }; 3236540e570SSam Clegg 324136d27abSRui Ueyama if (wasInserted || s->isLazy()) { 325136d27abSRui Ueyama replaceSym(s); 326136d27abSRui Ueyama return s; 327e3498ec5SRui Ueyama } 328e3498ec5SRui Ueyama 329136d27abSRui Ueyama auto existingFunction = dyn_cast<FunctionSymbol>(s); 330136d27abSRui Ueyama if (!existingFunction) { 331136d27abSRui Ueyama reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION); 332136d27abSRui Ueyama return s; 3338b0b48f3SSam Clegg } 3346540e570SSam Clegg 335136d27abSRui Ueyama bool checkSig = true; 336136d27abSRui Ueyama if (auto ud = dyn_cast<UndefinedFunction>(existingFunction)) 337136d27abSRui Ueyama checkSig = ud->isCalledDirectly; 33859f959ffSSam Clegg 339136d27abSRui Ueyama if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) { 340136d27abSRui Ueyama Symbol* variant; 341136d27abSRui Ueyama if (getFunctionVariant(s, &function->signature, file, &variant)) 3426540e570SSam Clegg // New variant, always replace 343136d27abSRui Ueyama replaceSym(variant); 344136d27abSRui Ueyama else if (shouldReplace(s, file, flags)) 3456540e570SSam Clegg // Variant already exists, replace it after checking shouldReplace 346136d27abSRui Ueyama replaceSym(variant); 3476540e570SSam Clegg 3486540e570SSam Clegg // This variant we found take the place in the symbol table as the primary 3496540e570SSam Clegg // variant. 350136d27abSRui Ueyama replace(name, variant); 351136d27abSRui Ueyama return variant; 3526540e570SSam Clegg } 3536540e570SSam Clegg 3546540e570SSam Clegg // Existing function with matching signature. 355136d27abSRui Ueyama if (shouldReplace(s, file, flags)) 356136d27abSRui Ueyama replaceSym(s); 3576540e570SSam Clegg 358136d27abSRui Ueyama return s; 35993e559b1SSam Clegg } 36093e559b1SSam Clegg 361136d27abSRui Ueyama Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags, 3625a9b25e1SSam Clegg InputFile *file, InputChunk *segment, 3633b29376eSWouter van Oortmerssen uint64_t address, uint64_t size) { 364136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address 365e7245b42SNicola Zaghen << "\n"); 366136d27abSRui Ueyama Symbol *s; 367136d27abSRui Ueyama bool wasInserted; 368136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 369c729c1b4SSam Clegg 370136d27abSRui Ueyama auto replaceSym = [&]() { 371136d27abSRui Ueyama replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size); 3726540e570SSam Clegg }; 3736540e570SSam Clegg 374136d27abSRui Ueyama if (wasInserted || s->isLazy()) { 375136d27abSRui Ueyama replaceSym(); 376136d27abSRui Ueyama return s; 377e3498ec5SRui Ueyama } 378e3498ec5SRui Ueyama 379136d27abSRui Ueyama checkDataType(s, file); 380e3498ec5SRui Ueyama 381136d27abSRui Ueyama if (shouldReplace(s, file, flags)) 382136d27abSRui Ueyama replaceSym(); 383136d27abSRui Ueyama return s; 384c94d393aSSam Clegg } 385c94d393aSSam Clegg 386136d27abSRui Ueyama Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags, 387136d27abSRui Ueyama InputFile *file, InputGlobal *global) { 388136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n"); 3894c2cbfe6SSam Clegg 390136d27abSRui Ueyama Symbol *s; 391136d27abSRui Ueyama bool wasInserted; 392136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 393c729c1b4SSam Clegg 394136d27abSRui Ueyama auto replaceSym = [&]() { 395136d27abSRui Ueyama replaceSymbol<DefinedGlobal>(s, name, flags, file, global); 3966540e570SSam Clegg }; 3976540e570SSam Clegg 398136d27abSRui Ueyama if (wasInserted || s->isLazy()) { 399136d27abSRui Ueyama replaceSym(); 400136d27abSRui Ueyama return s; 40193102974SSam Clegg } 40293102974SSam Clegg 403136d27abSRui Ueyama checkGlobalType(s, file, &global->getType()); 404e3498ec5SRui Ueyama 405136d27abSRui Ueyama if (shouldReplace(s, file, flags)) 406136d27abSRui Ueyama replaceSym(); 407136d27abSRui Ueyama return s; 408e3498ec5SRui Ueyama } 409e3498ec5SRui Ueyama 4101d891d44SHeejin Ahn Symbol *SymbolTable::addDefinedTag(StringRef name, uint32_t flags, 4111d891d44SHeejin Ahn InputFile *file, InputTag *tag) { 4121d891d44SHeejin Ahn LLVM_DEBUG(dbgs() << "addDefinedTag:" << name << "\n"); 413e915a71fSHeejin Ahn 414136d27abSRui Ueyama Symbol *s; 415136d27abSRui Ueyama bool wasInserted; 416136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 417e915a71fSHeejin Ahn 418136d27abSRui Ueyama auto replaceSym = [&]() { 4191d891d44SHeejin Ahn replaceSymbol<DefinedTag>(s, name, flags, file, tag); 4206540e570SSam Clegg }; 4216540e570SSam Clegg 422136d27abSRui Ueyama if (wasInserted || s->isLazy()) { 423136d27abSRui Ueyama replaceSym(); 424136d27abSRui Ueyama return s; 425e915a71fSHeejin Ahn } 426e915a71fSHeejin Ahn 4273ec1760dSHeejin Ahn checkTagType(s, file, &tag->signature); 428e915a71fSHeejin Ahn 429136d27abSRui Ueyama if (shouldReplace(s, file, flags)) 430136d27abSRui Ueyama replaceSym(); 431136d27abSRui Ueyama return s; 432e915a71fSHeejin Ahn } 433e915a71fSHeejin Ahn 43453e3b81fSAndy Wingo Symbol *SymbolTable::addDefinedTable(StringRef name, uint32_t flags, 43553e3b81fSAndy Wingo InputFile *file, InputTable *table) { 43653e3b81fSAndy Wingo LLVM_DEBUG(dbgs() << "addDefinedTable:" << name << "\n"); 43753e3b81fSAndy Wingo 43853e3b81fSAndy Wingo Symbol *s; 43953e3b81fSAndy Wingo bool wasInserted; 44053e3b81fSAndy Wingo std::tie(s, wasInserted) = insert(name, file); 44153e3b81fSAndy Wingo 44253e3b81fSAndy Wingo auto replaceSym = [&]() { 44353e3b81fSAndy Wingo replaceSymbol<DefinedTable>(s, name, flags, file, table); 44453e3b81fSAndy Wingo }; 44553e3b81fSAndy Wingo 44653e3b81fSAndy Wingo if (wasInserted || s->isLazy()) { 44753e3b81fSAndy Wingo replaceSym(); 44853e3b81fSAndy Wingo return s; 44953e3b81fSAndy Wingo } 45053e3b81fSAndy Wingo 45153e3b81fSAndy Wingo checkTableType(s, file, &table->getType()); 45253e3b81fSAndy Wingo 45353e3b81fSAndy Wingo if (shouldReplace(s, file, flags)) 45453e3b81fSAndy Wingo replaceSym(); 45553e3b81fSAndy Wingo return s; 45653e3b81fSAndy Wingo } 45753e3b81fSAndy Wingo 45867b05584SSam Clegg // This function get called when an undefined symbol is added, and there is 45967b05584SSam Clegg // already an existing one in the symbols table. In this case we check that 46067b05584SSam Clegg // custom 'import-module' and 'import-field' symbol attributes agree. 4617ae3d335SKazuaki Ishizaki // With LTO these attributes are not available when the bitcode is read and only 46267b05584SSam Clegg // become available when the LTO object is read. In this case we silently 46367b05584SSam Clegg // replace the empty attributes with the valid ones. 46467b05584SSam Clegg template <typename T> 465bd481277SSam Clegg static void setImportAttributes(T *existing, Optional<StringRef> importName, 466bd481277SSam Clegg Optional<StringRef> importModule, 467a57f1a54SSam Clegg uint32_t flags, InputFile *file) { 468bd481277SSam Clegg if (importName) { 469bd481277SSam Clegg if (!existing->importName) 47067b05584SSam Clegg existing->importName = importName; 47167b05584SSam Clegg if (existing->importName != importName) 47267b05584SSam Clegg error("import name mismatch for symbol: " + toString(*existing) + 473bd481277SSam Clegg "\n>>> defined as " + *existing->importName + " in " + 474bd481277SSam Clegg toString(existing->getFile()) + "\n>>> defined as " + *importName + 47567b05584SSam Clegg " in " + toString(file)); 47667b05584SSam Clegg } 47767b05584SSam Clegg 478bd481277SSam Clegg if (importModule) { 479bd481277SSam Clegg if (!existing->importModule) 48067b05584SSam Clegg existing->importModule = importModule; 48167b05584SSam Clegg if (existing->importModule != importModule) 48267b05584SSam Clegg error("import module mismatch for symbol: " + toString(*existing) + 483bd481277SSam Clegg "\n>>> defined as " + *existing->importModule + " in " + 484bd481277SSam Clegg toString(existing->getFile()) + "\n>>> defined as " + 485bd481277SSam Clegg *importModule + " in " + toString(file)); 48667b05584SSam Clegg } 487a57f1a54SSam Clegg 488a57f1a54SSam Clegg // Update symbol binding, if the existing symbol is weak 489a57f1a54SSam Clegg uint32_t binding = flags & WASM_SYMBOL_BINDING_MASK; 490a57f1a54SSam Clegg if (existing->isWeak() && binding != WASM_SYMBOL_BINDING_WEAK) { 491a57f1a54SSam Clegg existing->flags = (existing->flags & ~WASM_SYMBOL_BINDING_MASK) | binding; 492a57f1a54SSam Clegg } 49367b05584SSam Clegg } 49467b05584SSam Clegg 495bd481277SSam Clegg Symbol *SymbolTable::addUndefinedFunction(StringRef name, 496bd481277SSam Clegg Optional<StringRef> importName, 497bd481277SSam Clegg Optional<StringRef> importModule, 498136d27abSRui Ueyama uint32_t flags, InputFile *file, 499136d27abSRui Ueyama const WasmSignature *sig, 500136d27abSRui Ueyama bool isCalledDirectly) { 501136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " [" 502136d27abSRui Ueyama << (sig ? toString(*sig) : "none") 503a57f1a54SSam Clegg << "] IsCalledDirectly:" << isCalledDirectly << " flags=0x" 504a57f1a54SSam Clegg << utohexstr(flags) << "\n"); 505f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED); 506c03c9040SRui Ueyama 507136d27abSRui Ueyama Symbol *s; 508136d27abSRui Ueyama bool wasInserted; 509136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 510136d27abSRui Ueyama if (s->traced) 511136d27abSRui Ueyama printTraceSymbolUndefined(name, file); 512c729c1b4SSam Clegg 513136d27abSRui Ueyama auto replaceSym = [&]() { 514136d27abSRui Ueyama replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags, 515136d27abSRui Ueyama file, sig, isCalledDirectly); 5166540e570SSam Clegg }; 5176540e570SSam Clegg 518b34ec596SSam Clegg if (wasInserted) { 519136d27abSRui Ueyama replaceSym(); 520b34ec596SSam Clegg } else if (auto *lazy = dyn_cast<LazySymbol>(s)) { 521b34ec596SSam Clegg if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { 522b34ec596SSam Clegg lazy->setWeak(); 523b34ec596SSam Clegg lazy->signature = sig; 524b34ec596SSam Clegg } else { 525136d27abSRui Ueyama lazy->fetch(); 526b34ec596SSam Clegg } 527b34ec596SSam Clegg } else { 528136d27abSRui Ueyama auto existingFunction = dyn_cast<FunctionSymbol>(s); 529136d27abSRui Ueyama if (!existingFunction) { 530136d27abSRui Ueyama reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION); 531136d27abSRui Ueyama return s; 5326540e570SSam Clegg } 533136d27abSRui Ueyama if (!existingFunction->signature && sig) 534136d27abSRui Ueyama existingFunction->signature = sig; 535a57f1a54SSam Clegg auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction); 536fecfc592SSam Clegg if (isCalledDirectly && !signatureMatches(existingFunction, sig)) { 537a57f1a54SSam Clegg // If the existing undefined functions is not called directly then let 538fecfc592SSam Clegg // this one take precedence. Otherwise the existing function is either 539a57f1a54SSam Clegg // directly called or defined, in which case we need a function variant. 540fecfc592SSam Clegg if (existingUndefined && !existingUndefined->isCalledDirectly) 541136d27abSRui Ueyama replaceSym(); 542fecfc592SSam Clegg else if (getFunctionVariant(s, sig, file, &s)) 543fecfc592SSam Clegg replaceSym(); 544fecfc592SSam Clegg } 545e4888be7SSam Clegg if (existingUndefined) { 546a57f1a54SSam Clegg setImportAttributes(existingUndefined, importName, importModule, flags, 547a57f1a54SSam Clegg file); 548e4888be7SSam Clegg if (isCalledDirectly) 549e4888be7SSam Clegg existingUndefined->isCalledDirectly = true; 550e4888be7SSam Clegg } 5516540e570SSam Clegg } 552cefbf9acSSam Clegg 553136d27abSRui Ueyama return s; 554c03c9040SRui Ueyama } 555c03c9040SRui Ueyama 556136d27abSRui Ueyama Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags, 557136d27abSRui Ueyama InputFile *file) { 558136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n"); 559f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED); 56093102974SSam Clegg 561136d27abSRui Ueyama Symbol *s; 562136d27abSRui Ueyama bool wasInserted; 563136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 564136d27abSRui Ueyama if (s->traced) 565136d27abSRui Ueyama printTraceSymbolUndefined(name, file); 566f989a92dSSam Clegg 567b34ec596SSam Clegg if (wasInserted) { 568136d27abSRui Ueyama replaceSymbol<UndefinedData>(s, name, flags, file); 569b34ec596SSam Clegg } else if (auto *lazy = dyn_cast<LazySymbol>(s)) { 570b34ec596SSam Clegg if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) 571b34ec596SSam Clegg lazy->setWeak(); 572b34ec596SSam Clegg else 573136d27abSRui Ueyama lazy->fetch(); 574b34ec596SSam Clegg } else if (s->isDefined()) { 575136d27abSRui Ueyama checkDataType(s, file); 576b34ec596SSam Clegg } 577136d27abSRui Ueyama return s; 578c94d393aSSam Clegg } 579c94d393aSSam Clegg 580bd481277SSam Clegg Symbol *SymbolTable::addUndefinedGlobal(StringRef name, 581bd481277SSam Clegg Optional<StringRef> importName, 582bd481277SSam Clegg Optional<StringRef> importModule, 583bd481277SSam Clegg uint32_t flags, InputFile *file, 584136d27abSRui Ueyama const WasmGlobalType *type) { 585136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n"); 586f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED); 587e3498ec5SRui Ueyama 588136d27abSRui Ueyama Symbol *s; 589136d27abSRui Ueyama bool wasInserted; 590136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file); 591136d27abSRui Ueyama if (s->traced) 592136d27abSRui Ueyama printTraceSymbolUndefined(name, file); 593c729c1b4SSam Clegg 594136d27abSRui Ueyama if (wasInserted) 595136d27abSRui Ueyama replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags, 596136d27abSRui Ueyama file, type); 597136d27abSRui Ueyama else if (auto *lazy = dyn_cast<LazySymbol>(s)) 598136d27abSRui Ueyama lazy->fetch(); 599136d27abSRui Ueyama else if (s->isDefined()) 600136d27abSRui Ueyama checkGlobalType(s, file, type); 601136d27abSRui Ueyama return s; 602e3498ec5SRui Ueyama } 603e3498ec5SRui Ueyama 60453e3b81fSAndy Wingo Symbol *SymbolTable::addUndefinedTable(StringRef name, 60553e3b81fSAndy Wingo Optional<StringRef> importName, 60653e3b81fSAndy Wingo Optional<StringRef> importModule, 60753e3b81fSAndy Wingo uint32_t flags, InputFile *file, 60853e3b81fSAndy Wingo const WasmTableType *type) { 60953e3b81fSAndy Wingo LLVM_DEBUG(dbgs() << "addUndefinedTable: " << name << "\n"); 61053e3b81fSAndy Wingo assert(flags & WASM_SYMBOL_UNDEFINED); 61153e3b81fSAndy Wingo 61253e3b81fSAndy Wingo Symbol *s; 61353e3b81fSAndy Wingo bool wasInserted; 61453e3b81fSAndy Wingo std::tie(s, wasInserted) = insert(name, file); 61553e3b81fSAndy Wingo if (s->traced) 61653e3b81fSAndy Wingo printTraceSymbolUndefined(name, file); 61753e3b81fSAndy Wingo 61853e3b81fSAndy Wingo if (wasInserted) 61953e3b81fSAndy Wingo replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags, 62053e3b81fSAndy Wingo file, type); 62153e3b81fSAndy Wingo else if (auto *lazy = dyn_cast<LazySymbol>(s)) 62253e3b81fSAndy Wingo lazy->fetch(); 62353e3b81fSAndy Wingo else if (s->isDefined()) 62453e3b81fSAndy Wingo checkTableType(s, file, type); 62553e3b81fSAndy Wingo return s; 62653e3b81fSAndy Wingo } 62753e3b81fSAndy Wingo 628*9261ee32SHeejin Ahn Symbol *SymbolTable::addUndefinedTag(StringRef name, 629*9261ee32SHeejin Ahn Optional<StringRef> importName, 630*9261ee32SHeejin Ahn Optional<StringRef> importModule, 631*9261ee32SHeejin Ahn uint32_t flags, InputFile *file, 632*9261ee32SHeejin Ahn const WasmSignature *sig) { 633*9261ee32SHeejin Ahn LLVM_DEBUG(dbgs() << "addUndefinedTag: " << name << "\n"); 634*9261ee32SHeejin Ahn assert(flags & WASM_SYMBOL_UNDEFINED); 635*9261ee32SHeejin Ahn 636*9261ee32SHeejin Ahn Symbol *s; 637*9261ee32SHeejin Ahn bool wasInserted; 638*9261ee32SHeejin Ahn std::tie(s, wasInserted) = insert(name, file); 639*9261ee32SHeejin Ahn if (s->traced) 640*9261ee32SHeejin Ahn printTraceSymbolUndefined(name, file); 641*9261ee32SHeejin Ahn 642*9261ee32SHeejin Ahn if (wasInserted) 643*9261ee32SHeejin Ahn replaceSymbol<UndefinedTag>(s, name, importName, importModule, flags, file, 644*9261ee32SHeejin Ahn sig); 645*9261ee32SHeejin Ahn else if (auto *lazy = dyn_cast<LazySymbol>(s)) 646*9261ee32SHeejin Ahn lazy->fetch(); 647*9261ee32SHeejin Ahn else if (s->isDefined()) 648*9261ee32SHeejin Ahn checkTagType(s, file, sig); 649*9261ee32SHeejin Ahn return s; 650*9261ee32SHeejin Ahn } 651*9261ee32SHeejin Ahn 652e638d8b2SAndy Wingo TableSymbol *SymbolTable::createUndefinedIndirectFunctionTable(StringRef name) { 653e638d8b2SAndy Wingo WasmLimits limits{0, 0, 0}; // Set by the writer. 654e638d8b2SAndy Wingo WasmTableType *type = make<WasmTableType>(); 655e638d8b2SAndy Wingo type->ElemType = uint8_t(ValType::FUNCREF); 656e638d8b2SAndy Wingo type->Limits = limits; 657e638d8b2SAndy Wingo StringRef module(defaultModule); 658e638d8b2SAndy Wingo uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN; 659e638d8b2SAndy Wingo flags |= WASM_SYMBOL_UNDEFINED; 660e638d8b2SAndy Wingo Symbol *sym = addUndefinedTable(name, name, module, flags, nullptr, type); 661e638d8b2SAndy Wingo sym->markLive(); 662e638d8b2SAndy Wingo sym->forceExport = config->exportTable; 663e638d8b2SAndy Wingo return cast<TableSymbol>(sym); 664e638d8b2SAndy Wingo } 665e638d8b2SAndy Wingo 666e638d8b2SAndy Wingo TableSymbol *SymbolTable::createDefinedIndirectFunctionTable(StringRef name) { 667e638d8b2SAndy Wingo const uint32_t invalidIndex = -1; 668e638d8b2SAndy Wingo WasmLimits limits{0, 0, 0}; // Set by the writer. 669e638d8b2SAndy Wingo WasmTableType type{uint8_t(ValType::FUNCREF), limits}; 670e638d8b2SAndy Wingo WasmTable desc{invalidIndex, type, name}; 671e638d8b2SAndy Wingo InputTable *table = make<InputTable>(desc, nullptr); 672e638d8b2SAndy Wingo uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN; 673e638d8b2SAndy Wingo TableSymbol *sym = addSyntheticTable(name, flags, table); 674e638d8b2SAndy Wingo sym->markLive(); 675e638d8b2SAndy Wingo sym->forceExport = config->exportTable; 676e638d8b2SAndy Wingo return sym; 677e638d8b2SAndy Wingo } 678e638d8b2SAndy Wingo 679e638d8b2SAndy Wingo // Whether or not we need an indirect function table is usually a function of 680e638d8b2SAndy Wingo // whether an input declares a need for it. However sometimes it's possible for 681e638d8b2SAndy Wingo // no input to need the indirect function table, but then a late 682e638d8b2SAndy Wingo // addInternalGOTEntry causes a function to be allocated an address. In that 683e638d8b2SAndy Wingo // case address we synthesize a definition at the last minute. 684e638d8b2SAndy Wingo TableSymbol *SymbolTable::resolveIndirectFunctionTable(bool required) { 685e638d8b2SAndy Wingo Symbol *existing = find(functionTableName); 686e638d8b2SAndy Wingo if (existing) { 687e638d8b2SAndy Wingo if (!isa<TableSymbol>(existing)) { 688e638d8b2SAndy Wingo error(Twine("reserved symbol must be of type table: `") + 689e638d8b2SAndy Wingo functionTableName + "`"); 690e638d8b2SAndy Wingo return nullptr; 691e638d8b2SAndy Wingo } 692e638d8b2SAndy Wingo if (existing->isDefined()) { 693e638d8b2SAndy Wingo error(Twine("reserved symbol must not be defined in input files: `") + 694e638d8b2SAndy Wingo functionTableName + "`"); 695e638d8b2SAndy Wingo return nullptr; 696e638d8b2SAndy Wingo } 697e638d8b2SAndy Wingo } 698e638d8b2SAndy Wingo 699e638d8b2SAndy Wingo if (config->importTable) { 700e638d8b2SAndy Wingo if (existing) 701e638d8b2SAndy Wingo return cast<TableSymbol>(existing); 702e638d8b2SAndy Wingo if (required) 703e638d8b2SAndy Wingo return createUndefinedIndirectFunctionTable(functionTableName); 704e638d8b2SAndy Wingo } else if ((existing && existing->isLive()) || config->exportTable || 705e638d8b2SAndy Wingo required) { 706e638d8b2SAndy Wingo // A defined table is required. Either because the user request an exported 707e638d8b2SAndy Wingo // table or because the table symbol is already live. The existing table is 708e638d8b2SAndy Wingo // guaranteed to be undefined due to the check above. 709e638d8b2SAndy Wingo return createDefinedIndirectFunctionTable(functionTableName); 710e638d8b2SAndy Wingo } 711e638d8b2SAndy Wingo 712e638d8b2SAndy Wingo // An indirect function table will only be present in the symbol table if 713e638d8b2SAndy Wingo // needed by a reloc; if we get here, we don't need one. 714e638d8b2SAndy Wingo return nullptr; 715e638d8b2SAndy Wingo } 716e638d8b2SAndy Wingo 717136d27abSRui Ueyama void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) { 718136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n"); 719136d27abSRui Ueyama StringRef name = sym->getName(); 720c03c9040SRui Ueyama 721136d27abSRui Ueyama Symbol *s; 722136d27abSRui Ueyama bool wasInserted; 723136d27abSRui Ueyama std::tie(s, wasInserted) = insertName(name); 724c03c9040SRui Ueyama 725136d27abSRui Ueyama if (wasInserted) { 726136d27abSRui Ueyama replaceSymbol<LazySymbol>(s, name, 0, file, *sym); 727c03c9040SRui Ueyama return; 728c03c9040SRui Ueyama } 729c03c9040SRui Ueyama 730136d27abSRui Ueyama if (!s->isUndefined()) 73137b4ee52SSam Clegg return; 73237b4ee52SSam Clegg 73337b4ee52SSam Clegg // The existing symbol is undefined, load a new one from the archive, 7347ae3d335SKazuaki Ishizaki // unless the existing symbol is weak in which case replace the undefined 73537b4ee52SSam Clegg // symbols with a LazySymbol. 736136d27abSRui Ueyama if (s->isWeak()) { 737136d27abSRui Ueyama const WasmSignature *oldSig = nullptr; 73837b4ee52SSam Clegg // In the case of an UndefinedFunction we need to preserve the expected 73937b4ee52SSam Clegg // signature. 740136d27abSRui Ueyama if (auto *f = dyn_cast<UndefinedFunction>(s)) 741136d27abSRui Ueyama oldSig = f->signature; 74237b4ee52SSam Clegg LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n"); 743136d27abSRui Ueyama auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK, 744136d27abSRui Ueyama file, *sym); 745136d27abSRui Ueyama newSym->signature = oldSig; 74637b4ee52SSam Clegg return; 74737b4ee52SSam Clegg } 74837b4ee52SSam Clegg 749e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); 750136d27abSRui Ueyama file->addMember(sym); 751c94d393aSSam Clegg } 752e0f6fcd0SSam Clegg 753136d27abSRui Ueyama bool SymbolTable::addComdat(StringRef name) { 754136d27abSRui Ueyama return comdatGroups.insert(CachedHashStringRef(name)).second; 755e0f6fcd0SSam Clegg } 7561f3f774fSSam Clegg 7576540e570SSam Clegg // The new signature doesn't match. Create a variant to the symbol with the 7586540e570SSam Clegg // signature encoded in the name and return that instead. These symbols are 7596540e570SSam Clegg // then unified later in handleSymbolVariants. 760136d27abSRui Ueyama bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig, 761136d27abSRui Ueyama const InputFile *file, Symbol **out) { 762136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> " 763136d27abSRui Ueyama << " " << toString(*sig) << "\n"); 764136d27abSRui Ueyama Symbol *variant = nullptr; 7656540e570SSam Clegg 7666540e570SSam Clegg // Linear search through symbol variants. Should never be more than two 7676540e570SSam Clegg // or three entries here. 768136d27abSRui Ueyama auto &variants = symVariants[CachedHashStringRef(sym->getName())]; 769136d27abSRui Ueyama if (variants.empty()) 770136d27abSRui Ueyama variants.push_back(sym); 7716540e570SSam Clegg 772136d27abSRui Ueyama for (Symbol* v : variants) { 773136d27abSRui Ueyama if (*v->getSignature() == *sig) { 774136d27abSRui Ueyama variant = v; 7756540e570SSam Clegg break; 7766540e570SSam Clegg } 7776540e570SSam Clegg } 7786540e570SSam Clegg 779136d27abSRui Ueyama bool wasAdded = !variant; 780136d27abSRui Ueyama if (wasAdded) { 7816540e570SSam Clegg // Create a new variant; 7826540e570SSam Clegg LLVM_DEBUG(dbgs() << "added new variant\n"); 783136d27abSRui Ueyama variant = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 784064e9907SSam Clegg variant->isUsedInRegularObj = 785064e9907SSam Clegg !file || file->kind() == InputFile::ObjectKind; 786064e9907SSam Clegg variant->canInline = true; 787064e9907SSam Clegg variant->traced = false; 788064e9907SSam Clegg variant->forceExport = false; 789136d27abSRui Ueyama variants.push_back(variant); 7906540e570SSam Clegg } else { 791136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n"); 792136d27abSRui Ueyama assert(*variant->getSignature() == *sig); 7936540e570SSam Clegg } 7946540e570SSam Clegg 795136d27abSRui Ueyama *out = variant; 796136d27abSRui Ueyama return wasAdded; 7976540e570SSam Clegg } 7986540e570SSam Clegg 7991f3f774fSSam Clegg // Set a flag for --trace-symbol so that we can print out a log message 8001f3f774fSSam Clegg // if a new symbol with the same name is inserted into the symbol table. 801136d27abSRui Ueyama void SymbolTable::trace(StringRef name) { 802136d27abSRui Ueyama symMap.insert({CachedHashStringRef(name), -1}); 8031f3f774fSSam Clegg } 804230dc11dSSam Clegg 805136d27abSRui Ueyama void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) { 806a5ca34e6SSam Clegg // Swap symbols as instructed by -wrap. 807136d27abSRui Ueyama int &origIdx = symMap[CachedHashStringRef(sym->getName())]; 808136d27abSRui Ueyama int &realIdx= symMap[CachedHashStringRef(real->getName())]; 809136d27abSRui Ueyama int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())]; 810136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n"); 811a5ca34e6SSam Clegg 812a5ca34e6SSam Clegg // Anyone looking up __real symbols should get the original 813136d27abSRui Ueyama realIdx = origIdx; 814a5ca34e6SSam Clegg // Anyone looking up the original should get the __wrap symbol 815136d27abSRui Ueyama origIdx = wrapIdx; 816a5ca34e6SSam Clegg } 817a5ca34e6SSam Clegg 818136d27abSRui Ueyama static const uint8_t unreachableFn[] = { 819230dc11dSSam Clegg 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, 820230dc11dSSam Clegg 0x00 /* opcode unreachable */, 0x0b /* opcode end */ 821230dc11dSSam Clegg }; 822230dc11dSSam Clegg 823230dc11dSSam Clegg // Replace the given symbol body with an unreachable function. 824230dc11dSSam Clegg // This is used by handleWeakUndefines in order to generate a callable 8256540e570SSam Clegg // equivalent of an undefined function and also handleSymbolVariants for 8266540e570SSam Clegg // undefined functions that don't match the signature of the definition. 827136d27abSRui Ueyama InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym, 828136d27abSRui Ueyama const WasmSignature &sig, 829136d27abSRui Ueyama StringRef debugName) { 830136d27abSRui Ueyama auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName); 831136d27abSRui Ueyama func->setBody(unreachableFn); 832136d27abSRui Ueyama syntheticFunctions.emplace_back(func); 8334b8e2d8eSSam Clegg // Mark new symbols as local. For relocatable output we don't want them 8344b8e2d8eSSam Clegg // to be exported outside the object file. 8354b8e2d8eSSam Clegg replaceSymbol<DefinedFunction>(sym, debugName, WASM_SYMBOL_BINDING_LOCAL, 8364b8e2d8eSSam Clegg nullptr, func); 83748ddf5e1SSam Clegg // Ensure the stub function doesn't get a table entry. Its address 83848ddf5e1SSam Clegg // should always compare equal to the null pointer. 83948ddf5e1SSam Clegg sym->isStub = true; 840136d27abSRui Ueyama return func; 841230dc11dSSam Clegg } 842230dc11dSSam Clegg 843206884bfSSam Clegg void SymbolTable::replaceWithUndefined(Symbol *sym) { 844206884bfSSam Clegg // Add a synthetic dummy for weak undefined functions. These dummies will 845206884bfSSam Clegg // be GC'd if not used as the target of any "call" instructions. 846206884bfSSam Clegg StringRef debugName = saver.save("undefined_weak:" + toString(*sym)); 847206884bfSSam Clegg replaceWithUnreachable(sym, *sym->getSignature(), debugName); 848206884bfSSam Clegg // Hide our dummy to prevent export. 849206884bfSSam Clegg sym->setHidden(true); 850206884bfSSam Clegg } 851206884bfSSam Clegg 852230dc11dSSam Clegg // For weak undefined functions, there may be "call" instructions that reference 853230dc11dSSam Clegg // the symbol. In this case, we need to synthesise a dummy/stub function that 854230dc11dSSam Clegg // will abort at runtime, so that relocations can still provided an operand to 855230dc11dSSam Clegg // the call instruction that passes Wasm validation. 856230dc11dSSam Clegg void SymbolTable::handleWeakUndefines() { 857136d27abSRui Ueyama for (Symbol *sym : getSymbols()) { 858206884bfSSam Clegg if (sym->isUndefWeak()) { 859206884bfSSam Clegg if (sym->getSignature()) { 860206884bfSSam Clegg replaceWithUndefined(sym); 861206884bfSSam Clegg } else { 862206884bfSSam Clegg // It is possible for undefined functions not to have a signature (eg. 863206884bfSSam Clegg // if added via "--undefined"), but weak undefined ones do have a 864206884bfSSam Clegg // signature. Lazy symbols may not be functions and therefore Sig can 865206884bfSSam Clegg // still be null in some circumstance. 866136d27abSRui Ueyama assert(!isa<FunctionSymbol>(sym)); 867206884bfSSam Clegg } 868206884bfSSam Clegg } 869206884bfSSam Clegg } 8706540e570SSam Clegg } 871230dc11dSSam Clegg 872206884bfSSam Clegg DefinedFunction *SymbolTable::createUndefinedStub(const WasmSignature &sig) { 873206884bfSSam Clegg if (stubFunctions.count(sig)) 874206884bfSSam Clegg return stubFunctions[sig]; 875206884bfSSam Clegg LLVM_DEBUG(dbgs() << "createUndefinedStub: " << toString(sig) << "\n"); 876206884bfSSam Clegg auto *sym = reinterpret_cast<DefinedFunction *>(make<SymbolUnion>()); 877206884bfSSam Clegg sym->isUsedInRegularObj = true; 878206884bfSSam Clegg sym->canInline = true; 879206884bfSSam Clegg sym->traced = false; 880206884bfSSam Clegg sym->forceExport = false; 881206884bfSSam Clegg sym->signature = &sig; 882206884bfSSam Clegg replaceSymbol<DefinedFunction>( 883206884bfSSam Clegg sym, "undefined_stub", WASM_SYMBOL_VISIBILITY_HIDDEN, nullptr, nullptr); 884206884bfSSam Clegg replaceWithUnreachable(sym, sig, "undefined_stub"); 885206884bfSSam Clegg stubFunctions[sig] = sym; 886206884bfSSam Clegg return sym; 887230dc11dSSam Clegg } 8886540e570SSam Clegg 889136d27abSRui Ueyama static void reportFunctionSignatureMismatch(StringRef symName, 890136d27abSRui Ueyama FunctionSymbol *a, 891136d27abSRui Ueyama FunctionSymbol *b, bool isError) { 892136d27abSRui Ueyama std::string msg = ("function signature mismatch: " + symName + 893136d27abSRui Ueyama "\n>>> defined as " + toString(*a->signature) + " in " + 894136d27abSRui Ueyama toString(a->getFile()) + "\n>>> defined as " + 895136d27abSRui Ueyama toString(*b->signature) + " in " + toString(b->getFile())) 8966540e570SSam Clegg .str(); 897136d27abSRui Ueyama if (isError) 8986540e570SSam Clegg error(msg); 8996540e570SSam Clegg else 9006540e570SSam Clegg warn(msg); 9016540e570SSam Clegg } 9026540e570SSam Clegg 9036540e570SSam Clegg // Remove any variant symbols that were created due to function signature 9046540e570SSam Clegg // mismatches. 9056540e570SSam Clegg void SymbolTable::handleSymbolVariants() { 906136d27abSRui Ueyama for (auto pair : symVariants) { 9076540e570SSam Clegg // Push the initial symbol onto the list of variants. 908136d27abSRui Ueyama StringRef symName = pair.first.val(); 909136d27abSRui Ueyama std::vector<Symbol *> &variants = pair.second; 9106540e570SSam Clegg 9116540e570SSam Clegg #ifndef NDEBUG 912136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "symbol with (" << variants.size() 913136d27abSRui Ueyama << ") variants: " << symName << "\n"); 914136d27abSRui Ueyama for (auto *s: variants) { 915136d27abSRui Ueyama auto *f = cast<FunctionSymbol>(s); 916136d27abSRui Ueyama LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " " 917136d27abSRui Ueyama << toString(*f->signature) << "\n"); 9186540e570SSam Clegg } 9196540e570SSam Clegg #endif 9206540e570SSam Clegg 9216540e570SSam Clegg // Find the one definition. 922136d27abSRui Ueyama DefinedFunction *defined = nullptr; 923136d27abSRui Ueyama for (auto *symbol : variants) { 924136d27abSRui Ueyama if (auto f = dyn_cast<DefinedFunction>(symbol)) { 925136d27abSRui Ueyama defined = f; 9266540e570SSam Clegg break; 9276540e570SSam Clegg } 9286540e570SSam Clegg } 9296540e570SSam Clegg 9306540e570SSam Clegg // If there are no definitions, and the undefined symbols disagree on 9316540e570SSam Clegg // the signature, there is not we can do since we don't know which one 9326540e570SSam Clegg // to use as the signature on the import. 933136d27abSRui Ueyama if (!defined) { 934136d27abSRui Ueyama reportFunctionSignatureMismatch(symName, 935136d27abSRui Ueyama cast<FunctionSymbol>(variants[0]), 936136d27abSRui Ueyama cast<FunctionSymbol>(variants[1]), true); 9376540e570SSam Clegg return; 9386540e570SSam Clegg } 9396540e570SSam Clegg 940136d27abSRui Ueyama for (auto *symbol : variants) { 941136d27abSRui Ueyama if (symbol != defined) { 942136d27abSRui Ueyama auto *f = cast<FunctionSymbol>(symbol); 943136d27abSRui Ueyama reportFunctionSignatureMismatch(symName, f, defined, false); 94451b521c0SSam Clegg StringRef debugName = saver.save("signature_mismatch:" + toString(*f)); 945136d27abSRui Ueyama replaceWithUnreachable(f, *f->signature, debugName); 9466540e570SSam Clegg } 9476540e570SSam Clegg } 9486540e570SSam Clegg } 9496540e570SSam Clegg } 95033c59abfSFangrui Song 95133c59abfSFangrui Song } // namespace wasm 95233c59abfSFangrui Song } // namespace lld 953