xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision 88d10b68)
1 //===- Writer.cpp ---------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Writer.h"
11 #include "Config.h"
12 #include "LinkerScript.h"
13 #include "OutputSections.h"
14 #include "Relocations.h"
15 #include "Strings.h"
16 #include "SymbolTable.h"
17 #include "Target.h"
18 
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Support/FileOutputBuffer.h"
22 #include "llvm/Support/StringSaver.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <climits>
25 
26 using namespace llvm;
27 using namespace llvm::ELF;
28 using namespace llvm::object;
29 using namespace llvm::support;
30 using namespace llvm::support::endian;
31 
32 using namespace lld;
33 using namespace lld::elf;
34 
35 namespace {
36 // The writer writes a SymbolTable result to a file.
37 template <class ELFT> class Writer {
38 public:
39   typedef typename ELFT::uint uintX_t;
40   typedef typename ELFT::Shdr Elf_Shdr;
41   typedef typename ELFT::Ehdr Elf_Ehdr;
42   typedef typename ELFT::Phdr Elf_Phdr;
43   typedef typename ELFT::Sym Elf_Sym;
44   typedef typename ELFT::SymRange Elf_Sym_Range;
45   typedef typename ELFT::Rela Elf_Rela;
46   void run();
47 
48 private:
49   typedef PhdrEntry<ELFT> Phdr;
50 
51   void copyLocalSymbols();
52   void addReservedSymbols();
53   void createSections();
54   void forEachRelSec(
55       std::function<void(InputSectionBase<ELFT> &, const typename ELFT::Shdr &)>
56           Fn);
57   void sortSections();
58   void finalizeSections();
59   void addPredefinedSections();
60   bool needsGot();
61 
62   std::vector<Phdr> createPhdrs();
63   void assignAddresses();
64   void assignFileOffsets();
65   void assignFileOffsetsBinary();
66   void setPhdrs();
67   void fixHeaders();
68   void fixSectionAlignments();
69   void fixAbsoluteSymbols();
70   void openFile();
71   void writeHeader();
72   void writeSections();
73   void writeSectionsBinary();
74   void writeBuildId();
75 
76   std::unique_ptr<FileOutputBuffer> Buffer;
77 
78   BumpPtrAllocator Alloc;
79   std::vector<OutputSectionBase<ELFT> *> OutputSections;
80   OutputSectionFactory<ELFT> Factory;
81 
82   void addRelIpltSymbols();
83   void addStartEndSymbols();
84   void addStartStopSymbols(OutputSectionBase<ELFT> *Sec);
85   OutputSectionBase<ELFT> *findSection(StringRef Name);
86 
87   std::vector<Phdr> Phdrs;
88 
89   uintX_t FileSize;
90   uintX_t SectionHeaderOff;
91 };
92 } // anonymous namespace
93 
94 StringRef elf::getOutputSectionName(StringRef Name, BumpPtrAllocator &Alloc) {
95   if (Config->Relocatable)
96     return Name;
97 
98   for (StringRef V :
99        {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.",
100         ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
101         ".gcc_except_table.", ".tdata.", ".ARM.exidx."}) {
102     StringRef Prefix = V.drop_back();
103     if (Name.startswith(V) || Name == Prefix)
104       return Prefix;
105   }
106 
107   // ".zdebug_" is a prefix for ZLIB-compressed sections.
108   // Because we decompressed input sections, we want to remove 'z'.
109   if (Name.startswith(".zdebug_"))
110     return StringSaver(Alloc).save(Twine(".") + Name.substr(2));
111   return Name;
112 }
113 
114 template <class ELFT> void elf::reportDiscarded(InputSectionBase<ELFT> *IS) {
115   if (!Config->PrintGcSections || !IS || IS == &InputSection<ELFT>::Discarded ||
116       IS->Live)
117     return;
118   errs() << "removing unused section from '" << IS->Name << "' in file '"
119          << IS->getFile()->getName() << "'\n";
120 }
121 
122 template <class ELFT> static bool needsInterpSection() {
123   return !Symtab<ELFT>::X->getSharedFiles().empty() &&
124          !Config->DynamicLinker.empty() &&
125          !Script<ELFT>::X->ignoreInterpSection();
126 }
127 
128 template <class ELFT> void elf::writeResult() {
129   typedef typename ELFT::uint uintX_t;
130   typedef typename ELFT::Ehdr Elf_Ehdr;
131 
132   // Create singleton output sections.
133   OutputSection<ELFT> Bss(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
134   DynamicSection<ELFT> Dynamic;
135   EhOutputSection<ELFT> EhFrame;
136   GotSection<ELFT> Got;
137   PltSection<ELFT> Plt;
138   RelocationSection<ELFT> RelaDyn(Config->Rela ? ".rela.dyn" : ".rel.dyn",
139                                   Config->ZCombreloc);
140   StringTableSection<ELFT> ShStrTab(".shstrtab", false);
141   VersionTableSection<ELFT> VerSym;
142   VersionNeedSection<ELFT> VerNeed;
143 
144   OutputSectionBase<ELFT> ElfHeader("", 0, SHF_ALLOC);
145   ElfHeader.setSize(sizeof(Elf_Ehdr));
146   OutputSectionBase<ELFT> ProgramHeaders("", 0, SHF_ALLOC);
147   ProgramHeaders.updateAlignment(sizeof(uintX_t));
148 
149   // Instantiate optional output sections if they are needed.
150   std::unique_ptr<InterpSection<ELFT>> Interp;
151   std::unique_ptr<BuildIdSection<ELFT>> BuildId;
152   std::unique_ptr<StringTableSection<ELFT>> DynStrTab;
153   std::unique_ptr<SymbolTableSection<ELFT>> DynSymTab;
154   std::unique_ptr<EhFrameHeader<ELFT>> EhFrameHdr;
155   std::unique_ptr<GnuHashTableSection<ELFT>> GnuHashTab;
156   std::unique_ptr<GotPltSection<ELFT>> GotPlt;
157   std::unique_ptr<HashTableSection<ELFT>> HashTab;
158   std::unique_ptr<RelocationSection<ELFT>> RelaPlt;
159   std::unique_ptr<StringTableSection<ELFT>> StrTab;
160   std::unique_ptr<SymbolTableSection<ELFT>> SymTabSec;
161   std::unique_ptr<OutputSection<ELFT>> MipsRldMap;
162   std::unique_ptr<VersionDefinitionSection<ELFT>> VerDef;
163 
164   if (needsInterpSection<ELFT>())
165     Interp.reset(new InterpSection<ELFT>);
166 
167   if (Config->BuildId == BuildIdKind::Fast)
168     BuildId.reset(new BuildIdFastHash<ELFT>);
169   else if (Config->BuildId == BuildIdKind::Md5)
170     BuildId.reset(new BuildIdMd5<ELFT>);
171   else if (Config->BuildId == BuildIdKind::Sha1)
172     BuildId.reset(new BuildIdSha1<ELFT>);
173   else if (Config->BuildId == BuildIdKind::Uuid)
174     BuildId.reset(new BuildIdUuid<ELFT>);
175   else if (Config->BuildId == BuildIdKind::Hexstring)
176     BuildId.reset(new BuildIdHexstring<ELFT>);
177 
178   if (!Symtab<ELFT>::X->getSharedFiles().empty() || Config->Pic) {
179     DynStrTab.reset(new StringTableSection<ELFT>(".dynstr", true));
180     DynSymTab.reset(new SymbolTableSection<ELFT>(*DynStrTab));
181   }
182 
183   if (Config->EhFrameHdr)
184     EhFrameHdr.reset(new EhFrameHeader<ELFT>);
185 
186   if (Config->GnuHash)
187     GnuHashTab.reset(new GnuHashTableSection<ELFT>);
188   if (Config->SysvHash)
189     HashTab.reset(new HashTableSection<ELFT>);
190   StringRef S = Config->Rela ? ".rela.plt" : ".rel.plt";
191   GotPlt.reset(new GotPltSection<ELFT>);
192   RelaPlt.reset(new RelocationSection<ELFT>(S, false /*Sort*/));
193   if (Config->Strip != StripPolicy::All) {
194     StrTab.reset(new StringTableSection<ELFT>(".strtab", false));
195     SymTabSec.reset(new SymbolTableSection<ELFT>(*StrTab));
196   }
197   if (Config->EMachine == EM_MIPS && !Config->Shared) {
198     // This is a MIPS specific section to hold a space within the data segment
199     // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
200     // See "Dynamic section" in Chapter 5 in the following document:
201     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
202     MipsRldMap.reset(new OutputSection<ELFT>(".rld_map", SHT_PROGBITS,
203                                              SHF_ALLOC | SHF_WRITE));
204     MipsRldMap->setSize(sizeof(uintX_t));
205     MipsRldMap->updateAlignment(sizeof(uintX_t));
206   }
207   if (!Config->VersionDefinitions.empty())
208     VerDef.reset(new VersionDefinitionSection<ELFT>());
209 
210   Out<ELFT>::Bss = &Bss;
211   Out<ELFT>::BuildId = BuildId.get();
212   Out<ELFT>::DynStrTab = DynStrTab.get();
213   Out<ELFT>::DynSymTab = DynSymTab.get();
214   Out<ELFT>::Dynamic = &Dynamic;
215   Out<ELFT>::EhFrame = &EhFrame;
216   Out<ELFT>::EhFrameHdr = EhFrameHdr.get();
217   Out<ELFT>::GnuHashTab = GnuHashTab.get();
218   Out<ELFT>::Got = &Got;
219   Out<ELFT>::GotPlt = GotPlt.get();
220   Out<ELFT>::HashTab = HashTab.get();
221   Out<ELFT>::Interp = Interp.get();
222   Out<ELFT>::Plt = &Plt;
223   Out<ELFT>::RelaDyn = &RelaDyn;
224   Out<ELFT>::RelaPlt = RelaPlt.get();
225   Out<ELFT>::ShStrTab = &ShStrTab;
226   Out<ELFT>::StrTab = StrTab.get();
227   Out<ELFT>::SymTab = SymTabSec.get();
228   Out<ELFT>::VerDef = VerDef.get();
229   Out<ELFT>::VerSym = &VerSym;
230   Out<ELFT>::VerNeed = &VerNeed;
231   Out<ELFT>::MipsRldMap = MipsRldMap.get();
232   Out<ELFT>::Opd = nullptr;
233   Out<ELFT>::OpdBuf = nullptr;
234   Out<ELFT>::TlsPhdr = nullptr;
235   Out<ELFT>::ElfHeader = &ElfHeader;
236   Out<ELFT>::ProgramHeaders = &ProgramHeaders;
237 
238   Out<ELFT>::PreinitArray = nullptr;
239   Out<ELFT>::InitArray = nullptr;
240   Out<ELFT>::FiniArray = nullptr;
241 
242   Writer<ELFT>().run();
243   Out<ELFT>::Pool.clear();
244 }
245 
246 template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() {
247   std::vector<DefinedCommon *> V;
248   for (Symbol *S : Symtab<ELFT>::X->getSymbols())
249     if (auto *B = dyn_cast<DefinedCommon>(S->body()))
250       V.push_back(B);
251   return V;
252 }
253 
254 // The main function of the writer.
255 template <class ELFT> void Writer<ELFT>::run() {
256   addReservedSymbols();
257 
258   if (Target->NeedsThunks)
259     forEachRelSec(createThunks<ELFT>);
260 
261   CommonInputSection<ELFT> Common(getCommonSymbols<ELFT>());
262   CommonInputSection<ELFT>::X = &Common;
263 
264   Script<ELFT>::X->OutputSections = &OutputSections;
265   if (ScriptConfig->HasSections) {
266     Script<ELFT>::X->createSections(Factory);
267   } else {
268     createSections();
269     Script<ELFT>::X->processCommands(Factory);
270   }
271 
272   if (Config->Discard != DiscardPolicy::All)
273     copyLocalSymbols();
274 
275   finalizeSections();
276   if (HasError)
277     return;
278 
279   if (Config->Relocatable) {
280     assignFileOffsets();
281   } else {
282     Phdrs = Script<ELFT>::X->hasPhdrsCommands() ? Script<ELFT>::X->createPhdrs()
283                                                 : createPhdrs();
284     fixHeaders();
285     if (ScriptConfig->HasSections) {
286       Script<ELFT>::X->assignAddresses(Phdrs);
287     } else {
288       fixSectionAlignments();
289       assignAddresses();
290     }
291 
292     if (!Config->OFormatBinary)
293       assignFileOffsets();
294     else
295       assignFileOffsetsBinary();
296 
297     setPhdrs();
298     fixAbsoluteSymbols();
299   }
300 
301   openFile();
302   if (HasError)
303     return;
304   if (!Config->OFormatBinary) {
305     writeHeader();
306     writeSections();
307   } else {
308     writeSectionsBinary();
309   }
310   writeBuildId();
311   if (HasError)
312     return;
313   if (auto EC = Buffer->commit())
314     error(EC, "failed to write to the output file");
315 }
316 
317 template <class ELFT>
318 static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName,
319                                const SymbolBody &B) {
320   if (B.isFile())
321     return false;
322 
323   // We keep sections in symtab for relocatable output.
324   if (B.isSection())
325     return Config->Relocatable;
326 
327   // If sym references a section in a discarded group, don't keep it.
328   if (Sec == &InputSection<ELFT>::Discarded)
329     return false;
330 
331   if (Config->Discard == DiscardPolicy::None)
332     return true;
333 
334   // In ELF assembly .L symbols are normally discarded by the assembler.
335   // If the assembler fails to do so, the linker discards them if
336   // * --discard-locals is used.
337   // * The symbol is in a SHF_MERGE section, which is normally the reason for
338   //   the assembler keeping the .L symbol.
339   if (!SymName.startswith(".L") && !SymName.empty())
340     return true;
341 
342   if (Config->Discard == DiscardPolicy::Locals)
343     return false;
344 
345   return !Sec || !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
346 }
347 
348 template <class ELFT> static bool includeInSymtab(const SymbolBody &B) {
349   if (!B.isLocal() && !B.symbol()->IsUsedInRegularObj)
350     return false;
351 
352   if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) {
353     // Always include absolute symbols.
354     if (!D->Section)
355       return true;
356     // Exclude symbols pointing to garbage-collected sections.
357     if (!D->Section->Live)
358       return false;
359     if (auto *S = dyn_cast<MergeInputSection<ELFT>>(D->Section))
360       if (!S->getSectionPiece(D->Value)->Live)
361         return false;
362   }
363   return true;
364 }
365 
366 // Local symbols are not in the linker's symbol table. This function scans
367 // each object file's symbol table to copy local symbols to the output.
368 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
369   if (!Out<ELFT>::SymTab)
370     return;
371   for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
372     StringRef StrTab = F->getStringTable();
373     for (SymbolBody *B : F->getLocalSymbols()) {
374       if (!B->IsLocal)
375         fatal(getFilename(F) +
376               ": broken object: getLocalSymbols returns a non-local symbol");
377       auto *DR = dyn_cast<DefinedRegular<ELFT>>(B);
378       // No reason to keep local undefined symbol in symtab.
379       if (!DR)
380         continue;
381       if (!includeInSymtab<ELFT>(*B))
382         continue;
383       if (B->getNameOffset() >= StrTab.size())
384         fatal(getFilename(F) + ": invalid symbol name offset");
385       StringRef SymName(StrTab.data() + B->getNameOffset());
386       InputSectionBase<ELFT> *Sec = DR->Section;
387       if (!shouldKeepInSymtab<ELFT>(Sec, SymName, *B))
388         continue;
389       ++Out<ELFT>::SymTab->NumLocals;
390       if (Config->Relocatable)
391         B->DynsymIndex = Out<ELFT>::SymTab->NumLocals;
392       F->KeptLocalSyms.push_back(
393           std::make_pair(DR, Out<ELFT>::SymTab->StrTabSec.addString(SymName)));
394     }
395   }
396 }
397 
398 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
399 // we would like to make sure appear is a specific order to maximize their
400 // coverage by a single signed 16-bit offset from the TOC base pointer.
401 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
402 // sections. This will put it next to the loaded special PPC64 sections (and,
403 // thus, within reach of the TOC base pointer).
404 static int getPPC64SectionRank(StringRef SectionName) {
405   return StringSwitch<int>(SectionName)
406       .Case(".tocbss", 0)
407       .Case(".branch_lt", 2)
408       .Case(".toc", 3)
409       .Case(".toc1", 4)
410       .Case(".opd", 5)
411       .Default(1);
412 }
413 
414 template <class ELFT> bool elf::isRelroSection(OutputSectionBase<ELFT> *Sec) {
415   if (!Config->ZRelro)
416     return false;
417   typename ELFT::uint Flags = Sec->getFlags();
418   if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
419     return false;
420   if (Flags & SHF_TLS)
421     return true;
422   uint32_t Type = Sec->getType();
423   if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
424       Type == SHT_PREINIT_ARRAY)
425     return true;
426   if (Sec == Out<ELFT>::GotPlt)
427     return Config->ZNow;
428   if (Sec == Out<ELFT>::Dynamic || Sec == Out<ELFT>::Got)
429     return true;
430   StringRef S = Sec->getName();
431   return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
432          S == ".eh_frame";
433 }
434 
435 template <class ELFT>
436 static bool compareSectionsNonScript(OutputSectionBase<ELFT> *A,
437                                      OutputSectionBase<ELFT> *B) {
438   typedef typename ELFT::uint uintX_t;
439   uintX_t AFlags = A->getFlags();
440   uintX_t BFlags = B->getFlags();
441 
442   // Allocatable sections go first to reduce the total PT_LOAD size and
443   // so debug info doesn't change addresses in actual code.
444   bool AIsAlloc = AFlags & SHF_ALLOC;
445   bool BIsAlloc = BFlags & SHF_ALLOC;
446   if (AIsAlloc != BIsAlloc)
447     return AIsAlloc;
448 
449   // We don't have any special requirements for the relative order of two non
450   // allocatable sections.
451   if (!AIsAlloc)
452     return false;
453 
454   // We want the read only sections first so that they go in the PT_LOAD
455   // covering the program headers at the start of the file.
456   bool AIsWritable = AFlags & SHF_WRITE;
457   bool BIsWritable = BFlags & SHF_WRITE;
458   if (AIsWritable != BIsWritable)
459     return BIsWritable;
460 
461   if (!ScriptConfig->HasSections) {
462     // For a corresponding reason, put non exec sections first (the program
463     // header PT_LOAD is not executable).
464     // We only do that if we are not using linker scripts, since with linker
465     // scripts ro and rx sections are in the same PT_LOAD, so their relative
466     // order is not important.
467     bool AIsExec = AFlags & SHF_EXECINSTR;
468     bool BIsExec = BFlags & SHF_EXECINSTR;
469     if (AIsExec != BIsExec)
470       return BIsExec;
471   }
472 
473   // If we got here we know that both A and B are in the same PT_LOAD.
474 
475   // The TLS initialization block needs to be a single contiguous block in a R/W
476   // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS
477   // sections are placed here as they don't take up virtual address space in the
478   // PT_LOAD.
479   bool AIsTls = AFlags & SHF_TLS;
480   bool BIsTls = BFlags & SHF_TLS;
481   if (AIsTls != BIsTls)
482     return AIsTls;
483 
484   // The next requirement we have is to put nobits sections last. The
485   // reason is that the only thing the dynamic linker will see about
486   // them is a p_memsz that is larger than p_filesz. Seeing that it
487   // zeros the end of the PT_LOAD, so that has to correspond to the
488   // nobits sections.
489   bool AIsNoBits = A->getType() == SHT_NOBITS;
490   bool BIsNoBits = B->getType() == SHT_NOBITS;
491   if (AIsNoBits != BIsNoBits)
492     return BIsNoBits;
493 
494   // We place RelRo section before plain r/w ones.
495   bool AIsRelRo = isRelroSection(A);
496   bool BIsRelRo = isRelroSection(B);
497   if (AIsRelRo != BIsRelRo)
498     return AIsRelRo;
499 
500   // Some architectures have additional ordering restrictions for sections
501   // within the same PT_LOAD.
502   if (Config->EMachine == EM_PPC64)
503     return getPPC64SectionRank(A->getName()) <
504            getPPC64SectionRank(B->getName());
505 
506   return false;
507 }
508 
509 // Output section ordering is determined by this function.
510 template <class ELFT>
511 static bool compareSections(OutputSectionBase<ELFT> *A,
512                             OutputSectionBase<ELFT> *B) {
513   // For now, put sections mentioned in a linker script first.
514   int AIndex = Script<ELFT>::X->getSectionIndex(A->getName());
515   int BIndex = Script<ELFT>::X->getSectionIndex(B->getName());
516   bool AInScript = AIndex != INT_MAX;
517   bool BInScript = BIndex != INT_MAX;
518   if (AInScript != BInScript)
519     return AInScript;
520   // If both are in the script, use that order.
521   if (AInScript)
522     return AIndex < BIndex;
523 
524   return compareSectionsNonScript(A, B);
525 }
526 
527 template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
528   return !S || S == &InputSection<ELFT>::Discarded || !S->Live;
529 }
530 
531 // Program header entry
532 template<class ELFT>
533 PhdrEntry<ELFT>::PhdrEntry(unsigned Type, unsigned Flags) {
534   H.p_type = Type;
535   H.p_flags = Flags;
536 }
537 
538 template<class ELFT>
539 void PhdrEntry<ELFT>::add(OutputSectionBase<ELFT> *Sec) {
540   Last = Sec;
541   if (!First)
542     First = Sec;
543   H.p_align = std::max<typename ELFT::uint>(H.p_align, Sec->getAlignment());
544   if (H.p_type == PT_LOAD)
545     Sec->FirstInPtLoad = First;
546 }
547 
548 template <class ELFT>
549 static Symbol *addOptionalSynthetic(StringRef Name,
550                                     OutputSectionBase<ELFT> *Sec,
551                                     typename ELFT::uint Val) {
552   SymbolBody *S = Symtab<ELFT>::X->find(Name);
553   if (!S)
554     return nullptr;
555   if (!S->isUndefined() && !S->isShared())
556     return S->symbol();
557   return Symtab<ELFT>::X->addSynthetic(Name, Sec, Val, STV_HIDDEN);
558 }
559 
560 template <class ELFT>
561 static void addSynthetic(StringRef Name, OutputSectionBase<ELFT> *Sec,
562                          typename ELFT::uint Val) {
563   SymbolBody *S = Symtab<ELFT>::X->find(Name);
564   if (!S || S->isUndefined() || S->isShared())
565     Symtab<ELFT>::X->addSynthetic(Name, Sec, Val, STV_HIDDEN);
566 }
567 
568 // The beginning and the ending of .rel[a].plt section are marked
569 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
570 // executable. The runtime needs these symbols in order to resolve
571 // all IRELATIVE relocs on startup. For dynamic executables, we don't
572 // need these symbols, since IRELATIVE relocs are resolved through GOT
573 // and PLT. For details, see http://www.airs.com/blog/archives/403.
574 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
575   if (Out<ELFT>::DynSymTab || !Out<ELFT>::RelaPlt)
576     return;
577   StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start";
578   addOptionalSynthetic(S, Out<ELFT>::RelaPlt, 0);
579 
580   S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end";
581   addOptionalSynthetic(S, Out<ELFT>::RelaPlt,
582                        DefinedSynthetic<ELFT>::SectionEnd);
583 }
584 
585 // The linker is expected to define some symbols depending on
586 // the linking result. This function defines such symbols.
587 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
588   if (Config->EMachine == EM_MIPS && !Config->Relocatable) {
589     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
590     // so that it points to an absolute address which is relative to GOT.
591     // See "Global Data Symbols" in Chapter 6 in the following document:
592     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
593     Symtab<ELFT>::X->addSynthetic("_gp", Out<ELFT>::Got, MipsGPOffset,
594                                   STV_HIDDEN);
595 
596     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
597     // start of function and 'gp' pointer into GOT.
598     Symbol *Sym =
599         addOptionalSynthetic("_gp_disp", Out<ELFT>::Got, MipsGPOffset);
600     if (Sym)
601       ElfSym<ELFT>::MipsGpDisp = Sym->body();
602 
603     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
604     // pointer. This symbol is used in the code generated by .cpload pseudo-op
605     // in case of using -mno-shared option.
606     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
607     addOptionalSynthetic("__gnu_local_gp", Out<ELFT>::Got, MipsGPOffset);
608   }
609 
610   // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
611   // is magical and is used to produce a R_386_GOTPC relocation.
612   // The R_386_GOTPC relocation value doesn't actually depend on the
613   // symbol value, so it could use an index of STN_UNDEF which, according
614   // to the spec, means the symbol value is 0.
615   // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
616   // the object file.
617   // The situation is even stranger on x86_64 where the assembly doesn't
618   // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
619   // an undefined symbol in the .o files.
620   // Given that the symbol is effectively unused, we just create a dummy
621   // hidden one to avoid the undefined symbol error.
622   if (!Config->Relocatable)
623     Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");
624 
625   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
626   // static linking the linker is required to optimize away any references to
627   // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
628   // to avoid the undefined symbol error. As usual as special case is MIPS -
629   // MIPS libc defines __tls_get_addr itself because there are no TLS
630   // optimizations for this target.
631   if (!Out<ELFT>::DynSymTab && Config->EMachine != EM_MIPS)
632     Symtab<ELFT>::X->addIgnored("__tls_get_addr");
633 
634   // If linker script do layout we do not need to create any standart symbols.
635   if (ScriptConfig->HasSections)
636     return;
637 
638   ElfSym<ELFT>::EhdrStart = Symtab<ELFT>::X->addIgnored("__ehdr_start");
639 
640   auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1,
641                        DefinedRegular<ELFT> *&Sym2) {
642     Sym1 = Symtab<ELFT>::X->addIgnored(S, STV_DEFAULT);
643 
644     // The name without the underscore is not a reserved name,
645     // so it is defined only when there is a reference against it.
646     assert(S.startswith("_"));
647     S = S.substr(1);
648     if (SymbolBody *B = Symtab<ELFT>::X->find(S))
649       if (B->isUndefined())
650         Sym2 = Symtab<ELFT>::X->addAbsolute(S, STV_DEFAULT);
651   };
652 
653   Define("_end", ElfSym<ELFT>::End, ElfSym<ELFT>::End2);
654   Define("_etext", ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2);
655   Define("_edata", ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2);
656 }
657 
658 // Sort input sections by section name suffixes for
659 // __attribute__((init_priority(N))).
660 template <class ELFT> static void sortInitFini(OutputSectionBase<ELFT> *S) {
661   if (S)
662     reinterpret_cast<OutputSection<ELFT> *>(S)->sortInitFini();
663 }
664 
665 // Sort input sections by the special rule for .ctors and .dtors.
666 template <class ELFT> static void sortCtorsDtors(OutputSectionBase<ELFT> *S) {
667   if (S)
668     reinterpret_cast<OutputSection<ELFT> *>(S)->sortCtorsDtors();
669 }
670 
671 template <class ELFT>
672 void Writer<ELFT>::forEachRelSec(
673     std::function<void(InputSectionBase<ELFT> &, const typename ELFT::Shdr &)>
674         Fn) {
675   for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
676     for (InputSectionBase<ELFT> *IS : F->getSections()) {
677       if (isDiscarded(IS))
678         continue;
679       // Scan all relocations. Each relocation goes through a series
680       // of tests to determine if it needs special treatment, such as
681       // creating GOT, PLT, copy relocations, etc.
682       // Note that relocations for non-alloc sections are directly
683       // processed by InputSection::relocateNonAlloc.
684       if (!(IS->getSectionHdr()->sh_flags & SHF_ALLOC))
685         continue;
686       if (auto *S = dyn_cast<InputSection<ELFT>>(IS)) {
687         for (const Elf_Shdr *RelSec : S->RelocSections)
688           Fn(*S, *RelSec);
689         continue;
690       }
691       if (auto *S = dyn_cast<EhInputSection<ELFT>>(IS))
692         if (S->RelocSection)
693           Fn(*S, *S->RelocSection);
694     }
695   }
696 }
697 
698 template <class ELFT> void Writer<ELFT>::createSections() {
699   for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
700     for (InputSectionBase<ELFT> *IS : F->getSections()) {
701       if (isDiscarded(IS)) {
702         reportDiscarded(IS);
703         continue;
704       }
705       OutputSectionBase<ELFT> *Sec;
706       bool IsNew;
707       StringRef OutsecName = getOutputSectionName(IS->Name, Alloc);
708       std::tie(Sec, IsNew) = Factory.create(IS, OutsecName);
709       if (IsNew)
710         OutputSections.push_back(Sec);
711       Sec->addSection(IS);
712     }
713   }
714 
715   sortInitFini(findSection(".init_array"));
716   sortInitFini(findSection(".fini_array"));
717   sortCtorsDtors(findSection(".ctors"));
718   sortCtorsDtors(findSection(".dtors"));
719 
720   for (OutputSectionBase<ELFT> *Sec : OutputSections)
721     Sec->assignOffsets();
722 }
723 
724 template <class ELFT> void Writer<ELFT>::sortSections() {
725   if (!ScriptConfig->HasSections) {
726     std::stable_sort(OutputSections.begin(), OutputSections.end(),
727                      compareSectionsNonScript<ELFT>);
728     return;
729   }
730   Script<ELFT>::X->adjustSectionsBeforeSorting();
731 
732   // The order of the sections in the script is arbitrary and may not agree with
733   // compareSectionsNonScript. This means that we cannot easily define a
734   // strict weak ordering. To see why, consider a comparison of a section in the
735   // script and one not in the script. We have a two simple options:
736   // * Make them equivalent (a is not less than b, and b is not less than a).
737   //   The problem is then that equivalence has to be transitive and we can
738   //   have sections a, b and c with only b in a script and a less than c
739   //   which breaks this property.
740   // * Use compareSectionsNonScript. Given that the script order doesn't have
741   //   to match, we can end up with sections a, b, c, d where b and c are in the
742   //   script and c is compareSectionsNonScript less than b. In which case d
743   //   can be equivalent to c, a to b and d < a. As a concrete example:
744   //   .a (rx) # not in script
745   //   .b (rx) # in script
746   //   .c (ro) # in script
747   //   .d (ro) # not in script
748   //
749   // The way we define an order then is:
750   // *  First put script sections at the start and sort the script and
751   //    non-script sections independently.
752   // *  Move each non-script section to the first position where it
753   //    compareSectionsNonScript less than the successor.
754 
755   std::stable_sort(OutputSections.begin(), OutputSections.end(),
756                    compareSections<ELFT>);
757 
758   auto I = OutputSections.begin();
759   auto E = OutputSections.end();
760   auto NonScriptI = std::find_if(I, E, [](OutputSectionBase<ELFT> *S) {
761     return Script<ELFT>::X->getSectionIndex(S->getName()) == INT_MAX;
762   });
763   while (NonScriptI != E) {
764     auto FirstGreater =
765         std::find_if(I, NonScriptI, [&](OutputSectionBase<ELFT> *S) {
766           return compareSectionsNonScript<ELFT>(*NonScriptI, S);
767         });
768     std::rotate(FirstGreater, NonScriptI, NonScriptI + 1);
769     ++NonScriptI;
770     ++I;
771   }
772 }
773 
774 // Create output section objects and add them to OutputSections.
775 template <class ELFT> void Writer<ELFT>::finalizeSections() {
776   Out<ELFT>::PreinitArray = findSection(".preinit_array");
777   Out<ELFT>::InitArray = findSection(".init_array");
778   Out<ELFT>::FiniArray = findSection(".fini_array");
779 
780   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
781   // symbols for sections, so that the runtime can get the start and end
782   // addresses of each section by section name. Add such symbols.
783   if (!Config->Relocatable) {
784     addStartEndSymbols();
785     for (OutputSectionBase<ELFT> *Sec : OutputSections)
786       addStartStopSymbols(Sec);
787   }
788 
789   // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
790   // It should be okay as no one seems to care about the type.
791   // Even the author of gold doesn't remember why gold behaves that way.
792   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
793   if (Out<ELFT>::DynSymTab)
794     Symtab<ELFT>::X->addSynthetic("_DYNAMIC", Out<ELFT>::Dynamic, 0,
795                                   STV_HIDDEN);
796 
797   // Define __rel[a]_iplt_{start,end} symbols if needed.
798   addRelIpltSymbols();
799 
800   if (!Out<ELFT>::EhFrame->empty()) {
801     OutputSections.push_back(Out<ELFT>::EhFrame);
802     Out<ELFT>::EhFrame->finalize();
803   }
804 
805   // Scan relocations. This must be done after every symbol is declared so that
806   // we can correctly decide if a dynamic relocation is needed.
807   forEachRelSec(scanRelocations<ELFT>);
808 
809   // Now that we have defined all possible symbols including linker-
810   // synthesized ones. Visit all symbols to give the finishing touches.
811   for (Symbol *S : Symtab<ELFT>::X->getSymbols()) {
812     SymbolBody *Body = S->body();
813 
814     if (!includeInSymtab<ELFT>(*Body))
815       continue;
816     if (Out<ELFT>::SymTab)
817       Out<ELFT>::SymTab->addSymbol(Body);
818 
819     if (Out<ELFT>::DynSymTab && S->includeInDynsym()) {
820       Out<ELFT>::DynSymTab->addSymbol(Body);
821       if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
822         if (SS->file()->isNeeded())
823           Out<ELFT>::VerNeed->addSymbol(SS);
824     }
825   }
826 
827   // Do not proceed if there was an undefined symbol.
828   if (HasError)
829     return;
830 
831   // If linker script processor hasn't added common symbol section yet,
832   // then add it to .bss now.
833   if (!CommonInputSection<ELFT>::X->OutSec) {
834     Out<ELFT>::Bss->addSection(CommonInputSection<ELFT>::X);
835     Out<ELFT>::Bss->assignOffsets();
836   }
837 
838   // So far we have added sections from input object files.
839   // This function adds linker-created Out<ELFT>::* sections.
840   addPredefinedSections();
841 
842   sortSections();
843 
844   unsigned I = 1;
845   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
846     Sec->SectionIndex = I++;
847     Sec->setSHName(Out<ELFT>::ShStrTab->addString(Sec->getName()));
848   }
849 
850   // Finalizers fix each section's size.
851   // .dynsym is finalized early since that may fill up .gnu.hash.
852   if (Out<ELFT>::DynSymTab)
853     Out<ELFT>::DynSymTab->finalize();
854 
855   // Fill other section headers. The dynamic table is finalized
856   // at the end because some tags like RELSZ depend on result
857   // of finalizing other sections. The dynamic string table is
858   // finalized once the .dynamic finalizer has added a few last
859   // strings. See DynamicSection::finalize()
860   for (OutputSectionBase<ELFT> *Sec : OutputSections)
861     if (Sec != Out<ELFT>::DynStrTab && Sec != Out<ELFT>::Dynamic)
862       Sec->finalize();
863 
864   if (Out<ELFT>::DynSymTab)
865     Out<ELFT>::Dynamic->finalize();
866 
867   // Now that all output offsets are fixed. Finalize mergeable sections
868   // to fix their maps from input offsets to output offsets.
869   for (OutputSectionBase<ELFT> *Sec : OutputSections)
870     Sec->finalizePieces();
871 }
872 
873 template <class ELFT> bool Writer<ELFT>::needsGot() {
874   if (!Out<ELFT>::Got->empty())
875     return true;
876 
877   // We add the .got section to the result for dynamic MIPS target because
878   // its address and properties are mentioned in the .dynamic section.
879   if (Config->EMachine == EM_MIPS && !Config->Relocatable)
880     return true;
881 
882   // If we have a relocation that is relative to GOT (such as GOTOFFREL),
883   // we need to emit a GOT even if it's empty.
884   return Out<ELFT>::Got->HasGotOffRel;
885 }
886 
887 // This function add Out<ELFT>::* sections to OutputSections.
888 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
889   auto Add = [&](OutputSectionBase<ELFT> *OS) {
890     if (OS)
891       OutputSections.push_back(OS);
892   };
893 
894   // A core file does not usually contain unmodified segments except
895   // the first page of the executable. Add the build ID section to beginning of
896   // the file so that the section is included in the first page.
897   if (Out<ELFT>::BuildId)
898     OutputSections.insert(OutputSections.begin(), Out<ELFT>::BuildId);
899 
900   // Add .interp at first because some loaders want to see that section
901   // on the first page of the executable file when loaded into memory.
902   if (Out<ELFT>::Interp)
903     OutputSections.insert(OutputSections.begin(), Out<ELFT>::Interp);
904 
905   // This order is not the same as the final output order
906   // because we sort the sections using their attributes below.
907   Add(Out<ELFT>::SymTab);
908   Add(Out<ELFT>::ShStrTab);
909   Add(Out<ELFT>::StrTab);
910   if (Out<ELFT>::DynSymTab) {
911     Add(Out<ELFT>::DynSymTab);
912 
913     bool HasVerNeed = Out<ELFT>::VerNeed->getNeedNum() != 0;
914     if (Out<ELFT>::VerDef || HasVerNeed)
915       Add(Out<ELFT>::VerSym);
916     Add(Out<ELFT>::VerDef);
917     if (HasVerNeed)
918       Add(Out<ELFT>::VerNeed);
919 
920     Add(Out<ELFT>::GnuHashTab);
921     Add(Out<ELFT>::HashTab);
922     Add(Out<ELFT>::Dynamic);
923     Add(Out<ELFT>::DynStrTab);
924     if (Out<ELFT>::RelaDyn->hasRelocs())
925       Add(Out<ELFT>::RelaDyn);
926     Add(Out<ELFT>::MipsRldMap);
927   }
928 
929   // We always need to add rel[a].plt to output if it has entries.
930   // Even during static linking it can contain R_[*]_IRELATIVE relocations.
931   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs())
932     Add(Out<ELFT>::RelaPlt);
933 
934   if (needsGot())
935     Add(Out<ELFT>::Got);
936   if (Out<ELFT>::GotPlt && !Out<ELFT>::GotPlt->empty())
937     Add(Out<ELFT>::GotPlt);
938   if (!Out<ELFT>::Plt->empty())
939     Add(Out<ELFT>::Plt);
940   if (!Out<ELFT>::EhFrame->empty())
941     Add(Out<ELFT>::EhFrameHdr);
942   if (Out<ELFT>::Bss->getSize() > 0)
943     Add(Out<ELFT>::Bss);
944 }
945 
946 // The linker is expected to define SECNAME_start and SECNAME_end
947 // symbols for a few sections. This function defines them.
948 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
949   auto Define = [&](StringRef Start, StringRef End,
950                     OutputSectionBase<ELFT> *OS) {
951     if (OS) {
952       addSynthetic(Start, OS, 0);
953       addSynthetic(End, OS, DefinedSynthetic<ELFT>::SectionEnd);
954     } else {
955       addOptionalSynthetic(Start, (OutputSectionBase<ELFT> *)nullptr, 0);
956       addOptionalSynthetic(End, (OutputSectionBase<ELFT> *)nullptr, 0);
957     }
958   };
959 
960   Define("__preinit_array_start", "__preinit_array_end",
961          Out<ELFT>::PreinitArray);
962   Define("__init_array_start", "__init_array_end", Out<ELFT>::InitArray);
963   Define("__fini_array_start", "__fini_array_end", Out<ELFT>::FiniArray);
964 }
965 
966 // If a section name is valid as a C identifier (which is rare because of
967 // the leading '.'), linkers are expected to define __start_<secname> and
968 // __stop_<secname> symbols. They are at beginning and end of the section,
969 // respectively. This is not requested by the ELF standard, but GNU ld and
970 // gold provide the feature, and used by many programs.
971 template <class ELFT>
972 void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
973   StringRef S = Sec->getName();
974   if (!isValidCIdentifier(S))
975     return;
976   StringSaver Saver(Alloc);
977   StringRef Start = Saver.save("__start_" + S);
978   StringRef Stop = Saver.save("__stop_" + S);
979   if (SymbolBody *B = Symtab<ELFT>::X->find(Start))
980     if (B->isUndefined())
981       Symtab<ELFT>::X->addSynthetic(Start, Sec, 0, B->getVisibility());
982   if (SymbolBody *B = Symtab<ELFT>::X->find(Stop))
983     if (B->isUndefined())
984       Symtab<ELFT>::X->addSynthetic(
985           Stop, Sec, DefinedSynthetic<ELFT>::SectionEnd, B->getVisibility());
986 }
987 
988 template <class ELFT>
989 OutputSectionBase<ELFT> *Writer<ELFT>::findSection(StringRef Name) {
990   for (OutputSectionBase<ELFT> *Sec : OutputSections)
991     if (Sec->getName() == Name)
992       return Sec;
993   return nullptr;
994 }
995 
996 template <class ELFT> static bool needsPtLoad(OutputSectionBase<ELFT> *Sec) {
997   if (!(Sec->getFlags() & SHF_ALLOC))
998     return false;
999 
1000   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
1001   // responsible for allocating space for them, not the PT_LOAD that
1002   // contains the TLS initialization image.
1003   if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS)
1004     return false;
1005   return true;
1006 }
1007 
1008 // Linker scripts are responsible for aligning addresses. Unfortunately, most
1009 // linker scripts are designed for creating two PT_LOADs only, one RX and one
1010 // RW. This means that there is no alignment in the RO to RX transition and we
1011 // cannot create a PT_LOAD there.
1012 template <class ELFT>
1013 static typename ELFT::uint computeFlags(typename ELFT::uint F) {
1014   if (ScriptConfig->HasSections && !(F & PF_W))
1015     return F | PF_X;
1016   return F;
1017 }
1018 
1019 // Decide which program headers to create and which sections to include in each
1020 // one.
1021 template <class ELFT>
1022 std::vector<PhdrEntry<ELFT>> Writer<ELFT>::createPhdrs() {
1023   std::vector<Phdr> Ret;
1024   auto AddHdr = [&](unsigned Type, unsigned Flags) -> Phdr * {
1025     Ret.emplace_back(Type, Flags);
1026     return &Ret.back();
1027   };
1028 
1029   // The first phdr entry is PT_PHDR which describes the program header itself.
1030   Phdr &Hdr = *AddHdr(PT_PHDR, PF_R);
1031   Hdr.add(Out<ELFT>::ProgramHeaders);
1032 
1033   // PT_INTERP must be the second entry if exists.
1034   if (Out<ELFT>::Interp) {
1035     Phdr &Hdr = *AddHdr(PT_INTERP, Out<ELFT>::Interp->getPhdrFlags());
1036     Hdr.add(Out<ELFT>::Interp);
1037   }
1038 
1039   // Add the first PT_LOAD segment for regular output sections.
1040   uintX_t Flags = computeFlags<ELFT>(PF_R);
1041   Phdr *Load = AddHdr(PT_LOAD, Flags);
1042   if (!ScriptConfig->HasSections) {
1043     Load->add(Out<ELFT>::ElfHeader);
1044     Load->add(Out<ELFT>::ProgramHeaders);
1045   }
1046 
1047   Phdr TlsHdr(PT_TLS, PF_R);
1048   Phdr RelRo(PT_GNU_RELRO, PF_R);
1049   Phdr Note(PT_NOTE, PF_R);
1050   Phdr ARMExidx(PT_ARM_EXIDX, PF_R);
1051   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1052     if (!(Sec->getFlags() & SHF_ALLOC))
1053       break;
1054 
1055     // If we meet TLS section then we create TLS header
1056     // and put all TLS sections inside for further use when
1057     // assign addresses.
1058     if (Sec->getFlags() & SHF_TLS)
1059       TlsHdr.add(Sec);
1060 
1061     if (!needsPtLoad(Sec))
1062       continue;
1063 
1064     // Segments are contiguous memory regions that has the same attributes
1065     // (e.g. executable or writable). There is one phdr for each segment.
1066     // Therefore, we need to create a new phdr when the next section has
1067     // different flags or is loaded at a discontiguous address using AT linker
1068     // script command.
1069     uintX_t NewFlags = computeFlags<ELFT>(Sec->getPhdrFlags());
1070     if (Script<ELFT>::X->hasLMA(Sec->getName()) || Flags != NewFlags) {
1071       Load = AddHdr(PT_LOAD, NewFlags);
1072       Flags = NewFlags;
1073     }
1074 
1075     Load->add(Sec);
1076 
1077     if (isRelroSection(Sec))
1078       RelRo.add(Sec);
1079     if (Sec->getType() == SHT_NOTE)
1080       Note.add(Sec);
1081     if (Config->EMachine == EM_ARM && Sec->getType() == SHT_ARM_EXIDX)
1082       ARMExidx.add(Sec);
1083   }
1084 
1085   // Add the TLS segment unless it's empty.
1086   if (TlsHdr.First)
1087     Ret.push_back(std::move(TlsHdr));
1088 
1089   // Add an entry for .dynamic.
1090   if (Out<ELFT>::DynSymTab) {
1091     Phdr &H = *AddHdr(PT_DYNAMIC, Out<ELFT>::Dynamic->getPhdrFlags());
1092     H.add(Out<ELFT>::Dynamic);
1093   }
1094 
1095   // PT_GNU_RELRO includes all sections that should be marked as
1096   // read-only by dynamic linker after proccessing relocations.
1097   if (RelRo.First)
1098     Ret.push_back(std::move(RelRo));
1099 
1100   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1101   if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
1102     Phdr &Hdr = *AddHdr(PT_GNU_EH_FRAME, Out<ELFT>::EhFrameHdr->getPhdrFlags());
1103     Hdr.add(Out<ELFT>::EhFrameHdr);
1104   }
1105 
1106   // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1107   if (ARMExidx.First)
1108     Ret.push_back(std::move(ARMExidx));
1109 
1110   // PT_GNU_STACK is a special section to tell the loader to make the
1111   // pages for the stack non-executable.
1112   if (!Config->ZExecstack) {
1113     Phdr &Hdr = *AddHdr(PT_GNU_STACK, PF_R | PF_W);
1114     if (Config->ZStackSize != uint64_t(-1))
1115       Hdr.H.p_memsz = Config->ZStackSize;
1116   }
1117 
1118   if (Note.First)
1119     Ret.push_back(std::move(Note));
1120   return Ret;
1121 }
1122 
1123 // The first section of each PT_LOAD and the first section after PT_GNU_RELRO
1124 // have to be page aligned so that the dynamic linker can set the permissions.
1125 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
1126   for (const Phdr &P : Phdrs)
1127     if (P.H.p_type == PT_LOAD)
1128       P.First->PageAlign = true;
1129 
1130   for (const Phdr &P : Phdrs) {
1131     if (P.H.p_type != PT_GNU_RELRO)
1132       continue;
1133     // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1134     // have to align it to a page.
1135     auto End = OutputSections.end();
1136     auto I = std::find(OutputSections.begin(), End, P.Last);
1137     if (I == End || (I + 1) == End)
1138       continue;
1139     OutputSectionBase<ELFT> *Sec = *(I + 1);
1140     if (needsPtLoad(Sec))
1141       Sec->PageAlign = true;
1142   }
1143 }
1144 
1145 // We should set file offsets and VAs for elf header and program headers
1146 // sections. These are special, we do not include them into output sections
1147 // list, but have them to simplify the code.
1148 template <class ELFT> void Writer<ELFT>::fixHeaders() {
1149   uintX_t BaseVA = ScriptConfig->HasSections ? 0 : Config->ImageBase;
1150   Out<ELFT>::ElfHeader->setVA(BaseVA);
1151   uintX_t Off = Out<ELFT>::ElfHeader->getSize();
1152   Out<ELFT>::ProgramHeaders->setVA(Off + BaseVA);
1153   Out<ELFT>::ProgramHeaders->setSize(sizeof(Elf_Phdr) * Phdrs.size());
1154 }
1155 
1156 // Assign VAs (addresses at run-time) to output sections.
1157 template <class ELFT> void Writer<ELFT>::assignAddresses() {
1158   uintX_t VA = Config->ImageBase + getHeaderSize<ELFT>();
1159   uintX_t ThreadBssOffset = 0;
1160   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1161     uintX_t Alignment = Sec->getAlignment();
1162     if (Sec->PageAlign)
1163       Alignment = std::max<uintX_t>(Alignment, Config->MaxPageSize);
1164 
1165     auto I = Config->SectionStartMap.find(Sec->getName());
1166     if (I != Config->SectionStartMap.end())
1167       VA = I->second;
1168 
1169     // We only assign VAs to allocated sections.
1170     if (needsPtLoad(Sec)) {
1171       VA = alignTo(VA, Alignment);
1172       Sec->setVA(VA);
1173       VA += Sec->getSize();
1174     } else if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) {
1175       uintX_t TVA = VA + ThreadBssOffset;
1176       TVA = alignTo(TVA, Alignment);
1177       Sec->setVA(TVA);
1178       ThreadBssOffset = TVA - VA + Sec->getSize();
1179     }
1180   }
1181 }
1182 
1183 // Adjusts the file alignment for a given output section and returns
1184 // its new file offset. The file offset must be the same with its
1185 // virtual address (modulo the page size) so that the loader can load
1186 // executables without any address adjustment.
1187 template <class ELFT, class uintX_t>
1188 static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase<ELFT> *Sec) {
1189   uintX_t Alignment = Sec->getAlignment();
1190   if (Sec->PageAlign)
1191     Alignment = std::max<uintX_t>(Alignment, Config->MaxPageSize);
1192   Off = alignTo(Off, Alignment);
1193 
1194   OutputSectionBase<ELFT> *First = Sec->FirstInPtLoad;
1195   // If the section is not in a PT_LOAD, we have no other constraint.
1196   if (!First)
1197     return Off;
1198 
1199   // If two sections share the same PT_LOAD the file offset is calculated using
1200   // this formula: Off2 = Off1 + (VA2 - VA1).
1201   if (Sec == First)
1202     return alignTo(Off, Target->MaxPageSize, Sec->getVA());
1203   return First->getFileOffset() + Sec->getVA() - First->getVA();
1204 }
1205 
1206 template <class ELFT, class uintX_t>
1207 void setOffset(OutputSectionBase<ELFT> *Sec, uintX_t &Off) {
1208   if (Sec->getType() == SHT_NOBITS) {
1209     Sec->setFileOffset(Off);
1210     return;
1211   }
1212 
1213   Off = getFileAlignment<ELFT>(Off, Sec);
1214   Sec->setFileOffset(Off);
1215   Off += Sec->getSize();
1216 }
1217 
1218 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
1219   uintX_t Off = 0;
1220   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1221     if (Sec->getFlags() & SHF_ALLOC)
1222       setOffset(Sec, Off);
1223   FileSize = alignTo(Off, sizeof(uintX_t));
1224 }
1225 
1226 // Assign file offsets to output sections.
1227 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
1228   uintX_t Off = 0;
1229   setOffset(Out<ELFT>::ElfHeader, Off);
1230   setOffset(Out<ELFT>::ProgramHeaders, Off);
1231 
1232   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1233     setOffset(Sec, Off);
1234 
1235   SectionHeaderOff = alignTo(Off, sizeof(uintX_t));
1236   FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
1237 }
1238 
1239 // Finalize the program headers. We call this function after we assign
1240 // file offsets and VAs to all sections.
1241 template <class ELFT> void Writer<ELFT>::setPhdrs() {
1242   for (Phdr &P : Phdrs) {
1243     Elf_Phdr &H = P.H;
1244     OutputSectionBase<ELFT> *First = P.First;
1245     OutputSectionBase<ELFT> *Last = P.Last;
1246     if (First) {
1247       H.p_filesz = Last->getFileOff() - First->getFileOff();
1248       if (Last->getType() != SHT_NOBITS)
1249         H.p_filesz += Last->getSize();
1250       H.p_memsz = Last->getVA() + Last->getSize() - First->getVA();
1251       H.p_offset = First->getFileOff();
1252       H.p_vaddr = First->getVA();
1253       if (!P.HasLMA)
1254         H.p_paddr = First->getLMA();
1255     }
1256     if (H.p_type == PT_LOAD)
1257       H.p_align = Config->MaxPageSize;
1258     else if (H.p_type == PT_GNU_RELRO)
1259       H.p_align = 1;
1260 
1261     // The TLS pointer goes after PT_TLS. At least glibc will align it,
1262     // so round up the size to make sure the offsets are correct.
1263     if (H.p_type == PT_TLS) {
1264       Out<ELFT>::TlsPhdr = &H;
1265       if (H.p_memsz)
1266         H.p_memsz = alignTo(H.p_memsz, H.p_align);
1267     }
1268   }
1269 }
1270 
1271 template <class ELFT> static typename ELFT::uint getEntryAddr() {
1272   if (Symbol *S = Config->EntrySym)
1273     return S->body()->getVA<ELFT>();
1274   return Config->EntryAddr;
1275 }
1276 
1277 template <class ELFT> static uint8_t getELFEncoding() {
1278   if (ELFT::TargetEndianness == llvm::support::little)
1279     return ELFDATA2LSB;
1280   return ELFDATA2MSB;
1281 }
1282 
1283 static uint16_t getELFType() {
1284   if (Config->Pic)
1285     return ET_DYN;
1286   if (Config->Relocatable)
1287     return ET_REL;
1288   return ET_EXEC;
1289 }
1290 
1291 // This function is called after we have assigned address and size
1292 // to each section. This function fixes some predefined absolute
1293 // symbol values that depend on section address and size.
1294 template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() {
1295   // __ehdr_start is the location of program headers.
1296   if (ElfSym<ELFT>::EhdrStart)
1297     ElfSym<ELFT>::EhdrStart->Value = Out<ELFT>::ProgramHeaders->getVA();
1298 
1299   auto Set = [](DefinedRegular<ELFT> *S1, DefinedRegular<ELFT> *S2, uintX_t V) {
1300     if (S1)
1301       S1->Value = V;
1302     if (S2)
1303       S2->Value = V;
1304   };
1305 
1306   // _etext is the first location after the last read-only loadable segment.
1307   // _edata is the first location after the last read-write loadable segment.
1308   // _end is the first location after the uninitialized data region.
1309   for (Phdr &P : Phdrs) {
1310     Elf_Phdr &H = P.H;
1311     if (H.p_type != PT_LOAD)
1312       continue;
1313     Set(ElfSym<ELFT>::End, ElfSym<ELFT>::End2, H.p_vaddr + H.p_memsz);
1314 
1315     uintX_t Val = H.p_vaddr + H.p_filesz;
1316     if (H.p_flags & PF_W)
1317       Set(ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2, Val);
1318     else
1319       Set(ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2, Val);
1320   }
1321 }
1322 
1323 template <class ELFT> void Writer<ELFT>::writeHeader() {
1324   uint8_t *Buf = Buffer->getBufferStart();
1325   memcpy(Buf, "\177ELF", 4);
1326 
1327   auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
1328 
1329   // Write the ELF header.
1330   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1331   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1332   EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>();
1333   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1334   EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
1335   EHdr->e_type = getELFType();
1336   EHdr->e_machine = FirstObj.EMachine;
1337   EHdr->e_version = EV_CURRENT;
1338   EHdr->e_entry = getEntryAddr<ELFT>();
1339   EHdr->e_shoff = SectionHeaderOff;
1340   EHdr->e_ehsize = sizeof(Elf_Ehdr);
1341   EHdr->e_phnum = Phdrs.size();
1342   EHdr->e_shentsize = sizeof(Elf_Shdr);
1343   EHdr->e_shnum = OutputSections.size() + 1;
1344   EHdr->e_shstrndx = Out<ELFT>::ShStrTab->SectionIndex;
1345 
1346   if (Config->EMachine == EM_ARM)
1347     // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
1348     // but we don't have any firm guarantees of conformance. Linux AArch64
1349     // kernels (as of 2016) require an EABI version to be set.
1350     EHdr->e_flags = EF_ARM_EABI_VER5;
1351   else if (Config->EMachine == EM_MIPS)
1352     EHdr->e_flags = getMipsEFlags<ELFT>();
1353 
1354   if (!Config->Relocatable) {
1355     EHdr->e_phoff = sizeof(Elf_Ehdr);
1356     EHdr->e_phentsize = sizeof(Elf_Phdr);
1357   }
1358 
1359   // Write the program header table.
1360   auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1361   for (Phdr &P : Phdrs)
1362     *HBuf++ = P.H;
1363 
1364   // Write the section header table. Note that the first table entry is null.
1365   auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1366   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1367     Sec->writeHeaderTo(++SHdrs);
1368 }
1369 
1370 template <class ELFT> void Writer<ELFT>::openFile() {
1371   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1372       FileOutputBuffer::create(Config->OutputFile, FileSize,
1373                                FileOutputBuffer::F_executable);
1374   if (auto EC = BufferOrErr.getError())
1375     error(EC, "failed to open " + Config->OutputFile);
1376   else
1377     Buffer = std::move(*BufferOrErr);
1378 }
1379 
1380 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
1381   uint8_t *Buf = Buffer->getBufferStart();
1382   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1383     if (Sec->getFlags() & SHF_ALLOC)
1384       Sec->writeTo(Buf + Sec->getFileOff());
1385 }
1386 
1387 // Convert the .ARM.exidx table entries that use relative PREL31 offsets to
1388 // Absolute addresses. This form is internal to LLD and is only used to
1389 // make reordering the table simpler.
1390 static void ARMExidxEntryPrelToAbs(uint8_t *Loc, uint64_t EntryVA) {
1391   uint64_t Addr = Target->getImplicitAddend(Loc, R_ARM_PREL31) + EntryVA;
1392   bool InlineEntry =
1393       (read32le(Loc + 4) == 1 || (read32le(Loc + 4) & 0x80000000));
1394   if (InlineEntry)
1395     // Set flag in unused bit of code address so that when we convert back we
1396     // know which table entries to leave alone.
1397     Addr |= 0x1;
1398   else
1399     write32le(Loc + 4,
1400               Target->getImplicitAddend(Loc + 4, R_ARM_PREL31) + EntryVA + 4);
1401   write32le(Loc, Addr);
1402 }
1403 
1404 // Convert the .ARM.exidx table entries from the internal to LLD form using
1405 // absolute addresses back to relative PREL31 offsets.
1406 static void ARMExidxEntryAbsToPrel(uint8_t *Loc, uint64_t EntryVA) {
1407   uint64_t Off = read32le(Loc) - EntryVA;
1408   // ARMExidxEntryPreltoAbs sets bit 0 if the table entry has inline data
1409   // that is not an address
1410   bool InlineEntry = Off & 0x1;
1411   Target->relocateOne(Loc, R_ARM_PREL31, Off & ~0x1);
1412   if (!InlineEntry)
1413     Target->relocateOne(Loc + 4, R_ARM_PREL31,
1414                         read32le(Loc + 4) - (EntryVA + 4));
1415 }
1416 
1417 // The table formed by the .ARM.exidx OutputSection has entries with two
1418 // 4-byte fields:
1419 // | PREL31 offset to function | Action to take for function |
1420 // The table must be ordered in ascending virtual address of the functions
1421 // identified by the first field of the table. Instead of using the
1422 // SHF_LINK_ORDER dependency to reorder the sections prior to relocation we
1423 // sort the table post-relocation.
1424 // Ref: Exception handling ABI for the ARM architecture
1425 static void sortARMExidx(uint8_t *Buf, uint64_t OutSecVA, uint64_t Size) {
1426   struct ARMExidxEntry {
1427     ulittle32_t Target;
1428     ulittle32_t Action;
1429   };
1430   ARMExidxEntry *Start = (ARMExidxEntry *)Buf;
1431   size_t NumEnt = Size / sizeof(ARMExidxEntry);
1432   for (uint64_t Off = 0; Off < Size; Off += 8)
1433     ARMExidxEntryPrelToAbs(Buf + Off, OutSecVA + Off);
1434   std::stable_sort(Start, Start + NumEnt,
1435                    [](const ARMExidxEntry &A, const ARMExidxEntry &B) {
1436                      return A.Target < B.Target;
1437                    });
1438   for (uint64_t Off = 0; Off < Size; Off += 8)
1439     ARMExidxEntryAbsToPrel(Buf + Off, OutSecVA + Off);
1440 }
1441 
1442 // Write section contents to a mmap'ed file.
1443 template <class ELFT> void Writer<ELFT>::writeSections() {
1444   uint8_t *Buf = Buffer->getBufferStart();
1445 
1446   // PPC64 needs to process relocations in the .opd section
1447   // before processing relocations in code-containing sections.
1448   Out<ELFT>::Opd = findSection(".opd");
1449   if (Out<ELFT>::Opd) {
1450     Out<ELFT>::OpdBuf = Buf + Out<ELFT>::Opd->getFileOff();
1451     Out<ELFT>::Opd->writeTo(Buf + Out<ELFT>::Opd->getFileOff());
1452   }
1453 
1454   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1455     if (Sec != Out<ELFT>::Opd && Sec != Out<ELFT>::EhFrameHdr)
1456       Sec->writeTo(Buf + Sec->getFileOff());
1457 
1458   OutputSectionBase<ELFT> *ARMExidx = findSection(".ARM.exidx");
1459   if (!Config->Relocatable)
1460     if (auto *OS = dyn_cast_or_null<OutputSection<ELFT>>(ARMExidx))
1461       sortARMExidx(Buf + OS->getFileOff(), OS->getVA(), OS->getSize());
1462 
1463   // The .eh_frame_hdr depends on .eh_frame section contents, therefore
1464   // it should be written after .eh_frame is written.
1465   if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr)
1466     Out<ELFT>::EhFrameHdr->writeTo(Buf + Out<ELFT>::EhFrameHdr->getFileOff());
1467 }
1468 
1469 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1470   if (!Out<ELFT>::BuildId)
1471     return;
1472 
1473   // Compute a hash of all sections of the output file.
1474   uint8_t *Start = Buffer->getBufferStart();
1475   uint8_t *End = Start + FileSize;
1476   Out<ELFT>::BuildId->writeBuildId({Start, End});
1477 }
1478 
1479 template void elf::writeResult<ELF32LE>();
1480 template void elf::writeResult<ELF32BE>();
1481 template void elf::writeResult<ELF64LE>();
1482 template void elf::writeResult<ELF64BE>();
1483 
1484 template struct elf::PhdrEntry<ELF32LE>;
1485 template struct elf::PhdrEntry<ELF32BE>;
1486 template struct elf::PhdrEntry<ELF64LE>;
1487 template struct elf::PhdrEntry<ELF64BE>;
1488 
1489 template bool elf::isRelroSection<ELF32LE>(OutputSectionBase<ELF32LE> *);
1490 template bool elf::isRelroSection<ELF32BE>(OutputSectionBase<ELF32BE> *);
1491 template bool elf::isRelroSection<ELF64LE>(OutputSectionBase<ELF64LE> *);
1492 template bool elf::isRelroSection<ELF64BE>(OutputSectionBase<ELF64BE> *);
1493 
1494 template void elf::reportDiscarded<ELF32LE>(InputSectionBase<ELF32LE> *);
1495 template void elf::reportDiscarded<ELF32BE>(InputSectionBase<ELF32BE> *);
1496 template void elf::reportDiscarded<ELF64LE>(InputSectionBase<ELF64LE> *);
1497 template void elf::reportDiscarded<ELF64BE>(InputSectionBase<ELF64BE> *);
1498