]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lld/ELF/Symbols.cpp
zfs: merge openzfs/zfs@52bad4f23 (zfs-2.1-release) into stable/13
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lld / ELF / Symbols.cpp
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
68 static uint64_t getSymVA(const Symbol &sym, int64_t &addend) {
69   switch (sym.kind()) {
70   case Symbol::DefinedKind: {
71     auto &d = cast<Defined>(sym);
72     SectionBase *isec = d.section;
73
74     // This is an absolute symbol.
75     if (!isec)
76       return d.value;
77
78     assert(isec != &InputSection::discarded);
79     isec = isec->repl;
80
81     uint64_t offset = d.value;
82
83     // An object in an SHF_MERGE section might be referenced via a
84     // section symbol (as a hack for reducing the number of local
85     // symbols).
86     // Depending on the addend, the reference via a section symbol
87     // refers to a different object in the merge section.
88     // Since the objects in the merge section are not necessarily
89     // contiguous in the output, the addend can thus affect the final
90     // VA in a non-linear way.
91     // To make this work, we incorporate the addend into the section
92     // offset (and zero out the addend for later processing) so that
93     // we find the right object in the section.
94     if (d.isSection()) {
95       offset += addend;
96       addend = 0;
97     }
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
111     // MIPS relocatable files can mix regular and microMIPS code.
112     // Linker needs to distinguish such code. To do so microMIPS
113     // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
114     // field. Unfortunately, the `MIPS::relocate()` method has
115     // a symbol value only. To pass type of the symbol (regular/microMIPS)
116     // to that routine as well as other places where we write
117     // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
118     // field etc) do the same trick as compiler uses to mark microMIPS
119     // for CPU - set the less-significant bit.
120     if (config->emachine == EM_MIPS && isMicroMips() &&
121         ((sym.stOther & STO_MIPS_MICROMIPS) || sym.needsPltAddr))
122       va |= 1;
123
124     if (d.isTls() && !config->relocatable) {
125       // Use the address of the TLS segment's first section rather than the
126       // segment's address, because segment addresses aren't initialized until
127       // after sections are finalized. (e.g. Measuring the size of .rela.dyn
128       // for Android relocation packing requires knowing TLS symbol addresses
129       // during section finalization.)
130       if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec)
131         fatal(toString(d.file) +
132               " has an STT_TLS symbol but doesn't have an SHF_TLS section");
133       return va - Out::tlsPhdr->firstSec->addr;
134     }
135     return va;
136   }
137   case Symbol::SharedKind:
138   case Symbol::UndefinedKind:
139     return 0;
140   case Symbol::LazyArchiveKind:
141   case Symbol::LazyObjectKind:
142     assert(sym.isUsedInRegularObj && "lazy symbol reached writer");
143     return 0;
144   case Symbol::CommonKind:
145     llvm_unreachable("common symbol reached writer");
146   case Symbol::PlaceholderKind:
147     llvm_unreachable("placeholder symbol reached writer");
148   }
149   llvm_unreachable("invalid symbol kind");
150 }
151
152 uint64_t Symbol::getVA(int64_t addend) const {
153   uint64_t outVA = getSymVA(*this, addend);
154   return outVA + addend;
155 }
156
157 uint64_t Symbol::getGotVA() const {
158   if (gotInIgot)
159     return in.igotPlt->getVA() + getGotPltOffset();
160   return in.got->getVA() + getGotOffset();
161 }
162
163 uint64_t Symbol::getGotOffset() const {
164   return gotIndex * target->gotEntrySize;
165 }
166
167 uint64_t Symbol::getGotPltVA() const {
168   if (isInIplt)
169     return in.igotPlt->getVA() + getGotPltOffset();
170   return in.gotPlt->getVA() + getGotPltOffset();
171 }
172
173 uint64_t Symbol::getGotPltOffset() const {
174   if (isInIplt)
175     return pltIndex * target->gotEntrySize;
176   return (pltIndex + target->gotPltHeaderEntriesNum) * target->gotEntrySize;
177 }
178
179 uint64_t Symbol::getPltVA() const {
180   uint64_t outVA = isInIplt
181                        ? in.iplt->getVA() + pltIndex * target->ipltEntrySize
182                        : in.plt->getVA() + in.plt->headerSize +
183                              pltIndex * target->pltEntrySize;
184
185   // While linking microMIPS code PLT code are always microMIPS
186   // code. Set the less-significant bit to track that fact.
187   // See detailed comment in the `getSymVA` function.
188   if (config->emachine == EM_MIPS && isMicroMips())
189     outVA |= 1;
190   return outVA;
191 }
192
193 uint64_t Symbol::getSize() const {
194   if (const auto *dr = dyn_cast<Defined>(this))
195     return dr->size;
196   return cast<SharedSymbol>(this)->size;
197 }
198
199 OutputSection *Symbol::getOutputSection() const {
200   if (auto *s = dyn_cast<Defined>(this)) {
201     if (auto *sec = s->section)
202       return sec->repl->getOutputSection();
203     return nullptr;
204   }
205   return nullptr;
206 }
207
208 // If a symbol name contains '@', the characters after that is
209 // a symbol version name. This function parses that.
210 void Symbol::parseSymbolVersion() {
211   // Return if localized by a local: pattern in a version script.
212   if (versionId == VER_NDX_LOCAL)
213     return;
214   StringRef s = getName();
215   size_t pos = s.find('@');
216   if (pos == 0 || pos == StringRef::npos)
217     return;
218   StringRef verstr = s.substr(pos + 1);
219   if (verstr.empty())
220     return;
221
222   // Truncate the symbol name so that it doesn't include the version string.
223   nameSize = pos;
224
225   // If this is not in this DSO, it is not a definition.
226   if (!isDefined())
227     return;
228
229   // '@@' in a symbol name means the default version.
230   // It is usually the most recent one.
231   bool isDefault = (verstr[0] == '@');
232   if (isDefault)
233     verstr = verstr.substr(1);
234
235   for (const VersionDefinition &ver : namedVersionDefs()) {
236     if (ver.name != verstr)
237       continue;
238
239     if (isDefault)
240       versionId = ver.id;
241     else
242       versionId = ver.id | VERSYM_HIDDEN;
243     return;
244   }
245
246   // It is an error if the specified version is not defined.
247   // Usually version script is not provided when linking executable,
248   // but we may still want to override a versioned symbol from DSO,
249   // so we do not report error in this case. We also do not error
250   // if the symbol has a local version as it won't be in the dynamic
251   // symbol table.
252   if (config->shared && versionId != VER_NDX_LOCAL)
253     error(toString(file) + ": symbol " + s + " has undefined version " +
254           verstr);
255 }
256
257 void Symbol::fetch() const {
258   if (auto *sym = dyn_cast<LazyArchive>(this)) {
259     cast<ArchiveFile>(sym->file)->fetch(sym->sym);
260     return;
261   }
262
263   if (auto *sym = dyn_cast<LazyObject>(this)) {
264     dyn_cast<LazyObjFile>(sym->file)->fetch();
265     return;
266   }
267
268   llvm_unreachable("Symbol::fetch() is called on a non-lazy symbol");
269 }
270
271 MemoryBufferRef LazyArchive::getMemberBuffer() {
272   Archive::Child c =
273       CHECK(sym.getMember(),
274             "could not get the member for symbol " + toELFString(sym));
275
276   return CHECK(c.getMemoryBufferRef(),
277                "could not get the buffer for the member defining symbol " +
278                    toELFString(sym));
279 }
280
281 uint8_t Symbol::computeBinding() const {
282   if (config->relocatable)
283     return binding;
284   if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) ||
285       (versionId == VER_NDX_LOCAL && !isLazy()))
286     return STB_LOCAL;
287   if (!config->gnuUnique && binding == STB_GNU_UNIQUE)
288     return STB_GLOBAL;
289   return binding;
290 }
291
292 bool Symbol::includeInDynsym() const {
293   if (!config->hasDynSymTab)
294     return false;
295   if (computeBinding() == STB_LOCAL)
296     return false;
297   if (!isDefined() && !isCommon())
298     // This should unconditionally return true, unfortunately glibc -static-pie
299     // expects undefined weak symbols not to exist in .dynsym, e.g.
300     // __pthread_mutex_lock reference in _dl_add_to_namespace_list,
301     // __pthread_initialize_minimal reference in csu/libc-start.c.
302     return !(config->noDynamicLinker && isUndefWeak());
303
304   return exportDynamic || inDynamicList;
305 }
306
307 // Print out a log message for --trace-symbol.
308 void elf::printTraceSymbol(const Symbol *sym) {
309   std::string s;
310   if (sym->isUndefined())
311     s = ": reference to ";
312   else if (sym->isLazy())
313     s = ": lazy definition of ";
314   else if (sym->isShared())
315     s = ": shared definition of ";
316   else if (sym->isCommon())
317     s = ": common definition of ";
318   else
319     s = ": definition of ";
320
321   message(toString(sym->file) + s + sym->getName());
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 fetch archive members. See comment on Lazy in
475     // 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     // fetching an archive member written earlier in the command line. It can be
486     // used to keep compatibility with GNU linkers to some degree.
487     // I'll 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     fetch();
535
536     // We don't report backward references to weak symbols as they can be
537     // overridden later.
538     //
539     // A traditional linker does not error for -ldef1 -lref -ldef2 (linking
540     // sandwich), where def2 may or may not be the same as def1. We don't want
541     // to warn for this case, so dismiss the warning if we see a subsequent lazy
542     // definition. this->file needs to be saved because in the case of LTO it
543     // may be reset to nullptr or be replaced with a file named lto.tmp.
544     if (backref && !isWeak())
545       backwardReferences.try_emplace(this, std::make_pair(other.file, file));
546     return;
547   }
548
549   // Undefined symbols in a SharedFile do not change the binding.
550   if (dyn_cast_or_null<SharedFile>(other.file))
551     return;
552
553   if (isUndefined() || isShared()) {
554     // The binding will be weak if there is at least one reference and all are
555     // weak. The binding has one opportunity to change to weak: if the first
556     // reference is weak.
557     if (other.binding != STB_WEAK || !referenced)
558       binding = other.binding;
559   }
560 }
561
562 // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
563 // foo@@VER. We want to effectively ignore foo, so give precedence to
564 // foo@@VER.
565 // FIXME: If users can transition to using
566 // .symver foo,foo@@@VER
567 // we can delete this hack.
568 static int compareVersion(StringRef a, StringRef b) {
569   bool x = a.contains("@@");
570   bool y = b.contains("@@");
571   if (!x && y)
572     return 1;
573   if (x && !y)
574     return -1;
575   return 0;
576 }
577
578 // Compare two symbols. Return 1 if the new symbol should win, -1 if
579 // the new symbol should lose, or 0 if there is a conflict.
580 int Symbol::compare(const Symbol *other) const {
581   assert(other->isDefined() || other->isCommon());
582
583   if (!isDefined() && !isCommon())
584     return 1;
585
586   if (int cmp = compareVersion(getName(), other->getName()))
587     return cmp;
588
589   if (other->isWeak())
590     return -1;
591
592   if (isWeak())
593     return 1;
594
595   if (isCommon() && other->isCommon()) {
596     if (config->warnCommon)
597       warn("multiple common of " + getName());
598     return 0;
599   }
600
601   if (isCommon()) {
602     if (config->warnCommon)
603       warn("common " + getName() + " is overridden");
604     return 1;
605   }
606
607   if (other->isCommon()) {
608     if (config->warnCommon)
609       warn("common " + getName() + " is overridden");
610     return -1;
611   }
612
613   auto *oldSym = cast<Defined>(this);
614   auto *newSym = cast<Defined>(other);
615
616   if (dyn_cast_or_null<BitcodeFile>(other->file))
617     return 0;
618
619   if (!oldSym->section && !newSym->section && oldSym->value == newSym->value &&
620       newSym->binding == STB_GLOBAL)
621     return -1;
622
623   return 0;
624 }
625
626 static void reportDuplicate(Symbol *sym, InputFile *newFile,
627                             InputSectionBase *errSec, uint64_t errOffset) {
628   if (config->allowMultipleDefinition)
629     return;
630
631   Defined *d = cast<Defined>(sym);
632   if (!d->section || !errSec) {
633     error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " +
634           toString(sym->file) + "\n>>> defined in " + toString(newFile));
635     return;
636   }
637
638   // Construct and print an error message in the form of:
639   //
640   //   ld.lld: error: duplicate symbol: foo
641   //   >>> defined at bar.c:30
642   //   >>>            bar.o (/home/alice/src/bar.o)
643   //   >>> defined at baz.c:563
644   //   >>>            baz.o in archive libbaz.a
645   auto *sec1 = cast<InputSectionBase>(d->section);
646   std::string src1 = sec1->getSrcMsg(*sym, d->value);
647   std::string obj1 = sec1->getObjMsg(d->value);
648   std::string src2 = errSec->getSrcMsg(*sym, errOffset);
649   std::string obj2 = errSec->getObjMsg(errOffset);
650
651   std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at ";
652   if (!src1.empty())
653     msg += src1 + "\n>>>            ";
654   msg += obj1 + "\n>>> defined at ";
655   if (!src2.empty())
656     msg += src2 + "\n>>>            ";
657   msg += obj2;
658   error(msg);
659 }
660
661 void Symbol::resolveCommon(const CommonSymbol &other) {
662   int cmp = compare(&other);
663   if (cmp < 0)
664     return;
665
666   if (cmp > 0) {
667     if (auto *s = dyn_cast<SharedSymbol>(this)) {
668       // Increase st_size if the shared symbol has a larger st_size. The shared
669       // symbol may be created from common symbols. The fact that some object
670       // files were linked into a shared object first should not change the
671       // regular rule that picks the largest st_size.
672       uint64_t size = s->size;
673       replace(other);
674       if (size > cast<CommonSymbol>(this)->size)
675         cast<CommonSymbol>(this)->size = size;
676     } else {
677       replace(other);
678     }
679     return;
680   }
681
682   CommonSymbol *oldSym = cast<CommonSymbol>(this);
683
684   oldSym->alignment = std::max(oldSym->alignment, other.alignment);
685   if (oldSym->size < other.size) {
686     oldSym->file = other.file;
687     oldSym->size = other.size;
688   }
689 }
690
691 void Symbol::resolveDefined(const Defined &other) {
692   int cmp = compare(&other);
693   if (cmp > 0)
694     replace(other);
695   else if (cmp == 0)
696     reportDuplicate(this, other.file,
697                     dyn_cast_or_null<InputSectionBase>(other.section),
698                     other.value);
699 }
700
701 template <class LazyT>
702 static void replaceCommon(Symbol &oldSym, const LazyT &newSym) {
703   backwardReferences.erase(&oldSym);
704   oldSym.replace(newSym);
705   newSym.fetch();
706 }
707
708 template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
709   // For common objects, we want to look for global or weak definitions that
710   // should be fetched as the canonical definition instead.
711   if (isCommon() && elf::config->fortranCommon) {
712     if (auto *laSym = dyn_cast<LazyArchive>(&other)) {
713       ArchiveFile *archive = cast<ArchiveFile>(laSym->file);
714       const Archive::Symbol &archiveSym = laSym->sym;
715       if (archive->shouldFetchForCommon(archiveSym)) {
716         replaceCommon(*this, other);
717         return;
718       }
719     } else if (auto *loSym = dyn_cast<LazyObject>(&other)) {
720       LazyObjFile *obj = cast<LazyObjFile>(loSym->file);
721       if (obj->shouldFetchForCommon(loSym->getName())) {
722         replaceCommon(*this, other);
723         return;
724       }
725     }
726   }
727
728   if (!isUndefined()) {
729     // See the comment in resolveUndefined().
730     if (isDefined())
731       backwardReferences.erase(this);
732     return;
733   }
734
735   // An undefined weak will not fetch archive members. See comment on Lazy in
736   // Symbols.h for the details.
737   if (isWeak()) {
738     uint8_t ty = type;
739     replace(other);
740     type = ty;
741     binding = STB_WEAK;
742     return;
743   }
744
745   other.fetch();
746 }
747
748 void Symbol::resolveShared(const SharedSymbol &other) {
749   if (isCommon()) {
750     // See the comment in resolveCommon() above.
751     if (other.size > cast<CommonSymbol>(this)->size)
752       cast<CommonSymbol>(this)->size = other.size;
753     return;
754   }
755   if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) {
756     // An undefined symbol with non default visibility must be satisfied
757     // in the same DSO.
758     uint8_t bind = binding;
759     replace(other);
760     binding = bind;
761   } else if (traced)
762     printTraceSymbol(&other);
763 }