blob: a974778179889a921f5b48d917e16a28a9039683 [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;
Rui Ueyama0b289522016-02-25 18:43:5145 assert(SC->Live);
Rui Ueyamab5a69702016-02-01 21:00:3546
47 // Symbol offsets for AMDGPU need to be the offset in bytes of the symbol
48 // from the beginning of the section.
49 if (Config->EMachine == EM_AMDGPU)
50 return SC->getOffset(D->Sym);
51 if (D->Sym.getType() == STT_TLS)
52 return SC->OutSec->getVA() + SC->getOffset(D->Sym) -
53 Out<ELFT>::TlsPhdr->p_vaddr;
54 return SC->OutSec->getVA() + SC->getOffset(D->Sym);
55 }
56 case DefinedCommonKind:
57 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(this)->OffsetInBss;
58 case SharedKind: {
59 auto *SS = cast<SharedSymbol<ELFT>>(this);
Rafael Espindolaa0a65f92016-02-09 15:11:0160 if (!SS->NeedsCopyOrPltAddr)
61 return 0;
62 if (SS->IsFunc)
63 return getPltVA<ELFT>();
64 else
Rui Ueyamab5a69702016-02-01 21:00:3565 return Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:3566 }
67 case UndefinedElfKind:
68 case UndefinedKind:
69 return 0;
70 case LazyKind:
71 assert(isUsedInRegularObj() && "Lazy symbol reached writer");
72 return 0;
Rafael Espindola9f77ef02016-02-12 20:54:5773 case DefinedBitcodeKind:
74 llvm_unreachable("Should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:3575 }
76 llvm_unreachable("Invalid symbol kind");
77}
78
79template <class ELFT>
80typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
81 return Out<ELFT>::Got->getVA() +
82 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
83 sizeof(typename ELFFile<ELFT>::uintX_t);
84}
85
86template <class ELFT>
87typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
88 return Out<ELFT>::GotPlt->getVA() +
89 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
90}
91
92template <class ELFT>
93typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
94 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
95 PltIndex * Target->PltEntrySize;
96}
97
Rui Ueyama512c61d2016-02-03 00:12:2498template <class ELFT>
99typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
100 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
101 return B->Sym.st_size;
102 return 0;
103}
104
Rafael Espindola78471f02015-09-01 23:12:52105static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
106 if (VA == STV_DEFAULT)
107 return VB;
108 if (VB == STV_DEFAULT)
109 return VA;
110 return std::min(VA, VB);
111}
112
Michael J. Spencer84487f12015-07-24 21:03:07113// Returns 1, 0 or -1 if this symbol should take precedence
114// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19115template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rui Ueyama6be68522015-12-16 23:49:19116 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer1b348a62015-09-04 22:28:10117 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05118 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
119 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
120 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01121
Rafael Espindola3a63f3f2015-08-28 20:19:34122 // Normalize
123 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19124 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01125
Rui Ueyama8f2c4da2015-10-21 18:13:47126 Visibility = Other->Visibility =
127 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52128
Rui Ueyama86696f32015-10-21 19:41:03129 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
130 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05131
George Rimar02ca1792016-01-25 08:44:38132 // We want to export all symbols that exist both in the executable
133 // and in DSOs, so that the symbols in the executable can interrupt
134 // symbols in the DSO at runtime.
135 if (isShared() != Other->isShared())
136 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15137 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38138
Rafael Espindola3a63f3f2015-08-28 20:19:34139 if (L != R)
140 return -1;
Rafael Espindola0bc0c022016-01-18 23:54:05141 if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L))
Rafael Espindola8e5560d2015-09-23 14:23:59142 return 1;
Rui Ueyama7da94a52015-09-09 17:40:51143 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:09144 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:30145 return -1;
Rafael Espindola11191912015-12-24 16:23:37146 auto *ThisC = cast<DefinedCommon>(this);
147 auto *OtherC = cast<DefinedCommon>(Other);
Rui Ueyama6be68522015-12-16 23:49:19148 uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
Rafael Espindola11191912015-12-24 16:23:37149 if (ThisC->Size >= OtherC->Size) {
Rui Ueyama6be68522015-12-16 23:49:19150 ThisC->MaxAlignment = Align;
Rui Ueyama6666f6a2015-09-09 17:55:09151 return 1;
Rafael Espindoladaa92a62015-08-31 01:16:19152 }
Rui Ueyama6be68522015-12-16 23:49:19153 OtherC->MaxAlignment = Align;
Rui Ueyama7da94a52015-09-09 17:40:51154 return -1;
Rafael Espindola30e17972015-08-30 23:17:30155 }
Rui Ueyama7da94a52015-09-09 17:40:51156 if (Other->isCommon())
157 return 1;
158 return 0;
Michael J. Spencer84487f12015-07-24 21:03:07159}
Rafael Espindoladaa92a62015-08-31 01:16:19160
Rafael Espindola4d4b06a2015-12-24 00:47:42161Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
George Rimar5c36e592016-02-02 09:28:53162 bool IsTls, bool IsFunction)
163 : SymbolBody(K, Name, IsWeak, Visibility, IsTls, IsFunction) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42164
Rafael Espindola148445ef2016-02-25 16:25:41165DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak)
166 : Defined(DefinedBitcodeKind, Name, IsWeak, STV_DEFAULT, false, false) {}
Rafael Espindola9f77ef02016-02-12 20:54:57167
168bool DefinedBitcode::classof(const SymbolBody *S) {
169 return S->kind() == DefinedBitcodeKind;
170}
171
Rafael Espindola5d7593b2015-12-22 23:00:50172Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
173 uint8_t Visibility, bool IsTls)
George Rimar5c36e592016-02-02 09:28:53174 : SymbolBody(K, N, IsWeak, Visibility, IsTls, /*IsFunction*/ false),
175 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50176
177Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
178 bool CanKeepUndefined)
179 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility,
180 /*IsTls*/ false) {
181 this->CanKeepUndefined = CanKeepUndefined;
182}
183
184template <typename ELFT>
185UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
186 : Undefined(SymbolBody::UndefinedElfKind, N,
187 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
188 Sym.getType() == llvm::ELF::STT_TLS),
189 Sym(Sym) {}
190
Rafael Espindola4d4b06a2015-12-24 00:47:42191template <typename ELFT>
192DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
193 OutputSectionBase<ELFT> &Section)
George Rimar5c36e592016-02-02 09:28:53194 : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT,
195 /*IsTls*/ false, /*IsFunction*/ false),
Rafael Espindola4d4b06a2015-12-24 00:47:42196 Value(Value), Section(Section) {}
197
Rafael Espindola11191912015-12-24 16:23:37198DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
199 bool IsWeak, uint8_t Visibility)
George Rimar5c36e592016-02-02 09:28:53200 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility,
201 /*IsTls*/ false, /*IsFunction*/ false) {
Rafael Espindola11191912015-12-24 16:23:37202 MaxAlignment = Alignment;
203 this->Size = Size;
204}
205
Michael J. Spencer1b348a62015-09-04 22:28:10206std::unique_ptr<InputFile> Lazy::getMember() {
207 MemoryBufferRef MBRef = File->getMember(&Sym);
208
209 // getMember returns an empty buffer if the member was already
210 // read from the library.
211 if (MBRef.getBuffer().empty())
212 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41213 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10214}
215
Rui Ueyamaaca48ff2015-10-08 00:44:28216template <class ELFT> static void doInitSymbols() {
Rui Ueyamaa246e0942015-12-25 06:12:18217 ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
Rafael Espindola65e80b92016-01-19 21:19:52218 ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
219 ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
Rui Ueyamaaca48ff2015-10-08 00:44:28220}
221
Rui Ueyama83cd6e02016-01-06 20:11:55222void elf2::initSymbols() {
Rui Ueyamaaca48ff2015-10-08 00:44:28223 doInitSymbols<ELF32LE>();
224 doInitSymbols<ELF32BE>();
225 doInitSymbols<ELF64LE>();
226 doInitSymbols<ELF64BE>();
Rui Ueyama9ea49c72015-10-07 23:46:11227}
228
Rui Ueyamaa4a628f2016-01-13 18:55:39229// Returns the demangled C++ symbol name for Name.
230std::string elf2::demangle(StringRef Name) {
231#if !defined(HAVE_CXXABI_H)
232 return Name;
233#else
234 if (!Config->Demangle)
235 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13236
Rui Ueyamadf154512016-01-13 22:09:09237 // __cxa_demangle can be used to demangle strings other than symbol
238 // names which do not necessarily start with "_Z". Name can be
239 // either a C or C++ symbol. Don't call __cxa_demangle if the name
240 // does not look like a C++ symbol name to avoid getting unexpected
241 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13242 if (!Name.startswith("_Z"))
243 return Name;
244
Rui Ueyamaa4a628f2016-01-13 18:55:39245 char *Buf =
246 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
247 if (!Buf)
248 return Name;
249 std::string S(Buf);
250 free(Buf);
251 return S;
252#endif
253}
254
Rui Ueyamab5a69702016-02-01 21:00:35255template uint32_t SymbolBody::template getVA<ELF32LE>() const;
256template uint32_t SymbolBody::template getVA<ELF32BE>() const;
257template uint64_t SymbolBody::template getVA<ELF64LE>() const;
258template uint64_t SymbolBody::template getVA<ELF64BE>() const;
259
260template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
261template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
262template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
263template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
264
265template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
266template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
267template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
268template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
269
270template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
271template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
272template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
273template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
274
Rui Ueyama512c61d2016-02-03 00:12:24275template uint32_t SymbolBody::template getSize<ELF32LE>() const;
276template uint32_t SymbolBody::template getSize<ELF32BE>() const;
277template uint64_t SymbolBody::template getSize<ELF64LE>() const;
278template uint64_t SymbolBody::template getSize<ELF64BE>() const;
279
Rafael Espindoladaa92a62015-08-31 01:16:19280template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
281template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
282template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
283template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50284
Rui Ueyama83cd6e02016-01-06 20:11:55285template class elf2::UndefinedElf<ELF32LE>;
286template class elf2::UndefinedElf<ELF32BE>;
287template class elf2::UndefinedElf<ELF64LE>;
288template class elf2::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42289
Rui Ueyama83cd6e02016-01-06 20:11:55290template class elf2::DefinedSynthetic<ELF32LE>;
291template class elf2::DefinedSynthetic<ELF32BE>;
292template class elf2::DefinedSynthetic<ELF64LE>;
293template class elf2::DefinedSynthetic<ELF64BE>;