blob: dec844277c110840b5093b5acac545d6eea0927c [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:071//===- 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:1911#include "Error.h"
Michael J. Spencercdae0a42015-07-28 22:58:2512#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:3513#include "InputSection.h"
14#include "OutputSections.h"
15#include "Target.h"
Michael J. Spencer84487f12015-07-24 21:03:0716
Michael J. Spencer1b348a62015-09-04 22:28:1017#include "llvm/ADT/STLExtras.h"
Rui Ueyamaa4a628f2016-01-13 18:55:3918#include "llvm/Config/config.h"
19
20#ifdef HAVE_CXXABI_H
21#include <cxxabi.h>
22#endif
Michael J. Spencer1b348a62015-09-04 22:28:1023
24using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0725using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5226using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0727
28using namespace lld;
29using namespace lld::elf2;
30
Rui Ueyamab5a69702016-02-01 21:00:3531template <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);
59 if (SS->NeedsCopy)
60 return Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
61 return 0;
62 }
63 case UndefinedElfKind:
64 case UndefinedKind:
65 return 0;
66 case LazyKind:
67 assert(isUsedInRegularObj() && "Lazy symbol reached writer");
68 return 0;
69 }
70 llvm_unreachable("Invalid symbol kind");
71}
72
73template <class ELFT>
74typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
75 return Out<ELFT>::Got->getVA() +
76 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
77 sizeof(typename ELFFile<ELFT>::uintX_t);
78}
79
80template <class ELFT>
81typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
82 return Out<ELFT>::GotPlt->getVA() +
83 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
84}
85
86template <class ELFT>
87typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
88 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
89 PltIndex * Target->PltEntrySize;
90}
91
Rui Ueyama512c61d2016-02-03 00:12:2492template <class ELFT>
93typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
94 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
95 return B->Sym.st_size;
96 return 0;
97}
98
Rafael Espindola78471f02015-09-01 23:12:5299static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
100 if (VA == STV_DEFAULT)
101 return VB;
102 if (VB == STV_DEFAULT)
103 return VA;
104 return std::min(VA, VB);
105}
106
Michael J. Spencer84487f12015-07-24 21:03:07107// Returns 1, 0 or -1 if this symbol should take precedence
108// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19109template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rui Ueyama6be68522015-12-16 23:49:19110 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer1b348a62015-09-04 22:28:10111 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05112 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
113 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
114 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01115
Rafael Espindola3a63f3f2015-08-28 20:19:34116 // Normalize
117 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19118 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01119
Rui Ueyama8f2c4da2015-10-21 18:13:47120 Visibility = Other->Visibility =
121 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52122
Rui Ueyama86696f32015-10-21 19:41:03123 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
124 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05125
George Rimar02ca1792016-01-25 08:44:38126 // We want to export all symbols that exist both in the executable
127 // and in DSOs, so that the symbols in the executable can interrupt
128 // symbols in the DSO at runtime.
129 if (isShared() != Other->isShared())
130 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
131 IsUsedInDynamicReloc = Other->IsUsedInDynamicReloc = true;
132
Rafael Espindola3a63f3f2015-08-28 20:19:34133 if (L != R)
134 return -1;
Rafael Espindola0bc0c022016-01-18 23:54:05135 if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L))
Rafael Espindola8e5560d2015-09-23 14:23:59136 return 1;
Rui Ueyama7da94a52015-09-09 17:40:51137 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:09138 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:30139 return -1;
Rafael Espindola11191912015-12-24 16:23:37140 auto *ThisC = cast<DefinedCommon>(this);
141 auto *OtherC = cast<DefinedCommon>(Other);
Rui Ueyama6be68522015-12-16 23:49:19142 uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
Rafael Espindola11191912015-12-24 16:23:37143 if (ThisC->Size >= OtherC->Size) {
Rui Ueyama6be68522015-12-16 23:49:19144 ThisC->MaxAlignment = Align;
Rui Ueyama6666f6a2015-09-09 17:55:09145 return 1;
Rafael Espindoladaa92a62015-08-31 01:16:19146 }
Rui Ueyama6be68522015-12-16 23:49:19147 OtherC->MaxAlignment = Align;
Rui Ueyama7da94a52015-09-09 17:40:51148 return -1;
Rafael Espindola30e17972015-08-30 23:17:30149 }
Rui Ueyama7da94a52015-09-09 17:40:51150 if (Other->isCommon())
151 return 1;
152 return 0;
Michael J. Spencer84487f12015-07-24 21:03:07153}
Rafael Espindoladaa92a62015-08-31 01:16:19154
Rafael Espindola4d4b06a2015-12-24 00:47:42155Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
George Rimar5c36e592016-02-02 09:28:53156 bool IsTls, bool IsFunction)
157 : SymbolBody(K, Name, IsWeak, Visibility, IsTls, IsFunction) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42158
Rafael Espindola5d7593b2015-12-22 23:00:50159Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
160 uint8_t Visibility, bool IsTls)
George Rimar5c36e592016-02-02 09:28:53161 : SymbolBody(K, N, IsWeak, Visibility, IsTls, /*IsFunction*/ false),
162 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50163
164Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
165 bool CanKeepUndefined)
166 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility,
167 /*IsTls*/ false) {
168 this->CanKeepUndefined = CanKeepUndefined;
169}
170
171template <typename ELFT>
172UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
173 : Undefined(SymbolBody::UndefinedElfKind, N,
174 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
175 Sym.getType() == llvm::ELF::STT_TLS),
176 Sym(Sym) {}
177
Rafael Espindola4d4b06a2015-12-24 00:47:42178template <typename ELFT>
179DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
180 OutputSectionBase<ELFT> &Section)
George Rimar5c36e592016-02-02 09:28:53181 : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT,
182 /*IsTls*/ false, /*IsFunction*/ false),
Rafael Espindola4d4b06a2015-12-24 00:47:42183 Value(Value), Section(Section) {}
184
Rafael Espindola11191912015-12-24 16:23:37185DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
186 bool IsWeak, uint8_t Visibility)
George Rimar5c36e592016-02-02 09:28:53187 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility,
188 /*IsTls*/ false, /*IsFunction*/ false) {
Rafael Espindola11191912015-12-24 16:23:37189 MaxAlignment = Alignment;
190 this->Size = Size;
191}
192
Michael J. Spencer1b348a62015-09-04 22:28:10193std::unique_ptr<InputFile> Lazy::getMember() {
194 MemoryBufferRef MBRef = File->getMember(&Sym);
195
196 // getMember returns an empty buffer if the member was already
197 // read from the library.
198 if (MBRef.getBuffer().empty())
199 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41200 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10201}
202
Rui Ueyamaaca48ff2015-10-08 00:44:28203template <class ELFT> static void doInitSymbols() {
Rui Ueyamaa246e0942015-12-25 06:12:18204 ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
Rafael Espindola65e80b92016-01-19 21:19:52205 ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
206 ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
Rui Ueyamaaca48ff2015-10-08 00:44:28207}
208
Rui Ueyama83cd6e02016-01-06 20:11:55209void elf2::initSymbols() {
Rui Ueyamaaca48ff2015-10-08 00:44:28210 doInitSymbols<ELF32LE>();
211 doInitSymbols<ELF32BE>();
212 doInitSymbols<ELF64LE>();
213 doInitSymbols<ELF64BE>();
Rui Ueyama9ea49c72015-10-07 23:46:11214}
215
Rui Ueyamaa4a628f2016-01-13 18:55:39216// Returns the demangled C++ symbol name for Name.
217std::string elf2::demangle(StringRef Name) {
218#if !defined(HAVE_CXXABI_H)
219 return Name;
220#else
221 if (!Config->Demangle)
222 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13223
Rui Ueyamadf154512016-01-13 22:09:09224 // __cxa_demangle can be used to demangle strings other than symbol
225 // names which do not necessarily start with "_Z". Name can be
226 // either a C or C++ symbol. Don't call __cxa_demangle if the name
227 // does not look like a C++ symbol name to avoid getting unexpected
228 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13229 if (!Name.startswith("_Z"))
230 return Name;
231
Rui Ueyamaa4a628f2016-01-13 18:55:39232 char *Buf =
233 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
234 if (!Buf)
235 return Name;
236 std::string S(Buf);
237 free(Buf);
238 return S;
239#endif
240}
241
Rui Ueyamab5a69702016-02-01 21:00:35242template uint32_t SymbolBody::template getVA<ELF32LE>() const;
243template uint32_t SymbolBody::template getVA<ELF32BE>() const;
244template uint64_t SymbolBody::template getVA<ELF64LE>() const;
245template uint64_t SymbolBody::template getVA<ELF64BE>() const;
246
247template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
248template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
249template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
250template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
251
252template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
253template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
254template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
255template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
256
257template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
258template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
259template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
260template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
261
Rui Ueyama512c61d2016-02-03 00:12:24262template uint32_t SymbolBody::template getSize<ELF32LE>() const;
263template uint32_t SymbolBody::template getSize<ELF32BE>() const;
264template uint64_t SymbolBody::template getSize<ELF64LE>() const;
265template uint64_t SymbolBody::template getSize<ELF64BE>() const;
266
Rafael Espindoladaa92a62015-08-31 01:16:19267template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
268template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
269template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
270template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50271
Rui Ueyama83cd6e02016-01-06 20:11:55272template class elf2::UndefinedElf<ELF32LE>;
273template class elf2::UndefinedElf<ELF32BE>;
274template class elf2::UndefinedElf<ELF64LE>;
275template class elf2::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42276
Rui Ueyama83cd6e02016-01-06 20:11:55277template class elf2::DefinedSynthetic<ELF32LE>;
278template class elf2::DefinedSynthetic<ELF32BE>;
279template class elf2::DefinedSynthetic<ELF64LE>;
280template class elf2::DefinedSynthetic<ELF64BE>;