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