blob: 724a92247e8b4cb52ca4c441482e8225f4c0dbf7 [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());
Rui Ueyama663b8c22016-07-17 17:23:17 +0000101 return StringRef(Name.S, Name.Len);
102}
103
104void SymbolBody::setName(StringRef S) {
105 Name.S = S.data();
106 Name.Len = S.size();
George Rimar43651582016-06-28 08:21:10 +0000107}
108
Rui Ueyamac4466602016-03-13 19:48:18 +0000109// Returns true if a symbol can be replaced at load-time by a symbol
110// with the same name defined in other ELF executable or DSO.
111bool SymbolBody::isPreemptible() const {
112 if (isLocal())
113 return false;
114
Rafael Espindola66434562016-05-05 19:41:49 +0000115 // Shared symbols resolve to the definition in the DSO. The exceptions are
116 // symbols with copy relocations (which resolve to .bss) or preempt plt
117 // entries (which resolve to that plt entry).
Rui Ueyamac4466602016-03-13 19:48:18 +0000118 if (isShared())
Rafael Espindola66434562016-05-05 19:41:49 +0000119 return !NeedsCopyOrPltAddr;
Rui Ueyamac4466602016-03-13 19:48:18 +0000120
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000121 // That's all that can be preempted in a non-DSO.
Rui Ueyamac4466602016-03-13 19:48:18 +0000122 if (!Config->Shared)
123 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000124
Peter Collingbournedbe41872016-04-24 04:29:59 +0000125 // Only symbols that appear in dynsym can be preempted.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000126 if (!symbol()->includeInDynsym())
Rui Ueyamac4466602016-03-13 19:48:18 +0000127 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000128
Rafael Espindola580d7a12016-07-07 22:50:54 +0000129 // Only default visibility symbols can be preempted.
130 if (symbol()->Visibility != STV_DEFAULT)
131 return false;
132
133 // -Bsymbolic means that definitions are not preempted.
Peter Collingbournedbe41872016-04-24 04:29:59 +0000134 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
135 return !isDefined();
Rafael Espindola580d7a12016-07-07 22:50:54 +0000136 return true;
Rui Ueyamac4466602016-03-13 19:48:18 +0000137}
138
Peter Smithfb05cd92016-07-08 16:10:27 +0000139template <class ELFT> bool SymbolBody::hasThunk() const {
140 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
141 return DR->ThunkData != nullptr;
142 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
143 return S->ThunkData != nullptr;
144 return false;
145}
146
Rui Ueyamab5a69702016-02-01 21:00:35 +0000147template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000148typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19 +0000149 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
150 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000151}
152
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000153template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000154 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
155}
156
157template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000158 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000159}
160
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000161template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000162 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
163}
164
165template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000166 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000167}
168
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000169template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyama4a90f572016-06-16 16:28:50 +0000170 return Out<ELFT>::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35 +0000171 PltIndex * Target->PltEntrySize;
172}
173
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000174template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
Peter Smithfb05cd92016-07-08 16:10:27 +0000175 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
176 return DR->ThunkData->getVA();
177 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
178 return S->ThunkData->getVA();
179 fatal("getThunkVA() not supported for Symbol class\n");
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000180}
181
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000182template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000183 if (const auto *C = dyn_cast<DefinedCommon>(this))
184 return C->Size;
185 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
186 return DR->Size;
187 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
188 return S->Sym.st_size;
Rui Ueyama512c61d2016-02-03 00:12:24 +0000189 return 0;
190}
191
Peter Collingbourne4f952702016-05-01 04:55:03 +0000192Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
193 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000194
Rafael Espindolaf9b79a42016-04-06 12:14:31 +0000195Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
196 : SymbolBody(K, NameOffset, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000197
Peter Collingbourne4f952702016-05-01 04:55:03 +0000198DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
199 BitcodeFile *F)
Rui Ueyama434b5612016-07-17 03:11:46 +0000200 : Defined(DefinedBitcodeKind, Name, StOther, Type) {
201 this->File = F;
202}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000203
204bool DefinedBitcode::classof(const SymbolBody *S) {
205 return S->kind() == DefinedBitcodeKind;
206}
207
Rui Ueyama434b5612016-07-17 03:11:46 +0000208Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type,
209 InputFile *File)
210 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {
211 this->File = File;
212}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000213
Rui Ueyama434b5612016-07-17 03:11:46 +0000214Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type,
215 InputFile *File)
216 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {
217 this->File = File;
218}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000219
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000220template <typename ELFT>
221DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000222 OutputSectionBase<ELFT> *Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000223 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000224 Value(Value), Section(Section) {}
225
Rafael Espindola11191912015-12-24 16:23:37 +0000226DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
Rui Ueyama2a7c1c12016-07-17 17:36:22 +0000227 uint8_t StOther, uint8_t Type, InputFile *File)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000228 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama2a7c1c12016-07-17 17:36:22 +0000229 Alignment(Alignment), Size(Size) {
230 this->File = File;
231}
Rafael Espindola11191912015-12-24 16:23:37 +0000232
Rui Ueyama434b5612016-07-17 03:11:46 +0000233std::unique_ptr<InputFile> Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000234 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama434b5612016-07-17 03:11:46 +0000235 return S->fetch();
236 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000237}
238
Rui Ueyama434b5612016-07-17 03:11:46 +0000239LazyArchive::LazyArchive(ArchiveFile &File,
240 const llvm::object::Archive::Symbol S, uint8_t Type)
241 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
242 this->File = &File;
243}
244
245LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
246 : Lazy(LazyObjectKind, Name, Type) {
247 this->File = &File;
248}
249
250std::unique_ptr<InputFile> LazyArchive::fetch() {
251 MemoryBufferRef MBRef = file()->getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000252
253 // getMember returns an empty buffer if the member was already
254 // read from the library.
255 if (MBRef.getBuffer().empty())
256 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama434b5612016-07-17 03:11:46 +0000257 return createObjectFile(MBRef, file()->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000258}
259
Rui Ueyama434b5612016-07-17 03:11:46 +0000260std::unique_ptr<InputFile> LazyObject::fetch() {
261 MemoryBufferRef MBRef = file()->getBuffer();
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000262 if (MBRef.getBuffer().empty())
263 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000264 return createObjectFile(MBRef);
265}
266
Peter Collingbournedadcc172016-04-22 18:42:48 +0000267bool Symbol::includeInDynsym() const {
268 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000269 return false;
George Rimard3566302016-06-20 11:55:12 +0000270 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
Peter Collingbourne4f952702016-05-01 04:55:03 +0000271 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25 +0000272}
Peter Smithfb05cd92016-07-08 16:10:27 +0000273template bool SymbolBody::hasThunk<ELF32LE>() const;
274template bool SymbolBody::hasThunk<ELF32BE>() const;
275template bool SymbolBody::hasThunk<ELF64LE>() const;
276template bool SymbolBody::hasThunk<ELF64BE>() const;
Rafael Espindolaae605c12016-04-21 20:35:25 +0000277
Rafael Espindola87d9f102016-03-11 12:19:05 +0000278template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
279template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
280template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
281template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000282
283template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
284template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
285template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
286template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
287
Rafael Espindola74031ba2016-04-07 15:20:56 +0000288template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
289template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
290template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
291template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
292
Rui Ueyamab5a69702016-02-01 21:00:35 +0000293template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
294template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
295template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
296template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
297
Peter Smithfb05cd92016-07-08 16:10:27 +0000298template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
299template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
300template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
301template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
302
Rafael Espindola74031ba2016-04-07 15:20:56 +0000303template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
304template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
305template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
306template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
307
Rui Ueyamab5a69702016-02-01 21:00:35 +0000308template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
309template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
310template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
311template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
312
Rui Ueyama512c61d2016-02-03 00:12:24 +0000313template uint32_t SymbolBody::template getSize<ELF32LE>() const;
314template uint32_t SymbolBody::template getSize<ELF32BE>() const;
315template uint64_t SymbolBody::template getSize<ELF64LE>() const;
316template uint64_t SymbolBody::template getSize<ELF64BE>() const;
317
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000318template class elf::DefinedSynthetic<ELF32LE>;
319template class elf::DefinedSynthetic<ELF32BE>;
320template class elf::DefinedSynthetic<ELF64LE>;
321template class elf::DefinedSynthetic<ELF64BE>;