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" 1081ad6265SDimitry Andric #include "Driver.h" 110b57cec5SDimitry Andric #include "InputFiles.h" 120b57cec5SDimitry Andric #include "InputSection.h" 130b57cec5SDimitry Andric #include "OutputSections.h" 140b57cec5SDimitry Andric #include "SyntheticSections.h" 150b57cec5SDimitry Andric #include "Target.h" 160b57cec5SDimitry Andric #include "Writer.h" 170b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 18*bdd1243dSDimitry Andric #include "llvm/Demangle/Demangle.h" 1981ad6265SDimitry Andric #include "llvm/Support/Compiler.h" 200b57cec5SDimitry Andric #include <cstring> 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric using namespace llvm; 230b57cec5SDimitry Andric using namespace llvm::object; 240b57cec5SDimitry Andric using namespace llvm::ELF; 255ffd83dbSDimitry Andric using namespace lld; 265ffd83dbSDimitry Andric using namespace lld::elf; 270b57cec5SDimitry Andric 2881ad6265SDimitry Andric static_assert(sizeof(SymbolUnion) <= 64, "SymbolUnion too large"); 2981ad6265SDimitry Andric 3081ad6265SDimitry Andric template <typename T> struct AssertSymbol { 3181ad6265SDimitry Andric static_assert(std::is_trivially_destructible<T>(), 3281ad6265SDimitry Andric "Symbol types must be trivially destructible"); 3381ad6265SDimitry Andric static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small"); 3481ad6265SDimitry Andric static_assert(alignof(T) <= alignof(SymbolUnion), 3581ad6265SDimitry Andric "SymbolUnion not aligned enough"); 3681ad6265SDimitry Andric }; 3781ad6265SDimitry Andric 3881ad6265SDimitry Andric LLVM_ATTRIBUTE_UNUSED static inline void assertSymbols() { 3981ad6265SDimitry Andric AssertSymbol<Defined>(); 4081ad6265SDimitry Andric AssertSymbol<CommonSymbol>(); 4181ad6265SDimitry Andric AssertSymbol<Undefined>(); 4281ad6265SDimitry Andric AssertSymbol<SharedSymbol>(); 4381ad6265SDimitry Andric AssertSymbol<LazyObject>(); 4481ad6265SDimitry Andric } 4581ad6265SDimitry Andric 46*bdd1243dSDimitry Andric // Returns a symbol for an error message. 47*bdd1243dSDimitry Andric static std::string maybeDemangleSymbol(StringRef symName) { 48*bdd1243dSDimitry Andric if (elf::config->demangle) 49*bdd1243dSDimitry Andric return demangle(symName.str()); 50*bdd1243dSDimitry Andric return symName.str(); 51*bdd1243dSDimitry Andric } 52*bdd1243dSDimitry Andric 535ffd83dbSDimitry Andric std::string lld::toString(const elf::Symbol &sym) { 545ffd83dbSDimitry Andric StringRef name = sym.getName(); 55*bdd1243dSDimitry Andric std::string ret = maybeDemangleSymbol(name); 565ffd83dbSDimitry Andric 57e8d8bef9SDimitry Andric const char *suffix = sym.getVersionSuffix(); 58e8d8bef9SDimitry Andric if (*suffix == '@') 59e8d8bef9SDimitry Andric ret += suffix; 605ffd83dbSDimitry Andric return ret; 615ffd83dbSDimitry Andric } 625ffd83dbSDimitry Andric 630b57cec5SDimitry Andric Defined *ElfSym::bss; 640b57cec5SDimitry Andric Defined *ElfSym::etext1; 650b57cec5SDimitry Andric Defined *ElfSym::etext2; 660b57cec5SDimitry Andric Defined *ElfSym::edata1; 670b57cec5SDimitry Andric Defined *ElfSym::edata2; 680b57cec5SDimitry Andric Defined *ElfSym::end1; 690b57cec5SDimitry Andric Defined *ElfSym::end2; 700b57cec5SDimitry Andric Defined *ElfSym::globalOffsetTable; 710b57cec5SDimitry Andric Defined *ElfSym::mipsGp; 720b57cec5SDimitry Andric Defined *ElfSym::mipsGpDisp; 730b57cec5SDimitry Andric Defined *ElfSym::mipsLocalGp; 740b57cec5SDimitry Andric Defined *ElfSym::relaIpltStart; 750b57cec5SDimitry Andric Defined *ElfSym::relaIpltEnd; 760b57cec5SDimitry Andric Defined *ElfSym::tlsModuleBase; 7704eeddc0SDimitry Andric SmallVector<SymbolAux, 0> elf::symAux; 780b57cec5SDimitry Andric 790eae32dcSDimitry Andric static uint64_t getSymVA(const Symbol &sym, int64_t addend) { 800b57cec5SDimitry Andric switch (sym.kind()) { 810b57cec5SDimitry Andric case Symbol::DefinedKind: { 820b57cec5SDimitry Andric auto &d = cast<Defined>(sym); 830b57cec5SDimitry Andric SectionBase *isec = d.section; 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric // This is an absolute symbol. 860b57cec5SDimitry Andric if (!isec) 870b57cec5SDimitry Andric return d.value; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric assert(isec != &InputSection::discarded); 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric uint64_t offset = d.value; 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric // An object in an SHF_MERGE section might be referenced via a 940b57cec5SDimitry Andric // section symbol (as a hack for reducing the number of local 950b57cec5SDimitry Andric // symbols). 960b57cec5SDimitry Andric // Depending on the addend, the reference via a section symbol 970b57cec5SDimitry Andric // refers to a different object in the merge section. 980b57cec5SDimitry Andric // Since the objects in the merge section are not necessarily 990b57cec5SDimitry Andric // contiguous in the output, the addend can thus affect the final 1000b57cec5SDimitry Andric // VA in a non-linear way. 1010b57cec5SDimitry Andric // To make this work, we incorporate the addend into the section 1020b57cec5SDimitry Andric // offset (and zero out the addend for later processing) so that 1030b57cec5SDimitry Andric // we find the right object in the section. 1040eae32dcSDimitry Andric if (d.isSection()) 1050b57cec5SDimitry Andric offset += addend; 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // In the typical case, this is actually very simple and boils 1080b57cec5SDimitry Andric // down to adding together 3 numbers: 1090b57cec5SDimitry Andric // 1. The address of the output section. 1100b57cec5SDimitry Andric // 2. The offset of the input section within the output section. 1110b57cec5SDimitry Andric // 3. The offset within the input section (this addition happens 1120b57cec5SDimitry Andric // inside InputSection::getOffset). 1130b57cec5SDimitry Andric // 1140b57cec5SDimitry Andric // If you understand the data structures involved with this next 1150b57cec5SDimitry Andric // line (and how they get built), then you have a pretty good 1160b57cec5SDimitry Andric // understanding of the linker. 1170b57cec5SDimitry Andric uint64_t va = isec->getVA(offset); 1180eae32dcSDimitry Andric if (d.isSection()) 1190eae32dcSDimitry Andric va -= addend; 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric // MIPS relocatable files can mix regular and microMIPS code. 1220b57cec5SDimitry Andric // Linker needs to distinguish such code. To do so microMIPS 1230b57cec5SDimitry Andric // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other` 1245ffd83dbSDimitry Andric // field. Unfortunately, the `MIPS::relocate()` method has 1250b57cec5SDimitry Andric // a symbol value only. To pass type of the symbol (regular/microMIPS) 1260b57cec5SDimitry Andric // to that routine as well as other places where we write 1270b57cec5SDimitry Andric // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry` 1280b57cec5SDimitry Andric // field etc) do the same trick as compiler uses to mark microMIPS 1290b57cec5SDimitry Andric // for CPU - set the less-significant bit. 1300b57cec5SDimitry Andric if (config->emachine == EM_MIPS && isMicroMips() && 131*bdd1243dSDimitry Andric ((sym.stOther & STO_MIPS_MICROMIPS) || sym.hasFlag(NEEDS_COPY))) 1320b57cec5SDimitry Andric va |= 1; 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric if (d.isTls() && !config->relocatable) { 1350b57cec5SDimitry Andric // Use the address of the TLS segment's first section rather than the 1360b57cec5SDimitry Andric // segment's address, because segment addresses aren't initialized until 1370b57cec5SDimitry Andric // after sections are finalized. (e.g. Measuring the size of .rela.dyn 1380b57cec5SDimitry Andric // for Android relocation packing requires knowing TLS symbol addresses 1390b57cec5SDimitry Andric // during section finalization.) 1400b57cec5SDimitry Andric if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec) 1410b57cec5SDimitry Andric fatal(toString(d.file) + 1420b57cec5SDimitry Andric " has an STT_TLS symbol but doesn't have an SHF_TLS section"); 1430b57cec5SDimitry Andric return va - Out::tlsPhdr->firstSec->addr; 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric return va; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric case Symbol::SharedKind: 1480b57cec5SDimitry Andric case Symbol::UndefinedKind: 1490b57cec5SDimitry Andric return 0; 1500b57cec5SDimitry Andric case Symbol::LazyObjectKind: 15104eeddc0SDimitry Andric llvm_unreachable("lazy symbol reached writer"); 1520b57cec5SDimitry Andric case Symbol::CommonKind: 1530b57cec5SDimitry Andric llvm_unreachable("common symbol reached writer"); 1540b57cec5SDimitry Andric case Symbol::PlaceholderKind: 1550b57cec5SDimitry Andric llvm_unreachable("placeholder symbol reached writer"); 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric llvm_unreachable("invalid symbol kind"); 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric uint64_t Symbol::getVA(int64_t addend) const { 1610eae32dcSDimitry Andric return getSymVA(*this, addend) + addend; 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric uint64_t Symbol::getGotVA() const { 1650b57cec5SDimitry Andric if (gotInIgot) 1660b57cec5SDimitry Andric return in.igotPlt->getVA() + getGotPltOffset(); 1670b57cec5SDimitry Andric return in.got->getVA() + getGotOffset(); 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 170fe6060f1SDimitry Andric uint64_t Symbol::getGotOffset() const { 17104eeddc0SDimitry Andric return getGotIdx() * target->gotEntrySize; 172fe6060f1SDimitry Andric } 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric uint64_t Symbol::getGotPltVA() const { 1750b57cec5SDimitry Andric if (isInIplt) 1760b57cec5SDimitry Andric return in.igotPlt->getVA() + getGotPltOffset(); 1770b57cec5SDimitry Andric return in.gotPlt->getVA() + getGotPltOffset(); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric uint64_t Symbol::getGotPltOffset() const { 1810b57cec5SDimitry Andric if (isInIplt) 18204eeddc0SDimitry Andric return getPltIdx() * target->gotEntrySize; 18304eeddc0SDimitry Andric return (getPltIdx() + target->gotPltHeaderEntriesNum) * target->gotEntrySize; 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric uint64_t Symbol::getPltVA() const { 187480093f4SDimitry Andric uint64_t outVA = isInIplt 18804eeddc0SDimitry Andric ? in.iplt->getVA() + getPltIdx() * target->ipltEntrySize 189480093f4SDimitry Andric : in.plt->getVA() + in.plt->headerSize + 19004eeddc0SDimitry Andric getPltIdx() * target->pltEntrySize; 191480093f4SDimitry Andric 1920b57cec5SDimitry Andric // While linking microMIPS code PLT code are always microMIPS 1930b57cec5SDimitry Andric // code. Set the less-significant bit to track that fact. 1940b57cec5SDimitry Andric // See detailed comment in the `getSymVA` function. 1950b57cec5SDimitry Andric if (config->emachine == EM_MIPS && isMicroMips()) 1960b57cec5SDimitry Andric outVA |= 1; 1970b57cec5SDimitry Andric return outVA; 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric uint64_t Symbol::getSize() const { 2010b57cec5SDimitry Andric if (const auto *dr = dyn_cast<Defined>(this)) 2020b57cec5SDimitry Andric return dr->size; 2030b57cec5SDimitry Andric return cast<SharedSymbol>(this)->size; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric OutputSection *Symbol::getOutputSection() const { 2070b57cec5SDimitry Andric if (auto *s = dyn_cast<Defined>(this)) { 2080b57cec5SDimitry Andric if (auto *sec = s->section) 2090eae32dcSDimitry Andric return sec->getOutputSection(); 2100b57cec5SDimitry Andric return nullptr; 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric return nullptr; 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric // If a symbol name contains '@', the characters after that is 2160b57cec5SDimitry Andric // a symbol version name. This function parses that. 2170b57cec5SDimitry Andric void Symbol::parseSymbolVersion() { 2186e75b2fbSDimitry Andric // Return if localized by a local: pattern in a version script. 2196e75b2fbSDimitry Andric if (versionId == VER_NDX_LOCAL) 2206e75b2fbSDimitry Andric return; 2210b57cec5SDimitry Andric StringRef s = getName(); 2220b57cec5SDimitry Andric size_t pos = s.find('@'); 2230eae32dcSDimitry Andric if (pos == StringRef::npos) 2240b57cec5SDimitry Andric return; 2250b57cec5SDimitry Andric StringRef verstr = s.substr(pos + 1); 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric // Truncate the symbol name so that it doesn't include the version string. 2280b57cec5SDimitry Andric nameSize = pos; 2290b57cec5SDimitry Andric 23004eeddc0SDimitry Andric if (verstr.empty()) 23104eeddc0SDimitry Andric return; 23204eeddc0SDimitry Andric 2330b57cec5SDimitry Andric // If this is not in this DSO, it is not a definition. 2340b57cec5SDimitry Andric if (!isDefined()) 2350b57cec5SDimitry Andric return; 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // '@@' in a symbol name means the default version. 2380b57cec5SDimitry Andric // It is usually the most recent one. 2390b57cec5SDimitry Andric bool isDefault = (verstr[0] == '@'); 2400b57cec5SDimitry Andric if (isDefault) 2410b57cec5SDimitry Andric verstr = verstr.substr(1); 2420b57cec5SDimitry Andric 24385868e8aSDimitry Andric for (const VersionDefinition &ver : namedVersionDefs()) { 2440b57cec5SDimitry Andric if (ver.name != verstr) 2450b57cec5SDimitry Andric continue; 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric if (isDefault) 2480b57cec5SDimitry Andric versionId = ver.id; 2490b57cec5SDimitry Andric else 2500b57cec5SDimitry Andric versionId = ver.id | VERSYM_HIDDEN; 2510b57cec5SDimitry Andric return; 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric // It is an error if the specified version is not defined. 2550b57cec5SDimitry Andric // Usually version script is not provided when linking executable, 2560b57cec5SDimitry Andric // but we may still want to override a versioned symbol from DSO, 2570b57cec5SDimitry Andric // so we do not report error in this case. We also do not error 2580b57cec5SDimitry Andric // if the symbol has a local version as it won't be in the dynamic 2590b57cec5SDimitry Andric // symbol table. 2600b57cec5SDimitry Andric if (config->shared && versionId != VER_NDX_LOCAL) 2610b57cec5SDimitry Andric error(toString(file) + ": symbol " + s + " has undefined version " + 2620b57cec5SDimitry Andric verstr); 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric 2654824e7fdSDimitry Andric void Symbol::extract() const { 26681ad6265SDimitry Andric if (file->lazy) { 2670eae32dcSDimitry Andric file->lazy = false; 2680eae32dcSDimitry Andric parseFile(file); 2690eae32dcSDimitry Andric } 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric uint8_t Symbol::computeBinding() const { 273*bdd1243dSDimitry Andric auto v = visibility(); 274*bdd1243dSDimitry Andric if ((v != STV_DEFAULT && v != STV_PROTECTED) || versionId == VER_NDX_LOCAL) 2750b57cec5SDimitry Andric return STB_LOCAL; 27604eeddc0SDimitry Andric if (binding == STB_GNU_UNIQUE && !config->gnuUnique) 2770b57cec5SDimitry Andric return STB_GLOBAL; 2780b57cec5SDimitry Andric return binding; 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric bool Symbol::includeInDynsym() const { 2820b57cec5SDimitry Andric if (computeBinding() == STB_LOCAL) 2830b57cec5SDimitry Andric return false; 284480093f4SDimitry Andric if (!isDefined() && !isCommon()) 28555e4f9d5SDimitry Andric // This should unconditionally return true, unfortunately glibc -static-pie 28655e4f9d5SDimitry Andric // expects undefined weak symbols not to exist in .dynsym, e.g. 28755e4f9d5SDimitry Andric // __pthread_mutex_lock reference in _dl_add_to_namespace_list, 28855e4f9d5SDimitry Andric // __pthread_initialize_minimal reference in csu/libc-start.c. 28904eeddc0SDimitry Andric return !(isUndefWeak() && config->noDynamicLinker); 2900b57cec5SDimitry Andric 291480093f4SDimitry Andric return exportDynamic || inDynamicList; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric // Print out a log message for --trace-symbol. 29581ad6265SDimitry Andric void elf::printTraceSymbol(const Symbol &sym, StringRef name) { 2960b57cec5SDimitry Andric std::string s; 29781ad6265SDimitry Andric if (sym.isUndefined()) 2980b57cec5SDimitry Andric s = ": reference to "; 29981ad6265SDimitry Andric else if (sym.isLazy()) 3000b57cec5SDimitry Andric s = ": lazy definition of "; 30181ad6265SDimitry Andric else if (sym.isShared()) 3020b57cec5SDimitry Andric s = ": shared definition of "; 30381ad6265SDimitry Andric else if (sym.isCommon()) 3040b57cec5SDimitry Andric s = ": common definition of "; 3050b57cec5SDimitry Andric else 3060b57cec5SDimitry Andric s = ": definition of "; 3070b57cec5SDimitry Andric 30881ad6265SDimitry Andric message(toString(sym.file) + s + name); 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 311349cc55cSDimitry Andric static void recordWhyExtract(const InputFile *reference, 312349cc55cSDimitry Andric const InputFile &extracted, const Symbol &sym) { 313*bdd1243dSDimitry Andric ctx.whyExtractRecords.emplace_back(toString(reference), &extracted, sym); 314349cc55cSDimitry Andric } 315349cc55cSDimitry Andric 3165ffd83dbSDimitry Andric void elf::maybeWarnUnorderableSymbol(const Symbol *sym) { 3170b57cec5SDimitry Andric if (!config->warnSymbolOrdering) 3180b57cec5SDimitry Andric return; 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning 3210b57cec5SDimitry Andric // is emitted. It makes sense to not warn on undefined symbols. 3220b57cec5SDimitry Andric // 3230b57cec5SDimitry Andric // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols, 3240b57cec5SDimitry Andric // but we don't have to be compatible here. 3250b57cec5SDimitry Andric if (sym->isUndefined() && 3260b57cec5SDimitry Andric config->unresolvedSymbols == UnresolvedPolicy::Ignore) 3270b57cec5SDimitry Andric return; 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric const InputFile *file = sym->file; 3300b57cec5SDimitry Andric auto *d = dyn_cast<Defined>(sym); 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); }; 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric if (sym->isUndefined()) 3350b57cec5SDimitry Andric report(": unable to order undefined symbol: "); 3360b57cec5SDimitry Andric else if (sym->isShared()) 3370b57cec5SDimitry Andric report(": unable to order shared symbol: "); 3380b57cec5SDimitry Andric else if (d && !d->section) 3390b57cec5SDimitry Andric report(": unable to order absolute symbol: "); 3400b57cec5SDimitry Andric else if (d && isa<OutputSection>(d->section)) 3410b57cec5SDimitry Andric report(": unable to order synthetic symbol: "); 3420eae32dcSDimitry Andric else if (d && !d->section->isLive()) 3430b57cec5SDimitry Andric report(": unable to order discarded symbol: "); 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric 346480093f4SDimitry Andric // Returns true if a symbol can be replaced at load-time by a symbol 347480093f4SDimitry Andric // with the same name defined in other ELF executable or DSO. 3485ffd83dbSDimitry Andric bool elf::computeIsPreemptible(const Symbol &sym) { 34904eeddc0SDimitry Andric assert(!sym.isLocal() || sym.isPlaceholder()); 350480093f4SDimitry Andric 351480093f4SDimitry Andric // Only symbols with default visibility that appear in dynsym can be 352480093f4SDimitry Andric // preempted. Symbols with protected visibility cannot be preempted. 353*bdd1243dSDimitry Andric if (!sym.includeInDynsym() || sym.visibility() != STV_DEFAULT) 354480093f4SDimitry Andric return false; 355480093f4SDimitry Andric 356480093f4SDimitry Andric // At this point copy relocations have not been created yet, so any 357480093f4SDimitry Andric // symbol that is not defined locally is preemptible. 358480093f4SDimitry Andric if (!sym.isDefined()) 359480093f4SDimitry Andric return true; 360480093f4SDimitry Andric 361480093f4SDimitry Andric if (!config->shared) 362480093f4SDimitry Andric return false; 363480093f4SDimitry Andric 3645ffd83dbSDimitry Andric // If -Bsymbolic or --dynamic-list is specified, or -Bsymbolic-functions is 3655ffd83dbSDimitry Andric // specified and the symbol is STT_FUNC, the symbol is preemptible iff it is 3666e75b2fbSDimitry Andric // in the dynamic list. -Bsymbolic-non-weak-functions is a non-weak subset of 3676e75b2fbSDimitry Andric // -Bsymbolic-functions. 3686e75b2fbSDimitry Andric if (config->symbolic || 3696e75b2fbSDimitry Andric (config->bsymbolic == BsymbolicKind::Functions && sym.isFunc()) || 3706e75b2fbSDimitry Andric (config->bsymbolic == BsymbolicKind::NonWeakFunctions && sym.isFunc() && 3716e75b2fbSDimitry Andric sym.binding != STB_WEAK)) 372480093f4SDimitry Andric return sym.inDynamicList; 373480093f4SDimitry Andric return true; 374480093f4SDimitry Andric } 375480093f4SDimitry Andric 3760b57cec5SDimitry Andric // Merge symbol properties. 3770b57cec5SDimitry Andric // 3780b57cec5SDimitry Andric // When we have many symbols of the same name, we choose one of them, 3790b57cec5SDimitry Andric // and that's the result of symbol resolution. However, symbols that 3800b57cec5SDimitry Andric // were not chosen still affect some symbol properties. 3810b57cec5SDimitry Andric void Symbol::mergeProperties(const Symbol &other) { 3820b57cec5SDimitry Andric if (other.exportDynamic) 3830b57cec5SDimitry Andric exportDynamic = true; 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric // DSO symbols do not affect visibility in the output. 386*bdd1243dSDimitry Andric if (!other.isShared() && other.visibility() != STV_DEFAULT) { 387*bdd1243dSDimitry Andric uint8_t v = visibility(), ov = other.visibility(); 388*bdd1243dSDimitry Andric setVisibility(v == STV_DEFAULT ? ov : std::min(v, ov)); 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric 392*bdd1243dSDimitry Andric void Symbol::resolve(const Undefined &other) { 393*bdd1243dSDimitry Andric if (other.visibility() != STV_DEFAULT) { 394*bdd1243dSDimitry Andric uint8_t v = visibility(), ov = other.visibility(); 395*bdd1243dSDimitry Andric setVisibility(v == STV_DEFAULT ? ov : std::min(v, ov)); 396*bdd1243dSDimitry Andric } 3970b57cec5SDimitry Andric // An undefined symbol with non default visibility must be satisfied 3980b57cec5SDimitry Andric // in the same DSO. 3990b57cec5SDimitry Andric // 4000b57cec5SDimitry Andric // If this is a non-weak defined symbol in a discarded section, override the 4010b57cec5SDimitry Andric // existing undefined symbol for better error message later. 402*bdd1243dSDimitry Andric if (isPlaceholder() || (isShared() && other.visibility() != STV_DEFAULT) || 4030b57cec5SDimitry Andric (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) { 404*bdd1243dSDimitry Andric other.overwrite(*this); 4050b57cec5SDimitry Andric return; 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric if (traced) 40981ad6265SDimitry Andric printTraceSymbol(other, getName()); 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric if (isLazy()) { 4124824e7fdSDimitry Andric // An undefined weak will not extract archive members. See comment on Lazy 4134824e7fdSDimitry Andric // in Symbols.h for the details. 4140b57cec5SDimitry Andric if (other.binding == STB_WEAK) { 4150b57cec5SDimitry Andric binding = STB_WEAK; 4160b57cec5SDimitry Andric type = other.type; 4170b57cec5SDimitry Andric return; 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric // Do extra check for --warn-backrefs. 4210b57cec5SDimitry Andric // 4220b57cec5SDimitry Andric // --warn-backrefs is an option to prevent an undefined reference from 4234824e7fdSDimitry Andric // extracting an archive member written earlier in the command line. It can 4244824e7fdSDimitry Andric // be used to keep compatibility with GNU linkers to some degree. I'll 4254824e7fdSDimitry Andric // explain the feature and why you may find it useful in this comment. 4260b57cec5SDimitry Andric // 4270b57cec5SDimitry Andric // lld's symbol resolution semantics is more relaxed than traditional Unix 4280b57cec5SDimitry Andric // linkers. For example, 4290b57cec5SDimitry Andric // 4300b57cec5SDimitry Andric // ld.lld foo.a bar.o 4310b57cec5SDimitry Andric // 4320b57cec5SDimitry Andric // succeeds even if bar.o contains an undefined symbol that has to be 4330b57cec5SDimitry Andric // resolved by some object file in foo.a. Traditional Unix linkers don't 4340b57cec5SDimitry Andric // allow this kind of backward reference, as they visit each file only once 4350b57cec5SDimitry Andric // from left to right in the command line while resolving all undefined 4360b57cec5SDimitry Andric // symbols at the moment of visiting. 4370b57cec5SDimitry Andric // 4380b57cec5SDimitry Andric // In the above case, since there's no undefined symbol when a linker visits 4390b57cec5SDimitry Andric // foo.a, no files are pulled out from foo.a, and because the linker forgets 4400b57cec5SDimitry Andric // about foo.a after visiting, it can't resolve undefined symbols in bar.o 4410b57cec5SDimitry Andric // that could have been resolved otherwise. 4420b57cec5SDimitry Andric // 4430b57cec5SDimitry Andric // That lld accepts more relaxed form means that (besides it'd make more 4440b57cec5SDimitry Andric // sense) you can accidentally write a command line or a build file that 4450b57cec5SDimitry Andric // works only with lld, even if you have a plan to distribute it to wider 4460b57cec5SDimitry Andric // users who may be using GNU linkers. With --warn-backrefs, you can detect 4470b57cec5SDimitry Andric // a library order that doesn't work with other Unix linkers. 4480b57cec5SDimitry Andric // 4490b57cec5SDimitry Andric // The option is also useful to detect cyclic dependencies between static 4500b57cec5SDimitry Andric // archives. Again, lld accepts 4510b57cec5SDimitry Andric // 4520b57cec5SDimitry Andric // ld.lld foo.a bar.a 4530b57cec5SDimitry Andric // 4540b57cec5SDimitry Andric // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is 4550b57cec5SDimitry Andric // handled as an error. 4560b57cec5SDimitry Andric // 4570b57cec5SDimitry Andric // Here is how the option works. We assign a group ID to each file. A file 4580b57cec5SDimitry Andric // with a smaller group ID can pull out object files from an archive file 4590b57cec5SDimitry Andric // with an equal or greater group ID. Otherwise, it is a reverse dependency 4600b57cec5SDimitry Andric // and an error. 4610b57cec5SDimitry Andric // 4620b57cec5SDimitry Andric // A file outside --{start,end}-group gets a fresh ID when instantiated. All 4630b57cec5SDimitry Andric // files within the same --{start,end}-group get the same group ID. E.g. 4640b57cec5SDimitry Andric // 4650b57cec5SDimitry Andric // ld.lld A B --start-group C D --end-group E 4660b57cec5SDimitry Andric // 4670b57cec5SDimitry Andric // A forms group 0. B form group 1. C and D (including their member object 4680b57cec5SDimitry Andric // files) form group 2. E forms group 3. I think that you can see how this 4690b57cec5SDimitry Andric // group assignment rule simulates the traditional linker's semantics. 4700b57cec5SDimitry Andric bool backref = config->warnBackrefs && other.file && 4710b57cec5SDimitry Andric file->groupId < other.file->groupId; 4724824e7fdSDimitry Andric extract(); 4730b57cec5SDimitry Andric 474349cc55cSDimitry Andric if (!config->whyExtract.empty()) 475349cc55cSDimitry Andric recordWhyExtract(other.file, *file, *this); 476349cc55cSDimitry Andric 4770b57cec5SDimitry Andric // We don't report backward references to weak symbols as they can be 4780b57cec5SDimitry Andric // overridden later. 4795ffd83dbSDimitry Andric // 4805ffd83dbSDimitry Andric // A traditional linker does not error for -ldef1 -lref -ldef2 (linking 4815ffd83dbSDimitry Andric // sandwich), where def2 may or may not be the same as def1. We don't want 4825ffd83dbSDimitry Andric // to warn for this case, so dismiss the warning if we see a subsequent lazy 483e8d8bef9SDimitry Andric // definition. this->file needs to be saved because in the case of LTO it 484e8d8bef9SDimitry Andric // may be reset to nullptr or be replaced with a file named lto.tmp. 4850b57cec5SDimitry Andric if (backref && !isWeak()) 486*bdd1243dSDimitry Andric ctx.backwardReferences.try_emplace(this, 48781ad6265SDimitry Andric std::make_pair(other.file, file)); 4880b57cec5SDimitry Andric return; 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric // Undefined symbols in a SharedFile do not change the binding. 4920eae32dcSDimitry Andric if (isa_and_nonnull<SharedFile>(other.file)) 4930b57cec5SDimitry Andric return; 4940b57cec5SDimitry Andric 49585868e8aSDimitry Andric if (isUndefined() || isShared()) { 49685868e8aSDimitry Andric // The binding will be weak if there is at least one reference and all are 49785868e8aSDimitry Andric // weak. The binding has one opportunity to change to weak: if the first 49885868e8aSDimitry Andric // reference is weak. 49985868e8aSDimitry Andric if (other.binding != STB_WEAK || !referenced) 5000b57cec5SDimitry Andric binding = other.binding; 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 50481ad6265SDimitry Andric // Compare two symbols. Return true if the new symbol should win. 50581ad6265SDimitry Andric bool Symbol::shouldReplace(const Defined &other) const { 50681ad6265SDimitry Andric if (LLVM_UNLIKELY(isCommon())) { 50781ad6265SDimitry Andric if (config->warnCommon) 50881ad6265SDimitry Andric warn("common " + getName() + " is overridden"); 50981ad6265SDimitry Andric return !other.isWeak(); 51081ad6265SDimitry Andric } 51181ad6265SDimitry Andric if (!isDefined()) 51281ad6265SDimitry Andric return true; 5130b57cec5SDimitry Andric 51481ad6265SDimitry Andric // Incoming STB_GLOBAL overrides STB_WEAK/STB_GNU_UNIQUE. -fgnu-unique changes 51581ad6265SDimitry Andric // some vague linkage data in COMDAT from STB_WEAK to STB_GNU_UNIQUE. Treat 51681ad6265SDimitry Andric // STB_GNU_UNIQUE like STB_WEAK so that we prefer the first among all 51781ad6265SDimitry Andric // STB_WEAK/STB_GNU_UNIQUE copies. If we prefer an incoming STB_GNU_UNIQUE to 51881ad6265SDimitry Andric // an existing STB_WEAK, there may be discarded section errors because the 51981ad6265SDimitry Andric // selected copy may be in a non-prevailing COMDAT. 52081ad6265SDimitry Andric return !isGlobal() && other.isGlobal(); 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 52381ad6265SDimitry Andric void elf::reportDuplicate(const Symbol &sym, const InputFile *newFile, 5240b57cec5SDimitry Andric InputSectionBase *errSec, uint64_t errOffset) { 5250b57cec5SDimitry Andric if (config->allowMultipleDefinition) 5260b57cec5SDimitry Andric return; 52781ad6265SDimitry Andric // In glibc<2.32, crti.o has .gnu.linkonce.t.__x86.get_pc_thunk.bx, which 52881ad6265SDimitry Andric // is sort of proto-comdat. There is actually no duplicate if we have 52981ad6265SDimitry Andric // full support for .gnu.linkonce. 53081ad6265SDimitry Andric const Defined *d = dyn_cast<Defined>(&sym); 53181ad6265SDimitry Andric if (!d || d->getName() == "__x86.get_pc_thunk.bx") 53281ad6265SDimitry Andric return; 53381ad6265SDimitry Andric // Allow absolute symbols with the same value for GNU ld compatibility. 53481ad6265SDimitry Andric if (!d->section && !errSec && errOffset && d->value == errOffset) 53581ad6265SDimitry Andric return; 5360b57cec5SDimitry Andric if (!d->section || !errSec) { 53781ad6265SDimitry Andric error("duplicate symbol: " + toString(sym) + "\n>>> defined in " + 53881ad6265SDimitry Andric toString(sym.file) + "\n>>> defined in " + toString(newFile)); 5390b57cec5SDimitry Andric return; 5400b57cec5SDimitry Andric } 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Construct and print an error message in the form of: 5430b57cec5SDimitry Andric // 5440b57cec5SDimitry Andric // ld.lld: error: duplicate symbol: foo 5450b57cec5SDimitry Andric // >>> defined at bar.c:30 5460b57cec5SDimitry Andric // >>> bar.o (/home/alice/src/bar.o) 5470b57cec5SDimitry Andric // >>> defined at baz.c:563 5480b57cec5SDimitry Andric // >>> baz.o in archive libbaz.a 5490b57cec5SDimitry Andric auto *sec1 = cast<InputSectionBase>(d->section); 55081ad6265SDimitry Andric std::string src1 = sec1->getSrcMsg(sym, d->value); 5510b57cec5SDimitry Andric std::string obj1 = sec1->getObjMsg(d->value); 55281ad6265SDimitry Andric std::string src2 = errSec->getSrcMsg(sym, errOffset); 5530b57cec5SDimitry Andric std::string obj2 = errSec->getObjMsg(errOffset); 5540b57cec5SDimitry Andric 55581ad6265SDimitry Andric std::string msg = "duplicate symbol: " + toString(sym) + "\n>>> defined at "; 5560b57cec5SDimitry Andric if (!src1.empty()) 5570b57cec5SDimitry Andric msg += src1 + "\n>>> "; 5580b57cec5SDimitry Andric msg += obj1 + "\n>>> defined at "; 5590b57cec5SDimitry Andric if (!src2.empty()) 5600b57cec5SDimitry Andric msg += src2 + "\n>>> "; 5610b57cec5SDimitry Andric msg += obj2; 5620b57cec5SDimitry Andric error(msg); 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric 56581ad6265SDimitry Andric void Symbol::checkDuplicate(const Defined &other) const { 56681ad6265SDimitry Andric if (isDefined() && !isWeak() && !other.isWeak()) 56781ad6265SDimitry Andric reportDuplicate(*this, other.file, 56881ad6265SDimitry Andric dyn_cast_or_null<InputSectionBase>(other.section), 56981ad6265SDimitry Andric other.value); 57081ad6265SDimitry Andric } 5710b57cec5SDimitry Andric 572*bdd1243dSDimitry Andric void Symbol::resolve(const CommonSymbol &other) { 573*bdd1243dSDimitry Andric if (other.exportDynamic) 574*bdd1243dSDimitry Andric exportDynamic = true; 575*bdd1243dSDimitry Andric if (other.visibility() != STV_DEFAULT) { 576*bdd1243dSDimitry Andric uint8_t v = visibility(), ov = other.visibility(); 577*bdd1243dSDimitry Andric setVisibility(v == STV_DEFAULT ? ov : std::min(v, ov)); 578*bdd1243dSDimitry Andric } 57981ad6265SDimitry Andric if (isDefined() && !isWeak()) { 58081ad6265SDimitry Andric if (config->warnCommon) 58181ad6265SDimitry Andric warn("common " + getName() + " is overridden"); 58281ad6265SDimitry Andric return; 58381ad6265SDimitry Andric } 58481ad6265SDimitry Andric 58581ad6265SDimitry Andric if (CommonSymbol *oldSym = dyn_cast<CommonSymbol>(this)) { 58681ad6265SDimitry Andric if (config->warnCommon) 58781ad6265SDimitry Andric warn("multiple common of " + getName()); 58881ad6265SDimitry Andric oldSym->alignment = std::max(oldSym->alignment, other.alignment); 58981ad6265SDimitry Andric if (oldSym->size < other.size) { 59081ad6265SDimitry Andric oldSym->file = other.file; 59181ad6265SDimitry Andric oldSym->size = other.size; 59281ad6265SDimitry Andric } 59381ad6265SDimitry Andric return; 59481ad6265SDimitry Andric } 59581ad6265SDimitry Andric 596480093f4SDimitry Andric if (auto *s = dyn_cast<SharedSymbol>(this)) { 597480093f4SDimitry Andric // Increase st_size if the shared symbol has a larger st_size. The shared 598480093f4SDimitry Andric // symbol may be created from common symbols. The fact that some object 599480093f4SDimitry Andric // files were linked into a shared object first should not change the 600480093f4SDimitry Andric // regular rule that picks the largest st_size. 601480093f4SDimitry Andric uint64_t size = s->size; 602*bdd1243dSDimitry Andric other.overwrite(*this); 603480093f4SDimitry Andric if (size > cast<CommonSymbol>(this)->size) 604480093f4SDimitry Andric cast<CommonSymbol>(this)->size = size; 605480093f4SDimitry Andric } else { 606*bdd1243dSDimitry Andric other.overwrite(*this); 607480093f4SDimitry Andric } 6080b57cec5SDimitry Andric } 6090b57cec5SDimitry Andric 610*bdd1243dSDimitry Andric void Symbol::resolve(const Defined &other) { 611*bdd1243dSDimitry Andric if (other.exportDynamic) 612*bdd1243dSDimitry Andric exportDynamic = true; 613*bdd1243dSDimitry Andric if (other.visibility() != STV_DEFAULT) { 614*bdd1243dSDimitry Andric uint8_t v = visibility(), ov = other.visibility(); 615*bdd1243dSDimitry Andric setVisibility(v == STV_DEFAULT ? ov : std::min(v, ov)); 616*bdd1243dSDimitry Andric } 61781ad6265SDimitry Andric if (shouldReplace(other)) 618*bdd1243dSDimitry Andric other.overwrite(*this); 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 621*bdd1243dSDimitry Andric void Symbol::resolve(const LazyObject &other) { 622*bdd1243dSDimitry Andric if (isPlaceholder()) { 623*bdd1243dSDimitry Andric other.overwrite(*this); 624*bdd1243dSDimitry Andric return; 625*bdd1243dSDimitry Andric } 626*bdd1243dSDimitry Andric 627e8d8bef9SDimitry Andric // For common objects, we want to look for global or weak definitions that 6284824e7fdSDimitry Andric // should be extracted as the canonical definition instead. 62981ad6265SDimitry Andric if (LLVM_UNLIKELY(isCommon()) && elf::config->fortranCommon && 63081ad6265SDimitry Andric other.file->shouldExtractForCommon(getName())) { 631*bdd1243dSDimitry Andric ctx.backwardReferences.erase(this); 632*bdd1243dSDimitry Andric other.overwrite(*this); 63381ad6265SDimitry Andric other.extract(); 634e8d8bef9SDimitry Andric return; 635e8d8bef9SDimitry Andric } 636e8d8bef9SDimitry Andric 6375ffd83dbSDimitry Andric if (!isUndefined()) { 6385ffd83dbSDimitry Andric // See the comment in resolveUndefined(). 6395ffd83dbSDimitry Andric if (isDefined()) 640*bdd1243dSDimitry Andric ctx.backwardReferences.erase(this); 6410b57cec5SDimitry Andric return; 6425ffd83dbSDimitry Andric } 6430b57cec5SDimitry Andric 6444824e7fdSDimitry Andric // An undefined weak will not extract archive members. See comment on Lazy in 6450b57cec5SDimitry Andric // Symbols.h for the details. 6460b57cec5SDimitry Andric if (isWeak()) { 6470b57cec5SDimitry Andric uint8_t ty = type; 648*bdd1243dSDimitry Andric other.overwrite(*this); 6490b57cec5SDimitry Andric type = ty; 6500b57cec5SDimitry Andric binding = STB_WEAK; 6510b57cec5SDimitry Andric return; 6520b57cec5SDimitry Andric } 6530b57cec5SDimitry Andric 654349cc55cSDimitry Andric const InputFile *oldFile = file; 6554824e7fdSDimitry Andric other.extract(); 656349cc55cSDimitry Andric if (!config->whyExtract.empty()) 657349cc55cSDimitry Andric recordWhyExtract(oldFile, *file, *this); 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric 660*bdd1243dSDimitry Andric void Symbol::resolve(const SharedSymbol &other) { 661*bdd1243dSDimitry Andric exportDynamic = true; 662*bdd1243dSDimitry Andric if (isPlaceholder()) { 663*bdd1243dSDimitry Andric other.overwrite(*this); 664*bdd1243dSDimitry Andric return; 665*bdd1243dSDimitry Andric } 666480093f4SDimitry Andric if (isCommon()) { 667480093f4SDimitry Andric // See the comment in resolveCommon() above. 668480093f4SDimitry Andric if (other.size > cast<CommonSymbol>(this)->size) 669480093f4SDimitry Andric cast<CommonSymbol>(this)->size = other.size; 670480093f4SDimitry Andric return; 671480093f4SDimitry Andric } 672*bdd1243dSDimitry Andric if (visibility() == STV_DEFAULT && (isUndefined() || isLazy())) { 6730b57cec5SDimitry Andric // An undefined symbol with non default visibility must be satisfied 6740b57cec5SDimitry Andric // in the same DSO. 6750b57cec5SDimitry Andric uint8_t bind = binding; 676*bdd1243dSDimitry Andric other.overwrite(*this); 6770b57cec5SDimitry Andric binding = bind; 6785ffd83dbSDimitry Andric } else if (traced) 67981ad6265SDimitry Andric printTraceSymbol(other, getName()); 6800b57cec5SDimitry Andric } 681