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