]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/SyntheticSections.cpp
Merge ^/head r320042 through r320397.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / SyntheticSections.cpp
1 //===- SyntheticSections.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 // This file contains linker-synthesized sections. Currently,
11 // synthetic sections are created either output sections or input sections,
12 // but we are rewriting code so that all synthetic sections are created as
13 // input sections.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "SyntheticSections.h"
18 #include "Config.h"
19 #include "Error.h"
20 #include "InputFiles.h"
21 #include "LinkerScript.h"
22 #include "Memory.h"
23 #include "OutputSections.h"
24 #include "Strings.h"
25 #include "SymbolTable.h"
26 #include "Target.h"
27 #include "Threads.h"
28 #include "Writer.h"
29 #include "lld/Config/Version.h"
30 #include "llvm/BinaryFormat/Dwarf.h"
31 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
32 #include "llvm/Object/Decompressor.h"
33 #include "llvm/Object/ELFObjectFile.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/MD5.h"
36 #include "llvm/Support/RandomNumberGenerator.h"
37 #include "llvm/Support/SHA1.h"
38 #include "llvm/Support/xxhash.h"
39 #include <cstdlib>
40
41 using namespace llvm;
42 using namespace llvm::dwarf;
43 using namespace llvm::ELF;
44 using namespace llvm::object;
45 using namespace llvm::support;
46 using namespace llvm::support::endian;
47
48 using namespace lld;
49 using namespace lld::elf;
50
51 uint64_t SyntheticSection::getVA() const {
52   if (OutputSection *Sec = getParent())
53     return Sec->Addr + OutSecOff;
54   return 0;
55 }
56
57 template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() {
58   std::vector<DefinedCommon *> V;
59   for (Symbol *S : Symtab<ELFT>::X->getSymbols())
60     if (auto *B = dyn_cast<DefinedCommon>(S->body()))
61       V.push_back(B);
62   return V;
63 }
64
65 // Find all common symbols and allocate space for them.
66 template <class ELFT> InputSection *elf::createCommonSection() {
67   if (!Config->DefineCommon)
68     return nullptr;
69
70   // Sort the common symbols by alignment as an heuristic to pack them better.
71   std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>();
72   if (Syms.empty())
73     return nullptr;
74
75   std::stable_sort(Syms.begin(), Syms.end(),
76                    [](const DefinedCommon *A, const DefinedCommon *B) {
77                      return A->Alignment > B->Alignment;
78                    });
79
80   BssSection *Sec = make<BssSection>("COMMON");
81   for (DefinedCommon *Sym : Syms)
82     Sym->Offset = Sec->reserveSpace(Sym->Size, Sym->Alignment);
83   return Sec;
84 }
85
86 // Returns an LLD version string.
87 static ArrayRef<uint8_t> getVersion() {
88   // Check LLD_VERSION first for ease of testing.
89   // You can get consitent output by using the environment variable.
90   // This is only for testing.
91   StringRef S = getenv("LLD_VERSION");
92   if (S.empty())
93     S = Saver.save(Twine("Linker: ") + getLLDVersion());
94
95   // +1 to include the terminating '\0'.
96   return {(const uint8_t *)S.data(), S.size() + 1};
97 }
98
99 // Creates a .comment section containing LLD version info.
100 // With this feature, you can identify LLD-generated binaries easily
101 // by "readelf --string-dump .comment <file>".
102 // The returned object is a mergeable string section.
103 template <class ELFT> MergeInputSection *elf::createCommentSection() {
104   typename ELFT::Shdr Hdr = {};
105   Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
106   Hdr.sh_type = SHT_PROGBITS;
107   Hdr.sh_entsize = 1;
108   Hdr.sh_addralign = 1;
109
110   auto *Ret =
111       make<MergeInputSection>((ObjectFile<ELFT> *)nullptr, &Hdr, ".comment");
112   Ret->Data = getVersion();
113   Ret->splitIntoPieces();
114   return Ret;
115 }
116
117 // .MIPS.abiflags section.
118 template <class ELFT>
119 MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags)
120     : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
121       Flags(Flags) {
122   this->Entsize = sizeof(Elf_Mips_ABIFlags);
123 }
124
125 template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
126   memcpy(Buf, &Flags, sizeof(Flags));
127 }
128
129 template <class ELFT>
130 MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
131   Elf_Mips_ABIFlags Flags = {};
132   bool Create = false;
133
134   for (InputSectionBase *Sec : InputSections) {
135     if (Sec->Type != SHT_MIPS_ABIFLAGS)
136       continue;
137     Sec->Live = false;
138     Create = true;
139
140     std::string Filename = toString(Sec->getFile<ELFT>());
141     const size_t Size = Sec->Data.size();
142     // Older version of BFD (such as the default FreeBSD linker) concatenate
143     // .MIPS.abiflags instead of merging. To allow for this case (or potential
144     // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
145     if (Size < sizeof(Elf_Mips_ABIFlags)) {
146       error(Filename + ": invalid size of .MIPS.abiflags section: got " +
147             Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
148       return nullptr;
149     }
150     auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
151     if (S->version != 0) {
152       error(Filename + ": unexpected .MIPS.abiflags version " +
153             Twine(S->version));
154       return nullptr;
155     }
156
157     // LLD checks ISA compatibility in getMipsEFlags(). Here we just
158     // select the highest number of ISA/Rev/Ext.
159     Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
160     Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
161     Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
162     Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
163     Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
164     Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
165     Flags.ases |= S->ases;
166     Flags.flags1 |= S->flags1;
167     Flags.flags2 |= S->flags2;
168     Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
169   };
170
171   if (Create)
172     return make<MipsAbiFlagsSection<ELFT>>(Flags);
173   return nullptr;
174 }
175
176 // .MIPS.options section.
177 template <class ELFT>
178 MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo)
179     : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
180       Reginfo(Reginfo) {
181   this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
182 }
183
184 template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
185   auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
186   Options->kind = ODK_REGINFO;
187   Options->size = getSize();
188
189   if (!Config->Relocatable)
190     Reginfo.ri_gp_value = InX::MipsGot->getGp();
191   memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
192 }
193
194 template <class ELFT>
195 MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
196   // N64 ABI only.
197   if (!ELFT::Is64Bits)
198     return nullptr;
199
200   Elf_Mips_RegInfo Reginfo = {};
201   bool Create = false;
202
203   for (InputSectionBase *Sec : InputSections) {
204     if (Sec->Type != SHT_MIPS_OPTIONS)
205       continue;
206     Sec->Live = false;
207     Create = true;
208
209     std::string Filename = toString(Sec->getFile<ELFT>());
210     ArrayRef<uint8_t> D = Sec->Data;
211
212     while (!D.empty()) {
213       if (D.size() < sizeof(Elf_Mips_Options)) {
214         error(Filename + ": invalid size of .MIPS.options section");
215         break;
216       }
217
218       auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
219       if (Opt->kind == ODK_REGINFO) {
220         if (Config->Relocatable && Opt->getRegInfo().ri_gp_value)
221           error(Filename + ": unsupported non-zero ri_gp_value");
222         Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
223         Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
224         break;
225       }
226
227       if (!Opt->size)
228         fatal(Filename + ": zero option descriptor size");
229       D = D.slice(Opt->size);
230     }
231   };
232
233   if (Create)
234     return make<MipsOptionsSection<ELFT>>(Reginfo);
235   return nullptr;
236 }
237
238 // MIPS .reginfo section.
239 template <class ELFT>
240 MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo)
241     : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
242       Reginfo(Reginfo) {
243   this->Entsize = sizeof(Elf_Mips_RegInfo);
244 }
245
246 template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
247   if (!Config->Relocatable)
248     Reginfo.ri_gp_value = InX::MipsGot->getGp();
249   memcpy(Buf, &Reginfo, sizeof(Reginfo));
250 }
251
252 template <class ELFT>
253 MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
254   // Section should be alive for O32 and N32 ABIs only.
255   if (ELFT::Is64Bits)
256     return nullptr;
257
258   Elf_Mips_RegInfo Reginfo = {};
259   bool Create = false;
260
261   for (InputSectionBase *Sec : InputSections) {
262     if (Sec->Type != SHT_MIPS_REGINFO)
263       continue;
264     Sec->Live = false;
265     Create = true;
266
267     if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
268       error(toString(Sec->getFile<ELFT>()) +
269             ": invalid size of .reginfo section");
270       return nullptr;
271     }
272     auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
273     if (Config->Relocatable && R->ri_gp_value)
274       error(toString(Sec->getFile<ELFT>()) +
275             ": unsupported non-zero ri_gp_value");
276
277     Reginfo.ri_gprmask |= R->ri_gprmask;
278     Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
279   };
280
281   if (Create)
282     return make<MipsReginfoSection<ELFT>>(Reginfo);
283   return nullptr;
284 }
285
286 InputSection *elf::createInterpSection() {
287   // StringSaver guarantees that the returned string ends with '\0'.
288   StringRef S = Saver.save(Config->DynamicLinker);
289   ArrayRef<uint8_t> Contents = {(const uint8_t *)S.data(), S.size() + 1};
290
291   auto *Sec =
292       make<InputSection>(SHF_ALLOC, SHT_PROGBITS, 1, Contents, ".interp");
293   Sec->Live = true;
294   return Sec;
295 }
296
297 SymbolBody *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
298                                    uint64_t Size, InputSectionBase *Section) {
299   auto *S = make<DefinedRegular>(Name, /*IsLocal*/ true, STV_DEFAULT, Type,
300                                  Value, Size, Section, nullptr);
301   if (InX::SymTab)
302     InX::SymTab->addSymbol(S);
303   return S;
304 }
305
306 static size_t getHashSize() {
307   switch (Config->BuildId) {
308   case BuildIdKind::Fast:
309     return 8;
310   case BuildIdKind::Md5:
311   case BuildIdKind::Uuid:
312     return 16;
313   case BuildIdKind::Sha1:
314     return 20;
315   case BuildIdKind::Hexstring:
316     return Config->BuildIdVector.size();
317   default:
318     llvm_unreachable("unknown BuildIdKind");
319   }
320 }
321
322 BuildIdSection::BuildIdSection()
323     : SyntheticSection(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"),
324       HashSize(getHashSize()) {}
325
326 void BuildIdSection::writeTo(uint8_t *Buf) {
327   endianness E = Config->Endianness;
328   write32(Buf, 4, E);                   // Name size
329   write32(Buf + 4, HashSize, E);        // Content size
330   write32(Buf + 8, NT_GNU_BUILD_ID, E); // Type
331   memcpy(Buf + 12, "GNU", 4);           // Name string
332   HashBuf = Buf + 16;
333 }
334
335 // Split one uint8 array into small pieces of uint8 arrays.
336 static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
337                                             size_t ChunkSize) {
338   std::vector<ArrayRef<uint8_t>> Ret;
339   while (Arr.size() > ChunkSize) {
340     Ret.push_back(Arr.take_front(ChunkSize));
341     Arr = Arr.drop_front(ChunkSize);
342   }
343   if (!Arr.empty())
344     Ret.push_back(Arr);
345   return Ret;
346 }
347
348 // Computes a hash value of Data using a given hash function.
349 // In order to utilize multiple cores, we first split data into 1MB
350 // chunks, compute a hash for each chunk, and then compute a hash value
351 // of the hash values.
352 void BuildIdSection::computeHash(
353     llvm::ArrayRef<uint8_t> Data,
354     std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) {
355   std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024);
356   std::vector<uint8_t> Hashes(Chunks.size() * HashSize);
357
358   // Compute hash values.
359   parallelForEachN(0, Chunks.size(), [&](size_t I) {
360     HashFn(Hashes.data() + I * HashSize, Chunks[I]);
361   });
362
363   // Write to the final output buffer.
364   HashFn(HashBuf, Hashes);
365 }
366
367 BssSection::BssSection(StringRef Name)
368     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 0, Name) {}
369
370 size_t BssSection::reserveSpace(uint64_t Size, uint32_t Alignment) {
371   if (OutputSection *Sec = getParent())
372     Sec->updateAlignment(Alignment);
373   this->Size = alignTo(this->Size, Alignment) + Size;
374   this->Alignment = std::max(this->Alignment, Alignment);
375   return this->Size - Size;
376 }
377
378 void BuildIdSection::writeBuildId(ArrayRef<uint8_t> Buf) {
379   switch (Config->BuildId) {
380   case BuildIdKind::Fast:
381     computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
382       write64le(Dest, xxHash64(toStringRef(Arr)));
383     });
384     break;
385   case BuildIdKind::Md5:
386     computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
387       memcpy(Dest, MD5::hash(Arr).data(), 16);
388     });
389     break;
390   case BuildIdKind::Sha1:
391     computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
392       memcpy(Dest, SHA1::hash(Arr).data(), 20);
393     });
394     break;
395   case BuildIdKind::Uuid:
396     if (getRandomBytes(HashBuf, HashSize))
397       error("entropy source failure");
398     break;
399   case BuildIdKind::Hexstring:
400     memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size());
401     break;
402   default:
403     llvm_unreachable("unknown BuildIdKind");
404   }
405 }
406
407 template <class ELFT>
408 EhFrameSection<ELFT>::EhFrameSection()
409     : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
410
411 // Search for an existing CIE record or create a new one.
412 // CIE records from input object files are uniquified by their contents
413 // and where their relocations point to.
414 template <class ELFT>
415 template <class RelTy>
416 CieRecord *EhFrameSection<ELFT>::addCie(EhSectionPiece &Piece,
417                                         ArrayRef<RelTy> Rels) {
418   auto *Sec = cast<EhInputSection>(Piece.ID);
419   const endianness E = ELFT::TargetEndianness;
420   if (read32<E>(Piece.data().data() + 4) != 0)
421     fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
422
423   SymbolBody *Personality = nullptr;
424   unsigned FirstRelI = Piece.FirstRelocation;
425   if (FirstRelI != (unsigned)-1)
426     Personality =
427         &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
428
429   // Search for an existing CIE by CIE contents/relocation target pair.
430   CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
431
432   // If not found, create a new one.
433   if (Cie->Piece == nullptr) {
434     Cie->Piece = &Piece;
435     Cies.push_back(Cie);
436   }
437   return Cie;
438 }
439
440 // There is one FDE per function. Returns true if a given FDE
441 // points to a live function.
442 template <class ELFT>
443 template <class RelTy>
444 bool EhFrameSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
445                                      ArrayRef<RelTy> Rels) {
446   auto *Sec = cast<EhInputSection>(Piece.ID);
447   unsigned FirstRelI = Piece.FirstRelocation;
448   if (FirstRelI == (unsigned)-1)
449     return false;
450   const RelTy &Rel = Rels[FirstRelI];
451   SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
452   auto *D = dyn_cast<DefinedRegular>(&B);
453   if (!D || !D->Section)
454     return false;
455   auto *Target =
456       cast<InputSectionBase>(cast<InputSectionBase>(D->Section)->Repl);
457   return Target && Target->Live;
458 }
459
460 // .eh_frame is a sequence of CIE or FDE records. In general, there
461 // is one CIE record per input object file which is followed by
462 // a list of FDEs. This function searches an existing CIE or create a new
463 // one and associates FDEs to the CIE.
464 template <class ELFT>
465 template <class RelTy>
466 void EhFrameSection<ELFT>::addSectionAux(EhInputSection *Sec,
467                                          ArrayRef<RelTy> Rels) {
468   const endianness E = ELFT::TargetEndianness;
469
470   DenseMap<size_t, CieRecord *> OffsetToCie;
471   for (EhSectionPiece &Piece : Sec->Pieces) {
472     // The empty record is the end marker.
473     if (Piece.size() == 4)
474       return;
475
476     size_t Offset = Piece.InputOff;
477     uint32_t ID = read32<E>(Piece.data().data() + 4);
478     if (ID == 0) {
479       OffsetToCie[Offset] = addCie(Piece, Rels);
480       continue;
481     }
482
483     uint32_t CieOffset = Offset + 4 - ID;
484     CieRecord *Cie = OffsetToCie[CieOffset];
485     if (!Cie)
486       fatal(toString(Sec) + ": invalid CIE reference");
487
488     if (!isFdeLive(Piece, Rels))
489       continue;
490     Cie->FdePieces.push_back(&Piece);
491     NumFdes++;
492   }
493 }
494
495 template <class ELFT>
496 void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
497   auto *Sec = cast<EhInputSection>(C);
498   Sec->Parent = this;
499   updateAlignment(Sec->Alignment);
500   Sections.push_back(Sec);
501   for (auto *DS : Sec->DependentSections)
502     DependentSections.push_back(DS);
503
504   // .eh_frame is a sequence of CIE or FDE records. This function
505   // splits it into pieces so that we can call
506   // SplitInputSection::getSectionPiece on the section.
507   Sec->split<ELFT>();
508   if (Sec->Pieces.empty())
509     return;
510
511   if (Sec->NumRelocations) {
512     if (Sec->AreRelocsRela)
513       addSectionAux(Sec, Sec->template relas<ELFT>());
514     else
515       addSectionAux(Sec, Sec->template rels<ELFT>());
516     return;
517   }
518   addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
519 }
520
521 template <class ELFT>
522 static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
523   memcpy(Buf, D.data(), D.size());
524
525   // Fix the size field. -4 since size does not include the size field itself.
526   const endianness E = ELFT::TargetEndianness;
527   write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
528 }
529
530 template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
531   if (this->Size)
532     return; // Already finalized.
533
534   size_t Off = 0;
535   for (CieRecord *Cie : Cies) {
536     Cie->Piece->OutputOff = Off;
537     Off += alignTo(Cie->Piece->size(), Config->Wordsize);
538
539     for (EhSectionPiece *Fde : Cie->FdePieces) {
540       Fde->OutputOff = Off;
541       Off += alignTo(Fde->size(), Config->Wordsize);
542     }
543   }
544
545   // The LSB standard does not allow a .eh_frame section with zero
546   // Call Frame Information records. Therefore add a CIE record length
547   // 0 as a terminator if this .eh_frame section is empty.
548   if (Off == 0)
549     Off = 4;
550
551   this->Size = Off;
552 }
553
554 template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
555   const endianness E = ELFT::TargetEndianness;
556   switch (Size) {
557   case DW_EH_PE_udata2:
558     return read16<E>(Buf);
559   case DW_EH_PE_udata4:
560     return read32<E>(Buf);
561   case DW_EH_PE_udata8:
562     return read64<E>(Buf);
563   case DW_EH_PE_absptr:
564     if (ELFT::Is64Bits)
565       return read64<E>(Buf);
566     return read32<E>(Buf);
567   }
568   fatal("unknown FDE size encoding");
569 }
570
571 // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
572 // We need it to create .eh_frame_hdr section.
573 template <class ELFT>
574 uint64_t EhFrameSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
575                                         uint8_t Enc) {
576   // The starting address to which this FDE applies is
577   // stored at FDE + 8 byte.
578   size_t Off = FdeOff + 8;
579   uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
580   if ((Enc & 0x70) == DW_EH_PE_absptr)
581     return Addr;
582   if ((Enc & 0x70) == DW_EH_PE_pcrel)
583     return Addr + getParent()->Addr + Off;
584   fatal("unknown FDE size relative encoding");
585 }
586
587 template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
588   const endianness E = ELFT::TargetEndianness;
589   for (CieRecord *Cie : Cies) {
590     size_t CieOffset = Cie->Piece->OutputOff;
591     writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
592
593     for (EhSectionPiece *Fde : Cie->FdePieces) {
594       size_t Off = Fde->OutputOff;
595       writeCieFde<ELFT>(Buf + Off, Fde->data());
596
597       // FDE's second word should have the offset to an associated CIE.
598       // Write it.
599       write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
600     }
601   }
602
603   for (EhInputSection *S : Sections)
604     S->relocateAlloc(Buf, nullptr);
605
606   // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
607   // to get a FDE from an address to which FDE is applied. So here
608   // we obtain two addresses and pass them to EhFrameHdr object.
609   if (In<ELFT>::EhFrameHdr) {
610     for (CieRecord *Cie : Cies) {
611       uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece);
612       for (SectionPiece *Fde : Cie->FdePieces) {
613         uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
614         uint64_t FdeVA = getParent()->Addr + Fde->OutputOff;
615         In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
616       }
617     }
618   }
619 }
620
621 GotSection::GotSection()
622     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
623                        Target->GotEntrySize, ".got") {}
624
625 void GotSection::addEntry(SymbolBody &Sym) {
626   Sym.GotIndex = NumEntries;
627   ++NumEntries;
628 }
629
630 bool GotSection::addDynTlsEntry(SymbolBody &Sym) {
631   if (Sym.GlobalDynIndex != -1U)
632     return false;
633   Sym.GlobalDynIndex = NumEntries;
634   // Global Dynamic TLS entries take two GOT slots.
635   NumEntries += 2;
636   return true;
637 }
638
639 // Reserves TLS entries for a TLS module ID and a TLS block offset.
640 // In total it takes two GOT slots.
641 bool GotSection::addTlsIndex() {
642   if (TlsIndexOff != uint32_t(-1))
643     return false;
644   TlsIndexOff = NumEntries * Config->Wordsize;
645   NumEntries += 2;
646   return true;
647 }
648
649 uint64_t GotSection::getGlobalDynAddr(const SymbolBody &B) const {
650   return this->getVA() + B.GlobalDynIndex * Config->Wordsize;
651 }
652
653 uint64_t GotSection::getGlobalDynOffset(const SymbolBody &B) const {
654   return B.GlobalDynIndex * Config->Wordsize;
655 }
656
657 void GotSection::finalizeContents() { Size = NumEntries * Config->Wordsize; }
658
659 bool GotSection::empty() const {
660   // If we have a relocation that is relative to GOT (such as GOTOFFREL),
661   // we need to emit a GOT even if it's empty.
662   return NumEntries == 0 && !HasGotOffRel;
663 }
664
665 void GotSection::writeTo(uint8_t *Buf) { relocateAlloc(Buf, Buf + Size); }
666
667 MipsGotSection::MipsGotSection()
668     : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
669                        ".got") {}
670
671 void MipsGotSection::addEntry(SymbolBody &Sym, int64_t Addend, RelExpr Expr) {
672   // For "true" local symbols which can be referenced from the same module
673   // only compiler creates two instructions for address loading:
674   //
675   // lw   $8, 0($gp) # R_MIPS_GOT16
676   // addi $8, $8, 0  # R_MIPS_LO16
677   //
678   // The first instruction loads high 16 bits of the symbol address while
679   // the second adds an offset. That allows to reduce number of required
680   // GOT entries because only one global offset table entry is necessary
681   // for every 64 KBytes of local data. So for local symbols we need to
682   // allocate number of GOT entries to hold all required "page" addresses.
683   //
684   // All global symbols (hidden and regular) considered by compiler uniformly.
685   // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
686   // to load address of the symbol. So for each such symbol we need to
687   // allocate dedicated GOT entry to store its address.
688   //
689   // If a symbol is preemptible we need help of dynamic linker to get its
690   // final address. The corresponding GOT entries are allocated in the
691   // "global" part of GOT. Entries for non preemptible global symbol allocated
692   // in the "local" part of GOT.
693   //
694   // See "Global Offset Table" in Chapter 5:
695   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
696   if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
697     // At this point we do not know final symbol value so to reduce number
698     // of allocated GOT entries do the following trick. Save all output
699     // sections referenced by GOT relocations. Then later in the `finalize`
700     // method calculate number of "pages" required to cover all saved output
701     // section and allocate appropriate number of GOT entries.
702     PageIndexMap.insert({Sym.getOutputSection(), 0});
703     return;
704   }
705   if (Sym.isTls()) {
706     // GOT entries created for MIPS TLS relocations behave like
707     // almost GOT entries from other ABIs. They go to the end
708     // of the global offset table.
709     Sym.GotIndex = TlsEntries.size();
710     TlsEntries.push_back(&Sym);
711     return;
712   }
713   auto AddEntry = [&](SymbolBody &S, uint64_t A, GotEntries &Items) {
714     if (S.isInGot() && !A)
715       return;
716     size_t NewIndex = Items.size();
717     if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
718       return;
719     Items.emplace_back(&S, A);
720     if (!A)
721       S.GotIndex = NewIndex;
722   };
723   if (Sym.isPreemptible()) {
724     // Ignore addends for preemptible symbols. They got single GOT entry anyway.
725     AddEntry(Sym, 0, GlobalEntries);
726     Sym.IsInGlobalMipsGot = true;
727   } else if (Expr == R_MIPS_GOT_OFF32) {
728     AddEntry(Sym, Addend, LocalEntries32);
729     Sym.Is32BitMipsGot = true;
730   } else {
731     // Hold local GOT entries accessed via a 16-bit index separately.
732     // That allows to write them in the beginning of the GOT and keep
733     // their indexes as less as possible to escape relocation's overflow.
734     AddEntry(Sym, Addend, LocalEntries);
735   }
736 }
737
738 bool MipsGotSection::addDynTlsEntry(SymbolBody &Sym) {
739   if (Sym.GlobalDynIndex != -1U)
740     return false;
741   Sym.GlobalDynIndex = TlsEntries.size();
742   // Global Dynamic TLS entries take two GOT slots.
743   TlsEntries.push_back(nullptr);
744   TlsEntries.push_back(&Sym);
745   return true;
746 }
747
748 // Reserves TLS entries for a TLS module ID and a TLS block offset.
749 // In total it takes two GOT slots.
750 bool MipsGotSection::addTlsIndex() {
751   if (TlsIndexOff != uint32_t(-1))
752     return false;
753   TlsIndexOff = TlsEntries.size() * Config->Wordsize;
754   TlsEntries.push_back(nullptr);
755   TlsEntries.push_back(nullptr);
756   return true;
757 }
758
759 static uint64_t getMipsPageAddr(uint64_t Addr) {
760   return (Addr + 0x8000) & ~0xffff;
761 }
762
763 static uint64_t getMipsPageCount(uint64_t Size) {
764   return (Size + 0xfffe) / 0xffff + 1;
765 }
766
767 uint64_t MipsGotSection::getPageEntryOffset(const SymbolBody &B,
768                                             int64_t Addend) const {
769   const OutputSection *OutSec = B.getOutputSection();
770   uint64_t SecAddr = getMipsPageAddr(OutSec->Addr);
771   uint64_t SymAddr = getMipsPageAddr(B.getVA(Addend));
772   uint64_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
773   assert(Index < PageEntriesNum);
774   return (HeaderEntriesNum + Index) * Config->Wordsize;
775 }
776
777 uint64_t MipsGotSection::getBodyEntryOffset(const SymbolBody &B,
778                                             int64_t Addend) const {
779   // Calculate offset of the GOT entries block: TLS, global, local.
780   uint64_t Index = HeaderEntriesNum + PageEntriesNum;
781   if (B.isTls())
782     Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
783   else if (B.IsInGlobalMipsGot)
784     Index += LocalEntries.size() + LocalEntries32.size();
785   else if (B.Is32BitMipsGot)
786     Index += LocalEntries.size();
787   // Calculate offset of the GOT entry in the block.
788   if (B.isInGot())
789     Index += B.GotIndex;
790   else {
791     auto It = EntryIndexMap.find({&B, Addend});
792     assert(It != EntryIndexMap.end());
793     Index += It->second;
794   }
795   return Index * Config->Wordsize;
796 }
797
798 uint64_t MipsGotSection::getTlsOffset() const {
799   return (getLocalEntriesNum() + GlobalEntries.size()) * Config->Wordsize;
800 }
801
802 uint64_t MipsGotSection::getGlobalDynOffset(const SymbolBody &B) const {
803   return B.GlobalDynIndex * Config->Wordsize;
804 }
805
806 const SymbolBody *MipsGotSection::getFirstGlobalEntry() const {
807   return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first;
808 }
809
810 unsigned MipsGotSection::getLocalEntriesNum() const {
811   return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
812          LocalEntries32.size();
813 }
814
815 void MipsGotSection::finalizeContents() {
816   updateAllocSize();
817 }
818
819 void MipsGotSection::updateAllocSize() {
820   PageEntriesNum = 0;
821   for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) {
822     // For each output section referenced by GOT page relocations calculate
823     // and save into PageIndexMap an upper bound of MIPS GOT entries required
824     // to store page addresses of local symbols. We assume the worst case -
825     // each 64kb page of the output section has at least one GOT relocation
826     // against it. And take in account the case when the section intersects
827     // page boundaries.
828     P.second = PageEntriesNum;
829     PageEntriesNum += getMipsPageCount(P.first->Size);
830   }
831   Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
832          Config->Wordsize;
833 }
834
835 bool MipsGotSection::empty() const {
836   // We add the .got section to the result for dynamic MIPS target because
837   // its address and properties are mentioned in the .dynamic section.
838   return Config->Relocatable;
839 }
840
841 uint64_t MipsGotSection::getGp() const {
842   return ElfSym::MipsGp->getVA(0);
843 }
844
845 static uint64_t readUint(uint8_t *Buf) {
846   if (Config->Is64)
847     return read64(Buf, Config->Endianness);
848   return read32(Buf, Config->Endianness);
849 }
850
851 static void writeUint(uint8_t *Buf, uint64_t Val) {
852   if (Config->Is64)
853     write64(Buf, Val, Config->Endianness);
854   else
855     write32(Buf, Val, Config->Endianness);
856 }
857
858 void MipsGotSection::writeTo(uint8_t *Buf) {
859   // Set the MSB of the second GOT slot. This is not required by any
860   // MIPS ABI documentation, though.
861   //
862   // There is a comment in glibc saying that "The MSB of got[1] of a
863   // gnu object is set to identify gnu objects," and in GNU gold it
864   // says "the second entry will be used by some runtime loaders".
865   // But how this field is being used is unclear.
866   //
867   // We are not really willing to mimic other linkers behaviors
868   // without understanding why they do that, but because all files
869   // generated by GNU tools have this special GOT value, and because
870   // we've been doing this for years, it is probably a safe bet to
871   // keep doing this for now. We really need to revisit this to see
872   // if we had to do this.
873   writeUint(Buf + Config->Wordsize, (uint64_t)1 << (Config->Wordsize * 8 - 1));
874   Buf += HeaderEntriesNum * Config->Wordsize;
875   // Write 'page address' entries to the local part of the GOT.
876   for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) {
877     size_t PageCount = getMipsPageCount(L.first->Size);
878     uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
879     for (size_t PI = 0; PI < PageCount; ++PI) {
880       uint8_t *Entry = Buf + (L.second + PI) * Config->Wordsize;
881       writeUint(Entry, FirstPageAddr + PI * 0x10000);
882     }
883   }
884   Buf += PageEntriesNum * Config->Wordsize;
885   auto AddEntry = [&](const GotEntry &SA) {
886     uint8_t *Entry = Buf;
887     Buf += Config->Wordsize;
888     const SymbolBody *Body = SA.first;
889     uint64_t VA = Body->getVA(SA.second);
890     writeUint(Entry, VA);
891   };
892   std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
893   std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
894   std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
895   // Initialize TLS-related GOT entries. If the entry has a corresponding
896   // dynamic relocations, leave it initialized by zero. Write down adjusted
897   // TLS symbol's values otherwise. To calculate the adjustments use offsets
898   // for thread-local storage.
899   // https://www.linux-mips.org/wiki/NPTL
900   if (TlsIndexOff != -1U && !Config->Pic)
901     writeUint(Buf + TlsIndexOff, 1);
902   for (const SymbolBody *B : TlsEntries) {
903     if (!B || B->isPreemptible())
904       continue;
905     uint64_t VA = B->getVA();
906     if (B->GotIndex != -1U) {
907       uint8_t *Entry = Buf + B->GotIndex * Config->Wordsize;
908       writeUint(Entry, VA - 0x7000);
909     }
910     if (B->GlobalDynIndex != -1U) {
911       uint8_t *Entry = Buf + B->GlobalDynIndex * Config->Wordsize;
912       writeUint(Entry, 1);
913       Entry += Config->Wordsize;
914       writeUint(Entry, VA - 0x8000);
915     }
916   }
917 }
918
919 GotPltSection::GotPltSection()
920     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
921                        Target->GotPltEntrySize, ".got.plt") {}
922
923 void GotPltSection::addEntry(SymbolBody &Sym) {
924   Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
925   Entries.push_back(&Sym);
926 }
927
928 size_t GotPltSection::getSize() const {
929   return (Target->GotPltHeaderEntriesNum + Entries.size()) *
930          Target->GotPltEntrySize;
931 }
932
933 void GotPltSection::writeTo(uint8_t *Buf) {
934   Target->writeGotPltHeader(Buf);
935   Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
936   for (const SymbolBody *B : Entries) {
937     Target->writeGotPlt(Buf, *B);
938     Buf += Config->Wordsize;
939   }
940 }
941
942 // On ARM the IgotPltSection is part of the GotSection, on other Targets it is
943 // part of the .got.plt
944 IgotPltSection::IgotPltSection()
945     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
946                        Target->GotPltEntrySize,
947                        Config->EMachine == EM_ARM ? ".got" : ".got.plt") {}
948
949 void IgotPltSection::addEntry(SymbolBody &Sym) {
950   Sym.IsInIgot = true;
951   Sym.GotPltIndex = Entries.size();
952   Entries.push_back(&Sym);
953 }
954
955 size_t IgotPltSection::getSize() const {
956   return Entries.size() * Target->GotPltEntrySize;
957 }
958
959 void IgotPltSection::writeTo(uint8_t *Buf) {
960   for (const SymbolBody *B : Entries) {
961     Target->writeIgotPlt(Buf, *B);
962     Buf += Config->Wordsize;
963   }
964 }
965
966 StringTableSection::StringTableSection(StringRef Name, bool Dynamic)
967     : SyntheticSection(Dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name),
968       Dynamic(Dynamic) {
969   // ELF string tables start with a NUL byte.
970   addString("");
971 }
972
973 // Adds a string to the string table. If HashIt is true we hash and check for
974 // duplicates. It is optional because the name of global symbols are already
975 // uniqued and hashing them again has a big cost for a small value: uniquing
976 // them with some other string that happens to be the same.
977 unsigned StringTableSection::addString(StringRef S, bool HashIt) {
978   if (HashIt) {
979     auto R = StringMap.insert(std::make_pair(S, this->Size));
980     if (!R.second)
981       return R.first->second;
982   }
983   unsigned Ret = this->Size;
984   this->Size = this->Size + S.size() + 1;
985   Strings.push_back(S);
986   return Ret;
987 }
988
989 void StringTableSection::writeTo(uint8_t *Buf) {
990   for (StringRef S : Strings) {
991     memcpy(Buf, S.data(), S.size());
992     Buf += S.size() + 1;
993   }
994 }
995
996 // Returns the number of version definition entries. Because the first entry
997 // is for the version definition itself, it is the number of versioned symbols
998 // plus one. Note that we don't support multiple versions yet.
999 static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
1000
1001 template <class ELFT>
1002 DynamicSection<ELFT>::DynamicSection()
1003     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, Config->Wordsize,
1004                        ".dynamic") {
1005   this->Entsize = ELFT::Is64Bits ? 16 : 8;
1006
1007   // .dynamic section is not writable on MIPS and on Fuchsia OS
1008   // which passes -z rodynamic.
1009   // See "Special Section" in Chapter 4 in the following document:
1010   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1011   if (Config->EMachine == EM_MIPS || Config->ZRodynamic)
1012     this->Flags = SHF_ALLOC;
1013
1014   addEntries();
1015 }
1016
1017 // There are some dynamic entries that don't depend on other sections.
1018 // Such entries can be set early.
1019 template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1020   // Add strings to .dynstr early so that .dynstr's size will be
1021   // fixed early.
1022   for (StringRef S : Config->AuxiliaryList)
1023     add({DT_AUXILIARY, InX::DynStrTab->addString(S)});
1024   if (!Config->Rpath.empty())
1025     add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
1026          InX::DynStrTab->addString(Config->Rpath)});
1027   for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
1028     if (F->isNeeded())
1029       add({DT_NEEDED, InX::DynStrTab->addString(F->SoName)});
1030   if (!Config->SoName.empty())
1031     add({DT_SONAME, InX::DynStrTab->addString(Config->SoName)});
1032
1033   // Set DT_FLAGS and DT_FLAGS_1.
1034   uint32_t DtFlags = 0;
1035   uint32_t DtFlags1 = 0;
1036   if (Config->Bsymbolic)
1037     DtFlags |= DF_SYMBOLIC;
1038   if (Config->ZNodelete)
1039     DtFlags1 |= DF_1_NODELETE;
1040   if (Config->ZNodlopen)
1041     DtFlags1 |= DF_1_NOOPEN;
1042   if (Config->ZNow) {
1043     DtFlags |= DF_BIND_NOW;
1044     DtFlags1 |= DF_1_NOW;
1045   }
1046   if (Config->ZOrigin) {
1047     DtFlags |= DF_ORIGIN;
1048     DtFlags1 |= DF_1_ORIGIN;
1049   }
1050
1051   if (DtFlags)
1052     add({DT_FLAGS, DtFlags});
1053   if (DtFlags1)
1054     add({DT_FLAGS_1, DtFlags1});
1055
1056   // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1057   // need it for each process, so we don't write it for DSOs. The loader writes
1058   // the pointer into this entry.
1059   //
1060   // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1061   // systems (currently only Fuchsia OS) provide other means to give the
1062   // debugger this information. Such systems may choose make .dynamic read-only.
1063   // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1064   if (!Config->Shared && !Config->Relocatable && !Config->ZRodynamic)
1065     add({DT_DEBUG, (uint64_t)0});
1066 }
1067
1068 // Add remaining entries to complete .dynamic contents.
1069 template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1070   if (this->Size)
1071     return; // Already finalized.
1072
1073   this->Link = InX::DynStrTab->getParent()->SectionIndex;
1074   if (In<ELFT>::RelaDyn->getParent()->Size > 0) {
1075     bool IsRela = Config->IsRela;
1076     add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn});
1077     add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->getParent()->Size});
1078     add({IsRela ? DT_RELAENT : DT_RELENT,
1079          uint64_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
1080
1081     // MIPS dynamic loader does not support RELCOUNT tag.
1082     // The problem is in the tight relation between dynamic
1083     // relocations and GOT. So do not emit this tag on MIPS.
1084     if (Config->EMachine != EM_MIPS) {
1085       size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
1086       if (Config->ZCombreloc && NumRelativeRels)
1087         add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
1088     }
1089   }
1090   if (In<ELFT>::RelaPlt->getParent()->Size > 0) {
1091     add({DT_JMPREL, In<ELFT>::RelaPlt});
1092     add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getParent()->Size});
1093     add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
1094          InX::GotPlt});
1095     add({DT_PLTREL, uint64_t(Config->IsRela ? DT_RELA : DT_REL)});
1096   }
1097
1098   add({DT_SYMTAB, InX::DynSymTab});
1099   add({DT_SYMENT, sizeof(Elf_Sym)});
1100   add({DT_STRTAB, InX::DynStrTab});
1101   add({DT_STRSZ, InX::DynStrTab->getSize()});
1102   if (!Config->ZText)
1103     add({DT_TEXTREL, (uint64_t)0});
1104   if (InX::GnuHashTab)
1105     add({DT_GNU_HASH, InX::GnuHashTab});
1106   if (In<ELFT>::HashTab)
1107     add({DT_HASH, In<ELFT>::HashTab});
1108
1109   if (Out::PreinitArray) {
1110     add({DT_PREINIT_ARRAY, Out::PreinitArray});
1111     add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
1112   }
1113   if (Out::InitArray) {
1114     add({DT_INIT_ARRAY, Out::InitArray});
1115     add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
1116   }
1117   if (Out::FiniArray) {
1118     add({DT_FINI_ARRAY, Out::FiniArray});
1119     add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
1120   }
1121
1122   if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Init))
1123     add({DT_INIT, B});
1124   if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Fini))
1125     add({DT_FINI, B});
1126
1127   bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1128   if (HasVerNeed || In<ELFT>::VerDef)
1129     add({DT_VERSYM, In<ELFT>::VerSym});
1130   if (In<ELFT>::VerDef) {
1131     add({DT_VERDEF, In<ELFT>::VerDef});
1132     add({DT_VERDEFNUM, getVerDefNum()});
1133   }
1134   if (HasVerNeed) {
1135     add({DT_VERNEED, In<ELFT>::VerNeed});
1136     add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
1137   }
1138
1139   if (Config->EMachine == EM_MIPS) {
1140     add({DT_MIPS_RLD_VERSION, 1});
1141     add({DT_MIPS_FLAGS, RHF_NOTPOT});
1142     add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
1143     add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()});
1144     add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()});
1145     if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry())
1146       add({DT_MIPS_GOTSYM, B->DynsymIndex});
1147     else
1148       add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()});
1149     add({DT_PLTGOT, InX::MipsGot});
1150     if (InX::MipsRldMap)
1151       add({DT_MIPS_RLD_MAP, InX::MipsRldMap});
1152   }
1153
1154   getParent()->Link = this->Link;
1155
1156   // +1 for DT_NULL
1157   this->Size = (Entries.size() + 1) * this->Entsize;
1158 }
1159
1160 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1161   auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1162
1163   for (const Entry &E : Entries) {
1164     P->d_tag = E.Tag;
1165     switch (E.Kind) {
1166     case Entry::SecAddr:
1167       P->d_un.d_ptr = E.OutSec->Addr;
1168       break;
1169     case Entry::InSecAddr:
1170       P->d_un.d_ptr = E.InSec->getParent()->Addr + E.InSec->OutSecOff;
1171       break;
1172     case Entry::SecSize:
1173       P->d_un.d_val = E.OutSec->Size;
1174       break;
1175     case Entry::SymAddr:
1176       P->d_un.d_ptr = E.Sym->getVA();
1177       break;
1178     case Entry::PlainInt:
1179       P->d_un.d_val = E.Val;
1180       break;
1181     }
1182     ++P;
1183   }
1184 }
1185
1186 uint64_t DynamicReloc::getOffset() const {
1187   return InputSec->getOutputSection()->Addr + InputSec->getOffset(OffsetInSec);
1188 }
1189
1190 int64_t DynamicReloc::getAddend() const {
1191   if (UseSymVA)
1192     return Sym->getVA(Addend);
1193   return Addend;
1194 }
1195
1196 uint32_t DynamicReloc::getSymIndex() const {
1197   if (Sym && !UseSymVA)
1198     return Sym->DynsymIndex;
1199   return 0;
1200 }
1201
1202 template <class ELFT>
1203 RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
1204     : SyntheticSection(SHF_ALLOC, Config->IsRela ? SHT_RELA : SHT_REL,
1205                        Config->Wordsize, Name),
1206       Sort(Sort) {
1207   this->Entsize = Config->IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1208 }
1209
1210 template <class ELFT>
1211 void RelocationSection<ELFT>::addReloc(const DynamicReloc &Reloc) {
1212   if (Reloc.Type == Target->RelativeRel)
1213     ++NumRelativeRelocs;
1214   Relocs.push_back(Reloc);
1215 }
1216
1217 template <class ELFT, class RelTy>
1218 static bool compRelocations(const RelTy &A, const RelTy &B) {
1219   bool AIsRel = A.getType(Config->IsMips64EL) == Target->RelativeRel;
1220   bool BIsRel = B.getType(Config->IsMips64EL) == Target->RelativeRel;
1221   if (AIsRel != BIsRel)
1222     return AIsRel;
1223
1224   return A.getSymbol(Config->IsMips64EL) < B.getSymbol(Config->IsMips64EL);
1225 }
1226
1227 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1228   uint8_t *BufBegin = Buf;
1229   for (const DynamicReloc &Rel : Relocs) {
1230     auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1231     Buf += Config->IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1232
1233     if (Config->IsRela)
1234       P->r_addend = Rel.getAddend();
1235     P->r_offset = Rel.getOffset();
1236     if (Config->EMachine == EM_MIPS && Rel.getInputSec() == InX::MipsGot)
1237       // Dynamic relocation against MIPS GOT section make deal TLS entries
1238       // allocated in the end of the GOT. We need to adjust the offset to take
1239       // in account 'local' and 'global' GOT entries.
1240       P->r_offset += InX::MipsGot->getTlsOffset();
1241     P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
1242   }
1243
1244   if (Sort) {
1245     if (Config->IsRela)
1246       std::stable_sort((Elf_Rela *)BufBegin,
1247                        (Elf_Rela *)BufBegin + Relocs.size(),
1248                        compRelocations<ELFT, Elf_Rela>);
1249     else
1250       std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1251                        compRelocations<ELFT, Elf_Rel>);
1252   }
1253 }
1254
1255 template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1256   return this->Entsize * Relocs.size();
1257 }
1258
1259 template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
1260   this->Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex
1261                               : InX::SymTab->getParent()->SectionIndex;
1262
1263   // Set required output section properties.
1264   getParent()->Link = this->Link;
1265 }
1266
1267 SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &StrTabSec)
1268     : SyntheticSection(StrTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
1269                        StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1270                        Config->Wordsize,
1271                        StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1272       StrTabSec(StrTabSec) {}
1273
1274 // Orders symbols according to their positions in the GOT,
1275 // in compliance with MIPS ABI rules.
1276 // See "Global Offset Table" in Chapter 5 in the following document
1277 // for detailed description:
1278 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1279 static bool sortMipsSymbols(const SymbolTableEntry &L,
1280                             const SymbolTableEntry &R) {
1281   // Sort entries related to non-local preemptible symbols by GOT indexes.
1282   // All other entries go to the first part of GOT in arbitrary order.
1283   bool LIsInLocalGot = !L.Symbol->IsInGlobalMipsGot;
1284   bool RIsInLocalGot = !R.Symbol->IsInGlobalMipsGot;
1285   if (LIsInLocalGot || RIsInLocalGot)
1286     return !RIsInLocalGot;
1287   return L.Symbol->GotIndex < R.Symbol->GotIndex;
1288 }
1289
1290 // Finalize a symbol table. The ELF spec requires that all local
1291 // symbols precede global symbols, so we sort symbol entries in this
1292 // function. (For .dynsym, we don't do that because symbols for
1293 // dynamic linking are inherently all globals.)
1294 void SymbolTableBaseSection::finalizeContents() {
1295   getParent()->Link = StrTabSec.getParent()->SectionIndex;
1296
1297   // If it is a .dynsym, there should be no local symbols, but we need
1298   // to do a few things for the dynamic linker.
1299   if (this->Type == SHT_DYNSYM) {
1300     // Section's Info field has the index of the first non-local symbol.
1301     // Because the first symbol entry is a null entry, 1 is the first.
1302     getParent()->Info = 1;
1303
1304     if (InX::GnuHashTab) {
1305       // NB: It also sorts Symbols to meet the GNU hash table requirements.
1306       InX::GnuHashTab->addSymbols(Symbols);
1307     } else if (Config->EMachine == EM_MIPS) {
1308       std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
1309     }
1310
1311     size_t I = 0;
1312     for (const SymbolTableEntry &S : Symbols)
1313       S.Symbol->DynsymIndex = ++I;
1314     return;
1315   }
1316 }
1317
1318 void SymbolTableBaseSection::postThunkContents() {
1319   if (this->Type == SHT_DYNSYM)
1320     return;
1321   // move all local symbols before global symbols.
1322   auto It = std::stable_partition(
1323       Symbols.begin(), Symbols.end(), [](const SymbolTableEntry &S) {
1324         return S.Symbol->isLocal() ||
1325                S.Symbol->symbol()->computeBinding() == STB_LOCAL;
1326       });
1327   size_t NumLocals = It - Symbols.begin();
1328   getParent()->Info = NumLocals + 1;
1329 }
1330
1331 void SymbolTableBaseSection::addSymbol(SymbolBody *B) {
1332   // Adding a local symbol to a .dynsym is a bug.
1333   assert(this->Type != SHT_DYNSYM || !B->isLocal());
1334
1335   bool HashIt = B->isLocal();
1336   Symbols.push_back({B, StrTabSec.addString(B->getName(), HashIt)});
1337 }
1338
1339 size_t SymbolTableBaseSection::getSymbolIndex(SymbolBody *Body) {
1340   auto I = llvm::find_if(Symbols, [&](const SymbolTableEntry &E) {
1341     if (E.Symbol == Body)
1342       return true;
1343     // This is used for -r, so we have to handle multiple section
1344     // symbols being combined.
1345     if (Body->Type == STT_SECTION && E.Symbol->Type == STT_SECTION)
1346       return Body->getOutputSection() == E.Symbol->getOutputSection();
1347     return false;
1348   });
1349   if (I == Symbols.end())
1350     return 0;
1351   return I - Symbols.begin() + 1;
1352 }
1353
1354 template <class ELFT>
1355 SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &StrTabSec)
1356     : SymbolTableBaseSection(StrTabSec) {
1357   this->Entsize = sizeof(Elf_Sym);
1358 }
1359
1360 // Write the internal symbol table contents to the output symbol table.
1361 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1362   // The first entry is a null entry as per the ELF spec.
1363   Buf += sizeof(Elf_Sym);
1364
1365   auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1366
1367   for (SymbolTableEntry &Ent : Symbols) {
1368     SymbolBody *Body = Ent.Symbol;
1369
1370     // Set st_info and st_other.
1371     if (Body->isLocal()) {
1372       ESym->setBindingAndType(STB_LOCAL, Body->Type);
1373     } else {
1374       ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1375       ESym->setVisibility(Body->symbol()->Visibility);
1376     }
1377
1378     ESym->st_name = Ent.StrTabOffset;
1379     ESym->st_size = Body->getSize<ELFT>();
1380
1381     // Set a section index.
1382     if (const OutputSection *OutSec = Body->getOutputSection())
1383       ESym->st_shndx = OutSec->SectionIndex;
1384     else if (isa<DefinedRegular>(Body))
1385       ESym->st_shndx = SHN_ABS;
1386     else if (isa<DefinedCommon>(Body))
1387       ESym->st_shndx = SHN_COMMON;
1388
1389     // st_value is usually an address of a symbol, but that has a
1390     // special meaining for uninstantiated common symbols (this can
1391     // occur if -r is given).
1392     if (!Config->DefineCommon && isa<DefinedCommon>(Body))
1393       ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1394     else
1395       ESym->st_value = Body->getVA();
1396
1397     ++ESym;
1398   }
1399
1400   // On MIPS we need to mark symbol which has a PLT entry and requires
1401   // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1402   // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1403   // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1404   if (Config->EMachine == EM_MIPS) {
1405     auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1406
1407     for (SymbolTableEntry &Ent : Symbols) {
1408       SymbolBody *Body = Ent.Symbol;
1409       if (Body->isInPlt() && Body->NeedsPltAddr)
1410         ESym->st_other |= STO_MIPS_PLT;
1411
1412       if (Config->Relocatable)
1413         if (auto *D = dyn_cast<DefinedRegular>(Body))
1414           if (D->isMipsPIC<ELFT>())
1415             ESym->st_other |= STO_MIPS_PIC;
1416       ++ESym;
1417     }
1418   }
1419 }
1420
1421 // .hash and .gnu.hash sections contain on-disk hash tables that map
1422 // symbol names to their dynamic symbol table indices. Their purpose
1423 // is to help the dynamic linker resolve symbols quickly. If ELF files
1424 // don't have them, the dynamic linker has to do linear search on all
1425 // dynamic symbols, which makes programs slower. Therefore, a .hash
1426 // section is added to a DSO by default. A .gnu.hash is added if you
1427 // give the -hash-style=gnu or -hash-style=both option.
1428 //
1429 // The Unix semantics of resolving dynamic symbols is somewhat expensive.
1430 // Each ELF file has a list of DSOs that the ELF file depends on and a
1431 // list of dynamic symbols that need to be resolved from any of the
1432 // DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
1433 // where m is the number of DSOs and n is the number of dynamic
1434 // symbols. For modern large programs, both m and n are large.  So
1435 // making each step faster by using hash tables substiantially
1436 // improves time to load programs.
1437 //
1438 // (Note that this is not the only way to design the shared library.
1439 // For instance, the Windows DLL takes a different approach. On
1440 // Windows, each dynamic symbol has a name of DLL from which the symbol
1441 // has to be resolved. That makes the cost of symbol resolution O(n).
1442 // This disables some hacky techniques you can use on Unix such as
1443 // LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
1444 //
1445 // Due to historical reasons, we have two different hash tables, .hash
1446 // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
1447 // and better version of .hash. .hash is just an on-disk hash table, but
1448 // .gnu.hash has a bloom filter in addition to a hash table to skip
1449 // DSOs very quickly. If you are sure that your dynamic linker knows
1450 // about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a
1451 // safe bet is to specify -hash-style=both for backward compatibilty.
1452 GnuHashTableSection::GnuHashTableSection()
1453     : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, Config->Wordsize, ".gnu.hash") {
1454 }
1455
1456 void GnuHashTableSection::finalizeContents() {
1457   getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1458
1459   // Computes bloom filter size in word size. We want to allocate 8
1460   // bits for each symbol. It must be a power of two.
1461   if (Symbols.empty())
1462     MaskWords = 1;
1463   else
1464     MaskWords = NextPowerOf2((Symbols.size() - 1) / Config->Wordsize);
1465
1466   Size = 16;                            // Header
1467   Size += Config->Wordsize * MaskWords; // Bloom filter
1468   Size += NBuckets * 4;                 // Hash buckets
1469   Size += Symbols.size() * 4;           // Hash values
1470 }
1471
1472 void GnuHashTableSection::writeTo(uint8_t *Buf) {
1473   // Write a header.
1474   write32(Buf, NBuckets, Config->Endianness);
1475   write32(Buf + 4, InX::DynSymTab->getNumSymbols() - Symbols.size(),
1476           Config->Endianness);
1477   write32(Buf + 8, MaskWords, Config->Endianness);
1478   write32(Buf + 12, getShift2(), Config->Endianness);
1479   Buf += 16;
1480
1481   // Write a bloom filter and a hash table.
1482   writeBloomFilter(Buf);
1483   Buf += Config->Wordsize * MaskWords;
1484   writeHashTable(Buf);
1485 }
1486
1487 // This function writes a 2-bit bloom filter. This bloom filter alone
1488 // usually filters out 80% or more of all symbol lookups [1].
1489 // The dynamic linker uses the hash table only when a symbol is not
1490 // filtered out by a bloom filter.
1491 //
1492 // [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2),
1493 //     p.9, https://www.akkadia.org/drepper/dsohowto.pdf
1494 void GnuHashTableSection::writeBloomFilter(uint8_t *Buf) {
1495   const unsigned C = Config->Wordsize * 8;
1496   for (const Entry &Sym : Symbols) {
1497     size_t I = (Sym.Hash / C) & (MaskWords - 1);
1498     uint64_t Val = readUint(Buf + I * Config->Wordsize);
1499     Val |= uint64_t(1) << (Sym.Hash % C);
1500     Val |= uint64_t(1) << ((Sym.Hash >> getShift2()) % C);
1501     writeUint(Buf + I * Config->Wordsize, Val);
1502   }
1503 }
1504
1505 void GnuHashTableSection::writeHashTable(uint8_t *Buf) {
1506   // Group symbols by hash value.
1507   std::vector<std::vector<Entry>> Syms(NBuckets);
1508   for (const Entry &Ent : Symbols)
1509     Syms[Ent.Hash % NBuckets].push_back(Ent);
1510
1511   // Write hash buckets. Hash buckets contain indices in the following
1512   // hash value table.
1513   uint32_t *Buckets = reinterpret_cast<uint32_t *>(Buf);
1514   for (size_t I = 0; I < NBuckets; ++I)
1515     if (!Syms[I].empty())
1516       write32(Buckets + I, Syms[I][0].Body->DynsymIndex, Config->Endianness);
1517
1518   // Write a hash value table. It represents a sequence of chains that
1519   // share the same hash modulo value. The last element of each chain
1520   // is terminated by LSB 1.
1521   uint32_t *Values = Buckets + NBuckets;
1522   size_t I = 0;
1523   for (std::vector<Entry> &Vec : Syms) {
1524     if (Vec.empty())
1525       continue;
1526     for (const Entry &Ent : makeArrayRef(Vec).drop_back())
1527       write32(Values + I++, Ent.Hash & ~1, Config->Endianness);
1528     write32(Values + I++, Vec.back().Hash | 1, Config->Endianness);
1529   }
1530 }
1531
1532 static uint32_t hashGnu(StringRef Name) {
1533   uint32_t H = 5381;
1534   for (uint8_t C : Name)
1535     H = (H << 5) + H + C;
1536   return H;
1537 }
1538
1539 // Returns a number of hash buckets to accomodate given number of elements.
1540 // We want to choose a moderate number that is not too small (which
1541 // causes too many hash collisions) and not too large (which wastes
1542 // disk space.)
1543 //
1544 // We return a prime number because it (is believed to) achieve good
1545 // hash distribution.
1546 static size_t getBucketSize(size_t NumSymbols) {
1547   // List of largest prime numbers that are not greater than 2^n + 1.
1548   for (size_t N : {131071, 65521, 32749, 16381, 8191, 4093, 2039, 1021, 509,
1549                    251, 127, 61, 31, 13, 7, 3, 1})
1550     if (N <= NumSymbols)
1551       return N;
1552   return 0;
1553 }
1554
1555 // Add symbols to this symbol hash table. Note that this function
1556 // destructively sort a given vector -- which is needed because
1557 // GNU-style hash table places some sorting requirements.
1558 void GnuHashTableSection::addSymbols(std::vector<SymbolTableEntry> &V) {
1559   // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
1560   // its type correctly.
1561   std::vector<SymbolTableEntry>::iterator Mid =
1562       std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
1563         return S.Symbol->isUndefined();
1564       });
1565   if (Mid == V.end())
1566     return;
1567
1568   for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) {
1569     SymbolBody *B = Ent.Symbol;
1570     Symbols.push_back({B, Ent.StrTabOffset, hashGnu(B->getName())});
1571   }
1572
1573   NBuckets = getBucketSize(Symbols.size());
1574   std::stable_sort(Symbols.begin(), Symbols.end(),
1575                    [&](const Entry &L, const Entry &R) {
1576                      return L.Hash % NBuckets < R.Hash % NBuckets;
1577                    });
1578
1579   V.erase(Mid, V.end());
1580   for (const Entry &Ent : Symbols)
1581     V.push_back({Ent.Body, Ent.StrTabOffset});
1582 }
1583
1584 template <class ELFT>
1585 HashTableSection<ELFT>::HashTableSection()
1586     : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
1587   this->Entsize = 4;
1588 }
1589
1590 template <class ELFT> void HashTableSection<ELFT>::finalizeContents() {
1591   getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
1592
1593   unsigned NumEntries = 2;                            // nbucket and nchain.
1594   NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries.
1595
1596   // Create as many buckets as there are symbols.
1597   // FIXME: This is simplistic. We can try to optimize it, but implementing
1598   // support for SHT_GNU_HASH is probably even more profitable.
1599   NumEntries += InX::DynSymTab->getNumSymbols();
1600   this->Size = NumEntries * 4;
1601 }
1602
1603 template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1604   // A 32-bit integer type in the target endianness.
1605   typedef typename ELFT::Word Elf_Word;
1606
1607   unsigned NumSymbols = InX::DynSymTab->getNumSymbols();
1608
1609   auto *P = reinterpret_cast<Elf_Word *>(Buf);
1610   *P++ = NumSymbols; // nbucket
1611   *P++ = NumSymbols; // nchain
1612
1613   Elf_Word *Buckets = P;
1614   Elf_Word *Chains = P + NumSymbols;
1615
1616   for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) {
1617     SymbolBody *Body = S.Symbol;
1618     StringRef Name = Body->getName();
1619     unsigned I = Body->DynsymIndex;
1620     uint32_t Hash = hashSysV(Name) % NumSymbols;
1621     Chains[I] = Buckets[Hash];
1622     Buckets[Hash] = I;
1623   }
1624 }
1625
1626 PltSection::PltSection(size_t S)
1627     : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"),
1628       HeaderSize(S) {}
1629
1630 void PltSection::writeTo(uint8_t *Buf) {
1631   // At beginning of PLT but not the IPLT, we have code to call the dynamic
1632   // linker to resolve dynsyms at runtime. Write such code.
1633   if (HeaderSize != 0)
1634     Target->writePltHeader(Buf);
1635   size_t Off = HeaderSize;
1636   // The IPlt is immediately after the Plt, account for this in RelOff
1637   unsigned PltOff = getPltRelocOff();
1638
1639   for (auto &I : Entries) {
1640     const SymbolBody *B = I.first;
1641     unsigned RelOff = I.second + PltOff;
1642     uint64_t Got = B->getGotPltVA();
1643     uint64_t Plt = this->getVA() + Off;
1644     Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1645     Off += Target->PltEntrySize;
1646   }
1647 }
1648
1649 template <class ELFT> void PltSection::addEntry(SymbolBody &Sym) {
1650   Sym.PltIndex = Entries.size();
1651   RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1652   if (HeaderSize == 0) {
1653     PltRelocSection = In<ELFT>::RelaIplt;
1654     Sym.IsInIplt = true;
1655   }
1656   unsigned RelOff = PltRelocSection->getRelocOffset();
1657   Entries.push_back(std::make_pair(&Sym, RelOff));
1658 }
1659
1660 size_t PltSection::getSize() const {
1661   return HeaderSize + Entries.size() * Target->PltEntrySize;
1662 }
1663
1664 // Some architectures such as additional symbols in the PLT section. For
1665 // example ARM uses mapping symbols to aid disassembly
1666 void PltSection::addSymbols() {
1667   // The PLT may have symbols defined for the Header, the IPLT has no header
1668   if (HeaderSize != 0)
1669     Target->addPltHeaderSymbols(this);
1670   size_t Off = HeaderSize;
1671   for (size_t I = 0; I < Entries.size(); ++I) {
1672     Target->addPltSymbols(this, Off);
1673     Off += Target->PltEntrySize;
1674   }
1675 }
1676
1677 unsigned PltSection::getPltRelocOff() const {
1678   return (HeaderSize == 0) ? InX::Plt->getSize() : 0;
1679 }
1680
1681 GdbIndexSection::GdbIndexSection()
1682     : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"),
1683       StringPool(llvm::StringTableBuilder::ELF) {}
1684
1685 // Iterative hash function for symbol's name is described in .gdb_index format
1686 // specification. Note that we use one for version 5 to 7 here, it is different
1687 // for version 4.
1688 static uint32_t hash(StringRef Str) {
1689   uint32_t R = 0;
1690   for (uint8_t C : Str)
1691     R = R * 67 + tolower(C) - 113;
1692   return R;
1693 }
1694
1695 static std::vector<CompilationUnitEntry> readCuList(DWARFContext &Dwarf,
1696                                                     InputSection *Sec) {
1697   std::vector<CompilationUnitEntry> Ret;
1698   for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units())
1699     Ret.push_back({Sec->OutSecOff + CU->getOffset(), CU->getLength() + 4});
1700   return Ret;
1701 }
1702
1703 static std::vector<AddressEntry> readAddressArea(DWARFContext &Dwarf,
1704                                                  InputSection *Sec) {
1705   std::vector<AddressEntry> Ret;
1706
1707   uint32_t CurrentCu = 0;
1708   for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units()) {
1709     DWARFAddressRangesVector Ranges;
1710     CU->collectAddressRanges(Ranges);
1711
1712     ArrayRef<InputSectionBase *> Sections = Sec->File->getSections();
1713     for (DWARFAddressRange &R : Ranges) {
1714       InputSectionBase *S = Sections[R.SectionIndex];
1715       if (!S || S == &InputSection::Discarded || !S->Live)
1716         continue;
1717       // Range list with zero size has no effect.
1718       if (R.LowPC == R.HighPC)
1719         continue;
1720       Ret.push_back({cast<InputSection>(S), R.LowPC, R.HighPC, CurrentCu});
1721     }
1722     ++CurrentCu;
1723   }
1724   return Ret;
1725 }
1726
1727 static std::vector<NameTypeEntry> readPubNamesAndTypes(DWARFContext &Dwarf,
1728                                                        bool IsLE) {
1729   StringRef Data[] = {Dwarf.getGnuPubNamesSection(),
1730                       Dwarf.getGnuPubTypesSection()};
1731
1732   std::vector<NameTypeEntry> Ret;
1733   for (StringRef D : Data) {
1734     DWARFDebugPubTable PubTable(D, IsLE, true);
1735     for (const DWARFDebugPubTable::Set &Set : PubTable.getData())
1736       for (const DWARFDebugPubTable::Entry &Ent : Set.Entries)
1737         Ret.push_back({Ent.Name, Ent.Descriptor.toBits()});
1738   }
1739   return Ret;
1740 }
1741
1742 static std::vector<InputSection *> getDebugInfoSections() {
1743   std::vector<InputSection *> Ret;
1744   for (InputSectionBase *S : InputSections)
1745     if (InputSection *IS = dyn_cast<InputSection>(S))
1746       if (IS->getParent() && IS->Name == ".debug_info")
1747         Ret.push_back(IS);
1748   return Ret;
1749 }
1750
1751 void GdbIndexSection::buildIndex() {
1752   std::vector<InputSection *> V = getDebugInfoSections();
1753   if (V.empty())
1754     return;
1755
1756   for (InputSection *Sec : V)
1757     Chunks.push_back(readDwarf(Sec));
1758
1759   uint32_t CuId = 0;
1760   for (GdbIndexChunk &D : Chunks) {
1761     for (AddressEntry &E : D.AddressArea)
1762       E.CuIndex += CuId;
1763
1764     // Populate constant pool area.
1765     for (NameTypeEntry &NameType : D.NamesAndTypes) {
1766       uint32_t Hash = hash(NameType.Name);
1767       size_t Offset = StringPool.add(NameType.Name);
1768
1769       bool IsNew;
1770       GdbSymbol *Sym;
1771       std::tie(IsNew, Sym) = SymbolTable.add(Hash, Offset);
1772       if (IsNew) {
1773         Sym->CuVectorIndex = CuVectors.size();
1774         CuVectors.resize(CuVectors.size() + 1);
1775       }
1776
1777       CuVectors[Sym->CuVectorIndex].insert(CuId | (NameType.Type << 24));
1778     }
1779
1780     CuId += D.CompilationUnits.size();
1781   }
1782 }
1783
1784 GdbIndexChunk GdbIndexSection::readDwarf(InputSection *Sec) {
1785   Expected<std::unique_ptr<object::ObjectFile>> Obj =
1786       object::ObjectFile::createObjectFile(Sec->File->MB);
1787   if (!Obj) {
1788     error(toString(Sec->File) + ": error creating DWARF context");
1789     return {};
1790   }
1791
1792   DWARFContextInMemory Dwarf(*Obj.get());
1793
1794   GdbIndexChunk Ret;
1795   Ret.CompilationUnits = readCuList(Dwarf, Sec);
1796   Ret.AddressArea = readAddressArea(Dwarf, Sec);
1797   Ret.NamesAndTypes = readPubNamesAndTypes(Dwarf, Config->IsLE);
1798   return Ret;
1799 }
1800
1801 static size_t getCuSize(std::vector<GdbIndexChunk> &C) {
1802   size_t Ret = 0;
1803   for (GdbIndexChunk &D : C)
1804     Ret += D.CompilationUnits.size();
1805   return Ret;
1806 }
1807
1808 static size_t getAddressAreaSize(std::vector<GdbIndexChunk> &C) {
1809   size_t Ret = 0;
1810   for (GdbIndexChunk &D : C)
1811     Ret += D.AddressArea.size();
1812   return Ret;
1813 }
1814
1815 void GdbIndexSection::finalizeContents() {
1816   if (Finalized)
1817     return;
1818   Finalized = true;
1819
1820   buildIndex();
1821
1822   SymbolTable.finalizeContents();
1823
1824   // GdbIndex header consist from version fields
1825   // and 5 more fields with different kinds of offsets.
1826   CuTypesOffset = CuListOffset + getCuSize(Chunks) * CompilationUnitSize;
1827   SymTabOffset = CuTypesOffset + getAddressAreaSize(Chunks) * AddressEntrySize;
1828
1829   ConstantPoolOffset =
1830       SymTabOffset + SymbolTable.getCapacity() * SymTabEntrySize;
1831
1832   for (std::set<uint32_t> &CuVec : CuVectors) {
1833     CuVectorsOffset.push_back(CuVectorsSize);
1834     CuVectorsSize += OffsetTypeSize * (CuVec.size() + 1);
1835   }
1836   StringPoolOffset = ConstantPoolOffset + CuVectorsSize;
1837
1838   StringPool.finalizeInOrder();
1839 }
1840
1841 size_t GdbIndexSection::getSize() const {
1842   const_cast<GdbIndexSection *>(this)->finalizeContents();
1843   return StringPoolOffset + StringPool.getSize();
1844 }
1845
1846 void GdbIndexSection::writeTo(uint8_t *Buf) {
1847   write32le(Buf, 7);                       // Write version.
1848   write32le(Buf + 4, CuListOffset);        // CU list offset.
1849   write32le(Buf + 8, CuTypesOffset);       // Types CU list offset.
1850   write32le(Buf + 12, CuTypesOffset);      // Address area offset.
1851   write32le(Buf + 16, SymTabOffset);       // Symbol table offset.
1852   write32le(Buf + 20, ConstantPoolOffset); // Constant pool offset.
1853   Buf += 24;
1854
1855   // Write the CU list.
1856   for (GdbIndexChunk &D : Chunks) {
1857     for (CompilationUnitEntry &Cu : D.CompilationUnits) {
1858       write64le(Buf, Cu.CuOffset);
1859       write64le(Buf + 8, Cu.CuLength);
1860       Buf += 16;
1861     }
1862   }
1863
1864   // Write the address area.
1865   for (GdbIndexChunk &D : Chunks) {
1866     for (AddressEntry &E : D.AddressArea) {
1867       uint64_t BaseAddr =
1868           E.Section->getParent()->Addr + E.Section->getOffset(0);
1869       write64le(Buf, BaseAddr + E.LowAddress);
1870       write64le(Buf + 8, BaseAddr + E.HighAddress);
1871       write32le(Buf + 16, E.CuIndex);
1872       Buf += 20;
1873     }
1874   }
1875
1876   // Write the symbol table.
1877   for (size_t I = 0; I < SymbolTable.getCapacity(); ++I) {
1878     GdbSymbol *Sym = SymbolTable.getSymbol(I);
1879     if (Sym) {
1880       size_t NameOffset =
1881           Sym->NameOffset + StringPoolOffset - ConstantPoolOffset;
1882       size_t CuVectorOffset = CuVectorsOffset[Sym->CuVectorIndex];
1883       write32le(Buf, NameOffset);
1884       write32le(Buf + 4, CuVectorOffset);
1885     }
1886     Buf += 8;
1887   }
1888
1889   // Write the CU vectors into the constant pool.
1890   for (std::set<uint32_t> &CuVec : CuVectors) {
1891     write32le(Buf, CuVec.size());
1892     Buf += 4;
1893     for (uint32_t Val : CuVec) {
1894       write32le(Buf, Val);
1895       Buf += 4;
1896     }
1897   }
1898
1899   StringPool.write(Buf);
1900 }
1901
1902 bool GdbIndexSection::empty() const {
1903   return !Out::DebugInfo;
1904 }
1905
1906 template <class ELFT>
1907 EhFrameHeader<ELFT>::EhFrameHeader()
1908     : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
1909
1910 // .eh_frame_hdr contains a binary search table of pointers to FDEs.
1911 // Each entry of the search table consists of two values,
1912 // the starting PC from where FDEs covers, and the FDE's address.
1913 // It is sorted by PC.
1914 template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1915   const endianness E = ELFT::TargetEndianness;
1916
1917   // Sort the FDE list by their PC and uniqueify. Usually there is only
1918   // one FDE for a PC (i.e. function), but if ICF merges two functions
1919   // into one, there can be more than one FDEs pointing to the address.
1920   auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1921   std::stable_sort(Fdes.begin(), Fdes.end(), Less);
1922   auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
1923   Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
1924
1925   Buf[0] = 1;
1926   Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1927   Buf[2] = DW_EH_PE_udata4;
1928   Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
1929   write32<E>(Buf + 4, In<ELFT>::EhFrame->getParent()->Addr - this->getVA() - 4);
1930   write32<E>(Buf + 8, Fdes.size());
1931   Buf += 12;
1932
1933   uint64_t VA = this->getVA();
1934   for (FdeData &Fde : Fdes) {
1935     write32<E>(Buf, Fde.Pc - VA);
1936     write32<E>(Buf + 4, Fde.FdeVA - VA);
1937     Buf += 8;
1938   }
1939 }
1940
1941 template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const {
1942   // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
1943   return 12 + In<ELFT>::EhFrame->NumFdes * 8;
1944 }
1945
1946 template <class ELFT>
1947 void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
1948   Fdes.push_back({Pc, FdeVA});
1949 }
1950
1951 template <class ELFT> bool EhFrameHeader<ELFT>::empty() const {
1952   return In<ELFT>::EhFrame->empty();
1953 }
1954
1955 template <class ELFT>
1956 VersionDefinitionSection<ELFT>::VersionDefinitionSection()
1957     : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
1958                        ".gnu.version_d") {}
1959
1960 static StringRef getFileDefName() {
1961   if (!Config->SoName.empty())
1962     return Config->SoName;
1963   return Config->OutputFile;
1964 }
1965
1966 template <class ELFT> void VersionDefinitionSection<ELFT>::finalizeContents() {
1967   FileDefNameOff = InX::DynStrTab->addString(getFileDefName());
1968   for (VersionDefinition &V : Config->VersionDefinitions)
1969     V.NameOff = InX::DynStrTab->addString(V.Name);
1970
1971   getParent()->Link = InX::DynStrTab->getParent()->SectionIndex;
1972
1973   // sh_info should be set to the number of definitions. This fact is missed in
1974   // documentation, but confirmed by binutils community:
1975   // https://sourceware.org/ml/binutils/2014-11/msg00355.html
1976   getParent()->Info = getVerDefNum();
1977 }
1978
1979 template <class ELFT>
1980 void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1981                                               StringRef Name, size_t NameOff) {
1982   auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1983   Verdef->vd_version = 1;
1984   Verdef->vd_cnt = 1;
1985   Verdef->vd_aux = sizeof(Elf_Verdef);
1986   Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1987   Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
1988   Verdef->vd_ndx = Index;
1989   Verdef->vd_hash = hashSysV(Name);
1990
1991   auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1992   Verdaux->vda_name = NameOff;
1993   Verdaux->vda_next = 0;
1994 }
1995
1996 template <class ELFT>
1997 void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
1998   writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1999
2000   for (VersionDefinition &V : Config->VersionDefinitions) {
2001     Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
2002     writeOne(Buf, V.Id, V.Name, V.NameOff);
2003   }
2004
2005   // Need to terminate the last version definition.
2006   Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
2007   Verdef->vd_next = 0;
2008 }
2009
2010 template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const {
2011   return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
2012 }
2013
2014 template <class ELFT>
2015 VersionTableSection<ELFT>::VersionTableSection()
2016     : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t),
2017                        ".gnu.version") {
2018   this->Entsize = sizeof(Elf_Versym);
2019 }
2020
2021 template <class ELFT> void VersionTableSection<ELFT>::finalizeContents() {
2022   // At the moment of june 2016 GNU docs does not mention that sh_link field
2023   // should be set, but Sun docs do. Also readelf relies on this field.
2024   getParent()->Link = InX::DynSymTab->getParent()->SectionIndex;
2025 }
2026
2027 template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const {
2028   return sizeof(Elf_Versym) * (InX::DynSymTab->getSymbols().size() + 1);
2029 }
2030
2031 template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
2032   auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
2033   for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) {
2034     OutVersym->vs_index = S.Symbol->symbol()->VersionId;
2035     ++OutVersym;
2036   }
2037 }
2038
2039 template <class ELFT> bool VersionTableSection<ELFT>::empty() const {
2040   return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty();
2041 }
2042
2043 template <class ELFT>
2044 VersionNeedSection<ELFT>::VersionNeedSection()
2045     : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t),
2046                        ".gnu.version_r") {
2047   // Identifiers in verneed section start at 2 because 0 and 1 are reserved
2048   // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
2049   // First identifiers are reserved by verdef section if it exist.
2050   NextIndex = getVerDefNum() + 1;
2051 }
2052
2053 template <class ELFT>
2054 void VersionNeedSection<ELFT>::addSymbol(SharedSymbol *SS) {
2055   auto *Ver = reinterpret_cast<const typename ELFT::Verdef *>(SS->Verdef);
2056   if (!Ver) {
2057     SS->symbol()->VersionId = VER_NDX_GLOBAL;
2058     return;
2059   }
2060
2061   auto *File = cast<SharedFile<ELFT>>(SS->File);
2062
2063   // If we don't already know that we need an Elf_Verneed for this DSO, prepare
2064   // to create one by adding it to our needed list and creating a dynstr entry
2065   // for the soname.
2066   if (File->VerdefMap.empty())
2067     Needed.push_back({File, InX::DynStrTab->addString(File->SoName)});
2068   typename SharedFile<ELFT>::NeededVer &NV = File->VerdefMap[Ver];
2069   // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
2070   // prepare to create one by allocating a version identifier and creating a
2071   // dynstr entry for the version name.
2072   if (NV.Index == 0) {
2073     NV.StrTab = InX::DynStrTab->addString(File->getStringTable().data() +
2074                                           Ver->getAux()->vda_name);
2075     NV.Index = NextIndex++;
2076   }
2077   SS->symbol()->VersionId = NV.Index;
2078 }
2079
2080 template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
2081   // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
2082   auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
2083   auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
2084
2085   for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
2086     // Create an Elf_Verneed for this DSO.
2087     Verneed->vn_version = 1;
2088     Verneed->vn_cnt = P.first->VerdefMap.size();
2089     Verneed->vn_file = P.second;
2090     Verneed->vn_aux =
2091         reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
2092     Verneed->vn_next = sizeof(Elf_Verneed);
2093     ++Verneed;
2094
2095     // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
2096     // VerdefMap, which will only contain references to needed version
2097     // definitions. Each Elf_Vernaux is based on the information contained in
2098     // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
2099     // pointers, but is deterministic because the pointers refer to Elf_Verdef
2100     // data structures within a single input file.
2101     for (auto &NV : P.first->VerdefMap) {
2102       Vernaux->vna_hash = NV.first->vd_hash;
2103       Vernaux->vna_flags = 0;
2104       Vernaux->vna_other = NV.second.Index;
2105       Vernaux->vna_name = NV.second.StrTab;
2106       Vernaux->vna_next = sizeof(Elf_Vernaux);
2107       ++Vernaux;
2108     }
2109
2110     Vernaux[-1].vna_next = 0;
2111   }
2112   Verneed[-1].vn_next = 0;
2113 }
2114
2115 template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {
2116   getParent()->Link = InX::DynStrTab->getParent()->SectionIndex;
2117   getParent()->Info = Needed.size();
2118 }
2119
2120 template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
2121   unsigned Size = Needed.size() * sizeof(Elf_Verneed);
2122   for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
2123     Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
2124   return Size;
2125 }
2126
2127 template <class ELFT> bool VersionNeedSection<ELFT>::empty() const {
2128   return getNeedNum() == 0;
2129 }
2130
2131 MergeSyntheticSection::MergeSyntheticSection(StringRef Name, uint32_t Type,
2132                                              uint64_t Flags, uint32_t Alignment)
2133     : SyntheticSection(Flags, Type, Alignment, Name),
2134       Builder(StringTableBuilder::RAW, Alignment) {}
2135
2136 void MergeSyntheticSection::addSection(MergeInputSection *MS) {
2137   MS->Parent = this;
2138   Sections.push_back(MS);
2139 }
2140
2141 void MergeSyntheticSection::writeTo(uint8_t *Buf) { Builder.write(Buf); }
2142
2143 bool MergeSyntheticSection::shouldTailMerge() const {
2144   return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2;
2145 }
2146
2147 void MergeSyntheticSection::finalizeTailMerge() {
2148   // Add all string pieces to the string table builder to create section
2149   // contents.
2150   for (MergeInputSection *Sec : Sections)
2151     for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
2152       if (Sec->Pieces[I].Live)
2153         Builder.add(Sec->getData(I));
2154
2155   // Fix the string table content. After this, the contents will never change.
2156   Builder.finalize();
2157
2158   // finalize() fixed tail-optimized strings, so we can now get
2159   // offsets of strings. Get an offset for each string and save it
2160   // to a corresponding StringPiece for easy access.
2161   for (MergeInputSection *Sec : Sections)
2162     for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
2163       if (Sec->Pieces[I].Live)
2164         Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I));
2165 }
2166
2167 void MergeSyntheticSection::finalizeNoTailMerge() {
2168   // Add all string pieces to the string table builder to create section
2169   // contents. Because we are not tail-optimizing, offsets of strings are
2170   // fixed when they are added to the builder (string table builder contains
2171   // a hash table from strings to offsets).
2172   for (MergeInputSection *Sec : Sections)
2173     for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
2174       if (Sec->Pieces[I].Live)
2175         Sec->Pieces[I].OutputOff = Builder.add(Sec->getData(I));
2176
2177   Builder.finalizeInOrder();
2178 }
2179
2180 void MergeSyntheticSection::finalizeContents() {
2181   if (shouldTailMerge())
2182     finalizeTailMerge();
2183   else
2184     finalizeNoTailMerge();
2185 }
2186
2187 size_t MergeSyntheticSection::getSize() const {
2188   return Builder.getSize();
2189 }
2190
2191 // This function decompresses compressed sections and scans over the input
2192 // sections to create mergeable synthetic sections. It removes
2193 // MergeInputSections from the input section array and adds new synthetic
2194 // sections at the location of the first input section that it replaces. It then
2195 // finalizes each synthetic section in order to compute an output offset for
2196 // each piece of each input section.
2197 void elf::decompressAndMergeSections() {
2198   // splitIntoPieces needs to be called on each MergeInputSection before calling
2199   // finalizeContents(). Do that first.
2200   parallelForEach(InputSections.begin(), InputSections.end(),
2201                   [](InputSectionBase *S) {
2202                     if (!S->Live)
2203                       return;
2204                     if (Decompressor::isCompressedELFSection(S->Flags, S->Name))
2205                       S->uncompress();
2206                     if (auto *MS = dyn_cast<MergeInputSection>(S))
2207                       MS->splitIntoPieces();
2208                   });
2209
2210   std::vector<MergeSyntheticSection *> MergeSections;
2211   for (InputSectionBase *&S : InputSections) {
2212     MergeInputSection *MS = dyn_cast<MergeInputSection>(S);
2213     if (!MS)
2214       continue;
2215
2216     // We do not want to handle sections that are not alive, so just remove
2217     // them instead of trying to merge.
2218     if (!MS->Live)
2219       continue;
2220
2221     StringRef OutsecName = getOutputSectionName(MS->Name);
2222     uint64_t Flags = MS->Flags & ~(uint64_t)SHF_GROUP;
2223     uint32_t Alignment = std::max<uint32_t>(MS->Alignment, MS->Entsize);
2224
2225     auto I = llvm::find_if(MergeSections, [=](MergeSyntheticSection *Sec) {
2226       return Sec->Name == OutsecName && Sec->Flags == Flags &&
2227              Sec->Alignment == Alignment;
2228     });
2229     if (I == MergeSections.end()) {
2230       MergeSyntheticSection *Syn =
2231           make<MergeSyntheticSection>(OutsecName, MS->Type, Flags, Alignment);
2232       MergeSections.push_back(Syn);
2233       I = std::prev(MergeSections.end());
2234       S = Syn;
2235     } else {
2236       S = nullptr;
2237     }
2238     (*I)->addSection(MS);
2239   }
2240   for (auto *MS : MergeSections)
2241     MS->finalizeContents();
2242
2243   std::vector<InputSectionBase *> &V = InputSections;
2244   V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
2245 }
2246
2247 MipsRldMapSection::MipsRldMapSection()
2248     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, Config->Wordsize,
2249                        ".rld_map") {}
2250
2251 ARMExidxSentinelSection::ARMExidxSentinelSection()
2252     : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX,
2253                        Config->Wordsize, ".ARM.exidx") {}
2254
2255 // Write a terminating sentinel entry to the end of the .ARM.exidx table.
2256 // This section will have been sorted last in the .ARM.exidx table.
2257 // This table entry will have the form:
2258 // | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND |
2259 // The sentinel must have the PREL31 value of an address higher than any
2260 // address described by any other table entry.
2261 void ARMExidxSentinelSection::writeTo(uint8_t *Buf) {
2262   // The Sections are sorted in order of ascending PREL31 address with the
2263   // sentinel last. We need to find the InputSection that precedes the
2264   // sentinel. By construction the Sentinel is in the last
2265   // InputSectionDescription as the InputSection that precedes it.
2266   OutputSectionCommand *C = Script->getCmd(getParent());
2267   auto ISD = std::find_if(C->Commands.rbegin(), C->Commands.rend(),
2268                           [](const BaseCommand *Base) {
2269                             return isa<InputSectionDescription>(Base);
2270                           });
2271   auto L = cast<InputSectionDescription>(*ISD);
2272   InputSection *Highest = L->Sections[L->Sections.size() - 2];
2273   InputSection *LS = Highest->getLinkOrderDep();
2274   uint64_t S = LS->getParent()->Addr + LS->getOffset(LS->getSize());
2275   uint64_t P = getVA();
2276   Target->relocateOne(Buf, R_ARM_PREL31, S - P);
2277   write32le(Buf + 4, 0x1);
2278 }
2279
2280 ThunkSection::ThunkSection(OutputSection *OS, uint64_t Off)
2281     : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS,
2282                        Config->Wordsize, ".text.thunk") {
2283   this->Parent = OS;
2284   this->OutSecOff = Off;
2285 }
2286
2287 void ThunkSection::addThunk(Thunk *T) {
2288   uint64_t Off = alignTo(Size, T->alignment);
2289   T->Offset = Off;
2290   Thunks.push_back(T);
2291   T->addSymbols(*this);
2292   Size = Off + T->size();
2293 }
2294
2295 void ThunkSection::writeTo(uint8_t *Buf) {
2296   for (const Thunk *T : Thunks)
2297     T->writeTo(Buf + T->Offset, *this);
2298 }
2299
2300 InputSection *ThunkSection::getTargetInputSection() const {
2301   const Thunk *T = Thunks.front();
2302   return T->getTargetInputSection();
2303 }
2304
2305 InputSection *InX::ARMAttributes;
2306 BssSection *InX::Bss;
2307 BssSection *InX::BssRelRo;
2308 BuildIdSection *InX::BuildId;
2309 InputSection *InX::Common;
2310 SyntheticSection *InX::Dynamic;
2311 StringTableSection *InX::DynStrTab;
2312 SymbolTableBaseSection *InX::DynSymTab;
2313 InputSection *InX::Interp;
2314 GdbIndexSection *InX::GdbIndex;
2315 GotSection *InX::Got;
2316 GotPltSection *InX::GotPlt;
2317 GnuHashTableSection *InX::GnuHashTab;
2318 IgotPltSection *InX::IgotPlt;
2319 MipsGotSection *InX::MipsGot;
2320 MipsRldMapSection *InX::MipsRldMap;
2321 PltSection *InX::Plt;
2322 PltSection *InX::Iplt;
2323 StringTableSection *InX::ShStrTab;
2324 StringTableSection *InX::StrTab;
2325 SymbolTableBaseSection *InX::SymTab;
2326
2327 template void PltSection::addEntry<ELF32LE>(SymbolBody &Sym);
2328 template void PltSection::addEntry<ELF32BE>(SymbolBody &Sym);
2329 template void PltSection::addEntry<ELF64LE>(SymbolBody &Sym);
2330 template void PltSection::addEntry<ELF64BE>(SymbolBody &Sym);
2331
2332 template InputSection *elf::createCommonSection<ELF32LE>();
2333 template InputSection *elf::createCommonSection<ELF32BE>();
2334 template InputSection *elf::createCommonSection<ELF64LE>();
2335 template InputSection *elf::createCommonSection<ELF64BE>();
2336
2337 template MergeInputSection *elf::createCommentSection<ELF32LE>();
2338 template MergeInputSection *elf::createCommentSection<ELF32BE>();
2339 template MergeInputSection *elf::createCommentSection<ELF64LE>();
2340 template MergeInputSection *elf::createCommentSection<ELF64BE>();
2341
2342 template class elf::MipsAbiFlagsSection<ELF32LE>;
2343 template class elf::MipsAbiFlagsSection<ELF32BE>;
2344 template class elf::MipsAbiFlagsSection<ELF64LE>;
2345 template class elf::MipsAbiFlagsSection<ELF64BE>;
2346
2347 template class elf::MipsOptionsSection<ELF32LE>;
2348 template class elf::MipsOptionsSection<ELF32BE>;
2349 template class elf::MipsOptionsSection<ELF64LE>;
2350 template class elf::MipsOptionsSection<ELF64BE>;
2351
2352 template class elf::MipsReginfoSection<ELF32LE>;
2353 template class elf::MipsReginfoSection<ELF32BE>;
2354 template class elf::MipsReginfoSection<ELF64LE>;
2355 template class elf::MipsReginfoSection<ELF64BE>;
2356
2357 template class elf::DynamicSection<ELF32LE>;
2358 template class elf::DynamicSection<ELF32BE>;
2359 template class elf::DynamicSection<ELF64LE>;
2360 template class elf::DynamicSection<ELF64BE>;
2361
2362 template class elf::RelocationSection<ELF32LE>;
2363 template class elf::RelocationSection<ELF32BE>;
2364 template class elf::RelocationSection<ELF64LE>;
2365 template class elf::RelocationSection<ELF64BE>;
2366
2367 template class elf::SymbolTableSection<ELF32LE>;
2368 template class elf::SymbolTableSection<ELF32BE>;
2369 template class elf::SymbolTableSection<ELF64LE>;
2370 template class elf::SymbolTableSection<ELF64BE>;
2371
2372 template class elf::HashTableSection<ELF32LE>;
2373 template class elf::HashTableSection<ELF32BE>;
2374 template class elf::HashTableSection<ELF64LE>;
2375 template class elf::HashTableSection<ELF64BE>;
2376
2377 template class elf::EhFrameHeader<ELF32LE>;
2378 template class elf::EhFrameHeader<ELF32BE>;
2379 template class elf::EhFrameHeader<ELF64LE>;
2380 template class elf::EhFrameHeader<ELF64BE>;
2381
2382 template class elf::VersionTableSection<ELF32LE>;
2383 template class elf::VersionTableSection<ELF32BE>;
2384 template class elf::VersionTableSection<ELF64LE>;
2385 template class elf::VersionTableSection<ELF64BE>;
2386
2387 template class elf::VersionNeedSection<ELF32LE>;
2388 template class elf::VersionNeedSection<ELF32BE>;
2389 template class elf::VersionNeedSection<ELF64LE>;
2390 template class elf::VersionNeedSection<ELF64BE>;
2391
2392 template class elf::VersionDefinitionSection<ELF32LE>;
2393 template class elf::VersionDefinitionSection<ELF32BE>;
2394 template class elf::VersionDefinitionSection<ELF64LE>;
2395 template class elf::VersionDefinitionSection<ELF64BE>;
2396
2397 template class elf::EhFrameSection<ELF32LE>;
2398 template class elf::EhFrameSection<ELF32BE>;
2399 template class elf::EhFrameSection<ELF64LE>;
2400 template class elf::EhFrameSection<ELF64BE>;