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