blob: d3935f66178d2d263d3763fcf1e9ee234c97ff8d [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 }
Rui Ueyamaf52496e2017-11-03 21:21:47100 case Symbol::SharedKind: {
Rui Ueyama48882242017-11-04 00:31:04101 auto &SS = cast<SharedSymbol>(Sym);
Rafael Espindola0afcef22017-08-04 17:43:54102 if (SS.CopyRelSec)
Rafael Espindola4f058a22018-03-24 00:35:11103 return SS.CopyRelSec->getVA(0);
Rui Ueyama007c0022017-03-08 17:24:24104 if (SS.NeedsPltAddr)
Rui Ueyama48882242017-11-04 00:31:04105 return Sym.getPltVA();
Rui Ueyama924b3612017-02-16 06:12:22106 return 0;
Rui Ueyama007c0022017-03-08 17:24:24107 }
Rui Ueyamaf52496e2017-11-03 21:21:47108 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35109 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47110 case Symbol::LazyArchiveKind:
111 case Symbol::LazyObjectKind:
Rafael Espindola047f8572018-04-25 20:46:08112 llvm_unreachable("lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35113 }
George Rimar777f9632016-03-12 08:31:34114 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35115}
116
Rui Ueyamaf52496e2017-11-03 21:21:47117uint64_t Symbol::getVA(int64_t Addend) const {
George Rimarf64618a2017-03-17 11:56:54118 uint64_t OutVA = getSymVA(*this, Addend);
Rafael Espindola8381c562016-03-17 23:36:19119 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05120}
121
Rui Ueyamaf52496e2017-11-03 21:21:47122uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
Rafael Espindola74031ba2016-04-07 15:20:56123
Rui Ueyamaf52496e2017-11-03 21:21:47124uint64_t Symbol::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14125 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35126}
127
Rui Ueyamaf52496e2017-11-03 21:21:47128uint64_t Symbol::getGotPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55129 if (this->IsInIgot)
George Rimar4670bb02017-03-16 12:58:11130 return InX::IgotPlt->getVA() + getGotPltOffset();
131 return InX::GotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56132}
133
Rui Ueyamaf52496e2017-11-03 21:21:47134uint64_t Symbol::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14135 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35136}
137
Rui Ueyamaf52496e2017-11-03 21:21:47138uint64_t Symbol::getPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55139 if (this->IsInIplt)
George Rimarf64618a2017-03-17 11:56:54140 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
Rafael Espindola74acdfa2018-03-14 17:41:34141 return InX::Plt->getVA() + Target->getPltEntryOffset(PltIndex);
Rui Ueyamab5a69702016-02-01 21:00:35142}
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 }
158
Rui Ueyama007c0022017-03-08 17:24:24159 if (auto *S = dyn_cast<SharedSymbol>(this)) {
Rafael Espindola0afcef22017-08-04 17:43:54160 if (S->CopyRelSec)
Rafael Espindoladb5e56f2017-05-31 20:17:44161 return S->CopyRelSec->getParent();
Rui Ueyama968db482017-02-28 04:02:42162 return nullptr;
Rui Ueyama007c0022017-03-08 17:24:24163 }
Rui Ueyama968db482017-02-28 04:02:42164
Rui Ueyama968db482017-02-28 04:02:42165 return nullptr;
166}
167
Rui Ueyama35fa6c52016-11-23 05:48:40168// If a symbol name contains '@', the characters after that is
169// a symbol version name. This function parses that.
Rui Ueyamaf52496e2017-11-03 21:21:47170void Symbol::parseSymbolVersion() {
Rui Ueyama35fa6c52016-11-23 05:48:40171 StringRef S = getName();
172 size_t Pos = S.find('@');
173 if (Pos == 0 || Pos == StringRef::npos)
174 return;
175 StringRef Verstr = S.substr(Pos + 1);
176 if (Verstr.empty())
177 return;
178
179 // Truncate the symbol name so that it doesn't include the version string.
Rui Ueyamaa13efc22016-11-29 18:05:04180 Name = {S.data(), Pos};
Rui Ueyama35fa6c52016-11-23 05:48:40181
Rafael Espindola1d6d1b42017-01-17 16:08:06182 // If this is not in this DSO, it is not a definition.
Peter Collingbourneb472aa02017-11-06 04:39:07183 if (!isDefined())
Rafael Espindola2756e042017-01-06 22:30:35184 return;
185
Rui Ueyama35fa6c52016-11-23 05:48:40186 // '@@' in a symbol name means the default version.
187 // It is usually the most recent one.
188 bool IsDefault = (Verstr[0] == '@');
189 if (IsDefault)
190 Verstr = Verstr.substr(1);
191
192 for (VersionDefinition &Ver : Config->VersionDefinitions) {
193 if (Ver.Name != Verstr)
194 continue;
195
196 if (IsDefault)
Rui Ueyamaf1f00842017-10-31 16:07:41197 VersionId = Ver.Id;
Rui Ueyama35fa6c52016-11-23 05:48:40198 else
Rui Ueyamaf1f00842017-10-31 16:07:41199 VersionId = Ver.Id | VERSYM_HIDDEN;
Rui Ueyama35fa6c52016-11-23 05:48:40200 return;
201 }
202
203 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13204 // Usually version script is not provided when linking executable,
205 // but we may still want to override a versioned symbol from DSO,
206 // so we do not report error in this case.
207 if (Config->Shared)
Rafael Espindoladfebd362017-11-29 22:47:35208 error(toString(File) + ": symbol " + S + " has undefined version " +
George Rimar4d2f97622017-07-04 13:19:13209 Verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40210}
211
Rui Ueyama24a47202018-04-03 02:06:57212InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
Rui Ueyamaf8baa662016-04-07 19:24:51213
Rui Ueyamaf52496e2017-11-03 21:21:47214uint8_t Symbol::computeBinding() const {
Rafael Espindolab7e2ee22017-01-10 17:08:13215 if (Config->Relocatable)
216 return Binding;
Peter Collingbournedadcc172016-04-22 18:42:48217 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolab7e2ee22017-01-10 17:08:13218 return STB_LOCAL;
Peter Collingbourneb472aa02017-11-06 04:39:07219 if (VersionId == VER_NDX_LOCAL && isDefined())
Rafael Espindolab7e2ee22017-01-10 17:08:13220 return STB_LOCAL;
Rui Ueyamaaad2e322018-02-02 21:44:06221 if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE)
Rafael Espindolab7e2ee22017-01-10 17:08:13222 return STB_GLOBAL;
223 return Binding;
224}
225
Rui Ueyamaf52496e2017-11-03 21:21:47226bool Symbol::includeInDynsym() const {
Rafael Espindolae05e2f82017-09-15 18:05:02227 if (!Config->HasDynSymTab)
228 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13229 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25230 return false;
Peter Collingbourneb472aa02017-11-06 04:39:07231 if (!isDefined())
Rafael Espindola3d9f1c02017-09-13 20:43:04232 return true;
Rafael Espindolac57f8cd2017-09-13 20:47:53233 return ExportDynamic;
Rafael Espindolaae605c12016-04-21 20:35:25234}
Rui Ueyama69c778c2016-07-17 17:50:09235
236// Print out a log message for --trace-symbol.
Rui Ueyamaf52496e2017-11-03 21:21:47237void elf::printTraceSymbol(Symbol *Sym) {
Rui Ueyamae6e206d2017-02-21 23:22:56238 std::string S;
Rui Ueyamaf1f00842017-10-31 16:07:41239 if (Sym->isUndefined())
Rui Ueyamae6e206d2017-02-21 23:22:56240 S = ": reference to ";
Rui Ueyamaf1f00842017-10-31 16:07:41241 else if (Sym->isLazy())
Rafael Espindolabc2b1652017-10-27 18:30:11242 S = ": lazy definition of ";
Rui Ueyamaf1f00842017-10-31 16:07:41243 else if (Sym->isShared())
Rafael Espindolabc2b1652017-10-27 18:30:11244 S = ": shared definition of ";
Peter Collingbournee9a9e0a2017-11-06 04:35:31245 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
Peter Collingbourne6c55a702017-11-06 04:33:58246 S = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09247 else
Rui Ueyamae6e206d2017-02-21 23:22:56248 S = ": definition of ";
249
Rui Ueyamaf1f00842017-10-31 16:07:41250 message(toString(Sym->File) + S + Sym->getName());
Rui Ueyama69c778c2016-07-17 17:50:09251}
252
Michael J. Spencerb8427252018-04-17 23:30:05253void elf::warnUnorderableSymbol(const Symbol *Sym) {
254 if (!Config->WarnSymbolOrdering)
255 return;
256 const InputFile *File = Sym->File;
257 auto *D = dyn_cast<Defined>(Sym);
258 if (Sym->isUndefined())
259 warn(toString(File) +
260 ": unable to order undefined symbol: " + Sym->getName());
261 else if (Sym->isShared())
262 warn(toString(File) + ": unable to order shared symbol: " + Sym->getName());
263 else if (D && !D->Section)
264 warn(toString(File) +
265 ": unable to order absolute symbol: " + Sym->getName());
266 else if (D && isa<OutputSection>(D->Section))
267 warn(toString(File) +
268 ": unable to order synthetic symbol: " + Sym->getName());
269 else if (D && !D->Section->Repl->Live)
270 warn(toString(File) +
271 ": unable to order discarded symbol: " + Sym->getName());
272}
273
Rui Ueyamaa3ac1732016-11-24 20:24:18274// Returns a symbol for an error message.
Rui Ueyamaf52496e2017-11-03 21:21:47275std::string lld::toString(const Symbol &B) {
Rui Ueyamaa3ac1732016-11-24 20:24:18276 if (Config->Demangle)
Rui Ueyama53fe4692017-11-28 02:15:26277 if (Optional<std::string> S = demangleItanium(B.getName()))
Rui Ueyama4c5b8ce2016-12-07 23:17:05278 return *S;
Rui Ueyamaa3ac1732016-11-24 20:24:18279 return B.getName();
280}