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 | 49a2ca6 | 2015-08-06 15:33:19 +0000 | [diff] [blame] | 11 | #include "Error.h" |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 12 | #include "InputFiles.h" |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 13 | #include "InputSection.h" |
| 14 | #include "OutputSections.h" |
| 15 | #include "Target.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [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 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 25 | using namespace llvm::object; |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 26 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 29 | using namespace lld::elf; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 30 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 31 | template <class ELFT> |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 32 | static typename ELFT::uint getSymVA(const SymbolBody &Body, |
| 33 | typename ELFT::uint &Addend) { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 34 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 35 | |
| 36 | switch (Body.kind()) { |
| 37 | case SymbolBody::DefinedSyntheticKind: { |
| 38 | auto &D = cast<DefinedSynthetic<ELFT>>(Body); |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 39 | if (D.Value == DefinedSynthetic<ELFT>::SectionEnd) |
| 40 | return D.Section.getVA() + D.Section.getSize(); |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 41 | return D.Section.getVA() + D.Value; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 42 | } |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 43 | case SymbolBody::DefinedRegularKind: { |
| 44 | auto &D = cast<DefinedRegular<ELFT>>(Body); |
| 45 | InputSectionBase<ELFT> *SC = D.Section; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 46 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 47 | // According to the ELF spec reference to a local symbol from outside |
| 48 | // the group are not allowed. Unfortunately .eh_frame breaks that rule |
| 49 | // and must be treated specially. For now we just replace the symbol with |
| 50 | // 0. |
| 51 | if (SC == &InputSection<ELFT>::Discarded) |
| 52 | return 0; |
| 53 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 54 | // This is an absolute symbol. |
| 55 | if (!SC) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 56 | return D.Value; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 57 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 58 | uintX_t Offset = D.Value; |
| 59 | if (D.isSection()) { |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 60 | Offset += Addend; |
| 61 | Addend = 0; |
| 62 | } |
| 63 | uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 64 | if (D.isTls()) |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 65 | return VA - Out<ELFT>::TlsPhdr->p_vaddr; |
| 66 | return VA; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 67 | } |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 68 | case SymbolBody::DefinedCommonKind: |
| 69 | return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(Body).OffsetInBss; |
| 70 | case SymbolBody::SharedKind: { |
| 71 | auto &SS = cast<SharedSymbol<ELFT>>(Body); |
| 72 | if (!SS.NeedsCopyOrPltAddr) |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 73 | return 0; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 74 | if (SS.isFunc()) |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 75 | return Body.getPltVA<ELFT>(); |
Rui Ueyama | 2df7289 | 2016-03-13 20:54:38 +0000 | [diff] [blame] | 76 | return Out<ELFT>::Bss->getVA() + SS.OffsetInBss; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 77 | } |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 78 | case SymbolBody::UndefinedElfKind: |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 79 | case SymbolBody::UndefinedBitcodeKind: |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 80 | return 0; |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 81 | case SymbolBody::LazyArchiveKind: |
| 82 | case SymbolBody::LazyObjectKind: |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 83 | assert(Body.Backref->IsUsedInRegularObj && "lazy symbol reached writer"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 84 | return 0; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 85 | case SymbolBody::DefinedBitcodeKind: |
Davide Italiano | f6523ae | 2016-03-29 02:20:10 +0000 | [diff] [blame] | 86 | llvm_unreachable("should have been replaced"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 87 | } |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 88 | llvm_unreachable("invalid symbol kind"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 91 | SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, |
| 92 | uint8_t Type) |
Rafael Espindola | 5e34568 | 2016-04-06 14:31:03 +0000 | [diff] [blame] | 93 | : SymbolKind(K), Type(Type), Binding(STB_LOCAL), StOther(StOther), |
| 94 | NameOffset(NameOffset) { |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 95 | init(); |
| 96 | } |
| 97 | |
| 98 | SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther, |
| 99 | uint8_t Type) |
Rafael Espindola | 5e34568 | 2016-04-06 14:31:03 +0000 | [diff] [blame] | 100 | : SymbolKind(K), Type(Type), Binding(Binding), StOther(StOther), |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 101 | Name({Name.data(), Name.size()}) { |
| 102 | assert(!isLocal()); |
| 103 | init(); |
| 104 | } |
| 105 | |
| 106 | void SymbolBody::init() { |
Rafael Espindola | 5e34568 | 2016-04-06 14:31:03 +0000 | [diff] [blame] | 107 | CanKeepUndefined = false; |
Rafael Espindola | 5e34568 | 2016-04-06 14:31:03 +0000 | [diff] [blame] | 108 | NeedsCopyOrPltAddr = false; |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 109 | CanOmitFromDynSym = false; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 112 | // Returns true if a symbol can be replaced at load-time by a symbol |
| 113 | // with the same name defined in other ELF executable or DSO. |
| 114 | bool SymbolBody::isPreemptible() const { |
| 115 | if (isLocal()) |
| 116 | return false; |
| 117 | |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame^] | 118 | // Shared symbols resolve to the definition in the DSO. |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 119 | if (isShared()) |
| 120 | return true; |
| 121 | |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame^] | 122 | // That's all that can be preempted in a non-DSO. |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 123 | if (!Config->Shared) |
| 124 | return false; |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame^] | 125 | |
| 126 | // Undefined symbols in DSOs can only be preempted if they are strong. |
| 127 | // Weak symbols just resolve to zero. |
| 128 | if (isUndefined()) |
| 129 | return !isWeak(); |
| 130 | |
| 131 | // -Bsymbolic means that not even default visibility symbols can be preempted. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 132 | if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc())) |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 133 | return false; |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame^] | 134 | |
| 135 | // Only default visibility symbols that appear in the dynamic symbol table can |
| 136 | // be preempted. |
| 137 | return Backref->Visibility == STV_DEFAULT && Backref->includeInDynsym(); |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 140 | template <class ELFT> |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 141 | typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const { |
Rafael Espindola | 8381c56 | 2016-03-17 23:36:19 +0000 | [diff] [blame] | 142 | typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend); |
| 143 | return OutVA + Addend; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 146 | template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const { |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 147 | return Out<ELFT>::Got->getVA() + getGotOffset<ELFT>(); |
| 148 | } |
| 149 | |
| 150 | template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const { |
| 151 | return (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) * |
| 152 | sizeof(typename ELFT::uint); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 155 | template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const { |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 156 | return Out<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>(); |
| 157 | } |
| 158 | |
| 159 | template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const { |
| 160 | return GotPltIndex * sizeof(typename ELFT::uint); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 163 | template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const { |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 164 | return Out<ELFT>::Plt->getVA() + Target->PltZeroSize + |
| 165 | PltIndex * Target->PltEntrySize; |
| 166 | } |
| 167 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 168 | template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const { |
| 169 | auto *D = cast<DefinedRegular<ELFT>>(this); |
| 170 | auto *S = cast<InputSection<ELFT>>(D->Section); |
| 171 | return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() + |
| 172 | ThunkIndex * Target->ThunkSize; |
| 173 | } |
| 174 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 175 | template <class ELFT> typename ELFT::uint SymbolBody::getSize() const { |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 176 | if (const auto *C = dyn_cast<DefinedCommon>(this)) |
| 177 | return C->Size; |
| 178 | if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) |
| 179 | return DR->Size; |
| 180 | if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) |
| 181 | return S->Sym.st_size; |
| 182 | if (const auto *U = dyn_cast<UndefinedElf<ELFT>>(this)) |
| 183 | return U->Size; |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 187 | // Returns 1, 0 or -1 if this symbol should take precedence |
| 188 | // over the Other, tie or lose, respectively. |
Peter Collingbourne | d0856a6 | 2016-04-05 00:47:58 +0000 | [diff] [blame] | 189 | int SymbolBody::compare(SymbolBody *Other) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 190 | assert(!isLazy() && !Other->isLazy()); |
Rafael Espindola | 0bc0c02 | 2016-01-18 23:54:05 +0000 | [diff] [blame] | 191 | std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak()); |
| 192 | std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(), |
| 193 | !Other->isWeak()); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 194 | |
Rui Ueyama | 8bf7106 | 2016-04-22 19:34:59 +0000 | [diff] [blame] | 195 | // Compare the two by symbol type. |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 196 | if (L > R) |
Peter Collingbourne | d0856a6 | 2016-04-05 00:47:58 +0000 | [diff] [blame] | 197 | return -Other->compare(this); |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 198 | if (L != R) |
| 199 | return -1; |
George Rimar | 3498c7f | 2016-03-10 18:49:24 +0000 | [diff] [blame] | 200 | if (!isDefined() || isShared() || isWeak()) |
Rafael Espindola | 8e5560d | 2015-09-23 14:23:59 +0000 | [diff] [blame] | 201 | return 1; |
Rui Ueyama | 8bf7106 | 2016-04-22 19:34:59 +0000 | [diff] [blame] | 202 | |
| 203 | // If both are equal in terms of symbol type, then at least |
| 204 | // one of them must be a common symbol. Otherwise, they conflict. |
| 205 | auto *A = dyn_cast<DefinedCommon>(this); |
| 206 | auto *B = dyn_cast<DefinedCommon>(Other); |
| 207 | if (!A && !B) |
George Rimar | 3498c7f | 2016-03-10 18:49:24 +0000 | [diff] [blame] | 208 | return 0; |
Rui Ueyama | 8bf7106 | 2016-04-22 19:34:59 +0000 | [diff] [blame] | 209 | |
| 210 | // If both are common, the larger one is chosen. |
| 211 | if (A && B) { |
| 212 | if (Config->WarnCommon) |
| 213 | warning("multiple common of " + A->getName()); |
| 214 | A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment); |
| 215 | return A->Size < B->Size ? -1 : 1; |
| 216 | } |
| 217 | |
| 218 | // Non-common symbols takes precedence over common symbols. |
George Rimar | 3435800 | 2016-03-14 09:19:30 +0000 | [diff] [blame] | 219 | if (Config->WarnCommon) |
| 220 | warning("common " + this->getName() + " is overridden"); |
Rui Ueyama | 8bf7106 | 2016-04-22 19:34:59 +0000 | [diff] [blame] | 221 | return A ? -1 : 1; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 222 | } |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 223 | |
Rafael Espindola | f9b79a4 | 2016-04-06 12:14:31 +0000 | [diff] [blame] | 224 | Defined::Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t StOther, |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 225 | uint8_t Type) |
Rafael Espindola | f9b79a4 | 2016-04-06 12:14:31 +0000 | [diff] [blame] | 226 | : SymbolBody(K, Name, Binding, StOther, Type) {} |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 227 | |
Rafael Espindola | f9b79a4 | 2016-04-06 12:14:31 +0000 | [diff] [blame] | 228 | Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type) |
| 229 | : SymbolBody(K, NameOffset, StOther, Type) {} |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 230 | |
Rafael Espindola | f9b79a4 | 2016-04-06 12:14:31 +0000 | [diff] [blame] | 231 | DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t StOther) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 232 | : Defined(DefinedBitcodeKind, Name, IsWeak ? STB_WEAK : STB_GLOBAL, |
Rafael Espindola | f9b79a4 | 2016-04-06 12:14:31 +0000 | [diff] [blame] | 233 | StOther, 0 /* Type */) {} |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 234 | |
| 235 | bool DefinedBitcode::classof(const SymbolBody *S) { |
| 236 | return S->kind() == DefinedBitcodeKind; |
| 237 | } |
| 238 | |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 239 | UndefinedBitcode::UndefinedBitcode(StringRef N, bool IsWeak, uint8_t StOther) |
| 240 | : SymbolBody(SymbolBody::UndefinedBitcodeKind, N, |
| 241 | IsWeak ? STB_WEAK : STB_GLOBAL, StOther, 0 /* Type */) {} |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 242 | |
| 243 | template <typename ELFT> |
| 244 | UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym) |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 245 | : SymbolBody(SymbolBody::UndefinedElfKind, N, Sym.getBinding(), |
| 246 | Sym.st_other, Sym.getType()), |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 247 | Size(Sym.st_size) {} |
| 248 | |
| 249 | template <typename ELFT> |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 250 | UndefinedElf<ELFT>::UndefinedElf(StringRef Name, uint8_t Binding, |
| 251 | uint8_t StOther, uint8_t Type, |
| 252 | bool CanKeepUndefined) |
Rafael Espindola | 5e34568 | 2016-04-06 14:31:03 +0000 | [diff] [blame] | 253 | : SymbolBody(SymbolBody::UndefinedElfKind, Name, Binding, StOther, Type) { |
| 254 | this->CanKeepUndefined = CanKeepUndefined; |
| 255 | } |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 256 | |
| 257 | template <typename ELFT> |
Rafael Espindola | d9a1717 | 2016-04-05 11:47:46 +0000 | [diff] [blame] | 258 | UndefinedElf<ELFT>::UndefinedElf(const Elf_Sym &Sym) |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 259 | : SymbolBody(SymbolBody::UndefinedElfKind, Sym.st_name, Sym.st_other, |
| 260 | Sym.getType()), |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 261 | Size(Sym.st_size) { |
| 262 | assert(Sym.getBinding() == STB_LOCAL); |
| 263 | } |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 264 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 265 | template <typename ELFT> |
| 266 | DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value, |
Peter Collingbourne | f6e9b4e | 2016-04-13 16:57:28 +0000 | [diff] [blame] | 267 | OutputSectionBase<ELFT> &Section) |
| 268 | : Defined(SymbolBody::DefinedSyntheticKind, N, STB_GLOBAL, STV_HIDDEN, |
Davide Italiano | 255730c | 2016-03-04 01:55:28 +0000 | [diff] [blame] | 269 | 0 /* Type */), |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 270 | Value(Value), Section(Section) {} |
| 271 | |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 272 | DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, |
Rafael Espindola | f9b79a4 | 2016-04-06 12:14:31 +0000 | [diff] [blame] | 273 | uint8_t Binding, uint8_t StOther, uint8_t Type) |
| 274 | : Defined(SymbolBody::DefinedCommonKind, N, Binding, StOther, Type), |
Rui Ueyama | 17d6983 | 2016-03-10 18:58:53 +0000 | [diff] [blame] | 275 | Alignment(Alignment), Size(Size) {} |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 276 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 277 | std::unique_ptr<InputFile> Lazy::getFile() { |
| 278 | if (auto *S = dyn_cast<LazyArchive>(this)) |
| 279 | return S->getFile(); |
| 280 | return cast<LazyObject>(this)->getFile(); |
| 281 | } |
| 282 | |
| 283 | std::unique_ptr<InputFile> LazyArchive::getFile() { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 284 | MemoryBufferRef MBRef = File->getMember(&Sym); |
| 285 | |
| 286 | // getMember returns an empty buffer if the member was already |
| 287 | // read from the library. |
| 288 | if (MBRef.getBuffer().empty()) |
| 289 | return std::unique_ptr<InputFile>(nullptr); |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 290 | return createObjectFile(MBRef, File->getName()); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 293 | std::unique_ptr<InputFile> LazyObject::getFile() { |
| 294 | return createObjectFile(MBRef); |
| 295 | } |
| 296 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 297 | // Returns the demangled C++ symbol name for Name. |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 298 | std::string elf::demangle(StringRef Name) { |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 299 | #if !defined(HAVE_CXXABI_H) |
| 300 | return Name; |
| 301 | #else |
| 302 | if (!Config->Demangle) |
| 303 | return Name; |
Rui Ueyama | 5fa978b | 2016-01-13 19:40:13 +0000 | [diff] [blame] | 304 | |
Rui Ueyama | df15451 | 2016-01-13 22:09:09 +0000 | [diff] [blame] | 305 | // __cxa_demangle can be used to demangle strings other than symbol |
| 306 | // names which do not necessarily start with "_Z". Name can be |
| 307 | // either a C or C++ symbol. Don't call __cxa_demangle if the name |
| 308 | // does not look like a C++ symbol name to avoid getting unexpected |
| 309 | // 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] | 310 | if (!Name.startswith("_Z")) |
| 311 | return Name; |
| 312 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 313 | char *Buf = |
| 314 | abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr); |
| 315 | if (!Buf) |
| 316 | return Name; |
| 317 | std::string S(Buf); |
| 318 | free(Buf); |
| 319 | return S; |
| 320 | #endif |
| 321 | } |
| 322 | |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 323 | bool Symbol::includeInDynsym() const { |
| 324 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 325 | return false; |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame^] | 326 | return (ExportDynamic && VersionScriptGlobal) || Body->isShared(); |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 329 | template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const; |
| 330 | template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const; |
| 331 | template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const; |
| 332 | template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 333 | |
| 334 | template uint32_t SymbolBody::template getGotVA<ELF32LE>() const; |
| 335 | template uint32_t SymbolBody::template getGotVA<ELF32BE>() const; |
| 336 | template uint64_t SymbolBody::template getGotVA<ELF64LE>() const; |
| 337 | template uint64_t SymbolBody::template getGotVA<ELF64BE>() const; |
| 338 | |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 339 | template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const; |
| 340 | template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const; |
| 341 | template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const; |
| 342 | template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const; |
| 343 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 344 | template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const; |
| 345 | template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const; |
| 346 | template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const; |
| 347 | template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const; |
| 348 | |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 349 | template uint32_t SymbolBody::template getGotPltOffset<ELF32LE>() const; |
| 350 | template uint32_t SymbolBody::template getGotPltOffset<ELF32BE>() const; |
| 351 | template uint64_t SymbolBody::template getGotPltOffset<ELF64LE>() const; |
| 352 | template uint64_t SymbolBody::template getGotPltOffset<ELF64BE>() const; |
| 353 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 354 | template uint32_t SymbolBody::template getPltVA<ELF32LE>() const; |
| 355 | template uint32_t SymbolBody::template getPltVA<ELF32BE>() const; |
| 356 | template uint64_t SymbolBody::template getPltVA<ELF64LE>() const; |
| 357 | template uint64_t SymbolBody::template getPltVA<ELF64BE>() const; |
| 358 | |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 +0000 | [diff] [blame] | 359 | template uint32_t SymbolBody::template getSize<ELF32LE>() const; |
| 360 | template uint32_t SymbolBody::template getSize<ELF32BE>() const; |
| 361 | template uint64_t SymbolBody::template getSize<ELF64LE>() const; |
| 362 | template uint64_t SymbolBody::template getSize<ELF64BE>() const; |
| 363 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 364 | template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const; |
| 365 | template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const; |
| 366 | template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const; |
| 367 | template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const; |
| 368 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 369 | template class elf::UndefinedElf<ELF32LE>; |
| 370 | template class elf::UndefinedElf<ELF32BE>; |
| 371 | template class elf::UndefinedElf<ELF64LE>; |
| 372 | template class elf::UndefinedElf<ELF64BE>; |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 373 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 374 | template class elf::DefinedSynthetic<ELF32LE>; |
| 375 | template class elf::DefinedSynthetic<ELF32BE>; |
| 376 | template class elf::DefinedSynthetic<ELF64LE>; |
| 377 | template class elf::DefinedSynthetic<ELF64BE>; |