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   this->Header.sh_addralign = sizeof(uintX_t);
76 }
77 
78 template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
79   Sym->GotIndex = Entries.size();
80   Entries.push_back(Sym);
81 }
82 
83 template <class ELFT>
84 typename GotSection<ELFT>::uintX_t
85 GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
86   return this->getVA() + B.GotIndex * sizeof(uintX_t);
87 }
88 
89 template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
90   for (const SymbolBody *B : Entries) {
91     uint8_t *Entry = Buf;
92     Buf += sizeof(uintX_t);
93     if (canBePreempted(B, false))
94       continue; // The dynamic linker will take care of it.
95     uintX_t VA = getSymVA<ELFT>(*B);
96     write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
97   }
98 }
99 
100 template <class ELFT>
101 PltSection<ELFT>::PltSection()
102     : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
103                               llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
104   this->Header.sh_addralign = 16;
105 }
106 
107 template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
108   size_t Off = 0;
109   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   }
115   for (const SymbolBody *E : Entries) {
116     uint64_t Got = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
117                              : Out<ELFT>::Got->getEntryAddr(*E);
118     uint64_t Plt = this->getVA() + Off;
119     Target->writePltEntry(Buf + Off, Got, Plt, E->PltIndex);
120     Off += Target->getPltEntrySize();
121   }
122 }
123 
124 template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
125   Sym->PltIndex = Entries.size();
126   Entries.push_back(Sym);
127 }
128 
129 template <class ELFT>
130 typename PltSection<ELFT>::uintX_t
131 PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
132   return this->getVA() + Target->getPltZeroEntrySize() +
133          B.PltIndex * Target->getPltEntrySize();
134 }
135 
136 template <class ELFT> void PltSection<ELFT>::finalize() {
137   this->Header.sh_size = Target->getPltZeroEntrySize() +
138                          Entries.size() * Target->getPltEntrySize();
139 }
140 
141 template <class ELFT>
142 RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
143     : OutputSectionBase<ELFT>(Name,
144                               IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
145                               llvm::ELF::SHF_ALLOC),
146       IsRela(IsRela) {
147   this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
148   this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
149 }
150 
151 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
152   const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
153   for (const DynamicReloc<ELFT> &Rel : Relocs) {
154     auto *P = reinterpret_cast<Elf_Rel *>(Buf);
155     Buf += EntrySize;
156 
157     const InputSection<ELFT> &C = Rel.C;
158     const Elf_Rel &RI = Rel.RI;
159     uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
160     const ObjectFile<ELFT> &File = *C.getFile();
161     SymbolBody *Body = File.getSymbolBody(SymIndex);
162     if (Body)
163       Body = Body->repl();
164 
165     uint32_t Type = RI.getType(Config->Mips64EL);
166     bool NeedsCopy = Body && Target->relocNeedsCopy(Type, *Body);
167     bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
168     bool CanBePreempted = canBePreempted(Body, NeedsGot);
169     bool LazyReloc = Body && Target->supportsLazyRelocations() &&
170                      Target->relocNeedsPlt(Type, *Body);
171 
172     if (CanBePreempted) {
173       if (NeedsGot)
174         P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
175                             LazyReloc ? Target->getPltReloc()
176                                       : Target->getGotReloc(),
177                             Config->Mips64EL);
178       else
179         P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
180                             NeedsCopy ? Target->getCopyReloc() : Type,
181                             Config->Mips64EL);
182     } else {
183       P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
184     }
185 
186     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);
191     } else if (NeedsCopy) {
192       P->r_offset = Out<ELFT>::Bss->getVA() +
193                     dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBSS;
194     } else {
195       P->r_offset = RI.r_offset + C.OutSec->getVA() + C.OutSecOff;
196     }
197 
198     uintX_t OrigAddend = 0;
199     if (IsRela && !NeedsGot)
200       OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
201 
202     uintX_t Addend;
203     if (NeedsCopy)
204       Addend = 0;
205     else if (CanBePreempted)
206       Addend = OrigAddend;
207     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);
213 
214     if (IsRela)
215       static_cast<Elf_Rela *>(P)->r_addend = Addend;
216   }
217 }
218 
219 template <class ELFT> void RelocationSection<ELFT>::finalize() {
220   this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
221   this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
222 }
223 
224 template <class ELFT>
225 InterpSection<ELFT>::InterpSection()
226     : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
227                               llvm::ELF::SHF_ALLOC) {
228   this->Header.sh_size = Config->DynamicLinker.size() + 1;
229   this->Header.sh_addralign = 1;
230 }
231 
232 template <class ELFT>
233 void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
234   Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
235   *SHdr = Header;
236 }
237 
238 template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
239   memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
240 }
241 
242 template <class ELFT>
243 HashTableSection<ELFT>::HashTableSection()
244     : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
245                               llvm::ELF::SHF_ALLOC) {
246   this->Header.sh_entsize = sizeof(Elf_Word);
247   this->Header.sh_addralign = sizeof(Elf_Word);
248 }
249 
250 static uint32_t hashSysv(StringRef Name) {
251   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 
262 template <class ELFT> void HashTableSection<ELFT>::finalize() {
263   this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
264 
265   unsigned NumEntries = 2;                 // nbucket and nchain.
266   NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
267 
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.
271   NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
272   this->Header.sh_size = NumEntries * sizeof(Elf_Word);
273 }
274 
275 template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
276   unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
277   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 
284   for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
285     StringRef Name = Body->getName();
286     unsigned I = Body->getDynamicSymbolTableIndex();
287     uint32_t Hash = hashSysv(Name) % NumSymbols;
288     Chains[I] = Buckets[Hash];
289     Buckets[Hash] = I;
290   }
291 }
292 
293 static 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 
300 template <class ELFT>
301 GnuHashTableSection<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 
308 template <class ELFT>
309 unsigned 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
335 template <class ELFT>
336 unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
337   if (!NumHashed)
338     return 1;
339   return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
340 }
341 
342 template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
343   unsigned NumHashed = HashedSymbols.size();
344   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 
356 template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
357   writeHeader(Buf);
358   if (HashedSymbols.empty())
359     return;
360   writeBloomFilter(Buf);
361   writeHashTable(Buf);
362 }
363 
364 template <class ELFT>
365 void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
366   auto *P = reinterpret_cast<Elf_Word *>(Buf);
367   *P++ = NBuckets;
368   *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
369   *P++ = MaskWords;
370   *P++ = Shift2;
371   Buf = reinterpret_cast<uint8_t *>(P);
372 }
373 
374 template <class ELFT>
375 void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
376   unsigned C = sizeof(Elf_Off) * 8;
377 
378   auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
379   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));
383     Masks[Pos] |= V;
384   }
385   Buf += sizeof(Elf_Off) * MaskWords;
386 }
387 
388 template <class ELFT>
389 void 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;
395   for (const HashedSymbolData &Item : HashedSymbols) {
396     int Bucket = Item.Hash % NBuckets;
397     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     }
404     Values[I] = Item.Hash & ~1;
405     ++I;
406   }
407   if (I > 0)
408     Values[I - 1] |= 1;
409 }
410 
411 static bool includeInGnuHashTable(SymbolBody *B) {
412   // Assume that includeInDynamicSymtab() is already checked.
413   return !B->isUndefined();
414 }
415 
416 template <class ELFT>
417 void 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 
441 template <class ELFT>
442 DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
443     : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
444                               llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
445       SymTab(SymTab) {
446   Elf_Shdr &Header = this->Header;
447   Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
448   Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
449 }
450 
451 template <class ELFT> void DynamicSection<ELFT>::finalize() {
452   if (this->Header.sh_size)
453     return; // Already finalized.
454 
455   Elf_Shdr &Header = this->Header;
456   Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
457 
458   unsigned NumEntries = 0;
459   if (Out<ELFT>::RelaDyn->hasRelocs()) {
460     ++NumEntries; // DT_RELA / DT_REL
461     ++NumEntries; // DT_RELASZ / DT_RELSZ
462     ++NumEntries; // DT_RELAENT / DT_RELENT
463   }
464   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 
471   ++NumEntries; // DT_SYMTAB
472   ++NumEntries; // DT_SYMENT
473   ++NumEntries; // DT_STRTAB
474   ++NumEntries; // DT_STRSZ
475   if (Out<ELFT>::GnuHashTab)
476     ++NumEntries; // DT_GNU_HASH
477   if (Out<ELFT>::HashTab)
478     ++NumEntries; // DT_HASH
479 
480   if (!Config->RPath.empty()) {
481     ++NumEntries; // DT_RUNPATH / DT_RPATH
482     Out<ELFT>::DynStrTab->add(Config->RPath);
483   }
484 
485   if (!Config->SoName.empty()) {
486     ++NumEntries; // DT_SONAME
487     Out<ELFT>::DynStrTab->add(Config->SoName);
488   }
489 
490   if (PreInitArraySec)
491     NumEntries += 2;
492   if (InitArraySec)
493     NumEntries += 2;
494   if (FiniArraySec)
495     NumEntries += 2;
496 
497   for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
498     if (!F->isNeeded())
499       continue;
500     Out<ELFT>::DynStrTab->add(F->getSoName());
501     ++NumEntries;
502   }
503 
504   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);
508   if (InitSym)
509     ++NumEntries; // DT_INIT
510   if (FiniSym)
511     ++NumEntries; // DT_FINI
512 
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)
527     ++NumEntries; // DT_FLAGS
528   if (DtFlags1)
529     ++NumEntries; // DT_FLAGS_1
530   ++NumEntries; // DT_NULL
531 
532   Header.sh_size = NumEntries * Header.sh_entsize;
533 }
534 
535 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
536   auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
537 
538   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 
550   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());
554     WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
555              IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
556   }
557   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   }
563 
564   WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
565   WritePtr(DT_SYMENT, sizeof(Elf_Sym));
566   WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
567   WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
568   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());
572 
573   if (!Config->RPath.empty())
574 
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,
584              Out<ELFT>::DynStrTab->getOffset(Config->RPath));
585 
586   if (!Config->SoName.empty())
587     WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
588 
589   auto WriteArray = [&](int32_t T1, int32_t T2,
590                         const OutputSectionBase<ELFT> *Sec) {
591     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 
600   for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
601     if (F->isNeeded())
602       WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
603 
604   if (InitSym)
605     WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
606   if (FiniSym)
607     WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
608   if (DtFlags)
609     WriteVal(DT_FLAGS, DtFlags);
610   if (DtFlags1)
611     WriteVal(DT_FLAGS_1, DtFlags1);
612   WriteVal(DT_NULL, 0);
613 }
614 
615 template <class ELFT>
616 OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
617                                    uintX_t sh_flags)
618     : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
619 
620 template <class ELFT>
621 void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
622   Sections.push_back(C);
623   C->OutSec = this;
624   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);
630   C->OutSecOff = Off;
631   Off += C->getSize();
632   this->Header.sh_size = Off;
633 }
634 
635 template <class ELFT>
636 typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
637   switch (S.kind()) {
638   case SymbolBody::DefinedSyntheticKind: {
639     auto &D = cast<DefinedSynthetic<ELFT>>(S);
640     return D.Section.getVA() + D.Sym.st_value;
641   }
642   case SymbolBody::DefinedAbsoluteKind:
643     return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
644   case SymbolBody::DefinedRegularKind: {
645     const auto &DR = cast<DefinedRegular<ELFT>>(S);
646     InputSectionBase<ELFT> &SC = DR.Section;
647     if (DR.Sym.getType() == STT_TLS)
648       return SC.OutSec->getVA() + SC.getOffset(DR.Sym) -
649              Out<ELFT>::TlsInitImageVA;
650     return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
651   }
652   case SymbolBody::DefinedCommonKind:
653     return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
654   case SymbolBody::SharedKind: {
655     auto &SS = cast<SharedSymbol<ELFT>>(S);
656     if (SS.needsCopy())
657       return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
658     return 0;
659   }
660   case SymbolBody::UndefinedKind:
661     return 0;
662   case SymbolBody::LazyKind:
663     assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
664     return 0;
665   }
666   llvm_unreachable("Invalid symbol kind");
667 }
668 
669 // Returns a VA which a relocatin RI refers to. Used only for local symbols.
670 // For non-local symbols, use getSymVA instead.
671 template <class ELFT, bool IsRela>
672 typename ELFFile<ELFT>::uintX_t
673 lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
674                              const Elf_Rel_Impl<ELFT, IsRela> &RI) {
675   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
676   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
677 
678   uintX_t Addend = getAddend<ELFT>(RI);
679 
680   // PPC64 has a special relocation representing the TOC base pointer
681   // that does not have a corresponding symbol.
682   if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
683     return getPPC64TocBase() + Addend;
684 
685   const Elf_Sym *Sym =
686       File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
687 
688   if (!Sym)
689     error("Unsupported relocation without symbol");
690 
691   // According to the ELF spec reference to a local symbol from outside
692   // the group are not allowed. Unfortunately .eh_frame breaks that rule
693   // and must be treated specially. For now we just replace the symbol with
694   // 0.
695   InputSectionBase<ELFT> *Section = File.getSection(*Sym);
696   if (Section == &InputSection<ELFT>::Discarded)
697     return Addend;
698 
699   uintX_t VA = Section->OutSec->getVA();
700   if (isa<InputSection<ELFT>>(Section))
701     return VA + Section->getOffset(*Sym) + Addend;
702 
703   uintX_t Offset = Sym->st_value;
704   if (Sym->getType() == STT_SECTION) {
705     Offset += Addend;
706     Addend = 0;
707   }
708   return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
709          Addend;
710 }
711 
712 // Returns true if a symbol can be replaced at load-time by a symbol
713 // with the same name defined in other ELF executable or DSO.
714 bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
715   if (!Body)
716     return false;  // Body is a local symbol.
717   if (Body->isShared())
718     return true;
719 
720   if (Body->isUndefined()) {
721     if (!Body->isWeak())
722       return true;
723 
724     // This is an horrible corner case. Ideally we would like to say that any
725     // undefined symbol can be preempted so that the dynamic linker has a
726     // chance of finding it at runtime.
727     //
728     // The problem is that the code sequence used to test for weak undef
729     // functions looks like
730     // if (func) func()
731     // If the code is -fPIC the first reference is a load from the got and
732     // everything works.
733     // If the code is not -fPIC there is no reasonable way to solve it:
734     // * A relocation writing to the text segment will fail (it is ro).
735     // * A copy relocation doesn't work for functions.
736     // * The trick of using a plt entry as the address would fail here since
737     //   the plt entry would have a non zero address.
738     // Since we cannot do anything better, we just resolve the symbol to 0 and
739     // don't produce a dynamic relocation.
740     //
741     // As an extra hack, assume that if we are producing a shared library the
742     // user knows what he or she is doing and can handle a dynamic relocation.
743     return Config->Shared || NeedsGot;
744   }
745   if (!Config->Shared)
746     return false;
747   return Body->getVisibility() == STV_DEFAULT;
748 }
749 
750 template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
751   for (InputSection<ELFT> *C : Sections)
752     C->writeTo(Buf);
753 }
754 
755 template <class ELFT>
756 MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
757                                              uintX_t sh_flags)
758     : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
759 
760 template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
761   if (shouldTailMerge()) {
762     StringRef Data = Builder.data();
763     memcpy(Buf, Data.data(), Data.size());
764     return;
765   }
766   for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
767     StringRef Data = P.first;
768     memcpy(Buf + P.second, Data.data(), Data.size());
769   }
770 }
771 
772 static size_t findNull(StringRef S, size_t EntSize) {
773   // Optimize the common case.
774   if (EntSize == 1)
775     return S.find(0);
776 
777   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
778     const char *B = S.begin() + I;
779     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
780       return I;
781   }
782   return StringRef::npos;
783 }
784 
785 template <class ELFT>
786 void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
787   S->OutSec = this;
788   uint32_t Align = S->getAlign();
789   if (Align > this->Header.sh_addralign)
790     this->Header.sh_addralign = Align;
791 
792   ArrayRef<uint8_t> D = S->getSectionData();
793   StringRef Data((const char *)D.data(), D.size());
794   uintX_t EntSize = S->getSectionHdr()->sh_entsize;
795   uintX_t Offset = 0;
796 
797   if (this->Header.sh_flags & SHF_STRINGS) {
798     while (!Data.empty()) {
799       size_t End = findNull(Data, EntSize);
800       if (End == StringRef::npos)
801         error("String is not null terminated");
802       StringRef Entry = Data.substr(0, End + EntSize);
803       size_t OutputOffset = Builder.add(Entry);
804       if (shouldTailMerge())
805         OutputOffset = -1;
806       S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
807       uintX_t Size = End + EntSize;
808       Data = Data.substr(Size);
809       Offset += Size;
810     }
811   } else {
812     for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
813       StringRef Entry = Data.substr(I, EntSize);
814       size_t OutputOffset = Builder.add(Entry);
815       S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
816       Offset += EntSize;
817     }
818   }
819 }
820 
821 template <class ELFT>
822 unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
823   return Builder.getOffset(Val);
824 }
825 
826 template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
827   return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
828 }
829 
830 template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
831   if (shouldTailMerge())
832     Builder.finalize();
833   this->Header.sh_size = Builder.getSize();
834 }
835 
836 template <class ELFT>
837 StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
838     : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
839                               Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
840       Dynamic(Dynamic) {
841   this->Header.sh_addralign = 1;
842 }
843 
844 template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
845   StringRef Data = StrTabBuilder.data();
846   memcpy(Buf, Data.data(), Data.size());
847 }
848 
849 template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
850   if (!B.isUsedInRegularObj())
851     return false;
852 
853   // Don't include synthetic symbols like __init_array_start in every output.
854   if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
855     if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
856       return false;
857 
858   return true;
859 }
860 
861 bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
862   uint8_t V = B.getVisibility();
863   if (V != STV_DEFAULT && V != STV_PROTECTED)
864     return false;
865 
866   if (Config->ExportDynamic || Config->Shared)
867     return true;
868   return B.isUsedInDynamicReloc();
869 }
870 
871 template <class ELFT>
872 bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
873                                    StringRef SymName,
874                                    const typename ELFFile<ELFT>::Elf_Sym &Sym) {
875   if (Sym.getType() == STT_SECTION)
876     return false;
877 
878   // If sym references a section in a discarded group, don't keep it.
879   if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
880     return false;
881 
882   if (Config->DiscardNone)
883     return true;
884 
885   // ELF defines dynamic locals as symbols which name starts with ".L".
886   return !(Config->DiscardLocals && SymName.startswith(".L"));
887 }
888 
889 template <class ELFT>
890 SymbolTableSection<ELFT>::SymbolTableSection(
891     SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
892     : OutputSectionBase<ELFT>(
893           StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
894           StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
895           StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
896       Table(Table), StrTabSec(StrTabSec) {
897   typedef OutputSectionBase<ELFT> Base;
898   typename Base::Elf_Shdr &Header = this->Header;
899 
900   Header.sh_entsize = sizeof(Elf_Sym);
901   Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
902 }
903 
904 template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
905   if (this->Header.sh_size)
906     return; // Already finalized.
907 
908   this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
909   this->Header.sh_link = StrTabSec.SectionIndex;
910   this->Header.sh_info = NumLocals + 1;
911 
912   if (!StrTabSec.isDynamic()) {
913     std::stable_sort(Symbols.begin(), Symbols.end(),
914                      [](SymbolBody *L, SymbolBody *R) {
915                        return getSymbolBinding(L) == STB_LOCAL &&
916                               getSymbolBinding(R) != STB_LOCAL;
917                      });
918     return;
919   }
920   if (Out<ELFT>::GnuHashTab)
921     // NB: It also sorts Symbols to meet the GNU hash table requirements.
922     Out<ELFT>::GnuHashTab->addSymbols(Symbols);
923   size_t I = 0;
924   for (SymbolBody *B : Symbols)
925     B->setDynamicSymbolTableIndex(++I);
926 }
927 
928 template <class ELFT>
929 void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
930   StrTabSec.add(Name);
931   ++NumVisible;
932   ++NumLocals;
933 }
934 
935 template <class ELFT>
936 void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
937   StrTabSec.add(Body->getName());
938   Symbols.push_back(Body);
939   ++NumVisible;
940 }
941 
942 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
943   Buf += sizeof(Elf_Sym);
944 
945   // All symbols with STB_LOCAL binding precede the weak and global symbols.
946   // .dynsym only contains global symbols.
947   if (!Config->DiscardAll && !StrTabSec.isDynamic())
948     writeLocalSymbols(Buf);
949 
950   writeGlobalSymbols(Buf);
951 }
952 
953 template <class ELFT>
954 void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
955   // Iterate over all input object files to copy their local symbols
956   // to the output symbol table pointed by Buf.
957   for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
958     Elf_Sym_Range Syms = File->getLocalSymbols();
959     for (const Elf_Sym &Sym : Syms) {
960       ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
961       error(SymNameOrErr);
962       StringRef SymName = *SymNameOrErr;
963       if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
964         continue;
965 
966       auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
967       uintX_t VA = 0;
968       if (Sym.st_shndx == SHN_ABS) {
969         ESym->st_shndx = SHN_ABS;
970         VA = Sym.st_value;
971       } else {
972         InputSectionBase<ELFT> *Section = File->getSection(Sym);
973         if (!Section->isLive())
974           continue;
975         const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
976         ESym->st_shndx = OutSec->SectionIndex;
977         VA += OutSec->getVA() + Section->getOffset(Sym);
978       }
979       ESym->st_name = StrTabSec.getOffset(SymName);
980       ESym->st_size = Sym.st_size;
981       ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
982       ESym->st_value = VA;
983       Buf += sizeof(*ESym);
984     }
985   }
986 }
987 
988 template <class ELFT>
989 void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
990   // Write the internal symbol table contents to the output symbol table
991   // pointed by Buf.
992   auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
993   for (SymbolBody *Body : Symbols) {
994     const OutputSectionBase<ELFT> *OutSec = nullptr;
995 
996     switch (Body->kind()) {
997     case SymbolBody::DefinedSyntheticKind:
998       OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
999       break;
1000     case SymbolBody::DefinedRegularKind: {
1001       auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
1002       if (!Sym->Section.isLive())
1003         continue;
1004       OutSec = Sym->Section.OutSec;
1005       break;
1006     }
1007     case SymbolBody::DefinedCommonKind:
1008       OutSec = Out<ELFT>::Bss;
1009       break;
1010     case SymbolBody::SharedKind: {
1011       if (cast<SharedSymbol<ELFT>>(Body)->needsCopy())
1012         OutSec = Out<ELFT>::Bss;
1013       break;
1014     }
1015     case SymbolBody::UndefinedKind:
1016     case SymbolBody::DefinedAbsoluteKind:
1017     case SymbolBody::LazyKind:
1018       break;
1019     }
1020 
1021     StringRef Name = Body->getName();
1022     ESym->st_name = StrTabSec.getOffset(Name);
1023 
1024     unsigned char Type = STT_NOTYPE;
1025     uintX_t Size = 0;
1026     if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1027       const Elf_Sym &InputSym = EBody->Sym;
1028       Type = InputSym.getType();
1029       Size = InputSym.st_size;
1030     }
1031 
1032     ESym->setBindingAndType(getSymbolBinding(Body), Type);
1033     ESym->st_size = Size;
1034     ESym->setVisibility(Body->getVisibility());
1035     ESym->st_value = getSymVA<ELFT>(*Body);
1036 
1037     if (isa<DefinedAbsolute<ELFT>>(Body))
1038       ESym->st_shndx = SHN_ABS;
1039     else if (OutSec)
1040       ESym->st_shndx = OutSec->SectionIndex;
1041 
1042     ++ESym;
1043   }
1044 }
1045 
1046 template <class ELFT>
1047 uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
1048   uint8_t Visibility = Body->getVisibility();
1049   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1050     return STB_LOCAL;
1051   if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1052     return EBody->Sym.getBinding();
1053   return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1054 }
1055 
1056 namespace lld {
1057 namespace elf2 {
1058 template class OutputSectionBase<ELF32LE>;
1059 template class OutputSectionBase<ELF32BE>;
1060 template class OutputSectionBase<ELF64LE>;
1061 template class OutputSectionBase<ELF64BE>;
1062 
1063 template class GotPltSection<ELF32LE>;
1064 template class GotPltSection<ELF32BE>;
1065 template class GotPltSection<ELF64LE>;
1066 template class GotPltSection<ELF64BE>;
1067 
1068 template class GotSection<ELF32LE>;
1069 template class GotSection<ELF32BE>;
1070 template class GotSection<ELF64LE>;
1071 template class GotSection<ELF64BE>;
1072 
1073 template class PltSection<ELF32LE>;
1074 template class PltSection<ELF32BE>;
1075 template class PltSection<ELF64LE>;
1076 template class PltSection<ELF64BE>;
1077 
1078 template class RelocationSection<ELF32LE>;
1079 template class RelocationSection<ELF32BE>;
1080 template class RelocationSection<ELF64LE>;
1081 template class RelocationSection<ELF64BE>;
1082 
1083 template class InterpSection<ELF32LE>;
1084 template class InterpSection<ELF32BE>;
1085 template class InterpSection<ELF64LE>;
1086 template class InterpSection<ELF64BE>;
1087 
1088 template class GnuHashTableSection<ELF32LE>;
1089 template class GnuHashTableSection<ELF32BE>;
1090 template class GnuHashTableSection<ELF64LE>;
1091 template class GnuHashTableSection<ELF64BE>;
1092 
1093 template class HashTableSection<ELF32LE>;
1094 template class HashTableSection<ELF32BE>;
1095 template class HashTableSection<ELF64LE>;
1096 template class HashTableSection<ELF64BE>;
1097 
1098 template class DynamicSection<ELF32LE>;
1099 template class DynamicSection<ELF32BE>;
1100 template class DynamicSection<ELF64LE>;
1101 template class DynamicSection<ELF64BE>;
1102 
1103 template class OutputSection<ELF32LE>;
1104 template class OutputSection<ELF32BE>;
1105 template class OutputSection<ELF64LE>;
1106 template class OutputSection<ELF64BE>;
1107 
1108 template class MergeOutputSection<ELF32LE>;
1109 template class MergeOutputSection<ELF32BE>;
1110 template class MergeOutputSection<ELF64LE>;
1111 template class MergeOutputSection<ELF64BE>;
1112 
1113 template class StringTableSection<ELF32LE>;
1114 template class StringTableSection<ELF32BE>;
1115 template class StringTableSection<ELF64LE>;
1116 template class StringTableSection<ELF64BE>;
1117 
1118 template class SymbolTableSection<ELF32LE>;
1119 template class SymbolTableSection<ELF32BE>;
1120 template class SymbolTableSection<ELF64LE>;
1121 template class SymbolTableSection<ELF64BE>;
1122 
1123 template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1124 template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1125 template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1126 template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
1127 
1128 template ELFFile<ELF32LE>::uintX_t
1129 getLocalRelTarget(const ObjectFile<ELF32LE> &,
1130                   const ELFFile<ELF32LE>::Elf_Rel &);
1131 template ELFFile<ELF32BE>::uintX_t
1132 getLocalRelTarget(const ObjectFile<ELF32BE> &,
1133                   const ELFFile<ELF32BE>::Elf_Rel &);
1134 template ELFFile<ELF64LE>::uintX_t
1135 getLocalRelTarget(const ObjectFile<ELF64LE> &,
1136                   const ELFFile<ELF64LE>::Elf_Rel &);
1137 template ELFFile<ELF64BE>::uintX_t
1138 getLocalRelTarget(const ObjectFile<ELF64BE> &,
1139                   const ELFFile<ELF64BE>::Elf_Rel &);
1140 
1141 template ELFFile<ELF32LE>::uintX_t
1142 getLocalRelTarget(const ObjectFile<ELF32LE> &,
1143                   const ELFFile<ELF32LE>::Elf_Rela &);
1144 template ELFFile<ELF32BE>::uintX_t
1145 getLocalRelTarget(const ObjectFile<ELF32BE> &,
1146                   const ELFFile<ELF32BE>::Elf_Rela &);
1147 template ELFFile<ELF64LE>::uintX_t
1148 getLocalRelTarget(const ObjectFile<ELF64LE> &,
1149                   const ELFFile<ELF64LE>::Elf_Rela &);
1150 template ELFFile<ELF64BE>::uintX_t
1151 getLocalRelTarget(const ObjectFile<ELF64BE> &,
1152                   const ELFFile<ELF64BE>::Elf_Rela &);
1153 
1154 template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1155 template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1156 template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1157 template bool includeInSymtab<ELF64BE>(const SymbolBody &);
1158 
1159 template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1160                                           StringRef,
1161                                           const ELFFile<ELF32LE>::Elf_Sym &);
1162 template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1163                                           StringRef,
1164                                           const ELFFile<ELF32BE>::Elf_Sym &);
1165 template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1166                                           StringRef,
1167                                           const ELFFile<ELF64LE>::Elf_Sym &);
1168 template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1169                                           StringRef,
1170                                           const ELFFile<ELF64BE>::Elf_Sym &);
1171 }
1172 }
1173