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