blob: b087dd1823fd3b9c8ec40d06bb19a3bf8cd6c62b [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:071//===- Symbols.cpp --------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:563// 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
Michael J. Spencer84487f12015-07-24 21:03:076//
7//===----------------------------------------------------------------------===//
8
9#include "Symbols.h"
Fangrui Song7fd38492022-02-27 20:23:0910#include "Driver.h"
Michael J. Spencercdae0a42015-07-28 22:58:2511#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:3512#include "InputSection.h"
13#include "OutputSections.h"
Rui Ueyamae8a61022016-11-05 23:05:4714#include "SyntheticSections.h"
Rui Ueyamab5a69702016-02-01 21:00:3515#include "Target.h"
Rafael Espindola17cb7c02016-12-19 17:01:0116#include "Writer.h"
Bob Haarmanb8a59c82017-10-25 22:28:3817#include "lld/Common/ErrorHandler.h"
Rui Ueyama53fe4692017-11-28 02:15:2618#include "lld/Common/Strings.h"
Rui Ueyamac72ba3a2016-11-23 04:57:2519#include <cstring>
Michael J. Spencer1b348a62015-09-04 22:28:1020
21using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0722using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5223using namespace llvm::ELF;
Fangrui Song07837b82020-05-15 05:18:5824using namespace lld;
25using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:0726
Fangrui Song07837b82020-05-15 05:18:5827std::string lld::toString(const elf::Symbol &sym) {
Fangrui Songf2036a12020-03-28 22:48:3828 StringRef name = sym.getName();
Luís Ferreira10e40a42022-01-05 03:05:1029 std::string ret = demangle(name, config->demangle);
Fangrui Songf2036a12020-03-28 22:48:3830
Fangrui Song941e9332020-12-01 16:54:0131 const char *suffix = sym.getVersionSuffix();
32 if (*suffix == '@')
33 ret += suffix;
Fangrui Songf2036a12020-03-28 22:48:3834 return ret;
35}
36
Rui Ueyama3837f422019-07-10 05:00:3737Defined *ElfSym::bss;
38Defined *ElfSym::etext1;
39Defined *ElfSym::etext2;
40Defined *ElfSym::edata1;
41Defined *ElfSym::edata2;
42Defined *ElfSym::end1;
43Defined *ElfSym::end2;
44Defined *ElfSym::globalOffsetTable;
45Defined *ElfSym::mipsGp;
46Defined *ElfSym::mipsGpDisp;
47Defined *ElfSym::mipsLocalGp;
48Defined *ElfSym::relaIpltStart;
49Defined *ElfSym::relaIpltEnd;
50Defined *ElfSym::riscvGlobalPointer;
51Defined *ElfSym::tlsModuleBase;
Fangrui Song5d3bd7f2022-01-09 21:43:2652SmallVector<SymbolAux, 0> elf::symAux;
Rui Ueyama80474a22017-02-28 19:29:5553
Fangrui Songa8024df2021-12-13 03:12:0154static uint64_t getSymVA(const Symbol &sym, int64_t addend) {
Rui Ueyama3837f422019-07-10 05:00:3755 switch (sym.kind()) {
Peter Collingbournee9a9e0a2017-11-06 04:35:3156 case Symbol::DefinedKind: {
Rui Ueyama3837f422019-07-10 05:00:3757 auto &d = cast<Defined>(sym);
58 SectionBase *isec = d.section;
Rui Ueyamab5a69702016-02-01 21:00:3559
60 // This is an absolute symbol.
Rui Ueyama3837f422019-07-10 05:00:3761 if (!isec)
62 return d.value;
Rui Ueyamab5a69702016-02-01 21:00:3563
Rui Ueyama3837f422019-07-10 05:00:3764 assert(isec != &InputSection::discarded);
Rafael Espindolaf4d6e8c2018-04-19 17:26:5065
Rui Ueyama3837f422019-07-10 05:00:3766 uint64_t offset = d.value;
Sean Silvaa9ba4502017-02-28 08:32:5667
68 // An object in an SHF_MERGE section might be referenced via a
69 // section symbol (as a hack for reducing the number of local
70 // symbols).
Sean Silvad4e60622017-03-01 04:44:0471 // Depending on the addend, the reference via a section symbol
72 // refers to a different object in the merge section.
73 // Since the objects in the merge section are not necessarily
74 // contiguous in the output, the addend can thus affect the final
75 // VA in a non-linear way.
76 // To make this work, we incorporate the addend into the section
77 // offset (and zero out the addend for later processing) so that
78 // we find the right object in the section.
Fangrui Songa8024df2021-12-13 03:12:0179 if (d.isSection())
Rui Ueyama3837f422019-07-10 05:00:3780 offset += addend;
Sean Silvaa9ba4502017-02-28 08:32:5681
Sean Silva6ab39262017-02-28 09:01:5882 // In the typical case, this is actually very simple and boils
83 // down to adding together 3 numbers:
84 // 1. The address of the output section.
85 // 2. The offset of the input section within the output section.
86 // 3. The offset within the input section (this addition happens
87 // inside InputSection::getOffset).
88 //
89 // If you understand the data structures involved with this next
90 // line (and how they get built), then you have a pretty good
91 // understanding of the linker.
Rui Ueyama3837f422019-07-10 05:00:3792 uint64_t va = isec->getVA(offset);
Fangrui Songa8024df2021-12-13 03:12:0193 if (d.isSection())
94 va -= addend;
Sean Silva6ab39262017-02-28 09:01:5895
Simon Atanasyanfae2a5092019-02-19 10:36:5896 // MIPS relocatable files can mix regular and microMIPS code.
97 // Linker needs to distinguish such code. To do so microMIPS
98 // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
Fangrui Songdeb58192020-01-23 05:39:1699 // field. Unfortunately, the `MIPS::relocate()` method has
Simon Atanasyanfae2a5092019-02-19 10:36:58100 // a symbol value only. To pass type of the symbol (regular/microMIPS)
101 // to that routine as well as other places where we write
102 // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
103 // field etc) do the same trick as compiler uses to mark microMIPS
104 // for CPU - set the less-significant bit.
Rui Ueyama3837f422019-07-10 05:00:37105 if (config->emachine == EM_MIPS && isMicroMips() &&
Fangrui Songcf783be2021-12-15 00:28:41106 ((sym.stOther & STO_MIPS_MICROMIPS) || sym.needsCopy))
Rui Ueyama3837f422019-07-10 05:00:37107 va |= 1;
Simon Atanasyanfae2a5092019-02-19 10:36:58108
Rui Ueyama3837f422019-07-10 05:00:37109 if (d.isTls() && !config->relocatable) {
Ryan Prichard1c33d142018-09-18 00:24:48110 // Use the address of the TLS segment's first section rather than the
111 // segment's address, because segment addresses aren't initialized until
112 // after sections are finalized. (e.g. Measuring the size of .rela.dyn
113 // for Android relocation packing requires knowing TLS symbol addresses
114 // during section finalization.)
Rui Ueyama3837f422019-07-10 05:00:37115 if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec)
116 fatal(toString(d.file) +
Peter Collingbourne3e2abde2017-07-14 00:22:46117 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Rui Ueyama3837f422019-07-10 05:00:37118 return va - Out::tlsPhdr->firstSec->addr;
George Rimar6a3b1542016-10-04 08:52:51119 }
Rui Ueyama3837f422019-07-10 05:00:37120 return va;
Rui Ueyamab5a69702016-02-01 21:00:35121 }
Rafael Espindolaab0cce52018-04-26 17:58:58122 case Symbol::SharedKind:
Rui Ueyamaf52496e2017-11-03 21:21:47123 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35124 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47125 case Symbol::LazyObjectKind:
Fangrui Song935229f2022-01-05 08:46:48126 llvm_unreachable("lazy symbol reached writer");
Rui Ueyama5c073a92019-05-16 03:29:03127 case Symbol::CommonKind:
128 llvm_unreachable("common symbol reached writer");
Rui Ueyamaf3fad552018-10-12 18:29:18129 case Symbol::PlaceholderKind:
130 llvm_unreachable("placeholder symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35131 }
George Rimar777f9632016-03-12 08:31:34132 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35133}
134
Rui Ueyama3837f422019-07-10 05:00:37135uint64_t Symbol::getVA(int64_t addend) const {
Fangrui Songa8024df2021-12-13 03:12:01136 return getSymVA(*this, addend) + addend;
Rafael Espindola87d9f102016-03-11 12:19:05137}
138
Peter Collingbourne8331f612019-02-13 21:49:55139uint64_t Symbol::getGotVA() const {
Rui Ueyama3837f422019-07-10 05:00:37140 if (gotInIgot)
141 return in.igotPlt->getVA() + getGotPltOffset();
142 return in.got->getVA() + getGotOffset();
Peter Collingbourne8331f612019-02-13 21:49:55143}
Rafael Espindola74031ba2016-04-07 15:20:56144
Harald van Dijkd6241342021-05-16 23:13:00145uint64_t Symbol::getGotOffset() const {
Fangrui Song5d3bd7f2022-01-09 21:43:26146 return getGotIdx() * target->gotEntrySize;
Harald van Dijkd6241342021-05-16 23:13:00147}
Rui Ueyamab5a69702016-02-01 21:00:35148
Rui Ueyamaf52496e2017-11-03 21:21:47149uint64_t Symbol::getGotPltVA() const {
Rui Ueyama3837f422019-07-10 05:00:37150 if (isInIplt)
151 return in.igotPlt->getVA() + getGotPltOffset();
152 return in.gotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56153}
154
Rui Ueyamaf52496e2017-11-03 21:21:47155uint64_t Symbol::getGotPltOffset() const {
Rui Ueyama3837f422019-07-10 05:00:37156 if (isInIplt)
Fangrui Song5d3bd7f2022-01-09 21:43:26157 return getPltIdx() * target->gotEntrySize;
158 return (getPltIdx() + target->gotPltHeaderEntriesNum) * target->gotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35159}
160
Rui Ueyamaf52496e2017-11-03 21:21:47161uint64_t Symbol::getPltVA() const {
Fangrui Song891a8652019-12-14 22:17:35162 uint64_t outVA = isInIplt
Fangrui Song5d3bd7f2022-01-09 21:43:26163 ? in.iplt->getVA() + getPltIdx() * target->ipltEntrySize
Fangrui Song891a8652019-12-14 22:17:35164 : in.plt->getVA() + in.plt->headerSize +
Fangrui Song5d3bd7f2022-01-09 21:43:26165 getPltIdx() * target->pltEntrySize;
Fangrui Song891a8652019-12-14 22:17:35166
Simon Atanasyanfae2a5092019-02-19 10:36:58167 // While linking microMIPS code PLT code are always microMIPS
168 // code. Set the less-significant bit to track that fact.
169 // See detailed comment in the `getSymVA` function.
Rui Ueyama3837f422019-07-10 05:00:37170 if (config->emachine == EM_MIPS && isMicroMips())
171 outVA |= 1;
172 return outVA;
Rafael Espindolaab0cce52018-04-26 17:58:58173}
174
Rui Ueyamaf52496e2017-11-03 21:21:47175uint64_t Symbol::getSize() const {
Rui Ueyama3837f422019-07-10 05:00:37176 if (const auto *dr = dyn_cast<Defined>(this))
177 return dr->size;
178 return cast<SharedSymbol>(this)->size;
Rui Ueyama512c61d2016-02-03 00:12:24179}
180
Rui Ueyamaf52496e2017-11-03 21:21:47181OutputSection *Symbol::getOutputSection() const {
Rui Ueyama3837f422019-07-10 05:00:37182 if (auto *s = dyn_cast<Defined>(this)) {
183 if (auto *sec = s->section)
Fangrui Songc2f2bb02021-12-21 08:39:16184 return sec->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42185 return nullptr;
186 }
Rui Ueyama968db482017-02-28 04:02:42187 return nullptr;
188}
189
Rui Ueyama35fa6c52016-11-23 05:48:40190// If a symbol name contains '@', the characters after that is
191// a symbol version name. This function parses that.
Rui Ueyamaf52496e2017-11-03 21:21:47192void Symbol::parseSymbolVersion() {
Fangrui Song00809c82021-08-05 06:52:55193 // Return if localized by a local: pattern in a version script.
194 if (versionId == VER_NDX_LOCAL)
195 return;
Rui Ueyama3837f422019-07-10 05:00:37196 StringRef s = getName();
197 size_t pos = s.find('@');
Fangrui Song101407b2021-12-16 07:59:55198 if (pos == StringRef::npos)
Rui Ueyama35fa6c52016-11-23 05:48:40199 return;
Rui Ueyama3837f422019-07-10 05:00:37200 StringRef verstr = s.substr(pos + 1);
Rui Ueyama35fa6c52016-11-23 05:48:40201
202 // Truncate the symbol name so that it doesn't include the version string.
Rui Ueyama3837f422019-07-10 05:00:37203 nameSize = pos;
Rui Ueyama35fa6c52016-11-23 05:48:40204
Fangrui Song7924b382021-12-27 01:25:54205 if (verstr.empty())
206 return;
207
Rafael Espindola1d6d1b42017-01-17 16:08:06208 // If this is not in this DSO, it is not a definition.
Peter Collingbourneb472aa02017-11-06 04:39:07209 if (!isDefined())
Rafael Espindola2756e042017-01-06 22:30:35210 return;
211
Rui Ueyama35fa6c52016-11-23 05:48:40212 // '@@' in a symbol name means the default version.
213 // It is usually the most recent one.
Rui Ueyama3837f422019-07-10 05:00:37214 bool isDefault = (verstr[0] == '@');
215 if (isDefault)
216 verstr = verstr.substr(1);
Rui Ueyama35fa6c52016-11-23 05:48:40217
Fangrui Songe28a70d2019-08-05 14:31:39218 for (const VersionDefinition &ver : namedVersionDefs()) {
Rui Ueyama3837f422019-07-10 05:00:37219 if (ver.name != verstr)
Rui Ueyama35fa6c52016-11-23 05:48:40220 continue;
221
Rui Ueyama3837f422019-07-10 05:00:37222 if (isDefault)
223 versionId = ver.id;
Rui Ueyama35fa6c52016-11-23 05:48:40224 else
Rui Ueyama3837f422019-07-10 05:00:37225 versionId = ver.id | VERSYM_HIDDEN;
Rui Ueyama35fa6c52016-11-23 05:48:40226 return;
227 }
228
229 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13230 // Usually version script is not provided when linking executable,
231 // but we may still want to override a versioned symbol from DSO,
Peter Smith796fb992018-05-14 10:13:56232 // so we do not report error in this case. We also do not error
233 // if the symbol has a local version as it won't be in the dynamic
234 // symbol table.
Rui Ueyama3837f422019-07-10 05:00:37235 if (config->shared && versionId != VER_NDX_LOCAL)
236 error(toString(file) + ": symbol " + s + " has undefined version " +
237 verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40238}
239
Fangrui Song09401df2021-11-26 18:58:50240void Symbol::extract() const {
Fangrui Song3d854242022-02-15 17:38:00241 if (file->lazy) {
Fangrui Song3a5fb572021-12-23 01:41:50242 file->lazy = false;
243 parseFile(file);
244 }
Rui Ueyama7d476192019-05-16 02:14:00245}
Rui Ueyamaf8baa662016-04-07 19:24:51246
Rui Ueyamaf52496e2017-11-03 21:21:47247uint8_t Symbol::computeBinding() const {
Fangrui Songcfdd4582019-08-11 17:03:00248 if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) ||
Fangrui Song9e885ea2022-01-16 07:58:15249 versionId == VER_NDX_LOCAL)
Rafael Espindolab7e2ee22017-01-10 17:08:13250 return STB_LOCAL;
Fangrui Songb3cc4702022-01-16 07:40:43251 if (binding == STB_GNU_UNIQUE && !config->gnuUnique)
Rafael Espindolab7e2ee22017-01-10 17:08:13252 return STB_GLOBAL;
Rui Ueyama3837f422019-07-10 05:00:37253 return binding;
Rafael Espindolab7e2ee22017-01-10 17:08:13254}
255
Rui Ueyamaf52496e2017-11-03 21:21:47256bool Symbol::includeInDynsym() const {
Rafael Espindolab7e2ee22017-01-10 17:08:13257 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25258 return false;
Fangrui Song375371c2020-01-09 23:53:52259 if (!isDefined() && !isCommon())
Fangrui Song0fbf28f2020-01-23 19:52:03260 // This should unconditionally return true, unfortunately glibc -static-pie
261 // expects undefined weak symbols not to exist in .dynsym, e.g.
262 // __pthread_mutex_lock reference in _dl_add_to_namespace_list,
263 // __pthread_initialize_minimal reference in csu/libc-start.c.
Fangrui Song01a51622022-01-16 07:32:48264 return !(isUndefWeak() && config->noDynamicLinker);
Rui Ueyamae63ae7f2019-06-25 06:58:07265
Fangrui Song375371c2020-01-09 23:53:52266 return exportDynamic || inDynamicList;
Rafael Espindolaae605c12016-04-21 20:35:25267}
Rui Ueyama69c778c2016-07-17 17:50:09268
269// Print out a log message for --trace-symbol.
Fangrui Song977a1a52022-02-06 00:34:02270void elf::printTraceSymbol(const Symbol &sym, StringRef name) {
Rui Ueyama3837f422019-07-10 05:00:37271 std::string s;
Fangrui Song977a1a52022-02-06 00:34:02272 if (sym.isUndefined())
Rui Ueyama3837f422019-07-10 05:00:37273 s = ": reference to ";
Fangrui Song977a1a52022-02-06 00:34:02274 else if (sym.isLazy())
Rui Ueyama3837f422019-07-10 05:00:37275 s = ": lazy definition of ";
Fangrui Song977a1a52022-02-06 00:34:02276 else if (sym.isShared())
Rui Ueyama3837f422019-07-10 05:00:37277 s = ": shared definition of ";
Fangrui Song977a1a52022-02-06 00:34:02278 else if (sym.isCommon())
Rui Ueyama3837f422019-07-10 05:00:37279 s = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09280 else
Rui Ueyama3837f422019-07-10 05:00:37281 s = ": definition of ";
Rui Ueyamae6e206d2017-02-21 23:22:56282
Fangrui Song977a1a52022-02-06 00:34:02283 message(toString(sym.file) + s + name);
Rui Ueyama69c778c2016-07-17 17:50:09284}
285
Fangrui Songa954bb12021-09-20 16:52:30286static void recordWhyExtract(const InputFile *reference,
287 const InputFile &extracted, const Symbol &sym) {
Fangrui Song7fd38492022-02-27 20:23:09288 driver->whyExtract.emplace_back(toString(reference), &extracted, sym);
Fangrui Songa954bb12021-09-20 16:52:30289}
290
Fangrui Song07837b82020-05-15 05:18:58291void elf::maybeWarnUnorderableSymbol(const Symbol *sym) {
Rui Ueyama3837f422019-07-10 05:00:37292 if (!config->warnSymbolOrdering)
Michael J. Spencerb8427252018-04-17 23:30:05293 return;
Rui Ueyamab774c3c2018-04-26 01:38:29294
Fangrui Song11ca54f2018-10-10 22:48:57295 // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
296 // is emitted. It makes sense to not warn on undefined symbols.
297 //
298 // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
299 // but we don't have to be compatible here.
Rui Ueyama3837f422019-07-10 05:00:37300 if (sym->isUndefined() &&
301 config->unresolvedSymbols == UnresolvedPolicy::Ignore)
Fangrui Song11ca54f2018-10-10 22:48:57302 return;
303
Rui Ueyama3837f422019-07-10 05:00:37304 const InputFile *file = sym->file;
305 auto *d = dyn_cast<Defined>(sym);
Rui Ueyamab774c3c2018-04-26 01:38:29306
Rui Ueyama3837f422019-07-10 05:00:37307 auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); };
Rui Ueyamab774c3c2018-04-26 01:38:29308
Rui Ueyama3837f422019-07-10 05:00:37309 if (sym->isUndefined())
310 report(": unable to order undefined symbol: ");
311 else if (sym->isShared())
312 report(": unable to order shared symbol: ");
313 else if (d && !d->section)
314 report(": unable to order absolute symbol: ");
315 else if (d && isa<OutputSection>(d->section))
316 report(": unable to order synthetic symbol: ");
Fangrui Songe1b6b5b2021-12-24 20:09:48317 else if (d && !d->section->isLive())
Rui Ueyama3837f422019-07-10 05:00:37318 report(": unable to order discarded symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05319}
320
Fangrui Songcd0ab242019-11-30 05:58:36321// Returns true if a symbol can be replaced at load-time by a symbol
322// with the same name defined in other ELF executable or DSO.
Fangrui Song07837b82020-05-15 05:18:58323bool elf::computeIsPreemptible(const Symbol &sym) {
Fangrui Songe9262ed2021-12-27 02:11:45324 assert(!sym.isLocal() || sym.isPlaceholder());
Fangrui Songcd0ab242019-11-30 05:58:36325
326 // Only symbols with default visibility that appear in dynsym can be
327 // preempted. Symbols with protected visibility cannot be preempted.
328 if (!sym.includeInDynsym() || sym.visibility != STV_DEFAULT)
329 return false;
330
331 // At this point copy relocations have not been created yet, so any
332 // symbol that is not defined locally is preemptible.
333 if (!sym.isDefined())
334 return true;
335
336 if (!config->shared)
337 return false;
338
Fangrui Song751f18e2020-06-01 18:27:53339 // If -Bsymbolic or --dynamic-list is specified, or -Bsymbolic-functions is
340 // specified and the symbol is STT_FUNC, the symbol is preemptible iff it is
Fangrui Songb06426d2021-07-29 21:46:53341 // in the dynamic list. -Bsymbolic-non-weak-functions is a non-weak subset of
342 // -Bsymbolic-functions.
343 if (config->symbolic ||
344 (config->bsymbolic == BsymbolicKind::Functions && sym.isFunc()) ||
345 (config->bsymbolic == BsymbolicKind::NonWeakFunctions && sym.isFunc() &&
346 sym.binding != STB_WEAK))
Fangrui Songcd0ab242019-11-30 05:58:36347 return sym.inDynamicList;
Fangrui Songcd0ab242019-11-30 05:58:36348 return true;
349}
350
Rui Ueyama3837f422019-07-10 05:00:37351static uint8_t getMinVisibility(uint8_t va, uint8_t vb) {
352 if (va == STV_DEFAULT)
353 return vb;
354 if (vb == STV_DEFAULT)
355 return va;
356 return std::min(va, vb);
Rui Ueyama7f7d2b22019-05-23 09:58:08357}
358
359// Merge symbol properties.
360//
361// When we have many symbols of the same name, we choose one of them,
362// and that's the result of symbol resolution. However, symbols that
363// were not chosen still affect some symbol properties.
Rui Ueyama3837f422019-07-10 05:00:37364void Symbol::mergeProperties(const Symbol &other) {
365 if (other.exportDynamic)
366 exportDynamic = true;
Rui Ueyama7f7d2b22019-05-23 09:58:08367
368 // DSO symbols do not affect visibility in the output.
Rui Ueyama3837f422019-07-10 05:00:37369 if (!other.isShared())
370 visibility = getMinVisibility(visibility, other.visibility);
Rui Ueyama7f7d2b22019-05-23 09:58:08371}
372
Rui Ueyama3837f422019-07-10 05:00:37373void Symbol::resolve(const Symbol &other) {
374 mergeProperties(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08375
376 if (isPlaceholder()) {
Rui Ueyama3837f422019-07-10 05:00:37377 replace(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08378 return;
379 }
380
Rui Ueyama3837f422019-07-10 05:00:37381 switch (other.kind()) {
Rui Ueyama7f7d2b22019-05-23 09:58:08382 case Symbol::UndefinedKind:
Rui Ueyama3837f422019-07-10 05:00:37383 resolveUndefined(cast<Undefined>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08384 break;
385 case Symbol::CommonKind:
Rui Ueyama3837f422019-07-10 05:00:37386 resolveCommon(cast<CommonSymbol>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08387 break;
388 case Symbol::DefinedKind:
Rui Ueyama3837f422019-07-10 05:00:37389 resolveDefined(cast<Defined>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08390 break;
Rui Ueyama7f7d2b22019-05-23 09:58:08391 case Symbol::LazyObjectKind:
Rui Ueyama3837f422019-07-10 05:00:37392 resolveLazy(cast<LazyObject>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08393 break;
394 case Symbol::SharedKind:
Rui Ueyama3837f422019-07-10 05:00:37395 resolveShared(cast<SharedSymbol>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08396 break;
397 case Symbol::PlaceholderKind:
398 llvm_unreachable("bad symbol kind");
399 }
400}
401
Rui Ueyama3837f422019-07-10 05:00:37402void Symbol::resolveUndefined(const Undefined &other) {
Rui Ueyama7f7d2b22019-05-23 09:58:08403 // An undefined symbol with non default visibility must be satisfied
404 // in the same DSO.
405 //
406 // If this is a non-weak defined symbol in a discarded section, override the
407 // existing undefined symbol for better error message later.
Rui Ueyama3837f422019-07-10 05:00:37408 if ((isShared() && other.visibility != STV_DEFAULT) ||
409 (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) {
410 replace(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08411 return;
412 }
413
Rui Ueyama3837f422019-07-10 05:00:37414 if (traced)
Fangrui Song977a1a52022-02-06 00:34:02415 printTraceSymbol(other, getName());
Sam Clegg7991b682019-05-24 13:29:17416
Fangrui Songf3475412019-07-04 10:38:04417 if (isLazy()) {
Fangrui Song09401df2021-11-26 18:58:50418 // An undefined weak will not extract archive members. See comment on Lazy
419 // in Symbols.h for the details.
Rui Ueyama3837f422019-07-10 05:00:37420 if (other.binding == STB_WEAK) {
421 binding = STB_WEAK;
422 type = other.type;
Rui Ueyama7f7d2b22019-05-23 09:58:08423 return;
424 }
425
426 // Do extra check for --warn-backrefs.
427 //
428 // --warn-backrefs is an option to prevent an undefined reference from
Fangrui Song09401df2021-11-26 18:58:50429 // extracting an archive member written earlier in the command line. It can
430 // be used to keep compatibility with GNU linkers to some degree. I'll
431 // explain the feature and why you may find it useful in this comment.
Rui Ueyama7f7d2b22019-05-23 09:58:08432 //
433 // lld's symbol resolution semantics is more relaxed than traditional Unix
434 // linkers. For example,
435 //
436 // ld.lld foo.a bar.o
437 //
438 // succeeds even if bar.o contains an undefined symbol that has to be
439 // resolved by some object file in foo.a. Traditional Unix linkers don't
440 // allow this kind of backward reference, as they visit each file only once
441 // from left to right in the command line while resolving all undefined
442 // symbols at the moment of visiting.
443 //
444 // In the above case, since there's no undefined symbol when a linker visits
445 // foo.a, no files are pulled out from foo.a, and because the linker forgets
446 // about foo.a after visiting, it can't resolve undefined symbols in bar.o
447 // that could have been resolved otherwise.
448 //
449 // That lld accepts more relaxed form means that (besides it'd make more
450 // sense) you can accidentally write a command line or a build file that
451 // works only with lld, even if you have a plan to distribute it to wider
452 // users who may be using GNU linkers. With --warn-backrefs, you can detect
453 // a library order that doesn't work with other Unix linkers.
454 //
455 // The option is also useful to detect cyclic dependencies between static
456 // archives. Again, lld accepts
457 //
458 // ld.lld foo.a bar.a
459 //
460 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
461 // handled as an error.
462 //
463 // Here is how the option works. We assign a group ID to each file. A file
464 // with a smaller group ID can pull out object files from an archive file
465 // with an equal or greater group ID. Otherwise, it is a reverse dependency
466 // and an error.
467 //
468 // A file outside --{start,end}-group gets a fresh ID when instantiated. All
469 // files within the same --{start,end}-group get the same group ID. E.g.
470 //
471 // ld.lld A B --start-group C D --end-group E
472 //
473 // A forms group 0. B form group 1. C and D (including their member object
474 // files) form group 2. E forms group 3. I think that you can see how this
475 // group assignment rule simulates the traditional linker's semantics.
Rui Ueyama3837f422019-07-10 05:00:37476 bool backref = config->warnBackrefs && other.file &&
477 file->groupId < other.file->groupId;
Fangrui Song09401df2021-11-26 18:58:50478 extract();
Rui Ueyama7f7d2b22019-05-23 09:58:08479
Fangrui Songa954bb12021-09-20 16:52:30480 if (!config->whyExtract.empty())
481 recordWhyExtract(other.file, *file, *this);
482
Rui Ueyama7f7d2b22019-05-23 09:58:08483 // We don't report backward references to weak symbols as they can be
484 // overridden later.
Fangrui Song03c825c2020-04-06 05:27:46485 //
486 // A traditional linker does not error for -ldef1 -lref -ldef2 (linking
487 // sandwich), where def2 may or may not be the same as def1. We don't want
488 // to warn for this case, so dismiss the warning if we see a subsequent lazy
Fangrui Songce3c5da2020-10-22 22:26:52489 // definition. this->file needs to be saved because in the case of LTO it
490 // may be reset to nullptr or be replaced with a file named lto.tmp.
Rui Ueyama3837f422019-07-10 05:00:37491 if (backref && !isWeak())
Fangrui Songd14d8662022-02-27 20:33:28492 driver->backwardReferences.try_emplace(this,
493 std::make_pair(other.file, file));
Fangrui Songf3475412019-07-04 10:38:04494 return;
495 }
496
497 // Undefined symbols in a SharedFile do not change the binding.
Kazu Hirata62e48ed2021-12-25 05:22:27498 if (isa_and_nonnull<SharedFile>(other.file))
Fangrui Songf3475412019-07-04 10:38:04499 return;
500
Fangrui Songe49c4172019-08-06 14:03:45501 if (isUndefined() || isShared()) {
502 // The binding will be weak if there is at least one reference and all are
503 // weak. The binding has one opportunity to change to weak: if the first
504 // reference is weak.
505 if (other.binding != STB_WEAK || !referenced)
Rui Ueyama3837f422019-07-10 05:00:37506 binding = other.binding;
Rui Ueyama7f7d2b22019-05-23 09:58:08507 }
508}
509
Fangrui Song19e37a72022-02-24 22:09:00510// Compare two symbols. Return true if the new symbol should win.
Fangrui Songb07ef4d2022-02-28 18:25:21511bool Symbol::shouldReplace(const Defined &other) const {
Fangrui Song6d943402022-02-24 22:08:06512 if (LLVM_UNLIKELY(isCommon())) {
513 if (config->warnCommon)
514 warn("common " + getName() + " is overridden");
515 return !other.isWeak();
516 }
517 if (!isDefined())
518 return true;
Rui Ueyama7f7d2b22019-05-23 09:58:08519
Fangrui Song2bdad162021-12-15 23:19:35520 // .symver foo,foo@@VER unfortunately creates two defined symbols: foo and
521 // foo@@VER. In GNU ld, if foo and foo@@VER are in the same file, foo is
522 // ignored. In our implementation, when this is foo, this->getName() may still
Fangrui Song6d943402022-02-24 22:08:06523 // contain @@, return true in this case as well.
524 if (LLVM_UNLIKELY(file == other.file)) {
525 if (other.getName().contains("@@"))
526 return true;
Fangrui Song2bdad162021-12-15 23:19:35527 if (getName().contains("@@"))
Fangrui Song6d943402022-02-24 22:08:06528 return false;
Fangrui Song2bdad162021-12-15 23:19:35529 }
Rui Ueyama7f7d2b22019-05-23 09:58:08530
Fangrui Songc7cf9602022-03-14 19:00:15531 // Incoming STB_GLOBAL overrides STB_WEAK/STB_GNU_UNIQUE. -fgnu-unique changes
532 // some vague linkage data in COMDAT from STB_WEAK to STB_GNU_UNIQUE. Treat
533 // STB_GNU_UNIQUE like STB_WEAK so that we prefer the first among all
534 // STB_WEAK/STB_GNU_UNIQUE copies. If we prefer an incoming STB_GNU_UNIQUE to
535 // an existing STB_WEAK, there may be discarded section errors because the
536 // selected copy may be in a non-prevailing COMDAT.
537 return !isGlobal() && other.isGlobal();
Rui Ueyama7f7d2b22019-05-23 09:58:08538}
539
Fangrui Song7c7702b2022-03-16 02:24:41540void elf::reportDuplicate(const Symbol &sym, const InputFile *newFile,
Fangrui Song88d66f62022-02-22 18:07:58541 InputSectionBase *errSec, uint64_t errOffset) {
Rui Ueyama3837f422019-07-10 05:00:37542 if (config->allowMultipleDefinition)
Rui Ueyama7f7d2b22019-05-23 09:58:08543 return;
Fangrui Song7c7702b2022-03-16 02:24:41544 // In glibc<2.32, crti.o has .gnu.linkonce.t.__x86.get_pc_thunk.bx, which
545 // is sort of proto-comdat. There is actually no duplicate if we have
546 // full support for .gnu.linkonce.
547 const Defined *d = dyn_cast<Defined>(&sym);
548 if (!d || d->getName() == "__x86.get_pc_thunk.bx")
549 return;
550 // Allow absolute symbols with the same value for GNU ld compatibility.
551 if (!d->section && !errSec && errOffset && d->value == errOffset)
552 return;
Rui Ueyama3837f422019-07-10 05:00:37553 if (!d->section || !errSec) {
Fangrui Song467e1b32022-02-15 19:18:31554 error("duplicate symbol: " + toString(sym) + "\n>>> defined in " +
555 toString(sym.file) + "\n>>> defined in " + toString(newFile));
Rui Ueyama7f7d2b22019-05-23 09:58:08556 return;
557 }
558
559 // Construct and print an error message in the form of:
560 //
561 // ld.lld: error: duplicate symbol: foo
562 // >>> defined at bar.c:30
563 // >>> bar.o (/home/alice/src/bar.o)
564 // >>> defined at baz.c:563
565 // >>> baz.o in archive libbaz.a
Rui Ueyama3837f422019-07-10 05:00:37566 auto *sec1 = cast<InputSectionBase>(d->section);
Fangrui Song467e1b32022-02-15 19:18:31567 std::string src1 = sec1->getSrcMsg(sym, d->value);
Rui Ueyama3837f422019-07-10 05:00:37568 std::string obj1 = sec1->getObjMsg(d->value);
Fangrui Song467e1b32022-02-15 19:18:31569 std::string src2 = errSec->getSrcMsg(sym, errOffset);
Rui Ueyama3837f422019-07-10 05:00:37570 std::string obj2 = errSec->getObjMsg(errOffset);
Rui Ueyama7f7d2b22019-05-23 09:58:08571
Fangrui Song467e1b32022-02-15 19:18:31572 std::string msg = "duplicate symbol: " + toString(sym) + "\n>>> defined at ";
Rui Ueyama3837f422019-07-10 05:00:37573 if (!src1.empty())
574 msg += src1 + "\n>>> ";
575 msg += obj1 + "\n>>> defined at ";
576 if (!src2.empty())
577 msg += src2 + "\n>>> ";
578 msg += obj2;
579 error(msg);
Rui Ueyama7f7d2b22019-05-23 09:58:08580}
581
Fangrui Song88d66f62022-02-22 18:07:58582void Symbol::checkDuplicate(const Defined &other) const {
Fangrui Song6d943402022-02-24 22:08:06583 if (isDefined() && !isWeak() && !other.isWeak())
Fangrui Song88d66f62022-02-22 18:07:58584 reportDuplicate(*this, other.file,
585 dyn_cast_or_null<InputSectionBase>(other.section),
586 other.value);
587}
588
Rui Ueyama3837f422019-07-10 05:00:37589void Symbol::resolveCommon(const CommonSymbol &other) {
Fangrui Song6d943402022-02-24 22:08:06590 if (isDefined() && !isWeak()) {
591 if (config->warnCommon)
592 warn("common " + getName() + " is overridden");
Rui Ueyama7f7d2b22019-05-23 09:58:08593 return;
Fangrui Song6d943402022-02-24 22:08:06594 }
Rui Ueyama7f7d2b22019-05-23 09:58:08595
Fangrui Song6d943402022-02-24 22:08:06596 if (CommonSymbol *oldSym = dyn_cast<CommonSymbol>(this)) {
597 if (config->warnCommon)
598 warn("multiple common of " + getName());
599 oldSym->alignment = std::max(oldSym->alignment, other.alignment);
600 if (oldSym->size < other.size) {
601 oldSym->file = other.file;
602 oldSym->size = other.size;
Fangrui Song69d10d22019-12-07 05:18:31603 }
Rui Ueyama7f7d2b22019-05-23 09:58:08604 return;
605 }
606
Fangrui Song6d943402022-02-24 22:08:06607 if (auto *s = dyn_cast<SharedSymbol>(this)) {
608 // Increase st_size if the shared symbol has a larger st_size. The shared
609 // symbol may be created from common symbols. The fact that some object
610 // files were linked into a shared object first should not change the
611 // regular rule that picks the largest st_size.
612 uint64_t size = s->size;
613 replace(other);
614 if (size > cast<CommonSymbol>(this)->size)
615 cast<CommonSymbol>(this)->size = size;
616 } else {
617 replace(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08618 }
619}
620
Rui Ueyama3837f422019-07-10 05:00:37621void Symbol::resolveDefined(const Defined &other) {
Fangrui Songb07ef4d2022-02-28 18:25:21622 if (shouldReplace(other))
Rui Ueyama3837f422019-07-10 05:00:37623 replace(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08624}
625
Fangrui Song41298902022-02-24 20:20:05626void Symbol::resolveLazy(const LazyObject &other) {
Sean Fertile8f91f382020-12-07 14:28:17627 // For common objects, we want to look for global or weak definitions that
Fangrui Song09401df2021-11-26 18:58:50628 // should be extracted as the canonical definition instead.
Fangrui Song15617cd2022-02-24 20:21:40629 if (LLVM_UNLIKELY(isCommon()) && elf::config->fortranCommon &&
630 other.file->shouldExtractForCommon(getName())) {
Fangrui Songd14d8662022-02-27 20:33:28631 driver->backwardReferences.erase(this);
Fangrui Song15617cd2022-02-24 20:21:40632 replace(other);
633 other.extract();
634 return;
Sean Fertile8f91f382020-12-07 14:28:17635 }
636
Fangrui Song03c825c2020-04-06 05:27:46637 if (!isUndefined()) {
638 // See the comment in resolveUndefined().
639 if (isDefined())
Fangrui Songd14d8662022-02-27 20:33:28640 driver->backwardReferences.erase(this);
Rui Ueyama7f7d2b22019-05-23 09:58:08641 return;
Fangrui Song03c825c2020-04-06 05:27:46642 }
Rui Ueyama7f7d2b22019-05-23 09:58:08643
Fangrui Song09401df2021-11-26 18:58:50644 // An undefined weak will not extract archive members. See comment on Lazy in
Rui Ueyama7f7d2b22019-05-23 09:58:08645 // Symbols.h for the details.
646 if (isWeak()) {
Rui Ueyama3837f422019-07-10 05:00:37647 uint8_t ty = type;
648 replace(other);
649 type = ty;
650 binding = STB_WEAK;
Rui Ueyama7f7d2b22019-05-23 09:58:08651 return;
652 }
653
Fangrui Songa954bb12021-09-20 16:52:30654 const InputFile *oldFile = file;
Fangrui Song09401df2021-11-26 18:58:50655 other.extract();
Fangrui Songa954bb12021-09-20 16:52:30656 if (!config->whyExtract.empty())
657 recordWhyExtract(oldFile, *file, *this);
Rui Ueyama7f7d2b22019-05-23 09:58:08658}
659
Rui Ueyama3837f422019-07-10 05:00:37660void Symbol::resolveShared(const SharedSymbol &other) {
Fangrui Song69d10d22019-12-07 05:18:31661 if (isCommon()) {
662 // See the comment in resolveCommon() above.
663 if (other.size > cast<CommonSymbol>(this)->size)
664 cast<CommonSymbol>(this)->size = other.size;
665 return;
666 }
Rui Ueyama3837f422019-07-10 05:00:37667 if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) {
Rui Ueyama7f7d2b22019-05-23 09:58:08668 // An undefined symbol with non default visibility must be satisfied
669 // in the same DSO.
Rui Ueyama3837f422019-07-10 05:00:37670 uint8_t bind = binding;
671 replace(other);
672 binding = bind;
Fangrui Song64676492020-05-18 17:15:59673 } else if (traced)
Fangrui Song977a1a52022-02-06 00:34:02674 printTraceSymbol(other, getName());
Rui Ueyama7f7d2b22019-05-23 09:58:08675}