Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 1 | //===- 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 Espindola | 49a2ca6 | 2015-08-06 15:33:19 | [diff] [blame] | 11 | #include "Error.h" |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 | [diff] [blame] | 12 | #include "InputFiles.h" |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 13 | #include "InputSection.h" |
| 14 | #include "OutputSections.h" |
| 15 | #include "Target.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 16 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 | [diff] [blame] | 18 | #include "llvm/Config/config.h" |
| 19 | |
| 20 | #ifdef HAVE_CXXABI_H |
| 21 | #include <cxxabi.h> |
| 22 | #endif |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 25 | using namespace llvm::object; |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 | [diff] [blame] | 26 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 27 | |
| 28 | using namespace lld; |
| 29 | using namespace lld::elf2; |
| 30 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 31 | template <class ELFT> |
| 32 | typename 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 | |
| 73 | template <class ELFT> |
| 74 | typename 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 | |
| 80 | template <class ELFT> |
| 81 | typename ELFFile<ELFT>::uintX_t SymbolBody::getGotPltVA() const { |
| 82 | return Out<ELFT>::GotPlt->getVA() + |
| 83 | GotPltIndex * sizeof(typename ELFFile<ELFT>::uintX_t); |
| 84 | } |
| 85 | |
| 86 | template <class ELFT> |
| 87 | typename ELFFile<ELFT>::uintX_t SymbolBody::getPltVA() const { |
| 88 | return Out<ELFT>::Plt->getVA() + Target->PltZeroSize + |
| 89 | PltIndex * Target->PltEntrySize; |
| 90 | } |
| 91 | |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 | [diff] [blame^] | 92 | template <class ELFT> |
| 93 | typename ELFFile<ELFT>::uintX_t SymbolBody::getSize() const { |
| 94 | if (auto *B = dyn_cast<DefinedElf<ELFT>>(this)) |
| 95 | return B->Sym.st_size; |
| 96 | return 0; |
| 97 | } |
| 98 | |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 | [diff] [blame] | 99 | static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { |
| 100 | if (VA == STV_DEFAULT) |
| 101 | return VB; |
| 102 | if (VB == STV_DEFAULT) |
| 103 | return VA; |
| 104 | return std::min(VA, VB); |
| 105 | } |
| 106 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 107 | // Returns 1, 0 or -1 if this symbol should take precedence |
| 108 | // over the Other, tie or lose, respectively. |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 109 | template <class ELFT> int SymbolBody::compare(SymbolBody *Other) { |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 | [diff] [blame] | 110 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 111 | assert(!isLazy() && !Other->isLazy()); |
Rafael Espindola | 0bc0c02 | 2016-01-18 23:54:05 | [diff] [blame] | 112 | std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak()); |
| 113 | std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(), |
| 114 | !Other->isWeak()); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 | [diff] [blame] | 115 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 | [diff] [blame] | 116 | // Normalize |
| 117 | if (L > R) |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 118 | return -Other->compare<ELFT>(this); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 | [diff] [blame] | 119 | |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 | [diff] [blame] | 120 | Visibility = Other->Visibility = |
| 121 | getMinVisibility(Visibility, Other->Visibility); |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 | [diff] [blame] | 122 | |
Rui Ueyama | 86696f3 | 2015-10-21 19:41:03 | [diff] [blame] | 123 | if (IsUsedInRegularObj || Other->IsUsedInRegularObj) |
| 124 | IsUsedInRegularObj = Other->IsUsedInRegularObj = true; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 | [diff] [blame] | 125 | |
George Rimar | 02ca179 | 2016-01-25 08:44:38 | [diff] [blame] | 126 | // We want to export all symbols that exist both in the executable |
| 127 | // and in DSOs, so that the symbols in the executable can interrupt |
| 128 | // symbols in the DSO at runtime. |
| 129 | if (isShared() != Other->isShared()) |
| 130 | if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this)) |
| 131 | IsUsedInDynamicReloc = Other->IsUsedInDynamicReloc = true; |
| 132 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 | [diff] [blame] | 133 | if (L != R) |
| 134 | return -1; |
Rafael Espindola | 0bc0c02 | 2016-01-18 23:54:05 | [diff] [blame] | 135 | if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L)) |
Rafael Espindola | 8e5560d | 2015-09-23 14:23:59 | [diff] [blame] | 136 | return 1; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 | [diff] [blame] | 137 | if (isCommon()) { |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 | [diff] [blame] | 138 | if (!Other->isCommon()) |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 | [diff] [blame] | 139 | return -1; |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 | [diff] [blame] | 140 | auto *ThisC = cast<DefinedCommon>(this); |
| 141 | auto *OtherC = cast<DefinedCommon>(Other); |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 | [diff] [blame] | 142 | uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment); |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 | [diff] [blame] | 143 | if (ThisC->Size >= OtherC->Size) { |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 | [diff] [blame] | 144 | ThisC->MaxAlignment = Align; |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 | [diff] [blame] | 145 | return 1; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 146 | } |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 | [diff] [blame] | 147 | OtherC->MaxAlignment = Align; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 | [diff] [blame] | 148 | return -1; |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 | [diff] [blame] | 149 | } |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 | [diff] [blame] | 150 | if (Other->isCommon()) |
| 151 | return 1; |
| 152 | return 0; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 | [diff] [blame] | 153 | } |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 154 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 | [diff] [blame] | 155 | Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, |
George Rimar | 5c36e59 | 2016-02-02 09:28:53 | [diff] [blame] | 156 | bool IsTls, bool IsFunction) |
| 157 | : SymbolBody(K, Name, IsWeak, Visibility, IsTls, IsFunction) {} |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 | [diff] [blame] | 158 | |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 | [diff] [blame] | 159 | Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak, |
| 160 | uint8_t Visibility, bool IsTls) |
George Rimar | 5c36e59 | 2016-02-02 09:28:53 | [diff] [blame] | 161 | : SymbolBody(K, N, IsWeak, Visibility, IsTls, /*IsFunction*/ false), |
| 162 | CanKeepUndefined(false) {} |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 | [diff] [blame] | 163 | |
| 164 | Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility, |
| 165 | bool CanKeepUndefined) |
| 166 | : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, |
| 167 | /*IsTls*/ false) { |
| 168 | this->CanKeepUndefined = CanKeepUndefined; |
| 169 | } |
| 170 | |
| 171 | template <typename ELFT> |
| 172 | UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym) |
| 173 | : Undefined(SymbolBody::UndefinedElfKind, N, |
| 174 | Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(), |
| 175 | Sym.getType() == llvm::ELF::STT_TLS), |
| 176 | Sym(Sym) {} |
| 177 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 | [diff] [blame] | 178 | template <typename ELFT> |
| 179 | DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value, |
| 180 | OutputSectionBase<ELFT> &Section) |
George Rimar | 5c36e59 | 2016-02-02 09:28:53 | [diff] [blame] | 181 | : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, |
| 182 | /*IsTls*/ false, /*IsFunction*/ false), |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 | [diff] [blame] | 183 | Value(Value), Section(Section) {} |
| 184 | |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 | [diff] [blame] | 185 | DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, |
| 186 | bool IsWeak, uint8_t Visibility) |
George Rimar | 5c36e59 | 2016-02-02 09:28:53 | [diff] [blame] | 187 | : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility, |
| 188 | /*IsTls*/ false, /*IsFunction*/ false) { |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 | [diff] [blame] | 189 | MaxAlignment = Alignment; |
| 190 | this->Size = Size; |
| 191 | } |
| 192 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 193 | std::unique_ptr<InputFile> Lazy::getMember() { |
| 194 | MemoryBufferRef MBRef = File->getMember(&Sym); |
| 195 | |
| 196 | // getMember returns an empty buffer if the member was already |
| 197 | // read from the library. |
| 198 | if (MBRef.getBuffer().empty()) |
| 199 | return std::unique_ptr<InputFile>(nullptr); |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 | [diff] [blame] | 200 | return createObjectFile(MBRef, File->getName()); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 | [diff] [blame] | 201 | } |
| 202 | |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 | [diff] [blame] | 203 | template <class ELFT> static void doInitSymbols() { |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 | [diff] [blame] | 204 | ElfSym<ELFT>::End.setBinding(STB_GLOBAL); |
Rafael Espindola | 65e80b9 | 2016-01-19 21:19:52 | [diff] [blame] | 205 | ElfSym<ELFT>::Ignored.setBinding(STB_WEAK); |
| 206 | ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN); |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 | [diff] [blame] | 207 | } |
| 208 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 | [diff] [blame] | 209 | void elf2::initSymbols() { |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 | [diff] [blame] | 210 | doInitSymbols<ELF32LE>(); |
| 211 | doInitSymbols<ELF32BE>(); |
| 212 | doInitSymbols<ELF64LE>(); |
| 213 | doInitSymbols<ELF64BE>(); |
Rui Ueyama | 9ea49c7 | 2015-10-07 23:46:11 | [diff] [blame] | 214 | } |
| 215 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 | [diff] [blame] | 216 | // Returns the demangled C++ symbol name for Name. |
| 217 | std::string elf2::demangle(StringRef Name) { |
| 218 | #if !defined(HAVE_CXXABI_H) |
| 219 | return Name; |
| 220 | #else |
| 221 | if (!Config->Demangle) |
| 222 | return Name; |
Rui Ueyama | 5fa978b | 2016-01-13 19:40:13 | [diff] [blame] | 223 | |
Rui Ueyama | df15451 | 2016-01-13 22:09:09 | [diff] [blame] | 224 | // __cxa_demangle can be used to demangle strings other than symbol |
| 225 | // names which do not necessarily start with "_Z". Name can be |
| 226 | // either a C or C++ symbol. Don't call __cxa_demangle if the name |
| 227 | // does not look like a C++ symbol name to avoid getting unexpected |
| 228 | // result for a C symbol that happens to match a mangled type name. |
Rui Ueyama | 5fa978b | 2016-01-13 19:40:13 | [diff] [blame] | 229 | if (!Name.startswith("_Z")) |
| 230 | return Name; |
| 231 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 | [diff] [blame] | 232 | char *Buf = |
| 233 | abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr); |
| 234 | if (!Buf) |
| 235 | return Name; |
| 236 | std::string S(Buf); |
| 237 | free(Buf); |
| 238 | return S; |
| 239 | #endif |
| 240 | } |
| 241 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 | [diff] [blame] | 242 | template uint32_t SymbolBody::template getVA<ELF32LE>() const; |
| 243 | template uint32_t SymbolBody::template getVA<ELF32BE>() const; |
| 244 | template uint64_t SymbolBody::template getVA<ELF64LE>() const; |
| 245 | template uint64_t SymbolBody::template getVA<ELF64BE>() const; |
| 246 | |
| 247 | template uint32_t SymbolBody::template getGotVA<ELF32LE>() const; |
| 248 | template uint32_t SymbolBody::template getGotVA<ELF32BE>() const; |
| 249 | template uint64_t SymbolBody::template getGotVA<ELF64LE>() const; |
| 250 | template uint64_t SymbolBody::template getGotVA<ELF64BE>() const; |
| 251 | |
| 252 | template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const; |
| 253 | template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const; |
| 254 | template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const; |
| 255 | template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const; |
| 256 | |
| 257 | template uint32_t SymbolBody::template getPltVA<ELF32LE>() const; |
| 258 | template uint32_t SymbolBody::template getPltVA<ELF32BE>() const; |
| 259 | template uint64_t SymbolBody::template getPltVA<ELF64LE>() const; |
| 260 | template uint64_t SymbolBody::template getPltVA<ELF64BE>() const; |
| 261 | |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 | [diff] [blame^] | 262 | template uint32_t SymbolBody::template getSize<ELF32LE>() const; |
| 263 | template uint32_t SymbolBody::template getSize<ELF32BE>() const; |
| 264 | template uint64_t SymbolBody::template getSize<ELF64LE>() const; |
| 265 | template uint64_t SymbolBody::template getSize<ELF64BE>() const; |
| 266 | |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 | [diff] [blame] | 267 | template int SymbolBody::compare<ELF32LE>(SymbolBody *Other); |
| 268 | template int SymbolBody::compare<ELF32BE>(SymbolBody *Other); |
| 269 | template int SymbolBody::compare<ELF64LE>(SymbolBody *Other); |
| 270 | template int SymbolBody::compare<ELF64BE>(SymbolBody *Other); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 | [diff] [blame] | 271 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 | [diff] [blame] | 272 | template class elf2::UndefinedElf<ELF32LE>; |
| 273 | template class elf2::UndefinedElf<ELF32BE>; |
| 274 | template class elf2::UndefinedElf<ELF64LE>; |
| 275 | template class elf2::UndefinedElf<ELF64BE>; |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 | [diff] [blame] | 276 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 | [diff] [blame] | 277 | template class elf2::DefinedSynthetic<ELF32LE>; |
| 278 | template class elf2::DefinedSynthetic<ELF32BE>; |
| 279 | template class elf2::DefinedSynthetic<ELF64LE>; |
| 280 | template class elf2::DefinedSynthetic<ELF64BE>; |