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