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