blob: 1996400978148c578e6424d0d6010c723b91832d [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;
Rui Ueyama80474a22017-02-28 19:29:5541
Rui Ueyama48882242017-11-04 00:31:0442static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
43 switch (Sym.kind()) {
Peter Collingbournee9a9e0a2017-11-06 04:35:3144 case Symbol::DefinedKind: {
45 auto &D = cast<Defined>(Sym);
Rafael Espindola5616adf2017-03-08 22:36:2846 SectionBase *IS = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:3547
Rafael Espindolaccfe3cb2016-04-04 14:04:1648 // According to the ELF spec reference to a local symbol from outside
49 // the group are not allowed. Unfortunately .eh_frame breaks that rule
50 // and must be treated specially. For now we just replace the symbol with
51 // 0.
Rafael Espindola774ea7d2017-02-23 16:49:0752 if (IS == &InputSection::Discarded)
Rafael Espindolaccfe3cb2016-04-04 14:04:1653 return 0;
54
Rui Ueyamab5a69702016-02-01 21:00:3555 // This is an absolute symbol.
Sean Silva902ae3c2016-12-15 00:57:5356 if (!IS)
Rafael Espindolaccfe3cb2016-04-04 14:04:1657 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:3558
Rafael Espindola9371bab2017-03-08 15:21:3259 uint64_t Offset = D.Value;
Sean Silvaa9ba4502017-02-28 08:32:5660
61 // An object in an SHF_MERGE section might be referenced via a
62 // section symbol (as a hack for reducing the number of local
63 // symbols).
Sean Silvad4e60622017-03-01 04:44:0464 // Depending on the addend, the reference via a section symbol
65 // refers to a different object in the merge section.
66 // Since the objects in the merge section are not necessarily
67 // contiguous in the output, the addend can thus affect the final
68 // VA in a non-linear way.
69 // To make this work, we incorporate the addend into the section
70 // offset (and zero out the addend for later processing) so that
71 // we find the right object in the section.
Rafael Espindolaccfe3cb2016-04-04 14:04:1672 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:3773 Offset += Addend;
74 Addend = 0;
75 }
Sean Silvaa9ba4502017-02-28 08:32:5676
Sean Silva6ab39262017-02-28 09:01:5877 // In the typical case, this is actually very simple and boils
78 // down to adding together 3 numbers:
79 // 1. The address of the output section.
80 // 2. The offset of the input section within the output section.
81 // 3. The offset within the input section (this addition happens
82 // inside InputSection::getOffset).
83 //
84 // If you understand the data structures involved with this next
85 // line (and how they get built), then you have a pretty good
86 // understanding of the linker.
Rafael Espindola4f058a22018-03-24 00:35:1187 uint64_t VA = IS->getVA(Offset);
Sean Silva6ab39262017-02-28 09:01:5888
George Rimar6a3b1542016-10-04 08:52:5189 if (D.isTls() && !Config->Relocatable) {
Rui Ueyama9d1bacb12017-02-27 02:31:2690 if (!Out::TlsPhdr)
Rafael Espindoladfebd362017-11-29 22:47:3591 fatal(toString(D.File) +
Peter Collingbourne3e2abde2017-07-14 00:22:4692 " has an STT_TLS symbol but doesn't have an SHF_TLS section");
Rui Ueyama9d1bacb12017-02-27 02:31:2693 return VA - Out::TlsPhdr->p_vaddr;
George Rimar6a3b1542016-10-04 08:52:5194 }
Rafael Espindola1f5b70f2016-03-11 14:21:3795 return VA;
Rui Ueyamab5a69702016-02-01 21:00:3596 }
Rui Ueyamaf52496e2017-11-03 21:21:4797 case Symbol::SharedKind: {
Rui Ueyama48882242017-11-04 00:31:0498 auto &SS = cast<SharedSymbol>(Sym);
Rafael Espindola0afcef22017-08-04 17:43:5499 if (SS.CopyRelSec)
Rafael Espindola4f058a22018-03-24 00:35:11100 return SS.CopyRelSec->getVA(0);
Rui Ueyama007c0022017-03-08 17:24:24101 if (SS.NeedsPltAddr)
Rui Ueyama48882242017-11-04 00:31:04102 return Sym.getPltVA();
Rui Ueyama924b3612017-02-16 06:12:22103 return 0;
Rui Ueyama007c0022017-03-08 17:24:24104 }
Rui Ueyamaf52496e2017-11-03 21:21:47105 case Symbol::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35106 return 0;
Rui Ueyamaf52496e2017-11-03 21:21:47107 case Symbol::LazyArchiveKind:
108 case Symbol::LazyObjectKind:
Rui Ueyama48882242017-11-04 00:31:04109 assert(Sym.IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35110 return 0;
111 }
George Rimar777f9632016-03-12 08:31:34112 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35113}
114
Rui Ueyamaf52496e2017-11-03 21:21:47115uint64_t Symbol::getVA(int64_t Addend) const {
George Rimarf64618a2017-03-17 11:56:54116 uint64_t OutVA = getSymVA(*this, Addend);
Rafael Espindola8381c562016-03-17 23:36:19117 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05118}
119
Rui Ueyamaf52496e2017-11-03 21:21:47120uint64_t Symbol::getGotVA() const { return InX::Got->getVA() + getGotOffset(); }
Rafael Espindola74031ba2016-04-07 15:20:56121
Rui Ueyamaf52496e2017-11-03 21:21:47122uint64_t Symbol::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14123 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35124}
125
Rui Ueyamaf52496e2017-11-03 21:21:47126uint64_t Symbol::getGotPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55127 if (this->IsInIgot)
George Rimar4670bb02017-03-16 12:58:11128 return InX::IgotPlt->getVA() + getGotPltOffset();
129 return InX::GotPlt->getVA() + getGotPltOffset();
Rafael Espindola74031ba2016-04-07 15:20:56130}
131
Rui Ueyamaf52496e2017-11-03 21:21:47132uint64_t Symbol::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14133 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35134}
135
Rui Ueyamaf52496e2017-11-03 21:21:47136uint64_t Symbol::getPltVA() const {
Peter Smithbaffdb82016-12-08 12:58:55137 if (this->IsInIplt)
George Rimarf64618a2017-03-17 11:56:54138 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
Rafael Espindola74acdfa2018-03-14 17:41:34139 return InX::Plt->getVA() + Target->getPltEntryOffset(PltIndex);
Rui Ueyamab5a69702016-02-01 21:00:35140}
141
Rui Ueyamaf52496e2017-11-03 21:21:47142uint64_t Symbol::getSize() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31143 if (const auto *DR = dyn_cast<Defined>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16144 return DR->Size;
Rui Ueyama4076fa12017-02-26 23:35:34145 if (const auto *S = dyn_cast<SharedSymbol>(this))
Rui Ueyama7f9694a42017-10-28 20:15:56146 return S->Size;
Rui Ueyama512c61d2016-02-03 00:12:24147 return 0;
148}
149
Rui Ueyamaf52496e2017-11-03 21:21:47150OutputSection *Symbol::getOutputSection() const {
Peter Collingbournee9a9e0a2017-11-06 04:35:31151 if (auto *S = dyn_cast<Defined>(this)) {
Rafael Espindolaf4fb5fd2017-12-13 22:59:23152 if (auto *Sec = S->Section)
Rafael Espindola4e82a9e2018-03-23 23:53:01153 return Sec->getOutputSection();
Rui Ueyama968db482017-02-28 04:02:42154 return nullptr;
155 }
156
Rui Ueyama007c0022017-03-08 17:24:24157 if (auto *S = dyn_cast<SharedSymbol>(this)) {
Rafael Espindola0afcef22017-08-04 17:43:54158 if (S->CopyRelSec)
Rafael Espindoladb5e56f2017-05-31 20:17:44159 return S->CopyRelSec->getParent();
Rui Ueyama968db482017-02-28 04:02:42160 return nullptr;
Rui Ueyama007c0022017-03-08 17:24:24161 }
Rui Ueyama968db482017-02-28 04:02:42162
Rui Ueyama968db482017-02-28 04:02:42163 return nullptr;
164}
165
Rui Ueyama35fa6c52016-11-23 05:48:40166// 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:47168void Symbol::parseSymbolVersion() {
Rui Ueyama35fa6c52016-11-23 05:48:40169 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.
Rui Ueyamaa13efc22016-11-29 18:05:04178 Name = {S.data(), Pos};
Rui Ueyama35fa6c52016-11-23 05:48:40179
Rafael Espindola1d6d1b42017-01-17 16:08:06180 // If this is not in this DSO, it is not a definition.
Peter Collingbourneb472aa02017-11-06 04:39:07181 if (!isDefined())
Rafael Espindola2756e042017-01-06 22:30:35182 return;
183
Rui Ueyama35fa6c52016-11-23 05:48:40184 // '@@' 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:41195 VersionId = Ver.Id;
Rui Ueyama35fa6c52016-11-23 05:48:40196 else
Rui Ueyamaf1f00842017-10-31 16:07:41197 VersionId = Ver.Id | VERSYM_HIDDEN;
Rui Ueyama35fa6c52016-11-23 05:48:40198 return;
199 }
200
201 // It is an error if the specified version is not defined.
George Rimar4d2f97622017-07-04 13:19:13202 // Usually version script is not provided when linking executable,
203 // but we may still want to override a versioned symbol from DSO,
204 // so we do not report error in this case.
205 if (Config->Shared)
Rafael Espindoladfebd362017-11-29 22:47:35206 error(toString(File) + ": symbol " + S + " has undefined version " +
George Rimar4d2f97622017-07-04 13:19:13207 Verstr);
Rui Ueyama35fa6c52016-11-23 05:48:40208}
209
Rui Ueyama24a47202018-04-03 02:06:57210InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
Rui Ueyamaf8baa662016-04-07 19:24:51211
Rui Ueyamaf52496e2017-11-03 21:21:47212uint8_t Symbol::computeBinding() const {
Rafael Espindolab7e2ee22017-01-10 17:08:13213 if (Config->Relocatable)
214 return Binding;
Peter Collingbournedadcc172016-04-22 18:42:48215 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolab7e2ee22017-01-10 17:08:13216 return STB_LOCAL;
Peter Collingbourneb472aa02017-11-06 04:39:07217 if (VersionId == VER_NDX_LOCAL && isDefined())
Rafael Espindolab7e2ee22017-01-10 17:08:13218 return STB_LOCAL;
Rui Ueyamaaad2e322018-02-02 21:44:06219 if (!Config->GnuUnique && Binding == STB_GNU_UNIQUE)
Rafael Espindolab7e2ee22017-01-10 17:08:13220 return STB_GLOBAL;
221 return Binding;
222}
223
Rui Ueyamaf52496e2017-11-03 21:21:47224bool Symbol::includeInDynsym() const {
Rafael Espindolae05e2f82017-09-15 18:05:02225 if (!Config->HasDynSymTab)
226 return false;
Rafael Espindolab7e2ee22017-01-10 17:08:13227 if (computeBinding() == STB_LOCAL)
Rafael Espindolaae605c12016-04-21 20:35:25228 return false;
Peter Collingbourneb472aa02017-11-06 04:39:07229 if (!isDefined())
Rafael Espindola3d9f1c02017-09-13 20:43:04230 return true;
Rafael Espindolac57f8cd2017-09-13 20:47:53231 return ExportDynamic;
Rafael Espindolaae605c12016-04-21 20:35:25232}
Rui Ueyama69c778c2016-07-17 17:50:09233
234// Print out a log message for --trace-symbol.
Rui Ueyamaf52496e2017-11-03 21:21:47235void elf::printTraceSymbol(Symbol *Sym) {
Rui Ueyamae6e206d2017-02-21 23:22:56236 std::string S;
Rui Ueyamaf1f00842017-10-31 16:07:41237 if (Sym->isUndefined())
Rui Ueyamae6e206d2017-02-21 23:22:56238 S = ": reference to ";
Rui Ueyamaf1f00842017-10-31 16:07:41239 else if (Sym->isLazy())
Rafael Espindolabc2b1652017-10-27 18:30:11240 S = ": lazy definition of ";
Rui Ueyamaf1f00842017-10-31 16:07:41241 else if (Sym->isShared())
Rafael Espindolabc2b1652017-10-27 18:30:11242 S = ": shared definition of ";
Peter Collingbournee9a9e0a2017-11-06 04:35:31243 else if (dyn_cast_or_null<BssSection>(cast<Defined>(Sym)->Section))
Peter Collingbourne6c55a702017-11-06 04:33:58244 S = ": common definition of ";
Rui Ueyama69c778c2016-07-17 17:50:09245 else
Rui Ueyamae6e206d2017-02-21 23:22:56246 S = ": definition of ";
247
Rui Ueyamaf1f00842017-10-31 16:07:41248 message(toString(Sym->File) + S + Sym->getName());
Rui Ueyama69c778c2016-07-17 17:50:09249}
250
Michael J. Spencerb8427252018-04-17 23:30:05251void elf::warnUnorderableSymbol(const Symbol *Sym) {
252 if (!Config->WarnSymbolOrdering)
253 return;
254 const InputFile *File = Sym->File;
255 auto *D = dyn_cast<Defined>(Sym);
256 if (Sym->isUndefined())
257 warn(toString(File) +
258 ": unable to order undefined symbol: " + Sym->getName());
259 else if (Sym->isShared())
260 warn(toString(File) + ": unable to order shared symbol: " + Sym->getName());
261 else if (D && !D->Section)
262 warn(toString(File) +
263 ": unable to order absolute symbol: " + Sym->getName());
264 else if (D && isa<OutputSection>(D->Section))
265 warn(toString(File) +
266 ": unable to order synthetic symbol: " + Sym->getName());
267 else if (D && !D->Section->Repl->Live)
268 warn(toString(File) +
269 ": unable to order discarded symbol: " + Sym->getName());
270}
271
Rui Ueyamaa3ac1732016-11-24 20:24:18272// Returns a symbol for an error message.
Rui Ueyamaf52496e2017-11-03 21:21:47273std::string lld::toString(const Symbol &B) {
Rui Ueyamaa3ac1732016-11-24 20:24:18274 if (Config->Demangle)
Rui Ueyama53fe4692017-11-28 02:15:26275 if (Optional<std::string> S = demangleItanium(B.getName()))
Rui Ueyama4c5b8ce2016-12-07 23:17:05276 return *S;
Rui Ueyamaa3ac1732016-11-24 20:24:18277 return B.getName();
278}