blob: 49304e4bcdad2c7ece142ec158d0d86df4327bca [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:071//===- 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:1911#include "Error.h"
Michael J. Spencercdae0a42015-07-28 22:58:2512#include "InputFiles.h"
Rui Ueyamab5a69702016-02-01 21:00:3513#include "InputSection.h"
14#include "OutputSections.h"
15#include "Target.h"
Michael J. Spencer84487f12015-07-24 21:03:0716
Michael J. Spencer1b348a62015-09-04 22:28:1017#include "llvm/ADT/STLExtras.h"
Rui Ueyamaa4a628f2016-01-13 18:55:3918#include "llvm/Config/config.h"
19
20#ifdef HAVE_CXXABI_H
21#include <cxxabi.h>
22#endif
Michael J. Spencer1b348a62015-09-04 22:28:1023
24using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:0725using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:5226using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:0727
28using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:5429using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:0730
Rui Ueyamab5a69702016-02-01 21:00:3531template <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:5145 assert(SC->Live);
Rui Ueyamab5a69702016-02-01 21:00:3546
Rui Ueyama72acaa12016-02-26 15:39:2647 // Symbol offsets for AMDGPU are the offsets in bytes of the symbols
48 // from the beginning of the section. Note that this part of AMDGPU's
49 // ELF spec is odd and not in line with the standard ELF.
Rui Ueyamab5a69702016-02-01 21:00:3550 if (Config->EMachine == EM_AMDGPU)
51 return SC->getOffset(D->Sym);
Rui Ueyama72acaa12016-02-26 15:39:2652
Rui Ueyamab5a69702016-02-01 21:00:3553 if (D->Sym.getType() == STT_TLS)
54 return SC->OutSec->getVA() + SC->getOffset(D->Sym) -
55 Out<ELFT>::TlsPhdr->p_vaddr;
56 return SC->OutSec->getVA() + SC->getOffset(D->Sym);
57 }
58 case DefinedCommonKind:
59 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(this)->OffsetInBss;
60 case SharedKind: {
61 auto *SS = cast<SharedSymbol<ELFT>>(this);
Rafael Espindolaa0a65f92016-02-09 15:11:0162 if (!SS->NeedsCopyOrPltAddr)
63 return 0;
Davide Italiano255730c2016-03-04 01:55:2864 if (SS->isFunc())
Rafael Espindolaa0a65f92016-02-09 15:11:0165 return getPltVA<ELFT>();
66 else
Rui Ueyamab5a69702016-02-01 21:00:3567 return Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:3568 }
69 case UndefinedElfKind:
70 case UndefinedKind:
71 return 0;
72 case LazyKind:
73 assert(isUsedInRegularObj() && "Lazy symbol reached writer");
74 return 0;
Rafael Espindola9f77ef02016-02-12 20:54:5775 case DefinedBitcodeKind:
76 llvm_unreachable("Should have been replaced");
Rui Ueyamab5a69702016-02-01 21:00:3577 }
78 llvm_unreachable("Invalid symbol kind");
79}
80
81template <class ELFT>
82typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
83 return Out<ELFT>::Got->getVA() +
84 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
85 sizeof(typename ELFFile<ELFT>::uintX_t);
86}
87
88template <class ELFT>
89typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
90 return Out<ELFT>::GotPlt->getVA() +
91 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
92}
93
94template <class ELFT>
95typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
96 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
97 PltIndex * Target->PltEntrySize;
98}
99
Rui Ueyama512c61d2016-02-03 00:12:24100template <class ELFT>
101typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
102 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
103 return B->Sym.st_size;
104 return 0;
105}
106
Rafael Espindola78471f02015-09-01 23:12:52107static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
108 if (VA == STV_DEFAULT)
109 return VB;
110 if (VB == STV_DEFAULT)
111 return VA;
112 return std::min(VA, VB);
113}
114
Michael J. Spencer84487f12015-07-24 21:03:07115// Returns 1, 0 or -1 if this symbol should take precedence
116// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19117template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rui Ueyama6be68522015-12-16 23:49:19118 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer1b348a62015-09-04 22:28:10119 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05120 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
121 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
122 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01123
Rafael Espindola3a63f3f2015-08-28 20:19:34124 // Normalize
125 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19126 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01127
Rui Ueyama8f2c4da2015-10-21 18:13:47128 Visibility = Other->Visibility =
129 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52130
Rui Ueyama86696f32015-10-21 19:41:03131 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
132 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05133
George Rimar02ca1792016-01-25 08:44:38134 // We want to export all symbols that exist both in the executable
135 // and in DSOs, so that the symbols in the executable can interrupt
136 // symbols in the DSO at runtime.
137 if (isShared() != Other->isShared())
138 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15139 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38140
Rafael Espindola3a63f3f2015-08-28 20:19:34141 if (L != R)
142 return -1;
Rafael Espindola0bc0c022016-01-18 23:54:05143 if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L))
Rafael Espindola8e5560d2015-09-23 14:23:59144 return 1;
Rui Ueyama7da94a52015-09-09 17:40:51145 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:09146 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:30147 return -1;
Rafael Espindola11191912015-12-24 16:23:37148 auto *ThisC = cast<DefinedCommon>(this);
149 auto *OtherC = cast<DefinedCommon>(Other);
Rui Ueyama6be68522015-12-16 23:49:19150 uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
Rafael Espindola11191912015-12-24 16:23:37151 if (ThisC->Size >= OtherC->Size) {
Rui Ueyama6be68522015-12-16 23:49:19152 ThisC->MaxAlignment = Align;
Rui Ueyama6666f6a2015-09-09 17:55:09153 return 1;
Rafael Espindoladaa92a62015-08-31 01:16:19154 }
Rui Ueyama6be68522015-12-16 23:49:19155 OtherC->MaxAlignment = Align;
Rui Ueyama7da94a52015-09-09 17:40:51156 return -1;
Rafael Espindola30e17972015-08-30 23:17:30157 }
Rui Ueyama7da94a52015-09-09 17:40:51158 if (Other->isCommon())
159 return 1;
160 return 0;
Michael J. Spencer84487f12015-07-24 21:03:07161}
Rafael Espindoladaa92a62015-08-31 01:16:19162
Rafael Espindola4d4b06a2015-12-24 00:47:42163Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
Davide Italiano255730c2016-03-04 01:55:28164 uint8_t Type)
165 : SymbolBody(K, Name, IsWeak, Visibility, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42166
Rafael Espindola148445ef2016-02-25 16:25:41167DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak)
Davide Italiano255730c2016-03-04 01:55:28168 : Defined(DefinedBitcodeKind, Name, IsWeak, STV_DEFAULT, 0 /* Type */) {}
Rafael Espindola9f77ef02016-02-12 20:54:57169
170bool DefinedBitcode::classof(const SymbolBody *S) {
171 return S->kind() == DefinedBitcodeKind;
172}
173
Rafael Espindola5d7593b2015-12-22 23:00:50174Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
Davide Italiano255730c2016-03-04 01:55:28175 uint8_t Visibility, uint8_t Type)
176 : SymbolBody(K, N, IsWeak, Visibility, Type),
George Rimar5c36e592016-02-02 09:28:53177 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50178
179Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
180 bool CanKeepUndefined)
Davide Italiano255730c2016-03-04 01:55:28181 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, 0 /* Type */) {
Rafael Espindola5d7593b2015-12-22 23:00:50182 this->CanKeepUndefined = CanKeepUndefined;
183}
184
185template <typename ELFT>
186UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
187 : Undefined(SymbolBody::UndefinedElfKind, N,
188 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
Davide Italiano255730c2016-03-04 01:55:28189 Sym.getType()),
Rafael Espindola5d7593b2015-12-22 23:00:50190 Sym(Sym) {}
191
Rafael Espindola4d4b06a2015-12-24 00:47:42192template <typename ELFT>
193DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
George Rimaraa4dc202016-03-01 16:23:13194 OutputSectionBase<ELFT> &Section,
195 uint8_t Visibility)
196 : Defined(SymbolBody::DefinedSyntheticKind, N, false, Visibility,
Davide Italiano255730c2016-03-04 01:55:28197 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42198 Value(Value), Section(Section) {}
199
Rafael Espindola11191912015-12-24 16:23:37200DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
201 bool IsWeak, uint8_t Visibility)
George Rimar5c36e592016-02-02 09:28:53202 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility,
Davide Italiano255730c2016-03-04 01:55:28203 0 /* Type */) {
Rafael Espindola11191912015-12-24 16:23:37204 MaxAlignment = Alignment;
205 this->Size = Size;
206}
207
Michael J. Spencer1b348a62015-09-04 22:28:10208std::unique_ptr<InputFile> Lazy::getMember() {
209 MemoryBufferRef MBRef = File->getMember(&Sym);
210
211 // getMember returns an empty buffer if the member was already
212 // read from the library.
213 if (MBRef.getBuffer().empty())
214 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41215 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10216}
217
Rui Ueyamaaca48ff2015-10-08 00:44:28218template <class ELFT> static void doInitSymbols() {
George Rimar9e859392016-02-26 14:36:36219 ElfSym<ELFT>::Etext.setBinding(STB_GLOBAL);
220 ElfSym<ELFT>::Edata.setBinding(STB_GLOBAL);
Rui Ueyamaa246e0942015-12-25 06:12:18221 ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
Rafael Espindola65e80b92016-01-19 21:19:52222 ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
223 ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
Rui Ueyamaaca48ff2015-10-08 00:44:28224}
225
Rafael Espindolae0df00b2016-02-28 00:25:54226void elf::initSymbols() {
Rui Ueyamaaca48ff2015-10-08 00:44:28227 doInitSymbols<ELF32LE>();
228 doInitSymbols<ELF32BE>();
229 doInitSymbols<ELF64LE>();
230 doInitSymbols<ELF64BE>();
Rui Ueyama9ea49c72015-10-07 23:46:11231}
232
Rui Ueyamaa4a628f2016-01-13 18:55:39233// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54234std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39235#if !defined(HAVE_CXXABI_H)
236 return Name;
237#else
238 if (!Config->Demangle)
239 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13240
Rui Ueyamadf154512016-01-13 22:09:09241 // __cxa_demangle can be used to demangle strings other than symbol
242 // names which do not necessarily start with "_Z". Name can be
243 // either a C or C++ symbol. Don't call __cxa_demangle if the name
244 // does not look like a C++ symbol name to avoid getting unexpected
245 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13246 if (!Name.startswith("_Z"))
247 return Name;
248
Rui Ueyamaa4a628f2016-01-13 18:55:39249 char *Buf =
250 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
251 if (!Buf)
252 return Name;
253 std::string S(Buf);
254 free(Buf);
255 return S;
256#endif
257}
258
Rui Ueyamab5a69702016-02-01 21:00:35259template uint32_t SymbolBody::template getVA<ELF32LE>() const;
260template uint32_t SymbolBody::template getVA<ELF32BE>() const;
261template uint64_t SymbolBody::template getVA<ELF64LE>() const;
262template uint64_t SymbolBody::template getVA<ELF64BE>() const;
263
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:24279template 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:19284template 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:50288
Rafael Espindolae0df00b2016-02-28 00:25:54289template 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:42293
Rafael Espindolae0df00b2016-02-28 00:25:54294template class elf::DefinedSynthetic<ELF32LE>;
295template class elf::DefinedSynthetic<ELF32BE>;
296template class elf::DefinedSynthetic<ELF64LE>;
297template class elf::DefinedSynthetic<ELF64BE>;