blob: 7c9c748a6ea4b8ebb6375d61f977ddc3d05c86fa [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"
Rafael Espindola49a2ca62015-08-06 15:33:19 +000011#include "Error.h"
Michael J. Spencercdae0a42015-07-28 22:58:25 +000012#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:35 +000013#include "InputSection.h"
14#include "OutputSections.h"
15#include "Target.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
Michael J. Spencer1b348a62015-09-04 22:28:10 +000017#include "llvm/ADT/STLExtras.h"
18
19using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000020using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:52 +000021using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
23using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000024using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
Rui Ueyamab5a69702016-02-01 21:00:35 +000026template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000027static typename ELFT::uint getSymVA(const SymbolBody &Body,
28 typename ELFT::uint &Addend) {
Rui Ueyama9328b2c2016-03-14 23:16:09 +000029 typedef typename ELFT::uint uintX_t;
Rafael Espindola87d9f102016-03-11 12:19:05 +000030
31 switch (Body.kind()) {
32 case SymbolBody::DefinedSyntheticKind: {
33 auto &D = cast<DefinedSynthetic<ELFT>>(Body);
Peter Collingbourne6a422592016-05-03 01:21:08 +000034 const OutputSectionBase<ELFT> *Sec = D.Section;
35 if (!Sec)
36 return D.Value;
Simon Atanasyan13f6da12016-03-31 21:26:23 +000037 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
Peter Collingbourne6a422592016-05-03 01:21:08 +000038 return Sec->getVA() + Sec->getSize();
39 return Sec->getVA() + D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000040 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000041 case SymbolBody::DefinedRegularKind: {
42 auto &D = cast<DefinedRegular<ELFT>>(Body);
43 InputSectionBase<ELFT> *SC = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:35 +000044
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000045 // According to the ELF spec reference to a local symbol from outside
46 // the group are not allowed. Unfortunately .eh_frame breaks that rule
47 // and must be treated specially. For now we just replace the symbol with
48 // 0.
49 if (SC == &InputSection<ELFT>::Discarded)
50 return 0;
51
Rui Ueyamab5a69702016-02-01 21:00:35 +000052 // This is an absolute symbol.
53 if (!SC)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000054 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000055
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000056 uintX_t Offset = D.Value;
57 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000058 Offset += Addend;
59 Addend = 0;
60 }
61 uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000062 if (D.isTls())
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000063 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
64 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35 +000065 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000066 case SymbolBody::DefinedCommonKind:
67 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
68 case SymbolBody::SharedKind: {
69 auto &SS = cast<SharedSymbol<ELFT>>(Body);
70 if (!SS.NeedsCopyOrPltAddr)
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000071 return 0;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000072 if (SS.isFunc())
Rafael Espindola87d9f102016-03-11 12:19:05 +000073 return Body.getPltVA<ELFT>();
Rui Ueyama2df72892016-03-13 20:54:38 +000074 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:35 +000075 }
Peter Collingbourne60976ed2016-04-27 00:05:06 +000076 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35 +000077 return 0;
Rui Ueyamaf8baa662016-04-07 19:24:51 +000078 case SymbolBody::LazyArchiveKind:
79 case SymbolBody::LazyObjectKind:
Peter Collingbourne4f952702016-05-01 04:55:03 +000080 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35 +000081 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000082 case SymbolBody::DefinedBitcodeKind:
Davide Italianof6523ae2016-03-29 02:20:10 +000083 llvm_unreachable("should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:35 +000084 }
George Rimar777f9632016-03-12 08:31:34 +000085 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35 +000086}
87
Rui Ueyamab5792b22016-04-04 19:09:08 +000088SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther,
89 uint8_t Type)
Simon Atanasyan41325112016-06-19 21:39:37 +000090 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(true),
91 IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
92 NameOffset(NameOffset) {}
Rafael Espindolaf4765732016-04-06 13:22:41 +000093
Peter Collingbourne4f952702016-05-01 04:55:03 +000094SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
Simon Atanasyan41325112016-06-19 21:39:37 +000095 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(false),
96 IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
97 Name({Name.data(), Name.size()}) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000098
George Rimar43651582016-06-28 08:21:10 +000099StringRef SymbolBody::getName() const {
100 assert(!isLocal());
101 StringRef S = StringRef(Name.S, Name.Len);
102 if (!symbol()->VersionedName)
103 return S;
104 return S.substr(0, S.find('@'));
105}
106
Rui Ueyamac4466602016-03-13 19:48:18 +0000107// Returns true if a symbol can be replaced at load-time by a symbol
108// with the same name defined in other ELF executable or DSO.
109bool SymbolBody::isPreemptible() const {
110 if (isLocal())
111 return false;
112
Rafael Espindola66434562016-05-05 19:41:49 +0000113 // Shared symbols resolve to the definition in the DSO. The exceptions are
114 // symbols with copy relocations (which resolve to .bss) or preempt plt
115 // entries (which resolve to that plt entry).
Rui Ueyamac4466602016-03-13 19:48:18 +0000116 if (isShared())
Rafael Espindola66434562016-05-05 19:41:49 +0000117 return !NeedsCopyOrPltAddr;
Rui Ueyamac4466602016-03-13 19:48:18 +0000118
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000119 // That's all that can be preempted in a non-DSO.
Rui Ueyamac4466602016-03-13 19:48:18 +0000120 if (!Config->Shared)
121 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000122
Peter Collingbournedbe41872016-04-24 04:29:59 +0000123 // Only symbols that appear in dynsym can be preempted.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000124 if (!symbol()->includeInDynsym())
Rui Ueyamac4466602016-03-13 19:48:18 +0000125 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000126
Rafael Espindola580d7a12016-07-07 22:50:54 +0000127 // Only default visibility symbols can be preempted.
128 if (symbol()->Visibility != STV_DEFAULT)
129 return false;
130
131 // -Bsymbolic means that definitions are not preempted.
Peter Collingbournedbe41872016-04-24 04:29:59 +0000132 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
133 return !isDefined();
Rafael Espindola580d7a12016-07-07 22:50:54 +0000134 return true;
Rui Ueyamac4466602016-03-13 19:48:18 +0000135}
136
Peter Smithfb05cd92016-07-08 16:10:27 +0000137template <class ELFT> bool SymbolBody::hasThunk() const {
138 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
139 return DR->ThunkData != nullptr;
140 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
141 return S->ThunkData != nullptr;
142 return false;
143}
144
Rui Ueyamab5a69702016-02-01 21:00:35 +0000145template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000146typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19 +0000147 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
148 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000149}
150
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000151template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000152 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
153}
154
155template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000156 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000157}
158
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000159template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000160 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
161}
162
163template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000164 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000165}
166
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000167template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyama4a90f572016-06-16 16:28:50 +0000168 return Out<ELFT>::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35 +0000169 PltIndex * Target->PltEntrySize;
170}
171
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000172template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
Peter Smithfb05cd92016-07-08 16:10:27 +0000173 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
174 return DR->ThunkData->getVA();
175 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
176 return S->ThunkData->getVA();
177 fatal("getThunkVA() not supported for Symbol class\n");
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000178}
179
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000180template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000181 if (const auto *C = dyn_cast<DefinedCommon>(this))
182 return C->Size;
183 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
184 return DR->Size;
185 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
186 return S->Sym.st_size;
Rui Ueyama512c61d2016-02-03 00:12:24 +0000187 return 0;
188}
189
Peter Collingbourne4f952702016-05-01 04:55:03 +0000190Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
191 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000192
Rafael Espindolaf9b79a42016-04-06 12:14:31 +0000193Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
194 : SymbolBody(K, NameOffset, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000195
Peter Collingbourne4f952702016-05-01 04:55:03 +0000196DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
197 BitcodeFile *F)
Rui Ueyama434b5612016-07-17 03:11:46 +0000198 : Defined(DefinedBitcodeKind, Name, StOther, Type) {
199 this->File = F;
200}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000201
202bool DefinedBitcode::classof(const SymbolBody *S) {
203 return S->kind() == DefinedBitcodeKind;
204}
205
Rui Ueyama434b5612016-07-17 03:11:46 +0000206Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type,
207 InputFile *File)
208 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {
209 this->File = File;
210}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000211
Rui Ueyama434b5612016-07-17 03:11:46 +0000212Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type,
213 InputFile *File)
214 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {
215 this->File = File;
216}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000217
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000218template <typename ELFT>
219DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000220 OutputSectionBase<ELFT> *Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000221 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000222 Value(Value), Section(Section) {}
223
Rafael Espindola11191912015-12-24 16:23:37 +0000224DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000225 uint8_t StOther, uint8_t Type)
226 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama17d69832016-03-10 18:58:53 +0000227 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000228
Rui Ueyama434b5612016-07-17 03:11:46 +0000229std::unique_ptr<InputFile> Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000230 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama434b5612016-07-17 03:11:46 +0000231 return S->fetch();
232 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000233}
234
Rui Ueyama434b5612016-07-17 03:11:46 +0000235LazyArchive::LazyArchive(ArchiveFile &File,
236 const llvm::object::Archive::Symbol S, uint8_t Type)
237 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
238 this->File = &File;
239}
240
241LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
242 : Lazy(LazyObjectKind, Name, Type) {
243 this->File = &File;
244}
245
246std::unique_ptr<InputFile> LazyArchive::fetch() {
247 MemoryBufferRef MBRef = file()->getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000248
249 // getMember returns an empty buffer if the member was already
250 // read from the library.
251 if (MBRef.getBuffer().empty())
252 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama434b5612016-07-17 03:11:46 +0000253 return createObjectFile(MBRef, file()->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000254}
255
Rui Ueyama434b5612016-07-17 03:11:46 +0000256std::unique_ptr<InputFile> LazyObject::fetch() {
257 MemoryBufferRef MBRef = file()->getBuffer();
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000258 if (MBRef.getBuffer().empty())
259 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000260 return createObjectFile(MBRef);
261}
262
Peter Collingbournedadcc172016-04-22 18:42:48 +0000263bool Symbol::includeInDynsym() const {
264 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000265 return false;
George Rimard3566302016-06-20 11:55:12 +0000266 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
Peter Collingbourne4f952702016-05-01 04:55:03 +0000267 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25 +0000268}
Peter Smithfb05cd92016-07-08 16:10:27 +0000269template bool SymbolBody::hasThunk<ELF32LE>() const;
270template bool SymbolBody::hasThunk<ELF32BE>() const;
271template bool SymbolBody::hasThunk<ELF64LE>() const;
272template bool SymbolBody::hasThunk<ELF64BE>() const;
Rafael Espindolaae605c12016-04-21 20:35:25 +0000273
Rafael Espindola87d9f102016-03-11 12:19:05 +0000274template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
275template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
276template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
277template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000278
279template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
280template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
281template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
282template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
283
Rafael Espindola74031ba2016-04-07 15:20:56 +0000284template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
285template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
286template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
287template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
288
Rui Ueyamab5a69702016-02-01 21:00:35 +0000289template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
290template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
291template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
292template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
293
Peter Smithfb05cd92016-07-08 16:10:27 +0000294template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
295template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
296template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
297template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
298
Rafael Espindola74031ba2016-04-07 15:20:56 +0000299template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
300template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
301template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
302template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
303
Rui Ueyamab5a69702016-02-01 21:00:35 +0000304template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
305template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
306template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
307template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
308
Rui Ueyama512c61d2016-02-03 00:12:24 +0000309template uint32_t SymbolBody::template getSize<ELF32LE>() const;
310template uint32_t SymbolBody::template getSize<ELF32BE>() const;
311template uint64_t SymbolBody::template getSize<ELF64LE>() const;
312template uint64_t SymbolBody::template getSize<ELF64BE>() const;
313
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000314template class elf::DefinedSynthetic<ELF32LE>;
315template class elf::DefinedSynthetic<ELF32BE>;
316template class elf::DefinedSynthetic<ELF64LE>;
317template class elf::DefinedSynthetic<ELF64BE>;