blob: 1c8c9a8b97aa9e10ca019ae050235a713b61fc2e [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"
Rafael Espindola49a2ca62015-08-06 15:33:1911#include "Error.h"
Michael J. Spencercdae0a42015-07-28 22:58:2512#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:3513#include "InputSection.h"
14#include "OutputSections.h"
Rui Ueyamae8a61022016-11-05 23:05:4715#include "SyntheticSections.h"
Rui Ueyamab5a69702016-02-01 21:00:3516#include "Target.h"
Michael J. Spencer84487f12015-07-24 21:03:0717
Michael J. Spencer1b348a62015-09-04 22:28:1018#include "llvm/ADT/STLExtras.h"
Eugene Leviantc958d8d2016-10-12 08:19:3019#include "llvm/Support/Path.h"
Michael J. Spencer1b348a62015-09-04 22:28:1020
21using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0722using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5223using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0724
25using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:5426using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:0727
Rui Ueyamab5a69702016-02-01 21:00:3528template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:0929static typename ELFT::uint getSymVA(const SymbolBody &Body,
30 typename ELFT::uint &Addend) {
Rui Ueyama9328b2c2016-03-14 23:16:0931 typedef typename ELFT::uint uintX_t;
Rafael Espindola87d9f102016-03-11 12:19:0532
33 switch (Body.kind()) {
34 case SymbolBody::DefinedSyntheticKind: {
35 auto &D = cast<DefinedSynthetic<ELFT>>(Body);
Peter Collingbourne6a422592016-05-03 01:21:0836 const OutputSectionBase<ELFT> *Sec = D.Section;
37 if (!Sec)
38 return D.Value;
Simon Atanasyan13f6da12016-03-31 21:26:2339 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
Peter Collingbourne6a422592016-05-03 01:21:0840 return Sec->getVA() + Sec->getSize();
41 return Sec->getVA() + D.Value;
Rui Ueyamab5a69702016-02-01 21:00:3542 }
Rafael Espindola87d9f102016-03-11 12:19:0543 case SymbolBody::DefinedRegularKind: {
44 auto &D = cast<DefinedRegular<ELFT>>(Body);
45 InputSectionBase<ELFT> *SC = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:3546
Rafael Espindolaccfe3cb2016-04-04 14:04:1647 // According to the ELF spec reference to a local symbol from outside
48 // the group are not allowed. Unfortunately .eh_frame breaks that rule
49 // and must be treated specially. For now we just replace the symbol with
50 // 0.
51 if (SC == &InputSection<ELFT>::Discarded)
52 return 0;
53
Rui Ueyamab5a69702016-02-01 21:00:3554 // This is an absolute symbol.
55 if (!SC)
Rafael Espindolaccfe3cb2016-04-04 14:04:1656 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:3557
Rafael Espindolaccfe3cb2016-04-04 14:04:1658 uintX_t Offset = D.Value;
59 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:3760 Offset += Addend;
61 Addend = 0;
62 }
Eugene Leviantceabe802016-08-11 07:56:4363 uintX_t VA = (SC->OutSec ? SC->OutSec->getVA() : 0) + SC->getOffset(Offset);
George Rimar6a3b1542016-10-04 08:52:5164 if (D.isTls() && !Config->Relocatable) {
65 if (!Out<ELFT>::TlsPhdr)
66 fatal(getFilename(D.File) +
67 " has a STT_TLS symbol but doesn't have a PT_TLS section");
Rafael Espindola1f5b70f2016-03-11 14:21:3768 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
George Rimar6a3b1542016-10-04 08:52:5169 }
Rafael Espindola1f5b70f2016-03-11 14:21:3770 return VA;
Rui Ueyamab5a69702016-02-01 21:00:3571 }
Rui Ueyama07784902016-08-02 01:35:1372 case SymbolBody::DefinedCommonKind:
Rui Ueyamae8a61022016-11-05 23:05:4773 return In<ELFT>::Common->OutSec->getVA() + In<ELFT>::Common->OutSecOff +
Rafael Espindolae7553e42016-08-31 13:28:3374 cast<DefinedCommon>(Body).Offset;
Rafael Espindola87d9f102016-03-11 12:19:0575 case SymbolBody::SharedKind: {
76 auto &SS = cast<SharedSymbol<ELFT>>(Body);
77 if (!SS.NeedsCopyOrPltAddr)
Rafael Espindolaa0a65f92016-02-09 15:11:0178 return 0;
Rafael Espindolaccfe3cb2016-04-04 14:04:1679 if (SS.isFunc())
Rafael Espindola87d9f102016-03-11 12:19:0580 return Body.getPltVA<ELFT>();
Rui Ueyama2df72892016-03-13 20:54:3881 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:3582 }
Peter Collingbourne60976ed2016-04-27 00:05:0683 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:3584 return 0;
Rui Ueyamaf8baa662016-04-07 19:24:5185 case SymbolBody::LazyArchiveKind:
86 case SymbolBody::LazyObjectKind:
Peter Collingbourne4f952702016-05-01 04:55:0387 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:3588 return 0;
Rui Ueyamab5a69702016-02-01 21:00:3589 }
George Rimar777f9632016-03-12 08:31:3490 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:3591}
92
Rui Ueyamab5792b22016-04-04 19:09:0893SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther,
94 uint8_t Type)
Simon Atanasyan41325112016-06-19 21:39:3795 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(true),
Simon Atanasyanbed04bf2016-10-21 07:22:3096 IsInGlobalMipsGot(false), Is32BitMipsGot(false), Type(Type),
97 StOther(StOther), NameOffset(NameOffset) {}
Rafael Espindolaf4765732016-04-06 13:22:4198
Peter Collingbourne4f952702016-05-01 04:55:0399SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
Simon Atanasyan41325112016-06-19 21:39:37100 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(false),
Simon Atanasyanbed04bf2016-10-21 07:22:30101 IsInGlobalMipsGot(false), Is32BitMipsGot(false), Type(Type),
102 StOther(StOther), Name({Name.data(), Name.size()}) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16103
George Rimar43651582016-06-28 08:21:10104StringRef SymbolBody::getName() const {
105 assert(!isLocal());
Rui Ueyama663b8c22016-07-17 17:23:17106 return StringRef(Name.S, Name.Len);
107}
108
Rui Ueyamac4466602016-03-13 19:48:18109// 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:49115 // 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:18118 if (isShared())
Rafael Espindola66434562016-05-05 19:41:49119 return !NeedsCopyOrPltAddr;
Rui Ueyamac4466602016-03-13 19:48:18120
Peter Collingbourne66ac1d62016-04-22 20:21:26121 // That's all that can be preempted in a non-DSO.
Rui Ueyamac4466602016-03-13 19:48:18122 if (!Config->Shared)
123 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26124
Peter Collingbournedbe41872016-04-24 04:29:59125 // Only symbols that appear in dynsym can be preempted.
Peter Collingbourne4f952702016-05-01 04:55:03126 if (!symbol()->includeInDynsym())
Rui Ueyamac4466602016-03-13 19:48:18127 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26128
Rafael Espindola580d7a12016-07-07 22:50:54129 // 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:59134 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
135 return !isDefined();
Rafael Espindola580d7a12016-07-07 22:50:54136 return true;
Rui Ueyamac4466602016-03-13 19:48:18137}
138
Peter Smithfb05cd92016-07-08 16:10:27139template <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:35147template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09148typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19149 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
150 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05151}
152
Rui Ueyama9328b2c2016-03-14 23:16:09153template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56154 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
155}
156
157template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14158 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35159}
160
Rui Ueyama9328b2c2016-03-14 23:16:09161template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56162 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
163}
164
165template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14166 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35167}
168
Rui Ueyama9328b2c2016-03-14 23:16:09169template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyama4a90f572016-06-16 16:28:50170 return Out<ELFT>::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35171 PltIndex * Target->PltEntrySize;
172}
173
Simon Atanasyan13f6da12016-03-31 21:26:23174template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
Peter Smithfb05cd92016-07-08 16:10:27175 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:23180}
181
Rui Ueyama9328b2c2016-03-14 23:16:09182template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolae7553e42016-08-31 13:28:33183 if (const auto *C = dyn_cast<DefinedCommon>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16184 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:24189 return 0;
190}
191
Peter Collingbourne4f952702016-05-01 04:55:03192Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
193 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16194
Rafael Espindolaf9b79a42016-04-06 12:14:31195Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
196 : SymbolBody(K, NameOffset, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42197
Simon Atanasyanf967f092016-09-29 12:58:36198template <class ELFT> bool DefinedRegular<ELFT>::isMipsPIC() const {
199 if (!Section || !isFunc())
200 return false;
201 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
202 (Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC);
203}
204
Rui Ueyama434b5612016-07-17 03:11:46205Undefined::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:50210
Rui Ueyama434b5612016-07-17 03:11:46211Undefined::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:50216
Rafael Espindola4d4b06a2015-12-24 00:47:42217template <typename ELFT>
218DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbourne6a422592016-05-03 01:21:08219 OutputSectionBase<ELFT> *Section)
Peter Collingbourne4f952702016-05-01 04:55:03220 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42221 Value(Value), Section(Section) {}
222
Rafael Espindolae7553e42016-08-31 13:28:33223DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
224 uint8_t StOther, uint8_t Type, InputFile *File)
Peter Collingbourne4f952702016-05-01 04:55:03225 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama2a7c1c12016-07-17 17:36:22226 Alignment(Alignment), Size(Size) {
227 this->File = File;
228}
Rafael Espindola11191912015-12-24 16:23:37229
Rui Ueyama55518e72016-10-28 20:57:25230InputFile *Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51231 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama55518e72016-10-28 20:57:25232 return S->fetch();
233 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51234}
235
Rui Ueyama434b5612016-07-17 03:11:46236LazyArchive::LazyArchive(ArchiveFile &File,
237 const llvm::object::Archive::Symbol S, uint8_t Type)
238 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
239 this->File = &File;
240}
241
242LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
243 : Lazy(LazyObjectKind, Name, Type) {
244 this->File = &File;
245}
246
Rui Ueyama55518e72016-10-28 20:57:25247InputFile *LazyArchive::fetch() {
Davide Italianobcdd6c62016-10-12 19:35:54248 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10249
250 // getMember returns an empty buffer if the member was already
251 // read from the library.
Davide Italianobcdd6c62016-10-12 19:35:54252 if (MBInfo.first.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51253 return nullptr;
Rui Ueyama55518e72016-10-28 20:57:25254 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
Michael J. Spencer1b348a62015-09-04 22:28:10255}
256
Rui Ueyama55518e72016-10-28 20:57:25257InputFile *LazyObject::fetch() {
Rui Ueyama434b5612016-07-17 03:11:46258 MemoryBufferRef MBRef = file()->getBuffer();
Rafael Espindola65c65ce2016-06-14 21:56:36259 if (MBRef.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51260 return nullptr;
Rui Ueyama55518e72016-10-28 20:57:25261 return createObjectFile(MBRef);
Rui Ueyamaf8baa662016-04-07 19:24:51262}
263
Peter Collingbournedadcc172016-04-22 18:42:48264bool Symbol::includeInDynsym() const {
265 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25266 return false;
George Rimard3566302016-06-20 11:55:12267 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
Peter Collingbourne4f952702016-05-01 04:55:03268 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25269}
Rui Ueyama69c778c2016-07-17 17:50:09270
271// Print out a log message for --trace-symbol.
272void elf::printTraceSymbol(Symbol *Sym) {
273 SymbolBody *B = Sym->body();
274 outs() << getFilename(B->File);
275
276 if (B->isUndefined())
277 outs() << ": reference to ";
278 else if (B->isCommon())
279 outs() << ": common definition of ";
280 else
281 outs() << ": definition of ";
282 outs() << B->getName() << "\n";
283}
284
Peter Smithfb05cd92016-07-08 16:10:27285template bool SymbolBody::hasThunk<ELF32LE>() const;
286template bool SymbolBody::hasThunk<ELF32BE>() const;
287template bool SymbolBody::hasThunk<ELF64LE>() const;
288template bool SymbolBody::hasThunk<ELF64BE>() const;
Rafael Espindolaae605c12016-04-21 20:35:25289
Rafael Espindola87d9f102016-03-11 12:19:05290template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
291template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
292template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
293template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35294
295template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
296template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
297template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
298template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
299
Rafael Espindola74031ba2016-04-07 15:20:56300template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
301template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
302template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
303template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
304
Rui Ueyamab5a69702016-02-01 21:00:35305template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
306template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
307template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
308template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
309
Peter Smithfb05cd92016-07-08 16:10:27310template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
311template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
312template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
313template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
314
Rafael Espindola74031ba2016-04-07 15:20:56315template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
316template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
317template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
318template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
319
Rui Ueyamab5a69702016-02-01 21:00:35320template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
321template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
322template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
323template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
324
Rui Ueyama512c61d2016-02-03 00:12:24325template uint32_t SymbolBody::template getSize<ELF32LE>() const;
326template uint32_t SymbolBody::template getSize<ELF32BE>() const;
327template uint64_t SymbolBody::template getSize<ELF64LE>() const;
328template uint64_t SymbolBody::template getSize<ELF64BE>() const;
329
Simon Atanasyanf967f092016-09-29 12:58:36330template class elf::DefinedRegular<ELF32LE>;
331template class elf::DefinedRegular<ELF32BE>;
332template class elf::DefinedRegular<ELF64LE>;
333template class elf::DefinedRegular<ELF64BE>;
334
Rafael Espindolae0df00b2016-02-28 00:25:54335template class elf::DefinedSynthetic<ELF32LE>;
336template class elf::DefinedSynthetic<ELF32BE>;
337template class elf::DefinedSynthetic<ELF64LE>;
338template class elf::DefinedSynthetic<ELF64BE>;