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