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