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