blob: 190d5543f12fd86609c100a8ce8100294908cfc3 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- 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"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000012#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000013#include "Target.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000014#include "llvm/Support/MathExtras.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000015
Rafael Espindola5805c4f2015-09-21 21:38:08 +000016using namespace llvm;
17using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000018using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000019using namespace llvm::ELF;
20
21using namespace lld;
22using namespace lld::elf2;
23
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000024template <class ELFT>
25OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t sh_type,
26 uintX_t sh_flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000028 memset(&Header, 0, sizeof(Elf_Shdr));
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029 Header.sh_type = sh_type;
30 Header.sh_flags = sh_flags;
31}
32
Rafael Espindola35c6af32015-09-25 17:19:10 +000033template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000034GotPltSection<ELFT>::GotPltSection()
35 : OutputSectionBase<ELFT>(".got.plt", llvm::ELF::SHT_PROGBITS,
36 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
37 this->Header.sh_addralign = sizeof(uintX_t);
38 // .got.plt has 3 reserved entry
39 Entries.resize(3);
40}
41
42template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
43 Sym->GotPltIndex = Entries.size();
44 Entries.push_back(Sym);
45}
46
47template <class ELFT> bool GotPltSection<ELFT>::empty() const {
48 return Entries.size() == 3;
49}
50
51template <class ELFT>
52typename GotPltSection<ELFT>::uintX_t
53GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
54 return this->getVA() + B.GotPltIndex * sizeof(uintX_t);
55}
56
57template <class ELFT> void GotPltSection<ELFT>::finalize() {
58 this->Header.sh_size = Entries.size() * sizeof(uintX_t);
59}
60
61template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
62 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(
63 Buf, Out<ELFT>::Dynamic->getVA());
64 for (const SymbolBody *B : Entries) {
65 if (B)
66 Target->writeGotPltEntry(Buf, Out<ELFT>::Plt->getEntryAddr(*B));
67 Buf += sizeof(uintX_t);
68 }
69}
70
71template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000072GotSection<ELFT>::GotSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000073 : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS,
74 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
Rui Ueyama5f551ae2015-10-14 14:02:06 +000075 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000076}
77
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +000079 Sym->GotIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080 Entries.push_back(Sym);
81}
82
83template <class ELFT>
84typename GotSection<ELFT>::uintX_t
85GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama5f551ae2015-10-14 14:02:06 +000086 return this->getVA() + B.GotIndex * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087}
88
Rafael Espindolaa6627382015-10-06 23:56:53 +000089template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
90 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +000091 uint8_t *Entry = Buf;
92 Buf += sizeof(uintX_t);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000093 if (canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +000094 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +000095 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +000096 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +000097 }
98}
99
Rafael Espindola35c6af32015-09-25 17:19:10 +0000100template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000101PltSection<ELFT>::PltSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000102 : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
103 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000104 this->Header.sh_addralign = 16;
105}
106
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000108 size_t Off = 0;
George Rimar648a2c32015-10-20 08:54:27 +0000109 bool LazyReloc = Target->supportsLazyRelocations();
110 if (LazyReloc) {
111 // First write PLT[0] entry which is special.
112 Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
113 Off += Target->getPltZeroEntrySize();
114 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115 for (const SymbolBody *E : Entries) {
George Rimar648a2c32015-10-20 08:54:27 +0000116 uint64_t Got = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
117 : Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000118 uint64_t Plt = this->getVA() + Off;
George Rimar648a2c32015-10-20 08:54:27 +0000119 Target->writePltEntry(Buf + Off, Got, Plt, E->PltIndex);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000120 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000121 }
122}
123
124template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000125 Sym->PltIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126 Entries.push_back(Sym);
127}
128
129template <class ELFT>
130typename PltSection<ELFT>::uintX_t
131PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
George Rimar648a2c32015-10-20 08:54:27 +0000132 return this->getVA() + Target->getPltZeroEntrySize() +
133 B.PltIndex * Target->getPltEntrySize();
134}
135
136template <class ELFT> void PltSection<ELFT>::finalize() {
137 this->Header.sh_size = Target->getPltZeroEntrySize() +
138 Entries.size() * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000139}
140
141template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000142RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
143 : OutputSectionBase<ELFT>(Name,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000144 IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
145 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000146 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000147 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
148 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
149}
150
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000151template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000152 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000153 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000154 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
155 Buf += EntrySize;
156
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157 const InputSection<ELFT> &C = Rel.C;
158 const Elf_Rel &RI = Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000159 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000160 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000161 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000162 if (Body)
163 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000164
Rui Ueyama64558522015-10-16 22:51:43 +0000165 uint32_t Type = RI.getType(Config->Mips64EL);
George Rimarbc590fe2015-10-28 16:48:58 +0000166 bool NeedsCopy = Body && Target->relocNeedsCopy(Type, *Body);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000167 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
168 bool CanBePreempted = canBePreempted(Body, NeedsGot);
George Rimar648a2c32015-10-20 08:54:27 +0000169 bool LazyReloc = Body && Target->supportsLazyRelocations() &&
170 Target->relocNeedsPlt(Type, *Body);
Rafael Espindola49757522015-10-19 19:58:18 +0000171
172 if (CanBePreempted) {
173 if (NeedsGot)
174 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
George Rimar648a2c32015-10-20 08:54:27 +0000175 LazyReloc ? Target->getPltReloc()
176 : Target->getGotReloc(),
177 Config->Mips64EL);
Rafael Espindola49757522015-10-19 19:58:18 +0000178 else
George Rimarbc590fe2015-10-28 16:48:58 +0000179 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
180 NeedsCopy ? Target->getCopyReloc() : Type,
Rafael Espindola49757522015-10-19 19:58:18 +0000181 Config->Mips64EL);
182 } else {
Rui Ueyama64558522015-10-16 22:51:43 +0000183 P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000184 }
185
George Rimar648a2c32015-10-20 08:54:27 +0000186 if (NeedsGot) {
187 if (LazyReloc)
188 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
189 else
190 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimarbc590fe2015-10-28 16:48:58 +0000191 } else if (NeedsCopy) {
192 P->r_offset = Out<ELFT>::Bss->getVA() +
193 dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBSS;
George Rimar648a2c32015-10-20 08:54:27 +0000194 } else {
Rui Ueyama55c3f892015-10-15 01:58:40 +0000195 P->r_offset = RI.r_offset + C.OutSec->getVA() + C.OutSecOff;
George Rimar648a2c32015-10-20 08:54:27 +0000196 }
Rafael Espindola49757522015-10-19 19:58:18 +0000197
Rafael Espindola932efcf2015-10-19 20:24:44 +0000198 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000199 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000200 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000201
Rafael Espindola932efcf2015-10-19 20:24:44 +0000202 uintX_t Addend;
George Rimarbc590fe2015-10-28 16:48:58 +0000203 if (NeedsCopy)
204 Addend = 0;
205 else if (CanBePreempted)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000206 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000207 else if (Body)
208 Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend;
209 else if (IsRela)
210 Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI));
211 else
212 Addend = getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000213
214 if (IsRela)
215 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000216 }
217}
218
219template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000220 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
222}
223
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000224template <class ELFT>
225InterpSection<ELFT>::InterpSection()
226 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
227 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000228 this->Header.sh_size = Config->DynamicLinker.size() + 1;
229 this->Header.sh_addralign = 1;
230}
231
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000232template <class ELFT>
233void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
Rui Ueyama157c4332015-10-24 17:57:39 +0000234 Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000235 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000236}
237
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000238template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
240}
241
Rafael Espindola35c6af32015-09-25 17:19:10 +0000242template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000243HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000244 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
245 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000246 this->Header.sh_entsize = sizeof(Elf_Word);
247 this->Header.sh_addralign = sizeof(Elf_Word);
248}
249
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000250static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000251 uint32_t H = 0;
252 for (char C : Name) {
253 H = (H << 4) + C;
254 uint32_t G = H & 0xf0000000;
255 if (G)
256 H ^= G >> 24;
257 H &= ~G;
258 }
259 return H;
260}
261
Rui Ueyama0db335f2015-10-07 16:58:54 +0000262template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000263 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000264
Rui Ueyama0db335f2015-10-07 16:58:54 +0000265 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000266 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000267
268 // Create as many buckets as there are symbols.
269 // FIXME: This is simplistic. We can try to optimize it, but implementing
270 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000271 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000272 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
273}
274
275template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000276 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000277 auto *P = reinterpret_cast<Elf_Word *>(Buf);
278 *P++ = NumSymbols; // nbucket
279 *P++ = NumSymbols; // nchain
280
281 Elf_Word *Buckets = P;
282 Elf_Word *Chains = P + NumSymbols;
283
Igor Kudrinf1d60292015-10-28 07:05:56 +0000284 for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
Igor Kudrinab665fc2015-10-20 21:47:58 +0000285 StringRef Name = Body->getName();
286 unsigned I = Body->getDynamicSymbolTableIndex();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000287 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000288 Chains[I] = Buckets[Hash];
289 Buckets[Hash] = I;
290 }
291}
292
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000293static uint32_t hashGnu(StringRef Name) {
294 uint32_t H = 5381;
295 for (uint8_t C : Name)
296 H = (H << 5) + H + C;
297 return H;
298}
299
300template <class ELFT>
301GnuHashTableSection<ELFT>::GnuHashTableSection()
302 : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH,
303 llvm::ELF::SHF_ALLOC) {
304 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
305 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
306}
307
308template <class ELFT>
309unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
310 if (!NumHashed)
311 return 0;
312
313 // These values are prime numbers which are not greater than 2^(N-1) + 1.
314 // In result, for any particular NumHashed we return a prime number
315 // which is not greater than NumHashed.
316 static const unsigned Primes[] = {
317 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
318 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
319
320 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
321 array_lengthof(Primes) - 1)];
322}
323
324// Bloom filter estimation: at least 8 bits for each hashed symbol.
325// GNU Hash table requirement: it should be a power of 2,
326// the minimum value is 1, even for an empty table.
327// Expected results for a 32-bit target:
328// calcMaskWords(0..4) = 1
329// calcMaskWords(5..8) = 2
330// calcMaskWords(9..16) = 4
331// For a 64-bit target:
332// calcMaskWords(0..8) = 1
333// calcMaskWords(9..16) = 2
334// calcMaskWords(17..32) = 4
335template <class ELFT>
336unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
337 if (!NumHashed)
338 return 1;
339 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
340}
341
342template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000343 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000344 NBuckets = calcNBuckets(NumHashed);
345 MaskWords = calcMaskWords(NumHashed);
346 // Second hash shift estimation: just predefined values.
347 Shift2 = ELFT::Is64Bits ? 6 : 5;
348
349 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
350 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
351 + sizeof(Elf_Off) * MaskWords // Bloom Filter
352 + sizeof(Elf_Word) * NBuckets // Hash Buckets
353 + sizeof(Elf_Word) * NumHashed; // Hash Values
354}
355
356template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
357 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000358 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000359 return;
360 writeBloomFilter(Buf);
361 writeHashTable(Buf);
362}
363
364template <class ELFT>
365void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
366 auto *P = reinterpret_cast<Elf_Word *>(Buf);
367 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000368 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000369 *P++ = MaskWords;
370 *P++ = Shift2;
371 Buf = reinterpret_cast<uint8_t *>(P);
372}
373
374template <class ELFT>
375void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
376 unsigned C = sizeof(Elf_Off) * 8;
377
378 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000379 for (const HashedSymbolData &Item : HashedSymbols) {
380 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
381 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
382 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000383 Masks[Pos] |= V;
384 }
385 Buf += sizeof(Elf_Off) * MaskWords;
386}
387
388template <class ELFT>
389void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
390 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
391 Elf_Word *Values = Buckets + NBuckets;
392
393 int PrevBucket = -1;
394 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000395 for (const HashedSymbolData &Item : HashedSymbols) {
396 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000397 assert(PrevBucket <= Bucket);
398 if (Bucket != PrevBucket) {
399 Buckets[Bucket] = Item.Body->getDynamicSymbolTableIndex();
400 PrevBucket = Bucket;
401 if (I > 0)
402 Values[I - 1] |= 1;
403 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000404 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000405 ++I;
406 }
407 if (I > 0)
408 Values[I - 1] |= 1;
409}
410
Rafael Espindola31f88882015-11-02 14:33:11 +0000411static bool includeInGnuHashTable(SymbolBody *B) {
412 // Assume that includeInDynamicSymtab() is already checked.
413 return !B->isUndefined();
414}
415
Rafael Espindola35c6af32015-09-25 17:19:10 +0000416template <class ELFT>
Igor Kudrinf1d60292015-10-28 07:05:56 +0000417void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
418 std::vector<SymbolBody *> NotHashed;
419 NotHashed.reserve(Symbols.size());
420 HashedSymbols.reserve(Symbols.size());
421 for (SymbolBody *B : Symbols) {
422 if (includeInGnuHashTable(B))
423 HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
424 else
425 NotHashed.push_back(B);
426 }
427 if (HashedSymbols.empty())
428 return;
429
430 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
431 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
432 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
433 return L.Hash % NBuckets < R.Hash % NBuckets;
434 });
435
436 Symbols = std::move(NotHashed);
437 for (const HashedSymbolData &Item : HashedSymbols)
438 Symbols.push_back(Item.Body);
439}
440
441template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000442DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000443 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
444 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000445 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000446 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000447 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
448 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
449}
450
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000451template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000452 if (this->Header.sh_size)
453 return; // Already finalized.
454
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000455 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000456 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000457
458 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000459 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000460 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000461 ++NumEntries; // DT_RELASZ / DT_RELSZ
462 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000463 }
George Rimar648a2c32015-10-20 08:54:27 +0000464 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
465 ++NumEntries; // DT_JMPREL
466 ++NumEntries; // DT_PLTRELSZ
467 ++NumEntries; // DT_PLTGOT
468 ++NumEntries; // DT_PLTREL
469 }
470
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000471 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000472 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000473 ++NumEntries; // DT_STRTAB
474 ++NumEntries; // DT_STRSZ
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000475 if (Out<ELFT>::GnuHashTab)
476 ++NumEntries; // DT_GNU_HASH
477 if (Out<ELFT>::HashTab)
478 ++NumEntries; // DT_HASH
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000479
Rui Ueyama7de3f372015-10-01 19:36:04 +0000480 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000481 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000482 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000483 }
484
485 if (!Config->SoName.empty()) {
486 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000487 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000488 }
489
Rafael Espindola77572242015-10-02 19:37:55 +0000490 if (PreInitArraySec)
491 NumEntries += 2;
492 if (InitArraySec)
493 NumEntries += 2;
494 if (FiniArraySec)
495 NumEntries += 2;
496
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000497 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000498 if (!F->isNeeded())
499 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000500 Out<ELFT>::DynStrTab->add(F->getSoName());
501 ++NumEntries;
502 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000503
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000504 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
505 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
506 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
507 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000508 if (InitSym)
509 ++NumEntries; // DT_INIT
510 if (FiniSym)
511 ++NumEntries; // DT_FINI
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000512
513 if (Config->Bsymbolic)
514 DtFlags |= DF_SYMBOLIC;
515 if (Config->ZNodelete)
516 DtFlags1 |= DF_1_NODELETE;
517 if (Config->ZNow) {
518 DtFlags |= DF_BIND_NOW;
519 DtFlags1 |= DF_1_NOW;
520 }
521 if (Config->ZOrigin) {
522 DtFlags |= DF_ORIGIN;
523 DtFlags1 |= DF_1_ORIGIN;
524 }
525
526 if (DtFlags)
Davide Italiano58cbaf02015-10-19 23:32:16 +0000527 ++NumEntries; // DT_FLAGS
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000528 if (DtFlags1)
George Rimar97aad172015-10-07 15:00:21 +0000529 ++NumEntries; // DT_FLAGS_1
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000530 ++NumEntries; // DT_NULL
531
532 Header.sh_size = NumEntries * Header.sh_entsize;
533}
534
535template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000536 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
537
Rui Ueyama8c205d52015-10-02 01:33:31 +0000538 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
539 P->d_tag = Tag;
540 P->d_un.d_ptr = Val;
541 ++P;
542 };
543
544 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
545 P->d_tag = Tag;
546 P->d_un.d_val = Val;
547 ++P;
548 };
549
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000550 if (Out<ELFT>::RelaDyn->hasRelocs()) {
551 bool IsRela = Out<ELFT>::RelaDyn->isRela();
552 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
553 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000554 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
555 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000556 }
George Rimar648a2c32015-10-20 08:54:27 +0000557 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
558 WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
559 WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
560 WritePtr(DT_PLTGOT, Out<ELFT>::GotPlt->getVA());
561 WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
562 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000563
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000564 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000565 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000566 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
567 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000568 if (Out<ELFT>::GnuHashTab)
569 WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
570 if (Out<ELFT>::HashTab)
571 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000572
Rui Ueyama8c205d52015-10-02 01:33:31 +0000573 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000574
575 // If --enable-new-dtags is set lld emits DT_RUNPATH
576 // instead of DT_RPATH. The two tags are functionally
577 // equivalent except for the following:
578 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
579 // DT_RPATH is searched before.
580 // - DT_RUNPATH is used only to search for direct
581 // dependencies of the object it's contained in, while
582 // DT_RPATH is used for indirect dependencies as well.
583 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000584 Out<ELFT>::DynStrTab->getOffset(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000585
Rui Ueyama8c205d52015-10-02 01:33:31 +0000586 if (!Config->SoName.empty())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000587 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000588
Rafael Espindola77572242015-10-02 19:37:55 +0000589 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000590 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000591 if (!Sec)
592 return;
593 WritePtr(T1, Sec->getVA());
594 WriteVal(T2, Sec->getSize());
595 };
596 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
597 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
598 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
599
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000600 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000601 if (F->isNeeded())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000602 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000603
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000604 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000605 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000606 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000607 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000608 if (DtFlags)
609 WriteVal(DT_FLAGS, DtFlags);
610 if (DtFlags1)
611 WriteVal(DT_FLAGS_1, DtFlags1);
Rui Ueyama8c205d52015-10-02 01:33:31 +0000612 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000613}
614
615template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000616OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000617 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000618 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000619
620template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000621void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
622 Sections.push_back(C);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000623 C->OutSec = this;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000624 uint32_t Align = C->getAlign();
625 if (Align > this->Header.sh_addralign)
626 this->Header.sh_addralign = Align;
627
628 uintX_t Off = this->Header.sh_size;
629 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000630 C->OutSecOff = Off;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000631 Off += C->getSize();
632 this->Header.sh_size = Off;
633}
634
635template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000636typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000637 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000638 case SymbolBody::DefinedSyntheticKind: {
639 auto &D = cast<DefinedSynthetic<ELFT>>(S);
640 return D.Section.getVA() + D.Sym.st_value;
641 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000642 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000643 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000644 case SymbolBody::DefinedRegularKind: {
645 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola48225b42015-10-23 19:55:11 +0000646 InputSectionBase<ELFT> &SC = DR.Section;
Rafael Espindolac159c962015-10-19 21:00:02 +0000647 return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000648 }
649 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000650 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
George Rimarbc590fe2015-10-28 16:48:58 +0000651 case SymbolBody::SharedKind: {
652 auto &SS = cast<SharedSymbol<ELFT>>(S);
653 if (SS.NeedsCopy)
654 return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
655 return 0;
656 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000657 case SymbolBody::UndefinedKind:
658 return 0;
659 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000660 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
661 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000662 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000663 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000664}
665
Rui Ueyama34f29242015-10-13 19:51:57 +0000666// Returns a VA which a relocatin RI refers to. Used only for local symbols.
667// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000668template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000669typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000670lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000671 const Elf_Rel_Impl<ELFT, IsRela> &RI) {
672 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
673 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
674
675 uintX_t Addend = getAddend<ELFT>(RI);
676
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000677 // PPC64 has a special relocation representing the TOC base pointer
678 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000679 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000680 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000681
Rui Ueyama126d08f2015-10-12 20:28:22 +0000682 const Elf_Sym *Sym =
683 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000684
Rui Ueyama126d08f2015-10-12 20:28:22 +0000685 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000686 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000687
Rafael Espindola444576d2015-10-09 19:25:07 +0000688 // According to the ELF spec reference to a local symbol from outside
689 // the group are not allowed. Unfortunately .eh_frame breaks that rule
690 // and must be treated specially. For now we just replace the symbol with
691 // 0.
Rafael Espindolac159c962015-10-19 21:00:02 +0000692 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
Rafael Espindola444576d2015-10-09 19:25:07 +0000693 if (Section == &InputSection<ELFT>::Discarded)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000694 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000695
Rafael Espindolac159c962015-10-19 21:00:02 +0000696 uintX_t VA = Section->OutSec->getVA();
697 if (isa<InputSection<ELFT>>(Section))
698 return VA + Section->getOffset(*Sym) + Addend;
699
700 uintX_t Offset = Sym->st_value;
701 if (Sym->getType() == STT_SECTION) {
702 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000703 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000704 }
705 return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
706 Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000707}
708
Rui Ueyama34f29242015-10-13 19:51:57 +0000709// Returns true if a symbol can be replaced at load-time by a symbol
710// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000711bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000712 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000713 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000714 if (Body->isShared())
715 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000716
717 if (Body->isUndefined()) {
718 if (!Body->isWeak())
719 return true;
720
721 // This is an horrible corner case. Ideally we would like to say that any
722 // undefined symbol can be preempted so that the dynamic linker has a
723 // chance of finding it at runtime.
724 //
725 // The problem is that the code sequence used to test for weak undef
726 // functions looks like
727 // if (func) func()
728 // If the code is -fPIC the first reference is a load from the got and
729 // everything works.
730 // If the code is not -fPIC there is no reasonable way to solve it:
731 // * A relocation writing to the text segment will fail (it is ro).
732 // * A copy relocation doesn't work for functions.
733 // * The trick of using a plt entry as the address would fail here since
734 // the plt entry would have a non zero address.
735 // Since we cannot do anything better, we just resolve the symbol to 0 and
736 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000737 //
738 // As an extra hack, assume that if we are producing a shared library the
739 // user knows what he or she is doing and can handle a dynamic relocation.
740 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000741 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000742 if (!Config->Shared)
743 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000744 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000745}
746
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000747template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000748 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000749 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000750}
751
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000752template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000753MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
754 uintX_t sh_flags)
755 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
756
757template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000758 if (shouldTailMerge()) {
759 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +0000760 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000761 return;
Rafael Espindolac159c962015-10-19 21:00:02 +0000762 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000763 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
764 StringRef Data = P.first;
765 memcpy(Buf + P.second, Data.data(), Data.size());
766 }
767}
768
769static size_t findNull(StringRef S, size_t EntSize) {
770 // Optimize the common case.
771 if (EntSize == 1)
772 return S.find(0);
773
774 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
775 const char *B = S.begin() + I;
776 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
777 return I;
778 }
779 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +0000780}
781
782template <class ELFT>
783void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
784 S->OutSec = this;
785 uint32_t Align = S->getAlign();
786 if (Align > this->Header.sh_addralign)
787 this->Header.sh_addralign = Align;
788
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000789 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +0000790 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +0000791 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000792 uintX_t Offset = 0;
793
794 if (this->Header.sh_flags & SHF_STRINGS) {
795 while (!Data.empty()) {
796 size_t End = findNull(Data, EntSize);
797 if (End == StringRef::npos)
798 error("String is not null terminated");
799 StringRef Entry = Data.substr(0, End + EntSize);
800 size_t OutputOffset = Builder.add(Entry);
801 if (shouldTailMerge())
802 OutputOffset = -1;
803 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
804 uintX_t Size = End + EntSize;
805 Data = Data.substr(Size);
806 Offset += Size;
807 }
808 } else {
809 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
810 StringRef Entry = Data.substr(I, EntSize);
811 size_t OutputOffset = Builder.add(Entry);
812 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
813 Offset += EntSize;
814 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000815 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000816}
817
818template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000819unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
820 return Builder.getOffset(Val);
821}
822
823template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
824 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
825}
826
827template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
828 if (shouldTailMerge())
829 Builder.finalize();
830 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +0000831}
832
833template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +0000834StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
835 : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000836 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +0000837 Dynamic(Dynamic) {
838 this->Header.sh_addralign = 1;
839}
840
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000841template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000842 StringRef Data = StrTabBuilder.data();
843 memcpy(Buf, Data.data(), Data.size());
844}
845
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000846template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000847 if (!B.isUsedInRegularObj())
848 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000849
850 // Don't include synthetic symbols like __init_array_start in every output.
851 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
852 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
853 return false;
854
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000855 return true;
856}
857
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000858bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000859 uint8_t V = B.getVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000860 if (V != STV_DEFAULT && V != STV_PROTECTED)
861 return false;
862
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000863 if (Config->ExportDynamic || Config->Shared)
864 return true;
865 return B.isUsedInDynamicReloc();
866}
867
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000868template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +0000869bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
870 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000871 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
872 if (Sym.getType() == STT_SECTION)
873 return false;
874
Rafael Espindola444576d2015-10-09 19:25:07 +0000875 // If sym references a section in a discarded group, don't keep it.
Rafael Espindola4cda5812015-10-16 15:29:48 +0000876 if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
877 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +0000878
Davide Italiano6993ba42015-09-26 00:47:56 +0000879 if (Config->DiscardNone)
880 return true;
881
882 // ELF defines dynamic locals as symbols which name starts with ".L".
883 return !(Config->DiscardLocals && SymName.startswith(".L"));
884}
885
Rafael Espindola35c6af32015-09-25 17:19:10 +0000886template <class ELFT>
887SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000888 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
889 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +0000890 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
891 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
892 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000893 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000894 typedef OutputSectionBase<ELFT> Base;
895 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000896
897 Header.sh_entsize = sizeof(Elf_Sym);
898 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
899}
900
Rui Ueyama0db335f2015-10-07 16:58:54 +0000901template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000902 if (this->Header.sh_size)
903 return; // Already finalized.
904
Rui Ueyama0db335f2015-10-07 16:58:54 +0000905 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000906 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000907 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000908
909 if (!StrTabSec.isDynamic()) {
910 std::stable_sort(Symbols.begin(), Symbols.end(),
Igor Kudrinf1d60292015-10-28 07:05:56 +0000911 [](SymbolBody *L, SymbolBody *R) {
912 return getSymbolBinding(L) == STB_LOCAL &&
913 getSymbolBinding(R) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000914 });
915 return;
916 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000917 if (Out<ELFT>::GnuHashTab)
918 // NB: It also sorts Symbols to meet the GNU hash table requirements.
919 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +0000920 size_t I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000921 for (SymbolBody *B : Symbols)
922 B->setDynamicSymbolTableIndex(++I);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000923}
924
925template <class ELFT>
Igor Kudrinab665fc2015-10-20 21:47:58 +0000926void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
Rui Ueyama0db335f2015-10-07 16:58:54 +0000927 StrTabSec.add(Name);
928 ++NumVisible;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000929 ++NumLocals;
930}
931
932template <class ELFT>
933void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
934 StrTabSec.add(Body->getName());
Igor Kudrinf1d60292015-10-28 07:05:56 +0000935 Symbols.push_back(Body);
Igor Kudrinab665fc2015-10-20 21:47:58 +0000936 ++NumVisible;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000937}
938
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000939template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000940 Buf += sizeof(Elf_Sym);
941
942 // All symbols with STB_LOCAL binding precede the weak and global symbols.
943 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000944 if (!Config->DiscardAll && !StrTabSec.isDynamic())
945 writeLocalSymbols(Buf);
946
947 writeGlobalSymbols(Buf);
948}
949
950template <class ELFT>
951void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
952 // Iterate over all input object files to copy their local symbols
953 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000954 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
955 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000956 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000957 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000958 error(SymNameOrErr);
959 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000960 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000961 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +0000962
Rui Ueyamac55733e2015-09-30 00:54:29 +0000963 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +0000964 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000965 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000966 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +0000967 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000968 } else {
Rafael Espindola48225b42015-10-23 19:55:11 +0000969 InputSectionBase<ELFT> *Section = File->getSection(Sym);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000970 if (!Section->isLive())
971 continue;
Rafael Espindolac159c962015-10-19 21:00:02 +0000972 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
973 ESym->st_shndx = OutSec->SectionIndex;
974 VA += OutSec->getVA() + Section->getOffset(Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000975 }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000976 ESym->st_name = StrTabSec.getOffset(SymName);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000977 ESym->st_size = Sym.st_size;
978 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000979 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000980 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000981 }
982 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000983}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000984
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000985template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +0000986void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000987 // Write the internal symbol table contents to the output symbol table
988 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +0000989 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000990 for (SymbolBody *Body : Symbols) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000991 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000992
Rafael Espindola8614c562015-10-06 14:33:58 +0000993 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000994 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +0000995 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000996 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000997 case SymbolBody::DefinedRegularKind: {
998 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
999 if (!Sym->Section.isLive())
1000 continue;
1001 OutSec = Sym->Section.OutSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001002 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001003 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001004 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001005 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001006 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001007 case SymbolBody::SharedKind: {
1008 if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
1009 OutSec = Out<ELFT>::Bss;
1010 break;
1011 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001012 case SymbolBody::UndefinedKind:
1013 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001014 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001015 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001016 }
1017
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001018 StringRef Name = Body->getName();
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001019 ESym->st_name = StrTabSec.getOffset(Name);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001020
Rafael Espindola8614c562015-10-06 14:33:58 +00001021 unsigned char Type = STT_NOTYPE;
1022 uintX_t Size = 0;
1023 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1024 const Elf_Sym &InputSym = EBody->Sym;
Rafael Espindola8614c562015-10-06 14:33:58 +00001025 Type = InputSym.getType();
1026 Size = InputSym.st_size;
1027 }
1028
Igor Kudrin853b88d2015-10-20 20:52:14 +00001029 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001030 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001031 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001032 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001033
Rafael Espindola8614c562015-10-06 14:33:58 +00001034 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +00001035 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +00001036 else if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001037 ESym->st_shndx = OutSec->SectionIndex;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001038
1039 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001040 }
1041}
1042
Igor Kudrin853b88d2015-10-20 20:52:14 +00001043template <class ELFT>
1044uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001045 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001046 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1047 return STB_LOCAL;
1048 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1049 return EBody->Sym.getBinding();
1050 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1051}
1052
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001053namespace lld {
1054namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001055template class OutputSectionBase<ELF32LE>;
1056template class OutputSectionBase<ELF32BE>;
1057template class OutputSectionBase<ELF64LE>;
1058template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001059
George Rimar648a2c32015-10-20 08:54:27 +00001060template class GotPltSection<ELF32LE>;
1061template class GotPltSection<ELF32BE>;
1062template class GotPltSection<ELF64LE>;
1063template class GotPltSection<ELF64BE>;
1064
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001065template class GotSection<ELF32LE>;
1066template class GotSection<ELF32BE>;
1067template class GotSection<ELF64LE>;
1068template class GotSection<ELF64BE>;
1069
1070template class PltSection<ELF32LE>;
1071template class PltSection<ELF32BE>;
1072template class PltSection<ELF64LE>;
1073template class PltSection<ELF64BE>;
1074
1075template class RelocationSection<ELF32LE>;
1076template class RelocationSection<ELF32BE>;
1077template class RelocationSection<ELF64LE>;
1078template class RelocationSection<ELF64BE>;
1079
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001080template class InterpSection<ELF32LE>;
1081template class InterpSection<ELF32BE>;
1082template class InterpSection<ELF64LE>;
1083template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001084
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001085template class GnuHashTableSection<ELF32LE>;
1086template class GnuHashTableSection<ELF32BE>;
1087template class GnuHashTableSection<ELF64LE>;
1088template class GnuHashTableSection<ELF64BE>;
1089
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001090template class HashTableSection<ELF32LE>;
1091template class HashTableSection<ELF32BE>;
1092template class HashTableSection<ELF64LE>;
1093template class HashTableSection<ELF64BE>;
1094
1095template class DynamicSection<ELF32LE>;
1096template class DynamicSection<ELF32BE>;
1097template class DynamicSection<ELF64LE>;
1098template class DynamicSection<ELF64BE>;
1099
1100template class OutputSection<ELF32LE>;
1101template class OutputSection<ELF32BE>;
1102template class OutputSection<ELF64LE>;
1103template class OutputSection<ELF64BE>;
1104
Rafael Espindolac159c962015-10-19 21:00:02 +00001105template class MergeOutputSection<ELF32LE>;
1106template class MergeOutputSection<ELF32BE>;
1107template class MergeOutputSection<ELF64LE>;
1108template class MergeOutputSection<ELF64BE>;
1109
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001110template class StringTableSection<ELF32LE>;
1111template class StringTableSection<ELF32BE>;
1112template class StringTableSection<ELF64LE>;
1113template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001114
1115template class SymbolTableSection<ELF32LE>;
1116template class SymbolTableSection<ELF32BE>;
1117template class SymbolTableSection<ELF64LE>;
1118template class SymbolTableSection<ELF64BE>;
1119
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001120template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1121template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1122template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1123template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001124
Rafael Espindola56f965f2015-09-21 22:48:12 +00001125template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001126getLocalRelTarget(const ObjectFile<ELF32LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001127 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001128template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001129getLocalRelTarget(const ObjectFile<ELF32BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001130 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001131template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001132getLocalRelTarget(const ObjectFile<ELF64LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001133 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001134template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001135getLocalRelTarget(const ObjectFile<ELF64BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001136 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001137
Rafael Espindolac159c962015-10-19 21:00:02 +00001138template ELFFile<ELF32LE>::uintX_t
1139getLocalRelTarget(const ObjectFile<ELF32LE> &,
1140 const ELFFile<ELF32LE>::Elf_Rela &);
1141template ELFFile<ELF32BE>::uintX_t
1142getLocalRelTarget(const ObjectFile<ELF32BE> &,
1143 const ELFFile<ELF32BE>::Elf_Rela &);
1144template ELFFile<ELF64LE>::uintX_t
1145getLocalRelTarget(const ObjectFile<ELF64LE> &,
1146 const ELFFile<ELF64LE>::Elf_Rela &);
1147template ELFFile<ELF64BE>::uintX_t
1148getLocalRelTarget(const ObjectFile<ELF64BE> &,
1149 const ELFFile<ELF64BE>::Elf_Rela &);
1150
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001151template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1152template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1153template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1154template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001155
Rafael Espindola444576d2015-10-09 19:25:07 +00001156template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1157 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001158 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001159template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1160 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001161 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001162template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1163 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001164 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001165template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1166 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001167 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001168}
1169}