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