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, StringRef importName, 408 StringRef importModule, InputFile *file) { 409 if (!importName.empty()) { 410 if (existing->importName.empty()) 411 existing->importName = importName; 412 if (existing->importName != importName) 413 error("import name mismatch for symbol: " + toString(*existing) + 414 "\n>>> defined as " + existing->importName + " in " + 415 toString(existing->getFile()) + "\n>>> defined as " + importName + 416 " in " + toString(file)); 417 } 418 419 if (!importModule.empty()) { 420 if (existing->importModule.empty()) 421 existing->importModule = importModule; 422 if (existing->importModule != importModule) 423 error("import module mismatch for symbol: " + toString(*existing) + 424 "\n>>> defined as " + existing->importModule + " in " + 425 toString(existing->getFile()) + "\n>>> defined as " + importModule + 426 " in " + toString(file)); 427 } 428 } 429 430 Symbol *SymbolTable::addUndefinedFunction(StringRef name, StringRef importName, 431 StringRef importModule, 432 uint32_t flags, InputFile *file, 433 const WasmSignature *sig, 434 bool isCalledDirectly) { 435 LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " [" 436 << (sig ? toString(*sig) : "none") 437 << "] IsCalledDirectly:" << isCalledDirectly << "\n"); 438 assert(flags & WASM_SYMBOL_UNDEFINED); 439 440 Symbol *s; 441 bool wasInserted; 442 std::tie(s, wasInserted) = insert(name, file); 443 if (s->traced) 444 printTraceSymbolUndefined(name, file); 445 446 auto replaceSym = [&]() { 447 replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags, 448 file, sig, isCalledDirectly); 449 }; 450 451 if (wasInserted) 452 replaceSym(); 453 else if (auto *lazy = dyn_cast<LazySymbol>(s)) 454 lazy->fetch(); 455 else { 456 auto existingFunction = dyn_cast<FunctionSymbol>(s); 457 if (!existingFunction) { 458 reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION); 459 return s; 460 } 461 auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction); 462 if (!existingFunction->signature && sig) 463 existingFunction->signature = sig; 464 if (isCalledDirectly && !signatureMatches(existingFunction, sig)) { 465 // If the existing undefined functions is not called direcltly then let 466 // this one take precedence. Otherwise the existing function is either 467 // direclty called or defined, in which case we need a function variant. 468 if (existingUndefined && !existingUndefined->isCalledDirectly) 469 replaceSym(); 470 else if (getFunctionVariant(s, sig, file, &s)) 471 replaceSym(); 472 } 473 if (existingUndefined) 474 setImportAttributes(existingUndefined, importName, importModule, file); 475 } 476 477 return s; 478 } 479 480 Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags, 481 InputFile *file) { 482 LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n"); 483 assert(flags & WASM_SYMBOL_UNDEFINED); 484 485 Symbol *s; 486 bool wasInserted; 487 std::tie(s, wasInserted) = insert(name, file); 488 if (s->traced) 489 printTraceSymbolUndefined(name, file); 490 491 if (wasInserted) 492 replaceSymbol<UndefinedData>(s, name, flags, file); 493 else if (auto *lazy = dyn_cast<LazySymbol>(s)) 494 lazy->fetch(); 495 else if (s->isDefined()) 496 checkDataType(s, file); 497 return s; 498 } 499 500 Symbol *SymbolTable::addUndefinedGlobal(StringRef name, StringRef importName, 501 StringRef importModule, uint32_t flags, 502 InputFile *file, 503 const WasmGlobalType *type) { 504 LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n"); 505 assert(flags & WASM_SYMBOL_UNDEFINED); 506 507 Symbol *s; 508 bool wasInserted; 509 std::tie(s, wasInserted) = insert(name, file); 510 if (s->traced) 511 printTraceSymbolUndefined(name, file); 512 513 if (wasInserted) 514 replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags, 515 file, type); 516 else if (auto *lazy = dyn_cast<LazySymbol>(s)) 517 lazy->fetch(); 518 else if (s->isDefined()) 519 checkGlobalType(s, file, type); 520 return s; 521 } 522 523 void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) { 524 LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n"); 525 StringRef name = sym->getName(); 526 527 Symbol *s; 528 bool wasInserted; 529 std::tie(s, wasInserted) = insertName(name); 530 531 if (wasInserted) { 532 replaceSymbol<LazySymbol>(s, name, 0, file, *sym); 533 return; 534 } 535 536 if (!s->isUndefined()) 537 return; 538 539 // The existing symbol is undefined, load a new one from the archive, 540 // unless the existing symbol is weak in which case replace the undefined 541 // symbols with a LazySymbol. 542 if (s->isWeak()) { 543 const WasmSignature *oldSig = nullptr; 544 // In the case of an UndefinedFunction we need to preserve the expected 545 // signature. 546 if (auto *f = dyn_cast<UndefinedFunction>(s)) 547 oldSig = f->signature; 548 LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n"); 549 auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK, 550 file, *sym); 551 newSym->signature = oldSig; 552 return; 553 } 554 555 LLVM_DEBUG(dbgs() << "replacing existing undefined\n"); 556 file->addMember(sym); 557 } 558 559 bool SymbolTable::addComdat(StringRef name) { 560 return comdatGroups.insert(CachedHashStringRef(name)).second; 561 } 562 563 // The new signature doesn't match. Create a variant to the symbol with the 564 // signature encoded in the name and return that instead. These symbols are 565 // then unified later in handleSymbolVariants. 566 bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig, 567 const InputFile *file, Symbol **out) { 568 LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> " 569 << " " << toString(*sig) << "\n"); 570 Symbol *variant = nullptr; 571 572 // Linear search through symbol variants. Should never be more than two 573 // or three entries here. 574 auto &variants = symVariants[CachedHashStringRef(sym->getName())]; 575 if (variants.empty()) 576 variants.push_back(sym); 577 578 for (Symbol* v : variants) { 579 if (*v->getSignature() == *sig) { 580 variant = v; 581 break; 582 } 583 } 584 585 bool wasAdded = !variant; 586 if (wasAdded) { 587 // Create a new variant; 588 LLVM_DEBUG(dbgs() << "added new variant\n"); 589 variant = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 590 variants.push_back(variant); 591 } else { 592 LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n"); 593 assert(*variant->getSignature() == *sig); 594 } 595 596 *out = variant; 597 return wasAdded; 598 } 599 600 // Set a flag for --trace-symbol so that we can print out a log message 601 // if a new symbol with the same name is inserted into the symbol table. 602 void SymbolTable::trace(StringRef name) { 603 symMap.insert({CachedHashStringRef(name), -1}); 604 } 605 606 void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) { 607 // Swap symbols as instructed by -wrap. 608 int &origIdx = symMap[CachedHashStringRef(sym->getName())]; 609 int &realIdx= symMap[CachedHashStringRef(real->getName())]; 610 int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())]; 611 LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n"); 612 613 // Anyone looking up __real symbols should get the original 614 realIdx = origIdx; 615 // Anyone looking up the original should get the __wrap symbol 616 origIdx = wrapIdx; 617 } 618 619 static const uint8_t unreachableFn[] = { 620 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, 621 0x00 /* opcode unreachable */, 0x0b /* opcode end */ 622 }; 623 624 // Replace the given symbol body with an unreachable function. 625 // This is used by handleWeakUndefines in order to generate a callable 626 // equivalent of an undefined function and also handleSymbolVariants for 627 // undefined functions that don't match the signature of the definition. 628 InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym, 629 const WasmSignature &sig, 630 StringRef debugName) { 631 auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName); 632 func->setBody(unreachableFn); 633 syntheticFunctions.emplace_back(func); 634 replaceSymbol<DefinedFunction>(sym, sym->getName(), sym->getFlags(), nullptr, 635 func); 636 return func; 637 } 638 639 // For weak undefined functions, there may be "call" instructions that reference 640 // the symbol. In this case, we need to synthesise a dummy/stub function that 641 // will abort at runtime, so that relocations can still provided an operand to 642 // the call instruction that passes Wasm validation. 643 void SymbolTable::handleWeakUndefines() { 644 for (Symbol *sym : getSymbols()) { 645 if (!sym->isUndefWeak()) 646 continue; 647 648 const WasmSignature *sig = sym->getSignature(); 649 if (!sig) { 650 // It is possible for undefined functions not to have a signature (eg. if 651 // added via "--undefined"), but weak undefined ones do have a signature. 652 // Lazy symbols may not be functions and therefore Sig can still be null 653 // in some circumstance. 654 assert(!isa<FunctionSymbol>(sym)); 655 continue; 656 } 657 658 // Add a synthetic dummy for weak undefined functions. These dummies will 659 // be GC'd if not used as the target of any "call" instructions. 660 StringRef debugName = saver.save("undefined:" + toString(*sym)); 661 InputFunction* func = replaceWithUnreachable(sym, *sig, debugName); 662 // Ensure it compares equal to the null pointer, and so that table relocs 663 // don't pull in the stub body (only call-operand relocs should do that). 664 func->setTableIndex(0); 665 // Hide our dummy to prevent export. 666 sym->setHidden(true); 667 } 668 } 669 670 static void reportFunctionSignatureMismatch(StringRef symName, 671 FunctionSymbol *a, 672 FunctionSymbol *b, bool isError) { 673 std::string msg = ("function signature mismatch: " + symName + 674 "\n>>> defined as " + toString(*a->signature) + " in " + 675 toString(a->getFile()) + "\n>>> defined as " + 676 toString(*b->signature) + " in " + toString(b->getFile())) 677 .str(); 678 if (isError) 679 error(msg); 680 else 681 warn(msg); 682 } 683 684 // Remove any variant symbols that were created due to function signature 685 // mismatches. 686 void SymbolTable::handleSymbolVariants() { 687 for (auto pair : symVariants) { 688 // Push the initial symbol onto the list of variants. 689 StringRef symName = pair.first.val(); 690 std::vector<Symbol *> &variants = pair.second; 691 692 #ifndef NDEBUG 693 LLVM_DEBUG(dbgs() << "symbol with (" << variants.size() 694 << ") variants: " << symName << "\n"); 695 for (auto *s: variants) { 696 auto *f = cast<FunctionSymbol>(s); 697 LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " " 698 << toString(*f->signature) << "\n"); 699 } 700 #endif 701 702 // Find the one definition. 703 DefinedFunction *defined = nullptr; 704 for (auto *symbol : variants) { 705 if (auto f = dyn_cast<DefinedFunction>(symbol)) { 706 defined = f; 707 break; 708 } 709 } 710 711 // If there are no definitions, and the undefined symbols disagree on 712 // the signature, there is not we can do since we don't know which one 713 // to use as the signature on the import. 714 if (!defined) { 715 reportFunctionSignatureMismatch(symName, 716 cast<FunctionSymbol>(variants[0]), 717 cast<FunctionSymbol>(variants[1]), true); 718 return; 719 } 720 721 for (auto *symbol : variants) { 722 if (symbol != defined) { 723 auto *f = cast<FunctionSymbol>(symbol); 724 reportFunctionSignatureMismatch(symName, f, defined, false); 725 StringRef debugName = saver.save("signature_mismatch:" + toString(*f)); 726 replaceWithUnreachable(f, *f->signature, debugName); 727 } 728 } 729 } 730 } 731 732 } // namespace wasm 733 } // namespace lld 734