blob: ad6423f5e93f5323d1d5c07b09ed1e1994fe0856 [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 Espindola9d06ab62015-09-22 00:01:39 +000011#include "InputSection.h"
Rafael Espindola49a2ca62015-08-06 15:33:19 +000012#include "Error.h"
Michael J. Spencercdae0a42015-07-28 22:58:25 +000013#include "InputFiles.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000014
Michael J. Spencer1b348a62015-09-04 22:28:10 +000015#include "llvm/ADT/STLExtras.h"
Rui Ueyamaa4a628f2016-01-13 18:55:39 +000016#include "llvm/Config/config.h"
17
18#ifdef HAVE_CXXABI_H
19#include <cxxabi.h>
20#endif
Michael J. Spencer1b348a62015-09-04 22:28:10 +000021
22using namespace llvm;
Michael J. Spencer84487f12015-07-24 21:03:07 +000023using namespace llvm::object;
Rafael Espindola78471f02015-09-01 23:12:52 +000024using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace lld;
27using namespace lld::elf2;
28
Rafael Espindola78471f02015-09-01 23:12:52 +000029static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
30 if (VA == STV_DEFAULT)
31 return VB;
32 if (VB == STV_DEFAULT)
33 return VA;
34 return std::min(VA, VB);
35}
36
Michael J. Spencer84487f12015-07-24 21:03:07 +000037// Returns 1, 0 or -1 if this symbol should take precedence
38// over the Other, tie or lose, respectively.
Rafael Espindoladaa92a62015-08-31 01:16:19 +000039template <class ELFT> int SymbolBody::compare(SymbolBody *Other) {
Rui Ueyama6be68522015-12-16 23:49:19 +000040 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer1b348a62015-09-04 22:28:10 +000041 assert(!isLazy() && !Other->isLazy());
Rafael Espindola0bc0c022016-01-18 23:54:05 +000042 std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak());
43 std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(),
44 !Other->isWeak());
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000045
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000046 // Normalize
47 if (L > R)
Rafael Espindoladaa92a62015-08-31 01:16:19 +000048 return -Other->compare<ELFT>(this);
Rui Ueyamaa7ccb292015-07-27 20:39:01 +000049
Rui Ueyama8f2c4da2015-10-21 18:13:47 +000050 Visibility = Other->Visibility =
51 getMinVisibility(Visibility, Other->Visibility);
Rafael Espindola78471f02015-09-01 23:12:52 +000052
Rui Ueyama86696f32015-10-21 19:41:03 +000053 if (IsUsedInRegularObj || Other->IsUsedInRegularObj)
54 IsUsedInRegularObj = Other->IsUsedInRegularObj = true;
Rafael Espindola18173d42015-09-08 15:50:05 +000055
George Rimar02ca1792016-01-25 08:44:38 +000056 // We want to export all symbols that exist both in the executable
57 // and in DSOs, so that the symbols in the executable can interrupt
58 // symbols in the DSO at runtime.
59 if (isShared() != Other->isShared())
60 if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this))
61 IsUsedInDynamicReloc = Other->IsUsedInDynamicReloc = true;
62
Rafael Espindola3a63f3f2015-08-28 20:19:34 +000063 if (L != R)
64 return -1;
Rafael Espindola0bc0c022016-01-18 23:54:05 +000065 if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L))
Rafael Espindola8e5560d2015-09-23 14:23:59 +000066 return 1;
Rui Ueyama7da94a52015-09-09 17:40:51 +000067 if (isCommon()) {
Rui Ueyama6666f6a2015-09-09 17:55:09 +000068 if (!Other->isCommon())
Rafael Espindola30e17972015-08-30 23:17:30 +000069 return -1;
Rafael Espindola11191912015-12-24 16:23:37 +000070 auto *ThisC = cast<DefinedCommon>(this);
71 auto *OtherC = cast<DefinedCommon>(Other);
Rui Ueyama6be68522015-12-16 23:49:19 +000072 uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
Rafael Espindola11191912015-12-24 16:23:37 +000073 if (ThisC->Size >= OtherC->Size) {
Rui Ueyama6be68522015-12-16 23:49:19 +000074 ThisC->MaxAlignment = Align;
Rui Ueyama6666f6a2015-09-09 17:55:09 +000075 return 1;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000076 }
Rui Ueyama6be68522015-12-16 23:49:19 +000077 OtherC->MaxAlignment = Align;
Rui Ueyama7da94a52015-09-09 17:40:51 +000078 return -1;
Rafael Espindola30e17972015-08-30 23:17:30 +000079 }
Rui Ueyama7da94a52015-09-09 17:40:51 +000080 if (Other->isCommon())
81 return 1;
82 return 0;
Michael J. Spencer84487f12015-07-24 21:03:07 +000083}
Rafael Espindoladaa92a62015-08-31 01:16:19 +000084
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000085Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility,
86 bool IsTls)
87 : SymbolBody(K, Name, IsWeak, Visibility, IsTls) {}
88
Rafael Espindola5d7593b2015-12-22 23:00:50 +000089Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak,
90 uint8_t Visibility, bool IsTls)
91 : SymbolBody(K, N, IsWeak, Visibility, IsTls), CanKeepUndefined(false) {}
92
93Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility,
94 bool CanKeepUndefined)
95 : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility,
96 /*IsTls*/ false) {
97 this->CanKeepUndefined = CanKeepUndefined;
98}
99
100template <typename ELFT>
101UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym)
102 : Undefined(SymbolBody::UndefinedElfKind, N,
103 Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(),
104 Sym.getType() == llvm::ELF::STT_TLS),
105 Sym(Sym) {}
106
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000107template <typename ELFT>
108DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value,
109 OutputSectionBase<ELFT> &Section)
110 : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, false),
111 Value(Value), Section(Section) {}
112
Rafael Espindola11191912015-12-24 16:23:37 +0000113DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
114 bool IsWeak, uint8_t Visibility)
115 : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility, false) {
116 MaxAlignment = Alignment;
117 this->Size = Size;
118}
119
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000120std::unique_ptr<InputFile> Lazy::getMember() {
121 MemoryBufferRef MBRef = File->getMember(&Sym);
122
123 // getMember returns an empty buffer if the member was already
124 // read from the library.
125 if (MBRef.getBuffer().empty())
126 return std::unique_ptr<InputFile>(nullptr);
Rui Ueyama533c0302016-01-06 00:09:43 +0000127 return createObjectFile(MBRef);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000128}
129
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000130template <class ELFT> static void doInitSymbols() {
Rui Ueyamaa246e0942015-12-25 06:12:18 +0000131 ElfSym<ELFT>::End.setBinding(STB_GLOBAL);
Rafael Espindola65e80b92016-01-19 21:19:52 +0000132 ElfSym<ELFT>::Ignored.setBinding(STB_WEAK);
133 ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN);
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000134}
135
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000136void elf2::initSymbols() {
Rui Ueyamaaca48ff2015-10-08 00:44:28 +0000137 doInitSymbols<ELF32LE>();
138 doInitSymbols<ELF32BE>();
139 doInitSymbols<ELF64LE>();
140 doInitSymbols<ELF64BE>();
Rui Ueyama9ea49c72015-10-07 23:46:11 +0000141}
142
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000143// Returns the demangled C++ symbol name for Name.
144std::string elf2::demangle(StringRef Name) {
145#if !defined(HAVE_CXXABI_H)
146 return Name;
147#else
148 if (!Config->Demangle)
149 return Name;
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000150
Rui Ueyamadf154512016-01-13 22:09:09 +0000151 // __cxa_demangle can be used to demangle strings other than symbol
152 // names which do not necessarily start with "_Z". Name can be
153 // either a C or C++ symbol. Don't call __cxa_demangle if the name
154 // does not look like a C++ symbol name to avoid getting unexpected
155 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama5fa978b2016-01-13 19:40:13 +0000156 if (!Name.startswith("_Z"))
157 return Name;
158
Rui Ueyamaa4a628f2016-01-13 18:55:39 +0000159 char *Buf =
160 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);
161 if (!Buf)
162 return Name;
163 std::string S(Buf);
164 free(Buf);
165 return S;
166#endif
167}
168
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000169template int SymbolBody::compare<ELF32LE>(SymbolBody *Other);
170template int SymbolBody::compare<ELF32BE>(SymbolBody *Other);
171template int SymbolBody::compare<ELF64LE>(SymbolBody *Other);
172template int SymbolBody::compare<ELF64BE>(SymbolBody *Other);
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000173
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000174template class elf2::UndefinedElf<ELF32LE>;
175template class elf2::UndefinedElf<ELF32BE>;
176template class elf2::UndefinedElf<ELF64LE>;
177template class elf2::UndefinedElf<ELF64BE>;
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000178
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000179template class elf2::DefinedSynthetic<ELF32LE>;
180template class elf2::DefinedSynthetic<ELF32BE>;
181template class elf2::DefinedSynthetic<ELF64LE>;
182template class elf2::DefinedSynthetic<ELF64BE>;