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"
12 #include "SymbolTable.h"
13 #include "Target.h"
14 #include "llvm/Support/MathExtras.h"
15 
16 using namespace llvm;
17 using namespace llvm::object;
18 using namespace llvm::support::endian;
19 using namespace llvm::ELF;
20 
21 using namespace lld;
22 using namespace lld::elf2;
23 
24 template <class ELFT>
25 OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t sh_type,
26                                            uintX_t sh_flags)
27     : Name(Name) {
28   memset(&Header, 0, sizeof(Elf_Shdr));
29   Header.sh_type = sh_type;
30   Header.sh_flags = sh_flags;
31 }
32 
33 template <class ELFT>
34 GotPltSection<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 
42 template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
43   Sym->GotPltIndex = Entries.size();
44   Entries.push_back(Sym);
45 }
46 
47 template <class ELFT> bool GotPltSection<ELFT>::empty() const {
48   return Entries.size() == 3;
49 }
50 
51 template <class ELFT>
52 typename GotPltSection<ELFT>::uintX_t
53 GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
54   return this->getVA() + B.GotPltIndex * sizeof(uintX_t);
55 }
56 
57 template <class ELFT> void GotPltSection<ELFT>::finalize() {
58   this->Header.sh_size = Entries.size() * sizeof(uintX_t);
59 }
60 
61 template <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 
71 template <class ELFT>
72 GotSection<ELFT>::GotSection()
73     : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS,
74                               llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
75   if (Config->EMachine == EM_MIPS)
76     this->Header.sh_flags |= llvm::ELF::SHF_MIPS_GPREL;
77   this->Header.sh_addralign = sizeof(uintX_t);
78 }
79 
80 template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
81   Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size();
82   Entries.push_back(Sym);
83 }
84 
85 template <class ELFT>
86 typename GotSection<ELFT>::uintX_t
87 GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
88   return this->getVA() + B.GotIndex * sizeof(uintX_t);
89 }
90 
91 template <class ELFT> void GotSection<ELFT>::finalize() {
92   this->Header.sh_size =
93       (Target->getGotHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
94 }
95 
96 template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
97   Target->writeGotHeaderEntries(Buf);
98   Buf += Target->getGotHeaderEntriesNum() * sizeof(uintX_t);
99   for (const SymbolBody *B : Entries) {
100     uint8_t *Entry = Buf;
101     Buf += sizeof(uintX_t);
102     // MIPS has special rules to fill up GOT entries.
103     // See "Global Offset Table" in Chapter 5 in the following document
104     // for detailed description:
105     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
106     // As the first approach, we can just store addresses for all symbols.
107     if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
108       continue; // The dynamic linker will take care of it.
109     uintX_t VA = getSymVA<ELFT>(*B);
110     write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
111   }
112 }
113 
114 template <class ELFT>
115 PltSection<ELFT>::PltSection()
116     : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
117                               llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
118   this->Header.sh_addralign = 16;
119 }
120 
121 template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
122   size_t Off = 0;
123   bool LazyReloc = Target->supportsLazyRelocations();
124   if (LazyReloc) {
125     // First write PLT[0] entry which is special.
126     Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
127     Off += Target->getPltZeroEntrySize();
128   }
129   for (const SymbolBody *E : Entries) {
130     uint64_t Got = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
131                              : Out<ELFT>::Got->getEntryAddr(*E);
132     uint64_t Plt = this->getVA() + Off;
133     Target->writePltEntry(Buf + Off, Got, Plt, E->PltIndex);
134     Off += Target->getPltEntrySize();
135   }
136 }
137 
138 template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
139   Sym->PltIndex = Entries.size();
140   Entries.push_back(Sym);
141 }
142 
143 template <class ELFT>
144 typename PltSection<ELFT>::uintX_t
145 PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
146   return this->getVA() + Target->getPltZeroEntrySize() +
147          B.PltIndex * Target->getPltEntrySize();
148 }
149 
150 template <class ELFT> void PltSection<ELFT>::finalize() {
151   this->Header.sh_size = Target->getPltZeroEntrySize() +
152                          Entries.size() * Target->getPltEntrySize();
153 }
154 
155 template <class ELFT>
156 RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
157     : OutputSectionBase<ELFT>(Name,
158                               IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
159                               llvm::ELF::SHF_ALLOC),
160       IsRela(IsRela) {
161   this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
162   this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
163 }
164 
165 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
166   const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
167   for (const DynamicReloc<ELFT> &Rel : Relocs) {
168     auto *P = reinterpret_cast<Elf_Rel *>(Buf);
169     Buf += EntrySize;
170 
171     const InputSection<ELFT> &C = Rel.C;
172     const Elf_Rel &RI = Rel.RI;
173     uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
174     const ObjectFile<ELFT> &File = *C.getFile();
175     SymbolBody *Body = File.getSymbolBody(SymIndex);
176     if (Body)
177       Body = Body->repl();
178 
179     uint32_t Type = RI.getType(Config->Mips64EL);
180     bool NeedsCopy = Body && Target->relocNeedsCopy(Type, *Body);
181     bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
182     bool CanBePreempted = canBePreempted(Body, NeedsGot);
183     bool LazyReloc = Body && Target->supportsLazyRelocations() &&
184                      Target->relocNeedsPlt(Type, *Body);
185 
186     if (CanBePreempted) {
187       if (NeedsGot)
188         P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
189                             LazyReloc ? Target->getPltReloc()
190                                       : Target->getGotReloc(),
191                             Config->Mips64EL);
192       else
193         P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
194                             NeedsCopy ? Target->getCopyReloc() : Type,
195                             Config->Mips64EL);
196     } else {
197       P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
198     }
199 
200     if (NeedsGot) {
201       if (LazyReloc)
202         P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
203       else
204         P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
205     } else if (NeedsCopy) {
206       P->r_offset = Out<ELFT>::Bss->getVA() +
207                     dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBSS;
208     } else {
209       P->r_offset = RI.r_offset + C.OutSec->getVA() + C.OutSecOff;
210     }
211 
212     uintX_t OrigAddend = 0;
213     if (IsRela && !NeedsGot)
214       OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
215 
216     uintX_t Addend;
217     if (NeedsCopy)
218       Addend = 0;
219     else if (CanBePreempted)
220       Addend = OrigAddend;
221     else if (Body)
222       Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend;
223     else if (IsRela)
224       Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI));
225     else
226       Addend = getLocalRelTarget(File, RI);
227 
228     if (IsRela)
229       static_cast<Elf_Rela *>(P)->r_addend = Addend;
230   }
231 }
232 
233 template <class ELFT> void RelocationSection<ELFT>::finalize() {
234   this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
235   this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
236 }
237 
238 template <class ELFT>
239 InterpSection<ELFT>::InterpSection()
240     : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
241                               llvm::ELF::SHF_ALLOC) {
242   this->Header.sh_size = Config->DynamicLinker.size() + 1;
243   this->Header.sh_addralign = 1;
244 }
245 
246 template <class ELFT>
247 void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
248   Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
249   *SHdr = Header;
250 }
251 
252 template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
253   memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
254 }
255 
256 template <class ELFT>
257 HashTableSection<ELFT>::HashTableSection()
258     : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
259                               llvm::ELF::SHF_ALLOC) {
260   this->Header.sh_entsize = sizeof(Elf_Word);
261   this->Header.sh_addralign = sizeof(Elf_Word);
262 }
263 
264 static uint32_t hashSysv(StringRef Name) {
265   uint32_t H = 0;
266   for (char C : Name) {
267     H = (H << 4) + C;
268     uint32_t G = H & 0xf0000000;
269     if (G)
270       H ^= G >> 24;
271     H &= ~G;
272   }
273   return H;
274 }
275 
276 template <class ELFT> void HashTableSection<ELFT>::finalize() {
277   this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
278 
279   unsigned NumEntries = 2;                 // nbucket and nchain.
280   NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
281 
282   // Create as many buckets as there are symbols.
283   // FIXME: This is simplistic. We can try to optimize it, but implementing
284   // support for SHT_GNU_HASH is probably even more profitable.
285   NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
286   this->Header.sh_size = NumEntries * sizeof(Elf_Word);
287 }
288 
289 template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
290   unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
291   auto *P = reinterpret_cast<Elf_Word *>(Buf);
292   *P++ = NumSymbols; // nbucket
293   *P++ = NumSymbols; // nchain
294 
295   Elf_Word *Buckets = P;
296   Elf_Word *Chains = P + NumSymbols;
297 
298   for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
299     StringRef Name = Body->getName();
300     unsigned I = Body->getDynamicSymbolTableIndex();
301     uint32_t Hash = hashSysv(Name) % NumSymbols;
302     Chains[I] = Buckets[Hash];
303     Buckets[Hash] = I;
304   }
305 }
306 
307 static uint32_t hashGnu(StringRef Name) {
308   uint32_t H = 5381;
309   for (uint8_t C : Name)
310     H = (H << 5) + H + C;
311   return H;
312 }
313 
314 template <class ELFT>
315 GnuHashTableSection<ELFT>::GnuHashTableSection()
316     : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH,
317                               llvm::ELF::SHF_ALLOC) {
318   this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
319   this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
320 }
321 
322 template <class ELFT>
323 unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
324   if (!NumHashed)
325     return 0;
326 
327   // These values are prime numbers which are not greater than 2^(N-1) + 1.
328   // In result, for any particular NumHashed we return a prime number
329   // which is not greater than NumHashed.
330   static const unsigned Primes[] = {
331       1,   1,    3,    3,    7,    13,    31,    61,    127,   251,
332       509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
333 
334   return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
335                                    array_lengthof(Primes) - 1)];
336 }
337 
338 // Bloom filter estimation: at least 8 bits for each hashed symbol.
339 // GNU Hash table requirement: it should be a power of 2,
340 //   the minimum value is 1, even for an empty table.
341 // Expected results for a 32-bit target:
342 //   calcMaskWords(0..4)   = 1
343 //   calcMaskWords(5..8)   = 2
344 //   calcMaskWords(9..16)  = 4
345 // For a 64-bit target:
346 //   calcMaskWords(0..8)   = 1
347 //   calcMaskWords(9..16)  = 2
348 //   calcMaskWords(17..32) = 4
349 template <class ELFT>
350 unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
351   if (!NumHashed)
352     return 1;
353   return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
354 }
355 
356 template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
357   unsigned NumHashed = HashedSymbols.size();
358   NBuckets = calcNBuckets(NumHashed);
359   MaskWords = calcMaskWords(NumHashed);
360   // Second hash shift estimation: just predefined values.
361   Shift2 = ELFT::Is64Bits ? 6 : 5;
362 
363   this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
364   this->Header.sh_size = sizeof(Elf_Word) * 4            // Header
365                          + sizeof(Elf_Off) * MaskWords   // Bloom Filter
366                          + sizeof(Elf_Word) * NBuckets   // Hash Buckets
367                          + sizeof(Elf_Word) * NumHashed; // Hash Values
368 }
369 
370 template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
371   writeHeader(Buf);
372   if (HashedSymbols.empty())
373     return;
374   writeBloomFilter(Buf);
375   writeHashTable(Buf);
376 }
377 
378 template <class ELFT>
379 void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
380   auto *P = reinterpret_cast<Elf_Word *>(Buf);
381   *P++ = NBuckets;
382   *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
383   *P++ = MaskWords;
384   *P++ = Shift2;
385   Buf = reinterpret_cast<uint8_t *>(P);
386 }
387 
388 template <class ELFT>
389 void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
390   unsigned C = sizeof(Elf_Off) * 8;
391 
392   auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
393   for (const HashedSymbolData &Item : HashedSymbols) {
394     size_t Pos = (Item.Hash / C) & (MaskWords - 1);
395     uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
396                 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
397     Masks[Pos] |= V;
398   }
399   Buf += sizeof(Elf_Off) * MaskWords;
400 }
401 
402 template <class ELFT>
403 void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
404   Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
405   Elf_Word *Values = Buckets + NBuckets;
406 
407   int PrevBucket = -1;
408   int I = 0;
409   for (const HashedSymbolData &Item : HashedSymbols) {
410     int Bucket = Item.Hash % NBuckets;
411     assert(PrevBucket <= Bucket);
412     if (Bucket != PrevBucket) {
413       Buckets[Bucket] = Item.Body->getDynamicSymbolTableIndex();
414       PrevBucket = Bucket;
415       if (I > 0)
416         Values[I - 1] |= 1;
417     }
418     Values[I] = Item.Hash & ~1;
419     ++I;
420   }
421   if (I > 0)
422     Values[I - 1] |= 1;
423 }
424 
425 static bool includeInGnuHashTable(SymbolBody *B) {
426   // Assume that includeInDynamicSymtab() is already checked.
427   return !B->isUndefined();
428 }
429 
430 template <class ELFT>
431 void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
432   std::vector<SymbolBody *> NotHashed;
433   NotHashed.reserve(Symbols.size());
434   HashedSymbols.reserve(Symbols.size());
435   for (SymbolBody *B : Symbols) {
436     if (includeInGnuHashTable(B))
437       HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
438     else
439       NotHashed.push_back(B);
440   }
441   if (HashedSymbols.empty())
442     return;
443 
444   unsigned NBuckets = calcNBuckets(HashedSymbols.size());
445   std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
446                    [&](const HashedSymbolData &L, const HashedSymbolData &R) {
447                      return L.Hash % NBuckets < R.Hash % NBuckets;
448                    });
449 
450   Symbols = std::move(NotHashed);
451   for (const HashedSymbolData &Item : HashedSymbols)
452     Symbols.push_back(Item.Body);
453 }
454 
455 template <class ELFT>
456 DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
457     : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
458                               llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
459       SymTab(SymTab) {
460   Elf_Shdr &Header = this->Header;
461   Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
462   Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
463 }
464 
465 template <class ELFT> void DynamicSection<ELFT>::finalize() {
466   if (this->Header.sh_size)
467     return; // Already finalized.
468 
469   Elf_Shdr &Header = this->Header;
470   Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
471 
472   unsigned NumEntries = 0;
473   if (Out<ELFT>::RelaDyn->hasRelocs()) {
474     ++NumEntries; // DT_RELA / DT_REL
475     ++NumEntries; // DT_RELASZ / DT_RELSZ
476     ++NumEntries; // DT_RELAENT / DT_RELENT
477   }
478   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
479     ++NumEntries; // DT_JMPREL
480     ++NumEntries; // DT_PLTRELSZ
481     ++NumEntries; // DT_PLTGOT
482     ++NumEntries; // DT_PLTREL
483   }
484 
485   ++NumEntries; // DT_SYMTAB
486   ++NumEntries; // DT_SYMENT
487   ++NumEntries; // DT_STRTAB
488   ++NumEntries; // DT_STRSZ
489   if (Out<ELFT>::GnuHashTab)
490     ++NumEntries; // DT_GNU_HASH
491   if (Out<ELFT>::HashTab)
492     ++NumEntries; // DT_HASH
493 
494   if (!Config->RPath.empty()) {
495     ++NumEntries; // DT_RUNPATH / DT_RPATH
496     Out<ELFT>::DynStrTab->add(Config->RPath);
497   }
498 
499   if (!Config->SoName.empty()) {
500     ++NumEntries; // DT_SONAME
501     Out<ELFT>::DynStrTab->add(Config->SoName);
502   }
503 
504   if (PreInitArraySec)
505     NumEntries += 2;
506   if (InitArraySec)
507     NumEntries += 2;
508   if (FiniArraySec)
509     NumEntries += 2;
510 
511   for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
512     if (!F->isNeeded())
513       continue;
514     Out<ELFT>::DynStrTab->add(F->getSoName());
515     ++NumEntries;
516   }
517 
518   if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
519     InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
520   if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
521     FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
522   if (InitSym)
523     ++NumEntries; // DT_INIT
524   if (FiniSym)
525     ++NumEntries; // DT_FINI
526 
527   if (Config->Bsymbolic)
528     DtFlags |= DF_SYMBOLIC;
529   if (Config->ZNodelete)
530     DtFlags1 |= DF_1_NODELETE;
531   if (Config->ZNow) {
532     DtFlags |= DF_BIND_NOW;
533     DtFlags1 |= DF_1_NOW;
534   }
535   if (Config->ZOrigin) {
536     DtFlags |= DF_ORIGIN;
537     DtFlags1 |= DF_1_ORIGIN;
538   }
539 
540   if (DtFlags)
541     ++NumEntries; // DT_FLAGS
542   if (DtFlags1)
543     ++NumEntries; // DT_FLAGS_1
544   ++NumEntries; // DT_NULL
545 
546   Header.sh_size = NumEntries * Header.sh_entsize;
547 }
548 
549 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
550   auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
551 
552   auto WritePtr = [&](int32_t Tag, uint64_t Val) {
553     P->d_tag = Tag;
554     P->d_un.d_ptr = Val;
555     ++P;
556   };
557 
558   auto WriteVal = [&](int32_t Tag, uint32_t Val) {
559     P->d_tag = Tag;
560     P->d_un.d_val = Val;
561     ++P;
562   };
563 
564   if (Out<ELFT>::RelaDyn->hasRelocs()) {
565     bool IsRela = Out<ELFT>::RelaDyn->isRela();
566     WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
567     WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
568     WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
569              IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
570   }
571   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
572     WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
573     WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
574     WritePtr(DT_PLTGOT, Out<ELFT>::GotPlt->getVA());
575     WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
576   }
577 
578   WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
579   WritePtr(DT_SYMENT, sizeof(Elf_Sym));
580   WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
581   WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
582   if (Out<ELFT>::GnuHashTab)
583     WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
584   if (Out<ELFT>::HashTab)
585     WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
586 
587   if (!Config->RPath.empty())
588 
589     // If --enable-new-dtags is set lld emits DT_RUNPATH
590     // instead of DT_RPATH. The two tags are functionally
591     // equivalent except for the following:
592     // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
593     // DT_RPATH is searched before.
594     // - DT_RUNPATH is used only to search for direct
595     // dependencies of the object it's contained in, while
596     // DT_RPATH is used for indirect dependencies as well.
597     WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
598              Out<ELFT>::DynStrTab->getOffset(Config->RPath));
599 
600   if (!Config->SoName.empty())
601     WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
602 
603   auto WriteArray = [&](int32_t T1, int32_t T2,
604                         const OutputSectionBase<ELFT> *Sec) {
605     if (!Sec)
606       return;
607     WritePtr(T1, Sec->getVA());
608     WriteVal(T2, Sec->getSize());
609   };
610   WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
611   WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
612   WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
613 
614   for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
615     if (F->isNeeded())
616       WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
617 
618   if (InitSym)
619     WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
620   if (FiniSym)
621     WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
622   if (DtFlags)
623     WriteVal(DT_FLAGS, DtFlags);
624   if (DtFlags1)
625     WriteVal(DT_FLAGS_1, DtFlags1);
626   WriteVal(DT_NULL, 0);
627 }
628 
629 template <class ELFT>
630 OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
631                                    uintX_t sh_flags)
632     : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
633 
634 template <class ELFT>
635 void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
636   Sections.push_back(C);
637   C->OutSec = this;
638   uint32_t Align = C->getAlign();
639   if (Align > this->Header.sh_addralign)
640     this->Header.sh_addralign = Align;
641 
642   uintX_t Off = this->Header.sh_size;
643   Off = RoundUpToAlignment(Off, Align);
644   C->OutSecOff = Off;
645   Off += C->getSize();
646   this->Header.sh_size = Off;
647 }
648 
649 template <class ELFT>
650 typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
651   switch (S.kind()) {
652   case SymbolBody::DefinedSyntheticKind: {
653     auto &D = cast<DefinedSynthetic<ELFT>>(S);
654     return D.Section.getVA() + D.Sym.st_value;
655   }
656   case SymbolBody::DefinedAbsoluteKind:
657     return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
658   case SymbolBody::DefinedRegularKind: {
659     const auto &DR = cast<DefinedRegular<ELFT>>(S);
660     InputSectionBase<ELFT> &SC = DR.Section;
661     if (DR.Sym.getType() == STT_TLS)
662       return SC.OutSec->getVA() + SC.getOffset(DR.Sym) -
663              Out<ELFT>::TlsInitImageVA;
664     return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
665   }
666   case SymbolBody::DefinedCommonKind:
667     return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
668   case SymbolBody::SharedKind: {
669     auto &SS = cast<SharedSymbol<ELFT>>(S);
670     if (SS.needsCopy())
671       return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
672     return 0;
673   }
674   case SymbolBody::UndefinedKind:
675     return 0;
676   case SymbolBody::LazyKind:
677     assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
678     return 0;
679   }
680   llvm_unreachable("Invalid symbol kind");
681 }
682 
683 // Returns a VA which a relocatin RI refers to. Used only for local symbols.
684 // For non-local symbols, use getSymVA instead.
685 template <class ELFT, bool IsRela>
686 typename ELFFile<ELFT>::uintX_t
687 lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
688                              const Elf_Rel_Impl<ELFT, IsRela> &RI) {
689   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
690   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
691 
692   uintX_t Addend = getAddend<ELFT>(RI);
693 
694   // PPC64 has a special relocation representing the TOC base pointer
695   // that does not have a corresponding symbol.
696   if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
697     return getPPC64TocBase() + Addend;
698 
699   const Elf_Sym *Sym =
700       File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
701 
702   if (!Sym)
703     error("Unsupported relocation without symbol");
704 
705   // According to the ELF spec reference to a local symbol from outside
706   // the group are not allowed. Unfortunately .eh_frame breaks that rule
707   // and must be treated specially. For now we just replace the symbol with
708   // 0.
709   InputSectionBase<ELFT> *Section = File.getSection(*Sym);
710   if (Section == &InputSection<ELFT>::Discarded)
711     return Addend;
712 
713   uintX_t VA = Section->OutSec->getVA();
714   if (isa<InputSection<ELFT>>(Section))
715     return VA + Section->getOffset(*Sym) + Addend;
716 
717   uintX_t Offset = Sym->st_value;
718   if (Sym->getType() == STT_SECTION) {
719     Offset += Addend;
720     Addend = 0;
721   }
722   return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
723          Addend;
724 }
725 
726 // Returns true if a symbol can be replaced at load-time by a symbol
727 // with the same name defined in other ELF executable or DSO.
728 bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
729   if (!Body)
730     return false;  // Body is a local symbol.
731   if (Body->isShared())
732     return true;
733 
734   if (Body->isUndefined()) {
735     if (!Body->isWeak())
736       return true;
737 
738     // This is an horrible corner case. Ideally we would like to say that any
739     // undefined symbol can be preempted so that the dynamic linker has a
740     // chance of finding it at runtime.
741     //
742     // The problem is that the code sequence used to test for weak undef
743     // functions looks like
744     // if (func) func()
745     // If the code is -fPIC the first reference is a load from the got and
746     // everything works.
747     // If the code is not -fPIC there is no reasonable way to solve it:
748     // * A relocation writing to the text segment will fail (it is ro).
749     // * A copy relocation doesn't work for functions.
750     // * The trick of using a plt entry as the address would fail here since
751     //   the plt entry would have a non zero address.
752     // Since we cannot do anything better, we just resolve the symbol to 0 and
753     // don't produce a dynamic relocation.
754     //
755     // As an extra hack, assume that if we are producing a shared library the
756     // user knows what he or she is doing and can handle a dynamic relocation.
757     return Config->Shared || NeedsGot;
758   }
759   if (!Config->Shared)
760     return false;
761   return Body->getVisibility() == STV_DEFAULT;
762 }
763 
764 template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
765   for (InputSection<ELFT> *C : Sections)
766     C->writeTo(Buf);
767 }
768 
769 template <class ELFT>
770 MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
771                                              uintX_t sh_flags)
772     : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
773 
774 template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
775   if (shouldTailMerge()) {
776     StringRef Data = Builder.data();
777     memcpy(Buf, Data.data(), Data.size());
778     return;
779   }
780   for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
781     StringRef Data = P.first;
782     memcpy(Buf + P.second, Data.data(), Data.size());
783   }
784 }
785 
786 static size_t findNull(StringRef S, size_t EntSize) {
787   // Optimize the common case.
788   if (EntSize == 1)
789     return S.find(0);
790 
791   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
792     const char *B = S.begin() + I;
793     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
794       return I;
795   }
796   return StringRef::npos;
797 }
798 
799 template <class ELFT>
800 void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
801   S->OutSec = this;
802   uint32_t Align = S->getAlign();
803   if (Align > this->Header.sh_addralign)
804     this->Header.sh_addralign = Align;
805 
806   ArrayRef<uint8_t> D = S->getSectionData();
807   StringRef Data((const char *)D.data(), D.size());
808   uintX_t EntSize = S->getSectionHdr()->sh_entsize;
809   uintX_t Offset = 0;
810 
811   if (this->Header.sh_flags & SHF_STRINGS) {
812     while (!Data.empty()) {
813       size_t End = findNull(Data, EntSize);
814       if (End == StringRef::npos)
815         error("String is not null terminated");
816       StringRef Entry = Data.substr(0, End + EntSize);
817       size_t OutputOffset = Builder.add(Entry);
818       if (shouldTailMerge())
819         OutputOffset = -1;
820       S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
821       uintX_t Size = End + EntSize;
822       Data = Data.substr(Size);
823       Offset += Size;
824     }
825   } else {
826     for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
827       StringRef Entry = Data.substr(I, EntSize);
828       size_t OutputOffset = Builder.add(Entry);
829       S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
830       Offset += EntSize;
831     }
832   }
833 }
834 
835 template <class ELFT>
836 unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
837   return Builder.getOffset(Val);
838 }
839 
840 template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
841   return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
842 }
843 
844 template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
845   if (shouldTailMerge())
846     Builder.finalize();
847   this->Header.sh_size = Builder.getSize();
848 }
849 
850 template <class ELFT>
851 StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
852     : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
853                               Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
854       Dynamic(Dynamic) {
855   this->Header.sh_addralign = 1;
856 }
857 
858 template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
859   StringRef Data = StrTabBuilder.data();
860   memcpy(Buf, Data.data(), Data.size());
861 }
862 
863 template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
864   if (!B.isUsedInRegularObj())
865     return false;
866 
867   // Don't include synthetic symbols like __init_array_start in every output.
868   if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
869     if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
870       return false;
871 
872   return true;
873 }
874 
875 bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
876   uint8_t V = B.getVisibility();
877   if (V != STV_DEFAULT && V != STV_PROTECTED)
878     return false;
879 
880   if (Config->ExportDynamic || Config->Shared)
881     return true;
882   return B.isUsedInDynamicReloc();
883 }
884 
885 template <class ELFT>
886 bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
887                                    StringRef SymName,
888                                    const typename ELFFile<ELFT>::Elf_Sym &Sym) {
889   if (Sym.getType() == STT_SECTION)
890     return false;
891 
892   // If sym references a section in a discarded group, don't keep it.
893   if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
894     return false;
895 
896   if (Config->DiscardNone)
897     return true;
898 
899   // ELF defines dynamic locals as symbols which name starts with ".L".
900   return !(Config->DiscardLocals && SymName.startswith(".L"));
901 }
902 
903 template <class ELFT>
904 SymbolTableSection<ELFT>::SymbolTableSection(
905     SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
906     : OutputSectionBase<ELFT>(
907           StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
908           StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
909           StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
910       Table(Table), StrTabSec(StrTabSec) {
911   typedef OutputSectionBase<ELFT> Base;
912   typename Base::Elf_Shdr &Header = this->Header;
913 
914   Header.sh_entsize = sizeof(Elf_Sym);
915   Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
916 }
917 
918 template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
919   if (this->Header.sh_size)
920     return; // Already finalized.
921 
922   this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
923   this->Header.sh_link = StrTabSec.SectionIndex;
924   this->Header.sh_info = NumLocals + 1;
925 
926   if (!StrTabSec.isDynamic()) {
927     std::stable_sort(Symbols.begin(), Symbols.end(),
928                      [](SymbolBody *L, SymbolBody *R) {
929                        return getSymbolBinding(L) == STB_LOCAL &&
930                               getSymbolBinding(R) != STB_LOCAL;
931                      });
932     return;
933   }
934   if (Out<ELFT>::GnuHashTab)
935     // NB: It also sorts Symbols to meet the GNU hash table requirements.
936     Out<ELFT>::GnuHashTab->addSymbols(Symbols);
937   size_t I = 0;
938   for (SymbolBody *B : Symbols)
939     B->setDynamicSymbolTableIndex(++I);
940 }
941 
942 template <class ELFT>
943 void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
944   StrTabSec.add(Name);
945   ++NumVisible;
946   ++NumLocals;
947 }
948 
949 template <class ELFT>
950 void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
951   StrTabSec.add(Body->getName());
952   Symbols.push_back(Body);
953   ++NumVisible;
954 }
955 
956 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
957   Buf += sizeof(Elf_Sym);
958 
959   // All symbols with STB_LOCAL binding precede the weak and global symbols.
960   // .dynsym only contains global symbols.
961   if (!Config->DiscardAll && !StrTabSec.isDynamic())
962     writeLocalSymbols(Buf);
963 
964   writeGlobalSymbols(Buf);
965 }
966 
967 template <class ELFT>
968 void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
969   // Iterate over all input object files to copy their local symbols
970   // to the output symbol table pointed by Buf.
971   for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
972     Elf_Sym_Range Syms = File->getLocalSymbols();
973     for (const Elf_Sym &Sym : Syms) {
974       ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
975       error(SymNameOrErr);
976       StringRef SymName = *SymNameOrErr;
977       if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
978         continue;
979 
980       auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
981       uintX_t VA = 0;
982       if (Sym.st_shndx == SHN_ABS) {
983         ESym->st_shndx = SHN_ABS;
984         VA = Sym.st_value;
985       } else {
986         InputSectionBase<ELFT> *Section = File->getSection(Sym);
987         if (!Section->isLive())
988           continue;
989         const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
990         ESym->st_shndx = OutSec->SectionIndex;
991         VA += OutSec->getVA() + Section->getOffset(Sym);
992       }
993       ESym->st_name = StrTabSec.getOffset(SymName);
994       ESym->st_size = Sym.st_size;
995       ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
996       ESym->st_value = VA;
997       Buf += sizeof(*ESym);
998     }
999   }
1000 }
1001 
1002 template <class ELFT>
1003 void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
1004   // Write the internal symbol table contents to the output symbol table
1005   // pointed by Buf.
1006   auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1007   for (SymbolBody *Body : Symbols) {
1008     const OutputSectionBase<ELFT> *OutSec = nullptr;
1009 
1010     switch (Body->kind()) {
1011     case SymbolBody::DefinedSyntheticKind:
1012       OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
1013       break;
1014     case SymbolBody::DefinedRegularKind: {
1015       auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
1016       if (!Sym->Section.isLive())
1017         continue;
1018       OutSec = Sym->Section.OutSec;
1019       break;
1020     }
1021     case SymbolBody::DefinedCommonKind:
1022       OutSec = Out<ELFT>::Bss;
1023       break;
1024     case SymbolBody::SharedKind: {
1025       if (cast<SharedSymbol<ELFT>>(Body)->needsCopy())
1026         OutSec = Out<ELFT>::Bss;
1027       break;
1028     }
1029     case SymbolBody::UndefinedKind:
1030     case SymbolBody::DefinedAbsoluteKind:
1031     case SymbolBody::LazyKind:
1032       break;
1033     }
1034 
1035     StringRef Name = Body->getName();
1036     ESym->st_name = StrTabSec.getOffset(Name);
1037 
1038     unsigned char Type = STT_NOTYPE;
1039     uintX_t Size = 0;
1040     if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1041       const Elf_Sym &InputSym = EBody->Sym;
1042       Type = InputSym.getType();
1043       Size = InputSym.st_size;
1044     }
1045 
1046     ESym->setBindingAndType(getSymbolBinding(Body), Type);
1047     ESym->st_size = Size;
1048     ESym->setVisibility(Body->getVisibility());
1049     ESym->st_value = getSymVA<ELFT>(*Body);
1050 
1051     if (isa<DefinedAbsolute<ELFT>>(Body))
1052       ESym->st_shndx = SHN_ABS;
1053     else if (OutSec)
1054       ESym->st_shndx = OutSec->SectionIndex;
1055 
1056     ++ESym;
1057   }
1058 }
1059 
1060 template <class ELFT>
1061 uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
1062   uint8_t Visibility = Body->getVisibility();
1063   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1064     return STB_LOCAL;
1065   if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1066     return EBody->Sym.getBinding();
1067   return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1068 }
1069 
1070 namespace lld {
1071 namespace elf2 {
1072 template class OutputSectionBase<ELF32LE>;
1073 template class OutputSectionBase<ELF32BE>;
1074 template class OutputSectionBase<ELF64LE>;
1075 template class OutputSectionBase<ELF64BE>;
1076 
1077 template class GotPltSection<ELF32LE>;
1078 template class GotPltSection<ELF32BE>;
1079 template class GotPltSection<ELF64LE>;
1080 template class GotPltSection<ELF64BE>;
1081 
1082 template class GotSection<ELF32LE>;
1083 template class GotSection<ELF32BE>;
1084 template class GotSection<ELF64LE>;
1085 template class GotSection<ELF64BE>;
1086 
1087 template class PltSection<ELF32LE>;
1088 template class PltSection<ELF32BE>;
1089 template class PltSection<ELF64LE>;
1090 template class PltSection<ELF64BE>;
1091 
1092 template class RelocationSection<ELF32LE>;
1093 template class RelocationSection<ELF32BE>;
1094 template class RelocationSection<ELF64LE>;
1095 template class RelocationSection<ELF64BE>;
1096 
1097 template class InterpSection<ELF32LE>;
1098 template class InterpSection<ELF32BE>;
1099 template class InterpSection<ELF64LE>;
1100 template class InterpSection<ELF64BE>;
1101 
1102 template class GnuHashTableSection<ELF32LE>;
1103 template class GnuHashTableSection<ELF32BE>;
1104 template class GnuHashTableSection<ELF64LE>;
1105 template class GnuHashTableSection<ELF64BE>;
1106 
1107 template class HashTableSection<ELF32LE>;
1108 template class HashTableSection<ELF32BE>;
1109 template class HashTableSection<ELF64LE>;
1110 template class HashTableSection<ELF64BE>;
1111 
1112 template class DynamicSection<ELF32LE>;
1113 template class DynamicSection<ELF32BE>;
1114 template class DynamicSection<ELF64LE>;
1115 template class DynamicSection<ELF64BE>;
1116 
1117 template class OutputSection<ELF32LE>;
1118 template class OutputSection<ELF32BE>;
1119 template class OutputSection<ELF64LE>;
1120 template class OutputSection<ELF64BE>;
1121 
1122 template class MergeOutputSection<ELF32LE>;
1123 template class MergeOutputSection<ELF32BE>;
1124 template class MergeOutputSection<ELF64LE>;
1125 template class MergeOutputSection<ELF64BE>;
1126 
1127 template class StringTableSection<ELF32LE>;
1128 template class StringTableSection<ELF32BE>;
1129 template class StringTableSection<ELF64LE>;
1130 template class StringTableSection<ELF64BE>;
1131 
1132 template class SymbolTableSection<ELF32LE>;
1133 template class SymbolTableSection<ELF32BE>;
1134 template class SymbolTableSection<ELF64LE>;
1135 template class SymbolTableSection<ELF64BE>;
1136 
1137 template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1138 template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1139 template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1140 template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
1141 
1142 template ELFFile<ELF32LE>::uintX_t
1143 getLocalRelTarget(const ObjectFile<ELF32LE> &,
1144                   const ELFFile<ELF32LE>::Elf_Rel &);
1145 template ELFFile<ELF32BE>::uintX_t
1146 getLocalRelTarget(const ObjectFile<ELF32BE> &,
1147                   const ELFFile<ELF32BE>::Elf_Rel &);
1148 template ELFFile<ELF64LE>::uintX_t
1149 getLocalRelTarget(const ObjectFile<ELF64LE> &,
1150                   const ELFFile<ELF64LE>::Elf_Rel &);
1151 template ELFFile<ELF64BE>::uintX_t
1152 getLocalRelTarget(const ObjectFile<ELF64BE> &,
1153                   const ELFFile<ELF64BE>::Elf_Rel &);
1154 
1155 template ELFFile<ELF32LE>::uintX_t
1156 getLocalRelTarget(const ObjectFile<ELF32LE> &,
1157                   const ELFFile<ELF32LE>::Elf_Rela &);
1158 template ELFFile<ELF32BE>::uintX_t
1159 getLocalRelTarget(const ObjectFile<ELF32BE> &,
1160                   const ELFFile<ELF32BE>::Elf_Rela &);
1161 template ELFFile<ELF64LE>::uintX_t
1162 getLocalRelTarget(const ObjectFile<ELF64LE> &,
1163                   const ELFFile<ELF64LE>::Elf_Rela &);
1164 template ELFFile<ELF64BE>::uintX_t
1165 getLocalRelTarget(const ObjectFile<ELF64BE> &,
1166                   const ELFFile<ELF64BE>::Elf_Rela &);
1167 
1168 template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1169 template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1170 template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1171 template bool includeInSymtab<ELF64BE>(const SymbolBody &);
1172 
1173 template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1174                                           StringRef,
1175                                           const ELFFile<ELF32LE>::Elf_Sym &);
1176 template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1177                                           StringRef,
1178                                           const ELFFile<ELF32BE>::Elf_Sym &);
1179 template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1180                                           StringRef,
1181                                           const ELFFile<ELF64LE>::Elf_Sym &);
1182 template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1183                                           StringRef,
1184                                           const ELFFile<ELF64BE>::Elf_Sym &);
1185 }
1186 }
1187