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" |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 17 | #include "SymbolListFile.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 18 | #include "SymbolTable.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 19 | #include "SyntheticSections.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 20 | #include "Target.h" |
Rui Ueyama | e980950 | 2016-03-11 04:23:12 +0000 | [diff] [blame] | 21 | #include "lld/Core/Parallel.h" |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Dwarf.h" |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MD5.h" |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | a42b3bc | 2016-09-27 16:43:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/SHA1.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 26 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Rui Ueyama | dbcfedb | 2016-02-09 21:46:11 +0000 | [diff] [blame] | 28 | using namespace llvm::dwarf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 29 | using namespace llvm::object; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 30 | using namespace llvm::support::endian; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 31 | using namespace llvm::ELF; |
| 32 | |
| 33 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 34 | using namespace lld::elf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 35 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 36 | OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type, |
| 37 | uint64_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 | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 44 | uint32_t OutputSectionBase::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> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 54 | void OutputSectionBase::writeHeaderTo(typename ELFT::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() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 69 | : OutputSectionBase(".gdb_index", SHT_PROGBITS, 0) {} |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 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> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 112 | PltSection<ELFT>::PltSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 113 | : OutputSectionBase(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 114 | this->Addralign = 16; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 117 | template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | e4c86d83 | 2016-05-18 21:03:36 +0000 | [diff] [blame] | 118 | // At beginning of PLT, we have code to call the dynamic linker |
| 119 | // to resolve dynsyms at runtime. Write such code. |
Rui Ueyama | 4a90f57 | 2016-06-16 16:28:50 +0000 | [diff] [blame] | 120 | Target->writePltHeader(Buf); |
| 121 | size_t Off = Target->PltHeaderSize; |
Rui Ueyama | f57a590 | 2016-05-20 21:39:07 +0000 | [diff] [blame] | 122 | |
George Rimar | fb5d7f2 | 2015-11-26 19:58:51 +0000 | [diff] [blame] | 123 | for (auto &I : Entries) { |
Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 124 | const SymbolBody *B = I.first; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 125 | unsigned RelOff = I.second; |
Rafael Espindola | e4c86d83 | 2016-05-18 21:03:36 +0000 | [diff] [blame] | 126 | uint64_t Got = B->getGotPltVA<ELFT>(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 127 | uint64_t Plt = this->Addr + Off; |
Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 128 | Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff); |
Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 129 | Off += Target->PltEntrySize; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 133 | template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 134 | Sym.PltIndex = Entries.size(); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame^] | 135 | unsigned RelOff = In<ELFT>::RelaPlt->getRelocOffset(); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 136 | Entries.push_back(std::make_pair(&Sym, RelOff)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 137 | } |
| 138 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 139 | template <class ELFT> void PltSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 140 | this->Size = Target->PltHeaderSize + Entries.size() * Target->PltEntrySize; |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 144 | HashTableSection<ELFT>::HashTableSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 145 | : OutputSectionBase(".hash", SHT_HASH, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 146 | this->Entsize = sizeof(Elf_Word); |
| 147 | this->Addralign = sizeof(Elf_Word); |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 150 | template <class ELFT> void HashTableSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 151 | this->Link = Out<ELFT>::DynSymTab->SectionIndex; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 152 | |
George Rimar | e9e1d32 | 2016-02-18 15:17:01 +0000 | [diff] [blame] | 153 | unsigned NumEntries = 2; // nbucket and nchain. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 154 | NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries. |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 155 | |
| 156 | // Create as many buckets as there are symbols. |
| 157 | // FIXME: This is simplistic. We can try to optimize it, but implementing |
| 158 | // support for SHT_GNU_HASH is probably even more profitable. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 159 | NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 160 | this->Size = NumEntries * sizeof(Elf_Word); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 164 | unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 165 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 166 | *P++ = NumSymbols; // nbucket |
| 167 | *P++ = NumSymbols; // nchain |
| 168 | |
| 169 | Elf_Word *Buckets = P; |
| 170 | Elf_Word *Chains = P + NumSymbols; |
| 171 | |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 172 | for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) { |
| 173 | SymbolBody *Body = S.Symbol; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 174 | StringRef Name = Body->getName(); |
Rui Ueyama | 572a6f7 | 2016-01-29 01:49:33 +0000 | [diff] [blame] | 175 | unsigned I = Body->DynsymIndex; |
Davide Italiano | e7fd0be | 2016-11-07 21:56:56 +0000 | [diff] [blame] | 176 | uint32_t Hash = hashSysV(Name) % NumSymbols; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 177 | Chains[I] = Buckets[Hash]; |
| 178 | Buckets[Hash] = I; |
| 179 | } |
| 180 | } |
| 181 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 182 | static uint32_t hashGnu(StringRef Name) { |
| 183 | uint32_t H = 5381; |
| 184 | for (uint8_t C : Name) |
| 185 | H = (H << 5) + H + C; |
| 186 | return H; |
| 187 | } |
| 188 | |
| 189 | template <class ELFT> |
| 190 | GnuHashTableSection<ELFT>::GnuHashTableSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 191 | : OutputSectionBase(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 192 | this->Entsize = ELFT::Is64Bits ? 0 : 4; |
| 193 | this->Addralign = sizeof(uintX_t); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | template <class ELFT> |
| 197 | unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) { |
| 198 | if (!NumHashed) |
| 199 | return 0; |
| 200 | |
| 201 | // These values are prime numbers which are not greater than 2^(N-1) + 1. |
| 202 | // In result, for any particular NumHashed we return a prime number |
| 203 | // which is not greater than NumHashed. |
| 204 | static const unsigned Primes[] = { |
| 205 | 1, 1, 3, 3, 7, 13, 31, 61, 127, 251, |
| 206 | 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071}; |
| 207 | |
| 208 | return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed), |
| 209 | array_lengthof(Primes) - 1)]; |
| 210 | } |
| 211 | |
| 212 | // Bloom filter estimation: at least 8 bits for each hashed symbol. |
| 213 | // GNU Hash table requirement: it should be a power of 2, |
| 214 | // the minimum value is 1, even for an empty table. |
| 215 | // Expected results for a 32-bit target: |
| 216 | // calcMaskWords(0..4) = 1 |
| 217 | // calcMaskWords(5..8) = 2 |
| 218 | // calcMaskWords(9..16) = 4 |
| 219 | // For a 64-bit target: |
| 220 | // calcMaskWords(0..8) = 1 |
| 221 | // calcMaskWords(9..16) = 2 |
| 222 | // calcMaskWords(17..32) = 4 |
| 223 | template <class ELFT> |
| 224 | unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) { |
| 225 | if (!NumHashed) |
| 226 | return 1; |
| 227 | return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off)); |
| 228 | } |
| 229 | |
| 230 | template <class ELFT> void GnuHashTableSection<ELFT>::finalize() { |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 231 | unsigned NumHashed = Symbols.size(); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 232 | NBuckets = calcNBuckets(NumHashed); |
| 233 | MaskWords = calcMaskWords(NumHashed); |
| 234 | // Second hash shift estimation: just predefined values. |
| 235 | Shift2 = ELFT::Is64Bits ? 6 : 5; |
| 236 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 237 | this->Link = Out<ELFT>::DynSymTab->SectionIndex; |
| 238 | this->Size = sizeof(Elf_Word) * 4 // Header |
| 239 | + sizeof(Elf_Off) * MaskWords // Bloom Filter |
| 240 | + sizeof(Elf_Word) * NBuckets // Hash Buckets |
| 241 | + sizeof(Elf_Word) * NumHashed; // Hash Values |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 245 | writeHeader(Buf); |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 246 | if (Symbols.empty()) |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 247 | return; |
| 248 | writeBloomFilter(Buf); |
| 249 | writeHashTable(Buf); |
| 250 | } |
| 251 | |
| 252 | template <class ELFT> |
| 253 | void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) { |
| 254 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 255 | *P++ = NBuckets; |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 256 | *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size(); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 257 | *P++ = MaskWords; |
| 258 | *P++ = Shift2; |
| 259 | Buf = reinterpret_cast<uint8_t *>(P); |
| 260 | } |
| 261 | |
| 262 | template <class ELFT> |
| 263 | void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) { |
| 264 | unsigned C = sizeof(Elf_Off) * 8; |
| 265 | |
| 266 | auto *Masks = reinterpret_cast<Elf_Off *>(Buf); |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 267 | for (const SymbolData &Sym : Symbols) { |
| 268 | size_t Pos = (Sym.Hash / C) & (MaskWords - 1); |
| 269 | uintX_t V = (uintX_t(1) << (Sym.Hash % C)) | |
| 270 | (uintX_t(1) << ((Sym.Hash >> Shift2) % C)); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 271 | Masks[Pos] |= V; |
| 272 | } |
| 273 | Buf += sizeof(Elf_Off) * MaskWords; |
| 274 | } |
| 275 | |
| 276 | template <class ELFT> |
| 277 | void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) { |
| 278 | Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf); |
| 279 | Elf_Word *Values = Buckets + NBuckets; |
| 280 | |
| 281 | int PrevBucket = -1; |
| 282 | int I = 0; |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 283 | for (const SymbolData &Sym : Symbols) { |
| 284 | int Bucket = Sym.Hash % NBuckets; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 285 | assert(PrevBucket <= Bucket); |
| 286 | if (Bucket != PrevBucket) { |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 287 | Buckets[Bucket] = Sym.Body->DynsymIndex; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 288 | PrevBucket = Bucket; |
| 289 | if (I > 0) |
| 290 | Values[I - 1] |= 1; |
| 291 | } |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 292 | Values[I] = Sym.Hash & ~1; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 293 | ++I; |
| 294 | } |
| 295 | if (I > 0) |
| 296 | Values[I - 1] |= 1; |
| 297 | } |
| 298 | |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 299 | // Add symbols to this symbol hash table. Note that this function |
| 300 | // destructively sort a given vector -- which is needed because |
| 301 | // GNU-style hash table places some sorting requirements. |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 302 | template <class ELFT> |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 303 | void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) { |
Davide Italiano | b4b68b6 | 2016-07-04 19:49:55 +0000 | [diff] [blame] | 304 | // Ideally this will just be 'auto' but GCC 6.1 is not able |
| 305 | // to deduce it correctly. |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 306 | std::vector<SymbolTableEntry>::iterator Mid = |
| 307 | std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) { |
| 308 | return S.Symbol->isUndefined(); |
| 309 | }); |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 310 | if (Mid == V.end()) |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 311 | return; |
Davide Italiano | f8591cf | 2016-07-06 17:41:55 +0000 | [diff] [blame] | 312 | for (auto I = Mid, E = V.end(); I != E; ++I) { |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 313 | SymbolBody *B = I->Symbol; |
Reid Kleckner | 2918d0b | 2016-10-20 00:13:34 +0000 | [diff] [blame] | 314 | size_t StrOff = I->StrTabOffset; |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 315 | Symbols.push_back({B, StrOff, hashGnu(B->getName())}); |
| 316 | } |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 317 | |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 318 | unsigned NBuckets = calcNBuckets(Symbols.size()); |
| 319 | std::stable_sort(Symbols.begin(), Symbols.end(), |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 320 | [&](const SymbolData &L, const SymbolData &R) { |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 321 | return L.Hash % NBuckets < R.Hash % NBuckets; |
| 322 | }); |
| 323 | |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 324 | V.erase(Mid, V.end()); |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 325 | for (const SymbolData &Sym : Symbols) |
| 326 | V.push_back({Sym.Body, Sym.STName}); |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 327 | } |
| 328 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 329 | // Returns the number of version definition entries. Because the first entry |
| 330 | // is for the version definition itself, it is the number of versioned symbols |
| 331 | // plus one. Note that we don't support multiple versions yet. |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 332 | static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 333 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 334 | template <class ELFT> |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 335 | EhFrameHeader<ELFT>::EhFrameHeader() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 336 | : OutputSectionBase(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {} |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 337 | |
Rui Ueyama | 95a232e | 2016-05-23 01:45:05 +0000 | [diff] [blame] | 338 | // .eh_frame_hdr contains a binary search table of pointers to FDEs. |
| 339 | // Each entry of the search table consists of two values, |
| 340 | // the starting PC from where FDEs covers, and the FDE's address. |
| 341 | // It is sorted by PC. |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 342 | template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) { |
| 343 | const endianness E = ELFT::TargetEndianness; |
| 344 | |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 345 | // Sort the FDE list by their PC and uniqueify. Usually there is only |
| 346 | // one FDE for a PC (i.e. function), but if ICF merges two functions |
| 347 | // into one, there can be more than one FDEs pointing to the address. |
| 348 | auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; |
| 349 | std::stable_sort(Fdes.begin(), Fdes.end(), Less); |
| 350 | auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; |
| 351 | Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); |
Peter Collingbourne | c98de13 | 2016-04-11 16:40:08 +0000 | [diff] [blame] | 352 | |
Rui Ueyama | 1b2936f | 2016-05-23 01:31:10 +0000 | [diff] [blame] | 353 | Buf[0] = 1; |
| 354 | Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 355 | Buf[2] = DW_EH_PE_udata4; |
| 356 | Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 357 | write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->Addr - 4); |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 358 | write32<E>(Buf + 8, Fdes.size()); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 359 | Buf += 12; |
| 360 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 361 | uintX_t VA = this->Addr; |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 362 | for (FdeData &Fde : Fdes) { |
| 363 | write32<E>(Buf, Fde.Pc - VA); |
| 364 | write32<E>(Buf + 4, Fde.FdeVA - VA); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 365 | Buf += 8; |
| 366 | } |
| 367 | } |
| 368 | |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 369 | template <class ELFT> void EhFrameHeader<ELFT>::finalize() { |
| 370 | // .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] | 371 | this->Size = 12 + Out<ELFT>::EhFrame->NumFdes * 8; |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 372 | } |
| 373 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 374 | template <class ELFT> |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 375 | void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) { |
| 376 | Fdes.push_back({Pc, FdeVA}); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 379 | template <class ELFT> static uint64_t getEntsize(uint32_t Type) { |
| 380 | switch (Type) { |
| 381 | case SHT_RELA: |
| 382 | return sizeof(typename ELFT::Rela); |
| 383 | case SHT_REL: |
| 384 | return sizeof(typename ELFT::Rel); |
| 385 | case SHT_MIPS_REGINFO: |
| 386 | return sizeof(Elf_Mips_RegInfo<ELFT>); |
| 387 | case SHT_MIPS_OPTIONS: |
| 388 | return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>); |
| 389 | case SHT_MIPS_ABIFLAGS: |
| 390 | return sizeof(Elf_Mips_ABIFlags<ELFT>); |
| 391 | default: |
| 392 | return 0; |
| 393 | } |
| 394 | } |
| 395 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 396 | template <class ELFT> |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 397 | OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 398 | : OutputSectionBase(Name, Type, Flags) { |
Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 399 | this->Entsize = getEntsize<ELFT>(Type); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | template <class ELFT> void OutputSection<ELFT>::finalize() { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame^] | 403 | if (!Config->Relocatable) { |
| 404 | // SHF_LINK_ORDER only has meaning in relocatable objects |
| 405 | this->Flags &= ~SHF_LINK_ORDER; |
| 406 | return; |
| 407 | } |
| 408 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 409 | uint32_t Type = this->Type; |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame^] | 410 | if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) { |
| 411 | // When doing a relocatable link we must preserve the link order |
| 412 | // dependency of sections with the SHF_LINK_ORDER flag. The dependency |
| 413 | // is indicated by the sh_link field. We need to translate the |
| 414 | // InputSection sh_link to the OutputSection sh_link, all InputSections |
| 415 | // in the OutputSection have the same dependency. |
| 416 | if (auto *D = this->Sections.front()->getLinkOrderDep()) |
| 417 | this->Link = D->OutSec->SectionIndex; |
Peter Smith | 580ba95 | 2016-10-21 11:25:33 +0000 | [diff] [blame] | 418 | } |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 419 | |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 420 | if (Type != SHT_RELA && Type != SHT_REL) |
| 421 | return; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 422 | this->Link = Out<ELFT>::SymTab->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 423 | // sh_info for SHT_REL[A] sections should contain the section header index of |
| 424 | // the section to which the relocation applies. |
| 425 | InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 426 | this->Info = S->OutSec->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 427 | } |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 428 | |
| 429 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 430 | void OutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 431 | assert(C->Live); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 432 | auto *S = cast<InputSection<ELFT>>(C); |
| 433 | Sections.push_back(S); |
| 434 | S->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 435 | this->updateAlignment(S->Alignment); |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 436 | // Keep sh_entsize value of the input section to be able to perform merging |
| 437 | // later during a final linking using the generated relocatable object. |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 438 | if (Config->Relocatable && (S->Flags & SHF_MERGE)) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 439 | this->Entsize = S->Entsize; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 442 | // This function is called after we sort input sections |
| 443 | // and scan relocations to setup sections' offsets. |
| 444 | template <class ELFT> void OutputSection<ELFT>::assignOffsets() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 445 | uintX_t Off = this->Size; |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 446 | for (InputSection<ELFT> *S : Sections) { |
| 447 | Off = alignTo(Off, S->Alignment); |
| 448 | S->OutSecOff = Off; |
| 449 | Off += S->getSize(); |
| 450 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 451 | this->Size = Off; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 452 | } |
| 453 | |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 454 | template <class ELFT> |
| 455 | void OutputSection<ELFT>::sort( |
| 456 | std::function<unsigned(InputSection<ELFT> *S)> Order) { |
| 457 | typedef std::pair<unsigned, InputSection<ELFT> *> Pair; |
| 458 | auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; |
| 459 | |
| 460 | std::vector<Pair> V; |
| 461 | for (InputSection<ELFT> *S : Sections) |
| 462 | V.push_back({Order(S), S}); |
| 463 | std::stable_sort(V.begin(), V.end(), Comp); |
| 464 | Sections.clear(); |
| 465 | for (Pair &P : V) |
| 466 | Sections.push_back(P.second); |
| 467 | } |
| 468 | |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 469 | // Sorts input sections by section name suffixes, so that .foo.N comes |
| 470 | // 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] | 471 | // We want to keep the original order if the priorities are the same |
| 472 | // because the compiler keeps the original initialization order in a |
| 473 | // translation unit and we need to respect that. |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 474 | // 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] | 475 | template <class ELFT> void OutputSection<ELFT>::sortInitFini() { |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 476 | // Sort sections by priority. |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 477 | sort([](InputSection<ELFT> *S) { return getPriority(S->Name); }); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 478 | } |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 479 | |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 480 | // Returns true if S matches /Filename.?\.o$/. |
| 481 | static bool isCrtBeginEnd(StringRef S, StringRef Filename) { |
| 482 | if (!S.endswith(".o")) |
| 483 | return false; |
| 484 | S = S.drop_back(2); |
| 485 | if (S.endswith(Filename)) |
| 486 | return true; |
| 487 | return !S.empty() && S.drop_back().endswith(Filename); |
| 488 | } |
| 489 | |
| 490 | static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } |
| 491 | static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } |
| 492 | |
| 493 | // .ctors and .dtors are sorted by this priority from highest to lowest. |
| 494 | // |
| 495 | // 1. The section was contained in crtbegin (crtbegin contains |
| 496 | // some sentinel value in its .ctors and .dtors so that the runtime |
| 497 | // can find the beginning of the sections.) |
| 498 | // |
| 499 | // 2. The section has an optional priority value in the form of ".ctors.N" |
| 500 | // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, |
| 501 | // they are compared as string rather than number. |
| 502 | // |
| 503 | // 3. The section is just ".ctors" or ".dtors". |
| 504 | // |
| 505 | // 4. The section was contained in crtend, which contains an end marker. |
| 506 | // |
| 507 | // In an ideal world, we don't need this function because .init_array and |
| 508 | // .ctors are duplicate features (and .init_array is newer.) However, there |
| 509 | // are too many real-world use cases of .ctors, so we had no choice to |
| 510 | // support that with this rather ad-hoc semantics. |
| 511 | template <class ELFT> |
| 512 | static bool compCtors(const InputSection<ELFT> *A, |
| 513 | const InputSection<ELFT> *B) { |
| 514 | bool BeginA = isCrtbegin(A->getFile()->getName()); |
| 515 | bool BeginB = isCrtbegin(B->getFile()->getName()); |
| 516 | if (BeginA != BeginB) |
| 517 | return BeginA; |
| 518 | bool EndA = isCrtend(A->getFile()->getName()); |
| 519 | bool EndB = isCrtend(B->getFile()->getName()); |
| 520 | if (EndA != EndB) |
| 521 | return EndB; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 522 | StringRef X = A->Name; |
| 523 | StringRef Y = B->Name; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 524 | assert(X.startswith(".ctors") || X.startswith(".dtors")); |
| 525 | assert(Y.startswith(".ctors") || Y.startswith(".dtors")); |
| 526 | X = X.substr(6); |
| 527 | Y = Y.substr(6); |
Rui Ueyama | 24b794e | 2016-02-12 00:38:46 +0000 | [diff] [blame] | 528 | if (X.empty() && Y.empty()) |
| 529 | return false; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 530 | return X < Y; |
| 531 | } |
| 532 | |
| 533 | // Sorts input sections by the special rules for .ctors and .dtors. |
| 534 | // Unfortunately, the rules are different from the one for .{init,fini}_array. |
| 535 | // Read the comment above. |
| 536 | template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() { |
| 537 | std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>); |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 538 | } |
| 539 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 540 | static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) { |
| 541 | size_t I = 0; |
| 542 | for (; I + A.size() < Size; I += A.size()) |
| 543 | memcpy(Buf + I, A.data(), A.size()); |
| 544 | memcpy(Buf + I, A.data(), Size - I); |
| 545 | } |
| 546 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 547 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 548 | ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 549 | if (!Filler.empty()) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 550 | fill(Buf, this->Size, Filler); |
Rui Ueyama | eb943e8 | 2016-11-04 18:22:36 +0000 | [diff] [blame] | 551 | if (Config->Threads) { |
| 552 | parallel_for_each(Sections.begin(), Sections.end(), |
| 553 | [=](InputSection<ELFT> *C) { C->writeTo(Buf); }); |
| 554 | } else { |
| 555 | for (InputSection<ELFT> *C : Sections) |
| 556 | C->writeTo(Buf); |
| 557 | } |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 558 | // Linker scripts may have BYTE()-family commands with which you |
| 559 | // can write arbitrary bytes to the output. Process them if any. |
| 560 | Script<ELFT>::X->writeDataBytes(this->Name, Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 563 | template <class ELFT> |
Rui Ueyama | f86cb90 | 2016-05-23 15:12:41 +0000 | [diff] [blame] | 564 | EhOutputSection<ELFT>::EhOutputSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 565 | : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {} |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 566 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 567 | // Search for an existing CIE record or create a new one. |
| 568 | // CIE records from input object files are uniquified by their contents |
| 569 | // and where their relocations point to. |
| 570 | template <class ELFT> |
| 571 | template <class RelTy> |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 572 | CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece, |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 573 | EhInputSection<ELFT> *Sec, |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 574 | ArrayRef<RelTy> Rels) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 575 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 576 | if (read32<E>(Piece.data().data() + 4) != 0) |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 577 | fatal("CIE expected at beginning of .eh_frame: " + Sec->Name); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 578 | |
| 579 | SymbolBody *Personality = nullptr; |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 580 | unsigned FirstRelI = Piece.FirstRelocation; |
| 581 | if (FirstRelI != (unsigned)-1) |
| 582 | Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 583 | |
| 584 | // Search for an existing CIE by CIE contents/relocation target pair. |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 585 | CieRecord *Cie = &CieMap[{Piece.data(), Personality}]; |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 586 | |
| 587 | // If not found, create a new one. |
Rui Ueyama | e2060aa | 2016-05-22 23:52:56 +0000 | [diff] [blame] | 588 | if (Cie->Piece == nullptr) { |
| 589 | Cie->Piece = &Piece; |
Rui Ueyama | e2060aa | 2016-05-22 23:52:56 +0000 | [diff] [blame] | 590 | Cies.push_back(Cie); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 591 | } |
| 592 | return Cie; |
| 593 | } |
| 594 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 595 | // There is one FDE per function. Returns true if a given FDE |
| 596 | // points to a live function. |
| 597 | template <class ELFT> |
| 598 | template <class RelTy> |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 599 | bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece, |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 600 | EhInputSection<ELFT> *Sec, |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 601 | ArrayRef<RelTy> Rels) { |
| 602 | unsigned FirstRelI = Piece.FirstRelocation; |
| 603 | if (FirstRelI == (unsigned)-1) |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 604 | fatal("FDE doesn't reference another section"); |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 605 | const RelTy &Rel = Rels[FirstRelI]; |
| 606 | SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 607 | auto *D = dyn_cast<DefinedRegular<ELFT>>(&B); |
| 608 | if (!D || !D->Section) |
| 609 | return false; |
| 610 | InputSectionBase<ELFT> *Target = D->Section->Repl; |
| 611 | return Target && Target->Live; |
| 612 | } |
| 613 | |
| 614 | // .eh_frame is a sequence of CIE or FDE records. In general, there |
| 615 | // is one CIE record per input object file which is followed by |
| 616 | // a list of FDEs. This function searches an existing CIE or create a new |
| 617 | // one and associates FDEs to the CIE. |
Rui Ueyama | c0c9260 | 2016-02-05 22:56:03 +0000 | [diff] [blame] | 618 | template <class ELFT> |
Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 619 | template <class RelTy> |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 620 | void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec, |
Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 621 | ArrayRef<RelTy> Rels) { |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 622 | const endianness E = ELFT::TargetEndianness; |
Rafael Espindola | 820f4bb | 2016-05-24 15:17:47 +0000 | [diff] [blame] | 623 | |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 624 | DenseMap<size_t, CieRecord *> OffsetToCie; |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 625 | for (EhSectionPiece &Piece : Sec->Pieces) { |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 626 | // The empty record is the end marker. |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 627 | if (Piece.size() == 4) |
Rafael Espindola | 5ee9e7f | 2016-05-24 19:14:09 +0000 | [diff] [blame] | 628 | return; |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 629 | |
| 630 | size_t Offset = Piece.InputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 631 | uint32_t ID = read32<E>(Piece.data().data() + 4); |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 632 | if (ID == 0) { |
| 633 | OffsetToCie[Offset] = addCie(Piece, Sec, Rels); |
| 634 | continue; |
| 635 | } |
| 636 | |
| 637 | uint32_t CieOffset = Offset + 4 - ID; |
| 638 | CieRecord *Cie = OffsetToCie[CieOffset]; |
| 639 | if (!Cie) |
| 640 | fatal("invalid CIE reference"); |
| 641 | |
| 642 | if (!isFdeLive(Piece, Sec, Rels)) |
| 643 | continue; |
| 644 | Cie->FdePieces.push_back(&Piece); |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 645 | NumFdes++; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 650 | void EhOutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 651 | auto *Sec = cast<EhInputSection<ELFT>>(C); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 652 | Sec->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 653 | this->updateAlignment(Sec->Alignment); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 654 | Sections.push_back(Sec); |
| 655 | |
| 656 | // .eh_frame is a sequence of CIE or FDE records. This function |
| 657 | // splits it into pieces so that we can call |
| 658 | // SplitInputSection::getSectionPiece on the section. |
Rui Ueyama | 88abd9b | 2016-05-22 23:53:00 +0000 | [diff] [blame] | 659 | Sec->split(); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 660 | if (Sec->Pieces.empty()) |
| 661 | return; |
| 662 | |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 663 | if (Sec->NumRelocations) { |
| 664 | if (Sec->AreRelocsRela) |
| 665 | addSectionAux(Sec, Sec->relas()); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 666 | else |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 667 | addSectionAux(Sec, Sec->rels()); |
Rui Ueyama | 0de86c1 | 2016-01-27 22:23:44 +0000 | [diff] [blame] | 668 | return; |
| 669 | } |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 670 | addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr)); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 673 | template <class ELFT> |
Rui Ueyama | 644ac65 | 2016-05-22 00:17:11 +0000 | [diff] [blame] | 674 | static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) { |
| 675 | memcpy(Buf, D.data(), D.size()); |
Rui Ueyama | c0449a6 | 2016-05-21 18:10:13 +0000 | [diff] [blame] | 676 | |
| 677 | // 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] | 678 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | 644ac65 | 2016-05-22 00:17:11 +0000 | [diff] [blame] | 679 | write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 682 | template <class ELFT> void EhOutputSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 683 | if (this->Size) |
Rui Ueyama | e9381bd | 2016-07-16 02:47:42 +0000 | [diff] [blame] | 684 | return; // Already finalized. |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 685 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 686 | size_t Off = 0; |
| 687 | for (CieRecord *Cie : Cies) { |
| 688 | Cie->Piece->OutputOff = Off; |
| 689 | Off += alignTo(Cie->Piece->size(), sizeof(uintX_t)); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 690 | |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 691 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 692 | Fde->OutputOff = Off; |
| 693 | Off += alignTo(Fde->size(), sizeof(uintX_t)); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 696 | this->Size = Off; |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 699 | template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) { |
| 700 | const endianness E = ELFT::TargetEndianness; |
| 701 | switch (Size) { |
| 702 | case DW_EH_PE_udata2: |
| 703 | return read16<E>(Buf); |
| 704 | case DW_EH_PE_udata4: |
| 705 | return read32<E>(Buf); |
| 706 | case DW_EH_PE_udata8: |
| 707 | return read64<E>(Buf); |
| 708 | case DW_EH_PE_absptr: |
| 709 | if (ELFT::Is64Bits) |
| 710 | return read64<E>(Buf); |
| 711 | return read32<E>(Buf); |
| 712 | } |
| 713 | fatal("unknown FDE size encoding"); |
| 714 | } |
| 715 | |
| 716 | // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. |
| 717 | // We need it to create .eh_frame_hdr section. |
| 718 | template <class ELFT> |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 719 | typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff, |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 720 | uint8_t Enc) { |
Rui Ueyama | 2ab3d20 | 2016-05-23 16:36:47 +0000 | [diff] [blame] | 721 | // The starting address to which this FDE applies is |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 722 | // stored at FDE + 8 byte. |
| 723 | size_t Off = FdeOff + 8; |
| 724 | uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7); |
| 725 | if ((Enc & 0x70) == DW_EH_PE_absptr) |
| 726 | return Addr; |
| 727 | if ((Enc & 0x70) == DW_EH_PE_pcrel) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 728 | return Addr + this->Addr + Off; |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 729 | fatal("unknown FDE size relative encoding"); |
| 730 | } |
| 731 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 732 | template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 733 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 734 | for (CieRecord *Cie : Cies) { |
| 735 | size_t CieOffset = Cie->Piece->OutputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 736 | writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data()); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 737 | |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 738 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 739 | size_t Off = Fde->OutputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 740 | writeCieFde<ELFT>(Buf + Off, Fde->data()); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 741 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 742 | // FDE's second word should have the offset to an associated CIE. |
| 743 | // Write it. |
| 744 | write32<E>(Buf + Off + 4, Off + 4 - CieOffset); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 748 | for (EhInputSection<ELFT> *S : Sections) |
Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 749 | S->relocate(Buf, nullptr); |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 750 | |
| 751 | // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table |
Rui Ueyama | 2ab3d20 | 2016-05-23 16:36:47 +0000 | [diff] [blame] | 752 | // 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] | 753 | // we obtain two addresses and pass them to EhFrameHdr object. |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 754 | if (Out<ELFT>::EhFrameHdr) { |
| 755 | for (CieRecord *Cie : Cies) { |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 756 | uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data()); |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 757 | for (SectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | 6de2e68 | 2016-05-24 02:08:38 +0000 | [diff] [blame] | 758 | uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 759 | uintX_t FdeVA = this->Addr + Fde->OutputOff; |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 760 | Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA); |
| 761 | } |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | template <class ELFT> |
Rui Ueyama | d97e5c4 | 2016-01-07 18:33:11 +0000 | [diff] [blame] | 767 | MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type, |
Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 768 | uintX_t Flags, uintX_t Alignment) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 769 | : OutputSectionBase(Name, Type, Flags), |
Rui Ueyama | 818bb2f | 2016-07-16 18:55:47 +0000 | [diff] [blame] | 770 | Builder(StringTableBuilder::RAW, Alignment) {} |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 771 | |
| 772 | template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 259d645 | 2016-10-04 22:43:38 +0000 | [diff] [blame] | 773 | Builder.write(Buf); |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 776 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 777 | void MergeOutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 1080351 | 2016-05-22 00:25:30 +0000 | [diff] [blame] | 778 | auto *Sec = cast<MergeInputSection<ELFT>>(C); |
| 779 | Sec->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 780 | this->updateAlignment(Sec->Alignment); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 781 | this->Entsize = Sec->Entsize; |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 782 | Sections.push_back(Sec); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 783 | |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 784 | auto HashI = Sec->Hashes.begin(); |
| 785 | for (auto I = Sec->Pieces.begin(), E = Sec->Pieces.end(); I != E; ++I) { |
| 786 | SectionPiece &Piece = *I; |
| 787 | uint32_t Hash = *HashI; |
| 788 | ++HashI; |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 789 | if (!Piece.Live) |
Rafael Espindola | 0b9531c | 2016-04-22 22:09:35 +0000 | [diff] [blame] | 790 | continue; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 791 | StringRef Data = toStringRef(Sec->getData(I)); |
| 792 | CachedHashStringRef V(Data, Hash); |
Rafael Espindola | 5fc2b1d | 2016-10-05 19:36:02 +0000 | [diff] [blame] | 793 | uintX_t OutputOffset = Builder.add(V); |
Rafael Espindola | 7b8bb53 | 2016-10-05 15:35:18 +0000 | [diff] [blame] | 794 | if (!shouldTailMerge()) |
Rui Ueyama | c6ebb02 | 2016-05-22 01:03:41 +0000 | [diff] [blame] | 795 | Piece.OutputOff = OutputOffset; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 796 | } |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | template <class ELFT> |
Justin Lebar | ee34a73 | 2016-10-17 22:24:36 +0000 | [diff] [blame] | 800 | unsigned MergeOutputSection<ELFT>::getOffset(CachedHashStringRef Val) { |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 801 | return Builder.getOffset(Val); |
| 802 | } |
| 803 | |
| 804 | template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 805 | return Config->Optimize >= 2 && this->Flags & SHF_STRINGS; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | template <class ELFT> void MergeOutputSection<ELFT>::finalize() { |
| 809 | if (shouldTailMerge()) |
| 810 | Builder.finalize(); |
Rafael Espindola | 259d645 | 2016-10-04 22:43:38 +0000 | [diff] [blame] | 811 | else |
| 812 | Builder.finalizeInOrder(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 813 | this->Size = Builder.getSize(); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 816 | template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() { |
| 817 | for (MergeInputSection<ELFT> *Sec : Sections) |
| 818 | Sec->finalizePieces(); |
| 819 | } |
| 820 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 821 | template <class ELFT> |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 822 | SymbolTableSection<ELFT>::SymbolTableSection( |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 823 | StringTableSection<ELFT> &StrTabSec) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 824 | : OutputSectionBase(StrTabSec.isDynamic() ? ".dynsym" : ".symtab", |
| 825 | StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, |
| 826 | StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0), |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 827 | StrTabSec(StrTabSec) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 828 | this->Entsize = sizeof(Elf_Sym); |
| 829 | this->Addralign = sizeof(uintX_t); |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Igor Kudrin | f4cdfe88 | 2015-11-12 04:08:12 +0000 | [diff] [blame] | 832 | // Orders symbols according to their positions in the GOT, |
| 833 | // in compliance with MIPS ABI rules. |
| 834 | // See "Global Offset Table" in Chapter 5 in the following document |
| 835 | // for detailed description: |
| 836 | // 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] | 837 | static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) { |
Simon Atanasyan | d2980d3 | 2016-03-29 14:07:22 +0000 | [diff] [blame] | 838 | // Sort entries related to non-local preemptible symbols by GOT indexes. |
| 839 | // 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] | 840 | bool LIsInLocalGot = !L->IsInGlobalMipsGot; |
| 841 | bool RIsInLocalGot = !R->IsInGlobalMipsGot; |
Simon Atanasyan | 7b8481b | 2016-06-20 11:37:56 +0000 | [diff] [blame] | 842 | if (LIsInLocalGot || RIsInLocalGot) |
| 843 | return !RIsInLocalGot; |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 844 | return L->GotIndex < R->GotIndex; |
Igor Kudrin | f4cdfe88 | 2015-11-12 04:08:12 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Rafael Espindola | badd397 | 2016-04-08 15:30:56 +0000 | [diff] [blame] | 847 | static uint8_t getSymbolBinding(SymbolBody *Body) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 848 | Symbol *S = Body->symbol(); |
George Rimar | 11f83b7 | 2016-08-19 08:31:02 +0000 | [diff] [blame] | 849 | if (Config->Relocatable) |
| 850 | return S->Binding; |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 851 | uint8_t Visibility = S->Visibility; |
Rafael Espindola | badd397 | 2016-04-08 15:30:56 +0000 | [diff] [blame] | 852 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
| 853 | return STB_LOCAL; |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 854 | if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE) |
Rafael Espindola | badd397 | 2016-04-08 15:30:56 +0000 | [diff] [blame] | 855 | return STB_GLOBAL; |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 856 | return S->Binding; |
Rafael Espindola | badd397 | 2016-04-08 15:30:56 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 859 | template <class ELFT> void SymbolTableSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 860 | if (this->Size) |
Igor Kudrin | 2169b1b | 2015-11-02 10:46:14 +0000 | [diff] [blame] | 861 | return; // Already finalized. |
| 862 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 863 | this->Size = getNumSymbols() * sizeof(Elf_Sym); |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 864 | this->Link = StrTabSec.OutSec->SectionIndex; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 865 | this->Info = NumLocals + 1; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 866 | |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 867 | if (Config->Relocatable) { |
| 868 | size_t I = NumLocals; |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 869 | for (const SymbolTableEntry &S : Symbols) |
| 870 | S.Symbol->DynsymIndex = ++I; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 871 | return; |
| 872 | } |
| 873 | |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 874 | if (!StrTabSec.isDynamic()) { |
| 875 | std::stable_sort(Symbols.begin(), Symbols.end(), |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 876 | [](const SymbolTableEntry &L, const SymbolTableEntry &R) { |
| 877 | return getSymbolBinding(L.Symbol) == STB_LOCAL && |
| 878 | getSymbolBinding(R.Symbol) != STB_LOCAL; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 879 | }); |
| 880 | return; |
| 881 | } |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 882 | if (Out<ELFT>::GnuHashTab) |
| 883 | // NB: It also sorts Symbols to meet the GNU hash table requirements. |
| 884 | Out<ELFT>::GnuHashTab->addSymbols(Symbols); |
Igor Kudrin | f4cdfe88 | 2015-11-12 04:08:12 +0000 | [diff] [blame] | 885 | else if (Config->EMachine == EM_MIPS) |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 886 | std::stable_sort(Symbols.begin(), Symbols.end(), |
| 887 | [](const SymbolTableEntry &L, const SymbolTableEntry &R) { |
| 888 | return sortMipsSymbols(L.Symbol, R.Symbol); |
| 889 | }); |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 890 | size_t I = 0; |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 891 | for (const SymbolTableEntry &S : Symbols) |
| 892 | S.Symbol->DynsymIndex = ++I; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 893 | } |
| 894 | |
George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 895 | template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) { |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 896 | Symbols.push_back({B, StrTabSec.addString(B->getName(), false)}); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 899 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 900 | Buf += sizeof(Elf_Sym); |
| 901 | |
| 902 | // All symbols with STB_LOCAL binding precede the weak and global symbols. |
| 903 | // .dynsym only contains global symbols. |
George Rimar | 9503f6d | 2016-08-31 08:46:30 +0000 | [diff] [blame] | 904 | if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic()) |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 905 | writeLocalSymbols(Buf); |
| 906 | |
| 907 | writeGlobalSymbols(Buf); |
| 908 | } |
| 909 | |
| 910 | template <class ELFT> |
| 911 | void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) { |
| 912 | // Iterate over all input object files to copy their local symbols |
| 913 | // to the output symbol table pointed by Buf. |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 914 | for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) { |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 915 | for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P : |
| 916 | File->KeptLocalSyms) { |
| 917 | const DefinedRegular<ELFT> &Body = *P.first; |
| 918 | InputSectionBase<ELFT> *Section = Body.Section; |
Rui Ueyama | c55733e | 2015-09-30 00:54:29 +0000 | [diff] [blame] | 919 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 920 | |
| 921 | if (!Section) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 922 | ESym->st_shndx = SHN_ABS; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 923 | ESym->st_value = Body.Value; |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 924 | } else { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 925 | const OutputSectionBase *OutSec = Section->OutSec; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 926 | ESym->st_shndx = OutSec->SectionIndex; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 927 | ESym->st_value = OutSec->Addr + Section->getOffset(Body); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 928 | } |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 929 | ESym->st_name = P.second; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 930 | ESym->st_size = Body.template getSize<ELFT>(); |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 931 | ESym->setBindingAndType(STB_LOCAL, Body.Type); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 932 | Buf += sizeof(*ESym); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 935 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 936 | |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 937 | template <class ELFT> |
Igor Kudrin | ea6a835 | 2015-10-19 08:01:51 +0000 | [diff] [blame] | 938 | void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 939 | // Write the internal symbol table contents to the output symbol table |
| 940 | // pointed by Buf. |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 941 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 942 | for (const SymbolTableEntry &S : Symbols) { |
| 943 | SymbolBody *Body = S.Symbol; |
| 944 | size_t StrOff = S.StrTabOffset; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 945 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 946 | uint8_t Type = Body->Type; |
| 947 | uintX_t Size = Body->getSize<ELFT>(); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 948 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 949 | ESym->setBindingAndType(getSymbolBinding(Body), Type); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 950 | ESym->st_size = Size; |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 951 | ESym->st_name = StrOff; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 952 | ESym->setVisibility(Body->symbol()->Visibility); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 953 | ESym->st_value = Body->getVA<ELFT>(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 954 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 955 | if (const OutputSectionBase *OutSec = getOutputSection(Body)) |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 956 | ESym->st_shndx = OutSec->SectionIndex; |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 957 | else if (isa<DefinedRegular<ELFT>>(Body)) |
| 958 | ESym->st_shndx = SHN_ABS; |
Simon Atanasyan | d040a58 | 2016-02-25 16:19:15 +0000 | [diff] [blame] | 959 | |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 960 | if (Config->EMachine == EM_MIPS) { |
| 961 | // On MIPS we need to mark symbol which has a PLT entry and requires |
| 962 | // pointer equality by STO_MIPS_PLT flag. That is necessary to help |
| 963 | // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. |
| 964 | // https://sourceware.org/ml/binutils/2008-07/txt00000.txt |
| 965 | if (Body->isInPlt() && Body->NeedsCopyOrPltAddr) |
| 966 | ESym->st_other |= STO_MIPS_PLT; |
| 967 | if (Config->Relocatable) { |
| 968 | auto *D = dyn_cast<DefinedRegular<ELFT>>(Body); |
| 969 | if (D && D->isMipsPIC()) |
| 970 | ESym->st_other |= STO_MIPS_PIC; |
| 971 | } |
| 972 | } |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 973 | ++ESym; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 977 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 978 | const OutputSectionBase * |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 979 | SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) { |
| 980 | switch (Sym->kind()) { |
| 981 | case SymbolBody::DefinedSyntheticKind: |
Peter Collingbourne | 6a42259 | 2016-05-03 01:21:08 +0000 | [diff] [blame] | 982 | return cast<DefinedSynthetic<ELFT>>(Sym)->Section; |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 983 | case SymbolBody::DefinedRegularKind: { |
Rafael Espindola | 5ffa13c | 2016-04-15 00:15:02 +0000 | [diff] [blame] | 984 | auto &D = cast<DefinedRegular<ELFT>>(*Sym); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 985 | if (D.Section) |
| 986 | return D.Section->OutSec; |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 987 | break; |
| 988 | } |
| 989 | case SymbolBody::DefinedCommonKind: |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 990 | return In<ELFT>::Common->OutSec; |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 991 | case SymbolBody::SharedKind: |
| 992 | if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy()) |
| 993 | return Out<ELFT>::Bss; |
| 994 | break; |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 995 | case SymbolBody::UndefinedKind: |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 996 | case SymbolBody::LazyArchiveKind: |
| 997 | case SymbolBody::LazyObjectKind: |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 998 | break; |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 999 | } |
| 1000 | return nullptr; |
| 1001 | } |
| 1002 | |
| 1003 | template <class ELFT> |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1004 | VersionDefinitionSection<ELFT>::VersionDefinitionSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1005 | : OutputSectionBase(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1006 | this->Addralign = sizeof(uint32_t); |
Rui Ueyama | f524464 | 2016-07-16 02:36:00 +0000 | [diff] [blame] | 1007 | } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1008 | |
| 1009 | static StringRef getFileDefName() { |
| 1010 | if (!Config->SoName.empty()) |
| 1011 | return Config->SoName; |
| 1012 | return Config->OutputFile; |
| 1013 | } |
| 1014 | |
| 1015 | template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1016 | FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName()); |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 1017 | for (VersionDefinition &V : Config->VersionDefinitions) |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1018 | V.NameOff = In<ELFT>::DynStrTab->addString(V.Name); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1019 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1020 | this->Size = (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum(); |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1021 | this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1022 | |
| 1023 | // sh_info should be set to the number of definitions. This fact is missed in |
| 1024 | // documentation, but confirmed by binutils community: |
| 1025 | // https://sourceware.org/ml/binutils/2014-11/msg00355.html |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1026 | this->Info = getVerDefNum(); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1029 | template <class ELFT> |
| 1030 | void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index, |
| 1031 | StringRef Name, size_t NameOff) { |
| 1032 | auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1033 | Verdef->vd_version = 1; |
George Rimar | 33b9de4 | 2016-07-01 11:45:10 +0000 | [diff] [blame] | 1034 | Verdef->vd_cnt = 1; |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1035 | Verdef->vd_aux = sizeof(Elf_Verdef); |
| 1036 | Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 1037 | Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1038 | Verdef->vd_ndx = Index; |
Davide Italiano | e7fd0be | 2016-11-07 21:56:56 +0000 | [diff] [blame] | 1039 | Verdef->vd_hash = hashSysV(Name); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1040 | |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1041 | auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef)); |
| 1042 | Verdaux->vda_name = NameOff; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1043 | Verdaux->vda_next = 0; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | template <class ELFT> |
| 1047 | void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1048 | writeOne(Buf, 1, getFileDefName(), FileDefNameOff); |
| 1049 | |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 1050 | for (VersionDefinition &V : Config->VersionDefinitions) { |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1051 | Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 1052 | writeOne(Buf, V.Id, V.Name, V.NameOff); |
| 1053 | } |
| 1054 | |
| 1055 | // Need to terminate the last version definition. |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1056 | Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1057 | Verdef->vd_next = 0; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | template <class ELFT> |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1061 | VersionTableSection<ELFT>::VersionTableSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1062 | : OutputSectionBase(".gnu.version", SHT_GNU_versym, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1063 | this->Addralign = sizeof(uint16_t); |
Rafael Espindola | eaaec4a | 2016-04-29 17:19:45 +0000 | [diff] [blame] | 1064 | } |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1065 | |
| 1066 | template <class ELFT> void VersionTableSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1067 | this->Size = |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1068 | sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1069 | this->Entsize = sizeof(Elf_Versym); |
George Rimar | 8b3c5f2 | 2016-06-06 08:04:53 +0000 | [diff] [blame] | 1070 | // At the moment of june 2016 GNU docs does not mention that sh_link field |
| 1071 | // 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] | 1072 | this->Link = Out<ELFT>::DynSymTab->SectionIndex; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1076 | auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1; |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1077 | for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) { |
| 1078 | OutVersym->vs_index = S.Symbol->symbol()->VersionId; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1079 | ++OutVersym; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | template <class ELFT> |
| 1084 | VersionNeedSection<ELFT>::VersionNeedSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1085 | : OutputSectionBase(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1086 | this->Addralign = sizeof(uint32_t); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1087 | |
| 1088 | // Identifiers in verneed section start at 2 because 0 and 1 are reserved |
| 1089 | // for VER_NDX_LOCAL and VER_NDX_GLOBAL. |
| 1090 | // First identifiers are reserved by verdef section if it exist. |
| 1091 | NextIndex = getVerDefNum() + 1; |
Rafael Espindola | eaaec4a | 2016-04-29 17:19:45 +0000 | [diff] [blame] | 1092 | } |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1093 | |
| 1094 | template <class ELFT> |
| 1095 | void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) { |
| 1096 | if (!SS->Verdef) { |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1097 | SS->symbol()->VersionId = VER_NDX_GLOBAL; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1098 | return; |
| 1099 | } |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 1100 | SharedFile<ELFT> *F = SS->file(); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1101 | // If we don't already know that we need an Elf_Verneed for this DSO, prepare |
| 1102 | // to create one by adding it to our needed list and creating a dynstr entry |
| 1103 | // for the soname. |
| 1104 | if (F->VerdefMap.empty()) |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1105 | Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())}); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1106 | typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef]; |
| 1107 | // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, |
| 1108 | // prepare to create one by allocating a version identifier and creating a |
| 1109 | // dynstr entry for the version name. |
| 1110 | if (NV.Index == 0) { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1111 | NV.StrTab = In<ELFT>::DynStrTab->addString( |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 1112 | SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1113 | NV.Index = NextIndex++; |
| 1114 | } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1115 | SS->symbol()->VersionId = NV.Index; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1119 | // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. |
| 1120 | auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf); |
| 1121 | auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size()); |
| 1122 | |
| 1123 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) { |
| 1124 | // Create an Elf_Verneed for this DSO. |
| 1125 | Verneed->vn_version = 1; |
| 1126 | Verneed->vn_cnt = P.first->VerdefMap.size(); |
| 1127 | Verneed->vn_file = P.second; |
| 1128 | Verneed->vn_aux = |
| 1129 | reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed); |
| 1130 | Verneed->vn_next = sizeof(Elf_Verneed); |
| 1131 | ++Verneed; |
| 1132 | |
| 1133 | // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over |
| 1134 | // VerdefMap, which will only contain references to needed version |
| 1135 | // definitions. Each Elf_Vernaux is based on the information contained in |
| 1136 | // the Elf_Verdef in the source DSO. This loop iterates over a std::map of |
| 1137 | // pointers, but is deterministic because the pointers refer to Elf_Verdef |
| 1138 | // data structures within a single input file. |
| 1139 | for (auto &NV : P.first->VerdefMap) { |
| 1140 | Vernaux->vna_hash = NV.first->vd_hash; |
| 1141 | Vernaux->vna_flags = 0; |
| 1142 | Vernaux->vna_other = NV.second.Index; |
| 1143 | Vernaux->vna_name = NV.second.StrTab; |
| 1144 | Vernaux->vna_next = sizeof(Elf_Vernaux); |
| 1145 | ++Vernaux; |
| 1146 | } |
| 1147 | |
| 1148 | Vernaux[-1].vna_next = 0; |
| 1149 | } |
| 1150 | Verneed[-1].vn_next = 0; |
| 1151 | } |
| 1152 | |
| 1153 | template <class ELFT> void VersionNeedSection<ELFT>::finalize() { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1154 | this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1155 | this->Info = Needed.size(); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1156 | unsigned Size = Needed.size() * sizeof(Elf_Verneed); |
| 1157 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) |
| 1158 | Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1159 | this->Size = Size; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | template <class ELFT> |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1163 | static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 1164 | return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | template <class ELFT> |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1168 | static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C, |
| 1169 | StringRef OutsecName) { |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1170 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1171 | uintX_t Flags = getOutFlags(C); |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1172 | |
| 1173 | // For SHF_MERGE we create different output sections for each alignment. |
| 1174 | // This makes each output section simple and keeps a single level mapping from |
| 1175 | // input to output. |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 1176 | // In case of relocatable object generation we do not try to perform merging |
| 1177 | // and treat SHF_MERGE sections as regular ones, but also create different |
| 1178 | // output sections for them to allow merging at final linking stage. |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1179 | uintX_t Alignment = 0; |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 1180 | if (isa<MergeInputSection<ELFT>>(C) || |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 1181 | (Config->Relocatable && (C->Flags & SHF_MERGE))) |
| 1182 | Alignment = std::max<uintX_t>(C->Alignment, C->Entsize); |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1183 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 1184 | return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment}; |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1188 | std::pair<OutputSectionBase *, bool> |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1189 | OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C, |
| 1190 | StringRef OutsecName) { |
| 1191 | SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName); |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1192 | return create(Key, C); |
| 1193 | } |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1194 | |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1195 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1196 | std::pair<OutputSectionBase *, bool> |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1197 | OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key, |
| 1198 | InputSectionBase<ELFT> *C) { |
| 1199 | uintX_t Flags = getOutFlags(C); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1200 | OutputSectionBase *&Sec = Map[Key]; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1201 | if (Sec) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1202 | Sec->Flags |= Flags; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1203 | return {Sec, false}; |
| 1204 | } |
| 1205 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 1206 | uint32_t Type = C->Type; |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 1207 | switch (C->kind()) { |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1208 | case InputSectionBase<ELFT>::Regular: |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 1209 | case InputSectionBase<ELFT>::Synthetic: |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 1210 | Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1211 | break; |
| 1212 | case InputSectionBase<ELFT>::EHFrame: |
| 1213 | return {Out<ELFT>::EhFrame, false}; |
| 1214 | case InputSectionBase<ELFT>::Merge: |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 1215 | Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1216 | break; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1217 | } |
| 1218 | return {Sec, true}; |
| 1219 | } |
| 1220 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1221 | template <bool Is64Bits> |
| 1222 | typename lld::elf::SectionKey<Is64Bits> |
| 1223 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() { |
| 1224 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0}; |
| 1225 | } |
| 1226 | |
| 1227 | template <bool Is64Bits> |
| 1228 | typename lld::elf::SectionKey<Is64Bits> |
| 1229 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() { |
| 1230 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0, |
| 1231 | 0}; |
| 1232 | } |
| 1233 | |
| 1234 | template <bool Is64Bits> |
| 1235 | unsigned |
| 1236 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) { |
| 1237 | return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment); |
| 1238 | } |
| 1239 | |
| 1240 | template <bool Is64Bits> |
| 1241 | bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS, |
| 1242 | const Key &RHS) { |
| 1243 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
| 1244 | LHS.Type == RHS.Type && LHS.Flags == RHS.Flags && |
| 1245 | LHS.Alignment == RHS.Alignment; |
| 1246 | } |
| 1247 | |
| 1248 | namespace llvm { |
| 1249 | template struct DenseMapInfo<SectionKey<true>>; |
| 1250 | template struct DenseMapInfo<SectionKey<false>>; |
| 1251 | } |
| 1252 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1253 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 1254 | namespace elf { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 1255 | |
| 1256 | template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); |
| 1257 | template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr); |
| 1258 | template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr); |
| 1259 | template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1260 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 1261 | template class EhFrameHeader<ELF32LE>; |
| 1262 | template class EhFrameHeader<ELF32BE>; |
| 1263 | template class EhFrameHeader<ELF64LE>; |
| 1264 | template class EhFrameHeader<ELF64BE>; |
| 1265 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1266 | template class PltSection<ELF32LE>; |
| 1267 | template class PltSection<ELF32BE>; |
| 1268 | template class PltSection<ELF64LE>; |
| 1269 | template class PltSection<ELF64BE>; |
| 1270 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 1271 | template class GnuHashTableSection<ELF32LE>; |
| 1272 | template class GnuHashTableSection<ELF32BE>; |
| 1273 | template class GnuHashTableSection<ELF64LE>; |
| 1274 | template class GnuHashTableSection<ELF64BE>; |
| 1275 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1276 | template class HashTableSection<ELF32LE>; |
| 1277 | template class HashTableSection<ELF32BE>; |
| 1278 | template class HashTableSection<ELF64LE>; |
| 1279 | template class HashTableSection<ELF64BE>; |
| 1280 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1281 | template class OutputSection<ELF32LE>; |
| 1282 | template class OutputSection<ELF32BE>; |
| 1283 | template class OutputSection<ELF64LE>; |
| 1284 | template class OutputSection<ELF64BE>; |
| 1285 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 1286 | template class EhOutputSection<ELF32LE>; |
| 1287 | template class EhOutputSection<ELF32BE>; |
| 1288 | template class EhOutputSection<ELF64LE>; |
| 1289 | template class EhOutputSection<ELF64BE>; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1290 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1291 | template class MergeOutputSection<ELF32LE>; |
| 1292 | template class MergeOutputSection<ELF32BE>; |
| 1293 | template class MergeOutputSection<ELF64LE>; |
| 1294 | template class MergeOutputSection<ELF64BE>; |
| 1295 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1296 | template class SymbolTableSection<ELF32LE>; |
| 1297 | template class SymbolTableSection<ELF32BE>; |
| 1298 | template class SymbolTableSection<ELF64LE>; |
| 1299 | template class SymbolTableSection<ELF64BE>; |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 1300 | |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1301 | template class VersionTableSection<ELF32LE>; |
| 1302 | template class VersionTableSection<ELF32BE>; |
| 1303 | template class VersionTableSection<ELF64LE>; |
| 1304 | template class VersionTableSection<ELF64BE>; |
| 1305 | |
| 1306 | template class VersionNeedSection<ELF32LE>; |
| 1307 | template class VersionNeedSection<ELF32BE>; |
| 1308 | template class VersionNeedSection<ELF64LE>; |
| 1309 | template class VersionNeedSection<ELF64BE>; |
| 1310 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1311 | template class VersionDefinitionSection<ELF32LE>; |
| 1312 | template class VersionDefinitionSection<ELF32BE>; |
| 1313 | template class VersionDefinitionSection<ELF64LE>; |
| 1314 | template class VersionDefinitionSection<ELF64BE>; |
| 1315 | |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 1316 | template class GdbIndexSection<ELF32LE>; |
| 1317 | template class GdbIndexSection<ELF32BE>; |
| 1318 | template class GdbIndexSection<ELF64LE>; |
| 1319 | template class GdbIndexSection<ELF64BE>; |
| 1320 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1321 | template class OutputSectionFactory<ELF32LE>; |
| 1322 | template class OutputSectionFactory<ELF32BE>; |
| 1323 | template class OutputSectionFactory<ELF64LE>; |
| 1324 | template class OutputSectionFactory<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |