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: |
| 79 | case SymbolBody::UndefinedKind: |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 80 | return 0; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 81 | case SymbolBody::LazyKind: |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 82 | assert(Body.isUsedInRegularObj() && "lazy symbol reached writer"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 83 | return 0; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 84 | case SymbolBody::DefinedBitcodeKind: |
Davide Italiano | f6523ae | 2016-03-29 02:20:10 +0000 | [diff] [blame] | 85 | llvm_unreachable("should have been replaced"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 86 | } |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 87 | llvm_unreachable("invalid symbol kind"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame^] | 90 | SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, |
| 91 | uint8_t Type) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 92 | : SymbolKind(K), MustBeInDynSym(false), NeedsCopyOrPltAddr(false), |
Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame^] | 93 | Type(Type), Binding(STB_LOCAL), StOther(StOther), NameOffset(NameOffset) { |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 94 | IsUsedInRegularObj = |
| 95 | K != SharedKind && K != LazyKind && K != DefinedBitcodeKind; |
| 96 | } |
| 97 | |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 98 | // Returns true if a symbol can be replaced at load-time by a symbol |
| 99 | // with the same name defined in other ELF executable or DSO. |
| 100 | bool SymbolBody::isPreemptible() const { |
| 101 | if (isLocal()) |
| 102 | return false; |
| 103 | |
| 104 | if (isShared()) |
| 105 | return true; |
| 106 | |
| 107 | if (isUndefined()) { |
| 108 | if (!isWeak()) |
| 109 | return true; |
| 110 | |
| 111 | // Ideally the static linker should see a definition for every symbol, but |
| 112 | // shared object are normally allowed to have undefined references that the |
| 113 | // static linker never sees a definition for. |
| 114 | if (Config->Shared) |
| 115 | return true; |
| 116 | |
| 117 | // Otherwise, just resolve to 0. |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if (!Config->Shared) |
| 122 | return false; |
| 123 | if (getVisibility() != STV_DEFAULT) |
| 124 | return false; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 125 | if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc())) |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 126 | return false; |
| 127 | return true; |
| 128 | } |
| 129 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 130 | template <class ELFT> |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 131 | typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const { |
Rafael Espindola | 8381c56 | 2016-03-17 23:36:19 +0000 | [diff] [blame] | 132 | typename ELFT::uint OutVA = getSymVA<ELFT>(*this, Addend); |
| 133 | return OutVA + Addend; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 136 | template <class ELFT> typename ELFT::uint SymbolBody::getGotVA() const { |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 137 | return Out<ELFT>::Got->getVA() + |
| 138 | (Out<ELFT>::Got->getMipsLocalEntriesNum() + GotIndex) * |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 139 | sizeof(typename ELFT::uint); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 142 | template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const { |
| 143 | return Out<ELFT>::GotPlt->getVA() + GotPltIndex * sizeof(typename ELFT::uint); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +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::getPltVA() const { |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 147 | return Out<ELFT>::Plt->getVA() + Target->PltZeroSize + |
| 148 | PltIndex * Target->PltEntrySize; |
| 149 | } |
| 150 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 151 | template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const { |
| 152 | auto *D = cast<DefinedRegular<ELFT>>(this); |
| 153 | auto *S = cast<InputSection<ELFT>>(D->Section); |
| 154 | return S->OutSec->getVA() + S->OutSecOff + S->getThunkOff() + |
| 155 | ThunkIndex * Target->ThunkSize; |
| 156 | } |
| 157 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 158 | template <class ELFT> typename ELFT::uint SymbolBody::getSize() const { |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 159 | if (const auto *C = dyn_cast<DefinedCommon>(this)) |
| 160 | return C->Size; |
| 161 | if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) |
| 162 | return DR->Size; |
| 163 | if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) |
| 164 | return S->Sym.st_size; |
| 165 | if (const auto *U = dyn_cast<UndefinedElf<ELFT>>(this)) |
| 166 | return U->Size; |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 +0000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 170 | static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { |
| 171 | if (VA == STV_DEFAULT) |
| 172 | return VB; |
| 173 | if (VB == STV_DEFAULT) |
| 174 | return VA; |
| 175 | return std::min(VA, VB); |
| 176 | } |
| 177 | |
George Rimar | 3498c7f | 2016-03-10 18:49:24 +0000 | [diff] [blame] | 178 | static int compareCommons(DefinedCommon *A, DefinedCommon *B) { |
George Rimar | 3435800 | 2016-03-14 09:19:30 +0000 | [diff] [blame] | 179 | if (Config->WarnCommon) |
| 180 | warning("multiple common of " + A->getName()); |
Rui Ueyama | 17d6983 | 2016-03-10 18:58:53 +0000 | [diff] [blame] | 181 | A->Alignment = B->Alignment = std::max(A->Alignment, B->Alignment); |
Davide Italiano | 901de03 | 2016-03-21 22:44:24 +0000 | [diff] [blame] | 182 | return A->Size < B->Size ? -1 : 1; |
George Rimar | 3498c7f | 2016-03-10 18:49:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 185 | // Returns 1, 0 or -1 if this symbol should take precedence |
| 186 | // over the Other, tie or lose, respectively. |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 187 | template <class ELFT> int SymbolBody::compare(SymbolBody *Other) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 188 | assert(!isLazy() && !Other->isLazy()); |
Rafael Espindola | 0bc0c02 | 2016-01-18 23:54:05 +0000 | [diff] [blame] | 189 | std::tuple<bool, bool, bool> L(isDefined(), !isShared(), !isWeak()); |
| 190 | std::tuple<bool, bool, bool> R(Other->isDefined(), !Other->isShared(), |
| 191 | !Other->isWeak()); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 192 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 193 | // Normalize |
| 194 | if (L > R) |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 195 | return -Other->compare<ELFT>(this); |
Rui Ueyama | a7ccb29 | 2015-07-27 20:39:01 +0000 | [diff] [blame] | 196 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 197 | uint8_t V = getMinVisibility(getVisibility(), Other->getVisibility()); |
| 198 | setVisibility(V); |
| 199 | Other->setVisibility(V); |
Rafael Espindola | 78471f0 | 2015-09-01 23:12:52 +0000 | [diff] [blame] | 200 | |
Rui Ueyama | 86696f3 | 2015-10-21 19:41:03 +0000 | [diff] [blame] | 201 | if (IsUsedInRegularObj || Other->IsUsedInRegularObj) |
| 202 | IsUsedInRegularObj = Other->IsUsedInRegularObj = true; |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 203 | |
George Rimar | 02ca179 | 2016-01-25 08:44:38 +0000 | [diff] [blame] | 204 | // We want to export all symbols that exist both in the executable |
| 205 | // and in DSOs, so that the symbols in the executable can interrupt |
| 206 | // symbols in the DSO at runtime. |
| 207 | if (isShared() != Other->isShared()) |
| 208 | if (isa<DefinedRegular<ELFT>>(isShared() ? Other : this)) |
Rafael Espindola | abebed9 | 2016-02-05 15:27:15 +0000 | [diff] [blame] | 209 | MustBeInDynSym = Other->MustBeInDynSym = true; |
George Rimar | 02ca179 | 2016-01-25 08:44:38 +0000 | [diff] [blame] | 210 | |
Rafael Espindola | 3a63f3f | 2015-08-28 20:19:34 +0000 | [diff] [blame] | 211 | if (L != R) |
| 212 | return -1; |
George Rimar | 3498c7f | 2016-03-10 18:49:24 +0000 | [diff] [blame] | 213 | if (!isDefined() || isShared() || isWeak()) |
Rafael Espindola | 8e5560d | 2015-09-23 14:23:59 +0000 | [diff] [blame] | 214 | return 1; |
George Rimar | 3498c7f | 2016-03-10 18:49:24 +0000 | [diff] [blame] | 215 | if (!isCommon() && !Other->isCommon()) |
| 216 | return 0; |
| 217 | if (isCommon() && Other->isCommon()) |
| 218 | return compareCommons(cast<DefinedCommon>(this), |
| 219 | cast<DefinedCommon>(Other)); |
George Rimar | 3435800 | 2016-03-14 09:19:30 +0000 | [diff] [blame] | 220 | if (Config->WarnCommon) |
| 221 | warning("common " + this->getName() + " is overridden"); |
George Rimar | 3498c7f | 2016-03-10 18:49:24 +0000 | [diff] [blame] | 222 | return isCommon() ? -1 : 1; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 223 | } |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 224 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 225 | Defined::Defined(Kind K, StringRef Name, uint8_t Binding, uint8_t Visibility, |
| 226 | uint8_t Type) |
| 227 | : SymbolBody(K, Name, Binding, Visibility, Type) {} |
| 228 | |
| 229 | Defined::Defined(Kind K, uint32_t NameOffset, uint8_t Visibility, uint8_t Type) |
| 230 | : SymbolBody(K, NameOffset, Visibility, Type) {} |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 231 | |
Rafael Espindola | 4f29c1a | 2016-03-07 17:14:36 +0000 | [diff] [blame] | 232 | DefinedBitcode::DefinedBitcode(StringRef Name, bool IsWeak, uint8_t Visibility) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 233 | : Defined(DefinedBitcodeKind, Name, IsWeak ? STB_WEAK : STB_GLOBAL, |
| 234 | Visibility, 0 /* Type */) {} |
Rafael Espindola | 9f77ef0 | 2016-02-12 20:54:57 +0000 | [diff] [blame] | 235 | |
| 236 | bool DefinedBitcode::classof(const SymbolBody *S) { |
| 237 | return S->kind() == DefinedBitcodeKind; |
| 238 | } |
| 239 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 240 | Undefined::Undefined(SymbolBody::Kind K, StringRef N, uint8_t Binding, |
| 241 | uint8_t Other, uint8_t Type) |
| 242 | : SymbolBody(K, N, Binding, Other, Type), CanKeepUndefined(false) {} |
| 243 | |
| 244 | Undefined::Undefined(SymbolBody::Kind K, uint32_t NameOffset, |
Davide Italiano | 255730c | 2016-03-04 01:55:28 +0000 | [diff] [blame] | 245 | uint8_t Visibility, uint8_t Type) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 246 | : SymbolBody(K, NameOffset, Visibility, Type), CanKeepUndefined(false) {} |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 247 | |
| 248 | Undefined::Undefined(StringRef N, bool IsWeak, uint8_t Visibility, |
| 249 | bool CanKeepUndefined) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 250 | : Undefined(SymbolBody::UndefinedKind, N, IsWeak ? STB_WEAK : STB_GLOBAL, |
| 251 | Visibility, 0 /* Type */) { |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 252 | this->CanKeepUndefined = CanKeepUndefined; |
| 253 | } |
| 254 | |
| 255 | template <typename ELFT> |
| 256 | UndefinedElf<ELFT>::UndefinedElf(StringRef N, const Elf_Sym &Sym) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 257 | : Undefined(SymbolBody::UndefinedElfKind, N, Sym.getBinding(), Sym.st_other, |
Davide Italiano | 255730c | 2016-03-04 01:55:28 +0000 | [diff] [blame] | 258 | Sym.getType()), |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 259 | Size(Sym.st_size) {} |
| 260 | |
| 261 | template <typename ELFT> |
| 262 | UndefinedElf<ELFT>::UndefinedElf(uint32_t NameOffset, const Elf_Sym &Sym) |
| 263 | : Undefined(SymbolBody::UndefinedElfKind, NameOffset, Sym.st_other, |
| 264 | Sym.getType()), |
| 265 | Size(Sym.st_size) { |
| 266 | assert(Sym.getBinding() == STB_LOCAL); |
| 267 | } |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 268 | |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 269 | template <typename ELFT> |
| 270 | DefinedSynthetic<ELFT>::DefinedSynthetic(StringRef N, uintX_t Value, |
George Rimar | aa4dc20 | 2016-03-01 16:23:13 +0000 | [diff] [blame] | 271 | OutputSectionBase<ELFT> &Section, |
| 272 | uint8_t Visibility) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 273 | : Defined(SymbolBody::DefinedSyntheticKind, N, STB_GLOBAL, Visibility, |
Davide Italiano | 255730c | 2016-03-04 01:55:28 +0000 | [diff] [blame] | 274 | 0 /* Type */), |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 275 | Value(Value), Section(Section) {} |
| 276 | |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 277 | DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 278 | uint8_t Binding, uint8_t Visibility, uint8_t Type) |
| 279 | : Defined(SymbolBody::DefinedCommonKind, N, Binding, Visibility, Type), |
Rui Ueyama | 17d6983 | 2016-03-10 18:58:53 +0000 | [diff] [blame] | 280 | Alignment(Alignment), Size(Size) {} |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 281 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 282 | std::unique_ptr<InputFile> Lazy::getMember() { |
| 283 | MemoryBufferRef MBRef = File->getMember(&Sym); |
| 284 | |
| 285 | // getMember returns an empty buffer if the member was already |
| 286 | // read from the library. |
| 287 | if (MBRef.getBuffer().empty()) |
| 288 | return std::unique_ptr<InputFile>(nullptr); |
Rui Ueyama | 71c066d | 2016-02-02 08:22:41 +0000 | [diff] [blame] | 289 | return createObjectFile(MBRef, File->getName()); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 292 | // Returns the demangled C++ symbol name for Name. |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 293 | std::string elf::demangle(StringRef Name) { |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 294 | #if !defined(HAVE_CXXABI_H) |
| 295 | return Name; |
| 296 | #else |
| 297 | if (!Config->Demangle) |
| 298 | return Name; |
Rui Ueyama | 5fa978b | 2016-01-13 19:40:13 +0000 | [diff] [blame] | 299 | |
Rui Ueyama | df15451 | 2016-01-13 22:09:09 +0000 | [diff] [blame] | 300 | // __cxa_demangle can be used to demangle strings other than symbol |
| 301 | // names which do not necessarily start with "_Z". Name can be |
| 302 | // either a C or C++ symbol. Don't call __cxa_demangle if the name |
| 303 | // does not look like a C++ symbol name to avoid getting unexpected |
| 304 | // 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] | 305 | if (!Name.startswith("_Z")) |
| 306 | return Name; |
| 307 | |
Rui Ueyama | a4a628f | 2016-01-13 18:55:39 +0000 | [diff] [blame] | 308 | char *Buf = |
| 309 | abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr); |
| 310 | if (!Buf) |
| 311 | return Name; |
| 312 | std::string S(Buf); |
| 313 | free(Buf); |
| 314 | return S; |
| 315 | #endif |
| 316 | } |
| 317 | |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 318 | template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const; |
| 319 | template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const; |
| 320 | template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const; |
| 321 | template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 322 | |
| 323 | template uint32_t SymbolBody::template getGotVA<ELF32LE>() const; |
| 324 | template uint32_t SymbolBody::template getGotVA<ELF32BE>() const; |
| 325 | template uint64_t SymbolBody::template getGotVA<ELF64LE>() const; |
| 326 | template uint64_t SymbolBody::template getGotVA<ELF64BE>() const; |
| 327 | |
| 328 | template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const; |
| 329 | template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const; |
| 330 | template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const; |
| 331 | template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const; |
| 332 | |
| 333 | template uint32_t SymbolBody::template getPltVA<ELF32LE>() const; |
| 334 | template uint32_t SymbolBody::template getPltVA<ELF32BE>() const; |
| 335 | template uint64_t SymbolBody::template getPltVA<ELF64LE>() const; |
| 336 | template uint64_t SymbolBody::template getPltVA<ELF64BE>() const; |
| 337 | |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 +0000 | [diff] [blame] | 338 | template uint32_t SymbolBody::template getSize<ELF32LE>() const; |
| 339 | template uint32_t SymbolBody::template getSize<ELF32BE>() const; |
| 340 | template uint64_t SymbolBody::template getSize<ELF64LE>() const; |
| 341 | template uint64_t SymbolBody::template getSize<ELF64BE>() const; |
| 342 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 343 | template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const; |
| 344 | template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const; |
| 345 | template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const; |
| 346 | template uint64_t SymbolBody::template getThunkVA<ELF64BE>() const; |
| 347 | |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 348 | template int SymbolBody::compare<ELF32LE>(SymbolBody *Other); |
| 349 | template int SymbolBody::compare<ELF32BE>(SymbolBody *Other); |
| 350 | template int SymbolBody::compare<ELF64LE>(SymbolBody *Other); |
| 351 | template int SymbolBody::compare<ELF64BE>(SymbolBody *Other); |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 352 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 353 | template class elf::UndefinedElf<ELF32LE>; |
| 354 | template class elf::UndefinedElf<ELF32BE>; |
| 355 | template class elf::UndefinedElf<ELF64LE>; |
| 356 | template class elf::UndefinedElf<ELF64BE>; |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 357 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 358 | template class elf::DefinedSynthetic<ELF32LE>; |
| 359 | template class elf::DefinedSynthetic<ELF32BE>; |
| 360 | template class elf::DefinedSynthetic<ELF64LE>; |
| 361 | template class elf::DefinedSynthetic<ELF64BE>; |