blob: 4e94229f3e3688faca57592c99db1574930f4204 [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"
Rui Ueyamaa4a628f2016-01-13 18:55:39 +000018#include "llvm/Config/config.h"
19
20#ifdef HAVE_CXXABI_H
21#include <cxxabi.h>
22#endif
Michael J. Spencer1b348a62015-09-04 22:28:10 +000023
24using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:52 +000026using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
28using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000029using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
Rui Ueyamab5a69702016-02-01 21:00:35 +000031template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000032static typename ELFT::uint getSymVA(const SymbolBody &Body,
33 typename ELFT::uint &Addend) {
Rui Ueyama9328b2c2016-03-14 23:16:09 +000034 typedef typename ELFT::uint uintX_t;
Rafael Espindola87d9f102016-03-11 12:19:05 +000035
36 switch (Body.kind()) {
37 case SymbolBody::DefinedSyntheticKind: {
38 auto &D = cast<DefinedSynthetic<ELFT>>(Body);
Simon Atanasyan13f6da12016-03-31 21:26:23 +000039 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
40 return D.Section.getVA() + D.Section.getSize();
Rafael Espindola87d9f102016-03-11 12:19:05 +000041 return D.Section.getVA() + D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000042 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000043 case SymbolBody::DefinedRegularKind: {
44 auto &D = cast<DefinedRegular<ELFT>>(Body);
45 InputSectionBase<ELFT> *SC = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:35 +000046
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000047 // 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:35 +000054 // This is an absolute symbol.
55 if (!SC)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000056 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000057
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000058 uintX_t Offset = D.Value;
59 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000060 Offset += Addend;
61 Addend = 0;
62 }
63 uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000064 if (D.isTls())
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000065 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
66 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35 +000067 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000068 case SymbolBody::DefinedCommonKind:
69 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
70 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)
Peter Collingbourne4f952702016-05-01 04:55:03 +000092 : SymbolKind(K), IsLocal(true), Type(Type), StOther(StOther),
Rafael Espindola5e345682016-04-06 14:31:03 +000093 NameOffset(NameOffset) {
Rafael Espindolaf4765732016-04-06 13:22:41 +000094 init();
95}
96
Peter Collingbourne4f952702016-05-01 04:55:03 +000097SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
98 : SymbolKind(K), IsLocal(false), Type(Type), StOther(StOther),
Rafael Espindolaf4765732016-04-06 13:22:41 +000099 Name({Name.data(), Name.size()}) {
Rafael Espindolaf4765732016-04-06 13:22:41 +0000100 init();
101}
102
103void SymbolBody::init() {
Rafael Espindola5e345682016-04-06 14:31:03 +0000104 NeedsCopyOrPltAddr = false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000105}
106
Rui Ueyamac4466602016-03-13 19:48:18 +0000107// Returns true if a symbol can be replaced at load-time by a symbol
108// with the same name defined in other ELF executable or DSO.
109bool SymbolBody::isPreemptible() const {
110 if (isLocal())
111 return false;
112
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000113 // Shared symbols resolve to the definition in the DSO.
Rui Ueyamac4466602016-03-13 19:48:18 +0000114 if (isShared())
115 return true;
116
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000117 // That's all that can be preempted in a non-DSO.
Rui Ueyamac4466602016-03-13 19:48:18 +0000118 if (!Config->Shared)
119 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000120
Peter Collingbournedbe41872016-04-24 04:29:59 +0000121 // Only symbols that appear in dynsym can be preempted.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000122 if (!symbol()->includeInDynsym())
Rui Ueyamac4466602016-03-13 19:48:18 +0000123 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000124
Peter Collingbournedbe41872016-04-24 04:29:59 +0000125 // Normally only default visibility symbols can be preempted, but -Bsymbolic
126 // means that not even they can be preempted.
127 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
128 return !isDefined();
Peter Collingbourne4f952702016-05-01 04:55:03 +0000129 return symbol()->Visibility == STV_DEFAULT;
Rui Ueyamac4466602016-03-13 19:48:18 +0000130}
131
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000132template <class ELFT> InputFile *SymbolBody::getSourceFile() {
133 if (auto *S = dyn_cast<DefinedRegular<ELFT>>(this))
134 return S->Section ? S->Section->getFile() : nullptr;
135 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
136 return S->File;
137 if (auto *S = dyn_cast<DefinedBitcode>(this))
138 return S->File;
139 if (auto *S = dyn_cast<Undefined>(this))
140 return S->File;
141 return nullptr;
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 {
155 return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
156 sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000157}
158
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000159template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000160 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
161}
162
163template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
164 return GotPltIndex * sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000165}
166
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000167template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000168 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
169 PltIndex * Target->PltEntrySize;
170}
171
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000172template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
173 auto *D = cast<DefinedRegular<ELFT>>(this);
174 auto *S = cast<InputSection<ELFT>>(D->Section);
175 return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() +
176 ThunkIndex * Target->ThunkSize;
177}
178
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000179template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000180 if (const auto *C = dyn_cast<DefinedCommon>(this))
181 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)
197 : Defined(DefinedBitcodeKind, Name, StOther, Type), File(F) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000198
199bool DefinedBitcode::classof(const SymbolBody *S) {
200 return S->kind() == DefinedBitcodeKind;
201}
202
Peter Collingbourne4f952702016-05-01 04:55:03 +0000203Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type)
204 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000205
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000206Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000207 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000208
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000209template <typename ELFT>
210DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbournef6e9b4e2016-04-13 16:57:28 +0000211 OutputSectionBase<ELFT> &Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000212 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000213 Value(Value), Section(Section) {}
214
Rafael Espindola11191912015-12-24 16:23:37 +0000215DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000216 uint8_t StOther, uint8_t Type)
217 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama17d69832016-03-10 18:58:53 +0000218 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000219
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000220std::unique_ptr<InputFile> Lazy::getFile() {
221 if (auto *S = dyn_cast<LazyArchive>(this))
222 return S->getFile();
223 return cast<LazyObject>(this)->getFile();
224}
225
226std::unique_ptr<InputFile> LazyArchive::getFile() {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000227 MemoryBufferRef MBRef = File->getMember(&Sym);
228
229 // getMember returns an empty buffer if the member was already
230 // read from the library.
231 if (MBRef.getBuffer().empty())
232 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000233 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000234}
235
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000236std::unique_ptr<InputFile> LazyObject::getFile() {
237 return createObjectFile(MBRef);
238}
239
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000240// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000241std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000242#if !defined(HAVE_CXXABI_H)
243 return Name;
244#else
245 if (!Config->Demangle)
246 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000247
Rui Ueyamadf154512016-01-13 22:09:09 +0000248 // __cxa_demangle can be used to demangle strings other than symbol
249 // names which do not necessarily start with "_Z". Name can be
250 // either a C or C++ symbol. Don't call __cxa_demangle if the name
251 // does not look like a C++ symbol name to avoid getting unexpected
252 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000253 if (!Name.startswith("_Z"))
254 return Name;
255
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000256 char *Buf =
257 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
258 if (!Buf)
259 return Name;
260 std::string S(Buf);
261 free(Buf);
262 return S;
263#endif
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;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000269 return (ExportDynamic && VersionScriptGlobal) || body()->isShared() ||
270 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25 +0000271}
272
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000273template InputFile *SymbolBody::template getSourceFile<ELF32LE>();
274template InputFile *SymbolBody::template getSourceFile<ELF32BE>();
275template InputFile *SymbolBody::template getSourceFile<ELF64LE>();
276template InputFile *SymbolBody::template getSourceFile<ELF64BE>();
277
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
Rafael Espindola74031ba2016-04-07 15:20:56 +0000298template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
299template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
300template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
301template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
302
Rui Ueyamab5a69702016-02-01 21:00:35 +0000303template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
304template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
305template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
306template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
307
Rui Ueyama512c61d2016-02-03 00:12:24 +0000308template uint32_t SymbolBody::template getSize<ELF32LE>() const;
309template uint32_t SymbolBody::template getSize<ELF32BE>() const;
310template uint64_t SymbolBody::template getSize<ELF64LE>() const;
311template uint64_t SymbolBody::template getSize<ELF64BE>() const;
312
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000313template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
314template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
315template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
316template uint64_t SymbolBody::template getThunkVA<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>;