Symbols.cpp revision 360784
1290001Sglebius//===- Symbols.cpp --------------------------------------------------------===// 2290001Sglebius// 3290001Sglebius// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4290001Sglebius// See https://llvm.org/LICENSE.txt for license information. 5290001Sglebius// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6290001Sglebius// 7290001Sglebius//===----------------------------------------------------------------------===// 8290001Sglebius 9290001Sglebius#include "Symbols.h" 10290001Sglebius#include "InputFiles.h" 11290001Sglebius#include "InputSection.h" 12290001Sglebius#include "OutputSections.h" 13290001Sglebius#include "SyntheticSections.h" 14290001Sglebius#include "Target.h" 15290001Sglebius#include "Writer.h" 16290001Sglebius#include "lld/Common/ErrorHandler.h" 17290001Sglebius#include "lld/Common/Strings.h" 18290001Sglebius#include "llvm/ADT/STLExtras.h" 19290001Sglebius#include "llvm/Support/Path.h" 20290001Sglebius#include <cstring> 21290001Sglebius 22290001Sglebiususing namespace llvm; 23290001Sglebiususing namespace llvm::object; 24290001Sglebiususing namespace llvm::ELF; 25290001Sglebius 26290001Sglebiusnamespace lld { 27290001Sglebius// Returns a symbol for an error message. 28290001Sglebiusstatic std::string demangle(StringRef symName) { 29290001Sglebius if (elf::config->demangle) 30290001Sglebius return demangleItanium(symName); 31290001Sglebius return symName; 32290001Sglebius} 33290001Sglebius 34290001Sglebiusstd::string toString(const elf::Symbol &b) { return demangle(b.getName()); } 35290001Sglebiusstd::string toELFString(const Archive::Symbol &b) { 36290001Sglebius return demangle(b.getName()); 37290001Sglebius} 38290001Sglebius 39290001Sglebiusnamespace elf { 40290001SglebiusDefined *ElfSym::bss; 41290001SglebiusDefined *ElfSym::etext1; 42290001SglebiusDefined *ElfSym::etext2; 43290001SglebiusDefined *ElfSym::edata1; 44290001SglebiusDefined *ElfSym::edata2; 45290001SglebiusDefined *ElfSym::end1; 46290001SglebiusDefined *ElfSym::end2; 47290001SglebiusDefined *ElfSym::globalOffsetTable; 48290001SglebiusDefined *ElfSym::mipsGp; 49290001SglebiusDefined *ElfSym::mipsGpDisp; 50290001SglebiusDefined *ElfSym::mipsLocalGp; 51290001SglebiusDefined *ElfSym::relaIpltStart; 52290001SglebiusDefined *ElfSym::relaIpltEnd; 53290001SglebiusDefined *ElfSym::riscvGlobalPointer; 54290001SglebiusDefined *ElfSym::tlsModuleBase; 55290001Sglebius 56290001Sglebiusstatic uint64_t getSymVA(const Symbol &sym, int64_t &addend) { 57290001Sglebius switch (sym.kind()) { 58290001Sglebius case Symbol::DefinedKind: { 59290001Sglebius auto &d = cast<Defined>(sym); 60290001Sglebius SectionBase *isec = d.section; 61290001Sglebius 62290001Sglebius // This is an absolute symbol. 63290001Sglebius if (!isec) 64290001Sglebius return d.value; 65290001Sglebius 66290001Sglebius assert(isec != &InputSection::discarded); 67290001Sglebius isec = isec->repl; 68290001Sglebius 69290001Sglebius uint64_t offset = d.value; 70290001Sglebius 71290001Sglebius // An object in an SHF_MERGE section might be referenced via a 72290001Sglebius // section symbol (as a hack for reducing the number of local 73290001Sglebius // symbols). 74290001Sglebius // Depending on the addend, the reference via a section symbol 75290001Sglebius // refers to a different object in the merge section. 76290001Sglebius // Since the objects in the merge section are not necessarily 77290001Sglebius // contiguous in the output, the addend can thus affect the final 78290001Sglebius // VA in a non-linear way. 79290001Sglebius // To make this work, we incorporate the addend into the section 80290001Sglebius // offset (and zero out the addend for later processing) so that 81290001Sglebius // we find the right object in the section. 82290001Sglebius if (d.isSection()) { 83290001Sglebius offset += addend; 84290001Sglebius addend = 0; 85290001Sglebius } 86290001Sglebius 87290001Sglebius // In the typical case, this is actually very simple and boils 88290001Sglebius // down to adding together 3 numbers: 89290001Sglebius // 1. The address of the output section. 90290001Sglebius // 2. The offset of the input section within the output section. 91290001Sglebius // 3. The offset within the input section (this addition happens 92290001Sglebius // inside InputSection::getOffset). 93290001Sglebius // 94290001Sglebius // If you understand the data structures involved with this next 95290001Sglebius // line (and how they get built), then you have a pretty good 96290001Sglebius // understanding of the linker. 97290001Sglebius uint64_t va = isec->getVA(offset); 98290001Sglebius 99290001Sglebius // MIPS relocatable files can mix regular and microMIPS code. 100290001Sglebius // Linker needs to distinguish such code. To do so microMIPS 101290001Sglebius // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other` 102290001Sglebius // field. Unfortunately, the `MIPS::relocateOne()` method has 103290001Sglebius // a symbol value only. To pass type of the symbol (regular/microMIPS) 104290001Sglebius // to that routine as well as other places where we write 105290001Sglebius // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry` 106290001Sglebius // field etc) do the same trick as compiler uses to mark microMIPS 107290001Sglebius // for CPU - set the less-significant bit. 108290001Sglebius if (config->emachine == EM_MIPS && isMicroMips() && 109290001Sglebius ((sym.stOther & STO_MIPS_MICROMIPS) || sym.needsPltAddr)) 110290001Sglebius va |= 1; 111290001Sglebius 112290001Sglebius if (d.isTls() && !config->relocatable) { 113290001Sglebius // Use the address of the TLS segment's first section rather than the 114290001Sglebius // segment's address, because segment addresses aren't initialized until 115290001Sglebius // after sections are finalized. (e.g. Measuring the size of .rela.dyn 116290001Sglebius // for Android relocation packing requires knowing TLS symbol addresses 117290001Sglebius // during section finalization.) 118290001Sglebius if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec) 119290001Sglebius fatal(toString(d.file) + 120290001Sglebius " has an STT_TLS symbol but doesn't have an SHF_TLS section"); 121290001Sglebius return va - Out::tlsPhdr->firstSec->addr; 122290001Sglebius } 123290001Sglebius return va; 124290001Sglebius } 125290001Sglebius case Symbol::SharedKind: 126290001Sglebius case Symbol::UndefinedKind: 127290001Sglebius return 0; 128290001Sglebius case Symbol::LazyArchiveKind: 129290001Sglebius case Symbol::LazyObjectKind: 130290001Sglebius assert(sym.isUsedInRegularObj && "lazy symbol reached writer"); 131290001Sglebius return 0; 132290001Sglebius case Symbol::CommonKind: 133290001Sglebius llvm_unreachable("common symbol reached writer"); 134290001Sglebius case Symbol::PlaceholderKind: 135290001Sglebius llvm_unreachable("placeholder symbol reached writer"); 136290001Sglebius } 137290001Sglebius llvm_unreachable("invalid symbol kind"); 138290001Sglebius} 139290001Sglebius 140290001Sglebiusuint64_t Symbol::getVA(int64_t addend) const { 141290001Sglebius uint64_t outVA = getSymVA(*this, addend); 142290001Sglebius return outVA + addend; 143290001Sglebius} 144290001Sglebius 145290001Sglebiusuint64_t Symbol::getGotVA() const { 146290001Sglebius if (gotInIgot) 147290001Sglebius return in.igotPlt->getVA() + getGotPltOffset(); 148290001Sglebius return in.got->getVA() + getGotOffset(); 149290001Sglebius} 150290001Sglebius 151290001Sglebiusuint64_t Symbol::getGotOffset() const { return gotIndex * config->wordsize; } 152290001Sglebius 153290001Sglebiusuint64_t Symbol::getGotPltVA() const { 154290001Sglebius if (isInIplt) 155290001Sglebius return in.igotPlt->getVA() + getGotPltOffset(); 156290001Sglebius return in.gotPlt->getVA() + getGotPltOffset(); 157290001Sglebius} 158 159uint64_t Symbol::getGotPltOffset() const { 160 if (isInIplt) 161 return pltIndex * config->wordsize; 162 return (pltIndex + target->gotPltHeaderEntriesNum) * config->wordsize; 163} 164 165uint64_t Symbol::getPltVA() const { 166 uint64_t outVA = isInIplt 167 ? in.iplt->getVA() + pltIndex * target->ipltEntrySize 168 : in.plt->getVA() + in.plt->headerSize + 169 pltIndex * target->pltEntrySize; 170 171 // While linking microMIPS code PLT code are always microMIPS 172 // code. Set the less-significant bit to track that fact. 173 // See detailed comment in the `getSymVA` function. 174 if (config->emachine == EM_MIPS && isMicroMips()) 175 outVA |= 1; 176 return outVA; 177} 178 179uint64_t Symbol::getSize() const { 180 if (const auto *dr = dyn_cast<Defined>(this)) 181 return dr->size; 182 return cast<SharedSymbol>(this)->size; 183} 184 185OutputSection *Symbol::getOutputSection() const { 186 if (auto *s = dyn_cast<Defined>(this)) { 187 if (auto *sec = s->section) 188 return sec->repl->getOutputSection(); 189 return nullptr; 190 } 191 return nullptr; 192} 193 194// If a symbol name contains '@', the characters after that is 195// a symbol version name. This function parses that. 196void Symbol::parseSymbolVersion() { 197 StringRef s = getName(); 198 size_t pos = s.find('@'); 199 if (pos == 0 || pos == StringRef::npos) 200 return; 201 StringRef verstr = s.substr(pos + 1); 202 if (verstr.empty()) 203 return; 204 205 // Truncate the symbol name so that it doesn't include the version string. 206 nameSize = pos; 207 208 // If this is not in this DSO, it is not a definition. 209 if (!isDefined()) 210 return; 211 212 // '@@' in a symbol name means the default version. 213 // It is usually the most recent one. 214 bool isDefault = (verstr[0] == '@'); 215 if (isDefault) 216 verstr = verstr.substr(1); 217 218 for (const VersionDefinition &ver : namedVersionDefs()) { 219 if (ver.name != verstr) 220 continue; 221 222 if (isDefault) 223 versionId = ver.id; 224 else 225 versionId = ver.id | VERSYM_HIDDEN; 226 return; 227 } 228 229 // It is an error if the specified version is not defined. 230 // Usually version script is not provided when linking executable, 231 // but we may still want to override a versioned symbol from DSO, 232 // so we do not report error in this case. We also do not error 233 // if the symbol has a local version as it won't be in the dynamic 234 // symbol table. 235 if (config->shared && versionId != VER_NDX_LOCAL) 236 error(toString(file) + ": symbol " + s + " has undefined version " + 237 verstr); 238} 239 240void Symbol::fetch() const { 241 if (auto *sym = dyn_cast<LazyArchive>(this)) { 242 cast<ArchiveFile>(sym->file)->fetch(sym->sym); 243 return; 244 } 245 246 if (auto *sym = dyn_cast<LazyObject>(this)) { 247 dyn_cast<LazyObjFile>(sym->file)->fetch(); 248 return; 249 } 250 251 llvm_unreachable("Symbol::fetch() is called on a non-lazy symbol"); 252} 253 254MemoryBufferRef LazyArchive::getMemberBuffer() { 255 Archive::Child c = 256 CHECK(sym.getMember(), 257 "could not get the member for symbol " + toELFString(sym)); 258 259 return CHECK(c.getMemoryBufferRef(), 260 "could not get the buffer for the member defining symbol " + 261 toELFString(sym)); 262} 263 264uint8_t Symbol::computeBinding() const { 265 if (config->relocatable) 266 return binding; 267 if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) || 268 versionId == VER_NDX_LOCAL) 269 return STB_LOCAL; 270 if (!config->gnuUnique && binding == STB_GNU_UNIQUE) 271 return STB_GLOBAL; 272 return binding; 273} 274 275bool Symbol::includeInDynsym() const { 276 if (!config->hasDynSymTab) 277 return false; 278 if (computeBinding() == STB_LOCAL) 279 return false; 280 if (!isDefined() && !isCommon()) 281 // This should unconditionally return true, unfortunately glibc -static-pie 282 // expects undefined weak symbols not to exist in .dynsym, e.g. 283 // __pthread_mutex_lock reference in _dl_add_to_namespace_list, 284 // __pthread_initialize_minimal reference in csu/libc-start.c. 285 return !(config->noDynamicLinker && isUndefWeak()); 286 287 return exportDynamic || inDynamicList; 288} 289 290// Print out a log message for --trace-symbol. 291void printTraceSymbol(const Symbol *sym) { 292 std::string s; 293 if (sym->isUndefined()) 294 s = ": reference to "; 295 else if (sym->isLazy()) 296 s = ": lazy definition of "; 297 else if (sym->isShared()) 298 s = ": shared definition of "; 299 else if (sym->isCommon()) 300 s = ": common definition of "; 301 else 302 s = ": definition of "; 303 304 message(toString(sym->file) + s + sym->getName()); 305} 306 307void maybeWarnUnorderableSymbol(const Symbol *sym) { 308 if (!config->warnSymbolOrdering) 309 return; 310 311 // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning 312 // is emitted. It makes sense to not warn on undefined symbols. 313 // 314 // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols, 315 // but we don't have to be compatible here. 316 if (sym->isUndefined() && 317 config->unresolvedSymbols == UnresolvedPolicy::Ignore) 318 return; 319 320 const InputFile *file = sym->file; 321 auto *d = dyn_cast<Defined>(sym); 322 323 auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); }; 324 325 if (sym->isUndefined()) 326 report(": unable to order undefined symbol: "); 327 else if (sym->isShared()) 328 report(": unable to order shared symbol: "); 329 else if (d && !d->section) 330 report(": unable to order absolute symbol: "); 331 else if (d && isa<OutputSection>(d->section)) 332 report(": unable to order synthetic symbol: "); 333 else if (d && !d->section->repl->isLive()) 334 report(": unable to order discarded symbol: "); 335} 336 337// Returns true if a symbol can be replaced at load-time by a symbol 338// with the same name defined in other ELF executable or DSO. 339bool computeIsPreemptible(const Symbol &sym) { 340 assert(!sym.isLocal()); 341 342 // Only symbols with default visibility that appear in dynsym can be 343 // preempted. Symbols with protected visibility cannot be preempted. 344 if (!sym.includeInDynsym() || sym.visibility != STV_DEFAULT) 345 return false; 346 347 // At this point copy relocations have not been created yet, so any 348 // symbol that is not defined locally is preemptible. 349 if (!sym.isDefined()) 350 return true; 351 352 if (!config->shared) 353 return false; 354 355 // If the dynamic list is present, it specifies preemptable symbols in a DSO. 356 if (config->hasDynamicList) 357 return sym.inDynamicList; 358 359 // -Bsymbolic means that definitions are not preempted. 360 if (config->bsymbolic || (config->bsymbolicFunctions && sym.isFunc())) 361 return false; 362 return true; 363} 364 365static uint8_t getMinVisibility(uint8_t va, uint8_t vb) { 366 if (va == STV_DEFAULT) 367 return vb; 368 if (vb == STV_DEFAULT) 369 return va; 370 return std::min(va, vb); 371} 372 373// Merge symbol properties. 374// 375// When we have many symbols of the same name, we choose one of them, 376// and that's the result of symbol resolution. However, symbols that 377// were not chosen still affect some symbol properties. 378void Symbol::mergeProperties(const Symbol &other) { 379 if (other.exportDynamic) 380 exportDynamic = true; 381 if (other.isUsedInRegularObj) 382 isUsedInRegularObj = true; 383 384 // DSO symbols do not affect visibility in the output. 385 if (!other.isShared()) 386 visibility = getMinVisibility(visibility, other.visibility); 387} 388 389void Symbol::resolve(const Symbol &other) { 390 mergeProperties(other); 391 392 if (isPlaceholder()) { 393 replace(other); 394 return; 395 } 396 397 switch (other.kind()) { 398 case Symbol::UndefinedKind: 399 resolveUndefined(cast<Undefined>(other)); 400 break; 401 case Symbol::CommonKind: 402 resolveCommon(cast<CommonSymbol>(other)); 403 break; 404 case Symbol::DefinedKind: 405 resolveDefined(cast<Defined>(other)); 406 break; 407 case Symbol::LazyArchiveKind: 408 resolveLazy(cast<LazyArchive>(other)); 409 break; 410 case Symbol::LazyObjectKind: 411 resolveLazy(cast<LazyObject>(other)); 412 break; 413 case Symbol::SharedKind: 414 resolveShared(cast<SharedSymbol>(other)); 415 break; 416 case Symbol::PlaceholderKind: 417 llvm_unreachable("bad symbol kind"); 418 } 419} 420 421void Symbol::resolveUndefined(const Undefined &other) { 422 // An undefined symbol with non default visibility must be satisfied 423 // in the same DSO. 424 // 425 // If this is a non-weak defined symbol in a discarded section, override the 426 // existing undefined symbol for better error message later. 427 if ((isShared() && other.visibility != STV_DEFAULT) || 428 (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) { 429 replace(other); 430 return; 431 } 432 433 if (traced) 434 printTraceSymbol(&other); 435 436 if (isLazy()) { 437 // An undefined weak will not fetch archive members. See comment on Lazy in 438 // Symbols.h for the details. 439 if (other.binding == STB_WEAK) { 440 binding = STB_WEAK; 441 type = other.type; 442 return; 443 } 444 445 // Do extra check for --warn-backrefs. 446 // 447 // --warn-backrefs is an option to prevent an undefined reference from 448 // fetching an archive member written earlier in the command line. It can be 449 // used to keep compatibility with GNU linkers to some degree. 450 // I'll explain the feature and why you may find it useful in this comment. 451 // 452 // lld's symbol resolution semantics is more relaxed than traditional Unix 453 // linkers. For example, 454 // 455 // ld.lld foo.a bar.o 456 // 457 // succeeds even if bar.o contains an undefined symbol that has to be 458 // resolved by some object file in foo.a. Traditional Unix linkers don't 459 // allow this kind of backward reference, as they visit each file only once 460 // from left to right in the command line while resolving all undefined 461 // symbols at the moment of visiting. 462 // 463 // In the above case, since there's no undefined symbol when a linker visits 464 // foo.a, no files are pulled out from foo.a, and because the linker forgets 465 // about foo.a after visiting, it can't resolve undefined symbols in bar.o 466 // that could have been resolved otherwise. 467 // 468 // That lld accepts more relaxed form means that (besides it'd make more 469 // sense) you can accidentally write a command line or a build file that 470 // works only with lld, even if you have a plan to distribute it to wider 471 // users who may be using GNU linkers. With --warn-backrefs, you can detect 472 // a library order that doesn't work with other Unix linkers. 473 // 474 // The option is also useful to detect cyclic dependencies between static 475 // archives. Again, lld accepts 476 // 477 // ld.lld foo.a bar.a 478 // 479 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is 480 // handled as an error. 481 // 482 // Here is how the option works. We assign a group ID to each file. A file 483 // with a smaller group ID can pull out object files from an archive file 484 // with an equal or greater group ID. Otherwise, it is a reverse dependency 485 // and an error. 486 // 487 // A file outside --{start,end}-group gets a fresh ID when instantiated. All 488 // files within the same --{start,end}-group get the same group ID. E.g. 489 // 490 // ld.lld A B --start-group C D --end-group E 491 // 492 // A forms group 0. B form group 1. C and D (including their member object 493 // files) form group 2. E forms group 3. I think that you can see how this 494 // group assignment rule simulates the traditional linker's semantics. 495 bool backref = config->warnBackrefs && other.file && 496 file->groupId < other.file->groupId; 497 fetch(); 498 499 // We don't report backward references to weak symbols as they can be 500 // overridden later. 501 if (backref && !isWeak()) 502 warn("backward reference detected: " + other.getName() + " in " + 503 toString(other.file) + " refers to " + toString(file)); 504 return; 505 } 506 507 // Undefined symbols in a SharedFile do not change the binding. 508 if (dyn_cast_or_null<SharedFile>(other.file)) 509 return; 510 511 if (isUndefined() || isShared()) { 512 // The binding will be weak if there is at least one reference and all are 513 // weak. The binding has one opportunity to change to weak: if the first 514 // reference is weak. 515 if (other.binding != STB_WEAK || !referenced) 516 binding = other.binding; 517 referenced = true; 518 } 519} 520 521// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and 522// foo@@VER. We want to effectively ignore foo, so give precedence to 523// foo@@VER. 524// FIXME: If users can transition to using 525// .symver foo,foo@@@VER 526// we can delete this hack. 527static int compareVersion(StringRef a, StringRef b) { 528 bool x = a.contains("@@"); 529 bool y = b.contains("@@"); 530 if (!x && y) 531 return 1; 532 if (x && !y) 533 return -1; 534 return 0; 535} 536 537// Compare two symbols. Return 1 if the new symbol should win, -1 if 538// the new symbol should lose, or 0 if there is a conflict. 539int Symbol::compare(const Symbol *other) const { 540 assert(other->isDefined() || other->isCommon()); 541 542 if (!isDefined() && !isCommon()) 543 return 1; 544 545 if (int cmp = compareVersion(getName(), other->getName())) 546 return cmp; 547 548 if (other->isWeak()) 549 return -1; 550 551 if (isWeak()) 552 return 1; 553 554 if (isCommon() && other->isCommon()) { 555 if (config->warnCommon) 556 warn("multiple common of " + getName()); 557 return 0; 558 } 559 560 if (isCommon()) { 561 if (config->warnCommon) 562 warn("common " + getName() + " is overridden"); 563 return 1; 564 } 565 566 if (other->isCommon()) { 567 if (config->warnCommon) 568 warn("common " + getName() + " is overridden"); 569 return -1; 570 } 571 572 auto *oldSym = cast<Defined>(this); 573 auto *newSym = cast<Defined>(other); 574 575 if (dyn_cast_or_null<BitcodeFile>(other->file)) 576 return 0; 577 578 if (!oldSym->section && !newSym->section && oldSym->value == newSym->value && 579 newSym->binding == STB_GLOBAL) 580 return -1; 581 582 return 0; 583} 584 585static void reportDuplicate(Symbol *sym, InputFile *newFile, 586 InputSectionBase *errSec, uint64_t errOffset) { 587 if (config->allowMultipleDefinition) 588 return; 589 590 Defined *d = cast<Defined>(sym); 591 if (!d->section || !errSec) { 592 error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " + 593 toString(sym->file) + "\n>>> defined in " + toString(newFile)); 594 return; 595 } 596 597 // Construct and print an error message in the form of: 598 // 599 // ld.lld: error: duplicate symbol: foo 600 // >>> defined at bar.c:30 601 // >>> bar.o (/home/alice/src/bar.o) 602 // >>> defined at baz.c:563 603 // >>> baz.o in archive libbaz.a 604 auto *sec1 = cast<InputSectionBase>(d->section); 605 std::string src1 = sec1->getSrcMsg(*sym, d->value); 606 std::string obj1 = sec1->getObjMsg(d->value); 607 std::string src2 = errSec->getSrcMsg(*sym, errOffset); 608 std::string obj2 = errSec->getObjMsg(errOffset); 609 610 std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at "; 611 if (!src1.empty()) 612 msg += src1 + "\n>>> "; 613 msg += obj1 + "\n>>> defined at "; 614 if (!src2.empty()) 615 msg += src2 + "\n>>> "; 616 msg += obj2; 617 error(msg); 618} 619 620void Symbol::resolveCommon(const CommonSymbol &other) { 621 int cmp = compare(&other); 622 if (cmp < 0) 623 return; 624 625 if (cmp > 0) { 626 if (auto *s = dyn_cast<SharedSymbol>(this)) { 627 // Increase st_size if the shared symbol has a larger st_size. The shared 628 // symbol may be created from common symbols. The fact that some object 629 // files were linked into a shared object first should not change the 630 // regular rule that picks the largest st_size. 631 uint64_t size = s->size; 632 replace(other); 633 if (size > cast<CommonSymbol>(this)->size) 634 cast<CommonSymbol>(this)->size = size; 635 } else { 636 replace(other); 637 } 638 return; 639 } 640 641 CommonSymbol *oldSym = cast<CommonSymbol>(this); 642 643 oldSym->alignment = std::max(oldSym->alignment, other.alignment); 644 if (oldSym->size < other.size) { 645 oldSym->file = other.file; 646 oldSym->size = other.size; 647 } 648} 649 650void Symbol::resolveDefined(const Defined &other) { 651 int cmp = compare(&other); 652 if (cmp > 0) 653 replace(other); 654 else if (cmp == 0) 655 reportDuplicate(this, other.file, 656 dyn_cast_or_null<InputSectionBase>(other.section), 657 other.value); 658} 659 660template <class LazyT> void Symbol::resolveLazy(const LazyT &other) { 661 if (!isUndefined()) 662 return; 663 664 // An undefined weak will not fetch archive members. See comment on Lazy in 665 // Symbols.h for the details. 666 if (isWeak()) { 667 uint8_t ty = type; 668 replace(other); 669 type = ty; 670 binding = STB_WEAK; 671 return; 672 } 673 674 other.fetch(); 675} 676 677void Symbol::resolveShared(const SharedSymbol &other) { 678 if (isCommon()) { 679 // See the comment in resolveCommon() above. 680 if (other.size > cast<CommonSymbol>(this)->size) 681 cast<CommonSymbol>(this)->size = other.size; 682 return; 683 } 684 if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) { 685 // An undefined symbol with non default visibility must be satisfied 686 // in the same DSO. 687 uint8_t bind = binding; 688 replace(other); 689 binding = bind; 690 referenced = true; 691 } 692} 693 694} // namespace elf 695} // namespace lld 696