blob: 4819bb17ae8b88f395e619901b55ccf68bca1587 [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 }
Eugene Leviantceabe802016-08-11 07:56:43 +000061 uintX_t VA = (SC->OutSec ? SC->OutSec->getVA() : 0) + SC->getOffset(Offset);
Simon Atanasyand10a5ea2016-09-14 16:26:19 +000062 if (D.isTls() && !Config->Relocatable)
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 +
Rafael Espindolae7553e42016-08-31 13:28:33 +000069 cast<DefinedCommon>(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;
84 }
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
Rui Ueyamac4466602016-03-13 19:48:18 +0000104// Returns true if a symbol can be replaced at load-time by a symbol
105// with the same name defined in other ELF executable or DSO.
106bool SymbolBody::isPreemptible() const {
107 if (isLocal())
108 return false;
109
Rafael Espindola66434562016-05-05 19:41:49 +0000110 // Shared symbols resolve to the definition in the DSO. The exceptions are
111 // symbols with copy relocations (which resolve to .bss) or preempt plt
112 // entries (which resolve to that plt entry).
Rui Ueyamac4466602016-03-13 19:48:18 +0000113 if (isShared())
Rafael Espindola66434562016-05-05 19:41:49 +0000114 return !NeedsCopyOrPltAddr;
Rui Ueyamac4466602016-03-13 19:48:18 +0000115
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000116 // That's all that can be preempted in a non-DSO.
Rui Ueyamac4466602016-03-13 19:48:18 +0000117 if (!Config->Shared)
118 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000119
Peter Collingbournedbe41872016-04-24 04:29:59 +0000120 // Only symbols that appear in dynsym can be preempted.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000121 if (!symbol()->includeInDynsym())
Rui Ueyamac4466602016-03-13 19:48:18 +0000122 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000123
Rafael Espindola580d7a12016-07-07 22:50:54 +0000124 // Only default visibility symbols can be preempted.
125 if (symbol()->Visibility != STV_DEFAULT)
126 return false;
127
128 // -Bsymbolic means that definitions are not preempted.
Peter Collingbournedbe41872016-04-24 04:29:59 +0000129 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
130 return !isDefined();
Rafael Espindola580d7a12016-07-07 22:50:54 +0000131 return true;
Rui Ueyamac4466602016-03-13 19:48:18 +0000132}
133
Peter Smithfb05cd92016-07-08 16:10:27 +0000134template <class ELFT> bool SymbolBody::hasThunk() const {
135 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
136 return DR->ThunkData != nullptr;
137 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
138 return S->ThunkData != nullptr;
139 return false;
140}
141
Rui Ueyamab5a69702016-02-01 21:00:35 +0000142template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000143typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19 +0000144 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
145 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000146}
147
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000148template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000149 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
150}
151
152template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000153 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000154}
155
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000156template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000157 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
158}
159
160template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14 +0000161 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000162}
163
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000164template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyama4a90f572016-06-16 16:28:50 +0000165 return Out<ELFT>::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35 +0000166 PltIndex * Target->PltEntrySize;
167}
168
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000169template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
Peter Smithfb05cd92016-07-08 16:10:27 +0000170 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
171 return DR->ThunkData->getVA();
172 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
173 return S->ThunkData->getVA();
174 fatal("getThunkVA() not supported for Symbol class\n");
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000175}
176
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000177template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolae7553e42016-08-31 13:28:33 +0000178 if (const auto *C = dyn_cast<DefinedCommon>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000179 return C->Size;
180 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
181 return DR->Size;
182 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
183 return S->Sym.st_size;
Rui Ueyama512c61d2016-02-03 00:12:24 +0000184 return 0;
185}
186
Peter Collingbourne4f952702016-05-01 04:55:03 +0000187Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
188 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000189
Rafael Espindolaf9b79a42016-04-06 12:14:31 +0000190Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
191 : SymbolBody(K, NameOffset, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000192
Simon Atanasyanf967f092016-09-29 12:58:36 +0000193template <class ELFT> bool DefinedRegular<ELFT>::isMipsPIC() const {
194 if (!Section || !isFunc())
195 return false;
196 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
197 (Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC);
198}
199
Rui Ueyama434b5612016-07-17 03:11:46 +0000200Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type,
201 InputFile *File)
202 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {
203 this->File = File;
204}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000205
Rui Ueyama434b5612016-07-17 03:11:46 +0000206Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type,
207 InputFile *File)
208 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {
209 this->File = File;
210}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000211
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000212template <typename ELFT>
213DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000214 OutputSectionBase<ELFT> *Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000215 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000216 Value(Value), Section(Section) {}
217
Rafael Espindolae7553e42016-08-31 13:28:33 +0000218DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
219 uint8_t StOther, uint8_t Type, InputFile *File)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000220 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama2a7c1c12016-07-17 17:36:22 +0000221 Alignment(Alignment), Size(Size) {
222 this->File = File;
223}
Rafael Espindola11191912015-12-24 16:23:37 +0000224
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000225InputFile *Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000226 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama434b5612016-07-17 03:11:46 +0000227 return S->fetch();
228 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000229}
230
Rui Ueyama434b5612016-07-17 03:11:46 +0000231LazyArchive::LazyArchive(ArchiveFile &File,
232 const llvm::object::Archive::Symbol S, uint8_t Type)
233 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
234 this->File = &File;
235}
236
237LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
238 : Lazy(LazyObjectKind, Name, Type) {
239 this->File = &File;
240}
241
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000242InputFile *LazyArchive::fetch() {
Rui Ueyama434b5612016-07-17 03:11:46 +0000243 MemoryBufferRef MBRef = file()->getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000244
245 // getMember returns an empty buffer if the member was already
246 // read from the library.
247 if (MBRef.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000248 return nullptr;
Rui Ueyama434b5612016-07-17 03:11:46 +0000249 return createObjectFile(MBRef, file()->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000250}
251
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000252InputFile *LazyObject::fetch() {
Rui Ueyama434b5612016-07-17 03:11:46 +0000253 MemoryBufferRef MBRef = file()->getBuffer();
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000254 if (MBRef.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000255 return nullptr;
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000256 return createObjectFile(MBRef);
257}
258
Peter Collingbournedadcc172016-04-22 18:42:48 +0000259bool Symbol::includeInDynsym() const {
260 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000261 return false;
George Rimard3566302016-06-20 11:55:12 +0000262 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
Peter Collingbourne4f952702016-05-01 04:55:03 +0000263 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25 +0000264}
Rui Ueyama69c778c2016-07-17 17:50:09 +0000265
266// Print out a log message for --trace-symbol.
267void elf::printTraceSymbol(Symbol *Sym) {
268 SymbolBody *B = Sym->body();
269 outs() << getFilename(B->File);
270
271 if (B->isUndefined())
272 outs() << ": reference to ";
273 else if (B->isCommon())
274 outs() << ": common definition of ";
275 else
276 outs() << ": definition of ";
277 outs() << B->getName() << "\n";
278}
279
Peter Smithfb05cd92016-07-08 16:10:27 +0000280template bool SymbolBody::hasThunk<ELF32LE>() const;
281template bool SymbolBody::hasThunk<ELF32BE>() const;
282template bool SymbolBody::hasThunk<ELF64LE>() const;
283template bool SymbolBody::hasThunk<ELF64BE>() const;
Rafael Espindolaae605c12016-04-21 20:35:25 +0000284
Rafael Espindola87d9f102016-03-11 12:19:05 +0000285template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
286template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
287template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
288template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000289
290template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
291template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
292template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
293template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
294
Rafael Espindola74031ba2016-04-07 15:20:56 +0000295template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
296template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
297template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
298template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
299
Rui Ueyamab5a69702016-02-01 21:00:35 +0000300template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
301template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
302template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
303template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
304
Peter Smithfb05cd92016-07-08 16:10:27 +0000305template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
306template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
307template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
308template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
309
Rafael Espindola74031ba2016-04-07 15:20:56 +0000310template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
311template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
312template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
313template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
314
Rui Ueyamab5a69702016-02-01 21:00:35 +0000315template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
316template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
317template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
318template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
319
Rui Ueyama512c61d2016-02-03 00:12:24 +0000320template uint32_t SymbolBody::template getSize<ELF32LE>() const;
321template uint32_t SymbolBody::template getSize<ELF32BE>() const;
322template uint64_t SymbolBody::template getSize<ELF64LE>() const;
323template uint64_t SymbolBody::template getSize<ELF64BE>() const;
324
Simon Atanasyanf967f092016-09-29 12:58:36 +0000325template class elf::DefinedRegular<ELF32LE>;
326template class elf::DefinedRegular<ELF32BE>;
327template class elf::DefinedRegular<ELF64LE>;
328template class elf::DefinedRegular<ELF64BE>;
329
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000330template class elf::DefinedSynthetic<ELF32LE>;
331template class elf::DefinedSynthetic<ELF32BE>;
332template class elf::DefinedSynthetic<ELF64LE>;
333template class elf::DefinedSynthetic<ELF64BE>;