blob: 4956e27eb415db742d7d761daf5c072a801b85fe [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"
Michael J. Spencercdae0a42015-07-28 22:58:2510#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:3511#include "InputSection.h"
12#include "OutputSections.h"
Rui Ueyamae8a61022016-11-05 23:05:4713#include "SyntheticSections.h"
Rui Ueyamab5a69702016-02-01 21:00:3514#include "Target.h"
Rafael Espindola17cb7c02016-12-19 17:01:0115#include "Writer.h"
Bob Haarmanb8a59c82017-10-25 22:28:3816#include "lld/Common/ErrorHandler.h"
Rui Ueyama53fe4692017-11-28 02:15:2617#include "lld/Common/Strings.h"
Michael J. Spencer1b348a62015-09-04 22:28:1018#include "llvm/ADT/STLExtras.h"
Eugene Leviantc958d8d2016-10-12 08:19:3019#include "llvm/Support/Path.h"
Rui Ueyamac72ba3a2016-11-23 04:57:2520#include <cstring>
Michael J. Spencer1b348a62015-09-04 22:28:1021
22using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0723using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5224using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0725
Fangrui Songbd8cfe62019-10-07 08:31:1826namespace lld {
27// Returns a symbol for an error message.
28static std::string demangle(StringRef symName) {
29 if (elf::config->demangle)
30 return demangleItanium(symName);
31 return symName;
32}
Michael J. Spencer84487f12015-07-24 21:03:0733
Fangrui Songbd8cfe62019-10-07 08:31:1834std::string toString(const elf::Symbol &b) { return demangle(b.getName()); }
35std::string toELFString(const Archive::Symbol &b) {
36 return demangle(b.getName());
37}
38
39namespace elf {
Rui Ueyama3837f422019-07-10 05:00:3740Defined *ElfSym::bss;
41Defined *ElfSym::etext1;
42Defined *ElfSym::etext2;
43Defined *ElfSym::edata1;
44Defined *ElfSym::edata2;
45Defined *ElfSym::end1;
46Defined *ElfSym::end2;
47Defined *ElfSym::globalOffsetTable;
48Defined *ElfSym::mipsGp;
49Defined *ElfSym::mipsGpDisp;
50Defined *ElfSym::mipsLocalGp;
51Defined *ElfSym::relaIpltStart;
52Defined *ElfSym::relaIpltEnd;
53Defined *ElfSym::riscvGlobalPointer;
54Defined *ElfSym::tlsModuleBase;
Rui Ueyama80474a22017-02-28 19:29:5555
Rui Ueyama3837f422019-07-10 05:00:3756static uint64_t getSymVA(const Symbol &sym, int64_t &addend) {
57 switch (sym.kind()) {
Peter Collingbournee9a9e0a2017-11-06 04:35:3158 case Symbol::DefinedKind: {
Rui Ueyama3837f422019-07-10 05:00:3759 auto &d = cast<Defined>(sym);
60 SectionBase *isec = d.section;
Rui Ueyamab5a69702016-02-01 21:00:3561
62 // This is an absolute symbol.
Rui Ueyama3837f422019-07-10 05:00:3763 if (!isec)
64 return d.value;
Rui Ueyamab5a69702016-02-01 21:00:3565
Rui Ueyama3837f422019-07-10 05:00:3766 assert(isec != &InputSection::discarded);
67 isec = isec->repl;
Rafael Espindolaf4d6e8c2018-04-19 17:26:5068
Rui Ueyama3837f422019-07-10 05:00:3769 uint64_t offset = d.value;
Sean Silvaa9ba4502017-02-28 08:32:5670
71 // An object in an SHF_MERGE section might be referenced via a
72 // section symbol (as a hack for reducing the number of local
73 // symbols).
Sean Silvad4e60622017-03-01 04:44:0474 // Depending on the addend, the reference via a section symbol
75 // refers to a different object in the merge section.
76 // Since the objects in the merge section are not necessarily
77 // contiguous in the output, the addend can thus affect the final
78 // VA in a non-linear way.
79 // To make this work, we incorporate the addend into the section
80 // offset (and zero out the addend for later processing) so that
81 // we find the right object in the section.
Rui Ueyama3837f422019-07-10 05:00:3782 if (d.isSection()) {
83 offset += addend;
84 addend = 0;
Rafael Espindola1f5b70f2016-03-11 14:21:3785 }
Sean Silvaa9ba4502017-02-28 08:32:5686
Sean Silva6ab39262017-02-28 09:01:5887 // In the typical case, this is actually very simple and boils
88 // down to adding together 3 numbers:
89 // 1. The address of the output section.
90 // 2. The offset of the input section within the output section.
91 // 3. The offset within the input section (this addition happens
92 // inside InputSection::getOffset).
93 //
94 // If you understand the data structures involved with this next
95 // line (and how they get built), then you have a pretty good
96 // understanding of the linker.
Rui Ueyama3837f422019-07-10 05:00:3797 uint64_t va = isec->getVA(offset);
Sean Silva6ab39262017-02-28 09:01:5898
Simon Atanasyanfae2a5092019-02-19 10:36:5899 // MIPS relocatable files can mix regular and microMIPS code.
100 // Linker needs to distinguish such code. To do so microMIPS
101 // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
Fangrui Songdeb58192020-01-23 05:39:16102 // field. Unfortunately, the `MIPS::relocate()` method has
Simon Atanasyanfae2a5092019-02-19 10:36:58103 // a symbol value only. To pass type of the symbol (regular/microMIPS)
104 // to that routine as well as other places where we write
105 // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
106 // field etc) do the same trick as compiler uses to mark microMIPS
107 // for CPU - set the less-significant bit.
Rui Ueyama3837f422019-07-10 05:00:37108 if (config->emachine == EM_MIPS && isMicroMips() &&
109 ((sym.stOther & STO_MIPS_MICROMIPS) || sym.needsPltAddr))
110 va |= 1;
Simon Atanasyanfae2a5092019-02-19 10:36:58111
Rui Ueyama3837f422019-07-10 05:00:37112 if (d.isTls() && !config->relocatable) {
Ryan Prichard1c33d142018-09-18 00:24:48113 // Use the address of the TLS segment's first section rather than the
114 // segment's address, because segment addresses aren't initialized until
115 // after sections are finalized. (e.g. Measuring the size of .rela.dyn
116 // for Android relocation packing requires knowing TLS symbol addresses
117 // during section finalization.)
Rui Ueyama3837f422019-07-10 05:00:37118 if (!Out::tlsPhdr || !Out::tlsPhdr->firstSec)
119 fatal(toString(d.file) +
Peter Collingbourne3e2abde2017-07-14 00:22:46120 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Rui Ueyama3837f422019-07-10 05:00:37121 return va - Out::tlsPhdr->firstSec->addr;
George Rimar6a3b1542016-10-04 08:52:51122 }
Rui Ueyama3837f422019-07-10 05:00:37123 return va;
Rui Ueyamab5a69702016-02-01 21:00:35124 }
Rafael Espindolaab0cce52018-04-26 17:58:58125 case Symbol::SharedKind:
Rui Ueyamaf52496e2017-11-03 21:21:47126 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35127 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47128 case Symbol::LazyArchiveKind:
129 case Symbol::LazyObjectKind:
Rui Ueyama3837f422019-07-10 05:00:37130 assert(sym.isUsedInRegularObj && "lazy symbol reached writer");
Chih-Hung Hsieh73e04842018-09-11 23:00:36131 return 0;
Rui Ueyama5c073a92019-05-16 03:29:03132 case Symbol::CommonKind:
133 llvm_unreachable("common symbol reached writer");
Rui Ueyamaf3fad552018-10-12 18:29:18134 case Symbol::PlaceholderKind:
135 llvm_unreachable("placeholder symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35136 }
George Rimar777f9632016-03-12 08:31:34137 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35138}
139
Rui Ueyama3837f422019-07-10 05:00:37140uint64_t Symbol::getVA(int64_t addend) const {
141 uint64_t outVA = getSymVA(*this, addend);
142 return outVA + addend;
Rafael Espindola87d9f102016-03-11 12:19:05143}
144
Peter Collingbourne8331f612019-02-13 21:49:55145uint64_t Symbol::getGotVA() const {
Rui Ueyama3837f422019-07-10 05:00:37146 if (gotInIgot)
147 return in.igotPlt->getVA() + getGotPltOffset();
148 return in.got->getVA() + getGotOffset();
Peter Collingbourne8331f612019-02-13 21:49:55149}
Rafael Espindola74031ba2016-04-07 15:20:56150
Rui Ueyama3837f422019-07-10 05:00:37151uint64_t Symbol::getGotOffset() const { return gotIndex * config->wordsize; }
Rui Ueyamab5a69702016-02-01 21:00:35152
Rui Ueyamaf52496e2017-11-03 21:21:47153uint64_t Symbol::getGotPltVA() const {
Rui Ueyama3837f422019-07-10 05:00:37154 if (isInIplt)
155 return in.igotPlt->getVA() + getGotPltOffset();
156 return in.gotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56157}
158
Rui Ueyamaf52496e2017-11-03 21:21:47159uint64_t Symbol::getGotPltOffset() const {
Rui Ueyama3837f422019-07-10 05:00:37160 if (isInIplt)
161 return pltIndex * config->wordsize;
162 return (pltIndex + target->gotPltHeaderEntriesNum) * config->wordsize;
Rui Ueyamab5a69702016-02-01 21:00:35163}
164
Rui Ueyamaf52496e2017-11-03 21:21:47165uint64_t Symbol::getPltVA() const {
Fangrui Song891a8652019-12-14 22:17:35166 uint64_t outVA = isInIplt
167 ? in.iplt->getVA() + pltIndex * target->ipltEntrySize
168 : in.plt->getVA() + in.plt->headerSize +
169 pltIndex * target->pltEntrySize;
170
Simon Atanasyanfae2a5092019-02-19 10:36:58171 // While linking microMIPS code PLT code are always microMIPS
172 // code. Set the less-significant bit to track that fact.
173 // See detailed comment in the `getSymVA` function.
Rui Ueyama3837f422019-07-10 05:00:37174 if (config->emachine == EM_MIPS && isMicroMips())
175 outVA |= 1;
176 return outVA;
Rafael Espindolaab0cce52018-04-26 17:58:58177}
178
Rui Ueyamaf52496e2017-11-03 21:21:47179uint64_t Symbol::getSize() const {
Rui Ueyama3837f422019-07-10 05:00:37180 if (const auto *dr = dyn_cast<Defined>(this))
181 return dr->size;
182 return cast<SharedSymbol>(this)->size;
Rui Ueyama512c61d2016-02-03 00:12:24183}
184
Rui Ueyamaf52496e2017-11-03 21:21:47185OutputSection *Symbol::getOutputSection() const {
Rui Ueyama3837f422019-07-10 05:00:37186 if (auto *s = dyn_cast<Defined>(this)) {
187 if (auto *sec = s->section)
188 return sec->repl->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42189 return nullptr;
190 }
Rui Ueyama968db482017-02-28 04:02:42191 return nullptr;
192}
193
Rui Ueyama35fa6c52016-11-23 05:48:40194// If a symbol name contains '@', the characters after that is
195// a symbol version name. This function parses that.
Rui Ueyamaf52496e2017-11-03 21:21:47196void Symbol::parseSymbolVersion() {
Rui Ueyama3837f422019-07-10 05:00:37197 StringRef s = getName();
198 size_t pos = s.find('@');
199 if (pos == 0 || pos == StringRef::npos)
Rui Ueyama35fa6c52016-11-23 05:48:40200 return;
Rui Ueyama3837f422019-07-10 05:00:37201 StringRef verstr = s.substr(pos + 1);
202 if (verstr.empty())
Rui Ueyama35fa6c52016-11-23 05:48:40203 return;
204
205 // Truncate the symbol name so that it doesn't include the version string.
Rui Ueyama3837f422019-07-10 05:00:37206 nameSize = pos;
Rui Ueyama35fa6c52016-11-23 05:48:40207
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
Rui Ueyama7f7d2b22019-05-23 09:58:08240void Symbol::fetch() const {
Rui Ueyama3837f422019-07-10 05:00:37241 if (auto *sym = dyn_cast<LazyArchive>(this)) {
242 cast<ArchiveFile>(sym->file)->fetch(sym->sym);
Rui Ueyama7f7d2b22019-05-23 09:58:08243 return;
244 }
245
Rui Ueyama3837f422019-07-10 05:00:37246 if (auto *sym = dyn_cast<LazyObject>(this)) {
247 dyn_cast<LazyObjFile>(sym->file)->fetch();
Rui Ueyama7f7d2b22019-05-23 09:58:08248 return;
249 }
250
251 llvm_unreachable("Symbol::fetch() is called on a non-lazy symbol");
Rui Ueyama7d476192019-05-16 02:14:00252}
Rui Ueyamaf8baa662016-04-07 19:24:51253
Peter Collingbourne98930112018-08-08 23:48:12254MemoryBufferRef LazyArchive::getMemberBuffer() {
Nico Weber9c0716f2019-07-23 19:00:01255 Archive::Child c =
256 CHECK(sym.getMember(),
257 "could not get the member for symbol " + toELFString(sym));
Peter Collingbourne98930112018-08-08 23:48:12258
Rui Ueyama3837f422019-07-10 05:00:37259 return CHECK(c.getMemoryBufferRef(),
Peter Collingbourne98930112018-08-08 23:48:12260 "could not get the buffer for the member defining symbol " +
Nico Weber9c0716f2019-07-23 19:00:01261 toELFString(sym));
Peter Collingbourne98930112018-08-08 23:48:12262}
263
Rui Ueyamaf52496e2017-11-03 21:21:47264uint8_t Symbol::computeBinding() const {
Rui Ueyama3837f422019-07-10 05:00:37265 if (config->relocatable)
266 return binding;
Fangrui Songcfdd4582019-08-11 17:03:00267 if ((visibility != STV_DEFAULT && visibility != STV_PROTECTED) ||
268 versionId == VER_NDX_LOCAL)
Rafael Espindolab7e2ee22017-01-10 17:08:13269 return STB_LOCAL;
Rui Ueyama3837f422019-07-10 05:00:37270 if (!config->gnuUnique && binding == STB_GNU_UNIQUE)
Rafael Espindolab7e2ee22017-01-10 17:08:13271 return STB_GLOBAL;
Rui Ueyama3837f422019-07-10 05:00:37272 return binding;
Rafael Espindolab7e2ee22017-01-10 17:08:13273}
274
Rui Ueyamaf52496e2017-11-03 21:21:47275bool Symbol::includeInDynsym() const {
Rui Ueyama3837f422019-07-10 05:00:37276 if (!config->hasDynSymTab)
Rafael Espindolae05e2f82017-09-15 18:05:02277 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13278 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25279 return false;
Fangrui Song375371c2020-01-09 23:53:52280 if (!isDefined() && !isCommon())
Fangrui Song0fbf28f2020-01-23 19:52:03281 // This should unconditionally return true, unfortunately glibc -static-pie
282 // expects undefined weak symbols not to exist in .dynsym, e.g.
283 // __pthread_mutex_lock reference in _dl_add_to_namespace_list,
284 // __pthread_initialize_minimal reference in csu/libc-start.c.
285 return !(config->noDynamicLinker && isUndefWeak());
Rui Ueyamae63ae7f2019-06-25 06:58:07286
Fangrui Song375371c2020-01-09 23:53:52287 return exportDynamic || inDynamicList;
Rafael Espindolaae605c12016-04-21 20:35:25288}
Rui Ueyama69c778c2016-07-17 17:50:09289
290// Print out a log message for --trace-symbol.
Fangrui Songbd8cfe62019-10-07 08:31:18291void printTraceSymbol(const Symbol *sym) {
Rui Ueyama3837f422019-07-10 05:00:37292 std::string s;
293 if (sym->isUndefined())
294 s = ": reference to ";
295 else if (sym->isLazy())
296 s = ": lazy definition of ";
297 else if (sym->isShared())
298 s = ": shared definition of ";
299 else if (sym->isCommon())
300 s = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09301 else
Rui Ueyama3837f422019-07-10 05:00:37302 s = ": definition of ";
Rui Ueyamae6e206d2017-02-21 23:22:56303
Rui Ueyama3837f422019-07-10 05:00:37304 message(toString(sym->file) + s + sym->getName());
Rui Ueyama69c778c2016-07-17 17:50:09305}
306
Fangrui Songbd8cfe62019-10-07 08:31:18307void maybeWarnUnorderableSymbol(const Symbol *sym) {
Rui Ueyama3837f422019-07-10 05:00:37308 if (!config->warnSymbolOrdering)
Michael J. Spencerb8427252018-04-17 23:30:05309 return;
Rui Ueyamab774c3c2018-04-26 01:38:29310
Fangrui Song11ca54f2018-10-10 22:48:57311 // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
312 // is emitted. It makes sense to not warn on undefined symbols.
313 //
314 // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
315 // but we don't have to be compatible here.
Rui Ueyama3837f422019-07-10 05:00:37316 if (sym->isUndefined() &&
317 config->unresolvedSymbols == UnresolvedPolicy::Ignore)
Fangrui Song11ca54f2018-10-10 22:48:57318 return;
319
Rui Ueyama3837f422019-07-10 05:00:37320 const InputFile *file = sym->file;
321 auto *d = dyn_cast<Defined>(sym);
Rui Ueyamab774c3c2018-04-26 01:38:29322
Rui Ueyama3837f422019-07-10 05:00:37323 auto report = [&](StringRef s) { warn(toString(file) + s + sym->getName()); };
Rui Ueyamab774c3c2018-04-26 01:38:29324
Rui Ueyama3837f422019-07-10 05:00:37325 if (sym->isUndefined())
326 report(": unable to order undefined symbol: ");
327 else if (sym->isShared())
328 report(": unable to order shared symbol: ");
329 else if (d && !d->section)
330 report(": unable to order absolute symbol: ");
331 else if (d && isa<OutputSection>(d->section))
332 report(": unable to order synthetic symbol: ");
333 else if (d && !d->section->repl->isLive())
334 report(": unable to order discarded symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05335}
336
Fangrui Songcd0ab242019-11-30 05:58:36337// Returns true if a symbol can be replaced at load-time by a symbol
338// with the same name defined in other ELF executable or DSO.
339bool computeIsPreemptible(const Symbol &sym) {
340 assert(!sym.isLocal());
341
342 // Only symbols with default visibility that appear in dynsym can be
343 // preempted. Symbols with protected visibility cannot be preempted.
344 if (!sym.includeInDynsym() || sym.visibility != STV_DEFAULT)
345 return false;
346
347 // At this point copy relocations have not been created yet, so any
348 // symbol that is not defined locally is preemptible.
349 if (!sym.isDefined())
350 return true;
351
352 if (!config->shared)
353 return false;
354
355 // If the dynamic list is present, it specifies preemptable symbols in a DSO.
356 if (config->hasDynamicList)
357 return sym.inDynamicList;
358
359 // -Bsymbolic means that definitions are not preempted.
360 if (config->bsymbolic || (config->bsymbolicFunctions && sym.isFunc()))
361 return false;
362 return true;
363}
364
Rui Ueyama3837f422019-07-10 05:00:37365static uint8_t getMinVisibility(uint8_t va, uint8_t vb) {
366 if (va == STV_DEFAULT)
367 return vb;
368 if (vb == STV_DEFAULT)
369 return va;
370 return std::min(va, vb);
Rui Ueyama7f7d2b22019-05-23 09:58:08371}
372
373// Merge symbol properties.
374//
375// When we have many symbols of the same name, we choose one of them,
376// and that's the result of symbol resolution. However, symbols that
377// were not chosen still affect some symbol properties.
Rui Ueyama3837f422019-07-10 05:00:37378void Symbol::mergeProperties(const Symbol &other) {
379 if (other.exportDynamic)
380 exportDynamic = true;
381 if (other.isUsedInRegularObj)
382 isUsedInRegularObj = true;
Rui Ueyama7f7d2b22019-05-23 09:58:08383
384 // DSO symbols do not affect visibility in the output.
Rui Ueyama3837f422019-07-10 05:00:37385 if (!other.isShared())
386 visibility = getMinVisibility(visibility, other.visibility);
Rui Ueyama7f7d2b22019-05-23 09:58:08387}
388
Rui Ueyama3837f422019-07-10 05:00:37389void Symbol::resolve(const Symbol &other) {
390 mergeProperties(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08391
392 if (isPlaceholder()) {
Rui Ueyama3837f422019-07-10 05:00:37393 replace(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08394 return;
395 }
396
Rui Ueyama3837f422019-07-10 05:00:37397 switch (other.kind()) {
Rui Ueyama7f7d2b22019-05-23 09:58:08398 case Symbol::UndefinedKind:
Rui Ueyama3837f422019-07-10 05:00:37399 resolveUndefined(cast<Undefined>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08400 break;
401 case Symbol::CommonKind:
Rui Ueyama3837f422019-07-10 05:00:37402 resolveCommon(cast<CommonSymbol>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08403 break;
404 case Symbol::DefinedKind:
Rui Ueyama3837f422019-07-10 05:00:37405 resolveDefined(cast<Defined>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08406 break;
407 case Symbol::LazyArchiveKind:
Rui Ueyama3837f422019-07-10 05:00:37408 resolveLazy(cast<LazyArchive>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08409 break;
410 case Symbol::LazyObjectKind:
Rui Ueyama3837f422019-07-10 05:00:37411 resolveLazy(cast<LazyObject>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08412 break;
413 case Symbol::SharedKind:
Rui Ueyama3837f422019-07-10 05:00:37414 resolveShared(cast<SharedSymbol>(other));
Rui Ueyama7f7d2b22019-05-23 09:58:08415 break;
416 case Symbol::PlaceholderKind:
417 llvm_unreachable("bad symbol kind");
418 }
419}
420
Rui Ueyama3837f422019-07-10 05:00:37421void Symbol::resolveUndefined(const Undefined &other) {
Rui Ueyama7f7d2b22019-05-23 09:58:08422 // An undefined symbol with non default visibility must be satisfied
423 // in the same DSO.
424 //
425 // If this is a non-weak defined symbol in a discarded section, override the
426 // existing undefined symbol for better error message later.
Rui Ueyama3837f422019-07-10 05:00:37427 if ((isShared() && other.visibility != STV_DEFAULT) ||
428 (isUndefined() && other.binding != STB_WEAK && other.discardedSecIdx)) {
429 replace(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08430 return;
431 }
432
Rui Ueyama3837f422019-07-10 05:00:37433 if (traced)
434 printTraceSymbol(&other);
Sam Clegg7991b682019-05-24 13:29:17435
Fangrui Songf3475412019-07-04 10:38:04436 if (isLazy()) {
Rui Ueyama7f7d2b22019-05-23 09:58:08437 // An undefined weak will not fetch archive members. See comment on Lazy in
438 // Symbols.h for the details.
Rui Ueyama3837f422019-07-10 05:00:37439 if (other.binding == STB_WEAK) {
440 binding = STB_WEAK;
441 type = other.type;
Rui Ueyama7f7d2b22019-05-23 09:58:08442 return;
443 }
444
445 // Do extra check for --warn-backrefs.
446 //
447 // --warn-backrefs is an option to prevent an undefined reference from
448 // fetching an archive member written earlier in the command line. It can be
449 // used to keep compatibility with GNU linkers to some degree.
450 // I'll explain the feature and why you may find it useful in this comment.
451 //
452 // lld's symbol resolution semantics is more relaxed than traditional Unix
453 // linkers. For example,
454 //
455 // ld.lld foo.a bar.o
456 //
457 // succeeds even if bar.o contains an undefined symbol that has to be
458 // resolved by some object file in foo.a. Traditional Unix linkers don't
459 // allow this kind of backward reference, as they visit each file only once
460 // from left to right in the command line while resolving all undefined
461 // symbols at the moment of visiting.
462 //
463 // In the above case, since there's no undefined symbol when a linker visits
464 // foo.a, no files are pulled out from foo.a, and because the linker forgets
465 // about foo.a after visiting, it can't resolve undefined symbols in bar.o
466 // that could have been resolved otherwise.
467 //
468 // That lld accepts more relaxed form means that (besides it'd make more
469 // sense) you can accidentally write a command line or a build file that
470 // works only with lld, even if you have a plan to distribute it to wider
471 // users who may be using GNU linkers. With --warn-backrefs, you can detect
472 // a library order that doesn't work with other Unix linkers.
473 //
474 // The option is also useful to detect cyclic dependencies between static
475 // archives. Again, lld accepts
476 //
477 // ld.lld foo.a bar.a
478 //
479 // even if foo.a and bar.a depend on each other. With --warn-backrefs, it is
480 // handled as an error.
481 //
482 // Here is how the option works. We assign a group ID to each file. A file
483 // with a smaller group ID can pull out object files from an archive file
484 // with an equal or greater group ID. Otherwise, it is a reverse dependency
485 // and an error.
486 //
487 // A file outside --{start,end}-group gets a fresh ID when instantiated. All
488 // files within the same --{start,end}-group get the same group ID. E.g.
489 //
490 // ld.lld A B --start-group C D --end-group E
491 //
492 // A forms group 0. B form group 1. C and D (including their member object
493 // files) form group 2. E forms group 3. I think that you can see how this
494 // group assignment rule simulates the traditional linker's semantics.
Rui Ueyama3837f422019-07-10 05:00:37495 bool backref = config->warnBackrefs && other.file &&
496 file->groupId < other.file->groupId;
Rui Ueyama7f7d2b22019-05-23 09:58:08497 fetch();
498
499 // We don't report backward references to weak symbols as they can be
500 // overridden later.
Rui Ueyama3837f422019-07-10 05:00:37501 if (backref && !isWeak())
502 warn("backward reference detected: " + other.getName() + " in " +
503 toString(other.file) + " refers to " + toString(file));
Fangrui Songf3475412019-07-04 10:38:04504 return;
505 }
506
507 // Undefined symbols in a SharedFile do not change the binding.
Rui Ueyama3837f422019-07-10 05:00:37508 if (dyn_cast_or_null<SharedFile>(other.file))
Fangrui Songf3475412019-07-04 10:38:04509 return;
510
Fangrui Songe49c4172019-08-06 14:03:45511 if (isUndefined() || isShared()) {
512 // The binding will be weak if there is at least one reference and all are
513 // weak. The binding has one opportunity to change to weak: if the first
514 // reference is weak.
515 if (other.binding != STB_WEAK || !referenced)
Rui Ueyama3837f422019-07-10 05:00:37516 binding = other.binding;
Fangrui Songe49c4172019-08-06 14:03:45517 referenced = true;
Rui Ueyama7f7d2b22019-05-23 09:58:08518 }
519}
520
521// Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
522// foo@@VER. We want to effectively ignore foo, so give precedence to
523// foo@@VER.
524// FIXME: If users can transition to using
525// .symver foo,foo@@@VER
526// we can delete this hack.
Rui Ueyama3837f422019-07-10 05:00:37527static int compareVersion(StringRef a, StringRef b) {
528 bool x = a.contains("@@");
529 bool y = b.contains("@@");
530 if (!x && y)
Rui Ueyama7f7d2b22019-05-23 09:58:08531 return 1;
Rui Ueyama3837f422019-07-10 05:00:37532 if (x && !y)
Rui Ueyama7f7d2b22019-05-23 09:58:08533 return -1;
534 return 0;
535}
536
537// Compare two symbols. Return 1 if the new symbol should win, -1 if
538// the new symbol should lose, or 0 if there is a conflict.
Rui Ueyama3837f422019-07-10 05:00:37539int Symbol::compare(const Symbol *other) const {
540 assert(other->isDefined() || other->isCommon());
Rui Ueyama7f7d2b22019-05-23 09:58:08541
542 if (!isDefined() && !isCommon())
543 return 1;
544
Rui Ueyama3837f422019-07-10 05:00:37545 if (int cmp = compareVersion(getName(), other->getName()))
546 return cmp;
Rui Ueyama7f7d2b22019-05-23 09:58:08547
Rui Ueyama3837f422019-07-10 05:00:37548 if (other->isWeak())
Rui Ueyama7f7d2b22019-05-23 09:58:08549 return -1;
550
551 if (isWeak())
552 return 1;
553
Rui Ueyama3837f422019-07-10 05:00:37554 if (isCommon() && other->isCommon()) {
555 if (config->warnCommon)
Rui Ueyama7f7d2b22019-05-23 09:58:08556 warn("multiple common of " + getName());
557 return 0;
558 }
559
560 if (isCommon()) {
Rui Ueyama3837f422019-07-10 05:00:37561 if (config->warnCommon)
Rui Ueyama7f7d2b22019-05-23 09:58:08562 warn("common " + getName() + " is overridden");
563 return 1;
564 }
565
Rui Ueyama3837f422019-07-10 05:00:37566 if (other->isCommon()) {
567 if (config->warnCommon)
Rui Ueyama7f7d2b22019-05-23 09:58:08568 warn("common " + getName() + " is overridden");
569 return -1;
570 }
571
Rui Ueyama3837f422019-07-10 05:00:37572 auto *oldSym = cast<Defined>(this);
573 auto *newSym = cast<Defined>(other);
Rui Ueyama7f7d2b22019-05-23 09:58:08574
Fangrui Songd6c44822019-07-26 16:29:15575 if (dyn_cast_or_null<BitcodeFile>(other->file))
Rui Ueyama7f7d2b22019-05-23 09:58:08576 return 0;
577
Rui Ueyama3837f422019-07-10 05:00:37578 if (!oldSym->section && !newSym->section && oldSym->value == newSym->value &&
579 newSym->binding == STB_GLOBAL)
Rui Ueyama7f7d2b22019-05-23 09:58:08580 return -1;
581
582 return 0;
583}
584
Rui Ueyama3837f422019-07-10 05:00:37585static void reportDuplicate(Symbol *sym, InputFile *newFile,
586 InputSectionBase *errSec, uint64_t errOffset) {
587 if (config->allowMultipleDefinition)
Rui Ueyama7f7d2b22019-05-23 09:58:08588 return;
589
Rui Ueyama3837f422019-07-10 05:00:37590 Defined *d = cast<Defined>(sym);
591 if (!d->section || !errSec) {
592 error("duplicate symbol: " + toString(*sym) + "\n>>> defined in " +
593 toString(sym->file) + "\n>>> defined in " + toString(newFile));
Rui Ueyama7f7d2b22019-05-23 09:58:08594 return;
595 }
596
597 // Construct and print an error message in the form of:
598 //
599 // ld.lld: error: duplicate symbol: foo
600 // >>> defined at bar.c:30
601 // >>> bar.o (/home/alice/src/bar.o)
602 // >>> defined at baz.c:563
603 // >>> baz.o in archive libbaz.a
Rui Ueyama3837f422019-07-10 05:00:37604 auto *sec1 = cast<InputSectionBase>(d->section);
605 std::string src1 = sec1->getSrcMsg(*sym, d->value);
606 std::string obj1 = sec1->getObjMsg(d->value);
607 std::string src2 = errSec->getSrcMsg(*sym, errOffset);
608 std::string obj2 = errSec->getObjMsg(errOffset);
Rui Ueyama7f7d2b22019-05-23 09:58:08609
Rui Ueyama3837f422019-07-10 05:00:37610 std::string msg = "duplicate symbol: " + toString(*sym) + "\n>>> defined at ";
611 if (!src1.empty())
612 msg += src1 + "\n>>> ";
613 msg += obj1 + "\n>>> defined at ";
614 if (!src2.empty())
615 msg += src2 + "\n>>> ";
616 msg += obj2;
617 error(msg);
Rui Ueyama7f7d2b22019-05-23 09:58:08618}
619
Rui Ueyama3837f422019-07-10 05:00:37620void Symbol::resolveCommon(const CommonSymbol &other) {
621 int cmp = compare(&other);
622 if (cmp < 0)
Rui Ueyama7f7d2b22019-05-23 09:58:08623 return;
624
Rui Ueyama3837f422019-07-10 05:00:37625 if (cmp > 0) {
Fangrui Song69d10d22019-12-07 05:18:31626 if (auto *s = dyn_cast<SharedSymbol>(this)) {
627 // Increase st_size if the shared symbol has a larger st_size. The shared
628 // symbol may be created from common symbols. The fact that some object
629 // files were linked into a shared object first should not change the
630 // regular rule that picks the largest st_size.
631 uint64_t size = s->size;
632 replace(other);
633 if (size > cast<CommonSymbol>(this)->size)
634 cast<CommonSymbol>(this)->size = size;
635 } else {
636 replace(other);
637 }
Rui Ueyama7f7d2b22019-05-23 09:58:08638 return;
639 }
640
Rui Ueyama3837f422019-07-10 05:00:37641 CommonSymbol *oldSym = cast<CommonSymbol>(this);
Rui Ueyama7f7d2b22019-05-23 09:58:08642
Rui Ueyama3837f422019-07-10 05:00:37643 oldSym->alignment = std::max(oldSym->alignment, other.alignment);
644 if (oldSym->size < other.size) {
645 oldSym->file = other.file;
646 oldSym->size = other.size;
Rui Ueyama7f7d2b22019-05-23 09:58:08647 }
648}
649
Rui Ueyama3837f422019-07-10 05:00:37650void Symbol::resolveDefined(const Defined &other) {
651 int cmp = compare(&other);
652 if (cmp > 0)
653 replace(other);
654 else if (cmp == 0)
655 reportDuplicate(this, other.file,
656 dyn_cast_or_null<InputSectionBase>(other.section),
657 other.value);
Rui Ueyama7f7d2b22019-05-23 09:58:08658}
659
Rui Ueyama3837f422019-07-10 05:00:37660template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
Rui Ueyama7f7d2b22019-05-23 09:58:08661 if (!isUndefined())
662 return;
663
664 // An undefined weak will not fetch archive members. See comment on Lazy in
665 // Symbols.h for the details.
666 if (isWeak()) {
Rui Ueyama3837f422019-07-10 05:00:37667 uint8_t ty = type;
668 replace(other);
669 type = ty;
670 binding = STB_WEAK;
Rui Ueyama7f7d2b22019-05-23 09:58:08671 return;
672 }
673
Rui Ueyama3837f422019-07-10 05:00:37674 other.fetch();
Rui Ueyama7f7d2b22019-05-23 09:58:08675}
676
Rui Ueyama3837f422019-07-10 05:00:37677void Symbol::resolveShared(const SharedSymbol &other) {
Fangrui Song69d10d22019-12-07 05:18:31678 if (isCommon()) {
679 // See the comment in resolveCommon() above.
680 if (other.size > cast<CommonSymbol>(this)->size)
681 cast<CommonSymbol>(this)->size = other.size;
682 return;
683 }
Rui Ueyama3837f422019-07-10 05:00:37684 if (visibility == STV_DEFAULT && (isUndefined() || isLazy())) {
Rui Ueyama7f7d2b22019-05-23 09:58:08685 // An undefined symbol with non default visibility must be satisfied
686 // in the same DSO.
Rui Ueyama3837f422019-07-10 05:00:37687 uint8_t bind = binding;
688 replace(other);
689 binding = bind;
Fangrui Songe49c4172019-08-06 14:03:45690 referenced = true;
Rui Ueyama7f7d2b22019-05-23 09:58:08691 }
692}
Fangrui Songbd8cfe62019-10-07 08:31:18693
694} // namespace elf
695} // namespace lld