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   ArrayRef<SymbolBody *> A = Out<ELFT>::DynSymTab->getSymbols();
344   unsigned NumHashed = std::count_if(A.begin(), A.end(), includeInGnuHashTable);
345   NBuckets = calcNBuckets(NumHashed);
346   MaskWords = calcMaskWords(NumHashed);
347   // Second hash shift estimation: just predefined values.
348   Shift2 = ELFT::Is64Bits ? 6 : 5;
349 
350   this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
351   this->Header.sh_size = sizeof(Elf_Word) * 4            // Header
352                          + sizeof(Elf_Off) * MaskWords   // Bloom Filter
353                          + sizeof(Elf_Word) * NBuckets   // Hash Buckets
354                          + sizeof(Elf_Word) * NumHashed; // Hash Values
355 }
356 
357 template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
358   writeHeader(Buf);
359   if (HashedSymbols.empty())
360     return;
361   writeBloomFilter(Buf);
362   writeHashTable(Buf);
363 }
364 
365 template <class ELFT>
366 void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
367   auto *P = reinterpret_cast<Elf_Word *>(Buf);
368   *P++ = NBuckets;
369   *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
370   *P++ = MaskWords;
371   *P++ = Shift2;
372   Buf = reinterpret_cast<uint8_t *>(P);
373 }
374 
375 template <class ELFT>
376 void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
377   unsigned C = sizeof(Elf_Off) * 8;
378 
379   auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
380   for (const HashedSymbolData &Item : HashedSymbols) {
381     size_t Pos = (Item.Hash / C) & (MaskWords - 1);
382     uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
383                 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
384     Masks[Pos] |= V;
385   }
386   Buf += sizeof(Elf_Off) * MaskWords;
387 }
388 
389 template <class ELFT>
390 void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
391   Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
392   Elf_Word *Values = Buckets + NBuckets;
393 
394   int PrevBucket = -1;
395   int I = 0;
396   for (const HashedSymbolData &Item : HashedSymbols) {
397     int Bucket = Item.Hash % NBuckets;
398     assert(PrevBucket <= Bucket);
399     if (Bucket != PrevBucket) {
400       Buckets[Bucket] = Item.Body->getDynamicSymbolTableIndex();
401       PrevBucket = Bucket;
402       if (I > 0)
403         Values[I - 1] |= 1;
404     }
405     Values[I] = Item.Hash & ~1;
406     ++I;
407   }
408   if (I > 0)
409     Values[I - 1] |= 1;
410 }
411 
412 template <class ELFT>
413 void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
414   std::vector<SymbolBody *> NotHashed;
415   NotHashed.reserve(Symbols.size());
416   HashedSymbols.reserve(Symbols.size());
417   for (SymbolBody *B : Symbols) {
418     if (includeInGnuHashTable(B))
419       HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
420     else
421       NotHashed.push_back(B);
422   }
423   if (HashedSymbols.empty())
424     return;
425 
426   unsigned NBuckets = calcNBuckets(HashedSymbols.size());
427   std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
428                    [&](const HashedSymbolData &L, const HashedSymbolData &R) {
429                      return L.Hash % NBuckets < R.Hash % NBuckets;
430                    });
431 
432   Symbols = std::move(NotHashed);
433   for (const HashedSymbolData &Item : HashedSymbols)
434     Symbols.push_back(Item.Body);
435 }
436 
437 template <class ELFT>
438 DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
439     : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
440                               llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
441       SymTab(SymTab) {
442   Elf_Shdr &Header = this->Header;
443   Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
444   Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
445 }
446 
447 template <class ELFT> void DynamicSection<ELFT>::finalize() {
448   if (this->Header.sh_size)
449     return; // Already finalized.
450 
451   Elf_Shdr &Header = this->Header;
452   Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
453 
454   unsigned NumEntries = 0;
455   if (Out<ELFT>::RelaDyn->hasRelocs()) {
456     ++NumEntries; // DT_RELA / DT_REL
457     ++NumEntries; // DT_RELASZ / DT_RELSZ
458     ++NumEntries; // DT_RELAENT / DT_RELENT
459   }
460   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
461     ++NumEntries; // DT_JMPREL
462     ++NumEntries; // DT_PLTRELSZ
463     ++NumEntries; // DT_PLTGOT
464     ++NumEntries; // DT_PLTREL
465   }
466 
467   ++NumEntries; // DT_SYMTAB
468   ++NumEntries; // DT_SYMENT
469   ++NumEntries; // DT_STRTAB
470   ++NumEntries; // DT_STRSZ
471   if (Out<ELFT>::GnuHashTab)
472     ++NumEntries; // DT_GNU_HASH
473   if (Out<ELFT>::HashTab)
474     ++NumEntries; // DT_HASH
475 
476   if (!Config->RPath.empty()) {
477     ++NumEntries; // DT_RUNPATH / DT_RPATH
478     Out<ELFT>::DynStrTab->add(Config->RPath);
479   }
480 
481   if (!Config->SoName.empty()) {
482     ++NumEntries; // DT_SONAME
483     Out<ELFT>::DynStrTab->add(Config->SoName);
484   }
485 
486   if (PreInitArraySec)
487     NumEntries += 2;
488   if (InitArraySec)
489     NumEntries += 2;
490   if (FiniArraySec)
491     NumEntries += 2;
492 
493   for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
494     if (!F->isNeeded())
495       continue;
496     Out<ELFT>::DynStrTab->add(F->getSoName());
497     ++NumEntries;
498   }
499 
500   if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
501     InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
502   if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
503     FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
504   if (InitSym)
505     ++NumEntries; // DT_INIT
506   if (FiniSym)
507     ++NumEntries; // DT_FINI
508 
509   if (Config->Bsymbolic)
510     DtFlags |= DF_SYMBOLIC;
511   if (Config->ZNodelete)
512     DtFlags1 |= DF_1_NODELETE;
513   if (Config->ZNow) {
514     DtFlags |= DF_BIND_NOW;
515     DtFlags1 |= DF_1_NOW;
516   }
517   if (Config->ZOrigin) {
518     DtFlags |= DF_ORIGIN;
519     DtFlags1 |= DF_1_ORIGIN;
520   }
521 
522   if (DtFlags)
523     ++NumEntries; // DT_FLAGS
524   if (DtFlags1)
525     ++NumEntries; // DT_FLAGS_1
526   ++NumEntries; // DT_NULL
527 
528   Header.sh_size = NumEntries * Header.sh_entsize;
529 }
530 
531 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
532   auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
533 
534   auto WritePtr = [&](int32_t Tag, uint64_t Val) {
535     P->d_tag = Tag;
536     P->d_un.d_ptr = Val;
537     ++P;
538   };
539 
540   auto WriteVal = [&](int32_t Tag, uint32_t Val) {
541     P->d_tag = Tag;
542     P->d_un.d_val = Val;
543     ++P;
544   };
545 
546   if (Out<ELFT>::RelaDyn->hasRelocs()) {
547     bool IsRela = Out<ELFT>::RelaDyn->isRela();
548     WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
549     WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
550     WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
551              IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
552   }
553   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
554     WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
555     WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
556     WritePtr(DT_PLTGOT, Out<ELFT>::GotPlt->getVA());
557     WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
558   }
559 
560   WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
561   WritePtr(DT_SYMENT, sizeof(Elf_Sym));
562   WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
563   WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
564   if (Out<ELFT>::GnuHashTab)
565     WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
566   if (Out<ELFT>::HashTab)
567     WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
568 
569   if (!Config->RPath.empty())
570 
571     // If --enable-new-dtags is set lld emits DT_RUNPATH
572     // instead of DT_RPATH. The two tags are functionally
573     // equivalent except for the following:
574     // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
575     // DT_RPATH is searched before.
576     // - DT_RUNPATH is used only to search for direct
577     // dependencies of the object it's contained in, while
578     // DT_RPATH is used for indirect dependencies as well.
579     WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
580              Out<ELFT>::DynStrTab->getOffset(Config->RPath));
581 
582   if (!Config->SoName.empty())
583     WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
584 
585   auto WriteArray = [&](int32_t T1, int32_t T2,
586                         const OutputSectionBase<ELFT> *Sec) {
587     if (!Sec)
588       return;
589     WritePtr(T1, Sec->getVA());
590     WriteVal(T2, Sec->getSize());
591   };
592   WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
593   WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
594   WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
595 
596   for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
597     if (F->isNeeded())
598       WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
599 
600   if (InitSym)
601     WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
602   if (FiniSym)
603     WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
604   if (DtFlags)
605     WriteVal(DT_FLAGS, DtFlags);
606   if (DtFlags1)
607     WriteVal(DT_FLAGS_1, DtFlags1);
608   WriteVal(DT_NULL, 0);
609 }
610 
611 template <class ELFT>
612 OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
613                                    uintX_t sh_flags)
614     : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
615 
616 template <class ELFT>
617 void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
618   Sections.push_back(C);
619   C->OutSec = this;
620   uint32_t Align = C->getAlign();
621   if (Align > this->Header.sh_addralign)
622     this->Header.sh_addralign = Align;
623 
624   uintX_t Off = this->Header.sh_size;
625   Off = RoundUpToAlignment(Off, Align);
626   C->OutSecOff = Off;
627   Off += C->getSize();
628   this->Header.sh_size = Off;
629 }
630 
631 template <class ELFT>
632 typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
633   switch (S.kind()) {
634   case SymbolBody::DefinedSyntheticKind: {
635     auto &D = cast<DefinedSynthetic<ELFT>>(S);
636     return D.Section.getVA() + D.Sym.st_value;
637   }
638   case SymbolBody::DefinedAbsoluteKind:
639     return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
640   case SymbolBody::DefinedRegularKind: {
641     const auto &DR = cast<DefinedRegular<ELFT>>(S);
642     InputSectionBase<ELFT> &SC = DR.Section;
643     return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
644   }
645   case SymbolBody::DefinedCommonKind:
646     return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
647   case SymbolBody::SharedKind: {
648     auto &SS = cast<SharedSymbol<ELFT>>(S);
649     if (SS.NeedsCopy)
650       return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
651     return 0;
652   }
653   case SymbolBody::UndefinedKind:
654     return 0;
655   case SymbolBody::LazyKind:
656     assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
657     return 0;
658   }
659   llvm_unreachable("Invalid symbol kind");
660 }
661 
662 // Returns a VA which a relocatin RI refers to. Used only for local symbols.
663 // For non-local symbols, use getSymVA instead.
664 template <class ELFT, bool IsRela>
665 typename ELFFile<ELFT>::uintX_t
666 lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
667                              const Elf_Rel_Impl<ELFT, IsRela> &RI) {
668   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
669   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
670 
671   uintX_t Addend = getAddend<ELFT>(RI);
672 
673   // PPC64 has a special relocation representing the TOC base pointer
674   // that does not have a corresponding symbol.
675   if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
676     return getPPC64TocBase() + Addend;
677 
678   const Elf_Sym *Sym =
679       File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
680 
681   if (!Sym)
682     error("Unsupported relocation without symbol");
683 
684   // According to the ELF spec reference to a local symbol from outside
685   // the group are not allowed. Unfortunately .eh_frame breaks that rule
686   // and must be treated specially. For now we just replace the symbol with
687   // 0.
688   InputSectionBase<ELFT> *Section = File.getSection(*Sym);
689   if (Section == &InputSection<ELFT>::Discarded)
690     return Addend;
691 
692   uintX_t VA = Section->OutSec->getVA();
693   if (isa<InputSection<ELFT>>(Section))
694     return VA + Section->getOffset(*Sym) + Addend;
695 
696   uintX_t Offset = Sym->st_value;
697   if (Sym->getType() == STT_SECTION) {
698     Offset += Addend;
699     Addend = 0;
700   }
701   return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
702          Addend;
703 }
704 
705 // Returns true if a symbol can be replaced at load-time by a symbol
706 // with the same name defined in other ELF executable or DSO.
707 bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
708   if (!Body)
709     return false;  // Body is a local symbol.
710   if (Body->isShared())
711     return true;
712 
713   if (Body->isUndefined()) {
714     if (!Body->isWeak())
715       return true;
716 
717     // This is an horrible corner case. Ideally we would like to say that any
718     // undefined symbol can be preempted so that the dynamic linker has a
719     // chance of finding it at runtime.
720     //
721     // The problem is that the code sequence used to test for weak undef
722     // functions looks like
723     // if (func) func()
724     // If the code is -fPIC the first reference is a load from the got and
725     // everything works.
726     // If the code is not -fPIC there is no reasonable way to solve it:
727     // * A relocation writing to the text segment will fail (it is ro).
728     // * A copy relocation doesn't work for functions.
729     // * The trick of using a plt entry as the address would fail here since
730     //   the plt entry would have a non zero address.
731     // Since we cannot do anything better, we just resolve the symbol to 0 and
732     // don't produce a dynamic relocation.
733     //
734     // As an extra hack, assume that if we are producing a shared library the
735     // user knows what he or she is doing and can handle a dynamic relocation.
736     return Config->Shared || NeedsGot;
737   }
738   if (!Config->Shared)
739     return false;
740   return Body->getVisibility() == STV_DEFAULT;
741 }
742 
743 template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
744   for (InputSection<ELFT> *C : Sections)
745     C->writeTo(Buf);
746 }
747 
748 template <class ELFT>
749 MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
750                                              uintX_t sh_flags)
751     : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
752 
753 template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
754   if (shouldTailMerge()) {
755     StringRef Data = Builder.data();
756     memcpy(Buf, Data.data(), Data.size());
757     return;
758   }
759   for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
760     StringRef Data = P.first;
761     memcpy(Buf + P.second, Data.data(), Data.size());
762   }
763 }
764 
765 static size_t findNull(StringRef S, size_t EntSize) {
766   // Optimize the common case.
767   if (EntSize == 1)
768     return S.find(0);
769 
770   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
771     const char *B = S.begin() + I;
772     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
773       return I;
774   }
775   return StringRef::npos;
776 }
777 
778 template <class ELFT>
779 void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
780   S->OutSec = this;
781   uint32_t Align = S->getAlign();
782   if (Align > this->Header.sh_addralign)
783     this->Header.sh_addralign = Align;
784 
785   ArrayRef<uint8_t> D = S->getSectionData();
786   StringRef Data((const char *)D.data(), D.size());
787   uintX_t EntSize = S->getSectionHdr()->sh_entsize;
788   uintX_t Offset = 0;
789 
790   if (this->Header.sh_flags & SHF_STRINGS) {
791     while (!Data.empty()) {
792       size_t End = findNull(Data, EntSize);
793       if (End == StringRef::npos)
794         error("String is not null terminated");
795       StringRef Entry = Data.substr(0, End + EntSize);
796       size_t OutputOffset = Builder.add(Entry);
797       if (shouldTailMerge())
798         OutputOffset = -1;
799       S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
800       uintX_t Size = End + EntSize;
801       Data = Data.substr(Size);
802       Offset += Size;
803     }
804   } else {
805     for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
806       StringRef Entry = Data.substr(I, EntSize);
807       size_t OutputOffset = Builder.add(Entry);
808       S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
809       Offset += EntSize;
810     }
811   }
812 }
813 
814 template <class ELFT>
815 unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
816   return Builder.getOffset(Val);
817 }
818 
819 template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
820   return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
821 }
822 
823 template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
824   if (shouldTailMerge())
825     Builder.finalize();
826   this->Header.sh_size = Builder.getSize();
827 }
828 
829 template <class ELFT>
830 StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
831     : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
832                               Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
833       Dynamic(Dynamic) {
834   this->Header.sh_addralign = 1;
835 }
836 
837 template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
838   StringRef Data = StrTabBuilder.data();
839   memcpy(Buf, Data.data(), Data.size());
840 }
841 
842 template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
843   if (!B.isUsedInRegularObj())
844     return false;
845 
846   // Don't include synthetic symbols like __init_array_start in every output.
847   if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
848     if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
849       return false;
850 
851   return true;
852 }
853 
854 bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
855   uint8_t V = B.getVisibility();
856   if (V != STV_DEFAULT && V != STV_PROTECTED)
857     return false;
858 
859   if (Config->ExportDynamic || Config->Shared)
860     return true;
861   return B.isUsedInDynamicReloc();
862 }
863 
864 bool lld::elf2::includeInGnuHashTable(SymbolBody *B) {
865   // Assume that includeInDynamicSymtab() is already checked.
866   return !B->isUndefined();
867 }
868 
869 template <class ELFT>
870 bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
871                                    StringRef SymName,
872                                    const typename ELFFile<ELFT>::Elf_Sym &Sym) {
873   if (Sym.getType() == STT_SECTION)
874     return false;
875 
876   // If sym references a section in a discarded group, don't keep it.
877   if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
878     return false;
879 
880   if (Config->DiscardNone)
881     return true;
882 
883   // ELF defines dynamic locals as symbols which name starts with ".L".
884   return !(Config->DiscardLocals && SymName.startswith(".L"));
885 }
886 
887 template <class ELFT>
888 SymbolTableSection<ELFT>::SymbolTableSection(
889     SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
890     : OutputSectionBase<ELFT>(
891           StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
892           StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
893           StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
894       Table(Table), StrTabSec(StrTabSec) {
895   typedef OutputSectionBase<ELFT> Base;
896   typename Base::Elf_Shdr &Header = this->Header;
897 
898   Header.sh_entsize = sizeof(Elf_Sym);
899   Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
900 }
901 
902 template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
903   this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
904   this->Header.sh_link = StrTabSec.SectionIndex;
905   this->Header.sh_info = NumLocals + 1;
906 
907   if (!StrTabSec.isDynamic()) {
908     std::stable_sort(Symbols.begin(), Symbols.end(),
909                      [](SymbolBody *L, SymbolBody *R) {
910                        return getSymbolBinding(L) == STB_LOCAL &&
911                               getSymbolBinding(R) != STB_LOCAL;
912                      });
913     return;
914   }
915   if (Out<ELFT>::GnuHashTab)
916     // NB: It also sorts Symbols to meet the GNU hash table requirements.
917     Out<ELFT>::GnuHashTab->addSymbols(Symbols);
918   size_t I = 0;
919   for (SymbolBody *B : Symbols)
920     B->setDynamicSymbolTableIndex(++I);
921 }
922 
923 template <class ELFT>
924 void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
925   StrTabSec.add(Name);
926   ++NumVisible;
927   ++NumLocals;
928 }
929 
930 template <class ELFT>
931 void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
932   StrTabSec.add(Body->getName());
933   Symbols.push_back(Body);
934   ++NumVisible;
935 }
936 
937 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
938   Buf += sizeof(Elf_Sym);
939 
940   // All symbols with STB_LOCAL binding precede the weak and global symbols.
941   // .dynsym only contains global symbols.
942   if (!Config->DiscardAll && !StrTabSec.isDynamic())
943     writeLocalSymbols(Buf);
944 
945   writeGlobalSymbols(Buf);
946 }
947 
948 template <class ELFT>
949 void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
950   // Iterate over all input object files to copy their local symbols
951   // to the output symbol table pointed by Buf.
952   for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
953     Elf_Sym_Range Syms = File->getLocalSymbols();
954     for (const Elf_Sym &Sym : Syms) {
955       ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
956       error(SymNameOrErr);
957       StringRef SymName = *SymNameOrErr;
958       if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
959         continue;
960 
961       auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
962       uintX_t VA = 0;
963       if (Sym.st_shndx == SHN_ABS) {
964         ESym->st_shndx = SHN_ABS;
965         VA = Sym.st_value;
966       } else {
967         InputSectionBase<ELFT> *Section = File->getSection(Sym);
968         if (!Section->isLive())
969           continue;
970         const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
971         ESym->st_shndx = OutSec->SectionIndex;
972         VA += OutSec->getVA() + Section->getOffset(Sym);
973       }
974       ESym->st_name = StrTabSec.getOffset(SymName);
975       ESym->st_size = Sym.st_size;
976       ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
977       ESym->st_value = VA;
978       Buf += sizeof(*ESym);
979     }
980   }
981 }
982 
983 template <class ELFT>
984 void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
985   // Write the internal symbol table contents to the output symbol table
986   // pointed by Buf.
987   auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
988   for (SymbolBody *Body : Symbols) {
989     const OutputSectionBase<ELFT> *OutSec = nullptr;
990 
991     switch (Body->kind()) {
992     case SymbolBody::DefinedSyntheticKind:
993       OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
994       break;
995     case SymbolBody::DefinedRegularKind: {
996       auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
997       if (!Sym->Section.isLive())
998         continue;
999       OutSec = Sym->Section.OutSec;
1000       break;
1001     }
1002     case SymbolBody::DefinedCommonKind:
1003       OutSec = Out<ELFT>::Bss;
1004       break;
1005     case SymbolBody::SharedKind: {
1006       if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
1007         OutSec = Out<ELFT>::Bss;
1008       break;
1009     }
1010     case SymbolBody::UndefinedKind:
1011     case SymbolBody::DefinedAbsoluteKind:
1012     case SymbolBody::LazyKind:
1013       break;
1014     }
1015 
1016     StringRef Name = Body->getName();
1017     ESym->st_name = StrTabSec.getOffset(Name);
1018 
1019     unsigned char Type = STT_NOTYPE;
1020     uintX_t Size = 0;
1021     if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1022       const Elf_Sym &InputSym = EBody->Sym;
1023       Type = InputSym.getType();
1024       Size = InputSym.st_size;
1025     }
1026 
1027     ESym->setBindingAndType(getSymbolBinding(Body), Type);
1028     ESym->st_size = Size;
1029     ESym->setVisibility(Body->getVisibility());
1030     ESym->st_value = getSymVA<ELFT>(*Body);
1031 
1032     if (isa<DefinedAbsolute<ELFT>>(Body))
1033       ESym->st_shndx = SHN_ABS;
1034     else if (OutSec)
1035       ESym->st_shndx = OutSec->SectionIndex;
1036 
1037     ++ESym;
1038   }
1039 }
1040 
1041 template <class ELFT>
1042 uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
1043   uint8_t Visibility = Body->getVisibility();
1044   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1045     return STB_LOCAL;
1046   if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1047     return EBody->Sym.getBinding();
1048   return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1049 }
1050 
1051 namespace lld {
1052 namespace elf2 {
1053 template class OutputSectionBase<ELF32LE>;
1054 template class OutputSectionBase<ELF32BE>;
1055 template class OutputSectionBase<ELF64LE>;
1056 template class OutputSectionBase<ELF64BE>;
1057 
1058 template class GotPltSection<ELF32LE>;
1059 template class GotPltSection<ELF32BE>;
1060 template class GotPltSection<ELF64LE>;
1061 template class GotPltSection<ELF64BE>;
1062 
1063 template class GotSection<ELF32LE>;
1064 template class GotSection<ELF32BE>;
1065 template class GotSection<ELF64LE>;
1066 template class GotSection<ELF64BE>;
1067 
1068 template class PltSection<ELF32LE>;
1069 template class PltSection<ELF32BE>;
1070 template class PltSection<ELF64LE>;
1071 template class PltSection<ELF64BE>;
1072 
1073 template class RelocationSection<ELF32LE>;
1074 template class RelocationSection<ELF32BE>;
1075 template class RelocationSection<ELF64LE>;
1076 template class RelocationSection<ELF64BE>;
1077 
1078 template class InterpSection<ELF32LE>;
1079 template class InterpSection<ELF32BE>;
1080 template class InterpSection<ELF64LE>;
1081 template class InterpSection<ELF64BE>;
1082 
1083 template class GnuHashTableSection<ELF32LE>;
1084 template class GnuHashTableSection<ELF32BE>;
1085 template class GnuHashTableSection<ELF64LE>;
1086 template class GnuHashTableSection<ELF64BE>;
1087 
1088 template class HashTableSection<ELF32LE>;
1089 template class HashTableSection<ELF32BE>;
1090 template class HashTableSection<ELF64LE>;
1091 template class HashTableSection<ELF64BE>;
1092 
1093 template class DynamicSection<ELF32LE>;
1094 template class DynamicSection<ELF32BE>;
1095 template class DynamicSection<ELF64LE>;
1096 template class DynamicSection<ELF64BE>;
1097 
1098 template class OutputSection<ELF32LE>;
1099 template class OutputSection<ELF32BE>;
1100 template class OutputSection<ELF64LE>;
1101 template class OutputSection<ELF64BE>;
1102 
1103 template class MergeOutputSection<ELF32LE>;
1104 template class MergeOutputSection<ELF32BE>;
1105 template class MergeOutputSection<ELF64LE>;
1106 template class MergeOutputSection<ELF64BE>;
1107 
1108 template class StringTableSection<ELF32LE>;
1109 template class StringTableSection<ELF32BE>;
1110 template class StringTableSection<ELF64LE>;
1111 template class StringTableSection<ELF64BE>;
1112 
1113 template class SymbolTableSection<ELF32LE>;
1114 template class SymbolTableSection<ELF32BE>;
1115 template class SymbolTableSection<ELF64LE>;
1116 template class SymbolTableSection<ELF64BE>;
1117 
1118 template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1119 template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1120 template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1121 template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
1122 
1123 template ELFFile<ELF32LE>::uintX_t
1124 getLocalRelTarget(const ObjectFile<ELF32LE> &,
1125                   const ELFFile<ELF32LE>::Elf_Rel &);
1126 template ELFFile<ELF32BE>::uintX_t
1127 getLocalRelTarget(const ObjectFile<ELF32BE> &,
1128                   const ELFFile<ELF32BE>::Elf_Rel &);
1129 template ELFFile<ELF64LE>::uintX_t
1130 getLocalRelTarget(const ObjectFile<ELF64LE> &,
1131                   const ELFFile<ELF64LE>::Elf_Rel &);
1132 template ELFFile<ELF64BE>::uintX_t
1133 getLocalRelTarget(const ObjectFile<ELF64BE> &,
1134                   const ELFFile<ELF64BE>::Elf_Rel &);
1135 
1136 template ELFFile<ELF32LE>::uintX_t
1137 getLocalRelTarget(const ObjectFile<ELF32LE> &,
1138                   const ELFFile<ELF32LE>::Elf_Rela &);
1139 template ELFFile<ELF32BE>::uintX_t
1140 getLocalRelTarget(const ObjectFile<ELF32BE> &,
1141                   const ELFFile<ELF32BE>::Elf_Rela &);
1142 template ELFFile<ELF64LE>::uintX_t
1143 getLocalRelTarget(const ObjectFile<ELF64LE> &,
1144                   const ELFFile<ELF64LE>::Elf_Rela &);
1145 template ELFFile<ELF64BE>::uintX_t
1146 getLocalRelTarget(const ObjectFile<ELF64BE> &,
1147                   const ELFFile<ELF64BE>::Elf_Rela &);
1148 
1149 template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1150 template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1151 template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1152 template bool includeInSymtab<ELF64BE>(const SymbolBody &);
1153 
1154 template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1155                                           StringRef,
1156                                           const ELFFile<ELF32LE>::Elf_Sym &);
1157 template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1158                                           StringRef,
1159                                           const ELFFile<ELF32BE>::Elf_Sym &);
1160 template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1161                                           StringRef,
1162                                           const ELFFile<ELF64LE>::Elf_Sym &);
1163 template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1164                                           StringRef,
1165                                           const ELFFile<ELF64BE>::Elf_Sym &);
1166 }
1167 }
1168