xref: /llvm-project-15.0.7/lld/ELF/Writer.cpp (revision c1fe2c43)
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 "SymbolTable.h"
15 #include "Target.h"
16 
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Support/FileOutputBuffer.h"
21 #include "llvm/Support/StringSaver.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace llvm;
25 using namespace llvm::ELF;
26 using namespace llvm::object;
27 
28 using namespace lld;
29 using namespace lld::elf;
30 
31 namespace {
32 // The writer writes a SymbolTable result to a file.
33 template <class ELFT> class Writer {
34 public:
35   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
36   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
37   typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
38   typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
39   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
40   typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
41   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
42   Writer(SymbolTable<ELFT> &S) : Symtab(S) {}
43   void run();
44 
45 private:
46   // This describes a program header entry.
47   // Each contains type, access flags and range of output sections that will be
48   // placed in it.
49   struct Phdr {
50     Phdr(unsigned Type, unsigned Flags) {
51       H.p_type = Type;
52       H.p_flags = Flags;
53     }
54     Elf_Phdr H = {};
55     OutputSectionBase<ELFT> *First = nullptr;
56     OutputSectionBase<ELFT> *Last = nullptr;
57   };
58 
59   void copyLocalSymbols();
60   void addReservedSymbols();
61   bool createSections();
62   void addPredefinedSections();
63   bool needsGot();
64 
65   template <class RelTy>
66   void scanRelocs(InputSectionBase<ELFT> &C,
67                   iterator_range<const RelTy *> Rels);
68 
69   void scanRelocs(InputSection<ELFT> &C);
70   void scanRelocs(InputSectionBase<ELFT> &S, const Elf_Shdr &RelSec);
71   void createPhdrs();
72   void assignAddresses();
73   void assignAddressesRelocatable();
74   void fixAbsoluteSymbols();
75   bool openFile();
76   void writeHeader();
77   void writeSections();
78   void writeBuildId();
79   bool isDiscarded(InputSectionBase<ELFT> *IS) const;
80   StringRef getOutputSectionName(InputSectionBase<ELFT> *S) const;
81   bool needsInterpSection() const {
82     return !Symtab.getSharedFiles().empty() && !Config->DynamicLinker.empty();
83   }
84   bool isOutputDynamic() const {
85     return !Symtab.getSharedFiles().empty() || Config->Shared;
86   }
87 
88   void ensureBss();
89   void addCommonSymbols(std::vector<DefinedCommon *> &Syms);
90   void addCopyRelSymbols(std::vector<SharedSymbol<ELFT> *> &Syms);
91 
92   static uint32_t getAlignment(SharedSymbol<ELFT> *SS);
93 
94   std::unique_ptr<llvm::FileOutputBuffer> Buffer;
95 
96   BumpPtrAllocator Alloc;
97   std::vector<OutputSectionBase<ELFT> *> OutputSections;
98   std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> OwningSections;
99 
100   // We create a section for the ELF header and one for the program headers.
101   ArrayRef<OutputSectionBase<ELFT> *> getSections() const {
102     return makeArrayRef(OutputSections).slice(dummySectionsNum());
103   }
104   unsigned getNumSections() const {
105     return OutputSections.size() + 1 - dummySectionsNum();
106   }
107   // Usually there are 2 dummies sections: ELF header and program header.
108   // Relocatable output does not require program headers to be created.
109   unsigned dummySectionsNum() const { return Config->Relocatable ? 1 : 2; }
110 
111   void addRelIpltSymbols();
112   void addStartEndSymbols();
113   void addStartStopSymbols(OutputSectionBase<ELFT> *Sec);
114 
115   SymbolTable<ELFT> &Symtab;
116   std::vector<Phdr> Phdrs;
117 
118   uintX_t FileSize;
119   uintX_t SectionHeaderOff;
120 
121   // Flag to force GOT to be in output if we have relocations
122   // that relies on its address.
123   bool HasGotOffRel = false;
124 };
125 } // anonymous namespace
126 
127 template <class ELFT> void elf::writeResult(SymbolTable<ELFT> *Symtab) {
128   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
129 
130   // Create singleton output sections.
131   DynamicSection<ELFT> Dynamic(*Symtab);
132   EhFrameHeader<ELFT> EhFrameHdr;
133   GotSection<ELFT> Got;
134   InterpSection<ELFT> Interp;
135   PltSection<ELFT> Plt;
136   RelocationSection<ELFT> RelaDyn(Config->Rela ? ".rela.dyn" : ".rel.dyn");
137   StringTableSection<ELFT> DynStrTab(".dynstr", true);
138   StringTableSection<ELFT> ShStrTab(".shstrtab", false);
139   SymbolTableSection<ELFT> DynSymTab(*Symtab, DynStrTab);
140 
141   OutputSectionBase<ELFT> ElfHeader("", 0, SHF_ALLOC);
142   OutputSectionBase<ELFT> ProgramHeaders("", 0, SHF_ALLOC);
143   ProgramHeaders.updateAlign(sizeof(uintX_t));
144 
145   // Instantiate optional output sections if they are needed.
146   std::unique_ptr<BuildIdSection<ELFT>> BuildId;
147   std::unique_ptr<GnuHashTableSection<ELFT>> GnuHashTab;
148   std::unique_ptr<GotPltSection<ELFT>> GotPlt;
149   std::unique_ptr<HashTableSection<ELFT>> HashTab;
150   std::unique_ptr<RelocationSection<ELFT>> RelaPlt;
151   std::unique_ptr<StringTableSection<ELFT>> StrTab;
152   std::unique_ptr<SymbolTableSection<ELFT>> SymTabSec;
153   std::unique_ptr<OutputSection<ELFT>> MipsRldMap;
154 
155   if (Config->BuildId)
156     BuildId.reset(new BuildIdSection<ELFT>);
157   if (Config->GnuHash)
158     GnuHashTab.reset(new GnuHashTableSection<ELFT>);
159   if (Config->SysvHash)
160     HashTab.reset(new HashTableSection<ELFT>);
161   if (Target->UseLazyBinding) {
162     StringRef S = Config->Rela ? ".rela.plt" : ".rel.plt";
163     GotPlt.reset(new GotPltSection<ELFT>);
164     RelaPlt.reset(new RelocationSection<ELFT>(S));
165   }
166   if (!Config->StripAll) {
167     StrTab.reset(new StringTableSection<ELFT>(".strtab", false));
168     SymTabSec.reset(new SymbolTableSection<ELFT>(*Symtab, *StrTab));
169   }
170   if (Config->EMachine == EM_MIPS && !Config->Shared) {
171     // This is a MIPS specific section to hold a space within the data segment
172     // of executable file which is pointed to by the DT_MIPS_RLD_MAP entry.
173     // See "Dynamic section" in Chapter 5 in the following document:
174     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
175     MipsRldMap.reset(new OutputSection<ELFT>(".rld_map", SHT_PROGBITS,
176                                              SHF_ALLOC | SHF_WRITE));
177     MipsRldMap->setSize(sizeof(uintX_t));
178     MipsRldMap->updateAlign(sizeof(uintX_t));
179   }
180 
181   Out<ELFT>::BuildId = BuildId.get();
182   Out<ELFT>::DynStrTab = &DynStrTab;
183   Out<ELFT>::DynSymTab = &DynSymTab;
184   Out<ELFT>::Dynamic = &Dynamic;
185   Out<ELFT>::EhFrameHdr = &EhFrameHdr;
186   Out<ELFT>::GnuHashTab = GnuHashTab.get();
187   Out<ELFT>::Got = &Got;
188   Out<ELFT>::GotPlt = GotPlt.get();
189   Out<ELFT>::HashTab = HashTab.get();
190   Out<ELFT>::Interp = &Interp;
191   Out<ELFT>::Plt = &Plt;
192   Out<ELFT>::RelaDyn = &RelaDyn;
193   Out<ELFT>::RelaPlt = RelaPlt.get();
194   Out<ELFT>::ShStrTab = &ShStrTab;
195   Out<ELFT>::StrTab = StrTab.get();
196   Out<ELFT>::SymTab = SymTabSec.get();
197   Out<ELFT>::Bss = nullptr;
198   Out<ELFT>::MipsRldMap = MipsRldMap.get();
199   Out<ELFT>::Opd = nullptr;
200   Out<ELFT>::OpdBuf = nullptr;
201   Out<ELFT>::TlsPhdr = nullptr;
202   Out<ELFT>::ElfHeader = &ElfHeader;
203   Out<ELFT>::ProgramHeaders = &ProgramHeaders;
204 
205   Writer<ELFT>(*Symtab).run();
206 }
207 
208 // The main function of the writer.
209 template <class ELFT> void Writer<ELFT>::run() {
210   if (!Config->DiscardAll)
211     copyLocalSymbols();
212   addReservedSymbols();
213   if (!createSections())
214     return;
215   if (!Config->Relocatable) {
216     createPhdrs();
217     assignAddresses();
218   } else {
219     assignAddressesRelocatable();
220   }
221   fixAbsoluteSymbols();
222   if (!openFile())
223     return;
224   writeHeader();
225   writeSections();
226   writeBuildId();
227   if (HasError)
228     return;
229   check(Buffer->commit());
230 }
231 
232 namespace {
233 template <bool Is64Bits> struct SectionKey {
234   typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
235   StringRef Name;
236   uint32_t Type;
237   uintX_t Flags;
238   uintX_t Alignment;
239 };
240 }
241 namespace llvm {
242 template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> {
243   static SectionKey<Is64Bits> getEmptyKey() {
244     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0,
245                                 0};
246   }
247   static SectionKey<Is64Bits> getTombstoneKey() {
248     return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0,
249                                 0, 0};
250   }
251   static unsigned getHashValue(const SectionKey<Is64Bits> &Val) {
252     return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
253   }
254   static bool isEqual(const SectionKey<Is64Bits> &LHS,
255                       const SectionKey<Is64Bits> &RHS) {
256     return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
257            LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
258            LHS.Alignment == RHS.Alignment;
259   }
260 };
261 }
262 
263 template <class ELFT, class RelT>
264 static bool handleTlsRelocation(uint32_t Type, SymbolBody &Body,
265                                 InputSectionBase<ELFT> &C, RelT &RI) {
266   if (Target->pointsToLocalDynamicGotEntry(Type)) {
267     if (Target->canRelaxTls(Type, nullptr))
268       return true;
269     if (Out<ELFT>::Got->addTlsIndex())
270       Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel,
271                                     DynamicReloc<ELFT>::Off_LTlsIndex,
272                                     nullptr});
273     return true;
274   }
275 
276   if (!Body.IsTls)
277     return false;
278 
279   if (Target->isTlsGlobalDynamicRel(Type)) {
280     if (!Target->canRelaxTls(Type, &Body)) {
281       if (Out<ELFT>::Got->addDynTlsEntry(Body)) {
282         Out<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel,
283                                       DynamicReloc<ELFT>::Off_GTlsIndex,
284                                       &Body});
285         Out<ELFT>::RelaDyn->addReloc(
286             {Target->TlsOffsetRel, DynamicReloc<ELFT>::Off_GTlsOffset, &Body});
287       }
288       return true;
289     }
290     if (!Body.isPreemptible())
291       return true;
292   }
293   return false;
294 }
295 
296 // The reason we have to do this early scan is as follows
297 // * To mmap the output file, we need to know the size
298 // * For that, we need to know how many dynamic relocs we will have.
299 // It might be possible to avoid this by outputting the file with write:
300 // * Write the allocated output sections, computing addresses.
301 // * Apply relocations, recording which ones require a dynamic reloc.
302 // * Write the dynamic relocations.
303 // * Write the rest of the file.
304 // This would have some drawbacks. For example, we would only know if .rela.dyn
305 // is needed after applying relocations. If it is, it will go after rw and rx
306 // sections. Given that it is ro, we will need an extra PT_LOAD. This
307 // complicates things for the dynamic linker and means we would have to reserve
308 // space for the extra PT_LOAD even if we end up not using it.
309 template <class ELFT>
310 template <class RelTy>
311 void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &C,
312                               iterator_range<const RelTy *> Rels) {
313   const elf::ObjectFile<ELFT> &File = *C.getFile();
314   for (const RelTy &RI : Rels) {
315     uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
316     SymbolBody &OrigBody = File.getSymbolBody(SymIndex);
317     SymbolBody &Body = OrigBody.repl();
318     uint32_t Type = RI.getType(Config->Mips64EL);
319 
320     // Ignore "hint" relocation because it is for optional code optimization.
321     if (Target->isHintRel(Type))
322       continue;
323 
324     if (Target->isGotRelative(Type))
325       HasGotOffRel = true;
326 
327     // Set "used" bit for --as-needed.
328     if (OrigBody.isUndefined() && !OrigBody.isWeak())
329       if (auto *S = dyn_cast<SharedSymbol<ELFT>>(&Body))
330         S->File->IsUsed = true;
331 
332     bool Preemptible = Body.isPreemptible();
333     if (handleTlsRelocation<ELFT>(Type, Body, C, RI))
334       continue;
335 
336     if (Target->needsDynRelative(Type))
337       Out<ELFT>::RelaDyn->addReloc({Target->RelativeRel, &C, RI.r_offset, true,
338                                     &Body, getAddend<ELFT>(RI)});
339 
340     // MIPS has a special rule to create GOTs for local symbols.
341     if (Config->EMachine == EM_MIPS && !Preemptible &&
342         Target->needsGot(Type, Body)) {
343       // FIXME (simon): Do not add so many redundant entries.
344       Out<ELFT>::Got->addMipsLocalEntry();
345       continue;
346     }
347 
348     // If a symbol in a DSO is referenced directly instead of through GOT,
349     // we need to create a copy relocation for the symbol.
350     if (auto *B = dyn_cast<SharedSymbol<ELFT>>(&Body)) {
351       if (B->needsCopy())
352         continue;
353       if (Target->needsCopyRel<ELFT>(Type, *B)) {
354         B->NeedsCopyOrPltAddr = true;
355         Out<ELFT>::RelaDyn->addReloc(
356             {Target->CopyRel, DynamicReloc<ELFT>::Off_Bss, B});
357         continue;
358       }
359     }
360 
361     // An STT_GNU_IFUNC symbol always uses a PLT entry, and all references
362     // to the symbol go through the PLT. This is true even for a local
363     // symbol, although local symbols normally do not require PLT entries.
364     if (Body.isGnuIfunc<ELFT>()) {
365       if (Body.isInPlt())
366         continue;
367       Out<ELFT>::Plt->addEntry(Body);
368       if (Target->UseLazyBinding) {
369         Out<ELFT>::GotPlt->addEntry(Body);
370         Out<ELFT>::RelaPlt->addReloc(
371             {Preemptible ? Target->PltRel : Target->IRelativeRel,
372              DynamicReloc<ELFT>::Off_GotPlt, !Preemptible, &Body});
373       } else {
374         Out<ELFT>::Got->addEntry(Body);
375         Out<ELFT>::RelaDyn->addReloc(
376             {Preemptible ? Target->PltRel : Target->IRelativeRel,
377              DynamicReloc<ELFT>::Off_Got, !Preemptible, &Body});
378       }
379       continue;
380     }
381 
382     // If a relocation needs PLT, we create a PLT and a GOT slot
383     // for the symbol.
384     TargetInfo::PltNeed NeedPlt = Target->needsPlt<ELFT>(Type, Body);
385     if (NeedPlt) {
386       if (NeedPlt == TargetInfo::Plt_Implicit)
387         Body.NeedsCopyOrPltAddr = true;
388       if (Body.isInPlt())
389         continue;
390       Out<ELFT>::Plt->addEntry(Body);
391 
392       if (Target->UseLazyBinding) {
393         Out<ELFT>::GotPlt->addEntry(Body);
394         Out<ELFT>::RelaPlt->addReloc(
395             {Target->PltRel, DynamicReloc<ELFT>::Off_GotPlt, &Body});
396       } else {
397         if (Body.isInGot())
398           continue;
399         Out<ELFT>::Got->addEntry(Body);
400         Out<ELFT>::RelaDyn->addReloc(
401             {Target->GotRel, DynamicReloc<ELFT>::Off_Got, &Body});
402       }
403       continue;
404     }
405 
406     // If a relocation needs GOT, we create a GOT slot for the symbol.
407     if (Target->needsGot(Type, Body)) {
408       if (Body.isInGot())
409         continue;
410       Out<ELFT>::Got->addEntry(Body);
411 
412       if (Config->EMachine == EM_MIPS) {
413         // MIPS ABI has special rules to process GOT entries
414         // and doesn't require relocation entries for them.
415         // See "Global Offset Table" in Chapter 5 in the following document
416         // for detailed description:
417         // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
418         Body.MustBeInDynSym = true;
419         continue;
420       }
421 
422       bool Dynrel = Config->Shared && !Target->isRelRelative(Type) &&
423                     !Target->isSizeRel(Type);
424       if (Preemptible || Dynrel) {
425         uint32_t DynType;
426         if (Body.IsTls)
427           DynType = Target->TlsGotRel;
428         else if (Preemptible)
429           DynType = Target->GotRel;
430         else
431           DynType = Target->RelativeRel;
432         Out<ELFT>::RelaDyn->addReloc(
433             {DynType, DynamicReloc<ELFT>::Off_Got, !Preemptible, &Body});
434       }
435       continue;
436     }
437 
438     if (Config->EMachine == EM_MIPS) {
439       if (Type == R_MIPS_LO16)
440         // Ignore R_MIPS_LO16 relocation. If it is a pair for R_MIPS_GOT16 we
441         // already completed all required action (GOT entry allocation) when
442         // handle R_MIPS_GOT16a. If it is a pair for R_MIPS_HI16 against
443         // _gp_disp it does not require dynamic relocation. If its a pair for
444         // R_MIPS_HI16 against a regular symbol it does not require dynamic
445         // relocation too because that case is possible for executable file
446         // linking only.
447         continue;
448       if (&Body == Config->MipsGpDisp || &Body == Config->MipsLocalGp)
449         // MIPS _gp_disp designates offset between start of function and 'gp'
450         // pointer into GOT. __gnu_local_gp is equal to the current value of
451         // the 'gp'. Therefore any relocations against them do not require
452         // dynamic relocation.
453         continue;
454     }
455 
456     if (Preemptible) {
457       // We don't know anything about the finaly symbol. Just ask the dynamic
458       // linker to handle the relocation for us.
459       Out<ELFT>::RelaDyn->addReloc({Target->getDynRel(Type), &C, RI.r_offset,
460                                     false, &Body, getAddend<ELFT>(RI)});
461       continue;
462     }
463 
464     // We know that this is the final symbol. If the program being produced
465     // is position independent, the final value is still not known.
466     // If the relocation depends on the symbol value (not the size or distances
467     // in the output), we still need some help from the dynamic linker.
468     // We can however do better than just copying the incoming relocation. We
469     // can process some of it and and just ask the dynamic linker to add the
470     // load address.
471     if (!Config->Shared || Target->isRelRelative(Type) ||
472         Target->isSizeRel(Type))
473       continue;
474 
475     uintX_t Addend = getAddend<ELFT>(RI);
476     if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) {
477       Out<ELFT>::RelaDyn->addReloc({R_PPC64_RELATIVE, &C, RI.r_offset, false,
478                                     nullptr,
479                                     (uintX_t)getPPC64TocBase() + Addend});
480       continue;
481     }
482     Out<ELFT>::RelaDyn->addReloc(
483         {Target->RelativeRel, &C, RI.r_offset, true, &Body, Addend});
484   }
485 }
486 
487 template <class ELFT> void Writer<ELFT>::scanRelocs(InputSection<ELFT> &C) {
488   if (C.getSectionHdr()->sh_flags & SHF_ALLOC)
489     for (const Elf_Shdr *RelSec : C.RelocSections)
490       scanRelocs(C, *RelSec);
491 }
492 
493 template <class ELFT>
494 void Writer<ELFT>::scanRelocs(InputSectionBase<ELFT> &S,
495                               const Elf_Shdr &RelSec) {
496   ELFFile<ELFT> &EObj = S.getFile()->getObj();
497   if (RelSec.sh_type == SHT_RELA)
498     scanRelocs(S, EObj.relas(&RelSec));
499   else
500     scanRelocs(S, EObj.rels(&RelSec));
501 }
502 
503 template <class ELFT>
504 static void reportUndefined(SymbolTable<ELFT> &Symtab, SymbolBody *Sym) {
505   if ((Config->Relocatable || Config->Shared) && !Config->NoUndefined)
506     return;
507 
508   std::string Msg = "undefined symbol: " + Sym->getName().str();
509   if (InputFile *File = Symtab.findFile(Sym))
510     Msg += " in " + File->getName().str();
511   if (Config->NoinhibitExec)
512     warning(Msg);
513   else
514     error(Msg);
515 }
516 
517 template <class ELFT>
518 static bool shouldKeepInSymtab(const elf::ObjectFile<ELFT> &File,
519                                StringRef SymName,
520                                const typename ELFFile<ELFT>::Elf_Sym &Sym) {
521   if (Sym.getType() == STT_FILE)
522     return false;
523 
524   // We keep sections in symtab for relocatable output.
525   if (Sym.getType() == STT_SECTION)
526     return Config->Relocatable;
527 
528   InputSectionBase<ELFT> *Sec = File.getSection(Sym);
529   // If sym references a section in a discarded group, don't keep it.
530   if (Sec == InputSection<ELFT>::Discarded)
531     return false;
532 
533   if (Config->DiscardNone)
534     return true;
535 
536   // In ELF assembly .L symbols are normally discarded by the assembler.
537   // If the assembler fails to do so, the linker discards them if
538   // * --discard-locals is used.
539   // * The symbol is in a SHF_MERGE section, which is normally the reason for
540   //   the assembler keeping the .L symbol.
541   if (!SymName.startswith(".L") && !SymName.empty())
542     return true;
543 
544   if (Config->DiscardLocals)
545     return false;
546 
547   return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
548 }
549 
550 // Local symbols are not in the linker's symbol table. This function scans
551 // each object file's symbol table to copy local symbols to the output.
552 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
553   if (!Out<ELFT>::SymTab)
554     return;
555   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
556        Symtab.getObjectFiles()) {
557     for (SymbolBody *B : F->getLocalSymbols()) {
558       const Elf_Sym &Sym = cast<DefinedRegular<ELFT>>(B)->Sym;
559       StringRef SymName = check(Sym.getName(F->getStringTable()));
560       if (!shouldKeepInSymtab<ELFT>(*F, SymName, Sym))
561         continue;
562       if (Sym.st_shndx != SHN_ABS)
563         if (!F->getSection(Sym)->Live)
564           continue;
565       ++Out<ELFT>::SymTab->NumLocals;
566       if (Config->Relocatable)
567         B->DynsymIndex = Out<ELFT>::SymTab->NumLocals;
568       F->KeptLocalSyms.push_back(std::make_pair(
569           &Sym, Out<ELFT>::SymTab->StrTabSec.addString(SymName)));
570     }
571   }
572 }
573 
574 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
575 // we would like to make sure appear is a specific order to maximize their
576 // coverage by a single signed 16-bit offset from the TOC base pointer.
577 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
578 // sections. This will put it next to the loaded special PPC64 sections (and,
579 // thus, within reach of the TOC base pointer).
580 static int getPPC64SectionRank(StringRef SectionName) {
581   return StringSwitch<int>(SectionName)
582            .Case(".tocbss", 0)
583            .Case(".branch_lt", 2)
584            .Case(".toc", 3)
585            .Case(".toc1", 4)
586            .Case(".opd", 5)
587            .Default(1);
588 }
589 
590 template <class ELFT> static bool isRelroSection(OutputSectionBase<ELFT> *Sec) {
591   if (!Config->ZRelro)
592     return false;
593   typename OutputSectionBase<ELFT>::uintX_t Flags = Sec->getFlags();
594   if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
595     return false;
596   if (Flags & SHF_TLS)
597     return true;
598   uint32_t Type = Sec->getType();
599   if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
600       Type == SHT_PREINIT_ARRAY)
601     return true;
602   if (Sec == Out<ELFT>::GotPlt)
603     return Config->ZNow;
604   if (Sec == Out<ELFT>::Dynamic || Sec == Out<ELFT>::Got)
605     return true;
606   StringRef S = Sec->getName();
607   return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
608          S == ".eh_frame";
609 }
610 
611 // Output section ordering is determined by this function.
612 template <class ELFT>
613 static bool compareSections(OutputSectionBase<ELFT> *A,
614                             OutputSectionBase<ELFT> *B) {
615   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
616 
617   int Comp = Script->compareSections(A->getName(), B->getName());
618   if (Comp != 0)
619     return Comp < 0;
620 
621   uintX_t AFlags = A->getFlags();
622   uintX_t BFlags = B->getFlags();
623 
624   // Allocatable sections go first to reduce the total PT_LOAD size and
625   // so debug info doesn't change addresses in actual code.
626   bool AIsAlloc = AFlags & SHF_ALLOC;
627   bool BIsAlloc = BFlags & SHF_ALLOC;
628   if (AIsAlloc != BIsAlloc)
629     return AIsAlloc;
630 
631   // We don't have any special requirements for the relative order of
632   // two non allocatable sections.
633   if (!AIsAlloc)
634     return false;
635 
636   // We want the read only sections first so that they go in the PT_LOAD
637   // covering the program headers at the start of the file.
638   bool AIsWritable = AFlags & SHF_WRITE;
639   bool BIsWritable = BFlags & SHF_WRITE;
640   if (AIsWritable != BIsWritable)
641     return BIsWritable;
642 
643   // For a corresponding reason, put non exec sections first (the program
644   // header PT_LOAD is not executable).
645   bool AIsExec = AFlags & SHF_EXECINSTR;
646   bool BIsExec = BFlags & SHF_EXECINSTR;
647   if (AIsExec != BIsExec)
648     return BIsExec;
649 
650   // If we got here we know that both A and B are in the same PT_LOAD.
651 
652   // The TLS initialization block needs to be a single contiguous block in a R/W
653   // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS
654   // sections are placed here as they don't take up virtual address space in the
655   // PT_LOAD.
656   bool AIsTls = AFlags & SHF_TLS;
657   bool BIsTls = BFlags & SHF_TLS;
658   if (AIsTls != BIsTls)
659     return AIsTls;
660 
661   // The next requirement we have is to put nobits sections last. The
662   // reason is that the only thing the dynamic linker will see about
663   // them is a p_memsz that is larger than p_filesz. Seeing that it
664   // zeros the end of the PT_LOAD, so that has to correspond to the
665   // nobits sections.
666   bool AIsNoBits = A->getType() == SHT_NOBITS;
667   bool BIsNoBits = B->getType() == SHT_NOBITS;
668   if (AIsNoBits != BIsNoBits)
669     return BIsNoBits;
670 
671   // We place RelRo section before plain r/w ones.
672   bool AIsRelRo = isRelroSection(A);
673   bool BIsRelRo = isRelroSection(B);
674   if (AIsRelRo != BIsRelRo)
675     return AIsRelRo;
676 
677   // Some architectures have additional ordering restrictions for sections
678   // within the same PT_LOAD.
679   if (Config->EMachine == EM_PPC64)
680     return getPPC64SectionRank(A->getName()) <
681            getPPC64SectionRank(B->getName());
682 
683   return false;
684 }
685 
686 // The .bss section does not exist if no input file has a .bss section.
687 // This function creates one if that's the case.
688 template <class ELFT> void Writer<ELFT>::ensureBss() {
689   if (Out<ELFT>::Bss)
690     return;
691   Out<ELFT>::Bss =
692       new OutputSection<ELFT>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
693   OwningSections.emplace_back(Out<ELFT>::Bss);
694   OutputSections.push_back(Out<ELFT>::Bss);
695 }
696 
697 // Until this function is called, common symbols do not belong to any section.
698 // This function adds them to end of BSS section.
699 template <class ELFT>
700 void Writer<ELFT>::addCommonSymbols(std::vector<DefinedCommon *> &Syms) {
701   if (Syms.empty())
702     return;
703 
704   // Sort the common symbols by alignment as an heuristic to pack them better.
705   std::stable_sort(Syms.begin(), Syms.end(),
706                    [](const DefinedCommon *A, const DefinedCommon *B) {
707                      return A->Alignment > B->Alignment;
708                    });
709 
710   ensureBss();
711   uintX_t Off = Out<ELFT>::Bss->getSize();
712   for (DefinedCommon *C : Syms) {
713     Off = alignTo(Off, C->Alignment);
714     Out<ELFT>::Bss->updateAlign(C->Alignment);
715     C->OffsetInBss = Off;
716     Off += C->Size;
717   }
718 
719   Out<ELFT>::Bss->setSize(Off);
720 }
721 
722 template <class ELFT>
723 uint32_t Writer<ELFT>::getAlignment(SharedSymbol<ELFT> *SS) {
724   const Elf_Sym &Sym = SS->Sym;
725   const Elf_Shdr *Sec = SS->File->getSection(Sym);
726   uintX_t SecAlign = Sec->sh_addralign;
727   int TrailingZeros = std::min(countTrailingZeros(SecAlign),
728                                countTrailingZeros((uintX_t)Sym.st_value));
729   return 1 << TrailingZeros;
730 }
731 
732 // Reserve space in .bss for copy relocations.
733 template <class ELFT>
734 void Writer<ELFT>::addCopyRelSymbols(std::vector<SharedSymbol<ELFT> *> &Syms) {
735   if (Syms.empty())
736     return;
737   ensureBss();
738   uintX_t Off = Out<ELFT>::Bss->getSize();
739   uintX_t MaxAlign = Out<ELFT>::Bss->getAlign();
740   for (SharedSymbol<ELFT> *SS : Syms) {
741     uintX_t Align = getAlignment(SS);
742     Off = alignTo(Off, Align);
743     SS->OffsetInBss = Off;
744     Off += SS->Sym.st_size;
745     MaxAlign = std::max(MaxAlign, Align);
746   }
747   Out<ELFT>::Bss->setSize(Off);
748   Out<ELFT>::Bss->updateAlign(MaxAlign);
749 }
750 
751 template <class ELFT>
752 StringRef Writer<ELFT>::getOutputSectionName(InputSectionBase<ELFT> *S) const {
753   StringRef Dest = Script->getOutputSection<ELFT>(S);
754   if (!Dest.empty())
755     return Dest;
756 
757   StringRef Name = S->getSectionName();
758   for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.",
759                       ".init_array.", ".fini_array.", ".ctors.", ".dtors.",
760                       ".tbss.", ".gcc_except_table.", ".tdata."})
761     if (Name.startswith(V))
762       return V.drop_back();
763   return Name;
764 }
765 
766 template <class ELFT>
767 void reportDiscarded(InputSectionBase<ELFT> *IS,
768                      const std::unique_ptr<elf::ObjectFile<ELFT>> &File) {
769   if (!Config->PrintGcSections || !IS || IS->Live)
770     return;
771   llvm::errs() << "removing unused section from '" << IS->getSectionName()
772                << "' in file '" << File->getName() << "'\n";
773 }
774 
775 template <class ELFT>
776 bool Writer<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) const {
777   return !S || S == InputSection<ELFT>::Discarded || !S->Live ||
778          Script->isDiscarded(S);
779 }
780 
781 // The beginning and the ending of .rel[a].plt section are marked
782 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
783 // executable. The runtime needs these symbols in order to resolve
784 // all IRELATIVE relocs on startup. For dynamic executables, we don't
785 // need these symbols, since IRELATIVE relocs are resolved through GOT
786 // and PLT. For details, see http://www.airs.com/blog/archives/403.
787 template <class ELFT>
788 void Writer<ELFT>::addRelIpltSymbols() {
789   if (isOutputDynamic() || !Out<ELFT>::RelaPlt)
790     return;
791   StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start";
792   if (Symtab.find(S))
793     Symtab.addAbsolute(S, ElfSym<ELFT>::RelaIpltStart);
794 
795   S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end";
796   if (Symtab.find(S))
797     Symtab.addAbsolute(S, ElfSym<ELFT>::RelaIpltEnd);
798 }
799 
800 template <class ELFT> static bool includeInSymtab(const SymbolBody &B) {
801   if (!B.isUsedInRegularObj())
802     return false;
803 
804   if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) {
805     // Don't include synthetic symbols like __init_array_start in every output.
806     if (&D->Sym == &ElfSym<ELFT>::Ignored)
807       return false;
808     // Exclude symbols pointing to garbage-collected sections.
809     if (D->Section && !D->Section->Live)
810       return false;
811   }
812   return true;
813 }
814 
815 static bool includeInDynsym(const SymbolBody &B) {
816   uint8_t V = B.getVisibility();
817   if (V != STV_DEFAULT && V != STV_PROTECTED)
818     return false;
819   if (Config->ExportDynamic || Config->Shared)
820     return true;
821   return B.MustBeInDynSym;
822 }
823 
824 // This class knows how to create an output section for a given
825 // input section. Output section type is determined by various
826 // factors, including input section's sh_flags, sh_type and
827 // linker scripts.
828 namespace {
829 template <class ELFT> class OutputSectionFactory {
830   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
831   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
832 
833 public:
834   std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
835                                                     StringRef OutsecName);
836 
837   OutputSectionBase<ELFT> *lookup(StringRef Name, uint32_t Type, uintX_t Flags);
838 
839 private:
840   SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
841                                        StringRef OutsecName);
842 
843   SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSectionBase<ELFT> *> Map;
844 };
845 }
846 
847 template <class ELFT>
848 std::pair<OutputSectionBase<ELFT> *, bool>
849 OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
850                                    StringRef OutsecName) {
851   SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
852   OutputSectionBase<ELFT> *&Sec = Map[Key];
853   if (Sec)
854     return {Sec, false};
855 
856   switch (C->SectionKind) {
857   case InputSectionBase<ELFT>::Regular:
858     Sec = new OutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
859     break;
860   case InputSectionBase<ELFT>::EHFrame:
861     Sec = new EHOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
862     break;
863   case InputSectionBase<ELFT>::Merge:
864     Sec = new MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags,
865                                        Key.Alignment);
866     break;
867   case InputSectionBase<ELFT>::MipsReginfo:
868     Sec = new MipsReginfoOutputSection<ELFT>();
869     break;
870   }
871   return {Sec, true};
872 }
873 
874 template <class ELFT>
875 OutputSectionBase<ELFT> *OutputSectionFactory<ELFT>::lookup(StringRef Name,
876                                                             uint32_t Type,
877                                                             uintX_t Flags) {
878   return Map.lookup({Name, Type, Flags, 0});
879 }
880 
881 template <class ELFT>
882 SectionKey<ELFT::Is64Bits>
883 OutputSectionFactory<ELFT>::createKey(InputSectionBase<ELFT> *C,
884                                       StringRef OutsecName) {
885   const Elf_Shdr *H = C->getSectionHdr();
886   uintX_t Flags = H->sh_flags & ~SHF_GROUP;
887 
888   // For SHF_MERGE we create different output sections for each alignment.
889   // This makes each output section simple and keeps a single level mapping from
890   // input to output.
891   uintX_t Alignment = 0;
892   if (isa<MergeInputSection<ELFT>>(C)) {
893     Alignment = H->sh_addralign;
894     if (H->sh_entsize > Alignment)
895       Alignment = H->sh_entsize;
896   }
897 
898   // GNU as can give .eh_frame secion type SHT_PROGBITS or SHT_X86_64_UNWIND
899   // depending on the construct. We want to canonicalize it so that
900   // there is only one .eh_frame in the end.
901   uint32_t Type = H->sh_type;
902   if (Type == SHT_PROGBITS && Config->EMachine == EM_X86_64 &&
903       isa<EHInputSection<ELFT>>(C))
904     Type = SHT_X86_64_UNWIND;
905 
906   return SectionKey<ELFT::Is64Bits>{OutsecName, Type, Flags, Alignment};
907 }
908 
909 // The linker is expected to define some symbols depending on
910 // the linking result. This function defines such symbols.
911 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
912   // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
913   // static linking the linker is required to optimize away any references to
914   // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
915   // to avoid the undefined symbol error.
916   if (!isOutputDynamic())
917     Symtab.addIgnored("__tls_get_addr");
918 
919   auto Define = [this](StringRef S, Elf_Sym &Sym) {
920     if (Symtab.find(S))
921       Symtab.addAbsolute(S, Sym);
922 
923     // The name without the underscore is not a reserved name,
924     // so it is defined only when there is a reference against it.
925     assert(S.startswith("_"));
926     S = S.substr(1);
927     if (SymbolBody *B = Symtab.find(S))
928       if (B->isUndefined())
929         Symtab.addAbsolute(S, Sym);
930   };
931 
932   Define("_end", ElfSym<ELFT>::End);
933   Define("_etext", ElfSym<ELFT>::Etext);
934   Define("_edata", ElfSym<ELFT>::Edata);
935 }
936 
937 // Sort input sections by section name suffixes for
938 // __attribute__((init_priority(N))).
939 template <class ELFT> static void sortInitFini(OutputSectionBase<ELFT> *S) {
940   if (S)
941     reinterpret_cast<OutputSection<ELFT> *>(S)->sortInitFini();
942 }
943 
944 // Sort input sections by the special rule for .ctors and .dtors.
945 template <class ELFT> static void sortCtorsDtors(OutputSectionBase<ELFT> *S) {
946   if (S)
947     reinterpret_cast<OutputSection<ELFT> *>(S)->sortCtorsDtors();
948 }
949 
950 // Create output section objects and add them to OutputSections.
951 template <class ELFT> bool Writer<ELFT>::createSections() {
952   OutputSections.push_back(Out<ELFT>::ElfHeader);
953   if (!Config->Relocatable)
954     OutputSections.push_back(Out<ELFT>::ProgramHeaders);
955 
956   // Add .interp first because some loaders want to see that section
957   // on the first page of the executable file when loaded into memory.
958   if (needsInterpSection())
959     OutputSections.push_back(Out<ELFT>::Interp);
960 
961   // A core file does not usually contain unmodified segments except
962   // the first page of the executable. Add the build ID section now
963   // so that the section is included in the first page.
964   if (Out<ELFT>::BuildId)
965     OutputSections.push_back(Out<ELFT>::BuildId);
966 
967   // Create output sections for input object file sections.
968   std::vector<OutputSectionBase<ELFT> *> RegularSections;
969   OutputSectionFactory<ELFT> Factory;
970   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
971        Symtab.getObjectFiles()) {
972     for (InputSectionBase<ELFT> *C : F->getSections()) {
973       if (isDiscarded(C)) {
974         reportDiscarded(C, F);
975         continue;
976       }
977       OutputSectionBase<ELFT> *Sec;
978       bool IsNew;
979       std::tie(Sec, IsNew) = Factory.create(C, getOutputSectionName(C));
980       if (IsNew) {
981         OwningSections.emplace_back(Sec);
982         OutputSections.push_back(Sec);
983         RegularSections.push_back(Sec);
984       }
985       Sec->addSection(C);
986     }
987   }
988 
989   Out<ELFT>::Bss = static_cast<OutputSection<ELFT> *>(
990       Factory.lookup(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE));
991 
992   // If we have a .opd section (used under PPC64 for function descriptors),
993   // store a pointer to it here so that we can use it later when processing
994   // relocations.
995   Out<ELFT>::Opd = Factory.lookup(".opd", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC);
996 
997   Out<ELFT>::Dynamic->PreInitArraySec = Factory.lookup(
998       ".preinit_array", SHT_PREINIT_ARRAY, SHF_WRITE | SHF_ALLOC);
999   Out<ELFT>::Dynamic->InitArraySec =
1000       Factory.lookup(".init_array", SHT_INIT_ARRAY, SHF_WRITE | SHF_ALLOC);
1001   Out<ELFT>::Dynamic->FiniArraySec =
1002       Factory.lookup(".fini_array", SHT_FINI_ARRAY, SHF_WRITE | SHF_ALLOC);
1003 
1004   // Sort section contents for __attribute__((init_priority(N)).
1005   sortInitFini(Out<ELFT>::Dynamic->InitArraySec);
1006   sortInitFini(Out<ELFT>::Dynamic->FiniArraySec);
1007   sortCtorsDtors(Factory.lookup(".ctors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC));
1008   sortCtorsDtors(Factory.lookup(".dtors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC));
1009 
1010   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1011   // symbols for sections, so that the runtime can get the start and end
1012   // addresses of each section by section name. Add such symbols.
1013   if (!Config->Relocatable) {
1014     addStartEndSymbols();
1015     for (OutputSectionBase<ELFT> *Sec : RegularSections)
1016       addStartStopSymbols(Sec);
1017   }
1018 
1019   // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1020   // It should be okay as no one seems to care about the type.
1021   // Even the author of gold doesn't remember why gold behaves that way.
1022   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1023   if (isOutputDynamic())
1024     Symtab.addSynthetic("_DYNAMIC", *Out<ELFT>::Dynamic, 0, STV_HIDDEN);
1025 
1026   // Define __rel[a]_iplt_{start,end} symbols if needed.
1027   addRelIpltSymbols();
1028 
1029   // Scan relocations. This must be done after every symbol is declared so that
1030   // we can correctly decide if a dynamic relocation is needed.
1031   for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
1032        Symtab.getObjectFiles()) {
1033     for (InputSectionBase<ELFT> *C : F->getSections()) {
1034       if (isDiscarded(C))
1035         continue;
1036       if (auto *S = dyn_cast<InputSection<ELFT>>(C))
1037         scanRelocs(*S);
1038       else if (auto *S = dyn_cast<EHInputSection<ELFT>>(C))
1039         if (S->RelocSection)
1040           scanRelocs(*S, *S->RelocSection);
1041     }
1042   }
1043 
1044   // Now that we have defined all possible symbols including linker-
1045   // synthesized ones. Visit all symbols to give the finishing touches.
1046   std::vector<DefinedCommon *> CommonSymbols;
1047   std::vector<SharedSymbol<ELFT> *> CopyRelSymbols;
1048   for (auto &P : Symtab.getSymbols()) {
1049     SymbolBody *Body = P.second->Body;
1050     if (auto *U = dyn_cast<Undefined>(Body))
1051       if (!U->isWeak() && !U->canKeepUndefined())
1052         reportUndefined<ELFT>(Symtab, Body);
1053 
1054     if (auto *C = dyn_cast<DefinedCommon>(Body))
1055       CommonSymbols.push_back(C);
1056     if (auto *SC = dyn_cast<SharedSymbol<ELFT>>(Body))
1057       if (SC->needsCopy())
1058         CopyRelSymbols.push_back(SC);
1059 
1060     if (!includeInSymtab<ELFT>(*Body))
1061       continue;
1062     if (Out<ELFT>::SymTab)
1063       Out<ELFT>::SymTab->addSymbol(Body);
1064 
1065     if (isOutputDynamic() && includeInDynsym(*Body))
1066       Out<ELFT>::DynSymTab->addSymbol(Body);
1067   }
1068 
1069   // Do not proceed if there was an undefined symbol.
1070   if (HasError)
1071     return false;
1072 
1073   addCommonSymbols(CommonSymbols);
1074   addCopyRelSymbols(CopyRelSymbols);
1075 
1076   // So far we have added sections from input object files.
1077   // This function adds linker-created Out<ELFT>::* sections.
1078   addPredefinedSections();
1079 
1080   std::stable_sort(OutputSections.begin(), OutputSections.end(),
1081                    compareSections<ELFT>);
1082 
1083   for (unsigned I = dummySectionsNum(), N = OutputSections.size(); I < N; ++I)
1084     OutputSections[I]->SectionIndex = I + 1 - dummySectionsNum();
1085 
1086   for (OutputSectionBase<ELFT> *Sec : getSections())
1087     Sec->setSHName(Out<ELFT>::ShStrTab->addString(Sec->getName()));
1088 
1089   // Finalizers fix each section's size.
1090   // .dynsym is finalized early since that may fill up .gnu.hash.
1091   if (isOutputDynamic())
1092     Out<ELFT>::DynSymTab->finalize();
1093 
1094   // Fill other section headers. The dynamic table is finalized
1095   // at the end because some tags like RELSZ depend on result
1096   // of finalizing other sections. The dynamic string table is
1097   // finalized once the .dynamic finalizer has added a few last
1098   // strings. See DynamicSection::finalize()
1099   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1100     if (Sec != Out<ELFT>::DynStrTab && Sec != Out<ELFT>::Dynamic)
1101       Sec->finalize();
1102 
1103   if (isOutputDynamic())
1104     Out<ELFT>::Dynamic->finalize();
1105   return true;
1106 }
1107 
1108 template <class ELFT> bool Writer<ELFT>::needsGot() {
1109   if (!Out<ELFT>::Got->empty())
1110     return true;
1111 
1112   // We add the .got section to the result for dynamic MIPS target because
1113   // its address and properties are mentioned in the .dynamic section.
1114   if (Config->EMachine == EM_MIPS && isOutputDynamic())
1115     return true;
1116 
1117   // If we have a relocation that is relative to GOT (such as GOTOFFREL),
1118   // we need to emit a GOT even if it's empty.
1119   return HasGotOffRel;
1120 }
1121 
1122 // This function add Out<ELFT>::* sections to OutputSections.
1123 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
1124   auto Add = [&](OutputSectionBase<ELFT> *C) {
1125     if (C)
1126       OutputSections.push_back(C);
1127   };
1128 
1129   // This order is not the same as the final output order
1130   // because we sort the sections using their attributes below.
1131   Add(Out<ELFT>::SymTab);
1132   Add(Out<ELFT>::ShStrTab);
1133   Add(Out<ELFT>::StrTab);
1134   if (isOutputDynamic()) {
1135     Add(Out<ELFT>::DynSymTab);
1136     Add(Out<ELFT>::GnuHashTab);
1137     Add(Out<ELFT>::HashTab);
1138     Add(Out<ELFT>::Dynamic);
1139     Add(Out<ELFT>::DynStrTab);
1140     if (Out<ELFT>::RelaDyn->hasRelocs())
1141       Add(Out<ELFT>::RelaDyn);
1142     Add(Out<ELFT>::MipsRldMap);
1143   }
1144 
1145   // We always need to add rel[a].plt to output if it has entries.
1146   // Even during static linking it can contain R_[*]_IRELATIVE relocations.
1147   if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
1148     Add(Out<ELFT>::RelaPlt);
1149     Out<ELFT>::RelaPlt->Static = !isOutputDynamic();
1150   }
1151 
1152   if (needsGot())
1153     Add(Out<ELFT>::Got);
1154   if (Out<ELFT>::GotPlt && !Out<ELFT>::GotPlt->empty())
1155     Add(Out<ELFT>::GotPlt);
1156   if (!Out<ELFT>::Plt->empty())
1157     Add(Out<ELFT>::Plt);
1158   if (Out<ELFT>::EhFrameHdr->Live)
1159     Add(Out<ELFT>::EhFrameHdr);
1160 }
1161 
1162 // The linker is expected to define SECNAME_start and SECNAME_end
1163 // symbols for a few sections. This function defines them.
1164 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
1165   auto Define = [&](StringRef Start, StringRef End,
1166                     OutputSectionBase<ELFT> *OS) {
1167     if (OS) {
1168       Symtab.addSynthetic(Start, *OS, 0, STV_DEFAULT);
1169       Symtab.addSynthetic(End, *OS, OS->getSize(), STV_DEFAULT);
1170     } else {
1171       Symtab.addIgnored(Start);
1172       Symtab.addIgnored(End);
1173     }
1174   };
1175 
1176   Define("__preinit_array_start", "__preinit_array_end",
1177          Out<ELFT>::Dynamic->PreInitArraySec);
1178   Define("__init_array_start", "__init_array_end",
1179          Out<ELFT>::Dynamic->InitArraySec);
1180   Define("__fini_array_start", "__fini_array_end",
1181          Out<ELFT>::Dynamic->FiniArraySec);
1182 }
1183 
1184 // If a section name is valid as a C identifier (which is rare because of
1185 // the leading '.'), linkers are expected to define __start_<secname> and
1186 // __stop_<secname> symbols. They are at beginning and end of the section,
1187 // respectively. This is not requested by the ELF standard, but GNU ld and
1188 // gold provide the feature, and used by many programs.
1189 template <class ELFT>
1190 void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
1191   StringRef S = Sec->getName();
1192   if (!isValidCIdentifier(S))
1193     return;
1194   StringSaver Saver(Alloc);
1195   StringRef Start = Saver.save("__start_" + S);
1196   StringRef Stop = Saver.save("__stop_" + S);
1197   if (SymbolBody *B = Symtab.find(Start))
1198     if (B->isUndefined())
1199       Symtab.addSynthetic(Start, *Sec, 0, STV_DEFAULT);
1200   if (SymbolBody *B = Symtab.find(Stop))
1201     if (B->isUndefined())
1202       Symtab.addSynthetic(Stop, *Sec, Sec->getSize(), STV_DEFAULT);
1203 }
1204 
1205 template <class ELFT> static bool needsPtLoad(OutputSectionBase<ELFT> *Sec) {
1206   if (!(Sec->getFlags() & SHF_ALLOC))
1207     return false;
1208 
1209   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
1210   // responsible for allocating space for them, not the PT_LOAD that
1211   // contains the TLS initialization image.
1212   if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS)
1213     return false;
1214   return true;
1215 }
1216 
1217 static uint32_t toPhdrFlags(uint64_t Flags) {
1218   uint32_t Ret = PF_R;
1219   if (Flags & SHF_WRITE)
1220     Ret |= PF_W;
1221   if (Flags & SHF_EXECINSTR)
1222     Ret |= PF_X;
1223   return Ret;
1224 }
1225 
1226 // Decide which program headers to create and which sections to include in each
1227 // one.
1228 template <class ELFT> void Writer<ELFT>::createPhdrs() {
1229   auto AddHdr = [this](unsigned Type, unsigned Flags) {
1230     return &*Phdrs.emplace(Phdrs.end(), Type, Flags);
1231   };
1232 
1233   auto AddSec = [](Phdr &Hdr, OutputSectionBase<ELFT> *Sec) {
1234     Hdr.Last = Sec;
1235     if (!Hdr.First)
1236       Hdr.First = Sec;
1237     Hdr.H.p_align = std::max<uintX_t>(Hdr.H.p_align, Sec->getAlign());
1238   };
1239 
1240   // The first phdr entry is PT_PHDR which describes the program header itself.
1241   Phdr &Hdr = *AddHdr(PT_PHDR, PF_R);
1242   AddSec(Hdr, Out<ELFT>::ProgramHeaders);
1243 
1244   // PT_INTERP must be the second entry if exists.
1245   if (needsInterpSection()) {
1246     Phdr &Hdr = *AddHdr(PT_INTERP, toPhdrFlags(Out<ELFT>::Interp->getFlags()));
1247     AddSec(Hdr, Out<ELFT>::Interp);
1248   }
1249 
1250   // Add the first PT_LOAD segment for regular output sections.
1251   uintX_t Flags = PF_R;
1252   Phdr *Load = AddHdr(PT_LOAD, Flags);
1253   AddSec(*Load, Out<ELFT>::ElfHeader);
1254 
1255   Phdr TlsHdr(PT_TLS, PF_R);
1256   Phdr RelRo(PT_GNU_RELRO, PF_R);
1257   Phdr Note(PT_NOTE, PF_R);
1258   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1259     if (!(Sec->getFlags() & SHF_ALLOC))
1260       break;
1261 
1262     // If we meet TLS section then we create TLS header
1263     // and put all TLS sections inside for futher use when
1264     // assign addresses.
1265     if (Sec->getFlags() & SHF_TLS)
1266       AddSec(TlsHdr, Sec);
1267 
1268     if (!needsPtLoad<ELFT>(Sec))
1269       continue;
1270 
1271     // If flags changed then we want new load segment.
1272     uintX_t NewFlags = toPhdrFlags(Sec->getFlags());
1273     if (Flags != NewFlags) {
1274       Load = AddHdr(PT_LOAD, NewFlags);
1275       Flags = NewFlags;
1276     }
1277 
1278     AddSec(*Load, Sec);
1279 
1280     if (isRelroSection(Sec))
1281       AddSec(RelRo, Sec);
1282     if (Sec->getType() == SHT_NOTE)
1283       AddSec(Note, Sec);
1284   }
1285 
1286   // Add the TLS segment unless it's empty.
1287   if (TlsHdr.First)
1288     Phdrs.push_back(std::move(TlsHdr));
1289 
1290   // Add an entry for .dynamic.
1291   if (isOutputDynamic()) {
1292     Phdr &H = *AddHdr(PT_DYNAMIC, toPhdrFlags(Out<ELFT>::Dynamic->getFlags()));
1293     AddSec(H, Out<ELFT>::Dynamic);
1294   }
1295 
1296   // PT_GNU_RELRO includes all sections that should be marked as
1297   // read-only by dynamic linker after proccessing relocations.
1298   if (RelRo.First)
1299     Phdrs.push_back(std::move(RelRo));
1300 
1301   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1302   if (Out<ELFT>::EhFrameHdr->Live) {
1303     Phdr &Hdr = *AddHdr(PT_GNU_EH_FRAME,
1304                         toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags()));
1305     AddSec(Hdr, Out<ELFT>::EhFrameHdr);
1306   }
1307 
1308   // PT_GNU_STACK is a special section to tell the loader to make the
1309   // pages for the stack non-executable.
1310   if (!Config->ZExecStack)
1311     AddHdr(PT_GNU_STACK, PF_R | PF_W);
1312 
1313   if (Note.First)
1314     Phdrs.push_back(std::move(Note));
1315 }
1316 
1317 // Used for relocatable output (-r). In this case we create only ELF file
1318 // header, do not create program headers. Also assign of section addresses
1319 // is very straightforward: we just put all sections sequentually to the file.
1320 template <class ELFT> void Writer<ELFT>::assignAddressesRelocatable() {
1321   Out<ELFT>::ElfHeader->setSize(sizeof(Elf_Ehdr));
1322   uintX_t FileOff = 0;
1323   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1324     if (Sec->getType() != SHT_NOBITS)
1325       FileOff = alignTo(FileOff, Sec->getAlign());
1326     Sec->setFileOffset(FileOff);
1327     if (Sec->getType() != SHT_NOBITS)
1328       FileOff += Sec->getSize();
1329   }
1330   SectionHeaderOff = alignTo(FileOff, sizeof(uintX_t));
1331   FileSize = SectionHeaderOff + getNumSections() * sizeof(Elf_Shdr);
1332 }
1333 
1334 // Visits all headers in PhdrTable and assigns the adresses to
1335 // the output sections. Also creates common and special headers.
1336 template <class ELFT> void Writer<ELFT>::assignAddresses() {
1337   Out<ELFT>::ElfHeader->setSize(sizeof(Elf_Ehdr));
1338   size_t PhdrSize = sizeof(Elf_Phdr) * Phdrs.size();
1339   Out<ELFT>::ProgramHeaders->setSize(PhdrSize);
1340 
1341   // The first section of each PT_LOAD and the first section after PT_GNU_RELRO
1342   // have to be page aligned so that the dynamic linker can set the permissions.
1343   SmallPtrSet<OutputSectionBase<ELFT> *, 4> PageAlign;
1344   for (const Phdr &P : Phdrs) {
1345     if (P.H.p_type == PT_GNU_RELRO) {
1346       // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1347       // have to align it to a page.
1348       auto I = std::find(OutputSections.begin(), OutputSections.end(), P.Last);
1349       ++I;
1350       if (I != OutputSections.end() && needsPtLoad(*I))
1351         PageAlign.insert(*I);
1352     }
1353 
1354     if (P.H.p_type == PT_LOAD)
1355       PageAlign.insert(P.First);
1356   }
1357 
1358   uintX_t ThreadBssOffset = 0;
1359   uintX_t VA = Target->getVAStart();
1360   uintX_t FileOff = 0;
1361 
1362   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1363     uintX_t Align = Sec->getAlign();
1364     if (PageAlign.count(Sec))
1365       Align = std::max<uintX_t>(Align, Target->PageSize);
1366 
1367     if (Sec->getType() != SHT_NOBITS)
1368       FileOff = alignTo(FileOff, Align);
1369     Sec->setFileOffset(FileOff);
1370     if (Sec->getType() != SHT_NOBITS)
1371       FileOff += Sec->getSize();
1372 
1373     // We only assign VAs to allocated sections.
1374     if (needsPtLoad<ELFT>(Sec)) {
1375       VA = alignTo(VA, Align);
1376       Sec->setVA(VA);
1377       VA += Sec->getSize();
1378     } else if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) {
1379       uintX_t TVA = VA + ThreadBssOffset;
1380       TVA = alignTo(TVA, Align);
1381       Sec->setVA(TVA);
1382       ThreadBssOffset = TVA - VA + Sec->getSize();
1383     }
1384   }
1385 
1386   // Add space for section headers.
1387   SectionHeaderOff = alignTo(FileOff, sizeof(uintX_t));
1388   FileSize = SectionHeaderOff + getNumSections() * sizeof(Elf_Shdr);
1389 
1390   // Update "_end" and "end" symbols so that they
1391   // point to the end of the data segment.
1392   ElfSym<ELFT>::End.st_value = VA;
1393 
1394   for (Phdr &PHdr : Phdrs) {
1395     Elf_Phdr &H = PHdr.H;
1396     if (PHdr.First) {
1397       OutputSectionBase<ELFT> *Last = PHdr.Last;
1398       H.p_filesz = Last->getFileOff() - PHdr.First->getFileOff();
1399       if (Last->getType() != SHT_NOBITS)
1400         H.p_filesz += Last->getSize();
1401       H.p_memsz = Last->getVA() + Last->getSize() - PHdr.First->getVA();
1402       H.p_offset = PHdr.First->getFileOff();
1403       H.p_vaddr = PHdr.First->getVA();
1404     }
1405     if (H.p_type == PT_LOAD)
1406       H.p_align = Target->PageSize;
1407     else if (H.p_type == PT_GNU_RELRO)
1408       H.p_align = 1;
1409     H.p_paddr = H.p_vaddr;
1410 
1411     // The TLS pointer goes after PT_TLS. At least glibc will align it,
1412     // so round up the size to make sure the offsets are correct.
1413     if (H.p_type == PT_TLS) {
1414       Out<ELFT>::TlsPhdr = &H;
1415       H.p_memsz = alignTo(H.p_memsz, H.p_align);
1416     }
1417   }
1418 }
1419 
1420 static uint32_t getMipsEFlags() {
1421   // FIXME: In fact ELF flags depends on ELF flags of input object files
1422   // and selected emulation. For now just use hard coded values.
1423   uint32_t V = EF_MIPS_ABI_O32 | EF_MIPS_CPIC | EF_MIPS_ARCH_32R2;
1424   if (Config->Shared)
1425     V |= EF_MIPS_PIC;
1426   return V;
1427 }
1428 
1429 template <class ELFT>
1430 static typename ELFFile<ELFT>::uintX_t getEntryAddr() {
1431   if (SymbolBody *B = Config->EntrySym)
1432     return B->repl().getVA<ELFT>();
1433   if (Config->EntryAddr != uint64_t(-1))
1434     return Config->EntryAddr;
1435   return 0;
1436 }
1437 
1438 template <class ELFT> static uint8_t getELFEncoding() {
1439   if (ELFT::TargetEndianness == llvm::support::little)
1440     return ELFDATA2LSB;
1441   return ELFDATA2MSB;
1442 }
1443 
1444 static uint16_t getELFType() {
1445   if (Config->Shared)
1446     return ET_DYN;
1447   if (Config->Relocatable)
1448     return ET_REL;
1449   return ET_EXEC;
1450 }
1451 
1452 // This function is called after we have assigned address and size
1453 // to each section. This function fixes some predefined absolute
1454 // symbol values that depend on section address and size.
1455 template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() {
1456   // Update __rel[a]_iplt_{start,end} symbols so that they point
1457   // to beginning or ending of .rela.plt section, respectively.
1458   if (Out<ELFT>::RelaPlt) {
1459     uintX_t Start = Out<ELFT>::RelaPlt->getVA();
1460     ElfSym<ELFT>::RelaIpltStart.st_value = Start;
1461     ElfSym<ELFT>::RelaIpltEnd.st_value = Start + Out<ELFT>::RelaPlt->getSize();
1462   }
1463 
1464   // Update MIPS _gp absolute symbol so that it points to the static data.
1465   if (Config->EMachine == EM_MIPS)
1466     ElfSym<ELFT>::MipsGp.st_value = getMipsGpAddr<ELFT>();
1467 
1468   // _etext is the first location after the last read-only loadable segment.
1469   // _edata is the first location after the last read-write loadable segment.
1470   for (Phdr &PHdr : Phdrs) {
1471     if (PHdr.H.p_type != PT_LOAD)
1472       continue;
1473     uintX_t Val = PHdr.H.p_vaddr + PHdr.H.p_filesz;
1474     if (PHdr.H.p_flags & PF_W)
1475       ElfSym<ELFT>::Edata.st_value = Val;
1476     else
1477       ElfSym<ELFT>::Etext.st_value = Val;
1478   }
1479 }
1480 
1481 template <class ELFT> void Writer<ELFT>::writeHeader() {
1482   uint8_t *Buf = Buffer->getBufferStart();
1483   memcpy(Buf, "\177ELF", 4);
1484 
1485   auto &FirstObj = cast<ELFFileBase<ELFT>>(*Config->FirstElf);
1486 
1487   // Write the ELF header.
1488   auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1489   EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1490   EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>();
1491   EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1492   EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();
1493   EHdr->e_type = getELFType();
1494   EHdr->e_machine = FirstObj.getEMachine();
1495   EHdr->e_version = EV_CURRENT;
1496   EHdr->e_entry = getEntryAddr<ELFT>();
1497   EHdr->e_shoff = SectionHeaderOff;
1498   EHdr->e_ehsize = sizeof(Elf_Ehdr);
1499   EHdr->e_phnum = Phdrs.size();
1500   EHdr->e_shentsize = sizeof(Elf_Shdr);
1501   EHdr->e_shnum = getNumSections();
1502   EHdr->e_shstrndx = Out<ELFT>::ShStrTab->SectionIndex;
1503 
1504   if (Config->EMachine == EM_MIPS)
1505     EHdr->e_flags = getMipsEFlags();
1506 
1507   if (!Config->Relocatable) {
1508     EHdr->e_phoff = sizeof(Elf_Ehdr);
1509     EHdr->e_phentsize = sizeof(Elf_Phdr);
1510   }
1511 
1512   // Write the program header table.
1513   auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1514   for (Phdr &P : Phdrs)
1515     *HBuf++ = P.H;
1516 
1517   // Write the section header table. Note that the first table entry is null.
1518   auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1519   for (OutputSectionBase<ELFT> *Sec : getSections())
1520     Sec->writeHeaderTo(++SHdrs);
1521 }
1522 
1523 template <class ELFT> bool Writer<ELFT>::openFile() {
1524   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1525       FileOutputBuffer::create(Config->OutputFile, FileSize,
1526                                FileOutputBuffer::F_executable);
1527   if (!BufferOrErr) {
1528     error(BufferOrErr, "failed to open " + Config->OutputFile);
1529     return false;
1530   }
1531   Buffer = std::move(*BufferOrErr);
1532   return true;
1533 }
1534 
1535 // Write section contents to a mmap'ed file.
1536 template <class ELFT> void Writer<ELFT>::writeSections() {
1537   uint8_t *Buf = Buffer->getBufferStart();
1538 
1539   // PPC64 needs to process relocations in the .opd section before processing
1540   // relocations in code-containing sections.
1541   if (OutputSectionBase<ELFT> *Sec = Out<ELFT>::Opd) {
1542     Out<ELFT>::OpdBuf = Buf + Sec->getFileOff();
1543     Sec->writeTo(Buf + Sec->getFileOff());
1544   }
1545 
1546   for (OutputSectionBase<ELFT> *Sec : OutputSections)
1547     if (Sec != Out<ELFT>::Opd)
1548       Sec->writeTo(Buf + Sec->getFileOff());
1549 }
1550 
1551 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1552   BuildIdSection<ELFT> *S = Out<ELFT>::BuildId;
1553   if (!S)
1554     return;
1555 
1556   // Compute a hash of all sections except .debug_* sections.
1557   // We skip debug sections because they tend to be very large
1558   // and their contents are very likely to be the same as long as
1559   // other sections are the same.
1560   uint8_t *Start = Buffer->getBufferStart();
1561   uint8_t *Last = Start;
1562   for (OutputSectionBase<ELFT> *Sec : OutputSections) {
1563     uint8_t *End = Start + Sec->getFileOff();
1564     if (!Sec->getName().startswith(".debug_"))
1565       S->update({Last, End});
1566     Last = End;
1567   }
1568   S->update({Last, Start + FileSize});
1569 
1570   // Fill the hash value field in the .note.gnu.build-id section.
1571   S->writeBuildId();
1572 }
1573 
1574 template void elf::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab);
1575 template void elf::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab);
1576 template void elf::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab);
1577 template void elf::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab);
1578