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