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