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