1 //===- SymbolTable.cpp ----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "SymbolTable.h" 10 #include "Config.h" 11 #include "InputChunks.h" 12 #include "InputEvent.h" 13 #include "InputGlobal.h" 14 #include "WriterUtils.h" 15 #include "lld/Common/ErrorHandler.h" 16 #include "lld/Common/Memory.h" 17 #include "llvm/ADT/SetVector.h" 18 19 #define DEBUG_TYPE "lld" 20 21 using namespace llvm; 22 using namespace llvm::wasm; 23 using namespace llvm::object; 24 25 namespace lld { 26 namespace wasm { 27 SymbolTable *symtab; 28 29 void SymbolTable::addFile(InputFile *file) { 30 log("Processing: " + toString(file)); 31 32 // .a file 33 if (auto *f = dyn_cast<ArchiveFile>(file)) { 34 f->parse(); 35 return; 36 } 37 38 // .so file 39 if (auto *f = dyn_cast<SharedFile>(file)) { 40 sharedFiles.push_back(f); 41 return; 42 } 43 44 if (config->trace) 45 message(toString(file)); 46 47 // LLVM bitcode file 48 if (auto *f = dyn_cast<BitcodeFile>(file)) { 49 f->parse(); 50 bitcodeFiles.push_back(f); 51 return; 52 } 53 54 // Regular object file 55 auto *f = cast<ObjFile>(file); 56 f->parse(false); 57 objectFiles.push_back(f); 58 } 59 60 // This function is where all the optimizations of link-time 61 // optimization happens. When LTO is in use, some input files are 62 // not in native object file format but in the LLVM bitcode format. 63 // This function compiles bitcode files into a few big native files 64 // using LLVM functions and replaces bitcode symbols with the results. 65 // Because all bitcode files that the program consists of are passed 66 // to the compiler at once, it can do whole-program optimization. 67 void SymbolTable::addCombinedLTOObject() { 68 // Prevent further LTO objects being included 69 BitcodeFile::doneLTO = true; 70 71 if (bitcodeFiles.empty()) 72 return; 73 74 // Compile bitcode files and replace bitcode symbols. 75 lto.reset(new BitcodeCompiler); 76 for (BitcodeFile *f : bitcodeFiles) 77 lto->add(*f); 78 79 for (StringRef filename : lto->compile()) { 80 auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), ""); 81 obj->parse(true); 82 objectFiles.push_back(obj); 83 } 84 } 85 86 Symbol *SymbolTable::find(StringRef name) { 87 auto it = symMap.find(CachedHashStringRef(name)); 88 if (it == symMap.end() || it->second == -1) 89 return nullptr; 90 return symVector[it->second]; 91 } 92 93 void SymbolTable::replace(StringRef name, Symbol* sym) { 94 auto it = symMap.find(CachedHashStringRef(name)); 95 symVector[it->second] = sym; 96 } 97 98 std::pair<Symbol *, bool> SymbolTable::insertName(StringRef name) { 99 bool trace = false; 100 auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()}); 101 int &symIndex = p.first->second; 102 bool isNew = p.second; 103 if (symIndex == -1) { 104 symIndex = symVector.size(); 105 trace = true; 106 isNew = true; 107 } 108 109 if (!isNew) 110 return {symVector[symIndex], false}; 111 112 Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 113 sym->isUsedInRegularObj = false; 114 sym->canInline = true; 115 sym->traced = trace; 116 symVector.emplace_back(sym); 117 return {sym, true}; 118 } 119 120 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, 121 const InputFile *file) { 122 Symbol *s; 123 bool wasInserted; 124 std::tie(s, wasInserted) = insertName(name); 125 126 if (!file || file->kind() == InputFile::ObjectKind) 127 s->isUsedInRegularObj = true; 128 129 return {s, wasInserted}; 130 } 131 132 static void reportTypeError(const Symbol *existing, const InputFile *file, 133 llvm::wasm::WasmSymbolType type) { 134 error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " + 135 toString(existing->getWasmType()) + " in " + 136 toString(existing->getFile()) + "\n>>> defined as " + toString(type) + 137 " in " + toString(file)); 138 } 139 140 // Check the type of new symbol matches that of the symbol is replacing. 141 // Returns true if the function types match, false is there is a singature 142 // mismatch. 143 static bool signatureMatches(FunctionSymbol *existing, 144 const WasmSignature *newSig) { 145 const WasmSignature *oldSig = existing->signature; 146 147 // If either function is missing a signature (this happend for bitcode 148 // symbols) then assume they match. Any mismatch will be reported later 149 // when the LTO objects are added. 150 if (!newSig || !oldSig) 151 return true; 152 153 return *newSig == *oldSig; 154 } 155 156 static void checkGlobalType(const Symbol *existing, const InputFile *file, 157 const WasmGlobalType *newType) { 158 if (!isa<GlobalSymbol>(existing)) { 159 reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL); 160 return; 161 } 162 163 const WasmGlobalType *oldType = cast<GlobalSymbol>(existing)->getGlobalType(); 164 if (*newType != *oldType) { 165 error("Global type mismatch: " + existing->getName() + "\n>>> defined as " + 166 toString(*oldType) + " in " + toString(existing->getFile()) + 167 "\n>>> defined as " + toString(*newType) + " in " + toString(file)); 168 } 169 } 170 171 static void checkEventType(const Symbol *existing, const InputFile *file, 172 const WasmEventType *newType, 173 const WasmSignature *newSig) { 174 auto existingEvent = dyn_cast<EventSymbol>(existing); 175 if (!isa<EventSymbol>(existing)) { 176 reportTypeError(existing, file, WASM_SYMBOL_TYPE_EVENT); 177 return; 178 } 179 180 const WasmEventType *oldType = cast<EventSymbol>(existing)->getEventType(); 181 const WasmSignature *oldSig = existingEvent->signature; 182 if (newType->Attribute != oldType->Attribute) 183 error("Event type mismatch: " + existing->getName() + "\n>>> defined as " + 184 toString(*oldType) + " in " + toString(existing->getFile()) + 185 "\n>>> defined as " + toString(*newType) + " in " + toString(file)); 186 if (*newSig != *oldSig) 187 warn("Event signature mismatch: " + existing->getName() + 188 "\n>>> defined as " + toString(*oldSig) + " in " + 189 toString(existing->getFile()) + "\n>>> defined as " + 190 toString(*newSig) + " in " + toString(file)); 191 } 192 193 static void checkDataType(const Symbol *existing, const InputFile *file) { 194 if (!isa<DataSymbol>(existing)) 195 reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA); 196 } 197 198 DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name, 199 uint32_t flags, 200 InputFunction *function) { 201 LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n"); 202 assert(!find(name)); 203 syntheticFunctions.emplace_back(function); 204 return replaceSymbol<DefinedFunction>(insertName(name).first, name, 205 flags, nullptr, function); 206 } 207 208 // Adds an optional, linker generated, data symbols. The symbol will only be 209 // added if there is an undefine reference to it, or if it is explicitly 210 // exported via the --export flag. Otherwise we don't add the symbol and return 211 // nullptr. 212 DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name, 213 uint32_t value) { 214 Symbol *s = find(name); 215 if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0)) 216 s = insertName(name).first; 217 else if (!s || s->isDefined()) 218 return nullptr; 219 LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n"); 220 auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN); 221 rtn->setVirtualAddress(value); 222 rtn->referenced = true; 223 return rtn; 224 } 225 226 DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name, 227 uint32_t flags) { 228 LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n"); 229 assert(!find(name)); 230 return replaceSymbol<DefinedData>(insertName(name).first, name, flags); 231 } 232 233 DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags, 234 InputGlobal *global) { 235 LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global 236 << "\n"); 237 assert(!find(name)); 238 syntheticGlobals.emplace_back(global); 239 return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags, 240 nullptr, global); 241 } 242 243 static bool shouldReplace(const Symbol *existing, InputFile *newFile, 244 uint32_t newFlags) { 245 // If existing symbol is undefined, replace it. 246 if (!existing->isDefined()) { 247 LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " 248 << existing->getName() << "\n"); 249 return true; 250 } 251 252 // Now we have two defined symbols. If the new one is weak, we can ignore it. 253 if ((newFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { 254 LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n"); 255 return false; 256 } 257 258 // If the existing symbol is weak, we should replace it. 259 if (existing->isWeak()) { 260 LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n"); 261 return true; 262 } 263 264 // Neither symbol is week. They conflict. 265 error("duplicate symbol: " + toString(*existing) + "\n>>> defined in " + 266 toString(existing->getFile()) + "\n>>> defined in " + 267 toString(newFile)); 268 return true; 269 } 270 271 Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags, 272 InputFile *file, 273 InputFunction *function) { 274 LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " [" 275 << (function ? toString(function->signature) : "none") 276 << "]\n"); 277 Symbol *s; 278 bool wasInserted; 279 std::tie(s, wasInserted) = insert(name, file); 280 281 auto replaceSym = [&](Symbol *sym) { 282 // If the new defined function doesn't have signture (i.e. bitcode 283 // functions) but the old symbol does, then preserve the old signature 284 const WasmSignature *oldSig = s->getSignature(); 285 auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function); 286 if (!newSym->signature) 287 newSym->signature = oldSig; 288 }; 289 290 if (wasInserted || s->isLazy()) { 291 replaceSym(s); 292 return s; 293 } 294 295 auto existingFunction = dyn_cast<FunctionSymbol>(s); 296 if (!existingFunction) { 297 reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION); 298 return s; 299 } 300 301 bool checkSig = true; 302 if (auto ud = dyn_cast<UndefinedFunction>(existingFunction)) 303 checkSig = ud->isCalledDirectly; 304 305 if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) { 306 Symbol* variant; 307 if (getFunctionVariant(s, &function->signature, file, &variant)) 308 // New variant, always replace 309 replaceSym(variant); 310 else if (shouldReplace(s, file, flags)) 311 // Variant already exists, replace it after checking shouldReplace 312 replaceSym(variant); 313 314 // This variant we found take the place in the symbol table as the primary 315 // variant. 316 replace(name, variant); 317 return variant; 318 } 319 320 // Existing function with matching signature. 321 if (shouldReplace(s, file, flags)) 322 replaceSym(s); 323 324 return s; 325 } 326 327 Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags, 328 InputFile *file, InputSegment *segment, 329 uint32_t address, uint32_t size) { 330 LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address 331 << "\n"); 332 Symbol *s; 333 bool wasInserted; 334 std::tie(s, wasInserted) = insert(name, file); 335 336 auto replaceSym = [&]() { 337 replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size); 338 }; 339 340 if (wasInserted || s->isLazy()) { 341 replaceSym(); 342 return s; 343 } 344 345 checkDataType(s, file); 346 347 if (shouldReplace(s, file, flags)) 348 replaceSym(); 349 return s; 350 } 351 352 Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags, 353 InputFile *file, InputGlobal *global) { 354 LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n"); 355 356 Symbol *s; 357 bool wasInserted; 358 std::tie(s, wasInserted) = insert(name, file); 359 360 auto replaceSym = [&]() { 361 replaceSymbol<DefinedGlobal>(s, name, flags, file, global); 362 }; 363 364 if (wasInserted || s->isLazy()) { 365 replaceSym(); 366 return s; 367 } 368 369 checkGlobalType(s, file, &global->getType()); 370 371 if (shouldReplace(s, file, flags)) 372 replaceSym(); 373 return s; 374 } 375 376 Symbol *SymbolTable::addDefinedEvent(StringRef name, uint32_t flags, 377 InputFile *file, InputEvent *event) { 378 LLVM_DEBUG(dbgs() << "addDefinedEvent:" << name << "\n"); 379 380 Symbol *s; 381 bool wasInserted; 382 std::tie(s, wasInserted) = insert(name, file); 383 384 auto replaceSym = [&]() { 385 replaceSymbol<DefinedEvent>(s, name, flags, file, event); 386 }; 387 388 if (wasInserted || s->isLazy()) { 389 replaceSym(); 390 return s; 391 } 392 393 checkEventType(s, file, &event->getType(), &event->signature); 394 395 if (shouldReplace(s, file, flags)) 396 replaceSym(); 397 return s; 398 } 399 400 // This function get called when an undefined symbol is added, and there is 401 // already an existing one in the symbols table. In this case we check that 402 // custom 'import-module' and 'import-field' symbol attributes agree. 403 // With LTO these attributes are not available when the bitcode is read and only 404 // become available when the LTO object is read. In this case we silently 405 // replace the empty attributes with the valid ones. 406 template <typename T> 407 static void setImportAttributes(T *existing, Optional<StringRef> importName, 408 Optional<StringRef> importModule, 409 InputFile *file) { 410 if (importName) { 411 if (!existing->importName) 412 existing->importName = importName; 413 if (existing->importName != importName) 414 error("import name mismatch for symbol: " + toString(*existing) + 415 "\n>>> defined as " + *existing->importName + " in " + 416 toString(existing->getFile()) + "\n>>> defined as " + *importName + 417 " in " + toString(file)); 418 } 419 420 if (importModule) { 421 if (!existing->importModule) 422 existing->importModule = importModule; 423 if (existing->importModule != importModule) 424 error("import module mismatch for symbol: " + toString(*existing) + 425 "\n>>> defined as " + *existing->importModule + " in " + 426 toString(existing->getFile()) + "\n>>> defined as " + 427 *importModule + " in " + toString(file)); 428 } 429 } 430 431 Symbol *SymbolTable::addUndefinedFunction(StringRef name, 432 Optional<StringRef> importName, 433 Optional<StringRef> importModule, 434 uint32_t flags, InputFile *file, 435 const WasmSignature *sig, 436 bool isCalledDirectly) { 437 LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " [" 438 << (sig ? toString(*sig) : "none") 439 << "] IsCalledDirectly:" << isCalledDirectly << "\n"); 440 assert(flags & WASM_SYMBOL_UNDEFINED); 441 442 Symbol *s; 443 bool wasInserted; 444 std::tie(s, wasInserted) = insert(name, file); 445 if (s->traced) 446 printTraceSymbolUndefined(name, file); 447 448 auto replaceSym = [&]() { 449 replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags, 450 file, sig, isCalledDirectly); 451 }; 452 453 if (wasInserted) 454 replaceSym(); 455 else if (auto *lazy = dyn_cast<LazySymbol>(s)) 456 lazy->fetch(); 457 else { 458 auto existingFunction = dyn_cast<FunctionSymbol>(s); 459 if (!existingFunction) { 460 reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION); 461 return s; 462 } 463 auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction); 464 if (!existingFunction->signature && sig) 465 existingFunction->signature = sig; 466 if (isCalledDirectly && !signatureMatches(existingFunction, sig)) { 467 // If the existing undefined functions is not called direcltly then let 468 // this one take precedence. Otherwise the existing function is either 469 // direclty called or defined, in which case we need a function variant. 470 if (existingUndefined && !existingUndefined->isCalledDirectly) 471 replaceSym(); 472 else if (getFunctionVariant(s, sig, file, &s)) 473 replaceSym(); 474 } 475 if (existingUndefined) 476 setImportAttributes(existingUndefined, importName, importModule, file); 477 } 478 479 return s; 480 } 481 482 Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags, 483 InputFile *file) { 484 LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n"); 485 assert(flags & WASM_SYMBOL_UNDEFINED); 486 487 Symbol *s; 488 bool wasInserted; 489 std::tie(s, wasInserted) = insert(name, file); 490 if (s->traced) 491 printTraceSymbolUndefined(name, file); 492 493 if (wasInserted) 494 replaceSymbol<UndefinedData>(s, name, flags, file); 495 else if (auto *lazy = dyn_cast<LazySymbol>(s)) 496 lazy->fetch(); 497 else if (s->isDefined()) 498 checkDataType(s, file); 499 return s; 500 } 501 502 Symbol *SymbolTable::addUndefinedGlobal(StringRef name, 503 Optional<StringRef> importName, 504 Optional<StringRef> importModule, 505 uint32_t flags, InputFile *file, 506 const WasmGlobalType *type) { 507 LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n"); 508 assert(flags & WASM_SYMBOL_UNDEFINED); 509 510 Symbol *s; 511 bool wasInserted; 512 std::tie(s, wasInserted) = insert(name, file); 513 if (s->traced) 514 printTraceSymbolUndefined(name, file); 515 516 if (wasInserted) 517 replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags, 518 file, type); 519 else if (auto *lazy = dyn_cast<LazySymbol>(s)) 520 lazy->fetch(); 521 else if (s->isDefined()) 522 checkGlobalType(s, file, type); 523 return s; 524 } 525 526 void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) { 527 LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n"); 528 StringRef name = sym->getName(); 529 530 Symbol *s; 531 bool wasInserted; 532 std::tie(s, wasInserted) = insertName(name); 533 534 if (wasInserted) { 535 replaceSymbol<LazySymbol>(s, name, 0, file, *sym); 536 return; 537 } 538 539 if (!s->isUndefined()) 540 return; 541 542 // The existing symbol is undefined, load a new one from the archive, 543 // unless the existing symbol is weak in which case replace the undefined 544 // symbols with a LazySymbol. 545 if (s->isWeak()) { 546 const WasmSignature *oldSig = nullptr; 547 // In the case of an UndefinedFunction we need to preserve the expected 548 // signature. 549 if (auto *f = dyn_cast<UndefinedFunction>(s)) 550 oldSig = f->signature; 551 LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n"); 552 auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK, 553 file, *sym); 554 newSym->signature = oldSig; 555 return; 556 } 557 558 LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); 559 file->addMember(sym); 560 } 561 562 bool SymbolTable::addComdat(StringRef name) { 563 return comdatGroups.insert(CachedHashStringRef(name)).second; 564 } 565 566 // The new signature doesn't match. Create a variant to the symbol with the 567 // signature encoded in the name and return that instead. These symbols are 568 // then unified later in handleSymbolVariants. 569 bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig, 570 const InputFile *file, Symbol **out) { 571 LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> " 572 << " " << toString(*sig) << "\n"); 573 Symbol *variant = nullptr; 574 575 // Linear search through symbol variants. Should never be more than two 576 // or three entries here. 577 auto &variants = symVariants[CachedHashStringRef(sym->getName())]; 578 if (variants.empty()) 579 variants.push_back(sym); 580 581 for (Symbol* v : variants) { 582 if (*v->getSignature() == *sig) { 583 variant = v; 584 break; 585 } 586 } 587 588 bool wasAdded = !variant; 589 if (wasAdded) { 590 // Create a new variant; 591 LLVM_DEBUG(dbgs() << "added new variant\n"); 592 variant = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 593 variants.push_back(variant); 594 } else { 595 LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n"); 596 assert(*variant->getSignature() == *sig); 597 } 598 599 *out = variant; 600 return wasAdded; 601 } 602 603 // Set a flag for --trace-symbol so that we can print out a log message 604 // if a new symbol with the same name is inserted into the symbol table. 605 void SymbolTable::trace(StringRef name) { 606 symMap.insert({CachedHashStringRef(name), -1}); 607 } 608 609 void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) { 610 // Swap symbols as instructed by -wrap. 611 int &origIdx = symMap[CachedHashStringRef(sym->getName())]; 612 int &realIdx= symMap[CachedHashStringRef(real->getName())]; 613 int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())]; 614 LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n"); 615 616 // Anyone looking up __real symbols should get the original 617 realIdx = origIdx; 618 // Anyone looking up the original should get the __wrap symbol 619 origIdx = wrapIdx; 620 } 621 622 static const uint8_t unreachableFn[] = { 623 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, 624 0x00 /* opcode unreachable */, 0x0b /* opcode end */ 625 }; 626 627 // Replace the given symbol body with an unreachable function. 628 // This is used by handleWeakUndefines in order to generate a callable 629 // equivalent of an undefined function and also handleSymbolVariants for 630 // undefined functions that don't match the signature of the definition. 631 InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym, 632 const WasmSignature &sig, 633 StringRef debugName) { 634 auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName); 635 func->setBody(unreachableFn); 636 syntheticFunctions.emplace_back(func); 637 replaceSymbol<DefinedFunction>(sym, sym->getName(), sym->getFlags(), nullptr, 638 func); 639 return func; 640 } 641 642 // For weak undefined functions, there may be "call" instructions that reference 643 // the symbol. In this case, we need to synthesise a dummy/stub function that 644 // will abort at runtime, so that relocations can still provided an operand to 645 // the call instruction that passes Wasm validation. 646 void SymbolTable::handleWeakUndefines() { 647 for (Symbol *sym : getSymbols()) { 648 if (!sym->isUndefWeak()) 649 continue; 650 651 const WasmSignature *sig = sym->getSignature(); 652 if (!sig) { 653 // It is possible for undefined functions not to have a signature (eg. if 654 // added via "--undefined"), but weak undefined ones do have a signature. 655 // Lazy symbols may not be functions and therefore Sig can still be null 656 // in some circumstance. 657 assert(!isa<FunctionSymbol>(sym)); 658 continue; 659 } 660 661 // Add a synthetic dummy for weak undefined functions. These dummies will 662 // be GC'd if not used as the target of any "call" instructions. 663 StringRef debugName = saver.save("undefined:" + toString(*sym)); 664 InputFunction* func = replaceWithUnreachable(sym, *sig, debugName); 665 // Ensure it compares equal to the null pointer, and so that table relocs 666 // don't pull in the stub body (only call-operand relocs should do that). 667 func->setTableIndex(0); 668 // Hide our dummy to prevent export. 669 sym->setHidden(true); 670 } 671 } 672 673 static void reportFunctionSignatureMismatch(StringRef symName, 674 FunctionSymbol *a, 675 FunctionSymbol *b, bool isError) { 676 std::string msg = ("function signature mismatch: " + symName + 677 "\n>>> defined as " + toString(*a->signature) + " in " + 678 toString(a->getFile()) + "\n>>> defined as " + 679 toString(*b->signature) + " in " + toString(b->getFile())) 680 .str(); 681 if (isError) 682 error(msg); 683 else 684 warn(msg); 685 } 686 687 // Remove any variant symbols that were created due to function signature 688 // mismatches. 689 void SymbolTable::handleSymbolVariants() { 690 for (auto pair : symVariants) { 691 // Push the initial symbol onto the list of variants. 692 StringRef symName = pair.first.val(); 693 std::vector<Symbol *> &variants = pair.second; 694 695 #ifndef NDEBUG 696 LLVM_DEBUG(dbgs() << "symbol with (" << variants.size() 697 << ") variants: " << symName << "\n"); 698 for (auto *s: variants) { 699 auto *f = cast<FunctionSymbol>(s); 700 LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " " 701 << toString(*f->signature) << "\n"); 702 } 703 #endif 704 705 // Find the one definition. 706 DefinedFunction *defined = nullptr; 707 for (auto *symbol : variants) { 708 if (auto f = dyn_cast<DefinedFunction>(symbol)) { 709 defined = f; 710 break; 711 } 712 } 713 714 // If there are no definitions, and the undefined symbols disagree on 715 // the signature, there is not we can do since we don't know which one 716 // to use as the signature on the import. 717 if (!defined) { 718 reportFunctionSignatureMismatch(symName, 719 cast<FunctionSymbol>(variants[0]), 720 cast<FunctionSymbol>(variants[1]), true); 721 return; 722 } 723 724 for (auto *symbol : variants) { 725 if (symbol != defined) { 726 auto *f = cast<FunctionSymbol>(symbol); 727 reportFunctionSignatureMismatch(symName, f, defined, false); 728 StringRef debugName = saver.save("signature_mismatch:" + toString(*f)); 729 replaceWithUnreachable(f, *f->signature, debugName); 730 } 731 } 732 } 733 } 734 735 } // namespace wasm 736 } // namespace lld 737