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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of lld/ELF/Symbols.cpp