blob: 6bc285ce5132ffa0ff15224b3ad3c6b837fbef7f [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;
29using namespace lld::elf2;
30
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;
45
46 // Symbol offsets for AMDGPU need to be the offset in bytes of the symbol
47 // from the beginning of the section.
48 if (Config->EMachine == EM_AMDGPU)
49 return SC->getOffset(D->Sym);
50 if (D->Sym.getType() == STT_TLS)
51 return SC->OutSec->getVA() + SC->getOffset(D->Sym) -
52 Out<ELFT>::TlsPhdr->p_vaddr;
53 return SC->OutSec->getVA() + SC->getOffset(D->Sym);
54 }
55 case DefinedCommonKind:
56 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(this)->OffsetInBss;
57 case SharedKind: {
58 auto *SS = cast<SharedSymbol<ELFT>>(this);
59 if (SS->NeedsCopy)
60 return Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
61 return 0;
62 }
63 case UndefinedElfKind:
64 case UndefinedKind:
65 return 0;
66 case LazyKind:
67 assert(isUsedInRegularObj() && "Lazy symbol reached writer");
68 return 0;
69 }
70 llvm_unreachable("Invalid symbol kind");
71}
72
73template <class ELFT>
74typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
75 return Out<ELFT>::Got->getVA() +
76 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
77 sizeof(typename ELFFile<ELFT>::uintX_t);
78}
79
80template <class ELFT>
81typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
82 return Out<ELFT>::GotPlt->getVA() +
83 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
84}
85
86template <class ELFT>
87typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
88 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
89 PltIndex * Target->PltEntrySize;
90}
91
Rafael Espindola78471f02015-09-01 23:12:5292static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
93 if (VA == STV_DEFAULT)
94 return VB;
95 if (VB == STV_DEFAULT)
96 return VA;
97 return std::min(VA, VB);
98}
99
Michael J. Spencer84487f12015-07-24 21:03:07100// Returns 1, 0 or -1 if this symbol should take precedence
101// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19102template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rui Ueyama6be68522015-12-16 23:49:19103 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer1b348a62015-09-04 22:28:10104 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05105 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
106 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
107 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01108
Rafael Espindola3a63f3f2015-08-28 20:19:34109 // Normalize
110 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19111 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01112
Rui Ueyama8f2c4da2015-10-21 18:13:47113 Visibility = Other->Visibility =
114 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52115
Rui Ueyama86696f32015-10-21 19:41:03116 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
117 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05118
George Rimar02ca1792016-01-25 08:44:38119 // We want to export all symbols that exist both in the executable
120 // and in DSOs, so that the symbols in the executable can interrupt
121 // symbols in the DSO at runtime.
122 if (isShared() != Other->isShared())
123 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
124 IsUsedInDynamicReloc = Other->IsUsedInDynamicReloc = true;
125
Rafael Espindola3a63f3f2015-08-28 20:19:34126 if (L != R)
127 return -1;
Rafael Espindola0bc0c022016-01-18 23:54:05128 if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L))
Rafael Espindola8e5560d2015-09-23 14:23:59129 return 1;
Rui Ueyama7da94a52015-09-09 17:40:51130 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:09131 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:30132 return -1;
Rafael Espindola11191912015-12-24 16:23:37133 auto *ThisC = cast<DefinedCommon>(this);
134 auto *OtherC = cast<DefinedCommon>(Other);
Rui Ueyama6be68522015-12-16 23:49:19135 uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
Rafael Espindola11191912015-12-24 16:23:37136 if (ThisC->Size >= OtherC->Size) {
Rui Ueyama6be68522015-12-16 23:49:19137 ThisC->MaxAlignment = Align;
Rui Ueyama6666f6a2015-09-09 17:55:09138 return 1;
Rafael Espindoladaa92a62015-08-31 01:16:19139 }
Rui Ueyama6be68522015-12-16 23:49:19140 OtherC->MaxAlignment = Align;
Rui Ueyama7da94a52015-09-09 17:40:51141 return -1;
Rafael Espindola30e17972015-08-30 23:17:30142 }
Rui Ueyama7da94a52015-09-09 17:40:51143 if (Other->isCommon())
144 return 1;
145 return 0;
Michael J. Spencer84487f12015-07-24 21:03:07146}
Rafael Espindoladaa92a62015-08-31 01:16:19147
Rafael Espindola4d4b06a2015-12-24 00:47:42148Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
149 bool IsTls)
150 : SymbolBody(K, Name, IsWeak, Visibility, IsTls) {}
151
Rafael Espindola5d7593b2015-12-22 23:00:50152Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
153 uint8_t Visibility, bool IsTls)
154 : SymbolBody(K, N, IsWeak, Visibility, IsTls), CanKeepUndefined(false) {}
155
156Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
157 bool CanKeepUndefined)
158 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility,
159 /*IsTls*/ false) {
160 this->CanKeepUndefined = CanKeepUndefined;
161}
162
163template <typename ELFT>
164UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
165 : Undefined(SymbolBody::UndefinedElfKind, N,
166 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
167 Sym.getType() == llvm::ELF::STT_TLS),
168 Sym(Sym) {}
169
Rafael Espindola4d4b06a2015-12-24 00:47:42170template <typename ELFT>
171DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
172 OutputSectionBase<ELFT> &Section)
173 : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, false),
174 Value(Value), Section(Section) {}
175
Rafael Espindola11191912015-12-24 16:23:37176DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
177 bool IsWeak, uint8_t Visibility)
178 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility, false) {
179 MaxAlignment = Alignment;
180 this->Size = Size;
181}
182
Michael J. Spencer1b348a62015-09-04 22:28:10183std::unique_ptr<InputFile> Lazy::getMember() {
184 MemoryBufferRef MBRef = File->getMember(&Sym);
185
186 // getMember returns an empty buffer if the member was already
187 // read from the library.
188 if (MBRef.getBuffer().empty())
189 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama533c0302016-01-06 00:09:43190 return createObjectFile(MBRef);
Michael J. Spencer1b348a62015-09-04 22:28:10191}
192
Rui Ueyamaaca48ff2015-10-08 00:44:28193template <class ELFT> static void doInitSymbols() {
Rui Ueyamaa246e0942015-12-25 06:12:18194 ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
Rafael Espindola65e80b92016-01-19 21:19:52195 ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
196 ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
Rui Ueyamaaca48ff2015-10-08 00:44:28197}
198
Rui Ueyama83cd6e02016-01-06 20:11:55199void elf2::initSymbols() {
Rui Ueyamaaca48ff2015-10-08 00:44:28200 doInitSymbols<ELF32LE>();
201 doInitSymbols<ELF32BE>();
202 doInitSymbols<ELF64LE>();
203 doInitSymbols<ELF64BE>();
Rui Ueyama9ea49c72015-10-07 23:46:11204}
205
Rui Ueyamaa4a628f2016-01-13 18:55:39206// Returns the demangled C++ symbol name for Name.
207std::string elf2::demangle(StringRef Name) {
208#if !defined(HAVE_CXXABI_H)
209 return Name;
210#else
211 if (!Config->Demangle)
212 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13213
Rui Ueyamadf154512016-01-13 22:09:09214 // __cxa_demangle can be used to demangle strings other than symbol
215 // names which do not necessarily start with "_Z". Name can be
216 // either a C or C++ symbol. Don't call __cxa_demangle if the name
217 // does not look like a C++ symbol name to avoid getting unexpected
218 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13219 if (!Name.startswith("_Z"))
220 return Name;
221
Rui Ueyamaa4a628f2016-01-13 18:55:39222 char *Buf =
223 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
224 if (!Buf)
225 return Name;
226 std::string S(Buf);
227 free(Buf);
228 return S;
229#endif
230}
231
Rui Ueyamab5a69702016-02-01 21:00:35232template uint32_t SymbolBody::template getVA<ELF32LE>() const;
233template uint32_t SymbolBody::template getVA<ELF32BE>() const;
234template uint64_t SymbolBody::template getVA<ELF64LE>() const;
235template uint64_t SymbolBody::template getVA<ELF64BE>() const;
236
237template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
238template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
239template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
240template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
241
242template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
243template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
244template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
245template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
246
247template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
248template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
249template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
250template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
251
Rafael Espindoladaa92a62015-08-31 01:16:19252template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
253template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
254template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
255template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50256
Rui Ueyama83cd6e02016-01-06 20:11:55257template class elf2::UndefinedElf<ELF32LE>;
258template class elf2::UndefinedElf<ELF32BE>;
259template class elf2::UndefinedElf<ELF64LE>;
260template class elf2::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42261
Rui Ueyama83cd6e02016-01-06 20:11:55262template class elf2::DefinedSynthetic<ELF32LE>;
263template class elf2::DefinedSynthetic<ELF32BE>;
264template class elf2::DefinedSynthetic<ELF64LE>;
265template class elf2::DefinedSynthetic<ELF64BE>;