blob: b780a99a65580711870ebf8f3e2f0af27cb68004 [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");
Rafael Espindola67d72c02016-03-11 12:06:30 +000071 case DefinedLocalKind:
72 llvm_unreachable("Should not be used");
Rui Ueyamab5a69702016-02-01 21:00:35 +000073 }
74 llvm_unreachable("Invalid symbol kind");
75}
76
77template <class ELFT>
78typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
79 return Out<ELFT>::Got->getVA() +
80 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
81 sizeof(typename ELFFile<ELFT>::uintX_t);
82}
83
84template <class ELFT>
85typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
86 return Out<ELFT>::GotPlt->getVA() +
87 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
88}
89
90template <class ELFT>
91typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
92 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
93 PltIndex * Target->PltEntrySize;
94}
95
Rui Ueyama512c61d2016-02-03 00:12:24 +000096template <class ELFT>
97typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
98 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
99 return B->Sym.st_size;
100 return 0;
101}
102
Rafael Espindola78471f02015-09-01 23:12:52 +0000103static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
104 if (VA == STV_DEFAULT)
105 return VB;
106 if (VB == STV_DEFAULT)
107 return VA;
108 return std::min(VA, VB);
109}
110
George Rimar3498c7f2016-03-10 18:49:24 +0000111static int compareCommons(DefinedCommon *A, DefinedCommon *B) {
Rui Ueyama17d69832016-03-10 18:58:53 +0000112 A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment);
George Rimar3498c7f2016-03-10 18:49:24 +0000113 if (A->Size < B->Size)
114 return -1;
115 return 1;
116}
117
Michael J. Spencer84487f12015-07-24 21:03:07 +0000118// Returns 1, 0 or -1 if this symbol should take precedence
119// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000120template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000121 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05 +0000122 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
123 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
124 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000125
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000126 // Normalize
127 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000128 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +0000129
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000130 Visibility = Other->Visibility =
131 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52 +0000132
Rui Ueyama86696f32015-10-21 19:41:03 +0000133 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
134 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05 +0000135
George Rimar02ca1792016-01-25 08:44:38 +0000136 // We want to export all symbols that exist both in the executable
137 // and in DSOs, so that the symbols in the executable can interrupt
138 // symbols in the DSO at runtime.
139 if (isShared() != Other->isShared())
140 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15 +0000141 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38 +0000142
Rafael Espindola3a63f3f2015-08-28 20:19:34 +0000143 if (L != R)
144 return -1;
George Rimar3498c7f2016-03-10 18:49:24 +0000145 if (!isDefined() || isShared() || isWeak())
Rafael Espindola8e5560d2015-09-23 14:23:59 +0000146 return 1;
George Rimar3498c7f2016-03-10 18:49:24 +0000147 if (!isCommon() && !Other->isCommon())
148 return 0;
149 if (isCommon() && Other->isCommon())
150 return compareCommons(cast<DefinedCommon>(this),
151 cast<DefinedCommon>(Other));
152 return isCommon() ? -1 : 1;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000153}
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000154
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000155Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000156 uint8_t Type)
157 : SymbolBody(K, Name, IsWeak, Visibility, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000158
Rafael Espindola4f29c1a2016-03-07 17:14:36 +0000159DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility)
160 : Defined(DefinedBitcodeKind, Name, IsWeak, Visibility, 0 /* Type */) {}
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000161
162bool DefinedBitcode::classof(const SymbolBody *S) {
163 return S->kind() == DefinedBitcodeKind;
164}
165
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000166Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
Davide Italiano255730c2016-03-04 01:55:28 +0000167 uint8_t Visibility, uint8_t Type)
168 : SymbolBody(K, N, IsWeak, Visibility, Type),
George Rimar5c36e592016-02-02 09:28:53 +0000169 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000170
171Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
172 bool CanKeepUndefined)
Davide Italiano255730c2016-03-04 01:55:28 +0000173 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, 0 /* Type */) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000174 this->CanKeepUndefined = CanKeepUndefined;
175}
176
177template <typename ELFT>
178UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
179 : Undefined(SymbolBody::UndefinedElfKind, N,
180 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
Davide Italiano255730c2016-03-04 01:55:28 +0000181 Sym.getType()),
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000182 Sym(Sym) {}
183
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000184template <typename ELFT>
185DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
George Rimaraa4dc202016-03-01 16:23:13 +0000186 OutputSectionBase<ELFT> &Section,
187 uint8_t Visibility)
188 : Defined(SymbolBody::DefinedSyntheticKind, N, false, Visibility,
Davide Italiano255730c2016-03-04 01:55:28 +0000189 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000190 Value(Value), Section(Section) {}
191
Rafael Espindola11191912015-12-24 16:23:37 +0000192DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
193 bool IsWeak, uint8_t Visibility)
George Rimar5c36e592016-02-02 09:28:53 +0000194 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility,
Rui Ueyama17d69832016-03-10 18:58:53 +0000195 0 /* Type */),
196 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37 +0000197
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000198std::unique_ptr<InputFile> Lazy::getMember() {
199 MemoryBufferRef MBRef = File->getMember(&Sym);
200
201 // getMember returns an empty buffer if the member was already
202 // read from the library.
203 if (MBRef.getBuffer().empty())
204 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41 +0000205 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000206}
207
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000208// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000209std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000210#if !defined(HAVE_CXXABI_H)
211 return Name;
212#else
213 if (!Config->Demangle)
214 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000215
Rui Ueyamadf154512016-01-13 22:09:09 +0000216 // __cxa_demangle can be used to demangle strings other than symbol
217 // names which do not necessarily start with "_Z". Name can be
218 // either a C or C++ symbol. Don't call __cxa_demangle if the name
219 // does not look like a C++ symbol name to avoid getting unexpected
220 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000221 if (!Name.startswith("_Z"))
222 return Name;
223
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000224 char *Buf =
225 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
226 if (!Buf)
227 return Name;
228 std::string S(Buf);
229 free(Buf);
230 return S;
231#endif
232}
233
Rui Ueyamab5a69702016-02-01 21:00:35 +0000234template uint32_t SymbolBody::template getVA<ELF32LE>() const;
235template uint32_t SymbolBody::template getVA<ELF32BE>() const;
236template uint64_t SymbolBody::template getVA<ELF64LE>() const;
237template uint64_t SymbolBody::template getVA<ELF64BE>() const;
238
239template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
240template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
241template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
242template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
243
244template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
245template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
246template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
247template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
248
249template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
250template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
251template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
252template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
253
Rui Ueyama512c61d2016-02-03 00:12:24 +0000254template uint32_t SymbolBody::template getSize<ELF32LE>() const;
255template uint32_t SymbolBody::template getSize<ELF32BE>() const;
256template uint64_t SymbolBody::template getSize<ELF64LE>() const;
257template uint64_t SymbolBody::template getSize<ELF64BE>() const;
258
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000259template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
260template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
261template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
262template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000263
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000264template class elf::UndefinedElf<ELF32LE>;
265template class elf::UndefinedElf<ELF32BE>;
266template class elf::UndefinedElf<ELF64LE>;
267template class elf::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000268
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000269template class elf::DefinedSynthetic<ELF32LE>;
270template class elf::DefinedSynthetic<ELF32BE>;
271template class elf::DefinedSynthetic<ELF64LE>;
272template class elf::DefinedSynthetic<ELF64BE>;