blob: 0ba571b7e9abe337d300f177336a656a233aae65 [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);
Peter Collingbourne6a422592016-05-03 01:21:08 +000039 const OutputSectionBase<ELFT> *Sec = D.Section;
40 if (!Sec)
41 return D.Value;
Simon Atanasyan13f6da12016-03-31 21:26:23 +000042 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
Peter Collingbourne6a422592016-05-03 01:21:08 +000043 return Sec->getVA() + Sec->getSize();
44 return Sec->getVA() + D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000045 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000046 case SymbolBody::DefinedRegularKind: {
47 auto &D = cast<DefinedRegular<ELFT>>(Body);
48 InputSectionBase<ELFT> *SC = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:35 +000049
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000050 // According to the ELF spec reference to a local symbol from outside
51 // the group are not allowed. Unfortunately .eh_frame breaks that rule
52 // and must be treated specially. For now we just replace the symbol with
53 // 0.
54 if (SC == &InputSection<ELFT>::Discarded)
55 return 0;
56
Rui Ueyamab5a69702016-02-01 21:00:35 +000057 // This is an absolute symbol.
58 if (!SC)
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000059 return D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000060
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000061 uintX_t Offset = D.Value;
62 if (D.isSection()) {
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000063 Offset += Addend;
64 Addend = 0;
65 }
66 uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000067 if (D.isTls())
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000068 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
69 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35 +000070 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000071 case SymbolBody::DefinedCommonKind:
72 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
73 case SymbolBody::SharedKind: {
74 auto &SS = cast<SharedSymbol<ELFT>>(Body);
75 if (!SS.NeedsCopyOrPltAddr)
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000076 return 0;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000077 if (SS.isFunc())
Rafael Espindola87d9f102016-03-11 12:19:05 +000078 return Body.getPltVA<ELFT>();
Rui Ueyama2df72892016-03-13 20:54:38 +000079 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:35 +000080 }
Peter Collingbourne60976ed2016-04-27 00:05:06 +000081 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35 +000082 return 0;
Rui Ueyamaf8baa662016-04-07 19:24:51 +000083 case SymbolBody::LazyArchiveKind:
84 case SymbolBody::LazyObjectKind:
Peter Collingbourne4f952702016-05-01 04:55:03 +000085 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35 +000086 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000087 case SymbolBody::DefinedBitcodeKind:
Davide Italianof6523ae2016-03-29 02:20:10 +000088 llvm_unreachable("should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:35 +000089 }
George Rimar777f9632016-03-12 08:31:34 +000090 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35 +000091}
92
Rui Ueyamab5792b22016-04-04 19:09:08 +000093SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther,
94 uint8_t Type)
Simon Atanasyan41325112016-06-19 21:39:37 +000095 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(true),
96 IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
97 NameOffset(NameOffset) {}
Rafael Espindolaf4765732016-04-06 13:22:41 +000098
Peter Collingbourne4f952702016-05-01 04:55:03 +000099SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
Simon Atanasyan41325112016-06-19 21:39:37 +0000100 : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(false),
101 IsInGlobalMipsGot(false), Type(Type), StOther(StOther),
102 Name({Name.data(), Name.size()}) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000103
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
Peter Collingbournedbe41872016-04-24 04:29:59 +0000124 // Normally only default visibility symbols can be preempted, but -Bsymbolic
125 // means that not even they can be preempted.
126 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
127 return !isDefined();
Peter Collingbourne4f952702016-05-01 04:55:03 +0000128 return symbol()->Visibility == STV_DEFAULT;
Rui Ueyamac4466602016-03-13 19:48:18 +0000129}
130
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000131template <class ELFT> InputFile *SymbolBody::getSourceFile() {
132 if (auto *S = dyn_cast<DefinedRegular<ELFT>>(this))
133 return S->Section ? S->Section->getFile() : nullptr;
134 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
135 return S->File;
136 if (auto *S = dyn_cast<DefinedBitcode>(this))
137 return S->File;
138 if (auto *S = dyn_cast<Undefined>(this))
139 return S->File;
140 return nullptr;
141}
142
Rui Ueyamab5a69702016-02-01 21:00:35 +0000143template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000144typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19 +0000145 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
146 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000147}
148
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000149template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000150 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
151}
152
153template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
Simon Atanasyan41325112016-06-19 21:39:37 +0000154 return GotIndex * sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000155}
156
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000157template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000158 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
159}
160
161template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
162 return GotPltIndex * sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000163}
164
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000165template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyama4a90f572016-06-16 16:28:50 +0000166 return Out<ELFT>::Plt->getVA() + Target->PltHeaderSize +
Rui Ueyamab5a69702016-02-01 21:00:35 +0000167 PltIndex * Target->PltEntrySize;
168}
169
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000170template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
171 auto *D = cast<DefinedRegular<ELFT>>(this);
172 auto *S = cast<InputSection<ELFT>>(D->Section);
173 return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() +
174 ThunkIndex * Target->ThunkSize;
175}
176
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000177template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000178 if (const auto *C = dyn_cast<DefinedCommon>(this))
179 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
Peter Collingbourne4f952702016-05-01 04:55:03 +0000193DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
194 BitcodeFile *F)
195 : Defined(DefinedBitcodeKind, Name, StOther, Type), File(F) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000196
197bool DefinedBitcode::classof(const SymbolBody *S) {
198 return S->kind() == DefinedBitcodeKind;
199}
200
Peter Collingbourne4f952702016-05-01 04:55:03 +0000201Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type)
202 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000203
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000204Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000205 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000206
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000207template <typename ELFT>
208DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000209 OutputSectionBase<ELFT> *Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000210 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000211 Value(Value), Section(Section) {}
212
Rafael Espindola11191912015-12-24 16:23:37 +0000213DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000214 uint8_t StOther, uint8_t Type)
215 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama17d69832016-03-10 18:58:53 +0000216 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000217
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000218std::unique_ptr<InputFile> Lazy::getFile() {
219 if (auto *S = dyn_cast<LazyArchive>(this))
220 return S->getFile();
221 return cast<LazyObject>(this)->getFile();
222}
223
224std::unique_ptr<InputFile> LazyArchive::getFile() {
Rafael Espindola07543a82016-06-14 21:40:23 +0000225 MemoryBufferRef MBRef = File.getMember(&Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000226
227 // getMember returns an empty buffer if the member was already
228 // read from the library.
229 if (MBRef.getBuffer().empty())
230 return std::unique_ptr<InputFile>(nullptr);
Rafael Espindola07543a82016-06-14 21:40:23 +0000231 return createObjectFile(MBRef, File.getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000232}
233
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000234std::unique_ptr<InputFile> LazyObject::getFile() {
Rafael Espindola65c65ce2016-06-14 21:56:36 +0000235 MemoryBufferRef MBRef = File.getBuffer();
236 if (MBRef.getBuffer().empty())
237 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000238 return createObjectFile(MBRef);
239}
240
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000241// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000242std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000243#if !defined(HAVE_CXXABI_H)
244 return Name;
245#else
246 if (!Config->Demangle)
247 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000248
Rui Ueyamadf154512016-01-13 22:09:09 +0000249 // __cxa_demangle can be used to demangle strings other than symbol
250 // names which do not necessarily start with "_Z". Name can be
251 // either a C or C++ symbol. Don't call __cxa_demangle if the name
252 // does not look like a C++ symbol name to avoid getting unexpected
253 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000254 if (!Name.startswith("_Z"))
255 return Name;
256
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000257 char *Buf =
258 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
259 if (!Buf)
260 return Name;
261 std::string S(Buf);
262 free(Buf);
263 return S;
264#endif
265}
266
Peter Collingbournedadcc172016-04-22 18:42:48 +0000267bool Symbol::includeInDynsym() const {
268 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000269 return false;
George Rimard03f9722016-06-20 10:29:53 +0000270 return (ExportDynamic && VersionScriptGlobal) || body()->isShared() ||
Peter Collingbourne4f952702016-05-01 04:55:03 +0000271 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25 +0000272}
273
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000274template InputFile *SymbolBody::template getSourceFile<ELF32LE>();
275template InputFile *SymbolBody::template getSourceFile<ELF32BE>();
276template InputFile *SymbolBody::template getSourceFile<ELF64LE>();
277template InputFile *SymbolBody::template getSourceFile<ELF64BE>();
278
Rafael Espindola87d9f102016-03-11 12:19:05 +0000279template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
280template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
281template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
282template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000283
284template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
285template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
286template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
287template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
288
Rafael Espindola74031ba2016-04-07 15:20:56 +0000289template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
290template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
291template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
292template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
293
Rui Ueyamab5a69702016-02-01 21:00:35 +0000294template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
295template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
296template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
297template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
298
Rafael Espindola74031ba2016-04-07 15:20:56 +0000299template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
300template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
301template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
302template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
303
Rui Ueyamab5a69702016-02-01 21:00:35 +0000304template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
305template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
306template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
307template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
308
Rui Ueyama512c61d2016-02-03 00:12:24 +0000309template uint32_t SymbolBody::template getSize<ELF32LE>() const;
310template uint32_t SymbolBody::template getSize<ELF32BE>() const;
311template uint64_t SymbolBody::template getSize<ELF64LE>() const;
312template uint64_t SymbolBody::template getSize<ELF64BE>() const;
313
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000314template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
315template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
316template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
317template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
318
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000319template class elf::DefinedSynthetic<ELF32LE>;
320template class elf::DefinedSynthetic<ELF32BE>;
321template class elf::DefinedSynthetic<ELF64LE>;
322template class elf::DefinedSynthetic<ELF64BE>;