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