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