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