blob: b1056f8d508778581c064996136cc95d320ec1de [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 }
Rui Ueyama07784902016-08-02 01:35:13 +000066 case SymbolBody::DefinedCommonKind:
67 return CommonInputSection<ELFT>::X->OutSec->getVA() +
68 CommonInputSection<ELFT>::X->OutSecOff +
69 cast<DefinedCommon<ELFT>>(Body).Offset;
Rafael Espindola87d9f102016-03-11 12:19:05 +000070 case SymbolBody::SharedKind: {
71 auto &SS = cast<SharedSymbol<ELFT>>(Body);
72 if (!SS.NeedsCopyOrPltAddr)
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000073 return 0;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000074 if (SS.isFunc())
Rafael Espindola87d9f102016-03-11 12:19:05 +000075 return Body.getPltVA<ELFT>();
Rui Ueyama2df72892016-03-13 20:54:38 +000076 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:35 +000077 }
Peter Collingbourne60976ed2016-04-27 00:05:06 +000078 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35 +000079 return 0;
Rui Ueyamaf8baa662016-04-07 19:24:51 +000080 case SymbolBody::LazyArchiveKind:
81 case SymbolBody::LazyObjectKind:
Peter Collingbourne4f952702016-05-01 04:55:03 +000082 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35 +000083 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000084 case SymbolBody::DefinedBitcodeKind:
Davide Italianof6523ae2016-03-29 02:20:10 +000085 llvm_unreachable("should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:35 +000086 }
George Rimar777f9632016-03-12 08:31:34 +000087 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35 +000088}
89
Rui Ueyamab5792b22016-04-04 19:09:08 +000090SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther,
91 uint8_t Type)
Simon Atanasyan41325112016-06-19 21:39:37 +000092 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(true),
93 IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
94 NameOffset(NameOffset) {}
Rafael Espindolaf4765732016-04-06 13:22:41 +000095
Peter Collingbourne4f952702016-05-01 04:55:03 +000096SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
Simon Atanasyan41325112016-06-19 21:39:37 +000097 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(false),
98 IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
99 Name({Name.data(), Name.size()}) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000100
George Rimar43651582016-06-28 08:21:10 +0000101StringRef SymbolBody::getName() const {
102 assert(!isLocal());
Rui Ueyama663b8c22016-07-17 17:23:17 +0000103 return StringRef(Name.S, Name.Len);
104}
105
Rui Ueyamac4466602016-03-13 19:48:18 +0000106// Returns true if a symbol can be replaced at load-time by a symbol
107// with the same name defined in other ELF executable or DSO.
108bool SymbolBody::isPreemptible() const {
109 if (isLocal())
110 return false;
111
Rafael Espindola66434562016-05-05 19:41:49 +0000112 // Shared symbols resolve to the definition in the DSO. The exceptions are
113 // symbols with copy relocations (which resolve to .bss) or preempt plt
114 // entries (which resolve to that plt entry).
Rui Ueyamac4466602016-03-13 19:48:18 +0000115 if (isShared())
Rafael Espindola66434562016-05-05 19:41:49 +0000116 return !NeedsCopyOrPltAddr;
Rui Ueyamac4466602016-03-13 19:48:18 +0000117
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000118 // That's all that can be preempted in a non-DSO.
Rui Ueyamac4466602016-03-13 19:48:18 +0000119 if (!Config->Shared)
120 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000121
Peter Collingbournedbe41872016-04-24 04:29:59 +0000122 // Only symbols that appear in dynsym can be preempted.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000123 if (!symbol()->includeInDynsym())
Rui Ueyamac4466602016-03-13 19:48:18 +0000124 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000125
Rafael Espindola580d7a12016-07-07 22:50:54 +0000126 // Only default visibility symbols can be preempted.
127 if (symbol()->Visibility != STV_DEFAULT)
128 return false;
129
130 // -Bsymbolic means that definitions are not preempted.
Peter Collingbournedbe41872016-04-24 04:29:59 +0000131 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
132 return !isDefined();
Rafael Espindola580d7a12016-07-07 22:50:54 +0000133 return true;
Rui Ueyamac4466602016-03-13 19:48:18 +0000134}
135
Peter Smithfb05cd92016-07-08 16:10:27 +0000136template <class ELFT> bool SymbolBody::hasThunk() const {
137 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
138 return DR->ThunkData != nullptr;
139 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
140 return S->ThunkData != nullptr;
141 return false;
142}
143
Rui Ueyamab5a69702016-02-01 21:00:35 +0000144template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000145typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19 +0000146 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
147 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000148}
149
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000150template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000151 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
152}
153
154template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000155 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000156}
157
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000158template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000159 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
160}
161
162template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000163 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000164}
165
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000166template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyama4a90f572016-06-16 16:28:50 +0000167 return Out<ELFT>::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35 +0000168 PltIndex * Target->PltEntrySize;
169}
170
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000171template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
Peter Smithfb05cd92016-07-08 16:10:27 +0000172 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
173 return DR->ThunkData->getVA();
174 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
175 return S->ThunkData->getVA();
176 fatal("getThunkVA() not supported for Symbol class\n");
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000177}
178
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000179template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000180 if (const auto *C = dyn_cast<DefinedCommon<ELFT>>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000181 return C->Size;
182 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
183 return DR->Size;
184 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
185 return S->Sym.st_size;
Rui Ueyama512c61d2016-02-03 00:12:24 +0000186 return 0;
187}
188
Peter Collingbourne4f952702016-05-01 04:55:03 +0000189Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
190 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000191
Rafael Espindolaf9b79a42016-04-06 12:14:31 +0000192Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
193 : SymbolBody(K, NameOffset, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000194
Peter Collingbourne4f952702016-05-01 04:55:03 +0000195DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
196 BitcodeFile *F)
Rui Ueyama434b5612016-07-17 03:11:46 +0000197 : Defined(DefinedBitcodeKind, Name, StOther, Type) {
198 this->File = F;
199}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000200
201bool DefinedBitcode::classof(const SymbolBody *S) {
202 return S->kind() == DefinedBitcodeKind;
203}
204
Rui Ueyama434b5612016-07-17 03:11:46 +0000205Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type,
206 InputFile *File)
207 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {
208 this->File = File;
209}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000210
Rui Ueyama434b5612016-07-17 03:11:46 +0000211Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type,
212 InputFile *File)
213 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {
214 this->File = File;
215}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000216
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000217template <typename ELFT>
218DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000219 OutputSectionBase<ELFT> *Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000220 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000221 Value(Value), Section(Section) {}
222
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000223template <class ELFT>
224DefinedCommon<ELFT>::DefinedCommon(StringRef N, uint64_t Size,
225 uint64_t Alignment, uint8_t StOther,
226 uint8_t Type, InputFile *File)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000227 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama2a7c1c12016-07-17 17:36:22 +0000228 Alignment(Alignment), Size(Size) {
229 this->File = File;
230}
Rafael Espindola11191912015-12-24 16:23:37 +0000231
Rui Ueyama434b5612016-07-17 03:11:46 +0000232std::unique_ptr<InputFile> Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000233 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama434b5612016-07-17 03:11:46 +0000234 return S->fetch();
235 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000236}
237
Rui Ueyama434b5612016-07-17 03:11:46 +0000238LazyArchive::LazyArchive(ArchiveFile &File,
239 const llvm::object::Archive::Symbol S, uint8_t Type)
240 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
241 this->File = &File;
242}
243
244LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
245 : Lazy(LazyObjectKind, Name, Type) {
246 this->File = &File;
247}
248
249std::unique_ptr<InputFile> LazyArchive::fetch() {
250 MemoryBufferRef MBRef = file()->getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000251
252 // getMember returns an empty buffer if the member was already
253 // read from the library.
254 if (MBRef.getBuffer().empty())
255 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama434b5612016-07-17 03:11:46 +0000256 return createObjectFile(MBRef, file()->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000257}
258
Rui Ueyama434b5612016-07-17 03:11:46 +0000259std::unique_ptr<InputFile> LazyObject::fetch() {
260 MemoryBufferRef MBRef = file()->getBuffer();
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000261 if (MBRef.getBuffer().empty())
262 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000263 return createObjectFile(MBRef);
264}
265
Peter Collingbournedadcc172016-04-22 18:42:48 +0000266bool Symbol::includeInDynsym() const {
267 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000268 return false;
George Rimard3566302016-06-20 11:55:12 +0000269 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
Peter Collingbourne4f952702016-05-01 04:55:03 +0000270 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25 +0000271}
Rui Ueyama69c778c2016-07-17 17:50:09 +0000272
273// Print out a log message for --trace-symbol.
274void elf::printTraceSymbol(Symbol *Sym) {
275 SymbolBody *B = Sym->body();
276 outs() << getFilename(B->File);
277
278 if (B->isUndefined())
279 outs() << ": reference to ";
280 else if (B->isCommon())
281 outs() << ": common definition of ";
282 else
283 outs() << ": definition of ";
284 outs() << B->getName() << "\n";
285}
286
Peter Smithfb05cd92016-07-08 16:10:27 +0000287template bool SymbolBody::hasThunk<ELF32LE>() const;
288template bool SymbolBody::hasThunk<ELF32BE>() const;
289template bool SymbolBody::hasThunk<ELF64LE>() const;
290template bool SymbolBody::hasThunk<ELF64BE>() const;
Rafael Espindolaae605c12016-04-21 20:35:25 +0000291
Rafael Espindola87d9f102016-03-11 12:19:05 +0000292template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
293template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
294template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
295template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000296
297template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
298template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
299template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
300template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
301
Rafael Espindola74031ba2016-04-07 15:20:56 +0000302template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
303template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
304template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
305template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
306
Rui Ueyamab5a69702016-02-01 21:00:35 +0000307template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
308template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
309template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
310template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
311
Peter Smithfb05cd92016-07-08 16:10:27 +0000312template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
313template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
314template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
315template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
316
Rafael Espindola74031ba2016-04-07 15:20:56 +0000317template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
318template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
319template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
320template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
321
Rui Ueyamab5a69702016-02-01 21:00:35 +0000322template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
323template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
324template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
325template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
326
Rui Ueyama512c61d2016-02-03 00:12:24 +0000327template uint32_t SymbolBody::template getSize<ELF32LE>() const;
328template uint32_t SymbolBody::template getSize<ELF32BE>() const;
329template uint64_t SymbolBody::template getSize<ELF64LE>() const;
330template uint64_t SymbolBody::template getSize<ELF64BE>() const;
331
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000332template class elf::DefinedSynthetic<ELF32LE>;
333template class elf::DefinedSynthetic<ELF32BE>;
334template class elf::DefinedSynthetic<ELF64LE>;
335template class elf::DefinedSynthetic<ELF64BE>;
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000336
337template class elf::DefinedCommon<ELF32LE>;
338template class elf::DefinedCommon<ELF32BE>;
339template class elf::DefinedCommon<ELF64LE>;
340template class elf::DefinedCommon<ELF64BE>;