blob: e8cbd677a1fa17e83c1e41f2843876563d501e50 [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"
Rui Ueyamac72ba3a2016-11-23 04:57:2520#include <cstring>
Michael J. Spencer1b348a62015-09-04 22:28:1021
22using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0723using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5224using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0725
26using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:5427using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:0728
Rui Ueyamab5a69702016-02-01 21:00:3529template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:0930static typename ELFT::uint getSymVA(const SymbolBody &Body,
31 typename ELFT::uint &Addend) {
Rui Ueyama9328b2c2016-03-14 23:16:0932 typedef typename ELFT::uint uintX_t;
Rafael Espindola87d9f102016-03-11 12:19:0533
34 switch (Body.kind()) {
35 case SymbolBody::DefinedSyntheticKind: {
36 auto &D = cast<DefinedSynthetic<ELFT>>(Body);
Rafael Espindolae08e78d2016-11-09 23:23:4537 const OutputSectionBase *Sec = D.Section;
Peter Collingbourne6a422592016-05-03 01:21:0838 if (!Sec)
39 return D.Value;
Simon Atanasyan13f6da12016-03-31 21:26:2340 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
Rafael Espindola04a2e342016-11-09 01:42:4141 return Sec->Addr + Sec->Size;
42 return Sec->Addr + D.Value;
Rui Ueyamab5a69702016-02-01 21:00:3543 }
Rafael Espindola87d9f102016-03-11 12:19:0544 case SymbolBody::DefinedRegularKind: {
45 auto &D = cast<DefinedRegular<ELFT>>(Body);
46 InputSectionBase<ELFT> *SC = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:3547
Rafael Espindolaccfe3cb2016-04-04 14:04:1648 // According to the ELF spec reference to a local symbol from outside
49 // the group are not allowed. Unfortunately .eh_frame breaks that rule
50 // and must be treated specially. For now we just replace the symbol with
51 // 0.
52 if (SC == &InputSection<ELFT>::Discarded)
53 return 0;
54
Rui Ueyamab5a69702016-02-01 21:00:3555 // This is an absolute symbol.
56 if (!SC)
Rafael Espindolaccfe3cb2016-04-04 14:04:1657 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:3558
Rafael Espindolaccfe3cb2016-04-04 14:04:1659 uintX_t Offset = D.Value;
60 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:3761 Offset += Addend;
62 Addend = 0;
63 }
Rafael Espindola04a2e342016-11-09 01:42:4164 uintX_t VA = (SC->OutSec ? SC->OutSec->Addr : 0) + SC->getOffset(Offset);
George Rimar6a3b1542016-10-04 08:52:5165 if (D.isTls() && !Config->Relocatable) {
66 if (!Out<ELFT>::TlsPhdr)
67 fatal(getFilename(D.File) +
68 " has a STT_TLS symbol but doesn't have a PT_TLS section");
Rafael Espindola1f5b70f2016-03-11 14:21:3769 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
George Rimar6a3b1542016-10-04 08:52:5170 }
Rafael Espindola1f5b70f2016-03-11 14:21:3771 return VA;
Rui Ueyamab5a69702016-02-01 21:00:3572 }
Rui Ueyama07784902016-08-02 01:35:1373 case SymbolBody::DefinedCommonKind:
Rafael Espindola04a2e342016-11-09 01:42:4174 return In<ELFT>::Common->OutSec->Addr + In<ELFT>::Common->OutSecOff +
Rafael Espindolae7553e42016-08-31 13:28:3375 cast<DefinedCommon>(Body).Offset;
Rafael Espindola87d9f102016-03-11 12:19:0576 case SymbolBody::SharedKind: {
77 auto &SS = cast<SharedSymbol<ELFT>>(Body);
78 if (!SS.NeedsCopyOrPltAddr)
Rafael Espindolaa0a65f92016-02-09 15:11:0179 return 0;
Rafael Espindolaccfe3cb2016-04-04 14:04:1680 if (SS.isFunc())
Rafael Espindola87d9f102016-03-11 12:19:0581 return Body.getPltVA<ELFT>();
Rafael Espindola04a2e342016-11-09 01:42:4182 return Out<ELFT>::Bss->Addr + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:3583 }
Peter Collingbourne60976ed2016-04-27 00:05:0684 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:3585 return 0;
Rui Ueyamaf8baa662016-04-07 19:24:5186 case SymbolBody::LazyArchiveKind:
87 case SymbolBody::LazyObjectKind:
Peter Collingbourne4f952702016-05-01 04:55:0388 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:3589 return 0;
Rui Ueyamab5a69702016-02-01 21:00:3590 }
George Rimar777f9632016-03-12 08:31:3491 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:3592}
93
Rui Ueyamac72ba3a2016-11-23 04:57:2594SymbolBody::SymbolBody(Kind K, const char *Name, uint8_t StOther, 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),
Rui Ueyamac72ba3a2016-11-23 04:57:2597 StOther(StOther), Name(Name) {}
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),
Rui Ueyamac72ba3a2016-11-23 04:57:25102 StOther(StOther), NameLen(Name.size()), Name(Name.data()) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16103
George Rimar43651582016-06-28 08:21:10104StringRef SymbolBody::getName() const {
Rui Ueyamac72ba3a2016-11-23 04:57:25105 if (NameLen == (uint32_t)-1)
106 NameLen = strlen(Name);
107 return StringRef(Name, NameLen);
Rui Ueyama663b8c22016-07-17 17:23:17108}
109
Rui Ueyamac4466602016-03-13 19:48:18110// Returns true if a symbol can be replaced at load-time by a symbol
111// with the same name defined in other ELF executable or DSO.
112bool SymbolBody::isPreemptible() const {
113 if (isLocal())
114 return false;
115
Rafael Espindola66434562016-05-05 19:41:49116 // Shared symbols resolve to the definition in the DSO. The exceptions are
117 // symbols with copy relocations (which resolve to .bss) or preempt plt
118 // entries (which resolve to that plt entry).
Rui Ueyamac4466602016-03-13 19:48:18119 if (isShared())
Rafael Espindola66434562016-05-05 19:41:49120 return !NeedsCopyOrPltAddr;
Rui Ueyamac4466602016-03-13 19:48:18121
Peter Collingbourne66ac1d62016-04-22 20:21:26122 // That's all that can be preempted in a non-DSO.
Rui Ueyamac4466602016-03-13 19:48:18123 if (!Config->Shared)
124 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26125
Peter Collingbournedbe41872016-04-24 04:29:59126 // Only symbols that appear in dynsym can be preempted.
Peter Collingbourne4f952702016-05-01 04:55:03127 if (!symbol()->includeInDynsym())
Rui Ueyamac4466602016-03-13 19:48:18128 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26129
Rafael Espindola580d7a12016-07-07 22:50:54130 // Only default visibility symbols can be preempted.
131 if (symbol()->Visibility != STV_DEFAULT)
132 return false;
133
134 // -Bsymbolic means that definitions are not preempted.
Peter Collingbournedbe41872016-04-24 04:29:59135 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
136 return !isDefined();
Rafael Espindola580d7a12016-07-07 22:50:54137 return true;
Rui Ueyamac4466602016-03-13 19:48:18138}
139
Peter Smithfb05cd92016-07-08 16:10:27140template <class ELFT> bool SymbolBody::hasThunk() const {
141 if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
142 return DR->ThunkData != nullptr;
143 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
144 return S->ThunkData != nullptr;
145 return false;
146}
147
Rui Ueyamab5a69702016-02-01 21:00:35148template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09149typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19150 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
151 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05152}
153
Rui Ueyama9328b2c2016-03-14 23:16:09154template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Eugene Leviantad4439e2016-11-11 11:33:32155 return In<ELFT>::Got->getVA() + getGotOffset<ELFT>();
Rafael Espindola74031ba2016-04-07 15:20:56156}
157
158template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14159 return GotIndex * Target->GotEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35160}
161
Rui Ueyama9328b2c2016-03-14 23:16:09162template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Eugene Leviant41ca3272016-11-10 09:48:29163 return In<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
Rafael Espindola74031ba2016-04-07 15:20:56164}
165
166template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
Rui Ueyama803b1202016-07-13 18:55:14167 return GotPltIndex * Target->GotPltEntrySize;
Rui Ueyamab5a69702016-02-01 21:00:35168}
169
Rui Ueyama9328b2c2016-03-14 23:16:09170template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Eugene Leviantff23d3e2016-11-18 14:35:03171 return In<ELFT>::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35172 PltIndex * Target->PltEntrySize;
173}
174
Simon Atanasyan13f6da12016-03-31 21:26:23175template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
Peter Smithfb05cd92016-07-08 16:10:27176 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
177 return DR->ThunkData->getVA();
178 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
179 return S->ThunkData->getVA();
180 fatal("getThunkVA() not supported for Symbol class\n");
Simon Atanasyan13f6da12016-03-31 21:26:23181}
182
Rui Ueyama9328b2c2016-03-14 23:16:09183template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolae7553e42016-08-31 13:28:33184 if (const auto *C = dyn_cast<DefinedCommon>(this))
Rafael Espindolaccfe3cb2016-04-04 14:04:16185 return C->Size;
186 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
187 return DR->Size;
188 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
189 return S->Sym.st_size;
Rui Ueyama512c61d2016-02-03 00:12:24190 return 0;
191}
192
Peter Collingbourne4f952702016-05-01 04:55:03193Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
194 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16195
Rui Ueyamac72ba3a2016-11-23 04:57:25196Defined::Defined(Kind K, const char *Name, uint8_t StOther, uint8_t Type)
197 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42198
Simon Atanasyanf967f092016-09-29 12:58:36199template <class ELFT> bool DefinedRegular<ELFT>::isMipsPIC() const {
200 if (!Section || !isFunc())
201 return false;
202 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
203 (Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC);
204}
205
Rui Ueyama434b5612016-07-17 03:11:46206Undefined::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:50211
Rui Ueyamac72ba3a2016-11-23 04:57:25212Undefined::Undefined(const char *Name, uint8_t StOther, uint8_t Type,
Rui Ueyama434b5612016-07-17 03:11:46213 InputFile *File)
Rui Ueyamac72ba3a2016-11-23 04:57:25214 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {
Rui Ueyama434b5612016-07-17 03:11:46215 this->File = File;
216}
Rafael Espindola5d7593b2015-12-22 23:00:50217
Rafael Espindola4d4b06a2015-12-24 00:47:42218template <typename ELFT>
219DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Eugene Leviantafaa9342016-11-16 09:49:39220 const OutputSectionBase *Section)
Peter Collingbourne4f952702016-05-01 04:55:03221 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42222 Value(Value), Section(Section) {}
223
Rafael Espindolae7553e42016-08-31 13:28:33224DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
225 uint8_t StOther, uint8_t Type, InputFile *File)
Peter Collingbourne4f952702016-05-01 04:55:03226 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama2a7c1c12016-07-17 17:36:22227 Alignment(Alignment), Size(Size) {
228 this->File = File;
229}
Rafael Espindola11191912015-12-24 16:23:37230
Rui Ueyama55518e72016-10-28 20:57:25231InputFile *Lazy::fetch() {
Rui Ueyamaf8baa662016-04-07 19:24:51232 if (auto *S = dyn_cast<LazyArchive>(this))
Rui Ueyama55518e72016-10-28 20:57:25233 return S->fetch();
234 return cast<LazyObject>(this)->fetch();
Rui Ueyamaf8baa662016-04-07 19:24:51235}
236
Rui Ueyama434b5612016-07-17 03:11:46237LazyArchive::LazyArchive(ArchiveFile &File,
238 const llvm::object::Archive::Symbol S, uint8_t Type)
239 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
240 this->File = &File;
241}
242
243LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
244 : Lazy(LazyObjectKind, Name, Type) {
245 this->File = &File;
246}
247
Rui Ueyama55518e72016-10-28 20:57:25248InputFile *LazyArchive::fetch() {
Davide Italianobcdd6c62016-10-12 19:35:54249 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10250
251 // getMember returns an empty buffer if the member was already
252 // read from the library.
Davide Italianobcdd6c62016-10-12 19:35:54253 if (MBInfo.first.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51254 return nullptr;
Rui Ueyama55518e72016-10-28 20:57:25255 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
Michael J. Spencer1b348a62015-09-04 22:28:10256}
257
Rui Ueyama55518e72016-10-28 20:57:25258InputFile *LazyObject::fetch() {
Rui Ueyama434b5612016-07-17 03:11:46259 MemoryBufferRef MBRef = file()->getBuffer();
Rafael Espindola65c65ce2016-06-14 21:56:36260 if (MBRef.getBuffer().empty())
Rui Ueyama38dbd3e2016-09-14 00:05:51261 return nullptr;
Rui Ueyama55518e72016-10-28 20:57:25262 return createObjectFile(MBRef);
Rui Ueyamaf8baa662016-04-07 19:24:51263}
264
Peter Collingbournedadcc172016-04-22 18:42:48265bool Symbol::includeInDynsym() const {
266 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25267 return false;
George Rimard3566302016-06-20 11:55:12268 return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() ||
Peter Collingbourne4f952702016-05-01 04:55:03269 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25270}
Rui Ueyama69c778c2016-07-17 17:50:09271
272// Print out a log message for --trace-symbol.
273void elf::printTraceSymbol(Symbol *Sym) {
274 SymbolBody *B = Sym->body();
275 outs() << getFilename(B->File);
276
277 if (B->isUndefined())
278 outs() << ": reference to ";
279 else if (B->isCommon())
280 outs() << ": common definition of ";
281 else
282 outs() << ": definition of ";
283 outs() << B->getName() << "\n";
284}
285
Peter Smithfb05cd92016-07-08 16:10:27286template bool SymbolBody::hasThunk<ELF32LE>() const;
287template bool SymbolBody::hasThunk<ELF32BE>() const;
288template bool SymbolBody::hasThunk<ELF64LE>() const;
289template bool SymbolBody::hasThunk<ELF64BE>() const;
Rafael Espindolaae605c12016-04-21 20:35:25290
Rafael Espindola87d9f102016-03-11 12:19:05291template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
292template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
293template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
294template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35295
296template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
297template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
298template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
299template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
300
Rafael Espindola74031ba2016-04-07 15:20:56301template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
302template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
303template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
304template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
305
Rui Ueyamab5a69702016-02-01 21:00:35306template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
307template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
308template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
309template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
310
Peter Smithfb05cd92016-07-08 16:10:27311template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
312template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
313template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
314template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
315
Rafael Espindola74031ba2016-04-07 15:20:56316template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
317template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
318template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
319template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
320
Rui Ueyamab5a69702016-02-01 21:00:35321template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
322template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
323template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
324template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
325
Rui Ueyama512c61d2016-02-03 00:12:24326template uint32_t SymbolBody::template getSize<ELF32LE>() const;
327template uint32_t SymbolBody::template getSize<ELF32BE>() const;
328template uint64_t SymbolBody::template getSize<ELF64LE>() const;
329template uint64_t SymbolBody::template getSize<ELF64BE>() const;
330
Simon Atanasyanf967f092016-09-29 12:58:36331template class elf::DefinedRegular<ELF32LE>;
332template class elf::DefinedRegular<ELF32BE>;
333template class elf::DefinedRegular<ELF64LE>;
334template class elf::DefinedRegular<ELF64BE>;
335
Rafael Espindolae0df00b2016-02-28 00:25:54336template class elf::DefinedSynthetic<ELF32LE>;
337template class elf::DefinedSynthetic<ELF32BE>;
338template class elf::DefinedSynthetic<ELF64LE>;
339template class elf::DefinedSynthetic<ELF64BE>;