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 | 0bc0c02 | 2016-01-18 23:54:05 +0000 | [diff] [blame] | 42 | std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak()); |
| 43 | std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(), |
| 44 | !Other->isWeak()); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 45 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 46 | // Normalize |
| 47 | if (L > R) |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 48 | return -Other->compare<ELFT>(this); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 49 | |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 50 | Visibility = Other->Visibility = |
| 51 | getMinVisibility(Visibility, Other->Visibility); |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 52 | |
Rui Ueyama | 86696f3 | 2015-10-21 19:41:03 +0000 | [diff] [blame] | 53 | if (IsUsedInRegularObj || Other->IsUsedInRegularObj) |
| 54 | IsUsedInRegularObj = Other->IsUsedInRegularObj = true; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 55 | |
George Rimar | 02ca179 | 2016-01-25 08:44:38 +0000 | [diff] [blame^] | 56 | // 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 Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 63 | if (L != R) |
| 64 | return -1; |
Rafael Espindola | 0bc0c02 | 2016-01-18 23:54:05 +0000 | [diff] [blame] | 65 | if (!std::get<0>(L) || !std::get<1>(L) || !std::get<2>(L)) |
Rafael Espindola | 8e5560d | 2015-09-23 14:23:59 +0000 | [diff] [blame] | 66 | return 1; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 +0000 | [diff] [blame] | 67 | if (isCommon()) { |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 +0000 | [diff] [blame] | 68 | if (!Other->isCommon()) |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 69 | return -1; |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 70 | auto *ThisC = cast<DefinedCommon>(this); |
| 71 | auto *OtherC = cast<DefinedCommon>(Other); |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 +0000 | [diff] [blame] | 72 | uintX_t Align = std::max(ThisC->MaxAlignment, OtherC->MaxAlignment); |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 73 | if (ThisC->Size >= OtherC->Size) { |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 +0000 | [diff] [blame] | 74 | ThisC->MaxAlignment = Align; |
Rui Ueyama | 6666f6a | 2015-09-09 17:55:09 +0000 | [diff] [blame] | 75 | return 1; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 76 | } |
Rui Ueyama | 6be6852 | 2015-12-16 23:49:19 +0000 | [diff] [blame] | 77 | OtherC->MaxAlignment = Align; |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 +0000 | [diff] [blame] | 78 | return -1; |
Rafael Espindola | 30e1797 | 2015-08-30 23:17:30 +0000 | [diff] [blame] | 79 | } |
Rui Ueyama | 7da94a5 | 2015-09-09 17:40:51 +0000 | [diff] [blame] | 80 | if (Other->isCommon()) |
| 81 | return 1; |
| 82 | return 0; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 83 | } |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 84 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 85 | Defined::Defined(Kind K, StringRef Name, bool IsWeak, uint8_t Visibility, |
| 86 | bool IsTls) |
| 87 | : SymbolBody(K, Name, IsWeak, Visibility, IsTls) {} |
| 88 | |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 89 | Undefined::Undefined(SymbolBody::Kind K, StringRef N, bool IsWeak, |
| 90 | uint8_t Visibility, bool IsTls) |
| 91 | : SymbolBody(K, N, IsWeak, Visibility, IsTls), CanKeepUndefined(false) {} |
| 92 | |
| 93 | Undefined::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 | |
| 100 | template <typename ELFT> |
| 101 | UndefinedElf<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 Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 107 | template <typename ELFT> |
| 108 | DefinedSynthetic<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 Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 113 | DefinedCommon::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. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 120 | std::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 Ueyama | 533c030 | 2016-01-06 00:09:43 +0000 | [diff] [blame] | 127 | return createObjectFile(MBRef); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 +0000 | [diff] [blame] | 130 | template <class ELFT> static void doInitSymbols() { |
Rui Ueyama | a246e094 | 2015-12-25 06:12:18 +0000 | [diff] [blame] | 131 | ElfSym<ELFT>::End.setBinding(STB_GLOBAL); |
Rafael Espindola | 65e80b9 | 2016-01-19 21:19:52 +0000 | [diff] [blame] | 132 | ElfSym<ELFT>::Ignored.setBinding(STB_WEAK); |
| 133 | ElfSym<ELFT>::Ignored.setVisibility(STV_HIDDEN); |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 136 | void elf2::initSymbols() { |
Rui Ueyama | aca48ff | 2015-10-08 00:44:28 +0000 | [diff] [blame] | 137 | doInitSymbols<ELF32LE>(); |
| 138 | doInitSymbols<ELF32BE>(); |
| 139 | doInitSymbols<ELF64LE>(); |
| 140 | doInitSymbols<ELF64BE>(); |
Rui Ueyama | 9ea49c7 | 2015-10-07 23:46:11 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 143 | // Returns the demangled C++ symbol name for Name. |
| 144 | std::string elf2::demangle(StringRef Name) { |
| 145 | #if !defined(HAVE_CXXABI_H) |
| 146 | return Name; |
| 147 | #else |
| 148 | if (!Config->Demangle) |
| 149 | return Name; |
Rui Ueyama | 5fa978b | 2016-01-13 19:40:13 +0000 | [diff] [blame] | 150 | |
Rui Ueyama | df15451 | 2016-01-13 22:09:09 +0000 | [diff] [blame] | 151 | // __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 Ueyama | 5fa978b | 2016-01-13 19:40:13 +0000 | [diff] [blame] | 156 | if (!Name.startswith("_Z")) |
| 157 | return Name; |
| 158 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 159 | 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 Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 169 | template int SymbolBody::compare<ELF32LE>(SymbolBody *Other); |
| 170 | template int SymbolBody::compare<ELF32BE>(SymbolBody *Other); |
| 171 | template int SymbolBody::compare<ELF64LE>(SymbolBody *Other); |
| 172 | template int SymbolBody::compare<ELF64BE>(SymbolBody *Other); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 173 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 174 | template class elf2::UndefinedElf<ELF32LE>; |
| 175 | template class elf2::UndefinedElf<ELF32BE>; |
| 176 | template class elf2::UndefinedElf<ELF64LE>; |
| 177 | template class elf2::UndefinedElf<ELF64BE>; |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 178 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 179 | template class elf2::DefinedSynthetic<ELF32LE>; |
| 180 | template class elf2::DefinedSynthetic<ELF32BE>; |
| 181 | template class elf2::DefinedSynthetic<ELF64LE>; |
| 182 | template class elf2::DefinedSynthetic<ELF64BE>; |