blob: e32473e3bd8a12a63de3557f5601d55b03918c73 [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>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000032static typename ELFT::uint getSymVA(const SymbolBody &Body,
33 typename ELFT::uint &Addend) {
34 typedef typename ELFT::Sym Elf_Sym;
35 typedef typename ELFT::uint uintX_t;
Rafael Espindola87d9f102016-03-11 12:19:05 +000036
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>();
Rui Ueyama2df72892016-03-13 20:54:38 +000069 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:35 +000070 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000071 case SymbolBody::UndefinedElfKind:
72 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35 +000073 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000074 case SymbolBody::LazyKind:
George Rimar777f9632016-03-12 08:31:34 +000075 assert(Body.isUsedInRegularObj() && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35 +000076 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000077 case SymbolBody::DefinedBitcodeKind:
George Rimar777f9632016-03-12 08:31:34 +000078 llvm_unreachable("should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:35 +000079 }
George Rimar777f9632016-03-12 08:31:34 +000080 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35 +000081}
82
Rui Ueyamac4466602016-03-13 19:48:18 +000083// Returns true if a symbol can be replaced at load-time by a symbol
84// with the same name defined in other ELF executable or DSO.
85bool SymbolBody::isPreemptible() const {
86 if (isLocal())
87 return false;
88
89 if (isShared())
90 return true;
91
92 if (isUndefined()) {
93 if (!isWeak())
94 return true;
95
96 // Ideally the static linker should see a definition for every symbol, but
97 // shared object are normally allowed to have undefined references that the
98 // static linker never sees a definition for.
99 if (Config->Shared)
100 return true;
101
102 // Otherwise, just resolve to 0.
103 return false;
104 }
105
106 if (!Config->Shared)
107 return false;
108 if (getVisibility() != STV_DEFAULT)
109 return false;
110 if (Config->Bsymbolic || (Config->BsymbolicFunctions && IsFunc))
111 return false;
112 return true;
113}
114
Rui Ueyama7ede5432016-03-13 04:40:14 +0000115template <class ELFT> bool SymbolBody::isGnuIfunc() const {
116 if (auto *D = dyn_cast<DefinedElf<ELFT>>(this))
117 return D->Sym.getType() == STT_GNU_IFUNC;
118 return false;
119}
120
Rui Ueyamab5a69702016-02-01 21:00:35 +0000121template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000122typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola87d9f102016-03-11 12:19:05 +0000123 return getSymVA<ELFT>(*this, Addend) + Addend;
124}
125
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000126template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000127 return Out<ELFT>::Got->getVA() +
128 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000129 sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000130}
131
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000132template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
133 return Out<ELFT>::GotPlt->getVA() + GotPltIndex * sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000134}
135
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000136template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000137 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
138 PltIndex * Target->PltEntrySize;
139}
140
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000141template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rui Ueyama512c61d2016-02-03 00:12:24 +0000142 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
143 return B->Sym.st_size;
144 return 0;
145}
146
Rafael Espindola78471f02015-09-01 23:12:52 +0000147static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
148 if (VA == STV_DEFAULT)
149 return VB;
150 if (VB == STV_DEFAULT)
151 return VA;
152 return std::min(VA, VB);
153}
154
George Rimar3498c7f2016-03-10 18:49:24 +0000155static int compareCommons(DefinedCommon *A, DefinedCommon *B) {
George Rimar34358002016-03-14 09:19:30 +0000156 if (Config->WarnCommon)
157 warning("multiple common of " + A->getName());
Rui Ueyama17d69832016-03-10 18:58:53 +0000158 A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment);
George Rimar3498c7f2016-03-10 18:49:24 +0000159 if (A->Size < B->Size)
160 return -1;
161 return 1;
162}
163
Michael J. Spencer84487f12015-07-24 21:03:07 +0000164// Returns 1, 0 or -1 if this symbol should take precedence
165// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000166template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000167 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05 +0000168 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
169 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
170 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000171
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000172 // Normalize
173 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000174 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000175
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000176 Visibility = Other->Visibility =
177 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52 +0000178
Rui Ueyama86696f32015-10-21 19:41:03 +0000179 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
180 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05 +0000181
George Rimar02ca1792016-01-25 08:44:38 +0000182 // We want to export all symbols that exist both in the executable
183 // and in DSOs, so that the symbols in the executable can interrupt
184 // symbols in the DSO at runtime.
185 if (isShared() != Other->isShared())
186 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15 +0000187 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38 +0000188
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000189 if (L != R)
190 return -1;
George Rimar3498c7f2016-03-10 18:49:24 +0000191 if (!isDefined() || isShared() || isWeak())
Rafael Espindola8e5560d2015-09-23 14:23:59 +0000192 return 1;
George Rimar3498c7f2016-03-10 18:49:24 +0000193 if (!isCommon() && !Other->isCommon())
194 return 0;
195 if (isCommon() && Other->isCommon())
196 return compareCommons(cast<DefinedCommon>(this),
197 cast<DefinedCommon>(Other));
George Rimar34358002016-03-14 09:19:30 +0000198 if (Config->WarnCommon)
199 warning("common " + this->getName() + " is overridden");
George Rimar3498c7f2016-03-10 18:49:24 +0000200 return isCommon() ? -1 : 1;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000201}
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000202
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000203Defined::Defined(Kind K, StringRef Name, bool IsWeak, bool IsLocal,
204 uint8_t Visibility, uint8_t Type)
205 : SymbolBody(K, Name, IsWeak, IsLocal, Visibility, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000206
Rafael Espindola4f29c1a2016-03-07 17:14:36 +0000207DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000208 : Defined(DefinedBitcodeKind, Name, IsWeak, false, Visibility,
209 0 /* Type */) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000210
211bool DefinedBitcode::classof(const SymbolBody *S) {
212 return S->kind() == DefinedBitcodeKind;
213}
214
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000215Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
Davide Italiano255730c2016-03-04 01:55:28 +0000216 uint8_t Visibility, uint8_t Type)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000217 : SymbolBody(K, N, IsWeak, false, Visibility, Type),
George Rimar5c36e592016-02-02 09:28:53 +0000218 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000219
220Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
221 bool CanKeepUndefined)
Davide Italiano255730c2016-03-04 01:55:28 +0000222 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, 0 /* Type */) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000223 this->CanKeepUndefined = CanKeepUndefined;
224}
225
226template <typename ELFT>
227UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
228 : Undefined(SymbolBody::UndefinedElfKind, N,
229 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
Davide Italiano255730c2016-03-04 01:55:28 +0000230 Sym.getType()),
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000231 Sym(Sym) {}
232
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000233template <typename ELFT>
234DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
George Rimaraa4dc202016-03-01 16:23:13 +0000235 OutputSectionBase<ELFT> &Section,
236 uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000237 : Defined(SymbolBody::DefinedSyntheticKind, N, false, false, Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000238 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000239 Value(Value), Section(Section) {}
240
Rafael Espindola11191912015-12-24 16:23:37 +0000241DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
242 bool IsWeak, uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000243 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, false, Visibility,
Rui Ueyama17d69832016-03-10 18:58:53 +0000244 0 /* Type */),
245 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000246
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000247std::unique_ptr<InputFile> Lazy::getMember() {
248 MemoryBufferRef MBRef = File->getMember(&Sym);
249
250 // getMember returns an empty buffer if the member was already
251 // read from the library.
252 if (MBRef.getBuffer().empty())
253 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000254 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000255}
256
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000257// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000258std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000259#if !defined(HAVE_CXXABI_H)
260 return Name;
261#else
262 if (!Config->Demangle)
263 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000264
Rui Ueyamadf154512016-01-13 22:09:09 +0000265 // __cxa_demangle can be used to demangle strings other than symbol
266 // names which do not necessarily start with "_Z". Name can be
267 // either a C or C++ symbol. Don't call __cxa_demangle if the name
268 // does not look like a C++ symbol name to avoid getting unexpected
269 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000270 if (!Name.startswith("_Z"))
271 return Name;
272
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000273 char *Buf =
274 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
275 if (!Buf)
276 return Name;
277 std::string S(Buf);
278 free(Buf);
279 return S;
280#endif
281}
282
Rui Ueyama7ede5432016-03-13 04:40:14 +0000283template bool SymbolBody::template isGnuIfunc<ELF32LE>() const;
284template bool SymbolBody::template isGnuIfunc<ELF32BE>() const;
285template bool SymbolBody::template isGnuIfunc<ELF64LE>() const;
286template bool SymbolBody::template isGnuIfunc<ELF64BE>() const;
287
Rafael Espindola87d9f102016-03-11 12:19:05 +0000288template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
289template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
290template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
291template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000292
293template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
294template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
295template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
296template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
297
298template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
299template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
300template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
301template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
302
303template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
304template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
305template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
306template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
307
Rui Ueyama512c61d2016-02-03 00:12:24 +0000308template uint32_t SymbolBody::template getSize<ELF32LE>() const;
309template uint32_t SymbolBody::template getSize<ELF32BE>() const;
310template uint64_t SymbolBody::template getSize<ELF64LE>() const;
311template uint64_t SymbolBody::template getSize<ELF64BE>() const;
312
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000313template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
314template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
315template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
316template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000317
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000318template class elf::UndefinedElf<ELF32LE>;
319template class elf::UndefinedElf<ELF32BE>;
320template class elf::UndefinedElf<ELF64LE>;
321template class elf::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000322
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000323template class elf::DefinedSynthetic<ELF32LE>;
324template class elf::DefinedSynthetic<ELF32BE>;
325template class elf::DefinedSynthetic<ELF64LE>;
326template class elf::DefinedSynthetic<ELF64BE>;