Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1 | //===- OutputSections.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 "OutputSections.h" |
| 11 | #include "Config.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 12 | #include "SymbolTable.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 13 | #include "Target.h" |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 14 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 15 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | using namespace llvm::object; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 18 | using namespace llvm::support::endian; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 19 | using namespace llvm::ELF; |
| 20 | |
| 21 | using namespace lld; |
| 22 | using namespace lld::elf2; |
| 23 | |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 24 | bool elf2::HasGotOffRel = false; |
George Rimar | bfb7bf7 | 2015-12-21 10:00:12 +0000 | [diff] [blame] | 25 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 26 | template <class ELFT> |
| 27 | OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t sh_type, |
| 28 | uintX_t sh_flags) |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 29 | : Name(Name) { |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 30 | memset(&Header, 0, sizeof(Elf_Shdr)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 31 | Header.sh_type = sh_type; |
| 32 | Header.sh_flags = sh_flags; |
| 33 | } |
| 34 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 35 | template <class ELFT> |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 36 | GotPltSection<ELFT>::GotPltSection() |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 37 | : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) { |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 38 | this->Header.sh_addralign = sizeof(uintX_t); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) { |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 42 | Sym->GotPltIndex = Target->getGotPltHeaderEntriesNum() + Entries.size(); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 43 | Entries.push_back(Sym); |
| 44 | } |
| 45 | |
| 46 | template <class ELFT> bool GotPltSection<ELFT>::empty() const { |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 47 | return Entries.empty(); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | template <class ELFT> |
| 51 | typename GotPltSection<ELFT>::uintX_t |
| 52 | GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
| 53 | return this->getVA() + B.GotPltIndex * sizeof(uintX_t); |
| 54 | } |
| 55 | |
| 56 | template <class ELFT> void GotPltSection<ELFT>::finalize() { |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 57 | this->Header.sh_size = |
| 58 | (Target->getGotPltHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) { |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 62 | Target->writeGotPltHeaderEntries(Buf); |
| 63 | Buf += Target->getGotPltHeaderEntriesNum() * sizeof(uintX_t); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 64 | for (const SymbolBody *B : Entries) { |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 65 | Target->writeGotPltEntry(Buf, Out<ELFT>::Plt->getEntryAddr(*B)); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 66 | Buf += sizeof(uintX_t); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 71 | GotSection<ELFT>::GotSection() |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 72 | : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) { |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 73 | if (Config->EMachine == EM_MIPS) |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 74 | this->Header.sh_flags |= SHF_MIPS_GPREL; |
Rui Ueyama | 5f551ae | 2015-10-14 14:02:06 +0000 | [diff] [blame] | 75 | this->Header.sh_addralign = sizeof(uintX_t); |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 78 | template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) { |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 79 | Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 80 | Entries.push_back(Sym); |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 81 | } |
| 82 | |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 83 | template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) { |
| 84 | if (Sym->hasGlobalDynIndex()) |
| 85 | return false; |
| 86 | Sym->GlobalDynIndex = Target->getGotHeaderEntriesNum() + Entries.size(); |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 87 | // Global Dynamic TLS entries take two GOT slots. |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 88 | Entries.push_back(Sym); |
| 89 | Entries.push_back(nullptr); |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 90 | return true; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 91 | } |
| 92 | |
George Rimar | 95c1a58 | 2015-12-07 08:02:20 +0000 | [diff] [blame] | 93 | template <class ELFT> bool GotSection<ELFT>::addCurrentModuleTlsIndex() { |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 94 | if (LocalTlsIndexOff != uint32_t(-1)) |
| 95 | return false; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 96 | Entries.push_back(nullptr); |
| 97 | Entries.push_back(nullptr); |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 98 | LocalTlsIndexOff = (Entries.size() - 2) * sizeof(uintX_t); |
| 99 | return true; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 102 | template <class ELFT> |
| 103 | typename GotSection<ELFT>::uintX_t |
| 104 | GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
Rui Ueyama | 5f551ae | 2015-10-14 14:02:06 +0000 | [diff] [blame] | 105 | return this->getVA() + B.GotIndex * sizeof(uintX_t); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 108 | template <class ELFT> |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 109 | typename GotSection<ELFT>::uintX_t |
| 110 | GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const { |
| 111 | return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t); |
| 112 | } |
| 113 | |
| 114 | template <class ELFT> |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 115 | const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const { |
| 116 | return Entries.empty() ? nullptr : Entries.front(); |
| 117 | } |
| 118 | |
| 119 | template <class ELFT> |
| 120 | unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const { |
| 121 | // TODO: Update when the suppoort of GOT entries for local symbols is added. |
| 122 | return Target->getGotHeaderEntriesNum(); |
| 123 | } |
| 124 | |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 125 | template <class ELFT> void GotSection<ELFT>::finalize() { |
| 126 | this->Header.sh_size = |
| 127 | (Target->getGotHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t); |
| 128 | } |
| 129 | |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 130 | template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) { |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 131 | Target->writeGotHeaderEntries(Buf); |
| 132 | Buf += Target->getGotHeaderEntriesNum() * sizeof(uintX_t); |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 133 | for (const SymbolBody *B : Entries) { |
Rafael Espindola | e782f67 | 2015-10-07 03:56:05 +0000 | [diff] [blame] | 134 | uint8_t *Entry = Buf; |
| 135 | Buf += sizeof(uintX_t); |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 136 | if (!B) |
| 137 | continue; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 138 | // MIPS has special rules to fill up GOT entries. |
| 139 | // See "Global Offset Table" in Chapter 5 in the following document |
| 140 | // for detailed description: |
| 141 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 142 | // As the first approach, we can just store addresses for all symbols. |
| 143 | if (Config->EMachine != EM_MIPS && canBePreempted(B, false)) |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 144 | continue; // The dynamic linker will take care of it. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 145 | uintX_t VA = getSymVA<ELFT>(*B); |
Rafael Espindola | e782f67 | 2015-10-07 03:56:05 +0000 | [diff] [blame] | 146 | write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA); |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 150 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 151 | PltSection<ELFT>::PltSection() |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 152 | : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 153 | this->Header.sh_addralign = 16; |
| 154 | } |
| 155 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 156 | template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 242ddf4 | 2015-10-12 18:56:36 +0000 | [diff] [blame] | 157 | size_t Off = 0; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 158 | bool LazyReloc = Target->supportsLazyRelocations(); |
| 159 | if (LazyReloc) { |
| 160 | // First write PLT[0] entry which is special. |
| 161 | Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA()); |
| 162 | Off += Target->getPltZeroEntrySize(); |
| 163 | } |
George Rimar | fb5d7f2 | 2015-11-26 19:58:51 +0000 | [diff] [blame] | 164 | for (auto &I : Entries) { |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 165 | const SymbolBody *E = I.first; |
| 166 | unsigned RelOff = I.second; |
| 167 | uint64_t GotVA = |
| 168 | LazyReloc ? Out<ELFT>::GotPlt->getVA() : Out<ELFT>::Got->getVA(); |
| 169 | uint64_t GotE = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E) |
| 170 | : Out<ELFT>::Got->getEntryAddr(*E); |
Rui Ueyama | 242ddf4 | 2015-10-12 18:56:36 +0000 | [diff] [blame] | 171 | uint64_t Plt = this->getVA() + Off; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 172 | Target->writePltEntry(Buf + Off, GotVA, GotE, Plt, E->PltIndex, RelOff); |
Rui Ueyama | 242ddf4 | 2015-10-12 18:56:36 +0000 | [diff] [blame] | 173 | Off += Target->getPltEntrySize(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) { |
Rui Ueyama | 49c68a7 | 2015-10-09 00:42:06 +0000 | [diff] [blame] | 178 | Sym->PltIndex = Entries.size(); |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 179 | unsigned RelOff = Target->supportsLazyRelocations() |
| 180 | ? Out<ELFT>::RelaPlt->getRelocOffset() |
| 181 | : Out<ELFT>::RelaDyn->getRelocOffset(); |
| 182 | Entries.push_back(std::make_pair(Sym, RelOff)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | template <class ELFT> |
| 186 | typename PltSection<ELFT>::uintX_t |
| 187 | PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const { |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 188 | return this->getVA() + Target->getPltZeroEntrySize() + |
| 189 | B.PltIndex * Target->getPltEntrySize(); |
| 190 | } |
| 191 | |
| 192 | template <class ELFT> void PltSection<ELFT>::finalize() { |
| 193 | this->Header.sh_size = Target->getPltZeroEntrySize() + |
| 194 | Entries.size() * Target->getPltEntrySize(); |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | template <class ELFT> |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 198 | RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela) |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 199 | : OutputSectionBase<ELFT>(Name, IsRela ? SHT_RELA : SHT_REL, SHF_ALLOC), |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 200 | IsRela(IsRela) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 201 | this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 202 | this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 203 | } |
| 204 | |
George Rimar | 5828c23 | 2015-11-30 17:49:19 +0000 | [diff] [blame] | 205 | // Applies corresponding symbol and type for dynamic tls relocation. |
| 206 | // Returns true if relocation was handled. |
| 207 | template <class ELFT> |
| 208 | bool RelocationSection<ELFT>::applyTlsDynamicReloc(SymbolBody *Body, |
| 209 | uint32_t Type, Elf_Rel *P, |
| 210 | Elf_Rel *N) { |
| 211 | if (Target->isTlsLocalDynamicReloc(Type)) { |
| 212 | P->setSymbolAndType(0, Target->getTlsModuleIndexReloc(), Config->Mips64EL); |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 213 | P->r_offset = Out<ELFT>::Got->getLocalTlsIndexVA(); |
George Rimar | 5828c23 | 2015-11-30 17:49:19 +0000 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | |
George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 217 | if (!Body || !Target->isTlsGlobalDynamicReloc(Type)) |
| 218 | return false; |
| 219 | |
| 220 | if (Target->isTlsOptimized(Type, Body)) { |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 221 | P->setSymbolAndType(Body->DynamicSymbolTableIndex, |
George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 222 | Target->getTlsGotReloc(), Config->Mips64EL); |
| 223 | P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body); |
George Rimar | 5828c23 | 2015-11-30 17:49:19 +0000 | [diff] [blame] | 224 | return true; |
| 225 | } |
George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 226 | |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 227 | P->setSymbolAndType(Body->DynamicSymbolTableIndex, |
George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 228 | Target->getTlsModuleIndexReloc(), Config->Mips64EL); |
| 229 | P->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body); |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 230 | N->setSymbolAndType(Body->DynamicSymbolTableIndex, |
George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 231 | Target->getTlsOffsetReloc(), Config->Mips64EL); |
| 232 | N->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body) + sizeof(uintX_t); |
| 233 | return true; |
George Rimar | 5828c23 | 2015-11-30 17:49:19 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 236 | template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 237 | for (const DynamicReloc<ELFT> &Rel : Relocs) { |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 238 | auto *P = reinterpret_cast<Elf_Rel *>(Buf); |
Rui Ueyama | c7b073a | 2016-01-05 16:35:48 +0000 | [diff] [blame] | 239 | Buf += IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
Rafael Espindola | 50534c2 | 2015-09-22 17:49:38 +0000 | [diff] [blame] | 240 | |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 241 | // Skip placeholder for global dynamic TLS relocation pair. It was already |
| 242 | // handled by the previous relocation. |
Rui Ueyama | c7b073a | 2016-01-05 16:35:48 +0000 | [diff] [blame] | 243 | if (!Rel.C) |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 244 | continue; |
| 245 | |
| 246 | InputSectionBase<ELFT> &C = *Rel.C; |
| 247 | const Elf_Rel &RI = *Rel.RI; |
Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame] | 248 | uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); |
Rafael Espindola | 41127ad | 2015-10-05 22:49:16 +0000 | [diff] [blame] | 249 | const ObjectFile<ELFT> &File = *C.getFile(); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 250 | SymbolBody *Body = File.getSymbolBody(SymIndex); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 251 | if (Body) |
| 252 | Body = Body->repl(); |
Rafael Espindola | 41127ad | 2015-10-05 22:49:16 +0000 | [diff] [blame] | 253 | |
Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame] | 254 | uint32_t Type = RI.getType(Config->Mips64EL); |
George Rimar | 5828c23 | 2015-11-30 17:49:19 +0000 | [diff] [blame] | 255 | if (applyTlsDynamicReloc(Body, Type, P, reinterpret_cast<Elf_Rel *>(Buf))) |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 256 | continue; |
Rui Ueyama | 02dfd49 | 2015-12-17 01:18:40 +0000 | [diff] [blame] | 257 | bool NeedsCopy = Body && Target->needsCopyRel(Type, *Body); |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 258 | bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body); |
Rui Ueyama | c7b073a | 2016-01-05 16:35:48 +0000 | [diff] [blame] | 259 | bool CBP = canBePreempted(Body, NeedsGot); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 260 | bool LazyReloc = Body && Target->supportsLazyRelocations() && |
| 261 | Target->relocNeedsPlt(Type, *Body); |
George Rimar | 6f17e09 | 2015-12-17 09:32:21 +0000 | [diff] [blame] | 262 | bool IsDynRelative = Type == Target->getRelativeReloc(); |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 263 | |
Rui Ueyama | c7b073a | 2016-01-05 16:35:48 +0000 | [diff] [blame] | 264 | unsigned Sym = CBP ? Body->DynamicSymbolTableIndex : 0; |
George Rimar | c7dc0be | 2015-12-15 08:48:39 +0000 | [diff] [blame] | 265 | unsigned Reloc; |
Rui Ueyama | c7b073a | 2016-01-05 16:35:48 +0000 | [diff] [blame] | 266 | if (!CBP && Body && isGnuIFunc<ELFT>(*Body)) |
George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 267 | Reloc = Target->getIRelativeReloc(); |
Rui Ueyama | c7b073a | 2016-01-05 16:35:48 +0000 | [diff] [blame] | 268 | else if (!CBP || IsDynRelative) |
George Rimar | c7dc0be | 2015-12-15 08:48:39 +0000 | [diff] [blame] | 269 | Reloc = Target->getRelativeReloc(); |
| 270 | else if (LazyReloc) |
| 271 | Reloc = Target->getPltReloc(); |
| 272 | else if (NeedsGot) |
Rui Ueyama | 62d0e32 | 2015-12-17 00:04:18 +0000 | [diff] [blame] | 273 | Reloc = Body->isTls() ? Target->getTlsGotReloc() : Target->getGotReloc(); |
George Rimar | c7dc0be | 2015-12-15 08:48:39 +0000 | [diff] [blame] | 274 | else if (NeedsCopy) |
| 275 | Reloc = Target->getCopyReloc(); |
| 276 | else |
| 277 | Reloc = Target->getDynReloc(Type); |
| 278 | P->setSymbolAndType(Sym, Reloc, Config->Mips64EL); |
Rafael Espindola | 52dca34 | 2015-10-07 00:58:20 +0000 | [diff] [blame] | 279 | |
George Rimar | 4b5346f | 2015-12-29 16:17:32 +0000 | [diff] [blame] | 280 | if (LazyReloc) |
| 281 | P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body); |
| 282 | else if (NeedsGot) |
| 283 | P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body); |
| 284 | else if (NeedsCopy) |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 285 | P->r_offset = Out<ELFT>::Bss->getVA() + |
Rui Ueyama | c7b073a | 2016-01-05 16:35:48 +0000 | [diff] [blame] | 286 | cast<SharedSymbol<ELFT>>(Body)->OffsetInBss; |
George Rimar | 4b5346f | 2015-12-29 16:17:32 +0000 | [diff] [blame] | 287 | else |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 288 | P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA(); |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 289 | |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 290 | uintX_t OrigAddend = 0; |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 291 | if (IsRela && !NeedsGot) |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 292 | OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend; |
Rafael Espindola | 4975752 | 2015-10-19 19:58:18 +0000 | [diff] [blame] | 293 | |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 294 | uintX_t Addend; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 295 | if (NeedsCopy) |
| 296 | Addend = 0; |
Rui Ueyama | c7b073a | 2016-01-05 16:35:48 +0000 | [diff] [blame] | 297 | else if (CBP || IsDynRelative) |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 298 | Addend = OrigAddend; |
Rui Ueyama | b7f2867 | 2015-10-19 20:31:49 +0000 | [diff] [blame] | 299 | else if (Body) |
Rafael Espindola | 0234640 | 2015-12-21 20:18:04 +0000 | [diff] [blame] | 300 | Addend = getSymVA<ELFT>(*Body) + OrigAddend; |
Rui Ueyama | b7f2867 | 2015-10-19 20:31:49 +0000 | [diff] [blame] | 301 | else if (IsRela) |
George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 302 | Addend = |
| 303 | getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI), |
| 304 | getAddend<ELFT>(static_cast<const Elf_Rela &>(RI))); |
Rui Ueyama | b7f2867 | 2015-10-19 20:31:49 +0000 | [diff] [blame] | 305 | else |
George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 306 | Addend = getLocalRelTarget(File, RI, 0); |
Rafael Espindola | 52dca34 | 2015-10-07 00:58:20 +0000 | [diff] [blame] | 307 | |
| 308 | if (IsRela) |
| 309 | static_cast<Elf_Rela *>(P)->r_addend = Addend; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 313 | template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() { |
| 314 | const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 315 | return EntrySize * Relocs.size(); |
| 316 | } |
| 317 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 318 | template <class ELFT> void RelocationSection<ELFT>::finalize() { |
George Rimar | a07ff66 | 2015-12-21 10:12:06 +0000 | [diff] [blame] | 319 | this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex |
| 320 | : Out<ELFT>::DynSymTab->SectionIndex; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 321 | this->Header.sh_size = Relocs.size() * this->Header.sh_entsize; |
| 322 | } |
| 323 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 324 | template <class ELFT> |
| 325 | InterpSection<ELFT>::InterpSection() |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 326 | : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 327 | this->Header.sh_size = Config->DynamicLinker.size() + 1; |
| 328 | this->Header.sh_addralign = 1; |
| 329 | } |
| 330 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 331 | template <class ELFT> |
| 332 | void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) { |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 333 | Header.sh_name = Out<ELFT>::ShStrTab->addString(Name); |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 334 | *SHdr = Header; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 337 | template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 338 | memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size()); |
| 339 | } |
| 340 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 341 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 342 | HashTableSection<ELFT>::HashTableSection() |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 343 | : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) { |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 344 | this->Header.sh_entsize = sizeof(Elf_Word); |
| 345 | this->Header.sh_addralign = sizeof(Elf_Word); |
| 346 | } |
| 347 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 348 | static uint32_t hashSysv(StringRef Name) { |
Rui Ueyama | 5f1eee1a | 2015-10-15 21:27:17 +0000 | [diff] [blame] | 349 | uint32_t H = 0; |
| 350 | for (char C : Name) { |
| 351 | H = (H << 4) + C; |
| 352 | uint32_t G = H & 0xf0000000; |
| 353 | if (G) |
| 354 | H ^= G >> 24; |
| 355 | H &= ~G; |
| 356 | } |
| 357 | return H; |
| 358 | } |
| 359 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 360 | template <class ELFT> void HashTableSection<ELFT>::finalize() { |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 361 | this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 362 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 363 | unsigned NumEntries = 2; // nbucket and nchain. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 364 | NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries. |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 365 | |
| 366 | // Create as many buckets as there are symbols. |
| 367 | // FIXME: This is simplistic. We can try to optimize it, but implementing |
| 368 | // support for SHT_GNU_HASH is probably even more profitable. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 369 | NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 370 | this->Header.sh_size = NumEntries * sizeof(Elf_Word); |
| 371 | } |
| 372 | |
| 373 | template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 374 | unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 375 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 376 | *P++ = NumSymbols; // nbucket |
| 377 | *P++ = NumSymbols; // nchain |
| 378 | |
| 379 | Elf_Word *Buckets = P; |
| 380 | Elf_Word *Chains = P + NumSymbols; |
| 381 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 382 | for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) { |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 383 | StringRef Name = Body->getName(); |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 384 | unsigned I = Body->DynamicSymbolTableIndex; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 385 | uint32_t Hash = hashSysv(Name) % NumSymbols; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 386 | Chains[I] = Buckets[Hash]; |
| 387 | Buckets[Hash] = I; |
| 388 | } |
| 389 | } |
| 390 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 391 | static uint32_t hashGnu(StringRef Name) { |
| 392 | uint32_t H = 5381; |
| 393 | for (uint8_t C : Name) |
| 394 | H = (H << 5) + H + C; |
| 395 | return H; |
| 396 | } |
| 397 | |
| 398 | template <class ELFT> |
| 399 | GnuHashTableSection<ELFT>::GnuHashTableSection() |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 400 | : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) { |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 401 | this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4; |
| 402 | this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 403 | } |
| 404 | |
| 405 | template <class ELFT> |
| 406 | unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) { |
| 407 | if (!NumHashed) |
| 408 | return 0; |
| 409 | |
| 410 | // These values are prime numbers which are not greater than 2^(N-1) + 1. |
| 411 | // In result, for any particular NumHashed we return a prime number |
| 412 | // which is not greater than NumHashed. |
| 413 | static const unsigned Primes[] = { |
| 414 | 1, 1, 3, 3, 7, 13, 31, 61, 127, 251, |
| 415 | 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071}; |
| 416 | |
| 417 | return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed), |
| 418 | array_lengthof(Primes) - 1)]; |
| 419 | } |
| 420 | |
| 421 | // Bloom filter estimation: at least 8 bits for each hashed symbol. |
| 422 | // GNU Hash table requirement: it should be a power of 2, |
| 423 | // the minimum value is 1, even for an empty table. |
| 424 | // Expected results for a 32-bit target: |
| 425 | // calcMaskWords(0..4) = 1 |
| 426 | // calcMaskWords(5..8) = 2 |
| 427 | // calcMaskWords(9..16) = 4 |
| 428 | // For a 64-bit target: |
| 429 | // calcMaskWords(0..8) = 1 |
| 430 | // calcMaskWords(9..16) = 2 |
| 431 | // calcMaskWords(17..32) = 4 |
| 432 | template <class ELFT> |
| 433 | unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) { |
| 434 | if (!NumHashed) |
| 435 | return 1; |
| 436 | return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off)); |
| 437 | } |
| 438 | |
| 439 | template <class ELFT> void GnuHashTableSection<ELFT>::finalize() { |
Igor Kudrin | 2169b1b | 2015-11-02 10:46:14 +0000 | [diff] [blame] | 440 | unsigned NumHashed = HashedSymbols.size(); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 441 | NBuckets = calcNBuckets(NumHashed); |
| 442 | MaskWords = calcMaskWords(NumHashed); |
| 443 | // Second hash shift estimation: just predefined values. |
| 444 | Shift2 = ELFT::Is64Bits ? 6 : 5; |
| 445 | |
| 446 | this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex; |
| 447 | this->Header.sh_size = sizeof(Elf_Word) * 4 // Header |
| 448 | + sizeof(Elf_Off) * MaskWords // Bloom Filter |
| 449 | + sizeof(Elf_Word) * NBuckets // Hash Buckets |
| 450 | + sizeof(Elf_Word) * NumHashed; // Hash Values |
| 451 | } |
| 452 | |
| 453 | template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 454 | writeHeader(Buf); |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 455 | if (HashedSymbols.empty()) |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 456 | return; |
| 457 | writeBloomFilter(Buf); |
| 458 | writeHashTable(Buf); |
| 459 | } |
| 460 | |
| 461 | template <class ELFT> |
| 462 | void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) { |
| 463 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 464 | *P++ = NBuckets; |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 465 | *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size(); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 466 | *P++ = MaskWords; |
| 467 | *P++ = Shift2; |
| 468 | Buf = reinterpret_cast<uint8_t *>(P); |
| 469 | } |
| 470 | |
| 471 | template <class ELFT> |
| 472 | void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) { |
| 473 | unsigned C = sizeof(Elf_Off) * 8; |
| 474 | |
| 475 | auto *Masks = reinterpret_cast<Elf_Off *>(Buf); |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 476 | for (const HashedSymbolData &Item : HashedSymbols) { |
| 477 | size_t Pos = (Item.Hash / C) & (MaskWords - 1); |
| 478 | uintX_t V = (uintX_t(1) << (Item.Hash % C)) | |
| 479 | (uintX_t(1) << ((Item.Hash >> Shift2) % C)); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 480 | Masks[Pos] |= V; |
| 481 | } |
| 482 | Buf += sizeof(Elf_Off) * MaskWords; |
| 483 | } |
| 484 | |
| 485 | template <class ELFT> |
| 486 | void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) { |
| 487 | Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf); |
| 488 | Elf_Word *Values = Buckets + NBuckets; |
| 489 | |
| 490 | int PrevBucket = -1; |
| 491 | int I = 0; |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 492 | for (const HashedSymbolData &Item : HashedSymbols) { |
| 493 | int Bucket = Item.Hash % NBuckets; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 494 | assert(PrevBucket <= Bucket); |
| 495 | if (Bucket != PrevBucket) { |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 496 | Buckets[Bucket] = Item.Body->DynamicSymbolTableIndex; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 497 | PrevBucket = Bucket; |
| 498 | if (I > 0) |
| 499 | Values[I - 1] |= 1; |
| 500 | } |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 501 | Values[I] = Item.Hash & ~1; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 502 | ++I; |
| 503 | } |
| 504 | if (I > 0) |
| 505 | Values[I - 1] |= 1; |
| 506 | } |
| 507 | |
Rafael Espindola | 31f8888 | 2015-11-02 14:33:11 +0000 | [diff] [blame] | 508 | static bool includeInGnuHashTable(SymbolBody *B) { |
| 509 | // Assume that includeInDynamicSymtab() is already checked. |
| 510 | return !B->isUndefined(); |
| 511 | } |
| 512 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 513 | template <class ELFT> |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 514 | void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) { |
| 515 | std::vector<SymbolBody *> NotHashed; |
| 516 | NotHashed.reserve(Symbols.size()); |
| 517 | HashedSymbols.reserve(Symbols.size()); |
| 518 | for (SymbolBody *B : Symbols) { |
| 519 | if (includeInGnuHashTable(B)) |
| 520 | HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())}); |
| 521 | else |
| 522 | NotHashed.push_back(B); |
| 523 | } |
| 524 | if (HashedSymbols.empty()) |
| 525 | return; |
| 526 | |
| 527 | unsigned NBuckets = calcNBuckets(HashedSymbols.size()); |
| 528 | std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(), |
| 529 | [&](const HashedSymbolData &L, const HashedSymbolData &R) { |
| 530 | return L.Hash % NBuckets < R.Hash % NBuckets; |
| 531 | }); |
| 532 | |
| 533 | Symbols = std::move(NotHashed); |
| 534 | for (const HashedSymbolData &Item : HashedSymbols) |
| 535 | Symbols.push_back(Item.Body); |
| 536 | } |
| 537 | |
| 538 | template <class ELFT> |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 539 | DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab) |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 540 | : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE), |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 541 | SymTab(SymTab) { |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 542 | Elf_Shdr &Header = this->Header; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 543 | Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
| 544 | Header.sh_entsize = ELFT::Is64Bits ? 16 : 8; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 545 | |
| 546 | // .dynamic section is not writable on MIPS. |
| 547 | // See "Special Section" in Chapter 4 in the following document: |
| 548 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 549 | if (Config->EMachine == EM_MIPS) |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 550 | Header.sh_flags = SHF_ALLOC; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 553 | template <class ELFT> void DynamicSection<ELFT>::finalize() { |
Michael J. Spencer | 52bf0eb | 2015-10-01 21:15:02 +0000 | [diff] [blame] | 554 | if (this->Header.sh_size) |
| 555 | return; // Already finalized. |
| 556 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 557 | Elf_Shdr &Header = this->Header; |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 558 | Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 559 | |
| 560 | unsigned NumEntries = 0; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 561 | if (Out<ELFT>::RelaDyn->hasRelocs()) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 562 | ++NumEntries; // DT_RELA / DT_REL |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 563 | ++NumEntries; // DT_RELASZ / DT_RELSZ |
| 564 | ++NumEntries; // DT_RELAENT / DT_RELENT |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 565 | } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 566 | if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) { |
| 567 | ++NumEntries; // DT_JMPREL |
| 568 | ++NumEntries; // DT_PLTRELSZ |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 569 | ++NumEntries; // DT_PLTGOT / DT_MIPS_PLTGOT |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 570 | ++NumEntries; // DT_PLTREL |
| 571 | } |
| 572 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 573 | ++NumEntries; // DT_SYMTAB |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 574 | ++NumEntries; // DT_SYMENT |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 575 | ++NumEntries; // DT_STRTAB |
| 576 | ++NumEntries; // DT_STRSZ |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 577 | if (Out<ELFT>::GnuHashTab) |
| 578 | ++NumEntries; // DT_GNU_HASH |
| 579 | if (Out<ELFT>::HashTab) |
| 580 | ++NumEntries; // DT_HASH |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 581 | |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 582 | if (!Config->RPath.empty()) { |
Davide Italiano | c39c75d | 2015-10-06 16:20:00 +0000 | [diff] [blame] | 583 | ++NumEntries; // DT_RUNPATH / DT_RPATH |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 584 | Out<ELFT>::DynStrTab->reserve(Config->RPath); |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | if (!Config->SoName.empty()) { |
| 588 | ++NumEntries; // DT_SONAME |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 589 | Out<ELFT>::DynStrTab->reserve(Config->SoName); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 592 | if (PreInitArraySec) |
| 593 | NumEntries += 2; |
| 594 | if (InitArraySec) |
| 595 | NumEntries += 2; |
| 596 | if (FiniArraySec) |
| 597 | NumEntries += 2; |
| 598 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 599 | for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) { |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 600 | if (!F->isNeeded()) |
| 601 | continue; |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 602 | Out<ELFT>::DynStrTab->reserve(F->getSoName()); |
Rui Ueyama | 6ccc8ca | 2015-10-09 20:32:54 +0000 | [diff] [blame] | 603 | ++NumEntries; |
| 604 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 605 | |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 606 | if (Symbol *S = SymTab.getSymbols().lookup(Config->Init)) |
Rafael Espindola | 167e62f | 2015-12-21 20:59:29 +0000 | [diff] [blame] | 607 | InitSym = S->Body; |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 608 | if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini)) |
Rafael Espindola | 167e62f | 2015-12-21 20:59:29 +0000 | [diff] [blame] | 609 | FiniSym = S->Body; |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 610 | if (InitSym) |
| 611 | ++NumEntries; // DT_INIT |
| 612 | if (FiniSym) |
| 613 | ++NumEntries; // DT_FINI |
Rui Ueyama | c96d0dd | 2015-10-21 17:47:10 +0000 | [diff] [blame] | 614 | |
| 615 | if (Config->Bsymbolic) |
| 616 | DtFlags |= DF_SYMBOLIC; |
| 617 | if (Config->ZNodelete) |
| 618 | DtFlags1 |= DF_1_NODELETE; |
| 619 | if (Config->ZNow) { |
| 620 | DtFlags |= DF_BIND_NOW; |
| 621 | DtFlags1 |= DF_1_NOW; |
| 622 | } |
| 623 | if (Config->ZOrigin) { |
| 624 | DtFlags |= DF_ORIGIN; |
| 625 | DtFlags1 |= DF_1_ORIGIN; |
| 626 | } |
| 627 | |
| 628 | if (DtFlags) |
Davide Italiano | 58cbaf0 | 2015-10-19 23:32:16 +0000 | [diff] [blame] | 629 | ++NumEntries; // DT_FLAGS |
Rui Ueyama | c96d0dd | 2015-10-21 17:47:10 +0000 | [diff] [blame] | 630 | if (DtFlags1) |
George Rimar | 97aad17 | 2015-10-07 15:00:21 +0000 | [diff] [blame] | 631 | ++NumEntries; // DT_FLAGS_1 |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 632 | |
Ed Maste | f5d3cf6 | 2016-01-06 15:52:27 +0000 | [diff] [blame] | 633 | if (!Config->Entry.empty()) |
| 634 | ++NumEntries; // DT_DEBUG |
| 635 | |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 636 | if (Config->EMachine == EM_MIPS) { |
| 637 | ++NumEntries; // DT_MIPS_RLD_VERSION |
| 638 | ++NumEntries; // DT_MIPS_FLAGS |
| 639 | ++NumEntries; // DT_MIPS_BASE_ADDRESS |
| 640 | ++NumEntries; // DT_MIPS_SYMTABNO |
| 641 | ++NumEntries; // DT_MIPS_LOCAL_GOTNO |
| 642 | ++NumEntries; // DT_MIPS_GOTSYM; |
| 643 | ++NumEntries; // DT_PLTGOT |
| 644 | if (Out<ELFT>::MipsRldMap) |
| 645 | ++NumEntries; // DT_MIPS_RLD_MAP |
| 646 | } |
| 647 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 648 | ++NumEntries; // DT_NULL |
| 649 | |
| 650 | Header.sh_size = NumEntries * Header.sh_entsize; |
| 651 | } |
| 652 | |
| 653 | template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 654 | auto *P = reinterpret_cast<Elf_Dyn *>(Buf); |
| 655 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 656 | auto WritePtr = [&](int32_t Tag, uint64_t Val) { |
| 657 | P->d_tag = Tag; |
| 658 | P->d_un.d_ptr = Val; |
| 659 | ++P; |
| 660 | }; |
| 661 | |
| 662 | auto WriteVal = [&](int32_t Tag, uint32_t Val) { |
| 663 | P->d_tag = Tag; |
| 664 | P->d_un.d_val = Val; |
| 665 | ++P; |
| 666 | }; |
| 667 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 668 | if (Out<ELFT>::RelaDyn->hasRelocs()) { |
| 669 | bool IsRela = Out<ELFT>::RelaDyn->isRela(); |
| 670 | WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA()); |
| 671 | WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 672 | WriteVal(IsRela ? DT_RELAENT : DT_RELENT, |
| 673 | IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 674 | } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 675 | if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) { |
| 676 | WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA()); |
| 677 | WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()); |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 678 | // On MIPS, the address of the .got.plt section is stored in |
| 679 | // the DT_MIPS_PLTGOT entry because the DT_PLTGOT entry points to |
| 680 | // the .got section. See "Dynamic Section" in the following document: |
| 681 | // https://sourceware.org/ml/binutils/2008-07/txt00000.txt |
| 682 | WritePtr((Config->EMachine == EM_MIPS) ? DT_MIPS_PLTGOT : DT_PLTGOT, |
| 683 | Out<ELFT>::GotPlt->getVA()); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 684 | WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL); |
| 685 | } |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 686 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 687 | WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA()); |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 688 | WritePtr(DT_SYMENT, sizeof(Elf_Sym)); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 689 | WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA()); |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 690 | WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->getSize()); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 691 | if (Out<ELFT>::GnuHashTab) |
| 692 | WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA()); |
| 693 | if (Out<ELFT>::HashTab) |
| 694 | WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 695 | |
Rui Ueyama | dfa577b | 2015-11-19 23:30:10 +0000 | [diff] [blame] | 696 | // If --enable-new-dtags is set, lld emits DT_RUNPATH |
| 697 | // instead of DT_RPATH. The two tags are functionally |
| 698 | // equivalent except for the following: |
| 699 | // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while |
| 700 | // DT_RPATH is searched before. |
| 701 | // - DT_RUNPATH is used only to search for direct |
| 702 | // dependencies of the object it's contained in, while |
| 703 | // DT_RPATH is used for indirect dependencies as well. |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 704 | if (!Config->RPath.empty()) |
Davide Italiano | c39c75d | 2015-10-06 16:20:00 +0000 | [diff] [blame] | 705 | WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 706 | Out<ELFT>::DynStrTab->addString(Config->RPath)); |
Rui Ueyama | 2dfd74f | 2015-09-30 21:57:53 +0000 | [diff] [blame] | 707 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 708 | if (!Config->SoName.empty()) |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 709 | WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 710 | |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 711 | auto WriteArray = [&](int32_t T1, int32_t T2, |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 712 | const OutputSectionBase<ELFT> *Sec) { |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 713 | if (!Sec) |
| 714 | return; |
| 715 | WritePtr(T1, Sec->getVA()); |
| 716 | WriteVal(T2, Sec->getSize()); |
| 717 | }; |
| 718 | WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec); |
| 719 | WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec); |
| 720 | WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec); |
| 721 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 722 | for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 723 | if (F->isNeeded()) |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 724 | WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 725 | |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 726 | if (InitSym) |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 727 | WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym)); |
Igor Kudrin | b1f2b51 | 2015-10-05 10:29:46 +0000 | [diff] [blame] | 728 | if (FiniSym) |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 729 | WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym)); |
Rui Ueyama | c96d0dd | 2015-10-21 17:47:10 +0000 | [diff] [blame] | 730 | if (DtFlags) |
| 731 | WriteVal(DT_FLAGS, DtFlags); |
| 732 | if (DtFlags1) |
| 733 | WriteVal(DT_FLAGS_1, DtFlags1); |
Ed Maste | f5d3cf6 | 2016-01-06 15:52:27 +0000 | [diff] [blame] | 734 | if (!Config->Entry.empty()) |
| 735 | WriteVal(DT_DEBUG, 0); |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 736 | |
| 737 | // See "Dynamic Section" in Chapter 5 in the following document |
| 738 | // for detailed description: |
| 739 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 740 | if (Config->EMachine == EM_MIPS) { |
| 741 | WriteVal(DT_MIPS_RLD_VERSION, 1); |
| 742 | WriteVal(DT_MIPS_FLAGS, RHF_NOTPOT); |
| 743 | WritePtr(DT_MIPS_BASE_ADDRESS, Target->getVAStart()); |
| 744 | WriteVal(DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()); |
| 745 | WriteVal(DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()); |
| 746 | if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry()) |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 747 | WriteVal(DT_MIPS_GOTSYM, B->DynamicSymbolTableIndex); |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 748 | else |
| 749 | WriteVal(DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()); |
| 750 | WritePtr(DT_PLTGOT, Out<ELFT>::Got->getVA()); |
| 751 | if (Out<ELFT>::MipsRldMap) |
| 752 | WritePtr(DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap->getVA()); |
| 753 | } |
| 754 | |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 755 | WriteVal(DT_NULL, 0); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 759 | OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type, |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 760 | uintX_t sh_flags) |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 761 | : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {} |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 762 | |
| 763 | template <class ELFT> |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 764 | void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { |
| 765 | auto *S = cast<InputSection<ELFT>>(C); |
| 766 | Sections.push_back(S); |
| 767 | S->OutSec = this; |
| 768 | uint32_t Align = S->getAlign(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 769 | if (Align > this->Header.sh_addralign) |
| 770 | this->Header.sh_addralign = Align; |
| 771 | |
| 772 | uintX_t Off = this->Header.sh_size; |
Rui Ueyama | f71358d | 2016-01-06 23:25:42 +0000 | [diff] [blame] | 773 | Off = align(Off, Align); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 774 | S->OutSecOff = Off; |
| 775 | Off += S->getSize(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 776 | this->Header.sh_size = Off; |
| 777 | } |
| 778 | |
| 779 | template <class ELFT> |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 780 | typename ELFFile<ELFT>::uintX_t elf2::getSymVA(const SymbolBody &S) { |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 781 | switch (S.kind()) { |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 782 | case SymbolBody::DefinedSyntheticKind: { |
| 783 | auto &D = cast<DefinedSynthetic<ELFT>>(S); |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 784 | return D.Section.getVA() + D.Value; |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 785 | } |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 786 | case SymbolBody::DefinedRegularKind: { |
| 787 | const auto &DR = cast<DefinedRegular<ELFT>>(S); |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 788 | InputSectionBase<ELFT> *SC = DR.Section; |
| 789 | if (!SC) |
| 790 | return DR.Sym.st_value; |
Tom Stellard | 80efb16 | 2016-01-07 03:59:08 +0000 | [diff] [blame] | 791 | |
| 792 | // Symbol offsets for AMDGPU need to be the offset in bytes of the symbol |
| 793 | // from the beginning of the section. |
| 794 | if (Config->EMachine == EM_AMDGPU) |
| 795 | return SC->getOffset(DR.Sym); |
Michael J. Spencer | d77f0d2 | 2015-11-03 22:39:09 +0000 | [diff] [blame] | 796 | if (DR.Sym.getType() == STT_TLS) |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 797 | return SC->OutSec->getVA() + SC->getOffset(DR.Sym) - |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 798 | Out<ELFT>::TlsPhdr->p_vaddr; |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 799 | return SC->OutSec->getVA() + SC->getOffset(DR.Sym); |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 800 | } |
| 801 | case SymbolBody::DefinedCommonKind: |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 802 | return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(S).OffsetInBss; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 803 | case SymbolBody::SharedKind: { |
| 804 | auto &SS = cast<SharedSymbol<ELFT>>(S); |
Rui Ueyama | bb93606 | 2015-12-17 01:14:23 +0000 | [diff] [blame] | 805 | if (SS.NeedsCopy) |
Rui Ueyama | e57c487 | 2016-01-05 16:35:43 +0000 | [diff] [blame] | 806 | return Out<ELFT>::Bss->getVA() + SS.OffsetInBss; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 807 | return 0; |
| 808 | } |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 809 | case SymbolBody::UndefinedElfKind: |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 810 | case SymbolBody::UndefinedKind: |
| 811 | return 0; |
| 812 | case SymbolBody::LazyKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 813 | assert(S.isUsedInRegularObj() && "Lazy symbol reached writer"); |
| 814 | return 0; |
Rafael Espindola | cd076f0 | 2015-09-25 18:19:03 +0000 | [diff] [blame] | 815 | } |
Denis Protivensky | 92aa1c0 | 2015-10-07 08:21:34 +0000 | [diff] [blame] | 816 | llvm_unreachable("Invalid symbol kind"); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 819 | // Returns a VA which a relocatin RI refers to. Used only for local symbols. |
| 820 | // For non-local symbols, use getSymVA instead. |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 821 | template <class ELFT, bool IsRela> |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 822 | typename ELFFile<ELFT>::uintX_t |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 823 | elf2::getLocalRelTarget(const ObjectFile<ELFT> &File, |
| 824 | const Elf_Rel_Impl<ELFT, IsRela> &RI, |
| 825 | typename ELFFile<ELFT>::uintX_t Addend) { |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 826 | typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 827 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 828 | |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 829 | // PPC64 has a special relocation representing the TOC base pointer |
| 830 | // that does not have a corresponding symbol. |
Hal Finkel | 230c5c5 | 2015-10-16 22:37:32 +0000 | [diff] [blame] | 831 | if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 832 | return getPPC64TocBase() + Addend; |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 833 | |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 834 | const Elf_Sym *Sym = |
| 835 | File.getObj().getRelocationSymbol(&RI, File.getSymbolTable()); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 836 | |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 837 | if (!Sym) |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 838 | error("Unsupported relocation without symbol"); |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 839 | |
Michael J. Spencer | dc9c5df | 2015-11-11 01:28:23 +0000 | [diff] [blame] | 840 | InputSectionBase<ELFT> *Section = File.getSection(*Sym); |
| 841 | |
| 842 | if (Sym->getType() == STT_TLS) |
| 843 | return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) - |
George Rimar | cc06a6f | 2015-11-29 14:14:20 +0000 | [diff] [blame] | 844 | Out<ELFT>::TlsPhdr->p_vaddr; |
Michael J. Spencer | dc9c5df | 2015-11-11 01:28:23 +0000 | [diff] [blame] | 845 | |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 846 | // According to the ELF spec reference to a local symbol from outside |
| 847 | // the group are not allowed. Unfortunately .eh_frame breaks that rule |
| 848 | // and must be treated specially. For now we just replace the symbol with |
| 849 | // 0. |
Rafael Espindola | 8ea46e0 | 2015-11-09 17:44:10 +0000 | [diff] [blame] | 850 | if (Section == &InputSection<ELFT>::Discarded || !Section->isLive()) |
Rafael Espindola | 932efcf | 2015-10-19 20:24:44 +0000 | [diff] [blame] | 851 | return Addend; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 852 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 853 | uintX_t VA = Section->OutSec->getVA(); |
| 854 | if (isa<InputSection<ELFT>>(Section)) |
| 855 | return VA + Section->getOffset(*Sym) + Addend; |
| 856 | |
| 857 | uintX_t Offset = Sym->st_value; |
| 858 | if (Sym->getType() == STT_SECTION) { |
| 859 | Offset += Addend; |
Rafael Espindola | f5af835 | 2015-10-20 22:08:49 +0000 | [diff] [blame] | 860 | Addend = 0; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 861 | } |
Rafael Espindola | 7f040bf | 2015-12-25 01:00:41 +0000 | [diff] [blame] | 862 | return VA + Section->getOffset(Offset) + Addend; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 865 | // Returns true if a symbol can be replaced at load-time by a symbol |
| 866 | // with the same name defined in other ELF executable or DSO. |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 867 | bool elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) { |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 868 | if (!Body) |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 869 | return false; // Body is a local symbol. |
Rafael Espindola | cea0b3b | 2015-10-07 04:22:55 +0000 | [diff] [blame] | 870 | if (Body->isShared()) |
| 871 | return true; |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 872 | |
| 873 | if (Body->isUndefined()) { |
| 874 | if (!Body->isWeak()) |
| 875 | return true; |
| 876 | |
| 877 | // This is an horrible corner case. Ideally we would like to say that any |
| 878 | // undefined symbol can be preempted so that the dynamic linker has a |
| 879 | // chance of finding it at runtime. |
| 880 | // |
| 881 | // The problem is that the code sequence used to test for weak undef |
| 882 | // functions looks like |
| 883 | // if (func) func() |
| 884 | // If the code is -fPIC the first reference is a load from the got and |
| 885 | // everything works. |
| 886 | // If the code is not -fPIC there is no reasonable way to solve it: |
| 887 | // * A relocation writing to the text segment will fail (it is ro). |
| 888 | // * A copy relocation doesn't work for functions. |
| 889 | // * The trick of using a plt entry as the address would fail here since |
| 890 | // the plt entry would have a non zero address. |
| 891 | // Since we cannot do anything better, we just resolve the symbol to 0 and |
| 892 | // don't produce a dynamic relocation. |
Hal Finkel | c917406 | 2015-10-16 22:11:05 +0000 | [diff] [blame] | 893 | // |
| 894 | // As an extra hack, assume that if we are producing a shared library the |
| 895 | // user knows what he or she is doing and can handle a dynamic relocation. |
| 896 | return Config->Shared || NeedsGot; |
Rafael Espindola | cc6ebb8 | 2015-10-14 18:42:16 +0000 | [diff] [blame] | 897 | } |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 898 | if (!Config->Shared) |
| 899 | return false; |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 900 | return Body->getVisibility() == STV_DEFAULT; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 903 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 904 | for (InputSection<ELFT> *C : Sections) |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 905 | C->writeTo(Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 908 | template <class ELFT> |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 909 | EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t sh_type, |
| 910 | uintX_t sh_flags) |
| 911 | : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {} |
| 912 | |
| 913 | template <class ELFT> |
| 914 | EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index) |
| 915 | : S(S), Index(Index) {} |
| 916 | |
| 917 | template <class ELFT> StringRef EHRegion<ELFT>::data() const { |
| 918 | ArrayRef<uint8_t> SecData = S->getSectionData(); |
| 919 | ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets; |
| 920 | size_t Start = Offsets[Index].first; |
| 921 | size_t End = |
| 922 | Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first; |
| 923 | return StringRef((const char *)SecData.data() + Start, End - Start); |
| 924 | } |
| 925 | |
| 926 | template <class ELFT> |
| 927 | Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index) |
| 928 | : EHRegion<ELFT>(S, Index) {} |
| 929 | |
| 930 | template <class ELFT> |
| 931 | template <bool IsRela> |
| 932 | void EHOutputSection<ELFT>::addSectionAux( |
| 933 | EHInputSection<ELFT> *S, |
| 934 | iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) { |
| 935 | const endianness E = ELFT::TargetEndianness; |
| 936 | |
| 937 | S->OutSec = this; |
| 938 | uint32_t Align = S->getAlign(); |
| 939 | if (Align > this->Header.sh_addralign) |
| 940 | this->Header.sh_addralign = Align; |
| 941 | |
| 942 | Sections.push_back(S); |
| 943 | |
| 944 | ArrayRef<uint8_t> SecData = S->getSectionData(); |
| 945 | ArrayRef<uint8_t> D = SecData; |
| 946 | uintX_t Offset = 0; |
| 947 | auto RelI = Rels.begin(); |
| 948 | auto RelE = Rels.end(); |
| 949 | |
George Rimar | 147747a | 2016-01-02 16:55:01 +0000 | [diff] [blame] | 950 | DenseMap<unsigned, unsigned> OffsetToIndex; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 951 | while (!D.empty()) { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 952 | unsigned Index = S->Offsets.size(); |
| 953 | S->Offsets.push_back(std::make_pair(Offset, -1)); |
| 954 | |
George Rimar | 003be4f | 2015-12-17 09:23:40 +0000 | [diff] [blame] | 955 | uintX_t Length = readEntryLength(D); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 956 | StringRef Entry((const char *)D.data(), Length); |
| 957 | |
| 958 | while (RelI != RelE && RelI->r_offset < Offset) |
| 959 | ++RelI; |
| 960 | uintX_t NextOffset = Offset + Length; |
| 961 | bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset; |
| 962 | |
| 963 | uint32_t ID = read32<E>(D.data() + 4); |
| 964 | if (ID == 0) { |
| 965 | // CIE |
| 966 | Cie<ELFT> C(S, Index); |
| 967 | |
| 968 | StringRef Personality; |
| 969 | if (HasReloc) { |
| 970 | uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL); |
| 971 | SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl(); |
| 972 | Personality = Body.getName(); |
| 973 | } |
| 974 | |
| 975 | std::pair<StringRef, StringRef> CieInfo(Entry, Personality); |
| 976 | auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size())); |
George Rimar | 147747a | 2016-01-02 16:55:01 +0000 | [diff] [blame] | 977 | if (P.second) { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 978 | Cies.push_back(C); |
Rui Ueyama | f71358d | 2016-01-06 23:25:42 +0000 | [diff] [blame] | 979 | this->Header.sh_size += align(Length, sizeof(uintX_t)); |
George Rimar | 147747a | 2016-01-02 16:55:01 +0000 | [diff] [blame] | 980 | } |
| 981 | OffsetToIndex[Offset] = P.first->second; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 982 | } else { |
| 983 | if (!HasReloc) |
| 984 | error("FDE doesn't reference another section"); |
| 985 | InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI); |
| 986 | if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) { |
| 987 | uint32_t CieOffset = Offset + 4 - ID; |
George Rimar | 147747a | 2016-01-02 16:55:01 +0000 | [diff] [blame] | 988 | auto I = OffsetToIndex.find(CieOffset); |
| 989 | if (I == OffsetToIndex.end()) |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 990 | error("Invalid CIE reference"); |
George Rimar | 147747a | 2016-01-02 16:55:01 +0000 | [diff] [blame] | 991 | Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index)); |
Rui Ueyama | f71358d | 2016-01-06 23:25:42 +0000 | [diff] [blame] | 992 | this->Header.sh_size += align(Length, sizeof(uintX_t)); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | |
| 996 | Offset = NextOffset; |
| 997 | D = D.slice(Length); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | template <class ELFT> |
George Rimar | 003be4f | 2015-12-17 09:23:40 +0000 | [diff] [blame] | 1002 | typename EHOutputSection<ELFT>::uintX_t |
| 1003 | EHOutputSection<ELFT>::readEntryLength(ArrayRef<uint8_t> D) { |
| 1004 | const endianness E = ELFT::TargetEndianness; |
| 1005 | |
| 1006 | if (D.size() < 4) |
| 1007 | error("Truncated CIE/FDE length"); |
| 1008 | uint64_t Len = read32<E>(D.data()); |
| 1009 | if (Len < UINT32_MAX) { |
| 1010 | if (Len > (UINT32_MAX - 4)) |
| 1011 | error("CIE/FIE size is too large"); |
| 1012 | if (Len + 4 > D.size()) |
| 1013 | error("CIE/FIE ends past the end of the section"); |
| 1014 | return Len + 4; |
| 1015 | } |
| 1016 | |
| 1017 | if (D.size() < 12) |
| 1018 | error("Truncated CIE/FDE length"); |
| 1019 | Len = read64<E>(D.data() + 4); |
| 1020 | if (Len > (UINT64_MAX - 12)) |
| 1021 | error("CIE/FIE size is too large"); |
| 1022 | if (Len + 12 > D.size()) |
| 1023 | error("CIE/FIE ends past the end of the section"); |
| 1024 | return Len + 12; |
| 1025 | } |
| 1026 | |
| 1027 | template <class ELFT> |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 1028 | void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { |
| 1029 | auto *S = cast<EHInputSection<ELFT>>(C); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1030 | const Elf_Shdr *RelSec = S->RelocSection; |
| 1031 | if (!RelSec) |
| 1032 | return addSectionAux( |
| 1033 | S, make_range((const Elf_Rela *)nullptr, (const Elf_Rela *)nullptr)); |
| 1034 | ELFFile<ELFT> &Obj = S->getFile()->getObj(); |
| 1035 | if (RelSec->sh_type == SHT_RELA) |
| 1036 | return addSectionAux(S, Obj.relas(RelSec)); |
| 1037 | return addSectionAux(S, Obj.rels(RelSec)); |
| 1038 | } |
| 1039 | |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 1040 | template <class ELFT> |
| 1041 | static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data, |
| 1042 | uint8_t *Buf) { |
| 1043 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 1044 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | f71358d | 2016-01-06 23:25:42 +0000 | [diff] [blame] | 1045 | uint64_t Len = align(Data.size(), sizeof(uintX_t)); |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 1046 | write32<E>(Buf, Len - 4); |
| 1047 | memcpy(Buf + 4, Data.data() + 4, Data.size() - 4); |
| 1048 | return Len; |
| 1049 | } |
| 1050 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1051 | template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1052 | const endianness E = ELFT::TargetEndianness; |
| 1053 | size_t Offset = 0; |
| 1054 | for (const Cie<ELFT> &C : Cies) { |
| 1055 | size_t CieOffset = Offset; |
| 1056 | |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 1057 | uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1058 | C.S->Offsets[C.Index].second = Offset; |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 1059 | Offset += CIELen; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1060 | |
| 1061 | for (const EHRegion<ELFT> &F : C.Fdes) { |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 1062 | uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1063 | write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1064 | F.S->Offsets[F.Index].second = Offset; |
George Rimar | e72beba | 2015-12-21 09:38:59 +0000 | [diff] [blame] | 1065 | Offset += Len; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | for (EHInputSection<ELFT> *S : Sections) { |
| 1070 | const Elf_Shdr *RelSec = S->RelocSection; |
| 1071 | if (!RelSec) |
| 1072 | continue; |
| 1073 | ELFFile<ELFT> &EObj = S->getFile()->getObj(); |
| 1074 | if (RelSec->sh_type == SHT_RELA) |
| 1075 | S->relocate(Buf, nullptr, EObj.relas(RelSec)); |
| 1076 | else |
Rafael Espindola | e02c868 | 2015-11-23 15:28:28 +0000 | [diff] [blame] | 1077 | S->relocate(Buf, nullptr, EObj.rels(RelSec)); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | template <class ELFT> |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1082 | MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type, |
| 1083 | uintX_t sh_flags) |
| 1084 | : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {} |
| 1085 | |
| 1086 | template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1087 | if (shouldTailMerge()) { |
| 1088 | StringRef Data = Builder.data(); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1089 | memcpy(Buf, Data.data(), Data.size()); |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1090 | return; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1091 | } |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1092 | for (const std::pair<StringRef, size_t> &P : Builder.getMap()) { |
| 1093 | StringRef Data = P.first; |
| 1094 | memcpy(Buf + P.second, Data.data(), Data.size()); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | static size_t findNull(StringRef S, size_t EntSize) { |
| 1099 | // Optimize the common case. |
| 1100 | if (EntSize == 1) |
| 1101 | return S.find(0); |
| 1102 | |
| 1103 | for (unsigned I = 0, N = S.size(); I != N; I += EntSize) { |
| 1104 | const char *B = S.begin() + I; |
| 1105 | if (std::all_of(B, B + EntSize, [](char C) { return C == 0; })) |
| 1106 | return I; |
| 1107 | } |
| 1108 | return StringRef::npos; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | template <class ELFT> |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 1112 | void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { |
| 1113 | auto *S = cast<MergeInputSection<ELFT>>(C); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1114 | S->OutSec = this; |
| 1115 | uint32_t Align = S->getAlign(); |
| 1116 | if (Align > this->Header.sh_addralign) |
| 1117 | this->Header.sh_addralign = Align; |
| 1118 | |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1119 | ArrayRef<uint8_t> D = S->getSectionData(); |
George Rimar | f940d59 | 2015-10-25 20:14:07 +0000 | [diff] [blame] | 1120 | StringRef Data((const char *)D.data(), D.size()); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1121 | uintX_t EntSize = S->getSectionHdr()->sh_entsize; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1122 | |
| 1123 | if (this->Header.sh_flags & SHF_STRINGS) { |
Rui Ueyama | ad87ff7 | 2016-01-06 02:52:24 +0000 | [diff] [blame] | 1124 | uintX_t Offset = 0; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1125 | while (!Data.empty()) { |
| 1126 | size_t End = findNull(Data, EntSize); |
| 1127 | if (End == StringRef::npos) |
| 1128 | error("String is not null terminated"); |
| 1129 | StringRef Entry = Data.substr(0, End + EntSize); |
George Rimar | 4b40ebc | 2015-11-13 13:44:59 +0000 | [diff] [blame] | 1130 | uintX_t OutputOffset = Builder.add(Entry); |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1131 | if (shouldTailMerge()) |
| 1132 | OutputOffset = -1; |
| 1133 | S->Offsets.push_back(std::make_pair(Offset, OutputOffset)); |
| 1134 | uintX_t Size = End + EntSize; |
| 1135 | Data = Data.substr(Size); |
| 1136 | Offset += Size; |
| 1137 | } |
| 1138 | } else { |
| 1139 | for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) { |
| 1140 | StringRef Entry = Data.substr(I, EntSize); |
| 1141 | size_t OutputOffset = Builder.add(Entry); |
Rui Ueyama | ad87ff7 | 2016-01-06 02:52:24 +0000 | [diff] [blame] | 1142 | S->Offsets.push_back(std::make_pair(I, OutputOffset)); |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1143 | } |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1144 | } |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | template <class ELFT> |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1148 | unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) { |
| 1149 | return Builder.getOffset(Val); |
| 1150 | } |
| 1151 | |
| 1152 | template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const { |
| 1153 | return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS; |
| 1154 | } |
| 1155 | |
| 1156 | template <class ELFT> void MergeOutputSection<ELFT>::finalize() { |
| 1157 | if (shouldTailMerge()) |
| 1158 | Builder.finalize(); |
| 1159 | this->Header.sh_size = Builder.getSize(); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | template <class ELFT> |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 1163 | StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic) |
Rui Ueyama | 07fc399 | 2016-01-07 18:17:29 +0000 | [diff] [blame] | 1164 | : OutputSectionBase<ELFT>(Name, SHT_STRTAB, Dynamic ? SHF_ALLOC : 0), |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 1165 | Dynamic(Dynamic) { |
| 1166 | this->Header.sh_addralign = 1; |
| 1167 | } |
| 1168 | |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1169 | // String tables are created in two phases. First you call reserve() |
| 1170 | // to reserve room in the string table, and then call addString() to actually |
| 1171 | // add that string. |
| 1172 | // |
| 1173 | // Why two phases? We want to know the size of the string table as early as |
| 1174 | // possible to fix file layout. So we have separated finalize(), which |
| 1175 | // determines the size of the section, from writeTo(), which writes the section |
| 1176 | // contents to the output buffer. If we merge reserve() with addString(), |
| 1177 | // we need a plumbing work for finalize() and writeTo() so that offsets |
| 1178 | // we obtained in the former function can be written in the latter. |
| 1179 | // This design eliminated that need. |
| 1180 | template <class ELFT> void StringTableSection<ELFT>::reserve(StringRef S) { |
| 1181 | Reserved += S.size() + 1; // +1 for NUL |
| 1182 | } |
| 1183 | |
Rui Ueyama | 7562d0e | 2016-01-07 16:41:06 +0000 | [diff] [blame] | 1184 | // Adds a string to the string table. You must call reserve() with the |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1185 | // same string before calling addString(). |
| 1186 | template <class ELFT> size_t StringTableSection<ELFT>::addString(StringRef S) { |
| 1187 | size_t Pos = Used; |
| 1188 | Strings.push_back(S); |
| 1189 | Used += S.size() + 1; |
| 1190 | Reserved -= S.size() + 1; |
| 1191 | assert((int64_t)Reserved >= 0); |
| 1192 | return Pos; |
| 1193 | } |
| 1194 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1195 | template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1196 | // ELF string tables start with NUL byte, so advance the pointer by one. |
| 1197 | ++Buf; |
| 1198 | for (StringRef S : Strings) { |
| 1199 | memcpy(Buf, S.data(), S.size()); |
| 1200 | Buf += S.size() + 1; |
| 1201 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1204 | template <class ELFT> |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 1205 | bool elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File, StringRef SymName, |
| 1206 | const typename ELFFile<ELFT>::Elf_Sym &Sym) { |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1207 | if (Sym.getType() == STT_SECTION) |
| 1208 | return false; |
| 1209 | |
Rafael Espindola | a6763e8 | 2015-12-11 18:49:29 +0000 | [diff] [blame] | 1210 | InputSectionBase<ELFT> *Sec = File.getSection(Sym); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1211 | // If sym references a section in a discarded group, don't keep it. |
Rafael Espindola | a6763e8 | 2015-12-11 18:49:29 +0000 | [diff] [blame] | 1212 | if (Sec == &InputSection<ELFT>::Discarded) |
Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 1213 | return false; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1214 | |
Davide Italiano | 6993ba4 | 2015-09-26 00:47:56 +0000 | [diff] [blame] | 1215 | if (Config->DiscardNone) |
| 1216 | return true; |
| 1217 | |
Rafael Espindola | a6763e8 | 2015-12-11 18:49:29 +0000 | [diff] [blame] | 1218 | // In ELF assembly .L symbols are normally discarded by the assembler. |
| 1219 | // If the assembler fails to do so, the linker discards them if |
| 1220 | // * --discard-locals is used. |
| 1221 | // * The symbol is in a SHF_MERGE section, which is normally the reason for |
| 1222 | // the assembler keeping the .L symbol. |
Rafael Espindola | 2992563 | 2015-12-11 19:09:21 +0000 | [diff] [blame] | 1223 | if (!SymName.startswith(".L") && !SymName.empty()) |
Rafael Espindola | a6763e8 | 2015-12-11 18:49:29 +0000 | [diff] [blame] | 1224 | return true; |
| 1225 | |
| 1226 | if (Config->DiscardLocals) |
| 1227 | return false; |
| 1228 | |
| 1229 | return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE); |
Davide Italiano | 6993ba4 | 2015-09-26 00:47:56 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 1232 | template <class ELFT> |
| 1233 | SymbolTableSection<ELFT>::SymbolTableSection( |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1234 | SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec) |
Rui Ueyama | cf07a31 | 2016-01-06 22:53:58 +0000 | [diff] [blame] | 1235 | : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab", |
| 1236 | StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, |
Rui Ueyama | 07fc399 | 2016-01-07 18:17:29 +0000 | [diff] [blame] | 1237 | StrTabSec.isDynamic() ? SHF_ALLOC : 0), |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1238 | Table(Table), StrTabSec(StrTabSec) { |
Rui Ueyama | d1e92aa | 2016-01-07 18:20:02 +0000 | [diff] [blame^] | 1239 | this->Header.sh_entsize = sizeof(Elf_Sym); |
| 1240 | this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Igor Kudrin | f4cdfe88 | 2015-11-12 04:08:12 +0000 | [diff] [blame] | 1243 | // Orders symbols according to their positions in the GOT, |
| 1244 | // in compliance with MIPS ABI rules. |
| 1245 | // See "Global Offset Table" in Chapter 5 in the following document |
| 1246 | // for detailed description: |
| 1247 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 1248 | static bool sortMipsSymbols(SymbolBody *L, SymbolBody *R) { |
| 1249 | if (!L->isInGot() || !R->isInGot()) |
| 1250 | return R->isInGot(); |
| 1251 | return L->GotIndex < R->GotIndex; |
| 1252 | } |
| 1253 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 1254 | template <class ELFT> void SymbolTableSection<ELFT>::finalize() { |
Igor Kudrin | 2169b1b | 2015-11-02 10:46:14 +0000 | [diff] [blame] | 1255 | if (this->Header.sh_size) |
| 1256 | return; // Already finalized. |
| 1257 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 1258 | this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym); |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 1259 | this->Header.sh_link = StrTabSec.SectionIndex; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 1260 | this->Header.sh_info = NumLocals + 1; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1261 | |
| 1262 | if (!StrTabSec.isDynamic()) { |
| 1263 | std::stable_sort(Symbols.begin(), Symbols.end(), |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 1264 | [](SymbolBody *L, SymbolBody *R) { |
| 1265 | return getSymbolBinding(L) == STB_LOCAL && |
| 1266 | getSymbolBinding(R) != STB_LOCAL; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1267 | }); |
| 1268 | return; |
| 1269 | } |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 1270 | if (Out<ELFT>::GnuHashTab) |
| 1271 | // NB: It also sorts Symbols to meet the GNU hash table requirements. |
| 1272 | Out<ELFT>::GnuHashTab->addSymbols(Symbols); |
Igor Kudrin | f4cdfe88 | 2015-11-12 04:08:12 +0000 | [diff] [blame] | 1273 | else if (Config->EMachine == EM_MIPS) |
| 1274 | std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols); |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1275 | size_t I = 0; |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 1276 | for (SymbolBody *B : Symbols) |
Rui Ueyama | a02bba6 | 2015-12-17 00:12:04 +0000 | [diff] [blame] | 1277 | B->DynamicSymbolTableIndex = ++I; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | template <class ELFT> |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1281 | void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) { |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1282 | StrTabSec.reserve(Name); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 1283 | ++NumVisible; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1284 | ++NumLocals; |
| 1285 | } |
| 1286 | |
| 1287 | template <class ELFT> |
| 1288 | void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) { |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1289 | StrTabSec.reserve(Body->getName()); |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 1290 | Symbols.push_back(Body); |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1291 | ++NumVisible; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1294 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1295 | Buf += sizeof(Elf_Sym); |
| 1296 | |
| 1297 | // All symbols with STB_LOCAL binding precede the weak and global symbols. |
| 1298 | // .dynsym only contains global symbols. |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1299 | if (!Config->DiscardAll && !StrTabSec.isDynamic()) |
| 1300 | writeLocalSymbols(Buf); |
| 1301 | |
| 1302 | writeGlobalSymbols(Buf); |
| 1303 | } |
| 1304 | |
| 1305 | template <class ELFT> |
| 1306 | void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) { |
| 1307 | // Iterate over all input object files to copy their local symbols |
| 1308 | // to the output symbol table pointed by Buf. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 1309 | for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) { |
| 1310 | Elf_Sym_Range Syms = File->getLocalSymbols(); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1311 | for (const Elf_Sym &Sym : Syms) { |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 1312 | ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable()); |
Rafael Espindola | 26fd69d | 2015-10-09 16:15:57 +0000 | [diff] [blame] | 1313 | error(SymNameOrErr); |
| 1314 | StringRef SymName = *SymNameOrErr; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 1315 | if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym)) |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1316 | continue; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1317 | |
Rui Ueyama | c55733e | 2015-09-30 00:54:29 +0000 | [diff] [blame] | 1318 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1319 | uintX_t VA = 0; |
Rafael Espindola | 4cda581 | 2015-10-16 15:29:48 +0000 | [diff] [blame] | 1320 | if (Sym.st_shndx == SHN_ABS) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1321 | ESym->st_shndx = SHN_ABS; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1322 | VA = Sym.st_value; |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1323 | } else { |
Rafael Espindola | 48225b4 | 2015-10-23 19:55:11 +0000 | [diff] [blame] | 1324 | InputSectionBase<ELFT> *Section = File->getSection(Sym); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1325 | if (!Section->isLive()) |
| 1326 | continue; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1327 | const OutputSectionBase<ELFT> *OutSec = Section->OutSec; |
| 1328 | ESym->st_shndx = OutSec->SectionIndex; |
Tom Stellard | 80efb16 | 2016-01-07 03:59:08 +0000 | [diff] [blame] | 1329 | VA = Section->getOffset(Sym); |
| 1330 | // Symbol offsets for AMDGPU need to be the offset in bytes of the |
| 1331 | // symbol from the beginning of the section. |
| 1332 | if (Config->EMachine != EM_AMDGPU) |
| 1333 | VA += OutSec->getVA(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1334 | } |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1335 | ESym->st_name = StrTabSec.addString(SymName); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1336 | ESym->st_size = Sym.st_size; |
| 1337 | ESym->setBindingAndType(Sym.getBinding(), Sym.getType()); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1338 | ESym->st_value = VA; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1339 | Buf += sizeof(*ESym); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1342 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1343 | |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1344 | template <class ELFT> |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 1345 | static const typename llvm::object::ELFFile<ELFT>::Elf_Sym * |
| 1346 | getElfSym(SymbolBody &Body) { |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 1347 | if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body)) |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 1348 | return &EBody->Sym; |
| 1349 | if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body)) |
| 1350 | return &EBody->Sym; |
| 1351 | return nullptr; |
| 1352 | } |
| 1353 | |
| 1354 | template <class ELFT> |
Igor Kudrin | ea6a835 | 2015-10-19 08:01:51 +0000 | [diff] [blame] | 1355 | void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1356 | // Write the internal symbol table contents to the output symbol table |
| 1357 | // pointed by Buf. |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1358 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 1359 | for (SymbolBody *Body : Symbols) { |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1360 | const OutputSectionBase<ELFT> *OutSec = nullptr; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1361 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1362 | switch (Body->kind()) { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 1363 | case SymbolBody::DefinedSyntheticKind: |
Rui Ueyama | b490876 | 2015-10-07 17:04:18 +0000 | [diff] [blame] | 1364 | OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section; |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 1365 | break; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1366 | case SymbolBody::DefinedRegularKind: { |
| 1367 | auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl()); |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 1368 | if (InputSectionBase<ELFT> *Sec = Sym->Section) { |
| 1369 | if (!Sec->isLive()) |
| 1370 | continue; |
| 1371 | OutSec = Sec->OutSec; |
| 1372 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1373 | break; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1374 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1375 | case SymbolBody::DefinedCommonKind: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1376 | OutSec = Out<ELFT>::Bss; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1377 | break; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 1378 | case SymbolBody::SharedKind: { |
Rui Ueyama | bb93606 | 2015-12-17 01:14:23 +0000 | [diff] [blame] | 1379 | if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy) |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 1380 | OutSec = Out<ELFT>::Bss; |
| 1381 | break; |
| 1382 | } |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 1383 | case SymbolBody::UndefinedElfKind: |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1384 | case SymbolBody::UndefinedKind: |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1385 | case SymbolBody::LazyKind: |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1386 | break; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1389 | StringRef Name = Body->getName(); |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1390 | ESym->st_name = StrTabSec.addString(Name); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1391 | |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1392 | unsigned char Type = STT_NOTYPE; |
| 1393 | uintX_t Size = 0; |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 1394 | if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) { |
| 1395 | Type = InputSym->getType(); |
| 1396 | Size = InputSym->st_size; |
Rafael Espindola | 1119191 | 2015-12-24 16:23:37 +0000 | [diff] [blame] | 1397 | } else if (auto *C = dyn_cast<DefinedCommon>(Body)) { |
| 1398 | Type = STT_OBJECT; |
| 1399 | Size = C->Size; |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1402 | ESym->setBindingAndType(getSymbolBinding(Body), Type); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1403 | ESym->st_size = Size; |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 1404 | ESym->setVisibility(Body->getVisibility()); |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1405 | ESym->st_value = getSymVA<ELFT>(*Body); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1406 | |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 1407 | if (OutSec) |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 1408 | ESym->st_shndx = OutSec->SectionIndex; |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 1409 | else if (isa<DefinedRegular<ELFT>>(Body)) |
| 1410 | ESym->st_shndx = SHN_ABS; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1411 | |
| 1412 | ++ESym; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1416 | template <class ELFT> |
| 1417 | uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) { |
Rui Ueyama | 8f2c4da | 2015-10-21 18:13:47 +0000 | [diff] [blame] | 1418 | uint8_t Visibility = Body->getVisibility(); |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1419 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
| 1420 | return STB_LOCAL; |
Rafael Espindola | 5d7593b | 2015-12-22 23:00:50 +0000 | [diff] [blame] | 1421 | if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body)) |
| 1422 | return ESym->getBinding(); |
Rafael Espindola | 4d4b06a | 2015-12-24 00:47:42 +0000 | [diff] [blame] | 1423 | if (isa<DefinedSynthetic<ELFT>>(Body)) |
| 1424 | return STB_LOCAL; |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1425 | return Body->isWeak() ? STB_WEAK : STB_GLOBAL; |
| 1426 | } |
| 1427 | |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 1428 | template <class ELFT> |
| 1429 | MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection() |
| 1430 | : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) { |
| 1431 | this->Header.sh_addralign = 4; |
| 1432 | this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo); |
| 1433 | this->Header.sh_size = sizeof(Elf_Mips_RegInfo); |
| 1434 | } |
| 1435 | |
| 1436 | template <class ELFT> |
| 1437 | void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1438 | auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf); |
| 1439 | R->ri_gp_value = getMipsGpAddr<ELFT>(); |
Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 1440 | R->ri_gprmask = GprMask; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | template <class ELFT> |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 1444 | void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) { |
Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 1445 | // Copy input object file's .reginfo gprmask to output. |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 1446 | auto *S = cast<MipsReginfoInputSection<ELFT>>(C); |
Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 1447 | GprMask |= S->Reginfo->ri_gprmask; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1450 | namespace lld { |
| 1451 | namespace elf2 { |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1452 | template class OutputSectionBase<ELF32LE>; |
| 1453 | template class OutputSectionBase<ELF32BE>; |
| 1454 | template class OutputSectionBase<ELF64LE>; |
| 1455 | template class OutputSectionBase<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1456 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 1457 | template class GotPltSection<ELF32LE>; |
| 1458 | template class GotPltSection<ELF32BE>; |
| 1459 | template class GotPltSection<ELF64LE>; |
| 1460 | template class GotPltSection<ELF64BE>; |
| 1461 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1462 | template class GotSection<ELF32LE>; |
| 1463 | template class GotSection<ELF32BE>; |
| 1464 | template class GotSection<ELF64LE>; |
| 1465 | template class GotSection<ELF64BE>; |
| 1466 | |
| 1467 | template class PltSection<ELF32LE>; |
| 1468 | template class PltSection<ELF32BE>; |
| 1469 | template class PltSection<ELF64LE>; |
| 1470 | template class PltSection<ELF64BE>; |
| 1471 | |
| 1472 | template class RelocationSection<ELF32LE>; |
| 1473 | template class RelocationSection<ELF32BE>; |
| 1474 | template class RelocationSection<ELF64LE>; |
| 1475 | template class RelocationSection<ELF64BE>; |
| 1476 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1477 | template class InterpSection<ELF32LE>; |
| 1478 | template class InterpSection<ELF32BE>; |
| 1479 | template class InterpSection<ELF64LE>; |
| 1480 | template class InterpSection<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1481 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 1482 | template class GnuHashTableSection<ELF32LE>; |
| 1483 | template class GnuHashTableSection<ELF32BE>; |
| 1484 | template class GnuHashTableSection<ELF64LE>; |
| 1485 | template class GnuHashTableSection<ELF64BE>; |
| 1486 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1487 | template class HashTableSection<ELF32LE>; |
| 1488 | template class HashTableSection<ELF32BE>; |
| 1489 | template class HashTableSection<ELF64LE>; |
| 1490 | template class HashTableSection<ELF64BE>; |
| 1491 | |
| 1492 | template class DynamicSection<ELF32LE>; |
| 1493 | template class DynamicSection<ELF32BE>; |
| 1494 | template class DynamicSection<ELF64LE>; |
| 1495 | template class DynamicSection<ELF64BE>; |
| 1496 | |
| 1497 | template class OutputSection<ELF32LE>; |
| 1498 | template class OutputSection<ELF32BE>; |
| 1499 | template class OutputSection<ELF64LE>; |
| 1500 | template class OutputSection<ELF64BE>; |
| 1501 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1502 | template class EHOutputSection<ELF32LE>; |
| 1503 | template class EHOutputSection<ELF32BE>; |
| 1504 | template class EHOutputSection<ELF64LE>; |
| 1505 | template class EHOutputSection<ELF64BE>; |
| 1506 | |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 1507 | template class MipsReginfoOutputSection<ELF32LE>; |
| 1508 | template class MipsReginfoOutputSection<ELF32BE>; |
| 1509 | template class MipsReginfoOutputSection<ELF64LE>; |
| 1510 | template class MipsReginfoOutputSection<ELF64BE>; |
| 1511 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1512 | template class MergeOutputSection<ELF32LE>; |
| 1513 | template class MergeOutputSection<ELF32BE>; |
| 1514 | template class MergeOutputSection<ELF64LE>; |
| 1515 | template class MergeOutputSection<ELF64BE>; |
| 1516 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1517 | template class StringTableSection<ELF32LE>; |
| 1518 | template class StringTableSection<ELF32BE>; |
| 1519 | template class StringTableSection<ELF64LE>; |
| 1520 | template class StringTableSection<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1521 | |
| 1522 | template class SymbolTableSection<ELF32LE>; |
| 1523 | template class SymbolTableSection<ELF32BE>; |
| 1524 | template class SymbolTableSection<ELF64LE>; |
| 1525 | template class SymbolTableSection<ELF64BE>; |
| 1526 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 1527 | template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &); |
| 1528 | template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &); |
| 1529 | template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &); |
| 1530 | template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 1531 | |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 1532 | template ELFFile<ELF32LE>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 1533 | getLocalRelTarget(const ObjectFile<ELF32LE> &, |
George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 1534 | const ELFFile<ELF32LE>::Elf_Rel &, |
| 1535 | ELFFile<ELF32LE>::uintX_t Addend); |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 1536 | template ELFFile<ELF32BE>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 1537 | getLocalRelTarget(const ObjectFile<ELF32BE> &, |
George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 1538 | const ELFFile<ELF32BE>::Elf_Rel &, |
| 1539 | ELFFile<ELF32BE>::uintX_t Addend); |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 1540 | template ELFFile<ELF64LE>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 1541 | getLocalRelTarget(const ObjectFile<ELF64LE> &, |
George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 1542 | const ELFFile<ELF64LE>::Elf_Rel &, |
| 1543 | ELFFile<ELF64LE>::uintX_t Addend); |
Rafael Espindola | 56f965f | 2015-09-21 22:48:12 +0000 | [diff] [blame] | 1544 | template ELFFile<ELF64BE>::uintX_t |
Rui Ueyama | 126d08f | 2015-10-12 20:28:22 +0000 | [diff] [blame] | 1545 | getLocalRelTarget(const ObjectFile<ELF64BE> &, |
George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 1546 | const ELFFile<ELF64BE>::Elf_Rel &, |
| 1547 | ELFFile<ELF64BE>::uintX_t Addend); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1548 | |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1549 | template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &, |
| 1550 | StringRef, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1551 | const ELFFile<ELF32LE>::Elf_Sym &); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1552 | template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &, |
| 1553 | StringRef, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1554 | const ELFFile<ELF32BE>::Elf_Sym &); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1555 | template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &, |
| 1556 | StringRef, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1557 | const ELFFile<ELF64LE>::Elf_Sym &); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 1558 | template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &, |
| 1559 | StringRef, |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1560 | const ELFFile<ELF64BE>::Elf_Sym &); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |