Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [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 | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 11 | #include "InputSection.h" |
Rafael Espindola | 49a2ca6 | 2015-08-06 15:33:19 +0000 | [diff] [blame] | 12 | #include "Error.h" |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 14 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
| 17 | |
| 18 | #ifdef HAVE_CXXABI_H |
| 19 | #include <cxxabi.h> |
| 20 | #endif |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | using namespace llvm::object; |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 24 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace lld; |
| 27 | using namespace lld::elf2; |
| 28 | |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 29 | static 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. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 37 | // Returns 1, 0 or -1 if this symbol should take precedence |
| 38 | // over the Other, tie or lose, respectively. |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 39 | template <class ELFT> int SymbolBody::compare(SymbolBody *Other) { |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 +0000 | [diff] [blame] | 40 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 41 | assert(!isLazy() && !Other->isLazy()); |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 42 | std::pair<bool, bool> L(isDefined(), !isWeak()); |
| 43 | std::pair<bool, bool> R(Other->isDefined(), !Other->isWeak()); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 44 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 45 | // Normalize |
| 46 | if (L > R) |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 47 | return -Other->compare<ELFT>(this); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 48 | |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 49 | Visibility = Other->Visibility = |
| 50 | getMinVisibility(Visibility, Other->Visibility); |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 51 | |
Rui Ueyama | 86696f3 | 2015-10-21 19:41:03 +0000 | [diff] [blame] | 52 | if (IsUsedInRegularObj || Other->IsUsedInRegularObj) |
| 53 | IsUsedInRegularObj = Other->IsUsedInRegularObj = true; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 54 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 55 | if (L != R) |
| 56 | return -1; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 +0000 | [diff] [blame] | 57 | if (!L.first || !L.second) |
| 58 | return 1; |
Rafael Espindola | 8e5560d | 2015-09-23 14:23:59 +0000 | [diff] [blame] | 59 | if (isShared()) |
| 60 | return -1; |
| 61 | if (Other->isShared()) |
| 62 | return 1; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 +0000 | [diff] [blame] | 63 | if (isCommon()) { |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 +0000 | [diff] [blame] | 64 | if (!Other->isCommon()) |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 65 | return -1; |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 66 | auto *ThisC = cast<DefinedCommon>(this); |
| 67 | auto *OtherC = cast<DefinedCommon>(Other); |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 +0000 | [diff] [blame] | 68 | uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment); |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 69 | if (ThisC->Size >= OtherC->Size) { |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 +0000 | [diff] [blame] | 70 | ThisC->MaxAlignment = Align; |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 +0000 | [diff] [blame] | 71 | return 1; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 72 | } |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 +0000 | [diff] [blame] | 73 | OtherC->MaxAlignment = Align; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 +0000 | [diff] [blame] | 74 | return -1; |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 75 | } |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 +0000 | [diff] [blame] | 76 | if (Other->isCommon()) |
| 77 | return 1; |
| 78 | return 0; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 79 | } |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 80 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 81 | Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, |
| 82 | bool IsTls) |
| 83 | : SymbolBody(K, Name, IsWeak, Visibility, IsTls) {} |
| 84 | |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 85 | Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak, |
| 86 | uint8_t Visibility, bool IsTls) |
| 87 | : SymbolBody(K, N, IsWeak, Visibility, IsTls), CanKeepUndefined(false) {} |
| 88 | |
| 89 | Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility, |
| 90 | bool CanKeepUndefined) |
| 91 | : Undefined(SymbolBody::UndefinedKind, N, IsWeak, Visibility, |
| 92 | /*IsTls*/ false) { |
| 93 | this->CanKeepUndefined = CanKeepUndefined; |
| 94 | } |
| 95 | |
| 96 | template <typename ELFT> |
| 97 | UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym) |
| 98 | : Undefined(SymbolBody::UndefinedElfKind, N, |
| 99 | Sym.getBinding() == llvm::ELF::STB_WEAK, Sym.getVisibility(), |
| 100 | Sym.getType() == llvm::ELF::STT_TLS), |
| 101 | Sym(Sym) {} |
| 102 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 103 | template <typename ELFT> |
| 104 | DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value, |
| 105 | OutputSectionBase<ELFT> &Section) |
| 106 | : Defined(SymbolBody::DefinedSyntheticKind, N, false, STV_DEFAULT, false), |
| 107 | Value(Value), Section(Section) {} |
| 108 | |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 109 | DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, |
| 110 | bool IsWeak, uint8_t Visibility) |
| 111 | : Defined(SymbolBody::DefinedCommonKind, N, IsWeak, Visibility, false) { |
| 112 | MaxAlignment = Alignment; |
| 113 | this->Size = Size; |
| 114 | } |
| 115 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 116 | std::unique_ptr<InputFile> Lazy::getMember() { |
| 117 | MemoryBufferRef MBRef = File->getMember(&Sym); |
| 118 | |
| 119 | // getMember returns an empty buffer if the member was already |
| 120 | // read from the library. |
| 121 | if (MBRef.getBuffer().empty()) |
| 122 | return std::unique_ptr<InputFile>(nullptr); |
Rui Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 123 | return createObjectFile(MBRef); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 +0000 | [diff] [blame] | 126 | template <class ELFT> static void doInitSymbols() { |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 127 | ElfSym<ELFT>::End.setBinding(STB_GLOBAL); |
Rui Ueyama | 09eb0b3 | 2016-01-12 19:24:55 +0000 | [diff] [blame] | 128 | ElfSym<ELFT>::IgnoredWeak.setBinding(STB_WEAK); |
| 129 | ElfSym<ELFT>::IgnoredWeak.setVisibility(STV_HIDDEN); |
| 130 | ElfSym<ELFT>::Ignored.setBinding(STB_GLOBAL); |
| 131 | ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN); |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 134 | void elf2::initSymbols() { |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 +0000 | [diff] [blame] | 135 | doInitSymbols<ELF32LE>(); |
| 136 | doInitSymbols<ELF32BE>(); |
| 137 | doInitSymbols<ELF64LE>(); |
| 138 | doInitSymbols<ELF64BE>(); |
Rui Ueyama | 9ea49c7 | 2015-10-07 23:46:11 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 141 | // Returns the demangled C++ symbol name for Name. |
| 142 | std::string elf2::demangle(StringRef Name) { |
| 143 | #if !defined(HAVE_CXXABI_H) |
| 144 | return Name; |
| 145 | #else |
| 146 | if (!Config->Demangle) |
| 147 | return Name; |
Rui Ueyama | 5fa978b | 2016-01-13 19:40:13 +0000 | [diff] [blame] | 148 | |
Rui Ueyama | e16d1b0 | 2016-01-13 21:39:40 +0000 | [diff] [blame] | 149 | // Don't call __cxa_demangle if the name does not look like a C++ |
| 150 | // symbol name. We need this check because some implementations of the |
| 151 | // function try to demangle a name as something different (e.g. type name) |
| 152 | // if it is not a mangled symbol name. |
Rui Ueyama | 5fa978b | 2016-01-13 19:40:13 +0000 | [diff] [blame] | 153 | if (!Name.startswith("_Z")) |
| 154 | return Name; |
| 155 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 156 | char *Buf = |
| 157 | abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr); |
| 158 | if (!Buf) |
| 159 | return Name; |
| 160 | std::string S(Buf); |
| 161 | free(Buf); |
| 162 | return S; |
| 163 | #endif |
| 164 | } |
| 165 | |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 166 | template int SymbolBody::compare<ELF32LE>(SymbolBody *Other); |
| 167 | template int SymbolBody::compare<ELF32BE>(SymbolBody *Other); |
| 168 | template int SymbolBody::compare<ELF64LE>(SymbolBody *Other); |
| 169 | template int SymbolBody::compare<ELF64BE>(SymbolBody *Other); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 170 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 171 | template class elf2::UndefinedElf<ELF32LE>; |
| 172 | template class elf2::UndefinedElf<ELF32BE>; |
| 173 | template class elf2::UndefinedElf<ELF64LE>; |
| 174 | template class elf2::UndefinedElf<ELF64BE>; |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 175 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 176 | template class elf2::DefinedSynthetic<ELF32LE>; |
| 177 | template class elf2::DefinedSynthetic<ELF32BE>; |
| 178 | template class elf2::DefinedSynthetic<ELF64LE>; |
| 179 | template class elf2::DefinedSynthetic<ELF64BE>; |