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