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