xref: /freebsd/contrib/llvm-project/lld/ELF/Symbols.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric //===- Symbols.cpp --------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "Symbols.h"
100b57cec5SDimitry Andric #include "InputFiles.h"
110b57cec5SDimitry Andric #include "InputSection.h"
120b57cec5SDimitry Andric #include "OutputSections.h"
130b57cec5SDimitry Andric #include "SyntheticSections.h"
140b57cec5SDimitry Andric #include "Target.h"
150b57cec5SDimitry Andric #include "Writer.h"
160b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h"
170b57cec5SDimitry Andric #include "lld/Common/Strings.h"
180b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
195ffd83dbSDimitry Andric #include "llvm/Support/FileSystem.h"
200b57cec5SDimitry Andric #include "llvm/Support/Path.h"
210b57cec5SDimitry Andric #include <cstring>
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric using namespace llvm::object;
250b57cec5SDimitry Andric using namespace llvm::ELF;
265ffd83dbSDimitry Andric using namespace lld;
275ffd83dbSDimitry Andric using namespace lld::elf;
280b57cec5SDimitry Andric 
295ffd83dbSDimitry Andric std::string lld::toString(const elf::Symbol &sym) {
305ffd83dbSDimitry Andric   StringRef name = sym.getName();
31*04eeddc0SDimitry Andric   std::string ret = demangle(name, config->demangle);
325ffd83dbSDimitry Andric 
33e8d8bef9SDimitry Andric   const char *suffix = sym.getVersionSuffix();
34e8d8bef9SDimitry Andric   if (*suffix == '@')
35e8d8bef9SDimitry Andric     ret += suffix;
365ffd83dbSDimitry Andric   return ret;
375ffd83dbSDimitry Andric }
385ffd83dbSDimitry Andric 
395ffd83dbSDimitry Andric std::string lld::toELFString(const Archive::Symbol &b) {
40*04eeddc0SDimitry Andric   return demangle(b.getName(), config->demangle);
4185868e8aSDimitry Andric }
4285868e8aSDimitry Andric 
430b57cec5SDimitry Andric Defined *ElfSym::bss;
440b57cec5SDimitry Andric Defined *ElfSym::etext1;
450b57cec5SDimitry Andric Defined *ElfSym::etext2;
460b57cec5SDimitry Andric Defined *ElfSym::edata1;
470b57cec5SDimitry Andric Defined *ElfSym::edata2;
480b57cec5SDimitry Andric Defined *ElfSym::end1;
490b57cec5SDimitry Andric Defined *ElfSym::end2;
500b57cec5SDimitry Andric Defined *ElfSym::globalOffsetTable;
510b57cec5SDimitry Andric Defined *ElfSym::mipsGp;
520b57cec5SDimitry Andric Defined *ElfSym::mipsGpDisp;
530b57cec5SDimitry Andric Defined *ElfSym::mipsLocalGp;
540b57cec5SDimitry Andric Defined *ElfSym::relaIpltStart;
550b57cec5SDimitry Andric Defined *ElfSym::relaIpltEnd;
560b57cec5SDimitry Andric Defined *ElfSym::riscvGlobalPointer;
570b57cec5SDimitry Andric Defined *ElfSym::tlsModuleBase;
58e8d8bef9SDimitry Andric DenseMap<const Symbol *, std::pair<const InputFile *, const InputFile *>>
59e8d8bef9SDimitry Andric     elf::backwardReferences;
60349cc55cSDimitry Andric SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>, 0>
61349cc55cSDimitry Andric     elf::whyExtract;
62*04eeddc0SDimitry Andric SmallVector<SymbolAux, 0> elf::symAux;
630b57cec5SDimitry Andric 
640eae32dcSDimitry Andric static uint64_t getSymVA(const Symbol &sym, int64_t addend) {
650b57cec5SDimitry Andric   switch (sym.kind()) {
660b57cec5SDimitry Andric   case Symbol::DefinedKind: {
670b57cec5SDimitry Andric     auto &d = cast<Defined>(sym);
680b57cec5SDimitry Andric     SectionBase *isec = d.section;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric     // This is an absolute symbol.
710b57cec5SDimitry Andric     if (!isec)
720b57cec5SDimitry Andric       return d.value;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric     assert(isec != &InputSection::discarded);
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric     uint64_t offset = d.value;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric     // An object in an SHF_MERGE section might be referenced via a
790b57cec5SDimitry Andric     // section symbol (as a hack for reducing the number of local
800b57cec5SDimitry Andric     // symbols).
810b57cec5SDimitry Andric     // Depending on the addend, the reference via a section symbol
820b57cec5SDimitry Andric     // refers to a different object in the merge section.
830b57cec5SDimitry Andric     // Since the objects in the merge section are not necessarily
840b57cec5SDimitry Andric     // contiguous in the output, the addend can thus affect the final
850b57cec5SDimitry Andric     // VA in a non-linear way.
860b57cec5SDimitry Andric     // To make this work, we incorporate the addend into the section
870b57cec5SDimitry Andric     // offset (and zero out the addend for later processing) so that
880b57cec5SDimitry Andric     // we find the right object in the section.
890eae32dcSDimitry Andric     if (d.isSection())
900b57cec5SDimitry Andric       offset += addend;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric     // In the typical case, this is actually very simple and boils
930b57cec5SDimitry Andric     // down to adding together 3 numbers:
940b57cec5SDimitry Andric     // 1. The address of the output section.
950b57cec5SDimitry Andric     // 2. The offset of the input section within the output section.
960b57cec5SDimitry Andric     // 3. The offset within the input section (this addition happens
970b57cec5SDimitry Andric     //    inside InputSection::getOffset).
980b57cec5SDimitry Andric     //
990b57cec5SDimitry Andric     // If you understand the data structures involved with this next
1000b57cec5SDimitry Andric     // line (and how they get built), then you have a pretty good
1010b57cec5SDimitry Andric     // understanding of the linker.
1020b57cec5SDimitry Andric     uint64_t va = isec->getVA(offset);
1030eae32dcSDimitry Andric     if (d.isSection())
1040eae32dcSDimitry Andric       va -= addend;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric     // MIPS relocatable files can mix regular and microMIPS code.
1070b57cec5SDimitry Andric     // Linker needs to distinguish such code. To do so microMIPS
1080b57cec5SDimitry Andric     // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
1095ffd83dbSDimitry Andric     // field. Unfortunately, the `MIPS::relocate()` method has
1100b57cec5SDimitry Andric     // a symbol value only. To pass type of the symbol (regular/microMIPS)
1110b57cec5SDimitry Andric     // to that routine as well as other places where we write
1120b57cec5SDimitry Andric     // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
1130b57cec5SDimitry Andric     // field etc) do the same trick as compiler uses to mark microMIPS
1140b57cec5SDimitry Andric     // for CPU - set the less-significant bit.
1150b57cec5SDimitry Andric     if (config->emachine == EM_MIPS && isMicroMips() &&
1160eae32dcSDimitry Andric         ((sym.stOther & STO_MIPS_MICROMIPS) || sym.needsCopy))
1170b57cec5SDimitry Andric       va |= 1;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric     if (d.isTls() && !config->relocatable) {
1200b57cec5SDimitry Andric       // Use the address of the TLS segment's first section rather than the
1210b57cec5SDimitry Andric       // segment's address, because segment addresses aren't initialized until
1220b57cec5SDimitry Andric       // after sections are finalized. (e.g. Measuring the size of .rela.dyn
1230b57cec5SDimitry Andric       // for Android relocation packing requires knowing TLS symbol addresses
1240b57cec5SDimitry Andric       // during section finalization.)
1250b57cec5SDimitry Andric       if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec)
1260b57cec5SDimitry Andric         fatal(toString(d.file) +
1270b57cec5SDimitry Andric               " has an STT_TLS symbol but doesn't have an SHF_TLS section");
1280b57cec5SDimitry Andric       return va - Out::tlsPhdr->firstSec->addr;
1290b57cec5SDimitry Andric     }
1300b57cec5SDimitry Andric     return va;
1310b57cec5SDimitry Andric   }
1320b57cec5SDimitry Andric   case Symbol::SharedKind:
1330b57cec5SDimitry Andric   case Symbol::UndefinedKind:
1340b57cec5SDimitry Andric     return 0;
1350b57cec5SDimitry Andric   case Symbol::LazyArchiveKind:
1360b57cec5SDimitry Andric   case Symbol::LazyObjectKind:
137*04eeddc0SDimitry Andric     llvm_unreachable("lazy symbol reached writer");
1380b57cec5SDimitry Andric   case Symbol::CommonKind:
1390b57cec5SDimitry Andric     llvm_unreachable("common symbol reached writer");
1400b57cec5SDimitry Andric   case Symbol::PlaceholderKind:
1410b57cec5SDimitry Andric     llvm_unreachable("placeholder symbol reached writer");
1420b57cec5SDimitry Andric   }
1430b57cec5SDimitry Andric   llvm_unreachable("invalid symbol kind");
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric uint64_t Symbol::getVA(int64_t addend) const {
1470eae32dcSDimitry Andric   return getSymVA(*this, addend) + addend;
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric uint64_t Symbol::getGotVA() const {
1510b57cec5SDimitry Andric   if (gotInIgot)
1520b57cec5SDimitry Andric     return in.igotPlt->getVA() + getGotPltOffset();
1530b57cec5SDimitry Andric   return in.got->getVA() + getGotOffset();
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
156fe6060f1SDimitry Andric uint64_t Symbol::getGotOffset() const {
157*04eeddc0SDimitry Andric   return getGotIdx() * target->gotEntrySize;
158fe6060f1SDimitry Andric }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric uint64_t Symbol::getGotPltVA() const {
1610b57cec5SDimitry Andric   if (isInIplt)
1620b57cec5SDimitry Andric     return in.igotPlt->getVA() + getGotPltOffset();
1630b57cec5SDimitry Andric   return in.gotPlt->getVA() + getGotPltOffset();
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric uint64_t Symbol::getGotPltOffset() const {
1670b57cec5SDimitry Andric   if (isInIplt)
168*04eeddc0SDimitry Andric     return getPltIdx() * target->gotEntrySize;
169*04eeddc0SDimitry Andric   return (getPltIdx() + target->gotPltHeaderEntriesNum) * target->gotEntrySize;
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric uint64_t Symbol::getPltVA() const {
173480093f4SDimitry Andric   uint64_t outVA = isInIplt
174*04eeddc0SDimitry Andric                        ? in.iplt->getVA() + getPltIdx() * target->ipltEntrySize
175480093f4SDimitry Andric                        : in.plt->getVA() + in.plt->headerSize +
176*04eeddc0SDimitry Andric                              getPltIdx() * target->pltEntrySize;
177480093f4SDimitry Andric 
1780b57cec5SDimitry Andric   // While linking microMIPS code PLT code are always microMIPS
1790b57cec5SDimitry Andric   // code. Set the less-significant bit to track that fact.
1800b57cec5SDimitry Andric   // See detailed comment in the `getSymVA` function.
1810b57cec5SDimitry Andric   if (config->emachine == EM_MIPS && isMicroMips())
1820b57cec5SDimitry Andric     outVA |= 1;
1830b57cec5SDimitry Andric   return outVA;
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric uint64_t Symbol::getSize() const {
1870b57cec5SDimitry Andric   if (const auto *dr = dyn_cast<Defined>(this))
1880b57cec5SDimitry Andric     return dr->size;
1890b57cec5SDimitry Andric   return cast<SharedSymbol>(this)->size;
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric OutputSection *Symbol::getOutputSection() const {
1930b57cec5SDimitry Andric   if (auto *s = dyn_cast<Defined>(this)) {
1940b57cec5SDimitry Andric     if (auto *sec = s->section)
1950eae32dcSDimitry Andric       return sec->getOutputSection();
1960b57cec5SDimitry Andric     return nullptr;
1970b57cec5SDimitry Andric   }
1980b57cec5SDimitry Andric   return nullptr;
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric // If a symbol name contains '@', the characters after that is
2020b57cec5SDimitry Andric // a symbol version name. This function parses that.
2030b57cec5SDimitry Andric void Symbol::parseSymbolVersion() {
2046e75b2fbSDimitry Andric   // Return if localized by a local: pattern in a version script.
2056e75b2fbSDimitry Andric   if (versionId == VER_NDX_LOCAL)
2066e75b2fbSDimitry Andric     return;
2070b57cec5SDimitry Andric   StringRef s = getName();
2080b57cec5SDimitry Andric   size_t pos = s.find('@');
2090eae32dcSDimitry Andric   if (pos == StringRef::npos)
2100b57cec5SDimitry Andric     return;
2110b57cec5SDimitry Andric   StringRef verstr = s.substr(pos + 1);
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   // Truncate the symbol name so that it doesn't include the version string.
2140b57cec5SDimitry Andric   nameSize = pos;
2150b57cec5SDimitry Andric 
216*04eeddc0SDimitry Andric   if (verstr.empty())
217*04eeddc0SDimitry Andric     return;
218*04eeddc0SDimitry Andric 
2190b57cec5SDimitry Andric   // If this is not in this DSO, it is not a definition.
2200b57cec5SDimitry Andric   if (!isDefined())
2210b57cec5SDimitry Andric     return;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   // '@@' in a symbol name means the default version.
2240b57cec5SDimitry Andric   // It is usually the most recent one.
2250b57cec5SDimitry Andric   bool isDefault = (verstr[0] == '@');
2260b57cec5SDimitry Andric   if (isDefault)
2270b57cec5SDimitry Andric     verstr = verstr.substr(1);
2280b57cec5SDimitry Andric 
22985868e8aSDimitry Andric   for (const VersionDefinition &ver : namedVersionDefs()) {
2300b57cec5SDimitry Andric     if (ver.name != verstr)
2310b57cec5SDimitry Andric       continue;
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric     if (isDefault)
2340b57cec5SDimitry Andric       versionId = ver.id;
2350b57cec5SDimitry Andric     else
2360b57cec5SDimitry Andric       versionId = ver.id | VERSYM_HIDDEN;
2370b57cec5SDimitry Andric     return;
2380b57cec5SDimitry Andric   }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   // It is an error if the specified version is not defined.
2410b57cec5SDimitry Andric   // Usually version script is not provided when linking executable,
2420b57cec5SDimitry Andric   // but we may still want to override a versioned symbol from DSO,
2430b57cec5SDimitry Andric   // so we do not report error in this case. We also do not error
2440b57cec5SDimitry Andric   // if the symbol has a local version as it won't be in the dynamic
2450b57cec5SDimitry Andric   // symbol table.
2460b57cec5SDimitry Andric   if (config->shared && versionId != VER_NDX_LOCAL)
2470b57cec5SDimitry Andric     error(toString(file) + ": symbol " + s + " has undefined version " +
2480b57cec5SDimitry Andric           verstr);
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric 
2514824e7fdSDimitry Andric void Symbol::extract() const {
2520eae32dcSDimitry Andric   if (auto *sym = dyn_cast<LazyArchive>(this)) {
2534824e7fdSDimitry Andric     cast<ArchiveFile>(sym->file)->extract(sym->sym);
2540eae32dcSDimitry Andric   } else if (file->lazy) {
2550eae32dcSDimitry Andric     file->lazy = false;
2560eae32dcSDimitry Andric     parseFile(file);
2570eae32dcSDimitry Andric   }
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric MemoryBufferRef LazyArchive::getMemberBuffer() {
2610b57cec5SDimitry Andric   Archive::Child c =
2620b57cec5SDimitry Andric       CHECK(sym.getMember(),
2630b57cec5SDimitry Andric             "could not get the member for symbol " + toELFString(sym));
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   return CHECK(c.getMemoryBufferRef(),
2660b57cec5SDimitry Andric                "could not get the buffer for the member defining symbol " +
2670b57cec5SDimitry Andric                    toELFString(sym));
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric uint8_t Symbol::computeBinding() const {
27185868e8aSDimitry Andric   if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) ||
272*04eeddc0SDimitry Andric       versionId == VER_NDX_LOCAL)
2730b57cec5SDimitry Andric     return STB_LOCAL;
274*04eeddc0SDimitry Andric   if (binding == STB_GNU_UNIQUE && !config->gnuUnique)
2750b57cec5SDimitry Andric     return STB_GLOBAL;
2760b57cec5SDimitry Andric   return binding;
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric bool Symbol::includeInDynsym() const {
2800b57cec5SDimitry Andric   if (computeBinding() == STB_LOCAL)
2810b57cec5SDimitry Andric     return false;
282480093f4SDimitry Andric   if (!isDefined() && !isCommon())
28355e4f9d5SDimitry Andric     // This should unconditionally return true, unfortunately glibc -static-pie
28455e4f9d5SDimitry Andric     // expects undefined weak symbols not to exist in .dynsym, e.g.
28555e4f9d5SDimitry Andric     // __pthread_mutex_lock reference in _dl_add_to_namespace_list,
28655e4f9d5SDimitry Andric     // __pthread_initialize_minimal reference in csu/libc-start.c.
287*04eeddc0SDimitry Andric     return !(isUndefWeak() && config->noDynamicLinker);
2880b57cec5SDimitry Andric 
289480093f4SDimitry Andric   return exportDynamic || inDynamicList;
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric // Print out a log message for --trace-symbol.
2935ffd83dbSDimitry Andric void elf::printTraceSymbol(const Symbol *sym) {
2940b57cec5SDimitry Andric   std::string s;
2950b57cec5SDimitry Andric   if (sym->isUndefined())
2960b57cec5SDimitry Andric     s = ": reference to ";
2970b57cec5SDimitry Andric   else if (sym->isLazy())
2980b57cec5SDimitry Andric     s = ": lazy definition of ";
2990b57cec5SDimitry Andric   else if (sym->isShared())
3000b57cec5SDimitry Andric     s = ": shared definition of ";
3010b57cec5SDimitry Andric   else if (sym->isCommon())
3020b57cec5SDimitry Andric     s = ": common definition of ";
3030b57cec5SDimitry Andric   else
3040b57cec5SDimitry Andric     s = ": definition of ";
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric   message(toString(sym->file) + s + sym->getName());
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric 
309349cc55cSDimitry Andric static void recordWhyExtract(const InputFile *reference,
310349cc55cSDimitry Andric                              const InputFile &extracted, const Symbol &sym) {
311349cc55cSDimitry Andric   whyExtract.emplace_back(toString(reference), &extracted, sym);
312349cc55cSDimitry Andric }
313349cc55cSDimitry Andric 
3145ffd83dbSDimitry Andric void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
3150b57cec5SDimitry Andric   if (!config->warnSymbolOrdering)
3160b57cec5SDimitry Andric     return;
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
3190b57cec5SDimitry Andric   // is emitted. It makes sense to not warn on undefined symbols.
3200b57cec5SDimitry Andric   //
3210b57cec5SDimitry Andric   // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
3220b57cec5SDimitry Andric   // but we don't have to be compatible here.
3230b57cec5SDimitry Andric   if (sym->isUndefined() &&
3240b57cec5SDimitry Andric       config->unresolvedSymbols == UnresolvedPolicy::Ignore)
3250b57cec5SDimitry Andric     return;
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric   const InputFile *file = sym->file;
3280b57cec5SDimitry Andric   auto *d = dyn_cast<Defined>(sym);
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); };
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   if (sym->isUndefined())
3330b57cec5SDimitry Andric     report(": unable to order undefined symbol: ");
3340b57cec5SDimitry Andric   else if (sym->isShared())
3350b57cec5SDimitry Andric     report(": unable to order shared symbol: ");
3360b57cec5SDimitry Andric   else if (d && !d->section)
3370b57cec5SDimitry Andric     report(": unable to order absolute symbol: ");
3380b57cec5SDimitry Andric   else if (d && isa<OutputSection>(d->section))
3390b57cec5SDimitry Andric     report(": unable to order synthetic symbol: ");
3400eae32dcSDimitry Andric   else if (d && !d->section->isLive())
3410b57cec5SDimitry Andric     report(": unable to order discarded symbol: ");
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric 
344480093f4SDimitry Andric // Returns true if a symbol can be replaced at load-time by a symbol
345480093f4SDimitry Andric // with the same name defined in other ELF executable or DSO.
3465ffd83dbSDimitry Andric bool elf::computeIsPreemptible(const Symbol &sym) {
347*04eeddc0SDimitry Andric   assert(!sym.isLocal() || sym.isPlaceholder());
348480093f4SDimitry Andric 
349480093f4SDimitry Andric   // Only symbols with default visibility that appear in dynsym can be
350480093f4SDimitry Andric   // preempted. Symbols with protected visibility cannot be preempted.
351480093f4SDimitry Andric   if (!sym.includeInDynsym() || sym.visibility != STV_DEFAULT)
352480093f4SDimitry Andric     return false;
353480093f4SDimitry Andric 
354480093f4SDimitry Andric   // At this point copy relocations have not been created yet, so any
355480093f4SDimitry Andric   // symbol that is not defined locally is preemptible.
356480093f4SDimitry Andric   if (!sym.isDefined())
357480093f4SDimitry Andric     return true;
358480093f4SDimitry Andric 
359480093f4SDimitry Andric   if (!config->shared)
360480093f4SDimitry Andric     return false;
361480093f4SDimitry Andric 
3625ffd83dbSDimitry Andric   // If -Bsymbolic or --dynamic-list is specified, or -Bsymbolic-functions is
3635ffd83dbSDimitry Andric   // specified and the symbol is STT_FUNC, the symbol is preemptible iff it is
3646e75b2fbSDimitry Andric   // in the dynamic list. -Bsymbolic-non-weak-functions is a non-weak subset of
3656e75b2fbSDimitry Andric   // -Bsymbolic-functions.
3666e75b2fbSDimitry Andric   if (config->symbolic ||
3676e75b2fbSDimitry Andric       (config->bsymbolic == BsymbolicKind::Functions && sym.isFunc()) ||
3686e75b2fbSDimitry Andric       (config->bsymbolic == BsymbolicKind::NonWeakFunctions && sym.isFunc() &&
3696e75b2fbSDimitry Andric        sym.binding != STB_WEAK))
370480093f4SDimitry Andric     return sym.inDynamicList;
371480093f4SDimitry Andric   return true;
372480093f4SDimitry Andric }
373480093f4SDimitry Andric 
3745ffd83dbSDimitry Andric void elf::reportBackrefs() {
3755ffd83dbSDimitry Andric   for (auto &it : backwardReferences) {
3765ffd83dbSDimitry Andric     const Symbol &sym = *it.first;
377e8d8bef9SDimitry Andric     std::string to = toString(it.second.second);
378e8d8bef9SDimitry Andric     // Some libraries have known problems and can cause noise. Filter them out
379e8d8bef9SDimitry Andric     // with --warn-backrefs-exclude=. to may look like *.o or *.a(*.o).
380e8d8bef9SDimitry Andric     bool exclude = false;
381e8d8bef9SDimitry Andric     for (const llvm::GlobPattern &pat : config->warnBackrefsExclude)
382e8d8bef9SDimitry Andric       if (pat.match(to)) {
383e8d8bef9SDimitry Andric         exclude = true;
384e8d8bef9SDimitry Andric         break;
385e8d8bef9SDimitry Andric       }
386e8d8bef9SDimitry Andric     if (!exclude)
3875ffd83dbSDimitry Andric       warn("backward reference detected: " + sym.getName() + " in " +
388e8d8bef9SDimitry Andric            toString(it.second.first) + " refers to " + to);
3895ffd83dbSDimitry Andric   }
3905ffd83dbSDimitry Andric }
3915ffd83dbSDimitry Andric 
3920b57cec5SDimitry Andric static uint8_t getMinVisibility(uint8_t va, uint8_t vb) {
3930b57cec5SDimitry Andric   if (va == STV_DEFAULT)
3940b57cec5SDimitry Andric     return vb;
3950b57cec5SDimitry Andric   if (vb == STV_DEFAULT)
3960b57cec5SDimitry Andric     return va;
3970b57cec5SDimitry Andric   return std::min(va, vb);
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric // Merge symbol properties.
4010b57cec5SDimitry Andric //
4020b57cec5SDimitry Andric // When we have many symbols of the same name, we choose one of them,
4030b57cec5SDimitry Andric // and that's the result of symbol resolution. However, symbols that
4040b57cec5SDimitry Andric // were not chosen still affect some symbol properties.
4050b57cec5SDimitry Andric void Symbol::mergeProperties(const Symbol &other) {
4060b57cec5SDimitry Andric   if (other.exportDynamic)
4070b57cec5SDimitry Andric     exportDynamic = true;
4080b57cec5SDimitry Andric   if (other.isUsedInRegularObj)
4090b57cec5SDimitry Andric     isUsedInRegularObj = true;
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   // DSO symbols do not affect visibility in the output.
4120b57cec5SDimitry Andric   if (!other.isShared())
4130b57cec5SDimitry Andric     visibility = getMinVisibility(visibility, other.visibility);
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric void Symbol::resolve(const Symbol &other) {
4170b57cec5SDimitry Andric   mergeProperties(other);
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   if (isPlaceholder()) {
4200b57cec5SDimitry Andric     replace(other);
4210b57cec5SDimitry Andric     return;
4220b57cec5SDimitry Andric   }
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   switch (other.kind()) {
4250b57cec5SDimitry Andric   case Symbol::UndefinedKind:
4260b57cec5SDimitry Andric     resolveUndefined(cast<Undefined>(other));
4270b57cec5SDimitry Andric     break;
4280b57cec5SDimitry Andric   case Symbol::CommonKind:
4290b57cec5SDimitry Andric     resolveCommon(cast<CommonSymbol>(other));
4300b57cec5SDimitry Andric     break;
4310b57cec5SDimitry Andric   case Symbol::DefinedKind:
4320b57cec5SDimitry Andric     resolveDefined(cast<Defined>(other));
4330b57cec5SDimitry Andric     break;
4340b57cec5SDimitry Andric   case Symbol::LazyArchiveKind:
4350b57cec5SDimitry Andric     resolveLazy(cast<LazyArchive>(other));
4360b57cec5SDimitry Andric     break;
4370b57cec5SDimitry Andric   case Symbol::LazyObjectKind:
4380b57cec5SDimitry Andric     resolveLazy(cast<LazyObject>(other));
4390b57cec5SDimitry Andric     break;
4400b57cec5SDimitry Andric   case Symbol::SharedKind:
4410b57cec5SDimitry Andric     resolveShared(cast<SharedSymbol>(other));
4420b57cec5SDimitry Andric     break;
4430b57cec5SDimitry Andric   case Symbol::PlaceholderKind:
4440b57cec5SDimitry Andric     llvm_unreachable("bad symbol kind");
4450b57cec5SDimitry Andric   }
4460b57cec5SDimitry Andric }
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric void Symbol::resolveUndefined(const Undefined &other) {
4490b57cec5SDimitry Andric   // An undefined symbol with non default visibility must be satisfied
4500b57cec5SDimitry Andric   // in the same DSO.
4510b57cec5SDimitry Andric   //
4520b57cec5SDimitry Andric   // If this is a non-weak defined symbol in a discarded section, override the
4530b57cec5SDimitry Andric   // existing undefined symbol for better error message later.
4540b57cec5SDimitry Andric   if ((isShared() && other.visibility != STV_DEFAULT) ||
4550b57cec5SDimitry Andric       (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) {
4560b57cec5SDimitry Andric     replace(other);
4570b57cec5SDimitry Andric     return;
4580b57cec5SDimitry Andric   }
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric   if (traced)
4610b57cec5SDimitry Andric     printTraceSymbol(&other);
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   if (isLazy()) {
4644824e7fdSDimitry Andric     // An undefined weak will not extract archive members. See comment on Lazy
4654824e7fdSDimitry Andric     // in Symbols.h for the details.
4660b57cec5SDimitry Andric     if (other.binding == STB_WEAK) {
4670b57cec5SDimitry Andric       binding = STB_WEAK;
4680b57cec5SDimitry Andric       type = other.type;
4690b57cec5SDimitry Andric       return;
4700b57cec5SDimitry Andric     }
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric     // Do extra check for --warn-backrefs.
4730b57cec5SDimitry Andric     //
4740b57cec5SDimitry Andric     // --warn-backrefs is an option to prevent an undefined reference from
4754824e7fdSDimitry Andric     // extracting an archive member written earlier in the command line. It can
4764824e7fdSDimitry Andric     // be used to keep compatibility with GNU linkers to some degree. I'll
4774824e7fdSDimitry Andric     // explain the feature and why you may find it useful in this comment.
4780b57cec5SDimitry Andric     //
4790b57cec5SDimitry Andric     // lld's symbol resolution semantics is more relaxed than traditional Unix
4800b57cec5SDimitry Andric     // linkers. For example,
4810b57cec5SDimitry Andric     //
4820b57cec5SDimitry Andric     //   ld.lld foo.a bar.o
4830b57cec5SDimitry Andric     //
4840b57cec5SDimitry Andric     // succeeds even if bar.o contains an undefined symbol that has to be
4850b57cec5SDimitry Andric     // resolved by some object file in foo.a. Traditional Unix linkers don't
4860b57cec5SDimitry Andric     // allow this kind of backward reference, as they visit each file only once
4870b57cec5SDimitry Andric     // from left to right in the command line while resolving all undefined
4880b57cec5SDimitry Andric     // symbols at the moment of visiting.
4890b57cec5SDimitry Andric     //
4900b57cec5SDimitry Andric     // In the above case, since there's no undefined symbol when a linker visits
4910b57cec5SDimitry Andric     // foo.a, no files are pulled out from foo.a, and because the linker forgets
4920b57cec5SDimitry Andric     // about foo.a after visiting, it can't resolve undefined symbols in bar.o
4930b57cec5SDimitry Andric     // that could have been resolved otherwise.
4940b57cec5SDimitry Andric     //
4950b57cec5SDimitry Andric     // That lld accepts more relaxed form means that (besides it'd make more
4960b57cec5SDimitry Andric     // sense) you can accidentally write a command line or a build file that
4970b57cec5SDimitry Andric     // works only with lld, even if you have a plan to distribute it to wider
4980b57cec5SDimitry Andric     // users who may be using GNU linkers. With --warn-backrefs, you can detect
4990b57cec5SDimitry Andric     // a library order that doesn't work with other Unix linkers.
5000b57cec5SDimitry Andric     //
5010b57cec5SDimitry Andric     // The option is also useful to detect cyclic dependencies between static
5020b57cec5SDimitry Andric     // archives. Again, lld accepts
5030b57cec5SDimitry Andric     //
5040b57cec5SDimitry Andric     //   ld.lld foo.a bar.a
5050b57cec5SDimitry Andric     //
5060b57cec5SDimitry Andric     // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
5070b57cec5SDimitry Andric     // handled as an error.
5080b57cec5SDimitry Andric     //
5090b57cec5SDimitry Andric     // Here is how the option works. We assign a group ID to each file. A file
5100b57cec5SDimitry Andric     // with a smaller group ID can pull out object files from an archive file
5110b57cec5SDimitry Andric     // with an equal or greater group ID. Otherwise, it is a reverse dependency
5120b57cec5SDimitry Andric     // and an error.
5130b57cec5SDimitry Andric     //
5140b57cec5SDimitry Andric     // A file outside --{start,end}-group gets a fresh ID when instantiated. All
5150b57cec5SDimitry Andric     // files within the same --{start,end}-group get the same group ID. E.g.
5160b57cec5SDimitry Andric     //
5170b57cec5SDimitry Andric     //   ld.lld A B --start-group C D --end-group E
5180b57cec5SDimitry Andric     //
5190b57cec5SDimitry Andric     // A forms group 0. B form group 1. C and D (including their member object
5200b57cec5SDimitry Andric     // files) form group 2. E forms group 3. I think that you can see how this
5210b57cec5SDimitry Andric     // group assignment rule simulates the traditional linker's semantics.
5220b57cec5SDimitry Andric     bool backref = config->warnBackrefs && other.file &&
5230b57cec5SDimitry Andric                    file->groupId < other.file->groupId;
5244824e7fdSDimitry Andric     extract();
5250b57cec5SDimitry Andric 
526349cc55cSDimitry Andric     if (!config->whyExtract.empty())
527349cc55cSDimitry Andric       recordWhyExtract(other.file, *file, *this);
528349cc55cSDimitry Andric 
5290b57cec5SDimitry Andric     // We don't report backward references to weak symbols as they can be
5300b57cec5SDimitry Andric     // overridden later.
5315ffd83dbSDimitry Andric     //
5325ffd83dbSDimitry Andric     // A traditional linker does not error for -ldef1 -lref -ldef2 (linking
5335ffd83dbSDimitry Andric     // sandwich), where def2 may or may not be the same as def1. We don't want
5345ffd83dbSDimitry Andric     // to warn for this case, so dismiss the warning if we see a subsequent lazy
535e8d8bef9SDimitry Andric     // definition. this->file needs to be saved because in the case of LTO it
536e8d8bef9SDimitry Andric     // may be reset to nullptr or be replaced with a file named lto.tmp.
5370b57cec5SDimitry Andric     if (backref && !isWeak())
538e8d8bef9SDimitry Andric       backwardReferences.try_emplace(this, std::make_pair(other.file, file));
5390b57cec5SDimitry Andric     return;
5400b57cec5SDimitry Andric   }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric   // Undefined symbols in a SharedFile do not change the binding.
5430eae32dcSDimitry Andric   if (isa_and_nonnull<SharedFile>(other.file))
5440b57cec5SDimitry Andric     return;
5450b57cec5SDimitry Andric 
54685868e8aSDimitry Andric   if (isUndefined() || isShared()) {
54785868e8aSDimitry Andric     // The binding will be weak if there is at least one reference and all are
54885868e8aSDimitry Andric     // weak. The binding has one opportunity to change to weak: if the first
54985868e8aSDimitry Andric     // reference is weak.
55085868e8aSDimitry Andric     if (other.binding != STB_WEAK || !referenced)
5510b57cec5SDimitry Andric       binding = other.binding;
5520b57cec5SDimitry Andric   }
5530b57cec5SDimitry Andric }
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric // Compare two symbols. Return 1 if the new symbol should win, -1 if
5560b57cec5SDimitry Andric // the new symbol should lose, or 0 if there is a conflict.
5570b57cec5SDimitry Andric int Symbol::compare(const Symbol *other) const {
5580b57cec5SDimitry Andric   assert(other->isDefined() || other->isCommon());
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   if (!isDefined() && !isCommon())
5610b57cec5SDimitry Andric     return 1;
5620b57cec5SDimitry Andric 
5630eae32dcSDimitry Andric   // .symver foo,foo@@VER unfortunately creates two defined symbols: foo and
5640eae32dcSDimitry Andric   // foo@@VER. In GNU ld, if foo and foo@@VER are in the same file, foo is
5650eae32dcSDimitry Andric   // ignored. In our implementation, when this is foo, this->getName() may still
5660eae32dcSDimitry Andric   // contain @@, return 1 in this case as well.
5670eae32dcSDimitry Andric   if (file == other->file) {
5680eae32dcSDimitry Andric     if (other->getName().contains("@@"))
5690eae32dcSDimitry Andric       return 1;
5700eae32dcSDimitry Andric     if (getName().contains("@@"))
5710eae32dcSDimitry Andric       return -1;
5720eae32dcSDimitry Andric   }
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric   if (other->isWeak())
5750b57cec5SDimitry Andric     return -1;
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric   if (isWeak())
5780b57cec5SDimitry Andric     return 1;
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric   if (isCommon() && other->isCommon()) {
5810b57cec5SDimitry Andric     if (config->warnCommon)
5820b57cec5SDimitry Andric       warn("multiple common of " + getName());
5830b57cec5SDimitry Andric     return 0;
5840b57cec5SDimitry Andric   }
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   if (isCommon()) {
5870b57cec5SDimitry Andric     if (config->warnCommon)
5880b57cec5SDimitry Andric       warn("common " + getName() + " is overridden");
5890b57cec5SDimitry Andric     return 1;
5900b57cec5SDimitry Andric   }
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   if (other->isCommon()) {
5930b57cec5SDimitry Andric     if (config->warnCommon)
5940b57cec5SDimitry Andric       warn("common " + getName() + " is overridden");
5950b57cec5SDimitry Andric     return -1;
5960b57cec5SDimitry Andric   }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric   auto *oldSym = cast<Defined>(this);
5990b57cec5SDimitry Andric   auto *newSym = cast<Defined>(other);
6000b57cec5SDimitry Andric 
6010eae32dcSDimitry Andric   if (isa_and_nonnull<BitcodeFile>(other->file))
6020b57cec5SDimitry Andric     return 0;
6030b57cec5SDimitry Andric 
6040b57cec5SDimitry Andric   if (!oldSym->section && !newSym->section && oldSym->value == newSym->value &&
6050b57cec5SDimitry Andric       newSym->binding == STB_GLOBAL)
6060b57cec5SDimitry Andric     return -1;
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   return 0;
6090b57cec5SDimitry Andric }
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric static void reportDuplicate(Symbol *sym, InputFile *newFile,
6120b57cec5SDimitry Andric                             InputSectionBase *errSec, uint64_t errOffset) {
6130b57cec5SDimitry Andric   if (config->allowMultipleDefinition)
6140b57cec5SDimitry Andric     return;
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric   Defined *d = cast<Defined>(sym);
6170b57cec5SDimitry Andric   if (!d->section || !errSec) {
6180b57cec5SDimitry Andric     error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " +
6190b57cec5SDimitry Andric           toString(sym->file) + "\n>>> defined in " + toString(newFile));
6200b57cec5SDimitry Andric     return;
6210b57cec5SDimitry Andric   }
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric   // Construct and print an error message in the form of:
6240b57cec5SDimitry Andric   //
6250b57cec5SDimitry Andric   //   ld.lld: error: duplicate symbol: foo
6260b57cec5SDimitry Andric   //   >>> defined at bar.c:30
6270b57cec5SDimitry Andric   //   >>>            bar.o (/home/alice/src/bar.o)
6280b57cec5SDimitry Andric   //   >>> defined at baz.c:563
6290b57cec5SDimitry Andric   //   >>>            baz.o in archive libbaz.a
6300b57cec5SDimitry Andric   auto *sec1 = cast<InputSectionBase>(d->section);
6310b57cec5SDimitry Andric   std::string src1 = sec1->getSrcMsg(*sym, d->value);
6320b57cec5SDimitry Andric   std::string obj1 = sec1->getObjMsg(d->value);
6330b57cec5SDimitry Andric   std::string src2 = errSec->getSrcMsg(*sym, errOffset);
6340b57cec5SDimitry Andric   std::string obj2 = errSec->getObjMsg(errOffset);
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric   std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at ";
6370b57cec5SDimitry Andric   if (!src1.empty())
6380b57cec5SDimitry Andric     msg += src1 + "\n>>>            ";
6390b57cec5SDimitry Andric   msg += obj1 + "\n>>> defined at ";
6400b57cec5SDimitry Andric   if (!src2.empty())
6410b57cec5SDimitry Andric     msg += src2 + "\n>>>            ";
6420b57cec5SDimitry Andric   msg += obj2;
6430b57cec5SDimitry Andric   error(msg);
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric void Symbol::resolveCommon(const CommonSymbol &other) {
6470b57cec5SDimitry Andric   int cmp = compare(&other);
6480b57cec5SDimitry Andric   if (cmp < 0)
6490b57cec5SDimitry Andric     return;
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric   if (cmp > 0) {
652480093f4SDimitry Andric     if (auto *s = dyn_cast<SharedSymbol>(this)) {
653480093f4SDimitry Andric       // Increase st_size if the shared symbol has a larger st_size. The shared
654480093f4SDimitry Andric       // symbol may be created from common symbols. The fact that some object
655480093f4SDimitry Andric       // files were linked into a shared object first should not change the
656480093f4SDimitry Andric       // regular rule that picks the largest st_size.
657480093f4SDimitry Andric       uint64_t size = s->size;
6580b57cec5SDimitry Andric       replace(other);
659480093f4SDimitry Andric       if (size > cast<CommonSymbol>(this)->size)
660480093f4SDimitry Andric         cast<CommonSymbol>(this)->size = size;
661480093f4SDimitry Andric     } else {
662480093f4SDimitry Andric       replace(other);
663480093f4SDimitry Andric     }
6640b57cec5SDimitry Andric     return;
6650b57cec5SDimitry Andric   }
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric   CommonSymbol *oldSym = cast<CommonSymbol>(this);
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric   oldSym->alignment = std::max(oldSym->alignment, other.alignment);
6700b57cec5SDimitry Andric   if (oldSym->size < other.size) {
6710b57cec5SDimitry Andric     oldSym->file = other.file;
6720b57cec5SDimitry Andric     oldSym->size = other.size;
6730b57cec5SDimitry Andric   }
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric void Symbol::resolveDefined(const Defined &other) {
6770b57cec5SDimitry Andric   int cmp = compare(&other);
6780b57cec5SDimitry Andric   if (cmp > 0)
6790b57cec5SDimitry Andric     replace(other);
6800b57cec5SDimitry Andric   else if (cmp == 0)
6810b57cec5SDimitry Andric     reportDuplicate(this, other.file,
6820b57cec5SDimitry Andric                     dyn_cast_or_null<InputSectionBase>(other.section),
6830b57cec5SDimitry Andric                     other.value);
6840b57cec5SDimitry Andric }
6850b57cec5SDimitry Andric 
686e8d8bef9SDimitry Andric template <class LazyT>
687e8d8bef9SDimitry Andric static void replaceCommon(Symbol &oldSym, const LazyT &newSym) {
688e8d8bef9SDimitry Andric   backwardReferences.erase(&oldSym);
689e8d8bef9SDimitry Andric   oldSym.replace(newSym);
6904824e7fdSDimitry Andric   newSym.extract();
691e8d8bef9SDimitry Andric }
692e8d8bef9SDimitry Andric 
6930b57cec5SDimitry Andric template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
694e8d8bef9SDimitry Andric   // For common objects, we want to look for global or weak definitions that
6954824e7fdSDimitry Andric   // should be extracted as the canonical definition instead.
696e8d8bef9SDimitry Andric   if (isCommon() && elf::config->fortranCommon) {
697e8d8bef9SDimitry Andric     if (auto *laSym = dyn_cast<LazyArchive>(&other)) {
698e8d8bef9SDimitry Andric       ArchiveFile *archive = cast<ArchiveFile>(laSym->file);
699e8d8bef9SDimitry Andric       const Archive::Symbol &archiveSym = laSym->sym;
7004824e7fdSDimitry Andric       if (archive->shouldExtractForCommon(archiveSym)) {
701e8d8bef9SDimitry Andric         replaceCommon(*this, other);
702e8d8bef9SDimitry Andric         return;
703e8d8bef9SDimitry Andric       }
704e8d8bef9SDimitry Andric     } else if (auto *loSym = dyn_cast<LazyObject>(&other)) {
7050eae32dcSDimitry Andric       if (loSym->file->shouldExtractForCommon(loSym->getName())) {
706e8d8bef9SDimitry Andric         replaceCommon(*this, other);
707e8d8bef9SDimitry Andric         return;
708e8d8bef9SDimitry Andric       }
709e8d8bef9SDimitry Andric     }
710e8d8bef9SDimitry Andric   }
711e8d8bef9SDimitry Andric 
7125ffd83dbSDimitry Andric   if (!isUndefined()) {
7135ffd83dbSDimitry Andric     // See the comment in resolveUndefined().
7145ffd83dbSDimitry Andric     if (isDefined())
7155ffd83dbSDimitry Andric       backwardReferences.erase(this);
7160b57cec5SDimitry Andric     return;
7175ffd83dbSDimitry Andric   }
7180b57cec5SDimitry Andric 
7194824e7fdSDimitry Andric   // An undefined weak will not extract archive members. See comment on Lazy in
7200b57cec5SDimitry Andric   // Symbols.h for the details.
7210b57cec5SDimitry Andric   if (isWeak()) {
7220b57cec5SDimitry Andric     uint8_t ty = type;
7230b57cec5SDimitry Andric     replace(other);
7240b57cec5SDimitry Andric     type = ty;
7250b57cec5SDimitry Andric     binding = STB_WEAK;
7260b57cec5SDimitry Andric     return;
7270b57cec5SDimitry Andric   }
7280b57cec5SDimitry Andric 
729349cc55cSDimitry Andric   const InputFile *oldFile = file;
7304824e7fdSDimitry Andric   other.extract();
731349cc55cSDimitry Andric   if (!config->whyExtract.empty())
732349cc55cSDimitry Andric     recordWhyExtract(oldFile, *file, *this);
7330b57cec5SDimitry Andric }
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric void Symbol::resolveShared(const SharedSymbol &other) {
736480093f4SDimitry Andric   if (isCommon()) {
737480093f4SDimitry Andric     // See the comment in resolveCommon() above.
738480093f4SDimitry Andric     if (other.size > cast<CommonSymbol>(this)->size)
739480093f4SDimitry Andric       cast<CommonSymbol>(this)->size = other.size;
740480093f4SDimitry Andric     return;
741480093f4SDimitry Andric   }
7420b57cec5SDimitry Andric   if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) {
7430b57cec5SDimitry Andric     // An undefined symbol with non default visibility must be satisfied
7440b57cec5SDimitry Andric     // in the same DSO.
7450b57cec5SDimitry Andric     uint8_t bind = binding;
7460b57cec5SDimitry Andric     replace(other);
7470b57cec5SDimitry Andric     binding = bind;
7485ffd83dbSDimitry Andric   } else if (traced)
7495ffd83dbSDimitry Andric     printTraceSymbol(&other);
7500b57cec5SDimitry Andric }
751