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