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