1ece8a530Spatrick //===- Symbols.cpp --------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick
9ece8a530Spatrick #include "Symbols.h"
1005edf1c1Srobert #include "Driver.h"
11ece8a530Spatrick #include "InputFiles.h"
12ece8a530Spatrick #include "InputSection.h"
13ece8a530Spatrick #include "OutputSections.h"
14ece8a530Spatrick #include "SyntheticSections.h"
15ece8a530Spatrick #include "Target.h"
16ece8a530Spatrick #include "Writer.h"
17ece8a530Spatrick #include "lld/Common/ErrorHandler.h"
1805edf1c1Srobert #include "llvm/Demangle/Demangle.h"
1905edf1c1Srobert #include "llvm/Support/Compiler.h"
20ece8a530Spatrick #include <cstring>
21ece8a530Spatrick
22ece8a530Spatrick using namespace llvm;
23ece8a530Spatrick using namespace llvm::object;
24ece8a530Spatrick using namespace llvm::ELF;
25bb684c34Spatrick using namespace lld;
26bb684c34Spatrick using namespace lld::elf;
27ece8a530Spatrick
28*b1fea01fStobhe static_assert(sizeof(SymbolUnion) <= 72, "SymbolUnion too large");
2905edf1c1Srobert
3005edf1c1Srobert template <typename T> struct AssertSymbol {
3105edf1c1Srobert static_assert(std::is_trivially_destructible<T>(),
3205edf1c1Srobert "Symbol types must be trivially destructible");
3305edf1c1Srobert static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
3405edf1c1Srobert static_assert(alignof(T) <= alignof(SymbolUnion),
3505edf1c1Srobert "SymbolUnion not aligned enough");
3605edf1c1Srobert };
3705edf1c1Srobert
assertSymbols()3805edf1c1Srobert LLVM_ATTRIBUTE_UNUSED static inline void assertSymbols() {
3905edf1c1Srobert AssertSymbol<Defined>();
4005edf1c1Srobert AssertSymbol<CommonSymbol>();
4105edf1c1Srobert AssertSymbol<Undefined>();
4205edf1c1Srobert AssertSymbol<SharedSymbol>();
4305edf1c1Srobert AssertSymbol<LazyObject>();
4405edf1c1Srobert }
4505edf1c1Srobert
46ece8a530Spatrick // Returns a symbol for an error message.
maybeDemangleSymbol(StringRef symName)4705edf1c1Srobert static std::string maybeDemangleSymbol(StringRef symName) {
48ece8a530Spatrick if (elf::config->demangle)
4905edf1c1Srobert return demangle(symName.str());
5005edf1c1Srobert return symName.str();
51ece8a530Spatrick }
52ece8a530Spatrick
toString(const elf::Symbol & sym)53bb684c34Spatrick std::string lld::toString(const elf::Symbol &sym) {
54bb684c34Spatrick StringRef name = sym.getName();
5505edf1c1Srobert std::string ret = maybeDemangleSymbol(name);
56bb684c34Spatrick
57a0747c9fSpatrick const char *suffix = sym.getVersionSuffix();
58a0747c9fSpatrick if (*suffix == '@')
59a0747c9fSpatrick ret += suffix;
60bb684c34Spatrick return ret;
61bb684c34Spatrick }
62bb684c34Spatrick
63ece8a530Spatrick Defined *ElfSym::bss;
64adae0cfdSpatrick Defined *ElfSym::data;
65ece8a530Spatrick Defined *ElfSym::etext1;
66ece8a530Spatrick Defined *ElfSym::etext2;
67ece8a530Spatrick Defined *ElfSym::edata1;
68ece8a530Spatrick Defined *ElfSym::edata2;
69ece8a530Spatrick Defined *ElfSym::end1;
70ece8a530Spatrick Defined *ElfSym::end2;
71ece8a530Spatrick Defined *ElfSym::globalOffsetTable;
72ece8a530Spatrick Defined *ElfSym::mipsGp;
73ece8a530Spatrick Defined *ElfSym::mipsGpDisp;
74ece8a530Spatrick Defined *ElfSym::mipsLocalGp;
75ece8a530Spatrick Defined *ElfSym::relaIpltStart;
76ece8a530Spatrick Defined *ElfSym::relaIpltEnd;
77ece8a530Spatrick Defined *ElfSym::tlsModuleBase;
7805edf1c1Srobert SmallVector<SymbolAux, 0> elf::symAux;
79ece8a530Spatrick
getSymVA(const Symbol & sym,int64_t addend)8005edf1c1Srobert static uint64_t getSymVA(const Symbol &sym, int64_t addend) {
81ece8a530Spatrick switch (sym.kind()) {
82ece8a530Spatrick case Symbol::DefinedKind: {
83ece8a530Spatrick auto &d = cast<Defined>(sym);
84ece8a530Spatrick SectionBase *isec = d.section;
85ece8a530Spatrick
86ece8a530Spatrick // This is an absolute symbol.
87ece8a530Spatrick if (!isec)
88ece8a530Spatrick return d.value;
89ece8a530Spatrick
90ece8a530Spatrick assert(isec != &InputSection::discarded);
91ece8a530Spatrick
92ece8a530Spatrick uint64_t offset = d.value;
93ece8a530Spatrick
94ece8a530Spatrick // An object in an SHF_MERGE section might be referenced via a
95ece8a530Spatrick // section symbol (as a hack for reducing the number of local
96ece8a530Spatrick // symbols).
97ece8a530Spatrick // Depending on the addend, the reference via a section symbol
98ece8a530Spatrick // refers to a different object in the merge section.
99ece8a530Spatrick // Since the objects in the merge section are not necessarily
100ece8a530Spatrick // contiguous in the output, the addend can thus affect the final
101ece8a530Spatrick // VA in a non-linear way.
102ece8a530Spatrick // To make this work, we incorporate the addend into the section
103ece8a530Spatrick // offset (and zero out the addend for later processing) so that
104ece8a530Spatrick // we find the right object in the section.
10505edf1c1Srobert if (d.isSection())
106ece8a530Spatrick offset += addend;
107ece8a530Spatrick
108ece8a530Spatrick // In the typical case, this is actually very simple and boils
109ece8a530Spatrick // down to adding together 3 numbers:
110ece8a530Spatrick // 1. The address of the output section.
111ece8a530Spatrick // 2. The offset of the input section within the output section.
112ece8a530Spatrick // 3. The offset within the input section (this addition happens
113ece8a530Spatrick // inside InputSection::getOffset).
114ece8a530Spatrick //
115ece8a530Spatrick // If you understand the data structures involved with this next
116ece8a530Spatrick // line (and how they get built), then you have a pretty good
117ece8a530Spatrick // understanding of the linker.
118ece8a530Spatrick uint64_t va = isec->getVA(offset);
11905edf1c1Srobert if (d.isSection())
12005edf1c1Srobert va -= addend;
121ece8a530Spatrick
122ece8a530Spatrick // MIPS relocatable files can mix regular and microMIPS code.
123ece8a530Spatrick // Linker needs to distinguish such code. To do so microMIPS
124ece8a530Spatrick // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
125bb684c34Spatrick // field. Unfortunately, the `MIPS::relocate()` method has
126ece8a530Spatrick // a symbol value only. To pass type of the symbol (regular/microMIPS)
127ece8a530Spatrick // to that routine as well as other places where we write
128ece8a530Spatrick // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
129ece8a530Spatrick // field etc) do the same trick as compiler uses to mark microMIPS
130ece8a530Spatrick // for CPU - set the less-significant bit.
131ece8a530Spatrick if (config->emachine == EM_MIPS && isMicroMips() &&
13205edf1c1Srobert ((sym.stOther & STO_MIPS_MICROMIPS) || sym.hasFlag(NEEDS_COPY)))
133ece8a530Spatrick va |= 1;
134ece8a530Spatrick
135ece8a530Spatrick if (d.isTls() && !config->relocatable) {
136ece8a530Spatrick // Use the address of the TLS segment's first section rather than the
137ece8a530Spatrick // segment's address, because segment addresses aren't initialized until
138ece8a530Spatrick // after sections are finalized. (e.g. Measuring the size of .rela.dyn
139ece8a530Spatrick // for Android relocation packing requires knowing TLS symbol addresses
140ece8a530Spatrick // during section finalization.)
141ece8a530Spatrick if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec)
142ece8a530Spatrick fatal(toString(d.file) +
143ece8a530Spatrick " has an STT_TLS symbol but doesn't have an SHF_TLS section");
144ece8a530Spatrick return va - Out::tlsPhdr->firstSec->addr;
145ece8a530Spatrick }
146ece8a530Spatrick return va;
147ece8a530Spatrick }
148ece8a530Spatrick case Symbol::SharedKind:
149ece8a530Spatrick case Symbol::UndefinedKind:
150ece8a530Spatrick return 0;
151ece8a530Spatrick case Symbol::LazyObjectKind:
15205edf1c1Srobert llvm_unreachable("lazy symbol reached writer");
153ece8a530Spatrick case Symbol::CommonKind:
154ece8a530Spatrick llvm_unreachable("common symbol reached writer");
155ece8a530Spatrick case Symbol::PlaceholderKind:
156ece8a530Spatrick llvm_unreachable("placeholder symbol reached writer");
157ece8a530Spatrick }
158ece8a530Spatrick llvm_unreachable("invalid symbol kind");
159ece8a530Spatrick }
160ece8a530Spatrick
getVA(int64_t addend) const161ece8a530Spatrick uint64_t Symbol::getVA(int64_t addend) const {
16205edf1c1Srobert return getSymVA(*this, addend) + addend;
163ece8a530Spatrick }
164ece8a530Spatrick
getGotVA() const165ece8a530Spatrick uint64_t Symbol::getGotVA() const {
166ece8a530Spatrick if (gotInIgot)
167ece8a530Spatrick return in.igotPlt->getVA() + getGotPltOffset();
168ece8a530Spatrick return in.got->getVA() + getGotOffset();
169ece8a530Spatrick }
170ece8a530Spatrick
getGotOffset() const171a0747c9fSpatrick uint64_t Symbol::getGotOffset() const {
17205edf1c1Srobert return getGotIdx() * target->gotEntrySize;
173a0747c9fSpatrick }
174ece8a530Spatrick
getGotPltVA() const175ece8a530Spatrick uint64_t Symbol::getGotPltVA() const {
176ece8a530Spatrick if (isInIplt)
177ece8a530Spatrick return in.igotPlt->getVA() + getGotPltOffset();
178ece8a530Spatrick return in.gotPlt->getVA() + getGotPltOffset();
179ece8a530Spatrick }
180ece8a530Spatrick
getGotPltOffset() const181ece8a530Spatrick uint64_t Symbol::getGotPltOffset() const {
182ece8a530Spatrick if (isInIplt)
18305edf1c1Srobert return getPltIdx() * target->gotEntrySize;
18405edf1c1Srobert return (getPltIdx() + target->gotPltHeaderEntriesNum) * target->gotEntrySize;
185ece8a530Spatrick }
186ece8a530Spatrick
getPltVA() const187ece8a530Spatrick uint64_t Symbol::getPltVA() const {
188ece8a530Spatrick uint64_t outVA = isInIplt
18905edf1c1Srobert ? in.iplt->getVA() + getPltIdx() * target->ipltEntrySize
190ece8a530Spatrick : in.plt->getVA() + in.plt->headerSize +
19105edf1c1Srobert getPltIdx() * target->pltEntrySize;
192ece8a530Spatrick
193ece8a530Spatrick // While linking microMIPS code PLT code are always microMIPS
194ece8a530Spatrick // code. Set the less-significant bit to track that fact.
195ece8a530Spatrick // See detailed comment in the `getSymVA` function.
196ece8a530Spatrick if (config->emachine == EM_MIPS && isMicroMips())
197ece8a530Spatrick outVA |= 1;
198ece8a530Spatrick return outVA;
199ece8a530Spatrick }
200ece8a530Spatrick
getSize() const201ece8a530Spatrick uint64_t Symbol::getSize() const {
202ece8a530Spatrick if (const auto *dr = dyn_cast<Defined>(this))
203ece8a530Spatrick return dr->size;
204ece8a530Spatrick return cast<SharedSymbol>(this)->size;
205ece8a530Spatrick }
206ece8a530Spatrick
getOutputSection() const207ece8a530Spatrick OutputSection *Symbol::getOutputSection() const {
208ece8a530Spatrick if (auto *s = dyn_cast<Defined>(this)) {
209ece8a530Spatrick if (auto *sec = s->section)
21005edf1c1Srobert return sec->getOutputSection();
211ece8a530Spatrick return nullptr;
212ece8a530Spatrick }
213ece8a530Spatrick return nullptr;
214ece8a530Spatrick }
215ece8a530Spatrick
216ece8a530Spatrick // If a symbol name contains '@', the characters after that is
217ece8a530Spatrick // a symbol version name. This function parses that.
parseSymbolVersion()218ece8a530Spatrick void Symbol::parseSymbolVersion() {
219a0747c9fSpatrick // Return if localized by a local: pattern in a version script.
220a0747c9fSpatrick if (versionId == VER_NDX_LOCAL)
221a0747c9fSpatrick return;
222ece8a530Spatrick StringRef s = getName();
223ece8a530Spatrick size_t pos = s.find('@');
22405edf1c1Srobert if (pos == StringRef::npos)
225ece8a530Spatrick return;
226ece8a530Spatrick StringRef verstr = s.substr(pos + 1);
227ece8a530Spatrick
228ece8a530Spatrick // Truncate the symbol name so that it doesn't include the version string.
229ece8a530Spatrick nameSize = pos;
230ece8a530Spatrick
23105edf1c1Srobert if (verstr.empty())
23205edf1c1Srobert return;
23305edf1c1Srobert
234ece8a530Spatrick // If this is not in this DSO, it is not a definition.
235ece8a530Spatrick if (!isDefined())
236ece8a530Spatrick return;
237ece8a530Spatrick
238ece8a530Spatrick // '@@' in a symbol name means the default version.
239ece8a530Spatrick // It is usually the most recent one.
240ece8a530Spatrick bool isDefault = (verstr[0] == '@');
241ece8a530Spatrick if (isDefault)
242ece8a530Spatrick verstr = verstr.substr(1);
243ece8a530Spatrick
244ece8a530Spatrick for (const VersionDefinition &ver : namedVersionDefs()) {
245ece8a530Spatrick if (ver.name != verstr)
246ece8a530Spatrick continue;
247ece8a530Spatrick
248ece8a530Spatrick if (isDefault)
249ece8a530Spatrick versionId = ver.id;
250ece8a530Spatrick else
251ece8a530Spatrick versionId = ver.id | VERSYM_HIDDEN;
252ece8a530Spatrick return;
253ece8a530Spatrick }
254ece8a530Spatrick
255ece8a530Spatrick // It is an error if the specified version is not defined.
256ece8a530Spatrick // Usually version script is not provided when linking executable,
257ece8a530Spatrick // but we may still want to override a versioned symbol from DSO,
258ece8a530Spatrick // so we do not report error in this case. We also do not error
259ece8a530Spatrick // if the symbol has a local version as it won't be in the dynamic
260ece8a530Spatrick // symbol table.
261ece8a530Spatrick if (config->shared && versionId != VER_NDX_LOCAL)
262ece8a530Spatrick error(toString(file) + ": symbol " + s + " has undefined version " +
263ece8a530Spatrick verstr);
264ece8a530Spatrick }
265ece8a530Spatrick
extract() const26605edf1c1Srobert void Symbol::extract() const {
26705edf1c1Srobert if (file->lazy) {
26805edf1c1Srobert file->lazy = false;
26905edf1c1Srobert parseFile(file);
270ece8a530Spatrick }
271ece8a530Spatrick }
272ece8a530Spatrick
computeBinding() const273ece8a530Spatrick uint8_t Symbol::computeBinding() const {
27405edf1c1Srobert auto v = visibility();
27505edf1c1Srobert if ((v != STV_DEFAULT && v != STV_PROTECTED) || versionId == VER_NDX_LOCAL)
276ece8a530Spatrick return STB_LOCAL;
27705edf1c1Srobert if (binding == STB_GNU_UNIQUE && !config->gnuUnique)
278ece8a530Spatrick return STB_GLOBAL;
279ece8a530Spatrick return binding;
280ece8a530Spatrick }
281ece8a530Spatrick
includeInDynsym() const282ece8a530Spatrick bool Symbol::includeInDynsym() const {
283ece8a530Spatrick if (computeBinding() == STB_LOCAL)
284ece8a530Spatrick return false;
285ece8a530Spatrick if (!isDefined() && !isCommon())
286ece8a530Spatrick // This should unconditionally return true, unfortunately glibc -static-pie
287ece8a530Spatrick // expects undefined weak symbols not to exist in .dynsym, e.g.
288ece8a530Spatrick // __pthread_mutex_lock reference in _dl_add_to_namespace_list,
289ece8a530Spatrick // __pthread_initialize_minimal reference in csu/libc-start.c.
29005edf1c1Srobert return !(isUndefWeak() && config->noDynamicLinker);
291ece8a530Spatrick
292ece8a530Spatrick return exportDynamic || inDynamicList;
293ece8a530Spatrick }
294ece8a530Spatrick
295ece8a530Spatrick // Print out a log message for --trace-symbol.
printTraceSymbol(const Symbol & sym,StringRef name)29605edf1c1Srobert void elf::printTraceSymbol(const Symbol &sym, StringRef name) {
297ece8a530Spatrick std::string s;
29805edf1c1Srobert if (sym.isUndefined())
299ece8a530Spatrick s = ": reference to ";
30005edf1c1Srobert else if (sym.isLazy())
301ece8a530Spatrick s = ": lazy definition of ";
30205edf1c1Srobert else if (sym.isShared())
303ece8a530Spatrick s = ": shared definition of ";
30405edf1c1Srobert else if (sym.isCommon())
305ece8a530Spatrick s = ": common definition of ";
306ece8a530Spatrick else
307ece8a530Spatrick s = ": definition of ";
308ece8a530Spatrick
30905edf1c1Srobert message(toString(sym.file) + s + name);
31005edf1c1Srobert }
31105edf1c1Srobert
recordWhyExtract(const InputFile * reference,const InputFile & extracted,const Symbol & sym)31205edf1c1Srobert static void recordWhyExtract(const InputFile *reference,
31305edf1c1Srobert const InputFile &extracted, const Symbol &sym) {
31405edf1c1Srobert ctx.whyExtractRecords.emplace_back(toString(reference), &extracted, sym);
315ece8a530Spatrick }
316ece8a530Spatrick
maybeWarnUnorderableSymbol(const Symbol * sym)317bb684c34Spatrick void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
318ece8a530Spatrick if (!config->warnSymbolOrdering)
319ece8a530Spatrick return;
320ece8a530Spatrick
321ece8a530Spatrick // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
322ece8a530Spatrick // is emitted. It makes sense to not warn on undefined symbols.
323ece8a530Spatrick //
324ece8a530Spatrick // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
325ece8a530Spatrick // but we don't have to be compatible here.
326ece8a530Spatrick if (sym->isUndefined() &&
327ece8a530Spatrick config->unresolvedSymbols == UnresolvedPolicy::Ignore)
328ece8a530Spatrick return;
329ece8a530Spatrick
330ece8a530Spatrick const InputFile *file = sym->file;
331ece8a530Spatrick auto *d = dyn_cast<Defined>(sym);
332ece8a530Spatrick
333ece8a530Spatrick auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); };
334ece8a530Spatrick
335ece8a530Spatrick if (sym->isUndefined())
336ece8a530Spatrick report(": unable to order undefined symbol: ");
337ece8a530Spatrick else if (sym->isShared())
338ece8a530Spatrick report(": unable to order shared symbol: ");
339ece8a530Spatrick else if (d && !d->section)
340ece8a530Spatrick report(": unable to order absolute symbol: ");
341ece8a530Spatrick else if (d && isa<OutputSection>(d->section))
342ece8a530Spatrick report(": unable to order synthetic symbol: ");
34305edf1c1Srobert else if (d && !d->section->isLive())
344ece8a530Spatrick report(": unable to order discarded symbol: ");
345ece8a530Spatrick }
346ece8a530Spatrick
347ece8a530Spatrick // Returns true if a symbol can be replaced at load-time by a symbol
348ece8a530Spatrick // with the same name defined in other ELF executable or DSO.
computeIsPreemptible(const Symbol & sym)349bb684c34Spatrick bool elf::computeIsPreemptible(const Symbol &sym) {
35005edf1c1Srobert assert(!sym.isLocal() || sym.isPlaceholder());
351ece8a530Spatrick
352ece8a530Spatrick // Only symbols with default visibility that appear in dynsym can be
353ece8a530Spatrick // preempted. Symbols with protected visibility cannot be preempted.
35405edf1c1Srobert if (!sym.includeInDynsym() || sym.visibility() != STV_DEFAULT)
355ece8a530Spatrick return false;
356ece8a530Spatrick
357ece8a530Spatrick // At this point copy relocations have not been created yet, so any
358ece8a530Spatrick // symbol that is not defined locally is preemptible.
359ece8a530Spatrick if (!sym.isDefined())
360ece8a530Spatrick return true;
361ece8a530Spatrick
362ece8a530Spatrick if (!config->shared)
363ece8a530Spatrick return false;
364ece8a530Spatrick
365bb684c34Spatrick // If -Bsymbolic or --dynamic-list is specified, or -Bsymbolic-functions is
366bb684c34Spatrick // specified and the symbol is STT_FUNC, the symbol is preemptible iff it is
367a0747c9fSpatrick // in the dynamic list. -Bsymbolic-non-weak-functions is a non-weak subset of
368a0747c9fSpatrick // -Bsymbolic-functions.
369a0747c9fSpatrick if (config->symbolic ||
370a0747c9fSpatrick (config->bsymbolic == BsymbolicKind::Functions && sym.isFunc()) ||
371a0747c9fSpatrick (config->bsymbolic == BsymbolicKind::NonWeakFunctions && sym.isFunc() &&
372a0747c9fSpatrick sym.binding != STB_WEAK))
373ece8a530Spatrick return sym.inDynamicList;
374ece8a530Spatrick return true;
375ece8a530Spatrick }
376ece8a530Spatrick
377ece8a530Spatrick // Merge symbol properties.
378ece8a530Spatrick //
379ece8a530Spatrick // When we have many symbols of the same name, we choose one of them,
380ece8a530Spatrick // and that's the result of symbol resolution. However, symbols that
381ece8a530Spatrick // were not chosen still affect some symbol properties.
mergeProperties(const Symbol & other)382ece8a530Spatrick void Symbol::mergeProperties(const Symbol &other) {
383ece8a530Spatrick if (other.exportDynamic)
384ece8a530Spatrick exportDynamic = true;
385ece8a530Spatrick
386ece8a530Spatrick // DSO symbols do not affect visibility in the output.
38705edf1c1Srobert if (!other.isShared() && other.visibility() != STV_DEFAULT) {
38805edf1c1Srobert uint8_t v = visibility(), ov = other.visibility();
38905edf1c1Srobert setVisibility(v == STV_DEFAULT ? ov : std::min(v, ov));
390ece8a530Spatrick }
391ece8a530Spatrick }
392ece8a530Spatrick
resolve(const Undefined & other)39305edf1c1Srobert void Symbol::resolve(const Undefined &other) {
39405edf1c1Srobert if (other.visibility() != STV_DEFAULT) {
39505edf1c1Srobert uint8_t v = visibility(), ov = other.visibility();
39605edf1c1Srobert setVisibility(v == STV_DEFAULT ? ov : std::min(v, ov));
39705edf1c1Srobert }
398ece8a530Spatrick // An undefined symbol with non default visibility must be satisfied
399ece8a530Spatrick // in the same DSO.
400ece8a530Spatrick //
401ece8a530Spatrick // If this is a non-weak defined symbol in a discarded section, override the
402ece8a530Spatrick // existing undefined symbol for better error message later.
40305edf1c1Srobert if (isPlaceholder() || (isShared() && other.visibility() != STV_DEFAULT) ||
404ece8a530Spatrick (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) {
40505edf1c1Srobert other.overwrite(*this);
406ece8a530Spatrick return;
407ece8a530Spatrick }
408ece8a530Spatrick
409ece8a530Spatrick if (traced)
41005edf1c1Srobert printTraceSymbol(other, getName());
411ece8a530Spatrick
412ece8a530Spatrick if (isLazy()) {
41305edf1c1Srobert // An undefined weak will not extract archive members. See comment on Lazy
41405edf1c1Srobert // in Symbols.h for the details.
415ece8a530Spatrick if (other.binding == STB_WEAK) {
416ece8a530Spatrick binding = STB_WEAK;
417ece8a530Spatrick type = other.type;
418ece8a530Spatrick return;
419ece8a530Spatrick }
420ece8a530Spatrick
421ece8a530Spatrick // Do extra check for --warn-backrefs.
422ece8a530Spatrick //
423ece8a530Spatrick // --warn-backrefs is an option to prevent an undefined reference from
42405edf1c1Srobert // extracting an archive member written earlier in the command line. It can
42505edf1c1Srobert // be used to keep compatibility with GNU linkers to some degree. I'll
42605edf1c1Srobert // explain the feature and why you may find it useful in this comment.
427ece8a530Spatrick //
428ece8a530Spatrick // lld's symbol resolution semantics is more relaxed than traditional Unix
429ece8a530Spatrick // linkers. For example,
430ece8a530Spatrick //
431ece8a530Spatrick // ld.lld foo.a bar.o
432ece8a530Spatrick //
433ece8a530Spatrick // succeeds even if bar.o contains an undefined symbol that has to be
434ece8a530Spatrick // resolved by some object file in foo.a. Traditional Unix linkers don't
435ece8a530Spatrick // allow this kind of backward reference, as they visit each file only once
436ece8a530Spatrick // from left to right in the command line while resolving all undefined
437ece8a530Spatrick // symbols at the moment of visiting.
438ece8a530Spatrick //
439ece8a530Spatrick // In the above case, since there's no undefined symbol when a linker visits
440ece8a530Spatrick // foo.a, no files are pulled out from foo.a, and because the linker forgets
441ece8a530Spatrick // about foo.a after visiting, it can't resolve undefined symbols in bar.o
442ece8a530Spatrick // that could have been resolved otherwise.
443ece8a530Spatrick //
444ece8a530Spatrick // That lld accepts more relaxed form means that (besides it'd make more
445ece8a530Spatrick // sense) you can accidentally write a command line or a build file that
446ece8a530Spatrick // works only with lld, even if you have a plan to distribute it to wider
447ece8a530Spatrick // users who may be using GNU linkers. With --warn-backrefs, you can detect
448ece8a530Spatrick // a library order that doesn't work with other Unix linkers.
449ece8a530Spatrick //
450ece8a530Spatrick // The option is also useful to detect cyclic dependencies between static
451ece8a530Spatrick // archives. Again, lld accepts
452ece8a530Spatrick //
453ece8a530Spatrick // ld.lld foo.a bar.a
454ece8a530Spatrick //
455ece8a530Spatrick // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
456ece8a530Spatrick // handled as an error.
457ece8a530Spatrick //
458ece8a530Spatrick // Here is how the option works. We assign a group ID to each file. A file
459ece8a530Spatrick // with a smaller group ID can pull out object files from an archive file
460ece8a530Spatrick // with an equal or greater group ID. Otherwise, it is a reverse dependency
461ece8a530Spatrick // and an error.
462ece8a530Spatrick //
463ece8a530Spatrick // A file outside --{start,end}-group gets a fresh ID when instantiated. All
464ece8a530Spatrick // files within the same --{start,end}-group get the same group ID. E.g.
465ece8a530Spatrick //
466ece8a530Spatrick // ld.lld A B --start-group C D --end-group E
467ece8a530Spatrick //
468ece8a530Spatrick // A forms group 0. B form group 1. C and D (including their member object
469ece8a530Spatrick // files) form group 2. E forms group 3. I think that you can see how this
470ece8a530Spatrick // group assignment rule simulates the traditional linker's semantics.
471ece8a530Spatrick bool backref = config->warnBackrefs && other.file &&
472ece8a530Spatrick file->groupId < other.file->groupId;
47305edf1c1Srobert extract();
47405edf1c1Srobert
47505edf1c1Srobert if (!config->whyExtract.empty())
47605edf1c1Srobert recordWhyExtract(other.file, *file, *this);
477ece8a530Spatrick
478ece8a530Spatrick // We don't report backward references to weak symbols as they can be
479ece8a530Spatrick // overridden later.
480bb684c34Spatrick //
481bb684c34Spatrick // A traditional linker does not error for -ldef1 -lref -ldef2 (linking
482bb684c34Spatrick // sandwich), where def2 may or may not be the same as def1. We don't want
483bb684c34Spatrick // to warn for this case, so dismiss the warning if we see a subsequent lazy
484a0747c9fSpatrick // definition. this->file needs to be saved because in the case of LTO it
485a0747c9fSpatrick // may be reset to nullptr or be replaced with a file named lto.tmp.
486ece8a530Spatrick if (backref && !isWeak())
48705edf1c1Srobert ctx.backwardReferences.try_emplace(this,
48805edf1c1Srobert std::make_pair(other.file, file));
489ece8a530Spatrick return;
490ece8a530Spatrick }
491ece8a530Spatrick
492ece8a530Spatrick // Undefined symbols in a SharedFile do not change the binding.
49305edf1c1Srobert if (isa_and_nonnull<SharedFile>(other.file))
494ece8a530Spatrick return;
495ece8a530Spatrick
496ece8a530Spatrick if (isUndefined() || isShared()) {
497ece8a530Spatrick // The binding will be weak if there is at least one reference and all are
498ece8a530Spatrick // weak. The binding has one opportunity to change to weak: if the first
499ece8a530Spatrick // reference is weak.
500ece8a530Spatrick if (other.binding != STB_WEAK || !referenced)
501ece8a530Spatrick binding = other.binding;
502ece8a530Spatrick }
503ece8a530Spatrick }
504ece8a530Spatrick
50505edf1c1Srobert // Compare two symbols. Return true if the new symbol should win.
shouldReplace(const Defined & other) const50605edf1c1Srobert bool Symbol::shouldReplace(const Defined &other) const {
50705edf1c1Srobert if (LLVM_UNLIKELY(isCommon())) {
508ece8a530Spatrick if (config->warnCommon)
509ece8a530Spatrick warn("common " + getName() + " is overridden");
51005edf1c1Srobert return !other.isWeak();
51105edf1c1Srobert }
51205edf1c1Srobert if (!isDefined())
51305edf1c1Srobert return true;
51405edf1c1Srobert
51505edf1c1Srobert // Incoming STB_GLOBAL overrides STB_WEAK/STB_GNU_UNIQUE. -fgnu-unique changes
51605edf1c1Srobert // some vague linkage data in COMDAT from STB_WEAK to STB_GNU_UNIQUE. Treat
51705edf1c1Srobert // STB_GNU_UNIQUE like STB_WEAK so that we prefer the first among all
51805edf1c1Srobert // STB_WEAK/STB_GNU_UNIQUE copies. If we prefer an incoming STB_GNU_UNIQUE to
51905edf1c1Srobert // an existing STB_WEAK, there may be discarded section errors because the
52005edf1c1Srobert // selected copy may be in a non-prevailing COMDAT.
52105edf1c1Srobert return !isGlobal() && other.isGlobal();
522ece8a530Spatrick }
523ece8a530Spatrick
reportDuplicate(const Symbol & sym,const InputFile * newFile,InputSectionBase * errSec,uint64_t errOffset)52405edf1c1Srobert void elf::reportDuplicate(const Symbol &sym, const InputFile *newFile,
525ece8a530Spatrick InputSectionBase *errSec, uint64_t errOffset) {
526ece8a530Spatrick if (config->allowMultipleDefinition)
527ece8a530Spatrick return;
52805edf1c1Srobert // In glibc<2.32, crti.o has .gnu.linkonce.t.__x86.get_pc_thunk.bx, which
52905edf1c1Srobert // is sort of proto-comdat. There is actually no duplicate if we have
53005edf1c1Srobert // full support for .gnu.linkonce.
53105edf1c1Srobert const Defined *d = dyn_cast<Defined>(&sym);
53205edf1c1Srobert if (!d || d->getName() == "__x86.get_pc_thunk.bx")
53305edf1c1Srobert return;
53405edf1c1Srobert // Allow absolute symbols with the same value for GNU ld compatibility.
53505edf1c1Srobert if (!d->section && !errSec && errOffset && d->value == errOffset)
53605edf1c1Srobert return;
537ece8a530Spatrick if (!d->section || !errSec) {
53805edf1c1Srobert error("duplicate symbol: " + toString(sym) + "\n>>> defined in " +
53905edf1c1Srobert toString(sym.file) + "\n>>> defined in " + toString(newFile));
540ece8a530Spatrick return;
541ece8a530Spatrick }
542ece8a530Spatrick
543ece8a530Spatrick // Construct and print an error message in the form of:
544ece8a530Spatrick //
545ece8a530Spatrick // ld.lld: error: duplicate symbol: foo
546ece8a530Spatrick // >>> defined at bar.c:30
547ece8a530Spatrick // >>> bar.o (/home/alice/src/bar.o)
548ece8a530Spatrick // >>> defined at baz.c:563
549ece8a530Spatrick // >>> baz.o in archive libbaz.a
550ece8a530Spatrick auto *sec1 = cast<InputSectionBase>(d->section);
55105edf1c1Srobert std::string src1 = sec1->getSrcMsg(sym, d->value);
552ece8a530Spatrick std::string obj1 = sec1->getObjMsg(d->value);
55305edf1c1Srobert std::string src2 = errSec->getSrcMsg(sym, errOffset);
554ece8a530Spatrick std::string obj2 = errSec->getObjMsg(errOffset);
555ece8a530Spatrick
55605edf1c1Srobert std::string msg = "duplicate symbol: " + toString(sym) + "\n>>> defined at ";
557ece8a530Spatrick if (!src1.empty())
558ece8a530Spatrick msg += src1 + "\n>>> ";
559ece8a530Spatrick msg += obj1 + "\n>>> defined at ";
560ece8a530Spatrick if (!src2.empty())
561ece8a530Spatrick msg += src2 + "\n>>> ";
562ece8a530Spatrick msg += obj2;
563ece8a530Spatrick error(msg);
564ece8a530Spatrick }
565ece8a530Spatrick
checkDuplicate(const Defined & other) const56605edf1c1Srobert void Symbol::checkDuplicate(const Defined &other) const {
56705edf1c1Srobert if (isDefined() && !isWeak() && !other.isWeak())
56805edf1c1Srobert reportDuplicate(*this, other.file,
56905edf1c1Srobert dyn_cast_or_null<InputSectionBase>(other.section),
57005edf1c1Srobert other.value);
57105edf1c1Srobert }
572ece8a530Spatrick
resolve(const CommonSymbol & other)57305edf1c1Srobert void Symbol::resolve(const CommonSymbol &other) {
57405edf1c1Srobert if (other.exportDynamic)
57505edf1c1Srobert exportDynamic = true;
57605edf1c1Srobert if (other.visibility() != STV_DEFAULT) {
57705edf1c1Srobert uint8_t v = visibility(), ov = other.visibility();
57805edf1c1Srobert setVisibility(v == STV_DEFAULT ? ov : std::min(v, ov));
57905edf1c1Srobert }
58005edf1c1Srobert if (isDefined() && !isWeak()) {
58105edf1c1Srobert if (config->warnCommon)
58205edf1c1Srobert warn("common " + getName() + " is overridden");
58305edf1c1Srobert return;
58405edf1c1Srobert }
58505edf1c1Srobert
58605edf1c1Srobert if (CommonSymbol *oldSym = dyn_cast<CommonSymbol>(this)) {
58705edf1c1Srobert if (config->warnCommon)
58805edf1c1Srobert warn("multiple common of " + getName());
58905edf1c1Srobert oldSym->alignment = std::max(oldSym->alignment, other.alignment);
59005edf1c1Srobert if (oldSym->size < other.size) {
59105edf1c1Srobert oldSym->file = other.file;
59205edf1c1Srobert oldSym->size = other.size;
59305edf1c1Srobert }
59405edf1c1Srobert return;
59505edf1c1Srobert }
59605edf1c1Srobert
597ece8a530Spatrick if (auto *s = dyn_cast<SharedSymbol>(this)) {
598ece8a530Spatrick // Increase st_size if the shared symbol has a larger st_size. The shared
599ece8a530Spatrick // symbol may be created from common symbols. The fact that some object
600ece8a530Spatrick // files were linked into a shared object first should not change the
601ece8a530Spatrick // regular rule that picks the largest st_size.
602ece8a530Spatrick uint64_t size = s->size;
60305edf1c1Srobert other.overwrite(*this);
604ece8a530Spatrick if (size > cast<CommonSymbol>(this)->size)
605ece8a530Spatrick cast<CommonSymbol>(this)->size = size;
606ece8a530Spatrick } else {
60705edf1c1Srobert other.overwrite(*this);
608ece8a530Spatrick }
60905edf1c1Srobert }
61005edf1c1Srobert
resolve(const Defined & other)61105edf1c1Srobert void Symbol::resolve(const Defined &other) {
61205edf1c1Srobert if (other.exportDynamic)
61305edf1c1Srobert exportDynamic = true;
61405edf1c1Srobert if (other.visibility() != STV_DEFAULT) {
61505edf1c1Srobert uint8_t v = visibility(), ov = other.visibility();
61605edf1c1Srobert setVisibility(v == STV_DEFAULT ? ov : std::min(v, ov));
61705edf1c1Srobert }
61805edf1c1Srobert if (shouldReplace(other))
61905edf1c1Srobert other.overwrite(*this);
62005edf1c1Srobert }
62105edf1c1Srobert
resolve(const LazyObject & other)62205edf1c1Srobert void Symbol::resolve(const LazyObject &other) {
62305edf1c1Srobert if (isPlaceholder()) {
62405edf1c1Srobert other.overwrite(*this);
625ece8a530Spatrick return;
626ece8a530Spatrick }
627ece8a530Spatrick
628a0747c9fSpatrick // For common objects, we want to look for global or weak definitions that
62905edf1c1Srobert // should be extracted as the canonical definition instead.
63005edf1c1Srobert if (LLVM_UNLIKELY(isCommon()) && elf::config->fortranCommon &&
63105edf1c1Srobert other.file->shouldExtractForCommon(getName())) {
63205edf1c1Srobert ctx.backwardReferences.erase(this);
63305edf1c1Srobert other.overwrite(*this);
63405edf1c1Srobert other.extract();
635a0747c9fSpatrick return;
636a0747c9fSpatrick }
637a0747c9fSpatrick
638bb684c34Spatrick if (!isUndefined()) {
639bb684c34Spatrick // See the comment in resolveUndefined().
640bb684c34Spatrick if (isDefined())
64105edf1c1Srobert ctx.backwardReferences.erase(this);
642ece8a530Spatrick return;
643bb684c34Spatrick }
644ece8a530Spatrick
64505edf1c1Srobert // An undefined weak will not extract archive members. See comment on Lazy in
646ece8a530Spatrick // Symbols.h for the details.
647ece8a530Spatrick if (isWeak()) {
648ece8a530Spatrick uint8_t ty = type;
64905edf1c1Srobert other.overwrite(*this);
650ece8a530Spatrick type = ty;
651ece8a530Spatrick binding = STB_WEAK;
652ece8a530Spatrick return;
653ece8a530Spatrick }
654ece8a530Spatrick
65505edf1c1Srobert const InputFile *oldFile = file;
65605edf1c1Srobert other.extract();
65705edf1c1Srobert if (!config->whyExtract.empty())
65805edf1c1Srobert recordWhyExtract(oldFile, *file, *this);
659ece8a530Spatrick }
660ece8a530Spatrick
resolve(const SharedSymbol & other)66105edf1c1Srobert void Symbol::resolve(const SharedSymbol &other) {
66205edf1c1Srobert exportDynamic = true;
66305edf1c1Srobert if (isPlaceholder()) {
66405edf1c1Srobert other.overwrite(*this);
66505edf1c1Srobert return;
66605edf1c1Srobert }
667ece8a530Spatrick if (isCommon()) {
668ece8a530Spatrick // See the comment in resolveCommon() above.
669ece8a530Spatrick if (other.size > cast<CommonSymbol>(this)->size)
670ece8a530Spatrick cast<CommonSymbol>(this)->size = other.size;
671ece8a530Spatrick return;
672ece8a530Spatrick }
67305edf1c1Srobert if (visibility() == STV_DEFAULT && (isUndefined() || isLazy())) {
674ece8a530Spatrick // An undefined symbol with non default visibility must be satisfied
675ece8a530Spatrick // in the same DSO.
676ece8a530Spatrick uint8_t bind = binding;
67705edf1c1Srobert other.overwrite(*this);
678ece8a530Spatrick binding = bind;
679bb684c34Spatrick } else if (traced)
68005edf1c1Srobert printTraceSymbol(other, getName());
681ece8a530Spatrick }
682