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