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