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