blob: cabffc8b3e7af218b5e42429082b1432bb6bd48e [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
26using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:5427using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:0728
Peter Collingbournee9a9e0a2017-11-06 04:35:3129Defined *ElfSym::Bss;
30Defined *ElfSym::Etext1;
31Defined *ElfSym::Etext2;
32Defined *ElfSym::Edata1;
33Defined *ElfSym::Edata2;
34Defined *ElfSym::End1;
35Defined *ElfSym::End2;
36Defined *ElfSym::GlobalOffsetTable;
37Defined *ElfSym::MipsGp;
38Defined *ElfSym::MipsGpDisp;
39Defined *ElfSym::MipsLocalGp;
Rui Ueyama6f9d49c2019-01-15 18:30:2340Defined *ElfSym::RelaIpltStart;
Rafael Espindolaaded4092018-04-19 16:54:3041Defined *ElfSym::RelaIpltEnd;
Rui Ueyama80474a22017-02-28 19:29:5542
Rui Ueyama48882242017-11-04 00:31:0443static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
44 switch (Sym.kind()) {
Peter Collingbournee9a9e0a2017-11-06 04:35:3145 case Symbol::DefinedKind: {
46 auto &D = cast<Defined>(Sym);
Rafael Espindola5616adf2017-03-08 22:36:2847 SectionBase *IS = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:3548
Rafael Espindolaccfe3cb2016-04-04 14:04:1649 // According to the ELF spec reference to a local symbol from outside
50 // the group are not allowed. Unfortunately .eh_frame breaks that rule
51 // and must be treated specially. For now we just replace the symbol with
52 // 0.
Rafael Espindola774ea7d2017-02-23 16:49:0753 if (IS == &InputSection::Discarded)
Rafael Espindolaccfe3cb2016-04-04 14:04:1654 return 0;
55
Rui Ueyamab5a69702016-02-01 21:00:3556 // This is an absolute symbol.
Sean Silva902ae3c2016-12-15 00:57:5357 if (!IS)
Rafael Espindolaccfe3cb2016-04-04 14:04:1658 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:3559
Rafael Espindolaf4d6e8c2018-04-19 17:26:5060 IS = IS->Repl;
61
Rafael Espindola9371bab2017-03-08 15:21:3262 uint64_t Offset = D.Value;
Sean Silvaa9ba4502017-02-28 08:32:5663
64 // An object in an SHF_MERGE section might be referenced via a
65 // section symbol (as a hack for reducing the number of local
66 // symbols).
Sean Silvad4e60622017-03-01 04:44:0467 // Depending on the addend, the reference via a section symbol
68 // refers to a different object in the merge section.
69 // Since the objects in the merge section are not necessarily
70 // contiguous in the output, the addend can thus affect the final
71 // VA in a non-linear way.
72 // To make this work, we incorporate the addend into the section
73 // offset (and zero out the addend for later processing) so that
74 // we find the right object in the section.
Rafael Espindolaccfe3cb2016-04-04 14:04:1675 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:3776 Offset += Addend;
77 Addend = 0;
78 }
Sean Silvaa9ba4502017-02-28 08:32:5679
Sean Silva6ab39262017-02-28 09:01:5880 // In the typical case, this is actually very simple and boils
81 // down to adding together 3 numbers:
82 // 1. The address of the output section.
83 // 2. The offset of the input section within the output section.
84 // 3. The offset within the input section (this addition happens
85 // inside InputSection::getOffset).
86 //
87 // If you understand the data structures involved with this next
88 // line (and how they get built), then you have a pretty good
89 // understanding of the linker.
Rafael Espindola4f058a22018-03-24 00:35:1190 uint64_t VA = IS->getVA(Offset);
Sean Silva6ab39262017-02-28 09:01:5891
Simon Atanasyanfae2a5092019-02-19 10:36:5892 // MIPS relocatable files can mix regular and microMIPS code.
93 // Linker needs to distinguish such code. To do so microMIPS
94 // symbols has the `STO_MIPS_MICROMIPS` flag in the `st_other`
95 // field. Unfortunately, the `MIPS::relocateOne()` method has
96 // a symbol value only. To pass type of the symbol (regular/microMIPS)
97 // to that routine as well as other places where we write
98 // a symbol value as-is (.dynamic section, `Elf_Ehdr::e_entry`
99 // field etc) do the same trick as compiler uses to mark microMIPS
100 // for CPU - set the less-significant bit.
101 if (Config->EMachine == EM_MIPS && isMicroMips() &&
102 ((Sym.StOther & STO_MIPS_MICROMIPS) || Sym.NeedsPltAddr))
103 VA |= 1;
104
George Rimar6a3b1542016-10-04 08:52:51105 if (D.isTls() && !Config->Relocatable) {
Ryan Prichard1c33d142018-09-18 00:24:48106 // Use the address of the TLS segment's first section rather than the
107 // segment's address, because segment addresses aren't initialized until
108 // after sections are finalized. (e.g. Measuring the size of .rela.dyn
109 // for Android relocation packing requires knowing TLS symbol addresses
110 // during section finalization.)
111 if (!Out::TlsPhdr || !Out::TlsPhdr->FirstSec)
Rafael Espindoladfebd362017-11-29 22:47:35112 fatal(toString(D.File) +
Peter Collingbourne3e2abde2017-07-14 00:22:46113 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Ryan Prichard1c33d142018-09-18 00:24:48114 return VA - Out::TlsPhdr->FirstSec->Addr;
George Rimar6a3b1542016-10-04 08:52:51115 }
Rafael Espindola1f5b70f2016-03-11 14:21:37116 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35117 }
Rafael Espindolaab0cce52018-04-26 17:58:58118 case Symbol::SharedKind:
Rui Ueyamaf52496e2017-11-03 21:21:47119 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35120 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47121 case Symbol::LazyArchiveKind:
122 case Symbol::LazyObjectKind:
Chih-Hung Hsieh73e04842018-09-11 23:00:36123 assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
124 return 0;
Rui Ueyamaf3fad552018-10-12 18:29:18125 case Symbol::PlaceholderKind:
126 llvm_unreachable("placeholder symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35127 }
George Rimar777f9632016-03-12 08:31:34128 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35129}
130
Rui Ueyamaf52496e2017-11-03 21:21:47131uint64_t Symbol::getVA(int64_t Addend) const {
George Rimarf64618a2017-03-17 11:56:54132 uint64_t OutVA = getSymVA(*this, Addend);
Rafael Espindola8381c562016-03-17 23:36:19133 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05134}
135
Peter Collingbourne8331f612019-02-13 21:49:55136uint64_t Symbol::getGotVA() const {
137 if (GotInIgot)
138 return In.IgotPlt->getVA() + getGotPltOffset();
139 return In.Got->getVA() + getGotOffset();
140}
Rafael Espindola74031ba2016-04-07 15:20:56141
Rui Ueyamaf52496e2017-11-03 21:21:47142uint64_t Symbol::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14143 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35144}
145
Rui Ueyamaf52496e2017-11-03 21:21:47146uint64_t Symbol::getGotPltVA() const {
Peter Collingbourne8331f612019-02-13 21:49:55147 if (IsInIplt)
Rui Ueyama4e247522018-09-25 19:26:58148 return In.IgotPlt->getVA() + getGotPltOffset();
149 return In.GotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56150}
151
Rui Ueyamaf52496e2017-11-03 21:21:47152uint64_t Symbol::getGotPltOffset() const {
Peter Collingbourne8331f612019-02-13 21:49:55153 if (IsInIplt)
Rafael Espindolaf4a9d562018-04-26 16:09:30154 return PltIndex * Target->GotPltEntrySize;
155 return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35156}
157
Sean Fertile614dc112018-11-14 17:56:43158uint64_t Symbol::getPPC64LongBranchOffset() const {
159 assert(PPC64BranchltIndex != 0xffff);
160 return PPC64BranchltIndex * Target->GotPltEntrySize;
161}
162
Rui Ueyamaf52496e2017-11-03 21:21:47163uint64_t Symbol::getPltVA() const {
Rui Ueyama63d397e2018-11-28 17:42:59164 PltSection *Plt = IsInIplt ? In.Iplt : In.Plt;
Simon Atanasyanfae2a5092019-02-19 10:36:58165 uint64_t OutVA =
166 Plt->getVA() + Plt->HeaderSize + PltIndex * Target->PltEntrySize;
167 // 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.
170 if (Config->EMachine == EM_MIPS && isMicroMips())
171 OutVA |= 1;
172 return OutVA;
Rafael Espindolaab0cce52018-04-26 17:58:58173}
174
Sean Fertile614dc112018-11-14 17:56:43175uint64_t Symbol::getPPC64LongBranchTableVA() const {
176 assert(PPC64BranchltIndex != 0xffff);
177 return In.PPC64LongBranchTarget->getVA() +
178 PPC64BranchltIndex * Target->GotPltEntrySize;
179}
180
Rui Ueyamaf52496e2017-11-03 21:21:47181uint64_t Symbol::getSize() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31182 if (const auto *DR = dyn_cast<Defined>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16183 return DR->Size;
George Rimar904ed692018-07-17 11:35:28184 return cast<SharedSymbol>(this)->Size;
Rui Ueyama512c61d2016-02-03 00:12:24185}
186
Rui Ueyamaf52496e2017-11-03 21:21:47187OutputSection *Symbol::getOutputSection() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31188 if (auto *S = dyn_cast<Defined>(this)) {
Rafael Espindolaf4fb5fd2017-12-13 22:59:23189 if (auto *Sec = S->Section)
Rafael Espindolaf4d6e8c2018-04-19 17:26:50190 return Sec->Repl->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42191 return nullptr;
192 }
Rui Ueyama968db482017-02-28 04:02:42193 return nullptr;
194}
195
Rui Ueyama35fa6c52016-11-23 05:48:40196// If a symbol name contains '@', the characters after that is
197// a symbol version name. This function parses that.
Rui Ueyamaf52496e2017-11-03 21:21:47198void Symbol::parseSymbolVersion() {
Rui Ueyama35fa6c52016-11-23 05:48:40199 StringRef S = getName();
200 size_t Pos = S.find('@');
201 if (Pos == 0 || Pos == StringRef::npos)
202 return;
203 StringRef Verstr = S.substr(Pos + 1);
204 if (Verstr.empty())
205 return;
206
207 // Truncate the symbol name so that it doesn't include the version string.
Rafael Espindola1eeb2622018-04-25 21:44:37208 NameSize = Pos;
Rui Ueyama35fa6c52016-11-23 05:48:40209
Rafael Espindola1d6d1b42017-01-17 16:08:06210 // If this is not in this DSO, it is not a definition.
Peter Collingbourneb472aa02017-11-06 04:39:07211 if (!isDefined())
Rafael Espindola2756e042017-01-06 22:30:35212 return;
213
Rui Ueyama35fa6c52016-11-23 05:48:40214 // '@@' in a symbol name means the default version.
215 // It is usually the most recent one.
216 bool IsDefault = (Verstr[0] == '@');
217 if (IsDefault)
218 Verstr = Verstr.substr(1);
219
220 for (VersionDefinition &Ver : Config->VersionDefinitions) {
221 if (Ver.Name != Verstr)
222 continue;
223
224 if (IsDefault)
Rui Ueyamaf1f00842017-10-31 16:07:41225 VersionId = Ver.Id;
Rui Ueyama35fa6c52016-11-23 05:48:40226 else
Rui Ueyamaf1f00842017-10-31 16:07:41227 VersionId = Ver.Id | VERSYM_HIDDEN;
Rui Ueyama35fa6c52016-11-23 05:48:40228 return;
229 }
230
231 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13232 // Usually version script is not provided when linking executable,
233 // but we may still want to override a versioned symbol from DSO,
Peter Smith796fb992018-05-14 10:13:56234 // so we do not report error in this case. We also do not error
235 // if the symbol has a local version as it won't be in the dynamic
236 // symbol table.
237 if (Config->Shared && VersionId != VER_NDX_LOCAL)
Rafael Espindoladfebd362017-11-29 22:47:35238 error(toString(File) + ": symbol " + S + " has undefined version " +
George Rimar4d2f97622017-07-04 13:19:13239 Verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40240}
241
Rui Ueyama7d476192019-05-16 02:14:00242InputFile *LazyArchive::fetch() const {
243 return cast<ArchiveFile>(File)->fetch(Sym);
244}
Rui Ueyamaf8baa662016-04-07 19:24:51245
Peter Collingbourne98930112018-08-08 23:48:12246MemoryBufferRef LazyArchive::getMemberBuffer() {
247 Archive::Child C = CHECK(
248 Sym.getMember(), "could not get the member for symbol " + Sym.getName());
249
250 return CHECK(C.getMemoryBufferRef(),
251 "could not get the buffer for the member defining symbol " +
252 Sym.getName());
253}
254
Rui Ueyama7d476192019-05-16 02:14:00255InputFile *LazyObject::fetch() const {
256 return cast<LazyObjFile>(File)->fetch();
257}
258
Rui Ueyamaf52496e2017-11-03 21:21:47259uint8_t Symbol::computeBinding() const {
Rafael Espindolab7e2ee22017-01-10 17:08:13260 if (Config->Relocatable)
261 return Binding;
Peter Collingbournedadcc172016-04-22 18:42:48262 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolab7e2ee22017-01-10 17:08:13263 return STB_LOCAL;
George Rimarf79a8ef2018-10-03 09:33:00264 if (VersionId == VER_NDX_LOCAL && isDefined() && !IsPreemptible)
Rafael Espindolab7e2ee22017-01-10 17:08:13265 return STB_LOCAL;
Rui Ueyamaaad2e322018-02-02 21:44:06266 if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE)
Rafael Espindolab7e2ee22017-01-10 17:08:13267 return STB_GLOBAL;
268 return Binding;
269}
270
Rui Ueyamaf52496e2017-11-03 21:21:47271bool Symbol::includeInDynsym() const {
Rafael Espindolae05e2f82017-09-15 18:05:02272 if (!Config->HasDynSymTab)
273 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13274 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25275 return false;
Siva Chandra1915e2b2019-03-18 15:32:57276 // If a PIE binary was not linked against any shared libraries, then we can
277 // safely drop weak undef symbols from .dynsym.
278 if (isUndefWeak() && Config->Pie && SharedFiles.empty())
279 return false;
Peter Collingbourneb472aa02017-11-06 04:39:07280 if (!isDefined())
Rafael Espindola3d9f1c02017-09-13 20:43:04281 return true;
Rafael Espindolac57f8cd2017-09-13 20:47:53282 return ExportDynamic;
Rafael Espindolaae605c12016-04-21 20:35:25283}
Rui Ueyama69c778c2016-07-17 17:50:09284
285// Print out a log message for --trace-symbol.
Rui Ueyamaf52496e2017-11-03 21:21:47286void elf::printTraceSymbol(Symbol *Sym) {
Rui Ueyamae6e206d2017-02-21 23:22:56287 std::string S;
Rui Ueyamaf1f00842017-10-31 16:07:41288 if (Sym->isUndefined())
Rui Ueyamae6e206d2017-02-21 23:22:56289 S = ": reference to ";
Rui Ueyamaf1f00842017-10-31 16:07:41290 else if (Sym->isLazy())
Rafael Espindolabc2b1652017-10-27 18:30:11291 S = ": lazy definition of ";
Rui Ueyamaf1f00842017-10-31 16:07:41292 else if (Sym->isShared())
Rafael Espindolabc2b1652017-10-27 18:30:11293 S = ": shared definition of ";
Peter Collingbournee9a9e0a2017-11-06 04:35:31294 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
Peter Collingbourne6c55a702017-11-06 04:33:58295 S = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09296 else
Rui Ueyamae6e206d2017-02-21 23:22:56297 S = ": definition of ";
298
Rui Ueyamaf1f00842017-10-31 16:07:41299 message(toString(Sym->File) + S + Sym->getName());
Rui Ueyama69c778c2016-07-17 17:50:09300}
301
Rui Ueyama4c06a6c2018-10-26 15:07:12302void elf::maybeWarnUnorderableSymbol(const Symbol *Sym) {
Michael J. Spencerb8427252018-04-17 23:30:05303 if (!Config->WarnSymbolOrdering)
304 return;
Rui Ueyamab774c3c2018-04-26 01:38:29305
Fangrui Song11ca54f2018-10-10 22:48:57306 // If UnresolvedPolicy::Ignore is used, no "undefined symbol" error/warning
307 // is emitted. It makes sense to not warn on undefined symbols.
308 //
309 // Note, ld.bfd --symbol-ordering-file= does not warn on undefined symbols,
310 // but we don't have to be compatible here.
311 if (Sym->isUndefined() &&
312 Config->UnresolvedSymbols == UnresolvedPolicy::Ignore)
313 return;
314
Michael J. Spencerb8427252018-04-17 23:30:05315 const InputFile *File = Sym->File;
316 auto *D = dyn_cast<Defined>(Sym);
Rui Ueyamab774c3c2018-04-26 01:38:29317
318 auto Warn = [&](StringRef S) { warn(toString(File) + S + Sym->getName()); };
319
Michael J. Spencerb8427252018-04-17 23:30:05320 if (Sym->isUndefined())
Rui Ueyamab774c3c2018-04-26 01:38:29321 Warn(": unable to order undefined symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05322 else if (Sym->isShared())
Rui Ueyamab774c3c2018-04-26 01:38:29323 Warn(": unable to order shared symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05324 else if (D && !D->Section)
Rui Ueyamab774c3c2018-04-26 01:38:29325 Warn(": unable to order absolute symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05326 else if (D && isa<OutputSection>(D->Section))
Rui Ueyamab774c3c2018-04-26 01:38:29327 Warn(": unable to order synthetic symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05328 else if (D && !D->Section->Repl->Live)
Rui Ueyamab774c3c2018-04-26 01:38:29329 Warn(": unable to order discarded symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05330}
331
Rui Ueyamaa3ac1732016-11-24 20:24:18332// Returns a symbol for an error message.
Rui Ueyamaf52496e2017-11-03 21:21:47333std::string lld::toString(const Symbol &B) {
Rui Ueyamaa3ac1732016-11-24 20:24:18334 if (Config->Demangle)
Rui Ueyama53fe4692017-11-28 02:15:26335 if (Optional<std::string> S = demangleItanium(B.getName()))
Rui Ueyama4c5b8ce2016-12-07 23:17:05336 return *S;
Rui Ueyamaa3ac1732016-11-24 20:24:18337 return B.getName();
338}