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