blob: d4f612081ab425edcb6d1b694bd26f0055fd629f [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);
Simon Atanasyan13f6da12016-03-31 21:26:23 +000040 if (D.Value == DefinedSynthetic<ELFT>::SectionEnd)
41 return D.Section.getVA() + D.Section.getSize();
Rafael Espindola87d9f102016-03-11 12:19:05 +000042 return D.Section.getVA() + D.Value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000043 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000044 case SymbolBody::DefinedRegularKind: {
45 auto &D = cast<DefinedRegular<ELFT>>(Body);
46 InputSectionBase<ELFT> *SC = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:35 +000047
48 // This is an absolute symbol.
49 if (!SC)
Rafael Espindola87d9f102016-03-11 12:19:05 +000050 return D.Sym.st_value;
Rui Ueyamab5a69702016-02-01 21:00:35 +000051
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000052 const Elf_Sym &Sym = D.Sym;
53 uintX_t Offset = Sym.st_value;
54 if (Sym.getType() == STT_SECTION) {
55 Offset += Addend;
56 Addend = 0;
57 }
58 uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset);
59 if (Sym.getType() == STT_TLS)
60 return VA - Out<ELFT>::TlsPhdr->p_vaddr;
61 return VA;
Rui Ueyamab5a69702016-02-01 21:00:35 +000062 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000063 case SymbolBody::DefinedCommonKind:
64 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
65 case SymbolBody::SharedKind: {
66 auto &SS = cast<SharedSymbol<ELFT>>(Body);
67 if (!SS.NeedsCopyOrPltAddr)
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000068 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000069 if (SS.IsFunc)
70 return Body.getPltVA<ELFT>();
Rui Ueyama2df72892016-03-13 20:54:38 +000071 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:35 +000072 }
Rafael Espindola87d9f102016-03-11 12:19:05 +000073 case SymbolBody::UndefinedElfKind:
74 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:35 +000075 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000076 case SymbolBody::LazyKind:
George Rimar777f9632016-03-12 08:31:34 +000077 assert(Body.isUsedInRegularObj() && "lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:35 +000078 return 0;
Rafael Espindola87d9f102016-03-11 12:19:05 +000079 case SymbolBody::DefinedBitcodeKind:
Davide Italianof6523ae2016-03-29 02:20:10 +000080 llvm_unreachable("should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:35 +000081 }
George Rimar777f9632016-03-12 08:31:34 +000082 llvm_unreachable("invalid symbol kind");
Rui Ueyamab5a69702016-02-01 21:00:35 +000083}
84
Rui Ueyamac4466602016-03-13 19:48:18 +000085// Returns true if a symbol can be replaced at load-time by a symbol
86// with the same name defined in other ELF executable or DSO.
87bool SymbolBody::isPreemptible() const {
88 if (isLocal())
89 return false;
90
91 if (isShared())
92 return true;
93
94 if (isUndefined()) {
95 if (!isWeak())
96 return true;
97
98 // Ideally the static linker should see a definition for every symbol, but
99 // shared object are normally allowed to have undefined references that the
100 // static linker never sees a definition for.
101 if (Config->Shared)
102 return true;
103
104 // Otherwise, just resolve to 0.
105 return false;
106 }
107
108 if (!Config->Shared)
109 return false;
110 if (getVisibility() != STV_DEFAULT)
111 return false;
112 if (Config->Bsymbolic || (Config->BsymbolicFunctions && IsFunc))
113 return false;
114 return true;
115}
116
Rui Ueyamab5a69702016-02-01 21:00:35 +0000117template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000118typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const {
Rafael Espindola8381c562016-03-17 23:36:19 +0000119 typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend);
120 return OutVA + Addend;
Rafael Espindola87d9f102016-03-11 12:19:05 +0000121}
122
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000123template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000124 return Out<ELFT>::Got->getVA() +
125 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000126 sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000127}
128
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000129template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const {
130 return Out<ELFT>::GotPlt->getVA() + GotPltIndex * sizeof(typename ELFT::uint);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000131}
132
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000133template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000134 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
135 PltIndex * Target->PltEntrySize;
136}
137
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000138template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const {
139 auto *D = cast<DefinedRegular<ELFT>>(this);
140 auto *S = cast<InputSection<ELFT>>(D->Section);
141 return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() +
142 ThunkIndex * Target->ThunkSize;
143}
144
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000145template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000146 if (const typename ELFT::Sym *Sym = getElfSym<ELFT>())
147 return Sym->st_size;
Rui Ueyama512c61d2016-02-03 00:12:24 +0000148 return 0;
149}
150
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000151template <class ELFT> const typename ELFT::Sym *SymbolBody::getElfSym() const {
152 if (auto *S = dyn_cast<DefinedRegular<ELFT>>(this))
153 return &S->Sym;
154 if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this))
155 return &S->Sym;
156 if (auto *S = dyn_cast<UndefinedElf<ELFT>>(this))
157 return &S->Sym;
158 return nullptr;
159}
160
Rafael Espindola78471f02015-09-01 23:12:52 +0000161static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
162 if (VA == STV_DEFAULT)
163 return VB;
164 if (VB == STV_DEFAULT)
165 return VA;
166 return std::min(VA, VB);
167}
168
George Rimar3498c7f2016-03-10 18:49:24 +0000169static int compareCommons(DefinedCommon *A, DefinedCommon *B) {
George Rimar34358002016-03-14 09:19:30 +0000170 if (Config->WarnCommon)
171 warning("multiple common of " + A->getName());
Rui Ueyama17d69832016-03-10 18:58:53 +0000172 A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment);
Davide Italiano901de032016-03-21 22:44:24 +0000173 return A->Size < B->Size ? -1 : 1;
George Rimar3498c7f2016-03-10 18:49:24 +0000174}
175
Michael J. Spencer84487f12015-07-24 21:03:07 +0000176// Returns 1, 0 or -1 if this symbol should take precedence
177// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000178template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000179 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05 +0000180 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
181 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
182 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000183
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000184 // Normalize
185 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000186 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000187
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000188 Visibility = Other->Visibility =
189 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52 +0000190
Rui Ueyama86696f32015-10-21 19:41:03 +0000191 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
192 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05 +0000193
George Rimar02ca1792016-01-25 08:44:38 +0000194 // We want to export all symbols that exist both in the executable
195 // and in DSOs, so that the symbols in the executable can interrupt
196 // symbols in the DSO at runtime.
197 if (isShared() != Other->isShared())
198 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15 +0000199 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38 +0000200
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000201 if (L != R)
202 return -1;
George Rimar3498c7f2016-03-10 18:49:24 +0000203 if (!isDefined() || isShared() || isWeak())
Rafael Espindola8e5560d2015-09-23 14:23:59 +0000204 return 1;
George Rimar3498c7f2016-03-10 18:49:24 +0000205 if (!isCommon() && !Other->isCommon())
206 return 0;
207 if (isCommon() && Other->isCommon())
208 return compareCommons(cast<DefinedCommon>(this),
209 cast<DefinedCommon>(Other));
George Rimar34358002016-03-14 09:19:30 +0000210 if (Config->WarnCommon)
211 warning("common " + this->getName() + " is overridden");
George Rimar3498c7f2016-03-10 18:49:24 +0000212 return isCommon() ? -1 : 1;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000213}
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000214
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000215Defined::Defined(Kind K, StringRef Name, bool IsWeak, bool IsLocal,
216 uint8_t Visibility, uint8_t Type)
217 : SymbolBody(K, Name, IsWeak, IsLocal, Visibility, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000218
Rafael Espindola4f29c1a2016-03-07 17:14:36 +0000219DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000220 : Defined(DefinedBitcodeKind, Name, IsWeak, false, Visibility,
221 0 /* Type */) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000222
223bool DefinedBitcode::classof(const SymbolBody *S) {
224 return S->kind() == DefinedBitcodeKind;
225}
226
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000227Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
Davide Italiano255730c2016-03-04 01:55:28 +0000228 uint8_t Visibility, uint8_t Type)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000229 : SymbolBody(K, N, IsWeak, false, Visibility, Type),
George Rimar5c36e592016-02-02 09:28:53 +0000230 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000231
232Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
233 bool CanKeepUndefined)
Davide Italiano255730c2016-03-04 01:55:28 +0000234 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, 0 /* Type */) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000235 this->CanKeepUndefined = CanKeepUndefined;
236}
237
238template <typename ELFT>
239UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
240 : Undefined(SymbolBody::UndefinedElfKind, N,
241 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
Davide Italiano255730c2016-03-04 01:55:28 +0000242 Sym.getType()),
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000243 Sym(Sym) {}
244
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000245template <typename ELFT>
246DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
George Rimaraa4dc202016-03-01 16:23:13 +0000247 OutputSectionBase<ELFT> &Section,
248 uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000249 : Defined(SymbolBody::DefinedSyntheticKind, N, false, false, Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000250 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000251 Value(Value), Section(Section) {}
252
Rafael Espindola11191912015-12-24 16:23:37 +0000253DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
254 bool IsWeak, uint8_t Visibility)
Rafael Espindola1f5b70f2016-03-11 14:21:37 +0000255 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, false, Visibility,
Rui Ueyama17d69832016-03-10 18:58:53 +0000256 0 /* Type */),
257 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000258
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000259std::unique_ptr<InputFile> Lazy::getMember() {
260 MemoryBufferRef MBRef = File->getMember(&Sym);
261
262 // getMember returns an empty buffer if the member was already
263 // read from the library.
264 if (MBRef.getBuffer().empty())
265 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000266 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000267}
268
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000269// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000270std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000271#if !defined(HAVE_CXXABI_H)
272 return Name;
273#else
274 if (!Config->Demangle)
275 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000276
Rui Ueyamadf154512016-01-13 22:09:09 +0000277 // __cxa_demangle can be used to demangle strings other than symbol
278 // names which do not necessarily start with "_Z". Name can be
279 // either a C or C++ symbol. Don't call __cxa_demangle if the name
280 // does not look like a C++ symbol name to avoid getting unexpected
281 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000282 if (!Name.startswith("_Z"))
283 return Name;
284
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000285 char *Buf =
286 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
287 if (!Buf)
288 return Name;
289 std::string S(Buf);
290 free(Buf);
291 return S;
292#endif
293}
294
Rafael Espindola87d9f102016-03-11 12:19:05 +0000295template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
296template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
297template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
298template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000299
300template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
301template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
302template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
303template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
304
305template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
306template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
307template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
308template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
309
310template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
311template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
312template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
313template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
314
Rui Ueyama512c61d2016-02-03 00:12:24 +0000315template uint32_t SymbolBody::template getSize<ELF32LE>() const;
316template uint32_t SymbolBody::template getSize<ELF32BE>() const;
317template uint64_t SymbolBody::template getSize<ELF64LE>() const;
318template uint64_t SymbolBody::template getSize<ELF64BE>() const;
319
Rui Ueyamabfc1d9d2016-04-02 18:06:18 +0000320template const ELF32LE::Sym *SymbolBody::template getElfSym<ELF32LE>() const;
321template const ELF32BE::Sym *SymbolBody::template getElfSym<ELF32BE>() const;
322template const ELF64LE::Sym *SymbolBody::template getElfSym<ELF64LE>() const;
323template const ELF64BE::Sym *SymbolBody::template getElfSym<ELF64BE>() const;
324
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000325template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const;
326template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const;
327template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const;
328template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const;
329
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000330template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
331template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
332template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
333template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000334
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000335template class elf::UndefinedElf<ELF32LE>;
336template class elf::UndefinedElf<ELF32BE>;
337template class elf::UndefinedElf<ELF64LE>;
338template class elf::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000339
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000340template class elf::DefinedSynthetic<ELF32LE>;
341template class elf::DefinedSynthetic<ELF32BE>;
342template class elf::DefinedSynthetic<ELF64LE>;
343template class elf::DefinedSynthetic<ELF64BE>;