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