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