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