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