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" |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 +0000 | [diff] [blame] | 15 | #include "Strings.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 16 | #include "SyntheticSections.h" |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 17 | #include "Target.h" |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 18 | #include "Writer.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Eugene Leviant | c958d8d | 2016-10-12 08:19:30 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
Rui Ueyama | c72ba3a | 2016-11-23 04:57:25 +0000 | [diff] [blame] | 22 | #include <cstring> |
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: { |
Rui Ueyama | 4f2f50d | 2016-12-21 08:40:09 +0000 | [diff] [blame] | 38 | auto &D = cast<DefinedSynthetic>(Body); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 39 | const OutputSectionBase *Sec = D.Section; |
Peter Collingbourne | 6a42259 | 2016-05-03 01:21:08 +0000 | [diff] [blame] | 40 | if (!Sec) |
| 41 | return D.Value; |
Rui Ueyama | 4f2f50d | 2016-12-21 08:40:09 +0000 | [diff] [blame] | 42 | if (D.Value == uintX_t(-1)) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 43 | return Sec->Addr + Sec->Size; |
| 44 | return Sec->Addr + D.Value; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 45 | } |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 46 | case SymbolBody::DefinedRegularKind: { |
| 47 | auto &D = cast<DefinedRegular<ELFT>>(Body); |
Sean Silva | 902ae3c | 2016-12-15 00:57:53 +0000 | [diff] [blame] | 48 | InputSectionBase<ELFT> *IS = D.Section; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 49 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 50 | // According to the ELF spec reference to a local symbol from outside |
| 51 | // the group are not allowed. Unfortunately .eh_frame breaks that rule |
| 52 | // and must be treated specially. For now we just replace the symbol with |
| 53 | // 0. |
Sean Silva | 902ae3c | 2016-12-15 00:57:53 +0000 | [diff] [blame] | 54 | if (IS == &InputSection<ELFT>::Discarded) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 55 | return 0; |
| 56 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 57 | // This is an absolute symbol. |
Sean Silva | 902ae3c | 2016-12-15 00:57:53 +0000 | [diff] [blame] | 58 | if (!IS) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 59 | return D.Value; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 60 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 61 | uintX_t Offset = D.Value; |
| 62 | if (D.isSection()) { |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 63 | Offset += Addend; |
| 64 | Addend = 0; |
| 65 | } |
Sean Silva | 902ae3c | 2016-12-15 00:57:53 +0000 | [diff] [blame] | 66 | uintX_t VA = (IS->OutSec ? IS->OutSec->Addr : 0) + IS->getOffset(Offset); |
George Rimar | 6a3b154 | 2016-10-04 08:52:51 +0000 | [diff] [blame] | 67 | if (D.isTls() && !Config->Relocatable) { |
| 68 | if (!Out<ELFT>::TlsPhdr) |
Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 69 | fatal(toString(D.File) + |
George Rimar | 6a3b154 | 2016-10-04 08:52:51 +0000 | [diff] [blame] | 70 | " has a STT_TLS symbol but doesn't have a PT_TLS section"); |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 71 | return VA - Out<ELFT>::TlsPhdr->p_vaddr; |
George Rimar | 6a3b154 | 2016-10-04 08:52:51 +0000 | [diff] [blame] | 72 | } |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 73 | return VA; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 74 | } |
Rui Ueyama | 0778490 | 2016-08-02 01:35:13 +0000 | [diff] [blame] | 75 | case SymbolBody::DefinedCommonKind: |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 76 | return In<ELFT>::Common->OutSec->Addr + In<ELFT>::Common->OutSecOff + |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 77 | cast<DefinedCommon>(Body).Offset; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 78 | case SymbolBody::SharedKind: { |
| 79 | auto &SS = cast<SharedSymbol<ELFT>>(Body); |
| 80 | if (!SS.NeedsCopyOrPltAddr) |
Rafael Espindola | a0a65f9 | 2016-02-09 15:11:01 +0000 | [diff] [blame] | 81 | return 0; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 82 | if (SS.isFunc()) |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 83 | return Body.getPltVA<ELFT>(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 84 | return Out<ELFT>::Bss->Addr + SS.OffsetInBss; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 85 | } |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 86 | case SymbolBody::UndefinedKind: |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 87 | return 0; |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 88 | case SymbolBody::LazyArchiveKind: |
| 89 | case SymbolBody::LazyObjectKind: |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 90 | assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 91 | return 0; |
| 92 | } |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 93 | llvm_unreachable("invalid symbol kind"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 96 | SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, |
| 97 | uint8_t Type) |
| 98 | : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(IsLocal), |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 99 | IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false), |
| 100 | IsInIgot(false), Type(Type), StOther(StOther), Name(Name) {} |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 101 | |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 102 | // Returns true if a symbol can be replaced at load-time by a symbol |
| 103 | // with the same name defined in other ELF executable or DSO. |
| 104 | bool SymbolBody::isPreemptible() const { |
| 105 | if (isLocal()) |
| 106 | return false; |
| 107 | |
Rafael Espindola | 6643456 | 2016-05-05 19:41:49 +0000 | [diff] [blame] | 108 | // Shared symbols resolve to the definition in the DSO. The exceptions are |
| 109 | // symbols with copy relocations (which resolve to .bss) or preempt plt |
| 110 | // entries (which resolve to that plt entry). |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 111 | if (isShared()) |
Rafael Espindola | 6643456 | 2016-05-05 19:41:49 +0000 | [diff] [blame] | 112 | return !NeedsCopyOrPltAddr; |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 113 | |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 114 | // That's all that can be preempted in a non-DSO. |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 115 | if (!Config->Shared) |
| 116 | return false; |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 117 | |
Peter Collingbourne | dbe4187 | 2016-04-24 04:29:59 +0000 | [diff] [blame] | 118 | // Only symbols that appear in dynsym can be preempted. |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 119 | if (!symbol()->includeInDynsym()) |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 120 | return false; |
Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 121 | |
Rafael Espindola | 580d7a1 | 2016-07-07 22:50:54 +0000 | [diff] [blame] | 122 | // Only default visibility symbols can be preempted. |
| 123 | if (symbol()->Visibility != STV_DEFAULT) |
| 124 | return false; |
| 125 | |
| 126 | // -Bsymbolic means that definitions are not preempted. |
Peter Collingbourne | dbe4187 | 2016-04-24 04:29:59 +0000 | [diff] [blame] | 127 | if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc())) |
| 128 | return !isDefined(); |
Rafael Espindola | 580d7a1 | 2016-07-07 22:50:54 +0000 | [diff] [blame] | 129 | return true; |
Rui Ueyama | c446660 | 2016-03-13 19:48:18 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 132 | template <class ELFT> bool SymbolBody::hasThunk() const { |
| 133 | if (auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) |
| 134 | return DR->ThunkData != nullptr; |
| 135 | if (auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) |
| 136 | return S->ThunkData != nullptr; |
| 137 | return false; |
| 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 { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 147 | return In<ELFT>::Got->getVA() + getGotOffset<ELFT>(); |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | template <class ELFT> typename ELFT::uint SymbolBody::getGotOffset() const { |
Rui Ueyama | 803b120 | 2016-07-13 18:55:14 +0000 | [diff] [blame] | 151 | return GotIndex * Target->GotEntrySize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 154 | template <class ELFT> typename ELFT::uint SymbolBody::getGotPltVA() const { |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 155 | if (this->IsInIgot) |
| 156 | return In<ELFT>::IgotPlt->getVA() + getGotPltOffset<ELFT>(); |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 157 | return In<ELFT>::GotPlt->getVA() + getGotPltOffset<ELFT>(); |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | template <class ELFT> typename ELFT::uint SymbolBody::getGotPltOffset() const { |
Rui Ueyama | 803b120 | 2016-07-13 18:55:14 +0000 | [diff] [blame] | 161 | return GotPltIndex * Target->GotPltEntrySize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 164 | template <class ELFT> typename ELFT::uint SymbolBody::getPltVA() const { |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 165 | if (this->IsInIplt) |
| 166 | return In<ELFT>::Iplt->getVA() + PltIndex * Target->PltEntrySize; |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 167 | return In<ELFT>::Plt->getVA() + Target->PltHeaderSize + |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 168 | PltIndex * Target->PltEntrySize; |
| 169 | } |
| 170 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 171 | template <class ELFT> typename ELFT::uint SymbolBody::getThunkVA() const { |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 172 | if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) |
| 173 | return DR->ThunkData->getVA(); |
| 174 | if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) |
| 175 | return S->ThunkData->getVA(); |
Peter Smith | 97c6d78f | 2017-01-04 09:45:45 +0000 | [diff] [blame] | 176 | if (const auto *S = dyn_cast<Undefined<ELFT>>(this)) |
| 177 | return S->ThunkData->getVA(); |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 178 | fatal("getThunkVA() not supported for Symbol class\n"); |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 181 | template <class ELFT> typename ELFT::uint SymbolBody::getSize() const { |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 182 | if (const auto *C = dyn_cast<DefinedCommon>(this)) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 183 | return C->Size; |
| 184 | if (const auto *DR = dyn_cast<DefinedRegular<ELFT>>(this)) |
| 185 | return DR->Size; |
| 186 | if (const auto *S = dyn_cast<SharedSymbol<ELFT>>(this)) |
| 187 | return S->Sym.st_size; |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 191 | // If a symbol name contains '@', the characters after that is |
| 192 | // a symbol version name. This function parses that. |
| 193 | void SymbolBody::parseSymbolVersion() { |
| 194 | StringRef S = getName(); |
| 195 | size_t Pos = S.find('@'); |
| 196 | if (Pos == 0 || Pos == StringRef::npos) |
| 197 | return; |
| 198 | StringRef Verstr = S.substr(Pos + 1); |
| 199 | if (Verstr.empty()) |
| 200 | return; |
| 201 | |
| 202 | // Truncate the symbol name so that it doesn't include the version string. |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 203 | Name = {S.data(), Pos}; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 204 | |
Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 +0000 | [diff] [blame^] | 205 | // If this is an undefined or shared symbol it is not a definition. |
| 206 | if (isUndefined() || isShared()) |
| 207 | return; |
| 208 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 209 | // '@@' in a symbol name means the default version. |
| 210 | // It is usually the most recent one. |
| 211 | bool IsDefault = (Verstr[0] == '@'); |
| 212 | if (IsDefault) |
| 213 | Verstr = Verstr.substr(1); |
| 214 | |
| 215 | for (VersionDefinition &Ver : Config->VersionDefinitions) { |
| 216 | if (Ver.Name != Verstr) |
| 217 | continue; |
| 218 | |
| 219 | if (IsDefault) |
| 220 | symbol()->VersionId = Ver.Id; |
| 221 | else |
| 222 | symbol()->VersionId = Ver.Id | VERSYM_HIDDEN; |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // It is an error if the specified version is not defined. |
Rui Ueyama | 44da9de | 2016-12-05 18:40:14 +0000 | [diff] [blame] | 227 | error(toString(File) + ": symbol " + S + " has undefined version " + Verstr); |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 230 | Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, |
| 231 | uint8_t Type) |
| 232 | : SymbolBody(K, Name, IsLocal, StOther, Type) {} |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 233 | |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 234 | template <class ELFT> bool DefinedRegular<ELFT>::isMipsPIC() const { |
| 235 | if (!Section || !isFunc()) |
| 236 | return false; |
| 237 | return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC || |
| 238 | (Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC); |
| 239 | } |
| 240 | |
Peter Smith | 97c6d78f | 2017-01-04 09:45:45 +0000 | [diff] [blame] | 241 | template <typename ELFT> |
| 242 | Undefined<ELFT>::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, |
| 243 | uint8_t Type, InputFile *File) |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 244 | : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) { |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 245 | this->File = File; |
| 246 | } |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 247 | |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 248 | DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint64_t Alignment, |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 249 | uint8_t StOther, uint8_t Type, InputFile *File) |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 250 | : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther, |
| 251 | Type), |
Rui Ueyama | 2a7c1c1 | 2016-07-17 17:36:22 +0000 | [diff] [blame] | 252 | Alignment(Alignment), Size(Size) { |
| 253 | this->File = File; |
| 254 | } |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 255 | |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 256 | InputFile *Lazy::fetch() { |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 257 | if (auto *S = dyn_cast<LazyArchive>(this)) |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 258 | return S->fetch(); |
| 259 | return cast<LazyObject>(this)->fetch(); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 262 | LazyArchive::LazyArchive(ArchiveFile &File, |
| 263 | const llvm::object::Archive::Symbol S, uint8_t Type) |
| 264 | : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) { |
| 265 | this->File = &File; |
| 266 | } |
| 267 | |
| 268 | LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type) |
| 269 | : Lazy(LazyObjectKind, Name, Type) { |
| 270 | this->File = &File; |
| 271 | } |
| 272 | |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 273 | InputFile *LazyArchive::fetch() { |
Davide Italiano | bcdd6c6 | 2016-10-12 19:35:54 +0000 | [diff] [blame] | 274 | std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 275 | |
| 276 | // getMember returns an empty buffer if the member was already |
| 277 | // read from the library. |
Davide Italiano | bcdd6c6 | 2016-10-12 19:35:54 +0000 | [diff] [blame] | 278 | if (MBInfo.first.getBuffer().empty()) |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 279 | return nullptr; |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 280 | return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 283 | InputFile *LazyObject::fetch() { |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 284 | MemoryBufferRef MBRef = file()->getBuffer(); |
Rafael Espindola | 65c65ce | 2016-06-14 21:56:36 +0000 | [diff] [blame] | 285 | if (MBRef.getBuffer().empty()) |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 286 | return nullptr; |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 287 | return createObjectFile(MBRef); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 290 | bool Symbol::includeInDynsym() const { |
| 291 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 292 | return false; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 293 | return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() || |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 294 | (body()->isUndefined() && Config->Shared); |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 295 | } |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 296 | |
| 297 | // Print out a log message for --trace-symbol. |
| 298 | void elf::printTraceSymbol(Symbol *Sym) { |
| 299 | SymbolBody *B = Sym->body(); |
Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 300 | outs() << toString(B->File); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 301 | |
| 302 | if (B->isUndefined()) |
| 303 | outs() << ": reference to "; |
| 304 | else if (B->isCommon()) |
| 305 | outs() << ": common definition of "; |
| 306 | else |
| 307 | outs() << ": definition of "; |
| 308 | outs() << B->getName() << "\n"; |
| 309 | } |
| 310 | |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 +0000 | [diff] [blame] | 311 | // Returns a symbol for an error message. |
Rui Ueyama | ce03926 | 2017-01-06 10:04:08 +0000 | [diff] [blame] | 312 | std::string lld::toString(const SymbolBody &B) { |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 +0000 | [diff] [blame] | 313 | if (Config->Demangle) |
Rui Ueyama | 4c5b8ce | 2016-12-07 23:17:05 +0000 | [diff] [blame] | 314 | if (Optional<std::string> S = demangle(B.getName())) |
| 315 | return *S; |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 +0000 | [diff] [blame] | 316 | return B.getName(); |
| 317 | } |
| 318 | |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 319 | template bool SymbolBody::hasThunk<ELF32LE>() const; |
| 320 | template bool SymbolBody::hasThunk<ELF32BE>() const; |
| 321 | template bool SymbolBody::hasThunk<ELF64LE>() const; |
| 322 | template bool SymbolBody::hasThunk<ELF64BE>() const; |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 323 | |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 324 | template uint32_t SymbolBody::template getVA<ELF32LE>(uint32_t) const; |
| 325 | template uint32_t SymbolBody::template getVA<ELF32BE>(uint32_t) const; |
| 326 | template uint64_t SymbolBody::template getVA<ELF64LE>(uint64_t) const; |
| 327 | template uint64_t SymbolBody::template getVA<ELF64BE>(uint64_t) const; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 328 | |
| 329 | template uint32_t SymbolBody::template getGotVA<ELF32LE>() const; |
| 330 | template uint32_t SymbolBody::template getGotVA<ELF32BE>() const; |
| 331 | template uint64_t SymbolBody::template getGotVA<ELF64LE>() const; |
| 332 | template uint64_t SymbolBody::template getGotVA<ELF64BE>() const; |
| 333 | |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 334 | template uint32_t SymbolBody::template getGotOffset<ELF32LE>() const; |
| 335 | template uint32_t SymbolBody::template getGotOffset<ELF32BE>() const; |
| 336 | template uint64_t SymbolBody::template getGotOffset<ELF64LE>() const; |
| 337 | template uint64_t SymbolBody::template getGotOffset<ELF64BE>() const; |
| 338 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 339 | template uint32_t SymbolBody::template getGotPltVA<ELF32LE>() const; |
| 340 | template uint32_t SymbolBody::template getGotPltVA<ELF32BE>() const; |
| 341 | template uint64_t SymbolBody::template getGotPltVA<ELF64LE>() const; |
| 342 | template uint64_t SymbolBody::template getGotPltVA<ELF64BE>() const; |
| 343 | |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 344 | template uint32_t SymbolBody::template getThunkVA<ELF32LE>() const; |
| 345 | template uint32_t SymbolBody::template getThunkVA<ELF32BE>() const; |
| 346 | template uint64_t SymbolBody::template getThunkVA<ELF64LE>() const; |
| 347 | template uint64_t SymbolBody::template getThunkVA<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 | |
Peter Smith | 97c6d78f | 2017-01-04 09:45:45 +0000 | [diff] [blame] | 364 | template class elf::Undefined<ELF32LE>; |
| 365 | template class elf::Undefined<ELF32BE>; |
| 366 | template class elf::Undefined<ELF64LE>; |
| 367 | template class elf::Undefined<ELF64BE>; |
| 368 | |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 369 | template class elf::DefinedRegular<ELF32LE>; |
| 370 | template class elf::DefinedRegular<ELF32BE>; |
| 371 | template class elf::DefinedRegular<ELF64LE>; |
| 372 | template class elf::DefinedRegular<ELF64BE>; |