1 //===- SyntheticSections.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains linker-synthesized sections. Currently,
10 // synthetic sections are created either output sections or input sections,
11 // but we are rewriting code so that all synthetic sections are created as
12 // input sections.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "SyntheticSections.h"
17 #include "Config.h"
18 #include "InputFiles.h"
19 #include "LinkerScript.h"
20 #include "OutputSections.h"
21 #include "SymbolTable.h"
22 #include "Symbols.h"
23 #include "Target.h"
24 #include "Writer.h"
25 #include "lld/Common/ErrorHandler.h"
26 #include "lld/Common/Memory.h"
27 #include "lld/Common/Strings.h"
28 #include "lld/Common/Threads.h"
29 #include "lld/Common/Version.h"
30 #include "llvm/ADT/SetOperations.h"
31 #include "llvm/ADT/StringExtras.h"
32 #include "llvm/BinaryFormat/Dwarf.h"
33 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
34 #include "llvm/Object/ELFObjectFile.h"
35 #include "llvm/Support/Compression.h"
36 #include "llvm/Support/Endian.h"
37 #include "llvm/Support/LEB128.h"
38 #include "llvm/Support/MD5.h"
39 #include <cstdlib>
40 #include <thread>
41 
42 using namespace llvm;
43 using namespace llvm::dwarf;
44 using namespace llvm::ELF;
45 using namespace llvm::object;
46 using namespace llvm::support;
47 
48 using namespace lld;
49 using namespace lld::elf;
50 
51 using llvm::support::endian::read32le;
52 using llvm::support::endian::write32le;
53 using llvm::support::endian::write64le;
54 
55 constexpr size_t MergeNoTailSection::numShards;
56 
57 static uint64_t readUint(uint8_t *buf) {
58   return config->is64 ? read64(buf) : read32(buf);
59 }
60 
61 static void writeUint(uint8_t *buf, uint64_t val) {
62   if (config->is64)
63     write64(buf, val);
64   else
65     write32(buf, val);
66 }
67 
68 // Returns an LLD version string.
69 static ArrayRef<uint8_t> getVersion() {
70   // Check LLD_VERSION first for ease of testing.
71   // You can get consistent output by using the environment variable.
72   // This is only for testing.
73   StringRef s = getenv("LLD_VERSION");
74   if (s.empty())
75     s = saver.save(Twine("Linker: ") + getLLDVersion());
76 
77   // +1 to include the terminating '\0'.
78   return {(const uint8_t *)s.data(), s.size() + 1};
79 }
80 
81 // Creates a .comment section containing LLD version info.
82 // With this feature, you can identify LLD-generated binaries easily
83 // by "readelf --string-dump .comment <file>".
84 // The returned object is a mergeable string section.
85 MergeInputSection *elf::createCommentSection() {
86   return make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1,
87                                  getVersion(), ".comment");
88 }
89 
90 // .MIPS.abiflags section.
91 template <class ELFT>
92 MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags flags)
93     : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
94       flags(flags) {
95   this->entsize = sizeof(Elf_Mips_ABIFlags);
96 }
97 
98 template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *buf) {
99   memcpy(buf, &flags, sizeof(flags));
100 }
101 
102 template <class ELFT>
103 MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
104   Elf_Mips_ABIFlags flags = {};
105   bool create = false;
106 
107   for (InputSectionBase *sec : inputSections) {
108     if (sec->type != SHT_MIPS_ABIFLAGS)
109       continue;
110     sec->markDead();
111     create = true;
112 
113     std::string filename = toString(sec->file);
114     const size_t size = sec->data().size();
115     // Older version of BFD (such as the default FreeBSD linker) concatenate
116     // .MIPS.abiflags instead of merging. To allow for this case (or potential
117     // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
118     if (size < sizeof(Elf_Mips_ABIFlags)) {
119       error(filename + ": invalid size of .MIPS.abiflags section: got " +
120             Twine(size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
121       return nullptr;
122     }
123     auto *s = reinterpret_cast<const Elf_Mips_ABIFlags *>(sec->data().data());
124     if (s->version != 0) {
125       error(filename + ": unexpected .MIPS.abiflags version " +
126             Twine(s->version));
127       return nullptr;
128     }
129 
130     // LLD checks ISA compatibility in calcMipsEFlags(). Here we just
131     // select the highest number of ISA/Rev/Ext.
132     flags.isa_level = std::max(flags.isa_level, s->isa_level);
133     flags.isa_rev = std::max(flags.isa_rev, s->isa_rev);
134     flags.isa_ext = std::max(flags.isa_ext, s->isa_ext);
135     flags.gpr_size = std::max(flags.gpr_size, s->gpr_size);
136     flags.cpr1_size = std::max(flags.cpr1_size, s->cpr1_size);
137     flags.cpr2_size = std::max(flags.cpr2_size, s->cpr2_size);
138     flags.ases |= s->ases;
139     flags.flags1 |= s->flags1;
140     flags.flags2 |= s->flags2;
141     flags.fp_abi = elf::getMipsFpAbiFlag(flags.fp_abi, s->fp_abi, filename);
142   };
143 
144   if (create)
145     return make<MipsAbiFlagsSection<ELFT>>(flags);
146   return nullptr;
147 }
148 
149 // .MIPS.options section.
150 template <class ELFT>
151 MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo reginfo)
152     : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
153       reginfo(reginfo) {
154   this->entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
155 }
156 
157 template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *buf) {
158   auto *options = reinterpret_cast<Elf_Mips_Options *>(buf);
159   options->kind = ODK_REGINFO;
160   options->size = getSize();
161 
162   if (!config->relocatable)
163     reginfo.ri_gp_value = in.mipsGot->getGp();
164   memcpy(buf + sizeof(Elf_Mips_Options), &reginfo, sizeof(reginfo));
165 }
166 
167 template <class ELFT>
168 MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
169   // N64 ABI only.
170   if (!ELFT::Is64Bits)
171     return nullptr;
172 
173   std::vector<InputSectionBase *> sections;
174   for (InputSectionBase *sec : inputSections)
175     if (sec->type == SHT_MIPS_OPTIONS)
176       sections.push_back(sec);
177 
178   if (sections.empty())
179     return nullptr;
180 
181   Elf_Mips_RegInfo reginfo = {};
182   for (InputSectionBase *sec : sections) {
183     sec->markDead();
184 
185     std::string filename = toString(sec->file);
186     ArrayRef<uint8_t> d = sec->data();
187 
188     while (!d.empty()) {
189       if (d.size() < sizeof(Elf_Mips_Options)) {
190         error(filename + ": invalid size of .MIPS.options section");
191         break;
192       }
193 
194       auto *opt = reinterpret_cast<const Elf_Mips_Options *>(d.data());
195       if (opt->kind == ODK_REGINFO) {
196         reginfo.ri_gprmask |= opt->getRegInfo().ri_gprmask;
197         sec->getFile<ELFT>()->mipsGp0 = opt->getRegInfo().ri_gp_value;
198         break;
199       }
200 
201       if (!opt->size)
202         fatal(filename + ": zero option descriptor size");
203       d = d.slice(opt->size);
204     }
205   };
206 
207   return make<MipsOptionsSection<ELFT>>(reginfo);
208 }
209 
210 // MIPS .reginfo section.
211 template <class ELFT>
212 MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo reginfo)
213     : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
214       reginfo(reginfo) {
215   this->entsize = sizeof(Elf_Mips_RegInfo);
216 }
217 
218 template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *buf) {
219   if (!config->relocatable)
220     reginfo.ri_gp_value = in.mipsGot->getGp();
221   memcpy(buf, &reginfo, sizeof(reginfo));
222 }
223 
224 template <class ELFT>
225 MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
226   // Section should be alive for O32 and N32 ABIs only.
227   if (ELFT::Is64Bits)
228     return nullptr;
229 
230   std::vector<InputSectionBase *> sections;
231   for (InputSectionBase *sec : inputSections)
232     if (sec->type == SHT_MIPS_REGINFO)
233       sections.push_back(sec);
234 
235   if (sections.empty())
236     return nullptr;
237 
238   Elf_Mips_RegInfo reginfo = {};
239   for (InputSectionBase *sec : sections) {
240     sec->markDead();
241 
242     if (sec->data().size() != sizeof(Elf_Mips_RegInfo)) {
243       error(toString(sec->file) + ": invalid size of .reginfo section");
244       return nullptr;
245     }
246 
247     auto *r = reinterpret_cast<const Elf_Mips_RegInfo *>(sec->data().data());
248     reginfo.ri_gprmask |= r->ri_gprmask;
249     sec->getFile<ELFT>()->mipsGp0 = r->ri_gp_value;
250   };
251 
252   return make<MipsReginfoSection<ELFT>>(reginfo);
253 }
254 
255 InputSection *elf::createInterpSection() {
256   // StringSaver guarantees that the returned string ends with '\0'.
257   StringRef s = saver.save(config->dynamicLinker);
258   ArrayRef<uint8_t> contents = {(const uint8_t *)s.data(), s.size() + 1};
259 
260   return make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, contents,
261                             ".interp");
262 }
263 
264 Defined *elf::addSyntheticLocal(StringRef name, uint8_t type, uint64_t value,
265                                 uint64_t size, InputSectionBase &section) {
266   auto *s = make<Defined>(section.file, name, STB_LOCAL, STV_DEFAULT, type,
267                           value, size, &section);
268   if (in.symTab)
269     in.symTab->addSymbol(s);
270   return s;
271 }
272 
273 static size_t getHashSize() {
274   switch (config->buildId) {
275   case BuildIdKind::Fast:
276     return 8;
277   case BuildIdKind::Md5:
278   case BuildIdKind::Uuid:
279     return 16;
280   case BuildIdKind::Sha1:
281     return 20;
282   case BuildIdKind::Hexstring:
283     return config->buildIdVector.size();
284   default:
285     llvm_unreachable("unknown BuildIdKind");
286   }
287 }
288 
289 // This class represents a linker-synthesized .note.gnu.property section.
290 //
291 // In x86 and AArch64, object files may contain feature flags indicating the
292 // features that they have used. The flags are stored in a .note.gnu.property
293 // section.
294 //
295 // lld reads the sections from input files and merges them by computing AND of
296 // the flags. The result is written as a new .note.gnu.property section.
297 //
298 // If the flag is zero (which indicates that the intersection of the feature
299 // sets is empty, or some input files didn't have .note.gnu.property sections),
300 // we don't create this section.
301 GnuPropertySection::GnuPropertySection()
302     : SyntheticSection(llvm::ELF::SHF_ALLOC, llvm::ELF::SHT_NOTE, 4,
303                        ".note.gnu.property") {}
304 
305 void GnuPropertySection::writeTo(uint8_t *buf) {
306   uint32_t featureAndType = config->emachine == EM_AARCH64
307                                 ? GNU_PROPERTY_AARCH64_FEATURE_1_AND
308                                 : GNU_PROPERTY_X86_FEATURE_1_AND;
309 
310   write32(buf, 4);                                   // Name size
311   write32(buf + 4, config->is64 ? 16 : 12);          // Content size
312   write32(buf + 8, NT_GNU_PROPERTY_TYPE_0);          // Type
313   memcpy(buf + 12, "GNU", 4);                        // Name string
314   write32(buf + 16, featureAndType);                 // Feature type
315   write32(buf + 20, 4);                              // Feature size
316   write32(buf + 24, config->andFeatures);            // Feature flags
317   if (config->is64)
318     write32(buf + 28, 0); // Padding
319 }
320 
321 size_t GnuPropertySection::getSize() const { return config->is64 ? 32 : 28; }
322 
323 BuildIdSection::BuildIdSection()
324     : SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"),
325       hashSize(getHashSize()) {}
326 
327 void BuildIdSection::writeTo(uint8_t *buf) {
328   write32(buf, 4);                      // Name size
329   write32(buf + 4, hashSize);           // Content size
330   write32(buf + 8, NT_GNU_BUILD_ID);    // Type
331   memcpy(buf + 12, "GNU", 4);           // Name string
332   hashBuf = buf + 16;
333 }
334 
335 void BuildIdSection::writeBuildId(ArrayRef<uint8_t> buf) {
336   assert(buf.size() == hashSize);
337   memcpy(hashBuf, buf.data(), hashSize);
338 }
339 
340 BssSection::BssSection(StringRef name, uint64_t size, uint32_t alignment)
341     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, alignment, name) {
342   this->bss = true;
343   this->size = size;
344 }
345 
346 EhFrameSection::EhFrameSection()
347     : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
348 
349 // Search for an existing CIE record or create a new one.
350 // CIE records from input object files are uniquified by their contents
351 // and where their relocations point to.
352 template <class ELFT, class RelTy>
353 CieRecord *EhFrameSection::addCie(EhSectionPiece &cie, ArrayRef<RelTy> rels) {
354   Symbol *personality = nullptr;
355   unsigned firstRelI = cie.firstRelocation;
356   if (firstRelI != (unsigned)-1)
357     personality =
358         &cie.sec->template getFile<ELFT>()->getRelocTargetSym(rels[firstRelI]);
359 
360   // Search for an existing CIE by CIE contents/relocation target pair.
361   CieRecord *&rec = cieMap[{cie.data(), personality}];
362 
363   // If not found, create a new one.
364   if (!rec) {
365     rec = make<CieRecord>();
366     rec->cie = &cie;
367     cieRecords.push_back(rec);
368   }
369   return rec;
370 }
371 
372 // There is one FDE per function. Returns true if a given FDE
373 // points to a live function.
374 template <class ELFT, class RelTy>
375 bool EhFrameSection::isFdeLive(EhSectionPiece &fde, ArrayRef<RelTy> rels) {
376   auto *sec = cast<EhInputSection>(fde.sec);
377   unsigned firstRelI = fde.firstRelocation;
378 
379   // An FDE should point to some function because FDEs are to describe
380   // functions. That's however not always the case due to an issue of
381   // ld.gold with -r. ld.gold may discard only functions and leave their
382   // corresponding FDEs, which results in creating bad .eh_frame sections.
383   // To deal with that, we ignore such FDEs.
384   if (firstRelI == (unsigned)-1)
385     return false;
386 
387   const RelTy &rel = rels[firstRelI];
388   Symbol &b = sec->template getFile<ELFT>()->getRelocTargetSym(rel);
389 
390   // FDEs for garbage-collected or merged-by-ICF sections, or sections in
391   // another partition, are dead.
392   if (auto *d = dyn_cast<Defined>(&b))
393     if (SectionBase *sec = d->section)
394       return sec->partition == partition;
395   return false;
396 }
397 
398 // .eh_frame is a sequence of CIE or FDE records. In general, there
399 // is one CIE record per input object file which is followed by
400 // a list of FDEs. This function searches an existing CIE or create a new
401 // one and associates FDEs to the CIE.
402 template <class ELFT, class RelTy>
403 void EhFrameSection::addRecords(EhInputSection *sec, ArrayRef<RelTy> rels) {
404   offsetToCie.clear();
405   for (EhSectionPiece &piece : sec->pieces) {
406     // The empty record is the end marker.
407     if (piece.size == 4)
408       return;
409 
410     size_t offset = piece.inputOff;
411     uint32_t id = read32(piece.data().data() + 4);
412     if (id == 0) {
413       offsetToCie[offset] = addCie<ELFT>(piece, rels);
414       continue;
415     }
416 
417     uint32_t cieOffset = offset + 4 - id;
418     CieRecord *rec = offsetToCie[cieOffset];
419     if (!rec)
420       fatal(toString(sec) + ": invalid CIE reference");
421 
422     if (!isFdeLive<ELFT>(piece, rels))
423       continue;
424     rec->fdes.push_back(&piece);
425     numFdes++;
426   }
427 }
428 
429 template <class ELFT>
430 void EhFrameSection::addSectionAux(EhInputSection *sec) {
431   if (!sec->isLive())
432     return;
433   if (sec->areRelocsRela)
434     addRecords<ELFT>(sec, sec->template relas<ELFT>());
435   else
436     addRecords<ELFT>(sec, sec->template rels<ELFT>());
437 }
438 
439 void EhFrameSection::addSection(EhInputSection *sec) {
440   sec->parent = this;
441 
442   alignment = std::max(alignment, sec->alignment);
443   sections.push_back(sec);
444 
445   for (auto *ds : sec->dependentSections)
446     dependentSections.push_back(ds);
447 }
448 
449 static void writeCieFde(uint8_t *buf, ArrayRef<uint8_t> d) {
450   memcpy(buf, d.data(), d.size());
451 
452   size_t aligned = alignTo(d.size(), config->wordsize);
453 
454   // Zero-clear trailing padding if it exists.
455   memset(buf + d.size(), 0, aligned - d.size());
456 
457   // Fix the size field. -4 since size does not include the size field itself.
458   write32(buf, aligned - 4);
459 }
460 
461 void EhFrameSection::finalizeContents() {
462   assert(!this->size); // Not finalized.
463 
464   switch (config->ekind) {
465   case ELFNoneKind:
466     llvm_unreachable("invalid ekind");
467   case ELF32LEKind:
468     for (EhInputSection *sec : sections)
469       addSectionAux<ELF32LE>(sec);
470     break;
471   case ELF32BEKind:
472     for (EhInputSection *sec : sections)
473       addSectionAux<ELF32BE>(sec);
474     break;
475   case ELF64LEKind:
476     for (EhInputSection *sec : sections)
477       addSectionAux<ELF64LE>(sec);
478     break;
479   case ELF64BEKind:
480     for (EhInputSection *sec : sections)
481       addSectionAux<ELF64BE>(sec);
482     break;
483   }
484 
485   size_t off = 0;
486   for (CieRecord *rec : cieRecords) {
487     rec->cie->outputOff = off;
488     off += alignTo(rec->cie->size, config->wordsize);
489 
490     for (EhSectionPiece *fde : rec->fdes) {
491       fde->outputOff = off;
492       off += alignTo(fde->size, config->wordsize);
493     }
494   }
495 
496   // The LSB standard does not allow a .eh_frame section with zero
497   // Call Frame Information records. glibc unwind-dw2-fde.c
498   // classify_object_over_fdes expects there is a CIE record length 0 as a
499   // terminator. Thus we add one unconditionally.
500   off += 4;
501 
502   this->size = off;
503 }
504 
505 // Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table
506 // to get an FDE from an address to which FDE is applied. This function
507 // returns a list of such pairs.
508 std::vector<EhFrameSection::FdeData> EhFrameSection::getFdeData() const {
509   uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff;
510   std::vector<FdeData> ret;
511 
512   uint64_t va = getPartition().ehFrameHdr->getVA();
513   for (CieRecord *rec : cieRecords) {
514     uint8_t enc = getFdeEncoding(rec->cie);
515     for (EhSectionPiece *fde : rec->fdes) {
516       uint64_t pc = getFdePc(buf, fde->outputOff, enc);
517       uint64_t fdeVA = getParent()->addr + fde->outputOff;
518       if (!isInt<32>(pc - va))
519         fatal(toString(fde->sec) + ": PC offset is too large: 0x" +
520               Twine::utohexstr(pc - va));
521       ret.push_back({uint32_t(pc - va), uint32_t(fdeVA - va)});
522     }
523   }
524 
525   // Sort the FDE list by their PC and uniqueify. Usually there is only
526   // one FDE for a PC (i.e. function), but if ICF merges two functions
527   // into one, there can be more than one FDEs pointing to the address.
528   auto less = [](const FdeData &a, const FdeData &b) {
529     return a.pcRel < b.pcRel;
530   };
531   llvm::stable_sort(ret, less);
532   auto eq = [](const FdeData &a, const FdeData &b) {
533     return a.pcRel == b.pcRel;
534   };
535   ret.erase(std::unique(ret.begin(), ret.end(), eq), ret.end());
536 
537   return ret;
538 }
539 
540 static uint64_t readFdeAddr(uint8_t *buf, int size) {
541   switch (size) {
542   case DW_EH_PE_udata2:
543     return read16(buf);
544   case DW_EH_PE_sdata2:
545     return (int16_t)read16(buf);
546   case DW_EH_PE_udata4:
547     return read32(buf);
548   case DW_EH_PE_sdata4:
549     return (int32_t)read32(buf);
550   case DW_EH_PE_udata8:
551   case DW_EH_PE_sdata8:
552     return read64(buf);
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 & 0xf);
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   if (getPartition().ehFrameHdr && getPartition().ehFrameHdr->getParent())
597     getPartition().ehFrameHdr->write();
598 }
599 
600 GotSection::GotSection()
601     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize,
602                        ".got") {
603   // If ElfSym::globalOffsetTable is relative to .got and is referenced,
604   // increase numEntries by the number of entries used to emit
605   // ElfSym::globalOffsetTable.
606   if (ElfSym::globalOffsetTable && !target->gotBaseSymInGotPlt)
607     numEntries += target->gotHeaderEntriesNum;
608 }
609 
610 void GotSection::addEntry(Symbol &sym) {
611   sym.gotIndex = numEntries;
612   ++numEntries;
613 }
614 
615 bool GotSection::addDynTlsEntry(Symbol &sym) {
616   if (sym.globalDynIndex != -1U)
617     return false;
618   sym.globalDynIndex = numEntries;
619   // Global Dynamic TLS entries take two GOT slots.
620   numEntries += 2;
621   return true;
622 }
623 
624 // Reserves TLS entries for a TLS module ID and a TLS block offset.
625 // In total it takes two GOT slots.
626 bool GotSection::addTlsIndex() {
627   if (tlsIndexOff != uint32_t(-1))
628     return false;
629   tlsIndexOff = numEntries * config->wordsize;
630   numEntries += 2;
631   return true;
632 }
633 
634 uint64_t GotSection::getGlobalDynAddr(const Symbol &b) const {
635   return this->getVA() + b.globalDynIndex * config->wordsize;
636 }
637 
638 uint64_t GotSection::getGlobalDynOffset(const Symbol &b) const {
639   return b.globalDynIndex * config->wordsize;
640 }
641 
642 void GotSection::finalizeContents() {
643   size = numEntries * config->wordsize;
644 }
645 
646 bool GotSection::isNeeded() const {
647   // We need to emit a GOT even if it's empty if there's a relocation that is
648   // relative to GOT(such as GOTOFFREL).
649   return numEntries || hasGotOffRel;
650 }
651 
652 void GotSection::writeTo(uint8_t *buf) {
653   // Buf points to the start of this section's buffer,
654   // whereas InputSectionBase::relocateAlloc() expects its argument
655   // to point to the start of the output section.
656   target->writeGotHeader(buf);
657   relocateAlloc(buf - outSecOff, buf - outSecOff + size);
658 }
659 
660 static uint64_t getMipsPageAddr(uint64_t addr) {
661   return (addr + 0x8000) & ~0xffff;
662 }
663 
664 static uint64_t getMipsPageCount(uint64_t size) {
665   return (size + 0xfffe) / 0xffff + 1;
666 }
667 
668 MipsGotSection::MipsGotSection()
669     : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
670                        ".got") {}
671 
672 void MipsGotSection::addEntry(InputFile &file, Symbol &sym, int64_t addend,
673                               RelExpr expr) {
674   FileGot &g = getGot(file);
675   if (expr == R_MIPS_GOT_LOCAL_PAGE) {
676     if (const OutputSection *os = sym.getOutputSection())
677       g.pagesMap.insert({os, {}});
678     else
679       g.local16.insert({{nullptr, getMipsPageAddr(sym.getVA(addend))}, 0});
680   } else if (sym.isTls())
681     g.tls.insert({&sym, 0});
682   else if (sym.isPreemptible && expr == R_ABS)
683     g.relocs.insert({&sym, 0});
684   else if (sym.isPreemptible)
685     g.global.insert({&sym, 0});
686   else if (expr == R_MIPS_GOT_OFF32)
687     g.local32.insert({{&sym, addend}, 0});
688   else
689     g.local16.insert({{&sym, addend}, 0});
690 }
691 
692 void MipsGotSection::addDynTlsEntry(InputFile &file, Symbol &sym) {
693   getGot(file).dynTlsSymbols.insert({&sym, 0});
694 }
695 
696 void MipsGotSection::addTlsIndex(InputFile &file) {
697   getGot(file).dynTlsSymbols.insert({nullptr, 0});
698 }
699 
700 size_t MipsGotSection::FileGot::getEntriesNum() const {
701   return getPageEntriesNum() + local16.size() + global.size() + relocs.size() +
702          tls.size() + dynTlsSymbols.size() * 2;
703 }
704 
705 size_t MipsGotSection::FileGot::getPageEntriesNum() const {
706   size_t num = 0;
707   for (const std::pair<const OutputSection *, FileGot::PageBlock> &p : pagesMap)
708     num += p.second.count;
709   return num;
710 }
711 
712 size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {
713   size_t count = getPageEntriesNum() + local16.size() + global.size();
714   // If there are relocation-only entries in the GOT, TLS entries
715   // are allocated after them. TLS entries should be addressable
716   // by 16-bit index so count both reloc-only and TLS entries.
717   if (!tls.empty() || !dynTlsSymbols.empty())
718     count += relocs.size() + tls.size() + dynTlsSymbols.size() * 2;
719   return count;
720 }
721 
722 MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &f) {
723   if (!f.mipsGotIndex.hasValue()) {
724     gots.emplace_back();
725     gots.back().file = &f;
726     f.mipsGotIndex = gots.size() - 1;
727   }
728   return gots[*f.mipsGotIndex];
729 }
730 
731 uint64_t MipsGotSection::getPageEntryOffset(const InputFile *f,
732                                             const Symbol &sym,
733                                             int64_t addend) const {
734   const FileGot &g = gots[*f->mipsGotIndex];
735   uint64_t index = 0;
736   if (const OutputSection *outSec = sym.getOutputSection()) {
737     uint64_t secAddr = getMipsPageAddr(outSec->addr);
738     uint64_t symAddr = getMipsPageAddr(sym.getVA(addend));
739     index = g.pagesMap.lookup(outSec).firstIndex + (symAddr - secAddr) / 0xffff;
740   } else {
741     index = g.local16.lookup({nullptr, getMipsPageAddr(sym.getVA(addend))});
742   }
743   return index * config->wordsize;
744 }
745 
746 uint64_t MipsGotSection::getSymEntryOffset(const InputFile *f, const Symbol &s,
747                                            int64_t addend) const {
748   const FileGot &g = gots[*f->mipsGotIndex];
749   Symbol *sym = const_cast<Symbol *>(&s);
750   if (sym->isTls())
751     return g.tls.lookup(sym) * config->wordsize;
752   if (sym->isPreemptible)
753     return g.global.lookup(sym) * config->wordsize;
754   return g.local16.lookup({sym, addend}) * config->wordsize;
755 }
756 
757 uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *f) const {
758   const FileGot &g = gots[*f->mipsGotIndex];
759   return g.dynTlsSymbols.lookup(nullptr) * config->wordsize;
760 }
761 
762 uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *f,
763                                             const Symbol &s) const {
764   const FileGot &g = gots[*f->mipsGotIndex];
765   Symbol *sym = const_cast<Symbol *>(&s);
766   return g.dynTlsSymbols.lookup(sym) * config->wordsize;
767 }
768 
769 const Symbol *MipsGotSection::getFirstGlobalEntry() const {
770   if (gots.empty())
771     return nullptr;
772   const FileGot &primGot = gots.front();
773   if (!primGot.global.empty())
774     return primGot.global.front().first;
775   if (!primGot.relocs.empty())
776     return primGot.relocs.front().first;
777   return nullptr;
778 }
779 
780 unsigned MipsGotSection::getLocalEntriesNum() const {
781   if (gots.empty())
782     return headerEntriesNum;
783   return headerEntriesNum + gots.front().getPageEntriesNum() +
784          gots.front().local16.size();
785 }
786 
787 bool MipsGotSection::tryMergeGots(FileGot &dst, FileGot &src, bool isPrimary) {
788   FileGot tmp = dst;
789   set_union(tmp.pagesMap, src.pagesMap);
790   set_union(tmp.local16, src.local16);
791   set_union(tmp.global, src.global);
792   set_union(tmp.relocs, src.relocs);
793   set_union(tmp.tls, src.tls);
794   set_union(tmp.dynTlsSymbols, src.dynTlsSymbols);
795 
796   size_t count = isPrimary ? headerEntriesNum : 0;
797   count += tmp.getIndexedEntriesNum();
798 
799   if (count * config->wordsize > config->mipsGotSize)
800     return false;
801 
802   std::swap(tmp, dst);
803   return true;
804 }
805 
806 void MipsGotSection::finalizeContents() { updateAllocSize(); }
807 
808 bool MipsGotSection::updateAllocSize() {
809   size = headerEntriesNum * config->wordsize;
810   for (const FileGot &g : gots)
811     size += g.getEntriesNum() * config->wordsize;
812   return false;
813 }
814 
815 void MipsGotSection::build() {
816   if (gots.empty())
817     return;
818 
819   std::vector<FileGot> mergedGots(1);
820 
821   // For each GOT move non-preemptible symbols from the `Global`
822   // to `Local16` list. Preemptible symbol might become non-preemptible
823   // one if, for example, it gets a related copy relocation.
824   for (FileGot &got : gots) {
825     for (auto &p: got.global)
826       if (!p.first->isPreemptible)
827         got.local16.insert({{p.first, 0}, 0});
828     got.global.remove_if([&](const std::pair<Symbol *, size_t> &p) {
829       return !p.first->isPreemptible;
830     });
831   }
832 
833   // For each GOT remove "reloc-only" entry if there is "global"
834   // entry for the same symbol. And add local entries which indexed
835   // using 32-bit value at the end of 16-bit entries.
836   for (FileGot &got : gots) {
837     got.relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {
838       return got.global.count(p.first);
839     });
840     set_union(got.local16, got.local32);
841     got.local32.clear();
842   }
843 
844   // Evaluate number of "reloc-only" entries in the resulting GOT.
845   // To do that put all unique "reloc-only" and "global" entries
846   // from all GOTs to the future primary GOT.
847   FileGot *primGot = &mergedGots.front();
848   for (FileGot &got : gots) {
849     set_union(primGot->relocs, got.global);
850     set_union(primGot->relocs, got.relocs);
851     got.relocs.clear();
852   }
853 
854   // Evaluate number of "page" entries in each GOT.
855   for (FileGot &got : gots) {
856     for (std::pair<const OutputSection *, FileGot::PageBlock> &p :
857          got.pagesMap) {
858       const OutputSection *os = p.first;
859       uint64_t secSize = 0;
860       for (BaseCommand *cmd : os->sectionCommands) {
861         if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
862           for (InputSection *isec : isd->sections) {
863             uint64_t off = alignTo(secSize, isec->alignment);
864             secSize = off + isec->getSize();
865           }
866       }
867       p.second.count = getMipsPageCount(secSize);
868     }
869   }
870 
871   // Merge GOTs. Try to join as much as possible GOTs but do not exceed
872   // maximum GOT size. At first, try to fill the primary GOT because
873   // the primary GOT can be accessed in the most effective way. If it
874   // is not possible, try to fill the last GOT in the list, and finally
875   // create a new GOT if both attempts failed.
876   for (FileGot &srcGot : gots) {
877     InputFile *file = srcGot.file;
878     if (tryMergeGots(mergedGots.front(), srcGot, true)) {
879       file->mipsGotIndex = 0;
880     } else {
881       // If this is the first time we failed to merge with the primary GOT,
882       // MergedGots.back() will also be the primary GOT. We must make sure not
883       // to try to merge again with isPrimary=false, as otherwise, if the
884       // inputs are just right, we could allow the primary GOT to become 1 or 2
885       // words bigger due to ignoring the header size.
886       if (mergedGots.size() == 1 ||
887           !tryMergeGots(mergedGots.back(), srcGot, false)) {
888         mergedGots.emplace_back();
889         std::swap(mergedGots.back(), srcGot);
890       }
891       file->mipsGotIndex = mergedGots.size() - 1;
892     }
893   }
894   std::swap(gots, mergedGots);
895 
896   // Reduce number of "reloc-only" entries in the primary GOT
897   // by substracting "global" entries exist in the primary GOT.
898   primGot = &gots.front();
899   primGot->relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {
900     return primGot->global.count(p.first);
901   });
902 
903   // Calculate indexes for each GOT entry.
904   size_t index = headerEntriesNum;
905   for (FileGot &got : gots) {
906     got.startIndex = &got == primGot ? 0 : index;
907     for (std::pair<const OutputSection *, FileGot::PageBlock> &p :
908          got.pagesMap) {
909       // For each output section referenced by GOT page relocations calculate
910       // and save into pagesMap an upper bound of MIPS GOT entries required
911       // to store page addresses of local symbols. We assume the worst case -
912       // each 64kb page of the output section has at least one GOT relocation
913       // against it. And take in account the case when the section intersects
914       // page boundaries.
915       p.second.firstIndex = index;
916       index += p.second.count;
917     }
918     for (auto &p: got.local16)
919       p.second = index++;
920     for (auto &p: got.global)
921       p.second = index++;
922     for (auto &p: got.relocs)
923       p.second = index++;
924     for (auto &p: got.tls)
925       p.second = index++;
926     for (auto &p: got.dynTlsSymbols) {
927       p.second = index;
928       index += 2;
929     }
930   }
931 
932   // Update Symbol::gotIndex field to use this
933   // value later in the `sortMipsSymbols` function.
934   for (auto &p : primGot->global)
935     p.first->gotIndex = p.second;
936   for (auto &p : primGot->relocs)
937     p.first->gotIndex = p.second;
938 
939   // Create dynamic relocations.
940   for (FileGot &got : gots) {
941     // Create dynamic relocations for TLS entries.
942     for (std::pair<Symbol *, size_t> &p : got.tls) {
943       Symbol *s = p.first;
944       uint64_t offset = p.second * config->wordsize;
945       if (s->isPreemptible)
946         mainPart->relaDyn->addReloc(target->tlsGotRel, this, offset, s);
947     }
948     for (std::pair<Symbol *, size_t> &p : got.dynTlsSymbols) {
949       Symbol *s = p.first;
950       uint64_t offset = p.second * config->wordsize;
951       if (s == nullptr) {
952         if (!config->isPic)
953           continue;
954         mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, this, offset, s);
955       } else {
956         // When building a shared library we still need a dynamic relocation
957         // for the module index. Therefore only checking for
958         // S->isPreemptible is not sufficient (this happens e.g. for
959         // thread-locals that have been marked as local through a linker script)
960         if (!s->isPreemptible && !config->isPic)
961           continue;
962         mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, this, offset, s);
963         // However, we can skip writing the TLS offset reloc for non-preemptible
964         // symbols since it is known even in shared libraries
965         if (!s->isPreemptible)
966           continue;
967         offset += config->wordsize;
968         mainPart->relaDyn->addReloc(target->tlsOffsetRel, this, offset, s);
969       }
970     }
971 
972     // Do not create dynamic relocations for non-TLS
973     // entries in the primary GOT.
974     if (&got == primGot)
975       continue;
976 
977     // Dynamic relocations for "global" entries.
978     for (const std::pair<Symbol *, size_t> &p : got.global) {
979       uint64_t offset = p.second * config->wordsize;
980       mainPart->relaDyn->addReloc(target->relativeRel, this, offset, p.first);
981     }
982     if (!config->isPic)
983       continue;
984     // Dynamic relocations for "local" entries in case of PIC.
985     for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :
986          got.pagesMap) {
987       size_t pageCount = l.second.count;
988       for (size_t pi = 0; pi < pageCount; ++pi) {
989         uint64_t offset = (l.second.firstIndex + pi) * config->wordsize;
990         mainPart->relaDyn->addReloc({target->relativeRel, this, offset, l.first,
991                                  int64_t(pi * 0x10000)});
992       }
993     }
994     for (const std::pair<GotEntry, size_t> &p : got.local16) {
995       uint64_t offset = p.second * config->wordsize;
996       mainPart->relaDyn->addReloc({target->relativeRel, this, offset, true,
997                                p.first.first, p.first.second});
998     }
999   }
1000 }
1001 
1002 bool MipsGotSection::isNeeded() const {
1003   // We add the .got section to the result for dynamic MIPS target because
1004   // its address and properties are mentioned in the .dynamic section.
1005   return !config->relocatable;
1006 }
1007 
1008 uint64_t MipsGotSection::getGp(const InputFile *f) const {
1009   // For files without related GOT or files refer a primary GOT
1010   // returns "common" _gp value. For secondary GOTs calculate
1011   // individual _gp values.
1012   if (!f || !f->mipsGotIndex.hasValue() || *f->mipsGotIndex == 0)
1013     return ElfSym::mipsGp->getVA(0);
1014   return getVA() + gots[*f->mipsGotIndex].startIndex * config->wordsize +
1015          0x7ff0;
1016 }
1017 
1018 void MipsGotSection::writeTo(uint8_t *buf) {
1019   // Set the MSB of the second GOT slot. This is not required by any
1020   // MIPS ABI documentation, though.
1021   //
1022   // There is a comment in glibc saying that "The MSB of got[1] of a
1023   // gnu object is set to identify gnu objects," and in GNU gold it
1024   // says "the second entry will be used by some runtime loaders".
1025   // But how this field is being used is unclear.
1026   //
1027   // We are not really willing to mimic other linkers behaviors
1028   // without understanding why they do that, but because all files
1029   // generated by GNU tools have this special GOT value, and because
1030   // we've been doing this for years, it is probably a safe bet to
1031   // keep doing this for now. We really need to revisit this to see
1032   // if we had to do this.
1033   writeUint(buf + config->wordsize, (uint64_t)1 << (config->wordsize * 8 - 1));
1034   for (const FileGot &g : gots) {
1035     auto write = [&](size_t i, const Symbol *s, int64_t a) {
1036       uint64_t va = a;
1037       if (s)
1038         va = s->getVA(a);
1039       writeUint(buf + i * config->wordsize, va);
1040     };
1041     // Write 'page address' entries to the local part of the GOT.
1042     for (const std::pair<const OutputSection *, FileGot::PageBlock> &l :
1043          g.pagesMap) {
1044       size_t pageCount = l.second.count;
1045       uint64_t firstPageAddr = getMipsPageAddr(l.first->addr);
1046       for (size_t pi = 0; pi < pageCount; ++pi)
1047         write(l.second.firstIndex + pi, nullptr, firstPageAddr + pi * 0x10000);
1048     }
1049     // Local, global, TLS, reloc-only  entries.
1050     // If TLS entry has a corresponding dynamic relocations, leave it
1051     // initialized by zero. Write down adjusted TLS symbol's values otherwise.
1052     // To calculate the adjustments use offsets for thread-local storage.
1053     // https://www.linux-mips.org/wiki/NPTL
1054     for (const std::pair<GotEntry, size_t> &p : g.local16)
1055       write(p.second, p.first.first, p.first.second);
1056     // Write VA to the primary GOT only. For secondary GOTs that
1057     // will be done by REL32 dynamic relocations.
1058     if (&g == &gots.front())
1059       for (const std::pair<const Symbol *, size_t> &p : g.global)
1060         write(p.second, p.first, 0);
1061     for (const std::pair<Symbol *, size_t> &p : g.relocs)
1062       write(p.second, p.first, 0);
1063     for (const std::pair<Symbol *, size_t> &p : g.tls)
1064       write(p.second, p.first, p.first->isPreemptible ? 0 : -0x7000);
1065     for (const std::pair<Symbol *, size_t> &p : g.dynTlsSymbols) {
1066       if (p.first == nullptr && !config->isPic)
1067         write(p.second, nullptr, 1);
1068       else if (p.first && !p.first->isPreemptible) {
1069         // If we are emitting PIC code with relocations we mustn't write
1070         // anything to the GOT here. When using Elf_Rel relocations the value
1071         // one will be treated as an addend and will cause crashes at runtime
1072         if (!config->isPic)
1073           write(p.second, nullptr, 1);
1074         write(p.second + 1, p.first, -0x8000);
1075       }
1076     }
1077   }
1078 }
1079 
1080 // On PowerPC the .plt section is used to hold the table of function addresses
1081 // instead of the .got.plt, and the type is SHT_NOBITS similar to a .bss
1082 // section. I don't know why we have a BSS style type for the section but it is
1083 // consitent across both 64-bit PowerPC ABIs as well as the 32-bit PowerPC ABI.
1084 GotPltSection::GotPltSection()
1085     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize,
1086                        ".got.plt") {
1087   if (config->emachine == EM_PPC) {
1088     name = ".plt";
1089   } else if (config->emachine == EM_PPC64) {
1090     type = SHT_NOBITS;
1091     name = ".plt";
1092   }
1093 }
1094 
1095 void GotPltSection::addEntry(Symbol &sym) {
1096   assert(sym.pltIndex == entries.size());
1097   entries.push_back(&sym);
1098 }
1099 
1100 size_t GotPltSection::getSize() const {
1101   return (target->gotPltHeaderEntriesNum + entries.size()) * config->wordsize;
1102 }
1103 
1104 void GotPltSection::writeTo(uint8_t *buf) {
1105   target->writeGotPltHeader(buf);
1106   buf += target->gotPltHeaderEntriesNum * config->wordsize;
1107   for (const Symbol *b : entries) {
1108     target->writeGotPlt(buf, *b);
1109     buf += config->wordsize;
1110   }
1111 }
1112 
1113 bool GotPltSection::isNeeded() const {
1114   // We need to emit GOTPLT even if it's empty if there's a relocation relative
1115   // to it.
1116   return !entries.empty() || hasGotPltOffRel;
1117 }
1118 
1119 static StringRef getIgotPltName() {
1120   // On ARM the IgotPltSection is part of the GotSection.
1121   if (config->emachine == EM_ARM)
1122     return ".got";
1123 
1124   // On PowerPC64 the GotPltSection is renamed to '.plt' so the IgotPltSection
1125   // needs to be named the same.
1126   if (config->emachine == EM_PPC64)
1127     return ".plt";
1128 
1129   return ".got.plt";
1130 }
1131 
1132 // On PowerPC64 the GotPltSection type is SHT_NOBITS so we have to follow suit
1133 // with the IgotPltSection.
1134 IgotPltSection::IgotPltSection()
1135     : SyntheticSection(SHF_ALLOC | SHF_WRITE,
1136                        config->emachine == EM_PPC64 ? SHT_NOBITS : SHT_PROGBITS,
1137                        config->wordsize, getIgotPltName()) {}
1138 
1139 void IgotPltSection::addEntry(Symbol &sym) {
1140   assert(sym.pltIndex == entries.size());
1141   entries.push_back(&sym);
1142 }
1143 
1144 size_t IgotPltSection::getSize() const {
1145   return entries.size() * config->wordsize;
1146 }
1147 
1148 void IgotPltSection::writeTo(uint8_t *buf) {
1149   for (const Symbol *b : entries) {
1150     target->writeIgotPlt(buf, *b);
1151     buf += config->wordsize;
1152   }
1153 }
1154 
1155 StringTableSection::StringTableSection(StringRef name, bool dynamic)
1156     : SyntheticSection(dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, name),
1157       dynamic(dynamic) {
1158   // ELF string tables start with a NUL byte.
1159   addString("");
1160 }
1161 
1162 // Adds a string to the string table. If `hashIt` is true we hash and check for
1163 // duplicates. It is optional because the name of global symbols are already
1164 // uniqued and hashing them again has a big cost for a small value: uniquing
1165 // them with some other string that happens to be the same.
1166 unsigned StringTableSection::addString(StringRef s, bool hashIt) {
1167   if (hashIt) {
1168     auto r = stringMap.insert(std::make_pair(s, this->size));
1169     if (!r.second)
1170       return r.first->second;
1171   }
1172   unsigned ret = this->size;
1173   this->size = this->size + s.size() + 1;
1174   strings.push_back(s);
1175   return ret;
1176 }
1177 
1178 void StringTableSection::writeTo(uint8_t *buf) {
1179   for (StringRef s : strings) {
1180     memcpy(buf, s.data(), s.size());
1181     buf[s.size()] = '\0';
1182     buf += s.size() + 1;
1183   }
1184 }
1185 
1186 // Returns the number of entries in .gnu.version_d: the number of
1187 // non-VER_NDX_LOCAL-non-VER_NDX_GLOBAL definitions, plus 1.
1188 // Note that we don't support vd_cnt > 1 yet.
1189 static unsigned getVerDefNum() {
1190   return namedVersionDefs().size() + 1;
1191 }
1192 
1193 template <class ELFT>
1194 DynamicSection<ELFT>::DynamicSection()
1195     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, config->wordsize,
1196                        ".dynamic") {
1197   this->entsize = ELFT::Is64Bits ? 16 : 8;
1198 
1199   // .dynamic section is not writable on MIPS and on Fuchsia OS
1200   // which passes -z rodynamic.
1201   // See "Special Section" in Chapter 4 in the following document:
1202   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1203   if (config->emachine == EM_MIPS || config->zRodynamic)
1204     this->flags = SHF_ALLOC;
1205 }
1206 
1207 template <class ELFT>
1208 void DynamicSection<ELFT>::add(int32_t tag, std::function<uint64_t()> fn) {
1209   entries.push_back({tag, fn});
1210 }
1211 
1212 template <class ELFT>
1213 void DynamicSection<ELFT>::addInt(int32_t tag, uint64_t val) {
1214   entries.push_back({tag, [=] { return val; }});
1215 }
1216 
1217 template <class ELFT>
1218 void DynamicSection<ELFT>::addInSec(int32_t tag, InputSection *sec) {
1219   entries.push_back({tag, [=] { return sec->getVA(0); }});
1220 }
1221 
1222 template <class ELFT>
1223 void DynamicSection<ELFT>::addInSecRelative(int32_t tag, InputSection *sec) {
1224   size_t tagOffset = entries.size() * entsize;
1225   entries.push_back(
1226       {tag, [=] { return sec->getVA(0) - (getVA() + tagOffset); }});
1227 }
1228 
1229 template <class ELFT>
1230 void DynamicSection<ELFT>::addOutSec(int32_t tag, OutputSection *sec) {
1231   entries.push_back({tag, [=] { return sec->addr; }});
1232 }
1233 
1234 template <class ELFT>
1235 void DynamicSection<ELFT>::addSize(int32_t tag, OutputSection *sec) {
1236   entries.push_back({tag, [=] { return sec->size; }});
1237 }
1238 
1239 template <class ELFT>
1240 void DynamicSection<ELFT>::addSym(int32_t tag, Symbol *sym) {
1241   entries.push_back({tag, [=] { return sym->getVA(); }});
1242 }
1243 
1244 // The output section .rela.dyn may include these synthetic sections:
1245 //
1246 // - part.relaDyn
1247 // - in.relaIplt: this is included if in.relaIplt is named .rela.dyn
1248 // - in.relaPlt: this is included if a linker script places .rela.plt inside
1249 //   .rela.dyn
1250 //
1251 // DT_RELASZ is the total size of the included sections.
1252 static std::function<uint64_t()> addRelaSz(RelocationBaseSection *relaDyn) {
1253   return [=]() {
1254     size_t size = relaDyn->getSize();
1255     if (in.relaIplt->getParent() == relaDyn->getParent())
1256       size += in.relaIplt->getSize();
1257     if (in.relaPlt->getParent() == relaDyn->getParent())
1258       size += in.relaPlt->getSize();
1259     return size;
1260   };
1261 }
1262 
1263 // A Linker script may assign the RELA relocation sections to the same
1264 // output section. When this occurs we cannot just use the OutputSection
1265 // Size. Moreover the [DT_JMPREL, DT_JMPREL + DT_PLTRELSZ) is permitted to
1266 // overlap with the [DT_RELA, DT_RELA + DT_RELASZ).
1267 static uint64_t addPltRelSz() {
1268   size_t size = in.relaPlt->getSize();
1269   if (in.relaIplt->getParent() == in.relaPlt->getParent() &&
1270       in.relaIplt->name == in.relaPlt->name)
1271     size += in.relaIplt->getSize();
1272   return size;
1273 }
1274 
1275 // Add remaining entries to complete .dynamic contents.
1276 template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
1277   elf::Partition &part = getPartition();
1278   bool isMain = part.name.empty();
1279 
1280   for (StringRef s : config->filterList)
1281     addInt(DT_FILTER, part.dynStrTab->addString(s));
1282   for (StringRef s : config->auxiliaryList)
1283     addInt(DT_AUXILIARY, part.dynStrTab->addString(s));
1284 
1285   if (!config->rpath.empty())
1286     addInt(config->enableNewDtags ? DT_RUNPATH : DT_RPATH,
1287            part.dynStrTab->addString(config->rpath));
1288 
1289   for (SharedFile *file : sharedFiles)
1290     if (file->isNeeded)
1291       addInt(DT_NEEDED, part.dynStrTab->addString(file->soName));
1292 
1293   if (isMain) {
1294     if (!config->soName.empty())
1295       addInt(DT_SONAME, part.dynStrTab->addString(config->soName));
1296   } else {
1297     if (!config->soName.empty())
1298       addInt(DT_NEEDED, part.dynStrTab->addString(config->soName));
1299     addInt(DT_SONAME, part.dynStrTab->addString(part.name));
1300   }
1301 
1302   // Set DT_FLAGS and DT_FLAGS_1.
1303   uint32_t dtFlags = 0;
1304   uint32_t dtFlags1 = 0;
1305   if (config->bsymbolic)
1306     dtFlags |= DF_SYMBOLIC;
1307   if (config->zGlobal)
1308     dtFlags1 |= DF_1_GLOBAL;
1309   if (config->zInitfirst)
1310     dtFlags1 |= DF_1_INITFIRST;
1311   if (config->zInterpose)
1312     dtFlags1 |= DF_1_INTERPOSE;
1313   if (config->zNodefaultlib)
1314     dtFlags1 |= DF_1_NODEFLIB;
1315   if (config->zNodelete)
1316     dtFlags1 |= DF_1_NODELETE;
1317   if (config->zNodlopen)
1318     dtFlags1 |= DF_1_NOOPEN;
1319   if (config->zNow) {
1320     dtFlags |= DF_BIND_NOW;
1321     dtFlags1 |= DF_1_NOW;
1322   }
1323   if (config->zOrigin) {
1324     dtFlags |= DF_ORIGIN;
1325     dtFlags1 |= DF_1_ORIGIN;
1326   }
1327   if (!config->zText)
1328     dtFlags |= DF_TEXTREL;
1329   if (config->hasStaticTlsModel)
1330     dtFlags |= DF_STATIC_TLS;
1331 
1332   if (dtFlags)
1333     addInt(DT_FLAGS, dtFlags);
1334   if (dtFlags1)
1335     addInt(DT_FLAGS_1, dtFlags1);
1336 
1337   // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
1338   // need it for each process, so we don't write it for DSOs. The loader writes
1339   // the pointer into this entry.
1340   //
1341   // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
1342   // systems (currently only Fuchsia OS) provide other means to give the
1343   // debugger this information. Such systems may choose make .dynamic read-only.
1344   // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
1345   if (!config->shared && !config->relocatable && !config->zRodynamic)
1346     addInt(DT_DEBUG, 0);
1347 
1348   if (OutputSection *sec = part.dynStrTab->getParent())
1349     this->link = sec->sectionIndex;
1350 
1351   if (part.relaDyn->isNeeded() ||
1352       (in.relaIplt->isNeeded() &&
1353        part.relaDyn->getParent() == in.relaIplt->getParent())) {
1354     addInSec(part.relaDyn->dynamicTag, part.relaDyn);
1355     entries.push_back({part.relaDyn->sizeDynamicTag, addRelaSz(part.relaDyn)});
1356 
1357     bool isRela = config->isRela;
1358     addInt(isRela ? DT_RELAENT : DT_RELENT,
1359            isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
1360 
1361     // MIPS dynamic loader does not support RELCOUNT tag.
1362     // The problem is in the tight relation between dynamic
1363     // relocations and GOT. So do not emit this tag on MIPS.
1364     if (config->emachine != EM_MIPS) {
1365       size_t numRelativeRels = part.relaDyn->getRelativeRelocCount();
1366       if (config->zCombreloc && numRelativeRels)
1367         addInt(isRela ? DT_RELACOUNT : DT_RELCOUNT, numRelativeRels);
1368     }
1369   }
1370   if (part.relrDyn && !part.relrDyn->relocs.empty()) {
1371     addInSec(config->useAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,
1372              part.relrDyn);
1373     addSize(config->useAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,
1374             part.relrDyn->getParent());
1375     addInt(config->useAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,
1376            sizeof(Elf_Relr));
1377   }
1378   // .rel[a].plt section usually consists of two parts, containing plt and
1379   // iplt relocations. It is possible to have only iplt relocations in the
1380   // output. In that case relaPlt is empty and have zero offset, the same offset
1381   // as relaIplt has. And we still want to emit proper dynamic tags for that
1382   // case, so here we always use relaPlt as marker for the begining of
1383   // .rel[a].plt section.
1384   if (isMain && (in.relaPlt->isNeeded() || in.relaIplt->isNeeded())) {
1385     addInSec(DT_JMPREL, in.relaPlt);
1386     entries.push_back({DT_PLTRELSZ, addPltRelSz});
1387     switch (config->emachine) {
1388     case EM_MIPS:
1389       addInSec(DT_MIPS_PLTGOT, in.gotPlt);
1390       break;
1391     case EM_SPARCV9:
1392       addInSec(DT_PLTGOT, in.plt);
1393       break;
1394     default:
1395       addInSec(DT_PLTGOT, in.gotPlt);
1396       break;
1397     }
1398     addInt(DT_PLTREL, config->isRela ? DT_RELA : DT_REL);
1399   }
1400 
1401   if (config->emachine == EM_AARCH64) {
1402     if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
1403       addInt(DT_AARCH64_BTI_PLT, 0);
1404     if (config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)
1405       addInt(DT_AARCH64_PAC_PLT, 0);
1406   }
1407 
1408   addInSec(DT_SYMTAB, part.dynSymTab);
1409   addInt(DT_SYMENT, sizeof(Elf_Sym));
1410   addInSec(DT_STRTAB, part.dynStrTab);
1411   addInt(DT_STRSZ, part.dynStrTab->getSize());
1412   if (!config->zText)
1413     addInt(DT_TEXTREL, 0);
1414   if (part.gnuHashTab)
1415     addInSec(DT_GNU_HASH, part.gnuHashTab);
1416   if (part.hashTab)
1417     addInSec(DT_HASH, part.hashTab);
1418 
1419   if (isMain) {
1420     if (Out::preinitArray) {
1421       addOutSec(DT_PREINIT_ARRAY, Out::preinitArray);
1422       addSize(DT_PREINIT_ARRAYSZ, Out::preinitArray);
1423     }
1424     if (Out::initArray) {
1425       addOutSec(DT_INIT_ARRAY, Out::initArray);
1426       addSize(DT_INIT_ARRAYSZ, Out::initArray);
1427     }
1428     if (Out::finiArray) {
1429       addOutSec(DT_FINI_ARRAY, Out::finiArray);
1430       addSize(DT_FINI_ARRAYSZ, Out::finiArray);
1431     }
1432 
1433     if (Symbol *b = symtab->find(config->init))
1434       if (b->isDefined())
1435         addSym(DT_INIT, b);
1436     if (Symbol *b = symtab->find(config->fini))
1437       if (b->isDefined())
1438         addSym(DT_FINI, b);
1439   }
1440 
1441   bool hasVerNeed = SharedFile::vernauxNum != 0;
1442   if (hasVerNeed || part.verDef)
1443     addInSec(DT_VERSYM, part.verSym);
1444   if (part.verDef) {
1445     addInSec(DT_VERDEF, part.verDef);
1446     addInt(DT_VERDEFNUM, getVerDefNum());
1447   }
1448   if (hasVerNeed) {
1449     addInSec(DT_VERNEED, part.verNeed);
1450     unsigned needNum = 0;
1451     for (SharedFile *f : sharedFiles)
1452       if (!f->vernauxs.empty())
1453         ++needNum;
1454     addInt(DT_VERNEEDNUM, needNum);
1455   }
1456 
1457   if (config->emachine == EM_MIPS) {
1458     addInt(DT_MIPS_RLD_VERSION, 1);
1459     addInt(DT_MIPS_FLAGS, RHF_NOTPOT);
1460     addInt(DT_MIPS_BASE_ADDRESS, target->getImageBase());
1461     addInt(DT_MIPS_SYMTABNO, part.dynSymTab->getNumSymbols());
1462 
1463     add(DT_MIPS_LOCAL_GOTNO, [] { return in.mipsGot->getLocalEntriesNum(); });
1464 
1465     if (const Symbol *b = in.mipsGot->getFirstGlobalEntry())
1466       addInt(DT_MIPS_GOTSYM, b->dynsymIndex);
1467     else
1468       addInt(DT_MIPS_GOTSYM, part.dynSymTab->getNumSymbols());
1469     addInSec(DT_PLTGOT, in.mipsGot);
1470     if (in.mipsRldMap) {
1471       if (!config->pie)
1472         addInSec(DT_MIPS_RLD_MAP, in.mipsRldMap);
1473       // Store the offset to the .rld_map section
1474       // relative to the address of the tag.
1475       addInSecRelative(DT_MIPS_RLD_MAP_REL, in.mipsRldMap);
1476     }
1477   }
1478 
1479   // DT_PPC_GOT indicates to glibc Secure PLT is used. If DT_PPC_GOT is absent,
1480   // glibc assumes the old-style BSS PLT layout which we don't support.
1481   if (config->emachine == EM_PPC)
1482     add(DT_PPC_GOT, [] { return in.got->getVA(); });
1483 
1484   // Glink dynamic tag is required by the V2 abi if the plt section isn't empty.
1485   if (config->emachine == EM_PPC64 && in.plt->isNeeded()) {
1486     // The Glink tag points to 32 bytes before the first lazy symbol resolution
1487     // stub, which starts directly after the header.
1488     entries.push_back({DT_PPC64_GLINK, [=] {
1489                          unsigned offset = target->pltHeaderSize - 32;
1490                          return in.plt->getVA(0) + offset;
1491                        }});
1492   }
1493 
1494   addInt(DT_NULL, 0);
1495 
1496   getParent()->link = this->link;
1497   this->size = entries.size() * this->entsize;
1498 }
1499 
1500 template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
1501   auto *p = reinterpret_cast<Elf_Dyn *>(buf);
1502 
1503   for (std::pair<int32_t, std::function<uint64_t()>> &kv : entries) {
1504     p->d_tag = kv.first;
1505     p->d_un.d_val = kv.second();
1506     ++p;
1507   }
1508 }
1509 
1510 uint64_t DynamicReloc::getOffset() const {
1511   return inputSec->getVA(offsetInSec);
1512 }
1513 
1514 int64_t DynamicReloc::computeAddend() const {
1515   if (useSymVA)
1516     return sym->getVA(addend);
1517   if (!outputSec)
1518     return addend;
1519   // See the comment in the DynamicReloc ctor.
1520   return getMipsPageAddr(outputSec->addr) + addend;
1521 }
1522 
1523 uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
1524   if (sym && !useSymVA)
1525     return symTab->getSymbolIndex(sym);
1526   return 0;
1527 }
1528 
1529 RelocationBaseSection::RelocationBaseSection(StringRef name, uint32_t type,
1530                                              int32_t dynamicTag,
1531                                              int32_t sizeDynamicTag)
1532     : SyntheticSection(SHF_ALLOC, type, config->wordsize, name),
1533       dynamicTag(dynamicTag), sizeDynamicTag(sizeDynamicTag) {}
1534 
1535 void RelocationBaseSection::addReloc(RelType dynType, InputSectionBase *isec,
1536                                      uint64_t offsetInSec, Symbol *sym) {
1537   addReloc({dynType, isec, offsetInSec, false, sym, 0});
1538 }
1539 
1540 void RelocationBaseSection::addReloc(RelType dynType,
1541                                      InputSectionBase *inputSec,
1542                                      uint64_t offsetInSec, Symbol *sym,
1543                                      int64_t addend, RelExpr expr,
1544                                      RelType type) {
1545   // Write the addends to the relocated address if required. We skip
1546   // it if the written value would be zero.
1547   if (config->writeAddends && (expr != R_ADDEND || addend != 0))
1548     inputSec->relocations.push_back({expr, type, offsetInSec, addend, sym});
1549   addReloc({dynType, inputSec, offsetInSec, expr != R_ADDEND, sym, addend});
1550 }
1551 
1552 void RelocationBaseSection::addReloc(const DynamicReloc &reloc) {
1553   if (reloc.type == target->relativeRel)
1554     ++numRelativeRelocs;
1555   relocs.push_back(reloc);
1556 }
1557 
1558 void RelocationBaseSection::finalizeContents() {
1559   SymbolTableBaseSection *symTab = getPartition().dynSymTab;
1560 
1561   // When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
1562   // relocations due to IFUNC (e.g. strcpy). sh_link will be set to 0 in that
1563   // case.
1564   if (symTab && symTab->getParent())
1565     getParent()->link = symTab->getParent()->sectionIndex;
1566   else
1567     getParent()->link = 0;
1568 
1569   if (in.relaPlt == this)
1570     getParent()->info = in.gotPlt->getParent()->sectionIndex;
1571   if (in.relaIplt == this)
1572     getParent()->info = in.igotPlt->getParent()->sectionIndex;
1573 }
1574 
1575 RelrBaseSection::RelrBaseSection()
1576     : SyntheticSection(SHF_ALLOC,
1577                        config->useAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR,
1578                        config->wordsize, ".relr.dyn") {}
1579 
1580 template <class ELFT>
1581 static void encodeDynamicReloc(SymbolTableBaseSection *symTab,
1582                                typename ELFT::Rela *p,
1583                                const DynamicReloc &rel) {
1584   if (config->isRela)
1585     p->r_addend = rel.computeAddend();
1586   p->r_offset = rel.getOffset();
1587   p->setSymbolAndType(rel.getSymIndex(symTab), rel.type, config->isMips64EL);
1588 }
1589 
1590 template <class ELFT>
1591 RelocationSection<ELFT>::RelocationSection(StringRef name, bool sort)
1592     : RelocationBaseSection(name, config->isRela ? SHT_RELA : SHT_REL,
1593                             config->isRela ? DT_RELA : DT_REL,
1594                             config->isRela ? DT_RELASZ : DT_RELSZ),
1595       sort(sort) {
1596   this->entsize = config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1597 }
1598 
1599 template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *buf) {
1600   SymbolTableBaseSection *symTab = getPartition().dynSymTab;
1601 
1602   // Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1603   // place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1604   // is to make results easier to read.
1605   if (sort)
1606     llvm::stable_sort(
1607         relocs, [&](const DynamicReloc &a, const DynamicReloc &b) {
1608           return std::make_tuple(a.type != target->relativeRel,
1609                                  a.getSymIndex(symTab), a.getOffset()) <
1610                  std::make_tuple(b.type != target->relativeRel,
1611                                  b.getSymIndex(symTab), b.getOffset());
1612         });
1613 
1614   for (const DynamicReloc &rel : relocs) {
1615     encodeDynamicReloc<ELFT>(symTab, reinterpret_cast<Elf_Rela *>(buf), rel);
1616     buf += config->isRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1617   }
1618 }
1619 
1620 template <class ELFT>
1621 AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection(
1622     StringRef name)
1623     : RelocationBaseSection(
1624           name, config->isRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL,
1625           config->isRela ? DT_ANDROID_RELA : DT_ANDROID_REL,
1626           config->isRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) {
1627   this->entsize = 1;
1628 }
1629 
1630 template <class ELFT>
1631 bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() {
1632   // This function computes the contents of an Android-format packed relocation
1633   // section.
1634   //
1635   // This format compresses relocations by using relocation groups to factor out
1636   // fields that are common between relocations and storing deltas from previous
1637   // relocations in SLEB128 format (which has a short representation for small
1638   // numbers). A good example of a relocation type with common fields is
1639   // R_*_RELATIVE, which is normally used to represent function pointers in
1640   // vtables. In the REL format, each relative relocation has the same r_info
1641   // field, and is only different from other relative relocations in terms of
1642   // the r_offset field. By sorting relocations by offset, grouping them by
1643   // r_info and representing each relocation with only the delta from the
1644   // previous offset, each 8-byte relocation can be compressed to as little as 1
1645   // byte (or less with run-length encoding). This relocation packer was able to
1646   // reduce the size of the relocation section in an Android Chromium DSO from
1647   // 2,911,184 bytes to 174,693 bytes, or 6% of the original size.
1648   //
1649   // A relocation section consists of a header containing the literal bytes
1650   // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two
1651   // elements are the total number of relocations in the section and an initial
1652   // r_offset value. The remaining elements define a sequence of relocation
1653   // groups. Each relocation group starts with a header consisting of the
1654   // following elements:
1655   //
1656   // - the number of relocations in the relocation group
1657   // - flags for the relocation group
1658   // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta
1659   //   for each relocation in the group.
1660   // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info
1661   //   field for each relocation in the group.
1662   // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and
1663   //   RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for
1664   //   each relocation in the group.
1665   //
1666   // Following the relocation group header are descriptions of each of the
1667   // relocations in the group. They consist of the following elements:
1668   //
1669   // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset
1670   //   delta for this relocation.
1671   // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info
1672   //   field for this relocation.
1673   // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and
1674   //   RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for
1675   //   this relocation.
1676 
1677   size_t oldSize = relocData.size();
1678 
1679   relocData = {'A', 'P', 'S', '2'};
1680   raw_svector_ostream os(relocData);
1681   auto add = [&](int64_t v) { encodeSLEB128(v, os); };
1682 
1683   // The format header includes the number of relocations and the initial
1684   // offset (we set this to zero because the first relocation group will
1685   // perform the initial adjustment).
1686   add(relocs.size());
1687   add(0);
1688 
1689   std::vector<Elf_Rela> relatives, nonRelatives;
1690 
1691   for (const DynamicReloc &rel : relocs) {
1692     Elf_Rela r;
1693     encodeDynamicReloc<ELFT>(getPartition().dynSymTab, &r, rel);
1694 
1695     if (r.getType(config->isMips64EL) == target->relativeRel)
1696       relatives.push_back(r);
1697     else
1698       nonRelatives.push_back(r);
1699   }
1700 
1701   llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
1702     return a.r_offset < b.r_offset;
1703   });
1704 
1705   // Try to find groups of relative relocations which are spaced one word
1706   // apart from one another. These generally correspond to vtable entries. The
1707   // format allows these groups to be encoded using a sort of run-length
1708   // encoding, but each group will cost 7 bytes in addition to the offset from
1709   // the previous group, so it is only profitable to do this for groups of
1710   // size 8 or larger.
1711   std::vector<Elf_Rela> ungroupedRelatives;
1712   std::vector<std::vector<Elf_Rela>> relativeGroups;
1713   for (auto i = relatives.begin(), e = relatives.end(); i != e;) {
1714     std::vector<Elf_Rela> group;
1715     do {
1716       group.push_back(*i++);
1717     } while (i != e && (i - 1)->r_offset + config->wordsize == i->r_offset);
1718 
1719     if (group.size() < 8)
1720       ungroupedRelatives.insert(ungroupedRelatives.end(), group.begin(),
1721                                 group.end());
1722     else
1723       relativeGroups.emplace_back(std::move(group));
1724   }
1725 
1726   // For non-relative relocations, we would like to:
1727   //   1. Have relocations with the same symbol offset to be consecutive, so
1728   //      that the runtime linker can speed-up symbol lookup by implementing an
1729   //      1-entry cache.
1730   //   2. Group relocations by r_info to reduce the size of the relocation
1731   //      section.
1732   // Since the symbol offset is the high bits in r_info, sorting by r_info
1733   // allows us to do both.
1734   //
1735   // For Rela, we also want to sort by r_addend when r_info is the same. This
1736   // enables us to group by r_addend as well.
1737   llvm::stable_sort(nonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1738     if (a.r_info != b.r_info)
1739       return a.r_info < b.r_info;
1740     if (config->isRela)
1741       return a.r_addend < b.r_addend;
1742     return false;
1743   });
1744 
1745   // Group relocations with the same r_info. Note that each group emits a group
1746   // header and that may make the relocation section larger. It is hard to
1747   // estimate the size of a group header as the encoded size of that varies
1748   // based on r_info. However, we can approximate this trade-off by the number
1749   // of values encoded. Each group header contains 3 values, and each relocation
1750   // in a group encodes one less value, as compared to when it is not grouped.
1751   // Therefore, we only group relocations if there are 3 or more of them with
1752   // the same r_info.
1753   //
1754   // For Rela, the addend for most non-relative relocations is zero, and thus we
1755   // can usually get a smaller relocation section if we group relocations with 0
1756   // addend as well.
1757   std::vector<Elf_Rela> ungroupedNonRelatives;
1758   std::vector<std::vector<Elf_Rela>> nonRelativeGroups;
1759   for (auto i = nonRelatives.begin(), e = nonRelatives.end(); i != e;) {
1760     auto j = i + 1;
1761     while (j != e && i->r_info == j->r_info &&
1762            (!config->isRela || i->r_addend == j->r_addend))
1763       ++j;
1764     if (j - i < 3 || (config->isRela && i->r_addend != 0))
1765       ungroupedNonRelatives.insert(ungroupedNonRelatives.end(), i, j);
1766     else
1767       nonRelativeGroups.emplace_back(i, j);
1768     i = j;
1769   }
1770 
1771   // Sort ungrouped relocations by offset to minimize the encoded length.
1772   llvm::sort(ungroupedNonRelatives, [](const Elf_Rela &a, const Elf_Rela &b) {
1773     return a.r_offset < b.r_offset;
1774   });
1775 
1776   unsigned hasAddendIfRela =
1777       config->isRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0;
1778 
1779   uint64_t offset = 0;
1780   uint64_t addend = 0;
1781 
1782   // Emit the run-length encoding for the groups of adjacent relative
1783   // relocations. Each group is represented using two groups in the packed
1784   // format. The first is used to set the current offset to the start of the
1785   // group (and also encodes the first relocation), and the second encodes the
1786   // remaining relocations.
1787   for (std::vector<Elf_Rela> &g : relativeGroups) {
1788     // The first relocation in the group.
1789     add(1);
1790     add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1791         RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1792     add(g[0].r_offset - offset);
1793     add(target->relativeRel);
1794     if (config->isRela) {
1795       add(g[0].r_addend - addend);
1796       addend = g[0].r_addend;
1797     }
1798 
1799     // The remaining relocations.
1800     add(g.size() - 1);
1801     add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG |
1802         RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1803     add(config->wordsize);
1804     add(target->relativeRel);
1805     if (config->isRela) {
1806       for (auto i = g.begin() + 1, e = g.end(); i != e; ++i) {
1807         add(i->r_addend - addend);
1808         addend = i->r_addend;
1809       }
1810     }
1811 
1812     offset = g.back().r_offset;
1813   }
1814 
1815   // Now the ungrouped relatives.
1816   if (!ungroupedRelatives.empty()) {
1817     add(ungroupedRelatives.size());
1818     add(RELOCATION_GROUPED_BY_INFO_FLAG | hasAddendIfRela);
1819     add(target->relativeRel);
1820     for (Elf_Rela &r : ungroupedRelatives) {
1821       add(r.r_offset - offset);
1822       offset = r.r_offset;
1823       if (config->isRela) {
1824         add(r.r_addend - addend);
1825         addend = r.r_addend;
1826       }
1827     }
1828   }
1829 
1830   // Grouped non-relatives.
1831   for (ArrayRef<Elf_Rela> g : nonRelativeGroups) {
1832     add(g.size());
1833     add(RELOCATION_GROUPED_BY_INFO_FLAG);
1834     add(g[0].r_info);
1835     for (const Elf_Rela &r : g) {
1836       add(r.r_offset - offset);
1837       offset = r.r_offset;
1838     }
1839     addend = 0;
1840   }
1841 
1842   // Finally the ungrouped non-relative relocations.
1843   if (!ungroupedNonRelatives.empty()) {
1844     add(ungroupedNonRelatives.size());
1845     add(hasAddendIfRela);
1846     for (Elf_Rela &r : ungroupedNonRelatives) {
1847       add(r.r_offset - offset);
1848       offset = r.r_offset;
1849       add(r.r_info);
1850       if (config->isRela) {
1851         add(r.r_addend - addend);
1852         addend = r.r_addend;
1853       }
1854     }
1855   }
1856 
1857   // Don't allow the section to shrink; otherwise the size of the section can
1858   // oscillate infinitely.
1859   if (relocData.size() < oldSize)
1860     relocData.append(oldSize - relocData.size(), 0);
1861 
1862   // Returns whether the section size changed. We need to keep recomputing both
1863   // section layout and the contents of this section until the size converges
1864   // because changing this section's size can affect section layout, which in
1865   // turn can affect the sizes of the LEB-encoded integers stored in this
1866   // section.
1867   return relocData.size() != oldSize;
1868 }
1869 
1870 template <class ELFT> RelrSection<ELFT>::RelrSection() {
1871   this->entsize = config->wordsize;
1872 }
1873 
1874 template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
1875   // This function computes the contents of an SHT_RELR packed relocation
1876   // section.
1877   //
1878   // Proposal for adding SHT_RELR sections to generic-abi is here:
1879   //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
1880   //
1881   // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
1882   // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
1883   //
1884   // i.e. start with an address, followed by any number of bitmaps. The address
1885   // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
1886   // relocations each, at subsequent offsets following the last address entry.
1887   //
1888   // The bitmap entries must have 1 in the least significant bit. The assumption
1889   // here is that an address cannot have 1 in lsb. Odd addresses are not
1890   // supported.
1891   //
1892   // Excluding the least significant bit in the bitmap, each non-zero bit in
1893   // the bitmap represents a relocation to be applied to a corresponding machine
1894   // word that follows the base address word. The second least significant bit
1895   // represents the machine word immediately following the initial address, and
1896   // each bit that follows represents the next word, in linear order. As such,
1897   // a single bitmap can encode up to 31 relocations in a 32-bit object, and
1898   // 63 relocations in a 64-bit object.
1899   //
1900   // This encoding has a couple of interesting properties:
1901   // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
1902   //    even means address, odd means bitmap.
1903   // 2. Just a simple list of addresses is a valid encoding.
1904 
1905   size_t oldSize = relrRelocs.size();
1906   relrRelocs.clear();
1907 
1908   // Same as Config->Wordsize but faster because this is a compile-time
1909   // constant.
1910   const size_t wordsize = sizeof(typename ELFT::uint);
1911 
1912   // Number of bits to use for the relocation offsets bitmap.
1913   // Must be either 63 or 31.
1914   const size_t nBits = wordsize * 8 - 1;
1915 
1916   // Get offsets for all relative relocations and sort them.
1917   std::vector<uint64_t> offsets;
1918   for (const RelativeReloc &rel : relocs)
1919     offsets.push_back(rel.getOffset());
1920   llvm::sort(offsets);
1921 
1922   // For each leading relocation, find following ones that can be folded
1923   // as a bitmap and fold them.
1924   for (size_t i = 0, e = offsets.size(); i < e;) {
1925     // Add a leading relocation.
1926     relrRelocs.push_back(Elf_Relr(offsets[i]));
1927     uint64_t base = offsets[i] + wordsize;
1928     ++i;
1929 
1930     // Find foldable relocations to construct bitmaps.
1931     while (i < e) {
1932       uint64_t bitmap = 0;
1933 
1934       while (i < e) {
1935         uint64_t delta = offsets[i] - base;
1936 
1937         // If it is too far, it cannot be folded.
1938         if (delta >= nBits * wordsize)
1939           break;
1940 
1941         // If it is not a multiple of wordsize away, it cannot be folded.
1942         if (delta % wordsize)
1943           break;
1944 
1945         // Fold it.
1946         bitmap |= 1ULL << (delta / wordsize);
1947         ++i;
1948       }
1949 
1950       if (!bitmap)
1951         break;
1952 
1953       relrRelocs.push_back(Elf_Relr((bitmap << 1) | 1));
1954       base += nBits * wordsize;
1955     }
1956   }
1957 
1958   // Don't allow the section to shrink; otherwise the size of the section can
1959   // oscillate infinitely. Trailing 1s do not decode to more relocations.
1960   if (relrRelocs.size() < oldSize) {
1961     log(".relr.dyn needs " + Twine(oldSize - relrRelocs.size()) +
1962         " padding word(s)");
1963     relrRelocs.resize(oldSize, Elf_Relr(1));
1964   }
1965 
1966   return relrRelocs.size() != oldSize;
1967 }
1968 
1969 SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &strTabSec)
1970     : SyntheticSection(strTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
1971                        strTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1972                        config->wordsize,
1973                        strTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1974       strTabSec(strTabSec) {}
1975 
1976 // Orders symbols according to their positions in the GOT,
1977 // in compliance with MIPS ABI rules.
1978 // See "Global Offset Table" in Chapter 5 in the following document
1979 // for detailed description:
1980 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1981 static bool sortMipsSymbols(const SymbolTableEntry &l,
1982                             const SymbolTableEntry &r) {
1983   // Sort entries related to non-local preemptible symbols by GOT indexes.
1984   // All other entries go to the beginning of a dynsym in arbitrary order.
1985   if (l.sym->isInGot() && r.sym->isInGot())
1986     return l.sym->gotIndex < r.sym->gotIndex;
1987   if (!l.sym->isInGot() && !r.sym->isInGot())
1988     return false;
1989   return !l.sym->isInGot();
1990 }
1991 
1992 void SymbolTableBaseSection::finalizeContents() {
1993   if (OutputSection *sec = strTabSec.getParent())
1994     getParent()->link = sec->sectionIndex;
1995 
1996   if (this->type != SHT_DYNSYM) {
1997     sortSymTabSymbols();
1998     return;
1999   }
2000 
2001   // If it is a .dynsym, there should be no local symbols, but we need
2002   // to do a few things for the dynamic linker.
2003 
2004   // Section's Info field has the index of the first non-local symbol.
2005   // Because the first symbol entry is a null entry, 1 is the first.
2006   getParent()->info = 1;
2007 
2008   if (getPartition().gnuHashTab) {
2009     // NB: It also sorts Symbols to meet the GNU hash table requirements.
2010     getPartition().gnuHashTab->addSymbols(symbols);
2011   } else if (config->emachine == EM_MIPS) {
2012     llvm::stable_sort(symbols, sortMipsSymbols);
2013   }
2014 
2015   // Only the main partition's dynsym indexes are stored in the symbols
2016   // themselves. All other partitions use a lookup table.
2017   if (this == mainPart->dynSymTab) {
2018     size_t i = 0;
2019     for (const SymbolTableEntry &s : symbols)
2020       s.sym->dynsymIndex = ++i;
2021   }
2022 }
2023 
2024 // The ELF spec requires that all local symbols precede global symbols, so we
2025 // sort symbol entries in this function. (For .dynsym, we don't do that because
2026 // symbols for dynamic linking are inherently all globals.)
2027 //
2028 // Aside from above, we put local symbols in groups starting with the STT_FILE
2029 // symbol. That is convenient for purpose of identifying where are local symbols
2030 // coming from.
2031 void SymbolTableBaseSection::sortSymTabSymbols() {
2032   // Move all local symbols before global symbols.
2033   auto e = std::stable_partition(
2034       symbols.begin(), symbols.end(), [](const SymbolTableEntry &s) {
2035         return s.sym->isLocal() || s.sym->computeBinding() == STB_LOCAL;
2036       });
2037   size_t numLocals = e - symbols.begin();
2038   getParent()->info = numLocals + 1;
2039 
2040   // We want to group the local symbols by file. For that we rebuild the local
2041   // part of the symbols vector. We do not need to care about the STT_FILE
2042   // symbols, they are already naturally placed first in each group. That
2043   // happens because STT_FILE is always the first symbol in the object and hence
2044   // precede all other local symbols we add for a file.
2045   MapVector<InputFile *, std::vector<SymbolTableEntry>> arr;
2046   for (const SymbolTableEntry &s : llvm::make_range(symbols.begin(), e))
2047     arr[s.sym->file].push_back(s);
2048 
2049   auto i = symbols.begin();
2050   for (std::pair<InputFile *, std::vector<SymbolTableEntry>> &p : arr)
2051     for (SymbolTableEntry &entry : p.second)
2052       *i++ = entry;
2053 }
2054 
2055 void SymbolTableBaseSection::addSymbol(Symbol *b) {
2056   // Adding a local symbol to a .dynsym is a bug.
2057   assert(this->type != SHT_DYNSYM || !b->isLocal());
2058 
2059   bool hashIt = b->isLocal();
2060   symbols.push_back({b, strTabSec.addString(b->getName(), hashIt)});
2061 }
2062 
2063 size_t SymbolTableBaseSection::getSymbolIndex(Symbol *sym) {
2064   if (this == mainPart->dynSymTab)
2065     return sym->dynsymIndex;
2066 
2067   // Initializes symbol lookup tables lazily. This is used only for -r,
2068   // -emit-relocs and dynsyms in partitions other than the main one.
2069   llvm::call_once(onceFlag, [&] {
2070     symbolIndexMap.reserve(symbols.size());
2071     size_t i = 0;
2072     for (const SymbolTableEntry &e : symbols) {
2073       if (e.sym->type == STT_SECTION)
2074         sectionIndexMap[e.sym->getOutputSection()] = ++i;
2075       else
2076         symbolIndexMap[e.sym] = ++i;
2077     }
2078   });
2079 
2080   // Section symbols are mapped based on their output sections
2081   // to maintain their semantics.
2082   if (sym->type == STT_SECTION)
2083     return sectionIndexMap.lookup(sym->getOutputSection());
2084   return symbolIndexMap.lookup(sym);
2085 }
2086 
2087 template <class ELFT>
2088 SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &strTabSec)
2089     : SymbolTableBaseSection(strTabSec) {
2090   this->entsize = sizeof(Elf_Sym);
2091 }
2092 
2093 static BssSection *getCommonSec(Symbol *sym) {
2094   if (!config->defineCommon)
2095     if (auto *d = dyn_cast<Defined>(sym))
2096       return dyn_cast_or_null<BssSection>(d->section);
2097   return nullptr;
2098 }
2099 
2100 static uint32_t getSymSectionIndex(Symbol *sym) {
2101   if (getCommonSec(sym))
2102     return SHN_COMMON;
2103   if (!isa<Defined>(sym) || sym->needsPltAddr)
2104     return SHN_UNDEF;
2105   if (const OutputSection *os = sym->getOutputSection())
2106     return os->sectionIndex >= SHN_LORESERVE ? (uint32_t)SHN_XINDEX
2107                                              : os->sectionIndex;
2108   return SHN_ABS;
2109 }
2110 
2111 // Write the internal symbol table contents to the output symbol table.
2112 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
2113   // The first entry is a null entry as per the ELF spec.
2114   memset(buf, 0, sizeof(Elf_Sym));
2115   buf += sizeof(Elf_Sym);
2116 
2117   auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2118 
2119   for (SymbolTableEntry &ent : symbols) {
2120     Symbol *sym = ent.sym;
2121     bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition;
2122 
2123     // Set st_info and st_other.
2124     eSym->st_other = 0;
2125     if (sym->isLocal()) {
2126       eSym->setBindingAndType(STB_LOCAL, sym->type);
2127     } else {
2128       eSym->setBindingAndType(sym->computeBinding(), sym->type);
2129       eSym->setVisibility(sym->visibility);
2130     }
2131 
2132     // The 3 most significant bits of st_other are used by OpenPOWER ABI.
2133     // See getPPC64GlobalEntryToLocalEntryOffset() for more details.
2134     if (config->emachine == EM_PPC64)
2135       eSym->st_other |= sym->stOther & 0xe0;
2136 
2137     eSym->st_name = ent.strTabOffset;
2138     if (isDefinedHere)
2139       eSym->st_shndx = getSymSectionIndex(ent.sym);
2140     else
2141       eSym->st_shndx = 0;
2142 
2143     // Copy symbol size if it is a defined symbol. st_size is not significant
2144     // for undefined symbols, so whether copying it or not is up to us if that's
2145     // the case. We'll leave it as zero because by not setting a value, we can
2146     // get the exact same outputs for two sets of input files that differ only
2147     // in undefined symbol size in DSOs.
2148     if (eSym->st_shndx == SHN_UNDEF || !isDefinedHere)
2149       eSym->st_size = 0;
2150     else
2151       eSym->st_size = sym->getSize();
2152 
2153     // st_value is usually an address of a symbol, but that has a
2154     // special meaining for uninstantiated common symbols (this can
2155     // occur if -r is given).
2156     if (BssSection *commonSec = getCommonSec(ent.sym))
2157       eSym->st_value = commonSec->alignment;
2158     else if (isDefinedHere)
2159       eSym->st_value = sym->getVA();
2160     else
2161       eSym->st_value = 0;
2162 
2163     ++eSym;
2164   }
2165 
2166   // On MIPS we need to mark symbol which has a PLT entry and requires
2167   // pointer equality by STO_MIPS_PLT flag. That is necessary to help
2168   // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
2169   // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
2170   if (config->emachine == EM_MIPS) {
2171     auto *eSym = reinterpret_cast<Elf_Sym *>(buf);
2172 
2173     for (SymbolTableEntry &ent : symbols) {
2174       Symbol *sym = ent.sym;
2175       if (sym->isInPlt() && sym->needsPltAddr)
2176         eSym->st_other |= STO_MIPS_PLT;
2177       if (isMicroMips()) {
2178         // We already set the less-significant bit for symbols
2179         // marked by the `STO_MIPS_MICROMIPS` flag and for microMIPS PLT
2180         // records. That allows us to distinguish such symbols in
2181         // the `MIPS<ELFT>::relocateOne()` routine. Now we should
2182         // clear that bit for non-dynamic symbol table, so tools
2183         // like `objdump` will be able to deal with a correct
2184         // symbol position.
2185         if (sym->isDefined() &&
2186             ((sym->stOther & STO_MIPS_MICROMIPS) || sym->needsPltAddr)) {
2187           if (!strTabSec.isDynamic())
2188             eSym->st_value &= ~1;
2189           eSym->st_other |= STO_MIPS_MICROMIPS;
2190         }
2191       }
2192       if (config->relocatable)
2193         if (auto *d = dyn_cast<Defined>(sym))
2194           if (isMipsPIC<ELFT>(d))
2195             eSym->st_other |= STO_MIPS_PIC;
2196       ++eSym;
2197     }
2198   }
2199 }
2200 
2201 SymtabShndxSection::SymtabShndxSection()
2202     : SyntheticSection(0, SHT_SYMTAB_SHNDX, 4, ".symtab_shndx") {
2203   this->entsize = 4;
2204 }
2205 
2206 void SymtabShndxSection::writeTo(uint8_t *buf) {
2207   // We write an array of 32 bit values, where each value has 1:1 association
2208   // with an entry in .symtab. If the corresponding entry contains SHN_XINDEX,
2209   // we need to write actual index, otherwise, we must write SHN_UNDEF(0).
2210   buf += 4; // Ignore .symtab[0] entry.
2211   for (const SymbolTableEntry &entry : in.symTab->getSymbols()) {
2212     if (getSymSectionIndex(entry.sym) == SHN_XINDEX)
2213       write32(buf, entry.sym->getOutputSection()->sectionIndex);
2214     buf += 4;
2215   }
2216 }
2217 
2218 bool SymtabShndxSection::isNeeded() const {
2219   // SHT_SYMTAB can hold symbols with section indices values up to
2220   // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
2221   // section. Problem is that we reveal the final section indices a bit too
2222   // late, and we do not know them here. For simplicity, we just always create
2223   // a .symtab_shndx section when the amount of output sections is huge.
2224   size_t size = 0;
2225   for (BaseCommand *base : script->sectionCommands)
2226     if (isa<OutputSection>(base))
2227       ++size;
2228   return size >= SHN_LORESERVE;
2229 }
2230 
2231 void SymtabShndxSection::finalizeContents() {
2232   getParent()->link = in.symTab->getParent()->sectionIndex;
2233 }
2234 
2235 size_t SymtabShndxSection::getSize() const {
2236   return in.symTab->getNumSymbols() * 4;
2237 }
2238 
2239 // .hash and .gnu.hash sections contain on-disk hash tables that map
2240 // symbol names to their dynamic symbol table indices. Their purpose
2241 // is to help the dynamic linker resolve symbols quickly. If ELF files
2242 // don't have them, the dynamic linker has to do linear search on all
2243 // dynamic symbols, which makes programs slower. Therefore, a .hash
2244 // section is added to a DSO by default. A .gnu.hash is added if you
2245 // give the -hash-style=gnu or -hash-style=both option.
2246 //
2247 // The Unix semantics of resolving dynamic symbols is somewhat expensive.
2248 // Each ELF file has a list of DSOs that the ELF file depends on and a
2249 // list of dynamic symbols that need to be resolved from any of the
2250 // DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
2251 // where m is the number of DSOs and n is the number of dynamic
2252 // symbols. For modern large programs, both m and n are large.  So
2253 // making each step faster by using hash tables substiantially
2254 // improves time to load programs.
2255 //
2256 // (Note that this is not the only way to design the shared library.
2257 // For instance, the Windows DLL takes a different approach. On
2258 // Windows, each dynamic symbol has a name of DLL from which the symbol
2259 // has to be resolved. That makes the cost of symbol resolution O(n).
2260 // This disables some hacky techniques you can use on Unix such as
2261 // LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
2262 //
2263 // Due to historical reasons, we have two different hash tables, .hash
2264 // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
2265 // and better version of .hash. .hash is just an on-disk hash table, but
2266 // .gnu.hash has a bloom filter in addition to a hash table to skip
2267 // DSOs very quickly. If you are sure that your dynamic linker knows
2268 // about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a
2269 // safe bet is to specify -hash-style=both for backward compatibilty.
2270 GnuHashTableSection::GnuHashTableSection()
2271     : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, config->wordsize, ".gnu.hash") {
2272 }
2273 
2274 void GnuHashTableSection::finalizeContents() {
2275   if (OutputSection *sec = getPartition().dynSymTab->getParent())
2276     getParent()->link = sec->sectionIndex;
2277 
2278   // Computes bloom filter size in word size. We want to allocate 12
2279   // bits for each symbol. It must be a power of two.
2280   if (symbols.empty()) {
2281     maskWords = 1;
2282   } else {
2283     uint64_t numBits = symbols.size() * 12;
2284     maskWords = NextPowerOf2(numBits / (config->wordsize * 8));
2285   }
2286 
2287   size = 16;                            // Header
2288   size += config->wordsize * maskWords; // Bloom filter
2289   size += nBuckets * 4;                 // Hash buckets
2290   size += symbols.size() * 4;           // Hash values
2291 }
2292 
2293 void GnuHashTableSection::writeTo(uint8_t *buf) {
2294   // The output buffer is not guaranteed to be zero-cleared because we pre-
2295   // fill executable sections with trap instructions. This is a precaution
2296   // for that case, which happens only when -no-rosegment is given.
2297   memset(buf, 0, size);
2298 
2299   // Write a header.
2300   write32(buf, nBuckets);
2301   write32(buf + 4, getPartition().dynSymTab->getNumSymbols() - symbols.size());
2302   write32(buf + 8, maskWords);
2303   write32(buf + 12, Shift2);
2304   buf += 16;
2305 
2306   // Write a bloom filter and a hash table.
2307   writeBloomFilter(buf);
2308   buf += config->wordsize * maskWords;
2309   writeHashTable(buf);
2310 }
2311 
2312 // This function writes a 2-bit bloom filter. This bloom filter alone
2313 // usually filters out 80% or more of all symbol lookups [1].
2314 // The dynamic linker uses the hash table only when a symbol is not
2315 // filtered out by a bloom filter.
2316 //
2317 // [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2),
2318 //     p.9, https://www.akkadia.org/drepper/dsohowto.pdf
2319 void GnuHashTableSection::writeBloomFilter(uint8_t *buf) {
2320   unsigned c = config->is64 ? 64 : 32;
2321   for (const Entry &sym : symbols) {
2322     // When C = 64, we choose a word with bits [6:...] and set 1 to two bits in
2323     // the word using bits [0:5] and [26:31].
2324     size_t i = (sym.hash / c) & (maskWords - 1);
2325     uint64_t val = readUint(buf + i * config->wordsize);
2326     val |= uint64_t(1) << (sym.hash % c);
2327     val |= uint64_t(1) << ((sym.hash >> Shift2) % c);
2328     writeUint(buf + i * config->wordsize, val);
2329   }
2330 }
2331 
2332 void GnuHashTableSection::writeHashTable(uint8_t *buf) {
2333   uint32_t *buckets = reinterpret_cast<uint32_t *>(buf);
2334   uint32_t oldBucket = -1;
2335   uint32_t *values = buckets + nBuckets;
2336   for (auto i = symbols.begin(), e = symbols.end(); i != e; ++i) {
2337     // Write a hash value. It represents a sequence of chains that share the
2338     // same hash modulo value. The last element of each chain is terminated by
2339     // LSB 1.
2340     uint32_t hash = i->hash;
2341     bool isLastInChain = (i + 1) == e || i->bucketIdx != (i + 1)->bucketIdx;
2342     hash = isLastInChain ? hash | 1 : hash & ~1;
2343     write32(values++, hash);
2344 
2345     if (i->bucketIdx == oldBucket)
2346       continue;
2347     // Write a hash bucket. Hash buckets contain indices in the following hash
2348     // value table.
2349     write32(buckets + i->bucketIdx,
2350             getPartition().dynSymTab->getSymbolIndex(i->sym));
2351     oldBucket = i->bucketIdx;
2352   }
2353 }
2354 
2355 static uint32_t hashGnu(StringRef name) {
2356   uint32_t h = 5381;
2357   for (uint8_t c : name)
2358     h = (h << 5) + h + c;
2359   return h;
2360 }
2361 
2362 // Add symbols to this symbol hash table. Note that this function
2363 // destructively sort a given vector -- which is needed because
2364 // GNU-style hash table places some sorting requirements.
2365 void GnuHashTableSection::addSymbols(std::vector<SymbolTableEntry> &v) {
2366   // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
2367   // its type correctly.
2368   std::vector<SymbolTableEntry>::iterator mid =
2369       std::stable_partition(v.begin(), v.end(), [&](const SymbolTableEntry &s) {
2370         return !s.sym->isDefined() || s.sym->partition != partition;
2371       });
2372 
2373   // We chose load factor 4 for the on-disk hash table. For each hash
2374   // collision, the dynamic linker will compare a uint32_t hash value.
2375   // Since the integer comparison is quite fast, we believe we can
2376   // make the load factor even larger. 4 is just a conservative choice.
2377   //
2378   // Note that we don't want to create a zero-sized hash table because
2379   // Android loader as of 2018 doesn't like a .gnu.hash containing such
2380   // table. If that's the case, we create a hash table with one unused
2381   // dummy slot.
2382   nBuckets = std::max<size_t>((v.end() - mid) / 4, 1);
2383 
2384   if (mid == v.end())
2385     return;
2386 
2387   for (SymbolTableEntry &ent : llvm::make_range(mid, v.end())) {
2388     Symbol *b = ent.sym;
2389     uint32_t hash = hashGnu(b->getName());
2390     uint32_t bucketIdx = hash % nBuckets;
2391     symbols.push_back({b, ent.strTabOffset, hash, bucketIdx});
2392   }
2393 
2394   llvm::stable_sort(symbols, [](const Entry &l, const Entry &r) {
2395     return l.bucketIdx < r.bucketIdx;
2396   });
2397 
2398   v.erase(mid, v.end());
2399   for (const Entry &ent : symbols)
2400     v.push_back({ent.sym, ent.strTabOffset});
2401 }
2402 
2403 HashTableSection::HashTableSection()
2404     : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
2405   this->entsize = 4;
2406 }
2407 
2408 void HashTableSection::finalizeContents() {
2409   SymbolTableBaseSection *symTab = getPartition().dynSymTab;
2410 
2411   if (OutputSection *sec = symTab->getParent())
2412     getParent()->link = sec->sectionIndex;
2413 
2414   unsigned numEntries = 2;               // nbucket and nchain.
2415   numEntries += symTab->getNumSymbols(); // The chain entries.
2416 
2417   // Create as many buckets as there are symbols.
2418   numEntries += symTab->getNumSymbols();
2419   this->size = numEntries * 4;
2420 }
2421 
2422 void HashTableSection::writeTo(uint8_t *buf) {
2423   SymbolTableBaseSection *symTab = getPartition().dynSymTab;
2424 
2425   // See comment in GnuHashTableSection::writeTo.
2426   memset(buf, 0, size);
2427 
2428   unsigned numSymbols = symTab->getNumSymbols();
2429 
2430   uint32_t *p = reinterpret_cast<uint32_t *>(buf);
2431   write32(p++, numSymbols); // nbucket
2432   write32(p++, numSymbols); // nchain
2433 
2434   uint32_t *buckets = p;
2435   uint32_t *chains = p + numSymbols;
2436 
2437   for (const SymbolTableEntry &s : symTab->getSymbols()) {
2438     Symbol *sym = s.sym;
2439     StringRef name = sym->getName();
2440     unsigned i = sym->dynsymIndex;
2441     uint32_t hash = hashSysV(name) % numSymbols;
2442     chains[i] = buckets[hash];
2443     write32(buckets + hash, i);
2444   }
2445 }
2446 
2447 // On PowerPC64 the lazy symbol resolvers go into the `global linkage table`
2448 // in the .glink section, rather then the typical .plt section.
2449 PltSection::PltSection(bool isIplt)
2450     : SyntheticSection(
2451           SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16,
2452           (config->emachine == EM_PPC || config->emachine == EM_PPC64)
2453               ? ".glink"
2454               : ".plt"),
2455       headerSize(!isIplt || config->zRetpolineplt ? target->pltHeaderSize : 0),
2456       isIplt(isIplt) {
2457   // The PLT needs to be writable on SPARC as the dynamic linker will
2458   // modify the instructions in the PLT entries.
2459   if (config->emachine == EM_SPARCV9)
2460     this->flags |= SHF_WRITE;
2461 }
2462 
2463 void PltSection::writeTo(uint8_t *buf) {
2464   if (config->emachine == EM_PPC) {
2465     writePPC32GlinkSection(buf, entries.size());
2466     return;
2467   }
2468 
2469   // At beginning of PLT or retpoline IPLT, we have code to call the dynamic
2470   // linker to resolve dynsyms at runtime. Write such code.
2471   if (headerSize)
2472     target->writePltHeader(buf);
2473   size_t off = headerSize;
2474 
2475   RelocationBaseSection *relSec = isIplt ? in.relaIplt : in.relaPlt;
2476 
2477   // The IPlt is immediately after the Plt, account for this in relOff
2478   size_t pltOff = isIplt ? in.plt->getSize() : 0;
2479 
2480   for (size_t i = 0, e = entries.size(); i != e; ++i) {
2481     const Symbol *b = entries[i];
2482     unsigned relOff = relSec->entsize * i + pltOff;
2483     uint64_t got = b->getGotPltVA();
2484     uint64_t plt = this->getVA() + off;
2485     target->writePlt(buf + off, got, plt, b->pltIndex, relOff);
2486     off += target->pltEntrySize;
2487   }
2488 }
2489 
2490 template <class ELFT> void PltSection::addEntry(Symbol &sym) {
2491   sym.pltIndex = entries.size();
2492   entries.push_back(&sym);
2493 }
2494 
2495 size_t PltSection::getSize() const {
2496   return headerSize + entries.size() * target->pltEntrySize;
2497 }
2498 
2499 // Some architectures such as additional symbols in the PLT section. For
2500 // example ARM uses mapping symbols to aid disassembly
2501 void PltSection::addSymbols() {
2502   // The PLT may have symbols defined for the Header, the IPLT has no header
2503   if (!isIplt)
2504     target->addPltHeaderSymbols(*this);
2505 
2506   size_t off = headerSize;
2507   for (size_t i = 0; i < entries.size(); ++i) {
2508     target->addPltSymbols(*this, off);
2509     off += target->pltEntrySize;
2510   }
2511 }
2512 
2513 // The string hash function for .gdb_index.
2514 static uint32_t computeGdbHash(StringRef s) {
2515   uint32_t h = 0;
2516   for (uint8_t c : s)
2517     h = h * 67 + toLower(c) - 113;
2518   return h;
2519 }
2520 
2521 GdbIndexSection::GdbIndexSection()
2522     : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index") {}
2523 
2524 // Returns the desired size of an on-disk hash table for a .gdb_index section.
2525 // There's a tradeoff between size and collision rate. We aim 75% utilization.
2526 size_t GdbIndexSection::computeSymtabSize() const {
2527   return std::max<size_t>(NextPowerOf2(symbols.size() * 4 / 3), 1024);
2528 }
2529 
2530 // Compute the output section size.
2531 void GdbIndexSection::initOutputSize() {
2532   size = sizeof(GdbIndexHeader) + computeSymtabSize() * 8;
2533 
2534   for (GdbChunk &chunk : chunks)
2535     size += chunk.compilationUnits.size() * 16 + chunk.addressAreas.size() * 20;
2536 
2537   // Add the constant pool size if exists.
2538   if (!symbols.empty()) {
2539     GdbSymbol &sym = symbols.back();
2540     size += sym.nameOff + sym.name.size() + 1;
2541   }
2542 }
2543 
2544 static std::vector<InputSection *> getDebugInfoSections() {
2545   std::vector<InputSection *> ret;
2546   for (InputSectionBase *s : inputSections)
2547     if (InputSection *isec = dyn_cast<InputSection>(s))
2548       if (isec->name == ".debug_info")
2549         ret.push_back(isec);
2550   return ret;
2551 }
2552 
2553 static std::vector<GdbIndexSection::CuEntry> readCuList(DWARFContext &dwarf) {
2554   std::vector<GdbIndexSection::CuEntry> ret;
2555   for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units())
2556     ret.push_back({cu->getOffset(), cu->getLength() + 4});
2557   return ret;
2558 }
2559 
2560 static std::vector<GdbIndexSection::AddressEntry>
2561 readAddressAreas(DWARFContext &dwarf, InputSection *sec) {
2562   std::vector<GdbIndexSection::AddressEntry> ret;
2563 
2564   uint32_t cuIdx = 0;
2565   for (std::unique_ptr<DWARFUnit> &cu : dwarf.compile_units()) {
2566     if (Error e = cu->tryExtractDIEsIfNeeded(false)) {
2567       error(toString(sec) + ": " + toString(std::move(e)));
2568       return {};
2569     }
2570     Expected<DWARFAddressRangesVector> ranges = cu->collectAddressRanges();
2571     if (!ranges) {
2572       error(toString(sec) + ": " + toString(ranges.takeError()));
2573       return {};
2574     }
2575 
2576     ArrayRef<InputSectionBase *> sections = sec->file->getSections();
2577     for (DWARFAddressRange &r : *ranges) {
2578       if (r.SectionIndex == -1ULL)
2579         continue;
2580       InputSectionBase *s = sections[r.SectionIndex];
2581       if (!s || s == &InputSection::discarded || !s->isLive())
2582         continue;
2583       // Range list with zero size has no effect.
2584       if (r.LowPC == r.HighPC)
2585         continue;
2586       auto *isec = cast<InputSection>(s);
2587       uint64_t offset = isec->getOffsetInFile();
2588       ret.push_back({isec, r.LowPC - offset, r.HighPC - offset, cuIdx});
2589     }
2590     ++cuIdx;
2591   }
2592 
2593   return ret;
2594 }
2595 
2596 template <class ELFT>
2597 static std::vector<GdbIndexSection::NameAttrEntry>
2598 readPubNamesAndTypes(const LLDDwarfObj<ELFT> &obj,
2599                      const std::vector<GdbIndexSection::CuEntry> &cus) {
2600   const DWARFSection &pubNames = obj.getGnuPubnamesSection();
2601   const DWARFSection &pubTypes = obj.getGnuPubtypesSection();
2602 
2603   std::vector<GdbIndexSection::NameAttrEntry> ret;
2604   for (const DWARFSection *pub : {&pubNames, &pubTypes}) {
2605     DWARFDebugPubTable table(obj, *pub, config->isLE, true);
2606     for (const DWARFDebugPubTable::Set &set : table.getData()) {
2607       // The value written into the constant pool is kind << 24 | cuIndex. As we
2608       // don't know how many compilation units precede this object to compute
2609       // cuIndex, we compute (kind << 24 | cuIndexInThisObject) instead, and add
2610       // the number of preceding compilation units later.
2611       uint32_t i = llvm::partition_point(cus,
2612                                          [&](GdbIndexSection::CuEntry cu) {
2613                                            return cu.cuOffset < set.Offset;
2614                                          }) -
2615                    cus.begin();
2616       for (const DWARFDebugPubTable::Entry &ent : set.Entries)
2617         ret.push_back({{ent.Name, computeGdbHash(ent.Name)},
2618                        (ent.Descriptor.toBits() << 24) | i});
2619     }
2620   }
2621   return ret;
2622 }
2623 
2624 // Create a list of symbols from a given list of symbol names and types
2625 // by uniquifying them by name.
2626 static std::vector<GdbIndexSection::GdbSymbol>
2627 createSymbols(ArrayRef<std::vector<GdbIndexSection::NameAttrEntry>> nameAttrs,
2628               const std::vector<GdbIndexSection::GdbChunk> &chunks) {
2629   using GdbSymbol = GdbIndexSection::GdbSymbol;
2630   using NameAttrEntry = GdbIndexSection::NameAttrEntry;
2631 
2632   // For each chunk, compute the number of compilation units preceding it.
2633   uint32_t cuIdx = 0;
2634   std::vector<uint32_t> cuIdxs(chunks.size());
2635   for (uint32_t i = 0, e = chunks.size(); i != e; ++i) {
2636     cuIdxs[i] = cuIdx;
2637     cuIdx += chunks[i].compilationUnits.size();
2638   }
2639 
2640   // The number of symbols we will handle in this function is of the order
2641   // of millions for very large executables, so we use multi-threading to
2642   // speed it up.
2643   size_t numShards = 32;
2644   size_t concurrency = 1;
2645   if (threadsEnabled)
2646     concurrency =
2647         std::min<size_t>(PowerOf2Floor(hardware_concurrency()), numShards);
2648 
2649   // A sharded map to uniquify symbols by name.
2650   std::vector<DenseMap<CachedHashStringRef, size_t>> map(numShards);
2651   size_t shift = 32 - countTrailingZeros(numShards);
2652 
2653   // Instantiate GdbSymbols while uniqufying them by name.
2654   std::vector<std::vector<GdbSymbol>> symbols(numShards);
2655   parallelForEachN(0, concurrency, [&](size_t threadId) {
2656     uint32_t i = 0;
2657     for (ArrayRef<NameAttrEntry> entries : nameAttrs) {
2658       for (const NameAttrEntry &ent : entries) {
2659         size_t shardId = ent.name.hash() >> shift;
2660         if ((shardId & (concurrency - 1)) != threadId)
2661           continue;
2662 
2663         uint32_t v = ent.cuIndexAndAttrs + cuIdxs[i];
2664         size_t &idx = map[shardId][ent.name];
2665         if (idx) {
2666           symbols[shardId][idx - 1].cuVector.push_back(v);
2667           continue;
2668         }
2669 
2670         idx = symbols[shardId].size() + 1;
2671         symbols[shardId].push_back({ent.name, {v}, 0, 0});
2672       }
2673       ++i;
2674     }
2675   });
2676 
2677   size_t numSymbols = 0;
2678   for (ArrayRef<GdbSymbol> v : symbols)
2679     numSymbols += v.size();
2680 
2681   // The return type is a flattened vector, so we'll copy each vector
2682   // contents to Ret.
2683   std::vector<GdbSymbol> ret;
2684   ret.reserve(numSymbols);
2685   for (std::vector<GdbSymbol> &vec : symbols)
2686     for (GdbSymbol &sym : vec)
2687       ret.push_back(std::move(sym));
2688 
2689   // CU vectors and symbol names are adjacent in the output file.
2690   // We can compute their offsets in the output file now.
2691   size_t off = 0;
2692   for (GdbSymbol &sym : ret) {
2693     sym.cuVectorOff = off;
2694     off += (sym.cuVector.size() + 1) * 4;
2695   }
2696   for (GdbSymbol &sym : ret) {
2697     sym.nameOff = off;
2698     off += sym.name.size() + 1;
2699   }
2700 
2701   return ret;
2702 }
2703 
2704 // Returns a newly-created .gdb_index section.
2705 template <class ELFT> GdbIndexSection *GdbIndexSection::create() {
2706   std::vector<InputSection *> sections = getDebugInfoSections();
2707 
2708   // .debug_gnu_pub{names,types} are useless in executables.
2709   // They are present in input object files solely for creating
2710   // a .gdb_index. So we can remove them from the output.
2711   for (InputSectionBase *s : inputSections)
2712     if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes")
2713       s->markDead();
2714 
2715   std::vector<GdbChunk> chunks(sections.size());
2716   std::vector<std::vector<NameAttrEntry>> nameAttrs(sections.size());
2717 
2718   parallelForEachN(0, sections.size(), [&](size_t i) {
2719     ObjFile<ELFT> *file = sections[i]->getFile<ELFT>();
2720     DWARFContext dwarf(std::make_unique<LLDDwarfObj<ELFT>>(file));
2721 
2722     chunks[i].sec = sections[i];
2723     chunks[i].compilationUnits = readCuList(dwarf);
2724     chunks[i].addressAreas = readAddressAreas(dwarf, sections[i]);
2725     nameAttrs[i] = readPubNamesAndTypes<ELFT>(
2726         static_cast<const LLDDwarfObj<ELFT> &>(dwarf.getDWARFObj()),
2727         chunks[i].compilationUnits);
2728   });
2729 
2730   auto *ret = make<GdbIndexSection>();
2731   ret->chunks = std::move(chunks);
2732   ret->symbols = createSymbols(nameAttrs, ret->chunks);
2733   ret->initOutputSize();
2734   return ret;
2735 }
2736 
2737 void GdbIndexSection::writeTo(uint8_t *buf) {
2738   // Write the header.
2739   auto *hdr = reinterpret_cast<GdbIndexHeader *>(buf);
2740   uint8_t *start = buf;
2741   hdr->version = 7;
2742   buf += sizeof(*hdr);
2743 
2744   // Write the CU list.
2745   hdr->cuListOff = buf - start;
2746   for (GdbChunk &chunk : chunks) {
2747     for (CuEntry &cu : chunk.compilationUnits) {
2748       write64le(buf, chunk.sec->outSecOff + cu.cuOffset);
2749       write64le(buf + 8, cu.cuLength);
2750       buf += 16;
2751     }
2752   }
2753 
2754   // Write the address area.
2755   hdr->cuTypesOff = buf - start;
2756   hdr->addressAreaOff = buf - start;
2757   uint32_t cuOff = 0;
2758   for (GdbChunk &chunk : chunks) {
2759     for (AddressEntry &e : chunk.addressAreas) {
2760       uint64_t baseAddr = e.section->getVA(0);
2761       write64le(buf, baseAddr + e.lowAddress);
2762       write64le(buf + 8, baseAddr + e.highAddress);
2763       write32le(buf + 16, e.cuIndex + cuOff);
2764       buf += 20;
2765     }
2766     cuOff += chunk.compilationUnits.size();
2767   }
2768 
2769   // Write the on-disk open-addressing hash table containing symbols.
2770   hdr->symtabOff = buf - start;
2771   size_t symtabSize = computeSymtabSize();
2772   uint32_t mask = symtabSize - 1;
2773 
2774   for (GdbSymbol &sym : symbols) {
2775     uint32_t h = sym.name.hash();
2776     uint32_t i = h & mask;
2777     uint32_t step = ((h * 17) & mask) | 1;
2778 
2779     while (read32le(buf + i * 8))
2780       i = (i + step) & mask;
2781 
2782     write32le(buf + i * 8, sym.nameOff);
2783     write32le(buf + i * 8 + 4, sym.cuVectorOff);
2784   }
2785 
2786   buf += symtabSize * 8;
2787 
2788   // Write the string pool.
2789   hdr->constantPoolOff = buf - start;
2790   parallelForEach(symbols, [&](GdbSymbol &sym) {
2791     memcpy(buf + sym.nameOff, sym.name.data(), sym.name.size());
2792   });
2793 
2794   // Write the CU vectors.
2795   for (GdbSymbol &sym : symbols) {
2796     write32le(buf, sym.cuVector.size());
2797     buf += 4;
2798     for (uint32_t val : sym.cuVector) {
2799       write32le(buf, val);
2800       buf += 4;
2801     }
2802   }
2803 }
2804 
2805 bool GdbIndexSection::isNeeded() const { return !chunks.empty(); }
2806 
2807 EhFrameHeader::EhFrameHeader()
2808     : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".eh_frame_hdr") {}
2809 
2810 void EhFrameHeader::writeTo(uint8_t *buf) {
2811   // Unlike most sections, the EhFrameHeader section is written while writing
2812   // another section, namely EhFrameSection, which calls the write() function
2813   // below from its writeTo() function. This is necessary because the contents
2814   // of EhFrameHeader depend on the relocated contents of EhFrameSection and we
2815   // don't know which order the sections will be written in.
2816 }
2817 
2818 // .eh_frame_hdr contains a binary search table of pointers to FDEs.
2819 // Each entry of the search table consists of two values,
2820 // the starting PC from where FDEs covers, and the FDE's address.
2821 // It is sorted by PC.
2822 void EhFrameHeader::write() {
2823   uint8_t *buf = Out::bufferStart + getParent()->offset + outSecOff;
2824   using FdeData = EhFrameSection::FdeData;
2825 
2826   std::vector<FdeData> fdes = getPartition().ehFrame->getFdeData();
2827 
2828   buf[0] = 1;
2829   buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
2830   buf[2] = DW_EH_PE_udata4;
2831   buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
2832   write32(buf + 4,
2833           getPartition().ehFrame->getParent()->addr - this->getVA() - 4);
2834   write32(buf + 8, fdes.size());
2835   buf += 12;
2836 
2837   for (FdeData &fde : fdes) {
2838     write32(buf, fde.pcRel);
2839     write32(buf + 4, fde.fdeVARel);
2840     buf += 8;
2841   }
2842 }
2843 
2844 size_t EhFrameHeader::getSize() const {
2845   // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
2846   return 12 + getPartition().ehFrame->numFdes * 8;
2847 }
2848 
2849 bool EhFrameHeader::isNeeded() const {
2850   return isLive() && getPartition().ehFrame->isNeeded();
2851 }
2852 
2853 VersionDefinitionSection::VersionDefinitionSection()
2854     : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
2855                        ".gnu.version_d") {}
2856 
2857 StringRef VersionDefinitionSection::getFileDefName() {
2858   if (!getPartition().name.empty())
2859     return getPartition().name;
2860   if (!config->soName.empty())
2861     return config->soName;
2862   return config->outputFile;
2863 }
2864 
2865 void VersionDefinitionSection::finalizeContents() {
2866   fileDefNameOff = getPartition().dynStrTab->addString(getFileDefName());
2867   for (const VersionDefinition &v : namedVersionDefs())
2868     verDefNameOffs.push_back(getPartition().dynStrTab->addString(v.name));
2869 
2870   if (OutputSection *sec = getPartition().dynStrTab->getParent())
2871     getParent()->link = sec->sectionIndex;
2872 
2873   // sh_info should be set to the number of definitions. This fact is missed in
2874   // documentation, but confirmed by binutils community:
2875   // https://sourceware.org/ml/binutils/2014-11/msg00355.html
2876   getParent()->info = getVerDefNum();
2877 }
2878 
2879 void VersionDefinitionSection::writeOne(uint8_t *buf, uint32_t index,
2880                                         StringRef name, size_t nameOff) {
2881   uint16_t flags = index == 1 ? VER_FLG_BASE : 0;
2882 
2883   // Write a verdef.
2884   write16(buf, 1);                  // vd_version
2885   write16(buf + 2, flags);          // vd_flags
2886   write16(buf + 4, index);          // vd_ndx
2887   write16(buf + 6, 1);              // vd_cnt
2888   write32(buf + 8, hashSysV(name)); // vd_hash
2889   write32(buf + 12, 20);            // vd_aux
2890   write32(buf + 16, 28);            // vd_next
2891 
2892   // Write a veraux.
2893   write32(buf + 20, nameOff); // vda_name
2894   write32(buf + 24, 0);       // vda_next
2895 }
2896 
2897 void VersionDefinitionSection::writeTo(uint8_t *buf) {
2898   writeOne(buf, 1, getFileDefName(), fileDefNameOff);
2899 
2900   auto nameOffIt = verDefNameOffs.begin();
2901   for (const VersionDefinition &v : namedVersionDefs()) {
2902     buf += EntrySize;
2903     writeOne(buf, v.id, v.name, *nameOffIt++);
2904   }
2905 
2906   // Need to terminate the last version definition.
2907   write32(buf + 16, 0); // vd_next
2908 }
2909 
2910 size_t VersionDefinitionSection::getSize() const {
2911   return EntrySize * getVerDefNum();
2912 }
2913 
2914 // .gnu.version is a table where each entry is 2 byte long.
2915 VersionTableSection::VersionTableSection()
2916     : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t),
2917                        ".gnu.version") {
2918   this->entsize = 2;
2919 }
2920 
2921 void VersionTableSection::finalizeContents() {
2922   // At the moment of june 2016 GNU docs does not mention that sh_link field
2923   // should be set, but Sun docs do. Also readelf relies on this field.
2924   getParent()->link = getPartition().dynSymTab->getParent()->sectionIndex;
2925 }
2926 
2927 size_t VersionTableSection::getSize() const {
2928   return (getPartition().dynSymTab->getSymbols().size() + 1) * 2;
2929 }
2930 
2931 void VersionTableSection::writeTo(uint8_t *buf) {
2932   buf += 2;
2933   for (const SymbolTableEntry &s : getPartition().dynSymTab->getSymbols()) {
2934     write16(buf, s.sym->versionId);
2935     buf += 2;
2936   }
2937 }
2938 
2939 bool VersionTableSection::isNeeded() const {
2940   return getPartition().verDef || getPartition().verNeed->isNeeded();
2941 }
2942 
2943 void elf::addVerneed(Symbol *ss) {
2944   auto &file = cast<SharedFile>(*ss->file);
2945   if (ss->verdefIndex == VER_NDX_GLOBAL) {
2946     ss->versionId = VER_NDX_GLOBAL;
2947     return;
2948   }
2949 
2950   if (file.vernauxs.empty())
2951     file.vernauxs.resize(file.verdefs.size());
2952 
2953   // Select a version identifier for the vernaux data structure, if we haven't
2954   // already allocated one. The verdef identifiers cover the range
2955   // [1..getVerDefNum()]; this causes the vernaux identifiers to start from
2956   // getVerDefNum()+1.
2957   if (file.vernauxs[ss->verdefIndex] == 0)
2958     file.vernauxs[ss->verdefIndex] = ++SharedFile::vernauxNum + getVerDefNum();
2959 
2960   ss->versionId = file.vernauxs[ss->verdefIndex];
2961 }
2962 
2963 template <class ELFT>
2964 VersionNeedSection<ELFT>::VersionNeedSection()
2965     : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t),
2966                        ".gnu.version_r") {}
2967 
2968 template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {
2969   for (SharedFile *f : sharedFiles) {
2970     if (f->vernauxs.empty())
2971       continue;
2972     verneeds.emplace_back();
2973     Verneed &vn = verneeds.back();
2974     vn.nameStrTab = getPartition().dynStrTab->addString(f->soName);
2975     for (unsigned i = 0; i != f->vernauxs.size(); ++i) {
2976       if (f->vernauxs[i] == 0)
2977         continue;
2978       auto *verdef =
2979           reinterpret_cast<const typename ELFT::Verdef *>(f->verdefs[i]);
2980       vn.vernauxs.push_back(
2981           {verdef->vd_hash, f->vernauxs[i],
2982            getPartition().dynStrTab->addString(f->getStringTable().data() +
2983                                                verdef->getAux()->vda_name)});
2984     }
2985   }
2986 
2987   if (OutputSection *sec = getPartition().dynStrTab->getParent())
2988     getParent()->link = sec->sectionIndex;
2989   getParent()->info = verneeds.size();
2990 }
2991 
2992 template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *buf) {
2993   // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
2994   auto *verneed = reinterpret_cast<Elf_Verneed *>(buf);
2995   auto *vernaux = reinterpret_cast<Elf_Vernaux *>(verneed + verneeds.size());
2996 
2997   for (auto &vn : verneeds) {
2998     // Create an Elf_Verneed for this DSO.
2999     verneed->vn_version = 1;
3000     verneed->vn_cnt = vn.vernauxs.size();
3001     verneed->vn_file = vn.nameStrTab;
3002     verneed->vn_aux =
3003         reinterpret_cast<char *>(vernaux) - reinterpret_cast<char *>(verneed);
3004     verneed->vn_next = sizeof(Elf_Verneed);
3005     ++verneed;
3006 
3007     // Create the Elf_Vernauxs for this Elf_Verneed.
3008     for (auto &vna : vn.vernauxs) {
3009       vernaux->vna_hash = vna.hash;
3010       vernaux->vna_flags = 0;
3011       vernaux->vna_other = vna.verneedIndex;
3012       vernaux->vna_name = vna.nameStrTab;
3013       vernaux->vna_next = sizeof(Elf_Vernaux);
3014       ++vernaux;
3015     }
3016 
3017     vernaux[-1].vna_next = 0;
3018   }
3019   verneed[-1].vn_next = 0;
3020 }
3021 
3022 template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
3023   return verneeds.size() * sizeof(Elf_Verneed) +
3024          SharedFile::vernauxNum * sizeof(Elf_Vernaux);
3025 }
3026 
3027 template <class ELFT> bool VersionNeedSection<ELFT>::isNeeded() const {
3028   return SharedFile::vernauxNum != 0;
3029 }
3030 
3031 void MergeSyntheticSection::addSection(MergeInputSection *ms) {
3032   ms->parent = this;
3033   sections.push_back(ms);
3034   assert(alignment == ms->alignment || !(ms->flags & SHF_STRINGS));
3035   alignment = std::max(alignment, ms->alignment);
3036 }
3037 
3038 MergeTailSection::MergeTailSection(StringRef name, uint32_t type,
3039                                    uint64_t flags, uint32_t alignment)
3040     : MergeSyntheticSection(name, type, flags, alignment),
3041       builder(StringTableBuilder::RAW, alignment) {}
3042 
3043 size_t MergeTailSection::getSize() const { return builder.getSize(); }
3044 
3045 void MergeTailSection::writeTo(uint8_t *buf) { builder.write(buf); }
3046 
3047 void MergeTailSection::finalizeContents() {
3048   // Add all string pieces to the string table builder to create section
3049   // contents.
3050   for (MergeInputSection *sec : sections)
3051     for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3052       if (sec->pieces[i].live)
3053         builder.add(sec->getData(i));
3054 
3055   // Fix the string table content. After this, the contents will never change.
3056   builder.finalize();
3057 
3058   // finalize() fixed tail-optimized strings, so we can now get
3059   // offsets of strings. Get an offset for each string and save it
3060   // to a corresponding SectionPiece for easy access.
3061   for (MergeInputSection *sec : sections)
3062     for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3063       if (sec->pieces[i].live)
3064         sec->pieces[i].outputOff = builder.getOffset(sec->getData(i));
3065 }
3066 
3067 void MergeNoTailSection::writeTo(uint8_t *buf) {
3068   for (size_t i = 0; i < numShards; ++i)
3069     shards[i].write(buf + shardOffsets[i]);
3070 }
3071 
3072 // This function is very hot (i.e. it can take several seconds to finish)
3073 // because sometimes the number of inputs is in an order of magnitude of
3074 // millions. So, we use multi-threading.
3075 //
3076 // For any strings S and T, we know S is not mergeable with T if S's hash
3077 // value is different from T's. If that's the case, we can safely put S and
3078 // T into different string builders without worrying about merge misses.
3079 // We do it in parallel.
3080 void MergeNoTailSection::finalizeContents() {
3081   // Initializes string table builders.
3082   for (size_t i = 0; i < numShards; ++i)
3083     shards.emplace_back(StringTableBuilder::RAW, alignment);
3084 
3085   // Concurrency level. Must be a power of 2 to avoid expensive modulo
3086   // operations in the following tight loop.
3087   size_t concurrency = 1;
3088   if (threadsEnabled)
3089     concurrency =
3090         std::min<size_t>(PowerOf2Floor(hardware_concurrency()), numShards);
3091 
3092   // Add section pieces to the builders.
3093   parallelForEachN(0, concurrency, [&](size_t threadId) {
3094     for (MergeInputSection *sec : sections) {
3095       for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) {
3096         if (!sec->pieces[i].live)
3097           continue;
3098         size_t shardId = getShardId(sec->pieces[i].hash);
3099         if ((shardId & (concurrency - 1)) == threadId)
3100           sec->pieces[i].outputOff = shards[shardId].add(sec->getData(i));
3101       }
3102     }
3103   });
3104 
3105   // Compute an in-section offset for each shard.
3106   size_t off = 0;
3107   for (size_t i = 0; i < numShards; ++i) {
3108     shards[i].finalizeInOrder();
3109     if (shards[i].getSize() > 0)
3110       off = alignTo(off, alignment);
3111     shardOffsets[i] = off;
3112     off += shards[i].getSize();
3113   }
3114   size = off;
3115 
3116   // So far, section pieces have offsets from beginning of shards, but
3117   // we want offsets from beginning of the whole section. Fix them.
3118   parallelForEach(sections, [&](MergeInputSection *sec) {
3119     for (size_t i = 0, e = sec->pieces.size(); i != e; ++i)
3120       if (sec->pieces[i].live)
3121         sec->pieces[i].outputOff +=
3122             shardOffsets[getShardId(sec->pieces[i].hash)];
3123   });
3124 }
3125 
3126 MergeSyntheticSection *elf::createMergeSynthetic(StringRef name, uint32_t type,
3127                                                  uint64_t flags,
3128                                                  uint32_t alignment) {
3129   bool shouldTailMerge = (flags & SHF_STRINGS) && config->optimize >= 2;
3130   if (shouldTailMerge)
3131     return make<MergeTailSection>(name, type, flags, alignment);
3132   return make<MergeNoTailSection>(name, type, flags, alignment);
3133 }
3134 
3135 template <class ELFT> void elf::splitSections() {
3136   // splitIntoPieces needs to be called on each MergeInputSection
3137   // before calling finalizeContents().
3138   parallelForEach(inputSections, [](InputSectionBase *sec) {
3139     if (auto *s = dyn_cast<MergeInputSection>(sec))
3140       s->splitIntoPieces();
3141     else if (auto *eh = dyn_cast<EhInputSection>(sec))
3142       eh->split<ELFT>();
3143   });
3144 }
3145 
3146 MipsRldMapSection::MipsRldMapSection()
3147     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize,
3148                        ".rld_map") {}
3149 
3150 ARMExidxSyntheticSection::ARMExidxSyntheticSection()
3151     : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX,
3152                        config->wordsize, ".ARM.exidx") {}
3153 
3154 static InputSection *findExidxSection(InputSection *isec) {
3155   for (InputSection *d : isec->dependentSections)
3156     if (d->type == SHT_ARM_EXIDX)
3157       return d;
3158   return nullptr;
3159 }
3160 
3161 static bool isValidExidxSectionDep(InputSection *isec) {
3162   return (isec->flags & SHF_ALLOC) && (isec->flags & SHF_EXECINSTR) &&
3163          isec->getSize() > 0;
3164 }
3165 
3166 bool ARMExidxSyntheticSection::addSection(InputSection *isec) {
3167   if (isec->type == SHT_ARM_EXIDX) {
3168     if (InputSection* dep = isec->getLinkOrderDep())
3169       if (isValidExidxSectionDep(dep)) {
3170         exidxSections.push_back(isec);
3171         return true;
3172       }
3173     return false;
3174   }
3175 
3176   if (isValidExidxSectionDep(isec)) {
3177     executableSections.push_back(isec);
3178     return false;
3179   }
3180 
3181   // FIXME: we do not output a relocation section when --emit-relocs is used
3182   // as we do not have relocation sections for linker generated table entries
3183   // and we would have to erase at a late stage relocations from merged entries.
3184   // Given that exception tables are already position independent and a binary
3185   // analyzer could derive the relocations we choose to erase the relocations.
3186   if (config->emitRelocs && isec->type == SHT_REL)
3187     if (InputSectionBase *ex = isec->getRelocatedSection())
3188       if (isa<InputSection>(ex) && ex->type == SHT_ARM_EXIDX)
3189         return true;
3190 
3191   return false;
3192 }
3193 
3194 // References to .ARM.Extab Sections have bit 31 clear and are not the
3195 // special EXIDX_CANTUNWIND bit-pattern.
3196 static bool isExtabRef(uint32_t unwind) {
3197   return (unwind & 0x80000000) == 0 && unwind != 0x1;
3198 }
3199 
3200 // Return true if the .ARM.exidx section Cur can be merged into the .ARM.exidx
3201 // section Prev, where Cur follows Prev in the table. This can be done if the
3202 // unwinding instructions in Cur are identical to Prev. Linker generated
3203 // EXIDX_CANTUNWIND entries are represented by nullptr as they do not have an
3204 // InputSection.
3205 static bool isDuplicateArmExidxSec(InputSection *prev, InputSection *cur) {
3206 
3207   struct ExidxEntry {
3208     ulittle32_t fn;
3209     ulittle32_t unwind;
3210   };
3211   // Get the last table Entry from the previous .ARM.exidx section. If Prev is
3212   // nullptr then it will be a synthesized EXIDX_CANTUNWIND entry.
3213   ExidxEntry prevEntry = {ulittle32_t(0), ulittle32_t(1)};
3214   if (prev)
3215     prevEntry = prev->getDataAs<ExidxEntry>().back();
3216   if (isExtabRef(prevEntry.unwind))
3217     return false;
3218 
3219   // We consider the unwind instructions of an .ARM.exidx table entry
3220   // a duplicate if the previous unwind instructions if:
3221   // - Both are the special EXIDX_CANTUNWIND.
3222   // - Both are the same inline unwind instructions.
3223   // We do not attempt to follow and check links into .ARM.extab tables as
3224   // consecutive identical entries are rare and the effort to check that they
3225   // are identical is high.
3226 
3227   // If Cur is nullptr then this is synthesized EXIDX_CANTUNWIND entry.
3228   if (cur == nullptr)
3229     return prevEntry.unwind == 1;
3230 
3231   for (const ExidxEntry entry : cur->getDataAs<ExidxEntry>())
3232     if (isExtabRef(entry.unwind) || entry.unwind != prevEntry.unwind)
3233       return false;
3234 
3235   // All table entries in this .ARM.exidx Section can be merged into the
3236   // previous Section.
3237   return true;
3238 }
3239 
3240 // The .ARM.exidx table must be sorted in ascending order of the address of the
3241 // functions the table describes. Optionally duplicate adjacent table entries
3242 // can be removed. At the end of the function the executableSections must be
3243 // sorted in ascending order of address, Sentinel is set to the InputSection
3244 // with the highest address and any InputSections that have mergeable
3245 // .ARM.exidx table entries are removed from it.
3246 void ARMExidxSyntheticSection::finalizeContents() {
3247   // The executableSections and exidxSections that we use to derive the final
3248   // contents of this SyntheticSection are populated before
3249   // processSectionCommands() and ICF. A /DISCARD/ entry in SECTIONS command or
3250   // ICF may remove executable InputSections and their dependent .ARM.exidx
3251   // section that we recorded earlier.
3252   auto isDiscarded = [](const InputSection *isec) { return !isec->isLive(); };
3253   llvm::erase_if(executableSections, isDiscarded);
3254   llvm::erase_if(exidxSections, isDiscarded);
3255 
3256   // Sort the executable sections that may or may not have associated
3257   // .ARM.exidx sections by order of ascending address. This requires the
3258   // relative positions of InputSections to be known.
3259   auto compareByFilePosition = [](const InputSection *a,
3260                                   const InputSection *b) {
3261     OutputSection *aOut = a->getParent();
3262     OutputSection *bOut = b->getParent();
3263 
3264     if (aOut != bOut)
3265       return aOut->sectionIndex < bOut->sectionIndex;
3266     return a->outSecOff < b->outSecOff;
3267   };
3268   llvm::stable_sort(executableSections, compareByFilePosition);
3269   sentinel = executableSections.back();
3270   // Optionally merge adjacent duplicate entries.
3271   if (config->mergeArmExidx) {
3272     std::vector<InputSection *> selectedSections;
3273     selectedSections.reserve(executableSections.size());
3274     selectedSections.push_back(executableSections[0]);
3275     size_t prev = 0;
3276     for (size_t i = 1; i < executableSections.size(); ++i) {
3277       InputSection *ex1 = findExidxSection(executableSections[prev]);
3278       InputSection *ex2 = findExidxSection(executableSections[i]);
3279       if (!isDuplicateArmExidxSec(ex1, ex2)) {
3280         selectedSections.push_back(executableSections[i]);
3281         prev = i;
3282       }
3283     }
3284     executableSections = std::move(selectedSections);
3285   }
3286 
3287   size_t offset = 0;
3288   size = 0;
3289   for (InputSection *isec : executableSections) {
3290     if (InputSection *d = findExidxSection(isec)) {
3291       d->outSecOff = offset;
3292       d->parent = getParent();
3293       offset += d->getSize();
3294     } else {
3295       offset += 8;
3296     }
3297   }
3298   // Size includes Sentinel.
3299   size = offset + 8;
3300 }
3301 
3302 InputSection *ARMExidxSyntheticSection::getLinkOrderDep() const {
3303   return executableSections.front();
3304 }
3305 
3306 // To write the .ARM.exidx table from the ExecutableSections we have three cases
3307 // 1.) The InputSection has a .ARM.exidx InputSection in its dependent sections.
3308 //     We write the .ARM.exidx section contents and apply its relocations.
3309 // 2.) The InputSection does not have a dependent .ARM.exidx InputSection. We
3310 //     must write the contents of an EXIDX_CANTUNWIND directly. We use the
3311 //     start of the InputSection as the purpose of the linker generated
3312 //     section is to terminate the address range of the previous entry.
3313 // 3.) A trailing EXIDX_CANTUNWIND sentinel section is required at the end of
3314 //     the table to terminate the address range of the final entry.
3315 void ARMExidxSyntheticSection::writeTo(uint8_t *buf) {
3316 
3317   const uint8_t cantUnwindData[8] = {0, 0, 0, 0,  // PREL31 to target
3318                                      1, 0, 0, 0}; // EXIDX_CANTUNWIND
3319 
3320   uint64_t offset = 0;
3321   for (InputSection *isec : executableSections) {
3322     assert(isec->getParent() != nullptr);
3323     if (InputSection *d = findExidxSection(isec)) {
3324       memcpy(buf + offset, d->data().data(), d->data().size());
3325       d->relocateAlloc(buf, buf + d->getSize());
3326       offset += d->getSize();
3327     } else {
3328       // A Linker generated CANTUNWIND section.
3329       memcpy(buf + offset, cantUnwindData, sizeof(cantUnwindData));
3330       uint64_t s = isec->getVA();
3331       uint64_t p = getVA() + offset;
3332       target->relocateOne(buf + offset, R_ARM_PREL31, s - p);
3333       offset += 8;
3334     }
3335   }
3336   // Write Sentinel.
3337   memcpy(buf + offset, cantUnwindData, sizeof(cantUnwindData));
3338   uint64_t s = sentinel->getVA(sentinel->getSize());
3339   uint64_t p = getVA() + offset;
3340   target->relocateOne(buf + offset, R_ARM_PREL31, s - p);
3341   assert(size == offset + 8);
3342 }
3343 
3344 bool ARMExidxSyntheticSection::isNeeded() const {
3345   return llvm::find_if(exidxSections, [](InputSection *isec) {
3346            return isec->isLive();
3347          }) != exidxSections.end();
3348 }
3349 
3350 bool ARMExidxSyntheticSection::classof(const SectionBase *d) {
3351   return d->kind() == InputSectionBase::Synthetic && d->type == SHT_ARM_EXIDX;
3352 }
3353 
3354 ThunkSection::ThunkSection(OutputSection *os, uint64_t off)
3355     : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS,
3356                        config->wordsize, ".text.thunk") {
3357   this->parent = os;
3358   this->outSecOff = off;
3359 }
3360 
3361 void ThunkSection::addThunk(Thunk *t) {
3362   thunks.push_back(t);
3363   t->addSymbols(*this);
3364 }
3365 
3366 void ThunkSection::writeTo(uint8_t *buf) {
3367   for (Thunk *t : thunks)
3368     t->writeTo(buf + t->offset);
3369 }
3370 
3371 InputSection *ThunkSection::getTargetInputSection() const {
3372   if (thunks.empty())
3373     return nullptr;
3374   const Thunk *t = thunks.front();
3375   return t->getTargetInputSection();
3376 }
3377 
3378 bool ThunkSection::assignOffsets() {
3379   uint64_t off = 0;
3380   for (Thunk *t : thunks) {
3381     off = alignTo(off, t->alignment);
3382     t->setOffset(off);
3383     uint32_t size = t->size();
3384     t->getThunkTargetSym()->size = size;
3385     off += size;
3386   }
3387   bool changed = off != size;
3388   size = off;
3389   return changed;
3390 }
3391 
3392 PPC32Got2Section::PPC32Got2Section()
3393     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 4, ".got2") {}
3394 
3395 bool PPC32Got2Section::isNeeded() const {
3396   // See the comment below. This is not needed if there is no other
3397   // InputSection.
3398   for (BaseCommand *base : getParent()->sectionCommands)
3399     if (auto *isd = dyn_cast<InputSectionDescription>(base))
3400       for (InputSection *isec : isd->sections)
3401         if (isec != this)
3402           return true;
3403   return false;
3404 }
3405 
3406 void PPC32Got2Section::finalizeContents() {
3407   // PPC32 may create multiple GOT sections for -fPIC/-fPIE, one per file in
3408   // .got2 . This function computes outSecOff of each .got2 to be used in
3409   // PPC32PltCallStub::writeTo(). The purpose of this empty synthetic section is
3410   // to collect input sections named ".got2".
3411   uint32_t offset = 0;
3412   for (BaseCommand *base : getParent()->sectionCommands)
3413     if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
3414       for (InputSection *isec : isd->sections) {
3415         if (isec == this)
3416           continue;
3417         isec->file->ppc32Got2OutSecOff = offset;
3418         offset += (uint32_t)isec->getSize();
3419       }
3420     }
3421 }
3422 
3423 // If linking position-dependent code then the table will store the addresses
3424 // directly in the binary so the section has type SHT_PROGBITS. If linking
3425 // position-independent code the section has type SHT_NOBITS since it will be
3426 // allocated and filled in by the dynamic linker.
3427 PPC64LongBranchTargetSection::PPC64LongBranchTargetSection()
3428     : SyntheticSection(SHF_ALLOC | SHF_WRITE,
3429                        config->isPic ? SHT_NOBITS : SHT_PROGBITS, 8,
3430                        ".branch_lt") {}
3431 
3432 void PPC64LongBranchTargetSection::addEntry(Symbol &sym) {
3433   assert(sym.ppc64BranchltIndex == 0xffff);
3434   sym.ppc64BranchltIndex = entries.size();
3435   entries.push_back(&sym);
3436 }
3437 
3438 size_t PPC64LongBranchTargetSection::getSize() const {
3439   return entries.size() * 8;
3440 }
3441 
3442 void PPC64LongBranchTargetSection::writeTo(uint8_t *buf) {
3443   // If linking non-pic we have the final addresses of the targets and they get
3444   // written to the table directly. For pic the dynamic linker will allocate
3445   // the section and fill it it.
3446   if (config->isPic)
3447     return;
3448 
3449   for (const Symbol *sym : entries) {
3450     assert(sym->getVA());
3451     // Need calls to branch to the local entry-point since a long-branch
3452     // must be a local-call.
3453     write64(buf,
3454             sym->getVA() + getPPC64GlobalEntryToLocalEntryOffset(sym->stOther));
3455     buf += 8;
3456   }
3457 }
3458 
3459 bool PPC64LongBranchTargetSection::isNeeded() const {
3460   // `removeUnusedSyntheticSections()` is called before thunk allocation which
3461   // is too early to determine if this section will be empty or not. We need
3462   // Finalized to keep the section alive until after thunk creation. Finalized
3463   // only gets set to true once `finalizeSections()` is called after thunk
3464   // creation. Becuase of this, if we don't create any long-branch thunks we end
3465   // up with an empty .branch_lt section in the binary.
3466   return !finalized || !entries.empty();
3467 }
3468 
3469 static uint8_t getAbiVersion() {
3470   // MIPS non-PIC executable gets ABI version 1.
3471   if (config->emachine == EM_MIPS) {
3472     if (!config->isPic && !config->relocatable &&
3473         (config->eflags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
3474       return 1;
3475     return 0;
3476   }
3477 
3478   if (config->emachine == EM_AMDGPU) {
3479     uint8_t ver = objectFiles[0]->abiVersion;
3480     for (InputFile *file : makeArrayRef(objectFiles).slice(1))
3481       if (file->abiVersion != ver)
3482         error("incompatible ABI version: " + toString(file));
3483     return ver;
3484   }
3485 
3486   return 0;
3487 }
3488 
3489 template <typename ELFT> void elf::writeEhdr(uint8_t *buf, Partition &part) {
3490   // For executable segments, the trap instructions are written before writing
3491   // the header. Setting Elf header bytes to zero ensures that any unused bytes
3492   // in header are zero-cleared, instead of having trap instructions.
3493   memset(buf, 0, sizeof(typename ELFT::Ehdr));
3494   memcpy(buf, "\177ELF", 4);
3495 
3496   auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
3497   eHdr->e_ident[EI_CLASS] = config->is64 ? ELFCLASS64 : ELFCLASS32;
3498   eHdr->e_ident[EI_DATA] = config->isLE ? ELFDATA2LSB : ELFDATA2MSB;
3499   eHdr->e_ident[EI_VERSION] = EV_CURRENT;
3500   eHdr->e_ident[EI_OSABI] = config->osabi;
3501   eHdr->e_ident[EI_ABIVERSION] = getAbiVersion();
3502   eHdr->e_machine = config->emachine;
3503   eHdr->e_version = EV_CURRENT;
3504   eHdr->e_flags = config->eflags;
3505   eHdr->e_ehsize = sizeof(typename ELFT::Ehdr);
3506   eHdr->e_phnum = part.phdrs.size();
3507   eHdr->e_shentsize = sizeof(typename ELFT::Shdr);
3508 
3509   if (!config->relocatable) {
3510     eHdr->e_phoff = sizeof(typename ELFT::Ehdr);
3511     eHdr->e_phentsize = sizeof(typename ELFT::Phdr);
3512   }
3513 }
3514 
3515 template <typename ELFT> void elf::writePhdrs(uint8_t *buf, Partition &part) {
3516   // Write the program header table.
3517   auto *hBuf = reinterpret_cast<typename ELFT::Phdr *>(buf);
3518   for (PhdrEntry *p : part.phdrs) {
3519     hBuf->p_type = p->p_type;
3520     hBuf->p_flags = p->p_flags;
3521     hBuf->p_offset = p->p_offset;
3522     hBuf->p_vaddr = p->p_vaddr;
3523     hBuf->p_paddr = p->p_paddr;
3524     hBuf->p_filesz = p->p_filesz;
3525     hBuf->p_memsz = p->p_memsz;
3526     hBuf->p_align = p->p_align;
3527     ++hBuf;
3528   }
3529 }
3530 
3531 template <typename ELFT>
3532 PartitionElfHeaderSection<ELFT>::PartitionElfHeaderSection()
3533     : SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_EHDR, 1, "") {}
3534 
3535 template <typename ELFT>
3536 size_t PartitionElfHeaderSection<ELFT>::getSize() const {
3537   return sizeof(typename ELFT::Ehdr);
3538 }
3539 
3540 template <typename ELFT>
3541 void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) {
3542   writeEhdr<ELFT>(buf, getPartition());
3543 
3544   // Loadable partitions are always ET_DYN.
3545   auto *eHdr = reinterpret_cast<typename ELFT::Ehdr *>(buf);
3546   eHdr->e_type = ET_DYN;
3547 }
3548 
3549 template <typename ELFT>
3550 PartitionProgramHeadersSection<ELFT>::PartitionProgramHeadersSection()
3551     : SyntheticSection(SHF_ALLOC, SHT_LLVM_PART_PHDR, 1, ".phdrs") {}
3552 
3553 template <typename ELFT>
3554 size_t PartitionProgramHeadersSection<ELFT>::getSize() const {
3555   return sizeof(typename ELFT::Phdr) * getPartition().phdrs.size();
3556 }
3557 
3558 template <typename ELFT>
3559 void PartitionProgramHeadersSection<ELFT>::writeTo(uint8_t *buf) {
3560   writePhdrs<ELFT>(buf, getPartition());
3561 }
3562 
3563 PartitionIndexSection::PartitionIndexSection()
3564     : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".rodata") {}
3565 
3566 size_t PartitionIndexSection::getSize() const {
3567   return 12 * (partitions.size() - 1);
3568 }
3569 
3570 void PartitionIndexSection::finalizeContents() {
3571   for (size_t i = 1; i != partitions.size(); ++i)
3572     partitions[i].nameStrTab = mainPart->dynStrTab->addString(partitions[i].name);
3573 }
3574 
3575 void PartitionIndexSection::writeTo(uint8_t *buf) {
3576   uint64_t va = getVA();
3577   for (size_t i = 1; i != partitions.size(); ++i) {
3578     write32(buf, mainPart->dynStrTab->getVA() + partitions[i].nameStrTab - va);
3579     write32(buf + 4, partitions[i].elfHeader->getVA() - (va + 4));
3580 
3581     SyntheticSection *next =
3582         i == partitions.size() - 1 ? in.partEnd : partitions[i + 1].elfHeader;
3583     write32(buf + 8, next->getVA() - partitions[i].elfHeader->getVA());
3584 
3585     va += 12;
3586     buf += 12;
3587   }
3588 }
3589 
3590 InStruct elf::in;
3591 
3592 std::vector<Partition> elf::partitions;
3593 Partition *elf::mainPart;
3594 
3595 template GdbIndexSection *GdbIndexSection::create<ELF32LE>();
3596 template GdbIndexSection *GdbIndexSection::create<ELF32BE>();
3597 template GdbIndexSection *GdbIndexSection::create<ELF64LE>();
3598 template GdbIndexSection *GdbIndexSection::create<ELF64BE>();
3599 
3600 template void elf::splitSections<ELF32LE>();
3601 template void elf::splitSections<ELF32BE>();
3602 template void elf::splitSections<ELF64LE>();
3603 template void elf::splitSections<ELF64BE>();
3604 
3605 template void PltSection::addEntry<ELF32LE>(Symbol &Sym);
3606 template void PltSection::addEntry<ELF32BE>(Symbol &Sym);
3607 template void PltSection::addEntry<ELF64LE>(Symbol &Sym);
3608 template void PltSection::addEntry<ELF64BE>(Symbol &Sym);
3609 
3610 template class elf::MipsAbiFlagsSection<ELF32LE>;
3611 template class elf::MipsAbiFlagsSection<ELF32BE>;
3612 template class elf::MipsAbiFlagsSection<ELF64LE>;
3613 template class elf::MipsAbiFlagsSection<ELF64BE>;
3614 
3615 template class elf::MipsOptionsSection<ELF32LE>;
3616 template class elf::MipsOptionsSection<ELF32BE>;
3617 template class elf::MipsOptionsSection<ELF64LE>;
3618 template class elf::MipsOptionsSection<ELF64BE>;
3619 
3620 template class elf::MipsReginfoSection<ELF32LE>;
3621 template class elf::MipsReginfoSection<ELF32BE>;
3622 template class elf::MipsReginfoSection<ELF64LE>;
3623 template class elf::MipsReginfoSection<ELF64BE>;
3624 
3625 template class elf::DynamicSection<ELF32LE>;
3626 template class elf::DynamicSection<ELF32BE>;
3627 template class elf::DynamicSection<ELF64LE>;
3628 template class elf::DynamicSection<ELF64BE>;
3629 
3630 template class elf::RelocationSection<ELF32LE>;
3631 template class elf::RelocationSection<ELF32BE>;
3632 template class elf::RelocationSection<ELF64LE>;
3633 template class elf::RelocationSection<ELF64BE>;
3634 
3635 template class elf::AndroidPackedRelocationSection<ELF32LE>;
3636 template class elf::AndroidPackedRelocationSection<ELF32BE>;
3637 template class elf::AndroidPackedRelocationSection<ELF64LE>;
3638 template class elf::AndroidPackedRelocationSection<ELF64BE>;
3639 
3640 template class elf::RelrSection<ELF32LE>;
3641 template class elf::RelrSection<ELF32BE>;
3642 template class elf::RelrSection<ELF64LE>;
3643 template class elf::RelrSection<ELF64BE>;
3644 
3645 template class elf::SymbolTableSection<ELF32LE>;
3646 template class elf::SymbolTableSection<ELF32BE>;
3647 template class elf::SymbolTableSection<ELF64LE>;
3648 template class elf::SymbolTableSection<ELF64BE>;
3649 
3650 template class elf::VersionNeedSection<ELF32LE>;
3651 template class elf::VersionNeedSection<ELF32BE>;
3652 template class elf::VersionNeedSection<ELF64LE>;
3653 template class elf::VersionNeedSection<ELF64BE>;
3654 
3655 template void elf::writeEhdr<ELF32LE>(uint8_t *Buf, Partition &Part);
3656 template void elf::writeEhdr<ELF32BE>(uint8_t *Buf, Partition &Part);
3657 template void elf::writeEhdr<ELF64LE>(uint8_t *Buf, Partition &Part);
3658 template void elf::writeEhdr<ELF64BE>(uint8_t *Buf, Partition &Part);
3659 
3660 template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part);
3661 template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part);
3662 template void elf::writePhdrs<ELF64LE>(uint8_t *Buf, Partition &Part);
3663 template void elf::writePhdrs<ELF64BE>(uint8_t *Buf, Partition &Part);
3664 
3665 template class elf::PartitionElfHeaderSection<ELF32LE>;
3666 template class elf::PartitionElfHeaderSection<ELF32BE>;
3667 template class elf::PartitionElfHeaderSection<ELF64LE>;
3668 template class elf::PartitionElfHeaderSection<ELF64BE>;
3669 
3670 template class elf::PartitionProgramHeadersSection<ELF32LE>;
3671 template class elf::PartitionProgramHeadersSection<ELF32BE>;
3672 template class elf::PartitionProgramHeadersSection<ELF64LE>;
3673 template class elf::PartitionProgramHeadersSection<ELF64BE>;
3674