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