blob: c37d20c172539f2e75d3399f28c2eeff2a8e86ab [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- Symbols.cpp --------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Symbols.h"
Michael J. Spencercdae0a42015-07-28 22:58:25 +000011#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:35 +000012#include "InputSection.h"
13#include "OutputSections.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000014#include "SyntheticSections.h"
Rui Ueyamab5a69702016-02-01 21:00:35 +000015#include "Target.h"
Rafael Espindola17cb7c02016-12-19 17:01:01 +000016#include "Writer.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000017#include "lld/Common/ErrorHandler.h"
Rui Ueyama53fe4692017-11-28 02:15:26 +000018#include "lld/Common/Strings.h"
Michael J. Spencer1b348a62015-09-04 22:28:10 +000019#include "llvm/ADT/STLExtras.h"
Eugene Leviantc958d8d2016-10-12 08:19:30 +000020#include "llvm/Support/Path.h"
Rui Ueyamac72ba3a2016-11-23 04:57:25 +000021#include <cstring>
Michael J. Spencer1b348a62015-09-04 22:28:10 +000022
23using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000024using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:52 +000025using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000028using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000030Defined *ElfSym::Bss;
31Defined *ElfSym::Etext1;
32Defined *ElfSym::Etext2;
33Defined *ElfSym::Edata1;
34Defined *ElfSym::Edata2;
35Defined *ElfSym::End1;
36Defined *ElfSym::End2;
37Defined *ElfSym::GlobalOffsetTable;
38Defined *ElfSym::MipsGp;
39Defined *ElfSym::MipsGpDisp;
40Defined *ElfSym::MipsLocalGp;
Rafael Espindolaaded4092018-04-19 16:54:30 +000041Defined *ElfSym::RelaIpltEnd;
Rui Ueyama5cd9c6b2018-08-09 17:59:56 +000042Defined *ElfSym::RISCVGlobalPointer;
Rui Ueyama80474a22017-02-28 19:29:55 +000043
Rui Ueyama48882242017-11-04 00:31:04 +000044static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
45 switch (Sym.kind()) {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000046 case Symbol::DefinedKind: {
47 auto &D = cast<Defined>(Sym);
Rafael Espindola5616adf2017-03-08 22:36:28 +000048 SectionBase *IS = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:35 +000049
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000050 // According to the ELF spec reference to a local symbol from outside
51 // the group are not allowed. Unfortunately .eh_frame breaks that rule
52 // and must be treated specially. For now we just replace the symbol with
53 // 0.
Rafael Espindola774ea7d2017-02-23 16:49:07 +000054 if (IS == &InputSection::Discarded)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000055 return 0;
56
Rui Ueyamab5a69702016-02-01 21:00:35 +000057 // This is an absolute symbol.
Sean Silva902ae3c2016-12-15 00:57:53 +000058 if (!IS)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000059 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000060
Rafael Espindolaf4d6e8c2018-04-19 17:26:50 +000061 IS = IS->Repl;
62
Rafael Espindola9371bab2017-03-08 15:21:32 +000063 uint64_t Offset = D.Value;
Sean Silvaa9ba4502017-02-28 08:32:56 +000064
65 // An object in an SHF_MERGE section might be referenced via a
66 // section symbol (as a hack for reducing the number of local
67 // symbols).
Sean Silvad4e60622017-03-01 04:44:04 +000068 // Depending on the addend, the reference via a section symbol
69 // refers to a different object in the merge section.
70 // Since the objects in the merge section are not necessarily
71 // contiguous in the output, the addend can thus affect the final
72 // VA in a non-linear way.
73 // To make this work, we incorporate the addend into the section
74 // offset (and zero out the addend for later processing) so that
75 // we find the right object in the section.
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000076 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000077 Offset += Addend;
78 Addend = 0;
79 }
Sean Silvaa9ba4502017-02-28 08:32:56 +000080
Sean Silva6ab39262017-02-28 09:01:58 +000081 // In the typical case, this is actually very simple and boils
82 // down to adding together 3 numbers:
83 // 1. The address of the output section.
84 // 2. The offset of the input section within the output section.
85 // 3. The offset within the input section (this addition happens
86 // inside InputSection::getOffset).
87 //
88 // If you understand the data structures involved with this next
89 // line (and how they get built), then you have a pretty good
90 // understanding of the linker.
Rafael Espindola4f058a22018-03-24 00:35:11 +000091 uint64_t VA = IS->getVA(Offset);
Sean Silva6ab39262017-02-28 09:01:58 +000092
George Rimar6a3b1542016-10-04 08:52:51 +000093 if (D.isTls() && !Config->Relocatable) {
Ryan Prichard1c33d142018-09-18 00:24:48 +000094 // Use the address of the TLS segment's first section rather than the
95 // segment's address, because segment addresses aren't initialized until
96 // after sections are finalized. (e.g. Measuring the size of .rela.dyn
97 // for Android relocation packing requires knowing TLS symbol addresses
98 // during section finalization.)
99 if (!Out::TlsPhdr || !Out::TlsPhdr->FirstSec)
Rafael Espindoladfebd362017-11-29 22:47:35 +0000100 fatal(toString(D.File) +
Peter Collingbourne3e2abde2017-07-14 00:22:46 +0000101 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Ryan Prichard1c33d142018-09-18 00:24:48 +0000102 return VA - Out::TlsPhdr->FirstSec->Addr;
George Rimar6a3b1542016-10-04 08:52:51 +0000103 }
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000104 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000105 }
Rafael Espindolaab0cce52018-04-26 17:58:58 +0000106 case Symbol::SharedKind:
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000107 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000108 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000109 case Symbol::LazyArchiveKind:
110 case Symbol::LazyObjectKind:
Chih-Hung Hsieh73e04842018-09-11 23:00:36 +0000111 assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
112 return 0;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000113 }
George Rimar777f9632016-03-12 08:31:34 +0000114 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35 +0000115}
116
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000117uint64_t Symbol::getVA(int64_t Addend) const {
George Rimarf64618a2017-03-17 11:56:54 +0000118 uint64_t OutVA = getSymVA(*this, Addend);
Rafael Espindola8381c562016-03-17 23:36:19 +0000119 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000120}
121
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000122uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000123
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000124uint64_t Symbol::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000125 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000126}
127
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000128uint64_t Symbol::getGotPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55 +0000129 if (this->IsInIgot)
George Rimar4670bb02017-03-16 12:58:11 +0000130 return InX::IgotPlt->getVA() + getGotPltOffset();
131 return InX::GotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56 +0000132}
133
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000134uint64_t Symbol::getGotPltOffset() const {
Rafael Espindolaf4a9d562018-04-26 16:09:30 +0000135 if (IsInIgot)
136 return PltIndex * Target->GotPltEntrySize;
137 return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000138}
139
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000140uint64_t Symbol::getPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55 +0000141 if (this->IsInIplt)
George Rimarf64618a2017-03-17 11:56:54 +0000142 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
Rafael Espindola74acdfa2018-03-14 17:41:34 +0000143 return InX::Plt->getVA() + Target->getPltEntryOffset(PltIndex);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000144}
145
Rafael Espindolaab0cce52018-04-26 17:58:58 +0000146uint64_t Symbol::getPltOffset() const {
147 assert(!this->IsInIplt);
148 return Target->getPltEntryOffset(PltIndex);
149}
150
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000151uint64_t Symbol::getSize() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000152 if (const auto *DR = dyn_cast<Defined>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000153 return DR->Size;
George Rimar904ed692018-07-17 11:35:28 +0000154 return cast<SharedSymbol>(this)->Size;
Rui Ueyama512c61d2016-02-03 00:12:24 +0000155}
156
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000157OutputSection *Symbol::getOutputSection() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000158 if (auto *S = dyn_cast<Defined>(this)) {
Rafael Espindolaf4fb5fd2017-12-13 22:59:23 +0000159 if (auto *Sec = S->Section)
Rafael Espindolaf4d6e8c2018-04-19 17:26:50 +0000160 return Sec->Repl->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42 +0000161 return nullptr;
162 }
Rui Ueyama968db482017-02-28 04:02:42 +0000163 return nullptr;
164}
165
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000166// If a symbol name contains '@', the characters after that is
167// a symbol version name. This function parses that.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000168void Symbol::parseSymbolVersion() {
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000169 StringRef S = getName();
170 size_t Pos = S.find('@');
171 if (Pos == 0 || Pos == StringRef::npos)
172 return;
173 StringRef Verstr = S.substr(Pos + 1);
174 if (Verstr.empty())
175 return;
176
177 // Truncate the symbol name so that it doesn't include the version string.
Rafael Espindola1eeb2622018-04-25 21:44:37 +0000178 NameSize = Pos;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000179
Rafael Espindola1d6d1b42017-01-17 16:08:06 +0000180 // If this is not in this DSO, it is not a definition.
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000181 if (!isDefined())
Rafael Espindola2756e042017-01-06 22:30:35 +0000182 return;
183
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000184 // '@@' in a symbol name means the default version.
185 // It is usually the most recent one.
186 bool IsDefault = (Verstr[0] == '@');
187 if (IsDefault)
188 Verstr = Verstr.substr(1);
189
190 for (VersionDefinition &Ver : Config->VersionDefinitions) {
191 if (Ver.Name != Verstr)
192 continue;
193
194 if (IsDefault)
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000195 VersionId = Ver.Id;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000196 else
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000197 VersionId = Ver.Id | VERSYM_HIDDEN;
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000198 return;
199 }
200
201 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13 +0000202 // Usually version script is not provided when linking executable,
203 // but we may still want to override a versioned symbol from DSO,
Peter Smith796fb992018-05-14 10:13:56 +0000204 // so we do not report error in this case. We also do not error
205 // if the symbol has a local version as it won't be in the dynamic
206 // symbol table.
207 if (Config->Shared && VersionId != VER_NDX_LOCAL)
Rafael Espindoladfebd362017-11-29 22:47:35 +0000208 error(toString(File) + ": symbol " + S + " has undefined version " +
George Rimar4d2f97622017-07-04 13:19:13 +0000209 Verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40 +0000210}
211
Rui Ueyama24a47202018-04-03 02:06:57 +0000212InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000213
Peter Collingbourne98930112018-08-08 23:48:12 +0000214MemoryBufferRef LazyArchive::getMemberBuffer() {
215 Archive::Child C = CHECK(
216 Sym.getMember(), "could not get the member for symbol " + Sym.getName());
217
218 return CHECK(C.getMemoryBufferRef(),
219 "could not get the buffer for the member defining symbol " +
220 Sym.getName());
221}
222
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000223uint8_t Symbol::computeBinding() const {
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000224 if (Config->Relocatable)
225 return Binding;
Peter Collingbournedadcc172016-04-22 18:42:48 +0000226 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000227 return STB_LOCAL;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000228 if (VersionId == VER_NDX_LOCAL && isDefined())
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000229 return STB_LOCAL;
Rui Ueyamaaad2e322018-02-02 21:44:06 +0000230 if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE)
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000231 return STB_GLOBAL;
232 return Binding;
233}
234
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000235bool Symbol::includeInDynsym() const {
Rafael Espindolae05e2f82017-09-15 18:05:02 +0000236 if (!Config->HasDynSymTab)
237 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13 +0000238 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000239 return false;
Peter Collingbourneb472aa02017-11-06 04:39:07 +0000240 if (!isDefined())
Rafael Espindola3d9f1c02017-09-13 20:43:04 +0000241 return true;
Rafael Espindolac57f8cd2017-09-13 20:47:53 +0000242 return ExportDynamic;
Rafael Espindolaae605c12016-04-21 20:35:25 +0000243}
Rui Ueyama69c778c2016-07-17 17:50:09 +0000244
245// Print out a log message for --trace-symbol.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000246void elf::printTraceSymbol(Symbol *Sym) {
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000247 std::string S;
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000248 if (Sym->isUndefined())
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000249 S = ": reference to ";
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000250 else if (Sym->isLazy())
Rafael Espindolabc2b1652017-10-27 18:30:11 +0000251 S = ": lazy definition of ";
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000252 else if (Sym->isShared())
Rafael Espindolabc2b1652017-10-27 18:30:11 +0000253 S = ": shared definition of ";
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +0000254 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
Peter Collingbourne6c55a702017-11-06 04:33:58 +0000255 S = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09 +0000256 else
Rui Ueyamae6e206d2017-02-21 23:22:56 +0000257 S = ": definition of ";
258
Rui Ueyamaf1f00842017-10-31 16:07:41 +0000259 message(toString(Sym->File) + S + Sym->getName());
Rui Ueyama69c778c2016-07-17 17:50:09 +0000260}
261
Michael J. Spencerb8427252018-04-17 23:30:05 +0000262void elf::warnUnorderableSymbol(const Symbol *Sym) {
263 if (!Config->WarnSymbolOrdering)
264 return;
Rui Ueyamab774c3c2018-04-26 01:38:29 +0000265
Michael J. Spencerb8427252018-04-17 23:30:05 +0000266 const InputFile *File = Sym->File;
267 auto *D = dyn_cast<Defined>(Sym);
Rui Ueyamab774c3c2018-04-26 01:38:29 +0000268
269 auto Warn = [&](StringRef S) { warn(toString(File) + S + Sym->getName()); };
270
Michael J. Spencerb8427252018-04-17 23:30:05 +0000271 if (Sym->isUndefined())
Rui Ueyamab774c3c2018-04-26 01:38:29 +0000272 Warn(": unable to order undefined symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05 +0000273 else if (Sym->isShared())
Rui Ueyamab774c3c2018-04-26 01:38:29 +0000274 Warn(": unable to order shared symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05 +0000275 else if (D && !D->Section)
Rui Ueyamab774c3c2018-04-26 01:38:29 +0000276 Warn(": unable to order absolute symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05 +0000277 else if (D && isa<OutputSection>(D->Section))
Rui Ueyamab774c3c2018-04-26 01:38:29 +0000278 Warn(": unable to order synthetic symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05 +0000279 else if (D && !D->Section->Repl->Live)
Rui Ueyamab774c3c2018-04-26 01:38:29 +0000280 Warn(": unable to order discarded symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05 +0000281}
282
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000283// Returns a symbol for an error message.
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000284std::string lld::toString(const Symbol &B) {
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000285 if (Config->Demangle)
Rui Ueyama53fe4692017-11-28 02:15:26 +0000286 if (Optional<std::string> S = demangleItanium(B.getName()))
Rui Ueyama4c5b8ce2016-12-07 23:17:05 +0000287 return *S;
Rui Ueyamaa3ac1732016-11-24 20:24:18 +0000288 return B.getName();
289}