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