blob: f6786c27f585bddd986e77759a5637a6a73a60f6 [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>
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:51 +000045 assert(SC->Live);
Rui Ueyamab5a69702016-02-01 21:00:35 +000046
Rui Ueyamab5a69702016-02-01 21:00:35 +000047 if (D->Sym.getType() == STT_TLS)
48 return SC->OutSec->getVA() + SC->getOffset(D->Sym) -
49 Out<ELFT>::TlsPhdr->p_vaddr;
50 return SC->OutSec->getVA() + SC->getOffset(D->Sym);
51 }
52 case DefinedCommonKind:
53 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(this)->OffsetInBss;
54 case SharedKind: {
55 auto *SS = cast<SharedSymbol<ELFT>>(this);
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000056 if (!SS->NeedsCopyOrPltAddr)
57 return 0;
George Rimar2f0fab52016-03-06 06:26:18 +000058 if (SS->IsFunc)
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000059 return getPltVA<ELFT>();
60 else
Rui Ueyamab5a69702016-02-01 21:00:35 +000061 return Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:35 +000062 }
63 case UndefinedElfKind:
64 case UndefinedKind:
65 return 0;
66 case LazyKind:
67 assert(isUsedInRegularObj() && "Lazy symbol reached writer");
68 return 0;
Rafael Espindola9f77ef02016-02-12 20:54:57 +000069 case DefinedBitcodeKind:
70 llvm_unreachable("Should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:35 +000071 }
72 llvm_unreachable("Invalid symbol kind");
73}
74
75template <class ELFT>
76typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
77 return Out<ELFT>::Got->getVA() +
78 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
79 sizeof(typename ELFFile<ELFT>::uintX_t);
80}
81
82template <class ELFT>
83typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
84 return Out<ELFT>::GotPlt->getVA() +
85 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
86}
87
88template <class ELFT>
89typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
90 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
91 PltIndex * Target->PltEntrySize;
92}
93
Rui Ueyama512c61d2016-02-03 00:12:24 +000094template <class ELFT>
95typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
96 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
97 return B->Sym.st_size;
98 return 0;
99}
100
Rafael Espindola78471f02015-09-01 23:12:52 +0000101static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
102 if (VA == STV_DEFAULT)
103 return VB;
104 if (VB == STV_DEFAULT)
105 return VA;
106 return std::min(VA, VB);
107}
108
Michael J. Spencer84487f12015-07-24 21:03:07 +0000109// Returns 1, 0 or -1 if this symbol should take precedence
110// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000111template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rui Ueyama6be68522015-12-16 23:49:19 +0000112 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000113 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05 +0000114 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
115 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
116 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000117
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000118 // Normalize
119 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000120 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000121
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000122 Visibility = Other->Visibility =
123 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52 +0000124
Rui Ueyama86696f32015-10-21 19:41:03 +0000125 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
126 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05 +0000127
George Rimar02ca1792016-01-25 08:44:38 +0000128 // We want to export all symbols that exist both in the executable
129 // and in DSOs, so that the symbols in the executable can interrupt
130 // symbols in the DSO at runtime.
131 if (isShared() != Other->isShared())
132 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15 +0000133 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38 +0000134
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000135 if (L != R)
136 return -1;
Rafael Espindola0bc0c022016-01-18 23:54:05 +0000137 if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L))
Rafael Espindola8e5560d2015-09-23 14:23:59 +0000138 return 1;
Rui Ueyama7da94a52015-09-09 17:40:51 +0000139 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:09 +0000140 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:30 +0000141 return -1;
Rafael Espindola11191912015-12-24 16:23:37 +0000142 auto *ThisC = cast<DefinedCommon>(this);
143 auto *OtherC = cast<DefinedCommon>(Other);
Rui Ueyama6be68522015-12-16 23:49:19 +0000144 uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
Rafael Espindola11191912015-12-24 16:23:37 +0000145 if (ThisC->Size >= OtherC->Size) {
Rui Ueyama6be68522015-12-16 23:49:19 +0000146 ThisC->MaxAlignment = Align;
Rui Ueyama6666f6a2015-09-09 17:55:09 +0000147 return 1;
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000148 }
Rui Ueyama6be68522015-12-16 23:49:19 +0000149 OtherC->MaxAlignment = Align;
Rui Ueyama7da94a52015-09-09 17:40:51 +0000150 return -1;
Rafael Espindola30e17972015-08-30 23:17:30 +0000151 }
Rui Ueyama7da94a52015-09-09 17:40:51 +0000152 if (Other->isCommon())
153 return 1;
154 return 0;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000155}
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000156
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000157Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000158 uint8_t Type)
159 : SymbolBody(K, Name, IsWeak, Visibility, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000160
Rafael Espindola4f29c1a2016-03-07 17:14:36 +0000161DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility)
162 : Defined(DefinedBitcodeKind, Name, IsWeak, Visibility, 0 /* Type */) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000163
164bool DefinedBitcode::classof(const SymbolBody *S) {
165 return S->kind() == DefinedBitcodeKind;
166}
167
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000168Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
Davide Italiano255730c2016-03-04 01:55:28 +0000169 uint8_t Visibility, uint8_t Type)
170 : SymbolBody(K, N, IsWeak, Visibility, Type),
George Rimar5c36e592016-02-02 09:28:53 +0000171 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000172
173Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
174 bool CanKeepUndefined)
Davide Italiano255730c2016-03-04 01:55:28 +0000175 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, 0 /* Type */) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000176 this->CanKeepUndefined = CanKeepUndefined;
177}
178
179template <typename ELFT>
180UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
181 : Undefined(SymbolBody::UndefinedElfKind, N,
182 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
Davide Italiano255730c2016-03-04 01:55:28 +0000183 Sym.getType()),
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000184 Sym(Sym) {}
185
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000186template <typename ELFT>
187DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
George Rimaraa4dc202016-03-01 16:23:13 +0000188 OutputSectionBase<ELFT> &Section,
189 uint8_t Visibility)
190 : Defined(SymbolBody::DefinedSyntheticKind, N, false, Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000191 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000192 Value(Value), Section(Section) {}
193
Rafael Espindola11191912015-12-24 16:23:37 +0000194DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
195 bool IsWeak, uint8_t Visibility)
George Rimar5c36e592016-02-02 09:28:53 +0000196 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000197 0 /* Type */) {
Rafael Espindola11191912015-12-24 16:23:37 +0000198 MaxAlignment = Alignment;
199 this->Size = Size;
200}
201
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000202std::unique_ptr<InputFile> Lazy::getMember() {
203 MemoryBufferRef MBRef = File->getMember(&Sym);
204
205 // getMember returns an empty buffer if the member was already
206 // read from the library.
207 if (MBRef.getBuffer().empty())
208 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000209 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000210}
211
George Rimar5a3dcf42016-03-10 17:38:49 +0000212template <class ELFT> void elf::initSymbols() {
George Rimar9e859392016-02-26 14:36:36 +0000213 ElfSym<ELFT>::Etext.setBinding(STB_GLOBAL);
214 ElfSym<ELFT>::Edata.setBinding(STB_GLOBAL);
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000215 ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
Rafael Espindola65e80b92016-01-19 21:19:52 +0000216 ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
217 ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000218}
219
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000220// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000221std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000222#if !defined(HAVE_CXXABI_H)
223 return Name;
224#else
225 if (!Config->Demangle)
226 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000227
Rui Ueyamadf154512016-01-13 22:09:09 +0000228 // __cxa_demangle can be used to demangle strings other than symbol
229 // names which do not necessarily start with "_Z". Name can be
230 // either a C or C++ symbol. Don't call __cxa_demangle if the name
231 // does not look like a C++ symbol name to avoid getting unexpected
232 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000233 if (!Name.startswith("_Z"))
234 return Name;
235
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000236 char *Buf =
237 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
238 if (!Buf)
239 return Name;
240 std::string S(Buf);
241 free(Buf);
242 return S;
243#endif
244}
245
Rui Ueyamab5a69702016-02-01 21:00:35 +0000246template uint32_t SymbolBody::template getVA<ELF32LE>() const;
247template uint32_t SymbolBody::template getVA<ELF32BE>() const;
248template uint64_t SymbolBody::template getVA<ELF64LE>() const;
249template uint64_t SymbolBody::template getVA<ELF64BE>() const;
250
251template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
252template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
253template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
254template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
255
256template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
257template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
258template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
259template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
260
261template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
262template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
263template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
264template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
265
Rui Ueyama512c61d2016-02-03 00:12:24 +0000266template uint32_t SymbolBody::template getSize<ELF32LE>() const;
267template uint32_t SymbolBody::template getSize<ELF32BE>() const;
268template uint64_t SymbolBody::template getSize<ELF64LE>() const;
269template uint64_t SymbolBody::template getSize<ELF64BE>() const;
270
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000271template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
272template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
273template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
274template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000275
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000276template class elf::UndefinedElf<ELF32LE>;
277template class elf::UndefinedElf<ELF32BE>;
278template class elf::UndefinedElf<ELF64LE>;
279template class elf::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000280
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000281template class elf::DefinedSynthetic<ELF32LE>;
282template class elf::DefinedSynthetic<ELF32BE>;
283template class elf::DefinedSynthetic<ELF64LE>;
284template class elf::DefinedSynthetic<ELF64BE>;
George Rimar5a3dcf42016-03-10 17:38:49 +0000285
286template void elf::initSymbols<ELF32LE>();
287template void elf::initSymbols<ELF32BE>();
288template void elf::initSymbols<ELF64LE>();
289template void elf::initSymbols<ELF64BE>();