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