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 99 // In the typical case, this is actually very simple and boils 100 // down to adding together 3 numbers: 101 // 1. The address of the output section. 102 // 2. The offset of the input section within the output section. 103 // 3. The offset within the input section (this addition happens 104 // inside InputSection::getOffset). 105 // 106 // If you understand the data structures involved with this next 107 // line (and how they get built), then you have a pretty good 108 // understanding of the linker. 109 uint64_t va = isec->getVA(offset); 110 if (d.isSection()) 111 va -= addend; 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.needsCopy)) 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 return getSymVA(*this, addend) + 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->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 == 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::extract() const { 259 if (auto *sym = dyn_cast<LazyArchive>(this)) { 260 cast<ArchiveFile>(sym->file)->extract(sym->sym); 261 } else if (file->lazy) { 262 file->lazy = false; 263 parseFile(file); 264 } 265 } 266 267 MemoryBufferRef LazyArchive::getMemberBuffer() { 268 Archive::Child c = 269 CHECK(sym.getMember(), 270 "could not get the member for symbol " + toELFString(sym)); 271 272 return CHECK(c.getMemoryBufferRef(), 273 "could not get the buffer for the member defining symbol " + 274 toELFString(sym)); 275 } 276 277 uint8_t Symbol::computeBinding() const { 278 if (config->relocatable) 279 return binding; 280 if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) || 281 (versionId == VER_NDX_LOCAL && !isLazy())) 282 return STB_LOCAL; 283 if (!config->gnuUnique && binding == STB_GNU_UNIQUE) 284 return STB_GLOBAL; 285 return binding; 286 } 287 288 bool Symbol::includeInDynsym() const { 289 if (!config->hasDynSymTab) 290 return false; 291 if (computeBinding() == STB_LOCAL) 292 return false; 293 if (!isDefined() && !isCommon()) 294 // This should unconditionally return true, unfortunately glibc -static-pie 295 // expects undefined weak symbols not to exist in .dynsym, e.g. 296 // __pthread_mutex_lock reference in _dl_add_to_namespace_list, 297 // __pthread_initialize_minimal reference in csu/libc-start.c. 298 return !(config->noDynamicLinker && isUndefWeak()); 299 300 return exportDynamic || inDynamicList; 301 } 302 303 // Print out a log message for --trace-symbol. 304 void elf::printTraceSymbol(const Symbol *sym) { 305 std::string s; 306 if (sym->isUndefined()) 307 s = ": reference to "; 308 else if (sym->isLazy()) 309 s = ": lazy definition of "; 310 else if (sym->isShared()) 311 s = ": shared definition of "; 312 else if (sym->isCommon()) 313 s = ": common definition of "; 314 else 315 s = ": definition of "; 316 317 message(toString(sym->file) + s + sym->getName()); 318 } 319 320 static void recordWhyExtract(const InputFile *reference, 321 const InputFile &extracted, const Symbol &sym) { 322 whyExtract.emplace_back(toString(reference), &extracted, sym); 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 extract archive members. See comment on Lazy 476 // in 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 // extracting an archive member written earlier in the command line. It can 487 // be used to keep compatibility with GNU linkers to some degree. I'll 488 // 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 extract(); 536 537 if (!config->whyExtract.empty()) 538 recordWhyExtract(other.file, *file, *this); 539 540 // We don't report backward references to weak symbols as they can be 541 // overridden later. 542 // 543 // A traditional linker does not error for -ldef1 -lref -ldef2 (linking 544 // sandwich), where def2 may or may not be the same as def1. We don't want 545 // to warn for this case, so dismiss the warning if we see a subsequent lazy 546 // definition. this->file needs to be saved because in the case of LTO it 547 // may be reset to nullptr or be replaced with a file named lto.tmp. 548 if (backref && !isWeak()) 549 backwardReferences.try_emplace(this, std::make_pair(other.file, file)); 550 return; 551 } 552 553 // Undefined symbols in a SharedFile do not change the binding. 554 if (dyn_cast_or_null<SharedFile>(other.file)) 555 return; 556 557 if (isUndefined() || isShared()) { 558 // The binding will be weak if there is at least one reference and all are 559 // weak. The binding has one opportunity to change to weak: if the first 560 // reference is weak. 561 if (other.binding != STB_WEAK || !referenced) 562 binding = other.binding; 563 } 564 } 565 566 // Compare two symbols. Return 1 if the new symbol should win, -1 if 567 // the new symbol should lose, or 0 if there is a conflict. 568 int Symbol::compare(const Symbol *other) const { 569 assert(other->isDefined() || other->isCommon()); 570 571 if (!isDefined() && !isCommon()) 572 return 1; 573 574 // .symver foo,foo@@VER unfortunately creates two defined symbols: foo and 575 // foo@@VER. In GNU ld, if foo and foo@@VER are in the same file, foo is 576 // ignored. In our implementation, when this is foo, this->getName() may still 577 // contain @@, return 1 in this case as well. 578 if (file == other->file) { 579 if (other->getName().contains("@@")) 580 return 1; 581 if (getName().contains("@@")) 582 return -1; 583 } 584 585 if (other->isWeak()) 586 return -1; 587 588 if (isWeak()) 589 return 1; 590 591 if (isCommon() && other->isCommon()) { 592 if (config->warnCommon) 593 warn("multiple common of " + getName()); 594 return 0; 595 } 596 597 if (isCommon()) { 598 if (config->warnCommon) 599 warn("common " + getName() + " is overridden"); 600 return 1; 601 } 602 603 if (other->isCommon()) { 604 if (config->warnCommon) 605 warn("common " + getName() + " is overridden"); 606 return -1; 607 } 608 609 auto *oldSym = cast<Defined>(this); 610 auto *newSym = cast<Defined>(other); 611 612 if (dyn_cast_or_null<BitcodeFile>(other->file)) 613 return 0; 614 615 if (!oldSym->section && !newSym->section && oldSym->value == newSym->value && 616 newSym->binding == STB_GLOBAL) 617 return -1; 618 619 return 0; 620 } 621 622 static void reportDuplicate(Symbol *sym, InputFile *newFile, 623 InputSectionBase *errSec, uint64_t errOffset) { 624 if (config->allowMultipleDefinition) 625 return; 626 627 Defined *d = cast<Defined>(sym); 628 if (!d->section || !errSec) { 629 error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " + 630 toString(sym->file) + "\n>>> defined in " + toString(newFile)); 631 return; 632 } 633 634 // Construct and print an error message in the form of: 635 // 636 // ld.lld: error: duplicate symbol: foo 637 // >>> defined at bar.c:30 638 // >>> bar.o (/home/alice/src/bar.o) 639 // >>> defined at baz.c:563 640 // >>> baz.o in archive libbaz.a 641 auto *sec1 = cast<InputSectionBase>(d->section); 642 std::string src1 = sec1->getSrcMsg(*sym, d->value); 643 std::string obj1 = sec1->getObjMsg(d->value); 644 std::string src2 = errSec->getSrcMsg(*sym, errOffset); 645 std::string obj2 = errSec->getObjMsg(errOffset); 646 647 std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at "; 648 if (!src1.empty()) 649 msg += src1 + "\n>>> "; 650 msg += obj1 + "\n>>> defined at "; 651 if (!src2.empty()) 652 msg += src2 + "\n>>> "; 653 msg += obj2; 654 error(msg); 655 } 656 657 void Symbol::resolveCommon(const CommonSymbol &other) { 658 int cmp = compare(&other); 659 if (cmp < 0) 660 return; 661 662 if (cmp > 0) { 663 if (auto *s = dyn_cast<SharedSymbol>(this)) { 664 // Increase st_size if the shared symbol has a larger st_size. The shared 665 // symbol may be created from common symbols. The fact that some object 666 // files were linked into a shared object first should not change the 667 // regular rule that picks the largest st_size. 668 uint64_t size = s->size; 669 replace(other); 670 if (size > cast<CommonSymbol>(this)->size) 671 cast<CommonSymbol>(this)->size = size; 672 } else { 673 replace(other); 674 } 675 return; 676 } 677 678 CommonSymbol *oldSym = cast<CommonSymbol>(this); 679 680 oldSym->alignment = std::max(oldSym->alignment, other.alignment); 681 if (oldSym->size < other.size) { 682 oldSym->file = other.file; 683 oldSym->size = other.size; 684 } 685 } 686 687 void Symbol::resolveDefined(const Defined &other) { 688 int cmp = compare(&other); 689 if (cmp > 0) 690 replace(other); 691 else if (cmp == 0) 692 reportDuplicate(this, other.file, 693 dyn_cast_or_null<InputSectionBase>(other.section), 694 other.value); 695 } 696 697 template <class LazyT> 698 static void replaceCommon(Symbol &oldSym, const LazyT &newSym) { 699 backwardReferences.erase(&oldSym); 700 oldSym.replace(newSym); 701 newSym.extract(); 702 } 703 704 template <class LazyT> void Symbol::resolveLazy(const LazyT &other) { 705 // For common objects, we want to look for global or weak definitions that 706 // should be extracted as the canonical definition instead. 707 if (isCommon() && elf::config->fortranCommon) { 708 if (auto *laSym = dyn_cast<LazyArchive>(&other)) { 709 ArchiveFile *archive = cast<ArchiveFile>(laSym->file); 710 const Archive::Symbol &archiveSym = laSym->sym; 711 if (archive->shouldExtractForCommon(archiveSym)) { 712 replaceCommon(*this, other); 713 return; 714 } 715 } else if (auto *loSym = dyn_cast<LazyObject>(&other)) { 716 if (loSym->file->shouldExtractForCommon(loSym->getName())) { 717 replaceCommon(*this, other); 718 return; 719 } 720 } 721 } 722 723 if (!isUndefined()) { 724 // See the comment in resolveUndefined(). 725 if (isDefined()) 726 backwardReferences.erase(this); 727 return; 728 } 729 730 // An undefined weak will not extract archive members. See comment on Lazy in 731 // Symbols.h for the details. 732 if (isWeak()) { 733 uint8_t ty = type; 734 replace(other); 735 type = ty; 736 binding = STB_WEAK; 737 return; 738 } 739 740 const InputFile *oldFile = file; 741 other.extract(); 742 if (!config->whyExtract.empty()) 743 recordWhyExtract(oldFile, *file, *this); 744 } 745 746 void Symbol::resolveShared(const SharedSymbol &other) { 747 if (isCommon()) { 748 // See the comment in resolveCommon() above. 749 if (other.size > cast<CommonSymbol>(this)->size) 750 cast<CommonSymbol>(this)->size = other.size; 751 return; 752 } 753 if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) { 754 // An undefined symbol with non default visibility must be satisfied 755 // in the same DSO. 756 uint8_t bind = binding; 757 replace(other); 758 binding = bind; 759 } else if (traced) 760 printTraceSymbol(&other); 761 } 762