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