blob: 6f7ac8b8a5bca0192b5d4ea28d2b92721167edf8 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:071//===- 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: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"
Michael J. Spencer1b348a62015-09-04 22:28:1019#include "llvm/ADT/STLExtras.h"
Eugene Leviantc958d8d2016-10-12 08:19:3020#include "llvm/Support/Path.h"
Rui Ueyamac72ba3a2016-11-23 04:57:2521#include <cstring>
Michael J. Spencer1b348a62015-09-04 22:28:1022
23using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0724using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5225using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0726
27using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:5428using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:0729
Peter Collingbournee9a9e0a2017-11-06 04:35:3130Defined *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: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
George Rimar6a3b1542016-10-04 08:52:5192 if (D.isTls() && !Config->Relocatable) {
Rui Ueyama9d1bacb12017-02-27 02:31:2693 if (!Out::TlsPhdr)
Rafael Espindoladfebd362017-11-29 22:47:3594 fatal(toString(D.File) +
Peter Collingbourne3e2abde2017-07-14 00:22:4695 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Rui Ueyama9d1bacb12017-02-27 02:31:2696 return VA - Out::TlsPhdr->p_vaddr;
George Rimar6a3b1542016-10-04 08:52:5197 }
Rafael Espindola1f5b70f2016-03-11 14:21:3798 return VA;
Rui Ueyamab5a69702016-02-01 21:00:3599 }
Rafael Espindolaab0cce52018-04-26 17:58:58100 case Symbol::SharedKind:
Rui Ueyamaf52496e2017-11-03 21:21:47101 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35102 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47103 case Symbol::LazyArchiveKind:
104 case Symbol::LazyObjectKind:
Rafael Espindola047f8572018-04-25 20:46:08105 llvm_unreachable("lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35106 }
George Rimar777f9632016-03-12 08:31:34107 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35108}
109
Rui Ueyamaf52496e2017-11-03 21:21:47110uint64_t Symbol::getVA(int64_t Addend) const {
George Rimarf64618a2017-03-17 11:56:54111 uint64_t OutVA = getSymVA(*this, Addend);
Rafael Espindola8381c562016-03-17 23:36:19112 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05113}
114
Rui Ueyamaf52496e2017-11-03 21:21:47115uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
Rafael Espindola74031ba2016-04-07 15:20:56116
Rui Ueyamaf52496e2017-11-03 21:21:47117uint64_t Symbol::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14118 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35119}
120
Rui Ueyamaf52496e2017-11-03 21:21:47121uint64_t Symbol::getGotPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55122 if (this->IsInIgot)
George Rimar4670bb02017-03-16 12:58:11123 return InX::IgotPlt->getVA() + getGotPltOffset();
124 return InX::GotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56125}
126
Rui Ueyamaf52496e2017-11-03 21:21:47127uint64_t Symbol::getGotPltOffset() const {
Rafael Espindolaf4a9d562018-04-26 16:09:30128 if (IsInIgot)
129 return PltIndex * Target->GotPltEntrySize;
130 return (PltIndex + Target->GotPltHeaderEntriesNum) * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35131}
132
Rui Ueyamaf52496e2017-11-03 21:21:47133uint64_t Symbol::getPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55134 if (this->IsInIplt)
George Rimarf64618a2017-03-17 11:56:54135 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
Rafael Espindola74acdfa2018-03-14 17:41:34136 return InX::Plt->getVA() + Target->getPltEntryOffset(PltIndex);
Rui Ueyamab5a69702016-02-01 21:00:35137}
138
Rafael Espindolaab0cce52018-04-26 17:58:58139uint64_t Symbol::getPltOffset() const {
140 assert(!this->IsInIplt);
141 return Target->getPltEntryOffset(PltIndex);
142}
143
Rui Ueyamaf52496e2017-11-03 21:21:47144uint64_t Symbol::getSize() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31145 if (const auto *DR = dyn_cast<Defined>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16146 return DR->Size;
Rui Ueyama4076fa12017-02-26 23:35:34147 if (const auto *S = dyn_cast<SharedSymbol>(this))
Rui Ueyama7f9694a42017-10-28 20:15:56148 return S->Size;
Rui Ueyama512c61d2016-02-03 00:12:24149 return 0;
150}
151
Rui Ueyamaf52496e2017-11-03 21:21:47152OutputSection *Symbol::getOutputSection() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31153 if (auto *S = dyn_cast<Defined>(this)) {
Rafael Espindolaf4fb5fd2017-12-13 22:59:23154 if (auto *Sec = S->Section)
Rafael Espindolaf4d6e8c2018-04-19 17:26:50155 return Sec->Repl->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42156 return nullptr;
157 }
Rui Ueyama968db482017-02-28 04:02:42158 return nullptr;
159}
160
Rui Ueyama35fa6c52016-11-23 05:48:40161// If a symbol name contains '@', the characters after that is
162// a symbol version name. This function parses that.
Rui Ueyamaf52496e2017-11-03 21:21:47163void Symbol::parseSymbolVersion() {
Rui Ueyama35fa6c52016-11-23 05:48:40164 StringRef S = getName();
165 size_t Pos = S.find('@');
166 if (Pos == 0 || Pos == StringRef::npos)
167 return;
168 StringRef Verstr = S.substr(Pos + 1);
169 if (Verstr.empty())
170 return;
171
172 // Truncate the symbol name so that it doesn't include the version string.
Rafael Espindola1eeb2622018-04-25 21:44:37173 NameSize = Pos;
Rui Ueyama35fa6c52016-11-23 05:48:40174
Rafael Espindola1d6d1b42017-01-17 16:08:06175 // If this is not in this DSO, it is not a definition.
Peter Collingbourneb472aa02017-11-06 04:39:07176 if (!isDefined())
Rafael Espindola2756e042017-01-06 22:30:35177 return;
178
Rui Ueyama35fa6c52016-11-23 05:48:40179 // '@@' in a symbol name means the default version.
180 // It is usually the most recent one.
181 bool IsDefault = (Verstr[0] == '@');
182 if (IsDefault)
183 Verstr = Verstr.substr(1);
184
185 for (VersionDefinition &Ver : Config->VersionDefinitions) {
186 if (Ver.Name != Verstr)
187 continue;
188
189 if (IsDefault)
Rui Ueyamaf1f00842017-10-31 16:07:41190 VersionId = Ver.Id;
Rui Ueyama35fa6c52016-11-23 05:48:40191 else
Rui Ueyamaf1f00842017-10-31 16:07:41192 VersionId = Ver.Id | VERSYM_HIDDEN;
Rui Ueyama35fa6c52016-11-23 05:48:40193 return;
194 }
195
196 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13197 // Usually version script is not provided when linking executable,
198 // but we may still want to override a versioned symbol from DSO,
199 // so we do not report error in this case.
200 if (Config->Shared)
Rafael Espindoladfebd362017-11-29 22:47:35201 error(toString(File) + ": symbol " + S + " has undefined version " +
George Rimar4d2f97622017-07-04 13:19:13202 Verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40203}
204
Rui Ueyama24a47202018-04-03 02:06:57205InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
Rui Ueyamaf8baa662016-04-07 19:24:51206
Rui Ueyamaf52496e2017-11-03 21:21:47207uint8_t Symbol::computeBinding() const {
Rafael Espindolab7e2ee22017-01-10 17:08:13208 if (Config->Relocatable)
209 return Binding;
Peter Collingbournedadcc172016-04-22 18:42:48210 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolab7e2ee22017-01-10 17:08:13211 return STB_LOCAL;
Peter Collingbourneb472aa02017-11-06 04:39:07212 if (VersionId == VER_NDX_LOCAL && isDefined())
Rafael Espindolab7e2ee22017-01-10 17:08:13213 return STB_LOCAL;
Rui Ueyamaaad2e322018-02-02 21:44:06214 if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE)
Rafael Espindolab7e2ee22017-01-10 17:08:13215 return STB_GLOBAL;
216 return Binding;
217}
218
Rui Ueyamaf52496e2017-11-03 21:21:47219bool Symbol::includeInDynsym() const {
Rafael Espindolae05e2f82017-09-15 18:05:02220 if (!Config->HasDynSymTab)
221 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13222 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25223 return false;
Peter Collingbourneb472aa02017-11-06 04:39:07224 if (!isDefined())
Rafael Espindola3d9f1c02017-09-13 20:43:04225 return true;
Rafael Espindolac57f8cd2017-09-13 20:47:53226 return ExportDynamic;
Rafael Espindolaae605c12016-04-21 20:35:25227}
Rui Ueyama69c778c2016-07-17 17:50:09228
229// Print out a log message for --trace-symbol.
Rui Ueyamaf52496e2017-11-03 21:21:47230void elf::printTraceSymbol(Symbol *Sym) {
Rui Ueyamae6e206d2017-02-21 23:22:56231 std::string S;
Rui Ueyamaf1f00842017-10-31 16:07:41232 if (Sym->isUndefined())
Rui Ueyamae6e206d2017-02-21 23:22:56233 S = ": reference to ";
Rui Ueyamaf1f00842017-10-31 16:07:41234 else if (Sym->isLazy())
Rafael Espindolabc2b1652017-10-27 18:30:11235 S = ": lazy definition of ";
Rui Ueyamaf1f00842017-10-31 16:07:41236 else if (Sym->isShared())
Rafael Espindolabc2b1652017-10-27 18:30:11237 S = ": shared definition of ";
Peter Collingbournee9a9e0a2017-11-06 04:35:31238 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
Peter Collingbourne6c55a702017-11-06 04:33:58239 S = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09240 else
Rui Ueyamae6e206d2017-02-21 23:22:56241 S = ": definition of ";
242
Rui Ueyamaf1f00842017-10-31 16:07:41243 message(toString(Sym->File) + S + Sym->getName());
Rui Ueyama69c778c2016-07-17 17:50:09244}
245
Michael J. Spencerb8427252018-04-17 23:30:05246void elf::warnUnorderableSymbol(const Symbol *Sym) {
247 if (!Config->WarnSymbolOrdering)
248 return;
Rui Ueyamab774c3c2018-04-26 01:38:29249
Michael J. Spencerb8427252018-04-17 23:30:05250 const InputFile *File = Sym->File;
251 auto *D = dyn_cast<Defined>(Sym);
Rui Ueyamab774c3c2018-04-26 01:38:29252
253 auto Warn = [&](StringRef S) { warn(toString(File) + S + Sym->getName()); };
254
Michael J. Spencerb8427252018-04-17 23:30:05255 if (Sym->isUndefined())
Rui Ueyamab774c3c2018-04-26 01:38:29256 Warn(": unable to order undefined symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05257 else if (Sym->isShared())
Rui Ueyamab774c3c2018-04-26 01:38:29258 Warn(": unable to order shared symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05259 else if (D && !D->Section)
Rui Ueyamab774c3c2018-04-26 01:38:29260 Warn(": unable to order absolute symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05261 else if (D && isa<OutputSection>(D->Section))
Rui Ueyamab774c3c2018-04-26 01:38:29262 Warn(": unable to order synthetic symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05263 else if (D && !D->Section->Repl->Live)
Rui Ueyamab774c3c2018-04-26 01:38:29264 Warn(": unable to order discarded symbol: ");
Michael J. Spencerb8427252018-04-17 23:30:05265}
266
Rui Ueyamaa3ac1732016-11-24 20:24:18267// Returns a symbol for an error message.
Rui Ueyamaf52496e2017-11-03 21:21:47268std::string lld::toString(const Symbol &B) {
Rui Ueyamaa3ac1732016-11-24 20:24:18269 if (Config->Demangle)
Rui Ueyama53fe4692017-11-28 02:15:26270 if (Optional<std::string> S = demangleItanium(B.getName()))
Rui Ueyama4c5b8ce2016-12-07 23:17:05271 return *S;
Rui Ueyamaa3ac1732016-11-24 20:24:18272 return B.getName();
273}