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" |
Michael J. Spencer | cdae0a4 | 2015-07-28 22:58:25 +0000 | [diff] [blame] | 11 | #include "InputFiles.h" |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 12 | #include "InputSection.h" |
| 13 | #include "OutputSections.h" |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 +0000 | [diff] [blame] | 14 | #include "Strings.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" |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 17 | #include "Writer.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 18 | |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame^] | 19 | #include "lld/Common/ErrorHandler.h" |
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 | |
George Rimar | e6c5d386 | 2017-04-05 10:03:25 +0000 | [diff] [blame] | 31 | DefinedRegular *ElfSym::Bss; |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 32 | DefinedRegular *ElfSym::Etext1; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 33 | DefinedRegular *ElfSym::Etext2; |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 34 | DefinedRegular *ElfSym::Edata1; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 35 | DefinedRegular *ElfSym::Edata2; |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 36 | DefinedRegular *ElfSym::End1; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 37 | DefinedRegular *ElfSym::End2; |
Rui Ueyama | 92c3781 | 2017-06-26 15:11:24 +0000 | [diff] [blame] | 38 | DefinedRegular *ElfSym::GlobalOffsetTable; |
Rui Ueyama | 3e1fc3f | 2017-04-13 21:37:56 +0000 | [diff] [blame] | 39 | DefinedRegular *ElfSym::MipsGp; |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 40 | DefinedRegular *ElfSym::MipsGpDisp; |
| 41 | DefinedRegular *ElfSym::MipsLocalGp; |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 42 | |
George Rimar | f64618a | 2017-03-17 11:56:54 +0000 | [diff] [blame] | 43 | static uint64_t getSymVA(const SymbolBody &Body, int64_t &Addend) { |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 44 | switch (Body.kind()) { |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 45 | case SymbolBody::DefinedRegularKind: { |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 46 | auto &D = cast<DefinedRegular>(Body); |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 47 | SectionBase *IS = D.Section; |
| 48 | if (auto *ISB = dyn_cast_or_null<InputSectionBase>(IS)) |
| 49 | IS = ISB->Repl; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 50 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 51 | // According to the ELF spec reference to a local symbol from outside |
| 52 | // the group are not allowed. Unfortunately .eh_frame breaks that rule |
| 53 | // and must be treated specially. For now we just replace the symbol with |
| 54 | // 0. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 55 | if (IS == &InputSection::Discarded) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 58 | // This is an absolute symbol. |
Sean Silva | 902ae3c | 2016-12-15 00:57:53 +0000 | [diff] [blame] | 59 | if (!IS) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 60 | return D.Value; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 61 | |
Rafael Espindola | 9371bab | 2017-03-08 15:21:32 +0000 | [diff] [blame] | 62 | uint64_t Offset = D.Value; |
Sean Silva | a9ba450 | 2017-02-28 08:32:56 +0000 | [diff] [blame] | 63 | |
| 64 | // An object in an SHF_MERGE section might be referenced via a |
| 65 | // section symbol (as a hack for reducing the number of local |
| 66 | // symbols). |
Sean Silva | d4e6062 | 2017-03-01 04:44:04 +0000 | [diff] [blame] | 67 | // Depending on the addend, the reference via a section symbol |
| 68 | // refers to a different object in the merge section. |
| 69 | // Since the objects in the merge section are not necessarily |
| 70 | // contiguous in the output, the addend can thus affect the final |
| 71 | // VA in a non-linear way. |
| 72 | // To make this work, we incorporate the addend into the section |
| 73 | // offset (and zero out the addend for later processing) so that |
| 74 | // we find the right object in the section. |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 75 | if (D.isSection()) { |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 76 | Offset += Addend; |
| 77 | Addend = 0; |
| 78 | } |
Sean Silva | a9ba450 | 2017-02-28 08:32:56 +0000 | [diff] [blame] | 79 | |
Rafael Espindola | 5e434b3 | 2017-03-08 16:08:36 +0000 | [diff] [blame] | 80 | const OutputSection *OutSec = IS->getOutputSection(); |
Sean Silva | 6ab3926 | 2017-02-28 09:01:58 +0000 | [diff] [blame] | 81 | |
| 82 | // In the typical case, this is actually very simple and boils |
| 83 | // down to adding together 3 numbers: |
| 84 | // 1. The address of the output section. |
| 85 | // 2. The offset of the input section within the output section. |
| 86 | // 3. The offset within the input section (this addition happens |
| 87 | // inside InputSection::getOffset). |
| 88 | // |
| 89 | // If you understand the data structures involved with this next |
| 90 | // line (and how they get built), then you have a pretty good |
| 91 | // understanding of the linker. |
Rafael Espindola | e129409 | 2017-03-08 16:03:41 +0000 | [diff] [blame] | 92 | uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset); |
Sean Silva | 6ab3926 | 2017-02-28 09:01:58 +0000 | [diff] [blame] | 93 | |
George Rimar | 6a3b154 | 2016-10-04 08:52:51 +0000 | [diff] [blame] | 94 | if (D.isTls() && !Config->Relocatable) { |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 95 | if (!Out::TlsPhdr) |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 96 | fatal(toString(D.getFile()) + |
Peter Collingbourne | 3e2abde | 2017-07-14 00:22:46 +0000 | [diff] [blame] | 97 | " has an STT_TLS symbol but doesn't have an SHF_TLS section"); |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 98 | return VA - Out::TlsPhdr->p_vaddr; |
George Rimar | 6a3b154 | 2016-10-04 08:52:51 +0000 | [diff] [blame] | 99 | } |
Rafael Espindola | 1f5b70f | 2016-03-11 14:21:37 +0000 | [diff] [blame] | 100 | return VA; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 101 | } |
Ben Dunbobbin | 73eabf2 | 2017-09-29 09:08:26 +0000 | [diff] [blame] | 102 | case SymbolBody::DefinedCommonKind: |
| 103 | llvm_unreachable("common are converted to bss"); |
Rui Ueyama | 007c002 | 2017-03-08 17:24:24 +0000 | [diff] [blame] | 104 | case SymbolBody::SharedKind: { |
| 105 | auto &SS = cast<SharedSymbol>(Body); |
Rafael Espindola | 0afcef2 | 2017-08-04 17:43:54 +0000 | [diff] [blame] | 106 | if (SS.CopyRelSec) |
Rafael Espindola | f6c74c4 | 2017-09-13 16:59:12 +0000 | [diff] [blame] | 107 | return SS.CopyRelSec->getParent()->Addr + SS.CopyRelSec->OutSecOff; |
Rui Ueyama | 007c002 | 2017-03-08 17:24:24 +0000 | [diff] [blame] | 108 | if (SS.NeedsPltAddr) |
George Rimar | f64618a | 2017-03-17 11:56:54 +0000 | [diff] [blame] | 109 | return Body.getPltVA(); |
Rui Ueyama | 924b361 | 2017-02-16 06:12:22 +0000 | [diff] [blame] | 110 | return 0; |
Rui Ueyama | 007c002 | 2017-03-08 17:24:24 +0000 | [diff] [blame] | 111 | } |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 112 | case SymbolBody::UndefinedKind: |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 113 | return 0; |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 114 | case SymbolBody::LazyArchiveKind: |
| 115 | case SymbolBody::LazyObjectKind: |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 116 | assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | } |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 119 | llvm_unreachable("invalid symbol kind"); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 122 | SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, |
| 123 | uint8_t Type) |
Rui Ueyama | 662bb00 | 2017-10-13 03:37:26 +0000 | [diff] [blame] | 124 | : SymbolKind(K), IsLocal(IsLocal), NeedsPltAddr(false), |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 125 | IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false), |
Rafael Espindola | 35c908f | 2017-08-10 15:05:37 +0000 | [diff] [blame] | 126 | IsInIgot(false), IsPreemptible(false), Type(Type), StOther(StOther), |
| 127 | Name(Name) {} |
Rafael Espindola | f476573 | 2016-04-06 13:22:41 +0000 | [diff] [blame] | 128 | |
Rui Ueyama | ce2f5fd | 2017-10-13 02:57:59 +0000 | [diff] [blame] | 129 | // Returns true if this is a weak undefined symbol. |
Rafael Espindola | 3d9f1c0 | 2017-09-13 20:43:04 +0000 | [diff] [blame] | 130 | bool SymbolBody::isUndefWeak() const { |
Rafael Espindola | 9c8f853 | 2017-10-24 16:27:31 +0000 | [diff] [blame] | 131 | // See comment on Lazy in Symbols.h for the details. |
Rui Ueyama | ce2f5fd | 2017-10-13 02:57:59 +0000 | [diff] [blame] | 132 | return !isLocal() && symbol()->isWeak() && (isUndefined() || isLazy()); |
Rafael Espindola | 3d9f1c0 | 2017-09-13 20:43:04 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 135 | InputFile *SymbolBody::getFile() const { |
Rafael Espindola | db1af69 | 2017-08-11 17:47:12 +0000 | [diff] [blame] | 136 | if (isLocal()) { |
| 137 | const SectionBase *Sec = cast<DefinedRegular>(this)->Section; |
| 138 | // Local absolute symbols actually have a file, but that is not currently |
| 139 | // used. We could support that by having a mostly redundant InputFile in |
| 140 | // SymbolBody, or having a special absolute section if needed. |
| 141 | return Sec ? cast<InputSectionBase>(Sec)->File : nullptr; |
| 142 | } |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 143 | return symbol()->File; |
| 144 | } |
| 145 | |
Rafael Espindola | cf00d43 | 2017-07-05 00:43:18 +0000 | [diff] [blame] | 146 | // Overwrites all attributes with Other's so that this symbol becomes |
| 147 | // an alias to Other. This is useful for handling some options such as |
| 148 | // --wrap. |
Rui Ueyama | 5bbe4a4 | 2017-09-25 00:57:30 +0000 | [diff] [blame] | 149 | void SymbolBody::copyFrom(SymbolBody *Other) { |
Rui Ueyama | b2269ec | 2017-06-28 19:43:02 +0000 | [diff] [blame] | 150 | memcpy(symbol()->Body.buffer, Other->symbol()->Body.buffer, |
| 151 | sizeof(Symbol::Body)); |
Rui Ueyama | b2269ec | 2017-06-28 19:43:02 +0000 | [diff] [blame] | 152 | } |
| 153 | |
George Rimar | f64618a | 2017-03-17 11:56:54 +0000 | [diff] [blame] | 154 | uint64_t SymbolBody::getVA(int64_t Addend) const { |
| 155 | uint64_t OutVA = getSymVA(*this, Addend); |
Rafael Espindola | 8381c56 | 2016-03-17 23:36:19 +0000 | [diff] [blame] | 156 | return OutVA + Addend; |
Rafael Espindola | 87d9f10 | 2016-03-11 12:19:05 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Rafael Espindola | f9e3c9c | 2017-05-11 23:28:49 +0000 | [diff] [blame] | 159 | uint64_t SymbolBody::getGotVA() const { |
Rafael Espindola | 88ab9fb | 2017-05-11 23:26:03 +0000 | [diff] [blame] | 160 | return InX::Got->getVA() + getGotOffset(); |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 161 | } |
| 162 | |
George Rimar | 4afe42e | 2017-03-17 14:12:51 +0000 | [diff] [blame] | 163 | uint64_t SymbolBody::getGotOffset() const { |
Rui Ueyama | 803b120 | 2016-07-13 18:55:14 +0000 | [diff] [blame] | 164 | return GotIndex * Target->GotEntrySize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 165 | } |
| 166 | |
George Rimar | 4670bb0 | 2017-03-16 12:58:11 +0000 | [diff] [blame] | 167 | uint64_t SymbolBody::getGotPltVA() const { |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 168 | if (this->IsInIgot) |
George Rimar | 4670bb0 | 2017-03-16 12:58:11 +0000 | [diff] [blame] | 169 | return InX::IgotPlt->getVA() + getGotPltOffset(); |
| 170 | return InX::GotPlt->getVA() + getGotPltOffset(); |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 171 | } |
| 172 | |
George Rimar | 4670bb0 | 2017-03-16 12:58:11 +0000 | [diff] [blame] | 173 | uint64_t SymbolBody::getGotPltOffset() const { |
Rui Ueyama | 803b120 | 2016-07-13 18:55:14 +0000 | [diff] [blame] | 174 | return GotPltIndex * Target->GotPltEntrySize; |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 175 | } |
| 176 | |
George Rimar | f64618a | 2017-03-17 11:56:54 +0000 | [diff] [blame] | 177 | uint64_t SymbolBody::getPltVA() const { |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 178 | if (this->IsInIplt) |
George Rimar | f64618a | 2017-03-17 11:56:54 +0000 | [diff] [blame] | 179 | return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize; |
| 180 | return InX::Plt->getVA() + Target->PltHeaderSize + |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 181 | PltIndex * Target->PltEntrySize; |
| 182 | } |
| 183 | |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 184 | template <class ELFT> typename ELFT::uint SymbolBody::getSize() const { |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 185 | if (const auto *C = dyn_cast<DefinedCommon>(this)) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 186 | return C->Size; |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 187 | if (const auto *DR = dyn_cast<DefinedRegular>(this)) |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 188 | return DR->Size; |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 189 | if (const auto *S = dyn_cast<SharedSymbol>(this)) |
| 190 | return S->getSize<ELFT>(); |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
George Rimar | 69268a8 | 2017-03-16 11:06:13 +0000 | [diff] [blame] | 194 | OutputSection *SymbolBody::getOutputSection() const { |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 195 | if (auto *S = dyn_cast<DefinedRegular>(this)) { |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 +0000 | [diff] [blame] | 196 | if (S->Section) |
Rafael Espindola | 5e434b3 | 2017-03-08 16:08:36 +0000 | [diff] [blame] | 197 | return S->Section->getOutputSection(); |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 +0000 | [diff] [blame] | 198 | return nullptr; |
| 199 | } |
| 200 | |
Rui Ueyama | 007c002 | 2017-03-08 17:24:24 +0000 | [diff] [blame] | 201 | if (auto *S = dyn_cast<SharedSymbol>(this)) { |
Rafael Espindola | 0afcef2 | 2017-08-04 17:43:54 +0000 | [diff] [blame] | 202 | if (S->CopyRelSec) |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 203 | return S->CopyRelSec->getParent(); |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 +0000 | [diff] [blame] | 204 | return nullptr; |
Rui Ueyama | 007c002 | 2017-03-08 17:24:24 +0000 | [diff] [blame] | 205 | } |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 +0000 | [diff] [blame] | 206 | |
Dmitry Mikulin | 1e30f07 | 2017-09-08 16:22:43 +0000 | [diff] [blame] | 207 | if (auto *S = dyn_cast<DefinedCommon>(this)) { |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 +0000 | [diff] [blame] | 208 | if (Config->DefineCommon) |
Dmitry Mikulin | 1e30f07 | 2017-09-08 16:22:43 +0000 | [diff] [blame] | 209 | return S->Section->getParent(); |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 +0000 | [diff] [blame] | 210 | return nullptr; |
| 211 | } |
| 212 | |
Rui Ueyama | 968db48 | 2017-02-28 04:02:42 +0000 | [diff] [blame] | 213 | return nullptr; |
| 214 | } |
| 215 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 216 | // If a symbol name contains '@', the characters after that is |
| 217 | // a symbol version name. This function parses that. |
| 218 | void SymbolBody::parseSymbolVersion() { |
| 219 | StringRef S = getName(); |
| 220 | size_t Pos = S.find('@'); |
| 221 | if (Pos == 0 || Pos == StringRef::npos) |
| 222 | return; |
| 223 | StringRef Verstr = S.substr(Pos + 1); |
| 224 | if (Verstr.empty()) |
| 225 | return; |
| 226 | |
| 227 | // 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] | 228 | Name = {S.data(), Pos}; |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 229 | |
Rafael Espindola | 1d6d1b4 | 2017-01-17 16:08:06 +0000 | [diff] [blame] | 230 | // If this is not in this DSO, it is not a definition. |
| 231 | if (!isInCurrentDSO()) |
Rafael Espindola | 2756e04 | 2017-01-06 22:30:35 +0000 | [diff] [blame] | 232 | return; |
| 233 | |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 234 | // '@@' in a symbol name means the default version. |
| 235 | // It is usually the most recent one. |
| 236 | bool IsDefault = (Verstr[0] == '@'); |
| 237 | if (IsDefault) |
| 238 | Verstr = Verstr.substr(1); |
| 239 | |
| 240 | for (VersionDefinition &Ver : Config->VersionDefinitions) { |
| 241 | if (Ver.Name != Verstr) |
| 242 | continue; |
| 243 | |
| 244 | if (IsDefault) |
| 245 | symbol()->VersionId = Ver.Id; |
| 246 | else |
| 247 | symbol()->VersionId = Ver.Id | VERSYM_HIDDEN; |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | // It is an error if the specified version is not defined. |
George Rimar | 4d2f9762 | 2017-07-04 13:19:13 +0000 | [diff] [blame] | 252 | // Usually version script is not provided when linking executable, |
| 253 | // but we may still want to override a versioned symbol from DSO, |
| 254 | // so we do not report error in this case. |
| 255 | if (Config->Shared) |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 256 | error(toString(getFile()) + ": symbol " + S + " has undefined version " + |
George Rimar | 4d2f9762 | 2017-07-04 13:19:13 +0000 | [diff] [blame] | 257 | Verstr); |
Rui Ueyama | 35fa6c5 | 2016-11-23 05:48:40 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 260 | Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther, |
| 261 | uint8_t Type) |
| 262 | : SymbolBody(K, Name, IsLocal, StOther, Type) {} |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 263 | |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 264 | template <class ELFT> bool DefinedRegular::isMipsPIC() const { |
Rui Ueyama | d97265f | 2017-06-09 21:09:08 +0000 | [diff] [blame] | 265 | typedef typename ELFT::Ehdr Elf_Ehdr; |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 266 | if (!Section || !isFunc()) |
| 267 | return false; |
Rui Ueyama | d97265f | 2017-06-09 21:09:08 +0000 | [diff] [blame] | 268 | |
| 269 | auto *Sec = cast<InputSectionBase>(Section); |
| 270 | const Elf_Ehdr *Hdr = Sec->template getFile<ELFT>()->getObj().getHeader(); |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 271 | return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC || |
Rui Ueyama | d97265f | 2017-06-09 21:09:08 +0000 | [diff] [blame] | 272 | (Hdr->e_flags & EF_MIPS_PIC); |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 275 | Undefined::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther, |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 276 | uint8_t Type) |
| 277 | : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {} |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 278 | |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 279 | DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint32_t Alignment, |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 280 | uint8_t StOther, uint8_t Type) |
Rui Ueyama | a13efc2 | 2016-11-29 18:05:04 +0000 | [diff] [blame] | 281 | : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther, |
| 282 | Type), |
Ben Dunbobbin | 73eabf2 | 2017-09-29 09:08:26 +0000 | [diff] [blame] | 283 | Alignment(Alignment), Size(Size) {} |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 284 | |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 285 | // If a shared symbol is referred via a copy relocation, its alignment |
| 286 | // becomes part of the ABI. This function returns a symbol alignment. |
| 287 | // Because symbols don't have alignment attributes, we need to infer that. |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 288 | template <class ELFT> uint32_t SharedSymbol::getAlignment() const { |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 289 | SharedFile<ELFT> *File = getFile<ELFT>(); |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 290 | uint32_t SecAlign = File->getSection(getSym<ELFT>())->sh_addralign; |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 291 | uint64_t SymValue = getSym<ELFT>().st_value; |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 292 | uint32_t SymAlign = uint32_t(1) << countTrailingZeros(SymValue); |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 293 | return std::min(SecAlign, SymAlign); |
| 294 | } |
| 295 | |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 296 | InputFile *Lazy::fetch() { |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 297 | if (auto *S = dyn_cast<LazyArchive>(this)) |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 298 | return S->fetch(); |
| 299 | return cast<LazyObject>(this)->fetch(); |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 302 | LazyArchive::LazyArchive(const llvm::object::Archive::Symbol S, uint8_t Type) |
| 303 | : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {} |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 304 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 305 | LazyObject::LazyObject(StringRef Name, uint8_t Type) |
| 306 | : Lazy(LazyObjectKind, Name, Type) {} |
| 307 | |
| 308 | ArchiveFile *LazyArchive::getFile() { |
| 309 | return cast<ArchiveFile>(SymbolBody::getFile()); |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 312 | InputFile *LazyArchive::fetch() { |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 313 | std::pair<MemoryBufferRef, uint64_t> MBInfo = getFile()->getMember(&Sym); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 314 | |
| 315 | // getMember returns an empty buffer if the member was already |
| 316 | // read from the library. |
Davide Italiano | bcdd6c6 | 2016-10-12 19:35:54 +0000 | [diff] [blame] | 317 | if (MBInfo.first.getBuffer().empty()) |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 318 | return nullptr; |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 319 | return createObjectFile(MBInfo.first, getFile()->getName(), MBInfo.second); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 322 | LazyObjFile *LazyObject::getFile() { |
| 323 | return cast<LazyObjFile>(SymbolBody::getFile()); |
| 324 | } |
| 325 | |
| 326 | InputFile *LazyObject::fetch() { return getFile()->fetch(); } |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 327 | |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 +0000 | [diff] [blame] | 328 | uint8_t Symbol::computeBinding() const { |
| 329 | if (Config->Relocatable) |
| 330 | return Binding; |
Peter Collingbourne | dadcc17 | 2016-04-22 18:42:48 +0000 | [diff] [blame] | 331 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 +0000 | [diff] [blame] | 332 | return STB_LOCAL; |
Rui Ueyama | 273bbbc | 2017-04-24 23:50:58 +0000 | [diff] [blame] | 333 | if (VersionId == VER_NDX_LOCAL && body()->isInCurrentDSO()) |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 +0000 | [diff] [blame] | 334 | return STB_LOCAL; |
| 335 | if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE) |
| 336 | return STB_GLOBAL; |
| 337 | return Binding; |
| 338 | } |
| 339 | |
| 340 | bool Symbol::includeInDynsym() const { |
Rafael Espindola | e05e2f8 | 2017-09-15 18:05:02 +0000 | [diff] [blame] | 341 | if (!Config->HasDynSymTab) |
| 342 | return false; |
Rafael Espindola | b7e2ee2 | 2017-01-10 17:08:13 +0000 | [diff] [blame] | 343 | if (computeBinding() == STB_LOCAL) |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 344 | return false; |
Rafael Espindola | 3d9f1c0 | 2017-09-13 20:43:04 +0000 | [diff] [blame] | 345 | if (!body()->isInCurrentDSO()) |
| 346 | return true; |
Rafael Espindola | c57f8cd | 2017-09-13 20:47:53 +0000 | [diff] [blame] | 347 | return ExportDynamic; |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 348 | } |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 349 | |
| 350 | // Print out a log message for --trace-symbol. |
| 351 | void elf::printTraceSymbol(Symbol *Sym) { |
| 352 | SymbolBody *B = Sym->body(); |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 353 | std::string S; |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 354 | if (B->isUndefined()) |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 355 | S = ": reference to "; |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 356 | else if (B->isCommon()) |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 357 | S = ": common definition of "; |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 358 | else |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 359 | S = ": definition of "; |
| 360 | |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 361 | message(toString(Sym->File) + S + B->getName()); |
Rui Ueyama | 69c778c | 2016-07-17 17:50:09 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 +0000 | [diff] [blame] | 364 | // Returns a symbol for an error message. |
Rui Ueyama | ce03926 | 2017-01-06 10:04:08 +0000 | [diff] [blame] | 365 | std::string lld::toString(const SymbolBody &B) { |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 +0000 | [diff] [blame] | 366 | if (Config->Demangle) |
Rui Ueyama | 4c5b8ce | 2016-12-07 23:17:05 +0000 | [diff] [blame] | 367 | if (Optional<std::string> S = demangle(B.getName())) |
| 368 | return *S; |
Rui Ueyama | a3ac173 | 2016-11-24 20:24:18 +0000 | [diff] [blame] | 369 | return B.getName(); |
| 370 | } |
| 371 | |
Rui Ueyama | 512c61d | 2016-02-03 00:12:24 +0000 | [diff] [blame] | 372 | template uint32_t SymbolBody::template getSize<ELF32LE>() const; |
| 373 | template uint32_t SymbolBody::template getSize<ELF32BE>() const; |
| 374 | template uint64_t SymbolBody::template getSize<ELF64LE>() const; |
| 375 | template uint64_t SymbolBody::template getSize<ELF64BE>() const; |
| 376 | |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 377 | template bool DefinedRegular::template isMipsPIC<ELF32LE>() const; |
| 378 | template bool DefinedRegular::template isMipsPIC<ELF32BE>() const; |
| 379 | template bool DefinedRegular::template isMipsPIC<ELF64LE>() const; |
| 380 | template bool DefinedRegular::template isMipsPIC<ELF64BE>() const; |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 381 | |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 382 | template uint32_t SharedSymbol::template getAlignment<ELF32LE>() const; |
| 383 | template uint32_t SharedSymbol::template getAlignment<ELF32BE>() const; |
| 384 | template uint32_t SharedSymbol::template getAlignment<ELF64LE>() const; |
| 385 | template uint32_t SharedSymbol::template getAlignment<ELF64BE>() const; |