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