blob: 8b1c70c7baaa2d3e009c484d52b3d15379ece4c9 [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;
29using namespace lld::elf2;
30
Rui Ueyamab5a69702016-02-01 21:00:35 +000031template <class ELFT>
32typename ELFFile<ELFT>::uintX_t SymbolBody::getVA() const {
33 switch (kind()) {
34 case DefinedSyntheticKind: {
35 auto *D = cast<DefinedSynthetic<ELFT>>(this);
36 return D->Section.getVA() + D->Value;
37 }
38 case DefinedRegularKind: {
39 auto *D = cast<DefinedRegular<ELFT>>(this);
40 InputSectionBase<ELFT> *SC = D->Section;
41
42 // This is an absolute symbol.
43 if (!SC)
44 return D->Sym.st_value;
45
46 // Symbol offsets for AMDGPU need to be the offset in bytes of the symbol
47 // from the beginning of the section.
48 if (Config->EMachine == EM_AMDGPU)
49 return SC->getOffset(D->Sym);
50 if (D->Sym.getType() == STT_TLS)
51 return SC->OutSec->getVA() + SC->getOffset(D->Sym) -
52 Out<ELFT>::TlsPhdr->p_vaddr;
53 return SC->OutSec->getVA() + SC->getOffset(D->Sym);
54 }
55 case DefinedCommonKind:
56 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(this)->OffsetInBss;
57 case SharedKind: {
58 auto *SS = cast<SharedSymbol<ELFT>>(this);
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000059 if (!SS->NeedsCopyOrPltAddr)
60 return 0;
61 if (SS->IsFunc)
62 return getPltVA<ELFT>();
63 else
Rui Ueyamab5a69702016-02-01 21:00:35 +000064 return Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:35 +000065 }
66 case UndefinedElfKind:
67 case UndefinedKind:
68 return 0;
69 case LazyKind:
70 assert(isUsedInRegularObj() && "Lazy symbol reached writer");
71 return 0;
72 }
73 llvm_unreachable("Invalid symbol kind");
74}
75
76template <class ELFT>
77typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
78 return Out<ELFT>::Got->getVA() +
79 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
80 sizeof(typename ELFFile<ELFT>::uintX_t);
81}
82
83template <class ELFT>
84typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
85 return Out<ELFT>::GotPlt->getVA() +
86 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
87}
88
89template <class ELFT>
90typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
91 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
92 PltIndex * Target->PltEntrySize;
93}
94
Rui Ueyama512c61d2016-02-03 00:12:24 +000095template <class ELFT>
96typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
97 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
98 return B->Sym.st_size;
99 return 0;
100}
101
Rafael Espindola78471f02015-09-01 23:12:52 +0000102static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
103 if (VA == STV_DEFAULT)
104 return VB;
105 if (VB == STV_DEFAULT)
106 return VA;
107 return std::min(VA, VB);
108}
109
Michael J. Spencer84487f12015-07-24 21:03:07 +0000110// Returns 1, 0 or -1 if this symbol should take precedence
111// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000112template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rui Ueyama6be68522015-12-16 23:49:19 +0000113 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000114 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05 +0000115 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
116 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
117 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000118
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000119 // Normalize
120 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000121 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000122
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000123 Visibility = Other->Visibility =
124 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52 +0000125
Rui Ueyama86696f32015-10-21 19:41:03 +0000126 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
127 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05 +0000128
George Rimar02ca1792016-01-25 08:44:38 +0000129 // We want to export all symbols that exist both in the executable
130 // and in DSOs, so that the symbols in the executable can interrupt
131 // symbols in the DSO at runtime.
132 if (isShared() != Other->isShared())
133 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15 +0000134 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38 +0000135
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000136 if (L != R)
137 return -1;
Rafael Espindola0bc0c022016-01-18 23:54:05 +0000138 if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L))
Rafael Espindola8e5560d2015-09-23 14:23:59 +0000139 return 1;
Rui Ueyama7da94a52015-09-09 17:40:51 +0000140 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:09 +0000141 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:30 +0000142 return -1;
Rafael Espindola11191912015-12-24 16:23:37 +0000143 auto *ThisC = cast<DefinedCommon>(this);
144 auto *OtherC = cast<DefinedCommon>(Other);
Rui Ueyama6be68522015-12-16 23:49:19 +0000145 uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
Rafael Espindola11191912015-12-24 16:23:37 +0000146 if (ThisC->Size >= OtherC->Size) {
Rui Ueyama6be68522015-12-16 23:49:19 +0000147 ThisC->MaxAlignment = Align;
Rui Ueyama6666f6a2015-09-09 17:55:09 +0000148 return 1;
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000149 }
Rui Ueyama6be68522015-12-16 23:49:19 +0000150 OtherC->MaxAlignment = Align;
Rui Ueyama7da94a52015-09-09 17:40:51 +0000151 return -1;
Rafael Espindola30e17972015-08-30 23:17:30 +0000152 }
Rui Ueyama7da94a52015-09-09 17:40:51 +0000153 if (Other->isCommon())
154 return 1;
155 return 0;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000156}
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000157
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000158Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
George Rimar5c36e592016-02-02 09:28:53 +0000159 bool IsTls, bool IsFunction)
160 : SymbolBody(K, Name, IsWeak, Visibility, IsTls, IsFunction) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000161
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000162Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
163 uint8_t Visibility, bool IsTls)
George Rimar5c36e592016-02-02 09:28:53 +0000164 : SymbolBody(K, N, IsWeak, Visibility, IsTls, /*IsFunction*/ false),
165 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000166
167Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
168 bool CanKeepUndefined)
169 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility,
170 /*IsTls*/ false) {
171 this->CanKeepUndefined = CanKeepUndefined;
172}
173
174template <typename ELFT>
175UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
176 : Undefined(SymbolBody::UndefinedElfKind, N,
177 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
178 Sym.getType() == llvm::ELF::STT_TLS),
179 Sym(Sym) {}
180
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000181template <typename ELFT>
182DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
183 OutputSectionBase<ELFT> &Section)
George Rimar5c36e592016-02-02 09:28:53 +0000184 : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT,
185 /*IsTls*/ false, /*IsFunction*/ false),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000186 Value(Value), Section(Section) {}
187
Rafael Espindola11191912015-12-24 16:23:37 +0000188DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
189 bool IsWeak, uint8_t Visibility)
George Rimar5c36e592016-02-02 09:28:53 +0000190 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility,
191 /*IsTls*/ false, /*IsFunction*/ false) {
Rafael Espindola11191912015-12-24 16:23:37 +0000192 MaxAlignment = Alignment;
193 this->Size = Size;
194}
195
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000196std::unique_ptr<InputFile> Lazy::getMember() {
197 MemoryBufferRef MBRef = File->getMember(&Sym);
198
199 // getMember returns an empty buffer if the member was already
200 // read from the library.
201 if (MBRef.getBuffer().empty())
202 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000203 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000204}
205
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000206template <class ELFT> static void doInitSymbols() {
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000207 ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
Rafael Espindola65e80b92016-01-19 21:19:52 +0000208 ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
209 ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000210}
211
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000212void elf2::initSymbols() {
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000213 doInitSymbols<ELF32LE>();
214 doInitSymbols<ELF32BE>();
215 doInitSymbols<ELF64LE>();
216 doInitSymbols<ELF64BE>();
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000217}
218
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000219// Returns the demangled C++ symbol name for Name.
220std::string elf2::demangle(StringRef Name) {
221#if !defined(HAVE_CXXABI_H)
222 return Name;
223#else
224 if (!Config->Demangle)
225 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000226
Rui Ueyamadf154512016-01-13 22:09:09 +0000227 // __cxa_demangle can be used to demangle strings other than symbol
228 // names which do not necessarily start with "_Z". Name can be
229 // either a C or C++ symbol. Don't call __cxa_demangle if the name
230 // does not look like a C++ symbol name to avoid getting unexpected
231 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000232 if (!Name.startswith("_Z"))
233 return Name;
234
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000235 char *Buf =
236 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
237 if (!Buf)
238 return Name;
239 std::string S(Buf);
240 free(Buf);
241 return S;
242#endif
243}
244
Rui Ueyamab5a69702016-02-01 21:00:35 +0000245template uint32_t SymbolBody::template getVA<ELF32LE>() const;
246template uint32_t SymbolBody::template getVA<ELF32BE>() const;
247template uint64_t SymbolBody::template getVA<ELF64LE>() const;
248template uint64_t SymbolBody::template getVA<ELF64BE>() const;
249
250template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
251template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
252template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
253template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
254
255template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
256template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
257template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
258template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
259
260template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
261template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
262template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
263template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
264
Rui Ueyama512c61d2016-02-03 00:12:24 +0000265template uint32_t SymbolBody::template getSize<ELF32LE>() const;
266template uint32_t SymbolBody::template getSize<ELF32BE>() const;
267template uint64_t SymbolBody::template getSize<ELF64LE>() const;
268template uint64_t SymbolBody::template getSize<ELF64BE>() const;
269
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000270template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
271template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
272template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
273template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000274
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000275template class elf2::UndefinedElf<ELF32LE>;
276template class elf2::UndefinedElf<ELF32BE>;
277template class elf2::UndefinedElf<ELF64LE>;
278template class elf2::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000279
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000280template class elf2::DefinedSynthetic<ELF32LE>;
281template class elf2::DefinedSynthetic<ELF32BE>;
282template class elf2::DefinedSynthetic<ELF64LE>;
283template class elf2::DefinedSynthetic<ELF64BE>;