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