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