xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision 08ebd61a)
1 //===- Writer.cpp ---------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Writer.h"
11 #include "Config.h"
12 #include "LinkerScript.h"
13 #include "MapFile.h"
14 #include "Memory.h"
15 #include "OutputSections.h"
16 #include "Relocations.h"
17 #include "Strings.h"
18 #include "SymbolTable.h"
19 #include "SyntheticSections.h"
20 #include "Target.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Support/FileOutputBuffer.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <climits>
27 #include <thread>
28 
29 using namespace llvm;
30 using namespace llvm::ELF;
31 using namespace llvm::object;
32 using namespace llvm::support;
33 using namespace llvm::support::endian;
34 
35 using namespace lld;
36 using namespace lld::elf;
37 
38 namespace {
39 // The writer writes a SymbolTable result to a file.
40 template <class ELFT> class Writer {
41 public:
42   typedef typename ELFT::uint uintX_t;
43   typedef typename ELFT::Shdr Elf_Shdr;
44   typedef typename ELFT::Ehdr Elf_Ehdr;
45   typedef typename ELFT::Phdr Elf_Phdr;
46   typedef typename ELFT::Sym Elf_Sym;
47   typedef typename ELFT::SymRange Elf_Sym_Range;
48   typedef typename ELFT::Rela Elf_Rela;
49   void run();
50 
51 private:
52   void createSyntheticSections();
53   void copyLocalSymbols();
54   void addSectionSymbols();
55   void addReservedSymbols();
56   void createSections();
57   void forEachRelSec(std::function<void(InputSectionBase &)> Fn);
58   void sortSections();
59   void finalizeSections();
60   void addPredefinedSections();
61 
62   std::vector<PhdrEntry> createPhdrs();
63   void removeEmptyPTLoad();
64   void addPtArmExid(std::vector<PhdrEntry> &Phdrs);
65   void assignAddresses();
66   void assignFileOffsets();
67   void assignFileOffsetsBinary();
68   void setPhdrs();
69   void fixHeaders();
70   void fixSectionAlignments();
71   void fixPredefinedSymbols();
72   void openFile();
73   void writeHeader();
74   void writeSections();
75   void writeSectionsBinary();
76   void writeBuildId();
77 
78   std::unique_ptr<FileOutputBuffer> Buffer;
79 
80   std::vector<OutputSection *> OutputSections;
81   OutputSectionFactory Factory{OutputSections};
82 
83   void addRelIpltSymbols();
84   void addStartEndSymbols();
85   void addStartStopSymbols(OutputSection *Sec);
86   uintX_t getEntryAddr();
87   OutputSection *findSection(StringRef Name);
88 
89   std::vector<PhdrEntry> Phdrs;
90 
91   uintX_t FileSize;
92   uintX_t SectionHeaderOff;
93   bool AllocateHeader = true;
94 };
95 } // anonymous namespace
96 
97 StringRef elf::getOutputSectionName(StringRef Name) {
98   if (Config->Relocatable)
99     return Name;
100 
101   // If -emit-relocs is given (which is rare), we need to copy
102   // relocation sections to the output. If input section .foo is
103   // output as .bar, we want to rename .rel.foo .rel.bar as well.
104   if (Config->EmitRelocs) {
105     for (StringRef V : {".rel.", ".rela."}) {
106       if (Name.startswith(V)) {
107         StringRef Inner = getOutputSectionName(Name.substr(V.size() - 1));
108         return Saver.save(Twine(V.drop_back()) + Inner);
109       }
110     }
111   }
112 
113   for (StringRef V :
114        {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
115         ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
116         ".gcc_except_table.", ".tdata.", ".ARM.exidx."}) {
117     StringRef Prefix = V.drop_back();
118     if (Name.startswith(V) || Name == Prefix)
119       return Prefix;
120   }
121 
122   // CommonSection is identified as "COMMON" in linker scripts.
123   // By default, it should go to .bss section.
124   if (Name == "COMMON")
125     return ".bss";
126 
127   // ".zdebug_" is a prefix for ZLIB-compressed sections.
128   // Because we decompressed input sections, we want to remove 'z'.
129   if (Name.startswith(".zdebug_"))
130     return Saver.save(Twine(".") + Name.substr(2));
131   return Name;
132 }
133 
134 template <class ELFT> static bool needsInterpSection() {
135   return !Symtab<ELFT>::X->getSharedFiles().empty() &&
136          !Config->DynamicLinker.empty() && !Script->ignoreInterpSection();
137 }
138 
139 template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
140 
141 template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
142   auto I = std::remove_if(Phdrs.begin(), Phdrs.end(), [&](const PhdrEntry &P) {
143     if (P.p_type != PT_LOAD)
144       return false;
145     if (!P.First)
146       return true;
147     uintX_t Size = P.Last->Addr + P.Last->Size - P.First->Addr;
148     return Size == 0;
149   });
150   Phdrs.erase(I, Phdrs.end());
151 }
152 
153 template <class ELFT>
154 static typename ELFT::uint getOutFlags(InputSectionBase *S) {
155   return S->Flags & ~(typename ELFT::uint)(SHF_GROUP | SHF_COMPRESSED);
156 }
157 
158 // This function scans over the input sections and creates mergeable
159 // synthetic sections. It removes MergeInputSections from array and
160 // adds new synthetic ones. Each synthetic section is added to the
161 // location of the first input section it replaces.
162 template <class ELFT> static void combineMergableSections() {
163   typedef typename ELFT::uint uintX_t;
164 
165   std::vector<MergeSyntheticSection *> MergeSections;
166   for (InputSectionBase *&S : InputSections) {
167     MergeInputSection *MS = dyn_cast<MergeInputSection>(S);
168     if (!MS)
169       continue;
170 
171     // We do not want to handle sections that are not alive, so just remove
172     // them instead of trying to merge.
173     if (!MS->Live)
174       continue;
175 
176     StringRef OutsecName = getOutputSectionName(MS->Name);
177     uintX_t Flags = getOutFlags<ELFT>(MS);
178     uint32_t Alignment = std::max<uintX_t>(MS->Alignment, MS->Entsize);
179 
180     auto I =
181         llvm::find_if(MergeSections, [=](MergeSyntheticSection *Sec) {
182           return Sec->Name == OutsecName && Sec->Flags == Flags &&
183                  Sec->Alignment == Alignment;
184         });
185     if (I == MergeSections.end()) {
186       MergeSyntheticSection *Syn =
187           make<MergeSyntheticSection>(OutsecName, MS->Type, Flags, Alignment);
188       MergeSections.push_back(Syn);
189       I = std::prev(MergeSections.end());
190       S = Syn;
191     } else {
192       S = nullptr;
193     }
194     (*I)->addSection(MS);
195   }
196 
197   std::vector<InputSectionBase *> &V = InputSections;
198   V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
199 }
200 
201 template <class ELFT> static void combineEhFrameSections() {
202   for (InputSectionBase *&S : InputSections) {
203     EhInputSection *ES = dyn_cast<EhInputSection>(S);
204     if (!ES || !ES->Live)
205       continue;
206 
207     In<ELFT>::EhFrame->addSection(ES);
208     S = nullptr;
209   }
210 
211   std::vector<InputSectionBase *> &V = InputSections;
212   V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
213 }
214 
215 // The main function of the writer.
216 template <class ELFT> void Writer<ELFT>::run() {
217   // Create linker-synthesized sections such as .got or .plt.
218   // Such sections are of type input section.
219   createSyntheticSections();
220   combineMergableSections<ELFT>();
221 
222   if (!Config->Relocatable)
223     combineEhFrameSections<ELFT>();
224 
225   // We need to create some reserved symbols such as _end. Create them.
226   if (!Config->Relocatable)
227     addReservedSymbols();
228 
229   // Create output sections.
230   Script->OutputSections = &OutputSections;
231   if (ScriptConfig->HasSections) {
232     // If linker script contains SECTIONS commands, let it create sections.
233     Script->processCommands(Factory);
234 
235     // Linker scripts may have left some input sections unassigned.
236     // Assign such sections using the default rule.
237     Script->addOrphanSections(Factory);
238   } else {
239     // If linker script does not contain SECTIONS commands, create
240     // output sections by default rules. We still need to give the
241     // linker script a chance to run, because it might contain
242     // non-SECTIONS commands such as ASSERT.
243     createSections();
244     Script->processCommands(Factory);
245   }
246 
247   if (Config->Discard != DiscardPolicy::All)
248     copyLocalSymbols();
249 
250   if (Config->CopyRelocs)
251     addSectionSymbols();
252 
253   // Now that we have a complete set of output sections. This function
254   // completes section contents. For example, we need to add strings
255   // to the string table, and add entries to .got and .plt.
256   // finalizeSections does that.
257   finalizeSections();
258   if (ErrorCount)
259     return;
260 
261   if (Config->Relocatable) {
262     assignFileOffsets();
263   } else {
264     if (ScriptConfig->HasSections) {
265       Script->assignAddresses(Phdrs);
266     } else {
267       fixSectionAlignments();
268       assignAddresses();
269       Script->processNonSectionCommands();
270     }
271 
272     // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
273     // 0 sized region. This has to be done late since only after assignAddresses
274     // we know the size of the sections.
275     removeEmptyPTLoad();
276 
277     if (!Config->OFormatBinary)
278       assignFileOffsets();
279     else
280       assignFileOffsetsBinary();
281 
282     setPhdrs();
283     fixPredefinedSymbols();
284   }
285 
286   // It does not make sense try to open the file if we have error already.
287   if (ErrorCount)
288     return;
289   // Write the result down to a file.
290   openFile();
291   if (ErrorCount)
292     return;
293   if (!Config->OFormatBinary) {
294     writeHeader();
295     writeSections();
296   } else {
297     writeSectionsBinary();
298   }
299 
300   // Backfill .note.gnu.build-id section content. This is done at last
301   // because the content is usually a hash value of the entire output file.
302   writeBuildId();
303   if (ErrorCount)
304     return;
305 
306   // Handle -Map option.
307   writeMapFile<ELFT>(OutputSections);
308   if (ErrorCount)
309     return;
310 
311   if (auto EC = Buffer->commit())
312     error("failed to write to the output file: " + EC.message());
313 
314   // Flush the output streams and exit immediately. A full shutdown
315   // is a good test that we are keeping track of all allocated memory,
316   // but actually freeing it is a waste of time in a regular linker run.
317   if (Config->ExitEarly)
318     exitLld(0);
319 }
320 
321 // Initialize Out members.
322 template <class ELFT> void Writer<ELFT>::createSyntheticSections() {
323   // Initialize all pointers with NULL. This is needed because
324   // you can call lld::elf::main more than once as a library.
325   memset(&Out::First, 0, sizeof(Out));
326 
327   auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
328 
329   In<ELFT>::DynStrTab = make<StringTableSection>(".dynstr", true);
330   In<ELFT>::Dynamic = make<DynamicSection<ELFT>>();
331   In<ELFT>::RelaDyn = make<RelocationSection<ELFT>>(
332       Config->IsRela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc);
333   In<ELFT>::ShStrTab = make<StringTableSection>(".shstrtab", false);
334 
335   Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC);
336   Out::ElfHeader->Size = sizeof(Elf_Ehdr);
337   Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
338   Out::ProgramHeaders->updateAlignment(sizeof(uintX_t));
339 
340   if (needsInterpSection<ELFT>()) {
341     In<ELFT>::Interp = createInterpSection();
342     Add(In<ELFT>::Interp);
343   } else {
344     In<ELFT>::Interp = nullptr;
345   }
346 
347   if (!Config->Relocatable)
348     Add(createCommentSection<ELFT>());
349 
350   if (Config->Strip != StripPolicy::All) {
351     In<ELFT>::StrTab = make<StringTableSection>(".strtab", false);
352     In<ELFT>::SymTab = make<SymbolTableSection<ELFT>>(*In<ELFT>::StrTab);
353   }
354 
355   if (Config->BuildId != BuildIdKind::None) {
356     In<ELFT>::BuildId = make<BuildIdSection>();
357     Add(In<ELFT>::BuildId);
358   }
359 
360   In<ELFT>::Common = createCommonSection<ELFT>();
361   if (In<ELFT>::Common)
362     Add(InX::Common);
363 
364   In<ELFT>::Bss = make<BssSection>(".bss");
365   Add(In<ELFT>::Bss);
366   In<ELFT>::BssRelRo = make<BssSection>(".bss.rel.ro");
367   Add(In<ELFT>::BssRelRo);
368 
369   // Add MIPS-specific sections.
370   bool HasDynSymTab = !Symtab<ELFT>::X->getSharedFiles().empty() ||
371                       Config->Pic || Config->ExportDynamic;
372   if (Config->EMachine == EM_MIPS) {
373     if (!Config->Shared && HasDynSymTab) {
374       In<ELFT>::MipsRldMap = make<MipsRldMapSection>();
375       Add(In<ELFT>::MipsRldMap);
376     }
377     if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
378       Add(Sec);
379     if (auto *Sec = MipsOptionsSection<ELFT>::create())
380       Add(Sec);
381     if (auto *Sec = MipsReginfoSection<ELFT>::create())
382       Add(Sec);
383   }
384 
385   if (HasDynSymTab) {
386     In<ELFT>::DynSymTab = make<SymbolTableSection<ELFT>>(*In<ELFT>::DynStrTab);
387     Add(In<ELFT>::DynSymTab);
388 
389     In<ELFT>::VerSym = make<VersionTableSection<ELFT>>();
390     Add(In<ELFT>::VerSym);
391 
392     if (!Config->VersionDefinitions.empty()) {
393       In<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>();
394       Add(In<ELFT>::VerDef);
395     }
396 
397     In<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>();
398     Add(In<ELFT>::VerNeed);
399 
400     if (Config->GnuHash) {
401       In<ELFT>::GnuHashTab = make<GnuHashTableSection<ELFT>>();
402       Add(In<ELFT>::GnuHashTab);
403     }
404 
405     if (Config->SysvHash) {
406       In<ELFT>::HashTab = make<HashTableSection<ELFT>>();
407       Add(In<ELFT>::HashTab);
408     }
409 
410     Add(In<ELFT>::Dynamic);
411     Add(In<ELFT>::DynStrTab);
412     Add(In<ELFT>::RelaDyn);
413   }
414 
415   // Add .got. MIPS' .got is so different from the other archs,
416   // it has its own class.
417   if (Config->EMachine == EM_MIPS) {
418     In<ELFT>::MipsGot = make<MipsGotSection>();
419     Add(In<ELFT>::MipsGot);
420   } else {
421     In<ELFT>::Got = make<GotSection<ELFT>>();
422     Add(In<ELFT>::Got);
423   }
424 
425   In<ELFT>::GotPlt = make<GotPltSection>();
426   Add(In<ELFT>::GotPlt);
427   In<ELFT>::IgotPlt = make<IgotPltSection>();
428   Add(In<ELFT>::IgotPlt);
429 
430   if (Config->GdbIndex) {
431     In<ELFT>::GdbIndex = make<GdbIndexSection>();
432     Add(In<ELFT>::GdbIndex);
433   }
434 
435   // We always need to add rel[a].plt to output if it has entries.
436   // Even for static linking it can contain R_[*]_IRELATIVE relocations.
437   In<ELFT>::RelaPlt = make<RelocationSection<ELFT>>(
438       Config->IsRela ? ".rela.plt" : ".rel.plt", false /*Sort*/);
439   Add(In<ELFT>::RelaPlt);
440 
441   // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
442   // that the IRelative relocations are processed last by the dynamic loader
443   In<ELFT>::RelaIplt = make<RelocationSection<ELFT>>(
444       (Config->EMachine == EM_ARM) ? ".rel.dyn" : In<ELFT>::RelaPlt->Name,
445       false /*Sort*/);
446   Add(In<ELFT>::RelaIplt);
447 
448   In<ELFT>::Plt = make<PltSection>(Target->PltHeaderSize);
449   Add(In<ELFT>::Plt);
450   In<ELFT>::Iplt = make<PltSection>(0);
451   Add(In<ELFT>::Iplt);
452 
453   if (!Config->Relocatable) {
454     if (Config->EhFrameHdr) {
455       In<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
456       Add(In<ELFT>::EhFrameHdr);
457     }
458     In<ELFT>::EhFrame = make<EhFrameSection<ELFT>>();
459     Add(In<ELFT>::EhFrame);
460   }
461 
462   if (In<ELFT>::SymTab)
463     Add(In<ELFT>::SymTab);
464   Add(In<ELFT>::ShStrTab);
465   if (In<ELFT>::StrTab)
466     Add(In<ELFT>::StrTab);
467 }
468 
469 static bool shouldKeepInSymtab(SectionBase *Sec, StringRef SymName,
470                                const SymbolBody &B) {
471   if (B.isFile() || B.isSection())
472     return false;
473 
474   // If sym references a section in a discarded group, don't keep it.
475   if (Sec == &InputSection::Discarded)
476     return false;
477 
478   if (Config->Discard == DiscardPolicy::None)
479     return true;
480 
481   // In ELF assembly .L symbols are normally discarded by the assembler.
482   // If the assembler fails to do so, the linker discards them if
483   // * --discard-locals is used.
484   // * The symbol is in a SHF_MERGE section, which is normally the reason for
485   //   the assembler keeping the .L symbol.
486   if (!SymName.startswith(".L") && !SymName.empty())
487     return true;
488 
489   if (Config->Discard == DiscardPolicy::Locals)
490     return false;
491 
492   return !Sec || !(Sec->Flags & SHF_MERGE);
493 }
494 
495 static bool includeInSymtab(const SymbolBody &B) {
496   if (!B.isLocal() && !B.symbol()->IsUsedInRegularObj)
497     return false;
498 
499   if (auto *D = dyn_cast<DefinedRegular>(&B)) {
500     // Always include absolute symbols.
501     SectionBase *Sec = D->Section;
502     if (!Sec)
503       return true;
504     if (auto *IS = dyn_cast<InputSectionBase>(Sec)) {
505       Sec = IS->Repl;
506       IS = cast<InputSectionBase>(Sec);
507       // Exclude symbols pointing to garbage-collected sections.
508       if (!IS->Live)
509         return false;
510     }
511     if (auto *S = dyn_cast<MergeInputSection>(Sec))
512       if (!S->getSectionPiece(D->Value)->Live)
513         return false;
514   }
515   return true;
516 }
517 
518 // Local symbols are not in the linker's symbol table. This function scans
519 // each object file's symbol table to copy local symbols to the output.
520 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
521   if (!In<ELFT>::SymTab)
522     return;
523   for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
524     for (SymbolBody *B : F->getLocalSymbols()) {
525       if (!B->IsLocal)
526         fatal(toString(F) +
527               ": broken object: getLocalSymbols returns a non-local symbol");
528       auto *DR = dyn_cast<DefinedRegular>(B);
529 
530       // No reason to keep local undefined symbol in symtab.
531       if (!DR)
532         continue;
533       if (!includeInSymtab(*B))
534         continue;
535 
536       SectionBase *Sec = DR->Section;
537       if (!shouldKeepInSymtab(Sec, B->getName(), *B))
538         continue;
539       In<ELFT>::SymTab->addSymbol(B);
540     }
541   }
542 }
543 
544 template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
545   // Create one STT_SECTION symbol for each output section we might
546   // have a relocation with.
547   for (OutputSection *Sec : OutputSections) {
548     if (Sec->Sections.empty())
549       continue;
550 
551     InputSection *IS = Sec->Sections[0];
552     if (isa<SyntheticSection>(IS) || IS->Type == SHT_REL ||
553         IS->Type == SHT_RELA)
554       continue;
555 
556     auto *Sym =
557         make<DefinedRegular>("", /*IsLocal=*/true, /*StOther=*/0, STT_SECTION,
558                              /*Value=*/0, /*Size=*/0, IS, nullptr);
559     In<ELFT>::SymTab->addSymbol(Sym);
560   }
561 }
562 
563 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
564 // we would like to make sure appear is a specific order to maximize their
565 // coverage by a single signed 16-bit offset from the TOC base pointer.
566 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
567 // sections. This will put it next to the loaded special PPC64 sections (and,
568 // thus, within reach of the TOC base pointer).
569 static int getPPC64SectionRank(StringRef SectionName) {
570   return StringSwitch<int>(SectionName)
571       .Case(".tocbss", 0)
572       .Case(".branch_lt", 2)
573       .Case(".toc", 3)
574       .Case(".toc1", 4)
575       .Case(".opd", 5)
576       .Default(1);
577 }
578 
579 // All sections with SHF_MIPS_GPREL flag should be grouped together
580 // because data in these sections is addressable with a gp relative address.
581 static int getMipsSectionRank(const OutputSection *S) {
582   if ((S->Flags & SHF_MIPS_GPREL) == 0)
583     return 0;
584   if (S->Name == ".got")
585     return 1;
586   return 2;
587 }
588 
589 // Today's loaders have a feature to make segments read-only after
590 // processing dynamic relocations to enhance security. PT_GNU_RELRO
591 // is defined for that.
592 //
593 // This function returns true if a section needs to be put into a
594 // PT_GNU_RELRO segment.
595 template <class ELFT> bool elf::isRelroSection(const OutputSection *Sec) {
596   if (!Config->ZRelro)
597     return false;
598 
599   uint64_t Flags = Sec->Flags;
600   if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
601     return false;
602   if (Flags & SHF_TLS)
603     return true;
604 
605   uint32_t Type = Sec->Type;
606   if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
607       Type == SHT_PREINIT_ARRAY)
608     return true;
609 
610   if (Sec == In<ELFT>::GotPlt->OutSec)
611     return Config->ZNow;
612   if (Sec == In<ELFT>::Dynamic->OutSec)
613     return true;
614   if (In<ELFT>::Got && Sec == In<ELFT>::Got->OutSec)
615     return true;
616   if (Sec == In<ELFT>::BssRelRo->OutSec)
617     return true;
618 
619   StringRef S = Sec->Name;
620   return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
621          S == ".eh_frame" || S == ".openbsd.randomdata";
622 }
623 
624 template <class ELFT>
625 static bool compareSectionsNonScript(const OutputSection *A,
626                                      const OutputSection *B) {
627   // Put .interp first because some loaders want to see that section
628   // on the first page of the executable file when loaded into memory.
629   bool AIsInterp = A->Name == ".interp";
630   bool BIsInterp = B->Name == ".interp";
631   if (AIsInterp != BIsInterp)
632     return AIsInterp;
633 
634   // Allocatable sections go first to reduce the total PT_LOAD size and
635   // so debug info doesn't change addresses in actual code.
636   bool AIsAlloc = A->Flags & SHF_ALLOC;
637   bool BIsAlloc = B->Flags & SHF_ALLOC;
638   if (AIsAlloc != BIsAlloc)
639     return AIsAlloc;
640 
641   // We don't have any special requirements for the relative order of two non
642   // allocatable sections.
643   if (!AIsAlloc)
644     return false;
645 
646   // We want to put section specified by -T option first, so we
647   // can start assigning VA starting from them later.
648   auto AAddrSetI = Config->SectionStartMap.find(A->Name);
649   auto BAddrSetI = Config->SectionStartMap.find(B->Name);
650   bool AHasAddrSet = AAddrSetI != Config->SectionStartMap.end();
651   bool BHasAddrSet = BAddrSetI != Config->SectionStartMap.end();
652   if (AHasAddrSet != BHasAddrSet)
653     return AHasAddrSet;
654   if (AHasAddrSet)
655     return AAddrSetI->second < BAddrSetI->second;
656 
657   // We want the read only sections first so that they go in the PT_LOAD
658   // covering the program headers at the start of the file.
659   bool AIsWritable = A->Flags & SHF_WRITE;
660   bool BIsWritable = B->Flags & SHF_WRITE;
661   if (AIsWritable != BIsWritable)
662     return BIsWritable;
663 
664   if (!Config->SingleRoRx) {
665     // For a corresponding reason, put non exec sections first (the program
666     // header PT_LOAD is not executable).
667     // We only do that if we are not using linker scripts, since with linker
668     // scripts ro and rx sections are in the same PT_LOAD, so their relative
669     // order is not important. The same applies for -no-rosegment.
670     bool AIsExec = A->Flags & SHF_EXECINSTR;
671     bool BIsExec = B->Flags & SHF_EXECINSTR;
672     if (AIsExec != BIsExec)
673       return BIsExec;
674   }
675 
676   // If we got here we know that both A and B are in the same PT_LOAD.
677 
678   bool AIsTls = A->Flags & SHF_TLS;
679   bool BIsTls = B->Flags & SHF_TLS;
680   bool AIsNoBits = A->Type == SHT_NOBITS;
681   bool BIsNoBits = B->Type == SHT_NOBITS;
682 
683   // The first requirement we have is to put (non-TLS) nobits sections last. The
684   // reason is that the only thing the dynamic linker will see about them is a
685   // p_memsz that is larger than p_filesz. Seeing that it zeros the end of the
686   // PT_LOAD, so that has to correspond to the nobits sections.
687   bool AIsNonTlsNoBits = AIsNoBits && !AIsTls;
688   bool BIsNonTlsNoBits = BIsNoBits && !BIsTls;
689   if (AIsNonTlsNoBits != BIsNonTlsNoBits)
690     return BIsNonTlsNoBits;
691 
692   // We place nobits RelRo sections before plain r/w ones, and non-nobits RelRo
693   // sections after r/w ones, so that the RelRo sections are contiguous.
694   bool AIsRelRo = isRelroSection<ELFT>(A);
695   bool BIsRelRo = isRelroSection<ELFT>(B);
696   if (AIsRelRo != BIsRelRo)
697     return AIsNonTlsNoBits ? AIsRelRo : BIsRelRo;
698 
699   // The TLS initialization block needs to be a single contiguous block in a R/W
700   // PT_LOAD, so stick TLS sections directly before the other RelRo R/W
701   // sections. The TLS NOBITS sections are placed here as they don't take up
702   // virtual address space in the PT_LOAD.
703   if (AIsTls != BIsTls)
704     return AIsTls;
705 
706   // Within the TLS initialization block, the non-nobits sections need to appear
707   // first.
708   if (AIsNoBits != BIsNoBits)
709     return BIsNoBits;
710 
711   // Some architectures have additional ordering restrictions for sections
712   // within the same PT_LOAD.
713   if (Config->EMachine == EM_PPC64)
714     return getPPC64SectionRank(A->Name) < getPPC64SectionRank(B->Name);
715   if (Config->EMachine == EM_MIPS)
716     return getMipsSectionRank(A) < getMipsSectionRank(B);
717 
718   return false;
719 }
720 
721 // Output section ordering is determined by this function.
722 template <class ELFT>
723 static bool compareSections(const OutputSection *A, const OutputSection *B) {
724   // For now, put sections mentioned in a linker script first.
725   int AIndex = Script->getSectionIndex(A->Name);
726   int BIndex = Script->getSectionIndex(B->Name);
727   bool AInScript = AIndex != INT_MAX;
728   bool BInScript = BIndex != INT_MAX;
729   if (AInScript != BInScript)
730     return AInScript;
731   // If both are in the script, use that order.
732   if (AInScript)
733     return AIndex < BIndex;
734 
735   return compareSectionsNonScript<ELFT>(A, B);
736 }
737 
738 // Program header entry
739 PhdrEntry::PhdrEntry(unsigned Type, unsigned Flags) {
740   p_type = Type;
741   p_flags = Flags;
742 }
743 
744 void PhdrEntry::add(OutputSection *Sec) {
745   Last = Sec;
746   if (!First)
747     First = Sec;
748   p_align = std::max(p_align, Sec->Alignment);
749   if (p_type == PT_LOAD)
750     Sec->FirstInPtLoad = First;
751 }
752 
753 template <class ELFT>
754 static Symbol *addRegular(StringRef Name, SectionBase *Sec, uint64_t Value,
755                           uint8_t StOther = STV_HIDDEN,
756                           uint8_t Binding = STB_WEAK) {
757   // The linker generated symbols are added as STB_WEAK to allow user defined
758   // ones to override them.
759   return Symtab<ELFT>::X->addRegular(Name, StOther, STT_NOTYPE, Value,
760                                      /*Size=*/0, Binding, Sec,
761                                      /*File=*/nullptr);
762 }
763 
764 template <class ELFT>
765 static DefinedRegular *
766 addOptionalRegular(StringRef Name, SectionBase *Sec, uint64_t Val,
767                    uint8_t StOther = STV_HIDDEN, uint8_t Binding = STB_GLOBAL) {
768   SymbolBody *S = Symtab<ELFT>::X->find(Name);
769   if (!S)
770     return nullptr;
771   if (S->isInCurrentDSO())
772     return nullptr;
773   return cast<DefinedRegular>(
774       addRegular<ELFT>(Name, Sec, Val, StOther, Binding)->body());
775 }
776 
777 // The beginning and the ending of .rel[a].plt section are marked
778 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
779 // executable. The runtime needs these symbols in order to resolve
780 // all IRELATIVE relocs on startup. For dynamic executables, we don't
781 // need these symbols, since IRELATIVE relocs are resolved through GOT
782 // and PLT. For details, see http://www.airs.com/blog/archives/403.
783 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
784   if (In<ELFT>::DynSymTab)
785     return;
786   StringRef S = Config->IsRela ? "__rela_iplt_start" : "__rel_iplt_start";
787   addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, 0, STV_HIDDEN, STB_WEAK);
788 
789   S = Config->IsRela ? "__rela_iplt_end" : "__rel_iplt_end";
790   addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, -1, STV_HIDDEN, STB_WEAK);
791 }
792 
793 // The linker is expected to define some symbols depending on
794 // the linking result. This function defines such symbols.
795 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
796   if (Config->EMachine == EM_MIPS) {
797     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
798     // so that it points to an absolute address which by default is relative
799     // to GOT. Default offset is 0x7ff0.
800     // See "Global Data Symbols" in Chapter 6 in the following document:
801     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
802     ElfSym::MipsGp = Symtab<ELFT>::X->addAbsolute("_gp", STV_HIDDEN, STB_LOCAL);
803 
804     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
805     // start of function and 'gp' pointer into GOT.
806     if (Symtab<ELFT>::X->find("_gp_disp"))
807       ElfSym::MipsGpDisp =
808           Symtab<ELFT>::X->addAbsolute("_gp_disp", STV_HIDDEN, STB_LOCAL);
809 
810     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
811     // pointer. This symbol is used in the code generated by .cpload pseudo-op
812     // in case of using -mno-shared option.
813     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
814     if (Symtab<ELFT>::X->find("__gnu_local_gp"))
815       ElfSym::MipsLocalGp =
816           Symtab<ELFT>::X->addAbsolute("__gnu_local_gp", STV_HIDDEN, STB_LOCAL);
817   }
818 
819   // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
820   // is magical and is used to produce a R_386_GOTPC relocation.
821   // The R_386_GOTPC relocation value doesn't actually depend on the
822   // symbol value, so it could use an index of STN_UNDEF which, according
823   // to the spec, means the symbol value is 0.
824   // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
825   // the object file.
826   // The situation is even stranger on x86_64 where the assembly doesn't
827   // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
828   // an undefined symbol in the .o files.
829   // Given that the symbol is effectively unused, we just create a dummy
830   // hidden one to avoid the undefined symbol error.
831   Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");
832 
833   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
834   // static linking the linker is required to optimize away any references to
835   // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
836   // to avoid the undefined symbol error. As usual special cases are ARM and
837   // MIPS - the libc for these targets defines __tls_get_addr itself because
838   // there are no TLS optimizations for these targets.
839   if (!In<ELFT>::DynSymTab &&
840       (Config->EMachine != EM_MIPS && Config->EMachine != EM_ARM))
841     Symtab<ELFT>::X->addIgnored("__tls_get_addr");
842 
843   // If linker script do layout we do not need to create any standart symbols.
844   if (ScriptConfig->HasSections)
845     return;
846 
847   // __ehdr_start is the location of ELF file headers.
848   addOptionalRegular<ELFT>("__ehdr_start", Out::ElfHeader, 0, STV_HIDDEN);
849 
850   auto Define = [](StringRef S, DefinedRegular *&Sym1, DefinedRegular *&Sym2) {
851     Sym1 = addOptionalRegular<ELFT>(S, Out::ElfHeader, 0, STV_DEFAULT);
852     assert(S.startswith("_"));
853     S = S.substr(1);
854     Sym2 = addOptionalRegular<ELFT>(S, Out::ElfHeader, 0, STV_DEFAULT);
855   };
856 
857   Define("_end", ElfSym::End, ElfSym::End2);
858   Define("_etext", ElfSym::Etext, ElfSym::Etext2);
859   Define("_edata", ElfSym::Edata, ElfSym::Edata2);
860 }
861 
862 // Sort input sections by section name suffixes for
863 // __attribute__((init_priority(N))).
864 template <class ELFT> static void sortInitFini(OutputSection *S) {
865   if (S)
866     reinterpret_cast<OutputSection *>(S)->sortInitFini();
867 }
868 
869 // Sort input sections by the special rule for .ctors and .dtors.
870 template <class ELFT> static void sortCtorsDtors(OutputSection *S) {
871   if (S)
872     reinterpret_cast<OutputSection *>(S)->sortCtorsDtors();
873 }
874 
875 // Sort input sections using the list provided by --symbol-ordering-file.
876 template <class ELFT>
877 static void sortBySymbolsOrder(ArrayRef<OutputSection *> OutputSections) {
878   if (Config->SymbolOrderingFile.empty())
879     return;
880 
881   // Build a map from symbols to their priorities. Symbols that didn't
882   // appear in the symbol ordering file have the lowest priority 0.
883   // All explicitly mentioned symbols have negative (higher) priorities.
884   DenseMap<StringRef, int> SymbolOrder;
885   int Priority = -Config->SymbolOrderingFile.size();
886   for (StringRef S : Config->SymbolOrderingFile)
887     SymbolOrder.insert({S, Priority++});
888 
889   // Build a map from sections to their priorities.
890   DenseMap<SectionBase *, int> SectionOrder;
891   for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
892     for (SymbolBody *Body : File->getSymbols()) {
893       auto *D = dyn_cast<DefinedRegular>(Body);
894       if (!D || !D->Section)
895         continue;
896       int &Priority = SectionOrder[D->Section];
897       Priority = std::min(Priority, SymbolOrder.lookup(D->getName()));
898     }
899   }
900 
901   // Sort sections by priority.
902   for (OutputSection *Base : OutputSections)
903     if (auto *Sec = dyn_cast<OutputSection>(Base))
904       Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); });
905 }
906 
907 template <class ELFT>
908 void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
909   for (InputSectionBase *IS : InputSections) {
910     if (!IS->Live)
911       continue;
912     // Scan all relocations. Each relocation goes through a series
913     // of tests to determine if it needs special treatment, such as
914     // creating GOT, PLT, copy relocations, etc.
915     // Note that relocations for non-alloc sections are directly
916     // processed by InputSection::relocateNonAlloc.
917     if (!(IS->Flags & SHF_ALLOC))
918       continue;
919     if (isa<InputSection>(IS) || isa<EhInputSection>(IS))
920       Fn(*IS);
921   }
922 
923   if (!Config->Relocatable) {
924     for (EhInputSection *ES : In<ELFT>::EhFrame->Sections)
925       Fn(*ES);
926   }
927 }
928 
929 template <class ELFT> void Writer<ELFT>::createSections() {
930   for (InputSectionBase *IS : InputSections)
931     if (IS)
932       Factory.addInputSec(IS, getOutputSectionName(IS->Name));
933 
934   sortBySymbolsOrder<ELFT>(OutputSections);
935   sortInitFini<ELFT>(findSection(".init_array"));
936   sortInitFini<ELFT>(findSection(".fini_array"));
937   sortCtorsDtors<ELFT>(findSection(".ctors"));
938   sortCtorsDtors<ELFT>(findSection(".dtors"));
939 
940   for (OutputSection *Sec : OutputSections)
941     Sec->assignOffsets();
942 }
943 
944 static bool canSharePtLoad(const OutputSection &S1, const OutputSection &S2) {
945   if (!(S1.Flags & SHF_ALLOC) || !(S2.Flags & SHF_ALLOC))
946     return false;
947 
948   bool S1IsWrite = S1.Flags & SHF_WRITE;
949   bool S2IsWrite = S2.Flags & SHF_WRITE;
950   if (S1IsWrite != S2IsWrite)
951     return false;
952 
953   if (!S1IsWrite)
954     return true; // RO and RX share a PT_LOAD with linker scripts.
955   return (S1.Flags & SHF_EXECINSTR) == (S2.Flags & SHF_EXECINSTR);
956 }
957 
958 template <class ELFT> void Writer<ELFT>::sortSections() {
959   // Don't sort if using -r. It is not necessary and we want to preserve the
960   // relative order for SHF_LINK_ORDER sections.
961   if (Config->Relocatable)
962     return;
963   if (!ScriptConfig->HasSections) {
964     std::stable_sort(OutputSections.begin(), OutputSections.end(),
965                      compareSectionsNonScript<ELFT>);
966     return;
967   }
968   Script->adjustSectionsBeforeSorting();
969 
970   // The order of the sections in the script is arbitrary and may not agree with
971   // compareSectionsNonScript. This means that we cannot easily define a
972   // strict weak ordering. To see why, consider a comparison of a section in the
973   // script and one not in the script. We have a two simple options:
974   // * Make them equivalent (a is not less than b, and b is not less than a).
975   //   The problem is then that equivalence has to be transitive and we can
976   //   have sections a, b and c with only b in a script and a less than c
977   //   which breaks this property.
978   // * Use compareSectionsNonScript. Given that the script order doesn't have
979   //   to match, we can end up with sections a, b, c, d where b and c are in the
980   //   script and c is compareSectionsNonScript less than b. In which case d
981   //   can be equivalent to c, a to b and d < a. As a concrete example:
982   //   .a (rx) # not in script
983   //   .b (rx) # in script
984   //   .c (ro) # in script
985   //   .d (ro) # not in script
986   //
987   // The way we define an order then is:
988   // *  First put script sections at the start and sort the script and
989   //    non-script sections independently.
990   // *  Move each non-script section to its preferred position. We try
991   //    to put each section in the last position where it it can share
992   //    a PT_LOAD.
993 
994   std::stable_sort(OutputSections.begin(), OutputSections.end(),
995                    compareSections<ELFT>);
996 
997   auto I = OutputSections.begin();
998   auto E = OutputSections.end();
999   auto NonScriptI =
1000       std::find_if(OutputSections.begin(), E, [](OutputSection *S) {
1001         return Script->getSectionIndex(S->Name) == INT_MAX;
1002       });
1003   while (NonScriptI != E) {
1004     auto BestPos = std::max_element(
1005         I, NonScriptI, [&](OutputSection *&A, OutputSection *&B) {
1006           bool ACanSharePtLoad = canSharePtLoad(**NonScriptI, *A);
1007           bool BCanSharePtLoad = canSharePtLoad(**NonScriptI, *B);
1008           if (ACanSharePtLoad != BCanSharePtLoad)
1009             return BCanSharePtLoad;
1010 
1011           bool ACmp = compareSectionsNonScript<ELFT>(*NonScriptI, A);
1012           bool BCmp = compareSectionsNonScript<ELFT>(*NonScriptI, B);
1013           if (ACmp != BCmp)
1014             return BCmp; // FIXME: missing test
1015 
1016           size_t PosA = &A - &OutputSections[0];
1017           size_t PosB = &B - &OutputSections[0];
1018           return ACmp ? PosA > PosB : PosA < PosB;
1019         });
1020 
1021     // max_element only returns NonScriptI if the range is empty. If the range
1022     // is not empty we should consider moving the the element forward one
1023     // position.
1024     if (BestPos != NonScriptI &&
1025         !compareSectionsNonScript<ELFT>(*NonScriptI, *BestPos))
1026       ++BestPos;
1027     std::rotate(BestPos, NonScriptI, NonScriptI + 1);
1028     ++NonScriptI;
1029   }
1030 
1031   Script->adjustSectionsAfterSorting();
1032 }
1033 
1034 static void applySynthetic(const std::vector<SyntheticSection *> &Sections,
1035                            std::function<void(SyntheticSection *)> Fn) {
1036   for (SyntheticSection *SS : Sections)
1037     if (SS && SS->OutSec && !SS->empty()) {
1038       Fn(SS);
1039       SS->OutSec->assignOffsets();
1040     }
1041 }
1042 
1043 // We need to add input synthetic sections early in createSyntheticSections()
1044 // to make them visible from linkescript side. But not all sections are always
1045 // required to be in output. For example we don't need dynamic section content
1046 // sometimes. This function filters out such unused sections from output.
1047 static void removeUnusedSyntheticSections(std::vector<OutputSection *> &V) {
1048   // All input synthetic sections that can be empty are placed after
1049   // all regular ones. We iterate over them all and exit at first
1050   // non-synthetic.
1051   for (InputSectionBase *S : llvm::reverse(InputSections)) {
1052     SyntheticSection *SS = dyn_cast<SyntheticSection>(S);
1053     if (!SS)
1054       return;
1055     if (!SS->empty() || !SS->OutSec)
1056       continue;
1057 
1058     OutputSection *OutSec = cast<OutputSection>(SS->OutSec);
1059     OutSec->Sections.erase(
1060         std::find(OutSec->Sections.begin(), OutSec->Sections.end(), SS));
1061     // If there is no other sections in output section, remove it from output.
1062     if (OutSec->Sections.empty())
1063       V.erase(std::find(V.begin(), V.end(), OutSec));
1064   }
1065 }
1066 
1067 // Create output section objects and add them to OutputSections.
1068 template <class ELFT> void Writer<ELFT>::finalizeSections() {
1069   Out::DebugInfo = findSection(".debug_info");
1070   Out::PreinitArray = findSection(".preinit_array");
1071   Out::InitArray = findSection(".init_array");
1072   Out::FiniArray = findSection(".fini_array");
1073 
1074   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1075   // symbols for sections, so that the runtime can get the start and end
1076   // addresses of each section by section name. Add such symbols.
1077   if (!Config->Relocatable) {
1078     addStartEndSymbols();
1079     for (OutputSection *Sec : OutputSections)
1080       addStartStopSymbols(Sec);
1081   }
1082 
1083   // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1084   // It should be okay as no one seems to care about the type.
1085   // Even the author of gold doesn't remember why gold behaves that way.
1086   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1087   if (In<ELFT>::DynSymTab)
1088     addRegular<ELFT>("_DYNAMIC", In<ELFT>::Dynamic, 0);
1089 
1090   // Define __rel[a]_iplt_{start,end} symbols if needed.
1091   addRelIpltSymbols();
1092 
1093   // This responsible for splitting up .eh_frame section into
1094   // pieces. The relocation scan uses those pieces, so this has to be
1095   // earlier.
1096   applySynthetic({In<ELFT>::EhFrame},
1097                  [](SyntheticSection *SS) { SS->finalizeContents(); });
1098 
1099   // Scan relocations. This must be done after every symbol is declared so that
1100   // we can correctly decide if a dynamic relocation is needed.
1101   forEachRelSec(scanRelocations<ELFT>);
1102 
1103   if (In<ELFT>::Plt && !In<ELFT>::Plt->empty())
1104     In<ELFT>::Plt->addSymbols();
1105   if (In<ELFT>::Iplt && !In<ELFT>::Iplt->empty())
1106     In<ELFT>::Iplt->addSymbols();
1107 
1108   // Now that we have defined all possible global symbols including linker-
1109   // synthesized ones. Visit all symbols to give the finishing touches.
1110   for (Symbol *S : Symtab<ELFT>::X->getSymbols()) {
1111     SymbolBody *Body = S->body();
1112 
1113     if (!includeInSymtab(*Body))
1114       continue;
1115     if (In<ELFT>::SymTab)
1116       In<ELFT>::SymTab->addSymbol(Body);
1117 
1118     if (In<ELFT>::DynSymTab && S->includeInDynsym()) {
1119       In<ELFT>::DynSymTab->addSymbol(Body);
1120       if (auto *SS = dyn_cast<SharedSymbol>(Body))
1121         if (cast<SharedFile<ELFT>>(SS->File)->isNeeded())
1122           In<ELFT>::VerNeed->addSymbol(SS);
1123     }
1124   }
1125 
1126   // Do not proceed if there was an undefined symbol.
1127   if (ErrorCount)
1128     return;
1129 
1130   // So far we have added sections from input object files.
1131   // This function adds linker-created Out::* sections.
1132   addPredefinedSections();
1133   removeUnusedSyntheticSections(OutputSections);
1134 
1135   sortSections();
1136 
1137   // This is a bit of a hack. A value of 0 means undef, so we set it
1138   // to 1 t make __ehdr_start defined. The section number is not
1139   // particularly relevant.
1140   Out::ElfHeader->SectionIndex = 1;
1141 
1142   unsigned I = 1;
1143   for (OutputSection *Sec : OutputSections) {
1144     Sec->SectionIndex = I++;
1145     Sec->ShName = In<ELFT>::ShStrTab->addString(Sec->Name);
1146   }
1147 
1148   // Binary and relocatable output does not have PHDRS.
1149   // The headers have to be created before finalize as that can influence the
1150   // image base and the dynamic section on mips includes the image base.
1151   if (!Config->Relocatable && !Config->OFormatBinary) {
1152     Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs();
1153     addPtArmExid(Phdrs);
1154     fixHeaders();
1155   }
1156 
1157   // Dynamic section must be the last one in this list and dynamic
1158   // symbol table section (DynSymTab) must be the first one.
1159   applySynthetic({In<ELFT>::DynSymTab,  In<ELFT>::Bss,      In<ELFT>::BssRelRo,
1160                   In<ELFT>::GnuHashTab, In<ELFT>::HashTab,  In<ELFT>::SymTab,
1161                   In<ELFT>::ShStrTab,   In<ELFT>::StrTab,   In<ELFT>::VerDef,
1162                   In<ELFT>::DynStrTab,  In<ELFT>::GdbIndex, In<ELFT>::Got,
1163                   In<ELFT>::MipsGot,    In<ELFT>::IgotPlt,  In<ELFT>::GotPlt,
1164                   In<ELFT>::RelaDyn,    In<ELFT>::RelaIplt, In<ELFT>::RelaPlt,
1165                   In<ELFT>::Plt,        In<ELFT>::Iplt,     In<ELFT>::Plt,
1166                   In<ELFT>::EhFrameHdr, In<ELFT>::VerSym,   In<ELFT>::VerNeed,
1167                   In<ELFT>::Dynamic},
1168                  [](SyntheticSection *SS) { SS->finalizeContents(); });
1169 
1170   // Some architectures use small displacements for jump instructions.
1171   // It is linker's responsibility to create thunks containing long
1172   // jump instructions if jump targets are too far. Create thunks.
1173   if (Target->NeedsThunks) {
1174     // FIXME: only ARM Interworking and Mips LA25 Thunks are implemented,
1175     // these
1176     // do not require address information. To support range extension Thunks
1177     // we need to assign addresses so that we can tell if jump instructions
1178     // are out of range. This will need to turn into a loop that converges
1179     // when no more Thunks are added
1180     if (createThunks<ELFT>(OutputSections))
1181       applySynthetic({In<ELFT>::MipsGot},
1182                      [](SyntheticSection *SS) { SS->updateAllocSize(); });
1183   }
1184   // Fill other section headers. The dynamic table is finalized
1185   // at the end because some tags like RELSZ depend on result
1186   // of finalizing other sections.
1187   for (OutputSection *Sec : OutputSections)
1188     Sec->finalize<ELFT>();
1189 
1190   // createThunks may have added local symbols to the static symbol table
1191   applySynthetic({In<ELFT>::SymTab, In<ELFT>::ShStrTab, In<ELFT>::StrTab},
1192                  [](SyntheticSection *SS) { SS->postThunkContents(); });
1193 }
1194 
1195 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
1196   // ARM ABI requires .ARM.exidx to be terminated by some piece of data.
1197   // We have the terminater synthetic section class. Add that at the end.
1198   auto *OS = dyn_cast_or_null<OutputSection>(findSection(".ARM.exidx"));
1199   if (OS && !OS->Sections.empty() && !Config->Relocatable)
1200     OS->addSection(make<ARMExidxSentinelSection>());
1201 }
1202 
1203 // The linker is expected to define SECNAME_start and SECNAME_end
1204 // symbols for a few sections. This function defines them.
1205 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
1206   auto Define = [&](StringRef Start, StringRef End, OutputSection *OS) {
1207     // These symbols resolve to the image base if the section does not exist.
1208     // A special value -1 indicates end of the section.
1209     if (OS) {
1210       addOptionalRegular<ELFT>(Start, OS, 0);
1211       addOptionalRegular<ELFT>(End, OS, -1);
1212     } else {
1213       if (Config->Pic)
1214         OS = Out::ElfHeader;
1215       addOptionalRegular<ELFT>(Start, OS, 0);
1216       addOptionalRegular<ELFT>(End, OS, 0);
1217     }
1218   };
1219 
1220   Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray);
1221   Define("__init_array_start", "__init_array_end", Out::InitArray);
1222   Define("__fini_array_start", "__fini_array_end", Out::FiniArray);
1223 
1224   if (OutputSection *Sec = findSection(".ARM.exidx"))
1225     Define("__exidx_start", "__exidx_end", Sec);
1226 }
1227 
1228 // If a section name is valid as a C identifier (which is rare because of
1229 // the leading '.'), linkers are expected to define __start_<secname> and
1230 // __stop_<secname> symbols. They are at beginning and end of the section,
1231 // respectively. This is not requested by the ELF standard, but GNU ld and
1232 // gold provide the feature, and used by many programs.
1233 template <class ELFT>
1234 void Writer<ELFT>::addStartStopSymbols(OutputSection *Sec) {
1235   StringRef S = Sec->Name;
1236   if (!isValidCIdentifier(S))
1237     return;
1238   addOptionalRegular<ELFT>(Saver.save("__start_" + S), Sec, 0, STV_DEFAULT);
1239   addOptionalRegular<ELFT>(Saver.save("__stop_" + S), Sec, -1, STV_DEFAULT);
1240 }
1241 
1242 template <class ELFT> OutputSection *Writer<ELFT>::findSection(StringRef Name) {
1243   for (OutputSection *Sec : OutputSections)
1244     if (Sec->Name == Name)
1245       return Sec;
1246   return nullptr;
1247 }
1248 
1249 static bool needsPtLoad(OutputSection *Sec) {
1250   if (!(Sec->Flags & SHF_ALLOC))
1251     return false;
1252 
1253   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
1254   // responsible for allocating space for them, not the PT_LOAD that
1255   // contains the TLS initialization image.
1256   if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS)
1257     return false;
1258   return true;
1259 }
1260 
1261 // Linker scripts are responsible for aligning addresses. Unfortunately, most
1262 // linker scripts are designed for creating two PT_LOADs only, one RX and one
1263 // RW. This means that there is no alignment in the RO to RX transition and we
1264 // cannot create a PT_LOAD there.
1265 template <class ELFT>
1266 static typename ELFT::uint computeFlags(typename ELFT::uint F) {
1267   if (Config->Omagic)
1268     return PF_R | PF_W | PF_X;
1269   if (Config->SingleRoRx && !(F & PF_W))
1270     return F | PF_X;
1271   return F;
1272 }
1273 
1274 // Decide which program headers to create and which sections to include in each
1275 // one.
1276 template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() {
1277   std::vector<PhdrEntry> Ret;
1278   auto AddHdr = [&](unsigned Type, unsigned Flags) -> PhdrEntry * {
1279     Ret.emplace_back(Type, Flags);
1280     return &Ret.back();
1281   };
1282 
1283   // The first phdr entry is PT_PHDR which describes the program header itself.
1284   AddHdr(PT_PHDR, PF_R)->add(Out::ProgramHeaders);
1285 
1286   // PT_INTERP must be the second entry if exists.
1287   if (OutputSection *Sec = findSection(".interp"))
1288     AddHdr(PT_INTERP, Sec->getPhdrFlags())->add(Sec);
1289 
1290   // Add the first PT_LOAD segment for regular output sections.
1291   uintX_t Flags = computeFlags<ELFT>(PF_R);
1292   PhdrEntry *Load = AddHdr(PT_LOAD, Flags);
1293   for (OutputSection *Sec : OutputSections) {
1294     if (!(Sec->Flags & SHF_ALLOC))
1295       break;
1296     if (!needsPtLoad(Sec))
1297       continue;
1298 
1299     // Segments are contiguous memory regions that has the same attributes
1300     // (e.g. executable or writable). There is one phdr for each segment.
1301     // Therefore, we need to create a new phdr when the next section has
1302     // different flags or is loaded at a discontiguous address using AT linker
1303     // script command.
1304     uintX_t NewFlags = computeFlags<ELFT>(Sec->getPhdrFlags());
1305     if (Script->hasLMA(Sec->Name) || Flags != NewFlags) {
1306       Load = AddHdr(PT_LOAD, NewFlags);
1307       Flags = NewFlags;
1308     }
1309 
1310     Load->add(Sec);
1311   }
1312 
1313   // Add a TLS segment if any.
1314   PhdrEntry TlsHdr(PT_TLS, PF_R);
1315   for (OutputSection *Sec : OutputSections)
1316     if (Sec->Flags & SHF_TLS)
1317       TlsHdr.add(Sec);
1318   if (TlsHdr.First)
1319     Ret.push_back(std::move(TlsHdr));
1320 
1321   // Add an entry for .dynamic.
1322   if (In<ELFT>::DynSymTab)
1323     AddHdr(PT_DYNAMIC, In<ELFT>::Dynamic->OutSec->getPhdrFlags())
1324         ->add(In<ELFT>::Dynamic->OutSec);
1325 
1326   // PT_GNU_RELRO includes all sections that should be marked as
1327   // read-only by dynamic linker after proccessing relocations.
1328   PhdrEntry RelRo(PT_GNU_RELRO, PF_R);
1329   for (OutputSection *Sec : OutputSections)
1330     if (needsPtLoad(Sec) && isRelroSection<ELFT>(Sec))
1331       RelRo.add(Sec);
1332   if (RelRo.First)
1333     Ret.push_back(std::move(RelRo));
1334 
1335   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1336   if (!In<ELFT>::EhFrame->empty() && In<ELFT>::EhFrameHdr &&
1337       In<ELFT>::EhFrame->OutSec && In<ELFT>::EhFrameHdr->OutSec)
1338     AddHdr(PT_GNU_EH_FRAME, In<ELFT>::EhFrameHdr->OutSec->getPhdrFlags())
1339         ->add(In<ELFT>::EhFrameHdr->OutSec);
1340 
1341   // PT_OPENBSD_RANDOMIZE specifies the location and size of a part of the
1342   // memory image of the program that must be filled with random data before any
1343   // code in the object is executed.
1344   if (OutputSection *Sec = findSection(".openbsd.randomdata"))
1345     AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec);
1346 
1347   // PT_GNU_STACK is a special section to tell the loader to make the
1348   // pages for the stack non-executable. If you really want an executable
1349   // stack, you can pass -z execstack, but that's not recommended for
1350   // security reasons.
1351   unsigned Perm;
1352   if (Config->ZExecstack)
1353     Perm = PF_R | PF_W | PF_X;
1354   else
1355     Perm = PF_R | PF_W;
1356   AddHdr(PT_GNU_STACK, Perm)->p_memsz = Config->ZStackSize;
1357 
1358   // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
1359   // is expected to perform W^X violations, such as calling mprotect(2) or
1360   // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
1361   // OpenBSD.
1362   if (Config->ZWxneeded)
1363     AddHdr(PT_OPENBSD_WXNEEDED, PF_X);
1364 
1365   // Create one PT_NOTE per a group of contiguous .note sections.
1366   PhdrEntry *Note = nullptr;
1367   for (OutputSection *Sec : OutputSections) {
1368     if (Sec->Type == SHT_NOTE) {
1369       if (!Note || Script->hasLMA(Sec->Name))
1370         Note = AddHdr(PT_NOTE, PF_R);
1371       Note->add(Sec);
1372     } else {
1373       Note = nullptr;
1374     }
1375   }
1376   return Ret;
1377 }
1378 
1379 template <class ELFT>
1380 void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry> &Phdrs) {
1381   if (Config->EMachine != EM_ARM)
1382     return;
1383   auto I = std::find_if(
1384       OutputSections.begin(), OutputSections.end(),
1385       [](OutputSection *Sec) { return Sec->Type == SHT_ARM_EXIDX; });
1386   if (I == OutputSections.end())
1387     return;
1388 
1389   // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1390   PhdrEntry ARMExidx(PT_ARM_EXIDX, PF_R);
1391   ARMExidx.add(*I);
1392   Phdrs.push_back(ARMExidx);
1393 }
1394 
1395 // The first section of each PT_LOAD, the first section in PT_GNU_RELRO and the
1396 // first section after PT_GNU_RELRO have to be page aligned so that the dynamic
1397 // linker can set the permissions.
1398 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
1399   for (const PhdrEntry &P : Phdrs)
1400     if (P.p_type == PT_LOAD && P.First)
1401       P.First->PageAlign = true;
1402 
1403   for (const PhdrEntry &P : Phdrs) {
1404     if (P.p_type != PT_GNU_RELRO)
1405       continue;
1406     if (P.First)
1407       P.First->PageAlign = true;
1408     // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1409     // have to align it to a page.
1410     auto End = OutputSections.end();
1411     auto I = std::find(OutputSections.begin(), End, P.Last);
1412     if (I == End || (I + 1) == End)
1413       continue;
1414     OutputSection *Sec = *(I + 1);
1415     if (needsPtLoad(Sec))
1416       Sec->PageAlign = true;
1417   }
1418 }
1419 
1420 bool elf::allocateHeaders(std::vector<PhdrEntry> &Phdrs,
1421                           ArrayRef<OutputSection *> OutputSections,
1422                           uint64_t Min) {
1423   auto FirstPTLoad =
1424       std::find_if(Phdrs.begin(), Phdrs.end(),
1425                    [](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
1426   if (FirstPTLoad == Phdrs.end())
1427     return false;
1428 
1429   uint64_t HeaderSize = getHeaderSize();
1430   if (HeaderSize > Min) {
1431     auto PhdrI =
1432         std::find_if(Phdrs.begin(), Phdrs.end(),
1433                      [](const PhdrEntry &E) { return E.p_type == PT_PHDR; });
1434     if (PhdrI != Phdrs.end())
1435       Phdrs.erase(PhdrI);
1436     return false;
1437   }
1438   Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
1439 
1440   if (!ScriptConfig->HasSections)
1441     Config->ImageBase = Min = std::min(Min, Config->ImageBase);
1442 
1443   Out::ElfHeader->Addr = Min;
1444   Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
1445 
1446   if (Script->hasPhdrsCommands())
1447     return true;
1448 
1449   if (FirstPTLoad->First)
1450     for (OutputSection *Sec : OutputSections)
1451       if (Sec->FirstInPtLoad == FirstPTLoad->First)
1452         Sec->FirstInPtLoad = Out::ElfHeader;
1453   FirstPTLoad->First = Out::ElfHeader;
1454   if (!FirstPTLoad->Last)
1455     FirstPTLoad->Last = Out::ProgramHeaders;
1456   return true;
1457 }
1458 
1459 // We should set file offsets and VAs for elf header and program headers
1460 // sections. These are special, we do not include them into output sections
1461 // list, but have them to simplify the code.
1462 template <class ELFT> void Writer<ELFT>::fixHeaders() {
1463   Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
1464   // If the script has SECTIONS, assignAddresses will compute the values.
1465   if (ScriptConfig->HasSections)
1466     return;
1467 
1468   // When -T<section> option is specified, lower the base to make room for those
1469   // sections.
1470   uint64_t Min = -1;
1471   if (!Config->SectionStartMap.empty())
1472     for (const auto &P : Config->SectionStartMap)
1473       Min = std::min(Min, P.second);
1474 
1475   AllocateHeader = allocateHeaders(Phdrs, OutputSections, Min);
1476 }
1477 
1478 // Assign VAs (addresses at run-time) to output sections.
1479 template <class ELFT> void Writer<ELFT>::assignAddresses() {
1480   uintX_t VA = Config->ImageBase;
1481   if (AllocateHeader)
1482     VA += getHeaderSize();
1483   uintX_t ThreadBssOffset = 0;
1484   for (OutputSection *Sec : OutputSections) {
1485     uint32_t Alignment = Sec->Alignment;
1486     if (Sec->PageAlign)
1487       Alignment = std::max<uintX_t>(Alignment, Config->MaxPageSize);
1488 
1489     auto I = Config->SectionStartMap.find(Sec->Name);
1490     if (I != Config->SectionStartMap.end())
1491       VA = I->second;
1492 
1493     // We only assign VAs to allocated sections.
1494     if (needsPtLoad(Sec)) {
1495       VA = alignTo(VA, Alignment);
1496       Sec->Addr = VA;
1497       VA += Sec->Size;
1498     } else if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS) {
1499       uintX_t TVA = VA + ThreadBssOffset;
1500       TVA = alignTo(TVA, Alignment);
1501       Sec->Addr = TVA;
1502       ThreadBssOffset = TVA - VA + Sec->Size;
1503     }
1504   }
1505 }
1506 
1507 // Adjusts the file alignment for a given output section and returns
1508 // its new file offset. The file offset must be the same with its
1509 // virtual address (modulo the page size) so that the loader can load
1510 // executables without any address adjustment.
1511 template <class ELFT, class uintX_t>
1512 static uintX_t getFileAlignment(uintX_t Off, OutputSection *Sec) {
1513   OutputSection *First = Sec->FirstInPtLoad;
1514   // If the section is not in a PT_LOAD, we just have to align it.
1515   if (!First)
1516     return alignTo(Off, Sec->Alignment);
1517 
1518   // The first section in a PT_LOAD has to have congruent offset and address
1519   // module the page size.
1520   if (Sec == First)
1521     return alignTo(Off, Config->MaxPageSize, Sec->Addr);
1522 
1523   // If two sections share the same PT_LOAD the file offset is calculated
1524   // using this formula: Off2 = Off1 + (VA2 - VA1).
1525   return First->Offset + Sec->Addr - First->Addr;
1526 }
1527 
1528 template <class ELFT, class uintX_t>
1529 static uintX_t setOffset(OutputSection *Sec, uintX_t Off) {
1530   if (Sec->Type == SHT_NOBITS) {
1531     Sec->Offset = Off;
1532     return Off;
1533   }
1534 
1535   Off = getFileAlignment<ELFT>(Off, Sec);
1536   Sec->Offset = Off;
1537   return Off + Sec->Size;
1538 }
1539 
1540 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
1541   uintX_t Off = 0;
1542   for (OutputSection *Sec : OutputSections)
1543     if (Sec->Flags & SHF_ALLOC)
1544       Off = setOffset<ELFT>(Sec, Off);
1545   FileSize = alignTo(Off, sizeof(uintX_t));
1546 }
1547 
1548 // Assign file offsets to output sections.
1549 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
1550   uintX_t Off = 0;
1551   Off = setOffset<ELFT>(Out::ElfHeader, Off);
1552   Off = setOffset<ELFT>(Out::ProgramHeaders, Off);
1553 
1554   for (OutputSection *Sec : OutputSections)
1555     Off = setOffset<ELFT>(Sec, Off);
1556 
1557   SectionHeaderOff = alignTo(Off, sizeof(uintX_t));
1558   FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
1559 }
1560 
1561 // Finalize the program headers. We call this function after we assign
1562 // file offsets and VAs to all sections.
1563 template <class ELFT> void Writer<ELFT>::setPhdrs() {
1564   for (PhdrEntry &P : Phdrs) {
1565     OutputSection *First = P.First;
1566     OutputSection *Last = P.Last;
1567     if (First) {
1568       P.p_filesz = Last->Offset - First->Offset;
1569       if (Last->Type != SHT_NOBITS)
1570         P.p_filesz += Last->Size;
1571       P.p_memsz = Last->Addr + Last->Size - First->Addr;
1572       P.p_offset = First->Offset;
1573       P.p_vaddr = First->Addr;
1574       if (!P.HasLMA)
1575         P.p_paddr = First->getLMA();
1576     }
1577     if (P.p_type == PT_LOAD)
1578       P.p_align = Config->MaxPageSize;
1579     else if (P.p_type == PT_GNU_RELRO) {
1580       P.p_align = 1;
1581       // The glibc dynamic loader rounds the size down, so we need to round up
1582       // to protect the last page. This is a no-op on FreeBSD which always
1583       // rounds up.
1584       P.p_memsz = alignTo(P.p_memsz, Target->PageSize);
1585     }
1586 
1587     // The TLS pointer goes after PT_TLS. At least glibc will align it,
1588     // so round up the size to make sure the offsets are correct.
1589     if (P.p_type == PT_TLS) {
1590       Out::TlsPhdr = &P;
1591       if (P.p_memsz)
1592         P.p_memsz = alignTo(P.p_memsz, P.p_align);
1593     }
1594   }
1595 }
1596 
1597 // The entry point address is chosen in the following ways.
1598 //
1599 // 1. the '-e' entry command-line option;
1600 // 2. the ENTRY(symbol) command in a linker control script;
1601 // 3. the value of the symbol start, if present;
1602 // 4. the address of the first byte of the .text section, if present;
1603 // 5. the address 0.
1604 template <class ELFT> typename ELFT::uint Writer<ELFT>::getEntryAddr() {
1605   // Case 1, 2 or 3. As a special case, if the symbol is actually
1606   // a number, we'll use that number as an address.
1607   if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Entry))
1608     return B->getVA();
1609   uint64_t Addr;
1610   if (!Config->Entry.getAsInteger(0, Addr))
1611     return Addr;
1612 
1613   // Case 4
1614   if (OutputSection *Sec = findSection(".text")) {
1615     if (Config->WarnMissingEntry)
1616       warn("cannot find entry symbol " + Config->Entry + "; defaulting to 0x" +
1617            utohexstr(Sec->Addr));
1618     return Sec->Addr;
1619   }
1620 
1621   // Case 5
1622   if (Config->WarnMissingEntry)
1623     warn("cannot find entry symbol " + Config->Entry +
1624          "; not setting start address");
1625   return 0;
1626 }
1627 
1628 template <class ELFT> static uint8_t getELFEncoding() {
1629   if (ELFT::TargetEndianness == llvm::support::little)
1630     return ELFDATA2LSB;
1631   return ELFDATA2MSB;
1632 }
1633 
1634 static uint16_t getELFType() {
1635   if (Config->Pic)
1636     return ET_DYN;
1637   if (Config->Relocatable)
1638     return ET_REL;
1639   return ET_EXEC;
1640 }
1641 
1642 // This function is called after we have assigned address and size
1643 // to each section. This function fixes some predefined
1644 // symbol values that depend on section address and size.
1645 template <class ELFT> void Writer<ELFT>::fixPredefinedSymbols() {
1646   auto Set = [](DefinedRegular *S1, DefinedRegular *S2, OutputSection *Sec,
1647                 uint64_t Value) {
1648     if (S1) {
1649       S1->Section = Sec;
1650       S1->Value = Value;
1651     }
1652     if (S2) {
1653       S2->Section = Sec;
1654       S2->Value = Value;
1655     }
1656   };
1657 
1658   // _etext is the first location after the last read-only loadable segment.
1659   // _edata is the first location after the last read-write loadable segment.
1660   // _end is the first location after the uninitialized data region.
1661   PhdrEntry *Last = nullptr;
1662   PhdrEntry *LastRO = nullptr;
1663   PhdrEntry *LastRW = nullptr;
1664   for (PhdrEntry &P : Phdrs) {
1665     if (P.p_type != PT_LOAD)
1666       continue;
1667     Last = &P;
1668     if (P.p_flags & PF_W)
1669       LastRW = &P;
1670     else
1671       LastRO = &P;
1672   }
1673   if (Last)
1674     Set(ElfSym::End, ElfSym::End2, Last->First, Last->p_memsz);
1675   if (LastRO)
1676     Set(ElfSym::Etext, ElfSym::Etext2, LastRO->First, LastRO->p_filesz);
1677   if (LastRW)
1678     Set(ElfSym::Edata, ElfSym::Edata2, LastRW->First, LastRW->p_filesz);
1679 
1680   // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1681   // be equal to the _gp symbol's value.
1682   if (Config->EMachine == EM_MIPS) {
1683     if (!ElfSym::MipsGp->Value) {
1684       // Find GP-relative section with the lowest address
1685       // and use this address to calculate default _gp value.
1686       uintX_t Gp = -1;
1687       for (const OutputSection *OS : OutputSections)
1688         if ((OS->Flags & SHF_MIPS_GPREL) && OS->Addr < Gp)
1689           Gp = OS->Addr;
1690       if (Gp != (uintX_t)-1)
1691         ElfSym::MipsGp->Value = Gp + 0x7ff0;
1692     }
1693   }
1694 }
1695 
1696 template <class ELFT> void Writer<ELFT>::writeHeader() {
1697   uint8_t *Buf = Buffer->getBufferStart();
1698   memcpy(Buf, "\177ELF", 4);
1699 
1700   // Write the ELF header.
1701   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1702   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1703   EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>();
1704   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1705   EHdr->e_ident[EI_OSABI] = Config->OSABI;
1706   EHdr->e_type = getELFType();
1707   EHdr->e_machine = Config->EMachine;
1708   EHdr->e_version = EV_CURRENT;
1709   EHdr->e_entry = getEntryAddr();
1710   EHdr->e_shoff = SectionHeaderOff;
1711   EHdr->e_ehsize = sizeof(Elf_Ehdr);
1712   EHdr->e_phnum = Phdrs.size();
1713   EHdr->e_shentsize = sizeof(Elf_Shdr);
1714   EHdr->e_shnum = OutputSections.size() + 1;
1715   EHdr->e_shstrndx = In<ELFT>::ShStrTab->OutSec->SectionIndex;
1716 
1717   if (Config->EMachine == EM_ARM)
1718     // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
1719     // but we don't have any firm guarantees of conformance. Linux AArch64
1720     // kernels (as of 2016) require an EABI version to be set.
1721     EHdr->e_flags = EF_ARM_EABI_VER5;
1722   else if (Config->EMachine == EM_MIPS)
1723     EHdr->e_flags = getMipsEFlags<ELFT>();
1724 
1725   if (!Config->Relocatable) {
1726     EHdr->e_phoff = sizeof(Elf_Ehdr);
1727     EHdr->e_phentsize = sizeof(Elf_Phdr);
1728   }
1729 
1730   // Write the program header table.
1731   auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1732   for (PhdrEntry &P : Phdrs) {
1733     HBuf->p_type = P.p_type;
1734     HBuf->p_flags = P.p_flags;
1735     HBuf->p_offset = P.p_offset;
1736     HBuf->p_vaddr = P.p_vaddr;
1737     HBuf->p_paddr = P.p_paddr;
1738     HBuf->p_filesz = P.p_filesz;
1739     HBuf->p_memsz = P.p_memsz;
1740     HBuf->p_align = P.p_align;
1741     ++HBuf;
1742   }
1743 
1744   // Write the section header table. Note that the first table entry is null.
1745   auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1746   for (OutputSection *Sec : OutputSections)
1747     Sec->writeHeaderTo<ELFT>(++SHdrs);
1748 }
1749 
1750 // Removes a given file asynchronously. This is a performance hack,
1751 // so remove this when operating systems are improved.
1752 //
1753 // On Linux (and probably on other Unix-like systems), unlink(2) is a
1754 // noticeably slow system call. As of 2016, unlink takes 250
1755 // milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
1756 //
1757 // To create a new result file, we first remove existing file. So, if
1758 // you repeatedly link a 1 GB program in a regular compile-link-debug
1759 // cycle, every cycle wastes 250 milliseconds only to remove a file.
1760 // Since LLD can link a 1 GB binary in about 5 seconds, that waste
1761 // actually counts.
1762 //
1763 // This function spawns a background thread to call unlink.
1764 // The calling thread returns almost immediately.
1765 static void unlinkAsync(StringRef Path) {
1766   if (!Config->Threads || !sys::fs::exists(Config->OutputFile))
1767     return;
1768 
1769   // First, rename Path to avoid race condition. We cannot remove
1770   // Path from a different thread because we are now going to create
1771   // Path as a new file. If we do that in a different thread, the new
1772   // thread can remove the new file.
1773   SmallString<128> TempPath;
1774   if (sys::fs::createUniqueFile(Path + "tmp%%%%%%%%", TempPath))
1775     return;
1776   if (sys::fs::rename(Path, TempPath)) {
1777     sys::fs::remove(TempPath);
1778     return;
1779   }
1780 
1781   // Remove TempPath in background.
1782   std::thread([=] { ::remove(TempPath.str().str().c_str()); }).detach();
1783 }
1784 
1785 // Open a result file.
1786 template <class ELFT> void Writer<ELFT>::openFile() {
1787   unlinkAsync(Config->OutputFile);
1788   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1789       FileOutputBuffer::create(Config->OutputFile, FileSize,
1790                                FileOutputBuffer::F_executable);
1791 
1792   if (auto EC = BufferOrErr.getError())
1793     error("failed to open " + Config->OutputFile + ": " + EC.message());
1794   else
1795     Buffer = std::move(*BufferOrErr);
1796 }
1797 
1798 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
1799   uint8_t *Buf = Buffer->getBufferStart();
1800   for (OutputSection *Sec : OutputSections)
1801     if (Sec->Flags & SHF_ALLOC)
1802       Sec->writeTo<ELFT>(Buf + Sec->Offset);
1803 }
1804 
1805 // Write section contents to a mmap'ed file.
1806 template <class ELFT> void Writer<ELFT>::writeSections() {
1807   uint8_t *Buf = Buffer->getBufferStart();
1808 
1809   // PPC64 needs to process relocations in the .opd section
1810   // before processing relocations in code-containing sections.
1811   Out::Opd = findSection(".opd");
1812   if (Out::Opd) {
1813     Out::OpdBuf = Buf + Out::Opd->Offset;
1814     Out::Opd->template writeTo<ELFT>(Buf + Out::Opd->Offset);
1815   }
1816 
1817   OutputSection *EhFrameHdr =
1818       In<ELFT>::EhFrameHdr ? In<ELFT>::EhFrameHdr->OutSec : nullptr;
1819 
1820   // In -r or -emit-relocs mode, write the relocation sections first as in
1821   // ELf_Rel targets we might find out that we need to modify the relocated
1822   // section while doing it.
1823   for (OutputSection *Sec : OutputSections)
1824     if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
1825       Sec->writeTo<ELFT>(Buf + Sec->Offset);
1826 
1827   for (OutputSection *Sec : OutputSections)
1828     if (Sec != Out::Opd && Sec != EhFrameHdr && Sec->Type != SHT_REL &&
1829         Sec->Type != SHT_RELA)
1830       Sec->writeTo<ELFT>(Buf + Sec->Offset);
1831 
1832   // The .eh_frame_hdr depends on .eh_frame section contents, therefore
1833   // it should be written after .eh_frame is written.
1834   if (EhFrameHdr)
1835     EhFrameHdr->writeTo<ELFT>(Buf + EhFrameHdr->Offset);
1836 }
1837 
1838 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1839   if (!In<ELFT>::BuildId || !In<ELFT>::BuildId->OutSec)
1840     return;
1841 
1842   // Compute a hash of all sections of the output file.
1843   uint8_t *Start = Buffer->getBufferStart();
1844   uint8_t *End = Start + FileSize;
1845   In<ELFT>::BuildId->writeBuildId({Start, End});
1846 }
1847 
1848 template void elf::writeResult<ELF32LE>();
1849 template void elf::writeResult<ELF32BE>();
1850 template void elf::writeResult<ELF64LE>();
1851 template void elf::writeResult<ELF64BE>();
1852 
1853 template bool elf::isRelroSection<ELF32LE>(const OutputSection *);
1854 template bool elf::isRelroSection<ELF32BE>(const OutputSection *);
1855 template bool elf::isRelroSection<ELF64LE>(const OutputSection *);
1856 template bool elf::isRelroSection<ELF64BE>(const OutputSection *);
1857