blob: 2768f1865a2da7fc8b931ef0d50c6aac5ef087c8 [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)
Peter Collingbourne4f952702016-05-01 04:55:03 +000095 : SymbolKind(K), IsLocal(true), Type(Type), StOther(StOther),
Rafael Espindola5e345682016-04-06 14:31:03 +000096 NameOffset(NameOffset) {
Rafael Espindolaf4765732016-04-06 13:22:41 +000097 init();
98}
99
Peter Collingbourne4f952702016-05-01 04:55:03 +0000100SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
101 : SymbolKind(K), IsLocal(false), Type(Type), StOther(StOther),
Rafael Espindolaf4765732016-04-06 13:22:41 +0000102 Name({Name.data(), Name.size()}) {
Rafael Espindolaf4765732016-04-06 13:22:41 +0000103 init();
104}
105
106void SymbolBody::init() {
Rafael Espindola5e345682016-04-06 14:31:03 +0000107 NeedsCopyOrPltAddr = false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000108}
109
Rui Ueyamac4466602016-03-13 19:48:18 +0000110// 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
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000116 // Shared symbols resolve to the definition in the DSO.
Rui Ueyamac4466602016-03-13 19:48:18 +0000117 if (isShared())
118 return true;
119
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000120 // That's all that can be preempted in a non-DSO.
Rui Ueyamac4466602016-03-13 19:48:18 +0000121 if (!Config->Shared)
122 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000123
Peter Collingbournedbe41872016-04-24 04:29:59 +0000124 // Only symbols that appear in dynsym can be preempted.
Peter Collingbourne4f952702016-05-01 04:55:03 +0000125 if (!symbol()->includeInDynsym())
Rui Ueyamac4466602016-03-13 19:48:18 +0000126 return false;
Peter Collingbourne66ac1d62016-04-22 20:21:26 +0000127
Peter Collingbournedbe41872016-04-24 04:29:59 +0000128 // Normally only default visibility symbols can be preempted, but -Bsymbolic
129 // means that not even they can be preempted.
130 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
131 return !isDefined();
Peter Collingbourne4f952702016-05-01 04:55:03 +0000132 return symbol()->Visibility == STV_DEFAULT;
Rui Ueyamac4466602016-03-13 19:48:18 +0000133}
134
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000135template <class ELFT> InputFile *SymbolBody::getSourceFile() {
136 if (auto *S = dyn_cast<DefinedRegular<ELFT>>(this))
137 return S->Section ? S->Section->getFile() : nullptr;
138 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
139 return S->File;
140 if (auto *S = dyn_cast<DefinedBitcode>(this))
141 return S->File;
142 if (auto *S = dyn_cast<Undefined>(this))
143 return S->File;
144 return nullptr;
145}
146
Rui Ueyamab5a69702016-02-01 21:00:35 +0000147template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000148typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19 +0000149 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
150 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000151}
152
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000153template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000154 return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>();
155}
156
157template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const {
158 return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
159 sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000160}
161
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000162template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
Rafael Espindola74031ba2016-04-07 15:20:56 +0000163 return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>();
164}
165
166template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const {
167 return GotPltIndex * sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000168}
169
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000170template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000171 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
172 PltIndex * Target->PltEntrySize;
173}
174
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000175template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
176 auto *D = cast<DefinedRegular<ELFT>>(this);
177 auto *S = cast<InputSection<ELFT>>(D->Section);
178 return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() +
179 ThunkIndex * Target->ThunkSize;
180}
181
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000182template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000183 if (const auto *C = dyn_cast<DefinedCommon>(this))
184 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:24 +0000189 return 0;
190}
191
Peter Collingbourne4f952702016-05-01 04:55:03 +0000192Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type)
193 : SymbolBody(K, Name, StOther, Type) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000194
Rafael Espindolaf9b79a42016-04-06 12:14:31 +0000195Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type)
196 : SymbolBody(K, NameOffset, StOther, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000197
Peter Collingbourne4f952702016-05-01 04:55:03 +0000198DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type,
199 BitcodeFile *F)
200 : Defined(DefinedBitcodeKind, Name, StOther, Type), File(F) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000201
202bool DefinedBitcode::classof(const SymbolBody *S) {
203 return S->kind() == DefinedBitcodeKind;
204}
205
Peter Collingbourne4f952702016-05-01 04:55:03 +0000206Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type)
207 : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000208
Rui Ueyama62ee16f2016-04-28 00:26:54 +0000209Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000210 : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000211
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000212template <typename ELFT>
213DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
Peter Collingbourne6a422592016-05-03 01:21:08 +0000214 OutputSectionBase<ELFT> *Section)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000215 : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000216 Value(Value), Section(Section) {}
217
Rafael Espindola11191912015-12-24 16:23:37 +0000218DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
Peter Collingbourne4f952702016-05-01 04:55:03 +0000219 uint8_t StOther, uint8_t Type)
220 : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type),
Rui Ueyama17d69832016-03-10 18:58:53 +0000221 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000222
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000223std::unique_ptr<InputFile> Lazy::getFile() {
224 if (auto *S = dyn_cast<LazyArchive>(this))
225 return S->getFile();
226 return cast<LazyObject>(this)->getFile();
227}
228
229std::unique_ptr<InputFile> LazyArchive::getFile() {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000230 MemoryBufferRef MBRef = File->getMember(&Sym);
231
232 // getMember returns an empty buffer if the member was already
233 // read from the library.
234 if (MBRef.getBuffer().empty())
235 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000236 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000237}
238
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000239std::unique_ptr<InputFile> LazyObject::getFile() {
240 return createObjectFile(MBRef);
241}
242
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000243// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000244std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000245#if !defined(HAVE_CXXABI_H)
246 return Name;
247#else
248 if (!Config->Demangle)
249 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000250
Rui Ueyamadf154512016-01-13 22:09:09 +0000251 // __cxa_demangle can be used to demangle strings other than symbol
252 // names which do not necessarily start with "_Z". Name can be
253 // either a C or C++ symbol. Don't call __cxa_demangle if the name
254 // does not look like a C++ symbol name to avoid getting unexpected
255 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000256 if (!Name.startswith("_Z"))
257 return Name;
258
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000259 char *Buf =
260 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
261 if (!Buf)
262 return Name;
263 std::string S(Buf);
264 free(Buf);
265 return S;
266#endif
267}
268
Peter Collingbournedadcc172016-04-22 18:42:48 +0000269bool Symbol::includeInDynsym() const {
270 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
Rafael Espindolaae605c12016-04-21 20:35:25 +0000271 return false;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000272 return (ExportDynamic && VersionScriptGlobal) || body()->isShared() ||
273 (body()->isUndefined() && Config->Shared);
Rafael Espindolaae605c12016-04-21 20:35:25 +0000274}
275
Rui Ueyama6d0cd2b2016-05-02 21:30:42 +0000276template InputFile *SymbolBody::template getSourceFile<ELF32LE>();
277template InputFile *SymbolBody::template getSourceFile<ELF32BE>();
278template InputFile *SymbolBody::template getSourceFile<ELF64LE>();
279template InputFile *SymbolBody::template getSourceFile<ELF64BE>();
280
Rafael Espindola87d9f102016-03-11 12:19:05 +0000281template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
282template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
283template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
284template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000285
286template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
287template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
288template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
289template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
290
Rafael Espindola74031ba2016-04-07 15:20:56 +0000291template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const;
292template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const;
293template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const;
294template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const;
295
Rui Ueyamab5a69702016-02-01 21:00:35 +0000296template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
297template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
298template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
299template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
300
Rafael Espindola74031ba2016-04-07 15:20:56 +0000301template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const;
302template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const;
303template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const;
304template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const;
305
Rui Ueyamab5a69702016-02-01 21:00:35 +0000306template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
307template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
308template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
309template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
310
Rui Ueyama512c61d2016-02-03 00:12:24 +0000311template uint32_t SymbolBody::template getSize<ELF32LE>() const;
312template uint32_t SymbolBody::template getSize<ELF32BE>() const;
313template uint64_t SymbolBody::template getSize<ELF64LE>() const;
314template uint64_t SymbolBody::template getSize<ELF64BE>() const;
315
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000316template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
317template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
318template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
319template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
320
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000321template class elf::DefinedSynthetic<ELF32LE>;
322template class elf::DefinedSynthetic<ELF32BE>;
323template class elf::DefinedSynthetic<ELF64LE>;
324template class elf::DefinedSynthetic<ELF64BE>;