1 //===- SyntheticSections.cpp ----------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains linker-synthesized sections. Currently,
11 // synthetic sections are created either output sections or input sections,
12 // but we are rewriting code so that all synthetic sections are created as
13 // input sections.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "SyntheticSections.h"
18 #include "Config.h"
19 #include "Error.h"
20 #include "InputFiles.h"
21 #include "LinkerScript.h"
22 #include "Memory.h"
23 #include "OutputSections.h"
24 #include "Strings.h"
25 #include "SymbolTable.h"
26 #include "Target.h"
27 #include "Threads.h"
28 #include "Writer.h"
29 #include "lld/Config/Version.h"
30 #include "llvm/Support/Dwarf.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/MD5.h"
33 #include "llvm/Support/RandomNumberGenerator.h"
34 #include "llvm/Support/SHA1.h"
35 #include "llvm/Support/xxhash.h"
36 #include <cstdlib>
37 
38 using namespace llvm;
39 using namespace llvm::dwarf;
40 using namespace llvm::ELF;
41 using namespace llvm::object;
42 using namespace llvm::support;
43 using namespace llvm::support::endian;
44 
45 using namespace lld;
46 using namespace lld::elf;
47 
48 template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() {
49   std::vector<DefinedCommon *> V;
50   for (Symbol *S : Symtab<ELFT>::X->getSymbols())
51     if (auto *B = dyn_cast<DefinedCommon>(S->body()))
52       V.push_back(B);
53   return V;
54 }
55 
56 // Find all common symbols and allocate space for them.
57 template <class ELFT> InputSection<ELFT> *elf::createCommonSection() {
58   auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1,
59                                        ArrayRef<uint8_t>(), "COMMON");
60   Ret->Live = true;
61 
62   // Sort the common symbols by alignment as an heuristic to pack them better.
63   std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>();
64   std::stable_sort(Syms.begin(), Syms.end(),
65                    [](const DefinedCommon *A, const DefinedCommon *B) {
66                      return A->Alignment > B->Alignment;
67                    });
68 
69   // Assign offsets to symbols.
70   size_t Size = 0;
71   size_t Alignment = 1;
72   for (DefinedCommon *Sym : Syms) {
73     Alignment = std::max<size_t>(Alignment, Sym->Alignment);
74     Size = alignTo(Size, Sym->Alignment);
75 
76     // Compute symbol offset relative to beginning of input section.
77     Sym->Offset = Size;
78     Size += Sym->Size;
79   }
80   Ret->Alignment = Alignment;
81   Ret->Data = makeArrayRef<uint8_t>(nullptr, Size);
82   return Ret;
83 }
84 
85 // Returns an LLD version string.
86 static ArrayRef<uint8_t> getVersion() {
87   // Check LLD_VERSION first for ease of testing.
88   // You can get consitent output by using the environment variable.
89   // This is only for testing.
90   StringRef S = getenv("LLD_VERSION");
91   if (S.empty())
92     S = Saver.save(Twine("Linker: ") + getLLDVersion());
93 
94   // +1 to include the terminating '\0'.
95   return {(const uint8_t *)S.data(), S.size() + 1};
96 }
97 
98 // Creates a .comment section containing LLD version info.
99 // With this feature, you can identify LLD-generated binaries easily
100 // by "objdump -s -j .comment <file>".
101 // The returned object is a mergeable string section.
102 template <class ELFT> MergeInputSection<ELFT> *elf::createCommentSection() {
103   typename ELFT::Shdr Hdr = {};
104   Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
105   Hdr.sh_type = SHT_PROGBITS;
106   Hdr.sh_entsize = 1;
107   Hdr.sh_addralign = 1;
108 
109   auto *Ret = make<MergeInputSection<ELFT>>(/*file=*/nullptr, &Hdr, ".comment");
110   Ret->Data = getVersion();
111   Ret->splitIntoPieces();
112   return Ret;
113 }
114 
115 // .MIPS.abiflags section.
116 template <class ELFT>
117 MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags)
118     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
119       Flags(Flags) {}
120 
121 template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
122   memcpy(Buf, &Flags, sizeof(Flags));
123 }
124 
125 template <class ELFT>
126 MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
127   Elf_Mips_ABIFlags Flags = {};
128   bool Create = false;
129 
130   for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
131     if (!Sec->Live || Sec->Type != SHT_MIPS_ABIFLAGS)
132       continue;
133     Sec->Live = false;
134     Create = true;
135 
136     std::string Filename = toString(Sec->getFile());
137     if (Sec->Data.size() != sizeof(Elf_Mips_ABIFlags)) {
138       error(Filename + ": invalid size of .MIPS.abiflags section");
139       return nullptr;
140     }
141     auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
142     if (S->version != 0) {
143       error(Filename + ": unexpected .MIPS.abiflags version " +
144             Twine(S->version));
145       return nullptr;
146     }
147 
148     // LLD checks ISA compatibility in getMipsEFlags(). Here we just
149     // select the highest number of ISA/Rev/Ext.
150     Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
151     Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
152     Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
153     Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
154     Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
155     Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
156     Flags.ases |= S->ases;
157     Flags.flags1 |= S->flags1;
158     Flags.flags2 |= S->flags2;
159     Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
160   };
161 
162   if (Create)
163     return make<MipsAbiFlagsSection<ELFT>>(Flags);
164   return nullptr;
165 }
166 
167 // .MIPS.options section.
168 template <class ELFT>
169 MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo)
170     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
171       Reginfo(Reginfo) {}
172 
173 template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
174   auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
175   Options->kind = ODK_REGINFO;
176   Options->size = getSize();
177 
178   if (!Config->Relocatable)
179     Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp();
180   memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
181 }
182 
183 template <class ELFT>
184 MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
185   // N64 ABI only.
186   if (!ELFT::Is64Bits)
187     return nullptr;
188 
189   Elf_Mips_RegInfo Reginfo = {};
190   bool Create = false;
191 
192   for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
193     if (!Sec->Live || Sec->Type != SHT_MIPS_OPTIONS)
194       continue;
195     Sec->Live = false;
196     Create = true;
197 
198     std::string Filename = toString(Sec->getFile());
199     ArrayRef<uint8_t> D = Sec->Data;
200 
201     while (!D.empty()) {
202       if (D.size() < sizeof(Elf_Mips_Options)) {
203         error(Filename + ": invalid size of .MIPS.options section");
204         break;
205       }
206 
207       auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
208       if (Opt->kind == ODK_REGINFO) {
209         if (Config->Relocatable && Opt->getRegInfo().ri_gp_value)
210           error(Filename + ": unsupported non-zero ri_gp_value");
211         Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
212         Sec->getFile()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
213         break;
214       }
215 
216       if (!Opt->size)
217         fatal(Filename + ": zero option descriptor size");
218       D = D.slice(Opt->size);
219     }
220   };
221 
222   if (Create)
223     return make<MipsOptionsSection<ELFT>>(Reginfo);
224   return nullptr;
225 }
226 
227 // MIPS .reginfo section.
228 template <class ELFT>
229 MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo)
230     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
231       Reginfo(Reginfo) {}
232 
233 template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
234   if (!Config->Relocatable)
235     Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp();
236   memcpy(Buf, &Reginfo, sizeof(Reginfo));
237 }
238 
239 template <class ELFT>
240 MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
241   // Section should be alive for O32 and N32 ABIs only.
242   if (ELFT::Is64Bits)
243     return nullptr;
244 
245   Elf_Mips_RegInfo Reginfo = {};
246   bool Create = false;
247 
248   for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
249     if (!Sec->Live || Sec->Type != SHT_MIPS_REGINFO)
250       continue;
251     Sec->Live = false;
252     Create = true;
253 
254     if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
255       error(toString(Sec->getFile()) + ": invalid size of .reginfo section");
256       return nullptr;
257     }
258     auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
259     if (Config->Relocatable && R->ri_gp_value)
260       error(toString(Sec->getFile()) + ": unsupported non-zero ri_gp_value");
261 
262     Reginfo.ri_gprmask |= R->ri_gprmask;
263     Sec->getFile()->MipsGp0 = R->ri_gp_value;
264   };
265 
266   if (Create)
267     return make<MipsReginfoSection<ELFT>>(Reginfo);
268   return nullptr;
269 }
270 
271 template <class ELFT> InputSection<ELFT> *elf::createInterpSection() {
272   auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC, SHT_PROGBITS, 1,
273                                        ArrayRef<uint8_t>(), ".interp");
274   Ret->Live = true;
275 
276   // StringSaver guarantees that the returned string ends with '\0'.
277   StringRef S = Saver.save(Config->DynamicLinker);
278   Ret->Data = {(const uint8_t *)S.data(), S.size() + 1};
279   return Ret;
280 }
281 
282 static size_t getHashSize() {
283   switch (Config->BuildId) {
284   case BuildIdKind::Fast:
285     return 8;
286   case BuildIdKind::Md5:
287   case BuildIdKind::Uuid:
288     return 16;
289   case BuildIdKind::Sha1:
290     return 20;
291   case BuildIdKind::Hexstring:
292     return Config->BuildIdVector.size();
293   default:
294     llvm_unreachable("unknown BuildIdKind");
295   }
296 }
297 
298 template <class ELFT>
299 BuildIdSection<ELFT>::BuildIdSection()
300     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"),
301       HashSize(getHashSize()) {}
302 
303 template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
304   const endianness E = ELFT::TargetEndianness;
305   write32<E>(Buf, 4);                   // Name size
306   write32<E>(Buf + 4, HashSize);        // Content size
307   write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
308   memcpy(Buf + 12, "GNU", 4);           // Name string
309   HashBuf = Buf + 16;
310 }
311 
312 // Split one uint8 array into small pieces of uint8 arrays.
313 static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
314                                             size_t ChunkSize) {
315   std::vector<ArrayRef<uint8_t>> Ret;
316   while (Arr.size() > ChunkSize) {
317     Ret.push_back(Arr.take_front(ChunkSize));
318     Arr = Arr.drop_front(ChunkSize);
319   }
320   if (!Arr.empty())
321     Ret.push_back(Arr);
322   return Ret;
323 }
324 
325 // Computes a hash value of Data using a given hash function.
326 // In order to utilize multiple cores, we first split data into 1MB
327 // chunks, compute a hash for each chunk, and then compute a hash value
328 // of the hash values.
329 template <class ELFT>
330 void BuildIdSection<ELFT>::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   forLoop(0, Chunks.size(),
338           [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); });
339 
340   // Write to the final output buffer.
341   HashFn(HashBuf, Hashes);
342 }
343 
344 template <class ELFT>
345 void BuildIdSection<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
346   switch (Config->BuildId) {
347   case BuildIdKind::Fast:
348     computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
349       write64le(Dest, xxHash64(toStringRef(Arr)));
350     });
351     break;
352   case BuildIdKind::Md5:
353     computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
354       memcpy(Dest, MD5::hash(Arr).data(), 16);
355     });
356     break;
357   case BuildIdKind::Sha1:
358     computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
359       memcpy(Dest, SHA1::hash(Arr).data(), 20);
360     });
361     break;
362   case BuildIdKind::Uuid:
363     if (getRandomBytes(HashBuf, HashSize))
364       error("entropy source failure");
365     break;
366   case BuildIdKind::Hexstring:
367     memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size());
368     break;
369   default:
370     llvm_unreachable("unknown BuildIdKind");
371   }
372 }
373 
374 template <class ELFT>
375 GotSection<ELFT>::GotSection()
376     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
377                              Target->GotEntrySize, ".got") {}
378 
379 template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
380   Sym.GotIndex = NumEntries;
381   ++NumEntries;
382 }
383 
384 template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
385   if (Sym.GlobalDynIndex != -1U)
386     return false;
387   Sym.GlobalDynIndex = NumEntries;
388   // Global Dynamic TLS entries take two GOT slots.
389   NumEntries += 2;
390   return true;
391 }
392 
393 // Reserves TLS entries for a TLS module ID and a TLS block offset.
394 // In total it takes two GOT slots.
395 template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
396   if (TlsIndexOff != uint32_t(-1))
397     return false;
398   TlsIndexOff = NumEntries * sizeof(uintX_t);
399   NumEntries += 2;
400   return true;
401 }
402 
403 template <class ELFT>
404 typename GotSection<ELFT>::uintX_t
405 GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
406   return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
407 }
408 
409 template <class ELFT>
410 typename GotSection<ELFT>::uintX_t
411 GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
412   return B.GlobalDynIndex * sizeof(uintX_t);
413 }
414 
415 template <class ELFT> void GotSection<ELFT>::finalize() {
416   Size = NumEntries * sizeof(uintX_t);
417 }
418 
419 template <class ELFT> bool GotSection<ELFT>::empty() const {
420   // If we have a relocation that is relative to GOT (such as GOTOFFREL),
421   // we need to emit a GOT even if it's empty.
422   return NumEntries == 0 && !HasGotOffRel;
423 }
424 
425 template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
426   this->relocate(Buf, Buf + Size);
427 }
428 
429 template <class ELFT>
430 MipsGotSection<ELFT>::MipsGotSection()
431     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL,
432                              SHT_PROGBITS, Target->GotEntrySize, ".got") {}
433 
434 template <class ELFT>
435 void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, uintX_t Addend,
436                                     RelExpr Expr) {
437   // For "true" local symbols which can be referenced from the same module
438   // only compiler creates two instructions for address loading:
439   //
440   // lw   $8, 0($gp) # R_MIPS_GOT16
441   // addi $8, $8, 0  # R_MIPS_LO16
442   //
443   // The first instruction loads high 16 bits of the symbol address while
444   // the second adds an offset. That allows to reduce number of required
445   // GOT entries because only one global offset table entry is necessary
446   // for every 64 KBytes of local data. So for local symbols we need to
447   // allocate number of GOT entries to hold all required "page" addresses.
448   //
449   // All global symbols (hidden and regular) considered by compiler uniformly.
450   // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
451   // to load address of the symbol. So for each such symbol we need to
452   // allocate dedicated GOT entry to store its address.
453   //
454   // If a symbol is preemptible we need help of dynamic linker to get its
455   // final address. The corresponding GOT entries are allocated in the
456   // "global" part of GOT. Entries for non preemptible global symbol allocated
457   // in the "local" part of GOT.
458   //
459   // See "Global Offset Table" in Chapter 5:
460   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
461   if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
462     // At this point we do not know final symbol value so to reduce number
463     // of allocated GOT entries do the following trick. Save all output
464     // sections referenced by GOT relocations. Then later in the `finalize`
465     // method calculate number of "pages" required to cover all saved output
466     // section and allocate appropriate number of GOT entries.
467     PageIndexMap.insert({cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec, 0});
468     return;
469   }
470   if (Sym.isTls()) {
471     // GOT entries created for MIPS TLS relocations behave like
472     // almost GOT entries from other ABIs. They go to the end
473     // of the global offset table.
474     Sym.GotIndex = TlsEntries.size();
475     TlsEntries.push_back(&Sym);
476     return;
477   }
478   auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) {
479     if (S.isInGot() && !A)
480       return;
481     size_t NewIndex = Items.size();
482     if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
483       return;
484     Items.emplace_back(&S, A);
485     if (!A)
486       S.GotIndex = NewIndex;
487   };
488   if (Sym.isPreemptible()) {
489     // Ignore addends for preemptible symbols. They got single GOT entry anyway.
490     AddEntry(Sym, 0, GlobalEntries);
491     Sym.IsInGlobalMipsGot = true;
492   } else if (Expr == R_MIPS_GOT_OFF32) {
493     AddEntry(Sym, Addend, LocalEntries32);
494     Sym.Is32BitMipsGot = true;
495   } else {
496     // Hold local GOT entries accessed via a 16-bit index separately.
497     // That allows to write them in the beginning of the GOT and keep
498     // their indexes as less as possible to escape relocation's overflow.
499     AddEntry(Sym, Addend, LocalEntries);
500   }
501 }
502 
503 template <class ELFT>
504 bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
505   if (Sym.GlobalDynIndex != -1U)
506     return false;
507   Sym.GlobalDynIndex = TlsEntries.size();
508   // Global Dynamic TLS entries take two GOT slots.
509   TlsEntries.push_back(nullptr);
510   TlsEntries.push_back(&Sym);
511   return true;
512 }
513 
514 // Reserves TLS entries for a TLS module ID and a TLS block offset.
515 // In total it takes two GOT slots.
516 template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() {
517   if (TlsIndexOff != uint32_t(-1))
518     return false;
519   TlsIndexOff = TlsEntries.size() * sizeof(uintX_t);
520   TlsEntries.push_back(nullptr);
521   TlsEntries.push_back(nullptr);
522   return true;
523 }
524 
525 static uint64_t getMipsPageAddr(uint64_t Addr) {
526   return (Addr + 0x8000) & ~0xffff;
527 }
528 
529 static uint64_t getMipsPageCount(uint64_t Size) {
530   return (Size + 0xfffe) / 0xffff + 1;
531 }
532 
533 template <class ELFT>
534 typename MipsGotSection<ELFT>::uintX_t
535 MipsGotSection<ELFT>::getPageEntryOffset(const SymbolBody &B,
536                                          uintX_t Addend) const {
537   const OutputSectionBase *OutSec =
538       cast<DefinedRegular<ELFT>>(&B)->Section->OutSec;
539   uintX_t SecAddr = getMipsPageAddr(OutSec->Addr);
540   uintX_t SymAddr = getMipsPageAddr(B.getVA<ELFT>(Addend));
541   uintX_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
542   assert(Index < PageEntriesNum);
543   return (HeaderEntriesNum + Index) * sizeof(uintX_t);
544 }
545 
546 template <class ELFT>
547 typename MipsGotSection<ELFT>::uintX_t
548 MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B,
549                                          uintX_t Addend) const {
550   // Calculate offset of the GOT entries block: TLS, global, local.
551   uintX_t Index = HeaderEntriesNum + PageEntriesNum;
552   if (B.isTls())
553     Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
554   else if (B.IsInGlobalMipsGot)
555     Index += LocalEntries.size() + LocalEntries32.size();
556   else if (B.Is32BitMipsGot)
557     Index += LocalEntries.size();
558   // Calculate offset of the GOT entry in the block.
559   if (B.isInGot())
560     Index += B.GotIndex;
561   else {
562     auto It = EntryIndexMap.find({&B, Addend});
563     assert(It != EntryIndexMap.end());
564     Index += It->second;
565   }
566   return Index * sizeof(uintX_t);
567 }
568 
569 template <class ELFT>
570 typename MipsGotSection<ELFT>::uintX_t
571 MipsGotSection<ELFT>::getTlsOffset() const {
572   return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t);
573 }
574 
575 template <class ELFT>
576 typename MipsGotSection<ELFT>::uintX_t
577 MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
578   return B.GlobalDynIndex * sizeof(uintX_t);
579 }
580 
581 template <class ELFT>
582 const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const {
583   return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first;
584 }
585 
586 template <class ELFT>
587 unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const {
588   return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
589          LocalEntries32.size();
590 }
591 
592 template <class ELFT> void MipsGotSection<ELFT>::finalize() {
593   PageEntriesNum = 0;
594   for (std::pair<const OutputSectionBase *, size_t> &P : PageIndexMap) {
595     // For each output section referenced by GOT page relocations calculate
596     // and save into PageIndexMap an upper bound of MIPS GOT entries required
597     // to store page addresses of local symbols. We assume the worst case -
598     // each 64kb page of the output section has at least one GOT relocation
599     // against it. And take in account the case when the section intersects
600     // page boundaries.
601     P.second = PageEntriesNum;
602     PageEntriesNum += getMipsPageCount(P.first->Size);
603   }
604   Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
605          sizeof(uintX_t);
606 }
607 
608 template <class ELFT> bool MipsGotSection<ELFT>::empty() const {
609   // We add the .got section to the result for dynamic MIPS target because
610   // its address and properties are mentioned in the .dynamic section.
611   return Config->Relocatable;
612 }
613 
614 template <class ELFT>
615 typename MipsGotSection<ELFT>::uintX_t MipsGotSection<ELFT>::getGp() const {
616   return ElfSym<ELFT>::MipsGp->template getVA<ELFT>(0);
617 }
618 
619 template <class ELFT>
620 static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
621   typedef typename ELFT::uint uintX_t;
622   write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
623 }
624 
625 template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) {
626   // Set the MSB of the second GOT slot. This is not required by any
627   // MIPS ABI documentation, though.
628   //
629   // There is a comment in glibc saying that "The MSB of got[1] of a
630   // gnu object is set to identify gnu objects," and in GNU gold it
631   // says "the second entry will be used by some runtime loaders".
632   // But how this field is being used is unclear.
633   //
634   // We are not really willing to mimic other linkers behaviors
635   // without understanding why they do that, but because all files
636   // generated by GNU tools have this special GOT value, and because
637   // we've been doing this for years, it is probably a safe bet to
638   // keep doing this for now. We really need to revisit this to see
639   // if we had to do this.
640   auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
641   P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
642   Buf += HeaderEntriesNum * sizeof(uintX_t);
643   // Write 'page address' entries to the local part of the GOT.
644   for (std::pair<const OutputSectionBase *, size_t> &L : PageIndexMap) {
645     size_t PageCount = getMipsPageCount(L.first->Size);
646     uintX_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
647     for (size_t PI = 0; PI < PageCount; ++PI) {
648       uint8_t *Entry = Buf + (L.second + PI) * sizeof(uintX_t);
649       writeUint<ELFT>(Entry, FirstPageAddr + PI * 0x10000);
650     }
651   }
652   Buf += PageEntriesNum * sizeof(uintX_t);
653   auto AddEntry = [&](const GotEntry &SA) {
654     uint8_t *Entry = Buf;
655     Buf += sizeof(uintX_t);
656     const SymbolBody *Body = SA.first;
657     uintX_t VA = Body->template getVA<ELFT>(SA.second);
658     writeUint<ELFT>(Entry, VA);
659   };
660   std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
661   std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
662   std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
663   // Initialize TLS-related GOT entries. If the entry has a corresponding
664   // dynamic relocations, leave it initialized by zero. Write down adjusted
665   // TLS symbol's values otherwise. To calculate the adjustments use offsets
666   // for thread-local storage.
667   // https://www.linux-mips.org/wiki/NPTL
668   if (TlsIndexOff != -1U && !Config->Pic)
669     writeUint<ELFT>(Buf + TlsIndexOff, 1);
670   for (const SymbolBody *B : TlsEntries) {
671     if (!B || B->isPreemptible())
672       continue;
673     uintX_t VA = B->getVA<ELFT>();
674     if (B->GotIndex != -1U) {
675       uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
676       writeUint<ELFT>(Entry, VA - 0x7000);
677     }
678     if (B->GlobalDynIndex != -1U) {
679       uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
680       writeUint<ELFT>(Entry, 1);
681       Entry += sizeof(uintX_t);
682       writeUint<ELFT>(Entry, VA - 0x8000);
683     }
684   }
685 }
686 
687 template <class ELFT>
688 GotPltSection<ELFT>::GotPltSection()
689     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
690                              Target->GotPltEntrySize, ".got.plt") {}
691 
692 template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
693   Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
694   Entries.push_back(&Sym);
695 }
696 
697 template <class ELFT> size_t GotPltSection<ELFT>::getSize() const {
698   return (Target->GotPltHeaderEntriesNum + Entries.size()) *
699          Target->GotPltEntrySize;
700 }
701 
702 template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
703   Target->writeGotPltHeader(Buf);
704   Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
705   for (const SymbolBody *B : Entries) {
706     Target->writeGotPlt(Buf, *B);
707     Buf += sizeof(uintX_t);
708   }
709 }
710 
711 // On ARM the IgotPltSection is part of the GotSection, on other Targets it is
712 // part of the .got.plt
713 template <class ELFT>
714 IgotPltSection<ELFT>::IgotPltSection()
715     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
716                              Target->GotPltEntrySize,
717                              Config->EMachine == EM_ARM ? ".got" : ".got.plt") {
718 }
719 
720 template <class ELFT> void IgotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
721   Sym.IsInIgot = true;
722   Sym.GotPltIndex = Entries.size();
723   Entries.push_back(&Sym);
724 }
725 
726 template <class ELFT> size_t IgotPltSection<ELFT>::getSize() const {
727   return Entries.size() * Target->GotPltEntrySize;
728 }
729 
730 template <class ELFT> void IgotPltSection<ELFT>::writeTo(uint8_t *Buf) {
731   for (const SymbolBody *B : Entries) {
732     Target->writeIgotPlt(Buf, *B);
733     Buf += sizeof(uintX_t);
734   }
735 }
736 
737 template <class ELFT>
738 StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
739     : SyntheticSection<ELFT>(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1,
740                              Name),
741       Dynamic(Dynamic) {}
742 
743 // Adds a string to the string table. If HashIt is true we hash and check for
744 // duplicates. It is optional because the name of global symbols are already
745 // uniqued and hashing them again has a big cost for a small value: uniquing
746 // them with some other string that happens to be the same.
747 template <class ELFT>
748 unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
749   if (HashIt) {
750     auto R = StringMap.insert(std::make_pair(S, this->Size));
751     if (!R.second)
752       return R.first->second;
753   }
754   unsigned Ret = this->Size;
755   this->Size = this->Size + S.size() + 1;
756   Strings.push_back(S);
757   return Ret;
758 }
759 
760 template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
761   // ELF string tables start with NUL byte, so advance the pointer by one.
762   ++Buf;
763   for (StringRef S : Strings) {
764     memcpy(Buf, S.data(), S.size());
765     Buf += S.size() + 1;
766   }
767 }
768 
769 // Returns the number of version definition entries. Because the first entry
770 // is for the version definition itself, it is the number of versioned symbols
771 // plus one. Note that we don't support multiple versions yet.
772 static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
773 
774 template <class ELFT>
775 DynamicSection<ELFT>::DynamicSection()
776     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC,
777                              sizeof(uintX_t), ".dynamic") {
778   this->Entsize = ELFT::Is64Bits ? 16 : 8;
779   // .dynamic section is not writable on MIPS.
780   // See "Special Section" in Chapter 4 in the following document:
781   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
782   if (Config->EMachine == EM_MIPS)
783     this->Flags = SHF_ALLOC;
784 
785   addEntries();
786 }
787 
788 // There are some dynamic entries that don't depend on other sections.
789 // Such entries can be set early.
790 template <class ELFT> void DynamicSection<ELFT>::addEntries() {
791   // Add strings to .dynstr early so that .dynstr's size will be
792   // fixed early.
793   for (StringRef S : Config->AuxiliaryList)
794     add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)});
795   if (!Config->RPath.empty())
796     add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
797          In<ELFT>::DynStrTab->addString(Config->RPath)});
798   for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
799     if (F->isNeeded())
800       add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())});
801   if (!Config->SoName.empty())
802     add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)});
803 
804   // Set DT_FLAGS and DT_FLAGS_1.
805   uint32_t DtFlags = 0;
806   uint32_t DtFlags1 = 0;
807   if (Config->Bsymbolic)
808     DtFlags |= DF_SYMBOLIC;
809   if (Config->ZNodelete)
810     DtFlags1 |= DF_1_NODELETE;
811   if (Config->ZNow) {
812     DtFlags |= DF_BIND_NOW;
813     DtFlags1 |= DF_1_NOW;
814   }
815   if (Config->ZOrigin) {
816     DtFlags |= DF_ORIGIN;
817     DtFlags1 |= DF_1_ORIGIN;
818   }
819 
820   if (DtFlags)
821     add({DT_FLAGS, DtFlags});
822   if (DtFlags1)
823     add({DT_FLAGS_1, DtFlags1});
824 
825   if (!Config->Shared && !Config->Relocatable)
826     add({DT_DEBUG, (uint64_t)0});
827 }
828 
829 // Add remaining entries to complete .dynamic contents.
830 template <class ELFT> void DynamicSection<ELFT>::finalize() {
831   if (this->Size)
832     return; // Already finalized.
833 
834   this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
835   if (In<ELFT>::RelaDyn->OutSec->Size > 0) {
836     bool IsRela = Config->Rela;
837     add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn});
838     add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->OutSec->Size});
839     add({IsRela ? DT_RELAENT : DT_RELENT,
840          uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
841 
842     // MIPS dynamic loader does not support RELCOUNT tag.
843     // The problem is in the tight relation between dynamic
844     // relocations and GOT. So do not emit this tag on MIPS.
845     if (Config->EMachine != EM_MIPS) {
846       size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
847       if (Config->ZCombreloc && NumRelativeRels)
848         add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
849     }
850   }
851   if (In<ELFT>::RelaPlt->OutSec->Size > 0) {
852     add({DT_JMPREL, In<ELFT>::RelaPlt});
853     add({DT_PLTRELSZ, In<ELFT>::RelaPlt->OutSec->Size});
854     add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
855          In<ELFT>::GotPlt});
856     add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
857   }
858 
859   add({DT_SYMTAB, In<ELFT>::DynSymTab});
860   add({DT_SYMENT, sizeof(Elf_Sym)});
861   add({DT_STRTAB, In<ELFT>::DynStrTab});
862   add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()});
863   if (In<ELFT>::GnuHashTab)
864     add({DT_GNU_HASH, In<ELFT>::GnuHashTab});
865   if (In<ELFT>::HashTab)
866     add({DT_HASH, In<ELFT>::HashTab});
867 
868   if (Out<ELFT>::PreinitArray) {
869     add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray});
870     add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize});
871   }
872   if (Out<ELFT>::InitArray) {
873     add({DT_INIT_ARRAY, Out<ELFT>::InitArray});
874     add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize});
875   }
876   if (Out<ELFT>::FiniArray) {
877     add({DT_FINI_ARRAY, Out<ELFT>::FiniArray});
878     add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize});
879   }
880 
881   if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init))
882     add({DT_INIT, B});
883   if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini))
884     add({DT_FINI, B});
885 
886   bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
887   if (HasVerNeed || In<ELFT>::VerDef)
888     add({DT_VERSYM, In<ELFT>::VerSym});
889   if (In<ELFT>::VerDef) {
890     add({DT_VERDEF, In<ELFT>::VerDef});
891     add({DT_VERDEFNUM, getVerDefNum()});
892   }
893   if (HasVerNeed) {
894     add({DT_VERNEED, In<ELFT>::VerNeed});
895     add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
896   }
897 
898   if (Config->EMachine == EM_MIPS) {
899     add({DT_MIPS_RLD_VERSION, 1});
900     add({DT_MIPS_FLAGS, RHF_NOTPOT});
901     add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
902     add({DT_MIPS_SYMTABNO, In<ELFT>::DynSymTab->getNumSymbols()});
903     add({DT_MIPS_LOCAL_GOTNO, In<ELFT>::MipsGot->getLocalEntriesNum()});
904     if (const SymbolBody *B = In<ELFT>::MipsGot->getFirstGlobalEntry())
905       add({DT_MIPS_GOTSYM, B->DynsymIndex});
906     else
907       add({DT_MIPS_GOTSYM, In<ELFT>::DynSymTab->getNumSymbols()});
908     add({DT_PLTGOT, In<ELFT>::MipsGot});
909     if (In<ELFT>::MipsRldMap)
910       add({DT_MIPS_RLD_MAP, In<ELFT>::MipsRldMap});
911   }
912 
913   this->OutSec->Entsize = this->Entsize;
914   this->OutSec->Link = this->Link;
915 
916   // +1 for DT_NULL
917   this->Size = (Entries.size() + 1) * this->Entsize;
918 }
919 
920 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
921   auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
922 
923   for (const Entry &E : Entries) {
924     P->d_tag = E.Tag;
925     switch (E.Kind) {
926     case Entry::SecAddr:
927       P->d_un.d_ptr = E.OutSec->Addr;
928       break;
929     case Entry::InSecAddr:
930       P->d_un.d_ptr = E.InSec->OutSec->Addr + E.InSec->OutSecOff;
931       break;
932     case Entry::SecSize:
933       P->d_un.d_val = E.OutSec->Size;
934       break;
935     case Entry::SymAddr:
936       P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
937       break;
938     case Entry::PlainInt:
939       P->d_un.d_val = E.Val;
940       break;
941     }
942     ++P;
943   }
944 }
945 
946 template <class ELFT>
947 typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
948   if (OutputSec)
949     return OutputSec->Addr + OffsetInSec;
950   return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec);
951 }
952 
953 template <class ELFT>
954 typename ELFT::uint DynamicReloc<ELFT>::getAddend() const {
955   if (UseSymVA)
956     return Sym->getVA<ELFT>(Addend);
957   return Addend;
958 }
959 
960 template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
961   if (Sym && !UseSymVA)
962     return Sym->DynsymIndex;
963   return 0;
964 }
965 
966 template <class ELFT>
967 RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
968     : SyntheticSection<ELFT>(SHF_ALLOC, Config->Rela ? SHT_RELA : SHT_REL,
969                              sizeof(uintX_t), Name),
970       Sort(Sort) {
971   this->Entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
972 }
973 
974 template <class ELFT>
975 void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
976   if (Reloc.Type == Target->RelativeRel)
977     ++NumRelativeRelocs;
978   Relocs.push_back(Reloc);
979 }
980 
981 template <class ELFT, class RelTy>
982 static bool compRelocations(const RelTy &A, const RelTy &B) {
983   bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel;
984   bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel;
985   if (AIsRel != BIsRel)
986     return AIsRel;
987 
988   return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
989 }
990 
991 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
992   uint8_t *BufBegin = Buf;
993   for (const DynamicReloc<ELFT> &Rel : Relocs) {
994     auto *P = reinterpret_cast<Elf_Rela *>(Buf);
995     Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
996 
997     if (Config->Rela)
998       P->r_addend = Rel.getAddend();
999     P->r_offset = Rel.getOffset();
1000     if (Config->EMachine == EM_MIPS && Rel.getInputSec() == In<ELFT>::MipsGot)
1001       // Dynamic relocation against MIPS GOT section make deal TLS entries
1002       // allocated in the end of the GOT. We need to adjust the offset to take
1003       // in account 'local' and 'global' GOT entries.
1004       P->r_offset += In<ELFT>::MipsGot->getTlsOffset();
1005     P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL);
1006   }
1007 
1008   if (Sort) {
1009     if (Config->Rela)
1010       std::stable_sort((Elf_Rela *)BufBegin,
1011                        (Elf_Rela *)BufBegin + Relocs.size(),
1012                        compRelocations<ELFT, Elf_Rela>);
1013     else
1014       std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1015                        compRelocations<ELFT, Elf_Rel>);
1016   }
1017 }
1018 
1019 template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1020   return this->Entsize * Relocs.size();
1021 }
1022 
1023 template <class ELFT> void RelocationSection<ELFT>::finalize() {
1024   this->Link = In<ELFT>::DynSymTab ? In<ELFT>::DynSymTab->OutSec->SectionIndex
1025                                    : In<ELFT>::SymTab->OutSec->SectionIndex;
1026 
1027   // Set required output section properties.
1028   this->OutSec->Link = this->Link;
1029   this->OutSec->Entsize = this->Entsize;
1030 }
1031 
1032 template <class ELFT>
1033 SymbolTableSection<ELFT>::SymbolTableSection(
1034     StringTableSection<ELFT> &StrTabSec)
1035     : SyntheticSection<ELFT>(StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0,
1036                              StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1037                              sizeof(uintX_t),
1038                              StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1039       StrTabSec(StrTabSec) {
1040   this->Entsize = sizeof(Elf_Sym);
1041 }
1042 
1043 // Orders symbols according to their positions in the GOT,
1044 // in compliance with MIPS ABI rules.
1045 // See "Global Offset Table" in Chapter 5 in the following document
1046 // for detailed description:
1047 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1048 static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) {
1049   // Sort entries related to non-local preemptible symbols by GOT indexes.
1050   // All other entries go to the first part of GOT in arbitrary order.
1051   bool LIsInLocalGot = !L->IsInGlobalMipsGot;
1052   bool RIsInLocalGot = !R->IsInGlobalMipsGot;
1053   if (LIsInLocalGot || RIsInLocalGot)
1054     return !RIsInLocalGot;
1055   return L->GotIndex < R->GotIndex;
1056 }
1057 
1058 static uint8_t getSymbolBinding(SymbolBody *Body) {
1059   Symbol *S = Body->symbol();
1060   if (Config->Relocatable)
1061     return S->Binding;
1062   uint8_t Visibility = S->Visibility;
1063   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1064     return STB_LOCAL;
1065   if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
1066     return STB_GLOBAL;
1067   return S->Binding;
1068 }
1069 
1070 template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
1071   this->OutSec->Link = this->Link = StrTabSec.OutSec->SectionIndex;
1072   this->OutSec->Info = this->Info = NumLocals + 1;
1073   this->OutSec->Entsize = this->Entsize;
1074 
1075   if (Config->Relocatable) {
1076     size_t I = NumLocals;
1077     for (const SymbolTableEntry &S : Symbols)
1078       S.Symbol->DynsymIndex = ++I;
1079     return;
1080   }
1081 
1082   if (!StrTabSec.isDynamic()) {
1083     std::stable_sort(Symbols.begin(), Symbols.end(),
1084                      [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1085                        return getSymbolBinding(L.Symbol) == STB_LOCAL &&
1086                               getSymbolBinding(R.Symbol) != STB_LOCAL;
1087                      });
1088     return;
1089   }
1090   if (In<ELFT>::GnuHashTab)
1091     // NB: It also sorts Symbols to meet the GNU hash table requirements.
1092     In<ELFT>::GnuHashTab->addSymbols(Symbols);
1093   else if (Config->EMachine == EM_MIPS)
1094     std::stable_sort(Symbols.begin(), Symbols.end(),
1095                      [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1096                        return sortMipsSymbols(L.Symbol, R.Symbol);
1097                      });
1098   size_t I = 0;
1099   for (const SymbolTableEntry &S : Symbols)
1100     S.Symbol->DynsymIndex = ++I;
1101 }
1102 
1103 template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1104   Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
1105 }
1106 
1107 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1108   Buf += sizeof(Elf_Sym);
1109 
1110   // All symbols with STB_LOCAL binding precede the weak and global symbols.
1111   // .dynsym only contains global symbols.
1112   if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic())
1113     writeLocalSymbols(Buf);
1114 
1115   writeGlobalSymbols(Buf);
1116 }
1117 
1118 template <class ELFT>
1119 void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1120   // Iterate over all input object files to copy their local symbols
1121   // to the output symbol table pointed by Buf.
1122   for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
1123     for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
1124          File->KeptLocalSyms) {
1125       const DefinedRegular<ELFT> &Body = *P.first;
1126       InputSectionBase<ELFT> *Section = Body.Section;
1127       auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1128 
1129       if (!Section) {
1130         ESym->st_shndx = SHN_ABS;
1131         ESym->st_value = Body.Value;
1132       } else {
1133         const OutputSectionBase *OutSec = Section->OutSec;
1134         ESym->st_shndx = OutSec->SectionIndex;
1135         ESym->st_value = OutSec->Addr + Section->getOffset(Body);
1136       }
1137       ESym->st_name = P.second;
1138       ESym->st_size = Body.template getSize<ELFT>();
1139       ESym->setBindingAndType(STB_LOCAL, Body.Type);
1140       Buf += sizeof(*ESym);
1141     }
1142   }
1143 }
1144 
1145 template <class ELFT>
1146 void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
1147   // Write the internal symbol table contents to the output symbol table
1148   // pointed by Buf.
1149   auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1150   for (const SymbolTableEntry &S : Symbols) {
1151     SymbolBody *Body = S.Symbol;
1152     size_t StrOff = S.StrTabOffset;
1153 
1154     uint8_t Type = Body->Type;
1155     uintX_t Size = Body->getSize<ELFT>();
1156 
1157     ESym->setBindingAndType(getSymbolBinding(Body), Type);
1158     ESym->st_size = Size;
1159     ESym->st_name = StrOff;
1160     ESym->setVisibility(Body->symbol()->Visibility);
1161     ESym->st_value = Body->getVA<ELFT>();
1162 
1163     if (const OutputSectionBase *OutSec = getOutputSection(Body))
1164       ESym->st_shndx = OutSec->SectionIndex;
1165     else if (isa<DefinedRegular<ELFT>>(Body))
1166       ESym->st_shndx = SHN_ABS;
1167 
1168     if (Config->EMachine == EM_MIPS) {
1169       // On MIPS we need to mark symbol which has a PLT entry and requires
1170       // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1171       // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1172       // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1173       if (Body->isInPlt() && Body->NeedsCopyOrPltAddr)
1174         ESym->st_other |= STO_MIPS_PLT;
1175       if (Config->Relocatable) {
1176         auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
1177         if (D && D->isMipsPIC())
1178           ESym->st_other |= STO_MIPS_PIC;
1179       }
1180     }
1181     ++ESym;
1182   }
1183 }
1184 
1185 template <class ELFT>
1186 const OutputSectionBase *
1187 SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1188   switch (Sym->kind()) {
1189   case SymbolBody::DefinedSyntheticKind:
1190     return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
1191   case SymbolBody::DefinedRegularKind: {
1192     auto &D = cast<DefinedRegular<ELFT>>(*Sym);
1193     if (D.Section)
1194       return D.Section->OutSec;
1195     break;
1196   }
1197   case SymbolBody::DefinedCommonKind:
1198     return In<ELFT>::Common->OutSec;
1199   case SymbolBody::SharedKind:
1200     if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1201       return Out<ELFT>::Bss;
1202     break;
1203   case SymbolBody::UndefinedKind:
1204   case SymbolBody::LazyArchiveKind:
1205   case SymbolBody::LazyObjectKind:
1206     break;
1207   }
1208   return nullptr;
1209 }
1210 
1211 template <class ELFT>
1212 GnuHashTableSection<ELFT>::GnuHashTableSection()
1213     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_HASH, sizeof(uintX_t),
1214                              ".gnu.hash") {
1215   this->Entsize = ELFT::Is64Bits ? 0 : 4;
1216 }
1217 
1218 template <class ELFT>
1219 unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
1220   if (!NumHashed)
1221     return 0;
1222 
1223   // These values are prime numbers which are not greater than 2^(N-1) + 1.
1224   // In result, for any particular NumHashed we return a prime number
1225   // which is not greater than NumHashed.
1226   static const unsigned Primes[] = {
1227       1,   1,    3,    3,    7,    13,    31,    61,    127,   251,
1228       509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
1229 
1230   return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
1231                                    array_lengthof(Primes) - 1)];
1232 }
1233 
1234 // Bloom filter estimation: at least 8 bits for each hashed symbol.
1235 // GNU Hash table requirement: it should be a power of 2,
1236 //   the minimum value is 1, even for an empty table.
1237 // Expected results for a 32-bit target:
1238 //   calcMaskWords(0..4)   = 1
1239 //   calcMaskWords(5..8)   = 2
1240 //   calcMaskWords(9..16)  = 4
1241 // For a 64-bit target:
1242 //   calcMaskWords(0..8)   = 1
1243 //   calcMaskWords(9..16)  = 2
1244 //   calcMaskWords(17..32) = 4
1245 template <class ELFT>
1246 unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
1247   if (!NumHashed)
1248     return 1;
1249   return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
1250 }
1251 
1252 template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
1253   unsigned NumHashed = Symbols.size();
1254   NBuckets = calcNBuckets(NumHashed);
1255   MaskWords = calcMaskWords(NumHashed);
1256   // Second hash shift estimation: just predefined values.
1257   Shift2 = ELFT::Is64Bits ? 6 : 5;
1258 
1259   this->OutSec->Entsize = this->Entsize;
1260   this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1261   this->Size = sizeof(Elf_Word) * 4            // Header
1262                + sizeof(Elf_Off) * MaskWords   // Bloom Filter
1263                + sizeof(Elf_Word) * NBuckets   // Hash Buckets
1264                + sizeof(Elf_Word) * NumHashed; // Hash Values
1265 }
1266 
1267 template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1268   writeHeader(Buf);
1269   if (Symbols.empty())
1270     return;
1271   writeBloomFilter(Buf);
1272   writeHashTable(Buf);
1273 }
1274 
1275 template <class ELFT>
1276 void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
1277   auto *P = reinterpret_cast<Elf_Word *>(Buf);
1278   *P++ = NBuckets;
1279   *P++ = In<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
1280   *P++ = MaskWords;
1281   *P++ = Shift2;
1282   Buf = reinterpret_cast<uint8_t *>(P);
1283 }
1284 
1285 template <class ELFT>
1286 void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
1287   unsigned C = sizeof(Elf_Off) * 8;
1288 
1289   auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
1290   for (const SymbolData &Sym : Symbols) {
1291     size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
1292     uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
1293                 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
1294     Masks[Pos] |= V;
1295   }
1296   Buf += sizeof(Elf_Off) * MaskWords;
1297 }
1298 
1299 template <class ELFT>
1300 void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
1301   Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
1302   Elf_Word *Values = Buckets + NBuckets;
1303 
1304   int PrevBucket = -1;
1305   int I = 0;
1306   for (const SymbolData &Sym : Symbols) {
1307     int Bucket = Sym.Hash % NBuckets;
1308     assert(PrevBucket <= Bucket);
1309     if (Bucket != PrevBucket) {
1310       Buckets[Bucket] = Sym.Body->DynsymIndex;
1311       PrevBucket = Bucket;
1312       if (I > 0)
1313         Values[I - 1] |= 1;
1314     }
1315     Values[I] = Sym.Hash & ~1;
1316     ++I;
1317   }
1318   if (I > 0)
1319     Values[I - 1] |= 1;
1320 }
1321 
1322 static uint32_t hashGnu(StringRef Name) {
1323   uint32_t H = 5381;
1324   for (uint8_t C : Name)
1325     H = (H << 5) + H + C;
1326   return H;
1327 }
1328 
1329 // Add symbols to this symbol hash table. Note that this function
1330 // destructively sort a given vector -- which is needed because
1331 // GNU-style hash table places some sorting requirements.
1332 template <class ELFT>
1333 void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) {
1334   // Ideally this will just be 'auto' but GCC 6.1 is not able
1335   // to deduce it correctly.
1336   std::vector<SymbolTableEntry>::iterator Mid =
1337       std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
1338         return S.Symbol->isUndefined();
1339       });
1340   if (Mid == V.end())
1341     return;
1342   for (auto I = Mid, E = V.end(); I != E; ++I) {
1343     SymbolBody *B = I->Symbol;
1344     size_t StrOff = I->StrTabOffset;
1345     Symbols.push_back({B, StrOff, hashGnu(B->getName())});
1346   }
1347 
1348   unsigned NBuckets = calcNBuckets(Symbols.size());
1349   std::stable_sort(Symbols.begin(), Symbols.end(),
1350                    [&](const SymbolData &L, const SymbolData &R) {
1351                      return L.Hash % NBuckets < R.Hash % NBuckets;
1352                    });
1353 
1354   V.erase(Mid, V.end());
1355   for (const SymbolData &Sym : Symbols)
1356     V.push_back({Sym.Body, Sym.STName});
1357 }
1358 
1359 template <class ELFT>
1360 HashTableSection<ELFT>::HashTableSection()
1361     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_HASH, sizeof(Elf_Word), ".hash") {
1362   this->Entsize = sizeof(Elf_Word);
1363 }
1364 
1365 template <class ELFT> void HashTableSection<ELFT>::finalize() {
1366   this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1367   this->OutSec->Entsize = this->Entsize;
1368 
1369   unsigned NumEntries = 2;                            // nbucket and nchain.
1370   NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
1371 
1372   // Create as many buckets as there are symbols.
1373   // FIXME: This is simplistic. We can try to optimize it, but implementing
1374   // support for SHT_GNU_HASH is probably even more profitable.
1375   NumEntries += In<ELFT>::DynSymTab->getNumSymbols();
1376   this->Size = NumEntries * sizeof(Elf_Word);
1377 }
1378 
1379 template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1380   unsigned NumSymbols = In<ELFT>::DynSymTab->getNumSymbols();
1381   auto *P = reinterpret_cast<Elf_Word *>(Buf);
1382   *P++ = NumSymbols; // nbucket
1383   *P++ = NumSymbols; // nchain
1384 
1385   Elf_Word *Buckets = P;
1386   Elf_Word *Chains = P + NumSymbols;
1387 
1388   for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1389     SymbolBody *Body = S.Symbol;
1390     StringRef Name = Body->getName();
1391     unsigned I = Body->DynsymIndex;
1392     uint32_t Hash = hashSysV(Name) % NumSymbols;
1393     Chains[I] = Buckets[Hash];
1394     Buckets[Hash] = I;
1395   }
1396 }
1397 
1398 template <class ELFT>
1399 PltSection<ELFT>::PltSection()
1400     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16,
1401                              ".plt") {}
1402 
1403 template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
1404   // At beginning of PLT, we have code to call the dynamic linker
1405   // to resolve dynsyms at runtime. Write such code.
1406   Target->writePltHeader(Buf);
1407   size_t Off = Target->PltHeaderSize;
1408 
1409   for (auto &I : Entries) {
1410     const SymbolBody *B = I.first;
1411     unsigned RelOff = I.second;
1412     uint64_t Got = B->getGotPltVA<ELFT>();
1413     uint64_t Plt = this->getVA() + Off;
1414     Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1415     Off += Target->PltEntrySize;
1416   }
1417 }
1418 
1419 template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
1420   Sym.PltIndex = Entries.size();
1421   unsigned RelOff = In<ELFT>::RelaPlt->getRelocOffset();
1422   Entries.push_back(std::make_pair(&Sym, RelOff));
1423 }
1424 
1425 template <class ELFT> size_t PltSection<ELFT>::getSize() const {
1426   return Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
1427 }
1428 
1429 template <class ELFT>
1430 IpltSection<ELFT>::IpltSection()
1431     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16,
1432                              ".plt") {}
1433 
1434 template <class ELFT> void IpltSection<ELFT>::writeTo(uint8_t *Buf) {
1435   // The IRelative relocations do not support lazy binding so no header is
1436   // needed
1437   size_t Off = 0;
1438   for (auto &I : Entries) {
1439     const SymbolBody *B = I.first;
1440     unsigned RelOff = I.second + In<ELFT>::Plt->getSize();
1441     uint64_t Got = B->getGotPltVA<ELFT>();
1442     uint64_t Plt = this->getVA() + Off;
1443     Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1444     Off += Target->PltEntrySize;
1445   }
1446 }
1447 
1448 template <class ELFT> void IpltSection<ELFT>::addEntry(SymbolBody &Sym) {
1449   Sym.PltIndex = Entries.size();
1450   Sym.IsInIplt = true;
1451   unsigned RelOff = In<ELFT>::RelaIplt->getRelocOffset();
1452   Entries.push_back(std::make_pair(&Sym, RelOff));
1453 }
1454 
1455 template <class ELFT> size_t IpltSection<ELFT>::getSize() const {
1456   return Entries.size() * Target->PltEntrySize;
1457 }
1458 
1459 template <class ELFT>
1460 GdbIndexSection<ELFT>::GdbIndexSection()
1461     : SyntheticSection<ELFT>(0, SHT_PROGBITS, 1, ".gdb_index"),
1462       StringPool(llvm::StringTableBuilder::ELF) {}
1463 
1464 template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() {
1465   for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
1466     if (InputSection<ELFT> *IS = dyn_cast<InputSection<ELFT>>(S))
1467       if (IS->OutSec && IS->Name == ".debug_info")
1468         readDwarf(IS);
1469 }
1470 
1471 // Iterative hash function for symbol's name is described in .gdb_index format
1472 // specification. Note that we use one for version 5 to 7 here, it is different
1473 // for version 4.
1474 static uint32_t hash(StringRef Str) {
1475   uint32_t R = 0;
1476   for (uint8_t C : Str)
1477     R = R * 67 + tolower(C) - 113;
1478   return R;
1479 }
1480 
1481 template <class ELFT>
1482 void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) {
1483   GdbIndexBuilder<ELFT> Builder(I);
1484   if (ErrorCount)
1485     return;
1486 
1487   size_t CuId = CompilationUnits.size();
1488   std::vector<std::pair<uintX_t, uintX_t>> CuList = Builder.readCUList();
1489   CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end());
1490 
1491   std::vector<AddressEntry<ELFT>> AddrArea = Builder.readAddressArea(CuId);
1492   AddressArea.insert(AddressArea.end(), AddrArea.begin(), AddrArea.end());
1493 
1494   std::vector<std::pair<StringRef, uint8_t>> NamesAndTypes =
1495       Builder.readPubNamesAndTypes();
1496 
1497   for (std::pair<StringRef, uint8_t> &Pair : NamesAndTypes) {
1498     uint32_t Hash = hash(Pair.first);
1499     size_t Offset = StringPool.add(Pair.first);
1500 
1501     bool IsNew;
1502     GdbSymbol *Sym;
1503     std::tie(IsNew, Sym) = SymbolTable.add(Hash, Offset);
1504     if (IsNew) {
1505       Sym->CuVectorIndex = CuVectors.size();
1506       CuVectors.push_back({{CuId, Pair.second}});
1507       continue;
1508     }
1509 
1510     std::vector<std::pair<uint32_t, uint8_t>> &CuVec =
1511         CuVectors[Sym->CuVectorIndex];
1512     CuVec.push_back({CuId, Pair.second});
1513   }
1514 }
1515 
1516 template <class ELFT> void GdbIndexSection<ELFT>::finalize() {
1517   if (Finalized)
1518     return;
1519   Finalized = true;
1520 
1521   parseDebugSections();
1522 
1523   // GdbIndex header consist from version fields
1524   // and 5 more fields with different kinds of offsets.
1525   CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
1526   SymTabOffset = CuTypesOffset + AddressArea.size() * AddressEntrySize;
1527 
1528   ConstantPoolOffset =
1529       SymTabOffset + SymbolTable.getCapacity() * SymTabEntrySize;
1530 
1531   for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) {
1532     CuVectorsOffset.push_back(CuVectorsSize);
1533     CuVectorsSize += OffsetTypeSize * (CuVec.size() + 1);
1534   }
1535   StringPoolOffset = ConstantPoolOffset + CuVectorsSize;
1536 
1537   StringPool.finalizeInOrder();
1538 }
1539 
1540 template <class ELFT> size_t GdbIndexSection<ELFT>::getSize() const {
1541   const_cast<GdbIndexSection<ELFT> *>(this)->finalize();
1542   return StringPoolOffset + StringPool.getSize();
1543 }
1544 
1545 template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
1546   write32le(Buf, 7);                       // Write version.
1547   write32le(Buf + 4, CuListOffset);        // CU list offset.
1548   write32le(Buf + 8, CuTypesOffset);       // Types CU list offset.
1549   write32le(Buf + 12, CuTypesOffset);      // Address area offset.
1550   write32le(Buf + 16, SymTabOffset);       // Symbol table offset.
1551   write32le(Buf + 20, ConstantPoolOffset); // Constant pool offset.
1552   Buf += 24;
1553 
1554   // Write the CU list.
1555   for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
1556     write64le(Buf, CU.first);
1557     write64le(Buf + 8, CU.second);
1558     Buf += 16;
1559   }
1560 
1561   // Write the address area.
1562   for (AddressEntry<ELFT> &E : AddressArea) {
1563     uintX_t BaseAddr = E.Section->OutSec->Addr + E.Section->getOffset(0);
1564     write64le(Buf, BaseAddr + E.LowAddress);
1565     write64le(Buf + 8, BaseAddr + E.HighAddress);
1566     write32le(Buf + 16, E.CuIndex);
1567     Buf += 20;
1568   }
1569 
1570   // Write the symbol table.
1571   for (size_t I = 0; I < SymbolTable.getCapacity(); ++I) {
1572     GdbSymbol *Sym = SymbolTable.getSymbol(I);
1573     if (Sym) {
1574       size_t NameOffset =
1575           Sym->NameOffset + StringPoolOffset - ConstantPoolOffset;
1576       size_t CuVectorOffset = CuVectorsOffset[Sym->CuVectorIndex];
1577       write32le(Buf, NameOffset);
1578       write32le(Buf + 4, CuVectorOffset);
1579     }
1580     Buf += 8;
1581   }
1582 
1583   // Write the CU vectors into the constant pool.
1584   for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) {
1585     write32le(Buf, CuVec.size());
1586     Buf += 4;
1587     for (std::pair<uint32_t, uint8_t> &P : CuVec) {
1588       uint32_t Index = P.first;
1589       uint8_t Flags = P.second;
1590       Index |= Flags << 24;
1591       write32le(Buf, Index);
1592       Buf += 4;
1593     }
1594   }
1595 
1596   StringPool.write(Buf);
1597 }
1598 
1599 template <class ELFT> bool GdbIndexSection<ELFT>::empty() const {
1600   return !Out<ELFT>::DebugInfo;
1601 }
1602 
1603 template <class ELFT>
1604 EhFrameHeader<ELFT>::EhFrameHeader()
1605     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
1606 
1607 // .eh_frame_hdr contains a binary search table of pointers to FDEs.
1608 // Each entry of the search table consists of two values,
1609 // the starting PC from where FDEs covers, and the FDE's address.
1610 // It is sorted by PC.
1611 template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1612   const endianness E = ELFT::TargetEndianness;
1613 
1614   // Sort the FDE list by their PC and uniqueify. Usually there is only
1615   // one FDE for a PC (i.e. function), but if ICF merges two functions
1616   // into one, there can be more than one FDEs pointing to the address.
1617   auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1618   std::stable_sort(Fdes.begin(), Fdes.end(), Less);
1619   auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
1620   Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
1621 
1622   Buf[0] = 1;
1623   Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1624   Buf[2] = DW_EH_PE_udata4;
1625   Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
1626   write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->getVA() - 4);
1627   write32<E>(Buf + 8, Fdes.size());
1628   Buf += 12;
1629 
1630   uintX_t VA = this->getVA();
1631   for (FdeData &Fde : Fdes) {
1632     write32<E>(Buf, Fde.Pc - VA);
1633     write32<E>(Buf + 4, Fde.FdeVA - VA);
1634     Buf += 8;
1635   }
1636 }
1637 
1638 template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const {
1639   // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
1640   return 12 + Out<ELFT>::EhFrame->NumFdes * 8;
1641 }
1642 
1643 template <class ELFT>
1644 void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
1645   Fdes.push_back({Pc, FdeVA});
1646 }
1647 
1648 template <class ELFT> bool EhFrameHeader<ELFT>::empty() const {
1649   return Out<ELFT>::EhFrame->empty();
1650 }
1651 
1652 template <class ELFT>
1653 VersionDefinitionSection<ELFT>::VersionDefinitionSection()
1654     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
1655                              ".gnu.version_d") {}
1656 
1657 static StringRef getFileDefName() {
1658   if (!Config->SoName.empty())
1659     return Config->SoName;
1660   return Config->OutputFile;
1661 }
1662 
1663 template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1664   FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
1665   for (VersionDefinition &V : Config->VersionDefinitions)
1666     V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
1667 
1668   this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
1669 
1670   // sh_info should be set to the number of definitions. This fact is missed in
1671   // documentation, but confirmed by binutils community:
1672   // https://sourceware.org/ml/binutils/2014-11/msg00355.html
1673   this->OutSec->Info = this->Info = getVerDefNum();
1674 }
1675 
1676 template <class ELFT>
1677 void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1678                                               StringRef Name, size_t NameOff) {
1679   auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1680   Verdef->vd_version = 1;
1681   Verdef->vd_cnt = 1;
1682   Verdef->vd_aux = sizeof(Elf_Verdef);
1683   Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1684   Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
1685   Verdef->vd_ndx = Index;
1686   Verdef->vd_hash = hashSysV(Name);
1687 
1688   auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1689   Verdaux->vda_name = NameOff;
1690   Verdaux->vda_next = 0;
1691 }
1692 
1693 template <class ELFT>
1694 void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
1695   writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1696 
1697   for (VersionDefinition &V : Config->VersionDefinitions) {
1698     Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1699     writeOne(Buf, V.Id, V.Name, V.NameOff);
1700   }
1701 
1702   // Need to terminate the last version definition.
1703   Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1704   Verdef->vd_next = 0;
1705 }
1706 
1707 template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const {
1708   return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1709 }
1710 
1711 template <class ELFT>
1712 VersionTableSection<ELFT>::VersionTableSection()
1713     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t),
1714                              ".gnu.version") {}
1715 
1716 template <class ELFT> void VersionTableSection<ELFT>::finalize() {
1717   this->OutSec->Entsize = this->Entsize = sizeof(Elf_Versym);
1718   // At the moment of june 2016 GNU docs does not mention that sh_link field
1719   // should be set, but Sun docs do. Also readelf relies on this field.
1720   this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1721 }
1722 
1723 template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const {
1724   return sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1);
1725 }
1726 
1727 template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1728   auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
1729   for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1730     OutVersym->vs_index = S.Symbol->symbol()->VersionId;
1731     ++OutVersym;
1732   }
1733 }
1734 
1735 template <class ELFT> bool VersionTableSection<ELFT>::empty() const {
1736   return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty();
1737 }
1738 
1739 template <class ELFT>
1740 VersionNeedSection<ELFT>::VersionNeedSection()
1741     : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t),
1742                              ".gnu.version_r") {
1743   // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1744   // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1745   // First identifiers are reserved by verdef section if it exist.
1746   NextIndex = getVerDefNum() + 1;
1747 }
1748 
1749 template <class ELFT>
1750 void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1751   if (!SS->Verdef) {
1752     SS->symbol()->VersionId = VER_NDX_GLOBAL;
1753     return;
1754   }
1755   SharedFile<ELFT> *F = SS->file();
1756   // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1757   // to create one by adding it to our needed list and creating a dynstr entry
1758   // for the soname.
1759   if (F->VerdefMap.empty())
1760     Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())});
1761   typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1762   // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1763   // prepare to create one by allocating a version identifier and creating a
1764   // dynstr entry for the version name.
1765   if (NV.Index == 0) {
1766     NV.StrTab = In<ELFT>::DynStrTab->addString(
1767         SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
1768     NV.Index = NextIndex++;
1769   }
1770   SS->symbol()->VersionId = NV.Index;
1771 }
1772 
1773 template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1774   // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1775   auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1776   auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1777 
1778   for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1779     // Create an Elf_Verneed for this DSO.
1780     Verneed->vn_version = 1;
1781     Verneed->vn_cnt = P.first->VerdefMap.size();
1782     Verneed->vn_file = P.second;
1783     Verneed->vn_aux =
1784         reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1785     Verneed->vn_next = sizeof(Elf_Verneed);
1786     ++Verneed;
1787 
1788     // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1789     // VerdefMap, which will only contain references to needed version
1790     // definitions. Each Elf_Vernaux is based on the information contained in
1791     // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1792     // pointers, but is deterministic because the pointers refer to Elf_Verdef
1793     // data structures within a single input file.
1794     for (auto &NV : P.first->VerdefMap) {
1795       Vernaux->vna_hash = NV.first->vd_hash;
1796       Vernaux->vna_flags = 0;
1797       Vernaux->vna_other = NV.second.Index;
1798       Vernaux->vna_name = NV.second.StrTab;
1799       Vernaux->vna_next = sizeof(Elf_Vernaux);
1800       ++Vernaux;
1801     }
1802 
1803     Vernaux[-1].vna_next = 0;
1804   }
1805   Verneed[-1].vn_next = 0;
1806 }
1807 
1808 template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1809   this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
1810   this->OutSec->Info = this->Info = Needed.size();
1811 }
1812 
1813 template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
1814   unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1815   for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1816     Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
1817   return Size;
1818 }
1819 
1820 template <class ELFT> bool VersionNeedSection<ELFT>::empty() const {
1821   return getNeedNum() == 0;
1822 }
1823 
1824 template <class ELFT>
1825 MipsRldMapSection<ELFT>::MipsRldMapSection()
1826     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
1827                              sizeof(typename ELFT::uint), ".rld_map") {}
1828 
1829 template <class ELFT> void MipsRldMapSection<ELFT>::writeTo(uint8_t *Buf) {
1830   // Apply filler from linker script.
1831   uint64_t Filler = Script<ELFT>::X->getFiller(this->Name);
1832   Filler = (Filler << 32) | Filler;
1833   memcpy(Buf, &Filler, getSize());
1834 }
1835 
1836 template <class ELFT>
1837 ARMExidxSentinelSection<ELFT>::ARMExidxSentinelSection()
1838     : SyntheticSection<ELFT>(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX,
1839                              sizeof(typename ELFT::uint), ".ARM.exidx") {}
1840 
1841 // Write a terminating sentinel entry to the end of the .ARM.exidx table.
1842 // This section will have been sorted last in the .ARM.exidx table.
1843 // This table entry will have the form:
1844 // | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND |
1845 template <class ELFT>
1846 void ARMExidxSentinelSection<ELFT>::writeTo(uint8_t *Buf) {
1847   // Get the InputSection before us, we are by definition last
1848   auto RI = cast<OutputSection<ELFT>>(this->OutSec)->Sections.rbegin();
1849   InputSection<ELFT> *LE = *(++RI);
1850   InputSection<ELFT> *LC = cast<InputSection<ELFT>>(LE->getLinkOrderDep());
1851   uint64_t S = LC->OutSec->Addr + LC->getOffset(LC->getSize());
1852   uint64_t P = this->getVA();
1853   Target->relocateOne(Buf, R_ARM_PREL31, S - P);
1854   write32le(Buf + 4, 0x1);
1855 }
1856 
1857 template InputSection<ELF32LE> *elf::createCommonSection();
1858 template InputSection<ELF32BE> *elf::createCommonSection();
1859 template InputSection<ELF64LE> *elf::createCommonSection();
1860 template InputSection<ELF64BE> *elf::createCommonSection();
1861 
1862 template InputSection<ELF32LE> *elf::createInterpSection();
1863 template InputSection<ELF32BE> *elf::createInterpSection();
1864 template InputSection<ELF64LE> *elf::createInterpSection();
1865 template InputSection<ELF64BE> *elf::createInterpSection();
1866 
1867 template MergeInputSection<ELF32LE> *elf::createCommentSection();
1868 template MergeInputSection<ELF32BE> *elf::createCommentSection();
1869 template MergeInputSection<ELF64LE> *elf::createCommentSection();
1870 template MergeInputSection<ELF64BE> *elf::createCommentSection();
1871 
1872 template class elf::MipsAbiFlagsSection<ELF32LE>;
1873 template class elf::MipsAbiFlagsSection<ELF32BE>;
1874 template class elf::MipsAbiFlagsSection<ELF64LE>;
1875 template class elf::MipsAbiFlagsSection<ELF64BE>;
1876 
1877 template class elf::MipsOptionsSection<ELF32LE>;
1878 template class elf::MipsOptionsSection<ELF32BE>;
1879 template class elf::MipsOptionsSection<ELF64LE>;
1880 template class elf::MipsOptionsSection<ELF64BE>;
1881 
1882 template class elf::MipsReginfoSection<ELF32LE>;
1883 template class elf::MipsReginfoSection<ELF32BE>;
1884 template class elf::MipsReginfoSection<ELF64LE>;
1885 template class elf::MipsReginfoSection<ELF64BE>;
1886 
1887 template class elf::BuildIdSection<ELF32LE>;
1888 template class elf::BuildIdSection<ELF32BE>;
1889 template class elf::BuildIdSection<ELF64LE>;
1890 template class elf::BuildIdSection<ELF64BE>;
1891 
1892 template class elf::GotSection<ELF32LE>;
1893 template class elf::GotSection<ELF32BE>;
1894 template class elf::GotSection<ELF64LE>;
1895 template class elf::GotSection<ELF64BE>;
1896 
1897 template class elf::MipsGotSection<ELF32LE>;
1898 template class elf::MipsGotSection<ELF32BE>;
1899 template class elf::MipsGotSection<ELF64LE>;
1900 template class elf::MipsGotSection<ELF64BE>;
1901 
1902 template class elf::GotPltSection<ELF32LE>;
1903 template class elf::GotPltSection<ELF32BE>;
1904 template class elf::GotPltSection<ELF64LE>;
1905 template class elf::GotPltSection<ELF64BE>;
1906 
1907 template class elf::IgotPltSection<ELF32LE>;
1908 template class elf::IgotPltSection<ELF32BE>;
1909 template class elf::IgotPltSection<ELF64LE>;
1910 template class elf::IgotPltSection<ELF64BE>;
1911 
1912 template class elf::StringTableSection<ELF32LE>;
1913 template class elf::StringTableSection<ELF32BE>;
1914 template class elf::StringTableSection<ELF64LE>;
1915 template class elf::StringTableSection<ELF64BE>;
1916 
1917 template class elf::DynamicSection<ELF32LE>;
1918 template class elf::DynamicSection<ELF32BE>;
1919 template class elf::DynamicSection<ELF64LE>;
1920 template class elf::DynamicSection<ELF64BE>;
1921 
1922 template class elf::RelocationSection<ELF32LE>;
1923 template class elf::RelocationSection<ELF32BE>;
1924 template class elf::RelocationSection<ELF64LE>;
1925 template class elf::RelocationSection<ELF64BE>;
1926 
1927 template class elf::SymbolTableSection<ELF32LE>;
1928 template class elf::SymbolTableSection<ELF32BE>;
1929 template class elf::SymbolTableSection<ELF64LE>;
1930 template class elf::SymbolTableSection<ELF64BE>;
1931 
1932 template class elf::GnuHashTableSection<ELF32LE>;
1933 template class elf::GnuHashTableSection<ELF32BE>;
1934 template class elf::GnuHashTableSection<ELF64LE>;
1935 template class elf::GnuHashTableSection<ELF64BE>;
1936 
1937 template class elf::HashTableSection<ELF32LE>;
1938 template class elf::HashTableSection<ELF32BE>;
1939 template class elf::HashTableSection<ELF64LE>;
1940 template class elf::HashTableSection<ELF64BE>;
1941 
1942 template class elf::PltSection<ELF32LE>;
1943 template class elf::PltSection<ELF32BE>;
1944 template class elf::PltSection<ELF64LE>;
1945 template class elf::PltSection<ELF64BE>;
1946 
1947 template class elf::IpltSection<ELF32LE>;
1948 template class elf::IpltSection<ELF32BE>;
1949 template class elf::IpltSection<ELF64LE>;
1950 template class elf::IpltSection<ELF64BE>;
1951 
1952 template class elf::GdbIndexSection<ELF32LE>;
1953 template class elf::GdbIndexSection<ELF32BE>;
1954 template class elf::GdbIndexSection<ELF64LE>;
1955 template class elf::GdbIndexSection<ELF64BE>;
1956 
1957 template class elf::EhFrameHeader<ELF32LE>;
1958 template class elf::EhFrameHeader<ELF32BE>;
1959 template class elf::EhFrameHeader<ELF64LE>;
1960 template class elf::EhFrameHeader<ELF64BE>;
1961 
1962 template class elf::VersionTableSection<ELF32LE>;
1963 template class elf::VersionTableSection<ELF32BE>;
1964 template class elf::VersionTableSection<ELF64LE>;
1965 template class elf::VersionTableSection<ELF64BE>;
1966 
1967 template class elf::VersionNeedSection<ELF32LE>;
1968 template class elf::VersionNeedSection<ELF32BE>;
1969 template class elf::VersionNeedSection<ELF64LE>;
1970 template class elf::VersionNeedSection<ELF64BE>;
1971 
1972 template class elf::VersionDefinitionSection<ELF32LE>;
1973 template class elf::VersionDefinitionSection<ELF32BE>;
1974 template class elf::VersionDefinitionSection<ELF64LE>;
1975 template class elf::VersionDefinitionSection<ELF64BE>;
1976 
1977 template class elf::MipsRldMapSection<ELF32LE>;
1978 template class elf::MipsRldMapSection<ELF32BE>;
1979 template class elf::MipsRldMapSection<ELF64LE>;
1980 template class elf::MipsRldMapSection<ELF64BE>;
1981 
1982 template class elf::ARMExidxSentinelSection<ELF32LE>;
1983 template class elf::ARMExidxSentinelSection<ELF32BE>;
1984 template class elf::ARMExidxSentinelSection<ELF64LE>;
1985 template class elf::ARMExidxSentinelSection<ELF64BE>;
1986