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