blob: d4e2026c7c5b1f1c7f49c22539fa6ca3966997da [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 Ueyamab5a69702016-02-01 21:00:35 +0000132template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000133typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19 +0000134 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
135 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000136}
137
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000138template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000139 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
140}
141
142template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
143 return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
144 sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000145}
146
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000147template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000148 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
149}
150
151template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
152 return GotPltIndex * sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000153}
154
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000155template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000156 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
157 PltIndex * Target->PltEntrySize;
158}
159
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000160template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
161 auto *D = cast<DefinedRegular<ELFT>>(this);
162 auto *S = cast<InputSection<ELFT>>(D->Section);
163 return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() +
164 ThunkIndex * Target->ThunkSize;
165}
166
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000167template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000168 if (const auto *C = dyn_cast<DefinedCommon>(this))
169 return C->Size;
170 if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this))
171 return DR->Size;
172 if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
173 return S->Sym.st_size;
Rui Ueyama512c61d2016-02-03 00:12:24 +0000174 return 0;
175}
176
Peter Collingbourne4f952702016-05-01 04:55:03 +0000177Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
178 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000179
Rafael Espindolaf9b79a42016-04-06 12:14:31 +0000180Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
181 : SymbolBody(K, NameOffset, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000182
Peter Collingbourne4f952702016-05-01 04:55:03 +0000183DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
184 BitcodeFile *F)
185 : Defined(DefinedBitcodeKind, Name, StOther, Type), File(F) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000186
187bool DefinedBitcode::classof(const SymbolBody *S) {
188 return S->kind() == DefinedBitcodeKind;
189}
190
Peter Collingbourne4f952702016-05-01 04:55:03 +0000191Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type)
192 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000193
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000194Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000195 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000196
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000197template <typename ELFT>
198DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbournef6e9b4e2016-04-13 16:57:28 +0000199 OutputSectionBase<ELFT> &Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000200 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000201 Value(Value), Section(Section) {}
202
Rafael Espindola11191912015-12-24 16:23:37 +0000203DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000204 uint8_t StOther, uint8_t Type)
205 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama17d69832016-03-10 18:58:53 +0000206 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000207
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000208std::unique_ptr<InputFile> Lazy::getFile() {
209 if (auto *S = dyn_cast<LazyArchive>(this))
210 return S->getFile();
211 return cast<LazyObject>(this)->getFile();
212}
213
214std::unique_ptr<InputFile> LazyArchive::getFile() {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000215 MemoryBufferRef MBRef = File->getMember(&Sym);
216
217 // getMember returns an empty buffer if the member was already
218 // read from the library.
219 if (MBRef.getBuffer().empty())
220 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000221 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000222}
223
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000224std::unique_ptr<InputFile> LazyObject::getFile() {
225 return createObjectFile(MBRef);
226}
227
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000228// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000229std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000230#if !defined(HAVE_CXXABI_H)
231 return Name;
232#else
233 if (!Config->Demangle)
234 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000235
Rui Ueyamadf154512016-01-13 22:09:09 +0000236 // __cxa_demangle can be used to demangle strings other than symbol
237 // names which do not necessarily start with "_Z". Name can be
238 // either a C or C++ symbol. Don't call __cxa_demangle if the name
239 // does not look like a C++ symbol name to avoid getting unexpected
240 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000241 if (!Name.startswith("_Z"))
242 return Name;
243
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000244 char *Buf =
245 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
246 if (!Buf)
247 return Name;
248 std::string S(Buf);
249 free(Buf);
250 return S;
251#endif
252}
253
Peter Collingbournedadcc172016-04-22 18:42:48 +0000254bool Symbol::includeInDynsym() const {
255 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000256 return false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000257 return (ExportDynamic && VersionScriptGlobal) || body()->isShared() ||
258 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25 +0000259}
260
Rafael Espindola87d9f102016-03-11 12:19:05 +0000261template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
262template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
263template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
264template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000265
266template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
267template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
268template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
269template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
270
Rafael Espindola74031ba2016-04-07 15:20:56 +0000271template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
272template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
273template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
274template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
275
Rui Ueyamab5a69702016-02-01 21:00:35 +0000276template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
277template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
278template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
279template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
280
Rafael Espindola74031ba2016-04-07 15:20:56 +0000281template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
282template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
283template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
284template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
285
Rui Ueyamab5a69702016-02-01 21:00:35 +0000286template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
287template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
288template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
289template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
290
Rui Ueyama512c61d2016-02-03 00:12:24 +0000291template uint32_t SymbolBody::template getSize<ELF32LE>() const;
292template uint32_t SymbolBody::template getSize<ELF32BE>() const;
293template uint64_t SymbolBody::template getSize<ELF64LE>() const;
294template uint64_t SymbolBody::template getSize<ELF64BE>() const;
295
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000296template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
297template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
298template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
299template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
300
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000301template class elf::DefinedSynthetic<ELF32LE>;
302template class elf::DefinedSynthetic<ELF32BE>;
303template class elf::DefinedSynthetic<ELF64LE>;
304template class elf::DefinedSynthetic<ELF64BE>;