xref: /llvm-project/lld/ELF/Writer.cpp (revision 085f7fb560ee08a4d78a51dbf247ea816f8515a7)
1 //===- Writer.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 #include "Writer.h"
10 #include "AArch64ErrataFix.h"
11 #include "ARMErrataFix.h"
12 #include "CallGraphSort.h"
13 #include "Config.h"
14 #include "InputFiles.h"
15 #include "LinkerScript.h"
16 #include "MapFile.h"
17 #include "OutputSections.h"
18 #include "Relocations.h"
19 #include "SymbolTable.h"
20 #include "Symbols.h"
21 #include "SyntheticSections.h"
22 #include "Target.h"
23 #include "lld/Common/Arrays.h"
24 #include "lld/Common/CommonLinkerContext.h"
25 #include "lld/Common/Filesystem.h"
26 #include "lld/Common/Strings.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/StringMap.h"
29 #include "llvm/Support/BLAKE3.h"
30 #include "llvm/Support/Parallel.h"
31 #include "llvm/Support/RandomNumberGenerator.h"
32 #include "llvm/Support/TimeProfiler.h"
33 #include "llvm/Support/xxhash.h"
34 #include <climits>
35 
36 #define DEBUG_TYPE "lld"
37 
38 using namespace llvm;
39 using namespace llvm::ELF;
40 using namespace llvm::object;
41 using namespace llvm::support;
42 using namespace llvm::support::endian;
43 using namespace lld;
44 using namespace lld::elf;
45 
46 namespace {
47 // The writer writes a SymbolTable result to a file.
48 template <class ELFT> class Writer {
49 public:
50   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
51 
52   Writer(Ctx &ctx) : ctx(ctx), buffer(ctx.e.outputBuffer), tc(ctx) {}
53 
54   void run();
55 
56 private:
57   void addSectionSymbols();
58   void sortSections();
59   void resolveShfLinkOrder();
60   void finalizeAddressDependentContent();
61   void optimizeBasicBlockJumps();
62   void sortInputSections();
63   void sortOrphanSections();
64   void finalizeSections();
65   void checkExecuteOnly();
66   void setReservedSymbolSections();
67 
68   SmallVector<std::unique_ptr<PhdrEntry>, 0> createPhdrs(Partition &part);
69   void addPhdrForSection(Partition &part, unsigned shType, unsigned pType,
70                          unsigned pFlags);
71   void assignFileOffsets();
72   void assignFileOffsetsBinary();
73   void setPhdrs(Partition &part);
74   void checkSections();
75   void fixSectionAlignments();
76   void openFile();
77   void writeTrapInstr();
78   void writeHeader();
79   void writeSections();
80   void writeSectionsBinary();
81   void writeBuildId();
82 
83   Ctx &ctx;
84   std::unique_ptr<FileOutputBuffer> &buffer;
85   // ThunkCreator holds Thunks that are used at writeTo time.
86   ThunkCreator tc;
87 
88   void addRelIpltSymbols();
89   void addStartEndSymbols();
90   void addStartStopSymbols(OutputSection &osec);
91 
92   uint64_t fileSize;
93   uint64_t sectionHeaderOff;
94 };
95 } // anonymous namespace
96 
97 template <class ELFT> void elf::writeResult(Ctx &ctx) {
98   Writer<ELFT>(ctx).run();
99 }
100 
101 static void
102 removeEmptyPTLoad(Ctx &ctx, SmallVector<std::unique_ptr<PhdrEntry>, 0> &phdrs) {
103   auto it = std::stable_partition(phdrs.begin(), phdrs.end(), [&](auto &p) {
104     if (p->p_type != PT_LOAD)
105       return true;
106     if (!p->firstSec)
107       return false;
108     uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
109     return size != 0;
110   });
111 
112   // Clear OutputSection::ptLoad for sections contained in removed
113   // segments.
114   DenseSet<PhdrEntry *> removed;
115   for (auto it2 = it; it2 != phdrs.end(); ++it2)
116     removed.insert(it2->get());
117   for (OutputSection *sec : ctx.outputSections)
118     if (removed.count(sec->ptLoad))
119       sec->ptLoad = nullptr;
120   phdrs.erase(it, phdrs.end());
121 }
122 
123 void elf::copySectionsIntoPartitions(Ctx &ctx) {
124   SmallVector<InputSectionBase *, 0> newSections;
125   const size_t ehSize = ctx.ehInputSections.size();
126   for (unsigned part = 2; part != ctx.partitions.size() + 1; ++part) {
127     for (InputSectionBase *s : ctx.inputSections) {
128       if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)
129         continue;
130       auto *copy = make<InputSection>(cast<InputSection>(*s));
131       copy->partition = part;
132       newSections.push_back(copy);
133     }
134     for (size_t i = 0; i != ehSize; ++i) {
135       assert(ctx.ehInputSections[i]->isLive());
136       auto *copy = make<EhInputSection>(*ctx.ehInputSections[i]);
137       copy->partition = part;
138       ctx.ehInputSections.push_back(copy);
139     }
140   }
141 
142   ctx.inputSections.insert(ctx.inputSections.end(), newSections.begin(),
143                            newSections.end());
144 }
145 
146 static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,
147                                    uint64_t val, uint8_t stOther = STV_HIDDEN) {
148   Symbol *s = ctx.symtab->find(name);
149   if (!s || s->isDefined() || s->isCommon())
150     return nullptr;
151 
152   ctx.synthesizedSymbols.push_back(s);
153   s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,
154                           stOther, STT_NOTYPE, val,
155                           /*size=*/0, sec});
156   s->isUsedInRegularObj = true;
157   return cast<Defined>(s);
158 }
159 
160 // The linker is expected to define some symbols depending on
161 // the linking result. This function defines such symbols.
162 void elf::addReservedSymbols(Ctx &ctx) {
163   if (ctx.arg.emachine == EM_MIPS) {
164     auto addAbsolute = [&](StringRef name) {
165       Symbol *sym =
166           ctx.symtab->addSymbol(Defined{ctx, ctx.internalFile, name, STB_GLOBAL,
167                                         STV_HIDDEN, STT_NOTYPE, 0, 0, nullptr});
168       sym->isUsedInRegularObj = true;
169       return cast<Defined>(sym);
170     };
171     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
172     // so that it points to an absolute address which by default is relative
173     // to GOT. Default offset is 0x7ff0.
174     // See "Global Data Symbols" in Chapter 6 in the following document:
175     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
176     ctx.sym.mipsGp = addAbsolute("_gp");
177 
178     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
179     // start of function and 'gp' pointer into GOT.
180     if (ctx.symtab->find("_gp_disp"))
181       ctx.sym.mipsGpDisp = addAbsolute("_gp_disp");
182 
183     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
184     // pointer. This symbol is used in the code generated by .cpload pseudo-op
185     // in case of using -mno-shared option.
186     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
187     if (ctx.symtab->find("__gnu_local_gp"))
188       ctx.sym.mipsLocalGp = addAbsolute("__gnu_local_gp");
189   } else if (ctx.arg.emachine == EM_PPC) {
190     // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
191     // support Small Data Area, define it arbitrarily as 0.
192     addOptionalRegular(ctx, "_SDA_BASE_", nullptr, 0, STV_HIDDEN);
193   } else if (ctx.arg.emachine == EM_PPC64) {
194     addPPC64SaveRestore(ctx);
195   }
196 
197   // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
198   // combines the typical ELF GOT with the small data sections. It commonly
199   // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
200   // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
201   // represent the TOC base which is offset by 0x8000 bytes from the start of
202   // the .got section.
203   // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
204   // correctness of some relocations depends on its value.
205   StringRef gotSymName =
206       (ctx.arg.emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_";
207 
208   if (Symbol *s = ctx.symtab->find(gotSymName)) {
209     if (s->isDefined()) {
210       ErrAlways(ctx) << s->file << " cannot redefine linker defined symbol '"
211                      << gotSymName << "'";
212       return;
213     }
214 
215     uint64_t gotOff = 0;
216     if (ctx.arg.emachine == EM_PPC64)
217       gotOff = 0x8000;
218 
219     s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,
220                             STV_HIDDEN, STT_NOTYPE, gotOff, /*size=*/0,
221                             ctx.out.elfHeader.get()});
222     ctx.sym.globalOffsetTable = cast<Defined>(s);
223   }
224 
225   // __ehdr_start is the location of ELF file headers. Note that we define
226   // this symbol unconditionally even when using a linker script, which
227   // differs from the behavior implemented by GNU linker which only define
228   // this symbol if ELF headers are in the memory mapped segment.
229   addOptionalRegular(ctx, "__ehdr_start", ctx.out.elfHeader.get(), 0,
230                      STV_HIDDEN);
231 
232   // __executable_start is not documented, but the expectation of at
233   // least the Android libc is that it points to the ELF header.
234   addOptionalRegular(ctx, "__executable_start", ctx.out.elfHeader.get(), 0,
235                      STV_HIDDEN);
236 
237   // __dso_handle symbol is passed to cxa_finalize as a marker to identify
238   // each DSO. The address of the symbol doesn't matter as long as they are
239   // different in different DSOs, so we chose the start address of the DSO.
240   addOptionalRegular(ctx, "__dso_handle", ctx.out.elfHeader.get(), 0,
241                      STV_HIDDEN);
242 
243   // If linker script do layout we do not need to create any standard symbols.
244   if (ctx.script->hasSectionsCommand)
245     return;
246 
247   auto add = [&](StringRef s, int64_t pos) {
248     return addOptionalRegular(ctx, s, ctx.out.elfHeader.get(), pos,
249                               STV_DEFAULT);
250   };
251 
252   ctx.sym.bss = add("__bss_start", 0);
253   ctx.sym.end1 = add("end", -1);
254   ctx.sym.end2 = add("_end", -1);
255   ctx.sym.etext1 = add("etext", -1);
256   ctx.sym.etext2 = add("_etext", -1);
257   ctx.sym.edata1 = add("edata", -1);
258   ctx.sym.edata2 = add("_edata", -1);
259 }
260 
261 static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
262   if (map.empty())
263     for (auto [i, sec] : llvm::enumerate(sym.file->getSections()))
264       map.try_emplace(sec, i);
265   // Change WEAK to GLOBAL so that if a scanned relocation references sym,
266   // maybeReportUndefined will report an error.
267   uint8_t binding = sym.isWeak() ? uint8_t(STB_GLOBAL) : sym.binding;
268   Undefined(sym.file, sym.getName(), binding, sym.stOther, sym.type,
269             /*discardedSecIdx=*/map.lookup(sym.section))
270       .overwrite(sym);
271   // Eliminate from the symbol table, otherwise we would leave an undefined
272   // symbol if the symbol is unreferenced in the absence of GC.
273   sym.isUsedInRegularObj = false;
274 }
275 
276 // If all references to a DSO happen to be weak, the DSO is not added to
277 // DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
278 // dangling references to an unneeded DSO. Use a weak binding to avoid
279 // --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
280 //
281 // In addition, demote symbols defined in discarded sections, so that
282 // references to /DISCARD/ discarded symbols will lead to errors.
283 static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {
284   llvm::TimeTraceScope timeScope("Demote symbols");
285   DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
286   bool hasDynsym = ctx.hasDynsym;
287   for (Symbol *sym : ctx.symtab->getSymbols()) {
288     if (auto *d = dyn_cast<Defined>(sym)) {
289       if (d->section && !d->section->isLive())
290         demoteDefined(*d, sectionIndexMap[d->file]);
291     } else {
292       auto *s = dyn_cast<SharedSymbol>(sym);
293       if (sym->isLazy() || (s && !cast<SharedFile>(s->file)->isNeeded)) {
294         uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
295         Undefined(ctx.internalFile, sym->getName(), binding, sym->stOther,
296                   sym->type)
297             .overwrite(*sym);
298         sym->versionId = VER_NDX_GLOBAL;
299         if (hasDynsym && sym->includeInDynsym(ctx))
300           sym->isExported = true;
301       }
302     }
303 
304     if (hasDynsym)
305       sym->isPreemptible = sym->isExported && computeIsPreemptible(ctx, *sym);
306   }
307 }
308 
309 static OutputSection *findSection(Ctx &ctx, StringRef name,
310                                   unsigned partition = 1) {
311   for (SectionCommand *cmd : ctx.script->sectionCommands)
312     if (auto *osd = dyn_cast<OutputDesc>(cmd))
313       if (osd->osec.name == name && osd->osec.partition == partition)
314         return &osd->osec;
315   return nullptr;
316 }
317 
318 // The main function of the writer.
319 template <class ELFT> void Writer<ELFT>::run() {
320   // Now that we have a complete set of output sections. This function
321   // completes section contents. For example, we need to add strings
322   // to the string table, and add entries to .got and .plt.
323   // finalizeSections does that.
324   finalizeSections();
325   checkExecuteOnly();
326 
327   // If --compressed-debug-sections is specified, compress .debug_* sections.
328   // Do it right now because it changes the size of output sections.
329   for (OutputSection *sec : ctx.outputSections)
330     sec->maybeCompress<ELFT>(ctx);
331 
332   if (ctx.script->hasSectionsCommand)
333     ctx.script->allocateHeaders(ctx.mainPart->phdrs);
334 
335   // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
336   // 0 sized region. This has to be done late since only after assignAddresses
337   // we know the size of the sections.
338   for (Partition &part : ctx.partitions)
339     removeEmptyPTLoad(ctx, part.phdrs);
340 
341   if (!ctx.arg.oFormatBinary)
342     assignFileOffsets();
343   else
344     assignFileOffsetsBinary();
345 
346   for (Partition &part : ctx.partitions)
347     setPhdrs(part);
348 
349   // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
350   // because the files may be useful in case checkSections() or openFile()
351   // fails, for example, due to an erroneous file size.
352   writeMapAndCref(ctx);
353 
354   // Handle --print-memory-usage option.
355   if (ctx.arg.printMemoryUsage)
356     ctx.script->printMemoryUsage(ctx.e.outs());
357 
358   if (ctx.arg.checkSections)
359     checkSections();
360 
361   // It does not make sense try to open the file if we have error already.
362   if (errCount(ctx))
363     return;
364 
365   {
366     llvm::TimeTraceScope timeScope("Write output file");
367     // Write the result down to a file.
368     openFile();
369     if (errCount(ctx))
370       return;
371 
372     if (!ctx.arg.oFormatBinary) {
373       if (ctx.arg.zSeparate != SeparateSegmentKind::None)
374         writeTrapInstr();
375       writeHeader();
376       writeSections();
377     } else {
378       writeSectionsBinary();
379     }
380 
381     // Backfill .note.gnu.build-id section content. This is done at last
382     // because the content is usually a hash value of the entire output file.
383     writeBuildId();
384     if (errCount(ctx))
385       return;
386 
387     if (!ctx.e.disableOutput) {
388       if (auto e = buffer->commit())
389         Err(ctx) << "failed to write output '" << buffer->getPath()
390                  << "': " << std::move(e);
391     }
392 
393     if (!ctx.arg.cmseOutputLib.empty())
394       writeARMCmseImportLib<ELFT>(ctx);
395   }
396 }
397 
398 template <class ELFT, class RelTy>
399 static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,
400                                      llvm::ArrayRef<RelTy> rels) {
401   for (const RelTy &rel : rels) {
402     Symbol &sym = file->getRelocTargetSym(rel);
403     if (sym.isLocal())
404       sym.used = true;
405   }
406 }
407 
408 // The function ensures that the "used" field of local symbols reflects the fact
409 // that the symbol is used in a relocation from a live section.
410 template <class ELFT> static void markUsedLocalSymbols(Ctx &ctx) {
411   // With --gc-sections, the field is already filled.
412   // See MarkLive<ELFT>::resolveReloc().
413   if (ctx.arg.gcSections)
414     return;
415   for (ELFFileBase *file : ctx.objectFiles) {
416     ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
417     for (InputSectionBase *s : f->getSections()) {
418       InputSection *isec = dyn_cast_or_null<InputSection>(s);
419       if (!isec)
420         continue;
421       if (isec->type == SHT_REL) {
422         markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rel>());
423       } else if (isec->type == SHT_RELA) {
424         markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rela>());
425       } else if (isec->type == SHT_CREL) {
426         // The is64=true variant also works with ELF32 since only the r_symidx
427         // member is used.
428         for (Elf_Crel_Impl<true> r : RelocsCrel<true>(isec->content_)) {
429           Symbol &sym = file->getSymbol(r.r_symidx);
430           if (sym.isLocal())
431             sym.used = true;
432         }
433       }
434     }
435   }
436 }
437 
438 static bool shouldKeepInSymtab(Ctx &ctx, const Defined &sym) {
439   if (sym.isSection())
440     return false;
441 
442   // If --emit-reloc or -r is given, preserve symbols referenced by relocations
443   // from live sections.
444   if (sym.used && ctx.arg.copyRelocs)
445     return true;
446 
447   // Exclude local symbols pointing to .ARM.exidx sections.
448   // They are probably mapping symbols "$d", which are optional for these
449   // sections. After merging the .ARM.exidx sections, some of these symbols
450   // may become dangling. The easiest way to avoid the issue is not to add
451   // them to the symbol table from the beginning.
452   if (ctx.arg.emachine == EM_ARM && sym.section &&
453       sym.section->type == SHT_ARM_EXIDX)
454     return false;
455 
456   if (ctx.arg.discard == DiscardPolicy::None)
457     return true;
458   if (ctx.arg.discard == DiscardPolicy::All)
459     return false;
460 
461   // In ELF assembly .L symbols are normally discarded by the assembler.
462   // If the assembler fails to do so, the linker discards them if
463   // * --discard-locals is used.
464   // * The symbol is in a SHF_MERGE section, which is normally the reason for
465   //   the assembler keeping the .L symbol.
466   if (sym.getName().starts_with(".L") &&
467       (ctx.arg.discard == DiscardPolicy::Locals ||
468        (sym.section && (sym.section->flags & SHF_MERGE))))
469     return false;
470   return true;
471 }
472 
473 bool elf::includeInSymtab(Ctx &ctx, const Symbol &b) {
474   if (auto *d = dyn_cast<Defined>(&b)) {
475     // Always include absolute symbols.
476     SectionBase *sec = d->section;
477     if (!sec)
478       return true;
479     assert(sec->isLive());
480 
481     if (auto *s = dyn_cast<MergeInputSection>(sec))
482       return s->getSectionPiece(d->value).live;
483     return true;
484   }
485   return b.used || !ctx.arg.gcSections;
486 }
487 
488 // Scan local symbols to:
489 //
490 // - demote symbols defined relative to /DISCARD/ discarded input sections so
491 //   that relocations referencing them will lead to errors.
492 // - copy eligible symbols to .symTab
493 static void demoteAndCopyLocalSymbols(Ctx &ctx) {
494   llvm::TimeTraceScope timeScope("Add local symbols");
495   for (ELFFileBase *file : ctx.objectFiles) {
496     DenseMap<SectionBase *, size_t> sectionIndexMap;
497     for (Symbol *b : file->getLocalSymbols()) {
498       assert(b->isLocal() && "should have been caught in initializeSymbols()");
499       auto *dr = dyn_cast<Defined>(b);
500       if (!dr)
501         continue;
502 
503       if (dr->section && !dr->section->isLive())
504         demoteDefined(*dr, sectionIndexMap);
505       else if (ctx.in.symTab && includeInSymtab(ctx, *b) &&
506                shouldKeepInSymtab(ctx, *dr))
507         ctx.in.symTab->addSymbol(b);
508     }
509   }
510 }
511 
512 // Create a section symbol for each output section so that we can represent
513 // relocations that point to the section. If we know that no relocation is
514 // referring to a section (that happens if the section is a synthetic one), we
515 // don't create a section symbol for that section.
516 template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
517   for (SectionCommand *cmd : ctx.script->sectionCommands) {
518     auto *osd = dyn_cast<OutputDesc>(cmd);
519     if (!osd)
520       continue;
521     OutputSection &osec = osd->osec;
522     InputSectionBase *isec = nullptr;
523     // Iterate over all input sections and add a STT_SECTION symbol if any input
524     // section may be a relocation target.
525     for (SectionCommand *cmd : osec.commands) {
526       auto *isd = dyn_cast<InputSectionDescription>(cmd);
527       if (!isd)
528         continue;
529       for (InputSectionBase *s : isd->sections) {
530         // Relocations are not using REL[A] section symbols.
531         if (isStaticRelSecType(s->type))
532           continue;
533 
534         // Unlike other synthetic sections, mergeable output sections contain
535         // data copied from input sections, and there may be a relocation
536         // pointing to its contents if -r or --emit-reloc is given.
537         if (isa<SyntheticSection>(s) && !(s->flags & SHF_MERGE))
538           continue;
539 
540         isec = s;
541         break;
542       }
543     }
544     if (!isec)
545       continue;
546 
547     // Set the symbol to be relative to the output section so that its st_value
548     // equals the output section address. Note, there may be a gap between the
549     // start of the output section and isec.
550     ctx.in.symTab->addSymbol(makeDefined(ctx, isec->file, "", STB_LOCAL,
551                                          /*stOther=*/0, STT_SECTION,
552                                          /*value=*/0, /*size=*/0, &osec));
553   }
554 }
555 
556 // Today's loaders have a feature to make segments read-only after
557 // processing dynamic relocations to enhance security. PT_GNU_RELRO
558 // is defined for that.
559 //
560 // This function returns true if a section needs to be put into a
561 // PT_GNU_RELRO segment.
562 static bool isRelroSection(Ctx &ctx, const OutputSection *sec) {
563   if (!ctx.arg.zRelro)
564     return false;
565   if (sec->relro)
566     return true;
567 
568   uint64_t flags = sec->flags;
569 
570   // Non-allocatable or non-writable sections don't need RELRO because
571   // they are not writable or not even mapped to memory in the first place.
572   // RELRO is for sections that are essentially read-only but need to
573   // be writable only at process startup to allow dynamic linker to
574   // apply relocations.
575   if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))
576     return false;
577 
578   // Once initialized, TLS data segments are used as data templates
579   // for a thread-local storage. For each new thread, runtime
580   // allocates memory for a TLS and copy templates there. No thread
581   // are supposed to use templates directly. Thus, it can be in RELRO.
582   if (flags & SHF_TLS)
583     return true;
584 
585   // .init_array, .preinit_array and .fini_array contain pointers to
586   // functions that are executed on process startup or exit. These
587   // pointers are set by the static linker, and they are not expected
588   // to change at runtime. But if you are an attacker, you could do
589   // interesting things by manipulating pointers in .fini_array, for
590   // example. So they are put into RELRO.
591   uint32_t type = sec->type;
592   if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY ||
593       type == SHT_PREINIT_ARRAY)
594     return true;
595 
596   // .got contains pointers to external symbols. They are resolved by
597   // the dynamic linker when a module is loaded into memory, and after
598   // that they are not expected to change. So, it can be in RELRO.
599   if (ctx.in.got && sec == ctx.in.got->getParent())
600     return true;
601 
602   // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
603   // through r2 register, which is reserved for that purpose. Since r2 is used
604   // for accessing .got as well, .got and .toc need to be close enough in the
605   // virtual address space. Usually, .toc comes just after .got. Since we place
606   // .got into RELRO, .toc needs to be placed into RELRO too.
607   if (sec->name == ".toc")
608     return true;
609 
610   // .got.plt contains pointers to external function symbols. They are
611   // by default resolved lazily, so we usually cannot put it into RELRO.
612   // However, if "-z now" is given, the lazy symbol resolution is
613   // disabled, which enables us to put it into RELRO.
614   if (sec == ctx.in.gotPlt->getParent())
615     return ctx.arg.zNow;
616 
617   if (ctx.in.relroPadding && sec == ctx.in.relroPadding->getParent())
618     return true;
619 
620   // .dynamic section contains data for the dynamic linker, and
621   // there's no need to write to it at runtime, so it's better to put
622   // it into RELRO.
623   if (sec->name == ".dynamic")
624     return true;
625 
626   // Sections with some special names are put into RELRO. This is a
627   // bit unfortunate because section names shouldn't be significant in
628   // ELF in spirit. But in reality many linker features depend on
629   // magic section names.
630   StringRef s = sec->name;
631 
632   bool abiAgnostic = s == ".data.rel.ro" || s == ".bss.rel.ro" ||
633                      s == ".ctors" || s == ".dtors" || s == ".jcr" ||
634                      s == ".eh_frame" || s == ".fini_array" ||
635                      s == ".init_array" || s == ".preinit_array";
636 
637   bool abiSpecific =
638       ctx.arg.osabi == ELFOSABI_OPENBSD && s == ".openbsd.randomdata";
639 
640   return abiAgnostic || abiSpecific;
641 }
642 
643 // We compute a rank for each section. The rank indicates where the
644 // section should be placed in the file.  Instead of using simple
645 // numbers (0,1,2...), we use a series of flags. One for each decision
646 // point when placing the section.
647 // Using flags has two key properties:
648 // * It is easy to check if a give branch was taken.
649 // * It is easy two see how similar two ranks are (see getRankProximity).
650 enum RankFlags {
651   RF_NOT_ADDR_SET = 1 << 27,
652   RF_NOT_ALLOC = 1 << 26,
653   RF_PARTITION = 1 << 18, // Partition number (8 bits)
654   RF_LARGE_ALT = 1 << 15,
655   RF_WRITE = 1 << 14,
656   RF_EXEC_WRITE = 1 << 13,
657   RF_EXEC = 1 << 12,
658   RF_RODATA = 1 << 11,
659   RF_LARGE = 1 << 10,
660   RF_NOT_RELRO = 1 << 9,
661   RF_NOT_TLS = 1 << 8,
662   RF_BSS = 1 << 7,
663 };
664 
665 unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {
666   unsigned rank = osec.partition * RF_PARTITION;
667 
668   // We want to put section specified by -T option first, so we
669   // can start assigning VA starting from them later.
670   if (ctx.arg.sectionStartMap.count(osec.name))
671     return rank;
672   rank |= RF_NOT_ADDR_SET;
673 
674   // Allocatable sections go first to reduce the total PT_LOAD size and
675   // so debug info doesn't change addresses in actual code.
676   if (!(osec.flags & SHF_ALLOC))
677     return rank | RF_NOT_ALLOC;
678 
679   // Sort sections based on their access permission in the following
680   // order: R, RX, RXW, RW(RELRO), RW(non-RELRO).
681   //
682   // Read-only sections come first such that they go in the PT_LOAD covering the
683   // program headers at the start of the file.
684   //
685   // The layout for writable sections is PT_LOAD(PT_GNU_RELRO(.data.rel.ro
686   // .bss.rel.ro) | .data .bss), where | marks where page alignment happens.
687   // An alternative ordering is PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro
688   // .bss.rel.ro) | .bss), but it may waste more bytes due to 2 alignment
689   // places.
690   bool isExec = osec.flags & SHF_EXECINSTR;
691   bool isWrite = osec.flags & SHF_WRITE;
692 
693   if (!isWrite && !isExec) {
694     // Among PROGBITS sections, place .lrodata further from .text.
695     // For -z lrodata-after-bss, place .lrodata after .lbss like GNU ld. This
696     // layout has one extra PT_LOAD, but alleviates relocation overflow
697     // pressure for absolute relocations referencing small data from -fno-pic
698     // relocatable files.
699     if (osec.flags & SHF_X86_64_LARGE && ctx.arg.emachine == EM_X86_64)
700       rank |= ctx.arg.zLrodataAfterBss ? RF_LARGE_ALT : 0;
701     else
702       rank |= ctx.arg.zLrodataAfterBss ? 0 : RF_LARGE;
703 
704     if (osec.type == SHT_LLVM_PART_EHDR)
705       ;
706     else if (osec.type == SHT_LLVM_PART_PHDR)
707       rank |= 1;
708     else if (osec.name == ".interp")
709       rank |= 2;
710     // Put .note sections at the beginning so that they are likely to be
711     // included in a truncate core file. In particular, .note.gnu.build-id, if
712     // available, can identify the object file.
713     else if (osec.type == SHT_NOTE)
714       rank |= 3;
715     // Make PROGBITS sections (e.g .rodata .eh_frame) closer to .text to
716     // alleviate relocation overflow pressure. Large special sections such as
717     // .dynstr and .dynsym can be away from .text.
718     else if (osec.type != SHT_PROGBITS)
719       rank |= 4;
720     else
721       rank |= RF_RODATA;
722   } else if (isExec) {
723     rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC;
724   } else {
725     rank |= RF_WRITE;
726     // The TLS initialization block needs to be a single contiguous block. Place
727     // TLS sections directly before the other RELRO sections.
728     if (!(osec.flags & SHF_TLS))
729       rank |= RF_NOT_TLS;
730     if (isRelroSection(ctx, &osec))
731       osec.relro = true;
732     else
733       rank |= RF_NOT_RELRO;
734     // Place .ldata and .lbss after .bss. Making .bss closer to .text
735     // alleviates relocation overflow pressure.
736     // For -z lrodata-after-bss, place .lbss/.lrodata/.ldata after .bss.
737     // .bss/.lbss being adjacent reuses the NOBITS size optimization.
738     if (osec.flags & SHF_X86_64_LARGE && ctx.arg.emachine == EM_X86_64) {
739       rank |= ctx.arg.zLrodataAfterBss
740                   ? (osec.type == SHT_NOBITS ? 1 : RF_LARGE_ALT)
741                   : RF_LARGE;
742     }
743   }
744 
745   // Within TLS sections, or within other RelRo sections, or within non-RelRo
746   // sections, place non-NOBITS sections first.
747   if (osec.type == SHT_NOBITS)
748     rank |= RF_BSS;
749 
750   // Some architectures have additional ordering restrictions for sections
751   // within the same PT_LOAD.
752   if (ctx.arg.emachine == EM_PPC64) {
753     // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
754     // that we would like to make sure appear is a specific order to maximize
755     // their coverage by a single signed 16-bit offset from the TOC base
756     // pointer.
757     StringRef name = osec.name;
758     if (name == ".got")
759       rank |= 1;
760     else if (name == ".toc")
761       rank |= 2;
762   }
763 
764   if (ctx.arg.emachine == EM_MIPS) {
765     if (osec.name != ".got")
766       rank |= 1;
767     // All sections with SHF_MIPS_GPREL flag should be grouped together
768     // because data in these sections is addressable with a gp relative address.
769     if (osec.flags & SHF_MIPS_GPREL)
770       rank |= 2;
771   }
772 
773   if (ctx.arg.emachine == EM_RISCV) {
774     // .sdata and .sbss are placed closer to make GP relaxation more profitable
775     // and match GNU ld.
776     StringRef name = osec.name;
777     if (name == ".sdata" || (osec.type == SHT_NOBITS && name != ".sbss"))
778       rank |= 1;
779   }
780 
781   return rank;
782 }
783 
784 static bool compareSections(Ctx &ctx, const SectionCommand *aCmd,
785                             const SectionCommand *bCmd) {
786   const OutputSection *a = &cast<OutputDesc>(aCmd)->osec;
787   const OutputSection *b = &cast<OutputDesc>(bCmd)->osec;
788 
789   if (a->sortRank != b->sortRank)
790     return a->sortRank < b->sortRank;
791 
792   if (!(a->sortRank & RF_NOT_ADDR_SET))
793     return ctx.arg.sectionStartMap.lookup(a->name) <
794            ctx.arg.sectionStartMap.lookup(b->name);
795   return false;
796 }
797 
798 void PhdrEntry::add(OutputSection *sec) {
799   lastSec = sec;
800   if (!firstSec)
801     firstSec = sec;
802   p_align = std::max(p_align, sec->addralign);
803   if (p_type == PT_LOAD)
804     sec->ptLoad = this;
805 }
806 
807 // A statically linked position-dependent executable should only contain
808 // IRELATIVE relocations and no other dynamic relocations. Encapsulation symbols
809 // __rel[a]_iplt_{start,end} will be defined for .rel[a].dyn, to be
810 // processed by the libc runtime. Other executables or DSOs use dynamic tags
811 // instead.
812 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
813   if (ctx.arg.isPic)
814     return;
815 
816   // __rela_iplt_{start,end} are initially defined relative to dummy section 0.
817   // We'll override ctx.out.elfHeader with relaDyn later when we are sure that
818   // .rela.dyn will be present in the output.
819   std::string name = ctx.arg.isRela ? "__rela_iplt_start" : "__rel_iplt_start";
820   ctx.sym.relaIpltStart =
821       addOptionalRegular(ctx, name, ctx.out.elfHeader.get(), 0, STV_HIDDEN);
822   name.replace(name.size() - 5, 5, "end");
823   ctx.sym.relaIpltEnd =
824       addOptionalRegular(ctx, name, ctx.out.elfHeader.get(), 0, STV_HIDDEN);
825 }
826 
827 // This function generates assignments for predefined symbols (e.g. _end or
828 // _etext) and inserts them into the commands sequence to be processed at the
829 // appropriate time. This ensures that the value is going to be correct by the
830 // time any references to these symbols are processed and is equivalent to
831 // defining these symbols explicitly in the linker script.
832 template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
833   if (ctx.sym.globalOffsetTable) {
834     // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
835     // to the start of the .got or .got.plt section.
836     InputSection *sec = ctx.in.gotPlt.get();
837     if (!ctx.target->gotBaseSymInGotPlt)
838       sec = ctx.in.mipsGot ? cast<InputSection>(ctx.in.mipsGot.get())
839                            : cast<InputSection>(ctx.in.got.get());
840     ctx.sym.globalOffsetTable->section = sec;
841   }
842 
843   // .rela_iplt_{start,end} mark the start and the end of the section containing
844   // IRELATIVE relocations.
845   if (ctx.sym.relaIpltStart) {
846     auto &dyn = getIRelativeSection(ctx);
847     if (dyn.isNeeded()) {
848       ctx.sym.relaIpltStart->section = &dyn;
849       ctx.sym.relaIpltEnd->section = &dyn;
850       ctx.sym.relaIpltEnd->value = dyn.getSize();
851     }
852   }
853 
854   PhdrEntry *last = nullptr;
855   OutputSection *lastRO = nullptr;
856   auto isLarge = [&ctx = ctx](OutputSection *osec) {
857     return ctx.arg.emachine == EM_X86_64 && osec->flags & SHF_X86_64_LARGE;
858   };
859   for (Partition &part : ctx.partitions) {
860     for (auto &p : part.phdrs) {
861       if (p->p_type != PT_LOAD)
862         continue;
863       last = p.get();
864       if (!(p->p_flags & PF_W) && p->lastSec && !isLarge(p->lastSec))
865         lastRO = p->lastSec;
866     }
867   }
868 
869   if (lastRO) {
870     // _etext is the first location after the last read-only loadable segment
871     // that does not contain large sections.
872     if (ctx.sym.etext1)
873       ctx.sym.etext1->section = lastRO;
874     if (ctx.sym.etext2)
875       ctx.sym.etext2->section = lastRO;
876   }
877 
878   if (last) {
879     // _edata points to the end of the last non-large mapped initialized
880     // section.
881     OutputSection *edata = nullptr;
882     for (OutputSection *os : ctx.outputSections) {
883       if (os->type != SHT_NOBITS && !isLarge(os))
884         edata = os;
885       if (os == last->lastSec)
886         break;
887     }
888 
889     if (ctx.sym.edata1)
890       ctx.sym.edata1->section = edata;
891     if (ctx.sym.edata2)
892       ctx.sym.edata2->section = edata;
893 
894     // _end is the first location after the uninitialized data region.
895     if (ctx.sym.end1)
896       ctx.sym.end1->section = last->lastSec;
897     if (ctx.sym.end2)
898       ctx.sym.end2->section = last->lastSec;
899   }
900 
901   if (ctx.sym.bss) {
902     // On RISC-V, set __bss_start to the start of .sbss if present.
903     OutputSection *sbss =
904         ctx.arg.emachine == EM_RISCV ? findSection(ctx, ".sbss") : nullptr;
905     ctx.sym.bss->section = sbss ? sbss : findSection(ctx, ".bss");
906   }
907 
908   // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
909   // be equal to the _gp symbol's value.
910   if (ctx.sym.mipsGp) {
911     // Find GP-relative section with the lowest address
912     // and use this address to calculate default _gp value.
913     for (OutputSection *os : ctx.outputSections) {
914       if (os->flags & SHF_MIPS_GPREL) {
915         ctx.sym.mipsGp->section = os;
916         ctx.sym.mipsGp->value = 0x7ff0;
917         break;
918       }
919     }
920   }
921 }
922 
923 // We want to find how similar two ranks are.
924 // The more branches in getSectionRank that match, the more similar they are.
925 // Since each branch corresponds to a bit flag, we can just use
926 // countLeadingZeros.
927 static int getRankProximity(OutputSection *a, SectionCommand *b) {
928   auto *osd = dyn_cast<OutputDesc>(b);
929   return (osd && osd->osec.hasInputSections)
930              ? llvm::countl_zero(a->sortRank ^ osd->osec.sortRank)
931              : -1;
932 }
933 
934 // When placing orphan sections, we want to place them after symbol assignments
935 // so that an orphan after
936 //   begin_foo = .;
937 //   foo : { *(foo) }
938 //   end_foo = .;
939 // doesn't break the intended meaning of the begin/end symbols.
940 // We don't want to go over sections since findOrphanPos is the
941 // one in charge of deciding the order of the sections.
942 // We don't want to go over changes to '.', since doing so in
943 //  rx_sec : { *(rx_sec) }
944 //  . = ALIGN(0x1000);
945 //  /* The RW PT_LOAD starts here*/
946 //  rw_sec : { *(rw_sec) }
947 // would mean that the RW PT_LOAD would become unaligned.
948 static bool shouldSkip(SectionCommand *cmd) {
949   if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
950     return assign->name != ".";
951   return false;
952 }
953 
954 // We want to place orphan sections so that they share as much
955 // characteristics with their neighbors as possible. For example, if
956 // both are rw, or both are tls.
957 static SmallVectorImpl<SectionCommand *>::iterator
958 findOrphanPos(Ctx &ctx, SmallVectorImpl<SectionCommand *>::iterator b,
959               SmallVectorImpl<SectionCommand *>::iterator e) {
960   // Place non-alloc orphan sections at the end. This matches how we assign file
961   // offsets to non-alloc sections.
962   OutputSection *sec = &cast<OutputDesc>(*e)->osec;
963   if (!(sec->flags & SHF_ALLOC))
964     return e;
965 
966   // As a special case, place .relro_padding before the SymbolAssignment using
967   // DATA_SEGMENT_RELRO_END, if present.
968   if (ctx.in.relroPadding && sec == ctx.in.relroPadding->getParent()) {
969     auto i = std::find_if(b, e, [=](SectionCommand *a) {
970       if (auto *assign = dyn_cast<SymbolAssignment>(a))
971         return assign->dataSegmentRelroEnd;
972       return false;
973     });
974     if (i != e)
975       return i;
976   }
977 
978   // Find the most similar output section as the anchor. Rank Proximity is a
979   // value in the range [-1, 32] where [0, 32] indicates potential anchors (0:
980   // least similar; 32: identical). -1 means not an anchor.
981   //
982   // In the event of proximity ties, we select the first or last section
983   // depending on whether the orphan's rank is smaller.
984   int maxP = 0;
985   auto i = e;
986   for (auto j = b; j != e; ++j) {
987     int p = getRankProximity(sec, *j);
988     if (p > maxP ||
989         (p == maxP && cast<OutputDesc>(*j)->osec.sortRank <= sec->sortRank)) {
990       maxP = p;
991       i = j;
992     }
993   }
994   if (i == e)
995     return e;
996 
997   auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
998     auto *osd = dyn_cast<OutputDesc>(cmd);
999     return osd && osd->osec.hasInputSections;
1000   };
1001 
1002   // Then, scan backward or forward through the script for a suitable insertion
1003   // point. If i's rank is larger, the orphan section can be placed before i.
1004   //
1005   // However, don't do this if custom program headers are defined. Otherwise,
1006   // adding the orphan to a previous segment can change its flags, for example,
1007   // making a read-only segment writable. If memory regions are defined, an
1008   // orphan section should continue the same region as the found section to
1009   // better resemble the behavior of GNU ld.
1010   bool mustAfter =
1011       ctx.script->hasPhdrsCommands() || !ctx.script->memoryRegions.empty();
1012   if (cast<OutputDesc>(*i)->osec.sortRank <= sec->sortRank || mustAfter) {
1013     for (auto j = ++i; j != e; ++j) {
1014       if (!isOutputSecWithInputSections(*j))
1015         continue;
1016       if (getRankProximity(sec, *j) != maxP)
1017         break;
1018       i = j + 1;
1019     }
1020   } else {
1021     for (; i != b; --i)
1022       if (isOutputSecWithInputSections(i[-1]))
1023         break;
1024   }
1025 
1026   // As a special case, if the orphan section is the last section, put
1027   // it at the very end, past any other commands.
1028   // This matches bfd's behavior and is convenient when the linker script fully
1029   // specifies the start of the file, but doesn't care about the end (the non
1030   // alloc sections for example).
1031   if (std::find_if(i, e, isOutputSecWithInputSections) == e)
1032     return e;
1033 
1034   while (i != e && shouldSkip(*i))
1035     ++i;
1036   return i;
1037 }
1038 
1039 // Adds random priorities to sections not already in the map.
1040 static void maybeShuffle(Ctx &ctx,
1041                          DenseMap<const InputSectionBase *, int> &order) {
1042   if (ctx.arg.shuffleSections.empty())
1043     return;
1044 
1045   SmallVector<InputSectionBase *, 0> matched, sections = ctx.inputSections;
1046   matched.reserve(sections.size());
1047   for (const auto &patAndSeed : ctx.arg.shuffleSections) {
1048     matched.clear();
1049     for (InputSectionBase *sec : sections)
1050       if (patAndSeed.first.match(sec->name))
1051         matched.push_back(sec);
1052     const uint32_t seed = patAndSeed.second;
1053     if (seed == UINT32_MAX) {
1054       // If --shuffle-sections <section-glob>=-1, reverse the section order. The
1055       // section order is stable even if the number of sections changes. This is
1056       // useful to catch issues like static initialization order fiasco
1057       // reliably.
1058       std::reverse(matched.begin(), matched.end());
1059     } else {
1060       std::mt19937 g(seed ? seed : std::random_device()());
1061       llvm::shuffle(matched.begin(), matched.end(), g);
1062     }
1063     size_t i = 0;
1064     for (InputSectionBase *&sec : sections)
1065       if (patAndSeed.first.match(sec->name))
1066         sec = matched[i++];
1067   }
1068 
1069   // Existing priorities are < 0, so use priorities >= 0 for the missing
1070   // sections.
1071   int prio = 0;
1072   for (InputSectionBase *sec : sections) {
1073     if (order.try_emplace(sec, prio).second)
1074       ++prio;
1075   }
1076 }
1077 
1078 // Return section order within an InputSectionDescription.
1079 // If both --symbol-ordering-file and call graph profile are present, the order
1080 // file takes precedence, but the call graph profile is still used for symbols
1081 // that don't appear in the order file.
1082 static DenseMap<const InputSectionBase *, int> buildSectionOrder(Ctx &ctx) {
1083   DenseMap<const InputSectionBase *, int> sectionOrder;
1084   if (!ctx.arg.callGraphProfile.empty())
1085     sectionOrder = computeCallGraphProfileOrder(ctx);
1086 
1087   if (ctx.arg.symbolOrderingFile.empty())
1088     return sectionOrder;
1089 
1090   struct SymbolOrderEntry {
1091     int priority;
1092     bool present;
1093   };
1094 
1095   // Build a map from symbols to their priorities. Symbols that didn't
1096   // appear in the symbol ordering file have the lowest priority 0.
1097   // All explicitly mentioned symbols have negative (higher) priorities.
1098   DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder;
1099   int priority = -sectionOrder.size() - ctx.arg.symbolOrderingFile.size();
1100   for (StringRef s : ctx.arg.symbolOrderingFile)
1101     symbolOrder.insert({CachedHashStringRef(s), {priority++, false}});
1102 
1103   // Build a map from sections to their priorities.
1104   auto addSym = [&](Symbol &sym) {
1105     auto it = symbolOrder.find(CachedHashStringRef(sym.getName()));
1106     if (it == symbolOrder.end())
1107       return;
1108     SymbolOrderEntry &ent = it->second;
1109     ent.present = true;
1110 
1111     maybeWarnUnorderableSymbol(ctx, &sym);
1112 
1113     if (auto *d = dyn_cast<Defined>(&sym)) {
1114       if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {
1115         int &priority = sectionOrder[cast<InputSectionBase>(sec)];
1116         priority = std::min(priority, ent.priority);
1117       }
1118     }
1119   };
1120 
1121   // We want both global and local symbols. We get the global ones from the
1122   // symbol table and iterate the object files for the local ones.
1123   for (Symbol *sym : ctx.symtab->getSymbols())
1124     addSym(*sym);
1125 
1126   for (ELFFileBase *file : ctx.objectFiles)
1127     for (Symbol *sym : file->getLocalSymbols())
1128       addSym(*sym);
1129 
1130   if (ctx.arg.warnSymbolOrdering)
1131     for (auto orderEntry : symbolOrder)
1132       if (!orderEntry.second.present)
1133         Warn(ctx) << "symbol ordering file: no such symbol: "
1134                   << orderEntry.first.val();
1135 
1136   return sectionOrder;
1137 }
1138 
1139 // Sorts the sections in ISD according to the provided section order.
1140 static void
1141 sortISDBySectionOrder(Ctx &ctx, InputSectionDescription *isd,
1142                       const DenseMap<const InputSectionBase *, int> &order,
1143                       bool executableOutputSection) {
1144   SmallVector<InputSection *, 0> unorderedSections;
1145   SmallVector<std::pair<InputSection *, int>, 0> orderedSections;
1146   uint64_t unorderedSize = 0;
1147   uint64_t totalSize = 0;
1148 
1149   for (InputSection *isec : isd->sections) {
1150     if (executableOutputSection)
1151       totalSize += isec->getSize();
1152     auto i = order.find(isec);
1153     if (i == order.end()) {
1154       unorderedSections.push_back(isec);
1155       unorderedSize += isec->getSize();
1156       continue;
1157     }
1158     orderedSections.push_back({isec, i->second});
1159   }
1160   llvm::sort(orderedSections, llvm::less_second());
1161 
1162   // Find an insertion point for the ordered section list in the unordered
1163   // section list. On targets with limited-range branches, this is the mid-point
1164   // of the unordered section list. This decreases the likelihood that a range
1165   // extension thunk will be needed to enter or exit the ordered region. If the
1166   // ordered section list is a list of hot functions, we can generally expect
1167   // the ordered functions to be called more often than the unordered functions,
1168   // making it more likely that any particular call will be within range, and
1169   // therefore reducing the number of thunks required.
1170   //
1171   // For example, imagine that you have 8MB of hot code and 32MB of cold code.
1172   // If the layout is:
1173   //
1174   // 8MB hot
1175   // 32MB cold
1176   //
1177   // only the first 8-16MB of the cold code (depending on which hot function it
1178   // is actually calling) can call the hot code without a range extension thunk.
1179   // However, if we use this layout:
1180   //
1181   // 16MB cold
1182   // 8MB hot
1183   // 16MB cold
1184   //
1185   // both the last 8-16MB of the first block of cold code and the first 8-16MB
1186   // of the second block of cold code can call the hot code without a thunk. So
1187   // we effectively double the amount of code that could potentially call into
1188   // the hot code without a thunk.
1189   //
1190   // The above is not necessary if total size of input sections in this "isd"
1191   // is small. Note that we assume all input sections are executable if the
1192   // output section is executable (which is not always true but supposed to
1193   // cover most cases).
1194   size_t insPt = 0;
1195   if (executableOutputSection && !orderedSections.empty() &&
1196       ctx.target->getThunkSectionSpacing() &&
1197       totalSize >= ctx.target->getThunkSectionSpacing()) {
1198     uint64_t unorderedPos = 0;
1199     for (; insPt != unorderedSections.size(); ++insPt) {
1200       unorderedPos += unorderedSections[insPt]->getSize();
1201       if (unorderedPos > unorderedSize / 2)
1202         break;
1203     }
1204   }
1205 
1206   isd->sections.clear();
1207   for (InputSection *isec : ArrayRef(unorderedSections).slice(0, insPt))
1208     isd->sections.push_back(isec);
1209   for (std::pair<InputSection *, int> p : orderedSections)
1210     isd->sections.push_back(p.first);
1211   for (InputSection *isec : ArrayRef(unorderedSections).slice(insPt))
1212     isd->sections.push_back(isec);
1213 }
1214 
1215 static void sortSection(Ctx &ctx, OutputSection &osec,
1216                         const DenseMap<const InputSectionBase *, int> &order) {
1217   StringRef name = osec.name;
1218 
1219   // Never sort these.
1220   if (name == ".init" || name == ".fini")
1221     return;
1222 
1223   // Sort input sections by priority using the list provided by
1224   // --symbol-ordering-file or --shuffle-sections=. This is a least significant
1225   // digit radix sort. The sections may be sorted stably again by a more
1226   // significant key.
1227   if (!order.empty())
1228     for (SectionCommand *b : osec.commands)
1229       if (auto *isd = dyn_cast<InputSectionDescription>(b))
1230         sortISDBySectionOrder(ctx, isd, order, osec.flags & SHF_EXECINSTR);
1231 
1232   if (ctx.script->hasSectionsCommand)
1233     return;
1234 
1235   if (name == ".init_array" || name == ".fini_array") {
1236     osec.sortInitFini();
1237   } else if (name == ".ctors" || name == ".dtors") {
1238     osec.sortCtorsDtors();
1239   } else if (ctx.arg.emachine == EM_PPC64 && name == ".toc") {
1240     // .toc is allocated just after .got and is accessed using GOT-relative
1241     // relocations. Object files compiled with small code model have an
1242     // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
1243     // To reduce the risk of relocation overflow, .toc contents are sorted so
1244     // that sections having smaller relocation offsets are at beginning of .toc
1245     assert(osec.commands.size() == 1);
1246     auto *isd = cast<InputSectionDescription>(osec.commands[0]);
1247     llvm::stable_sort(isd->sections,
1248                       [](const InputSection *a, const InputSection *b) -> bool {
1249                         return a->file->ppc64SmallCodeModelTocRelocs &&
1250                                !b->file->ppc64SmallCodeModelTocRelocs;
1251                       });
1252   }
1253 }
1254 
1255 // Sort sections within each InputSectionDescription.
1256 template <class ELFT> void Writer<ELFT>::sortInputSections() {
1257   // Assign negative priorities.
1258   DenseMap<const InputSectionBase *, int> order = buildSectionOrder(ctx);
1259   // Assign non-negative priorities due to --shuffle-sections.
1260   maybeShuffle(ctx, order);
1261   for (SectionCommand *cmd : ctx.script->sectionCommands)
1262     if (auto *osd = dyn_cast<OutputDesc>(cmd))
1263       sortSection(ctx, osd->osec, order);
1264 }
1265 
1266 template <class ELFT> void Writer<ELFT>::sortSections() {
1267   llvm::TimeTraceScope timeScope("Sort sections");
1268 
1269   // Don't sort if using -r. It is not necessary and we want to preserve the
1270   // relative order for SHF_LINK_ORDER sections.
1271   if (ctx.arg.relocatable) {
1272     ctx.script->adjustOutputSections();
1273     return;
1274   }
1275 
1276   sortInputSections();
1277 
1278   for (SectionCommand *cmd : ctx.script->sectionCommands)
1279     if (auto *osd = dyn_cast<OutputDesc>(cmd))
1280       osd->osec.sortRank = getSectionRank(ctx, osd->osec);
1281   if (!ctx.script->hasSectionsCommand) {
1282     // OutputDescs are mostly contiguous, but may be interleaved with
1283     // SymbolAssignments in the presence of INSERT commands.
1284     auto mid = std::stable_partition(
1285         ctx.script->sectionCommands.begin(), ctx.script->sectionCommands.end(),
1286         [](SectionCommand *cmd) { return isa<OutputDesc>(cmd); });
1287     std::stable_sort(
1288         ctx.script->sectionCommands.begin(), mid,
1289         [&ctx = ctx](auto *l, auto *r) { return compareSections(ctx, l, r); });
1290   }
1291 
1292   // Process INSERT commands and update output section attributes. From this
1293   // point onwards the order of script->sectionCommands is fixed.
1294   ctx.script->processInsertCommands();
1295   ctx.script->adjustOutputSections();
1296 
1297   if (ctx.script->hasSectionsCommand)
1298     sortOrphanSections();
1299 
1300   ctx.script->adjustSectionsAfterSorting();
1301 }
1302 
1303 template <class ELFT> void Writer<ELFT>::sortOrphanSections() {
1304   // Orphan sections are sections present in the input files which are
1305   // not explicitly placed into the output file by the linker script.
1306   //
1307   // The sections in the linker script are already in the correct
1308   // order. We have to figuere out where to insert the orphan
1309   // sections.
1310   //
1311   // The order of the sections in the script is arbitrary and may not agree with
1312   // compareSections. This means that we cannot easily define a strict weak
1313   // ordering. To see why, consider a comparison of a section in the script and
1314   // one not in the script. We have a two simple options:
1315   // * Make them equivalent (a is not less than b, and b is not less than a).
1316   //   The problem is then that equivalence has to be transitive and we can
1317   //   have sections a, b and c with only b in a script and a less than c
1318   //   which breaks this property.
1319   // * Use compareSectionsNonScript. Given that the script order doesn't have
1320   //   to match, we can end up with sections a, b, c, d where b and c are in the
1321   //   script and c is compareSectionsNonScript less than b. In which case d
1322   //   can be equivalent to c, a to b and d < a. As a concrete example:
1323   //   .a (rx) # not in script
1324   //   .b (rx) # in script
1325   //   .c (ro) # in script
1326   //   .d (ro) # not in script
1327   //
1328   // The way we define an order then is:
1329   // *  Sort only the orphan sections. They are in the end right now.
1330   // *  Move each orphan section to its preferred position. We try
1331   //    to put each section in the last position where it can share
1332   //    a PT_LOAD.
1333   //
1334   // There is some ambiguity as to where exactly a new entry should be
1335   // inserted, because Commands contains not only output section
1336   // commands but also other types of commands such as symbol assignment
1337   // expressions. There's no correct answer here due to the lack of the
1338   // formal specification of the linker script. We use heuristics to
1339   // determine whether a new output command should be added before or
1340   // after another commands. For the details, look at shouldSkip
1341   // function.
1342 
1343   auto i = ctx.script->sectionCommands.begin();
1344   auto e = ctx.script->sectionCommands.end();
1345   auto nonScriptI = std::find_if(i, e, [](SectionCommand *cmd) {
1346     if (auto *osd = dyn_cast<OutputDesc>(cmd))
1347       return osd->osec.sectionIndex == UINT32_MAX;
1348     return false;
1349   });
1350 
1351   // Sort the orphan sections.
1352   std::stable_sort(nonScriptI, e, [&ctx = ctx](auto *l, auto *r) {
1353     return compareSections(ctx, l, r);
1354   });
1355 
1356   // As a horrible special case, skip the first . assignment if it is before any
1357   // section. We do this because it is common to set a load address by starting
1358   // the script with ". = 0xabcd" and the expectation is that every section is
1359   // after that.
1360   auto firstSectionOrDotAssignment =
1361       std::find_if(i, e, [](SectionCommand *cmd) { return !shouldSkip(cmd); });
1362   if (firstSectionOrDotAssignment != e &&
1363       isa<SymbolAssignment>(**firstSectionOrDotAssignment))
1364     ++firstSectionOrDotAssignment;
1365   i = firstSectionOrDotAssignment;
1366 
1367   while (nonScriptI != e) {
1368     auto pos = findOrphanPos(ctx, i, nonScriptI);
1369     OutputSection *orphan = &cast<OutputDesc>(*nonScriptI)->osec;
1370 
1371     // As an optimization, find all sections with the same sort rank
1372     // and insert them with one rotate.
1373     unsigned rank = orphan->sortRank;
1374     auto end = std::find_if(nonScriptI + 1, e, [=](SectionCommand *cmd) {
1375       return cast<OutputDesc>(cmd)->osec.sortRank != rank;
1376     });
1377     std::rotate(pos, nonScriptI, end);
1378     nonScriptI = end;
1379   }
1380 }
1381 
1382 static bool compareByFilePosition(InputSection *a, InputSection *b) {
1383   InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr;
1384   InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr;
1385   // SHF_LINK_ORDER sections with non-zero sh_link are ordered before
1386   // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link.
1387   if (!la || !lb)
1388     return la && !lb;
1389   OutputSection *aOut = la->getParent();
1390   OutputSection *bOut = lb->getParent();
1391 
1392   if (aOut == bOut)
1393     return la->outSecOff < lb->outSecOff;
1394   if (aOut->addr == bOut->addr)
1395     return aOut->sectionIndex < bOut->sectionIndex;
1396   return aOut->addr < bOut->addr;
1397 }
1398 
1399 template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1400   llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER");
1401   for (OutputSection *sec : ctx.outputSections) {
1402     if (!(sec->flags & SHF_LINK_ORDER))
1403       continue;
1404 
1405     // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1406     // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1407     if (!ctx.arg.relocatable && ctx.arg.emachine == EM_ARM &&
1408         sec->type == SHT_ARM_EXIDX)
1409       continue;
1410 
1411     // Link order may be distributed across several InputSectionDescriptions.
1412     // Sorting is performed separately.
1413     SmallVector<InputSection **, 0> scriptSections;
1414     SmallVector<InputSection *, 0> sections;
1415     for (SectionCommand *cmd : sec->commands) {
1416       auto *isd = dyn_cast<InputSectionDescription>(cmd);
1417       if (!isd)
1418         continue;
1419       bool hasLinkOrder = false;
1420       scriptSections.clear();
1421       sections.clear();
1422       for (InputSection *&isec : isd->sections) {
1423         if (isec->flags & SHF_LINK_ORDER) {
1424           InputSection *link = isec->getLinkOrderDep();
1425           if (link && !link->getParent())
1426             ErrAlways(ctx) << isec << ": sh_link points to discarded section "
1427                            << link;
1428           hasLinkOrder = true;
1429         }
1430         scriptSections.push_back(&isec);
1431         sections.push_back(isec);
1432       }
1433       if (hasLinkOrder && errCount(ctx) == 0) {
1434         llvm::stable_sort(sections, compareByFilePosition);
1435         for (int i = 0, n = sections.size(); i != n; ++i)
1436           *scriptSections[i] = sections[i];
1437       }
1438     }
1439   }
1440 }
1441 
1442 static void finalizeSynthetic(Ctx &ctx, SyntheticSection *sec) {
1443   if (sec && sec->isNeeded() && sec->getParent()) {
1444     llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name);
1445     sec->finalizeContents();
1446   }
1447 }
1448 
1449 static bool canInsertPadding(OutputSection *sec) {
1450   StringRef s = sec->name;
1451   return s == ".bss" || s == ".data" || s == ".data.rel.ro" || s == ".lbss" ||
1452          s == ".ldata" || s == ".lrodata" || s == ".ltext" || s == ".rodata" ||
1453          s.starts_with(".text");
1454 }
1455 
1456 static void randomizeSectionPadding(Ctx &ctx) {
1457   std::mt19937 g(*ctx.arg.randomizeSectionPadding);
1458   PhdrEntry *curPtLoad = nullptr;
1459   for (OutputSection *os : ctx.outputSections) {
1460     if (!canInsertPadding(os))
1461       continue;
1462     for (SectionCommand *bc : os->commands) {
1463       if (auto *isd = dyn_cast<InputSectionDescription>(bc)) {
1464         SmallVector<InputSection *, 0> tmp;
1465         if (os->ptLoad != curPtLoad) {
1466           tmp.push_back(make<RandomizePaddingSection>(
1467               ctx, g() % ctx.arg.maxPageSize, os));
1468           curPtLoad = os->ptLoad;
1469         }
1470         for (InputSection *isec : isd->sections) {
1471           // Probability of inserting padding is 1 in 16.
1472           if (g() % 16 == 0)
1473             tmp.push_back(
1474                 make<RandomizePaddingSection>(ctx, isec->addralign, os));
1475           tmp.push_back(isec);
1476         }
1477         isd->sections = std::move(tmp);
1478       }
1479     }
1480   }
1481 }
1482 
1483 // We need to generate and finalize the content that depends on the address of
1484 // InputSections. As the generation of the content may also alter InputSection
1485 // addresses we must converge to a fixed point. We do that here. See the comment
1486 // in Writer<ELFT>::finalizeSections().
1487 template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1488   llvm::TimeTraceScope timeScope("Finalize address dependent content");
1489   AArch64Err843419Patcher a64p(ctx);
1490   ARMErr657417Patcher a32p(ctx);
1491   ctx.script->assignAddresses();
1492 
1493   // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
1494   // do require the relative addresses of OutputSections because linker scripts
1495   // can assign Virtual Addresses to OutputSections that are not monotonically
1496   // increasing. Anything here must be repeatable, since spilling may change
1497   // section order.
1498   const auto finalizeOrderDependentContent = [this] {
1499     for (Partition &part : ctx.partitions)
1500       finalizeSynthetic(ctx, part.armExidx.get());
1501     resolveShfLinkOrder();
1502   };
1503   finalizeOrderDependentContent();
1504 
1505   // Converts call x@GDPLT to call __tls_get_addr
1506   if (ctx.arg.emachine == EM_HEXAGON)
1507     hexagonTLSSymbolUpdate(ctx);
1508 
1509   if (ctx.arg.randomizeSectionPadding)
1510     randomizeSectionPadding(ctx);
1511 
1512   uint32_t pass = 0, assignPasses = 0;
1513   for (;;) {
1514     bool changed = ctx.target->needsThunks
1515                        ? tc.createThunks(pass, ctx.outputSections)
1516                        : ctx.target->relaxOnce(pass);
1517     bool spilled = ctx.script->spillSections();
1518     changed |= spilled;
1519     ++pass;
1520 
1521     // With Thunk Size much smaller than branch range we expect to
1522     // converge quickly; if we get to 30 something has gone wrong.
1523     if (changed && pass >= 30) {
1524       Err(ctx) << (ctx.target->needsThunks ? "thunk creation not converged"
1525                                            : "relaxation not converged");
1526       break;
1527     }
1528 
1529     if (ctx.arg.fixCortexA53Errata843419) {
1530       if (changed)
1531         ctx.script->assignAddresses();
1532       changed |= a64p.createFixes();
1533     }
1534     if (ctx.arg.fixCortexA8) {
1535       if (changed)
1536         ctx.script->assignAddresses();
1537       changed |= a32p.createFixes();
1538     }
1539 
1540     finalizeSynthetic(ctx, ctx.in.got.get());
1541     if (ctx.in.mipsGot)
1542       ctx.in.mipsGot->updateAllocSize(ctx);
1543 
1544     for (Partition &part : ctx.partitions) {
1545       // The R_AARCH64_AUTH_RELATIVE has a smaller addend field as bits [63:32]
1546       // encode the signing schema. We've put relocations in .relr.auth.dyn
1547       // during RelocationScanner::processAux, but the target VA for some of
1548       // them might be wider than 32 bits. We can only know the final VA at this
1549       // point, so move relocations with large values from .relr.auth.dyn to
1550       // .rela.dyn. See also AArch64::relocate.
1551       if (part.relrAuthDyn) {
1552         auto it = llvm::remove_if(
1553             part.relrAuthDyn->relocs, [this, &part](const RelativeReloc &elem) {
1554               const Relocation &reloc = elem.inputSec->relocs()[elem.relocIdx];
1555               if (isInt<32>(reloc.sym->getVA(ctx, reloc.addend)))
1556                 return false;
1557               part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, elem.inputSec,
1558                                       reloc.offset,
1559                                       DynamicReloc::AddendOnlyWithTargetVA,
1560                                       *reloc.sym, reloc.addend, R_ABS});
1561               return true;
1562             });
1563         changed |= (it != part.relrAuthDyn->relocs.end());
1564         part.relrAuthDyn->relocs.erase(it, part.relrAuthDyn->relocs.end());
1565       }
1566       if (part.relaDyn)
1567         changed |= part.relaDyn->updateAllocSize(ctx);
1568       if (part.relrDyn)
1569         changed |= part.relrDyn->updateAllocSize(ctx);
1570       if (part.relrAuthDyn)
1571         changed |= part.relrAuthDyn->updateAllocSize(ctx);
1572       if (part.memtagGlobalDescriptors)
1573         changed |= part.memtagGlobalDescriptors->updateAllocSize(ctx);
1574     }
1575 
1576     std::pair<const OutputSection *, const Defined *> changes =
1577         ctx.script->assignAddresses();
1578     if (!changed) {
1579       // Some symbols may be dependent on section addresses. When we break the
1580       // loop, the symbol values are finalized because a previous
1581       // assignAddresses() finalized section addresses.
1582       if (!changes.first && !changes.second)
1583         break;
1584       if (++assignPasses == 5) {
1585         if (changes.first)
1586           Err(ctx) << "address (0x" << Twine::utohexstr(changes.first->addr)
1587                    << ") of section '" << changes.first->name
1588                    << "' does not converge";
1589         if (changes.second)
1590           Err(ctx) << "assignment to symbol " << changes.second
1591                    << " does not converge";
1592         break;
1593       }
1594     } else if (spilled) {
1595       // Spilling can change relative section order.
1596       finalizeOrderDependentContent();
1597     }
1598   }
1599   if (!ctx.arg.relocatable)
1600     ctx.target->finalizeRelax(pass);
1601 
1602   if (ctx.arg.relocatable)
1603     for (OutputSection *sec : ctx.outputSections)
1604       sec->addr = 0;
1605 
1606   // If addrExpr is set, the address may not be a multiple of the alignment.
1607   // Warn because this is error-prone.
1608   for (SectionCommand *cmd : ctx.script->sectionCommands)
1609     if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
1610       OutputSection *osec = &osd->osec;
1611       if (osec->addr % osec->addralign != 0)
1612         Warn(ctx) << "address (0x" << Twine::utohexstr(osec->addr)
1613                   << ") of section " << osec->name
1614                   << " is not a multiple of alignment (" << osec->addralign
1615                   << ")";
1616     }
1617 
1618   // Sizes are no longer allowed to grow, so all allowable spills have been
1619   // taken. Remove any leftover potential spills.
1620   ctx.script->erasePotentialSpillSections();
1621 }
1622 
1623 // If Input Sections have been shrunk (basic block sections) then
1624 // update symbol values and sizes associated with these sections.  With basic
1625 // block sections, input sections can shrink when the jump instructions at
1626 // the end of the section are relaxed.
1627 static void fixSymbolsAfterShrinking(Ctx &ctx) {
1628   for (InputFile *File : ctx.objectFiles) {
1629     parallelForEach(File->getSymbols(), [&](Symbol *Sym) {
1630       auto *def = dyn_cast<Defined>(Sym);
1631       if (!def)
1632         return;
1633 
1634       const SectionBase *sec = def->section;
1635       if (!sec)
1636         return;
1637 
1638       const InputSectionBase *inputSec = dyn_cast<InputSectionBase>(sec);
1639       if (!inputSec || !inputSec->bytesDropped)
1640         return;
1641 
1642       const size_t OldSize = inputSec->content().size();
1643       const size_t NewSize = OldSize - inputSec->bytesDropped;
1644 
1645       if (def->value > NewSize && def->value <= OldSize) {
1646         LLVM_DEBUG(llvm::dbgs()
1647                    << "Moving symbol " << Sym->getName() << " from "
1648                    << def->value << " to "
1649                    << def->value - inputSec->bytesDropped << " bytes\n");
1650         def->value -= inputSec->bytesDropped;
1651         return;
1652       }
1653 
1654       if (def->value + def->size > NewSize && def->value <= OldSize &&
1655           def->value + def->size <= OldSize) {
1656         LLVM_DEBUG(llvm::dbgs()
1657                    << "Shrinking symbol " << Sym->getName() << " from "
1658                    << def->size << " to " << def->size - inputSec->bytesDropped
1659                    << " bytes\n");
1660         def->size -= inputSec->bytesDropped;
1661       }
1662     });
1663   }
1664 }
1665 
1666 // If basic block sections exist, there are opportunities to delete fall thru
1667 // jumps and shrink jump instructions after basic block reordering.  This
1668 // relaxation pass does that.  It is only enabled when --optimize-bb-jumps
1669 // option is used.
1670 template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
1671   assert(ctx.arg.optimizeBBJumps);
1672   SmallVector<InputSection *, 0> storage;
1673 
1674   ctx.script->assignAddresses();
1675   // For every output section that has executable input sections, this
1676   // does the following:
1677   //   1. Deletes all direct jump instructions in input sections that
1678   //      jump to the following section as it is not required.
1679   //   2. If there are two consecutive jump instructions, it checks
1680   //      if they can be flipped and one can be deleted.
1681   for (OutputSection *osec : ctx.outputSections) {
1682     if (!(osec->flags & SHF_EXECINSTR))
1683       continue;
1684     ArrayRef<InputSection *> sections = getInputSections(*osec, storage);
1685     size_t numDeleted = 0;
1686     // Delete all fall through jump instructions.  Also, check if two
1687     // consecutive jump instructions can be flipped so that a fall
1688     // through jmp instruction can be deleted.
1689     for (size_t i = 0, e = sections.size(); i != e; ++i) {
1690       InputSection *next = i + 1 < sections.size() ? sections[i + 1] : nullptr;
1691       InputSection &sec = *sections[i];
1692       numDeleted += ctx.target->deleteFallThruJmpInsn(sec, sec.file, next);
1693     }
1694     if (numDeleted > 0) {
1695       ctx.script->assignAddresses();
1696       LLVM_DEBUG(llvm::dbgs()
1697                  << "Removing " << numDeleted << " fall through jumps\n");
1698     }
1699   }
1700 
1701   fixSymbolsAfterShrinking(ctx);
1702 
1703   for (OutputSection *osec : ctx.outputSections)
1704     for (InputSection *is : getInputSections(*osec, storage))
1705       is->trim();
1706 }
1707 
1708 // In order to allow users to manipulate linker-synthesized sections,
1709 // we had to add synthetic sections to the input section list early,
1710 // even before we make decisions whether they are needed. This allows
1711 // users to write scripts like this: ".mygot : { .got }".
1712 //
1713 // Doing it has an unintended side effects. If it turns out that we
1714 // don't need a .got (for example) at all because there's no
1715 // relocation that needs a .got, we don't want to emit .got.
1716 //
1717 // To deal with the above problem, this function is called after
1718 // scanRelocations is called to remove synthetic sections that turn
1719 // out to be empty.
1720 static void removeUnusedSyntheticSections(Ctx &ctx) {
1721   // All input synthetic sections that can be empty are placed after
1722   // all regular ones. Reverse iterate to find the first synthetic section
1723   // after a non-synthetic one which will be our starting point.
1724   auto start =
1725       llvm::find_if(llvm::reverse(ctx.inputSections), [](InputSectionBase *s) {
1726         return !isa<SyntheticSection>(s);
1727       }).base();
1728 
1729   // Remove unused synthetic sections from ctx.inputSections;
1730   DenseSet<InputSectionBase *> unused;
1731   auto end =
1732       std::remove_if(start, ctx.inputSections.end(), [&](InputSectionBase *s) {
1733         auto *sec = cast<SyntheticSection>(s);
1734         if (sec->getParent() && sec->isNeeded())
1735           return false;
1736         // .relr.auth.dyn relocations may be moved to .rela.dyn in
1737         // finalizeAddressDependentContent, making .rela.dyn no longer empty.
1738         // Conservatively keep .rela.dyn. .relr.auth.dyn can be made empty, but
1739         // we would fail to remove it here.
1740         if (ctx.arg.emachine == EM_AARCH64 && ctx.arg.relrPackDynRelocs &&
1741             sec == ctx.mainPart->relaDyn.get())
1742           return false;
1743         unused.insert(sec);
1744         return true;
1745       });
1746   ctx.inputSections.erase(end, ctx.inputSections.end());
1747 
1748   // Remove unused synthetic sections from the corresponding input section
1749   // description and orphanSections.
1750   for (auto *sec : unused)
1751     if (OutputSection *osec = cast<SyntheticSection>(sec)->getParent())
1752       for (SectionCommand *cmd : osec->commands)
1753         if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
1754           llvm::erase_if(isd->sections, [&](InputSection *isec) {
1755             return unused.count(isec);
1756           });
1757   llvm::erase_if(ctx.script->orphanSections, [&](const InputSectionBase *sec) {
1758     return unused.count(sec);
1759   });
1760 }
1761 
1762 // Create output section objects and add them to OutputSections.
1763 template <class ELFT> void Writer<ELFT>::finalizeSections() {
1764   if (!ctx.arg.relocatable) {
1765     ctx.out.preinitArray = findSection(ctx, ".preinit_array");
1766     ctx.out.initArray = findSection(ctx, ".init_array");
1767     ctx.out.finiArray = findSection(ctx, ".fini_array");
1768 
1769     // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1770     // symbols for sections, so that the runtime can get the start and end
1771     // addresses of each section by section name. Add such symbols.
1772     addStartEndSymbols();
1773     for (SectionCommand *cmd : ctx.script->sectionCommands)
1774       if (auto *osd = dyn_cast<OutputDesc>(cmd))
1775         addStartStopSymbols(osd->osec);
1776 
1777     // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1778     // It should be okay as no one seems to care about the type.
1779     // Even the author of gold doesn't remember why gold behaves that way.
1780     // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1781     if (ctx.mainPart->dynamic->parent) {
1782       Symbol *s = ctx.symtab->addSymbol(Defined{
1783           ctx, ctx.internalFile, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,
1784           /*value=*/0, /*size=*/0, ctx.mainPart->dynamic.get()});
1785       s->isUsedInRegularObj = true;
1786     }
1787 
1788     // Define __rel[a]_iplt_{start,end} symbols if needed.
1789     addRelIpltSymbols();
1790 
1791     // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
1792     // should only be defined in an executable. If .sdata does not exist, its
1793     // value/section does not matter but it has to be relative, so set its
1794     // st_shndx arbitrarily to 1 (ctx.out.elfHeader).
1795     if (ctx.arg.emachine == EM_RISCV) {
1796       if (!ctx.arg.shared) {
1797         OutputSection *sec = findSection(ctx, ".sdata");
1798         addOptionalRegular(ctx, "__global_pointer$",
1799                            sec ? sec : ctx.out.elfHeader.get(), 0x800,
1800                            STV_DEFAULT);
1801         // Set riscvGlobalPointer to be used by the optional global pointer
1802         // relaxation.
1803         if (ctx.arg.relaxGP) {
1804           Symbol *s = ctx.symtab->find("__global_pointer$");
1805           if (s && s->isDefined())
1806             ctx.sym.riscvGlobalPointer = cast<Defined>(s);
1807         }
1808       }
1809     }
1810 
1811     if (ctx.arg.emachine == EM_386 || ctx.arg.emachine == EM_X86_64) {
1812       // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a
1813       // way that:
1814       //
1815       // 1) Without relaxation: it produces a dynamic TLSDESC relocation that
1816       // computes 0.
1817       // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address
1818       // in the TLS block).
1819       //
1820       // 2) is special cased in @tpoff computation. To satisfy 1), we define it
1821       // as an absolute symbol of zero. This is different from GNU linkers which
1822       // define _TLS_MODULE_BASE_ relative to the first TLS section.
1823       Symbol *s = ctx.symtab->find("_TLS_MODULE_BASE_");
1824       if (s && s->isUndefined()) {
1825         s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,
1826                                 STV_HIDDEN, STT_TLS, /*value=*/0, 0,
1827                                 /*section=*/nullptr});
1828         ctx.sym.tlsModuleBase = cast<Defined>(s);
1829       }
1830     }
1831 
1832     // This responsible for splitting up .eh_frame section into
1833     // pieces. The relocation scan uses those pieces, so this has to be
1834     // earlier.
1835     {
1836       llvm::TimeTraceScope timeScope("Finalize .eh_frame");
1837       for (Partition &part : ctx.partitions)
1838         finalizeSynthetic(ctx, part.ehFrame.get());
1839     }
1840   }
1841 
1842   // If the previous code block defines any non-hidden symbols (e.g.
1843   // __global_pointer$), they may be exported.
1844   if (ctx.hasDynsym)
1845     for (Symbol *sym : ctx.synthesizedSymbols)
1846       sym->isExported = sym->includeInDynsym(ctx);
1847 
1848   demoteSymbolsAndComputeIsPreemptible(ctx);
1849 
1850   if (ctx.arg.copyRelocs && ctx.arg.discard != DiscardPolicy::None)
1851     markUsedLocalSymbols<ELFT>(ctx);
1852   demoteAndCopyLocalSymbols(ctx);
1853 
1854   if (ctx.arg.copyRelocs)
1855     addSectionSymbols();
1856 
1857   // Change values of linker-script-defined symbols from placeholders (assigned
1858   // by declareSymbols) to actual definitions.
1859   ctx.script->processSymbolAssignments();
1860 
1861   if (!ctx.arg.relocatable) {
1862     llvm::TimeTraceScope timeScope("Scan relocations");
1863     // Scan relocations. This must be done after every symbol is declared so
1864     // that we can correctly decide if a dynamic relocation is needed. This is
1865     // called after processSymbolAssignments() because it needs to know whether
1866     // a linker-script-defined symbol is absolute.
1867     scanRelocations<ELFT>(ctx);
1868     reportUndefinedSymbols(ctx);
1869     postScanRelocations(ctx);
1870 
1871     if (ctx.in.plt && ctx.in.plt->isNeeded())
1872       ctx.in.plt->addSymbols();
1873     if (ctx.in.iplt && ctx.in.iplt->isNeeded())
1874       ctx.in.iplt->addSymbols();
1875 
1876     if (ctx.arg.unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {
1877       auto diag =
1878           ctx.arg.unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError &&
1879                   !ctx.arg.noinhibitExec
1880               ? DiagLevel::Err
1881               : DiagLevel::Warn;
1882       // Error on undefined symbols in a shared object, if all of its DT_NEEDED
1883       // entries are seen. These cases would otherwise lead to runtime errors
1884       // reported by the dynamic linker.
1885       //
1886       // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker
1887       // to catch more cases. That is too much for us. Our approach resembles
1888       // the one used in ld.gold, achieves a good balance to be useful but not
1889       // too smart.
1890       //
1891       // If a DSO reference is resolved by a SharedSymbol, but the SharedSymbol
1892       // is overridden by a hidden visibility Defined (which is later discarded
1893       // due to GC), don't report the diagnostic. However, this may indicate an
1894       // unintended SharedSymbol.
1895       for (SharedFile *file : ctx.sharedFiles) {
1896         bool allNeededIsKnown =
1897             llvm::all_of(file->dtNeeded, [&](StringRef needed) {
1898               return ctx.symtab->soNames.count(CachedHashStringRef(needed));
1899             });
1900         if (!allNeededIsKnown)
1901           continue;
1902         for (Symbol *sym : file->requiredSymbols) {
1903           if (sym->dsoDefined)
1904             continue;
1905           if (sym->isUndefined() && !sym->isWeak()) {
1906             ELFSyncStream(ctx, diag)
1907                 << "undefined reference: " << sym << "\n>>> referenced by "
1908                 << file << " (disallowed by --no-allow-shlib-undefined)";
1909           } else if (sym->isDefined() &&
1910                      sym->computeBinding(ctx) == STB_LOCAL) {
1911             ELFSyncStream(ctx, diag)
1912                 << "non-exported symbol '" << sym << "' in '" << sym->file
1913                 << "' is referenced by DSO '" << file << "'";
1914           }
1915         }
1916       }
1917     }
1918   }
1919 
1920   {
1921     llvm::TimeTraceScope timeScope("Add symbols to symtabs");
1922     // Now that we have defined all possible global symbols including linker-
1923     // synthesized ones. Visit all symbols to give the finishing touches.
1924     for (Symbol *sym : ctx.symtab->getSymbols()) {
1925       if (!sym->isUsedInRegularObj || !includeInSymtab(ctx, *sym))
1926         continue;
1927       if (!ctx.arg.relocatable)
1928         sym->binding = sym->computeBinding(ctx);
1929       if (ctx.in.symTab)
1930         ctx.in.symTab->addSymbol(sym);
1931 
1932       // computeBinding might localize a linker-synthesized hidden symbol
1933       // (e.g. __global_pointer$) that was considered exported.
1934       if (sym->isExported && !sym->isLocal()) {
1935         ctx.partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
1936         if (auto *file = dyn_cast<SharedFile>(sym->file))
1937           if (file->isNeeded && !sym->isUndefined())
1938             addVerneed(ctx, *sym);
1939       }
1940     }
1941 
1942     // We also need to scan the dynamic relocation tables of the other
1943     // partitions and add any referenced symbols to the partition's dynsym.
1944     for (Partition &part :
1945          MutableArrayRef<Partition>(ctx.partitions).slice(1)) {
1946       DenseSet<Symbol *> syms;
1947       for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
1948         syms.insert(e.sym);
1949       for (DynamicReloc &reloc : part.relaDyn->relocs)
1950         if (reloc.sym && reloc.needsDynSymIndex() &&
1951             syms.insert(reloc.sym).second)
1952           part.dynSymTab->addSymbol(reloc.sym);
1953     }
1954   }
1955 
1956   if (ctx.in.mipsGot)
1957     ctx.in.mipsGot->build();
1958 
1959   removeUnusedSyntheticSections(ctx);
1960   ctx.script->diagnoseOrphanHandling();
1961   ctx.script->diagnoseMissingSGSectionAddress();
1962 
1963   sortSections();
1964 
1965   // Create a list of OutputSections, assign sectionIndex, and populate
1966   // ctx.in.shStrTab. If -z nosectionheader is specified, drop non-ALLOC
1967   // sections.
1968   for (SectionCommand *cmd : ctx.script->sectionCommands)
1969     if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
1970       OutputSection *osec = &osd->osec;
1971       if (!ctx.in.shStrTab && !(osec->flags & SHF_ALLOC))
1972         continue;
1973       ctx.outputSections.push_back(osec);
1974       osec->sectionIndex = ctx.outputSections.size();
1975       if (ctx.in.shStrTab)
1976         osec->shName = ctx.in.shStrTab->addString(osec->name);
1977     }
1978 
1979   // Prefer command line supplied address over other constraints.
1980   for (OutputSection *sec : ctx.outputSections) {
1981     auto i = ctx.arg.sectionStartMap.find(sec->name);
1982     if (i != ctx.arg.sectionStartMap.end())
1983       sec->addrExpr = [=] { return i->second; };
1984   }
1985 
1986   // With the ctx.outputSections available check for GDPLT relocations
1987   // and add __tls_get_addr symbol if needed.
1988   if (ctx.arg.emachine == EM_HEXAGON &&
1989       hexagonNeedsTLSSymbol(ctx.outputSections)) {
1990     Symbol *sym =
1991         ctx.symtab->addSymbol(Undefined{ctx.internalFile, "__tls_get_addr",
1992                                         STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});
1993     sym->isPreemptible = true;
1994     ctx.partitions[0].dynSymTab->addSymbol(sym);
1995   }
1996 
1997   // This is a bit of a hack. A value of 0 means undef, so we set it
1998   // to 1 to make __ehdr_start defined. The section number is not
1999   // particularly relevant.
2000   ctx.out.elfHeader->sectionIndex = 1;
2001   ctx.out.elfHeader->size = sizeof(typename ELFT::Ehdr);
2002 
2003   // Binary and relocatable output does not have PHDRS.
2004   // The headers have to be created before finalize as that can influence the
2005   // image base and the dynamic section on mips includes the image base.
2006   if (!ctx.arg.relocatable && !ctx.arg.oFormatBinary) {
2007     for (Partition &part : ctx.partitions) {
2008       part.phdrs = ctx.script->hasPhdrsCommands() ? ctx.script->createPhdrs()
2009                                                   : createPhdrs(part);
2010       if (ctx.arg.emachine == EM_ARM) {
2011         // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
2012         addPhdrForSection(part, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
2013       }
2014       if (ctx.arg.emachine == EM_MIPS) {
2015         // Add separate segments for MIPS-specific sections.
2016         addPhdrForSection(part, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
2017         addPhdrForSection(part, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
2018         addPhdrForSection(part, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
2019       }
2020       if (ctx.arg.emachine == EM_RISCV)
2021         addPhdrForSection(part, SHT_RISCV_ATTRIBUTES, PT_RISCV_ATTRIBUTES,
2022                           PF_R);
2023     }
2024     ctx.out.programHeaders->size =
2025         sizeof(Elf_Phdr) * ctx.mainPart->phdrs.size();
2026 
2027     // Find the TLS segment. This happens before the section layout loop so that
2028     // Android relocation packing can look up TLS symbol addresses. We only need
2029     // to care about the main partition here because all TLS symbols were moved
2030     // to the main partition (see MarkLive.cpp).
2031     for (auto &p : ctx.mainPart->phdrs)
2032       if (p->p_type == PT_TLS)
2033         ctx.tlsPhdr = p.get();
2034   }
2035 
2036   // Some symbols are defined in term of program headers. Now that we
2037   // have the headers, we can find out which sections they point to.
2038   setReservedSymbolSections();
2039 
2040   if (ctx.script->noCrossRefs.size()) {
2041     llvm::TimeTraceScope timeScope("Check NOCROSSREFS");
2042     checkNoCrossRefs<ELFT>(ctx);
2043   }
2044 
2045   {
2046     llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2047 
2048     finalizeSynthetic(ctx, ctx.in.bss.get());
2049     finalizeSynthetic(ctx, ctx.in.bssRelRo.get());
2050     finalizeSynthetic(ctx, ctx.in.symTabShndx.get());
2051     finalizeSynthetic(ctx, ctx.in.shStrTab.get());
2052     finalizeSynthetic(ctx, ctx.in.strTab.get());
2053     finalizeSynthetic(ctx, ctx.in.got.get());
2054     finalizeSynthetic(ctx, ctx.in.mipsGot.get());
2055     finalizeSynthetic(ctx, ctx.in.igotPlt.get());
2056     finalizeSynthetic(ctx, ctx.in.gotPlt.get());
2057     finalizeSynthetic(ctx, ctx.in.relaPlt.get());
2058     finalizeSynthetic(ctx, ctx.in.plt.get());
2059     finalizeSynthetic(ctx, ctx.in.iplt.get());
2060     finalizeSynthetic(ctx, ctx.in.ppc32Got2.get());
2061     finalizeSynthetic(ctx, ctx.in.partIndex.get());
2062 
2063     // Dynamic section must be the last one in this list and dynamic
2064     // symbol table section (dynSymTab) must be the first one.
2065     for (Partition &part : ctx.partitions) {
2066       if (part.relaDyn) {
2067         part.relaDyn->mergeRels();
2068         // Compute DT_RELACOUNT to be used by part.dynamic.
2069         part.relaDyn->partitionRels();
2070         finalizeSynthetic(ctx, part.relaDyn.get());
2071       }
2072       if (part.relrDyn) {
2073         part.relrDyn->mergeRels();
2074         finalizeSynthetic(ctx, part.relrDyn.get());
2075       }
2076       if (part.relrAuthDyn) {
2077         part.relrAuthDyn->mergeRels();
2078         finalizeSynthetic(ctx, part.relrAuthDyn.get());
2079       }
2080 
2081       finalizeSynthetic(ctx, part.dynSymTab.get());
2082       finalizeSynthetic(ctx, part.gnuHashTab.get());
2083       finalizeSynthetic(ctx, part.hashTab.get());
2084       finalizeSynthetic(ctx, part.verDef.get());
2085       finalizeSynthetic(ctx, part.ehFrameHdr.get());
2086       finalizeSynthetic(ctx, part.verSym.get());
2087       finalizeSynthetic(ctx, part.verNeed.get());
2088       finalizeSynthetic(ctx, part.dynamic.get());
2089     }
2090   }
2091 
2092   if (!ctx.script->hasSectionsCommand && !ctx.arg.relocatable)
2093     fixSectionAlignments();
2094 
2095   // This is used to:
2096   // 1) Create "thunks":
2097   //    Jump instructions in many ISAs have small displacements, and therefore
2098   //    they cannot jump to arbitrary addresses in memory. For example, RISC-V
2099   //    JAL instruction can target only +-1 MiB from PC. It is a linker's
2100   //    responsibility to create and insert small pieces of code between
2101   //    sections to extend the ranges if jump targets are out of range. Such
2102   //    code pieces are called "thunks".
2103   //
2104   //    We add thunks at this stage. We couldn't do this before this point
2105   //    because this is the earliest point where we know sizes of sections and
2106   //    their layouts (that are needed to determine if jump targets are in
2107   //    range).
2108   //
2109   // 2) Update the sections. We need to generate content that depends on the
2110   //    address of InputSections. For example, MIPS GOT section content or
2111   //    android packed relocations sections content.
2112   //
2113   // 3) Assign the final values for the linker script symbols. Linker scripts
2114   //    sometimes using forward symbol declarations. We want to set the correct
2115   //    values. They also might change after adding the thunks.
2116   finalizeAddressDependentContent();
2117 
2118   // All information needed for OutputSection part of Map file is available.
2119   if (errCount(ctx))
2120     return;
2121 
2122   {
2123     llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2124     // finalizeAddressDependentContent may have added local symbols to the
2125     // static symbol table.
2126     finalizeSynthetic(ctx, ctx.in.symTab.get());
2127     finalizeSynthetic(ctx, ctx.in.debugNames.get());
2128     finalizeSynthetic(ctx, ctx.in.ppc64LongBranchTarget.get());
2129     finalizeSynthetic(ctx, ctx.in.armCmseSGSection.get());
2130   }
2131 
2132   // Relaxation to delete inter-basic block jumps created by basic block
2133   // sections. Run after ctx.in.symTab is finalized as optimizeBasicBlockJumps
2134   // can relax jump instructions based on symbol offset.
2135   if (ctx.arg.optimizeBBJumps)
2136     optimizeBasicBlockJumps();
2137 
2138   // Fill other section headers. The dynamic table is finalized
2139   // at the end because some tags like RELSZ depend on result
2140   // of finalizing other sections.
2141   for (OutputSection *sec : ctx.outputSections)
2142     sec->finalize(ctx);
2143 
2144   ctx.script->checkFinalScriptConditions();
2145 
2146   if (ctx.arg.emachine == EM_ARM && !ctx.arg.isLE && ctx.arg.armBe8) {
2147     addArmInputSectionMappingSymbols(ctx);
2148     sortArmMappingSymbols(ctx);
2149   }
2150 }
2151 
2152 // Ensure data sections are not mixed with executable sections when
2153 // --execute-only is used. --execute-only make pages executable but not
2154 // readable.
2155 template <class ELFT> void Writer<ELFT>::checkExecuteOnly() {
2156   if (!ctx.arg.executeOnly)
2157     return;
2158 
2159   SmallVector<InputSection *, 0> storage;
2160   for (OutputSection *osec : ctx.outputSections)
2161     if (osec->flags & SHF_EXECINSTR)
2162       for (InputSection *isec : getInputSections(*osec, storage))
2163         if (!(isec->flags & SHF_EXECINSTR))
2164           ErrAlways(ctx) << "cannot place " << isec << " into " << osec->name
2165                          << ": --execute-only does not support intermingling "
2166                             "data and code";
2167 }
2168 
2169 // The linker is expected to define SECNAME_start and SECNAME_end
2170 // symbols for a few sections. This function defines them.
2171 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
2172   // If the associated output section does not exist, there is ambiguity as to
2173   // how we define _start and _end symbols for an init/fini section. Users
2174   // expect no "undefined symbol" linker errors and loaders expect equal
2175   // st_value but do not particularly care whether the symbols are defined or
2176   // not. We retain the output section so that the section indexes will be
2177   // correct.
2178   auto define = [=](StringRef start, StringRef end, OutputSection *os) {
2179     if (os) {
2180       Defined *startSym = addOptionalRegular(ctx, start, os, 0);
2181       Defined *stopSym = addOptionalRegular(ctx, end, os, -1);
2182       if (startSym || stopSym)
2183         os->usedInExpression = true;
2184     } else {
2185       addOptionalRegular(ctx, start, ctx.out.elfHeader.get(), 0);
2186       addOptionalRegular(ctx, end, ctx.out.elfHeader.get(), 0);
2187     }
2188   };
2189 
2190   define("__preinit_array_start", "__preinit_array_end", ctx.out.preinitArray);
2191   define("__init_array_start", "__init_array_end", ctx.out.initArray);
2192   define("__fini_array_start", "__fini_array_end", ctx.out.finiArray);
2193 
2194   // As a special case, don't unnecessarily retain .ARM.exidx, which would
2195   // create an empty PT_ARM_EXIDX.
2196   if (OutputSection *sec = findSection(ctx, ".ARM.exidx"))
2197     define("__exidx_start", "__exidx_end", sec);
2198 }
2199 
2200 // If a section name is valid as a C identifier (which is rare because of
2201 // the leading '.'), linkers are expected to define __start_<secname> and
2202 // __stop_<secname> symbols. They are at beginning and end of the section,
2203 // respectively. This is not requested by the ELF standard, but GNU ld and
2204 // gold provide the feature, and used by many programs.
2205 template <class ELFT>
2206 void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) {
2207   StringRef s = osec.name;
2208   if (!isValidCIdentifier(s))
2209     return;
2210   StringSaver &ss = ctx.saver;
2211   Defined *startSym = addOptionalRegular(ctx, ss.save("__start_" + s), &osec, 0,
2212                                          ctx.arg.zStartStopVisibility);
2213   Defined *stopSym = addOptionalRegular(ctx, ss.save("__stop_" + s), &osec, -1,
2214                                         ctx.arg.zStartStopVisibility);
2215   if (startSym || stopSym)
2216     osec.usedInExpression = true;
2217 }
2218 
2219 static bool needsPtLoad(OutputSection *sec) {
2220   if (!(sec->flags & SHF_ALLOC))
2221     return false;
2222 
2223   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
2224   // responsible for allocating space for them, not the PT_LOAD that
2225   // contains the TLS initialization image.
2226   if ((sec->flags & SHF_TLS) && sec->type == SHT_NOBITS)
2227     return false;
2228   return true;
2229 }
2230 
2231 // Adjust phdr flags according to certain options.
2232 static uint64_t computeFlags(Ctx &ctx, uint64_t flags) {
2233   if (ctx.arg.omagic)
2234     return PF_R | PF_W | PF_X;
2235   if (ctx.arg.executeOnly && (flags & PF_X))
2236     return flags & ~PF_R;
2237   return flags;
2238 }
2239 
2240 // Decide which program headers to create and which sections to include in each
2241 // one.
2242 template <class ELFT>
2243 SmallVector<std::unique_ptr<PhdrEntry>, 0>
2244 Writer<ELFT>::createPhdrs(Partition &part) {
2245   SmallVector<std::unique_ptr<PhdrEntry>, 0> ret;
2246   auto addHdr = [&, &ctx = ctx](unsigned type, unsigned flags) -> PhdrEntry * {
2247     ret.push_back(std::make_unique<PhdrEntry>(ctx, type, flags));
2248     return ret.back().get();
2249   };
2250 
2251   unsigned partNo = part.getNumber(ctx);
2252   bool isMain = partNo == 1;
2253 
2254   // Add the first PT_LOAD segment for regular output sections.
2255   uint64_t flags = computeFlags(ctx, PF_R);
2256   PhdrEntry *load = nullptr;
2257 
2258   // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
2259   // PT_LOAD.
2260   if (!ctx.arg.nmagic && !ctx.arg.omagic) {
2261     // The first phdr entry is PT_PHDR which describes the program header
2262     // itself.
2263     if (isMain)
2264       addHdr(PT_PHDR, PF_R)->add(ctx.out.programHeaders.get());
2265     else
2266       addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());
2267 
2268     // PT_INTERP must be the second entry if exists.
2269     if (OutputSection *cmd = findSection(ctx, ".interp", partNo))
2270       addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);
2271 
2272     // Add the headers. We will remove them if they don't fit.
2273     // In the other partitions the headers are ordinary sections, so they don't
2274     // need to be added here.
2275     if (isMain) {
2276       load = addHdr(PT_LOAD, flags);
2277       load->add(ctx.out.elfHeader.get());
2278       load->add(ctx.out.programHeaders.get());
2279     }
2280   }
2281 
2282   // PT_GNU_RELRO includes all sections that should be marked as
2283   // read-only by dynamic linker after processing relocations.
2284   // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give
2285   // an error message if more than one PT_GNU_RELRO PHDR is required.
2286   auto relRo = std::make_unique<PhdrEntry>(ctx, PT_GNU_RELRO, PF_R);
2287   bool inRelroPhdr = false;
2288   OutputSection *relroEnd = nullptr;
2289   for (OutputSection *sec : ctx.outputSections) {
2290     if (sec->partition != partNo || !needsPtLoad(sec))
2291       continue;
2292     if (isRelroSection(ctx, sec)) {
2293       inRelroPhdr = true;
2294       if (!relroEnd)
2295         relRo->add(sec);
2296       else
2297         ErrAlways(ctx) << "section: " << sec->name
2298                        << " is not contiguous with other relro" << " sections";
2299     } else if (inRelroPhdr) {
2300       inRelroPhdr = false;
2301       relroEnd = sec;
2302     }
2303   }
2304   relRo->p_align = 1;
2305 
2306   for (OutputSection *sec : ctx.outputSections) {
2307     if (!needsPtLoad(sec))
2308       continue;
2309 
2310     // Normally, sections in partitions other than the current partition are
2311     // ignored. But partition number 255 is a special case: it contains the
2312     // partition end marker (.part.end). It needs to be added to the main
2313     // partition so that a segment is created for it in the main partition,
2314     // which will cause the dynamic loader to reserve space for the other
2315     // partitions.
2316     if (sec->partition != partNo) {
2317       if (isMain && sec->partition == 255)
2318         addHdr(PT_LOAD, computeFlags(ctx, sec->getPhdrFlags()))->add(sec);
2319       continue;
2320     }
2321 
2322     // Segments are contiguous memory regions that has the same attributes
2323     // (e.g. executable or writable). There is one phdr for each segment.
2324     // Therefore, we need to create a new phdr when the next section has
2325     // incompatible flags or is loaded at a discontiguous address or memory
2326     // region using AT or AT> linker script command, respectively.
2327     //
2328     // As an exception, we don't create a separate load segment for the ELF
2329     // headers, even if the first "real" output has an AT or AT> attribute.
2330     //
2331     // In addition, NOBITS sections should only be placed at the end of a LOAD
2332     // segment (since it's represented as p_filesz < p_memsz). If we have a
2333     // not-NOBITS section after a NOBITS, we create a new LOAD for the latter
2334     // even if flags match, so as not to require actually writing the
2335     // supposed-to-be-NOBITS section to the output file. (However, we cannot do
2336     // so when hasSectionsCommand, since we cannot introduce the extra alignment
2337     // needed to create a new LOAD)
2338     uint64_t newFlags = computeFlags(ctx, sec->getPhdrFlags());
2339     // When --no-rosegment is specified, RO and RX sections are compatible.
2340     uint32_t incompatible = flags ^ newFlags;
2341     if (ctx.arg.singleRoRx && !(newFlags & PF_W))
2342       incompatible &= ~PF_X;
2343     if (incompatible)
2344       load = nullptr;
2345 
2346     bool sameLMARegion =
2347         load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion;
2348     if (load && sec != relroEnd &&
2349         sec->memRegion == load->firstSec->memRegion &&
2350         (sameLMARegion || load->lastSec == ctx.out.programHeaders.get()) &&
2351         (ctx.script->hasSectionsCommand || sec->type == SHT_NOBITS ||
2352          load->lastSec->type != SHT_NOBITS)) {
2353       load->p_flags |= newFlags;
2354     } else {
2355       load = addHdr(PT_LOAD, newFlags);
2356       flags = newFlags;
2357     }
2358 
2359     load->add(sec);
2360   }
2361 
2362   // Add a TLS segment if any.
2363   auto tlsHdr = std::make_unique<PhdrEntry>(ctx, PT_TLS, PF_R);
2364   for (OutputSection *sec : ctx.outputSections)
2365     if (sec->partition == partNo && sec->flags & SHF_TLS)
2366       tlsHdr->add(sec);
2367   if (tlsHdr->firstSec)
2368     ret.push_back(std::move(tlsHdr));
2369 
2370   // Add an entry for .dynamic.
2371   if (OutputSection *sec = part.dynamic->getParent())
2372     addHdr(PT_DYNAMIC, sec->getPhdrFlags())->add(sec);
2373 
2374   if (relRo->firstSec)
2375     ret.push_back(std::move(relRo));
2376 
2377   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
2378   if (part.ehFrame->isNeeded() && part.ehFrameHdr &&
2379       part.ehFrame->getParent() && part.ehFrameHdr->getParent())
2380     addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())
2381         ->add(part.ehFrameHdr->getParent());
2382 
2383   if (ctx.arg.osabi == ELFOSABI_OPENBSD) {
2384     // PT_OPENBSD_MUTABLE makes the dynamic linker fill the segment with
2385     // zero data, like bss, but it can be treated differently.
2386     if (OutputSection *cmd = findSection(ctx, ".openbsd.mutable", partNo))
2387       addHdr(PT_OPENBSD_MUTABLE, cmd->getPhdrFlags())->add(cmd);
2388 
2389     // PT_OPENBSD_RANDOMIZE makes the dynamic linker fill the segment
2390     // with random data.
2391     if (OutputSection *cmd = findSection(ctx, ".openbsd.randomdata", partNo))
2392       addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
2393 
2394     // PT_OPENBSD_SYSCALLS makes the kernel and dynamic linker register
2395     // system call sites.
2396     if (OutputSection *cmd = findSection(ctx, ".openbsd.syscalls", partNo))
2397       addHdr(PT_OPENBSD_SYSCALLS, cmd->getPhdrFlags())->add(cmd);
2398   }
2399 
2400   if (ctx.arg.zGnustack != GnuStackKind::None) {
2401     // PT_GNU_STACK is a special section to tell the loader to make the
2402     // pages for the stack non-executable. If you really want an executable
2403     // stack, you can pass -z execstack, but that's not recommended for
2404     // security reasons.
2405     unsigned perm = PF_R | PF_W;
2406     if (ctx.arg.zGnustack == GnuStackKind::Exec)
2407       perm |= PF_X;
2408     addHdr(PT_GNU_STACK, perm)->p_memsz = ctx.arg.zStackSize;
2409   }
2410 
2411   // PT_OPENBSD_NOBTCFI is an OpenBSD-specific header to mark that the
2412   // executable is expected to violate branch-target CFI checks.
2413   if (ctx.arg.zNoBtCfi)
2414     addHdr(PT_OPENBSD_NOBTCFI, PF_X);
2415 
2416   // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
2417   // is expected to perform W^X violations, such as calling mprotect(2) or
2418   // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
2419   // OpenBSD.
2420   if (ctx.arg.zWxneeded)
2421     addHdr(PT_OPENBSD_WXNEEDED, PF_X);
2422 
2423   if (OutputSection *cmd = findSection(ctx, ".note.gnu.property", partNo))
2424     addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd);
2425 
2426   // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the
2427   // same alignment.
2428   PhdrEntry *note = nullptr;
2429   for (OutputSection *sec : ctx.outputSections) {
2430     if (sec->partition != partNo)
2431       continue;
2432     if (sec->type == SHT_NOTE && (sec->flags & SHF_ALLOC)) {
2433       if (!note || sec->lmaExpr || note->lastSec->addralign != sec->addralign)
2434         note = addHdr(PT_NOTE, PF_R);
2435       note->add(sec);
2436     } else {
2437       note = nullptr;
2438     }
2439   }
2440   return ret;
2441 }
2442 
2443 template <class ELFT>
2444 void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType,
2445                                      unsigned pType, unsigned pFlags) {
2446   unsigned partNo = part.getNumber(ctx);
2447   auto i = llvm::find_if(ctx.outputSections, [=](OutputSection *cmd) {
2448     return cmd->partition == partNo && cmd->type == shType;
2449   });
2450   if (i == ctx.outputSections.end())
2451     return;
2452 
2453   auto entry = std::make_unique<PhdrEntry>(ctx, pType, pFlags);
2454   entry->add(*i);
2455   part.phdrs.push_back(std::move(entry));
2456 }
2457 
2458 // Place the first section of each PT_LOAD to a different page (of maxPageSize).
2459 // This is achieved by assigning an alignment expression to addrExpr of each
2460 // such section.
2461 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
2462   const PhdrEntry *prev;
2463   auto pageAlign = [&, &ctx = this->ctx](const PhdrEntry *p) {
2464     OutputSection *cmd = p->firstSec;
2465     if (!cmd)
2466       return;
2467     cmd->alignExpr = [align = cmd->addralign]() { return align; };
2468     if (!cmd->addrExpr) {
2469       // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid
2470       // padding in the file contents.
2471       //
2472       // When -z separate-code is used we must not have any overlap in pages
2473       // between an executable segment and a non-executable segment. We align to
2474       // the next maximum page size boundary on transitions between executable
2475       // and non-executable segments.
2476       //
2477       // SHT_LLVM_PART_EHDR marks the start of a partition. The partition
2478       // sections will be extracted to a separate file. Align to the next
2479       // maximum page size boundary so that we can find the ELF header at the
2480       // start. We cannot benefit from overlapping p_offset ranges with the
2481       // previous segment anyway.
2482       if (ctx.arg.zSeparate == SeparateSegmentKind::Loadable ||
2483           (ctx.arg.zSeparate == SeparateSegmentKind::Code && prev &&
2484            (prev->p_flags & PF_X) != (p->p_flags & PF_X)) ||
2485           cmd->type == SHT_LLVM_PART_EHDR)
2486         cmd->addrExpr = [&ctx = this->ctx] {
2487           return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize);
2488         };
2489       // PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS,
2490       // it must be the RW. Align to p_align(PT_TLS) to make sure
2491       // p_vaddr(PT_LOAD)%p_align(PT_LOAD) = 0. Otherwise, if
2492       // sh_addralign(.tdata) < sh_addralign(.tbss), we will set p_align(PT_TLS)
2493       // to sh_addralign(.tbss), while p_vaddr(PT_TLS)=p_vaddr(PT_LOAD) may not
2494       // be congruent to 0 modulo p_align(PT_TLS).
2495       //
2496       // Technically this is not required, but as of 2019, some dynamic loaders
2497       // don't handle p_vaddr%p_align != 0 correctly, e.g. glibc (i386 and
2498       // x86-64) doesn't make runtime address congruent to p_vaddr modulo
2499       // p_align for dynamic TLS blocks (PR/24606), FreeBSD rtld has the same
2500       // bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS
2501       // blocks correctly. We need to keep the workaround for a while.
2502       else if (ctx.tlsPhdr && ctx.tlsPhdr->firstSec == p->firstSec)
2503         cmd->addrExpr = [&ctx] {
2504           return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize) +
2505                  alignToPowerOf2(ctx.script->getDot() % ctx.arg.maxPageSize,
2506                                  ctx.tlsPhdr->p_align);
2507         };
2508       else
2509         cmd->addrExpr = [&ctx] {
2510           return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize) +
2511                  ctx.script->getDot() % ctx.arg.maxPageSize;
2512         };
2513     }
2514   };
2515 
2516   for (Partition &part : ctx.partitions) {
2517     prev = nullptr;
2518     for (auto &p : part.phdrs)
2519       if (p->p_type == PT_LOAD && p->firstSec) {
2520         pageAlign(p.get());
2521         prev = p.get();
2522       }
2523   }
2524 }
2525 
2526 // Compute an in-file position for a given section. The file offset must be the
2527 // same with its virtual address modulo the page size, so that the loader can
2528 // load executables without any address adjustment.
2529 static uint64_t computeFileOffset(Ctx &ctx, OutputSection *os, uint64_t off) {
2530   // The first section in a PT_LOAD has to have congruent offset and address
2531   // modulo the maximum page size.
2532   if (os->ptLoad && os->ptLoad->firstSec == os)
2533     return alignTo(off, os->ptLoad->p_align, os->addr);
2534 
2535   // File offsets are not significant for .bss sections other than the first one
2536   // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically
2537   // increasing rather than setting to zero.
2538   if (os->type == SHT_NOBITS && (!ctx.tlsPhdr || ctx.tlsPhdr->firstSec != os))
2539     return off;
2540 
2541   // If the section is not in a PT_LOAD, we just have to align it.
2542   if (!os->ptLoad)
2543      return alignToPowerOf2(off, os->addralign);
2544 
2545   // If two sections share the same PT_LOAD the file offset is calculated
2546   // using this formula: Off2 = Off1 + (VA2 - VA1).
2547   OutputSection *first = os->ptLoad->firstSec;
2548   return first->offset + os->addr - first->addr;
2549 }
2550 
2551 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
2552   // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr.
2553   auto needsOffset = [](OutputSection &sec) {
2554     return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;
2555   };
2556   uint64_t minAddr = UINT64_MAX;
2557   for (OutputSection *sec : ctx.outputSections)
2558     if (needsOffset(*sec)) {
2559       sec->offset = sec->getLMA();
2560       minAddr = std::min(minAddr, sec->offset);
2561     }
2562 
2563   // Sections are laid out at LMA minus minAddr.
2564   fileSize = 0;
2565   for (OutputSection *sec : ctx.outputSections)
2566     if (needsOffset(*sec)) {
2567       sec->offset -= minAddr;
2568       fileSize = std::max(fileSize, sec->offset + sec->size);
2569     }
2570 }
2571 
2572 static std::string rangeToString(uint64_t addr, uint64_t len) {
2573   return "[0x" + utohexstr(addr) + ", 0x" + utohexstr(addr + len - 1) + "]";
2574 }
2575 
2576 // Assign file offsets to output sections.
2577 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
2578   ctx.out.programHeaders->offset = ctx.out.elfHeader->size;
2579   uint64_t off = ctx.out.elfHeader->size + ctx.out.programHeaders->size;
2580 
2581   PhdrEntry *lastRX = nullptr;
2582   for (Partition &part : ctx.partitions)
2583     for (auto &p : part.phdrs)
2584       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
2585         lastRX = p.get();
2586 
2587   // Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC
2588   // will not occupy file offsets contained by a PT_LOAD.
2589   for (OutputSection *sec : ctx.outputSections) {
2590     if (!(sec->flags & SHF_ALLOC))
2591       continue;
2592     off = computeFileOffset(ctx, sec, off);
2593     sec->offset = off;
2594     if (sec->type != SHT_NOBITS)
2595       off += sec->size;
2596 
2597     // If this is a last section of the last executable segment and that
2598     // segment is the last loadable segment, align the offset of the
2599     // following section to avoid loading non-segments parts of the file.
2600     if (ctx.arg.zSeparate != SeparateSegmentKind::None && lastRX &&
2601         lastRX->lastSec == sec)
2602       off = alignToPowerOf2(off, ctx.arg.maxPageSize);
2603   }
2604   for (OutputSection *osec : ctx.outputSections) {
2605     if (osec->flags & SHF_ALLOC)
2606       continue;
2607     osec->offset = alignToPowerOf2(off, osec->addralign);
2608     off = osec->offset + osec->size;
2609   }
2610 
2611   sectionHeaderOff = alignToPowerOf2(off, ctx.arg.wordsize);
2612   fileSize =
2613       sectionHeaderOff + (ctx.outputSections.size() + 1) * sizeof(Elf_Shdr);
2614 
2615   // Our logic assumes that sections have rising VA within the same segment.
2616   // With use of linker scripts it is possible to violate this rule and get file
2617   // offset overlaps or overflows. That should never happen with a valid script
2618   // which does not move the location counter backwards and usually scripts do
2619   // not do that. Unfortunately, there are apps in the wild, for example, Linux
2620   // kernel, which control segment distribution explicitly and move the counter
2621   // backwards, so we have to allow doing that to support linking them. We
2622   // perform non-critical checks for overlaps in checkSectionOverlap(), but here
2623   // we want to prevent file size overflows because it would crash the linker.
2624   for (OutputSection *sec : ctx.outputSections) {
2625     if (sec->type == SHT_NOBITS)
2626       continue;
2627     if ((sec->offset > fileSize) || (sec->offset + sec->size > fileSize))
2628       ErrAlways(ctx) << "unable to place section " << sec->name
2629                      << " at file offset "
2630                      << rangeToString(sec->offset, sec->size)
2631                      << "; check your linker script for overflows";
2632   }
2633 }
2634 
2635 // Finalize the program headers. We call this function after we assign
2636 // file offsets and VAs to all sections.
2637 template <class ELFT> void Writer<ELFT>::setPhdrs(Partition &part) {
2638   for (std::unique_ptr<PhdrEntry> &p : part.phdrs) {
2639     OutputSection *first = p->firstSec;
2640     OutputSection *last = p->lastSec;
2641 
2642     // .ARM.exidx sections may not be within a single .ARM.exidx
2643     // output section. We always want to describe just the
2644     // SyntheticSection.
2645     if (part.armExidx && p->p_type == PT_ARM_EXIDX) {
2646       p->p_filesz = part.armExidx->getSize();
2647       p->p_memsz = p->p_filesz;
2648       p->p_offset = first->offset + part.armExidx->outSecOff;
2649       p->p_vaddr = first->addr + part.armExidx->outSecOff;
2650       p->p_align = part.armExidx->addralign;
2651       if (part.elfHeader)
2652         p->p_offset -= part.elfHeader->getParent()->offset;
2653 
2654       if (!p->hasLMA)
2655         p->p_paddr = first->getLMA() + part.armExidx->outSecOff;
2656       return;
2657     }
2658 
2659     if (first) {
2660       p->p_filesz = last->offset - first->offset;
2661       if (last->type != SHT_NOBITS)
2662         p->p_filesz += last->size;
2663 
2664       p->p_memsz = last->addr + last->size - first->addr;
2665       p->p_offset = first->offset;
2666       p->p_vaddr = first->addr;
2667 
2668       // File offsets in partitions other than the main partition are relative
2669       // to the offset of the ELF headers. Perform that adjustment now.
2670       if (part.elfHeader)
2671         p->p_offset -= part.elfHeader->getParent()->offset;
2672 
2673       if (!p->hasLMA)
2674         p->p_paddr = first->getLMA();
2675     }
2676   }
2677 }
2678 
2679 // A helper struct for checkSectionOverlap.
2680 namespace {
2681 struct SectionOffset {
2682   OutputSection *sec;
2683   uint64_t offset;
2684 };
2685 } // namespace
2686 
2687 // Check whether sections overlap for a specific address range (file offsets,
2688 // load and virtual addresses).
2689 static void checkOverlap(Ctx &ctx, StringRef name,
2690                          std::vector<SectionOffset> &sections,
2691                          bool isVirtualAddr) {
2692   llvm::sort(sections, [=](const SectionOffset &a, const SectionOffset &b) {
2693     return a.offset < b.offset;
2694   });
2695 
2696   // Finding overlap is easy given a vector is sorted by start position.
2697   // If an element starts before the end of the previous element, they overlap.
2698   for (size_t i = 1, end = sections.size(); i < end; ++i) {
2699     SectionOffset a = sections[i - 1];
2700     SectionOffset b = sections[i];
2701     if (b.offset >= a.offset + a.sec->size)
2702       continue;
2703 
2704     // If both sections are in OVERLAY we allow the overlapping of virtual
2705     // addresses, because it is what OVERLAY was designed for.
2706     if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay)
2707       continue;
2708 
2709     Err(ctx) << "section " << a.sec->name << " " << name
2710              << " range overlaps with " << b.sec->name << "\n>>> "
2711              << a.sec->name << " range is "
2712              << rangeToString(a.offset, a.sec->size) << "\n>>> " << b.sec->name
2713              << " range is " << rangeToString(b.offset, b.sec->size);
2714   }
2715 }
2716 
2717 // Check for overlapping sections and address overflows.
2718 //
2719 // In this function we check that none of the output sections have overlapping
2720 // file offsets. For SHF_ALLOC sections we also check that the load address
2721 // ranges and the virtual address ranges don't overlap
2722 template <class ELFT> void Writer<ELFT>::checkSections() {
2723   // First, check that section's VAs fit in available address space for target.
2724   for (OutputSection *os : ctx.outputSections)
2725     if ((os->addr + os->size < os->addr) ||
2726         (!ELFT::Is64Bits && os->addr + os->size > uint64_t(UINT32_MAX) + 1))
2727       Err(ctx) << "section " << os->name << " at 0x"
2728                << utohexstr(os->addr, true) << " of size 0x"
2729                << utohexstr(os->size, true)
2730                << " exceeds available address space";
2731 
2732   // Check for overlapping file offsets. In this case we need to skip any
2733   // section marked as SHT_NOBITS. These sections don't actually occupy space in
2734   // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat
2735   // binary is specified only add SHF_ALLOC sections are added to the output
2736   // file so we skip any non-allocated sections in that case.
2737   std::vector<SectionOffset> fileOffs;
2738   for (OutputSection *sec : ctx.outputSections)
2739     if (sec->size > 0 && sec->type != SHT_NOBITS &&
2740         (!ctx.arg.oFormatBinary || (sec->flags & SHF_ALLOC)))
2741       fileOffs.push_back({sec, sec->offset});
2742   checkOverlap(ctx, "file", fileOffs, false);
2743 
2744   // When linking with -r there is no need to check for overlapping virtual/load
2745   // addresses since those addresses will only be assigned when the final
2746   // executable/shared object is created.
2747   if (ctx.arg.relocatable)
2748     return;
2749 
2750   // Checking for overlapping virtual and load addresses only needs to take
2751   // into account SHF_ALLOC sections since others will not be loaded.
2752   // Furthermore, we also need to skip SHF_TLS sections since these will be
2753   // mapped to other addresses at runtime and can therefore have overlapping
2754   // ranges in the file.
2755   std::vector<SectionOffset> vmas;
2756   for (OutputSection *sec : ctx.outputSections)
2757     if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
2758       vmas.push_back({sec, sec->addr});
2759   checkOverlap(ctx, "virtual address", vmas, true);
2760 
2761   // Finally, check that the load addresses don't overlap. This will usually be
2762   // the same as the virtual addresses but can be different when using a linker
2763   // script with AT().
2764   std::vector<SectionOffset> lmas;
2765   for (OutputSection *sec : ctx.outputSections)
2766     if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
2767       lmas.push_back({sec, sec->getLMA()});
2768   checkOverlap(ctx, "load address", lmas, false);
2769 }
2770 
2771 // The entry point address is chosen in the following ways.
2772 //
2773 // 1. the '-e' entry command-line option;
2774 // 2. the ENTRY(symbol) command in a linker control script;
2775 // 3. the value of the symbol _start, if present;
2776 // 4. the number represented by the entry symbol, if it is a number;
2777 // 5. the address 0.
2778 static uint64_t getEntryAddr(Ctx &ctx) {
2779   // Case 1, 2 or 3
2780   if (Symbol *b = ctx.symtab->find(ctx.arg.entry))
2781     return b->getVA(ctx);
2782 
2783   // Case 4
2784   uint64_t addr;
2785   if (to_integer(ctx.arg.entry, addr))
2786     return addr;
2787 
2788   // Case 5
2789   if (ctx.arg.warnMissingEntry)
2790     Warn(ctx) << "cannot find entry symbol " << ctx.arg.entry
2791               << "; not setting start address";
2792   return 0;
2793 }
2794 
2795 static uint16_t getELFType(Ctx &ctx) {
2796   if (ctx.arg.isPic)
2797     return ET_DYN;
2798   if (ctx.arg.relocatable)
2799     return ET_REL;
2800   return ET_EXEC;
2801 }
2802 
2803 template <class ELFT> void Writer<ELFT>::writeHeader() {
2804   writeEhdr<ELFT>(ctx, ctx.bufferStart, *ctx.mainPart);
2805   writePhdrs<ELFT>(ctx.bufferStart + sizeof(Elf_Ehdr), *ctx.mainPart);
2806 
2807   auto *eHdr = reinterpret_cast<Elf_Ehdr *>(ctx.bufferStart);
2808   eHdr->e_type = getELFType(ctx);
2809   eHdr->e_entry = getEntryAddr(ctx);
2810 
2811   // If -z nosectionheader is specified, omit the section header table.
2812   if (!ctx.in.shStrTab)
2813     return;
2814   eHdr->e_shoff = sectionHeaderOff;
2815 
2816   // Write the section header table.
2817   //
2818   // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum
2819   // and e_shstrndx fields. When the value of one of these fields exceeds
2820   // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and
2821   // use fields in the section header at index 0 to store
2822   // the value. The sentinel values and fields are:
2823   // e_shnum = 0, SHdrs[0].sh_size = number of sections.
2824   // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index.
2825   auto *sHdrs = reinterpret_cast<Elf_Shdr *>(ctx.bufferStart + eHdr->e_shoff);
2826   size_t num = ctx.outputSections.size() + 1;
2827   if (num >= SHN_LORESERVE)
2828     sHdrs->sh_size = num;
2829   else
2830     eHdr->e_shnum = num;
2831 
2832   uint32_t strTabIndex = ctx.in.shStrTab->getParent()->sectionIndex;
2833   if (strTabIndex >= SHN_LORESERVE) {
2834     sHdrs->sh_link = strTabIndex;
2835     eHdr->e_shstrndx = SHN_XINDEX;
2836   } else {
2837     eHdr->e_shstrndx = strTabIndex;
2838   }
2839 
2840   for (OutputSection *sec : ctx.outputSections)
2841     sec->writeHeaderTo<ELFT>(++sHdrs);
2842 }
2843 
2844 // Open a result file.
2845 template <class ELFT> void Writer<ELFT>::openFile() {
2846   uint64_t maxSize = ctx.arg.is64 ? INT64_MAX : UINT32_MAX;
2847   if (fileSize != size_t(fileSize) || maxSize < fileSize) {
2848     std::string msg;
2849     raw_string_ostream s(msg);
2850     s << "output file too large: " << fileSize << " bytes\n"
2851       << "section sizes:\n";
2852     for (OutputSection *os : ctx.outputSections)
2853       s << os->name << ' ' << os->size << "\n";
2854     ErrAlways(ctx) << msg;
2855     return;
2856   }
2857 
2858   unlinkAsync(ctx.arg.outputFile);
2859   unsigned flags = 0;
2860   if (!ctx.arg.relocatable)
2861     flags |= FileOutputBuffer::F_executable;
2862   if (!ctx.arg.mmapOutputFile)
2863     flags |= FileOutputBuffer::F_no_mmap;
2864   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
2865       FileOutputBuffer::create(ctx.arg.outputFile, fileSize, flags);
2866 
2867   if (!bufferOrErr) {
2868     ErrAlways(ctx) << "failed to open " << ctx.arg.outputFile << ": "
2869                    << bufferOrErr.takeError();
2870     return;
2871   }
2872   buffer = std::move(*bufferOrErr);
2873   ctx.bufferStart = buffer->getBufferStart();
2874 }
2875 
2876 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
2877   parallel::TaskGroup tg;
2878   for (OutputSection *sec : ctx.outputSections)
2879     if (sec->flags & SHF_ALLOC)
2880       sec->writeTo<ELFT>(ctx, ctx.bufferStart + sec->offset, tg);
2881 }
2882 
2883 static void fillTrap(std::array<uint8_t, 4> trapInstr, uint8_t *i,
2884                      uint8_t *end) {
2885   for (; i + 4 <= end; i += 4)
2886     memcpy(i, trapInstr.data(), 4);
2887 }
2888 
2889 // Fill the last page of executable segments with trap instructions
2890 // instead of leaving them as zero. Even though it is not required by any
2891 // standard, it is in general a good thing to do for security reasons.
2892 //
2893 // We'll leave other pages in segments as-is because the rest will be
2894 // overwritten by output sections.
2895 template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
2896   for (Partition &part : ctx.partitions) {
2897     // Fill the last page.
2898     for (std::unique_ptr<PhdrEntry> &p : part.phdrs)
2899       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
2900         fillTrap(
2901             ctx.target->trapInstr,
2902             ctx.bufferStart + alignDown(p->firstSec->offset + p->p_filesz, 4),
2903             ctx.bufferStart + alignToPowerOf2(p->firstSec->offset + p->p_filesz,
2904                                               ctx.arg.maxPageSize));
2905 
2906     // Round up the file size of the last segment to the page boundary iff it is
2907     // an executable segment to ensure that other tools don't accidentally
2908     // trim the instruction padding (e.g. when stripping the file).
2909     PhdrEntry *last = nullptr;
2910     for (std::unique_ptr<PhdrEntry> &p : part.phdrs)
2911       if (p->p_type == PT_LOAD)
2912         last = p.get();
2913 
2914     if (last && (last->p_flags & PF_X))
2915       last->p_memsz = last->p_filesz =
2916           alignToPowerOf2(last->p_filesz, ctx.arg.maxPageSize);
2917   }
2918 }
2919 
2920 // Write section contents to a mmap'ed file.
2921 template <class ELFT> void Writer<ELFT>::writeSections() {
2922   llvm::TimeTraceScope timeScope("Write sections");
2923 
2924   {
2925     // In -r or --emit-relocs mode, write the relocation sections first as in
2926     // ELf_Rel targets we might find out that we need to modify the relocated
2927     // section while doing it.
2928     parallel::TaskGroup tg;
2929     for (OutputSection *sec : ctx.outputSections)
2930       if (isStaticRelSecType(sec->type))
2931         sec->writeTo<ELFT>(ctx, ctx.bufferStart + sec->offset, tg);
2932   }
2933   {
2934     parallel::TaskGroup tg;
2935     for (OutputSection *sec : ctx.outputSections)
2936       if (!isStaticRelSecType(sec->type))
2937         sec->writeTo<ELFT>(ctx, ctx.bufferStart + sec->offset, tg);
2938   }
2939 
2940   // Finally, check that all dynamic relocation addends were written correctly.
2941   if (ctx.arg.checkDynamicRelocs && ctx.arg.writeAddends) {
2942     for (OutputSection *sec : ctx.outputSections)
2943       if (isStaticRelSecType(sec->type))
2944         sec->checkDynRelAddends(ctx);
2945   }
2946 }
2947 
2948 // Computes a hash value of Data using a given hash function.
2949 // In order to utilize multiple cores, we first split data into 1MB
2950 // chunks, compute a hash for each chunk, and then compute a hash value
2951 // of the hash values.
2952 static void
2953 computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,
2954             llvm::ArrayRef<uint8_t> data,
2955             std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {
2956   std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024);
2957   const size_t hashesSize = chunks.size() * hashBuf.size();
2958   std::unique_ptr<uint8_t[]> hashes(new uint8_t[hashesSize]);
2959 
2960   // Compute hash values.
2961   parallelFor(0, chunks.size(), [&](size_t i) {
2962     hashFn(hashes.get() + i * hashBuf.size(), chunks[i]);
2963   });
2964 
2965   // Write to the final output buffer.
2966   hashFn(hashBuf.data(), ArrayRef(hashes.get(), hashesSize));
2967 }
2968 
2969 template <class ELFT> void Writer<ELFT>::writeBuildId() {
2970   if (!ctx.mainPart->buildId || !ctx.mainPart->buildId->getParent())
2971     return;
2972 
2973   if (ctx.arg.buildId == BuildIdKind::Hexstring) {
2974     for (Partition &part : ctx.partitions)
2975       part.buildId->writeBuildId(ctx.arg.buildIdVector);
2976     return;
2977   }
2978 
2979   // Compute a hash of all sections of the output file.
2980   size_t hashSize = ctx.mainPart->buildId->hashSize;
2981   std::unique_ptr<uint8_t[]> buildId(new uint8_t[hashSize]);
2982   MutableArrayRef<uint8_t> output(buildId.get(), hashSize);
2983   llvm::ArrayRef<uint8_t> input{ctx.bufferStart, size_t(fileSize)};
2984 
2985   // Fedora introduced build ID as "approximation of true uniqueness across all
2986   // binaries that might be used by overlapping sets of people". It does not
2987   // need some security goals that some hash algorithms strive to provide, e.g.
2988   // (second-)preimage and collision resistance. In practice people use 'md5'
2989   // and 'sha1' just for different lengths. Implement them with the more
2990   // efficient BLAKE3.
2991   switch (ctx.arg.buildId) {
2992   case BuildIdKind::Fast:
2993     computeHash(output, input, [](uint8_t *dest, ArrayRef<uint8_t> arr) {
2994       write64le(dest, xxh3_64bits(arr));
2995     });
2996     break;
2997   case BuildIdKind::Md5:
2998     computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
2999       memcpy(dest, BLAKE3::hash<16>(arr).data(), hashSize);
3000     });
3001     break;
3002   case BuildIdKind::Sha1:
3003     computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
3004       memcpy(dest, BLAKE3::hash<20>(arr).data(), hashSize);
3005     });
3006     break;
3007   case BuildIdKind::Uuid:
3008     if (auto ec = llvm::getRandomBytes(buildId.get(), hashSize))
3009       ErrAlways(ctx) << "entropy source failure: " << ec.message();
3010     break;
3011   default:
3012     llvm_unreachable("unknown BuildIdKind");
3013   }
3014   for (Partition &part : ctx.partitions)
3015     part.buildId->writeBuildId(output);
3016 }
3017 
3018 template void elf::writeResult<ELF32LE>(Ctx &);
3019 template void elf::writeResult<ELF32BE>(Ctx &);
3020 template void elf::writeResult<ELF64LE>(Ctx &);
3021 template void elf::writeResult<ELF64BE>(Ctx &);
3022