blob: 8a7a18ca97911d583a40aa090f647e92a6f9ba67 [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>
Rafael Espindola87d9f102016-03-11 12:19:05 +000032static typename ELFFile<ELFT>::uintX_t
33getSymVA(const SymbolBody &Body, typename ELFFile<ELFT>::uintX_t &Addend) {
34 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
35 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
36
37 switch (Body.kind()) {
38 case SymbolBody::DefinedSyntheticKind: {
39 auto &D = cast<DefinedSynthetic<ELFT>>(Body);
40 return D.Section.getVA() + D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000041 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000042 case SymbolBody::DefinedRegularKind: {
43 auto &D = cast<DefinedRegular<ELFT>>(Body);
44 InputSectionBase<ELFT> *SC = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:35 +000045
46 // This is an absolute symbol.
47 if (!SC)
Rafael Espindola87d9f102016-03-11 12:19:05 +000048 return D.Sym.st_value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000049
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000050 const Elf_Sym &Sym = D.Sym;
51 uintX_t Offset = Sym.st_value;
52 if (Sym.getType() == STT_SECTION) {
53 Offset += Addend;
54 Addend = 0;
55 }
56 uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
57 if (Sym.getType() == STT_TLS)
58 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
59 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35 +000060 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000061 case SymbolBody::DefinedCommonKind:
62 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
63 case SymbolBody::SharedKind: {
64 auto &SS = cast<SharedSymbol<ELFT>>(Body);
65 if (!SS.NeedsCopyOrPltAddr)
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000066 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000067 if (SS.IsFunc)
68 return Body.getPltVA<ELFT>();
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000069 else
Rafael Espindola87d9f102016-03-11 12:19:05 +000070 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:35 +000071 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000072 case SymbolBody::UndefinedElfKind:
73 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35 +000074 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000075 case SymbolBody::LazyKind:
George Rimar777f9632016-03-12 08:31:34 +000076 assert(Body.isUsedInRegularObj() && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35 +000077 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000078 case SymbolBody::DefinedBitcodeKind:
George Rimar777f9632016-03-12 08:31:34 +000079 llvm_unreachable("should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:35 +000080 }
George Rimar777f9632016-03-12 08:31:34 +000081 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35 +000082}
83
Rui Ueyama7ede5432016-03-13 04:40:14 +000084template <class ELFT> bool SymbolBody::isGnuIfunc() const {
85 if (auto *D = dyn_cast<DefinedElf<ELFT>>(this))
86 return D->Sym.getType() == STT_GNU_IFUNC;
87 return false;
88}
89
Rui Ueyamab5a69702016-02-01 21:00:35 +000090template <class ELFT>
Rafael Espindola87d9f102016-03-11 12:19:05 +000091typename ELFFile<ELFT>::uintX_t
92SymbolBody::getVA(typename ELFFile<ELFT>::uintX_t Addend) const {
93 return getSymVA<ELFT>(*this, Addend) + Addend;
94}
95
96template <class ELFT>
Rui Ueyamab5a69702016-02-01 21:00:35 +000097typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
98 return Out<ELFT>::Got->getVA() +
99 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
100 sizeof(typename ELFFile<ELFT>::uintX_t);
101}
102
103template <class ELFT>
104typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
105 return Out<ELFT>::GotPlt->getVA() +
106 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
107}
108
109template <class ELFT>
110typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
111 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
112 PltIndex * Target->PltEntrySize;
113}
114
Rui Ueyama512c61d2016-02-03 00:12:24 +0000115template <class ELFT>
116typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
117 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
118 return B->Sym.st_size;
119 return 0;
120}
121
Rafael Espindola78471f02015-09-01 23:12:52 +0000122static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
123 if (VA == STV_DEFAULT)
124 return VB;
125 if (VB == STV_DEFAULT)
126 return VA;
127 return std::min(VA, VB);
128}
129
George Rimar3498c7f2016-03-10 18:49:24 +0000130static int compareCommons(DefinedCommon *A, DefinedCommon *B) {
Rui Ueyama17d69832016-03-10 18:58:53 +0000131 A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment);
George Rimar3498c7f2016-03-10 18:49:24 +0000132 if (A->Size < B->Size)
133 return -1;
134 return 1;
135}
136
Michael J. Spencer84487f12015-07-24 21:03:07 +0000137// Returns 1, 0 or -1 if this symbol should take precedence
138// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000139template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000140 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05 +0000141 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
142 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
143 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000144
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000145 // Normalize
146 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000147 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000148
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000149 Visibility = Other->Visibility =
150 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52 +0000151
Rui Ueyama86696f32015-10-21 19:41:03 +0000152 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
153 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05 +0000154
George Rimar02ca1792016-01-25 08:44:38 +0000155 // We want to export all symbols that exist both in the executable
156 // and in DSOs, so that the symbols in the executable can interrupt
157 // symbols in the DSO at runtime.
158 if (isShared() != Other->isShared())
159 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15 +0000160 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38 +0000161
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000162 if (L != R)
163 return -1;
George Rimar3498c7f2016-03-10 18:49:24 +0000164 if (!isDefined() || isShared() || isWeak())
Rafael Espindola8e5560d2015-09-23 14:23:59 +0000165 return 1;
George Rimar3498c7f2016-03-10 18:49:24 +0000166 if (!isCommon() && !Other->isCommon())
167 return 0;
168 if (isCommon() && Other->isCommon())
169 return compareCommons(cast<DefinedCommon>(this),
170 cast<DefinedCommon>(Other));
171 return isCommon() ? -1 : 1;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000172}
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000173
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000174Defined::Defined(Kind K, StringRef Name, bool IsWeak, bool IsLocal,
175 uint8_t Visibility, uint8_t Type)
176 : SymbolBody(K, Name, IsWeak, IsLocal, Visibility, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000177
Rafael Espindola4f29c1a2016-03-07 17:14:36 +0000178DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000179 : Defined(DefinedBitcodeKind, Name, IsWeak, false, Visibility,
180 0 /* Type */) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000181
182bool DefinedBitcode::classof(const SymbolBody *S) {
183 return S->kind() == DefinedBitcodeKind;
184}
185
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000186Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
Davide Italiano255730c2016-03-04 01:55:28 +0000187 uint8_t Visibility, uint8_t Type)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000188 : SymbolBody(K, N, IsWeak, false, Visibility, Type),
George Rimar5c36e592016-02-02 09:28:53 +0000189 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000190
191Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
192 bool CanKeepUndefined)
Davide Italiano255730c2016-03-04 01:55:28 +0000193 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, 0 /* Type */) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000194 this->CanKeepUndefined = CanKeepUndefined;
195}
196
197template <typename ELFT>
198UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
199 : Undefined(SymbolBody::UndefinedElfKind, N,
200 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
Davide Italiano255730c2016-03-04 01:55:28 +0000201 Sym.getType()),
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000202 Sym(Sym) {}
203
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000204template <typename ELFT>
205DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
George Rimaraa4dc202016-03-01 16:23:13 +0000206 OutputSectionBase<ELFT> &Section,
207 uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000208 : Defined(SymbolBody::DefinedSyntheticKind, N, false, false, Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000209 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000210 Value(Value), Section(Section) {}
211
Rafael Espindola11191912015-12-24 16:23:37 +0000212DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
213 bool IsWeak, uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000214 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, false, Visibility,
Rui Ueyama17d69832016-03-10 18:58:53 +0000215 0 /* Type */),
216 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000217
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000218std::unique_ptr<InputFile> Lazy::getMember() {
219 MemoryBufferRef MBRef = File->getMember(&Sym);
220
221 // getMember returns an empty buffer if the member was already
222 // read from the library.
223 if (MBRef.getBuffer().empty())
224 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000225 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000226}
227
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000228// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000229std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000230#if !defined(HAVE_CXXABI_H)
231 return Name;
232#else
233 if (!Config->Demangle)
234 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000235
Rui Ueyamadf154512016-01-13 22:09:09 +0000236 // __cxa_demangle can be used to demangle strings other than symbol
237 // names which do not necessarily start with "_Z". Name can be
238 // either a C or C++ symbol. Don't call __cxa_demangle if the name
239 // does not look like a C++ symbol name to avoid getting unexpected
240 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000241 if (!Name.startswith("_Z"))
242 return Name;
243
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000244 char *Buf =
245 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
246 if (!Buf)
247 return Name;
248 std::string S(Buf);
249 free(Buf);
250 return S;
251#endif
252}
253
Rui Ueyama7ede5432016-03-13 04:40:14 +0000254template bool SymbolBody::template isGnuIfunc<ELF32LE>() const;
255template bool SymbolBody::template isGnuIfunc<ELF32BE>() const;
256template bool SymbolBody::template isGnuIfunc<ELF64LE>() const;
257template bool SymbolBody::template isGnuIfunc<ELF64BE>() const;
258
Rafael Espindola87d9f102016-03-11 12:19:05 +0000259template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
260template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
261template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
262template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000263
264template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
265template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
266template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
267template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
268
269template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
270template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
271template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
272template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
273
274template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
275template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
276template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
277template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
278
Rui Ueyama512c61d2016-02-03 00:12:24 +0000279template uint32_t SymbolBody::template getSize<ELF32LE>() const;
280template uint32_t SymbolBody::template getSize<ELF32BE>() const;
281template uint64_t SymbolBody::template getSize<ELF64LE>() const;
282template uint64_t SymbolBody::template getSize<ELF64BE>() const;
283
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000284template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
285template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
286template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
287template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000288
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000289template class elf::UndefinedElf<ELF32LE>;
290template class elf::UndefinedElf<ELF32BE>;
291template class elf::UndefinedElf<ELF64LE>;
292template class elf::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000293
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000294template class elf::DefinedSynthetic<ELF32LE>;
295template class elf::DefinedSynthetic<ELF32BE>;
296template class elf::DefinedSynthetic<ELF64LE>;
297template class elf::DefinedSynthetic<ELF64BE>;