1 //===- Symbols.cpp --------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "InputFiles.h"
11 #include "InputSection.h"
12 #include "OutputSections.h"
13 #include "SyntheticSections.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "lld/Common/Strings.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Path.h"
24 using namespace llvm::object;
25 using namespace llvm::ELF;
27 using namespace lld::elf;
29 // Returns a symbol for an error message.
30 static std::string demangle(StringRef symName) {
31 if (elf::config->demangle)
32 return demangleItanium(symName);
33 return std::string(symName);
36 std::string lld::toString(const elf::Symbol &sym) {
37 StringRef name = sym.getName();
38 std::string ret = demangle(name);
40 const char *suffix = sym.getVersionSuffix();
46 std::string lld::toELFString(const Archive::Symbol &b) {
47 return demangle(b.getName());
51 Defined *ElfSym::etext1;
52 Defined *ElfSym::etext2;
53 Defined *ElfSym::edata1;
54 Defined *ElfSym::edata2;
55 Defined *ElfSym::end1;
56 Defined *ElfSym::end2;
57 Defined *ElfSym::globalOffsetTable;
58 Defined *ElfSym::mipsGp;
59 Defined *ElfSym::mipsGpDisp;
60 Defined *ElfSym::mipsLocalGp;
61 Defined *ElfSym::relaIpltStart;
62 Defined *ElfSym::relaIpltEnd;
63 Defined *ElfSym::riscvGlobalPointer;
64 Defined *ElfSym::tlsModuleBase;
65 DenseMap<const Symbol *, std::pair<const InputFile *, const InputFile *>>
66 elf::backwardReferences;
68 static uint64_t getSymVA(const Symbol &sym, int64_t &addend) {
70 case Symbol::DefinedKind: {
71 auto &d = cast<Defined>(sym);
72 SectionBase *isec = d.section;
74 // This is an absolute symbol.
78 assert(isec != &InputSection::discarded);
81 uint64_t offset = d.value;
83 // An object in an SHF_MERGE section might be referenced via a
84 // section symbol (as a hack for reducing the number of local
86 // Depending on the addend, the reference via a section symbol
87 // refers to a different object in the merge section.
88 // Since the objects in the merge section are not necessarily
89 // contiguous in the output, the addend can thus affect the final
90 // VA in a non-linear way.
91 // To make this work, we incorporate the addend into the section
92 // offset (and zero out the addend for later processing) so that
93 // we find the right object in the section.
99 // In the typical case, this is actually very simple and boils
100 // down to adding together 3 numbers:
101 // 1. The address of the output section.
102 // 2. The offset of the input section within the output section.
103 // 3. The offset within the input section (this addition happens
104 // inside InputSection::getOffset).
106 // If you understand the data structures involved with this next
107 // line (and how they get built), then you have a pretty good
108 // understanding of the linker.
109 uint64_t va = isec->getVA(offset);
111 // MIPS relocatable files can mix regular and microMIPS code.
112 // Linker needs to distinguish such code. To do so microMIPS
113 // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
114 // field. Unfortunately, the `MIPS::relocate()` method has
115 // a symbol value only. To pass type of the symbol (regular/microMIPS)
116 // to that routine as well as other places where we write
117 // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
118 // field etc) do the same trick as compiler uses to mark microMIPS
119 // for CPU - set the less-significant bit.
120 if (config->emachine == EM_MIPS && isMicroMips() &&
121 ((sym.stOther & STO_MIPS_MICROMIPS) || sym.needsPltAddr))
124 if (d.isTls() && !config->relocatable) {
125 // Use the address of the TLS segment's first section rather than the
126 // segment's address, because segment addresses aren't initialized until
127 // after sections are finalized. (e.g. Measuring the size of .rela.dyn
128 // for Android relocation packing requires knowing TLS symbol addresses
129 // during section finalization.)
130 if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec)
131 fatal(toString(d.file) +
132 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
133 return va - Out::tlsPhdr->firstSec->addr;
137 case Symbol::SharedKind:
138 case Symbol::UndefinedKind:
140 case Symbol::LazyArchiveKind:
141 case Symbol::LazyObjectKind:
142 assert(sym.isUsedInRegularObj && "lazy symbol reached writer");
144 case Symbol::CommonKind:
145 llvm_unreachable("common symbol reached writer");
146 case Symbol::PlaceholderKind:
147 llvm_unreachable("placeholder symbol reached writer");
149 llvm_unreachable("invalid symbol kind");
152 uint64_t Symbol::getVA(int64_t addend) const {
153 uint64_t outVA = getSymVA(*this, addend);
154 return outVA + addend;
157 uint64_t Symbol::getGotVA() const {
159 return in.igotPlt->getVA() + getGotPltOffset();
160 return in.got->getVA() + getGotOffset();
163 uint64_t Symbol::getGotOffset() const {
164 return gotIndex * target->gotEntrySize;
167 uint64_t Symbol::getGotPltVA() const {
169 return in.igotPlt->getVA() + getGotPltOffset();
170 return in.gotPlt->getVA() + getGotPltOffset();
173 uint64_t Symbol::getGotPltOffset() const {
175 return pltIndex * target->gotEntrySize;
176 return (pltIndex + target->gotPltHeaderEntriesNum) * target->gotEntrySize;
179 uint64_t Symbol::getPltVA() const {
180 uint64_t outVA = isInIplt
181 ? in.iplt->getVA() + pltIndex * target->ipltEntrySize
182 : in.plt->getVA() + in.plt->headerSize +
183 pltIndex * target->pltEntrySize;
185 // While linking microMIPS code PLT code are always microMIPS
186 // code. Set the less-significant bit to track that fact.
187 // See detailed comment in the `getSymVA` function.
188 if (config->emachine == EM_MIPS && isMicroMips())
193 uint64_t Symbol::getSize() const {
194 if (const auto *dr = dyn_cast<Defined>(this))
196 return cast<SharedSymbol>(this)->size;
199 OutputSection *Symbol::getOutputSection() const {
200 if (auto *s = dyn_cast<Defined>(this)) {
201 if (auto *sec = s->section)
202 return sec->repl->getOutputSection();
208 // If a symbol name contains '@', the characters after that is
209 // a symbol version name. This function parses that.
210 void Symbol::parseSymbolVersion() {
211 // Return if localized by a local: pattern in a version script.
212 if (versionId == VER_NDX_LOCAL)
214 StringRef s = getName();
215 size_t pos = s.find('@');
216 if (pos == 0 || pos == StringRef::npos)
218 StringRef verstr = s.substr(pos + 1);
222 // Truncate the symbol name so that it doesn't include the version string.
225 // If this is not in this DSO, it is not a definition.
229 // '@@' in a symbol name means the default version.
230 // It is usually the most recent one.
231 bool isDefault = (verstr[0] == '@');
233 verstr = verstr.substr(1);
235 for (const VersionDefinition &ver : namedVersionDefs()) {
236 if (ver.name != verstr)
242 versionId = ver.id | VERSYM_HIDDEN;
246 // It is an error if the specified version is not defined.
247 // Usually version script is not provided when linking executable,
248 // but we may still want to override a versioned symbol from DSO,
249 // so we do not report error in this case. We also do not error
250 // if the symbol has a local version as it won't be in the dynamic
252 if (config->shared && versionId != VER_NDX_LOCAL)
253 error(toString(file) + ": symbol " + s + " has undefined version " +
257 void Symbol::fetch() const {
258 if (auto *sym = dyn_cast<LazyArchive>(this)) {
259 cast<ArchiveFile>(sym->file)->fetch(sym->sym);
263 if (auto *sym = dyn_cast<LazyObject>(this)) {
264 dyn_cast<LazyObjFile>(sym->file)->fetch();
268 llvm_unreachable("Symbol::fetch() is called on a non-lazy symbol");
271 MemoryBufferRef LazyArchive::getMemberBuffer() {
273 CHECK(sym.getMember(),
274 "could not get the member for symbol " + toELFString(sym));
276 return CHECK(c.getMemoryBufferRef(),
277 "could not get the buffer for the member defining symbol " +
281 uint8_t Symbol::computeBinding() const {
282 if (config->relocatable)
284 if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) ||
285 (versionId == VER_NDX_LOCAL && !isLazy()))
287 if (!config->gnuUnique && binding == STB_GNU_UNIQUE)
292 bool Symbol::includeInDynsym() const {
293 if (!config->hasDynSymTab)
295 if (computeBinding() == STB_LOCAL)
297 if (!isDefined() && !isCommon())
298 // This should unconditionally return true, unfortunately glibc -static-pie
299 // expects undefined weak symbols not to exist in .dynsym, e.g.
300 // __pthread_mutex_lock reference in _dl_add_to_namespace_list,
301 // __pthread_initialize_minimal reference in csu/libc-start.c.
302 return !(config->noDynamicLinker && isUndefWeak());
304 return exportDynamic || inDynamicList;
307 // Print out a log message for --trace-symbol.
308 void elf::printTraceSymbol(const Symbol *sym) {
310 if (sym->isUndefined())
311 s = ": reference to ";
312 else if (sym->isLazy())
313 s = ": lazy definition of ";
314 else if (sym->isShared())
315 s = ": shared definition of ";
316 else if (sym->isCommon())
317 s = ": common definition of ";
319 s = ": definition of ";
321 message(toString(sym->file) + s + sym->getName());
324 void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
325 if (!config->warnSymbolOrdering)
328 // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
329 // is emitted. It makes sense to not warn on undefined symbols.
331 // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
332 // but we don't have to be compatible here.
333 if (sym->isUndefined() &&
334 config->unresolvedSymbols == UnresolvedPolicy::Ignore)
337 const InputFile *file = sym->file;
338 auto *d = dyn_cast<Defined>(sym);
340 auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); };
342 if (sym->isUndefined())
343 report(": unable to order undefined symbol: ");
344 else if (sym->isShared())
345 report(": unable to order shared symbol: ");
346 else if (d && !d->section)
347 report(": unable to order absolute symbol: ");
348 else if (d && isa<OutputSection>(d->section))
349 report(": unable to order synthetic symbol: ");
350 else if (d && !d->section->repl->isLive())
351 report(": unable to order discarded symbol: ");
354 // Returns true if a symbol can be replaced at load-time by a symbol
355 // with the same name defined in other ELF executable or DSO.
356 bool elf::computeIsPreemptible(const Symbol &sym) {
357 assert(!sym.isLocal());
359 // Only symbols with default visibility that appear in dynsym can be
360 // preempted. Symbols with protected visibility cannot be preempted.
361 if (!sym.includeInDynsym() || sym.visibility != STV_DEFAULT)
364 // At this point copy relocations have not been created yet, so any
365 // symbol that is not defined locally is preemptible.
366 if (!sym.isDefined())
372 // If -Bsymbolic or --dynamic-list is specified, or -Bsymbolic-functions is
373 // specified and the symbol is STT_FUNC, the symbol is preemptible iff it is
374 // in the dynamic list. -Bsymbolic-non-weak-functions is a non-weak subset of
375 // -Bsymbolic-functions.
376 if (config->symbolic ||
377 (config->bsymbolic == BsymbolicKind::Functions && sym.isFunc()) ||
378 (config->bsymbolic == BsymbolicKind::NonWeakFunctions && sym.isFunc() &&
379 sym.binding != STB_WEAK))
380 return sym.inDynamicList;
384 void elf::reportBackrefs() {
385 for (auto &it : backwardReferences) {
386 const Symbol &sym = *it.first;
387 std::string to = toString(it.second.second);
388 // Some libraries have known problems and can cause noise. Filter them out
389 // with --warn-backrefs-exclude=. to may look like *.o or *.a(*.o).
390 bool exclude = false;
391 for (const llvm::GlobPattern &pat : config->warnBackrefsExclude)
397 warn("backward reference detected: " + sym.getName() + " in " +
398 toString(it.second.first) + " refers to " + to);
402 static uint8_t getMinVisibility(uint8_t va, uint8_t vb) {
403 if (va == STV_DEFAULT)
405 if (vb == STV_DEFAULT)
407 return std::min(va, vb);
410 // Merge symbol properties.
412 // When we have many symbols of the same name, we choose one of them,
413 // and that's the result of symbol resolution. However, symbols that
414 // were not chosen still affect some symbol properties.
415 void Symbol::mergeProperties(const Symbol &other) {
416 if (other.exportDynamic)
417 exportDynamic = true;
418 if (other.isUsedInRegularObj)
419 isUsedInRegularObj = true;
421 // DSO symbols do not affect visibility in the output.
422 if (!other.isShared())
423 visibility = getMinVisibility(visibility, other.visibility);
426 void Symbol::resolve(const Symbol &other) {
427 mergeProperties(other);
429 if (isPlaceholder()) {
434 switch (other.kind()) {
435 case Symbol::UndefinedKind:
436 resolveUndefined(cast<Undefined>(other));
438 case Symbol::CommonKind:
439 resolveCommon(cast<CommonSymbol>(other));
441 case Symbol::DefinedKind:
442 resolveDefined(cast<Defined>(other));
444 case Symbol::LazyArchiveKind:
445 resolveLazy(cast<LazyArchive>(other));
447 case Symbol::LazyObjectKind:
448 resolveLazy(cast<LazyObject>(other));
450 case Symbol::SharedKind:
451 resolveShared(cast<SharedSymbol>(other));
453 case Symbol::PlaceholderKind:
454 llvm_unreachable("bad symbol kind");
458 void Symbol::resolveUndefined(const Undefined &other) {
459 // An undefined symbol with non default visibility must be satisfied
462 // If this is a non-weak defined symbol in a discarded section, override the
463 // existing undefined symbol for better error message later.
464 if ((isShared() && other.visibility != STV_DEFAULT) ||
465 (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) {
471 printTraceSymbol(&other);
474 // An undefined weak will not fetch archive members. See comment on Lazy in
475 // Symbols.h for the details.
476 if (other.binding == STB_WEAK) {
482 // Do extra check for --warn-backrefs.
484 // --warn-backrefs is an option to prevent an undefined reference from
485 // fetching an archive member written earlier in the command line. It can be
486 // used to keep compatibility with GNU linkers to some degree.
487 // I'll explain the feature and why you may find it useful in this comment.
489 // lld's symbol resolution semantics is more relaxed than traditional Unix
490 // linkers. For example,
492 // ld.lld foo.a bar.o
494 // succeeds even if bar.o contains an undefined symbol that has to be
495 // resolved by some object file in foo.a. Traditional Unix linkers don't
496 // allow this kind of backward reference, as they visit each file only once
497 // from left to right in the command line while resolving all undefined
498 // symbols at the moment of visiting.
500 // In the above case, since there's no undefined symbol when a linker visits
501 // foo.a, no files are pulled out from foo.a, and because the linker forgets
502 // about foo.a after visiting, it can't resolve undefined symbols in bar.o
503 // that could have been resolved otherwise.
505 // That lld accepts more relaxed form means that (besides it'd make more
506 // sense) you can accidentally write a command line or a build file that
507 // works only with lld, even if you have a plan to distribute it to wider
508 // users who may be using GNU linkers. With --warn-backrefs, you can detect
509 // a library order that doesn't work with other Unix linkers.
511 // The option is also useful to detect cyclic dependencies between static
512 // archives. Again, lld accepts
514 // ld.lld foo.a bar.a
516 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
517 // handled as an error.
519 // Here is how the option works. We assign a group ID to each file. A file
520 // with a smaller group ID can pull out object files from an archive file
521 // with an equal or greater group ID. Otherwise, it is a reverse dependency
524 // A file outside --{start,end}-group gets a fresh ID when instantiated. All
525 // files within the same --{start,end}-group get the same group ID. E.g.
527 // ld.lld A B --start-group C D --end-group E
529 // A forms group 0. B form group 1. C and D (including their member object
530 // files) form group 2. E forms group 3. I think that you can see how this
531 // group assignment rule simulates the traditional linker's semantics.
532 bool backref = config->warnBackrefs && other.file &&
533 file->groupId < other.file->groupId;
536 // We don't report backward references to weak symbols as they can be
539 // A traditional linker does not error for -ldef1 -lref -ldef2 (linking
540 // sandwich), where def2 may or may not be the same as def1. We don't want
541 // to warn for this case, so dismiss the warning if we see a subsequent lazy
542 // definition. this->file needs to be saved because in the case of LTO it
543 // may be reset to nullptr or be replaced with a file named lto.tmp.
544 if (backref && !isWeak())
545 backwardReferences.try_emplace(this, std::make_pair(other.file, file));
549 // Undefined symbols in a SharedFile do not change the binding.
550 if (dyn_cast_or_null<SharedFile>(other.file))
553 if (isUndefined() || isShared()) {
554 // The binding will be weak if there is at least one reference and all are
555 // weak. The binding has one opportunity to change to weak: if the first
556 // reference is weak.
557 if (other.binding != STB_WEAK || !referenced)
558 binding = other.binding;
562 // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
563 // foo@@VER. We want to effectively ignore foo, so give precedence to
565 // FIXME: If users can transition to using
566 // .symver foo,foo@@@VER
567 // we can delete this hack.
568 static int compareVersion(StringRef a, StringRef b) {
569 bool x = a.contains("@@");
570 bool y = b.contains("@@");
578 // Compare two symbols. Return 1 if the new symbol should win, -1 if
579 // the new symbol should lose, or 0 if there is a conflict.
580 int Symbol::compare(const Symbol *other) const {
581 assert(other->isDefined() || other->isCommon());
583 if (!isDefined() && !isCommon())
586 if (int cmp = compareVersion(getName(), other->getName()))
595 if (isCommon() && other->isCommon()) {
596 if (config->warnCommon)
597 warn("multiple common of " + getName());
602 if (config->warnCommon)
603 warn("common " + getName() + " is overridden");
607 if (other->isCommon()) {
608 if (config->warnCommon)
609 warn("common " + getName() + " is overridden");
613 auto *oldSym = cast<Defined>(this);
614 auto *newSym = cast<Defined>(other);
616 if (dyn_cast_or_null<BitcodeFile>(other->file))
619 if (!oldSym->section && !newSym->section && oldSym->value == newSym->value &&
620 newSym->binding == STB_GLOBAL)
626 static void reportDuplicate(Symbol *sym, InputFile *newFile,
627 InputSectionBase *errSec, uint64_t errOffset) {
628 if (config->allowMultipleDefinition)
631 Defined *d = cast<Defined>(sym);
632 if (!d->section || !errSec) {
633 error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " +
634 toString(sym->file) + "\n>>> defined in " + toString(newFile));
638 // Construct and print an error message in the form of:
640 // ld.lld: error: duplicate symbol: foo
641 // >>> defined at bar.c:30
642 // >>> bar.o (/home/alice/src/bar.o)
643 // >>> defined at baz.c:563
644 // >>> baz.o in archive libbaz.a
645 auto *sec1 = cast<InputSectionBase>(d->section);
646 std::string src1 = sec1->getSrcMsg(*sym, d->value);
647 std::string obj1 = sec1->getObjMsg(d->value);
648 std::string src2 = errSec->getSrcMsg(*sym, errOffset);
649 std::string obj2 = errSec->getObjMsg(errOffset);
651 std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at ";
653 msg += src1 + "\n>>> ";
654 msg += obj1 + "\n>>> defined at ";
656 msg += src2 + "\n>>> ";
661 void Symbol::resolveCommon(const CommonSymbol &other) {
662 int cmp = compare(&other);
667 if (auto *s = dyn_cast<SharedSymbol>(this)) {
668 // Increase st_size if the shared symbol has a larger st_size. The shared
669 // symbol may be created from common symbols. The fact that some object
670 // files were linked into a shared object first should not change the
671 // regular rule that picks the largest st_size.
672 uint64_t size = s->size;
674 if (size > cast<CommonSymbol>(this)->size)
675 cast<CommonSymbol>(this)->size = size;
682 CommonSymbol *oldSym = cast<CommonSymbol>(this);
684 oldSym->alignment = std::max(oldSym->alignment, other.alignment);
685 if (oldSym->size < other.size) {
686 oldSym->file = other.file;
687 oldSym->size = other.size;
691 void Symbol::resolveDefined(const Defined &other) {
692 int cmp = compare(&other);
696 reportDuplicate(this, other.file,
697 dyn_cast_or_null<InputSectionBase>(other.section),
701 template <class LazyT>
702 static void replaceCommon(Symbol &oldSym, const LazyT &newSym) {
703 backwardReferences.erase(&oldSym);
704 oldSym.replace(newSym);
708 template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
709 // For common objects, we want to look for global or weak definitions that
710 // should be fetched as the canonical definition instead.
711 if (isCommon() && elf::config->fortranCommon) {
712 if (auto *laSym = dyn_cast<LazyArchive>(&other)) {
713 ArchiveFile *archive = cast<ArchiveFile>(laSym->file);
714 const Archive::Symbol &archiveSym = laSym->sym;
715 if (archive->shouldFetchForCommon(archiveSym)) {
716 replaceCommon(*this, other);
719 } else if (auto *loSym = dyn_cast<LazyObject>(&other)) {
720 LazyObjFile *obj = cast<LazyObjFile>(loSym->file);
721 if (obj->shouldFetchForCommon(loSym->getName())) {
722 replaceCommon(*this, other);
728 if (!isUndefined()) {
729 // See the comment in resolveUndefined().
731 backwardReferences.erase(this);
735 // An undefined weak will not fetch archive members. See comment on Lazy in
736 // Symbols.h for the details.
748 void Symbol::resolveShared(const SharedSymbol &other) {
750 // See the comment in resolveCommon() above.
751 if (other.size > cast<CommonSymbol>(this)->size)
752 cast<CommonSymbol>(this)->size = other.size;
755 if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) {
756 // An undefined symbol with non default visibility must be satisfied
758 uint8_t bind = binding;
762 printTraceSymbol(&other);