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