1 //===- Symbols.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 "Symbols.h" 10 #include "InputFiles.h" 11 #include "InputSection.h" 12 #include "OutputSections.h" 13 #include "SyntheticSections.h" 14 #include "Target.h" 15 #include "Writer.h" 16 #include "lld/Common/ErrorHandler.h" 17 #include "lld/Common/Strings.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/Support/FileSystem.h" 20 #include "llvm/Support/Path.h" 21 #include <cstring> 22 23 using namespace llvm; 24 using namespace llvm::object; 25 using namespace llvm::ELF; 26 using namespace lld; 27 using namespace lld::elf; 28 29 std::string lld::toString(const elf::Symbol &sym) { 30 StringRef name = sym.getName(); 31 std::string ret = demangle(name, config->demangle); 32 33 const char *suffix = sym.getVersionSuffix(); 34 if (*suffix == '@') 35 ret += suffix; 36 return ret; 37 } 38 39 std::string lld::toELFString(const Archive::Symbol &b) { 40 return demangle(b.getName(), config->demangle); 41 } 42 43 Defined *ElfSym::bss; 44 Defined *ElfSym::etext1; 45 Defined *ElfSym::etext2; 46 Defined *ElfSym::edata1; 47 Defined *ElfSym::edata2; 48 Defined *ElfSym::end1; 49 Defined *ElfSym::end2; 50 Defined *ElfSym::globalOffsetTable; 51 Defined *ElfSym::mipsGp; 52 Defined *ElfSym::mipsGpDisp; 53 Defined *ElfSym::mipsLocalGp; 54 Defined *ElfSym::relaIpltStart; 55 Defined *ElfSym::relaIpltEnd; 56 Defined *ElfSym::riscvGlobalPointer; 57 Defined *ElfSym::tlsModuleBase; 58 DenseMap<const Symbol *, std::pair<const InputFile *, const InputFile *>> 59 elf::backwardReferences; 60 SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0> 61 elf::whyExtract; 62 63 static uint64_t getSymVA(const Symbol &sym, int64_t addend) { 64 switch (sym.kind()) { 65 case Symbol::DefinedKind: { 66 auto &d = cast<Defined>(sym); 67 SectionBase *isec = d.section; 68 69 // This is an absolute symbol. 70 if (!isec) 71 return d.value; 72 73 assert(isec != &InputSection::discarded); 74 75 uint64_t offset = d.value; 76 77 // An object in an SHF_MERGE section might be referenced via a 78 // section symbol (as a hack for reducing the number of local 79 // symbols). 80 // Depending on the addend, the reference via a section symbol 81 // refers to a different object in the merge section. 82 // Since the objects in the merge section are not necessarily 83 // contiguous in the output, the addend can thus affect the final 84 // VA in a non-linear way. 85 // To make this work, we incorporate the addend into the section 86 // offset (and zero out the addend for later processing) so that 87 // we find the right object in the section. 88 if (d.isSection()) 89 offset += addend; 90 91 // In the typical case, this is actually very simple and boils 92 // down to adding together 3 numbers: 93 // 1. The address of the output section. 94 // 2. The offset of the input section within the output section. 95 // 3. The offset within the input section (this addition happens 96 // inside InputSection::getOffset). 97 // 98 // If you understand the data structures involved with this next 99 // line (and how they get built), then you have a pretty good 100 // understanding of the linker. 101 uint64_t va = isec->getVA(offset); 102 if (d.isSection()) 103 va -= addend; 104 105 // MIPS relocatable files can mix regular and microMIPS code. 106 // Linker needs to distinguish such code. To do so microMIPS 107 // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other` 108 // field. Unfortunately, the `MIPS::relocate()` method has 109 // a symbol value only. To pass type of the symbol (regular/microMIPS) 110 // to that routine as well as other places where we write 111 // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry` 112 // field etc) do the same trick as compiler uses to mark microMIPS 113 // for CPU - set the less-significant bit. 114 if (config->emachine == EM_MIPS && isMicroMips() && 115 ((sym.stOther & STO_MIPS_MICROMIPS) || sym.needsCopy)) 116 va |= 1; 117 118 if (d.isTls() && !config->relocatable) { 119 // Use the address of the TLS segment's first section rather than the 120 // segment's address, because segment addresses aren't initialized until 121 // after sections are finalized. (e.g. Measuring the size of .rela.dyn 122 // for Android relocation packing requires knowing TLS symbol addresses 123 // during section finalization.) 124 if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec) 125 fatal(toString(d.file) + 126 " has an STT_TLS symbol but doesn't have an SHF_TLS section"); 127 return va - Out::tlsPhdr->firstSec->addr; 128 } 129 return va; 130 } 131 case Symbol::SharedKind: 132 case Symbol::UndefinedKind: 133 return 0; 134 case Symbol::LazyArchiveKind: 135 case Symbol::LazyObjectKind: 136 llvm_unreachable("lazy symbol reached writer"); 137 case Symbol::CommonKind: 138 llvm_unreachable("common symbol reached writer"); 139 case Symbol::PlaceholderKind: 140 llvm_unreachable("placeholder symbol reached writer"); 141 } 142 llvm_unreachable("invalid symbol kind"); 143 } 144 145 uint64_t Symbol::getVA(int64_t addend) const { 146 return getSymVA(*this, addend) + addend; 147 } 148 149 uint64_t Symbol::getGotVA() const { 150 if (gotInIgot) 151 return in.igotPlt->getVA() + getGotPltOffset(); 152 return in.got->getVA() + getGotOffset(); 153 } 154 155 uint64_t Symbol::getGotOffset() const { 156 return gotIndex * target->gotEntrySize; 157 } 158 159 uint64_t Symbol::getGotPltVA() const { 160 if (isInIplt) 161 return in.igotPlt->getVA() + getGotPltOffset(); 162 return in.gotPlt->getVA() + getGotPltOffset(); 163 } 164 165 uint64_t Symbol::getGotPltOffset() const { 166 if (isInIplt) 167 return pltIndex * target->gotEntrySize; 168 return (pltIndex + target->gotPltHeaderEntriesNum) * target->gotEntrySize; 169 } 170 171 uint64_t Symbol::getPltVA() const { 172 uint64_t outVA = isInIplt 173 ? in.iplt->getVA() + pltIndex * target->ipltEntrySize 174 : in.plt->getVA() + in.plt->headerSize + 175 pltIndex * target->pltEntrySize; 176 177 // While linking microMIPS code PLT code are always microMIPS 178 // code. Set the less-significant bit to track that fact. 179 // See detailed comment in the `getSymVA` function. 180 if (config->emachine == EM_MIPS && isMicroMips()) 181 outVA |= 1; 182 return outVA; 183 } 184 185 uint64_t Symbol::getSize() const { 186 if (const auto *dr = dyn_cast<Defined>(this)) 187 return dr->size; 188 return cast<SharedSymbol>(this)->size; 189 } 190 191 OutputSection *Symbol::getOutputSection() const { 192 if (auto *s = dyn_cast<Defined>(this)) { 193 if (auto *sec = s->section) 194 return sec->getOutputSection(); 195 return nullptr; 196 } 197 return nullptr; 198 } 199 200 // If a symbol name contains '@', the characters after that is 201 // a symbol version name. This function parses that. 202 void Symbol::parseSymbolVersion() { 203 // Return if localized by a local: pattern in a version script. 204 if (versionId == VER_NDX_LOCAL) 205 return; 206 StringRef s = getName(); 207 size_t pos = s.find('@'); 208 if (pos == StringRef::npos) 209 return; 210 StringRef verstr = s.substr(pos + 1); 211 212 // Truncate the symbol name so that it doesn't include the version string. 213 nameSize = pos; 214 215 if (verstr.empty()) 216 return; 217 218 // If this is not in this DSO, it is not a definition. 219 if (!isDefined()) 220 return; 221 222 // '@@' in a symbol name means the default version. 223 // It is usually the most recent one. 224 bool isDefault = (verstr[0] == '@'); 225 if (isDefault) 226 verstr = verstr.substr(1); 227 228 for (const VersionDefinition &ver : namedVersionDefs()) { 229 if (ver.name != verstr) 230 continue; 231 232 if (isDefault) 233 versionId = ver.id; 234 else 235 versionId = ver.id | VERSYM_HIDDEN; 236 return; 237 } 238 239 // It is an error if the specified version is not defined. 240 // Usually version script is not provided when linking executable, 241 // but we may still want to override a versioned symbol from DSO, 242 // so we do not report error in this case. We also do not error 243 // if the symbol has a local version as it won't be in the dynamic 244 // symbol table. 245 if (config->shared && versionId != VER_NDX_LOCAL) 246 error(toString(file) + ": symbol " + s + " has undefined version " + 247 verstr); 248 } 249 250 void Symbol::extract() const { 251 if (auto *sym = dyn_cast<LazyArchive>(this)) { 252 cast<ArchiveFile>(sym->file)->extract(sym->sym); 253 } else if (file->lazy) { 254 file->lazy = false; 255 parseFile(file); 256 } 257 } 258 259 MemoryBufferRef LazyArchive::getMemberBuffer() { 260 Archive::Child c = 261 CHECK(sym.getMember(), 262 "could not get the member for symbol " + toELFString(sym)); 263 264 return CHECK(c.getMemoryBufferRef(), 265 "could not get the buffer for the member defining symbol " + 266 toELFString(sym)); 267 } 268 269 uint8_t Symbol::computeBinding() const { 270 if (config->relocatable) 271 return binding; 272 if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) || 273 (versionId == VER_NDX_LOCAL && !isLazy())) 274 return STB_LOCAL; 275 if (!config->gnuUnique && binding == STB_GNU_UNIQUE) 276 return STB_GLOBAL; 277 return binding; 278 } 279 280 bool Symbol::includeInDynsym() const { 281 if (!config->hasDynSymTab) 282 return false; 283 if (computeBinding() == STB_LOCAL) 284 return false; 285 if (!isDefined() && !isCommon()) 286 // This should unconditionally return true, unfortunately glibc -static-pie 287 // expects undefined weak symbols not to exist in .dynsym, e.g. 288 // __pthread_mutex_lock reference in _dl_add_to_namespace_list, 289 // __pthread_initialize_minimal reference in csu/libc-start.c. 290 return !(config->noDynamicLinker && isUndefWeak()); 291 292 return exportDynamic || inDynamicList; 293 } 294 295 // Print out a log message for --trace-symbol. 296 void elf::printTraceSymbol(const Symbol *sym) { 297 std::string s; 298 if (sym->isUndefined()) 299 s = ": reference to "; 300 else if (sym->isLazy()) 301 s = ": lazy definition of "; 302 else if (sym->isShared()) 303 s = ": shared definition of "; 304 else if (sym->isCommon()) 305 s = ": common definition of "; 306 else 307 s = ": definition of "; 308 309 message(toString(sym->file) + s + sym->getName()); 310 } 311 312 static void recordWhyExtract(const InputFile *reference, 313 const InputFile &extracted, const Symbol &sym) { 314 whyExtract.emplace_back(toString(reference), &extracted, sym); 315 } 316 317 void elf::maybeWarnUnorderableSymbol(const Symbol *sym) { 318 if (!config->warnSymbolOrdering) 319 return; 320 321 // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning 322 // is emitted. It makes sense to not warn on undefined symbols. 323 // 324 // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols, 325 // but we don't have to be compatible here. 326 if (sym->isUndefined() && 327 config->unresolvedSymbols == UnresolvedPolicy::Ignore) 328 return; 329 330 const InputFile *file = sym->file; 331 auto *d = dyn_cast<Defined>(sym); 332 333 auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); }; 334 335 if (sym->isUndefined()) 336 report(": unable to order undefined symbol: "); 337 else if (sym->isShared()) 338 report(": unable to order shared symbol: "); 339 else if (d && !d->section) 340 report(": unable to order absolute symbol: "); 341 else if (d && isa<OutputSection>(d->section)) 342 report(": unable to order synthetic symbol: "); 343 else if (d && !d->section->isLive()) 344 report(": unable to order discarded symbol: "); 345 } 346 347 // Returns true if a symbol can be replaced at load-time by a symbol 348 // with the same name defined in other ELF executable or DSO. 349 bool elf::computeIsPreemptible(const Symbol &sym) { 350 assert(!sym.isLocal() || sym.isPlaceholder()); 351 352 // Only symbols with default visibility that appear in dynsym can be 353 // preempted. Symbols with protected visibility cannot be preempted. 354 if (!sym.includeInDynsym() || sym.visibility != STV_DEFAULT) 355 return false; 356 357 // At this point copy relocations have not been created yet, so any 358 // symbol that is not defined locally is preemptible. 359 if (!sym.isDefined()) 360 return true; 361 362 if (!config->shared) 363 return false; 364 365 // If -Bsymbolic or --dynamic-list is specified, or -Bsymbolic-functions is 366 // specified and the symbol is STT_FUNC, the symbol is preemptible iff it is 367 // in the dynamic list. -Bsymbolic-non-weak-functions is a non-weak subset of 368 // -Bsymbolic-functions. 369 if (config->symbolic || 370 (config->bsymbolic == BsymbolicKind::Functions && sym.isFunc()) || 371 (config->bsymbolic == BsymbolicKind::NonWeakFunctions && sym.isFunc() && 372 sym.binding != STB_WEAK)) 373 return sym.inDynamicList; 374 return true; 375 } 376 377 void elf::reportBackrefs() { 378 for (auto &it : backwardReferences) { 379 const Symbol &sym = *it.first; 380 std::string to = toString(it.second.second); 381 // Some libraries have known problems and can cause noise. Filter them out 382 // with --warn-backrefs-exclude=. to may look like *.o or *.a(*.o). 383 bool exclude = false; 384 for (const llvm::GlobPattern &pat : config->warnBackrefsExclude) 385 if (pat.match(to)) { 386 exclude = true; 387 break; 388 } 389 if (!exclude) 390 warn("backward reference detected: " + sym.getName() + " in " + 391 toString(it.second.first) + " refers to " + to); 392 } 393 } 394 395 static uint8_t getMinVisibility(uint8_t va, uint8_t vb) { 396 if (va == STV_DEFAULT) 397 return vb; 398 if (vb == STV_DEFAULT) 399 return va; 400 return std::min(va, vb); 401 } 402 403 // Merge symbol properties. 404 // 405 // When we have many symbols of the same name, we choose one of them, 406 // and that's the result of symbol resolution. However, symbols that 407 // were not chosen still affect some symbol properties. 408 void Symbol::mergeProperties(const Symbol &other) { 409 if (other.exportDynamic) 410 exportDynamic = true; 411 if (other.isUsedInRegularObj) 412 isUsedInRegularObj = true; 413 414 // DSO symbols do not affect visibility in the output. 415 if (!other.isShared()) 416 visibility = getMinVisibility(visibility, other.visibility); 417 } 418 419 void Symbol::resolve(const Symbol &other) { 420 mergeProperties(other); 421 422 if (isPlaceholder()) { 423 replace(other); 424 return; 425 } 426 427 switch (other.kind()) { 428 case Symbol::UndefinedKind: 429 resolveUndefined(cast<Undefined>(other)); 430 break; 431 case Symbol::CommonKind: 432 resolveCommon(cast<CommonSymbol>(other)); 433 break; 434 case Symbol::DefinedKind: 435 resolveDefined(cast<Defined>(other)); 436 break; 437 case Symbol::LazyArchiveKind: 438 resolveLazy(cast<LazyArchive>(other)); 439 break; 440 case Symbol::LazyObjectKind: 441 resolveLazy(cast<LazyObject>(other)); 442 break; 443 case Symbol::SharedKind: 444 resolveShared(cast<SharedSymbol>(other)); 445 break; 446 case Symbol::PlaceholderKind: 447 llvm_unreachable("bad symbol kind"); 448 } 449 } 450 451 void Symbol::resolveUndefined(const Undefined &other) { 452 // An undefined symbol with non default visibility must be satisfied 453 // in the same DSO. 454 // 455 // If this is a non-weak defined symbol in a discarded section, override the 456 // existing undefined symbol for better error message later. 457 if ((isShared() && other.visibility != STV_DEFAULT) || 458 (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) { 459 replace(other); 460 return; 461 } 462 463 if (traced) 464 printTraceSymbol(&other); 465 466 if (isLazy()) { 467 // An undefined weak will not extract archive members. See comment on Lazy 468 // in Symbols.h for the details. 469 if (other.binding == STB_WEAK) { 470 binding = STB_WEAK; 471 type = other.type; 472 return; 473 } 474 475 // Do extra check for --warn-backrefs. 476 // 477 // --warn-backrefs is an option to prevent an undefined reference from 478 // extracting an archive member written earlier in the command line. It can 479 // be used to keep compatibility with GNU linkers to some degree. I'll 480 // explain the feature and why you may find it useful in this comment. 481 // 482 // lld's symbol resolution semantics is more relaxed than traditional Unix 483 // linkers. For example, 484 // 485 // ld.lld foo.a bar.o 486 // 487 // succeeds even if bar.o contains an undefined symbol that has to be 488 // resolved by some object file in foo.a. Traditional Unix linkers don't 489 // allow this kind of backward reference, as they visit each file only once 490 // from left to right in the command line while resolving all undefined 491 // symbols at the moment of visiting. 492 // 493 // In the above case, since there's no undefined symbol when a linker visits 494 // foo.a, no files are pulled out from foo.a, and because the linker forgets 495 // about foo.a after visiting, it can't resolve undefined symbols in bar.o 496 // that could have been resolved otherwise. 497 // 498 // That lld accepts more relaxed form means that (besides it'd make more 499 // sense) you can accidentally write a command line or a build file that 500 // works only with lld, even if you have a plan to distribute it to wider 501 // users who may be using GNU linkers. With --warn-backrefs, you can detect 502 // a library order that doesn't work with other Unix linkers. 503 // 504 // The option is also useful to detect cyclic dependencies between static 505 // archives. Again, lld accepts 506 // 507 // ld.lld foo.a bar.a 508 // 509 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is 510 // handled as an error. 511 // 512 // Here is how the option works. We assign a group ID to each file. A file 513 // with a smaller group ID can pull out object files from an archive file 514 // with an equal or greater group ID. Otherwise, it is a reverse dependency 515 // and an error. 516 // 517 // A file outside --{start,end}-group gets a fresh ID when instantiated. All 518 // files within the same --{start,end}-group get the same group ID. E.g. 519 // 520 // ld.lld A B --start-group C D --end-group E 521 // 522 // A forms group 0. B form group 1. C and D (including their member object 523 // files) form group 2. E forms group 3. I think that you can see how this 524 // group assignment rule simulates the traditional linker's semantics. 525 bool backref = config->warnBackrefs && other.file && 526 file->groupId < other.file->groupId; 527 extract(); 528 529 if (!config->whyExtract.empty()) 530 recordWhyExtract(other.file, *file, *this); 531 532 // We don't report backward references to weak symbols as they can be 533 // overridden later. 534 // 535 // A traditional linker does not error for -ldef1 -lref -ldef2 (linking 536 // sandwich), where def2 may or may not be the same as def1. We don't want 537 // to warn for this case, so dismiss the warning if we see a subsequent lazy 538 // definition. this->file needs to be saved because in the case of LTO it 539 // may be reset to nullptr or be replaced with a file named lto.tmp. 540 if (backref && !isWeak()) 541 backwardReferences.try_emplace(this, std::make_pair(other.file, file)); 542 return; 543 } 544 545 // Undefined symbols in a SharedFile do not change the binding. 546 if (isa_and_nonnull<SharedFile>(other.file)) 547 return; 548 549 if (isUndefined() || isShared()) { 550 // The binding will be weak if there is at least one reference and all are 551 // weak. The binding has one opportunity to change to weak: if the first 552 // reference is weak. 553 if (other.binding != STB_WEAK || !referenced) 554 binding = other.binding; 555 } 556 } 557 558 // Compare two symbols. Return 1 if the new symbol should win, -1 if 559 // the new symbol should lose, or 0 if there is a conflict. 560 int Symbol::compare(const Symbol *other) const { 561 assert(other->isDefined() || other->isCommon()); 562 563 if (!isDefined() && !isCommon()) 564 return 1; 565 566 // .symver foo,foo@@VER unfortunately creates two defined symbols: foo and 567 // foo@@VER. In GNU ld, if foo and foo@@VER are in the same file, foo is 568 // ignored. In our implementation, when this is foo, this->getName() may still 569 // contain @@, return 1 in this case as well. 570 if (file == other->file) { 571 if (other->getName().contains("@@")) 572 return 1; 573 if (getName().contains("@@")) 574 return -1; 575 } 576 577 if (other->isWeak()) 578 return -1; 579 580 if (isWeak()) 581 return 1; 582 583 if (isCommon() && other->isCommon()) { 584 if (config->warnCommon) 585 warn("multiple common of " + getName()); 586 return 0; 587 } 588 589 if (isCommon()) { 590 if (config->warnCommon) 591 warn("common " + getName() + " is overridden"); 592 return 1; 593 } 594 595 if (other->isCommon()) { 596 if (config->warnCommon) 597 warn("common " + getName() + " is overridden"); 598 return -1; 599 } 600 601 auto *oldSym = cast<Defined>(this); 602 auto *newSym = cast<Defined>(other); 603 604 if (isa_and_nonnull<BitcodeFile>(other->file)) 605 return 0; 606 607 if (!oldSym->section && !newSym->section && oldSym->value == newSym->value && 608 newSym->binding == STB_GLOBAL) 609 return -1; 610 611 return 0; 612 } 613 614 static void reportDuplicate(Symbol *sym, InputFile *newFile, 615 InputSectionBase *errSec, uint64_t errOffset) { 616 if (config->allowMultipleDefinition) 617 return; 618 619 Defined *d = cast<Defined>(sym); 620 if (!d->section || !errSec) { 621 error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " + 622 toString(sym->file) + "\n>>> defined in " + toString(newFile)); 623 return; 624 } 625 626 // Construct and print an error message in the form of: 627 // 628 // ld.lld: error: duplicate symbol: foo 629 // >>> defined at bar.c:30 630 // >>> bar.o (/home/alice/src/bar.o) 631 // >>> defined at baz.c:563 632 // >>> baz.o in archive libbaz.a 633 auto *sec1 = cast<InputSectionBase>(d->section); 634 std::string src1 = sec1->getSrcMsg(*sym, d->value); 635 std::string obj1 = sec1->getObjMsg(d->value); 636 std::string src2 = errSec->getSrcMsg(*sym, errOffset); 637 std::string obj2 = errSec->getObjMsg(errOffset); 638 639 std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at "; 640 if (!src1.empty()) 641 msg += src1 + "\n>>> "; 642 msg += obj1 + "\n>>> defined at "; 643 if (!src2.empty()) 644 msg += src2 + "\n>>> "; 645 msg += obj2; 646 error(msg); 647 } 648 649 void Symbol::resolveCommon(const CommonSymbol &other) { 650 int cmp = compare(&other); 651 if (cmp < 0) 652 return; 653 654 if (cmp > 0) { 655 if (auto *s = dyn_cast<SharedSymbol>(this)) { 656 // Increase st_size if the shared symbol has a larger st_size. The shared 657 // symbol may be created from common symbols. The fact that some object 658 // files were linked into a shared object first should not change the 659 // regular rule that picks the largest st_size. 660 uint64_t size = s->size; 661 replace(other); 662 if (size > cast<CommonSymbol>(this)->size) 663 cast<CommonSymbol>(this)->size = size; 664 } else { 665 replace(other); 666 } 667 return; 668 } 669 670 CommonSymbol *oldSym = cast<CommonSymbol>(this); 671 672 oldSym->alignment = std::max(oldSym->alignment, other.alignment); 673 if (oldSym->size < other.size) { 674 oldSym->file = other.file; 675 oldSym->size = other.size; 676 } 677 } 678 679 void Symbol::resolveDefined(const Defined &other) { 680 int cmp = compare(&other); 681 if (cmp > 0) 682 replace(other); 683 else if (cmp == 0) 684 reportDuplicate(this, other.file, 685 dyn_cast_or_null<InputSectionBase>(other.section), 686 other.value); 687 } 688 689 template <class LazyT> 690 static void replaceCommon(Symbol &oldSym, const LazyT &newSym) { 691 backwardReferences.erase(&oldSym); 692 oldSym.replace(newSym); 693 newSym.extract(); 694 } 695 696 template <class LazyT> void Symbol::resolveLazy(const LazyT &other) { 697 // For common objects, we want to look for global or weak definitions that 698 // should be extracted as the canonical definition instead. 699 if (isCommon() && elf::config->fortranCommon) { 700 if (auto *laSym = dyn_cast<LazyArchive>(&other)) { 701 ArchiveFile *archive = cast<ArchiveFile>(laSym->file); 702 const Archive::Symbol &archiveSym = laSym->sym; 703 if (archive->shouldExtractForCommon(archiveSym)) { 704 replaceCommon(*this, other); 705 return; 706 } 707 } else if (auto *loSym = dyn_cast<LazyObject>(&other)) { 708 if (loSym->file->shouldExtractForCommon(loSym->getName())) { 709 replaceCommon(*this, other); 710 return; 711 } 712 } 713 } 714 715 if (!isUndefined()) { 716 // See the comment in resolveUndefined(). 717 if (isDefined()) 718 backwardReferences.erase(this); 719 return; 720 } 721 722 // An undefined weak will not extract archive members. See comment on Lazy in 723 // Symbols.h for the details. 724 if (isWeak()) { 725 uint8_t ty = type; 726 replace(other); 727 type = ty; 728 binding = STB_WEAK; 729 return; 730 } 731 732 const InputFile *oldFile = file; 733 other.extract(); 734 if (!config->whyExtract.empty()) 735 recordWhyExtract(oldFile, *file, *this); 736 } 737 738 void Symbol::resolveShared(const SharedSymbol &other) { 739 if (isCommon()) { 740 // See the comment in resolveCommon() above. 741 if (other.size > cast<CommonSymbol>(this)->size) 742 cast<CommonSymbol>(this)->size = other.size; 743 return; 744 } 745 if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) { 746 // An undefined symbol with non default visibility must be satisfied 747 // in the same DSO. 748 uint8_t bind = binding; 749 replace(other); 750 binding = bind; 751 } else if (traced) 752 printTraceSymbol(&other); 753 } 754