blob: e2fa00585ee6cf1f31e48e17e51b02373b33dd6f [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>
Rafael Espindola87d9f102016-03-11 12:19:0532static typename ELFFile<ELFT>::uintX_t
33getSymVA(const SymbolBody &Body, typename ELFFile<ELFT>::uintX_t &Addend) {
34 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
35 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
36
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:3541 }
Rafael Espindola87d9f102016-03-11 12:19:0542 case SymbolBody::DefinedRegularKind: {
43 auto &D = cast<DefinedRegular<ELFT>>(Body);
44 InputSectionBase<ELFT> *SC = D.Section;
Rui Ueyamab5a69702016-02-01 21:00:3545
46 // This is an absolute symbol.
47 if (!SC)
Rafael Espindola87d9f102016-03-11 12:19:0548 return D.Sym.st_value;
Rui Ueyama0b289522016-02-25 18:43:5149 assert(SC->Live);
Rui Ueyamab5a69702016-02-01 21:00:3550
Rafael Espindola87d9f102016-03-11 12:19:0551 if (D.Sym.getType() == STT_TLS)
52 return SC->OutSec->getVA() + SC->getOffset(D.Sym) -
Rui Ueyamab5a69702016-02-01 21:00:3553 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindola87d9f102016-03-11 12:19:0554 return SC->OutSec->getVA() + SC->getOffset(D.Sym);
Rui Ueyamab5a69702016-02-01 21:00:3555 }
Rafael Espindola87d9f102016-03-11 12:19:0556 case SymbolBody::DefinedCommonKind:
57 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss;
58 case SymbolBody::SharedKind: {
59 auto &SS = cast<SharedSymbol<ELFT>>(Body);
60 if (!SS.NeedsCopyOrPltAddr)
Rafael Espindolaa0a65f92016-02-09 15:11:0161 return 0;
Rafael Espindola87d9f102016-03-11 12:19:0562 if (SS.IsFunc)
63 return Body.getPltVA<ELFT>();
Rafael Espindolaa0a65f92016-02-09 15:11:0164 else
Rafael Espindola87d9f102016-03-11 12:19:0565 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
Rui Ueyamab5a69702016-02-01 21:00:3566 }
Rafael Espindola87d9f102016-03-11 12:19:0567 case SymbolBody::UndefinedElfKind:
68 case SymbolBody::UndefinedKind:
Rui Ueyamab5a69702016-02-01 21:00:3569 return 0;
Rafael Espindola87d9f102016-03-11 12:19:0570 case SymbolBody::LazyKind:
71 assert(Body.isUsedInRegularObj() && "Lazy symbol reached writer");
Rui Ueyamab5a69702016-02-01 21:00:3572 return 0;
Rafael Espindola87d9f102016-03-11 12:19:0573 case SymbolBody::DefinedBitcodeKind:
Rafael Espindola9f77ef02016-02-12 20:54:5774 llvm_unreachable("Should have been replaced");
Rafael Espindola87d9f102016-03-11 12:19:0575 case SymbolBody::DefinedLocalKind: {
76 auto &L = cast<LocalSymbol<ELFT>>(Body);
77 InputSectionBase<ELFT> *SC = L.Section;
78
79 // According to the ELF spec reference to a local symbol from outside the
80 // group are not allowed. Unfortunately .eh_frame breaks that rule and must
81 // be treated specially. For now we just replace the symbol with 0.
82 if (SC == InputSection<ELFT>::Discarded || !SC->Live)
83 return 0;
84
85 const Elf_Sym &Sym = L.Sym;
86 uintX_t Offset = Sym.st_value;
87 if (Sym.getType() == STT_TLS)
88 return (SC->OutSec->getVA() + SC->getOffset(Sym) + Addend) -
89 Out<ELFT>::TlsPhdr->p_vaddr;
90 if (Sym.getType() == STT_SECTION) {
91 Offset += Addend;
92 Addend = 0;
93 }
94 return SC->OutSec->getVA() + SC->getOffset(Offset);
95 }
Rui Ueyamab5a69702016-02-01 21:00:3596 }
97 llvm_unreachable("Invalid symbol kind");
98}
99
100template <class ELFT>
Rafael Espindola87d9f102016-03-11 12:19:05101typename ELFFile<ELFT>::uintX_t
102SymbolBody::getVA(typename ELFFile<ELFT>::uintX_t Addend) const {
103 return getSymVA<ELFT>(*this, Addend) + Addend;
104}
105
106template <class ELFT>
Rui Ueyamab5a69702016-02-01 21:00:35107typename ELFFile<ELFT>::uintX_t SymbolBody::getGotVA() const {
108 return Out<ELFT>::Got->getVA() +
109 (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) *
110 sizeof(typename ELFFile<ELFT>::uintX_t);
111}
112
113template <class ELFT>
114typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const {
115 return Out<ELFT>::GotPlt->getVA() +
116 GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t);
117}
118
119template <class ELFT>
120typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const {
121 return Out<ELFT>::Plt->getVA() + Target->PltZeroSize +
122 PltIndex * Target->PltEntrySize;
123}
124
Rui Ueyama512c61d2016-02-03 00:12:24125template <class ELFT>
126typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const {
127 if (auto *B = dyn_cast<DefinedElf<ELFT>>(this))
128 return B->Sym.st_size;
129 return 0;
130}
131
Rafael Espindola78471f02015-09-01 23:12:52132static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
133 if (VA == STV_DEFAULT)
134 return VB;
135 if (VB == STV_DEFAULT)
136 return VA;
137 return std::min(VA, VB);
138}
139
George Rimar3498c7f2016-03-10 18:49:24140static int compareCommons(DefinedCommon *A, DefinedCommon *B) {
Rui Ueyama17d69832016-03-10 18:58:53141 A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment);
George Rimar3498c7f2016-03-10 18:49:24142 if (A->Size < B->Size)
143 return -1;
144 return 1;
145}
146
Michael J. Spencer84487f12015-07-24 21:03:07147// Returns 1, 0 or -1 if this symbol should take precedence
148// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19149template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Michael J. Spencer1b348a62015-09-04 22:28:10150 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05151 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
152 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
153 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01154
Rafael Espindola3a63f3f2015-08-28 20:19:34155 // Normalize
156 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19157 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01158
Rui Ueyama8f2c4da2015-10-21 18:13:47159 Visibility = Other->Visibility =
160 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52161
Rui Ueyama86696f32015-10-21 19:41:03162 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
163 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05164
George Rimar02ca1792016-01-25 08:44:38165 // We want to export all symbols that exist both in the executable
166 // and in DSOs, so that the symbols in the executable can interrupt
167 // symbols in the DSO at runtime.
168 if (isShared() != Other->isShared())
169 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
Rafael Espindolaabebed92016-02-05 15:27:15170 MustBeInDynSym = Other->MustBeInDynSym = true;
George Rimar02ca1792016-01-25 08:44:38171
Rafael Espindola3a63f3f2015-08-28 20:19:34172 if (L != R)
173 return -1;
George Rimar3498c7f2016-03-10 18:49:24174 if (!isDefined() || isShared() || isWeak())
Rafael Espindola8e5560d2015-09-23 14:23:59175 return 1;
George Rimar3498c7f2016-03-10 18:49:24176 if (!isCommon() && !Other->isCommon())
177 return 0;
178 if (isCommon() && Other->isCommon())
179 return compareCommons(cast<DefinedCommon>(this),
180 cast<DefinedCommon>(Other));
181 return isCommon() ? -1 : 1;
Michael J. Spencer84487f12015-07-24 21:03:07182}
Rafael Espindoladaa92a62015-08-31 01:16:19183
Rafael Espindola4d4b06a2015-12-24 00:47:42184Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
Davide Italiano255730c2016-03-04 01:55:28185 uint8_t Type)
186 : SymbolBody(K, Name, IsWeak, Visibility, Type) {}
Rafael Espindola4d4b06a2015-12-24 00:47:42187
Rafael Espindola4f29c1a2016-03-07 17:14:36188DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility)
189 : Defined(DefinedBitcodeKind, Name, IsWeak, Visibility, 0 /* Type */) {}
Rafael Espindola9f77ef02016-02-12 20:54:57190
191bool DefinedBitcode::classof(const SymbolBody *S) {
192 return S->kind() == DefinedBitcodeKind;
193}
194
Rafael Espindola5d7593b2015-12-22 23:00:50195Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
Davide Italiano255730c2016-03-04 01:55:28196 uint8_t Visibility, uint8_t Type)
197 : SymbolBody(K, N, IsWeak, Visibility, Type),
George Rimar5c36e592016-02-02 09:28:53198 CanKeepUndefined(false) {}
Rafael Espindola5d7593b2015-12-22 23:00:50199
200Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
201 bool CanKeepUndefined)
Davide Italiano255730c2016-03-04 01:55:28202 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, 0 /* Type */) {
Rafael Espindola5d7593b2015-12-22 23:00:50203 this->CanKeepUndefined = CanKeepUndefined;
204}
205
206template <typename ELFT>
207UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
208 : Undefined(SymbolBody::UndefinedElfKind, N,
209 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
Davide Italiano255730c2016-03-04 01:55:28210 Sym.getType()),
Rafael Espindola5d7593b2015-12-22 23:00:50211 Sym(Sym) {}
212
Rafael Espindola4d4b06a2015-12-24 00:47:42213template <typename ELFT>
214DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
George Rimaraa4dc202016-03-01 16:23:13215 OutputSectionBase<ELFT> &Section,
216 uint8_t Visibility)
217 : Defined(SymbolBody::DefinedSyntheticKind, N, false, Visibility,
Davide Italiano255730c2016-03-04 01:55:28218 0 /* Type */),
Rafael Espindola4d4b06a2015-12-24 00:47:42219 Value(Value), Section(Section) {}
220
Rafael Espindola11191912015-12-24 16:23:37221DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
222 bool IsWeak, uint8_t Visibility)
George Rimar5c36e592016-02-02 09:28:53223 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility,
Rui Ueyama17d69832016-03-10 18:58:53224 0 /* Type */),
225 Alignment(Alignment), Size(Size) {}
Rafael Espindola11191912015-12-24 16:23:37226
Michael J. Spencer1b348a62015-09-04 22:28:10227std::unique_ptr<InputFile> Lazy::getMember() {
228 MemoryBufferRef MBRef = File->getMember(&Sym);
229
230 // getMember returns an empty buffer if the member was already
231 // read from the library.
232 if (MBRef.getBuffer().empty())
233 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama71c066d2016-02-02 08:22:41234 return createObjectFile(MBRef, File->getName());
Michael J. Spencer1b348a62015-09-04 22:28:10235}
236
Rui Ueyamaa4a628f2016-01-13 18:55:39237// Returns the demangled C++ symbol name for Name.
Rafael Espindolae0df00b2016-02-28 00:25:54238std::string elf::demangle(StringRef Name) {
Rui Ueyamaa4a628f2016-01-13 18:55:39239#if !defined(HAVE_CXXABI_H)
240 return Name;
241#else
242 if (!Config->Demangle)
243 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13244
Rui Ueyamadf154512016-01-13 22:09:09245 // __cxa_demangle can be used to demangle strings other than symbol
246 // names which do not necessarily start with "_Z". Name can be
247 // either a C or C++ symbol. Don't call __cxa_demangle if the name
248 // does not look like a C++ symbol name to avoid getting unexpected
249 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13250 if (!Name.startswith("_Z"))
251 return Name;
252
Rui Ueyamaa4a628f2016-01-13 18:55:39253 char *Buf =
254 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
255 if (!Buf)
256 return Name;
257 std::string S(Buf);
258 free(Buf);
259 return S;
260#endif
261}
262
Rafael Espindola87d9f102016-03-11 12:19:05263template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const;
264template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const;
265template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const;
266template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const;
Rui Ueyamab5a69702016-02-01 21:00:35267
268template uint32_t SymbolBody::template getGotVA<ELF32LE>() const;
269template uint32_t SymbolBody::template getGotVA<ELF32BE>() const;
270template uint64_t SymbolBody::template getGotVA<ELF64LE>() const;
271template uint64_t SymbolBody::template getGotVA<ELF64BE>() const;
272
273template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const;
274template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const;
275template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const;
276template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const;
277
278template uint32_t SymbolBody::template getPltVA<ELF32LE>() const;
279template uint32_t SymbolBody::template getPltVA<ELF32BE>() const;
280template uint64_t SymbolBody::template getPltVA<ELF64LE>() const;
281template uint64_t SymbolBody::template getPltVA<ELF64BE>() const;
282
Rui Ueyama512c61d2016-02-03 00:12:24283template uint32_t SymbolBody::template getSize<ELF32LE>() const;
284template uint32_t SymbolBody::template getSize<ELF32BE>() const;
285template uint64_t SymbolBody::template getSize<ELF64LE>() const;
286template uint64_t SymbolBody::template getSize<ELF64BE>() const;
287
Rafael Espindoladaa92a62015-08-31 01:16:19288template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
289template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
290template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
291template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50292
Rafael Espindolae0df00b2016-02-28 00:25:54293template class elf::UndefinedElf<ELF32LE>;
294template class elf::UndefinedElf<ELF32BE>;
295template class elf::UndefinedElf<ELF64LE>;
296template class elf::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42297
Rafael Espindolae0df00b2016-02-28 00:25:54298template class elf::DefinedSynthetic<ELF32LE>;
299template class elf::DefinedSynthetic<ELF32BE>;
300template class elf::DefinedSynthetic<ELF64LE>;
301template class elf::DefinedSynthetic<ELF64BE>;