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 "LinkerScript.h"
13 #include "SymbolTable.h"
14 #include "Target.h"
15 #include "lld/Core/Parallel.h"
16 #include "llvm/Support/Dwarf.h"
17 #include "llvm/Support/MathExtras.h"
18 #include <map>
19 
20 using namespace llvm;
21 using namespace llvm::dwarf;
22 using namespace llvm::object;
23 using namespace llvm::support::endian;
24 using namespace llvm::ELF;
25 
26 using namespace lld;
27 using namespace lld::elf;
28 
29 static bool isAlpha(char C) {
30   return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
31 }
32 
33 static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
34 
35 // Returns true if S is valid as a C language identifier.
36 bool elf::isValidCIdentifier(StringRef S) {
37   return !S.empty() && isAlpha(S[0]) &&
38          std::all_of(S.begin() + 1, S.end(), isAlnum);
39 }
40 
41 template <class ELFT>
42 OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
43                                            uintX_t Flags)
44     : Name(Name) {
45   memset(&Header, 0, sizeof(Elf_Shdr));
46   Header.sh_type = Type;
47   Header.sh_flags = Flags;
48 }
49 
50 template <class ELFT>
51 void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *Shdr) {
52   *Shdr = Header;
53 }
54 
55 template <class ELFT>
56 GotPltSection<ELFT>::GotPltSection()
57     : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
58   this->Header.sh_addralign = sizeof(uintX_t);
59 }
60 
61 template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
62   Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
63   Entries.push_back(&Sym);
64 }
65 
66 template <class ELFT> bool GotPltSection<ELFT>::empty() const {
67   return Entries.empty();
68 }
69 
70 template <class ELFT> void GotPltSection<ELFT>::finalize() {
71   this->Header.sh_size =
72       (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t);
73 }
74 
75 template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
76   Target->writeGotPltHeader(Buf);
77   Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t);
78   for (const SymbolBody *B : Entries) {
79     Target->writeGotPlt(Buf, B->getPltVA<ELFT>());
80     Buf += sizeof(uintX_t);
81   }
82 }
83 
84 template <class ELFT>
85 GotSection<ELFT>::GotSection()
86     : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
87   if (Config->EMachine == EM_MIPS)
88     this->Header.sh_flags |= SHF_MIPS_GPREL;
89   this->Header.sh_addralign = sizeof(uintX_t);
90 }
91 
92 template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
93   Sym.GotIndex = Entries.size();
94   Entries.push_back(&Sym);
95 }
96 
97 template <class ELFT> void GotSection<ELFT>::addMipsLocalEntry() {
98   ++MipsLocalEntries;
99 }
100 
101 template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
102   if (Sym.hasGlobalDynIndex())
103     return false;
104   Sym.GlobalDynIndex = Target->GotHeaderEntriesNum + Entries.size();
105   // Global Dynamic TLS entries take two GOT slots.
106   Entries.push_back(&Sym);
107   Entries.push_back(nullptr);
108   return true;
109 }
110 
111 // Reserves TLS entries for a TLS module ID and a TLS block offset.
112 // In total it takes two GOT slots.
113 template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
114   if (TlsIndexOff != uint32_t(-1))
115     return false;
116   TlsIndexOff = Entries.size() * sizeof(uintX_t);
117   Entries.push_back(nullptr);
118   Entries.push_back(nullptr);
119   return true;
120 }
121 
122 template <class ELFT>
123 typename GotSection<ELFT>::uintX_t
124 GotSection<ELFT>::getMipsLocalFullAddr(const SymbolBody &B) {
125   return getMipsLocalEntryAddr(B.getVA<ELFT>());
126 }
127 
128 template <class ELFT>
129 typename GotSection<ELFT>::uintX_t
130 GotSection<ELFT>::getMipsLocalPageAddr(uintX_t EntryValue) {
131   // Initialize the entry by the %hi(EntryValue) expression
132   // but without right-shifting.
133   return getMipsLocalEntryAddr((EntryValue + 0x8000) & ~0xffff);
134 }
135 
136 template <class ELFT>
137 typename GotSection<ELFT>::uintX_t
138 GotSection<ELFT>::getMipsLocalEntryAddr(uintX_t EntryValue) {
139   size_t NewIndex = Target->GotHeaderEntriesNum + MipsLocalGotPos.size();
140   auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
141   assert(!P.second || MipsLocalGotPos.size() <= MipsLocalEntries);
142   return this->getVA() + P.first->second * sizeof(uintX_t);
143 }
144 
145 template <class ELFT>
146 typename GotSection<ELFT>::uintX_t
147 GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
148   return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
149 }
150 
151 template <class ELFT>
152 const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
153   return Entries.empty() ? nullptr : Entries.front();
154 }
155 
156 template <class ELFT>
157 unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
158   return Target->GotHeaderEntriesNum + MipsLocalEntries;
159 }
160 
161 template <class ELFT> void GotSection<ELFT>::finalize() {
162   this->Header.sh_size =
163       (Target->GotHeaderEntriesNum + MipsLocalEntries + Entries.size()) *
164       sizeof(uintX_t);
165 }
166 
167 template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
168   Target->writeGotHeader(Buf);
169   for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
170     uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
171     write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
172   }
173   Buf += Target->GotHeaderEntriesNum * sizeof(uintX_t);
174   Buf += MipsLocalEntries * sizeof(uintX_t);
175   for (const SymbolBody *B : Entries) {
176     uint8_t *Entry = Buf;
177     Buf += sizeof(uintX_t);
178     if (!B)
179       continue;
180     // MIPS has special rules to fill up GOT entries.
181     // See "Global Offset Table" in Chapter 5 in the following document
182     // for detailed description:
183     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
184     // As the first approach, we can just store addresses for all symbols.
185     if (Config->EMachine != EM_MIPS && B->isPreemptible())
186       continue; // The dynamic linker will take care of it.
187     uintX_t VA = B->getVA<ELFT>();
188     write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
189   }
190 }
191 
192 template <class ELFT>
193 PltSection<ELFT>::PltSection()
194     : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
195   this->Header.sh_addralign = 16;
196 }
197 
198 template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
199   size_t Off = 0;
200   if (Target->UseLazyBinding) {
201     // At beginning of PLT, we have code to call the dynamic linker
202     // to resolve dynsyms at runtime. Write such code.
203     Target->writePltZero(Buf);
204     Off += Target->PltZeroSize;
205   }
206   for (auto &I : Entries) {
207     const SymbolBody *B = I.first;
208     unsigned RelOff = I.second;
209     uint64_t Got =
210         Target->UseLazyBinding ? B->getGotPltVA<ELFT>() : B->getGotVA<ELFT>();
211     uint64_t Plt = this->getVA() + Off;
212     Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
213     Off += Target->PltEntrySize;
214   }
215 }
216 
217 template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
218   Sym.PltIndex = Entries.size();
219   unsigned RelOff = Target->UseLazyBinding
220                         ? Out<ELFT>::RelaPlt->getRelocOffset()
221                         : Out<ELFT>::RelaDyn->getRelocOffset();
222   Entries.push_back(std::make_pair(&Sym, RelOff));
223 }
224 
225 template <class ELFT> void PltSection<ELFT>::finalize() {
226   this->Header.sh_size =
227       Target->PltZeroSize + Entries.size() * Target->PltEntrySize;
228 }
229 
230 template <class ELFT>
231 RelocationSection<ELFT>::RelocationSection(StringRef Name)
232     : OutputSectionBase<ELFT>(Name, Config->Rela ? SHT_RELA : SHT_REL,
233                               SHF_ALLOC) {
234   this->Header.sh_entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
235   this->Header.sh_addralign = sizeof(uintX_t);
236 }
237 
238 template <class ELFT>
239 void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
240   SymbolBody *Sym = Reloc.Sym;
241   if (!Reloc.UseSymVA && Sym)
242     Sym->MustBeInDynSym = true;
243   Relocs.push_back(Reloc);
244 }
245 
246 template <class ELFT>
247 typename ELFFile<ELFT>::uintX_t DynamicReloc<ELFT>::getOffset() const {
248   switch (OKind) {
249   case Off_GTlsIndex:
250     return Out<ELFT>::Got->getGlobalDynAddr(*Sym);
251   case Off_GTlsOffset:
252     return Out<ELFT>::Got->getGlobalDynAddr(*Sym) + sizeof(uintX_t);
253   case Off_LTlsIndex:
254     return Out<ELFT>::Got->getTlsIndexVA();
255   case Off_Sec:
256     return OffsetSec->getOffset(OffsetInSec) + OffsetSec->OutSec->getVA();
257   case Off_Bss:
258     return cast<SharedSymbol<ELFT>>(Sym)->OffsetInBss + Out<ELFT>::Bss->getVA();
259   case Off_Got:
260     return Sym->getGotVA<ELFT>();
261   case Off_GotPlt:
262     return Sym->getGotPltVA<ELFT>();
263   }
264   llvm_unreachable("invalid offset kind");
265 }
266 
267 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
268   for (const DynamicReloc<ELFT> &Rel : Relocs) {
269     auto *P = reinterpret_cast<Elf_Rela *>(Buf);
270     Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
271     SymbolBody *Sym = Rel.Sym;
272 
273     if (Config->Rela)
274       P->r_addend = Rel.UseSymVA ? Sym->getVA<ELFT>(Rel.Addend) : Rel.Addend;
275     P->r_offset = Rel.getOffset();
276     uint32_t SymIdx = (!Rel.UseSymVA && Sym) ? Sym->DynsymIndex : 0;
277     P->setSymbolAndType(SymIdx, Rel.Type, Config->Mips64EL);
278   }
279 }
280 
281 template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
282   return this->Header.sh_entsize * Relocs.size();
283 }
284 
285 template <class ELFT> void RelocationSection<ELFT>::finalize() {
286   this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
287                                 : Out<ELFT>::DynSymTab->SectionIndex;
288   this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
289 }
290 
291 template <class ELFT>
292 InterpSection<ELFT>::InterpSection()
293     : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
294   this->Header.sh_size = Config->DynamicLinker.size() + 1;
295   this->Header.sh_addralign = 1;
296 }
297 
298 template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
299   StringRef S = Config->DynamicLinker;
300   memcpy(Buf, S.data(), S.size());
301 }
302 
303 template <class ELFT>
304 HashTableSection<ELFT>::HashTableSection()
305     : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
306   this->Header.sh_entsize = sizeof(Elf_Word);
307   this->Header.sh_addralign = sizeof(Elf_Word);
308 }
309 
310 static uint32_t hashSysv(StringRef Name) {
311   uint32_t H = 0;
312   for (char C : Name) {
313     H = (H << 4) + C;
314     uint32_t G = H & 0xf0000000;
315     if (G)
316       H ^= G >> 24;
317     H &= ~G;
318   }
319   return H;
320 }
321 
322 template <class ELFT> void HashTableSection<ELFT>::finalize() {
323   this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
324 
325   unsigned NumEntries = 2;                             // nbucket and nchain.
326   NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
327 
328   // Create as many buckets as there are symbols.
329   // FIXME: This is simplistic. We can try to optimize it, but implementing
330   // support for SHT_GNU_HASH is probably even more profitable.
331   NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
332   this->Header.sh_size = NumEntries * sizeof(Elf_Word);
333 }
334 
335 template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
336   unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
337   auto *P = reinterpret_cast<Elf_Word *>(Buf);
338   *P++ = NumSymbols; // nbucket
339   *P++ = NumSymbols; // nchain
340 
341   Elf_Word *Buckets = P;
342   Elf_Word *Chains = P + NumSymbols;
343 
344   for (const std::pair<SymbolBody *, unsigned> &P :
345        Out<ELFT>::DynSymTab->getSymbols()) {
346     SymbolBody *Body = P.first;
347     StringRef Name = Body->getName();
348     unsigned I = Body->DynsymIndex;
349     uint32_t Hash = hashSysv(Name) % NumSymbols;
350     Chains[I] = Buckets[Hash];
351     Buckets[Hash] = I;
352   }
353 }
354 
355 static uint32_t hashGnu(StringRef Name) {
356   uint32_t H = 5381;
357   for (uint8_t C : Name)
358     H = (H << 5) + H + C;
359   return H;
360 }
361 
362 template <class ELFT>
363 GnuHashTableSection<ELFT>::GnuHashTableSection()
364     : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
365   this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
366   this->Header.sh_addralign = sizeof(uintX_t);
367 }
368 
369 template <class ELFT>
370 unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
371   if (!NumHashed)
372     return 0;
373 
374   // These values are prime numbers which are not greater than 2^(N-1) + 1.
375   // In result, for any particular NumHashed we return a prime number
376   // which is not greater than NumHashed.
377   static const unsigned Primes[] = {
378       1,   1,    3,    3,    7,    13,    31,    61,    127,   251,
379       509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
380 
381   return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
382                                    array_lengthof(Primes) - 1)];
383 }
384 
385 // Bloom filter estimation: at least 8 bits for each hashed symbol.
386 // GNU Hash table requirement: it should be a power of 2,
387 //   the minimum value is 1, even for an empty table.
388 // Expected results for a 32-bit target:
389 //   calcMaskWords(0..4)   = 1
390 //   calcMaskWords(5..8)   = 2
391 //   calcMaskWords(9..16)  = 4
392 // For a 64-bit target:
393 //   calcMaskWords(0..8)   = 1
394 //   calcMaskWords(9..16)  = 2
395 //   calcMaskWords(17..32) = 4
396 template <class ELFT>
397 unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
398   if (!NumHashed)
399     return 1;
400   return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
401 }
402 
403 template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
404   unsigned NumHashed = Symbols.size();
405   NBuckets = calcNBuckets(NumHashed);
406   MaskWords = calcMaskWords(NumHashed);
407   // Second hash shift estimation: just predefined values.
408   Shift2 = ELFT::Is64Bits ? 6 : 5;
409 
410   this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
411   this->Header.sh_size = sizeof(Elf_Word) * 4            // Header
412                          + sizeof(Elf_Off) * MaskWords   // Bloom Filter
413                          + sizeof(Elf_Word) * NBuckets   // Hash Buckets
414                          + sizeof(Elf_Word) * NumHashed; // Hash Values
415 }
416 
417 template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
418   writeHeader(Buf);
419   if (Symbols.empty())
420     return;
421   writeBloomFilter(Buf);
422   writeHashTable(Buf);
423 }
424 
425 template <class ELFT>
426 void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
427   auto *P = reinterpret_cast<Elf_Word *>(Buf);
428   *P++ = NBuckets;
429   *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
430   *P++ = MaskWords;
431   *P++ = Shift2;
432   Buf = reinterpret_cast<uint8_t *>(P);
433 }
434 
435 template <class ELFT>
436 void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
437   unsigned C = sizeof(Elf_Off) * 8;
438 
439   auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
440   for (const SymbolData &Sym : Symbols) {
441     size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
442     uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
443                 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
444     Masks[Pos] |= V;
445   }
446   Buf += sizeof(Elf_Off) * MaskWords;
447 }
448 
449 template <class ELFT>
450 void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
451   Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
452   Elf_Word *Values = Buckets + NBuckets;
453 
454   int PrevBucket = -1;
455   int I = 0;
456   for (const SymbolData &Sym : Symbols) {
457     int Bucket = Sym.Hash % NBuckets;
458     assert(PrevBucket <= Bucket);
459     if (Bucket != PrevBucket) {
460       Buckets[Bucket] = Sym.Body->DynsymIndex;
461       PrevBucket = Bucket;
462       if (I > 0)
463         Values[I - 1] |= 1;
464     }
465     Values[I] = Sym.Hash & ~1;
466     ++I;
467   }
468   if (I > 0)
469     Values[I - 1] |= 1;
470 }
471 
472 static bool includeInGnuHashTable(SymbolBody *B) {
473   // Assume that includeInDynsym() is already checked.
474   return !B->isUndefined();
475 }
476 
477 // Add symbols to this symbol hash table. Note that this function
478 // destructively sort a given vector -- which is needed because
479 // GNU-style hash table places some sorting requirements.
480 template <class ELFT>
481 void GnuHashTableSection<ELFT>::addSymbols(
482     std::vector<std::pair<SymbolBody *, size_t>> &V) {
483   auto Mid = std::stable_partition(V.begin(), V.end(),
484                                    [](std::pair<SymbolBody *, size_t> &P) {
485                                      return !includeInGnuHashTable(P.first);
486                                    });
487   if (Mid == V.end())
488     return;
489   for (auto I = Mid, E = V.end(); I != E; ++I) {
490     SymbolBody *B = I->first;
491     size_t StrOff = I->second;
492     Symbols.push_back({B, StrOff, hashGnu(B->getName())});
493   }
494 
495   unsigned NBuckets = calcNBuckets(Symbols.size());
496   std::stable_sort(Symbols.begin(), Symbols.end(),
497                    [&](const SymbolData &L, const SymbolData &R) {
498                      return L.Hash % NBuckets < R.Hash % NBuckets;
499                    });
500 
501   V.erase(Mid, V.end());
502   for (const SymbolData &Sym : Symbols)
503     V.push_back({Sym.Body, Sym.STName});
504 }
505 
506 template <class ELFT>
507 DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
508     : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
509       SymTab(SymTab) {
510   Elf_Shdr &Header = this->Header;
511   Header.sh_addralign = sizeof(uintX_t);
512   Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
513 
514   // .dynamic section is not writable on MIPS.
515   // See "Special Section" in Chapter 4 in the following document:
516   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
517   if (Config->EMachine == EM_MIPS)
518     Header.sh_flags = SHF_ALLOC;
519 }
520 
521 template <class ELFT> void DynamicSection<ELFT>::finalize() {
522   if (this->Header.sh_size)
523     return; // Already finalized.
524 
525   Elf_Shdr &Header = this->Header;
526   Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
527 
528   auto Add = [=](Entry E) { Entries.push_back(E); };
529 
530   // Add strings. We know that these are the last strings to be added to
531   // DynStrTab and doing this here allows this function to set DT_STRSZ.
532   if (!Config->RPath.empty())
533     Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
534          Out<ELFT>::DynStrTab->addString(Config->RPath)});
535   for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
536     if (F->isNeeded())
537       Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
538   if (!Config->SoName.empty())
539     Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
540 
541   Out<ELFT>::DynStrTab->finalize();
542 
543   if (Out<ELFT>::RelaDyn->hasRelocs()) {
544     bool IsRela = Config->Rela;
545     Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
546     Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
547     Add({IsRela ? DT_RELAENT : DT_RELENT,
548          uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
549   }
550   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
551     Add({DT_JMPREL, Out<ELFT>::RelaPlt});
552     Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
553     Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
554          Out<ELFT>::GotPlt});
555     Add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
556   }
557 
558   Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
559   Add({DT_SYMENT, sizeof(Elf_Sym)});
560   Add({DT_STRTAB, Out<ELFT>::DynStrTab});
561   Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
562   if (Out<ELFT>::GnuHashTab)
563     Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
564   if (Out<ELFT>::HashTab)
565     Add({DT_HASH, Out<ELFT>::HashTab});
566 
567   if (PreInitArraySec) {
568     Add({DT_PREINIT_ARRAY, PreInitArraySec});
569     Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()});
570   }
571   if (InitArraySec) {
572     Add({DT_INIT_ARRAY, InitArraySec});
573     Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()});
574   }
575   if (FiniArraySec) {
576     Add({DT_FINI_ARRAY, FiniArraySec});
577     Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()});
578   }
579 
580   if (SymbolBody *B = SymTab.find(Config->Init))
581     Add({DT_INIT, B});
582   if (SymbolBody *B = SymTab.find(Config->Fini))
583     Add({DT_FINI, B});
584 
585   uint32_t DtFlags = 0;
586   uint32_t DtFlags1 = 0;
587   if (Config->Bsymbolic)
588     DtFlags |= DF_SYMBOLIC;
589   if (Config->ZNodelete)
590     DtFlags1 |= DF_1_NODELETE;
591   if (Config->ZNow) {
592     DtFlags |= DF_BIND_NOW;
593     DtFlags1 |= DF_1_NOW;
594   }
595   if (Config->ZOrigin) {
596     DtFlags |= DF_ORIGIN;
597     DtFlags1 |= DF_1_ORIGIN;
598   }
599 
600   if (DtFlags)
601     Add({DT_FLAGS, DtFlags});
602   if (DtFlags1)
603     Add({DT_FLAGS_1, DtFlags1});
604 
605   if (!Config->Entry.empty())
606     Add({DT_DEBUG, (uint64_t)0});
607 
608   if (Config->EMachine == EM_MIPS) {
609     Add({DT_MIPS_RLD_VERSION, 1});
610     Add({DT_MIPS_FLAGS, RHF_NOTPOT});
611     Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()});
612     Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
613     Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
614     if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
615       Add({DT_MIPS_GOTSYM, B->DynsymIndex});
616     else
617       Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
618     Add({DT_PLTGOT, Out<ELFT>::Got});
619     if (Out<ELFT>::MipsRldMap)
620       Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
621   }
622 
623   // +1 for DT_NULL
624   Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
625 }
626 
627 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
628   auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
629 
630   for (const Entry &E : Entries) {
631     P->d_tag = E.Tag;
632     switch (E.Kind) {
633     case Entry::SecAddr:
634       P->d_un.d_ptr = E.OutSec->getVA();
635       break;
636     case Entry::SymAddr:
637       P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
638       break;
639     case Entry::PlainInt:
640       P->d_un.d_val = E.Val;
641       break;
642     }
643     ++P;
644   }
645 }
646 
647 template <class ELFT>
648 EhFrameHeader<ELFT>::EhFrameHeader()
649     : OutputSectionBase<ELFT>(".eh_frame_hdr", llvm::ELF::SHT_PROGBITS,
650                               SHF_ALLOC) {
651   // It's a 4 bytes of header + pointer to the contents of the .eh_frame section
652   // + the number of FDE pointers in the table.
653   this->Header.sh_size = 12;
654 }
655 
656 // We have to get PC values of FDEs. They depend on relocations
657 // which are target specific, so we run this code after performing
658 // all relocations. We read the values from ouput buffer according to the
659 // encoding given for FDEs. Return value is an offset to the initial PC value
660 // for the FDE.
661 template <class ELFT>
662 typename EhFrameHeader<ELFT>::uintX_t
663 EhFrameHeader<ELFT>::getFdePc(uintX_t EhVA, const FdeData &F) {
664   const endianness E = ELFT::TargetEndianness;
665   uint8_t Size = F.Enc & 0x7;
666   if (Size == DW_EH_PE_absptr)
667     Size = sizeof(uintX_t) == 8 ? DW_EH_PE_udata8 : DW_EH_PE_udata4;
668   uint64_t PC;
669   switch (Size) {
670   case DW_EH_PE_udata2:
671     PC = read16<E>(F.PCRel);
672     break;
673   case DW_EH_PE_udata4:
674     PC = read32<E>(F.PCRel);
675     break;
676   case DW_EH_PE_udata8:
677     PC = read64<E>(F.PCRel);
678     break;
679   default:
680     fatal("unknown FDE size encoding");
681   }
682   switch (F.Enc & 0x70) {
683   case DW_EH_PE_absptr:
684     return PC;
685   case DW_EH_PE_pcrel:
686     return PC + EhVA + F.Off + 8;
687   default:
688     fatal("unknown FDE size relative encoding");
689   }
690 }
691 
692 template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
693   const endianness E = ELFT::TargetEndianness;
694 
695   const uint8_t Header[] = {1, DW_EH_PE_pcrel | DW_EH_PE_sdata4,
696                             DW_EH_PE_udata4,
697                             DW_EH_PE_datarel | DW_EH_PE_sdata4};
698   memcpy(Buf, Header, sizeof(Header));
699 
700   uintX_t EhVA = Sec->getVA();
701   uintX_t VA = this->getVA();
702   uintX_t EhOff = EhVA - VA - 4;
703   write32<E>(Buf + 4, EhOff);
704   write32<E>(Buf + 8, this->FdeList.size());
705   Buf += 12;
706 
707   // InitialPC -> Offset in .eh_frame, sorted by InitialPC.
708   std::map<uintX_t, size_t> PcToOffset;
709   for (const FdeData &F : FdeList)
710     PcToOffset[getFdePc(EhVA, F)] = F.Off;
711 
712   for (auto &I : PcToOffset) {
713     // The first four bytes are an offset to the initial PC value for the FDE.
714     write32<E>(Buf, I.first - VA);
715     // The last four bytes are an offset to the FDE data itself.
716     write32<E>(Buf + 4, EhVA + I.second - VA);
717     Buf += 8;
718   }
719 }
720 
721 template <class ELFT>
722 void EhFrameHeader<ELFT>::assignEhFrame(EHOutputSection<ELFT> *Sec) {
723   assert((!this->Sec || this->Sec == Sec) &&
724          "multiple .eh_frame sections not supported for .eh_frame_hdr");
725   Live = Config->EhFrameHdr;
726   this->Sec = Sec;
727 }
728 
729 template <class ELFT>
730 void EhFrameHeader<ELFT>::addFde(uint8_t Enc, size_t Off, uint8_t *PCRel) {
731   if (Live && (Enc & 0xF0) == DW_EH_PE_datarel)
732     fatal("DW_EH_PE_datarel encoding unsupported for FDEs by .eh_frame_hdr");
733   FdeList.push_back(FdeData{Enc, Off, PCRel});
734 }
735 
736 template <class ELFT> void EhFrameHeader<ELFT>::reserveFde() {
737   // Each FDE entry is 8 bytes long:
738   // The first four bytes are an offset to the initial PC value for the FDE. The
739   // last four byte are an offset to the FDE data itself.
740   this->Header.sh_size += 8;
741 }
742 
743 template <class ELFT>
744 OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
745     : OutputSectionBase<ELFT>(Name, Type, Flags) {
746   if (Type == SHT_RELA)
747     this->Header.sh_entsize = sizeof(Elf_Rela);
748   else if (Type == SHT_REL)
749     this->Header.sh_entsize = sizeof(Elf_Rel);
750 }
751 
752 template <class ELFT> void OutputSection<ELFT>::finalize() {
753   uint32_t Type = this->Header.sh_type;
754   if (Type != SHT_RELA && Type != SHT_REL)
755     return;
756   this->Header.sh_link = Out<ELFT>::SymTab->SectionIndex;
757   // sh_info for SHT_REL[A] sections should contain the section header index of
758   // the section to which the relocation applies.
759   InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
760   this->Header.sh_info = S->OutSec->SectionIndex;
761 }
762 
763 template <class ELFT>
764 void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
765   assert(C->Live);
766   auto *S = cast<InputSection<ELFT>>(C);
767   Sections.push_back(S);
768   S->OutSec = this;
769   this->updateAlign(S->Align);
770 
771   uintX_t Off = this->Header.sh_size;
772   Off = alignTo(Off, S->Align);
773   S->OutSecOff = Off;
774   Off += S->getSize();
775   this->Header.sh_size = Off;
776 }
777 
778 // If an input string is in the form of "foo.N" where N is a number,
779 // return N. Otherwise, returns 65536, which is one greater than the
780 // lowest priority.
781 static int getPriority(StringRef S) {
782   size_t Pos = S.rfind('.');
783   if (Pos == StringRef::npos)
784     return 65536;
785   int V;
786   if (S.substr(Pos + 1).getAsInteger(10, V))
787     return 65536;
788   return V;
789 }
790 
791 // This function is called after we sort input sections
792 // to update their offsets.
793 template <class ELFT> void OutputSection<ELFT>::reassignOffsets() {
794   uintX_t Off = 0;
795   for (InputSection<ELFT> *S : Sections) {
796     Off = alignTo(Off, S->Align);
797     S->OutSecOff = Off;
798     Off += S->getSize();
799   }
800   this->Header.sh_size = Off;
801 }
802 
803 // Sorts input sections by section name suffixes, so that .foo.N comes
804 // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
805 // We want to keep the original order if the priorities are the same
806 // because the compiler keeps the original initialization order in a
807 // translation unit and we need to respect that.
808 // For more detail, read the section of the GCC's manual about init_priority.
809 template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
810   // Sort sections by priority.
811   typedef std::pair<int, InputSection<ELFT> *> Pair;
812   auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
813 
814   std::vector<Pair> V;
815   for (InputSection<ELFT> *S : Sections)
816     V.push_back({getPriority(S->getSectionName()), S});
817   std::stable_sort(V.begin(), V.end(), Comp);
818   Sections.clear();
819   for (Pair &P : V)
820     Sections.push_back(P.second);
821   reassignOffsets();
822 }
823 
824 // Returns true if S matches /Filename.?\.o$/.
825 static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
826   if (!S.endswith(".o"))
827     return false;
828   S = S.drop_back(2);
829   if (S.endswith(Filename))
830     return true;
831   return !S.empty() && S.drop_back().endswith(Filename);
832 }
833 
834 static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
835 static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
836 
837 // .ctors and .dtors are sorted by this priority from highest to lowest.
838 //
839 //  1. The section was contained in crtbegin (crtbegin contains
840 //     some sentinel value in its .ctors and .dtors so that the runtime
841 //     can find the beginning of the sections.)
842 //
843 //  2. The section has an optional priority value in the form of ".ctors.N"
844 //     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
845 //     they are compared as string rather than number.
846 //
847 //  3. The section is just ".ctors" or ".dtors".
848 //
849 //  4. The section was contained in crtend, which contains an end marker.
850 //
851 // In an ideal world, we don't need this function because .init_array and
852 // .ctors are duplicate features (and .init_array is newer.) However, there
853 // are too many real-world use cases of .ctors, so we had no choice to
854 // support that with this rather ad-hoc semantics.
855 template <class ELFT>
856 static bool compCtors(const InputSection<ELFT> *A,
857                       const InputSection<ELFT> *B) {
858   bool BeginA = isCrtbegin(A->getFile()->getName());
859   bool BeginB = isCrtbegin(B->getFile()->getName());
860   if (BeginA != BeginB)
861     return BeginA;
862   bool EndA = isCrtend(A->getFile()->getName());
863   bool EndB = isCrtend(B->getFile()->getName());
864   if (EndA != EndB)
865     return EndB;
866   StringRef X = A->getSectionName();
867   StringRef Y = B->getSectionName();
868   assert(X.startswith(".ctors") || X.startswith(".dtors"));
869   assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
870   X = X.substr(6);
871   Y = Y.substr(6);
872   if (X.empty() && Y.empty())
873     return false;
874   return X < Y;
875 }
876 
877 // Sorts input sections by the special rules for .ctors and .dtors.
878 // Unfortunately, the rules are different from the one for .{init,fini}_array.
879 // Read the comment above.
880 template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
881   std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
882   reassignOffsets();
883 }
884 
885 static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
886   size_t I = 0;
887   for (; I + A.size() < Size; I += A.size())
888     memcpy(Buf + I, A.data(), A.size());
889   memcpy(Buf + I, A.data(), Size - I);
890 }
891 
892 template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
893   ArrayRef<uint8_t> Filler = Script->getFiller(this->Name);
894   if (!Filler.empty())
895     fill(Buf, this->getSize(), Filler);
896   if (Config->Threads) {
897     parallel_for_each(Sections.begin(), Sections.end(),
898                       [=](InputSection<ELFT> *C) { C->writeTo(Buf); });
899   } else {
900     for (InputSection<ELFT> *C : Sections)
901       C->writeTo(Buf);
902   }
903 }
904 
905 template <class ELFT>
906 EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t Type,
907                                        uintX_t Flags)
908     : OutputSectionBase<ELFT>(Name, Type, Flags) {
909   Out<ELFT>::EhFrameHdr->assignEhFrame(this);
910 }
911 
912 template <class ELFT>
913 EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
914     : S(S), Index(Index) {}
915 
916 template <class ELFT> StringRef EHRegion<ELFT>::data() const {
917   ArrayRef<uint8_t> SecData = S->getSectionData();
918   ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
919   size_t Start = Offsets[Index].first;
920   size_t End =
921       Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
922   return StringRef((const char *)SecData.data() + Start, End - Start);
923 }
924 
925 template <class ELFT>
926 Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
927     : EHRegion<ELFT>(S, Index) {}
928 
929 // Read a byte and advance D by one byte.
930 static uint8_t readByte(ArrayRef<uint8_t> &D) {
931   if (D.empty())
932     fatal("corrupted or unsupported CIE information");
933   uint8_t B = D.front();
934   D = D.slice(1);
935   return B;
936 }
937 
938 static void skipLeb128(ArrayRef<uint8_t> &D) {
939   while (!D.empty()) {
940     uint8_t Val = D.front();
941     D = D.slice(1);
942     if ((Val & 0x80) == 0)
943       return;
944   }
945   fatal("corrupted or unsupported CIE information");
946 }
947 
948 template <class ELFT> static size_t getAugPSize(unsigned Enc) {
949   switch (Enc & 0x0f) {
950   case DW_EH_PE_absptr:
951   case DW_EH_PE_signed:
952     return ELFT::Is64Bits ? 8 : 4;
953   case DW_EH_PE_udata2:
954   case DW_EH_PE_sdata2:
955     return 2;
956   case DW_EH_PE_udata4:
957   case DW_EH_PE_sdata4:
958     return 4;
959   case DW_EH_PE_udata8:
960   case DW_EH_PE_sdata8:
961     return 8;
962   }
963   fatal("unknown FDE encoding");
964 }
965 
966 template <class ELFT> static void skipAugP(ArrayRef<uint8_t> &D) {
967   uint8_t Enc = readByte(D);
968   if ((Enc & 0xf0) == DW_EH_PE_aligned)
969     fatal("DW_EH_PE_aligned encoding is not supported");
970   size_t Size = getAugPSize<ELFT>(Enc);
971   if (Size >= D.size())
972     fatal("corrupted CIE");
973   D = D.slice(Size);
974 }
975 
976 template <class ELFT>
977 uint8_t EHOutputSection<ELFT>::getFdeEncoding(ArrayRef<uint8_t> D) {
978   if (D.size() < 8)
979     fatal("CIE too small");
980   D = D.slice(8);
981 
982   uint8_t Version = readByte(D);
983   if (Version != 1 && Version != 3)
984     fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
985 
986   const unsigned char *AugEnd = std::find(D.begin() + 1, D.end(), '\0');
987   if (AugEnd == D.end())
988     fatal("corrupted CIE");
989   StringRef Aug(reinterpret_cast<const char *>(D.begin()), AugEnd - D.begin());
990   D = D.slice(Aug.size() + 1);
991 
992   // Code alignment factor should always be 1 for .eh_frame.
993   if (readByte(D) != 1)
994     fatal("CIE code alignment must be 1");
995 
996   // Skip data alignment factor.
997   skipLeb128(D);
998 
999   // Skip the return address register. In CIE version 1 this is a single
1000   // byte. In CIE version 3 this is an unsigned LEB128.
1001   if (Version == 1)
1002     readByte(D);
1003   else
1004     skipLeb128(D);
1005 
1006   // We only care about an 'R' value, but other records may precede an 'R'
1007   // record. Records are not in TLV (type-length-value) format, so we need
1008   // to teach the linker how to skip records for each type.
1009   for (char C : Aug) {
1010     if (C == 'R')
1011       return readByte(D);
1012     if (C == 'z') {
1013       skipLeb128(D);
1014       continue;
1015     }
1016     if (C == 'P') {
1017       skipAugP<ELFT>(D);
1018       continue;
1019     }
1020     if (C == 'L') {
1021       readByte(D);
1022       continue;
1023     }
1024     fatal("unknown .eh_frame augmentation string: " + Aug);
1025   }
1026   return DW_EH_PE_absptr;
1027 }
1028 
1029 template <class ELFT>
1030 static typename ELFFile<ELFT>::uintX_t readEntryLength(ArrayRef<uint8_t> D) {
1031   const endianness E = ELFT::TargetEndianness;
1032   if (D.size() < 4)
1033     fatal("CIE/FDE too small");
1034 
1035   // First 4 bytes of CIE/FDE is the size of the record.
1036   // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead.
1037   uint64_t V = read32<E>(D.data());
1038   if (V < UINT32_MAX) {
1039     uint64_t Len = V + 4;
1040     if (Len > D.size())
1041       fatal("CIE/FIE ends past the end of the section");
1042     return Len;
1043   }
1044 
1045   if (D.size() < 12)
1046     fatal("CIE/FDE too small");
1047   V = read64<E>(D.data() + 4);
1048   uint64_t Len = V + 12;
1049   if (Len < V || D.size() < Len)
1050     fatal("CIE/FIE ends past the end of the section");
1051   return Len;
1052 }
1053 
1054 template <class ELFT>
1055 template <class RelTy>
1056 void EHOutputSection<ELFT>::addSectionAux(EHInputSection<ELFT> *S,
1057                                           iterator_range<const RelTy *> Rels) {
1058   const endianness E = ELFT::TargetEndianness;
1059 
1060   S->OutSec = this;
1061   this->updateAlign(S->Align);
1062   Sections.push_back(S);
1063 
1064   ArrayRef<uint8_t> SecData = S->getSectionData();
1065   ArrayRef<uint8_t> D = SecData;
1066   uintX_t Offset = 0;
1067   auto RelI = Rels.begin();
1068   auto RelE = Rels.end();
1069 
1070   DenseMap<unsigned, unsigned> OffsetToIndex;
1071   while (!D.empty()) {
1072     unsigned Index = S->Offsets.size();
1073     S->Offsets.push_back(std::make_pair(Offset, -1));
1074 
1075     uintX_t Length = readEntryLength<ELFT>(D);
1076     // If CIE/FDE data length is zero then Length is 4, this
1077     // shall be considered a terminator and processing shall end.
1078     if (Length == 4)
1079       break;
1080     StringRef Entry((const char *)D.data(), Length);
1081 
1082     while (RelI != RelE && RelI->r_offset < Offset)
1083       ++RelI;
1084     uintX_t NextOffset = Offset + Length;
1085     bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
1086 
1087     uint32_t ID = read32<E>(D.data() + 4);
1088     if (ID == 0) {
1089       // CIE
1090       Cie<ELFT> C(S, Index);
1091       if (Config->EhFrameHdr)
1092         C.FdeEncoding = getFdeEncoding(D);
1093 
1094       SymbolBody *Personality = nullptr;
1095       if (HasReloc) {
1096         uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
1097         Personality = &S->getFile()->getSymbolBody(SymIndex).repl();
1098       }
1099 
1100       std::pair<StringRef, SymbolBody *> CieInfo(Entry, Personality);
1101       auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
1102       if (P.second) {
1103         Cies.push_back(C);
1104         this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
1105       }
1106       OffsetToIndex[Offset] = P.first->second;
1107     } else {
1108       if (!HasReloc)
1109         fatal("FDE doesn't reference another section");
1110       InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
1111       if (Target && Target->Live) {
1112         uint32_t CieOffset = Offset + 4 - ID;
1113         auto I = OffsetToIndex.find(CieOffset);
1114         if (I == OffsetToIndex.end())
1115           fatal("invalid CIE reference");
1116         Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
1117         Out<ELFT>::EhFrameHdr->reserveFde();
1118         this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
1119       }
1120     }
1121 
1122     Offset = NextOffset;
1123     D = D.slice(Length);
1124   }
1125 }
1126 
1127 template <class ELFT>
1128 void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1129   auto *S = cast<EHInputSection<ELFT>>(C);
1130   const Elf_Shdr *RelSec = S->RelocSection;
1131   if (!RelSec) {
1132     addSectionAux(S, make_range<const Elf_Rela *>(nullptr, nullptr));
1133     return;
1134   }
1135   ELFFile<ELFT> &Obj = S->getFile()->getObj();
1136   if (RelSec->sh_type == SHT_RELA)
1137     addSectionAux(S, Obj.relas(RelSec));
1138   else
1139     addSectionAux(S, Obj.rels(RelSec));
1140 }
1141 
1142 template <class ELFT>
1143 static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1144                                                             uint8_t *Buf) {
1145   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1146   const endianness E = ELFT::TargetEndianness;
1147   uint64_t Len = alignTo(Data.size(), sizeof(uintX_t));
1148   write32<E>(Buf, Len - 4);
1149   memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1150   return Len;
1151 }
1152 
1153 template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1154   const endianness E = ELFT::TargetEndianness;
1155   size_t Offset = 0;
1156   for (const Cie<ELFT> &C : Cies) {
1157     size_t CieOffset = Offset;
1158 
1159     uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
1160     C.S->Offsets[C.Index].second = Offset;
1161     Offset += CIELen;
1162 
1163     for (const EHRegion<ELFT> &F : C.Fdes) {
1164       uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
1165       write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
1166       F.S->Offsets[F.Index].second = Offset;
1167       Out<ELFT>::EhFrameHdr->addFde(C.FdeEncoding, Offset, Buf + Offset + 8);
1168       Offset += Len;
1169     }
1170   }
1171 
1172   for (EHInputSection<ELFT> *S : Sections) {
1173     const Elf_Shdr *RelSec = S->RelocSection;
1174     if (!RelSec)
1175       continue;
1176     ELFFile<ELFT> &EObj = S->getFile()->getObj();
1177     if (RelSec->sh_type == SHT_RELA)
1178       S->relocate(Buf, nullptr, EObj.relas(RelSec));
1179     else
1180       S->relocate(Buf, nullptr, EObj.rels(RelSec));
1181   }
1182 }
1183 
1184 template <class ELFT>
1185 MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
1186                                              uintX_t Flags, uintX_t Alignment)
1187     : OutputSectionBase<ELFT>(Name, Type, Flags),
1188       Builder(llvm::StringTableBuilder::RAW, Alignment) {}
1189 
1190 template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1191   if (shouldTailMerge()) {
1192     StringRef Data = Builder.data();
1193     memcpy(Buf, Data.data(), Data.size());
1194     return;
1195   }
1196   for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1197     StringRef Data = P.first;
1198     memcpy(Buf + P.second, Data.data(), Data.size());
1199   }
1200 }
1201 
1202 static size_t findNull(StringRef S, size_t EntSize) {
1203   // Optimize the common case.
1204   if (EntSize == 1)
1205     return S.find(0);
1206 
1207   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1208     const char *B = S.begin() + I;
1209     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1210       return I;
1211   }
1212   return StringRef::npos;
1213 }
1214 
1215 template <class ELFT>
1216 void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1217   auto *S = cast<MergeInputSection<ELFT>>(C);
1218   S->OutSec = this;
1219   this->updateAlign(S->Align);
1220 
1221   ArrayRef<uint8_t> D = S->getSectionData();
1222   StringRef Data((const char *)D.data(), D.size());
1223   uintX_t EntSize = S->getSectionHdr()->sh_entsize;
1224 
1225   // If this is of type string, the contents are null-terminated strings.
1226   if (this->Header.sh_flags & SHF_STRINGS) {
1227     uintX_t Offset = 0;
1228     while (!Data.empty()) {
1229       size_t End = findNull(Data, EntSize);
1230       if (End == StringRef::npos)
1231         fatal("string is not null terminated");
1232       StringRef Entry = Data.substr(0, End + EntSize);
1233       uintX_t OutputOffset = Builder.add(Entry);
1234       if (shouldTailMerge())
1235         OutputOffset = -1;
1236       S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1237       uintX_t Size = End + EntSize;
1238       Data = Data.substr(Size);
1239       Offset += Size;
1240     }
1241     return;
1242   }
1243 
1244   // If this is not of type string, every entry has the same size.
1245   for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1246     StringRef Entry = Data.substr(I, EntSize);
1247     size_t OutputOffset = Builder.add(Entry);
1248     S->Offsets.push_back(std::make_pair(I, OutputOffset));
1249   }
1250 }
1251 
1252 template <class ELFT>
1253 unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1254   return Builder.getOffset(Val);
1255 }
1256 
1257 template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1258   return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1259 }
1260 
1261 template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1262   if (shouldTailMerge())
1263     Builder.finalize();
1264   this->Header.sh_size = Builder.getSize();
1265 }
1266 
1267 template <class ELFT>
1268 StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
1269     : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1270                               Dynamic ? (uintX_t)SHF_ALLOC : 0),
1271       Dynamic(Dynamic) {
1272   this->Header.sh_addralign = 1;
1273 }
1274 
1275 // Adds a string to the string table. If HashIt is true we hash and check for
1276 // duplicates. It is optional because the name of global symbols are already
1277 // uniqued and hashing them again has a big cost for a small value: uniquing
1278 // them with some other string that happens to be the same.
1279 template <class ELFT>
1280 unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1281   if (HashIt) {
1282     auto R = StringMap.insert(std::make_pair(S, Size));
1283     if (!R.second)
1284       return R.first->second;
1285   }
1286   unsigned Ret = Size;
1287   Size += S.size() + 1;
1288   Strings.push_back(S);
1289   return Ret;
1290 }
1291 
1292 template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
1293   // ELF string tables start with NUL byte, so advance the pointer by one.
1294   ++Buf;
1295   for (StringRef S : Strings) {
1296     memcpy(Buf, S.data(), S.size());
1297     Buf += S.size() + 1;
1298   }
1299 }
1300 
1301 template <class ELFT>
1302 SymbolTableSection<ELFT>::SymbolTableSection(
1303     SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
1304     : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1305                               StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1306                               StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
1307       StrTabSec(StrTabSec), Table(Table) {
1308   this->Header.sh_entsize = sizeof(Elf_Sym);
1309   this->Header.sh_addralign = sizeof(uintX_t);
1310 }
1311 
1312 // Orders symbols according to their positions in the GOT,
1313 // in compliance with MIPS ABI rules.
1314 // See "Global Offset Table" in Chapter 5 in the following document
1315 // for detailed description:
1316 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1317 static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1318                             const std::pair<SymbolBody *, unsigned> &R) {
1319   if (!L.first->isInGot() || !R.first->isInGot())
1320     return R.first->isInGot();
1321   return L.first->GotIndex < R.first->GotIndex;
1322 }
1323 
1324 template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
1325   if (this->Header.sh_size)
1326     return; // Already finalized.
1327 
1328   this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
1329   this->Header.sh_link = StrTabSec.SectionIndex;
1330   this->Header.sh_info = NumLocals + 1;
1331 
1332   if (Config->Relocatable) {
1333     size_t I = NumLocals;
1334     for (const std::pair<SymbolBody *, size_t> &P : Symbols)
1335       P.first->DynsymIndex = ++I;
1336     return;
1337   }
1338 
1339   if (!StrTabSec.isDynamic()) {
1340     std::stable_sort(Symbols.begin(), Symbols.end(),
1341                      [](const std::pair<SymbolBody *, unsigned> &L,
1342                         const std::pair<SymbolBody *, unsigned> &R) {
1343                        return getSymbolBinding(L.first) == STB_LOCAL &&
1344                               getSymbolBinding(R.first) != STB_LOCAL;
1345                      });
1346     return;
1347   }
1348   if (Out<ELFT>::GnuHashTab)
1349     // NB: It also sorts Symbols to meet the GNU hash table requirements.
1350     Out<ELFT>::GnuHashTab->addSymbols(Symbols);
1351   else if (Config->EMachine == EM_MIPS)
1352     std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
1353   size_t I = 0;
1354   for (const std::pair<SymbolBody *, size_t> &P : Symbols)
1355     P.first->DynsymIndex = ++I;
1356 }
1357 
1358 template <class ELFT>
1359 void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1360   Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
1361 }
1362 
1363 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1364   Buf += sizeof(Elf_Sym);
1365 
1366   // All symbols with STB_LOCAL binding precede the weak and global symbols.
1367   // .dynsym only contains global symbols.
1368   if (!Config->DiscardAll && !StrTabSec.isDynamic())
1369     writeLocalSymbols(Buf);
1370 
1371   writeGlobalSymbols(Buf);
1372 }
1373 
1374 template <class ELFT>
1375 void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1376   // Iterate over all input object files to copy their local symbols
1377   // to the output symbol table pointed by Buf.
1378   for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
1379     for (const std::pair<const Elf_Sym *, size_t> &P : File->KeptLocalSyms) {
1380       const Elf_Sym *Sym = P.first;
1381 
1382       auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1383       uintX_t VA = 0;
1384       if (Sym->st_shndx == SHN_ABS) {
1385         ESym->st_shndx = SHN_ABS;
1386         VA = Sym->st_value;
1387       } else {
1388         InputSectionBase<ELFT> *Section = File->getSection(*Sym);
1389         const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1390         ESym->st_shndx = OutSec->SectionIndex;
1391         VA = Section->getOffset(*Sym);
1392         VA += OutSec->getVA();
1393       }
1394       ESym->st_name = P.second;
1395       ESym->st_size = Sym->st_size;
1396       ESym->setBindingAndType(Sym->getBinding(), Sym->getType());
1397       ESym->st_value = VA;
1398       Buf += sizeof(*ESym);
1399     }
1400   }
1401 }
1402 
1403 template <class ELFT>
1404 static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1405 getElfSym(SymbolBody &Body) {
1406   if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
1407     return &EBody->Sym;
1408   if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1409     return &EBody->Sym;
1410   return nullptr;
1411 }
1412 
1413 template <class ELFT>
1414 void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
1415   // Write the internal symbol table contents to the output symbol table
1416   // pointed by Buf.
1417   auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1418   for (const std::pair<SymbolBody *, size_t> &P : Symbols) {
1419     SymbolBody *Body = P.first;
1420     size_t StrOff = P.second;
1421 
1422     uint8_t Type = STT_NOTYPE;
1423     uintX_t Size = 0;
1424     if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1425       Type = InputSym->getType();
1426       Size = InputSym->st_size;
1427     } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1428       Type = STT_OBJECT;
1429       Size = C->Size;
1430     }
1431 
1432     ESym->setBindingAndType(getSymbolBinding(Body), Type);
1433     ESym->st_size = Size;
1434     ESym->st_name = StrOff;
1435     ESym->setVisibility(Body->getVisibility());
1436     ESym->st_value = Body->getVA<ELFT>();
1437 
1438     if (const OutputSectionBase<ELFT> *OutSec = getOutputSection(Body))
1439       ESym->st_shndx = OutSec->SectionIndex;
1440     else if (isa<DefinedRegular<ELFT>>(Body))
1441       ESym->st_shndx = SHN_ABS;
1442 
1443     // On MIPS we need to mark symbol which has a PLT entry and requires pointer
1444     // equality by STO_MIPS_PLT flag. That is necessary to help dynamic linker
1445     // distinguish such symbols and MIPS lazy-binding stubs.
1446     // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1447     if (Config->EMachine == EM_MIPS && Body->isInPlt() &&
1448         Body->NeedsCopyOrPltAddr)
1449       ESym->st_other |= STO_MIPS_PLT;
1450     ++ESym;
1451   }
1452 }
1453 
1454 template <class ELFT>
1455 const OutputSectionBase<ELFT> *
1456 SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1457   switch (Sym->kind()) {
1458   case SymbolBody::DefinedSyntheticKind:
1459     return &cast<DefinedSynthetic<ELFT>>(Sym)->Section;
1460   case SymbolBody::DefinedRegularKind: {
1461     auto &D = cast<DefinedRegular<ELFT>>(Sym->repl());
1462     if (D.Section)
1463       return D.Section->OutSec;
1464     break;
1465   }
1466   case SymbolBody::DefinedCommonKind:
1467     return Out<ELFT>::Bss;
1468   case SymbolBody::SharedKind:
1469     if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1470       return Out<ELFT>::Bss;
1471     break;
1472   case SymbolBody::UndefinedElfKind:
1473   case SymbolBody::UndefinedKind:
1474   case SymbolBody::LazyKind:
1475     break;
1476   case SymbolBody::DefinedBitcodeKind:
1477     llvm_unreachable("should have been replaced");
1478   }
1479   return nullptr;
1480 }
1481 
1482 template <class ELFT>
1483 uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
1484   uint8_t Visibility = Body->getVisibility();
1485   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1486     return STB_LOCAL;
1487   if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1488     return ESym->getBinding();
1489   if (isa<DefinedSynthetic<ELFT>>(Body))
1490     return STB_LOCAL;
1491   return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1492 }
1493 
1494 template <class ELFT>
1495 BuildIdSection<ELFT>::BuildIdSection()
1496     : OutputSectionBase<ELFT>(".note.gnu.build-id", SHT_NOTE, SHF_ALLOC) {
1497   // 16 bytes for the note section header and 8 bytes for FNV1 hash.
1498   this->Header.sh_size = 24;
1499 }
1500 
1501 template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
1502   const endianness E = ELFT::TargetEndianness;
1503   write32<E>(Buf, 4);                   // Name size
1504   write32<E>(Buf + 4, sizeof(Hash));    // Content size
1505   write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
1506   memcpy(Buf + 12, "GNU", 4);           // Name string
1507   HashBuf = Buf + 16;
1508 }
1509 
1510 template <class ELFT> void BuildIdSection<ELFT>::update(ArrayRef<uint8_t> Buf) {
1511   // 64-bit FNV1 hash
1512   const uint64_t Prime = 0x100000001b3;
1513   for (uint8_t B : Buf) {
1514     Hash *= Prime;
1515     Hash ^= B;
1516   }
1517 }
1518 
1519 template <class ELFT> void BuildIdSection<ELFT>::writeBuildId() {
1520   const endianness E = ELFT::TargetEndianness;
1521   write64<E>(HashBuf, Hash);
1522 }
1523 
1524 template <class ELFT>
1525 MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1526     : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1527   this->Header.sh_addralign = 4;
1528   this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1529   this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1530 }
1531 
1532 template <class ELFT>
1533 void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1534   auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1535   R->ri_gp_value = getMipsGpAddr<ELFT>();
1536   R->ri_gprmask = GprMask;
1537 }
1538 
1539 template <class ELFT>
1540 void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1541   // Copy input object file's .reginfo gprmask to output.
1542   auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
1543   GprMask |= S->Reginfo->ri_gprmask;
1544 }
1545 
1546 namespace lld {
1547 namespace elf {
1548 template class OutputSectionBase<ELF32LE>;
1549 template class OutputSectionBase<ELF32BE>;
1550 template class OutputSectionBase<ELF64LE>;
1551 template class OutputSectionBase<ELF64BE>;
1552 
1553 template class EhFrameHeader<ELF32LE>;
1554 template class EhFrameHeader<ELF32BE>;
1555 template class EhFrameHeader<ELF64LE>;
1556 template class EhFrameHeader<ELF64BE>;
1557 
1558 template class GotPltSection<ELF32LE>;
1559 template class GotPltSection<ELF32BE>;
1560 template class GotPltSection<ELF64LE>;
1561 template class GotPltSection<ELF64BE>;
1562 
1563 template class GotSection<ELF32LE>;
1564 template class GotSection<ELF32BE>;
1565 template class GotSection<ELF64LE>;
1566 template class GotSection<ELF64BE>;
1567 
1568 template class PltSection<ELF32LE>;
1569 template class PltSection<ELF32BE>;
1570 template class PltSection<ELF64LE>;
1571 template class PltSection<ELF64BE>;
1572 
1573 template class RelocationSection<ELF32LE>;
1574 template class RelocationSection<ELF32BE>;
1575 template class RelocationSection<ELF64LE>;
1576 template class RelocationSection<ELF64BE>;
1577 
1578 template class InterpSection<ELF32LE>;
1579 template class InterpSection<ELF32BE>;
1580 template class InterpSection<ELF64LE>;
1581 template class InterpSection<ELF64BE>;
1582 
1583 template class GnuHashTableSection<ELF32LE>;
1584 template class GnuHashTableSection<ELF32BE>;
1585 template class GnuHashTableSection<ELF64LE>;
1586 template class GnuHashTableSection<ELF64BE>;
1587 
1588 template class HashTableSection<ELF32LE>;
1589 template class HashTableSection<ELF32BE>;
1590 template class HashTableSection<ELF64LE>;
1591 template class HashTableSection<ELF64BE>;
1592 
1593 template class DynamicSection<ELF32LE>;
1594 template class DynamicSection<ELF32BE>;
1595 template class DynamicSection<ELF64LE>;
1596 template class DynamicSection<ELF64BE>;
1597 
1598 template class OutputSection<ELF32LE>;
1599 template class OutputSection<ELF32BE>;
1600 template class OutputSection<ELF64LE>;
1601 template class OutputSection<ELF64BE>;
1602 
1603 template class EHOutputSection<ELF32LE>;
1604 template class EHOutputSection<ELF32BE>;
1605 template class EHOutputSection<ELF64LE>;
1606 template class EHOutputSection<ELF64BE>;
1607 
1608 template class MipsReginfoOutputSection<ELF32LE>;
1609 template class MipsReginfoOutputSection<ELF32BE>;
1610 template class MipsReginfoOutputSection<ELF64LE>;
1611 template class MipsReginfoOutputSection<ELF64BE>;
1612 
1613 template class MergeOutputSection<ELF32LE>;
1614 template class MergeOutputSection<ELF32BE>;
1615 template class MergeOutputSection<ELF64LE>;
1616 template class MergeOutputSection<ELF64BE>;
1617 
1618 template class StringTableSection<ELF32LE>;
1619 template class StringTableSection<ELF32BE>;
1620 template class StringTableSection<ELF64LE>;
1621 template class StringTableSection<ELF64BE>;
1622 
1623 template class SymbolTableSection<ELF32LE>;
1624 template class SymbolTableSection<ELF32BE>;
1625 template class SymbolTableSection<ELF64LE>;
1626 template class SymbolTableSection<ELF64BE>;
1627 
1628 template class BuildIdSection<ELF32LE>;
1629 template class BuildIdSection<ELF32BE>;
1630 template class BuildIdSection<ELF64LE>;
1631 template class BuildIdSection<ELF64BE>;
1632 }
1633 }
1634