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